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
63 #include <sys/cdefs.h>
65 #include "opt_inet6.h"
66 #include "opt_ipsec.h"
67 #include "opt_kern_tls.h"
68 #include "opt_ratelimit.h"
69 #include "opt_route.h"
73 #include <sys/param.h>
74 #include <sys/kernel.h>
76 #include <sys/malloc.h>
78 #include <sys/errno.h>
81 #include <sys/protosw.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/syslog.h>
85 #include <sys/ucred.h>
87 #include <machine/in_cksum.h>
90 #include <net/if_var.h>
91 #include <net/if_private.h>
92 #include <net/if_vlan_var.h>
93 #include <net/if_llatbl.h>
94 #include <net/ethernet.h>
95 #include <net/netisr.h>
96 #include <net/route.h>
97 #include <net/route/nhop.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>
116 #if defined(SCTP) || defined(SCTP_SUPPORT)
117 #include <netinet/sctp.h>
118 #include <netinet/sctp_crc32.h>
121 #include <netinet6/scope6_var.h>
123 extern int in6_mcast_loop;
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;
133 static MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
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 inpcb *, int, struct sockopt *);
140 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
141 struct ucred *, int, int, int);
143 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
144 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
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,
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);
157 * Make an extension header from option data. hp is the source,
158 * mp is the destination, and _ol is the optlen.
160 #define MAKE_EXTHDR(hp, mp, _ol) \
163 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
164 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
165 ((eh)->ip6e_len + 1) << 3); \
168 (_ol) += (*(mp))->m_len; \
170 } while (/*CONSTCOND*/ 0)
173 * Form a chain of extension headers.
174 * m is the extension header mbuf
175 * mp is the previous mbuf in the chain
176 * p is the next header
177 * i is the type of option.
179 #define MAKE_CHAIN(m, mp, p, i)\
183 panic("%s:%d: assumption failed: "\
184 "hdr not split: hdrsplit %d exthdrs %p",\
185 __func__, __LINE__, hdrsplit, &exthdrs);\
186 *mtod((m), u_char *) = *(p);\
188 p = mtod((m), u_char *);\
189 (m)->m_next = (mp)->m_next;\
193 } while (/*CONSTCOND*/ 0)
196 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
200 csum = in_cksum_skip(m, offset + plen, offset);
201 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
203 offset += m->m_pkthdr.csum_data; /* checksum offset */
205 if (offset + sizeof(csum) > m->m_len)
206 m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
208 *(u_short *)mtodo(m, offset) = csum;
212 ip6_output_delayed_csum(struct mbuf *m, struct ifnet *ifp, int csum_flags,
213 int plen, int optlen)
216 KASSERT((plen >= optlen), ("%s:%d: plen %d < optlen %d, m %p, ifp %p "
218 __func__, __LINE__, plen, optlen, m, ifp, csum_flags));
220 if (csum_flags & CSUM_DELAY_DATA_IPV6) {
221 in6_delayed_cksum(m, plen - optlen,
222 sizeof(struct ip6_hdr) + optlen);
223 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
225 #if defined(SCTP) || defined(SCTP_SUPPORT)
226 if (csum_flags & CSUM_SCTP_IPV6) {
227 sctp_delayed_cksum(m, sizeof(struct ip6_hdr) + optlen);
228 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
234 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
235 int fraglen , uint32_t id)
237 struct mbuf *m, **mnext, *m_frgpart;
238 struct ip6_hdr *ip6, *mhip6;
239 struct ip6_frag *ip6f;
242 int tlen = m0->m_pkthdr.len;
244 KASSERT((fraglen % 8 == 0), ("Fragment length must be a multiple of 8"));
247 ip6 = mtod(m, struct ip6_hdr *);
248 mnext = &m->m_nextpkt;
250 for (off = hlen; off < tlen; off += fraglen) {
251 m = m_gethdr(M_NOWAIT, MT_DATA);
253 IP6STAT_INC(ip6s_odropped);
258 * Make sure the complete packet header gets copied
259 * from the originating mbuf to the newly created
260 * mbuf. This also ensures that existing firewall
261 * classification(s), VLAN tags and so on get copied
262 * to the resulting fragmented packet(s):
264 if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
266 IP6STAT_INC(ip6s_odropped);
271 mnext = &m->m_nextpkt;
272 m->m_data += max_linkhdr;
273 mhip6 = mtod(m, struct ip6_hdr *);
275 m->m_len = sizeof(*mhip6);
276 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
278 IP6STAT_INC(ip6s_odropped);
281 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
282 if (off + fraglen >= tlen)
283 fraglen = tlen - off;
285 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
286 mhip6->ip6_plen = htons((u_short)(fraglen + hlen +
287 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
288 if ((m_frgpart = m_copym(m0, off, fraglen, M_NOWAIT)) == NULL) {
289 IP6STAT_INC(ip6s_odropped);
293 m->m_pkthdr.len = fraglen + hlen + sizeof(*ip6f);
294 ip6f->ip6f_reserved = 0;
295 ip6f->ip6f_ident = id;
296 ip6f->ip6f_nxt = nextproto;
297 IP6STAT_INC(ip6s_ofragments);
298 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
305 ip6_output_send(struct inpcb *inp, struct ifnet *ifp, struct ifnet *origifp,
306 struct mbuf *m, struct sockaddr_in6 *dst, struct route_in6 *ro,
310 struct ktls_session *tls = NULL;
312 struct m_snd_tag *mst;
315 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
320 * If this is an unencrypted TLS record, save a reference to
321 * the record. This local reference is used to call
322 * ktls_output_eagain after the mbuf has been freed (thus
323 * dropping the mbuf's reference) in if_output.
325 if (m->m_next != NULL && mbuf_has_tls_session(m->m_next)) {
326 tls = ktls_hold(m->m_next->m_epg_tls);
330 * If a TLS session doesn't have a valid tag, it must
331 * have had an earlier ifp mismatch, so drop this
340 * Always stamp tags that include NIC ktls.
346 if (inp != NULL && mst == NULL) {
347 if ((inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) != 0 ||
348 (inp->inp_snd_tag != NULL &&
349 inp->inp_snd_tag->ifp != ifp))
350 in_pcboutput_txrtlmt(inp, ifp, m);
352 if (inp->inp_snd_tag != NULL)
353 mst = inp->inp_snd_tag;
356 if (stamp_tag && mst != NULL) {
357 KASSERT(m->m_pkthdr.rcvif == NULL,
358 ("trying to add a send tag to a forwarded packet"));
359 if (mst->ifp != ifp) {
365 /* stamp send tag on mbuf */
366 m->m_pkthdr.snd_tag = m_snd_tag_ref(mst);
367 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
370 error = nd6_output_ifp(ifp, origifp, m, dst, (struct route *)ro);
373 /* Check for route change invalidating send tags. */
377 error = ktls_output_eagain(inp, tls);
383 in_pcboutput_eagain(inp);
390 * The packet in mbuf chain m contains a skeletal IP6 header (with pri, len,
391 * nxt, hlim, src, dst).
392 * This function may modify ver and hlim only.
393 * The mbuf chain containing the packet will be freed.
394 * The mbuf opt, if present, will not be freed.
395 * If route_in6 ro is present and has ro_nh initialized, route lookup would be
396 * skipped and ro->ro_nh would be used. If ro is present but ro->ro_nh is NULL,
397 * then result of route lookup is stored in ro->ro_nh.
399 * Type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and nd_ifinfo.linkmtu
400 * is uint32_t. So we use u_long to hold largest one, which is rt_mtu.
402 * ifpp - XXX: just for statistics
405 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
406 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
407 struct ifnet **ifpp, struct inpcb *inp)
410 struct ifnet *ifp, *origifp;
413 struct route_in6 *ro_pmtu;
414 struct nhop_object *nh;
415 struct sockaddr_in6 *dst, sin6, src_sa, dst_sa;
416 struct in6_addr odst;
421 struct in6_ifaddr *ia = NULL;
423 int alwaysfrag, dontfrag;
424 u_int32_t optlen, plen = 0, unfragpartlen;
425 struct ip6_exthdrs exthdrs;
426 struct in6_addr src0, dst0;
432 struct m_tag *fwd_tag = NULL;
438 INP_LOCK_ASSERT(inp);
439 M_SETFIB(m, inp->inp_inc.inc_fibnum);
440 if ((flags & IP_NODEFAULTFLOWID) == 0) {
441 /* Unconditionally set flowid. */
442 m->m_pkthdr.flowid = inp->inp_flowid;
443 M_HASHTYPE_SET(m, inp->inp_flowtype);
445 if ((inp->inp_flags2 & INP_2PCP_SET) != 0)
446 vlan_pcp = (inp->inp_flags2 & INP_2PCP_MASK) >>
449 m->m_pkthdr.numa_domain = inp->inp_numa_domain;
453 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
455 * IPSec checking which handles several cases.
456 * FAST IPSEC: We re-injected the packet.
457 * XXX: need scope argument.
459 if (IPSEC_ENABLED(ipv6)) {
460 m = mb_unmapped_to_ext(m);
462 IP6STAT_INC(ip6s_odropped);
466 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
467 if (error == EINPROGRESS)
474 /* Source address validation. */
475 ip6 = mtod(m, struct ip6_hdr *);
476 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
477 (flags & IPV6_UNSPECSRC) == 0) {
479 IP6STAT_INC(ip6s_badscope);
482 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
484 IP6STAT_INC(ip6s_badscope);
489 * If we are given packet options to add extension headers prepare them.
490 * Calculate the total length of the extension header chain.
491 * Keep the length of the unfragmentable part for fragmentation.
493 bzero(&exthdrs, sizeof(exthdrs));
495 unfragpartlen = sizeof(struct ip6_hdr);
497 /* Hop-by-Hop options header. */
498 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh, optlen);
500 /* Destination options header (1st part). */
501 if (opt->ip6po_rthdr) {
502 #ifndef RTHDR_SUPPORT_IMPLEMENTED
504 * If there is a routing header, discard the packet
505 * right away here. RH0/1 are obsolete and we do not
506 * currently support RH2/3/4.
507 * People trying to use RH253/254 may want to disable
509 * The moment we do support any routing header (again)
510 * this block should check the routing type more
518 * Destination options header (1st part).
519 * This only makes sense with a routing header.
520 * See Section 9.2 of RFC 3542.
521 * Disabling this part just for MIP6 convenience is
522 * a bad idea. We need to think carefully about a
523 * way to make the advanced API coexist with MIP6
524 * options, which might automatically be inserted in
527 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1,
530 /* Routing header. */
531 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr, optlen);
533 unfragpartlen += optlen;
536 * NOTE: we don't add AH/ESP length here (done in
537 * ip6_ipsec_output()).
540 /* Destination options header (2nd part). */
541 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2, optlen);
545 * If there is at least one extension header,
546 * separate IP6 header from the payload.
550 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
554 m = exthdrs.ip6e_ip6;
555 ip6 = mtod(m, struct ip6_hdr *);
559 /* Adjust mbuf packet header length. */
560 m->m_pkthdr.len += optlen;
561 plen = m->m_pkthdr.len - sizeof(*ip6);
563 /* If this is a jumbo payload, insert a jumbo payload option. */
564 if (plen > IPV6_MAXPACKET) {
566 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
570 m = exthdrs.ip6e_ip6;
571 ip6 = mtod(m, struct ip6_hdr *);
574 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
578 ip6->ip6_plen = htons(plen);
579 nexthdrp = &ip6->ip6_nxt;
583 * Concatenate headers and fill in next header fields.
584 * Here we have, on "m"
586 * and we insert headers accordingly.
587 * Finally, we should be getting:
588 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload].
590 * During the header composing process "m" points to IPv6
591 * header. "mprev" points to an extension header prior to esp.
596 * We treat dest2 specially. This makes IPsec processing
597 * much easier. The goal here is to make mprev point the
598 * mbuf prior to dest2.
600 * Result: IPv6 dest2 payload.
601 * m and mprev will point to IPv6 header.
603 if (exthdrs.ip6e_dest2) {
605 panic("%s:%d: assumption failed: "
606 "hdr not split: hdrsplit %d exthdrs %p",
607 __func__, __LINE__, hdrsplit, &exthdrs);
608 exthdrs.ip6e_dest2->m_next = m->m_next;
609 m->m_next = exthdrs.ip6e_dest2;
610 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
611 ip6->ip6_nxt = IPPROTO_DSTOPTS;
615 * Result: IPv6 hbh dest1 rthdr dest2 payload.
616 * m will point to IPv6 header. mprev will point to the
617 * extension header prior to dest2 (rthdr in the above case).
619 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
620 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
622 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
626 IP6STAT_INC(ip6s_localout);
630 if (opt && opt->ip6po_rthdr)
631 ro = &opt->ip6po_route;
633 dst = (struct sockaddr_in6 *)&ro->ro_dst;
636 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
640 * If specified, try to fill in the traffic class field.
641 * Do not override if a non-zero value is already set.
642 * We check the diffserv field and the ECN field separately.
644 if (opt && opt->ip6po_tclass >= 0) {
647 if (IPV6_DSCP(ip6) == 0)
649 if (IPV6_ECN(ip6) == 0)
652 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
655 /* Fill in or override the hop limit field, if necessary. */
656 if (opt && opt->ip6po_hlim != -1)
657 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
658 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
660 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
662 ip6->ip6_hlim = V_ip6_defmcasthlim;
665 if (ro == NULL || ro->ro_nh == NULL) {
666 bzero(dst, sizeof(*dst));
667 dst->sin6_family = AF_INET6;
668 dst->sin6_len = sizeof(*dst);
669 dst->sin6_addr = ip6->ip6_dst;
672 * Validate route against routing table changes.
673 * Make sure that the address family is set in route.
679 if (ro->ro_nh != NULL && inp != NULL) {
680 ro->ro_dst.sin6_family = AF_INET6; /* XXX KASSERT? */
681 NH_VALIDATE((struct route *)ro, &inp->inp_rt_cookie,
684 if (ro->ro_nh != NULL && fwd_tag == NULL &&
685 (!NH_IS_VALID(ro->ro_nh) ||
686 ro->ro_dst.sin6_family != AF_INET6 ||
687 !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)))
688 RO_INVALIDATE_CACHE(ro);
690 if (ro->ro_nh != NULL && fwd_tag == NULL &&
691 ro->ro_dst.sin6_family == AF_INET6 &&
692 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
693 /* Nexthop is valid and contains valid ifp */
697 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
699 if (fwd_tag == NULL) {
700 bzero(&dst_sa, sizeof(dst_sa));
701 dst_sa.sin6_family = AF_INET6;
702 dst_sa.sin6_len = sizeof(dst_sa);
703 dst_sa.sin6_addr = ip6->ip6_dst;
705 error = in6_selectroute(&dst_sa, opt, im6o, ro, &ifp,
706 &nh, fibnum, m->m_pkthdr.flowid);
708 IP6STAT_INC(ip6s_noroute);
710 in6_ifstat_inc(ifp, ifs6_out_discard);
714 * At this point at least @ifp is not NULL
715 * Can be the case when dst is multicast, link-local or
716 * interface is explicitly specificed by the caller.
721 * If in6_selectroute() does not return a nexthop
722 * dst may not have been updated.
724 *dst = dst_sa; /* XXX */
729 origifp = nh->nh_aifp;
730 ia = (struct in6_ifaddr *)(nh->nh_ifa);
731 counter_u64_add(nh->nh_pksent, 1);
734 struct nhop_object *nh;
735 struct in6_addr kdst;
738 if (fwd_tag == NULL) {
739 bzero(&dst_sa, sizeof(dst_sa));
740 dst_sa.sin6_family = AF_INET6;
741 dst_sa.sin6_len = sizeof(dst_sa);
742 dst_sa.sin6_addr = ip6->ip6_dst;
745 if (IN6_IS_ADDR_MULTICAST(&dst_sa.sin6_addr) &&
747 (ifp = im6o->im6o_multicast_ifp) != NULL) {
748 /* We do not need a route lookup. */
749 *dst = dst_sa; /* XXX */
754 in6_splitscope(&dst_sa.sin6_addr, &kdst, &scopeid);
756 if (IN6_IS_ADDR_MC_LINKLOCAL(&dst_sa.sin6_addr) ||
757 IN6_IS_ADDR_MC_NODELOCAL(&dst_sa.sin6_addr)) {
759 ifp = in6_getlinkifnet(scopeid);
761 error = EHOSTUNREACH;
764 *dst = dst_sa; /* XXX */
770 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE,
773 IP6STAT_INC(ip6s_noroute);
774 /* No ifp in6_ifstat_inc(ifp, ifs6_out_discard); */
775 error = EHOSTUNREACH;
780 origifp = nh->nh_aifp;
781 ia = ifatoia6(nh->nh_ifa);
782 if (nh->nh_flags & NHF_GATEWAY)
783 dst->sin6_addr = nh->gw6_sa.sin6_addr;
784 else if (fwd_tag != NULL)
785 dst->sin6_addr = dst_sa.sin6_addr;
790 * At this point ifp MUST be pointing to the valid transmit ifp.
791 * origifp MUST be valid and pointing to either the same ifp or,
792 * in case of loopback output, to the interface which ip6_src
795 * fe80::1%em0 -> fe80::2%em0 -> ifp=em0, origifp=em0
796 * fe80::1%em0 -> fe80::1%em0 -> ifp=lo0, origifp=em0
797 * ::1 -> ::1 -> ifp=lo0, origifp=lo0
799 * mtu can be 0 and will be refined later.
801 KASSERT((ifp != NULL), ("output interface must not be NULL"));
802 KASSERT((origifp != NULL), ("output address interface must not be NULL"));
804 if ((flags & IPV6_FORWARDING) == 0) {
805 /* XXX: the FORWARDING flag can be set for mrouting. */
806 in6_ifstat_inc(ifp, ifs6_out_request);
809 /* Setup data structures for scope ID checks. */
811 bzero(&src_sa, sizeof(src_sa));
812 src_sa.sin6_family = AF_INET6;
813 src_sa.sin6_len = sizeof(src_sa);
814 src_sa.sin6_addr = ip6->ip6_src;
817 /* Re-initialize to be sure. */
818 bzero(&dst_sa, sizeof(dst_sa));
819 dst_sa.sin6_family = AF_INET6;
820 dst_sa.sin6_len = sizeof(dst_sa);
821 dst_sa.sin6_addr = ip6->ip6_dst;
823 /* Check for valid scope ID. */
824 if (in6_setscope(&src0, origifp, &zone) == 0 &&
825 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
826 in6_setscope(&dst0, origifp, &zone) == 0 &&
827 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
829 * The outgoing interface is in the zone of the source
830 * and destination addresses.
833 } else if ((origifp->if_flags & IFF_LOOPBACK) == 0 ||
834 sa6_recoverscope(&src_sa) != 0 ||
835 sa6_recoverscope(&dst_sa) != 0 ||
836 dst_sa.sin6_scope_id == 0 ||
837 (src_sa.sin6_scope_id != 0 &&
838 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
839 ifnet_byindex(dst_sa.sin6_scope_id) == NULL) {
841 * If the destination network interface is not a
842 * loopback interface, or the destination network
843 * address has no scope ID, or the source address has
844 * a scope ID set which is different from the
845 * destination address one, or there is no network
846 * interface representing this scope ID, the address
847 * pair is considered invalid.
849 IP6STAT_INC(ip6s_badscope);
850 in6_ifstat_inc(origifp, ifs6_out_discard);
852 error = EHOSTUNREACH; /* XXX */
855 /* All scope ID checks are successful. */
857 if (nh && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
858 if (opt && opt->ip6po_nextroute.ro_nh) {
860 * The nexthop is explicitly specified by the
861 * application. We assume the next hop is an IPv6
864 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
866 else if ((nh->nh_flags & NHF_GATEWAY))
870 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
871 m->m_flags &= ~(M_BCAST | M_MCAST); /* Just in case. */
873 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
874 in6_ifstat_inc(ifp, ifs6_out_mcast);
876 /* Confirm that the outgoing interface supports multicast. */
877 if (!(ifp->if_flags & IFF_MULTICAST)) {
878 IP6STAT_INC(ip6s_noroute);
879 in6_ifstat_inc(ifp, ifs6_out_discard);
883 if ((im6o == NULL && in6_mcast_loop) ||
884 (im6o && im6o->im6o_multicast_loop)) {
886 * Loop back multicast datagram if not expressly
887 * forbidden to do so, even if we have not joined
888 * the address; protocols will filter it later,
889 * thus deferring a hash lookup and lock acquisition
890 * at the expense of an m_copym().
892 ip6_mloopback(ifp, m);
895 * If we are acting as a multicast router, perform
896 * multicast forwarding as if the packet had just
897 * arrived on the interface to which we are about
898 * to send. The multicast forwarding function
899 * recursively calls this function, using the
900 * IPV6_FORWARDING flag to prevent infinite recursion.
902 * Multicasts that are looped back by ip6_mloopback(),
903 * above, will be forwarded by the ip6_input() routine,
906 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
908 * XXX: ip6_mforward expects that rcvif is NULL
909 * when it is called from the originating path.
910 * However, it may not always be the case.
912 m->m_pkthdr.rcvif = NULL;
913 if (ip6_mforward(ip6, ifp, m) != 0) {
920 * Multicasts with a hoplimit of zero may be looped back,
921 * above, but must not be transmitted on a network.
922 * Also, multicasts addressed to the loopback interface
923 * are not sent -- the above call to ip6_mloopback() will
924 * loop back a copy if this host actually belongs to the
925 * destination group on the loopback interface.
927 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
928 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
935 * Fill the outgoing inteface to tell the upper layer
936 * to increment per-interface statistics.
941 /* Determine path MTU. */
942 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
943 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
945 KASSERT(mtu > 0, ("%s:%d: mtu %ld, ro_pmtu %p ro %p ifp %p "
946 "alwaysfrag %d fibnum %u\n", __func__, __LINE__, mtu, ro_pmtu, ro,
947 ifp, alwaysfrag, fibnum));
950 * The caller of this function may specify to use the minimum MTU
952 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
953 * setting. The logic is a bit complicated; by default, unicast
954 * packets will follow path MTU while multicast packets will be sent at
955 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
956 * including unicast ones will be sent at the minimum MTU. Multicast
957 * packets will always be sent at the minimum MTU unless
958 * IP6PO_MINMTU_DISABLE is explicitly specified.
959 * See RFC 3542 for more details.
961 if (mtu > IPV6_MMTU) {
962 if ((flags & IPV6_MINMTU))
964 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
966 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
968 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
974 * Clear embedded scope identifiers if necessary.
975 * in6_clearscope() will touch the addresses only when necessary.
977 in6_clearscope(&ip6->ip6_src);
978 in6_clearscope(&ip6->ip6_dst);
981 * If the outgoing packet contains a hop-by-hop options header,
982 * it must be examined and processed even by the source node.
983 * (RFC 2460, section 4.)
985 if (exthdrs.ip6e_hbh) {
986 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
987 u_int32_t dummy; /* XXX unused */
988 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
991 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
992 panic("ip6e_hbh is not contiguous");
995 * XXX: if we have to send an ICMPv6 error to the sender,
996 * we need the M_LOOP flag since icmp6_error() expects
997 * the IPv6 and the hop-by-hop options header are
998 * contiguous unless the flag is set.
1000 m->m_flags |= M_LOOP;
1001 m->m_pkthdr.rcvif = ifp;
1002 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
1003 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
1004 &dummy, &plen) < 0) {
1005 /* m was already freed at this point. */
1006 error = EINVAL;/* better error? */
1009 m->m_flags &= ~M_LOOP; /* XXX */
1010 m->m_pkthdr.rcvif = NULL;
1013 /* Jump over all PFIL processing if hooks are not active. */
1014 if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
1017 odst = ip6->ip6_dst;
1018 /* Run through list of hooks for output packets. */
1019 switch (pfil_mbuf_out(V_inet6_pfil_head, &m, ifp, inp)) {
1021 ip6 = mtod(m, struct ip6_hdr *);
1031 /* See if destination IP address was changed by packet filter. */
1032 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
1033 m->m_flags |= M_SKIP_FIREWALL;
1034 /* If destination is now ourself drop to ip6_input(). */
1035 if (in6_localip(&ip6->ip6_dst)) {
1036 m->m_flags |= M_FASTFWD_OURS;
1037 if (m->m_pkthdr.rcvif == NULL)
1038 m->m_pkthdr.rcvif = V_loif;
1039 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1040 m->m_pkthdr.csum_flags |=
1041 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1042 m->m_pkthdr.csum_data = 0xffff;
1044 #if defined(SCTP) || defined(SCTP_SUPPORT)
1045 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1046 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1048 error = netisr_queue(NETISR_IPV6, m);
1052 RO_INVALIDATE_CACHE(ro);
1053 needfiblookup = 1; /* Redo the routing table lookup. */
1056 /* See if fib was changed by packet filter. */
1057 if (fibnum != M_GETFIB(m)) {
1058 m->m_flags |= M_SKIP_FIREWALL;
1059 fibnum = M_GETFIB(m);
1061 RO_INVALIDATE_CACHE(ro);
1067 /* See if local, if yes, send it to netisr. */
1068 if (m->m_flags & M_FASTFWD_OURS) {
1069 if (m->m_pkthdr.rcvif == NULL)
1070 m->m_pkthdr.rcvif = V_loif;
1071 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1072 m->m_pkthdr.csum_flags |=
1073 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1074 m->m_pkthdr.csum_data = 0xffff;
1076 #if defined(SCTP) || defined(SCTP_SUPPORT)
1077 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1078 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1080 error = netisr_queue(NETISR_IPV6, m);
1083 /* Or forward to some other address? */
1084 if ((m->m_flags & M_IP6_NEXTHOP) &&
1085 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
1087 dst = (struct sockaddr_in6 *)&ro->ro_dst;
1090 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
1091 m->m_flags |= M_SKIP_FIREWALL;
1092 m->m_flags &= ~M_IP6_NEXTHOP;
1093 m_tag_delete(m, fwd_tag);
1099 EVL_APPLY_PRI(m, vlan_pcp);
1101 /* Ensure the packet data is mapped if the interface requires it. */
1102 if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) {
1103 m = mb_unmapped_to_ext(m);
1105 IP6STAT_INC(ip6s_odropped);
1111 * Send the packet to the outgoing interface.
1112 * If necessary, do IPv6 fragmentation before sending.
1114 * The logic here is rather complex:
1115 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
1116 * 1-a: send as is if tlen <= path mtu
1117 * 1-b: fragment if tlen > path mtu
1119 * 2: if user asks us not to fragment (dontfrag == 1)
1120 * 2-a: send as is if tlen <= interface mtu
1121 * 2-b: error if tlen > interface mtu
1123 * 3: if we always need to attach fragment header (alwaysfrag == 1)
1126 * 4: if dontfrag == 1 && alwaysfrag == 1
1127 * error, as we cannot handle this conflicting request.
1129 sw_csum = m->m_pkthdr.csum_flags;
1131 tso = ((sw_csum & ifp->if_hwassist &
1132 (CSUM_TSO | CSUM_INNER_TSO)) != 0) ? 1 : 0;
1133 sw_csum &= ~ifp->if_hwassist;
1137 * If we added extension headers, we will not do TSO and calculate the
1138 * checksums ourselves for now.
1139 * XXX-BZ Need a framework to know when the NIC can handle it, even
1142 ip6_output_delayed_csum(m, ifp, sw_csum, plen, optlen);
1143 /* XXX-BZ m->m_pkthdr.csum_flags &= ~ifp->if_hwassist; */
1144 tlen = m->m_pkthdr.len;
1146 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
1150 if (dontfrag && alwaysfrag) { /* Case 4. */
1151 /* Conflicting request - can't transmit. */
1155 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* Case 2-b. */
1157 * Even if the DONTFRAG option is specified, we cannot send the
1158 * packet when the data length is larger than the MTU of the
1159 * outgoing interface.
1160 * Notify the error by sending IPV6_PATHMTU ancillary data if
1161 * application wanted to know the MTU value. Also return an
1162 * error code (this is not described in the API spec).
1165 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
1170 /* Transmit packet without fragmentation. */
1171 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* Cases 1-a and 2-a. */
1172 struct in6_ifaddr *ia6;
1174 ip6 = mtod(m, struct ip6_hdr *);
1175 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1177 /* Record statistics for this interface address. */
1178 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
1179 counter_u64_add(ia6->ia_ifa.ifa_obytes,
1182 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1183 (flags & IP_NO_SND_TAG_RL) ? false : true);
1187 /* Try to fragment the packet. Cases 1-b and 3. */
1188 if (mtu < IPV6_MMTU) {
1189 /* Path MTU cannot be less than IPV6_MMTU. */
1191 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1193 } else if (ip6->ip6_plen == 0) {
1194 /* Jumbo payload cannot be fragmented. */
1196 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1202 * Too large for the destination or interface;
1203 * fragment if possible.
1204 * Must be able to put at least 8 bytes per fragment.
1206 if (mtu > IPV6_MAXPACKET)
1207 mtu = IPV6_MAXPACKET;
1209 len = (mtu - unfragpartlen - sizeof(struct ip6_frag)) & ~7;
1212 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1217 * If the interface will not calculate checksums on
1218 * fragmented packets, then do it here.
1219 * XXX-BZ handle the hw offloading case. Need flags.
1221 ip6_output_delayed_csum(m, ifp, m->m_pkthdr.csum_flags, plen,
1225 * Change the next header field of the last header in the
1226 * unfragmentable part.
1228 if (exthdrs.ip6e_rthdr) {
1229 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1230 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1231 } else if (exthdrs.ip6e_dest1) {
1232 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1233 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1234 } else if (exthdrs.ip6e_hbh) {
1235 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1236 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1238 ip6 = mtod(m, struct ip6_hdr *);
1239 nextproto = ip6->ip6_nxt;
1240 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1244 * Loop through length of segment after first fragment,
1245 * make new header and copy data of each part and link onto
1249 id = htonl(ip6_randomid());
1250 error = ip6_fragment(ifp, m, unfragpartlen, nextproto,len, id);
1254 in6_ifstat_inc(ifp, ifs6_out_fragok);
1257 /* Remove leading garbage. */
1266 /* Record statistics for this interface address. */
1268 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1269 counter_u64_add(ia->ia_ifa.ifa_obytes,
1273 EVL_APPLY_PRI(m, vlan_pcp);
1274 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1281 IP6STAT_INC(ip6s_fragmented);
1287 m_freem(exthdrs.ip6e_hbh); /* m_freem() checks if mbuf is NULL. */
1288 m_freem(exthdrs.ip6e_dest1);
1289 m_freem(exthdrs.ip6e_rthdr);
1290 m_freem(exthdrs.ip6e_dest2);
1299 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1303 if (hlen > MCLBYTES)
1304 return (ENOBUFS); /* XXX */
1307 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1309 m = m_get(M_NOWAIT, MT_DATA);
1314 bcopy(hdr, mtod(m, caddr_t), hlen);
1321 * Insert jumbo payload option.
1324 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1330 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1333 * If there is no hop-by-hop options header, allocate new one.
1334 * If there is one but it doesn't have enough space to store the
1335 * jumbo payload option, allocate a cluster to store the whole options.
1336 * Otherwise, use it to store the options.
1338 if (exthdrs->ip6e_hbh == NULL) {
1339 mopt = m_get(M_NOWAIT, MT_DATA);
1342 mopt->m_len = JUMBOOPTLEN;
1343 optbuf = mtod(mopt, u_char *);
1344 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1345 exthdrs->ip6e_hbh = mopt;
1347 struct ip6_hbh *hbh;
1349 mopt = exthdrs->ip6e_hbh;
1350 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1353 * - exthdrs->ip6e_hbh is not referenced from places
1354 * other than exthdrs.
1355 * - exthdrs->ip6e_hbh is not an mbuf chain.
1357 int oldoptlen = mopt->m_len;
1361 * XXX: give up if the whole (new) hbh header does
1362 * not fit even in an mbuf cluster.
1364 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1368 * As a consequence, we must always prepare a cluster
1371 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1374 n->m_len = oldoptlen + JUMBOOPTLEN;
1375 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1377 optbuf = mtod(n, caddr_t) + oldoptlen;
1379 mopt = exthdrs->ip6e_hbh = n;
1381 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1382 mopt->m_len += JUMBOOPTLEN;
1384 optbuf[0] = IP6OPT_PADN;
1388 * Adjust the header length according to the pad and
1389 * the jumbo payload option.
1391 hbh = mtod(mopt, struct ip6_hbh *);
1392 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1395 /* fill in the option. */
1396 optbuf[2] = IP6OPT_JUMBO;
1398 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1399 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1401 /* finally, adjust the packet header length */
1402 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1409 * Insert fragment header and copy unfragmentable header portions.
1412 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1413 struct ip6_frag **frghdrp)
1415 struct mbuf *n, *mlast;
1417 if (hlen > sizeof(struct ip6_hdr)) {
1418 n = m_copym(m0, sizeof(struct ip6_hdr),
1419 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1426 /* Search for the last mbuf of unfragmentable part. */
1427 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1430 if (M_WRITABLE(mlast) &&
1431 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1432 /* use the trailing space of the last mbuf for the fragment hdr */
1433 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1435 mlast->m_len += sizeof(struct ip6_frag);
1436 m->m_pkthdr.len += sizeof(struct ip6_frag);
1438 /* allocate a new mbuf for the fragment header */
1441 mfrg = m_get(M_NOWAIT, MT_DATA);
1444 mfrg->m_len = sizeof(struct ip6_frag);
1445 *frghdrp = mtod(mfrg, struct ip6_frag *);
1446 mlast->m_next = mfrg;
1453 * Calculates IPv6 path mtu for destination @dst.
1454 * Resulting MTU is stored in @mtup.
1456 * Returns 0 on success.
1459 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1461 struct epoch_tracker et;
1462 struct nhop_object *nh;
1463 struct in6_addr kdst;
1467 in6_splitscope(dst, &kdst, &scopeid);
1469 NET_EPOCH_ENTER(et);
1470 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1472 error = ip6_calcmtu(nh->nh_ifp, dst, nh->nh_mtu, mtup, NULL, 0);
1474 error = EHOSTUNREACH;
1481 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1482 * and cached data in @ro_pmtu.
1483 * MTU from (successful) route lookup is saved (along with dst)
1484 * inside @ro_pmtu to avoid subsequent route lookups after packet
1485 * filter processing.
1487 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1488 * Returns 0 on success.
1491 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1492 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1493 int *alwaysfragp, u_int fibnum, u_int proto)
1495 struct nhop_object *nh;
1496 struct in6_addr kdst;
1498 struct sockaddr_in6 *sa6_dst, sin6;
1504 if (ro_pmtu == NULL || do_lookup) {
1506 * Here ro_pmtu has final destination address, while
1507 * ro might represent immediate destination.
1508 * Use ro_pmtu destination since mtu might differ.
1510 if (ro_pmtu != NULL) {
1511 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1512 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1513 ro_pmtu->ro_mtu = 0;
1517 if (ro_pmtu == NULL || ro_pmtu->ro_mtu == 0) {
1518 bzero(sa6_dst, sizeof(*sa6_dst));
1519 sa6_dst->sin6_family = AF_INET6;
1520 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1521 sa6_dst->sin6_addr = *dst;
1523 in6_splitscope(dst, &kdst, &scopeid);
1524 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1527 if (ro_pmtu != NULL)
1528 ro_pmtu->ro_mtu = mtu;
1531 mtu = ro_pmtu->ro_mtu;
1534 if (ro_pmtu != NULL && ro_pmtu->ro_nh != NULL)
1535 mtu = ro_pmtu->ro_nh->nh_mtu;
1537 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1541 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1542 * hostcache data for @dst.
1543 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1545 * Returns 0 on success.
1548 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1549 u_long *mtup, int *alwaysfragp, u_int proto)
1557 struct in_conninfo inc;
1559 bzero(&inc, sizeof(inc));
1560 inc.inc_flags |= INC_ISIPV6;
1561 inc.inc6_faddr = *dst;
1563 ifmtu = IN6_LINKMTU(ifp);
1565 /* TCP is known to react to pmtu changes so skip hc */
1566 if (proto != IPPROTO_TCP)
1567 mtu = tcp_hc_getmtu(&inc);
1570 mtu = min(mtu, rt_mtu);
1575 else if (mtu < IPV6_MMTU) {
1577 * RFC2460 section 5, last paragraph:
1578 * if we record ICMPv6 too big message with
1579 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1580 * or smaller, with framgent header attached.
1581 * (fragment header is needed regardless from the
1582 * packet size, for translators to identify packets)
1588 mtu = IN6_LINKMTU(ifp);
1590 error = EHOSTUNREACH; /* XXX */
1594 *alwaysfragp = alwaysfrag;
1599 * IP6 socket option processing.
1602 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1604 int optdatalen, uproto;
1606 struct inpcb *inp = sotoinpcb(so);
1608 int level, op, optname;
1612 uint32_t rss_bucket;
1617 * Don't use more than a quarter of mbuf clusters. N.B.:
1618 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1619 * on LP64 architectures, so cast to u_long to avoid undefined
1620 * behavior. ILP32 architectures cannot have nmbclusters
1621 * large enough to overflow for other reasons.
1623 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1625 level = sopt->sopt_level;
1626 op = sopt->sopt_dir;
1627 optname = sopt->sopt_name;
1628 optlen = sopt->sopt_valsize;
1632 uproto = (int)so->so_proto->pr_protocol;
1634 if (level != IPPROTO_IPV6) {
1637 if (sopt->sopt_level == SOL_SOCKET &&
1638 sopt->sopt_dir == SOPT_SET) {
1639 switch (sopt->sopt_name) {
1642 inp->inp_inc.inc_fibnum = so->so_fibnum;
1646 case SO_MAX_PACING_RATE:
1649 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1660 } else { /* level == IPPROTO_IPV6 */
1664 case IPV6_2292PKTOPTIONS:
1665 #ifdef IPV6_PKTOPTIONS
1666 case IPV6_PKTOPTIONS:
1671 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1672 printf("ip6_ctloutput: mbuf limit hit\n");
1677 error = soopt_getm(sopt, &m); /* XXX */
1680 error = soopt_mcopyin(sopt, m); /* XXX */
1684 error = ip6_pcbopts(&inp->in6p_outputopts, m,
1687 m_freem(m); /* XXX */
1692 * Use of some Hop-by-Hop options or some
1693 * Destination options, might require special
1694 * privilege. That is, normal applications
1695 * (without special privilege) might be forbidden
1696 * from setting certain options in outgoing packets,
1697 * and might never see certain options in received
1698 * packets. [RFC 2292 Section 6]
1699 * KAME specific note:
1700 * KAME prevents non-privileged users from sending or
1701 * receiving ANY hbh/dst options in order to avoid
1702 * overhead of parsing options in the kernel.
1704 case IPV6_RECVHOPOPTS:
1705 case IPV6_RECVDSTOPTS:
1706 case IPV6_RECVRTHDRDSTOPTS:
1708 error = priv_check(td,
1709 PRIV_NETINET_SETHDROPTS);
1714 case IPV6_UNICAST_HOPS:
1717 case IPV6_RECVPKTINFO:
1718 case IPV6_RECVHOPLIMIT:
1719 case IPV6_RECVRTHDR:
1720 case IPV6_RECVPATHMTU:
1721 case IPV6_RECVTCLASS:
1722 case IPV6_RECVFLOWID:
1724 case IPV6_RECVRSSBUCKETID:
1727 case IPV6_AUTOFLOWLABEL:
1728 case IPV6_ORIGDSTADDR:
1731 if (optname == IPV6_BINDANY && td != NULL) {
1732 error = priv_check(td,
1733 PRIV_NETINET_BINDANY);
1738 if (optlen != sizeof(int)) {
1742 error = sooptcopyin(sopt, &optval,
1743 sizeof optval, sizeof optval);
1747 case IPV6_UNICAST_HOPS:
1748 if (optval < -1 || optval >= 256)
1751 /* -1 = kernel default */
1752 inp->in6p_hops = optval;
1753 if ((inp->inp_vflag &
1755 inp->inp_ip_ttl = optval;
1758 #define OPTSET(bit) \
1762 inp->inp_flags |= (bit); \
1764 inp->inp_flags &= ~(bit); \
1766 } while (/*CONSTCOND*/ 0)
1767 #define OPTSET2292(bit) \
1770 inp->inp_flags |= IN6P_RFC2292; \
1772 inp->inp_flags |= (bit); \
1774 inp->inp_flags &= ~(bit); \
1776 } while (/*CONSTCOND*/ 0)
1777 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0)
1779 #define OPTSET2_N(bit, val) do { \
1781 inp->inp_flags2 |= bit; \
1783 inp->inp_flags2 &= ~bit; \
1785 #define OPTSET2(bit, val) do { \
1787 OPTSET2_N(bit, val); \
1790 #define OPTBIT2(bit) (inp->inp_flags2 & (bit) ? 1 : 0)
1791 #define OPTSET2292_EXCLUSIVE(bit) \
1794 if (OPTBIT(IN6P_RFC2292)) { \
1798 inp->inp_flags |= (bit); \
1800 inp->inp_flags &= ~(bit); \
1803 } while (/*CONSTCOND*/ 0)
1805 case IPV6_RECVPKTINFO:
1806 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1811 struct ip6_pktopts **optp;
1813 /* cannot mix with RFC2292 */
1814 if (OPTBIT(IN6P_RFC2292)) {
1819 if (inp->inp_flags & INP_DROPPED) {
1821 return (ECONNRESET);
1823 optp = &inp->in6p_outputopts;
1824 error = ip6_pcbopt(IPV6_HOPLIMIT,
1825 (u_char *)&optval, sizeof(optval),
1826 optp, (td != NULL) ? td->td_ucred :
1832 case IPV6_RECVHOPLIMIT:
1833 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1836 case IPV6_RECVHOPOPTS:
1837 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1840 case IPV6_RECVDSTOPTS:
1841 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1844 case IPV6_RECVRTHDRDSTOPTS:
1845 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1848 case IPV6_RECVRTHDR:
1849 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1852 case IPV6_RECVPATHMTU:
1854 * We ignore this option for TCP
1856 * (RFC3542 leaves this case
1859 if (uproto != IPPROTO_TCP)
1863 case IPV6_RECVFLOWID:
1864 OPTSET2(INP_RECVFLOWID, optval);
1868 case IPV6_RECVRSSBUCKETID:
1869 OPTSET2(INP_RECVRSSBUCKETID, optval);
1875 if (inp->inp_lport ||
1876 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1878 * The socket is already bound.
1885 inp->inp_flags |= IN6P_IPV6_V6ONLY;
1886 inp->inp_vflag &= ~INP_IPV4;
1888 inp->inp_flags &= ~IN6P_IPV6_V6ONLY;
1889 inp->inp_vflag |= INP_IPV4;
1893 case IPV6_RECVTCLASS:
1894 /* cannot mix with RFC2292 XXX */
1895 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1897 case IPV6_AUTOFLOWLABEL:
1898 OPTSET(IN6P_AUTOFLOWLABEL);
1901 case IPV6_ORIGDSTADDR:
1902 OPTSET2(INP_ORIGDSTADDR, optval);
1905 OPTSET(INP_BINDANY);
1908 if ((optval >= -1) && (optval <=
1909 (INP_2PCP_MASK >> INP_2PCP_SHIFT))) {
1935 case IPV6_USE_MIN_MTU:
1936 case IPV6_PREFER_TEMPADDR:
1937 if (optlen != sizeof(optval)) {
1941 error = sooptcopyin(sopt, &optval,
1942 sizeof optval, sizeof optval);
1946 struct ip6_pktopts **optp;
1948 if (inp->inp_flags & INP_DROPPED) {
1950 return (ECONNRESET);
1952 optp = &inp->in6p_outputopts;
1953 error = ip6_pcbopt(optname,
1954 (u_char *)&optval, sizeof(optval),
1955 optp, (td != NULL) ? td->td_ucred :
1961 case IPV6_2292PKTINFO:
1962 case IPV6_2292HOPLIMIT:
1963 case IPV6_2292HOPOPTS:
1964 case IPV6_2292DSTOPTS:
1965 case IPV6_2292RTHDR:
1967 if (optlen != sizeof(int)) {
1971 error = sooptcopyin(sopt, &optval,
1972 sizeof optval, sizeof optval);
1976 case IPV6_2292PKTINFO:
1977 OPTSET2292(IN6P_PKTINFO);
1979 case IPV6_2292HOPLIMIT:
1980 OPTSET2292(IN6P_HOPLIMIT);
1982 case IPV6_2292HOPOPTS:
1984 * Check super-user privilege.
1985 * See comments for IPV6_RECVHOPOPTS.
1988 error = priv_check(td,
1989 PRIV_NETINET_SETHDROPTS);
1993 OPTSET2292(IN6P_HOPOPTS);
1995 case IPV6_2292DSTOPTS:
1997 error = priv_check(td,
1998 PRIV_NETINET_SETHDROPTS);
2002 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
2004 case IPV6_2292RTHDR:
2005 OPTSET2292(IN6P_RTHDR);
2013 case IPV6_RTHDRDSTOPTS:
2016 /* new advanced API (RFC3542) */
2018 u_char optbuf_storage[MCLBYTES];
2020 struct ip6_pktopts **optp;
2022 /* cannot mix with RFC2292 */
2023 if (OPTBIT(IN6P_RFC2292)) {
2029 * We only ensure valsize is not too large
2030 * here. Further validation will be done
2033 error = sooptcopyin(sopt, optbuf_storage,
2034 sizeof(optbuf_storage), 0);
2037 optlen = sopt->sopt_valsize;
2038 optbuf = optbuf_storage;
2040 if (inp->inp_flags & INP_DROPPED) {
2042 return (ECONNRESET);
2044 optp = &inp->in6p_outputopts;
2045 error = ip6_pcbopt(optname, optbuf, optlen,
2046 optp, (td != NULL) ? td->td_ucred : NULL,
2053 case IPV6_MULTICAST_IF:
2054 case IPV6_MULTICAST_HOPS:
2055 case IPV6_MULTICAST_LOOP:
2056 case IPV6_JOIN_GROUP:
2057 case IPV6_LEAVE_GROUP:
2059 case MCAST_BLOCK_SOURCE:
2060 case MCAST_UNBLOCK_SOURCE:
2061 case MCAST_JOIN_GROUP:
2062 case MCAST_LEAVE_GROUP:
2063 case MCAST_JOIN_SOURCE_GROUP:
2064 case MCAST_LEAVE_SOURCE_GROUP:
2065 error = ip6_setmoptions(inp, sopt);
2068 case IPV6_PORTRANGE:
2069 error = sooptcopyin(sopt, &optval,
2070 sizeof optval, sizeof optval);
2076 case IPV6_PORTRANGE_DEFAULT:
2077 inp->inp_flags &= ~(INP_LOWPORT);
2078 inp->inp_flags &= ~(INP_HIGHPORT);
2081 case IPV6_PORTRANGE_HIGH:
2082 inp->inp_flags &= ~(INP_LOWPORT);
2083 inp->inp_flags |= INP_HIGHPORT;
2086 case IPV6_PORTRANGE_LOW:
2087 inp->inp_flags &= ~(INP_HIGHPORT);
2088 inp->inp_flags |= INP_LOWPORT;
2098 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2099 case IPV6_IPSEC_POLICY:
2100 if (IPSEC_ENABLED(ipv6)) {
2101 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2108 error = ENOPROTOOPT;
2115 case IPV6_2292PKTOPTIONS:
2116 #ifdef IPV6_PKTOPTIONS
2117 case IPV6_PKTOPTIONS:
2120 * RFC3542 (effectively) deprecated the
2121 * semantics of the 2292-style pktoptions.
2122 * Since it was not reliable in nature (i.e.,
2123 * applications had to expect the lack of some
2124 * information after all), it would make sense
2125 * to simplify this part by always returning
2128 sopt->sopt_valsize = 0;
2131 case IPV6_RECVHOPOPTS:
2132 case IPV6_RECVDSTOPTS:
2133 case IPV6_RECVRTHDRDSTOPTS:
2134 case IPV6_UNICAST_HOPS:
2135 case IPV6_RECVPKTINFO:
2136 case IPV6_RECVHOPLIMIT:
2137 case IPV6_RECVRTHDR:
2138 case IPV6_RECVPATHMTU:
2141 case IPV6_PORTRANGE:
2142 case IPV6_RECVTCLASS:
2143 case IPV6_AUTOFLOWLABEL:
2147 case IPV6_RECVFLOWID:
2149 case IPV6_RSSBUCKETID:
2150 case IPV6_RECVRSSBUCKETID:
2154 case IPV6_RECVHOPOPTS:
2155 optval = OPTBIT(IN6P_HOPOPTS);
2158 case IPV6_RECVDSTOPTS:
2159 optval = OPTBIT(IN6P_DSTOPTS);
2162 case IPV6_RECVRTHDRDSTOPTS:
2163 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2166 case IPV6_UNICAST_HOPS:
2167 optval = inp->in6p_hops;
2170 case IPV6_RECVPKTINFO:
2171 optval = OPTBIT(IN6P_PKTINFO);
2174 case IPV6_RECVHOPLIMIT:
2175 optval = OPTBIT(IN6P_HOPLIMIT);
2178 case IPV6_RECVRTHDR:
2179 optval = OPTBIT(IN6P_RTHDR);
2182 case IPV6_RECVPATHMTU:
2183 optval = OPTBIT(IN6P_MTU);
2187 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2190 case IPV6_PORTRANGE:
2193 flags = inp->inp_flags;
2194 if (flags & INP_HIGHPORT)
2195 optval = IPV6_PORTRANGE_HIGH;
2196 else if (flags & INP_LOWPORT)
2197 optval = IPV6_PORTRANGE_LOW;
2202 case IPV6_RECVTCLASS:
2203 optval = OPTBIT(IN6P_TCLASS);
2206 case IPV6_AUTOFLOWLABEL:
2207 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2210 case IPV6_ORIGDSTADDR:
2211 optval = OPTBIT2(INP_ORIGDSTADDR);
2215 optval = OPTBIT(INP_BINDANY);
2219 optval = inp->inp_flowid;
2223 optval = inp->inp_flowtype;
2226 case IPV6_RECVFLOWID:
2227 optval = OPTBIT2(INP_RECVFLOWID);
2230 case IPV6_RSSBUCKETID:
2232 rss_hash2bucket(inp->inp_flowid,
2236 optval = rss_bucket;
2241 case IPV6_RECVRSSBUCKETID:
2242 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2248 if (OPTBIT2(INP_2PCP_SET)) {
2249 optval = (inp->inp_flags2 &
2260 error = sooptcopyout(sopt, &optval,
2267 struct ip6_mtuinfo mtuinfo;
2268 struct in6_addr addr;
2270 if (!(so->so_state & SS_ISCONNECTED))
2273 * XXX: we dot not consider the case of source
2274 * routing, or optional information to specify
2275 * the outgoing interface.
2276 * Copy faddr out of inp to avoid holding lock
2277 * on inp during route lookup.
2280 bcopy(&inp->in6p_faddr, &addr, sizeof(addr));
2282 error = ip6_getpmtu_ctl(so->so_fibnum,
2286 if (pmtu > IPV6_MAXPACKET)
2287 pmtu = IPV6_MAXPACKET;
2289 bzero(&mtuinfo, sizeof(mtuinfo));
2290 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2291 optdata = (void *)&mtuinfo;
2292 optdatalen = sizeof(mtuinfo);
2293 error = sooptcopyout(sopt, optdata,
2298 case IPV6_2292PKTINFO:
2299 case IPV6_2292HOPLIMIT:
2300 case IPV6_2292HOPOPTS:
2301 case IPV6_2292RTHDR:
2302 case IPV6_2292DSTOPTS:
2304 case IPV6_2292PKTINFO:
2305 optval = OPTBIT(IN6P_PKTINFO);
2307 case IPV6_2292HOPLIMIT:
2308 optval = OPTBIT(IN6P_HOPLIMIT);
2310 case IPV6_2292HOPOPTS:
2311 optval = OPTBIT(IN6P_HOPOPTS);
2313 case IPV6_2292RTHDR:
2314 optval = OPTBIT(IN6P_RTHDR);
2316 case IPV6_2292DSTOPTS:
2317 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2320 error = sooptcopyout(sopt, &optval,
2327 case IPV6_RTHDRDSTOPTS:
2331 case IPV6_USE_MIN_MTU:
2332 case IPV6_PREFER_TEMPADDR:
2333 error = ip6_getpcbopt(inp, optname, sopt);
2336 case IPV6_MULTICAST_IF:
2337 case IPV6_MULTICAST_HOPS:
2338 case IPV6_MULTICAST_LOOP:
2340 error = ip6_getmoptions(inp, sopt);
2343 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2344 case IPV6_IPSEC_POLICY:
2345 if (IPSEC_ENABLED(ipv6)) {
2346 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2352 error = ENOPROTOOPT;
2362 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2364 int error = 0, optval, optlen;
2365 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2366 struct inpcb *inp = sotoinpcb(so);
2367 int level, op, optname;
2369 level = sopt->sopt_level;
2370 op = sopt->sopt_dir;
2371 optname = sopt->sopt_name;
2372 optlen = sopt->sopt_valsize;
2374 if (level != IPPROTO_IPV6) {
2381 * For ICMPv6 sockets, no modification allowed for checksum
2382 * offset, permit "no change" values to help existing apps.
2384 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2385 * for an ICMPv6 socket will fail."
2386 * The current behavior does not meet RFC3542.
2390 if (optlen != sizeof(int)) {
2394 error = sooptcopyin(sopt, &optval, sizeof(optval),
2398 if (optval < -1 || (optval % 2) != 0) {
2400 * The API assumes non-negative even offset
2401 * values or -1 as a special value.
2404 } else if (inp->inp_ip_p == IPPROTO_ICMPV6) {
2405 if (optval != icmp6off)
2408 inp->in6p_cksum = optval;
2412 if (inp->inp_ip_p == IPPROTO_ICMPV6)
2415 optval = inp->in6p_cksum;
2417 error = sooptcopyout(sopt, &optval, sizeof(optval));
2427 error = ENOPROTOOPT;
2435 * Set up IP6 options in pcb for insertion in output packets or
2436 * specifying behavior of outgoing packets.
2439 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2440 struct socket *so, struct sockopt *sopt)
2442 struct ip6_pktopts *opt = *pktopt;
2444 struct thread *td = sopt->sopt_td;
2445 struct epoch_tracker et;
2447 /* turn off any old options. */
2450 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2451 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2452 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2453 printf("ip6_pcbopts: all specified options are cleared.\n");
2455 ip6_clearpktopts(opt, -1);
2457 opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT);
2463 if (!m || m->m_len == 0) {
2465 * Only turning off any previous options, regardless of
2466 * whether the opt is just created or given.
2468 free(opt, M_IP6OPT);
2472 /* set options specified by user. */
2473 NET_EPOCH_ENTER(et);
2474 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2475 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2476 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2477 free(opt, M_IP6OPT);
2487 * initialize ip6_pktopts. beware that there are non-zero default values in
2491 ip6_initpktopts(struct ip6_pktopts *opt)
2494 bzero(opt, sizeof(*opt));
2495 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2496 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2497 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2498 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2502 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2503 struct ucred *cred, int uproto)
2505 struct epoch_tracker et;
2506 struct ip6_pktopts *opt;
2509 if (*pktopt == NULL) {
2510 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2512 if (*pktopt == NULL)
2514 ip6_initpktopts(*pktopt);
2518 NET_EPOCH_ENTER(et);
2519 ret = ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto);
2525 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2526 if (pktopt && pktopt->field) { \
2528 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2529 malloc_optdata = true; \
2531 if (inp->inp_flags & INP_DROPPED) { \
2533 free(optdata, M_TEMP); \
2534 return (ECONNRESET); \
2536 pktopt = inp->in6p_outputopts; \
2537 if (pktopt && pktopt->field) { \
2538 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2539 bcopy(pktopt->field, optdata, optdatalen); \
2541 free(optdata, M_TEMP); \
2543 malloc_optdata = false; \
2548 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2549 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2551 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2552 pktopt->field->sa_len)
2555 ip6_getpcbopt(struct inpcb *inp, int optname, struct sockopt *sopt)
2557 void *optdata = NULL;
2558 bool malloc_optdata = false;
2561 struct in6_pktinfo null_pktinfo;
2562 int deftclass = 0, on;
2563 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2564 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2565 struct ip6_pktopts *pktopt;
2568 pktopt = inp->in6p_outputopts;
2572 optdata = (void *)&null_pktinfo;
2573 if (pktopt && pktopt->ip6po_pktinfo) {
2574 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2575 sizeof(null_pktinfo));
2576 in6_clearscope(&null_pktinfo.ipi6_addr);
2578 /* XXX: we don't have to do this every time... */
2579 bzero(&null_pktinfo, sizeof(null_pktinfo));
2581 optdatalen = sizeof(struct in6_pktinfo);
2584 if (pktopt && pktopt->ip6po_tclass >= 0)
2585 deftclass = pktopt->ip6po_tclass;
2586 optdata = (void *)&deftclass;
2587 optdatalen = sizeof(int);
2590 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2593 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2595 case IPV6_RTHDRDSTOPTS:
2596 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2599 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2602 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2604 case IPV6_USE_MIN_MTU:
2606 defminmtu = pktopt->ip6po_minmtu;
2607 optdata = (void *)&defminmtu;
2608 optdatalen = sizeof(int);
2611 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2615 optdata = (void *)&on;
2616 optdatalen = sizeof(on);
2618 case IPV6_PREFER_TEMPADDR:
2620 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2621 optdata = (void *)&defpreftemp;
2622 optdatalen = sizeof(int);
2624 default: /* should not happen */
2626 panic("ip6_getpcbopt: unexpected option\n");
2629 return (ENOPROTOOPT);
2633 error = sooptcopyout(sopt, optdata, optdatalen);
2635 free(optdata, M_TEMP);
2641 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2646 if (optname == -1 || optname == IPV6_PKTINFO) {
2647 if (pktopt->ip6po_pktinfo)
2648 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2649 pktopt->ip6po_pktinfo = NULL;
2651 if (optname == -1 || optname == IPV6_HOPLIMIT)
2652 pktopt->ip6po_hlim = -1;
2653 if (optname == -1 || optname == IPV6_TCLASS)
2654 pktopt->ip6po_tclass = -1;
2655 if (optname == -1 || optname == IPV6_NEXTHOP) {
2656 if (pktopt->ip6po_nextroute.ro_nh) {
2657 NH_FREE(pktopt->ip6po_nextroute.ro_nh);
2658 pktopt->ip6po_nextroute.ro_nh = NULL;
2660 if (pktopt->ip6po_nexthop)
2661 free(pktopt->ip6po_nexthop, M_IP6OPT);
2662 pktopt->ip6po_nexthop = NULL;
2664 if (optname == -1 || optname == IPV6_HOPOPTS) {
2665 if (pktopt->ip6po_hbh)
2666 free(pktopt->ip6po_hbh, M_IP6OPT);
2667 pktopt->ip6po_hbh = NULL;
2669 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2670 if (pktopt->ip6po_dest1)
2671 free(pktopt->ip6po_dest1, M_IP6OPT);
2672 pktopt->ip6po_dest1 = NULL;
2674 if (optname == -1 || optname == IPV6_RTHDR) {
2675 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2676 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2677 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2678 if (pktopt->ip6po_route.ro_nh) {
2679 NH_FREE(pktopt->ip6po_route.ro_nh);
2680 pktopt->ip6po_route.ro_nh = NULL;
2683 if (optname == -1 || optname == IPV6_DSTOPTS) {
2684 if (pktopt->ip6po_dest2)
2685 free(pktopt->ip6po_dest2, M_IP6OPT);
2686 pktopt->ip6po_dest2 = NULL;
2690 #define PKTOPT_EXTHDRCPY(type) \
2693 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2694 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2695 if (dst->type == NULL)\
2697 bcopy(src->type, dst->type, hlen);\
2699 } while (/*CONSTCOND*/ 0)
2702 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2704 if (dst == NULL || src == NULL) {
2705 printf("ip6_clearpktopts: invalid argument\n");
2709 dst->ip6po_hlim = src->ip6po_hlim;
2710 dst->ip6po_tclass = src->ip6po_tclass;
2711 dst->ip6po_flags = src->ip6po_flags;
2712 dst->ip6po_minmtu = src->ip6po_minmtu;
2713 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2714 if (src->ip6po_pktinfo) {
2715 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2717 if (dst->ip6po_pktinfo == NULL)
2719 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2721 if (src->ip6po_nexthop) {
2722 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2724 if (dst->ip6po_nexthop == NULL)
2726 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2727 src->ip6po_nexthop->sa_len);
2729 PKTOPT_EXTHDRCPY(ip6po_hbh);
2730 PKTOPT_EXTHDRCPY(ip6po_dest1);
2731 PKTOPT_EXTHDRCPY(ip6po_dest2);
2732 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2736 ip6_clearpktopts(dst, -1);
2739 #undef PKTOPT_EXTHDRCPY
2741 struct ip6_pktopts *
2742 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2745 struct ip6_pktopts *dst;
2747 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2750 ip6_initpktopts(dst);
2752 if ((error = copypktopts(dst, src, canwait)) != 0) {
2753 free(dst, M_IP6OPT);
2761 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2766 ip6_clearpktopts(pktopt, -1);
2768 free(pktopt, M_IP6OPT);
2772 * Set IPv6 outgoing packet options based on advanced API.
2775 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2776 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2778 struct cmsghdr *cm = NULL;
2780 if (control == NULL || opt == NULL)
2784 * ip6_setpktopt can call ifnet_byindex(), so it's imperative that we
2785 * are in the network epoch here.
2789 ip6_initpktopts(opt);
2794 * If stickyopt is provided, make a local copy of the options
2795 * for this particular packet, then override them by ancillary
2797 * XXX: copypktopts() does not copy the cached route to a next
2798 * hop (if any). This is not very good in terms of efficiency,
2799 * but we can allow this since this option should be rarely
2802 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2807 * XXX: Currently, we assume all the optional information is stored
2810 if (control->m_next)
2813 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2814 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2817 if (control->m_len < CMSG_LEN(0))
2820 cm = mtod(control, struct cmsghdr *);
2821 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2823 if (cm->cmsg_level != IPPROTO_IPV6)
2826 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2827 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2836 * Set a particular packet option, as a sticky option or an ancillary data
2837 * item. "len" can be 0 only when it's a sticky option.
2838 * We have 4 cases of combination of "sticky" and "cmsg":
2839 * "sticky=0, cmsg=0": impossible
2840 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2841 * "sticky=1, cmsg=0": RFC3542 socket option
2842 * "sticky=1, cmsg=1": RFC2292 socket option
2845 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2846 struct ucred *cred, int sticky, int cmsg, int uproto)
2848 int minmtupolicy, preftemp;
2853 if (!sticky && !cmsg) {
2855 printf("ip6_setpktopt: impossible case\n");
2861 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2862 * not be specified in the context of RFC3542. Conversely,
2863 * RFC3542 types should not be specified in the context of RFC2292.
2867 case IPV6_2292PKTINFO:
2868 case IPV6_2292HOPLIMIT:
2869 case IPV6_2292NEXTHOP:
2870 case IPV6_2292HOPOPTS:
2871 case IPV6_2292DSTOPTS:
2872 case IPV6_2292RTHDR:
2873 case IPV6_2292PKTOPTIONS:
2874 return (ENOPROTOOPT);
2877 if (sticky && cmsg) {
2884 case IPV6_RTHDRDSTOPTS:
2886 case IPV6_USE_MIN_MTU:
2889 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2890 return (ENOPROTOOPT);
2895 case IPV6_2292PKTINFO:
2898 struct ifnet *ifp = NULL;
2899 struct in6_pktinfo *pktinfo;
2901 if (len != sizeof(struct in6_pktinfo))
2904 pktinfo = (struct in6_pktinfo *)buf;
2907 * An application can clear any sticky IPV6_PKTINFO option by
2908 * doing a "regular" setsockopt with ipi6_addr being
2909 * in6addr_any and ipi6_ifindex being zero.
2910 * [RFC 3542, Section 6]
2912 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2913 pktinfo->ipi6_ifindex == 0 &&
2914 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2915 ip6_clearpktopts(opt, optname);
2919 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2920 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2923 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2925 /* validate the interface index if specified. */
2926 if (pktinfo->ipi6_ifindex) {
2927 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2931 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2932 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2936 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2937 struct in6_ifaddr *ia;
2939 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2940 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2942 return (EADDRNOTAVAIL);
2943 ifa_free(&ia->ia_ifa);
2946 * We store the address anyway, and let in6_selectsrc()
2947 * validate the specified address. This is because ipi6_addr
2948 * may not have enough information about its scope zone, and
2949 * we may need additional information (such as outgoing
2950 * interface or the scope zone of a destination address) to
2951 * disambiguate the scope.
2952 * XXX: the delay of the validation may confuse the
2953 * application when it is used as a sticky option.
2955 if (opt->ip6po_pktinfo == NULL) {
2956 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2957 M_IP6OPT, M_NOWAIT);
2958 if (opt->ip6po_pktinfo == NULL)
2961 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2965 case IPV6_2292HOPLIMIT:
2971 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2972 * to simplify the ordering among hoplimit options.
2974 if (optname == IPV6_HOPLIMIT && sticky)
2975 return (ENOPROTOOPT);
2977 if (len != sizeof(int))
2980 if (*hlimp < -1 || *hlimp > 255)
2983 opt->ip6po_hlim = *hlimp;
2991 if (len != sizeof(int))
2993 tclass = *(int *)buf;
2994 if (tclass < -1 || tclass > 255)
2997 opt->ip6po_tclass = tclass;
3001 case IPV6_2292NEXTHOP:
3004 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3009 if (len == 0) { /* just remove the option */
3010 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3014 /* check if cmsg_len is large enough for sa_len */
3015 if (len < sizeof(struct sockaddr) || len < *buf)
3018 switch (((struct sockaddr *)buf)->sa_family) {
3021 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3024 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3027 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3028 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3031 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
3037 case AF_LINK: /* should eventually be supported */
3039 return (EAFNOSUPPORT);
3042 /* turn off the previous option, then set the new option. */
3043 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3044 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3045 if (opt->ip6po_nexthop == NULL)
3047 bcopy(buf, opt->ip6po_nexthop, *buf);
3050 case IPV6_2292HOPOPTS:
3053 struct ip6_hbh *hbh;
3057 * XXX: We don't allow a non-privileged user to set ANY HbH
3058 * options, since per-option restriction has too much
3062 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3068 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3069 break; /* just remove the option */
3072 /* message length validation */
3073 if (len < sizeof(struct ip6_hbh))
3075 hbh = (struct ip6_hbh *)buf;
3076 hbhlen = (hbh->ip6h_len + 1) << 3;
3080 /* turn off the previous option, then set the new option. */
3081 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3082 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3083 if (opt->ip6po_hbh == NULL)
3085 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3090 case IPV6_2292DSTOPTS:
3092 case IPV6_RTHDRDSTOPTS:
3094 struct ip6_dest *dest, **newdest = NULL;
3097 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
3098 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3104 ip6_clearpktopts(opt, optname);
3105 break; /* just remove the option */
3108 /* message length validation */
3109 if (len < sizeof(struct ip6_dest))
3111 dest = (struct ip6_dest *)buf;
3112 destlen = (dest->ip6d_len + 1) << 3;
3117 * Determine the position that the destination options header
3118 * should be inserted; before or after the routing header.
3121 case IPV6_2292DSTOPTS:
3123 * The old advacned API is ambiguous on this point.
3124 * Our approach is to determine the position based
3125 * according to the existence of a routing header.
3126 * Note, however, that this depends on the order of the
3127 * extension headers in the ancillary data; the 1st
3128 * part of the destination options header must appear
3129 * before the routing header in the ancillary data,
3131 * RFC3542 solved the ambiguity by introducing
3132 * separate ancillary data or option types.
3134 if (opt->ip6po_rthdr == NULL)
3135 newdest = &opt->ip6po_dest1;
3137 newdest = &opt->ip6po_dest2;
3139 case IPV6_RTHDRDSTOPTS:
3140 newdest = &opt->ip6po_dest1;
3143 newdest = &opt->ip6po_dest2;
3147 /* turn off the previous option, then set the new option. */
3148 ip6_clearpktopts(opt, optname);
3149 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3150 if (*newdest == NULL)
3152 bcopy(dest, *newdest, destlen);
3157 case IPV6_2292RTHDR:
3160 struct ip6_rthdr *rth;
3164 ip6_clearpktopts(opt, IPV6_RTHDR);
3165 break; /* just remove the option */
3168 /* message length validation */
3169 if (len < sizeof(struct ip6_rthdr))
3171 rth = (struct ip6_rthdr *)buf;
3172 rthlen = (rth->ip6r_len + 1) << 3;
3176 switch (rth->ip6r_type) {
3177 case IPV6_RTHDR_TYPE_0:
3178 if (rth->ip6r_len == 0) /* must contain one addr */
3180 if (rth->ip6r_len % 2) /* length must be even */
3182 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3186 return (EINVAL); /* not supported */
3189 /* turn off the previous option */
3190 ip6_clearpktopts(opt, IPV6_RTHDR);
3191 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3192 if (opt->ip6po_rthdr == NULL)
3194 bcopy(rth, opt->ip6po_rthdr, rthlen);
3199 case IPV6_USE_MIN_MTU:
3200 if (len != sizeof(int))
3202 minmtupolicy = *(int *)buf;
3203 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3204 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3205 minmtupolicy != IP6PO_MINMTU_ALL) {
3208 opt->ip6po_minmtu = minmtupolicy;
3212 if (len != sizeof(int))
3215 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3217 * we ignore this option for TCP sockets.
3218 * (RFC3542 leaves this case unspecified.)
3220 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3222 opt->ip6po_flags |= IP6PO_DONTFRAG;
3225 case IPV6_PREFER_TEMPADDR:
3226 if (len != sizeof(int))
3228 preftemp = *(int *)buf;
3229 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3230 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3231 preftemp != IP6PO_TEMPADDR_PREFER) {
3234 opt->ip6po_prefer_tempaddr = preftemp;
3238 return (ENOPROTOOPT);
3239 } /* end of switch */
3245 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3246 * packet to the input queue of a specified interface. Note that this
3247 * calls the output routine of the loopback "driver", but with an interface
3248 * pointer that might NOT be &loif -- easier than replicating that code here.
3251 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3254 struct ip6_hdr *ip6;
3256 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3261 * Make sure to deep-copy IPv6 header portion in case the data
3262 * is in an mbuf cluster, so that we can safely override the IPv6
3263 * header portion later.
3265 if (!M_WRITABLE(copym) ||
3266 copym->m_len < sizeof(struct ip6_hdr)) {
3267 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3271 ip6 = mtod(copym, struct ip6_hdr *);
3273 * clear embedded scope identifiers if necessary.
3274 * in6_clearscope will touch the addresses only when necessary.
3276 in6_clearscope(&ip6->ip6_src);
3277 in6_clearscope(&ip6->ip6_dst);
3278 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3279 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3281 copym->m_pkthdr.csum_data = 0xffff;
3283 if_simloop(ifp, copym, AF_INET6, 0);
3287 * Chop IPv6 header off from the payload.
3290 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3293 struct ip6_hdr *ip6;
3295 ip6 = mtod(m, struct ip6_hdr *);
3296 if (m->m_len > sizeof(*ip6)) {
3297 mh = m_gethdr(M_NOWAIT, MT_DATA);
3302 m_move_pkthdr(mh, m);
3303 M_ALIGN(mh, sizeof(*ip6));
3304 m->m_len -= sizeof(*ip6);
3305 m->m_data += sizeof(*ip6);
3308 m->m_len = sizeof(*ip6);
3309 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3311 exthdrs->ip6e_ip6 = m;
3316 * Compute IPv6 extension header length.
3319 ip6_optlen(struct inpcb *inp)
3323 if (!inp->in6p_outputopts)
3328 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3330 len += elen(inp->in6p_outputopts->ip6po_hbh);
3331 if (inp->in6p_outputopts->ip6po_rthdr)
3332 /* dest1 is valid with rthdr only */
3333 len += elen(inp->in6p_outputopts->ip6po_dest1);
3334 len += elen(inp->in6p_outputopts->ip6po_rthdr);
3335 len += elen(inp->in6p_outputopts->ip6po_dest2);