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_vlan_var.h>
96 #include <net/if_llatbl.h>
97 #include <net/ethernet.h>
98 #include <net/netisr.h>
99 #include <net/route.h>
100 #include <net/route/nhop.h>
101 #include <net/pfil.h>
102 #include <net/rss_config.h>
103 #include <net/vnet.h>
105 #include <netinet/in.h>
106 #include <netinet/in_var.h>
107 #include <netinet/ip_var.h>
108 #include <netinet6/in6_fib.h>
109 #include <netinet6/in6_var.h>
110 #include <netinet/ip6.h>
111 #include <netinet/icmp6.h>
112 #include <netinet6/ip6_var.h>
113 #include <netinet/in_pcb.h>
114 #include <netinet/tcp_var.h>
115 #include <netinet6/nd6.h>
116 #include <netinet6/in6_rss.h>
118 #include <netipsec/ipsec_support.h>
119 #if defined(SCTP) || defined(SCTP_SUPPORT)
120 #include <netinet/sctp.h>
121 #include <netinet/sctp_crc32.h>
124 #include <netinet6/ip6protosw.h>
125 #include <netinet6/scope6_var.h>
127 extern int in6_mcast_loop;
130 struct mbuf *ip6e_ip6;
131 struct mbuf *ip6e_hbh;
132 struct mbuf *ip6e_dest1;
133 struct mbuf *ip6e_rthdr;
134 struct mbuf *ip6e_dest2;
137 static MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
139 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
140 struct ucred *, int);
141 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
142 struct socket *, struct sockopt *);
143 static int ip6_getpcbopt(struct inpcb *, int, struct sockopt *);
144 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
145 struct ucred *, int, int, int);
147 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
148 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
150 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
151 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
152 static int ip6_getpmtu(struct route_in6 *, int,
153 struct ifnet *, const struct in6_addr *, u_long *, int *, u_int,
155 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
156 u_long *, int *, u_int);
157 static int ip6_getpmtu_ctl(u_int, const struct in6_addr *, u_long *);
158 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
161 * Make an extension header from option data. hp is the source,
162 * mp is the destination, and _ol is the optlen.
164 #define MAKE_EXTHDR(hp, mp, _ol) \
167 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
168 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
169 ((eh)->ip6e_len + 1) << 3); \
172 (_ol) += (*(mp))->m_len; \
174 } while (/*CONSTCOND*/ 0)
177 * Form a chain of extension headers.
178 * m is the extension header mbuf
179 * mp is the previous mbuf in the chain
180 * p is the next header
181 * i is the type of option.
183 #define MAKE_CHAIN(m, mp, p, i)\
187 panic("%s:%d: assumption failed: "\
188 "hdr not split: hdrsplit %d exthdrs %p",\
189 __func__, __LINE__, hdrsplit, &exthdrs);\
190 *mtod((m), u_char *) = *(p);\
192 p = mtod((m), u_char *);\
193 (m)->m_next = (mp)->m_next;\
197 } while (/*CONSTCOND*/ 0)
200 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
204 csum = in_cksum_skip(m, offset + plen, offset);
205 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
207 offset += m->m_pkthdr.csum_data; /* checksum offset */
209 if (offset + sizeof(csum) > m->m_len)
210 m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
212 *(u_short *)mtodo(m, offset) = csum;
216 ip6_output_delayed_csum(struct mbuf *m, struct ifnet *ifp, int csum_flags,
217 int plen, int optlen, bool frag)
220 KASSERT((plen >= optlen), ("%s:%d: plen %d < optlen %d, m %p, ifp %p "
221 "csum_flags %#x frag %d\n",
222 __func__, __LINE__, plen, optlen, m, ifp, csum_flags, frag));
224 if ((csum_flags & CSUM_DELAY_DATA_IPV6) ||
225 #if defined(SCTP) || defined(SCTP_SUPPORT)
226 (csum_flags & CSUM_SCTP_IPV6) ||
228 (!frag && (ifp->if_capenable & IFCAP_NOMAP) == 0)) {
229 m = mb_unmapped_to_ext(m);
232 in6_ifstat_inc(ifp, ifs6_out_fragfail);
234 IP6STAT_INC(ip6s_odropped);
237 if (csum_flags & CSUM_DELAY_DATA_IPV6) {
238 in6_delayed_cksum(m, plen - optlen,
239 sizeof(struct ip6_hdr) + optlen);
240 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
242 #if defined(SCTP) || defined(SCTP_SUPPORT)
243 if (csum_flags & CSUM_SCTP_IPV6) {
244 sctp_delayed_cksum(m, sizeof(struct ip6_hdr) + optlen);
245 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
254 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
255 int fraglen , uint32_t id)
257 struct mbuf *m, **mnext, *m_frgpart;
258 struct ip6_hdr *ip6, *mhip6;
259 struct ip6_frag *ip6f;
262 int tlen = m0->m_pkthdr.len;
264 KASSERT((fraglen % 8 == 0), ("Fragment length must be a multiple of 8"));
267 ip6 = mtod(m, struct ip6_hdr *);
268 mnext = &m->m_nextpkt;
270 for (off = hlen; off < tlen; off += fraglen) {
271 m = m_gethdr(M_NOWAIT, MT_DATA);
273 IP6STAT_INC(ip6s_odropped);
278 * Make sure the complete packet header gets copied
279 * from the originating mbuf to the newly created
280 * mbuf. This also ensures that existing firewall
281 * classification(s), VLAN tags and so on get copied
282 * to the resulting fragmented packet(s):
284 if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
286 IP6STAT_INC(ip6s_odropped);
291 mnext = &m->m_nextpkt;
292 m->m_data += max_linkhdr;
293 mhip6 = mtod(m, struct ip6_hdr *);
295 m->m_len = sizeof(*mhip6);
296 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
298 IP6STAT_INC(ip6s_odropped);
301 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
302 if (off + fraglen >= tlen)
303 fraglen = tlen - off;
305 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
306 mhip6->ip6_plen = htons((u_short)(fraglen + hlen +
307 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
308 if ((m_frgpart = m_copym(m0, off, fraglen, M_NOWAIT)) == NULL) {
309 IP6STAT_INC(ip6s_odropped);
313 m->m_pkthdr.len = fraglen + hlen + sizeof(*ip6f);
314 ip6f->ip6f_reserved = 0;
315 ip6f->ip6f_ident = id;
316 ip6f->ip6f_nxt = nextproto;
317 IP6STAT_INC(ip6s_ofragments);
318 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
325 ip6_output_send(struct inpcb *inp, struct ifnet *ifp, struct ifnet *origifp,
326 struct mbuf *m, struct sockaddr_in6 *dst, struct route_in6 *ro,
330 struct ktls_session *tls = NULL;
332 struct m_snd_tag *mst;
335 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
340 * If this is an unencrypted TLS record, save a reference to
341 * the record. This local reference is used to call
342 * ktls_output_eagain after the mbuf has been freed (thus
343 * dropping the mbuf's reference) in if_output.
345 if (m->m_next != NULL && mbuf_has_tls_session(m->m_next)) {
346 tls = ktls_hold(m->m_next->m_epg_tls);
350 * If a TLS session doesn't have a valid tag, it must
351 * have had an earlier ifp mismatch, so drop this
359 * Always stamp tags that include NIC ktls.
365 if (inp != NULL && mst == NULL) {
366 if ((inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) != 0 ||
367 (inp->inp_snd_tag != NULL &&
368 inp->inp_snd_tag->ifp != ifp))
369 in_pcboutput_txrtlmt(inp, ifp, m);
371 if (inp->inp_snd_tag != NULL)
372 mst = inp->inp_snd_tag;
375 if (stamp_tag && mst != NULL) {
376 KASSERT(m->m_pkthdr.rcvif == NULL,
377 ("trying to add a send tag to a forwarded packet"));
378 if (mst->ifp != ifp) {
383 /* stamp send tag on mbuf */
384 m->m_pkthdr.snd_tag = m_snd_tag_ref(mst);
385 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
388 error = nd6_output_ifp(ifp, origifp, m, dst, (struct route *)ro);
391 /* Check for route change invalidating send tags. */
395 error = ktls_output_eagain(inp, tls);
401 in_pcboutput_eagain(inp);
408 * The packet in mbuf chain m contains a skeletal IP6 header (with pri, len,
409 * nxt, hlim, src, dst).
410 * This function may modify ver and hlim only.
411 * The mbuf chain containing the packet will be freed.
412 * The mbuf opt, if present, will not be freed.
413 * If route_in6 ro is present and has ro_nh initialized, route lookup would be
414 * skipped and ro->ro_nh would be used. If ro is present but ro->ro_nh is NULL,
415 * then result of route lookup is stored in ro->ro_nh.
417 * Type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and nd_ifinfo.linkmtu
418 * is uint32_t. So we use u_long to hold largest one, which is rt_mtu.
420 * ifpp - XXX: just for statistics
423 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
424 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
425 struct ifnet **ifpp, struct inpcb *inp)
428 struct ifnet *ifp, *origifp;
431 struct route_in6 *ro_pmtu;
432 struct nhop_object *nh;
433 struct sockaddr_in6 *dst, sin6, src_sa, dst_sa;
434 struct in6_addr odst;
439 struct in6_ifaddr *ia = NULL;
441 int alwaysfrag, dontfrag;
442 u_int32_t optlen, plen = 0, unfragpartlen;
443 struct ip6_exthdrs exthdrs;
444 struct in6_addr src0, dst0;
450 struct m_tag *fwd_tag = NULL;
456 INP_LOCK_ASSERT(inp);
457 M_SETFIB(m, inp->inp_inc.inc_fibnum);
458 if ((flags & IP_NODEFAULTFLOWID) == 0) {
459 /* Unconditionally set flowid. */
460 m->m_pkthdr.flowid = inp->inp_flowid;
461 M_HASHTYPE_SET(m, inp->inp_flowtype);
463 if ((inp->inp_flags2 & INP_2PCP_SET) != 0)
464 vlan_pcp = (inp->inp_flags2 & INP_2PCP_MASK) >>
467 m->m_pkthdr.numa_domain = inp->inp_numa_domain;
471 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
473 * IPSec checking which handles several cases.
474 * FAST IPSEC: We re-injected the packet.
475 * XXX: need scope argument.
477 if (IPSEC_ENABLED(ipv6)) {
478 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
479 if (error == EINPROGRESS)
486 /* Source address validation. */
487 ip6 = mtod(m, struct ip6_hdr *);
488 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
489 (flags & IPV6_UNSPECSRC) == 0) {
491 IP6STAT_INC(ip6s_badscope);
494 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
496 IP6STAT_INC(ip6s_badscope);
501 * If we are given packet options to add extension headers prepare them.
502 * Calculate the total length of the extension header chain.
503 * Keep the length of the unfragmentable part for fragmentation.
505 bzero(&exthdrs, sizeof(exthdrs));
507 unfragpartlen = sizeof(struct ip6_hdr);
509 /* Hop-by-Hop options header. */
510 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh, optlen);
512 /* Destination options header (1st part). */
513 if (opt->ip6po_rthdr) {
514 #ifndef RTHDR_SUPPORT_IMPLEMENTED
516 * If there is a routing header, discard the packet
517 * right away here. RH0/1 are obsolete and we do not
518 * currently support RH2/3/4.
519 * People trying to use RH253/254 may want to disable
521 * The moment we do support any routing header (again)
522 * this block should check the routing type more
530 * Destination options header (1st part).
531 * This only makes sense with a routing header.
532 * See Section 9.2 of RFC 3542.
533 * Disabling this part just for MIP6 convenience is
534 * a bad idea. We need to think carefully about a
535 * way to make the advanced API coexist with MIP6
536 * options, which might automatically be inserted in
539 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1,
542 /* Routing header. */
543 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr, optlen);
545 unfragpartlen += optlen;
548 * NOTE: we don't add AH/ESP length here (done in
549 * ip6_ipsec_output()).
552 /* Destination options header (2nd part). */
553 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2, optlen);
557 * If there is at least one extension header,
558 * separate IP6 header from the payload.
562 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
566 m = exthdrs.ip6e_ip6;
567 ip6 = mtod(m, struct ip6_hdr *);
571 /* Adjust mbuf packet header length. */
572 m->m_pkthdr.len += optlen;
573 plen = m->m_pkthdr.len - sizeof(*ip6);
575 /* If this is a jumbo payload, insert a jumbo payload option. */
576 if (plen > IPV6_MAXPACKET) {
578 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
582 m = exthdrs.ip6e_ip6;
583 ip6 = mtod(m, struct ip6_hdr *);
586 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
590 ip6->ip6_plen = htons(plen);
591 nexthdrp = &ip6->ip6_nxt;
595 * Concatenate headers and fill in next header fields.
596 * Here we have, on "m"
598 * and we insert headers accordingly.
599 * Finally, we should be getting:
600 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload].
602 * During the header composing process "m" points to IPv6
603 * header. "mprev" points to an extension header prior to esp.
608 * We treat dest2 specially. This makes IPsec processing
609 * much easier. The goal here is to make mprev point the
610 * mbuf prior to dest2.
612 * Result: IPv6 dest2 payload.
613 * m and mprev will point to IPv6 header.
615 if (exthdrs.ip6e_dest2) {
617 panic("%s:%d: assumption failed: "
618 "hdr not split: hdrsplit %d exthdrs %p",
619 __func__, __LINE__, hdrsplit, &exthdrs);
620 exthdrs.ip6e_dest2->m_next = m->m_next;
621 m->m_next = exthdrs.ip6e_dest2;
622 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
623 ip6->ip6_nxt = IPPROTO_DSTOPTS;
627 * Result: IPv6 hbh dest1 rthdr dest2 payload.
628 * m will point to IPv6 header. mprev will point to the
629 * extension header prior to dest2 (rthdr in the above case).
631 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
632 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
634 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
638 IP6STAT_INC(ip6s_localout);
642 if (opt && opt->ip6po_rthdr)
643 ro = &opt->ip6po_route;
645 dst = (struct sockaddr_in6 *)&ro->ro_dst;
648 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
652 * If specified, try to fill in the traffic class field.
653 * Do not override if a non-zero value is already set.
654 * We check the diffserv field and the ECN field separately.
656 if (opt && opt->ip6po_tclass >= 0) {
659 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
661 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
664 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
667 /* Fill in or override the hop limit field, if necessary. */
668 if (opt && opt->ip6po_hlim != -1)
669 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
670 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
672 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
674 ip6->ip6_hlim = V_ip6_defmcasthlim;
677 if (ro == NULL || ro->ro_nh == NULL) {
678 bzero(dst, sizeof(*dst));
679 dst->sin6_family = AF_INET6;
680 dst->sin6_len = sizeof(*dst);
681 dst->sin6_addr = ip6->ip6_dst;
684 * Validate route against routing table changes.
685 * Make sure that the address family is set in route.
691 if (ro->ro_nh != NULL && inp != NULL) {
692 ro->ro_dst.sin6_family = AF_INET6; /* XXX KASSERT? */
693 NH_VALIDATE((struct route *)ro, &inp->inp_rt_cookie,
696 if (ro->ro_nh != NULL && fwd_tag == NULL &&
697 (!NH_IS_VALID(ro->ro_nh) ||
698 ro->ro_dst.sin6_family != AF_INET6 ||
699 !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)))
700 RO_INVALIDATE_CACHE(ro);
702 if (ro->ro_nh != NULL && fwd_tag == NULL &&
703 ro->ro_dst.sin6_family == AF_INET6 &&
704 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
709 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
711 if (fwd_tag == NULL) {
712 bzero(&dst_sa, sizeof(dst_sa));
713 dst_sa.sin6_family = AF_INET6;
714 dst_sa.sin6_len = sizeof(dst_sa);
715 dst_sa.sin6_addr = ip6->ip6_dst;
717 error = in6_selectroute(&dst_sa, opt, im6o, ro, &ifp,
718 &nh, fibnum, m->m_pkthdr.flowid);
720 IP6STAT_INC(ip6s_noroute);
722 in6_ifstat_inc(ifp, ifs6_out_discard);
730 * If in6_selectroute() does not return a nexthop
731 * dst may not have been updated.
733 *dst = dst_sa; /* XXX */
735 if (nh->nh_flags & NHF_HOST)
737 ia = (struct in6_ifaddr *)(nh->nh_ifa);
738 counter_u64_add(nh->nh_pksent, 1);
741 struct nhop_object *nh;
742 struct in6_addr kdst;
745 if (fwd_tag == NULL) {
746 bzero(&dst_sa, sizeof(dst_sa));
747 dst_sa.sin6_family = AF_INET6;
748 dst_sa.sin6_len = sizeof(dst_sa);
749 dst_sa.sin6_addr = ip6->ip6_dst;
752 if (IN6_IS_ADDR_MULTICAST(&dst_sa.sin6_addr) &&
754 (ifp = im6o->im6o_multicast_ifp) != NULL) {
755 /* We do not need a route lookup. */
756 *dst = dst_sa; /* XXX */
760 in6_splitscope(&dst_sa.sin6_addr, &kdst, &scopeid);
762 if (IN6_IS_ADDR_MC_LINKLOCAL(&dst_sa.sin6_addr) ||
763 IN6_IS_ADDR_MC_NODELOCAL(&dst_sa.sin6_addr)) {
765 ifp = in6_getlinkifnet(scopeid);
767 error = EHOSTUNREACH;
770 *dst = dst_sa; /* XXX */
775 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE,
778 IP6STAT_INC(ip6s_noroute);
779 /* No ifp in6_ifstat_inc(ifp, ifs6_out_discard); */
780 error = EHOSTUNREACH;;
786 ia = ifatoia6(nh->nh_ifa);
787 if (nh->nh_flags & NHF_GATEWAY)
788 dst->sin6_addr = nh->gw6_sa.sin6_addr;
793 /* Then nh (for unicast) and ifp must be non-NULL valid values. */
794 if ((flags & IPV6_FORWARDING) == 0) {
795 /* XXX: the FORWARDING flag can be set for mrouting. */
796 in6_ifstat_inc(ifp, ifs6_out_request);
799 /* Setup data structures for scope ID checks. */
801 bzero(&src_sa, sizeof(src_sa));
802 src_sa.sin6_family = AF_INET6;
803 src_sa.sin6_len = sizeof(src_sa);
804 src_sa.sin6_addr = ip6->ip6_src;
807 /* Re-initialize to be sure. */
808 bzero(&dst_sa, sizeof(dst_sa));
809 dst_sa.sin6_family = AF_INET6;
810 dst_sa.sin6_len = sizeof(dst_sa);
811 dst_sa.sin6_addr = ip6->ip6_dst;
813 /* Check for valid scope ID. */
814 if (in6_setscope(&src0, ifp, &zone) == 0 &&
815 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
816 in6_setscope(&dst0, ifp, &zone) == 0 &&
817 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
819 * The outgoing interface is in the zone of the source
820 * and destination addresses.
822 * Because the loopback interface cannot receive
823 * packets with a different scope ID than its own,
824 * there is a trick to pretend the outgoing packet
825 * was received by the real network interface, by
826 * setting "origifp" different from "ifp". This is
827 * only allowed when "ifp" is a loopback network
828 * interface. Refer to code in nd6_output_ifp() for
834 * We should use ia_ifp to support the case of sending
835 * packets to an address of our own.
837 if (ia != NULL && ia->ia_ifp)
840 } else if ((ifp->if_flags & IFF_LOOPBACK) == 0 ||
841 sa6_recoverscope(&src_sa) != 0 ||
842 sa6_recoverscope(&dst_sa) != 0 ||
843 dst_sa.sin6_scope_id == 0 ||
844 (src_sa.sin6_scope_id != 0 &&
845 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
846 (origifp = ifnet_byindex(dst_sa.sin6_scope_id)) == NULL) {
848 * If the destination network interface is not a
849 * loopback interface, or the destination network
850 * address has no scope ID, or the source address has
851 * a scope ID set which is different from the
852 * destination address one, or there is no network
853 * interface representing this scope ID, the address
854 * pair is considered invalid.
856 IP6STAT_INC(ip6s_badscope);
857 in6_ifstat_inc(ifp, ifs6_out_discard);
859 error = EHOSTUNREACH; /* XXX */
862 /* All scope ID checks are successful. */
864 if (nh && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
865 if (opt && opt->ip6po_nextroute.ro_nh) {
867 * The nexthop is explicitly specified by the
868 * application. We assume the next hop is an IPv6
871 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
873 else if ((nh->nh_flags & NHF_GATEWAY))
877 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
878 m->m_flags &= ~(M_BCAST | M_MCAST); /* Just in case. */
880 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
881 in6_ifstat_inc(ifp, ifs6_out_mcast);
883 /* Confirm that the outgoing interface supports multicast. */
884 if (!(ifp->if_flags & IFF_MULTICAST)) {
885 IP6STAT_INC(ip6s_noroute);
886 in6_ifstat_inc(ifp, ifs6_out_discard);
890 if ((im6o == NULL && in6_mcast_loop) ||
891 (im6o && im6o->im6o_multicast_loop)) {
893 * Loop back multicast datagram if not expressly
894 * forbidden to do so, even if we have not joined
895 * the address; protocols will filter it later,
896 * thus deferring a hash lookup and lock acquisition
897 * at the expense of an m_copym().
899 ip6_mloopback(ifp, m);
902 * If we are acting as a multicast router, perform
903 * multicast forwarding as if the packet had just
904 * arrived on the interface to which we are about
905 * to send. The multicast forwarding function
906 * recursively calls this function, using the
907 * IPV6_FORWARDING flag to prevent infinite recursion.
909 * Multicasts that are looped back by ip6_mloopback(),
910 * above, will be forwarded by the ip6_input() routine,
913 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
915 * XXX: ip6_mforward expects that rcvif is NULL
916 * when it is called from the originating path.
917 * However, it may not always be the case.
919 m->m_pkthdr.rcvif = NULL;
920 if (ip6_mforward(ip6, ifp, m) != 0) {
927 * Multicasts with a hoplimit of zero may be looped back,
928 * above, but must not be transmitted on a network.
929 * Also, multicasts addressed to the loopback interface
930 * are not sent -- the above call to ip6_mloopback() will
931 * loop back a copy if this host actually belongs to the
932 * destination group on the loopback interface.
934 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
935 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
942 * Fill the outgoing inteface to tell the upper layer
943 * to increment per-interface statistics.
948 /* Determine path MTU. */
949 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
950 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
952 KASSERT(mtu > 0, ("%s:%d: mtu %ld, ro_pmtu %p ro %p ifp %p "
953 "alwaysfrag %d fibnum %u\n", __func__, __LINE__, mtu, ro_pmtu, ro,
954 ifp, alwaysfrag, fibnum));
957 * The caller of this function may specify to use the minimum MTU
959 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
960 * setting. The logic is a bit complicated; by default, unicast
961 * packets will follow path MTU while multicast packets will be sent at
962 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
963 * including unicast ones will be sent at the minimum MTU. Multicast
964 * packets will always be sent at the minimum MTU unless
965 * IP6PO_MINMTU_DISABLE is explicitly specified.
966 * See RFC 3542 for more details.
968 if (mtu > IPV6_MMTU) {
969 if ((flags & IPV6_MINMTU))
971 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
973 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
975 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
981 * Clear embedded scope identifiers if necessary.
982 * in6_clearscope() will touch the addresses only when necessary.
984 in6_clearscope(&ip6->ip6_src);
985 in6_clearscope(&ip6->ip6_dst);
988 * If the outgoing packet contains a hop-by-hop options header,
989 * it must be examined and processed even by the source node.
990 * (RFC 2460, section 4.)
992 if (exthdrs.ip6e_hbh) {
993 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
994 u_int32_t dummy; /* XXX unused */
995 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
998 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
999 panic("ip6e_hbh is not contiguous");
1002 * XXX: if we have to send an ICMPv6 error to the sender,
1003 * we need the M_LOOP flag since icmp6_error() expects
1004 * the IPv6 and the hop-by-hop options header are
1005 * contiguous unless the flag is set.
1007 m->m_flags |= M_LOOP;
1008 m->m_pkthdr.rcvif = ifp;
1009 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
1010 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
1011 &dummy, &plen) < 0) {
1012 /* m was already freed at this point. */
1013 error = EINVAL;/* better error? */
1016 m->m_flags &= ~M_LOOP; /* XXX */
1017 m->m_pkthdr.rcvif = NULL;
1020 /* Jump over all PFIL processing if hooks are not active. */
1021 if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
1024 odst = ip6->ip6_dst;
1025 /* Run through list of hooks for output packets. */
1026 switch (pfil_run_hooks(V_inet6_pfil_head, &m, ifp, PFIL_OUT, inp)) {
1028 ip6 = mtod(m, struct ip6_hdr *);
1038 /* See if destination IP address was changed by packet filter. */
1039 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
1040 m->m_flags |= M_SKIP_FIREWALL;
1041 /* If destination is now ourself drop to ip6_input(). */
1042 if (in6_localip(&ip6->ip6_dst)) {
1043 m->m_flags |= M_FASTFWD_OURS;
1044 if (m->m_pkthdr.rcvif == NULL)
1045 m->m_pkthdr.rcvif = V_loif;
1046 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1047 m->m_pkthdr.csum_flags |=
1048 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1049 m->m_pkthdr.csum_data = 0xffff;
1051 #if defined(SCTP) || defined(SCTP_SUPPORT)
1052 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1053 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1055 error = netisr_queue(NETISR_IPV6, m);
1059 RO_INVALIDATE_CACHE(ro);
1060 needfiblookup = 1; /* Redo the routing table lookup. */
1063 /* See if fib was changed by packet filter. */
1064 if (fibnum != M_GETFIB(m)) {
1065 m->m_flags |= M_SKIP_FIREWALL;
1066 fibnum = M_GETFIB(m);
1068 RO_INVALIDATE_CACHE(ro);
1074 /* See if local, if yes, send it to netisr. */
1075 if (m->m_flags & M_FASTFWD_OURS) {
1076 if (m->m_pkthdr.rcvif == NULL)
1077 m->m_pkthdr.rcvif = V_loif;
1078 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1079 m->m_pkthdr.csum_flags |=
1080 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1081 m->m_pkthdr.csum_data = 0xffff;
1083 #if defined(SCTP) || defined(SCTP_SUPPORT)
1084 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1085 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1087 error = netisr_queue(NETISR_IPV6, m);
1090 /* Or forward to some other address? */
1091 if ((m->m_flags & M_IP6_NEXTHOP) &&
1092 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
1094 dst = (struct sockaddr_in6 *)&ro->ro_dst;
1097 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
1098 m->m_flags |= M_SKIP_FIREWALL;
1099 m->m_flags &= ~M_IP6_NEXTHOP;
1100 m_tag_delete(m, fwd_tag);
1106 EVL_APPLY_PRI(m, vlan_pcp);
1108 * Send the packet to the outgoing interface.
1109 * If necessary, do IPv6 fragmentation before sending.
1111 * The logic here is rather complex:
1112 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
1113 * 1-a: send as is if tlen <= path mtu
1114 * 1-b: fragment if tlen > path mtu
1116 * 2: if user asks us not to fragment (dontfrag == 1)
1117 * 2-a: send as is if tlen <= interface mtu
1118 * 2-b: error if tlen > interface mtu
1120 * 3: if we always need to attach fragment header (alwaysfrag == 1)
1123 * 4: if dontfrag == 1 && alwaysfrag == 1
1124 * error, as we cannot handle this conflicting request.
1126 sw_csum = m->m_pkthdr.csum_flags;
1128 tso = ((sw_csum & ifp->if_hwassist &
1129 (CSUM_TSO | CSUM_INNER_TSO)) != 0) ? 1 : 0;
1130 sw_csum &= ~ifp->if_hwassist;
1134 * If we added extension headers, we will not do TSO and calculate the
1135 * checksums ourselves for now.
1136 * XXX-BZ Need a framework to know when the NIC can handle it, even
1139 error = ip6_output_delayed_csum(m, ifp, sw_csum, plen, optlen, false);
1142 /* XXX-BZ m->m_pkthdr.csum_flags &= ~ifp->if_hwassist; */
1143 tlen = m->m_pkthdr.len;
1145 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
1149 if (dontfrag && alwaysfrag) { /* Case 4. */
1150 /* Conflicting request - can't transmit. */
1154 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* Case 2-b. */
1156 * Even if the DONTFRAG option is specified, we cannot send the
1157 * packet when the data length is larger than the MTU of the
1158 * outgoing interface.
1159 * Notify the error by sending IPV6_PATHMTU ancillary data if
1160 * application wanted to know the MTU value. Also return an
1161 * error code (this is not described in the API spec).
1164 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
1169 /* Transmit packet without fragmentation. */
1170 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* Cases 1-a and 2-a. */
1171 struct in6_ifaddr *ia6;
1173 ip6 = mtod(m, struct ip6_hdr *);
1174 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1176 /* Record statistics for this interface address. */
1177 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
1178 counter_u64_add(ia6->ia_ifa.ifa_obytes,
1180 ifa_free(&ia6->ia_ifa);
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 error = ip6_output_delayed_csum(m, ifp, m->m_pkthdr.csum_flags,
1222 plen, optlen, true);
1227 * Change the next header field of the last header in the
1228 * unfragmentable part.
1230 if (exthdrs.ip6e_rthdr) {
1231 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1232 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1233 } else if (exthdrs.ip6e_dest1) {
1234 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1235 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1236 } else if (exthdrs.ip6e_hbh) {
1237 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1238 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1240 ip6 = mtod(m, struct ip6_hdr *);
1241 nextproto = ip6->ip6_nxt;
1242 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1246 * Loop through length of segment after first fragment,
1247 * make new header and copy data of each part and link onto
1251 id = htonl(ip6_randomid());
1252 error = ip6_fragment(ifp, m, unfragpartlen, nextproto,len, id);
1256 in6_ifstat_inc(ifp, ifs6_out_fragok);
1259 /* Remove leading garbage. */
1268 /* Record statistics for this interface address. */
1270 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1271 counter_u64_add(ia->ia_ifa.ifa_obytes,
1275 EVL_APPLY_PRI(m, vlan_pcp);
1276 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1283 IP6STAT_INC(ip6s_fragmented);
1289 m_freem(exthdrs.ip6e_hbh); /* m_freem() checks if mbuf is NULL. */
1290 m_freem(exthdrs.ip6e_dest1);
1291 m_freem(exthdrs.ip6e_rthdr);
1292 m_freem(exthdrs.ip6e_dest2);
1301 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1305 if (hlen > MCLBYTES)
1306 return (ENOBUFS); /* XXX */
1309 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1311 m = m_get(M_NOWAIT, MT_DATA);
1316 bcopy(hdr, mtod(m, caddr_t), hlen);
1323 * Insert jumbo payload option.
1326 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1332 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1335 * If there is no hop-by-hop options header, allocate new one.
1336 * If there is one but it doesn't have enough space to store the
1337 * jumbo payload option, allocate a cluster to store the whole options.
1338 * Otherwise, use it to store the options.
1340 if (exthdrs->ip6e_hbh == NULL) {
1341 mopt = m_get(M_NOWAIT, MT_DATA);
1344 mopt->m_len = JUMBOOPTLEN;
1345 optbuf = mtod(mopt, u_char *);
1346 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1347 exthdrs->ip6e_hbh = mopt;
1349 struct ip6_hbh *hbh;
1351 mopt = exthdrs->ip6e_hbh;
1352 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1355 * - exthdrs->ip6e_hbh is not referenced from places
1356 * other than exthdrs.
1357 * - exthdrs->ip6e_hbh is not an mbuf chain.
1359 int oldoptlen = mopt->m_len;
1363 * XXX: give up if the whole (new) hbh header does
1364 * not fit even in an mbuf cluster.
1366 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1370 * As a consequence, we must always prepare a cluster
1373 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1376 n->m_len = oldoptlen + JUMBOOPTLEN;
1377 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1379 optbuf = mtod(n, caddr_t) + oldoptlen;
1381 mopt = exthdrs->ip6e_hbh = n;
1383 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1384 mopt->m_len += JUMBOOPTLEN;
1386 optbuf[0] = IP6OPT_PADN;
1390 * Adjust the header length according to the pad and
1391 * the jumbo payload option.
1393 hbh = mtod(mopt, struct ip6_hbh *);
1394 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1397 /* fill in the option. */
1398 optbuf[2] = IP6OPT_JUMBO;
1400 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1401 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1403 /* finally, adjust the packet header length */
1404 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1411 * Insert fragment header and copy unfragmentable header portions.
1414 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1415 struct ip6_frag **frghdrp)
1417 struct mbuf *n, *mlast;
1419 if (hlen > sizeof(struct ip6_hdr)) {
1420 n = m_copym(m0, sizeof(struct ip6_hdr),
1421 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1428 /* Search for the last mbuf of unfragmentable part. */
1429 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1432 if (M_WRITABLE(mlast) &&
1433 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1434 /* use the trailing space of the last mbuf for the fragment hdr */
1435 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1437 mlast->m_len += sizeof(struct ip6_frag);
1438 m->m_pkthdr.len += sizeof(struct ip6_frag);
1440 /* allocate a new mbuf for the fragment header */
1443 mfrg = m_get(M_NOWAIT, MT_DATA);
1446 mfrg->m_len = sizeof(struct ip6_frag);
1447 *frghdrp = mtod(mfrg, struct ip6_frag *);
1448 mlast->m_next = mfrg;
1455 * Calculates IPv6 path mtu for destination @dst.
1456 * Resulting MTU is stored in @mtup.
1458 * Returns 0 on success.
1461 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1463 struct epoch_tracker et;
1464 struct nhop_object *nh;
1465 struct in6_addr kdst;
1469 in6_splitscope(dst, &kdst, &scopeid);
1471 NET_EPOCH_ENTER(et);
1472 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1474 error = ip6_calcmtu(nh->nh_ifp, dst, nh->nh_mtu, mtup, NULL, 0);
1476 error = EHOSTUNREACH;
1483 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1484 * and cached data in @ro_pmtu.
1485 * MTU from (successful) route lookup is saved (along with dst)
1486 * inside @ro_pmtu to avoid subsequent route lookups after packet
1487 * filter processing.
1489 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1490 * Returns 0 on success.
1493 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1494 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1495 int *alwaysfragp, u_int fibnum, u_int proto)
1497 struct nhop_object *nh;
1498 struct in6_addr kdst;
1500 struct sockaddr_in6 *sa6_dst, sin6;
1506 if (ro_pmtu == NULL || do_lookup) {
1508 * Here ro_pmtu has final destination address, while
1509 * ro might represent immediate destination.
1510 * Use ro_pmtu destination since mtu might differ.
1512 if (ro_pmtu != NULL) {
1513 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1514 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1515 ro_pmtu->ro_mtu = 0;
1519 if (ro_pmtu == NULL || ro_pmtu->ro_mtu == 0) {
1520 bzero(sa6_dst, sizeof(*sa6_dst));
1521 sa6_dst->sin6_family = AF_INET6;
1522 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1523 sa6_dst->sin6_addr = *dst;
1525 in6_splitscope(dst, &kdst, &scopeid);
1526 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1529 if (ro_pmtu != NULL)
1530 ro_pmtu->ro_mtu = mtu;
1533 mtu = ro_pmtu->ro_mtu;
1536 if (ro_pmtu != NULL && ro_pmtu->ro_nh != NULL)
1537 mtu = ro_pmtu->ro_nh->nh_mtu;
1539 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1543 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1544 * hostcache data for @dst.
1545 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1547 * Returns 0 on success.
1550 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1551 u_long *mtup, int *alwaysfragp, u_int proto)
1559 struct in_conninfo inc;
1561 bzero(&inc, sizeof(inc));
1562 inc.inc_flags |= INC_ISIPV6;
1563 inc.inc6_faddr = *dst;
1565 ifmtu = IN6_LINKMTU(ifp);
1567 /* TCP is known to react to pmtu changes so skip hc */
1568 if (proto != IPPROTO_TCP)
1569 mtu = tcp_hc_getmtu(&inc);
1572 mtu = min(mtu, rt_mtu);
1577 else if (mtu < IPV6_MMTU) {
1579 * RFC2460 section 5, last paragraph:
1580 * if we record ICMPv6 too big message with
1581 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1582 * or smaller, with framgent header attached.
1583 * (fragment header is needed regardless from the
1584 * packet size, for translators to identify packets)
1590 mtu = IN6_LINKMTU(ifp);
1592 error = EHOSTUNREACH; /* XXX */
1596 *alwaysfragp = alwaysfrag;
1601 * IP6 socket option processing.
1604 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1606 int optdatalen, uproto;
1608 struct inpcb *inp = sotoinpcb(so);
1610 int level, op, optname;
1614 uint32_t rss_bucket;
1619 * Don't use more than a quarter of mbuf clusters. N.B.:
1620 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1621 * on LP64 architectures, so cast to u_long to avoid undefined
1622 * behavior. ILP32 architectures cannot have nmbclusters
1623 * large enough to overflow for other reasons.
1625 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1627 level = sopt->sopt_level;
1628 op = sopt->sopt_dir;
1629 optname = sopt->sopt_name;
1630 optlen = sopt->sopt_valsize;
1634 uproto = (int)so->so_proto->pr_protocol;
1636 if (level != IPPROTO_IPV6) {
1639 if (sopt->sopt_level == SOL_SOCKET &&
1640 sopt->sopt_dir == SOPT_SET) {
1641 switch (sopt->sopt_name) {
1644 if ((so->so_options & SO_REUSEADDR) != 0)
1645 inp->inp_flags2 |= INP_REUSEADDR;
1647 inp->inp_flags2 &= ~INP_REUSEADDR;
1653 if ((so->so_options & SO_REUSEPORT) != 0)
1654 inp->inp_flags2 |= INP_REUSEPORT;
1656 inp->inp_flags2 &= ~INP_REUSEPORT;
1660 case SO_REUSEPORT_LB:
1662 if ((so->so_options & SO_REUSEPORT_LB) != 0)
1663 inp->inp_flags2 |= INP_REUSEPORT_LB;
1665 inp->inp_flags2 &= ~INP_REUSEPORT_LB;
1671 inp->inp_inc.inc_fibnum = so->so_fibnum;
1675 case SO_MAX_PACING_RATE:
1678 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1689 } else { /* level == IPPROTO_IPV6 */
1693 case IPV6_2292PKTOPTIONS:
1694 #ifdef IPV6_PKTOPTIONS
1695 case IPV6_PKTOPTIONS:
1700 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1701 printf("ip6_ctloutput: mbuf limit hit\n");
1706 error = soopt_getm(sopt, &m); /* XXX */
1709 error = soopt_mcopyin(sopt, m); /* XXX */
1713 error = ip6_pcbopts(&inp->in6p_outputopts, m,
1716 m_freem(m); /* XXX */
1721 * Use of some Hop-by-Hop options or some
1722 * Destination options, might require special
1723 * privilege. That is, normal applications
1724 * (without special privilege) might be forbidden
1725 * from setting certain options in outgoing packets,
1726 * and might never see certain options in received
1727 * packets. [RFC 2292 Section 6]
1728 * KAME specific note:
1729 * KAME prevents non-privileged users from sending or
1730 * receiving ANY hbh/dst options in order to avoid
1731 * overhead of parsing options in the kernel.
1733 case IPV6_RECVHOPOPTS:
1734 case IPV6_RECVDSTOPTS:
1735 case IPV6_RECVRTHDRDSTOPTS:
1737 error = priv_check(td,
1738 PRIV_NETINET_SETHDROPTS);
1743 case IPV6_UNICAST_HOPS:
1746 case IPV6_RECVPKTINFO:
1747 case IPV6_RECVHOPLIMIT:
1748 case IPV6_RECVRTHDR:
1749 case IPV6_RECVPATHMTU:
1750 case IPV6_RECVTCLASS:
1751 case IPV6_RECVFLOWID:
1753 case IPV6_RECVRSSBUCKETID:
1756 case IPV6_AUTOFLOWLABEL:
1757 case IPV6_ORIGDSTADDR:
1759 case IPV6_BINDMULTI:
1761 case IPV6_RSS_LISTEN_BUCKET:
1764 if (optname == IPV6_BINDANY && td != NULL) {
1765 error = priv_check(td,
1766 PRIV_NETINET_BINDANY);
1771 if (optlen != sizeof(int)) {
1775 error = sooptcopyin(sopt, &optval,
1776 sizeof optval, sizeof optval);
1780 case IPV6_UNICAST_HOPS:
1781 if (optval < -1 || optval >= 256)
1784 /* -1 = kernel default */
1785 inp->in6p_hops = optval;
1786 if ((inp->inp_vflag &
1788 inp->inp_ip_ttl = optval;
1791 #define OPTSET(bit) \
1795 inp->inp_flags |= (bit); \
1797 inp->inp_flags &= ~(bit); \
1799 } while (/*CONSTCOND*/ 0)
1800 #define OPTSET2292(bit) \
1803 inp->inp_flags |= IN6P_RFC2292; \
1805 inp->inp_flags |= (bit); \
1807 inp->inp_flags &= ~(bit); \
1809 } while (/*CONSTCOND*/ 0)
1810 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0)
1812 #define OPTSET2_N(bit, val) do { \
1814 inp->inp_flags2 |= bit; \
1816 inp->inp_flags2 &= ~bit; \
1818 #define OPTSET2(bit, val) do { \
1820 OPTSET2_N(bit, val); \
1823 #define OPTBIT2(bit) (inp->inp_flags2 & (bit) ? 1 : 0)
1824 #define OPTSET2292_EXCLUSIVE(bit) \
1827 if (OPTBIT(IN6P_RFC2292)) { \
1831 inp->inp_flags |= (bit); \
1833 inp->inp_flags &= ~(bit); \
1836 } while (/*CONSTCOND*/ 0)
1838 case IPV6_RECVPKTINFO:
1839 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1844 struct ip6_pktopts **optp;
1846 /* cannot mix with RFC2292 */
1847 if (OPTBIT(IN6P_RFC2292)) {
1852 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1854 return (ECONNRESET);
1856 optp = &inp->in6p_outputopts;
1857 error = ip6_pcbopt(IPV6_HOPLIMIT,
1858 (u_char *)&optval, sizeof(optval),
1859 optp, (td != NULL) ? td->td_ucred :
1865 case IPV6_RECVHOPLIMIT:
1866 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1869 case IPV6_RECVHOPOPTS:
1870 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1873 case IPV6_RECVDSTOPTS:
1874 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1877 case IPV6_RECVRTHDRDSTOPTS:
1878 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1881 case IPV6_RECVRTHDR:
1882 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1885 case IPV6_RECVPATHMTU:
1887 * We ignore this option for TCP
1889 * (RFC3542 leaves this case
1892 if (uproto != IPPROTO_TCP)
1896 case IPV6_RECVFLOWID:
1897 OPTSET2(INP_RECVFLOWID, optval);
1901 case IPV6_RECVRSSBUCKETID:
1902 OPTSET2(INP_RECVRSSBUCKETID, optval);
1908 if (inp->inp_lport ||
1909 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1911 * The socket is already bound.
1918 inp->inp_flags |= IN6P_IPV6_V6ONLY;
1919 inp->inp_vflag &= ~INP_IPV4;
1921 inp->inp_flags &= ~IN6P_IPV6_V6ONLY;
1922 inp->inp_vflag |= INP_IPV4;
1926 case IPV6_RECVTCLASS:
1927 /* cannot mix with RFC2292 XXX */
1928 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1930 case IPV6_AUTOFLOWLABEL:
1931 OPTSET(IN6P_AUTOFLOWLABEL);
1934 case IPV6_ORIGDSTADDR:
1935 OPTSET2(INP_ORIGDSTADDR, optval);
1938 OPTSET(INP_BINDANY);
1941 case IPV6_BINDMULTI:
1942 OPTSET2(INP_BINDMULTI, optval);
1945 case IPV6_RSS_LISTEN_BUCKET:
1946 if ((optval >= 0) &&
1947 (optval < rss_getnumbuckets())) {
1949 inp->inp_rss_listen_bucket = optval;
1950 OPTSET2_N(INP_RSS_BUCKET_SET, 1);
1958 if ((optval >= -1) && (optval <=
1959 (INP_2PCP_MASK >> INP_2PCP_SHIFT))) {
1985 case IPV6_USE_MIN_MTU:
1986 case IPV6_PREFER_TEMPADDR:
1987 if (optlen != sizeof(optval)) {
1991 error = sooptcopyin(sopt, &optval,
1992 sizeof optval, sizeof optval);
1996 struct ip6_pktopts **optp;
1998 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2000 return (ECONNRESET);
2002 optp = &inp->in6p_outputopts;
2003 error = ip6_pcbopt(optname,
2004 (u_char *)&optval, sizeof(optval),
2005 optp, (td != NULL) ? td->td_ucred :
2011 case IPV6_2292PKTINFO:
2012 case IPV6_2292HOPLIMIT:
2013 case IPV6_2292HOPOPTS:
2014 case IPV6_2292DSTOPTS:
2015 case IPV6_2292RTHDR:
2017 if (optlen != sizeof(int)) {
2021 error = sooptcopyin(sopt, &optval,
2022 sizeof optval, sizeof optval);
2026 case IPV6_2292PKTINFO:
2027 OPTSET2292(IN6P_PKTINFO);
2029 case IPV6_2292HOPLIMIT:
2030 OPTSET2292(IN6P_HOPLIMIT);
2032 case IPV6_2292HOPOPTS:
2034 * Check super-user privilege.
2035 * See comments for IPV6_RECVHOPOPTS.
2038 error = priv_check(td,
2039 PRIV_NETINET_SETHDROPTS);
2043 OPTSET2292(IN6P_HOPOPTS);
2045 case IPV6_2292DSTOPTS:
2047 error = priv_check(td,
2048 PRIV_NETINET_SETHDROPTS);
2052 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
2054 case IPV6_2292RTHDR:
2055 OPTSET2292(IN6P_RTHDR);
2063 case IPV6_RTHDRDSTOPTS:
2066 /* new advanced API (RFC3542) */
2068 u_char optbuf_storage[MCLBYTES];
2070 struct ip6_pktopts **optp;
2072 /* cannot mix with RFC2292 */
2073 if (OPTBIT(IN6P_RFC2292)) {
2079 * We only ensure valsize is not too large
2080 * here. Further validation will be done
2083 error = sooptcopyin(sopt, optbuf_storage,
2084 sizeof(optbuf_storage), 0);
2087 optlen = sopt->sopt_valsize;
2088 optbuf = optbuf_storage;
2090 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2092 return (ECONNRESET);
2094 optp = &inp->in6p_outputopts;
2095 error = ip6_pcbopt(optname, optbuf, optlen,
2096 optp, (td != NULL) ? td->td_ucred : NULL,
2103 case IPV6_MULTICAST_IF:
2104 case IPV6_MULTICAST_HOPS:
2105 case IPV6_MULTICAST_LOOP:
2106 case IPV6_JOIN_GROUP:
2107 case IPV6_LEAVE_GROUP:
2109 case MCAST_BLOCK_SOURCE:
2110 case MCAST_UNBLOCK_SOURCE:
2111 case MCAST_JOIN_GROUP:
2112 case MCAST_LEAVE_GROUP:
2113 case MCAST_JOIN_SOURCE_GROUP:
2114 case MCAST_LEAVE_SOURCE_GROUP:
2115 error = ip6_setmoptions(inp, sopt);
2118 case IPV6_PORTRANGE:
2119 error = sooptcopyin(sopt, &optval,
2120 sizeof optval, sizeof optval);
2126 case IPV6_PORTRANGE_DEFAULT:
2127 inp->inp_flags &= ~(INP_LOWPORT);
2128 inp->inp_flags &= ~(INP_HIGHPORT);
2131 case IPV6_PORTRANGE_HIGH:
2132 inp->inp_flags &= ~(INP_LOWPORT);
2133 inp->inp_flags |= INP_HIGHPORT;
2136 case IPV6_PORTRANGE_LOW:
2137 inp->inp_flags &= ~(INP_HIGHPORT);
2138 inp->inp_flags |= INP_LOWPORT;
2148 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2149 case IPV6_IPSEC_POLICY:
2150 if (IPSEC_ENABLED(ipv6)) {
2151 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2158 error = ENOPROTOOPT;
2165 case IPV6_2292PKTOPTIONS:
2166 #ifdef IPV6_PKTOPTIONS
2167 case IPV6_PKTOPTIONS:
2170 * RFC3542 (effectively) deprecated the
2171 * semantics of the 2292-style pktoptions.
2172 * Since it was not reliable in nature (i.e.,
2173 * applications had to expect the lack of some
2174 * information after all), it would make sense
2175 * to simplify this part by always returning
2178 sopt->sopt_valsize = 0;
2181 case IPV6_RECVHOPOPTS:
2182 case IPV6_RECVDSTOPTS:
2183 case IPV6_RECVRTHDRDSTOPTS:
2184 case IPV6_UNICAST_HOPS:
2185 case IPV6_RECVPKTINFO:
2186 case IPV6_RECVHOPLIMIT:
2187 case IPV6_RECVRTHDR:
2188 case IPV6_RECVPATHMTU:
2191 case IPV6_PORTRANGE:
2192 case IPV6_RECVTCLASS:
2193 case IPV6_AUTOFLOWLABEL:
2197 case IPV6_RECVFLOWID:
2199 case IPV6_RSSBUCKETID:
2200 case IPV6_RECVRSSBUCKETID:
2202 case IPV6_BINDMULTI:
2205 case IPV6_RECVHOPOPTS:
2206 optval = OPTBIT(IN6P_HOPOPTS);
2209 case IPV6_RECVDSTOPTS:
2210 optval = OPTBIT(IN6P_DSTOPTS);
2213 case IPV6_RECVRTHDRDSTOPTS:
2214 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2217 case IPV6_UNICAST_HOPS:
2218 optval = inp->in6p_hops;
2221 case IPV6_RECVPKTINFO:
2222 optval = OPTBIT(IN6P_PKTINFO);
2225 case IPV6_RECVHOPLIMIT:
2226 optval = OPTBIT(IN6P_HOPLIMIT);
2229 case IPV6_RECVRTHDR:
2230 optval = OPTBIT(IN6P_RTHDR);
2233 case IPV6_RECVPATHMTU:
2234 optval = OPTBIT(IN6P_MTU);
2238 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2241 case IPV6_PORTRANGE:
2244 flags = inp->inp_flags;
2245 if (flags & INP_HIGHPORT)
2246 optval = IPV6_PORTRANGE_HIGH;
2247 else if (flags & INP_LOWPORT)
2248 optval = IPV6_PORTRANGE_LOW;
2253 case IPV6_RECVTCLASS:
2254 optval = OPTBIT(IN6P_TCLASS);
2257 case IPV6_AUTOFLOWLABEL:
2258 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2261 case IPV6_ORIGDSTADDR:
2262 optval = OPTBIT2(INP_ORIGDSTADDR);
2266 optval = OPTBIT(INP_BINDANY);
2270 optval = inp->inp_flowid;
2274 optval = inp->inp_flowtype;
2277 case IPV6_RECVFLOWID:
2278 optval = OPTBIT2(INP_RECVFLOWID);
2281 case IPV6_RSSBUCKETID:
2283 rss_hash2bucket(inp->inp_flowid,
2287 optval = rss_bucket;
2292 case IPV6_RECVRSSBUCKETID:
2293 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2297 case IPV6_BINDMULTI:
2298 optval = OPTBIT2(INP_BINDMULTI);
2302 if (OPTBIT2(INP_2PCP_SET)) {
2303 optval = (inp->inp_flags2 &
2314 error = sooptcopyout(sopt, &optval,
2321 struct ip6_mtuinfo mtuinfo;
2322 struct in6_addr addr;
2324 if (!(so->so_state & SS_ISCONNECTED))
2327 * XXX: we dot not consider the case of source
2328 * routing, or optional information to specify
2329 * the outgoing interface.
2330 * Copy faddr out of inp to avoid holding lock
2331 * on inp during route lookup.
2334 bcopy(&inp->in6p_faddr, &addr, sizeof(addr));
2336 error = ip6_getpmtu_ctl(so->so_fibnum,
2340 if (pmtu > IPV6_MAXPACKET)
2341 pmtu = IPV6_MAXPACKET;
2343 bzero(&mtuinfo, sizeof(mtuinfo));
2344 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2345 optdata = (void *)&mtuinfo;
2346 optdatalen = sizeof(mtuinfo);
2347 error = sooptcopyout(sopt, optdata,
2352 case IPV6_2292PKTINFO:
2353 case IPV6_2292HOPLIMIT:
2354 case IPV6_2292HOPOPTS:
2355 case IPV6_2292RTHDR:
2356 case IPV6_2292DSTOPTS:
2358 case IPV6_2292PKTINFO:
2359 optval = OPTBIT(IN6P_PKTINFO);
2361 case IPV6_2292HOPLIMIT:
2362 optval = OPTBIT(IN6P_HOPLIMIT);
2364 case IPV6_2292HOPOPTS:
2365 optval = OPTBIT(IN6P_HOPOPTS);
2367 case IPV6_2292RTHDR:
2368 optval = OPTBIT(IN6P_RTHDR);
2370 case IPV6_2292DSTOPTS:
2371 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2374 error = sooptcopyout(sopt, &optval,
2381 case IPV6_RTHDRDSTOPTS:
2385 case IPV6_USE_MIN_MTU:
2386 case IPV6_PREFER_TEMPADDR:
2387 error = ip6_getpcbopt(inp, optname, sopt);
2390 case IPV6_MULTICAST_IF:
2391 case IPV6_MULTICAST_HOPS:
2392 case IPV6_MULTICAST_LOOP:
2394 error = ip6_getmoptions(inp, sopt);
2397 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2398 case IPV6_IPSEC_POLICY:
2399 if (IPSEC_ENABLED(ipv6)) {
2400 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2406 error = ENOPROTOOPT;
2416 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2418 int error = 0, optval, optlen;
2419 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2420 struct inpcb *inp = sotoinpcb(so);
2421 int level, op, optname;
2423 level = sopt->sopt_level;
2424 op = sopt->sopt_dir;
2425 optname = sopt->sopt_name;
2426 optlen = sopt->sopt_valsize;
2428 if (level != IPPROTO_IPV6) {
2435 * For ICMPv6 sockets, no modification allowed for checksum
2436 * offset, permit "no change" values to help existing apps.
2438 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2439 * for an ICMPv6 socket will fail."
2440 * The current behavior does not meet RFC3542.
2444 if (optlen != sizeof(int)) {
2448 error = sooptcopyin(sopt, &optval, sizeof(optval),
2452 if (optval < -1 || (optval % 2) != 0) {
2454 * The API assumes non-negative even offset
2455 * values or -1 as a special value.
2458 } else if (so->so_proto->pr_protocol ==
2460 if (optval != icmp6off)
2463 inp->in6p_cksum = optval;
2467 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2470 optval = inp->in6p_cksum;
2472 error = sooptcopyout(sopt, &optval, sizeof(optval));
2482 error = ENOPROTOOPT;
2490 * Set up IP6 options in pcb for insertion in output packets or
2491 * specifying behavior of outgoing packets.
2494 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2495 struct socket *so, struct sockopt *sopt)
2497 struct ip6_pktopts *opt = *pktopt;
2499 struct thread *td = sopt->sopt_td;
2501 /* turn off any old options. */
2504 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2505 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2506 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2507 printf("ip6_pcbopts: all specified options are cleared.\n");
2509 ip6_clearpktopts(opt, -1);
2511 opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT);
2517 if (!m || m->m_len == 0) {
2519 * Only turning off any previous options, regardless of
2520 * whether the opt is just created or given.
2522 free(opt, M_IP6OPT);
2526 /* set options specified by user. */
2527 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2528 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2529 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2530 free(opt, M_IP6OPT);
2538 * initialize ip6_pktopts. beware that there are non-zero default values in
2542 ip6_initpktopts(struct ip6_pktopts *opt)
2545 bzero(opt, sizeof(*opt));
2546 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2547 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2548 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2549 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2553 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2554 struct ucred *cred, int uproto)
2556 struct ip6_pktopts *opt;
2558 if (*pktopt == NULL) {
2559 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2561 if (*pktopt == NULL)
2563 ip6_initpktopts(*pktopt);
2567 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2570 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2571 if (pktopt && pktopt->field) { \
2573 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2574 malloc_optdata = true; \
2576 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
2578 free(optdata, M_TEMP); \
2579 return (ECONNRESET); \
2581 pktopt = inp->in6p_outputopts; \
2582 if (pktopt && pktopt->field) { \
2583 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2584 bcopy(&pktopt->field, optdata, optdatalen); \
2586 free(optdata, M_TEMP); \
2588 malloc_optdata = false; \
2593 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2594 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2596 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2597 pktopt->field->sa_len)
2600 ip6_getpcbopt(struct inpcb *inp, int optname, struct sockopt *sopt)
2602 void *optdata = NULL;
2603 bool malloc_optdata = false;
2606 struct in6_pktinfo null_pktinfo;
2607 int deftclass = 0, on;
2608 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2609 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2610 struct ip6_pktopts *pktopt;
2613 pktopt = inp->in6p_outputopts;
2617 optdata = (void *)&null_pktinfo;
2618 if (pktopt && pktopt->ip6po_pktinfo) {
2619 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2620 sizeof(null_pktinfo));
2621 in6_clearscope(&null_pktinfo.ipi6_addr);
2623 /* XXX: we don't have to do this every time... */
2624 bzero(&null_pktinfo, sizeof(null_pktinfo));
2626 optdatalen = sizeof(struct in6_pktinfo);
2629 if (pktopt && pktopt->ip6po_tclass >= 0)
2630 deftclass = pktopt->ip6po_tclass;
2631 optdata = (void *)&deftclass;
2632 optdatalen = sizeof(int);
2635 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2638 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2640 case IPV6_RTHDRDSTOPTS:
2641 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2644 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2647 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2649 case IPV6_USE_MIN_MTU:
2651 defminmtu = pktopt->ip6po_minmtu;
2652 optdata = (void *)&defminmtu;
2653 optdatalen = sizeof(int);
2656 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2660 optdata = (void *)&on;
2661 optdatalen = sizeof(on);
2663 case IPV6_PREFER_TEMPADDR:
2665 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2666 optdata = (void *)&defpreftemp;
2667 optdatalen = sizeof(int);
2669 default: /* should not happen */
2671 panic("ip6_getpcbopt: unexpected option\n");
2674 return (ENOPROTOOPT);
2678 error = sooptcopyout(sopt, optdata, optdatalen);
2680 free(optdata, M_TEMP);
2686 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2691 if (optname == -1 || optname == IPV6_PKTINFO) {
2692 if (pktopt->ip6po_pktinfo)
2693 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2694 pktopt->ip6po_pktinfo = NULL;
2696 if (optname == -1 || optname == IPV6_HOPLIMIT)
2697 pktopt->ip6po_hlim = -1;
2698 if (optname == -1 || optname == IPV6_TCLASS)
2699 pktopt->ip6po_tclass = -1;
2700 if (optname == -1 || optname == IPV6_NEXTHOP) {
2701 if (pktopt->ip6po_nextroute.ro_nh) {
2702 NH_FREE(pktopt->ip6po_nextroute.ro_nh);
2703 pktopt->ip6po_nextroute.ro_nh = NULL;
2705 if (pktopt->ip6po_nexthop)
2706 free(pktopt->ip6po_nexthop, M_IP6OPT);
2707 pktopt->ip6po_nexthop = NULL;
2709 if (optname == -1 || optname == IPV6_HOPOPTS) {
2710 if (pktopt->ip6po_hbh)
2711 free(pktopt->ip6po_hbh, M_IP6OPT);
2712 pktopt->ip6po_hbh = NULL;
2714 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2715 if (pktopt->ip6po_dest1)
2716 free(pktopt->ip6po_dest1, M_IP6OPT);
2717 pktopt->ip6po_dest1 = NULL;
2719 if (optname == -1 || optname == IPV6_RTHDR) {
2720 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2721 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2722 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2723 if (pktopt->ip6po_route.ro_nh) {
2724 NH_FREE(pktopt->ip6po_route.ro_nh);
2725 pktopt->ip6po_route.ro_nh = NULL;
2728 if (optname == -1 || optname == IPV6_DSTOPTS) {
2729 if (pktopt->ip6po_dest2)
2730 free(pktopt->ip6po_dest2, M_IP6OPT);
2731 pktopt->ip6po_dest2 = NULL;
2735 #define PKTOPT_EXTHDRCPY(type) \
2738 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2739 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2740 if (dst->type == NULL)\
2742 bcopy(src->type, dst->type, hlen);\
2744 } while (/*CONSTCOND*/ 0)
2747 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2749 if (dst == NULL || src == NULL) {
2750 printf("ip6_clearpktopts: invalid argument\n");
2754 dst->ip6po_hlim = src->ip6po_hlim;
2755 dst->ip6po_tclass = src->ip6po_tclass;
2756 dst->ip6po_flags = src->ip6po_flags;
2757 dst->ip6po_minmtu = src->ip6po_minmtu;
2758 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2759 if (src->ip6po_pktinfo) {
2760 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2762 if (dst->ip6po_pktinfo == NULL)
2764 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2766 if (src->ip6po_nexthop) {
2767 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2769 if (dst->ip6po_nexthop == NULL)
2771 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2772 src->ip6po_nexthop->sa_len);
2774 PKTOPT_EXTHDRCPY(ip6po_hbh);
2775 PKTOPT_EXTHDRCPY(ip6po_dest1);
2776 PKTOPT_EXTHDRCPY(ip6po_dest2);
2777 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2781 ip6_clearpktopts(dst, -1);
2784 #undef PKTOPT_EXTHDRCPY
2786 struct ip6_pktopts *
2787 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2790 struct ip6_pktopts *dst;
2792 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2795 ip6_initpktopts(dst);
2797 if ((error = copypktopts(dst, src, canwait)) != 0) {
2798 free(dst, M_IP6OPT);
2806 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2811 ip6_clearpktopts(pktopt, -1);
2813 free(pktopt, M_IP6OPT);
2817 * Set IPv6 outgoing packet options based on advanced API.
2820 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2821 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2823 struct cmsghdr *cm = NULL;
2825 if (control == NULL || opt == NULL)
2828 ip6_initpktopts(opt);
2833 * If stickyopt is provided, make a local copy of the options
2834 * for this particular packet, then override them by ancillary
2836 * XXX: copypktopts() does not copy the cached route to a next
2837 * hop (if any). This is not very good in terms of efficiency,
2838 * but we can allow this since this option should be rarely
2841 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2846 * XXX: Currently, we assume all the optional information is stored
2849 if (control->m_next)
2852 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2853 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2856 if (control->m_len < CMSG_LEN(0))
2859 cm = mtod(control, struct cmsghdr *);
2860 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2862 if (cm->cmsg_level != IPPROTO_IPV6)
2865 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2866 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2875 * Set a particular packet option, as a sticky option or an ancillary data
2876 * item. "len" can be 0 only when it's a sticky option.
2877 * We have 4 cases of combination of "sticky" and "cmsg":
2878 * "sticky=0, cmsg=0": impossible
2879 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2880 * "sticky=1, cmsg=0": RFC3542 socket option
2881 * "sticky=1, cmsg=1": RFC2292 socket option
2884 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2885 struct ucred *cred, int sticky, int cmsg, int uproto)
2887 int minmtupolicy, preftemp;
2890 if (!sticky && !cmsg) {
2892 printf("ip6_setpktopt: impossible case\n");
2898 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2899 * not be specified in the context of RFC3542. Conversely,
2900 * RFC3542 types should not be specified in the context of RFC2292.
2904 case IPV6_2292PKTINFO:
2905 case IPV6_2292HOPLIMIT:
2906 case IPV6_2292NEXTHOP:
2907 case IPV6_2292HOPOPTS:
2908 case IPV6_2292DSTOPTS:
2909 case IPV6_2292RTHDR:
2910 case IPV6_2292PKTOPTIONS:
2911 return (ENOPROTOOPT);
2914 if (sticky && cmsg) {
2921 case IPV6_RTHDRDSTOPTS:
2923 case IPV6_USE_MIN_MTU:
2926 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2927 return (ENOPROTOOPT);
2932 case IPV6_2292PKTINFO:
2935 struct ifnet *ifp = NULL;
2936 struct in6_pktinfo *pktinfo;
2938 if (len != sizeof(struct in6_pktinfo))
2941 pktinfo = (struct in6_pktinfo *)buf;
2944 * An application can clear any sticky IPV6_PKTINFO option by
2945 * doing a "regular" setsockopt with ipi6_addr being
2946 * in6addr_any and ipi6_ifindex being zero.
2947 * [RFC 3542, Section 6]
2949 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2950 pktinfo->ipi6_ifindex == 0 &&
2951 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2952 ip6_clearpktopts(opt, optname);
2956 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2957 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2960 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2962 /* validate the interface index if specified. */
2963 if (pktinfo->ipi6_ifindex > V_if_index)
2965 if (pktinfo->ipi6_ifindex) {
2966 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2970 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2971 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2975 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2976 struct in6_ifaddr *ia;
2978 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2979 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2981 return (EADDRNOTAVAIL);
2982 ifa_free(&ia->ia_ifa);
2985 * We store the address anyway, and let in6_selectsrc()
2986 * validate the specified address. This is because ipi6_addr
2987 * may not have enough information about its scope zone, and
2988 * we may need additional information (such as outgoing
2989 * interface or the scope zone of a destination address) to
2990 * disambiguate the scope.
2991 * XXX: the delay of the validation may confuse the
2992 * application when it is used as a sticky option.
2994 if (opt->ip6po_pktinfo == NULL) {
2995 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2996 M_IP6OPT, M_NOWAIT);
2997 if (opt->ip6po_pktinfo == NULL)
3000 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
3004 case IPV6_2292HOPLIMIT:
3010 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3011 * to simplify the ordering among hoplimit options.
3013 if (optname == IPV6_HOPLIMIT && sticky)
3014 return (ENOPROTOOPT);
3016 if (len != sizeof(int))
3019 if (*hlimp < -1 || *hlimp > 255)
3022 opt->ip6po_hlim = *hlimp;
3030 if (len != sizeof(int))
3032 tclass = *(int *)buf;
3033 if (tclass < -1 || tclass > 255)
3036 opt->ip6po_tclass = tclass;
3040 case IPV6_2292NEXTHOP:
3043 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3048 if (len == 0) { /* just remove the option */
3049 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3053 /* check if cmsg_len is large enough for sa_len */
3054 if (len < sizeof(struct sockaddr) || len < *buf)
3057 switch (((struct sockaddr *)buf)->sa_family) {
3060 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3063 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3066 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3067 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3070 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
3076 case AF_LINK: /* should eventually be supported */
3078 return (EAFNOSUPPORT);
3081 /* turn off the previous option, then set the new option. */
3082 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3083 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3084 if (opt->ip6po_nexthop == NULL)
3086 bcopy(buf, opt->ip6po_nexthop, *buf);
3089 case IPV6_2292HOPOPTS:
3092 struct ip6_hbh *hbh;
3096 * XXX: We don't allow a non-privileged user to set ANY HbH
3097 * options, since per-option restriction has too much
3101 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3107 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3108 break; /* just remove the option */
3111 /* message length validation */
3112 if (len < sizeof(struct ip6_hbh))
3114 hbh = (struct ip6_hbh *)buf;
3115 hbhlen = (hbh->ip6h_len + 1) << 3;
3119 /* turn off the previous option, then set the new option. */
3120 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3121 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3122 if (opt->ip6po_hbh == NULL)
3124 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3129 case IPV6_2292DSTOPTS:
3131 case IPV6_RTHDRDSTOPTS:
3133 struct ip6_dest *dest, **newdest = NULL;
3136 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
3137 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3143 ip6_clearpktopts(opt, optname);
3144 break; /* just remove the option */
3147 /* message length validation */
3148 if (len < sizeof(struct ip6_dest))
3150 dest = (struct ip6_dest *)buf;
3151 destlen = (dest->ip6d_len + 1) << 3;
3156 * Determine the position that the destination options header
3157 * should be inserted; before or after the routing header.
3160 case IPV6_2292DSTOPTS:
3162 * The old advacned API is ambiguous on this point.
3163 * Our approach is to determine the position based
3164 * according to the existence of a routing header.
3165 * Note, however, that this depends on the order of the
3166 * extension headers in the ancillary data; the 1st
3167 * part of the destination options header must appear
3168 * before the routing header in the ancillary data,
3170 * RFC3542 solved the ambiguity by introducing
3171 * separate ancillary data or option types.
3173 if (opt->ip6po_rthdr == NULL)
3174 newdest = &opt->ip6po_dest1;
3176 newdest = &opt->ip6po_dest2;
3178 case IPV6_RTHDRDSTOPTS:
3179 newdest = &opt->ip6po_dest1;
3182 newdest = &opt->ip6po_dest2;
3186 /* turn off the previous option, then set the new option. */
3187 ip6_clearpktopts(opt, optname);
3188 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3189 if (*newdest == NULL)
3191 bcopy(dest, *newdest, destlen);
3196 case IPV6_2292RTHDR:
3199 struct ip6_rthdr *rth;
3203 ip6_clearpktopts(opt, IPV6_RTHDR);
3204 break; /* just remove the option */
3207 /* message length validation */
3208 if (len < sizeof(struct ip6_rthdr))
3210 rth = (struct ip6_rthdr *)buf;
3211 rthlen = (rth->ip6r_len + 1) << 3;
3215 switch (rth->ip6r_type) {
3216 case IPV6_RTHDR_TYPE_0:
3217 if (rth->ip6r_len == 0) /* must contain one addr */
3219 if (rth->ip6r_len % 2) /* length must be even */
3221 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3225 return (EINVAL); /* not supported */
3228 /* turn off the previous option */
3229 ip6_clearpktopts(opt, IPV6_RTHDR);
3230 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3231 if (opt->ip6po_rthdr == NULL)
3233 bcopy(rth, opt->ip6po_rthdr, rthlen);
3238 case IPV6_USE_MIN_MTU:
3239 if (len != sizeof(int))
3241 minmtupolicy = *(int *)buf;
3242 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3243 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3244 minmtupolicy != IP6PO_MINMTU_ALL) {
3247 opt->ip6po_minmtu = minmtupolicy;
3251 if (len != sizeof(int))
3254 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3256 * we ignore this option for TCP sockets.
3257 * (RFC3542 leaves this case unspecified.)
3259 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3261 opt->ip6po_flags |= IP6PO_DONTFRAG;
3264 case IPV6_PREFER_TEMPADDR:
3265 if (len != sizeof(int))
3267 preftemp = *(int *)buf;
3268 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3269 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3270 preftemp != IP6PO_TEMPADDR_PREFER) {
3273 opt->ip6po_prefer_tempaddr = preftemp;
3277 return (ENOPROTOOPT);
3278 } /* end of switch */
3284 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3285 * packet to the input queue of a specified interface. Note that this
3286 * calls the output routine of the loopback "driver", but with an interface
3287 * pointer that might NOT be &loif -- easier than replicating that code here.
3290 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3293 struct ip6_hdr *ip6;
3295 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3300 * Make sure to deep-copy IPv6 header portion in case the data
3301 * is in an mbuf cluster, so that we can safely override the IPv6
3302 * header portion later.
3304 if (!M_WRITABLE(copym) ||
3305 copym->m_len < sizeof(struct ip6_hdr)) {
3306 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3310 ip6 = mtod(copym, struct ip6_hdr *);
3312 * clear embedded scope identifiers if necessary.
3313 * in6_clearscope will touch the addresses only when necessary.
3315 in6_clearscope(&ip6->ip6_src);
3316 in6_clearscope(&ip6->ip6_dst);
3317 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3318 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3320 copym->m_pkthdr.csum_data = 0xffff;
3322 if_simloop(ifp, copym, AF_INET6, 0);
3326 * Chop IPv6 header off from the payload.
3329 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3332 struct ip6_hdr *ip6;
3334 ip6 = mtod(m, struct ip6_hdr *);
3335 if (m->m_len > sizeof(*ip6)) {
3336 mh = m_gethdr(M_NOWAIT, MT_DATA);
3341 m_move_pkthdr(mh, m);
3342 M_ALIGN(mh, sizeof(*ip6));
3343 m->m_len -= sizeof(*ip6);
3344 m->m_data += sizeof(*ip6);
3347 m->m_len = sizeof(*ip6);
3348 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3350 exthdrs->ip6e_ip6 = m;
3355 * Compute IPv6 extension header length.
3358 ip6_optlen(struct inpcb *inp)
3362 if (!inp->in6p_outputopts)
3367 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3369 len += elen(inp->in6p_outputopts->ip6po_hbh);
3370 if (inp->in6p_outputopts->ip6po_rthdr)
3371 /* dest1 is valid with rthdr only */
3372 len += elen(inp->in6p_outputopts->ip6po_dest1);
3373 len += elen(inp->in6p_outputopts->ip6po_rthdr);
3374 len += elen(inp->in6p_outputopts->ip6po_dest2);