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/scope6_var.h>
126 extern int in6_mcast_loop;
129 struct mbuf *ip6e_ip6;
130 struct mbuf *ip6e_hbh;
131 struct mbuf *ip6e_dest1;
132 struct mbuf *ip6e_rthdr;
133 struct mbuf *ip6e_dest2;
136 static MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
138 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
139 struct ucred *, int);
140 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
141 struct socket *, struct sockopt *);
142 static int ip6_getpcbopt(struct inpcb *, int, struct sockopt *);
143 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
144 struct ucred *, int, int, int);
146 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
147 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
149 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
150 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
151 static int ip6_getpmtu(struct route_in6 *, int,
152 struct ifnet *, const struct in6_addr *, u_long *, int *, u_int,
154 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
155 u_long *, int *, u_int);
156 static int ip6_getpmtu_ctl(u_int, const struct in6_addr *, u_long *);
157 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
160 * Make an extension header from option data. hp is the source,
161 * mp is the destination, and _ol is the optlen.
163 #define MAKE_EXTHDR(hp, mp, _ol) \
166 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
167 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
168 ((eh)->ip6e_len + 1) << 3); \
171 (_ol) += (*(mp))->m_len; \
173 } while (/*CONSTCOND*/ 0)
176 * Form a chain of extension headers.
177 * m is the extension header mbuf
178 * mp is the previous mbuf in the chain
179 * p is the next header
180 * i is the type of option.
182 #define MAKE_CHAIN(m, mp, p, i)\
186 panic("%s:%d: assumption failed: "\
187 "hdr not split: hdrsplit %d exthdrs %p",\
188 __func__, __LINE__, hdrsplit, &exthdrs);\
189 *mtod((m), u_char *) = *(p);\
191 p = mtod((m), u_char *);\
192 (m)->m_next = (mp)->m_next;\
196 } while (/*CONSTCOND*/ 0)
199 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
203 csum = in_cksum_skip(m, offset + plen, offset);
204 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
206 offset += m->m_pkthdr.csum_data; /* checksum offset */
208 if (offset + sizeof(csum) > m->m_len)
209 m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
211 *(u_short *)mtodo(m, offset) = csum;
215 ip6_output_delayed_csum(struct mbuf *m, struct ifnet *ifp, int csum_flags,
216 int plen, int optlen)
219 KASSERT((plen >= optlen), ("%s:%d: plen %d < optlen %d, m %p, ifp %p "
221 __func__, __LINE__, plen, optlen, m, ifp, csum_flags));
223 if (csum_flags & CSUM_DELAY_DATA_IPV6) {
224 in6_delayed_cksum(m, plen - optlen,
225 sizeof(struct ip6_hdr) + optlen);
226 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
228 #if defined(SCTP) || defined(SCTP_SUPPORT)
229 if (csum_flags & CSUM_SCTP_IPV6) {
230 sctp_delayed_cksum(m, sizeof(struct ip6_hdr) + optlen);
231 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
237 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
238 int fraglen , uint32_t id)
240 struct mbuf *m, **mnext, *m_frgpart;
241 struct ip6_hdr *ip6, *mhip6;
242 struct ip6_frag *ip6f;
245 int tlen = m0->m_pkthdr.len;
247 KASSERT((fraglen % 8 == 0), ("Fragment length must be a multiple of 8"));
250 ip6 = mtod(m, struct ip6_hdr *);
251 mnext = &m->m_nextpkt;
253 for (off = hlen; off < tlen; off += fraglen) {
254 m = m_gethdr(M_NOWAIT, MT_DATA);
256 IP6STAT_INC(ip6s_odropped);
261 * Make sure the complete packet header gets copied
262 * from the originating mbuf to the newly created
263 * mbuf. This also ensures that existing firewall
264 * classification(s), VLAN tags and so on get copied
265 * to the resulting fragmented packet(s):
267 if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
269 IP6STAT_INC(ip6s_odropped);
274 mnext = &m->m_nextpkt;
275 m->m_data += max_linkhdr;
276 mhip6 = mtod(m, struct ip6_hdr *);
278 m->m_len = sizeof(*mhip6);
279 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
281 IP6STAT_INC(ip6s_odropped);
284 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
285 if (off + fraglen >= tlen)
286 fraglen = tlen - off;
288 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
289 mhip6->ip6_plen = htons((u_short)(fraglen + hlen +
290 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
291 if ((m_frgpart = m_copym(m0, off, fraglen, M_NOWAIT)) == NULL) {
292 IP6STAT_INC(ip6s_odropped);
296 m->m_pkthdr.len = fraglen + hlen + sizeof(*ip6f);
297 ip6f->ip6f_reserved = 0;
298 ip6f->ip6f_ident = id;
299 ip6f->ip6f_nxt = nextproto;
300 IP6STAT_INC(ip6s_ofragments);
301 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
308 ip6_output_send(struct inpcb *inp, struct ifnet *ifp, struct ifnet *origifp,
309 struct mbuf *m, struct sockaddr_in6 *dst, struct route_in6 *ro,
313 struct ktls_session *tls = NULL;
315 struct m_snd_tag *mst;
318 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
323 * If this is an unencrypted TLS record, save a reference to
324 * the record. This local reference is used to call
325 * ktls_output_eagain after the mbuf has been freed (thus
326 * dropping the mbuf's reference) in if_output.
328 if (m->m_next != NULL && mbuf_has_tls_session(m->m_next)) {
329 tls = ktls_hold(m->m_next->m_epg_tls);
333 * If a TLS session doesn't have a valid tag, it must
334 * have had an earlier ifp mismatch, so drop this
343 * Always stamp tags that include NIC ktls.
349 if (inp != NULL && mst == NULL) {
350 if ((inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) != 0 ||
351 (inp->inp_snd_tag != NULL &&
352 inp->inp_snd_tag->ifp != ifp))
353 in_pcboutput_txrtlmt(inp, ifp, m);
355 if (inp->inp_snd_tag != NULL)
356 mst = inp->inp_snd_tag;
359 if (stamp_tag && mst != NULL) {
360 KASSERT(m->m_pkthdr.rcvif == NULL,
361 ("trying to add a send tag to a forwarded packet"));
362 if (mst->ifp != ifp) {
368 /* stamp send tag on mbuf */
369 m->m_pkthdr.snd_tag = m_snd_tag_ref(mst);
370 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
373 error = nd6_output_ifp(ifp, origifp, m, dst, (struct route *)ro);
376 /* Check for route change invalidating send tags. */
380 error = ktls_output_eagain(inp, tls);
386 in_pcboutput_eagain(inp);
393 * The packet in mbuf chain m contains a skeletal IP6 header (with pri, len,
394 * nxt, hlim, src, dst).
395 * This function may modify ver and hlim only.
396 * The mbuf chain containing the packet will be freed.
397 * The mbuf opt, if present, will not be freed.
398 * If route_in6 ro is present and has ro_nh initialized, route lookup would be
399 * skipped and ro->ro_nh would be used. If ro is present but ro->ro_nh is NULL,
400 * then result of route lookup is stored in ro->ro_nh.
402 * Type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and nd_ifinfo.linkmtu
403 * is uint32_t. So we use u_long to hold largest one, which is rt_mtu.
405 * ifpp - XXX: just for statistics
408 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
409 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
410 struct ifnet **ifpp, struct inpcb *inp)
413 struct ifnet *ifp, *origifp;
416 struct route_in6 *ro_pmtu;
417 struct nhop_object *nh;
418 struct sockaddr_in6 *dst, sin6, src_sa, dst_sa;
419 struct in6_addr odst;
424 struct in6_ifaddr *ia = NULL;
426 int alwaysfrag, dontfrag;
427 u_int32_t optlen, plen = 0, unfragpartlen;
428 struct ip6_exthdrs exthdrs;
429 struct in6_addr src0, dst0;
435 struct m_tag *fwd_tag = NULL;
441 INP_LOCK_ASSERT(inp);
442 M_SETFIB(m, inp->inp_inc.inc_fibnum);
443 if ((flags & IP_NODEFAULTFLOWID) == 0) {
444 /* Unconditionally set flowid. */
445 m->m_pkthdr.flowid = inp->inp_flowid;
446 M_HASHTYPE_SET(m, inp->inp_flowtype);
448 if ((inp->inp_flags2 & INP_2PCP_SET) != 0)
449 vlan_pcp = (inp->inp_flags2 & INP_2PCP_MASK) >>
452 m->m_pkthdr.numa_domain = inp->inp_numa_domain;
456 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
458 * IPSec checking which handles several cases.
459 * FAST IPSEC: We re-injected the packet.
460 * XXX: need scope argument.
462 if (IPSEC_ENABLED(ipv6)) {
463 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
464 if (error == EINPROGRESS)
471 /* Source address validation. */
472 ip6 = mtod(m, struct ip6_hdr *);
473 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
474 (flags & IPV6_UNSPECSRC) == 0) {
476 IP6STAT_INC(ip6s_badscope);
479 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
481 IP6STAT_INC(ip6s_badscope);
486 * If we are given packet options to add extension headers prepare them.
487 * Calculate the total length of the extension header chain.
488 * Keep the length of the unfragmentable part for fragmentation.
490 bzero(&exthdrs, sizeof(exthdrs));
492 unfragpartlen = sizeof(struct ip6_hdr);
494 /* Hop-by-Hop options header. */
495 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh, optlen);
497 /* Destination options header (1st part). */
498 if (opt->ip6po_rthdr) {
499 #ifndef RTHDR_SUPPORT_IMPLEMENTED
501 * If there is a routing header, discard the packet
502 * right away here. RH0/1 are obsolete and we do not
503 * currently support RH2/3/4.
504 * People trying to use RH253/254 may want to disable
506 * The moment we do support any routing header (again)
507 * this block should check the routing type more
515 * Destination options header (1st part).
516 * This only makes sense with a routing header.
517 * See Section 9.2 of RFC 3542.
518 * Disabling this part just for MIP6 convenience is
519 * a bad idea. We need to think carefully about a
520 * way to make the advanced API coexist with MIP6
521 * options, which might automatically be inserted in
524 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1,
527 /* Routing header. */
528 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr, optlen);
530 unfragpartlen += optlen;
533 * NOTE: we don't add AH/ESP length here (done in
534 * ip6_ipsec_output()).
537 /* Destination options header (2nd part). */
538 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2, optlen);
542 * If there is at least one extension header,
543 * separate IP6 header from the payload.
547 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
551 m = exthdrs.ip6e_ip6;
552 ip6 = mtod(m, struct ip6_hdr *);
556 /* Adjust mbuf packet header length. */
557 m->m_pkthdr.len += optlen;
558 plen = m->m_pkthdr.len - sizeof(*ip6);
560 /* If this is a jumbo payload, insert a jumbo payload option. */
561 if (plen > IPV6_MAXPACKET) {
563 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
567 m = exthdrs.ip6e_ip6;
568 ip6 = mtod(m, struct ip6_hdr *);
571 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
575 ip6->ip6_plen = htons(plen);
576 nexthdrp = &ip6->ip6_nxt;
580 * Concatenate headers and fill in next header fields.
581 * Here we have, on "m"
583 * and we insert headers accordingly.
584 * Finally, we should be getting:
585 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload].
587 * During the header composing process "m" points to IPv6
588 * header. "mprev" points to an extension header prior to esp.
593 * We treat dest2 specially. This makes IPsec processing
594 * much easier. The goal here is to make mprev point the
595 * mbuf prior to dest2.
597 * Result: IPv6 dest2 payload.
598 * m and mprev will point to IPv6 header.
600 if (exthdrs.ip6e_dest2) {
602 panic("%s:%d: assumption failed: "
603 "hdr not split: hdrsplit %d exthdrs %p",
604 __func__, __LINE__, hdrsplit, &exthdrs);
605 exthdrs.ip6e_dest2->m_next = m->m_next;
606 m->m_next = exthdrs.ip6e_dest2;
607 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
608 ip6->ip6_nxt = IPPROTO_DSTOPTS;
612 * Result: IPv6 hbh dest1 rthdr dest2 payload.
613 * m will point to IPv6 header. mprev will point to the
614 * extension header prior to dest2 (rthdr in the above case).
616 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
617 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
619 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
623 IP6STAT_INC(ip6s_localout);
627 if (opt && opt->ip6po_rthdr)
628 ro = &opt->ip6po_route;
630 dst = (struct sockaddr_in6 *)&ro->ro_dst;
633 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
637 * If specified, try to fill in the traffic class field.
638 * Do not override if a non-zero value is already set.
639 * We check the diffserv field and the ECN field separately.
641 if (opt && opt->ip6po_tclass >= 0) {
644 if (IPV6_DSCP(ip6) == 0)
646 if (IPV6_ECN(ip6) == 0)
649 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
652 /* Fill in or override the hop limit field, if necessary. */
653 if (opt && opt->ip6po_hlim != -1)
654 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
655 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
657 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
659 ip6->ip6_hlim = V_ip6_defmcasthlim;
662 if (ro == NULL || ro->ro_nh == NULL) {
663 bzero(dst, sizeof(*dst));
664 dst->sin6_family = AF_INET6;
665 dst->sin6_len = sizeof(*dst);
666 dst->sin6_addr = ip6->ip6_dst;
669 * Validate route against routing table changes.
670 * Make sure that the address family is set in route.
676 if (ro->ro_nh != NULL && inp != NULL) {
677 ro->ro_dst.sin6_family = AF_INET6; /* XXX KASSERT? */
678 NH_VALIDATE((struct route *)ro, &inp->inp_rt_cookie,
681 if (ro->ro_nh != NULL && fwd_tag == NULL &&
682 (!NH_IS_VALID(ro->ro_nh) ||
683 ro->ro_dst.sin6_family != AF_INET6 ||
684 !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)))
685 RO_INVALIDATE_CACHE(ro);
687 if (ro->ro_nh != NULL && fwd_tag == NULL &&
688 ro->ro_dst.sin6_family == AF_INET6 &&
689 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
690 /* Nexthop is valid and contains valid ifp */
694 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
696 if (fwd_tag == NULL) {
697 bzero(&dst_sa, sizeof(dst_sa));
698 dst_sa.sin6_family = AF_INET6;
699 dst_sa.sin6_len = sizeof(dst_sa);
700 dst_sa.sin6_addr = ip6->ip6_dst;
702 error = in6_selectroute(&dst_sa, opt, im6o, ro, &ifp,
703 &nh, fibnum, m->m_pkthdr.flowid);
705 IP6STAT_INC(ip6s_noroute);
707 in6_ifstat_inc(ifp, ifs6_out_discard);
711 * At this point at least @ifp is not NULL
712 * Can be the case when dst is multicast, link-local or
713 * interface is explicitly specificed by the caller.
718 * If in6_selectroute() does not return a nexthop
719 * dst may not have been updated.
721 *dst = dst_sa; /* XXX */
726 origifp = nh->nh_aifp;
727 ia = (struct in6_ifaddr *)(nh->nh_ifa);
728 counter_u64_add(nh->nh_pksent, 1);
731 struct nhop_object *nh;
732 struct in6_addr kdst;
735 if (fwd_tag == NULL) {
736 bzero(&dst_sa, sizeof(dst_sa));
737 dst_sa.sin6_family = AF_INET6;
738 dst_sa.sin6_len = sizeof(dst_sa);
739 dst_sa.sin6_addr = ip6->ip6_dst;
742 if (IN6_IS_ADDR_MULTICAST(&dst_sa.sin6_addr) &&
744 (ifp = im6o->im6o_multicast_ifp) != NULL) {
745 /* We do not need a route lookup. */
746 *dst = dst_sa; /* XXX */
751 in6_splitscope(&dst_sa.sin6_addr, &kdst, &scopeid);
753 if (IN6_IS_ADDR_MC_LINKLOCAL(&dst_sa.sin6_addr) ||
754 IN6_IS_ADDR_MC_NODELOCAL(&dst_sa.sin6_addr)) {
756 ifp = in6_getlinkifnet(scopeid);
758 error = EHOSTUNREACH;
761 *dst = dst_sa; /* XXX */
767 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE,
770 IP6STAT_INC(ip6s_noroute);
771 /* No ifp in6_ifstat_inc(ifp, ifs6_out_discard); */
772 error = EHOSTUNREACH;;
777 origifp = nh->nh_aifp;
778 ia = ifatoia6(nh->nh_ifa);
779 if (nh->nh_flags & NHF_GATEWAY)
780 dst->sin6_addr = nh->gw6_sa.sin6_addr;
781 else if (fwd_tag != NULL)
782 dst->sin6_addr = dst_sa.sin6_addr;
787 * At this point ifp MUST be pointing to the valid transmit ifp.
788 * origifp MUST be valid and pointing to either the same ifp or,
789 * in case of loopback output, to the interface which ip6_src
792 * fe80::1%em0 -> fe80::2%em0 -> ifp=em0, origifp=em0
793 * fe80::1%em0 -> fe80::1%em0 -> ifp=lo0, origifp=em0
794 * ::1 -> ::1 -> ifp=lo0, origifp=lo0
796 * mtu can be 0 and will be refined later.
798 KASSERT((ifp != NULL), ("output interface must not be NULL"));
799 KASSERT((origifp != NULL), ("output address interface must not be NULL"));
801 if ((flags & IPV6_FORWARDING) == 0) {
802 /* XXX: the FORWARDING flag can be set for mrouting. */
803 in6_ifstat_inc(ifp, ifs6_out_request);
806 /* Setup data structures for scope ID checks. */
808 bzero(&src_sa, sizeof(src_sa));
809 src_sa.sin6_family = AF_INET6;
810 src_sa.sin6_len = sizeof(src_sa);
811 src_sa.sin6_addr = ip6->ip6_src;
814 /* Re-initialize to be sure. */
815 bzero(&dst_sa, sizeof(dst_sa));
816 dst_sa.sin6_family = AF_INET6;
817 dst_sa.sin6_len = sizeof(dst_sa);
818 dst_sa.sin6_addr = ip6->ip6_dst;
820 /* Check for valid scope ID. */
821 if (in6_setscope(&src0, origifp, &zone) == 0 &&
822 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
823 in6_setscope(&dst0, origifp, &zone) == 0 &&
824 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
826 * The outgoing interface is in the zone of the source
827 * and destination addresses.
830 } else if ((origifp->if_flags & IFF_LOOPBACK) == 0 ||
831 sa6_recoverscope(&src_sa) != 0 ||
832 sa6_recoverscope(&dst_sa) != 0 ||
833 dst_sa.sin6_scope_id == 0 ||
834 (src_sa.sin6_scope_id != 0 &&
835 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
836 ifnet_byindex(dst_sa.sin6_scope_id) == NULL) {
838 * If the destination network interface is not a
839 * loopback interface, or the destination network
840 * address has no scope ID, or the source address has
841 * a scope ID set which is different from the
842 * destination address one, or there is no network
843 * interface representing this scope ID, the address
844 * pair is considered invalid.
846 IP6STAT_INC(ip6s_badscope);
847 in6_ifstat_inc(origifp, ifs6_out_discard);
849 error = EHOSTUNREACH; /* XXX */
852 /* All scope ID checks are successful. */
854 if (nh && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
855 if (opt && opt->ip6po_nextroute.ro_nh) {
857 * The nexthop is explicitly specified by the
858 * application. We assume the next hop is an IPv6
861 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
863 else if ((nh->nh_flags & NHF_GATEWAY))
867 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
868 m->m_flags &= ~(M_BCAST | M_MCAST); /* Just in case. */
870 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
871 in6_ifstat_inc(ifp, ifs6_out_mcast);
873 /* Confirm that the outgoing interface supports multicast. */
874 if (!(ifp->if_flags & IFF_MULTICAST)) {
875 IP6STAT_INC(ip6s_noroute);
876 in6_ifstat_inc(ifp, ifs6_out_discard);
880 if ((im6o == NULL && in6_mcast_loop) ||
881 (im6o && im6o->im6o_multicast_loop)) {
883 * Loop back multicast datagram if not expressly
884 * forbidden to do so, even if we have not joined
885 * the address; protocols will filter it later,
886 * thus deferring a hash lookup and lock acquisition
887 * at the expense of an m_copym().
889 ip6_mloopback(ifp, m);
892 * If we are acting as a multicast router, perform
893 * multicast forwarding as if the packet had just
894 * arrived on the interface to which we are about
895 * to send. The multicast forwarding function
896 * recursively calls this function, using the
897 * IPV6_FORWARDING flag to prevent infinite recursion.
899 * Multicasts that are looped back by ip6_mloopback(),
900 * above, will be forwarded by the ip6_input() routine,
903 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
905 * XXX: ip6_mforward expects that rcvif is NULL
906 * when it is called from the originating path.
907 * However, it may not always be the case.
909 m->m_pkthdr.rcvif = NULL;
910 if (ip6_mforward(ip6, ifp, m) != 0) {
917 * Multicasts with a hoplimit of zero may be looped back,
918 * above, but must not be transmitted on a network.
919 * Also, multicasts addressed to the loopback interface
920 * are not sent -- the above call to ip6_mloopback() will
921 * loop back a copy if this host actually belongs to the
922 * destination group on the loopback interface.
924 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
925 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
932 * Fill the outgoing inteface to tell the upper layer
933 * to increment per-interface statistics.
938 /* Determine path MTU. */
939 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
940 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
942 KASSERT(mtu > 0, ("%s:%d: mtu %ld, ro_pmtu %p ro %p ifp %p "
943 "alwaysfrag %d fibnum %u\n", __func__, __LINE__, mtu, ro_pmtu, ro,
944 ifp, alwaysfrag, fibnum));
947 * The caller of this function may specify to use the minimum MTU
949 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
950 * setting. The logic is a bit complicated; by default, unicast
951 * packets will follow path MTU while multicast packets will be sent at
952 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
953 * including unicast ones will be sent at the minimum MTU. Multicast
954 * packets will always be sent at the minimum MTU unless
955 * IP6PO_MINMTU_DISABLE is explicitly specified.
956 * See RFC 3542 for more details.
958 if (mtu > IPV6_MMTU) {
959 if ((flags & IPV6_MINMTU))
961 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
963 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
965 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
971 * Clear embedded scope identifiers if necessary.
972 * in6_clearscope() will touch the addresses only when necessary.
974 in6_clearscope(&ip6->ip6_src);
975 in6_clearscope(&ip6->ip6_dst);
978 * If the outgoing packet contains a hop-by-hop options header,
979 * it must be examined and processed even by the source node.
980 * (RFC 2460, section 4.)
982 if (exthdrs.ip6e_hbh) {
983 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
984 u_int32_t dummy; /* XXX unused */
985 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
988 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
989 panic("ip6e_hbh is not contiguous");
992 * XXX: if we have to send an ICMPv6 error to the sender,
993 * we need the M_LOOP flag since icmp6_error() expects
994 * the IPv6 and the hop-by-hop options header are
995 * contiguous unless the flag is set.
997 m->m_flags |= M_LOOP;
998 m->m_pkthdr.rcvif = ifp;
999 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
1000 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
1001 &dummy, &plen) < 0) {
1002 /* m was already freed at this point. */
1003 error = EINVAL;/* better error? */
1006 m->m_flags &= ~M_LOOP; /* XXX */
1007 m->m_pkthdr.rcvif = NULL;
1010 /* Jump over all PFIL processing if hooks are not active. */
1011 if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
1014 odst = ip6->ip6_dst;
1015 /* Run through list of hooks for output packets. */
1016 switch (pfil_mbuf_out(V_inet6_pfil_head, &m, ifp, inp)) {
1018 ip6 = mtod(m, struct ip6_hdr *);
1028 /* See if destination IP address was changed by packet filter. */
1029 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
1030 m->m_flags |= M_SKIP_FIREWALL;
1031 /* If destination is now ourself drop to ip6_input(). */
1032 if (in6_localip(&ip6->ip6_dst)) {
1033 m->m_flags |= M_FASTFWD_OURS;
1034 if (m->m_pkthdr.rcvif == NULL)
1035 m->m_pkthdr.rcvif = V_loif;
1036 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1037 m->m_pkthdr.csum_flags |=
1038 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1039 m->m_pkthdr.csum_data = 0xffff;
1041 #if defined(SCTP) || defined(SCTP_SUPPORT)
1042 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1043 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1045 error = netisr_queue(NETISR_IPV6, m);
1049 RO_INVALIDATE_CACHE(ro);
1050 needfiblookup = 1; /* Redo the routing table lookup. */
1053 /* See if fib was changed by packet filter. */
1054 if (fibnum != M_GETFIB(m)) {
1055 m->m_flags |= M_SKIP_FIREWALL;
1056 fibnum = M_GETFIB(m);
1058 RO_INVALIDATE_CACHE(ro);
1064 /* See if local, if yes, send it to netisr. */
1065 if (m->m_flags & M_FASTFWD_OURS) {
1066 if (m->m_pkthdr.rcvif == NULL)
1067 m->m_pkthdr.rcvif = V_loif;
1068 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1069 m->m_pkthdr.csum_flags |=
1070 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1071 m->m_pkthdr.csum_data = 0xffff;
1073 #if defined(SCTP) || defined(SCTP_SUPPORT)
1074 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1075 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1077 error = netisr_queue(NETISR_IPV6, m);
1080 /* Or forward to some other address? */
1081 if ((m->m_flags & M_IP6_NEXTHOP) &&
1082 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
1084 dst = (struct sockaddr_in6 *)&ro->ro_dst;
1087 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
1088 m->m_flags |= M_SKIP_FIREWALL;
1089 m->m_flags &= ~M_IP6_NEXTHOP;
1090 m_tag_delete(m, fwd_tag);
1096 EVL_APPLY_PRI(m, vlan_pcp);
1098 /* Ensure the packet data is mapped if the interface requires it. */
1099 if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) {
1100 m = mb_unmapped_to_ext(m);
1102 IP6STAT_INC(ip6s_odropped);
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 ip6_output_delayed_csum(m, ifp, sw_csum, plen, optlen);
1140 /* XXX-BZ m->m_pkthdr.csum_flags &= ~ifp->if_hwassist; */
1141 tlen = m->m_pkthdr.len;
1143 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
1147 if (dontfrag && alwaysfrag) { /* Case 4. */
1148 /* Conflicting request - can't transmit. */
1152 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* Case 2-b. */
1154 * Even if the DONTFRAG option is specified, we cannot send the
1155 * packet when the data length is larger than the MTU of the
1156 * outgoing interface.
1157 * Notify the error by sending IPV6_PATHMTU ancillary data if
1158 * application wanted to know the MTU value. Also return an
1159 * error code (this is not described in the API spec).
1162 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
1167 /* Transmit packet without fragmentation. */
1168 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* Cases 1-a and 2-a. */
1169 struct in6_ifaddr *ia6;
1171 ip6 = mtod(m, struct ip6_hdr *);
1172 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1174 /* Record statistics for this interface address. */
1175 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
1176 counter_u64_add(ia6->ia_ifa.ifa_obytes,
1179 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1180 (flags & IP_NO_SND_TAG_RL) ? false : true);
1184 /* Try to fragment the packet. Cases 1-b and 3. */
1185 if (mtu < IPV6_MMTU) {
1186 /* Path MTU cannot be less than IPV6_MMTU. */
1188 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1190 } else if (ip6->ip6_plen == 0) {
1191 /* Jumbo payload cannot be fragmented. */
1193 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1199 * Too large for the destination or interface;
1200 * fragment if possible.
1201 * Must be able to put at least 8 bytes per fragment.
1203 if (mtu > IPV6_MAXPACKET)
1204 mtu = IPV6_MAXPACKET;
1206 len = (mtu - unfragpartlen - sizeof(struct ip6_frag)) & ~7;
1209 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1214 * If the interface will not calculate checksums on
1215 * fragmented packets, then do it here.
1216 * XXX-BZ handle the hw offloading case. Need flags.
1218 ip6_output_delayed_csum(m, ifp, m->m_pkthdr.csum_flags, plen,
1222 * Change the next header field of the last header in the
1223 * unfragmentable part.
1225 if (exthdrs.ip6e_rthdr) {
1226 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1227 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1228 } else if (exthdrs.ip6e_dest1) {
1229 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1230 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1231 } else if (exthdrs.ip6e_hbh) {
1232 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1233 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1235 ip6 = mtod(m, struct ip6_hdr *);
1236 nextproto = ip6->ip6_nxt;
1237 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1241 * Loop through length of segment after first fragment,
1242 * make new header and copy data of each part and link onto
1246 id = htonl(ip6_randomid());
1247 error = ip6_fragment(ifp, m, unfragpartlen, nextproto,len, id);
1251 in6_ifstat_inc(ifp, ifs6_out_fragok);
1254 /* Remove leading garbage. */
1263 /* Record statistics for this interface address. */
1265 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1266 counter_u64_add(ia->ia_ifa.ifa_obytes,
1270 EVL_APPLY_PRI(m, vlan_pcp);
1271 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1278 IP6STAT_INC(ip6s_fragmented);
1284 m_freem(exthdrs.ip6e_hbh); /* m_freem() checks if mbuf is NULL. */
1285 m_freem(exthdrs.ip6e_dest1);
1286 m_freem(exthdrs.ip6e_rthdr);
1287 m_freem(exthdrs.ip6e_dest2);
1296 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1300 if (hlen > MCLBYTES)
1301 return (ENOBUFS); /* XXX */
1304 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1306 m = m_get(M_NOWAIT, MT_DATA);
1311 bcopy(hdr, mtod(m, caddr_t), hlen);
1318 * Insert jumbo payload option.
1321 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1327 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1330 * If there is no hop-by-hop options header, allocate new one.
1331 * If there is one but it doesn't have enough space to store the
1332 * jumbo payload option, allocate a cluster to store the whole options.
1333 * Otherwise, use it to store the options.
1335 if (exthdrs->ip6e_hbh == NULL) {
1336 mopt = m_get(M_NOWAIT, MT_DATA);
1339 mopt->m_len = JUMBOOPTLEN;
1340 optbuf = mtod(mopt, u_char *);
1341 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1342 exthdrs->ip6e_hbh = mopt;
1344 struct ip6_hbh *hbh;
1346 mopt = exthdrs->ip6e_hbh;
1347 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1350 * - exthdrs->ip6e_hbh is not referenced from places
1351 * other than exthdrs.
1352 * - exthdrs->ip6e_hbh is not an mbuf chain.
1354 int oldoptlen = mopt->m_len;
1358 * XXX: give up if the whole (new) hbh header does
1359 * not fit even in an mbuf cluster.
1361 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1365 * As a consequence, we must always prepare a cluster
1368 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1371 n->m_len = oldoptlen + JUMBOOPTLEN;
1372 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1374 optbuf = mtod(n, caddr_t) + oldoptlen;
1376 mopt = exthdrs->ip6e_hbh = n;
1378 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1379 mopt->m_len += JUMBOOPTLEN;
1381 optbuf[0] = IP6OPT_PADN;
1385 * Adjust the header length according to the pad and
1386 * the jumbo payload option.
1388 hbh = mtod(mopt, struct ip6_hbh *);
1389 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1392 /* fill in the option. */
1393 optbuf[2] = IP6OPT_JUMBO;
1395 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1396 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1398 /* finally, adjust the packet header length */
1399 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1406 * Insert fragment header and copy unfragmentable header portions.
1409 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1410 struct ip6_frag **frghdrp)
1412 struct mbuf *n, *mlast;
1414 if (hlen > sizeof(struct ip6_hdr)) {
1415 n = m_copym(m0, sizeof(struct ip6_hdr),
1416 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1423 /* Search for the last mbuf of unfragmentable part. */
1424 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1427 if (M_WRITABLE(mlast) &&
1428 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1429 /* use the trailing space of the last mbuf for the fragment hdr */
1430 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1432 mlast->m_len += sizeof(struct ip6_frag);
1433 m->m_pkthdr.len += sizeof(struct ip6_frag);
1435 /* allocate a new mbuf for the fragment header */
1438 mfrg = m_get(M_NOWAIT, MT_DATA);
1441 mfrg->m_len = sizeof(struct ip6_frag);
1442 *frghdrp = mtod(mfrg, struct ip6_frag *);
1443 mlast->m_next = mfrg;
1450 * Calculates IPv6 path mtu for destination @dst.
1451 * Resulting MTU is stored in @mtup.
1453 * Returns 0 on success.
1456 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1458 struct epoch_tracker et;
1459 struct nhop_object *nh;
1460 struct in6_addr kdst;
1464 in6_splitscope(dst, &kdst, &scopeid);
1466 NET_EPOCH_ENTER(et);
1467 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1469 error = ip6_calcmtu(nh->nh_ifp, dst, nh->nh_mtu, mtup, NULL, 0);
1471 error = EHOSTUNREACH;
1478 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1479 * and cached data in @ro_pmtu.
1480 * MTU from (successful) route lookup is saved (along with dst)
1481 * inside @ro_pmtu to avoid subsequent route lookups after packet
1482 * filter processing.
1484 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1485 * Returns 0 on success.
1488 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1489 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1490 int *alwaysfragp, u_int fibnum, u_int proto)
1492 struct nhop_object *nh;
1493 struct in6_addr kdst;
1495 struct sockaddr_in6 *sa6_dst, sin6;
1501 if (ro_pmtu == NULL || do_lookup) {
1503 * Here ro_pmtu has final destination address, while
1504 * ro might represent immediate destination.
1505 * Use ro_pmtu destination since mtu might differ.
1507 if (ro_pmtu != NULL) {
1508 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1509 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1510 ro_pmtu->ro_mtu = 0;
1514 if (ro_pmtu == NULL || ro_pmtu->ro_mtu == 0) {
1515 bzero(sa6_dst, sizeof(*sa6_dst));
1516 sa6_dst->sin6_family = AF_INET6;
1517 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1518 sa6_dst->sin6_addr = *dst;
1520 in6_splitscope(dst, &kdst, &scopeid);
1521 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1524 if (ro_pmtu != NULL)
1525 ro_pmtu->ro_mtu = mtu;
1528 mtu = ro_pmtu->ro_mtu;
1531 if (ro_pmtu != NULL && ro_pmtu->ro_nh != NULL)
1532 mtu = ro_pmtu->ro_nh->nh_mtu;
1534 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1538 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1539 * hostcache data for @dst.
1540 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1542 * Returns 0 on success.
1545 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1546 u_long *mtup, int *alwaysfragp, u_int proto)
1554 struct in_conninfo inc;
1556 bzero(&inc, sizeof(inc));
1557 inc.inc_flags |= INC_ISIPV6;
1558 inc.inc6_faddr = *dst;
1560 ifmtu = IN6_LINKMTU(ifp);
1562 /* TCP is known to react to pmtu changes so skip hc */
1563 if (proto != IPPROTO_TCP)
1564 mtu = tcp_hc_getmtu(&inc);
1567 mtu = min(mtu, rt_mtu);
1572 else if (mtu < IPV6_MMTU) {
1574 * RFC2460 section 5, last paragraph:
1575 * if we record ICMPv6 too big message with
1576 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1577 * or smaller, with framgent header attached.
1578 * (fragment header is needed regardless from the
1579 * packet size, for translators to identify packets)
1585 mtu = IN6_LINKMTU(ifp);
1587 error = EHOSTUNREACH; /* XXX */
1591 *alwaysfragp = alwaysfrag;
1596 * IP6 socket option processing.
1599 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1601 int optdatalen, uproto;
1603 struct inpcb *inp = sotoinpcb(so);
1605 int level, op, optname;
1609 uint32_t rss_bucket;
1614 * Don't use more than a quarter of mbuf clusters. N.B.:
1615 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1616 * on LP64 architectures, so cast to u_long to avoid undefined
1617 * behavior. ILP32 architectures cannot have nmbclusters
1618 * large enough to overflow for other reasons.
1620 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1622 level = sopt->sopt_level;
1623 op = sopt->sopt_dir;
1624 optname = sopt->sopt_name;
1625 optlen = sopt->sopt_valsize;
1629 uproto = (int)so->so_proto->pr_protocol;
1631 if (level != IPPROTO_IPV6) {
1634 if (sopt->sopt_level == SOL_SOCKET &&
1635 sopt->sopt_dir == SOPT_SET) {
1636 switch (sopt->sopt_name) {
1639 if ((so->so_options & SO_REUSEADDR) != 0)
1640 inp->inp_flags2 |= INP_REUSEADDR;
1642 inp->inp_flags2 &= ~INP_REUSEADDR;
1648 if ((so->so_options & SO_REUSEPORT) != 0)
1649 inp->inp_flags2 |= INP_REUSEPORT;
1651 inp->inp_flags2 &= ~INP_REUSEPORT;
1655 case SO_REUSEPORT_LB:
1657 if ((so->so_options & SO_REUSEPORT_LB) != 0)
1658 inp->inp_flags2 |= INP_REUSEPORT_LB;
1660 inp->inp_flags2 &= ~INP_REUSEPORT_LB;
1666 inp->inp_inc.inc_fibnum = so->so_fibnum;
1670 case SO_MAX_PACING_RATE:
1673 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1684 } else { /* level == IPPROTO_IPV6 */
1688 case IPV6_2292PKTOPTIONS:
1689 #ifdef IPV6_PKTOPTIONS
1690 case IPV6_PKTOPTIONS:
1695 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1696 printf("ip6_ctloutput: mbuf limit hit\n");
1701 error = soopt_getm(sopt, &m); /* XXX */
1704 error = soopt_mcopyin(sopt, m); /* XXX */
1708 error = ip6_pcbopts(&inp->in6p_outputopts, m,
1711 m_freem(m); /* XXX */
1716 * Use of some Hop-by-Hop options or some
1717 * Destination options, might require special
1718 * privilege. That is, normal applications
1719 * (without special privilege) might be forbidden
1720 * from setting certain options in outgoing packets,
1721 * and might never see certain options in received
1722 * packets. [RFC 2292 Section 6]
1723 * KAME specific note:
1724 * KAME prevents non-privileged users from sending or
1725 * receiving ANY hbh/dst options in order to avoid
1726 * overhead of parsing options in the kernel.
1728 case IPV6_RECVHOPOPTS:
1729 case IPV6_RECVDSTOPTS:
1730 case IPV6_RECVRTHDRDSTOPTS:
1732 error = priv_check(td,
1733 PRIV_NETINET_SETHDROPTS);
1738 case IPV6_UNICAST_HOPS:
1741 case IPV6_RECVPKTINFO:
1742 case IPV6_RECVHOPLIMIT:
1743 case IPV6_RECVRTHDR:
1744 case IPV6_RECVPATHMTU:
1745 case IPV6_RECVTCLASS:
1746 case IPV6_RECVFLOWID:
1748 case IPV6_RECVRSSBUCKETID:
1751 case IPV6_AUTOFLOWLABEL:
1752 case IPV6_ORIGDSTADDR:
1754 case IPV6_BINDMULTI:
1756 case IPV6_RSS_LISTEN_BUCKET:
1759 if (optname == IPV6_BINDANY && td != NULL) {
1760 error = priv_check(td,
1761 PRIV_NETINET_BINDANY);
1766 if (optlen != sizeof(int)) {
1770 error = sooptcopyin(sopt, &optval,
1771 sizeof optval, sizeof optval);
1775 case IPV6_UNICAST_HOPS:
1776 if (optval < -1 || optval >= 256)
1779 /* -1 = kernel default */
1780 inp->in6p_hops = optval;
1781 if ((inp->inp_vflag &
1783 inp->inp_ip_ttl = optval;
1786 #define OPTSET(bit) \
1790 inp->inp_flags |= (bit); \
1792 inp->inp_flags &= ~(bit); \
1794 } while (/*CONSTCOND*/ 0)
1795 #define OPTSET2292(bit) \
1798 inp->inp_flags |= IN6P_RFC2292; \
1800 inp->inp_flags |= (bit); \
1802 inp->inp_flags &= ~(bit); \
1804 } while (/*CONSTCOND*/ 0)
1805 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0)
1807 #define OPTSET2_N(bit, val) do { \
1809 inp->inp_flags2 |= bit; \
1811 inp->inp_flags2 &= ~bit; \
1813 #define OPTSET2(bit, val) do { \
1815 OPTSET2_N(bit, val); \
1818 #define OPTBIT2(bit) (inp->inp_flags2 & (bit) ? 1 : 0)
1819 #define OPTSET2292_EXCLUSIVE(bit) \
1822 if (OPTBIT(IN6P_RFC2292)) { \
1826 inp->inp_flags |= (bit); \
1828 inp->inp_flags &= ~(bit); \
1831 } while (/*CONSTCOND*/ 0)
1833 case IPV6_RECVPKTINFO:
1834 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1839 struct ip6_pktopts **optp;
1841 /* cannot mix with RFC2292 */
1842 if (OPTBIT(IN6P_RFC2292)) {
1847 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1849 return (ECONNRESET);
1851 optp = &inp->in6p_outputopts;
1852 error = ip6_pcbopt(IPV6_HOPLIMIT,
1853 (u_char *)&optval, sizeof(optval),
1854 optp, (td != NULL) ? td->td_ucred :
1860 case IPV6_RECVHOPLIMIT:
1861 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1864 case IPV6_RECVHOPOPTS:
1865 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1868 case IPV6_RECVDSTOPTS:
1869 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1872 case IPV6_RECVRTHDRDSTOPTS:
1873 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1876 case IPV6_RECVRTHDR:
1877 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1880 case IPV6_RECVPATHMTU:
1882 * We ignore this option for TCP
1884 * (RFC3542 leaves this case
1887 if (uproto != IPPROTO_TCP)
1891 case IPV6_RECVFLOWID:
1892 OPTSET2(INP_RECVFLOWID, optval);
1896 case IPV6_RECVRSSBUCKETID:
1897 OPTSET2(INP_RECVRSSBUCKETID, optval);
1903 if (inp->inp_lport ||
1904 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1906 * The socket is already bound.
1913 inp->inp_flags |= IN6P_IPV6_V6ONLY;
1914 inp->inp_vflag &= ~INP_IPV4;
1916 inp->inp_flags &= ~IN6P_IPV6_V6ONLY;
1917 inp->inp_vflag |= INP_IPV4;
1921 case IPV6_RECVTCLASS:
1922 /* cannot mix with RFC2292 XXX */
1923 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1925 case IPV6_AUTOFLOWLABEL:
1926 OPTSET(IN6P_AUTOFLOWLABEL);
1929 case IPV6_ORIGDSTADDR:
1930 OPTSET2(INP_ORIGDSTADDR, optval);
1933 OPTSET(INP_BINDANY);
1936 case IPV6_BINDMULTI:
1937 OPTSET2(INP_BINDMULTI, optval);
1940 case IPV6_RSS_LISTEN_BUCKET:
1941 if ((optval >= 0) &&
1942 (optval < rss_getnumbuckets())) {
1944 inp->inp_rss_listen_bucket = optval;
1945 OPTSET2_N(INP_RSS_BUCKET_SET, 1);
1953 if ((optval >= -1) && (optval <=
1954 (INP_2PCP_MASK >> INP_2PCP_SHIFT))) {
1980 case IPV6_USE_MIN_MTU:
1981 case IPV6_PREFER_TEMPADDR:
1982 if (optlen != sizeof(optval)) {
1986 error = sooptcopyin(sopt, &optval,
1987 sizeof optval, sizeof optval);
1991 struct ip6_pktopts **optp;
1993 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1995 return (ECONNRESET);
1997 optp = &inp->in6p_outputopts;
1998 error = ip6_pcbopt(optname,
1999 (u_char *)&optval, sizeof(optval),
2000 optp, (td != NULL) ? td->td_ucred :
2006 case IPV6_2292PKTINFO:
2007 case IPV6_2292HOPLIMIT:
2008 case IPV6_2292HOPOPTS:
2009 case IPV6_2292DSTOPTS:
2010 case IPV6_2292RTHDR:
2012 if (optlen != sizeof(int)) {
2016 error = sooptcopyin(sopt, &optval,
2017 sizeof optval, sizeof optval);
2021 case IPV6_2292PKTINFO:
2022 OPTSET2292(IN6P_PKTINFO);
2024 case IPV6_2292HOPLIMIT:
2025 OPTSET2292(IN6P_HOPLIMIT);
2027 case IPV6_2292HOPOPTS:
2029 * Check super-user privilege.
2030 * See comments for IPV6_RECVHOPOPTS.
2033 error = priv_check(td,
2034 PRIV_NETINET_SETHDROPTS);
2038 OPTSET2292(IN6P_HOPOPTS);
2040 case IPV6_2292DSTOPTS:
2042 error = priv_check(td,
2043 PRIV_NETINET_SETHDROPTS);
2047 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
2049 case IPV6_2292RTHDR:
2050 OPTSET2292(IN6P_RTHDR);
2058 case IPV6_RTHDRDSTOPTS:
2061 /* new advanced API (RFC3542) */
2063 u_char optbuf_storage[MCLBYTES];
2065 struct ip6_pktopts **optp;
2067 /* cannot mix with RFC2292 */
2068 if (OPTBIT(IN6P_RFC2292)) {
2074 * We only ensure valsize is not too large
2075 * here. Further validation will be done
2078 error = sooptcopyin(sopt, optbuf_storage,
2079 sizeof(optbuf_storage), 0);
2082 optlen = sopt->sopt_valsize;
2083 optbuf = optbuf_storage;
2085 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2087 return (ECONNRESET);
2089 optp = &inp->in6p_outputopts;
2090 error = ip6_pcbopt(optname, optbuf, optlen,
2091 optp, (td != NULL) ? td->td_ucred : NULL,
2098 case IPV6_MULTICAST_IF:
2099 case IPV6_MULTICAST_HOPS:
2100 case IPV6_MULTICAST_LOOP:
2101 case IPV6_JOIN_GROUP:
2102 case IPV6_LEAVE_GROUP:
2104 case MCAST_BLOCK_SOURCE:
2105 case MCAST_UNBLOCK_SOURCE:
2106 case MCAST_JOIN_GROUP:
2107 case MCAST_LEAVE_GROUP:
2108 case MCAST_JOIN_SOURCE_GROUP:
2109 case MCAST_LEAVE_SOURCE_GROUP:
2110 error = ip6_setmoptions(inp, sopt);
2113 case IPV6_PORTRANGE:
2114 error = sooptcopyin(sopt, &optval,
2115 sizeof optval, sizeof optval);
2121 case IPV6_PORTRANGE_DEFAULT:
2122 inp->inp_flags &= ~(INP_LOWPORT);
2123 inp->inp_flags &= ~(INP_HIGHPORT);
2126 case IPV6_PORTRANGE_HIGH:
2127 inp->inp_flags &= ~(INP_LOWPORT);
2128 inp->inp_flags |= INP_HIGHPORT;
2131 case IPV6_PORTRANGE_LOW:
2132 inp->inp_flags &= ~(INP_HIGHPORT);
2133 inp->inp_flags |= INP_LOWPORT;
2143 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2144 case IPV6_IPSEC_POLICY:
2145 if (IPSEC_ENABLED(ipv6)) {
2146 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2153 error = ENOPROTOOPT;
2160 case IPV6_2292PKTOPTIONS:
2161 #ifdef IPV6_PKTOPTIONS
2162 case IPV6_PKTOPTIONS:
2165 * RFC3542 (effectively) deprecated the
2166 * semantics of the 2292-style pktoptions.
2167 * Since it was not reliable in nature (i.e.,
2168 * applications had to expect the lack of some
2169 * information after all), it would make sense
2170 * to simplify this part by always returning
2173 sopt->sopt_valsize = 0;
2176 case IPV6_RECVHOPOPTS:
2177 case IPV6_RECVDSTOPTS:
2178 case IPV6_RECVRTHDRDSTOPTS:
2179 case IPV6_UNICAST_HOPS:
2180 case IPV6_RECVPKTINFO:
2181 case IPV6_RECVHOPLIMIT:
2182 case IPV6_RECVRTHDR:
2183 case IPV6_RECVPATHMTU:
2186 case IPV6_PORTRANGE:
2187 case IPV6_RECVTCLASS:
2188 case IPV6_AUTOFLOWLABEL:
2192 case IPV6_RECVFLOWID:
2194 case IPV6_RSSBUCKETID:
2195 case IPV6_RECVRSSBUCKETID:
2197 case IPV6_BINDMULTI:
2200 case IPV6_RECVHOPOPTS:
2201 optval = OPTBIT(IN6P_HOPOPTS);
2204 case IPV6_RECVDSTOPTS:
2205 optval = OPTBIT(IN6P_DSTOPTS);
2208 case IPV6_RECVRTHDRDSTOPTS:
2209 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2212 case IPV6_UNICAST_HOPS:
2213 optval = inp->in6p_hops;
2216 case IPV6_RECVPKTINFO:
2217 optval = OPTBIT(IN6P_PKTINFO);
2220 case IPV6_RECVHOPLIMIT:
2221 optval = OPTBIT(IN6P_HOPLIMIT);
2224 case IPV6_RECVRTHDR:
2225 optval = OPTBIT(IN6P_RTHDR);
2228 case IPV6_RECVPATHMTU:
2229 optval = OPTBIT(IN6P_MTU);
2233 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2236 case IPV6_PORTRANGE:
2239 flags = inp->inp_flags;
2240 if (flags & INP_HIGHPORT)
2241 optval = IPV6_PORTRANGE_HIGH;
2242 else if (flags & INP_LOWPORT)
2243 optval = IPV6_PORTRANGE_LOW;
2248 case IPV6_RECVTCLASS:
2249 optval = OPTBIT(IN6P_TCLASS);
2252 case IPV6_AUTOFLOWLABEL:
2253 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2256 case IPV6_ORIGDSTADDR:
2257 optval = OPTBIT2(INP_ORIGDSTADDR);
2261 optval = OPTBIT(INP_BINDANY);
2265 optval = inp->inp_flowid;
2269 optval = inp->inp_flowtype;
2272 case IPV6_RECVFLOWID:
2273 optval = OPTBIT2(INP_RECVFLOWID);
2276 case IPV6_RSSBUCKETID:
2278 rss_hash2bucket(inp->inp_flowid,
2282 optval = rss_bucket;
2287 case IPV6_RECVRSSBUCKETID:
2288 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2292 case IPV6_BINDMULTI:
2293 optval = OPTBIT2(INP_BINDMULTI);
2297 if (OPTBIT2(INP_2PCP_SET)) {
2298 optval = (inp->inp_flags2 &
2309 error = sooptcopyout(sopt, &optval,
2316 struct ip6_mtuinfo mtuinfo;
2317 struct in6_addr addr;
2319 if (!(so->so_state & SS_ISCONNECTED))
2322 * XXX: we dot not consider the case of source
2323 * routing, or optional information to specify
2324 * the outgoing interface.
2325 * Copy faddr out of inp to avoid holding lock
2326 * on inp during route lookup.
2329 bcopy(&inp->in6p_faddr, &addr, sizeof(addr));
2331 error = ip6_getpmtu_ctl(so->so_fibnum,
2335 if (pmtu > IPV6_MAXPACKET)
2336 pmtu = IPV6_MAXPACKET;
2338 bzero(&mtuinfo, sizeof(mtuinfo));
2339 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2340 optdata = (void *)&mtuinfo;
2341 optdatalen = sizeof(mtuinfo);
2342 error = sooptcopyout(sopt, optdata,
2347 case IPV6_2292PKTINFO:
2348 case IPV6_2292HOPLIMIT:
2349 case IPV6_2292HOPOPTS:
2350 case IPV6_2292RTHDR:
2351 case IPV6_2292DSTOPTS:
2353 case IPV6_2292PKTINFO:
2354 optval = OPTBIT(IN6P_PKTINFO);
2356 case IPV6_2292HOPLIMIT:
2357 optval = OPTBIT(IN6P_HOPLIMIT);
2359 case IPV6_2292HOPOPTS:
2360 optval = OPTBIT(IN6P_HOPOPTS);
2362 case IPV6_2292RTHDR:
2363 optval = OPTBIT(IN6P_RTHDR);
2365 case IPV6_2292DSTOPTS:
2366 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2369 error = sooptcopyout(sopt, &optval,
2376 case IPV6_RTHDRDSTOPTS:
2380 case IPV6_USE_MIN_MTU:
2381 case IPV6_PREFER_TEMPADDR:
2382 error = ip6_getpcbopt(inp, optname, sopt);
2385 case IPV6_MULTICAST_IF:
2386 case IPV6_MULTICAST_HOPS:
2387 case IPV6_MULTICAST_LOOP:
2389 error = ip6_getmoptions(inp, sopt);
2392 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2393 case IPV6_IPSEC_POLICY:
2394 if (IPSEC_ENABLED(ipv6)) {
2395 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2401 error = ENOPROTOOPT;
2411 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2413 int error = 0, optval, optlen;
2414 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2415 struct inpcb *inp = sotoinpcb(so);
2416 int level, op, optname;
2418 level = sopt->sopt_level;
2419 op = sopt->sopt_dir;
2420 optname = sopt->sopt_name;
2421 optlen = sopt->sopt_valsize;
2423 if (level != IPPROTO_IPV6) {
2430 * For ICMPv6 sockets, no modification allowed for checksum
2431 * offset, permit "no change" values to help existing apps.
2433 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2434 * for an ICMPv6 socket will fail."
2435 * The current behavior does not meet RFC3542.
2439 if (optlen != sizeof(int)) {
2443 error = sooptcopyin(sopt, &optval, sizeof(optval),
2447 if (optval < -1 || (optval % 2) != 0) {
2449 * The API assumes non-negative even offset
2450 * values or -1 as a special value.
2453 } else if (inp->inp_ip_p == IPPROTO_ICMPV6) {
2454 if (optval != icmp6off)
2457 inp->in6p_cksum = optval;
2461 if (inp->inp_ip_p == IPPROTO_ICMPV6)
2464 optval = inp->in6p_cksum;
2466 error = sooptcopyout(sopt, &optval, sizeof(optval));
2476 error = ENOPROTOOPT;
2484 * Set up IP6 options in pcb for insertion in output packets or
2485 * specifying behavior of outgoing packets.
2488 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2489 struct socket *so, struct sockopt *sopt)
2491 struct ip6_pktopts *opt = *pktopt;
2493 struct thread *td = sopt->sopt_td;
2494 struct epoch_tracker et;
2496 /* turn off any old options. */
2499 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2500 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2501 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2502 printf("ip6_pcbopts: all specified options are cleared.\n");
2504 ip6_clearpktopts(opt, -1);
2506 opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT);
2512 if (!m || m->m_len == 0) {
2514 * Only turning off any previous options, regardless of
2515 * whether the opt is just created or given.
2517 free(opt, M_IP6OPT);
2521 /* set options specified by user. */
2522 NET_EPOCH_ENTER(et);
2523 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2524 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2525 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2526 free(opt, M_IP6OPT);
2536 * initialize ip6_pktopts. beware that there are non-zero default values in
2540 ip6_initpktopts(struct ip6_pktopts *opt)
2543 bzero(opt, sizeof(*opt));
2544 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2545 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2546 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2547 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2551 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2552 struct ucred *cred, int uproto)
2554 struct epoch_tracker et;
2555 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 NET_EPOCH_ENTER(et);
2568 ret = ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto);
2574 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2575 if (pktopt && pktopt->field) { \
2577 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2578 malloc_optdata = true; \
2580 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
2582 free(optdata, M_TEMP); \
2583 return (ECONNRESET); \
2585 pktopt = inp->in6p_outputopts; \
2586 if (pktopt && pktopt->field) { \
2587 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2588 bcopy(pktopt->field, optdata, optdatalen); \
2590 free(optdata, M_TEMP); \
2592 malloc_optdata = false; \
2597 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2598 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2600 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2601 pktopt->field->sa_len)
2604 ip6_getpcbopt(struct inpcb *inp, int optname, struct sockopt *sopt)
2606 void *optdata = NULL;
2607 bool malloc_optdata = false;
2610 struct in6_pktinfo null_pktinfo;
2611 int deftclass = 0, on;
2612 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2613 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2614 struct ip6_pktopts *pktopt;
2617 pktopt = inp->in6p_outputopts;
2621 optdata = (void *)&null_pktinfo;
2622 if (pktopt && pktopt->ip6po_pktinfo) {
2623 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2624 sizeof(null_pktinfo));
2625 in6_clearscope(&null_pktinfo.ipi6_addr);
2627 /* XXX: we don't have to do this every time... */
2628 bzero(&null_pktinfo, sizeof(null_pktinfo));
2630 optdatalen = sizeof(struct in6_pktinfo);
2633 if (pktopt && pktopt->ip6po_tclass >= 0)
2634 deftclass = pktopt->ip6po_tclass;
2635 optdata = (void *)&deftclass;
2636 optdatalen = sizeof(int);
2639 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2642 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2644 case IPV6_RTHDRDSTOPTS:
2645 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2648 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2651 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2653 case IPV6_USE_MIN_MTU:
2655 defminmtu = pktopt->ip6po_minmtu;
2656 optdata = (void *)&defminmtu;
2657 optdatalen = sizeof(int);
2660 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2664 optdata = (void *)&on;
2665 optdatalen = sizeof(on);
2667 case IPV6_PREFER_TEMPADDR:
2669 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2670 optdata = (void *)&defpreftemp;
2671 optdatalen = sizeof(int);
2673 default: /* should not happen */
2675 panic("ip6_getpcbopt: unexpected option\n");
2678 return (ENOPROTOOPT);
2682 error = sooptcopyout(sopt, optdata, optdatalen);
2684 free(optdata, M_TEMP);
2690 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2695 if (optname == -1 || optname == IPV6_PKTINFO) {
2696 if (pktopt->ip6po_pktinfo)
2697 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2698 pktopt->ip6po_pktinfo = NULL;
2700 if (optname == -1 || optname == IPV6_HOPLIMIT)
2701 pktopt->ip6po_hlim = -1;
2702 if (optname == -1 || optname == IPV6_TCLASS)
2703 pktopt->ip6po_tclass = -1;
2704 if (optname == -1 || optname == IPV6_NEXTHOP) {
2705 if (pktopt->ip6po_nextroute.ro_nh) {
2706 NH_FREE(pktopt->ip6po_nextroute.ro_nh);
2707 pktopt->ip6po_nextroute.ro_nh = NULL;
2709 if (pktopt->ip6po_nexthop)
2710 free(pktopt->ip6po_nexthop, M_IP6OPT);
2711 pktopt->ip6po_nexthop = NULL;
2713 if (optname == -1 || optname == IPV6_HOPOPTS) {
2714 if (pktopt->ip6po_hbh)
2715 free(pktopt->ip6po_hbh, M_IP6OPT);
2716 pktopt->ip6po_hbh = NULL;
2718 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2719 if (pktopt->ip6po_dest1)
2720 free(pktopt->ip6po_dest1, M_IP6OPT);
2721 pktopt->ip6po_dest1 = NULL;
2723 if (optname == -1 || optname == IPV6_RTHDR) {
2724 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2725 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2726 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2727 if (pktopt->ip6po_route.ro_nh) {
2728 NH_FREE(pktopt->ip6po_route.ro_nh);
2729 pktopt->ip6po_route.ro_nh = NULL;
2732 if (optname == -1 || optname == IPV6_DSTOPTS) {
2733 if (pktopt->ip6po_dest2)
2734 free(pktopt->ip6po_dest2, M_IP6OPT);
2735 pktopt->ip6po_dest2 = NULL;
2739 #define PKTOPT_EXTHDRCPY(type) \
2742 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2743 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2744 if (dst->type == NULL)\
2746 bcopy(src->type, dst->type, hlen);\
2748 } while (/*CONSTCOND*/ 0)
2751 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2753 if (dst == NULL || src == NULL) {
2754 printf("ip6_clearpktopts: invalid argument\n");
2758 dst->ip6po_hlim = src->ip6po_hlim;
2759 dst->ip6po_tclass = src->ip6po_tclass;
2760 dst->ip6po_flags = src->ip6po_flags;
2761 dst->ip6po_minmtu = src->ip6po_minmtu;
2762 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2763 if (src->ip6po_pktinfo) {
2764 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2766 if (dst->ip6po_pktinfo == NULL)
2768 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2770 if (src->ip6po_nexthop) {
2771 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2773 if (dst->ip6po_nexthop == NULL)
2775 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2776 src->ip6po_nexthop->sa_len);
2778 PKTOPT_EXTHDRCPY(ip6po_hbh);
2779 PKTOPT_EXTHDRCPY(ip6po_dest1);
2780 PKTOPT_EXTHDRCPY(ip6po_dest2);
2781 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2785 ip6_clearpktopts(dst, -1);
2788 #undef PKTOPT_EXTHDRCPY
2790 struct ip6_pktopts *
2791 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2794 struct ip6_pktopts *dst;
2796 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2799 ip6_initpktopts(dst);
2801 if ((error = copypktopts(dst, src, canwait)) != 0) {
2802 free(dst, M_IP6OPT);
2810 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2815 ip6_clearpktopts(pktopt, -1);
2817 free(pktopt, M_IP6OPT);
2821 * Set IPv6 outgoing packet options based on advanced API.
2824 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2825 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2827 struct cmsghdr *cm = NULL;
2829 if (control == NULL || opt == NULL)
2833 * ip6_setpktopt can call ifnet_byindex(), so it's imperative that we
2834 * are in the network epoch here.
2838 ip6_initpktopts(opt);
2843 * If stickyopt is provided, make a local copy of the options
2844 * for this particular packet, then override them by ancillary
2846 * XXX: copypktopts() does not copy the cached route to a next
2847 * hop (if any). This is not very good in terms of efficiency,
2848 * but we can allow this since this option should be rarely
2851 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2856 * XXX: Currently, we assume all the optional information is stored
2859 if (control->m_next)
2862 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2863 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2866 if (control->m_len < CMSG_LEN(0))
2869 cm = mtod(control, struct cmsghdr *);
2870 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2872 if (cm->cmsg_level != IPPROTO_IPV6)
2875 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2876 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2885 * Set a particular packet option, as a sticky option or an ancillary data
2886 * item. "len" can be 0 only when it's a sticky option.
2887 * We have 4 cases of combination of "sticky" and "cmsg":
2888 * "sticky=0, cmsg=0": impossible
2889 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2890 * "sticky=1, cmsg=0": RFC3542 socket option
2891 * "sticky=1, cmsg=1": RFC2292 socket option
2894 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2895 struct ucred *cred, int sticky, int cmsg, int uproto)
2897 int minmtupolicy, preftemp;
2902 if (!sticky && !cmsg) {
2904 printf("ip6_setpktopt: impossible case\n");
2910 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2911 * not be specified in the context of RFC3542. Conversely,
2912 * RFC3542 types should not be specified in the context of RFC2292.
2916 case IPV6_2292PKTINFO:
2917 case IPV6_2292HOPLIMIT:
2918 case IPV6_2292NEXTHOP:
2919 case IPV6_2292HOPOPTS:
2920 case IPV6_2292DSTOPTS:
2921 case IPV6_2292RTHDR:
2922 case IPV6_2292PKTOPTIONS:
2923 return (ENOPROTOOPT);
2926 if (sticky && cmsg) {
2933 case IPV6_RTHDRDSTOPTS:
2935 case IPV6_USE_MIN_MTU:
2938 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2939 return (ENOPROTOOPT);
2944 case IPV6_2292PKTINFO:
2947 struct ifnet *ifp = NULL;
2948 struct in6_pktinfo *pktinfo;
2950 if (len != sizeof(struct in6_pktinfo))
2953 pktinfo = (struct in6_pktinfo *)buf;
2956 * An application can clear any sticky IPV6_PKTINFO option by
2957 * doing a "regular" setsockopt with ipi6_addr being
2958 * in6addr_any and ipi6_ifindex being zero.
2959 * [RFC 3542, Section 6]
2961 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2962 pktinfo->ipi6_ifindex == 0 &&
2963 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2964 ip6_clearpktopts(opt, optname);
2968 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2969 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2972 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2974 /* validate the interface index if specified. */
2975 if (pktinfo->ipi6_ifindex) {
2976 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2980 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2981 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2985 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2986 struct in6_ifaddr *ia;
2988 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2989 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2991 return (EADDRNOTAVAIL);
2992 ifa_free(&ia->ia_ifa);
2995 * We store the address anyway, and let in6_selectsrc()
2996 * validate the specified address. This is because ipi6_addr
2997 * may not have enough information about its scope zone, and
2998 * we may need additional information (such as outgoing
2999 * interface or the scope zone of a destination address) to
3000 * disambiguate the scope.
3001 * XXX: the delay of the validation may confuse the
3002 * application when it is used as a sticky option.
3004 if (opt->ip6po_pktinfo == NULL) {
3005 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
3006 M_IP6OPT, M_NOWAIT);
3007 if (opt->ip6po_pktinfo == NULL)
3010 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
3014 case IPV6_2292HOPLIMIT:
3020 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3021 * to simplify the ordering among hoplimit options.
3023 if (optname == IPV6_HOPLIMIT && sticky)
3024 return (ENOPROTOOPT);
3026 if (len != sizeof(int))
3029 if (*hlimp < -1 || *hlimp > 255)
3032 opt->ip6po_hlim = *hlimp;
3040 if (len != sizeof(int))
3042 tclass = *(int *)buf;
3043 if (tclass < -1 || tclass > 255)
3046 opt->ip6po_tclass = tclass;
3050 case IPV6_2292NEXTHOP:
3053 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3058 if (len == 0) { /* just remove the option */
3059 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3063 /* check if cmsg_len is large enough for sa_len */
3064 if (len < sizeof(struct sockaddr) || len < *buf)
3067 switch (((struct sockaddr *)buf)->sa_family) {
3070 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3073 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3076 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3077 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3080 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
3086 case AF_LINK: /* should eventually be supported */
3088 return (EAFNOSUPPORT);
3091 /* turn off the previous option, then set the new option. */
3092 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3093 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3094 if (opt->ip6po_nexthop == NULL)
3096 bcopy(buf, opt->ip6po_nexthop, *buf);
3099 case IPV6_2292HOPOPTS:
3102 struct ip6_hbh *hbh;
3106 * XXX: We don't allow a non-privileged user to set ANY HbH
3107 * options, since per-option restriction has too much
3111 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3117 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3118 break; /* just remove the option */
3121 /* message length validation */
3122 if (len < sizeof(struct ip6_hbh))
3124 hbh = (struct ip6_hbh *)buf;
3125 hbhlen = (hbh->ip6h_len + 1) << 3;
3129 /* turn off the previous option, then set the new option. */
3130 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3131 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3132 if (opt->ip6po_hbh == NULL)
3134 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3139 case IPV6_2292DSTOPTS:
3141 case IPV6_RTHDRDSTOPTS:
3143 struct ip6_dest *dest, **newdest = NULL;
3146 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
3147 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3153 ip6_clearpktopts(opt, optname);
3154 break; /* just remove the option */
3157 /* message length validation */
3158 if (len < sizeof(struct ip6_dest))
3160 dest = (struct ip6_dest *)buf;
3161 destlen = (dest->ip6d_len + 1) << 3;
3166 * Determine the position that the destination options header
3167 * should be inserted; before or after the routing header.
3170 case IPV6_2292DSTOPTS:
3172 * The old advacned API is ambiguous on this point.
3173 * Our approach is to determine the position based
3174 * according to the existence of a routing header.
3175 * Note, however, that this depends on the order of the
3176 * extension headers in the ancillary data; the 1st
3177 * part of the destination options header must appear
3178 * before the routing header in the ancillary data,
3180 * RFC3542 solved the ambiguity by introducing
3181 * separate ancillary data or option types.
3183 if (opt->ip6po_rthdr == NULL)
3184 newdest = &opt->ip6po_dest1;
3186 newdest = &opt->ip6po_dest2;
3188 case IPV6_RTHDRDSTOPTS:
3189 newdest = &opt->ip6po_dest1;
3192 newdest = &opt->ip6po_dest2;
3196 /* turn off the previous option, then set the new option. */
3197 ip6_clearpktopts(opt, optname);
3198 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3199 if (*newdest == NULL)
3201 bcopy(dest, *newdest, destlen);
3206 case IPV6_2292RTHDR:
3209 struct ip6_rthdr *rth;
3213 ip6_clearpktopts(opt, IPV6_RTHDR);
3214 break; /* just remove the option */
3217 /* message length validation */
3218 if (len < sizeof(struct ip6_rthdr))
3220 rth = (struct ip6_rthdr *)buf;
3221 rthlen = (rth->ip6r_len + 1) << 3;
3225 switch (rth->ip6r_type) {
3226 case IPV6_RTHDR_TYPE_0:
3227 if (rth->ip6r_len == 0) /* must contain one addr */
3229 if (rth->ip6r_len % 2) /* length must be even */
3231 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3235 return (EINVAL); /* not supported */
3238 /* turn off the previous option */
3239 ip6_clearpktopts(opt, IPV6_RTHDR);
3240 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3241 if (opt->ip6po_rthdr == NULL)
3243 bcopy(rth, opt->ip6po_rthdr, rthlen);
3248 case IPV6_USE_MIN_MTU:
3249 if (len != sizeof(int))
3251 minmtupolicy = *(int *)buf;
3252 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3253 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3254 minmtupolicy != IP6PO_MINMTU_ALL) {
3257 opt->ip6po_minmtu = minmtupolicy;
3261 if (len != sizeof(int))
3264 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3266 * we ignore this option for TCP sockets.
3267 * (RFC3542 leaves this case unspecified.)
3269 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3271 opt->ip6po_flags |= IP6PO_DONTFRAG;
3274 case IPV6_PREFER_TEMPADDR:
3275 if (len != sizeof(int))
3277 preftemp = *(int *)buf;
3278 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3279 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3280 preftemp != IP6PO_TEMPADDR_PREFER) {
3283 opt->ip6po_prefer_tempaddr = preftemp;
3287 return (ENOPROTOOPT);
3288 } /* end of switch */
3294 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3295 * packet to the input queue of a specified interface. Note that this
3296 * calls the output routine of the loopback "driver", but with an interface
3297 * pointer that might NOT be &loif -- easier than replicating that code here.
3300 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3303 struct ip6_hdr *ip6;
3305 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3310 * Make sure to deep-copy IPv6 header portion in case the data
3311 * is in an mbuf cluster, so that we can safely override the IPv6
3312 * header portion later.
3314 if (!M_WRITABLE(copym) ||
3315 copym->m_len < sizeof(struct ip6_hdr)) {
3316 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3320 ip6 = mtod(copym, struct ip6_hdr *);
3322 * clear embedded scope identifiers if necessary.
3323 * in6_clearscope will touch the addresses only when necessary.
3325 in6_clearscope(&ip6->ip6_src);
3326 in6_clearscope(&ip6->ip6_dst);
3327 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3328 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3330 copym->m_pkthdr.csum_data = 0xffff;
3332 if_simloop(ifp, copym, AF_INET6, 0);
3336 * Chop IPv6 header off from the payload.
3339 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3342 struct ip6_hdr *ip6;
3344 ip6 = mtod(m, struct ip6_hdr *);
3345 if (m->m_len > sizeof(*ip6)) {
3346 mh = m_gethdr(M_NOWAIT, MT_DATA);
3351 m_move_pkthdr(mh, m);
3352 M_ALIGN(mh, sizeof(*ip6));
3353 m->m_len -= sizeof(*ip6);
3354 m->m_data += sizeof(*ip6);
3357 m->m_len = sizeof(*ip6);
3358 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3360 exthdrs->ip6e_ip6 = m;
3365 * Compute IPv6 extension header length.
3368 ip6_optlen(struct inpcb *inp)
3372 if (!inp->in6p_outputopts)
3377 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3379 len += elen(inp->in6p_outputopts->ip6po_hbh);
3380 if (inp->in6p_outputopts->ip6po_rthdr)
3381 /* dest1 is valid with rthdr only */
3382 len += elen(inp->in6p_outputopts->ip6po_dest1);
3383 len += elen(inp->in6p_outputopts->ip6po_rthdr);
3384 len += elen(inp->in6p_outputopts->ip6po_dest2);