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_MEXTPG) == 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 (IPV6_DSCP(ip6) == 0)
661 if (IPV6_ECN(ip6) == 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,
1181 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1182 (flags & IP_NO_SND_TAG_RL) ? false : true);
1186 /* Try to fragment the packet. Cases 1-b and 3. */
1187 if (mtu < IPV6_MMTU) {
1188 /* Path MTU cannot be less than IPV6_MMTU. */
1190 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1192 } else if (ip6->ip6_plen == 0) {
1193 /* Jumbo payload cannot be fragmented. */
1195 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1201 * Too large for the destination or interface;
1202 * fragment if possible.
1203 * Must be able to put at least 8 bytes per fragment.
1205 if (mtu > IPV6_MAXPACKET)
1206 mtu = IPV6_MAXPACKET;
1208 len = (mtu - unfragpartlen - sizeof(struct ip6_frag)) & ~7;
1211 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1216 * If the interface will not calculate checksums on
1217 * fragmented packets, then do it here.
1218 * XXX-BZ handle the hw offloading case. Need flags.
1220 error = ip6_output_delayed_csum(m, ifp, m->m_pkthdr.csum_flags,
1221 plen, optlen, true);
1226 * Change the next header field of the last header in the
1227 * unfragmentable part.
1229 if (exthdrs.ip6e_rthdr) {
1230 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1231 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1232 } else if (exthdrs.ip6e_dest1) {
1233 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1234 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1235 } else if (exthdrs.ip6e_hbh) {
1236 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1237 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1239 ip6 = mtod(m, struct ip6_hdr *);
1240 nextproto = ip6->ip6_nxt;
1241 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1245 * Loop through length of segment after first fragment,
1246 * make new header and copy data of each part and link onto
1250 id = htonl(ip6_randomid());
1251 error = ip6_fragment(ifp, m, unfragpartlen, nextproto,len, id);
1255 in6_ifstat_inc(ifp, ifs6_out_fragok);
1258 /* Remove leading garbage. */
1267 /* Record statistics for this interface address. */
1269 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1270 counter_u64_add(ia->ia_ifa.ifa_obytes,
1274 EVL_APPLY_PRI(m, vlan_pcp);
1275 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1282 IP6STAT_INC(ip6s_fragmented);
1288 m_freem(exthdrs.ip6e_hbh); /* m_freem() checks if mbuf is NULL. */
1289 m_freem(exthdrs.ip6e_dest1);
1290 m_freem(exthdrs.ip6e_rthdr);
1291 m_freem(exthdrs.ip6e_dest2);
1300 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1304 if (hlen > MCLBYTES)
1305 return (ENOBUFS); /* XXX */
1308 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1310 m = m_get(M_NOWAIT, MT_DATA);
1315 bcopy(hdr, mtod(m, caddr_t), hlen);
1322 * Insert jumbo payload option.
1325 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1331 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1334 * If there is no hop-by-hop options header, allocate new one.
1335 * If there is one but it doesn't have enough space to store the
1336 * jumbo payload option, allocate a cluster to store the whole options.
1337 * Otherwise, use it to store the options.
1339 if (exthdrs->ip6e_hbh == NULL) {
1340 mopt = m_get(M_NOWAIT, MT_DATA);
1343 mopt->m_len = JUMBOOPTLEN;
1344 optbuf = mtod(mopt, u_char *);
1345 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1346 exthdrs->ip6e_hbh = mopt;
1348 struct ip6_hbh *hbh;
1350 mopt = exthdrs->ip6e_hbh;
1351 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1354 * - exthdrs->ip6e_hbh is not referenced from places
1355 * other than exthdrs.
1356 * - exthdrs->ip6e_hbh is not an mbuf chain.
1358 int oldoptlen = mopt->m_len;
1362 * XXX: give up if the whole (new) hbh header does
1363 * not fit even in an mbuf cluster.
1365 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1369 * As a consequence, we must always prepare a cluster
1372 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1375 n->m_len = oldoptlen + JUMBOOPTLEN;
1376 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1378 optbuf = mtod(n, caddr_t) + oldoptlen;
1380 mopt = exthdrs->ip6e_hbh = n;
1382 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1383 mopt->m_len += JUMBOOPTLEN;
1385 optbuf[0] = IP6OPT_PADN;
1389 * Adjust the header length according to the pad and
1390 * the jumbo payload option.
1392 hbh = mtod(mopt, struct ip6_hbh *);
1393 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1396 /* fill in the option. */
1397 optbuf[2] = IP6OPT_JUMBO;
1399 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1400 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1402 /* finally, adjust the packet header length */
1403 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1410 * Insert fragment header and copy unfragmentable header portions.
1413 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1414 struct ip6_frag **frghdrp)
1416 struct mbuf *n, *mlast;
1418 if (hlen > sizeof(struct ip6_hdr)) {
1419 n = m_copym(m0, sizeof(struct ip6_hdr),
1420 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1427 /* Search for the last mbuf of unfragmentable part. */
1428 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1431 if (M_WRITABLE(mlast) &&
1432 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1433 /* use the trailing space of the last mbuf for the fragment hdr */
1434 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1436 mlast->m_len += sizeof(struct ip6_frag);
1437 m->m_pkthdr.len += sizeof(struct ip6_frag);
1439 /* allocate a new mbuf for the fragment header */
1442 mfrg = m_get(M_NOWAIT, MT_DATA);
1445 mfrg->m_len = sizeof(struct ip6_frag);
1446 *frghdrp = mtod(mfrg, struct ip6_frag *);
1447 mlast->m_next = mfrg;
1454 * Calculates IPv6 path mtu for destination @dst.
1455 * Resulting MTU is stored in @mtup.
1457 * Returns 0 on success.
1460 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1462 struct epoch_tracker et;
1463 struct nhop_object *nh;
1464 struct in6_addr kdst;
1468 in6_splitscope(dst, &kdst, &scopeid);
1470 NET_EPOCH_ENTER(et);
1471 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1473 error = ip6_calcmtu(nh->nh_ifp, dst, nh->nh_mtu, mtup, NULL, 0);
1475 error = EHOSTUNREACH;
1482 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1483 * and cached data in @ro_pmtu.
1484 * MTU from (successful) route lookup is saved (along with dst)
1485 * inside @ro_pmtu to avoid subsequent route lookups after packet
1486 * filter processing.
1488 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1489 * Returns 0 on success.
1492 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1493 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1494 int *alwaysfragp, u_int fibnum, u_int proto)
1496 struct nhop_object *nh;
1497 struct in6_addr kdst;
1499 struct sockaddr_in6 *sa6_dst, sin6;
1505 if (ro_pmtu == NULL || do_lookup) {
1507 * Here ro_pmtu has final destination address, while
1508 * ro might represent immediate destination.
1509 * Use ro_pmtu destination since mtu might differ.
1511 if (ro_pmtu != NULL) {
1512 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1513 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1514 ro_pmtu->ro_mtu = 0;
1518 if (ro_pmtu == NULL || ro_pmtu->ro_mtu == 0) {
1519 bzero(sa6_dst, sizeof(*sa6_dst));
1520 sa6_dst->sin6_family = AF_INET6;
1521 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1522 sa6_dst->sin6_addr = *dst;
1524 in6_splitscope(dst, &kdst, &scopeid);
1525 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1528 if (ro_pmtu != NULL)
1529 ro_pmtu->ro_mtu = mtu;
1532 mtu = ro_pmtu->ro_mtu;
1535 if (ro_pmtu != NULL && ro_pmtu->ro_nh != NULL)
1536 mtu = ro_pmtu->ro_nh->nh_mtu;
1538 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1542 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1543 * hostcache data for @dst.
1544 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1546 * Returns 0 on success.
1549 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1550 u_long *mtup, int *alwaysfragp, u_int proto)
1558 struct in_conninfo inc;
1560 bzero(&inc, sizeof(inc));
1561 inc.inc_flags |= INC_ISIPV6;
1562 inc.inc6_faddr = *dst;
1564 ifmtu = IN6_LINKMTU(ifp);
1566 /* TCP is known to react to pmtu changes so skip hc */
1567 if (proto != IPPROTO_TCP)
1568 mtu = tcp_hc_getmtu(&inc);
1571 mtu = min(mtu, rt_mtu);
1576 else if (mtu < IPV6_MMTU) {
1578 * RFC2460 section 5, last paragraph:
1579 * if we record ICMPv6 too big message with
1580 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1581 * or smaller, with framgent header attached.
1582 * (fragment header is needed regardless from the
1583 * packet size, for translators to identify packets)
1589 mtu = IN6_LINKMTU(ifp);
1591 error = EHOSTUNREACH; /* XXX */
1595 *alwaysfragp = alwaysfrag;
1600 * IP6 socket option processing.
1603 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1605 int optdatalen, uproto;
1607 struct inpcb *inp = sotoinpcb(so);
1609 int level, op, optname;
1613 uint32_t rss_bucket;
1618 * Don't use more than a quarter of mbuf clusters. N.B.:
1619 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1620 * on LP64 architectures, so cast to u_long to avoid undefined
1621 * behavior. ILP32 architectures cannot have nmbclusters
1622 * large enough to overflow for other reasons.
1624 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1626 level = sopt->sopt_level;
1627 op = sopt->sopt_dir;
1628 optname = sopt->sopt_name;
1629 optlen = sopt->sopt_valsize;
1633 uproto = (int)so->so_proto->pr_protocol;
1635 if (level != IPPROTO_IPV6) {
1638 if (sopt->sopt_level == SOL_SOCKET &&
1639 sopt->sopt_dir == SOPT_SET) {
1640 switch (sopt->sopt_name) {
1643 if ((so->so_options & SO_REUSEADDR) != 0)
1644 inp->inp_flags2 |= INP_REUSEADDR;
1646 inp->inp_flags2 &= ~INP_REUSEADDR;
1652 if ((so->so_options & SO_REUSEPORT) != 0)
1653 inp->inp_flags2 |= INP_REUSEPORT;
1655 inp->inp_flags2 &= ~INP_REUSEPORT;
1659 case SO_REUSEPORT_LB:
1661 if ((so->so_options & SO_REUSEPORT_LB) != 0)
1662 inp->inp_flags2 |= INP_REUSEPORT_LB;
1664 inp->inp_flags2 &= ~INP_REUSEPORT_LB;
1670 inp->inp_inc.inc_fibnum = so->so_fibnum;
1674 case SO_MAX_PACING_RATE:
1677 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1688 } else { /* level == IPPROTO_IPV6 */
1692 case IPV6_2292PKTOPTIONS:
1693 #ifdef IPV6_PKTOPTIONS
1694 case IPV6_PKTOPTIONS:
1699 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1700 printf("ip6_ctloutput: mbuf limit hit\n");
1705 error = soopt_getm(sopt, &m); /* XXX */
1708 error = soopt_mcopyin(sopt, m); /* XXX */
1712 error = ip6_pcbopts(&inp->in6p_outputopts, m,
1715 m_freem(m); /* XXX */
1720 * Use of some Hop-by-Hop options or some
1721 * Destination options, might require special
1722 * privilege. That is, normal applications
1723 * (without special privilege) might be forbidden
1724 * from setting certain options in outgoing packets,
1725 * and might never see certain options in received
1726 * packets. [RFC 2292 Section 6]
1727 * KAME specific note:
1728 * KAME prevents non-privileged users from sending or
1729 * receiving ANY hbh/dst options in order to avoid
1730 * overhead of parsing options in the kernel.
1732 case IPV6_RECVHOPOPTS:
1733 case IPV6_RECVDSTOPTS:
1734 case IPV6_RECVRTHDRDSTOPTS:
1736 error = priv_check(td,
1737 PRIV_NETINET_SETHDROPTS);
1742 case IPV6_UNICAST_HOPS:
1745 case IPV6_RECVPKTINFO:
1746 case IPV6_RECVHOPLIMIT:
1747 case IPV6_RECVRTHDR:
1748 case IPV6_RECVPATHMTU:
1749 case IPV6_RECVTCLASS:
1750 case IPV6_RECVFLOWID:
1752 case IPV6_RECVRSSBUCKETID:
1755 case IPV6_AUTOFLOWLABEL:
1756 case IPV6_ORIGDSTADDR:
1758 case IPV6_BINDMULTI:
1760 case IPV6_RSS_LISTEN_BUCKET:
1763 if (optname == IPV6_BINDANY && td != NULL) {
1764 error = priv_check(td,
1765 PRIV_NETINET_BINDANY);
1770 if (optlen != sizeof(int)) {
1774 error = sooptcopyin(sopt, &optval,
1775 sizeof optval, sizeof optval);
1779 case IPV6_UNICAST_HOPS:
1780 if (optval < -1 || optval >= 256)
1783 /* -1 = kernel default */
1784 inp->in6p_hops = optval;
1785 if ((inp->inp_vflag &
1787 inp->inp_ip_ttl = optval;
1790 #define OPTSET(bit) \
1794 inp->inp_flags |= (bit); \
1796 inp->inp_flags &= ~(bit); \
1798 } while (/*CONSTCOND*/ 0)
1799 #define OPTSET2292(bit) \
1802 inp->inp_flags |= IN6P_RFC2292; \
1804 inp->inp_flags |= (bit); \
1806 inp->inp_flags &= ~(bit); \
1808 } while (/*CONSTCOND*/ 0)
1809 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0)
1811 #define OPTSET2_N(bit, val) do { \
1813 inp->inp_flags2 |= bit; \
1815 inp->inp_flags2 &= ~bit; \
1817 #define OPTSET2(bit, val) do { \
1819 OPTSET2_N(bit, val); \
1822 #define OPTBIT2(bit) (inp->inp_flags2 & (bit) ? 1 : 0)
1823 #define OPTSET2292_EXCLUSIVE(bit) \
1826 if (OPTBIT(IN6P_RFC2292)) { \
1830 inp->inp_flags |= (bit); \
1832 inp->inp_flags &= ~(bit); \
1835 } while (/*CONSTCOND*/ 0)
1837 case IPV6_RECVPKTINFO:
1838 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1843 struct ip6_pktopts **optp;
1845 /* cannot mix with RFC2292 */
1846 if (OPTBIT(IN6P_RFC2292)) {
1851 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1853 return (ECONNRESET);
1855 optp = &inp->in6p_outputopts;
1856 error = ip6_pcbopt(IPV6_HOPLIMIT,
1857 (u_char *)&optval, sizeof(optval),
1858 optp, (td != NULL) ? td->td_ucred :
1864 case IPV6_RECVHOPLIMIT:
1865 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1868 case IPV6_RECVHOPOPTS:
1869 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1872 case IPV6_RECVDSTOPTS:
1873 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1876 case IPV6_RECVRTHDRDSTOPTS:
1877 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1880 case IPV6_RECVRTHDR:
1881 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1884 case IPV6_RECVPATHMTU:
1886 * We ignore this option for TCP
1888 * (RFC3542 leaves this case
1891 if (uproto != IPPROTO_TCP)
1895 case IPV6_RECVFLOWID:
1896 OPTSET2(INP_RECVFLOWID, optval);
1900 case IPV6_RECVRSSBUCKETID:
1901 OPTSET2(INP_RECVRSSBUCKETID, optval);
1907 if (inp->inp_lport ||
1908 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1910 * The socket is already bound.
1917 inp->inp_flags |= IN6P_IPV6_V6ONLY;
1918 inp->inp_vflag &= ~INP_IPV4;
1920 inp->inp_flags &= ~IN6P_IPV6_V6ONLY;
1921 inp->inp_vflag |= INP_IPV4;
1925 case IPV6_RECVTCLASS:
1926 /* cannot mix with RFC2292 XXX */
1927 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1929 case IPV6_AUTOFLOWLABEL:
1930 OPTSET(IN6P_AUTOFLOWLABEL);
1933 case IPV6_ORIGDSTADDR:
1934 OPTSET2(INP_ORIGDSTADDR, optval);
1937 OPTSET(INP_BINDANY);
1940 case IPV6_BINDMULTI:
1941 OPTSET2(INP_BINDMULTI, optval);
1944 case IPV6_RSS_LISTEN_BUCKET:
1945 if ((optval >= 0) &&
1946 (optval < rss_getnumbuckets())) {
1948 inp->inp_rss_listen_bucket = optval;
1949 OPTSET2_N(INP_RSS_BUCKET_SET, 1);
1957 if ((optval >= -1) && (optval <=
1958 (INP_2PCP_MASK >> INP_2PCP_SHIFT))) {
1984 case IPV6_USE_MIN_MTU:
1985 case IPV6_PREFER_TEMPADDR:
1986 if (optlen != sizeof(optval)) {
1990 error = sooptcopyin(sopt, &optval,
1991 sizeof optval, sizeof optval);
1995 struct ip6_pktopts **optp;
1997 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1999 return (ECONNRESET);
2001 optp = &inp->in6p_outputopts;
2002 error = ip6_pcbopt(optname,
2003 (u_char *)&optval, sizeof(optval),
2004 optp, (td != NULL) ? td->td_ucred :
2010 case IPV6_2292PKTINFO:
2011 case IPV6_2292HOPLIMIT:
2012 case IPV6_2292HOPOPTS:
2013 case IPV6_2292DSTOPTS:
2014 case IPV6_2292RTHDR:
2016 if (optlen != sizeof(int)) {
2020 error = sooptcopyin(sopt, &optval,
2021 sizeof optval, sizeof optval);
2025 case IPV6_2292PKTINFO:
2026 OPTSET2292(IN6P_PKTINFO);
2028 case IPV6_2292HOPLIMIT:
2029 OPTSET2292(IN6P_HOPLIMIT);
2031 case IPV6_2292HOPOPTS:
2033 * Check super-user privilege.
2034 * See comments for IPV6_RECVHOPOPTS.
2037 error = priv_check(td,
2038 PRIV_NETINET_SETHDROPTS);
2042 OPTSET2292(IN6P_HOPOPTS);
2044 case IPV6_2292DSTOPTS:
2046 error = priv_check(td,
2047 PRIV_NETINET_SETHDROPTS);
2051 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
2053 case IPV6_2292RTHDR:
2054 OPTSET2292(IN6P_RTHDR);
2062 case IPV6_RTHDRDSTOPTS:
2065 /* new advanced API (RFC3542) */
2067 u_char optbuf_storage[MCLBYTES];
2069 struct ip6_pktopts **optp;
2071 /* cannot mix with RFC2292 */
2072 if (OPTBIT(IN6P_RFC2292)) {
2078 * We only ensure valsize is not too large
2079 * here. Further validation will be done
2082 error = sooptcopyin(sopt, optbuf_storage,
2083 sizeof(optbuf_storage), 0);
2086 optlen = sopt->sopt_valsize;
2087 optbuf = optbuf_storage;
2089 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2091 return (ECONNRESET);
2093 optp = &inp->in6p_outputopts;
2094 error = ip6_pcbopt(optname, optbuf, optlen,
2095 optp, (td != NULL) ? td->td_ucred : NULL,
2102 case IPV6_MULTICAST_IF:
2103 case IPV6_MULTICAST_HOPS:
2104 case IPV6_MULTICAST_LOOP:
2105 case IPV6_JOIN_GROUP:
2106 case IPV6_LEAVE_GROUP:
2108 case MCAST_BLOCK_SOURCE:
2109 case MCAST_UNBLOCK_SOURCE:
2110 case MCAST_JOIN_GROUP:
2111 case MCAST_LEAVE_GROUP:
2112 case MCAST_JOIN_SOURCE_GROUP:
2113 case MCAST_LEAVE_SOURCE_GROUP:
2114 error = ip6_setmoptions(inp, sopt);
2117 case IPV6_PORTRANGE:
2118 error = sooptcopyin(sopt, &optval,
2119 sizeof optval, sizeof optval);
2125 case IPV6_PORTRANGE_DEFAULT:
2126 inp->inp_flags &= ~(INP_LOWPORT);
2127 inp->inp_flags &= ~(INP_HIGHPORT);
2130 case IPV6_PORTRANGE_HIGH:
2131 inp->inp_flags &= ~(INP_LOWPORT);
2132 inp->inp_flags |= INP_HIGHPORT;
2135 case IPV6_PORTRANGE_LOW:
2136 inp->inp_flags &= ~(INP_HIGHPORT);
2137 inp->inp_flags |= INP_LOWPORT;
2147 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2148 case IPV6_IPSEC_POLICY:
2149 if (IPSEC_ENABLED(ipv6)) {
2150 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2157 error = ENOPROTOOPT;
2164 case IPV6_2292PKTOPTIONS:
2165 #ifdef IPV6_PKTOPTIONS
2166 case IPV6_PKTOPTIONS:
2169 * RFC3542 (effectively) deprecated the
2170 * semantics of the 2292-style pktoptions.
2171 * Since it was not reliable in nature (i.e.,
2172 * applications had to expect the lack of some
2173 * information after all), it would make sense
2174 * to simplify this part by always returning
2177 sopt->sopt_valsize = 0;
2180 case IPV6_RECVHOPOPTS:
2181 case IPV6_RECVDSTOPTS:
2182 case IPV6_RECVRTHDRDSTOPTS:
2183 case IPV6_UNICAST_HOPS:
2184 case IPV6_RECVPKTINFO:
2185 case IPV6_RECVHOPLIMIT:
2186 case IPV6_RECVRTHDR:
2187 case IPV6_RECVPATHMTU:
2190 case IPV6_PORTRANGE:
2191 case IPV6_RECVTCLASS:
2192 case IPV6_AUTOFLOWLABEL:
2196 case IPV6_RECVFLOWID:
2198 case IPV6_RSSBUCKETID:
2199 case IPV6_RECVRSSBUCKETID:
2201 case IPV6_BINDMULTI:
2204 case IPV6_RECVHOPOPTS:
2205 optval = OPTBIT(IN6P_HOPOPTS);
2208 case IPV6_RECVDSTOPTS:
2209 optval = OPTBIT(IN6P_DSTOPTS);
2212 case IPV6_RECVRTHDRDSTOPTS:
2213 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2216 case IPV6_UNICAST_HOPS:
2217 optval = inp->in6p_hops;
2220 case IPV6_RECVPKTINFO:
2221 optval = OPTBIT(IN6P_PKTINFO);
2224 case IPV6_RECVHOPLIMIT:
2225 optval = OPTBIT(IN6P_HOPLIMIT);
2228 case IPV6_RECVRTHDR:
2229 optval = OPTBIT(IN6P_RTHDR);
2232 case IPV6_RECVPATHMTU:
2233 optval = OPTBIT(IN6P_MTU);
2237 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2240 case IPV6_PORTRANGE:
2243 flags = inp->inp_flags;
2244 if (flags & INP_HIGHPORT)
2245 optval = IPV6_PORTRANGE_HIGH;
2246 else if (flags & INP_LOWPORT)
2247 optval = IPV6_PORTRANGE_LOW;
2252 case IPV6_RECVTCLASS:
2253 optval = OPTBIT(IN6P_TCLASS);
2256 case IPV6_AUTOFLOWLABEL:
2257 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2260 case IPV6_ORIGDSTADDR:
2261 optval = OPTBIT2(INP_ORIGDSTADDR);
2265 optval = OPTBIT(INP_BINDANY);
2269 optval = inp->inp_flowid;
2273 optval = inp->inp_flowtype;
2276 case IPV6_RECVFLOWID:
2277 optval = OPTBIT2(INP_RECVFLOWID);
2280 case IPV6_RSSBUCKETID:
2282 rss_hash2bucket(inp->inp_flowid,
2286 optval = rss_bucket;
2291 case IPV6_RECVRSSBUCKETID:
2292 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2296 case IPV6_BINDMULTI:
2297 optval = OPTBIT2(INP_BINDMULTI);
2301 if (OPTBIT2(INP_2PCP_SET)) {
2302 optval = (inp->inp_flags2 &
2313 error = sooptcopyout(sopt, &optval,
2320 struct ip6_mtuinfo mtuinfo;
2321 struct in6_addr addr;
2323 if (!(so->so_state & SS_ISCONNECTED))
2326 * XXX: we dot not consider the case of source
2327 * routing, or optional information to specify
2328 * the outgoing interface.
2329 * Copy faddr out of inp to avoid holding lock
2330 * on inp during route lookup.
2333 bcopy(&inp->in6p_faddr, &addr, sizeof(addr));
2335 error = ip6_getpmtu_ctl(so->so_fibnum,
2339 if (pmtu > IPV6_MAXPACKET)
2340 pmtu = IPV6_MAXPACKET;
2342 bzero(&mtuinfo, sizeof(mtuinfo));
2343 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2344 optdata = (void *)&mtuinfo;
2345 optdatalen = sizeof(mtuinfo);
2346 error = sooptcopyout(sopt, optdata,
2351 case IPV6_2292PKTINFO:
2352 case IPV6_2292HOPLIMIT:
2353 case IPV6_2292HOPOPTS:
2354 case IPV6_2292RTHDR:
2355 case IPV6_2292DSTOPTS:
2357 case IPV6_2292PKTINFO:
2358 optval = OPTBIT(IN6P_PKTINFO);
2360 case IPV6_2292HOPLIMIT:
2361 optval = OPTBIT(IN6P_HOPLIMIT);
2363 case IPV6_2292HOPOPTS:
2364 optval = OPTBIT(IN6P_HOPOPTS);
2366 case IPV6_2292RTHDR:
2367 optval = OPTBIT(IN6P_RTHDR);
2369 case IPV6_2292DSTOPTS:
2370 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2373 error = sooptcopyout(sopt, &optval,
2380 case IPV6_RTHDRDSTOPTS:
2384 case IPV6_USE_MIN_MTU:
2385 case IPV6_PREFER_TEMPADDR:
2386 error = ip6_getpcbopt(inp, optname, sopt);
2389 case IPV6_MULTICAST_IF:
2390 case IPV6_MULTICAST_HOPS:
2391 case IPV6_MULTICAST_LOOP:
2393 error = ip6_getmoptions(inp, sopt);
2396 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2397 case IPV6_IPSEC_POLICY:
2398 if (IPSEC_ENABLED(ipv6)) {
2399 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2405 error = ENOPROTOOPT;
2415 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2417 int error = 0, optval, optlen;
2418 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2419 struct inpcb *inp = sotoinpcb(so);
2420 int level, op, optname;
2422 level = sopt->sopt_level;
2423 op = sopt->sopt_dir;
2424 optname = sopt->sopt_name;
2425 optlen = sopt->sopt_valsize;
2427 if (level != IPPROTO_IPV6) {
2434 * For ICMPv6 sockets, no modification allowed for checksum
2435 * offset, permit "no change" values to help existing apps.
2437 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2438 * for an ICMPv6 socket will fail."
2439 * The current behavior does not meet RFC3542.
2443 if (optlen != sizeof(int)) {
2447 error = sooptcopyin(sopt, &optval, sizeof(optval),
2451 if (optval < -1 || (optval % 2) != 0) {
2453 * The API assumes non-negative even offset
2454 * values or -1 as a special value.
2457 } else if (so->so_proto->pr_protocol ==
2459 if (optval != icmp6off)
2462 inp->in6p_cksum = optval;
2466 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2469 optval = inp->in6p_cksum;
2471 error = sooptcopyout(sopt, &optval, sizeof(optval));
2481 error = ENOPROTOOPT;
2489 * Set up IP6 options in pcb for insertion in output packets or
2490 * specifying behavior of outgoing packets.
2493 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2494 struct socket *so, struct sockopt *sopt)
2496 struct ip6_pktopts *opt = *pktopt;
2498 struct thread *td = sopt->sopt_td;
2499 struct epoch_tracker et;
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 NET_EPOCH_ENTER(et);
2528 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2529 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2530 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2531 free(opt, M_IP6OPT);
2541 * initialize ip6_pktopts. beware that there are non-zero default values in
2545 ip6_initpktopts(struct ip6_pktopts *opt)
2548 bzero(opt, sizeof(*opt));
2549 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2550 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2551 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2552 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2556 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2557 struct ucred *cred, int uproto)
2559 struct ip6_pktopts *opt;
2561 if (*pktopt == NULL) {
2562 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2564 if (*pktopt == NULL)
2566 ip6_initpktopts(*pktopt);
2570 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2573 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2574 if (pktopt && pktopt->field) { \
2576 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2577 malloc_optdata = true; \
2579 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
2581 free(optdata, M_TEMP); \
2582 return (ECONNRESET); \
2584 pktopt = inp->in6p_outputopts; \
2585 if (pktopt && pktopt->field) { \
2586 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2587 bcopy(pktopt->field, optdata, optdatalen); \
2589 free(optdata, M_TEMP); \
2591 malloc_optdata = false; \
2596 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2597 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2599 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2600 pktopt->field->sa_len)
2603 ip6_getpcbopt(struct inpcb *inp, int optname, struct sockopt *sopt)
2605 void *optdata = NULL;
2606 bool malloc_optdata = false;
2609 struct in6_pktinfo null_pktinfo;
2610 int deftclass = 0, on;
2611 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2612 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2613 struct ip6_pktopts *pktopt;
2616 pktopt = inp->in6p_outputopts;
2620 optdata = (void *)&null_pktinfo;
2621 if (pktopt && pktopt->ip6po_pktinfo) {
2622 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2623 sizeof(null_pktinfo));
2624 in6_clearscope(&null_pktinfo.ipi6_addr);
2626 /* XXX: we don't have to do this every time... */
2627 bzero(&null_pktinfo, sizeof(null_pktinfo));
2629 optdatalen = sizeof(struct in6_pktinfo);
2632 if (pktopt && pktopt->ip6po_tclass >= 0)
2633 deftclass = pktopt->ip6po_tclass;
2634 optdata = (void *)&deftclass;
2635 optdatalen = sizeof(int);
2638 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2641 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2643 case IPV6_RTHDRDSTOPTS:
2644 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2647 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2650 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2652 case IPV6_USE_MIN_MTU:
2654 defminmtu = pktopt->ip6po_minmtu;
2655 optdata = (void *)&defminmtu;
2656 optdatalen = sizeof(int);
2659 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2663 optdata = (void *)&on;
2664 optdatalen = sizeof(on);
2666 case IPV6_PREFER_TEMPADDR:
2668 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2669 optdata = (void *)&defpreftemp;
2670 optdatalen = sizeof(int);
2672 default: /* should not happen */
2674 panic("ip6_getpcbopt: unexpected option\n");
2677 return (ENOPROTOOPT);
2681 error = sooptcopyout(sopt, optdata, optdatalen);
2683 free(optdata, M_TEMP);
2689 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2694 if (optname == -1 || optname == IPV6_PKTINFO) {
2695 if (pktopt->ip6po_pktinfo)
2696 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2697 pktopt->ip6po_pktinfo = NULL;
2699 if (optname == -1 || optname == IPV6_HOPLIMIT)
2700 pktopt->ip6po_hlim = -1;
2701 if (optname == -1 || optname == IPV6_TCLASS)
2702 pktopt->ip6po_tclass = -1;
2703 if (optname == -1 || optname == IPV6_NEXTHOP) {
2704 if (pktopt->ip6po_nextroute.ro_nh) {
2705 NH_FREE(pktopt->ip6po_nextroute.ro_nh);
2706 pktopt->ip6po_nextroute.ro_nh = NULL;
2708 if (pktopt->ip6po_nexthop)
2709 free(pktopt->ip6po_nexthop, M_IP6OPT);
2710 pktopt->ip6po_nexthop = NULL;
2712 if (optname == -1 || optname == IPV6_HOPOPTS) {
2713 if (pktopt->ip6po_hbh)
2714 free(pktopt->ip6po_hbh, M_IP6OPT);
2715 pktopt->ip6po_hbh = NULL;
2717 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2718 if (pktopt->ip6po_dest1)
2719 free(pktopt->ip6po_dest1, M_IP6OPT);
2720 pktopt->ip6po_dest1 = NULL;
2722 if (optname == -1 || optname == IPV6_RTHDR) {
2723 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2724 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2725 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2726 if (pktopt->ip6po_route.ro_nh) {
2727 NH_FREE(pktopt->ip6po_route.ro_nh);
2728 pktopt->ip6po_route.ro_nh = NULL;
2731 if (optname == -1 || optname == IPV6_DSTOPTS) {
2732 if (pktopt->ip6po_dest2)
2733 free(pktopt->ip6po_dest2, M_IP6OPT);
2734 pktopt->ip6po_dest2 = NULL;
2738 #define PKTOPT_EXTHDRCPY(type) \
2741 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2742 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2743 if (dst->type == NULL)\
2745 bcopy(src->type, dst->type, hlen);\
2747 } while (/*CONSTCOND*/ 0)
2750 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2752 if (dst == NULL || src == NULL) {
2753 printf("ip6_clearpktopts: invalid argument\n");
2757 dst->ip6po_hlim = src->ip6po_hlim;
2758 dst->ip6po_tclass = src->ip6po_tclass;
2759 dst->ip6po_flags = src->ip6po_flags;
2760 dst->ip6po_minmtu = src->ip6po_minmtu;
2761 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2762 if (src->ip6po_pktinfo) {
2763 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2765 if (dst->ip6po_pktinfo == NULL)
2767 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2769 if (src->ip6po_nexthop) {
2770 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2772 if (dst->ip6po_nexthop == NULL)
2774 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2775 src->ip6po_nexthop->sa_len);
2777 PKTOPT_EXTHDRCPY(ip6po_hbh);
2778 PKTOPT_EXTHDRCPY(ip6po_dest1);
2779 PKTOPT_EXTHDRCPY(ip6po_dest2);
2780 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2784 ip6_clearpktopts(dst, -1);
2787 #undef PKTOPT_EXTHDRCPY
2789 struct ip6_pktopts *
2790 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2793 struct ip6_pktopts *dst;
2795 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2798 ip6_initpktopts(dst);
2800 if ((error = copypktopts(dst, src, canwait)) != 0) {
2801 free(dst, M_IP6OPT);
2809 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2814 ip6_clearpktopts(pktopt, -1);
2816 free(pktopt, M_IP6OPT);
2820 * Set IPv6 outgoing packet options based on advanced API.
2823 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2824 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2826 struct cmsghdr *cm = NULL;
2828 if (control == NULL || opt == NULL)
2832 * ip6_setpktopt can call ifnet_by_index(), so it's imperative that we are
2833 * in the net epoch here.
2837 ip6_initpktopts(opt);
2842 * If stickyopt is provided, make a local copy of the options
2843 * for this particular packet, then override them by ancillary
2845 * XXX: copypktopts() does not copy the cached route to a next
2846 * hop (if any). This is not very good in terms of efficiency,
2847 * but we can allow this since this option should be rarely
2850 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2855 * XXX: Currently, we assume all the optional information is stored
2858 if (control->m_next)
2861 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2862 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2865 if (control->m_len < CMSG_LEN(0))
2868 cm = mtod(control, struct cmsghdr *);
2869 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2871 if (cm->cmsg_level != IPPROTO_IPV6)
2874 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2875 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2884 * Set a particular packet option, as a sticky option or an ancillary data
2885 * item. "len" can be 0 only when it's a sticky option.
2886 * We have 4 cases of combination of "sticky" and "cmsg":
2887 * "sticky=0, cmsg=0": impossible
2888 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2889 * "sticky=1, cmsg=0": RFC3542 socket option
2890 * "sticky=1, cmsg=1": RFC2292 socket option
2893 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2894 struct ucred *cred, int sticky, int cmsg, int uproto)
2896 int minmtupolicy, preftemp;
2899 if (!sticky && !cmsg) {
2901 printf("ip6_setpktopt: impossible case\n");
2907 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2908 * not be specified in the context of RFC3542. Conversely,
2909 * RFC3542 types should not be specified in the context of RFC2292.
2913 case IPV6_2292PKTINFO:
2914 case IPV6_2292HOPLIMIT:
2915 case IPV6_2292NEXTHOP:
2916 case IPV6_2292HOPOPTS:
2917 case IPV6_2292DSTOPTS:
2918 case IPV6_2292RTHDR:
2919 case IPV6_2292PKTOPTIONS:
2920 return (ENOPROTOOPT);
2923 if (sticky && cmsg) {
2930 case IPV6_RTHDRDSTOPTS:
2932 case IPV6_USE_MIN_MTU:
2935 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2936 return (ENOPROTOOPT);
2941 case IPV6_2292PKTINFO:
2944 struct ifnet *ifp = NULL;
2945 struct in6_pktinfo *pktinfo;
2947 if (len != sizeof(struct in6_pktinfo))
2950 pktinfo = (struct in6_pktinfo *)buf;
2953 * An application can clear any sticky IPV6_PKTINFO option by
2954 * doing a "regular" setsockopt with ipi6_addr being
2955 * in6addr_any and ipi6_ifindex being zero.
2956 * [RFC 3542, Section 6]
2958 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2959 pktinfo->ipi6_ifindex == 0 &&
2960 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2961 ip6_clearpktopts(opt, optname);
2965 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2966 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2969 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2971 /* validate the interface index if specified. */
2972 if (pktinfo->ipi6_ifindex > V_if_index)
2974 if (pktinfo->ipi6_ifindex) {
2975 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2979 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2980 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2984 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2985 struct in6_ifaddr *ia;
2987 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2988 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2990 return (EADDRNOTAVAIL);
2991 ifa_free(&ia->ia_ifa);
2994 * We store the address anyway, and let in6_selectsrc()
2995 * validate the specified address. This is because ipi6_addr
2996 * may not have enough information about its scope zone, and
2997 * we may need additional information (such as outgoing
2998 * interface or the scope zone of a destination address) to
2999 * disambiguate the scope.
3000 * XXX: the delay of the validation may confuse the
3001 * application when it is used as a sticky option.
3003 if (opt->ip6po_pktinfo == NULL) {
3004 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
3005 M_IP6OPT, M_NOWAIT);
3006 if (opt->ip6po_pktinfo == NULL)
3009 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
3013 case IPV6_2292HOPLIMIT:
3019 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3020 * to simplify the ordering among hoplimit options.
3022 if (optname == IPV6_HOPLIMIT && sticky)
3023 return (ENOPROTOOPT);
3025 if (len != sizeof(int))
3028 if (*hlimp < -1 || *hlimp > 255)
3031 opt->ip6po_hlim = *hlimp;
3039 if (len != sizeof(int))
3041 tclass = *(int *)buf;
3042 if (tclass < -1 || tclass > 255)
3045 opt->ip6po_tclass = tclass;
3049 case IPV6_2292NEXTHOP:
3052 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3057 if (len == 0) { /* just remove the option */
3058 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3062 /* check if cmsg_len is large enough for sa_len */
3063 if (len < sizeof(struct sockaddr) || len < *buf)
3066 switch (((struct sockaddr *)buf)->sa_family) {
3069 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3072 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3075 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3076 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3079 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
3085 case AF_LINK: /* should eventually be supported */
3087 return (EAFNOSUPPORT);
3090 /* turn off the previous option, then set the new option. */
3091 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3092 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3093 if (opt->ip6po_nexthop == NULL)
3095 bcopy(buf, opt->ip6po_nexthop, *buf);
3098 case IPV6_2292HOPOPTS:
3101 struct ip6_hbh *hbh;
3105 * XXX: We don't allow a non-privileged user to set ANY HbH
3106 * options, since per-option restriction has too much
3110 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3116 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3117 break; /* just remove the option */
3120 /* message length validation */
3121 if (len < sizeof(struct ip6_hbh))
3123 hbh = (struct ip6_hbh *)buf;
3124 hbhlen = (hbh->ip6h_len + 1) << 3;
3128 /* turn off the previous option, then set the new option. */
3129 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3130 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3131 if (opt->ip6po_hbh == NULL)
3133 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3138 case IPV6_2292DSTOPTS:
3140 case IPV6_RTHDRDSTOPTS:
3142 struct ip6_dest *dest, **newdest = NULL;
3145 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
3146 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3152 ip6_clearpktopts(opt, optname);
3153 break; /* just remove the option */
3156 /* message length validation */
3157 if (len < sizeof(struct ip6_dest))
3159 dest = (struct ip6_dest *)buf;
3160 destlen = (dest->ip6d_len + 1) << 3;
3165 * Determine the position that the destination options header
3166 * should be inserted; before or after the routing header.
3169 case IPV6_2292DSTOPTS:
3171 * The old advacned API is ambiguous on this point.
3172 * Our approach is to determine the position based
3173 * according to the existence of a routing header.
3174 * Note, however, that this depends on the order of the
3175 * extension headers in the ancillary data; the 1st
3176 * part of the destination options header must appear
3177 * before the routing header in the ancillary data,
3179 * RFC3542 solved the ambiguity by introducing
3180 * separate ancillary data or option types.
3182 if (opt->ip6po_rthdr == NULL)
3183 newdest = &opt->ip6po_dest1;
3185 newdest = &opt->ip6po_dest2;
3187 case IPV6_RTHDRDSTOPTS:
3188 newdest = &opt->ip6po_dest1;
3191 newdest = &opt->ip6po_dest2;
3195 /* turn off the previous option, then set the new option. */
3196 ip6_clearpktopts(opt, optname);
3197 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3198 if (*newdest == NULL)
3200 bcopy(dest, *newdest, destlen);
3205 case IPV6_2292RTHDR:
3208 struct ip6_rthdr *rth;
3212 ip6_clearpktopts(opt, IPV6_RTHDR);
3213 break; /* just remove the option */
3216 /* message length validation */
3217 if (len < sizeof(struct ip6_rthdr))
3219 rth = (struct ip6_rthdr *)buf;
3220 rthlen = (rth->ip6r_len + 1) << 3;
3224 switch (rth->ip6r_type) {
3225 case IPV6_RTHDR_TYPE_0:
3226 if (rth->ip6r_len == 0) /* must contain one addr */
3228 if (rth->ip6r_len % 2) /* length must be even */
3230 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3234 return (EINVAL); /* not supported */
3237 /* turn off the previous option */
3238 ip6_clearpktopts(opt, IPV6_RTHDR);
3239 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3240 if (opt->ip6po_rthdr == NULL)
3242 bcopy(rth, opt->ip6po_rthdr, rthlen);
3247 case IPV6_USE_MIN_MTU:
3248 if (len != sizeof(int))
3250 minmtupolicy = *(int *)buf;
3251 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3252 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3253 minmtupolicy != IP6PO_MINMTU_ALL) {
3256 opt->ip6po_minmtu = minmtupolicy;
3260 if (len != sizeof(int))
3263 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3265 * we ignore this option for TCP sockets.
3266 * (RFC3542 leaves this case unspecified.)
3268 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3270 opt->ip6po_flags |= IP6PO_DONTFRAG;
3273 case IPV6_PREFER_TEMPADDR:
3274 if (len != sizeof(int))
3276 preftemp = *(int *)buf;
3277 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3278 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3279 preftemp != IP6PO_TEMPADDR_PREFER) {
3282 opt->ip6po_prefer_tempaddr = preftemp;
3286 return (ENOPROTOOPT);
3287 } /* end of switch */
3293 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3294 * packet to the input queue of a specified interface. Note that this
3295 * calls the output routine of the loopback "driver", but with an interface
3296 * pointer that might NOT be &loif -- easier than replicating that code here.
3299 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3302 struct ip6_hdr *ip6;
3304 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3309 * Make sure to deep-copy IPv6 header portion in case the data
3310 * is in an mbuf cluster, so that we can safely override the IPv6
3311 * header portion later.
3313 if (!M_WRITABLE(copym) ||
3314 copym->m_len < sizeof(struct ip6_hdr)) {
3315 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3319 ip6 = mtod(copym, struct ip6_hdr *);
3321 * clear embedded scope identifiers if necessary.
3322 * in6_clearscope will touch the addresses only when necessary.
3324 in6_clearscope(&ip6->ip6_src);
3325 in6_clearscope(&ip6->ip6_dst);
3326 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3327 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3329 copym->m_pkthdr.csum_data = 0xffff;
3331 if_simloop(ifp, copym, AF_INET6, 0);
3335 * Chop IPv6 header off from the payload.
3338 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3341 struct ip6_hdr *ip6;
3343 ip6 = mtod(m, struct ip6_hdr *);
3344 if (m->m_len > sizeof(*ip6)) {
3345 mh = m_gethdr(M_NOWAIT, MT_DATA);
3350 m_move_pkthdr(mh, m);
3351 M_ALIGN(mh, sizeof(*ip6));
3352 m->m_len -= sizeof(*ip6);
3353 m->m_data += sizeof(*ip6);
3356 m->m_len = sizeof(*ip6);
3357 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3359 exthdrs->ip6e_ip6 = m;
3364 * Compute IPv6 extension header length.
3367 ip6_optlen(struct inpcb *inp)
3371 if (!inp->in6p_outputopts)
3376 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3378 len += elen(inp->in6p_outputopts->ip6po_hbh);
3379 if (inp->in6p_outputopts->ip6po_rthdr)
3380 /* dest1 is valid with rthdr only */
3381 len += elen(inp->in6p_outputopts->ip6po_dest1);
3382 len += elen(inp->in6p_outputopts->ip6po_rthdr);
3383 len += elen(inp->in6p_outputopts->ip6po_dest2);