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)
220 KASSERT((plen >= optlen), ("%s:%d: plen %d < optlen %d, m %p, ifp %p "
222 __func__, __LINE__, plen, optlen, m, ifp, csum_flags));
224 if (csum_flags & CSUM_DELAY_DATA_IPV6) {
225 in6_delayed_cksum(m, plen - optlen,
226 sizeof(struct ip6_hdr) + optlen);
227 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
229 #if defined(SCTP) || defined(SCTP_SUPPORT)
230 if (csum_flags & CSUM_SCTP_IPV6) {
231 sctp_delayed_cksum(m, sizeof(struct ip6_hdr) + optlen);
232 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
238 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
239 int fraglen , uint32_t id)
241 struct mbuf *m, **mnext, *m_frgpart;
242 struct ip6_hdr *ip6, *mhip6;
243 struct ip6_frag *ip6f;
246 int tlen = m0->m_pkthdr.len;
248 KASSERT((fraglen % 8 == 0), ("Fragment length must be a multiple of 8"));
251 ip6 = mtod(m, struct ip6_hdr *);
252 mnext = &m->m_nextpkt;
254 for (off = hlen; off < tlen; off += fraglen) {
255 m = m_gethdr(M_NOWAIT, MT_DATA);
257 IP6STAT_INC(ip6s_odropped);
262 * Make sure the complete packet header gets copied
263 * from the originating mbuf to the newly created
264 * mbuf. This also ensures that existing firewall
265 * classification(s), VLAN tags and so on get copied
266 * to the resulting fragmented packet(s):
268 if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
270 IP6STAT_INC(ip6s_odropped);
275 mnext = &m->m_nextpkt;
276 m->m_data += max_linkhdr;
277 mhip6 = mtod(m, struct ip6_hdr *);
279 m->m_len = sizeof(*mhip6);
280 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
282 IP6STAT_INC(ip6s_odropped);
285 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
286 if (off + fraglen >= tlen)
287 fraglen = tlen - off;
289 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
290 mhip6->ip6_plen = htons((u_short)(fraglen + hlen +
291 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
292 if ((m_frgpart = m_copym(m0, off, fraglen, M_NOWAIT)) == NULL) {
293 IP6STAT_INC(ip6s_odropped);
297 m->m_pkthdr.len = fraglen + hlen + sizeof(*ip6f);
298 ip6f->ip6f_reserved = 0;
299 ip6f->ip6f_ident = id;
300 ip6f->ip6f_nxt = nextproto;
301 IP6STAT_INC(ip6s_ofragments);
302 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
309 ip6_output_send(struct inpcb *inp, struct ifnet *ifp, struct ifnet *origifp,
310 struct mbuf *m, struct sockaddr_in6 *dst, struct route_in6 *ro,
314 struct ktls_session *tls = NULL;
316 struct m_snd_tag *mst;
319 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
324 * If this is an unencrypted TLS record, save a reference to
325 * the record. This local reference is used to call
326 * ktls_output_eagain after the mbuf has been freed (thus
327 * dropping the mbuf's reference) in if_output.
329 if (m->m_next != NULL && mbuf_has_tls_session(m->m_next)) {
330 tls = ktls_hold(m->m_next->m_epg_tls);
334 * If a TLS session doesn't have a valid tag, it must
335 * 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) {
367 /* stamp send tag on mbuf */
368 m->m_pkthdr.snd_tag = m_snd_tag_ref(mst);
369 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
372 error = nd6_output_ifp(ifp, origifp, m, dst, (struct route *)ro);
375 /* Check for route change invalidating send tags. */
379 error = ktls_output_eagain(inp, tls);
385 in_pcboutput_eagain(inp);
392 * The packet in mbuf chain m contains a skeletal IP6 header (with pri, len,
393 * nxt, hlim, src, dst).
394 * This function may modify ver and hlim only.
395 * The mbuf chain containing the packet will be freed.
396 * The mbuf opt, if present, will not be freed.
397 * If route_in6 ro is present and has ro_nh initialized, route lookup would be
398 * skipped and ro->ro_nh would be used. If ro is present but ro->ro_nh is NULL,
399 * then result of route lookup is stored in ro->ro_nh.
401 * Type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and nd_ifinfo.linkmtu
402 * is uint32_t. So we use u_long to hold largest one, which is rt_mtu.
404 * ifpp - XXX: just for statistics
407 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
408 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
409 struct ifnet **ifpp, struct inpcb *inp)
412 struct ifnet *ifp, *origifp;
415 struct route_in6 *ro_pmtu;
416 struct nhop_object *nh;
417 struct sockaddr_in6 *dst, sin6, src_sa, dst_sa;
418 struct in6_addr odst;
423 struct in6_ifaddr *ia = NULL;
425 int alwaysfrag, dontfrag;
426 u_int32_t optlen, plen = 0, unfragpartlen;
427 struct ip6_exthdrs exthdrs;
428 struct in6_addr src0, dst0;
434 struct m_tag *fwd_tag = NULL;
440 INP_LOCK_ASSERT(inp);
441 M_SETFIB(m, inp->inp_inc.inc_fibnum);
442 if ((flags & IP_NODEFAULTFLOWID) == 0) {
443 /* Unconditionally set flowid. */
444 m->m_pkthdr.flowid = inp->inp_flowid;
445 M_HASHTYPE_SET(m, inp->inp_flowtype);
447 if ((inp->inp_flags2 & INP_2PCP_SET) != 0)
448 vlan_pcp = (inp->inp_flags2 & INP_2PCP_MASK) >>
451 m->m_pkthdr.numa_domain = inp->inp_numa_domain;
455 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
457 * IPSec checking which handles several cases.
458 * FAST IPSEC: We re-injected the packet.
459 * XXX: need scope argument.
461 if (IPSEC_ENABLED(ipv6)) {
462 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
463 if (error == EINPROGRESS)
470 /* Source address validation. */
471 ip6 = mtod(m, struct ip6_hdr *);
472 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
473 (flags & IPV6_UNSPECSRC) == 0) {
475 IP6STAT_INC(ip6s_badscope);
478 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
480 IP6STAT_INC(ip6s_badscope);
485 * If we are given packet options to add extension headers prepare them.
486 * Calculate the total length of the extension header chain.
487 * Keep the length of the unfragmentable part for fragmentation.
489 bzero(&exthdrs, sizeof(exthdrs));
491 unfragpartlen = sizeof(struct ip6_hdr);
493 /* Hop-by-Hop options header. */
494 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh, optlen);
496 /* Destination options header (1st part). */
497 if (opt->ip6po_rthdr) {
498 #ifndef RTHDR_SUPPORT_IMPLEMENTED
500 * If there is a routing header, discard the packet
501 * right away here. RH0/1 are obsolete and we do not
502 * currently support RH2/3/4.
503 * People trying to use RH253/254 may want to disable
505 * The moment we do support any routing header (again)
506 * this block should check the routing type more
514 * Destination options header (1st part).
515 * This only makes sense with a routing header.
516 * See Section 9.2 of RFC 3542.
517 * Disabling this part just for MIP6 convenience is
518 * a bad idea. We need to think carefully about a
519 * way to make the advanced API coexist with MIP6
520 * options, which might automatically be inserted in
523 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1,
526 /* Routing header. */
527 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr, optlen);
529 unfragpartlen += optlen;
532 * NOTE: we don't add AH/ESP length here (done in
533 * ip6_ipsec_output()).
536 /* Destination options header (2nd part). */
537 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2, optlen);
541 * If there is at least one extension header,
542 * separate IP6 header from the payload.
546 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
550 m = exthdrs.ip6e_ip6;
551 ip6 = mtod(m, struct ip6_hdr *);
555 /* Adjust mbuf packet header length. */
556 m->m_pkthdr.len += optlen;
557 plen = m->m_pkthdr.len - sizeof(*ip6);
559 /* If this is a jumbo payload, insert a jumbo payload option. */
560 if (plen > IPV6_MAXPACKET) {
562 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
566 m = exthdrs.ip6e_ip6;
567 ip6 = mtod(m, struct ip6_hdr *);
570 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
574 ip6->ip6_plen = htons(plen);
575 nexthdrp = &ip6->ip6_nxt;
579 * Concatenate headers and fill in next header fields.
580 * Here we have, on "m"
582 * and we insert headers accordingly.
583 * Finally, we should be getting:
584 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload].
586 * During the header composing process "m" points to IPv6
587 * header. "mprev" points to an extension header prior to esp.
592 * We treat dest2 specially. This makes IPsec processing
593 * much easier. The goal here is to make mprev point the
594 * mbuf prior to dest2.
596 * Result: IPv6 dest2 payload.
597 * m and mprev will point to IPv6 header.
599 if (exthdrs.ip6e_dest2) {
601 panic("%s:%d: assumption failed: "
602 "hdr not split: hdrsplit %d exthdrs %p",
603 __func__, __LINE__, hdrsplit, &exthdrs);
604 exthdrs.ip6e_dest2->m_next = m->m_next;
605 m->m_next = exthdrs.ip6e_dest2;
606 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
607 ip6->ip6_nxt = IPPROTO_DSTOPTS;
611 * Result: IPv6 hbh dest1 rthdr dest2 payload.
612 * m will point to IPv6 header. mprev will point to the
613 * extension header prior to dest2 (rthdr in the above case).
615 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
616 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
618 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
622 IP6STAT_INC(ip6s_localout);
626 if (opt && opt->ip6po_rthdr)
627 ro = &opt->ip6po_route;
629 dst = (struct sockaddr_in6 *)&ro->ro_dst;
632 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
636 * If specified, try to fill in the traffic class field.
637 * Do not override if a non-zero value is already set.
638 * We check the diffserv field and the ECN field separately.
640 if (opt && opt->ip6po_tclass >= 0) {
643 if (IPV6_DSCP(ip6) == 0)
645 if (IPV6_ECN(ip6) == 0)
648 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
651 /* Fill in or override the hop limit field, if necessary. */
652 if (opt && opt->ip6po_hlim != -1)
653 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
654 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
656 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
658 ip6->ip6_hlim = V_ip6_defmcasthlim;
661 if (ro == NULL || ro->ro_nh == NULL) {
662 bzero(dst, sizeof(*dst));
663 dst->sin6_family = AF_INET6;
664 dst->sin6_len = sizeof(*dst);
665 dst->sin6_addr = ip6->ip6_dst;
668 * Validate route against routing table changes.
669 * Make sure that the address family is set in route.
675 if (ro->ro_nh != NULL && inp != NULL) {
676 ro->ro_dst.sin6_family = AF_INET6; /* XXX KASSERT? */
677 NH_VALIDATE((struct route *)ro, &inp->inp_rt_cookie,
680 if (ro->ro_nh != NULL && fwd_tag == NULL &&
681 (!NH_IS_VALID(ro->ro_nh) ||
682 ro->ro_dst.sin6_family != AF_INET6 ||
683 !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)))
684 RO_INVALIDATE_CACHE(ro);
686 if (ro->ro_nh != NULL && fwd_tag == NULL &&
687 ro->ro_dst.sin6_family == AF_INET6 &&
688 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
693 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
695 if (fwd_tag == NULL) {
696 bzero(&dst_sa, sizeof(dst_sa));
697 dst_sa.sin6_family = AF_INET6;
698 dst_sa.sin6_len = sizeof(dst_sa);
699 dst_sa.sin6_addr = ip6->ip6_dst;
701 error = in6_selectroute(&dst_sa, opt, im6o, ro, &ifp,
702 &nh, fibnum, m->m_pkthdr.flowid);
704 IP6STAT_INC(ip6s_noroute);
706 in6_ifstat_inc(ifp, ifs6_out_discard);
714 * If in6_selectroute() does not return a nexthop
715 * dst may not have been updated.
717 *dst = dst_sa; /* XXX */
719 if (nh->nh_flags & NHF_HOST)
721 ia = (struct in6_ifaddr *)(nh->nh_ifa);
722 counter_u64_add(nh->nh_pksent, 1);
725 struct nhop_object *nh;
726 struct in6_addr kdst;
729 if (fwd_tag == NULL) {
730 bzero(&dst_sa, sizeof(dst_sa));
731 dst_sa.sin6_family = AF_INET6;
732 dst_sa.sin6_len = sizeof(dst_sa);
733 dst_sa.sin6_addr = ip6->ip6_dst;
736 if (IN6_IS_ADDR_MULTICAST(&dst_sa.sin6_addr) &&
738 (ifp = im6o->im6o_multicast_ifp) != NULL) {
739 /* We do not need a route lookup. */
740 *dst = dst_sa; /* XXX */
744 in6_splitscope(&dst_sa.sin6_addr, &kdst, &scopeid);
746 if (IN6_IS_ADDR_MC_LINKLOCAL(&dst_sa.sin6_addr) ||
747 IN6_IS_ADDR_MC_NODELOCAL(&dst_sa.sin6_addr)) {
749 ifp = in6_getlinkifnet(scopeid);
751 error = EHOSTUNREACH;
754 *dst = dst_sa; /* XXX */
759 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE,
762 IP6STAT_INC(ip6s_noroute);
763 /* No ifp in6_ifstat_inc(ifp, ifs6_out_discard); */
764 error = EHOSTUNREACH;;
770 ia = ifatoia6(nh->nh_ifa);
771 if (nh->nh_flags & NHF_GATEWAY)
772 dst->sin6_addr = nh->gw6_sa.sin6_addr;
777 /* Then nh (for unicast) and ifp must be non-NULL valid values. */
778 if ((flags & IPV6_FORWARDING) == 0) {
779 /* XXX: the FORWARDING flag can be set for mrouting. */
780 in6_ifstat_inc(ifp, ifs6_out_request);
783 /* Setup data structures for scope ID checks. */
785 bzero(&src_sa, sizeof(src_sa));
786 src_sa.sin6_family = AF_INET6;
787 src_sa.sin6_len = sizeof(src_sa);
788 src_sa.sin6_addr = ip6->ip6_src;
791 /* Re-initialize to be sure. */
792 bzero(&dst_sa, sizeof(dst_sa));
793 dst_sa.sin6_family = AF_INET6;
794 dst_sa.sin6_len = sizeof(dst_sa);
795 dst_sa.sin6_addr = ip6->ip6_dst;
797 /* Check for valid scope ID. */
798 if (in6_setscope(&src0, ifp, &zone) == 0 &&
799 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
800 in6_setscope(&dst0, ifp, &zone) == 0 &&
801 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
803 * The outgoing interface is in the zone of the source
804 * and destination addresses.
806 * Because the loopback interface cannot receive
807 * packets with a different scope ID than its own,
808 * there is a trick to pretend the outgoing packet
809 * was received by the real network interface, by
810 * setting "origifp" different from "ifp". This is
811 * only allowed when "ifp" is a loopback network
812 * interface. Refer to code in nd6_output_ifp() for
818 * We should use ia_ifp to support the case of sending
819 * packets to an address of our own.
821 if (ia != NULL && ia->ia_ifp)
824 } else if ((ifp->if_flags & IFF_LOOPBACK) == 0 ||
825 sa6_recoverscope(&src_sa) != 0 ||
826 sa6_recoverscope(&dst_sa) != 0 ||
827 dst_sa.sin6_scope_id == 0 ||
828 (src_sa.sin6_scope_id != 0 &&
829 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
830 (origifp = ifnet_byindex(dst_sa.sin6_scope_id)) == NULL) {
832 * If the destination network interface is not a
833 * loopback interface, or the destination network
834 * address has no scope ID, or the source address has
835 * a scope ID set which is different from the
836 * destination address one, or there is no network
837 * interface representing this scope ID, the address
838 * pair is considered invalid.
840 IP6STAT_INC(ip6s_badscope);
841 in6_ifstat_inc(ifp, ifs6_out_discard);
843 error = EHOSTUNREACH; /* XXX */
846 /* All scope ID checks are successful. */
848 if (nh && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
849 if (opt && opt->ip6po_nextroute.ro_nh) {
851 * The nexthop is explicitly specified by the
852 * application. We assume the next hop is an IPv6
855 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
857 else if ((nh->nh_flags & NHF_GATEWAY))
861 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
862 m->m_flags &= ~(M_BCAST | M_MCAST); /* Just in case. */
864 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
865 in6_ifstat_inc(ifp, ifs6_out_mcast);
867 /* Confirm that the outgoing interface supports multicast. */
868 if (!(ifp->if_flags & IFF_MULTICAST)) {
869 IP6STAT_INC(ip6s_noroute);
870 in6_ifstat_inc(ifp, ifs6_out_discard);
874 if ((im6o == NULL && in6_mcast_loop) ||
875 (im6o && im6o->im6o_multicast_loop)) {
877 * Loop back multicast datagram if not expressly
878 * forbidden to do so, even if we have not joined
879 * the address; protocols will filter it later,
880 * thus deferring a hash lookup and lock acquisition
881 * at the expense of an m_copym().
883 ip6_mloopback(ifp, m);
886 * If we are acting as a multicast router, perform
887 * multicast forwarding as if the packet had just
888 * arrived on the interface to which we are about
889 * to send. The multicast forwarding function
890 * recursively calls this function, using the
891 * IPV6_FORWARDING flag to prevent infinite recursion.
893 * Multicasts that are looped back by ip6_mloopback(),
894 * above, will be forwarded by the ip6_input() routine,
897 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
899 * XXX: ip6_mforward expects that rcvif is NULL
900 * when it is called from the originating path.
901 * However, it may not always be the case.
903 m->m_pkthdr.rcvif = NULL;
904 if (ip6_mforward(ip6, ifp, m) != 0) {
911 * Multicasts with a hoplimit of zero may be looped back,
912 * above, but must not be transmitted on a network.
913 * Also, multicasts addressed to the loopback interface
914 * are not sent -- the above call to ip6_mloopback() will
915 * loop back a copy if this host actually belongs to the
916 * destination group on the loopback interface.
918 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
919 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
926 * Fill the outgoing inteface to tell the upper layer
927 * to increment per-interface statistics.
932 /* Determine path MTU. */
933 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
934 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
936 KASSERT(mtu > 0, ("%s:%d: mtu %ld, ro_pmtu %p ro %p ifp %p "
937 "alwaysfrag %d fibnum %u\n", __func__, __LINE__, mtu, ro_pmtu, ro,
938 ifp, alwaysfrag, fibnum));
941 * The caller of this function may specify to use the minimum MTU
943 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
944 * setting. The logic is a bit complicated; by default, unicast
945 * packets will follow path MTU while multicast packets will be sent at
946 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
947 * including unicast ones will be sent at the minimum MTU. Multicast
948 * packets will always be sent at the minimum MTU unless
949 * IP6PO_MINMTU_DISABLE is explicitly specified.
950 * See RFC 3542 for more details.
952 if (mtu > IPV6_MMTU) {
953 if ((flags & IPV6_MINMTU))
955 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
957 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
959 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
965 * Clear embedded scope identifiers if necessary.
966 * in6_clearscope() will touch the addresses only when necessary.
968 in6_clearscope(&ip6->ip6_src);
969 in6_clearscope(&ip6->ip6_dst);
972 * If the outgoing packet contains a hop-by-hop options header,
973 * it must be examined and processed even by the source node.
974 * (RFC 2460, section 4.)
976 if (exthdrs.ip6e_hbh) {
977 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
978 u_int32_t dummy; /* XXX unused */
979 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
982 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
983 panic("ip6e_hbh is not contiguous");
986 * XXX: if we have to send an ICMPv6 error to the sender,
987 * we need the M_LOOP flag since icmp6_error() expects
988 * the IPv6 and the hop-by-hop options header are
989 * contiguous unless the flag is set.
991 m->m_flags |= M_LOOP;
992 m->m_pkthdr.rcvif = ifp;
993 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
994 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
995 &dummy, &plen) < 0) {
996 /* m was already freed at this point. */
997 error = EINVAL;/* better error? */
1000 m->m_flags &= ~M_LOOP; /* XXX */
1001 m->m_pkthdr.rcvif = NULL;
1004 /* Jump over all PFIL processing if hooks are not active. */
1005 if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
1008 odst = ip6->ip6_dst;
1009 /* Run through list of hooks for output packets. */
1010 switch (pfil_run_hooks(V_inet6_pfil_head, &m, ifp, PFIL_OUT, inp)) {
1012 ip6 = mtod(m, struct ip6_hdr *);
1022 /* See if destination IP address was changed by packet filter. */
1023 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
1024 m->m_flags |= M_SKIP_FIREWALL;
1025 /* If destination is now ourself drop to ip6_input(). */
1026 if (in6_localip(&ip6->ip6_dst)) {
1027 m->m_flags |= M_FASTFWD_OURS;
1028 if (m->m_pkthdr.rcvif == NULL)
1029 m->m_pkthdr.rcvif = V_loif;
1030 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1031 m->m_pkthdr.csum_flags |=
1032 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1033 m->m_pkthdr.csum_data = 0xffff;
1035 #if defined(SCTP) || defined(SCTP_SUPPORT)
1036 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1037 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1039 error = netisr_queue(NETISR_IPV6, m);
1043 RO_INVALIDATE_CACHE(ro);
1044 needfiblookup = 1; /* Redo the routing table lookup. */
1047 /* See if fib was changed by packet filter. */
1048 if (fibnum != M_GETFIB(m)) {
1049 m->m_flags |= M_SKIP_FIREWALL;
1050 fibnum = M_GETFIB(m);
1052 RO_INVALIDATE_CACHE(ro);
1058 /* See if local, if yes, send it to netisr. */
1059 if (m->m_flags & M_FASTFWD_OURS) {
1060 if (m->m_pkthdr.rcvif == NULL)
1061 m->m_pkthdr.rcvif = V_loif;
1062 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1063 m->m_pkthdr.csum_flags |=
1064 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1065 m->m_pkthdr.csum_data = 0xffff;
1067 #if defined(SCTP) || defined(SCTP_SUPPORT)
1068 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1069 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1071 error = netisr_queue(NETISR_IPV6, m);
1074 /* Or forward to some other address? */
1075 if ((m->m_flags & M_IP6_NEXTHOP) &&
1076 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
1078 dst = (struct sockaddr_in6 *)&ro->ro_dst;
1081 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
1082 m->m_flags |= M_SKIP_FIREWALL;
1083 m->m_flags &= ~M_IP6_NEXTHOP;
1084 m_tag_delete(m, fwd_tag);
1090 EVL_APPLY_PRI(m, vlan_pcp);
1092 /* Ensure the packet data is mapped if the interface requires it. */
1093 if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) {
1094 m = mb_unmapped_to_ext(m);
1096 IP6STAT_INC(ip6s_odropped);
1102 * Send the packet to the outgoing interface.
1103 * If necessary, do IPv6 fragmentation before sending.
1105 * The logic here is rather complex:
1106 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
1107 * 1-a: send as is if tlen <= path mtu
1108 * 1-b: fragment if tlen > path mtu
1110 * 2: if user asks us not to fragment (dontfrag == 1)
1111 * 2-a: send as is if tlen <= interface mtu
1112 * 2-b: error if tlen > interface mtu
1114 * 3: if we always need to attach fragment header (alwaysfrag == 1)
1117 * 4: if dontfrag == 1 && alwaysfrag == 1
1118 * error, as we cannot handle this conflicting request.
1120 sw_csum = m->m_pkthdr.csum_flags;
1122 tso = ((sw_csum & ifp->if_hwassist &
1123 (CSUM_TSO | CSUM_INNER_TSO)) != 0) ? 1 : 0;
1124 sw_csum &= ~ifp->if_hwassist;
1128 * If we added extension headers, we will not do TSO and calculate the
1129 * checksums ourselves for now.
1130 * XXX-BZ Need a framework to know when the NIC can handle it, even
1133 ip6_output_delayed_csum(m, ifp, sw_csum, plen, optlen);
1134 /* XXX-BZ m->m_pkthdr.csum_flags &= ~ifp->if_hwassist; */
1135 tlen = m->m_pkthdr.len;
1137 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
1141 if (dontfrag && alwaysfrag) { /* Case 4. */
1142 /* Conflicting request - can't transmit. */
1146 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* Case 2-b. */
1148 * Even if the DONTFRAG option is specified, we cannot send the
1149 * packet when the data length is larger than the MTU of the
1150 * outgoing interface.
1151 * Notify the error by sending IPV6_PATHMTU ancillary data if
1152 * application wanted to know the MTU value. Also return an
1153 * error code (this is not described in the API spec).
1156 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
1161 /* Transmit packet without fragmentation. */
1162 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* Cases 1-a and 2-a. */
1163 struct in6_ifaddr *ia6;
1165 ip6 = mtod(m, struct ip6_hdr *);
1166 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1168 /* Record statistics for this interface address. */
1169 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
1170 counter_u64_add(ia6->ia_ifa.ifa_obytes,
1173 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1174 (flags & IP_NO_SND_TAG_RL) ? false : true);
1178 /* Try to fragment the packet. Cases 1-b and 3. */
1179 if (mtu < IPV6_MMTU) {
1180 /* Path MTU cannot be less than IPV6_MMTU. */
1182 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1184 } else if (ip6->ip6_plen == 0) {
1185 /* Jumbo payload cannot be fragmented. */
1187 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1193 * Too large for the destination or interface;
1194 * fragment if possible.
1195 * Must be able to put at least 8 bytes per fragment.
1197 if (mtu > IPV6_MAXPACKET)
1198 mtu = IPV6_MAXPACKET;
1200 len = (mtu - unfragpartlen - sizeof(struct ip6_frag)) & ~7;
1203 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1208 * If the interface will not calculate checksums on
1209 * fragmented packets, then do it here.
1210 * XXX-BZ handle the hw offloading case. Need flags.
1212 ip6_output_delayed_csum(m, ifp, m->m_pkthdr.csum_flags, plen,
1216 * Change the next header field of the last header in the
1217 * unfragmentable part.
1219 if (exthdrs.ip6e_rthdr) {
1220 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1221 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1222 } else if (exthdrs.ip6e_dest1) {
1223 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1224 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1225 } else if (exthdrs.ip6e_hbh) {
1226 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1227 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1229 ip6 = mtod(m, struct ip6_hdr *);
1230 nextproto = ip6->ip6_nxt;
1231 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1235 * Loop through length of segment after first fragment,
1236 * make new header and copy data of each part and link onto
1240 id = htonl(ip6_randomid());
1241 error = ip6_fragment(ifp, m, unfragpartlen, nextproto,len, id);
1245 in6_ifstat_inc(ifp, ifs6_out_fragok);
1248 /* Remove leading garbage. */
1257 /* Record statistics for this interface address. */
1259 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1260 counter_u64_add(ia->ia_ifa.ifa_obytes,
1264 EVL_APPLY_PRI(m, vlan_pcp);
1265 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1272 IP6STAT_INC(ip6s_fragmented);
1278 m_freem(exthdrs.ip6e_hbh); /* m_freem() checks if mbuf is NULL. */
1279 m_freem(exthdrs.ip6e_dest1);
1280 m_freem(exthdrs.ip6e_rthdr);
1281 m_freem(exthdrs.ip6e_dest2);
1290 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1294 if (hlen > MCLBYTES)
1295 return (ENOBUFS); /* XXX */
1298 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1300 m = m_get(M_NOWAIT, MT_DATA);
1305 bcopy(hdr, mtod(m, caddr_t), hlen);
1312 * Insert jumbo payload option.
1315 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1321 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1324 * If there is no hop-by-hop options header, allocate new one.
1325 * If there is one but it doesn't have enough space to store the
1326 * jumbo payload option, allocate a cluster to store the whole options.
1327 * Otherwise, use it to store the options.
1329 if (exthdrs->ip6e_hbh == NULL) {
1330 mopt = m_get(M_NOWAIT, MT_DATA);
1333 mopt->m_len = JUMBOOPTLEN;
1334 optbuf = mtod(mopt, u_char *);
1335 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1336 exthdrs->ip6e_hbh = mopt;
1338 struct ip6_hbh *hbh;
1340 mopt = exthdrs->ip6e_hbh;
1341 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1344 * - exthdrs->ip6e_hbh is not referenced from places
1345 * other than exthdrs.
1346 * - exthdrs->ip6e_hbh is not an mbuf chain.
1348 int oldoptlen = mopt->m_len;
1352 * XXX: give up if the whole (new) hbh header does
1353 * not fit even in an mbuf cluster.
1355 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1359 * As a consequence, we must always prepare a cluster
1362 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1365 n->m_len = oldoptlen + JUMBOOPTLEN;
1366 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1368 optbuf = mtod(n, caddr_t) + oldoptlen;
1370 mopt = exthdrs->ip6e_hbh = n;
1372 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1373 mopt->m_len += JUMBOOPTLEN;
1375 optbuf[0] = IP6OPT_PADN;
1379 * Adjust the header length according to the pad and
1380 * the jumbo payload option.
1382 hbh = mtod(mopt, struct ip6_hbh *);
1383 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1386 /* fill in the option. */
1387 optbuf[2] = IP6OPT_JUMBO;
1389 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1390 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1392 /* finally, adjust the packet header length */
1393 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1400 * Insert fragment header and copy unfragmentable header portions.
1403 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1404 struct ip6_frag **frghdrp)
1406 struct mbuf *n, *mlast;
1408 if (hlen > sizeof(struct ip6_hdr)) {
1409 n = m_copym(m0, sizeof(struct ip6_hdr),
1410 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1417 /* Search for the last mbuf of unfragmentable part. */
1418 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1421 if (M_WRITABLE(mlast) &&
1422 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1423 /* use the trailing space of the last mbuf for the fragment hdr */
1424 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1426 mlast->m_len += sizeof(struct ip6_frag);
1427 m->m_pkthdr.len += sizeof(struct ip6_frag);
1429 /* allocate a new mbuf for the fragment header */
1432 mfrg = m_get(M_NOWAIT, MT_DATA);
1435 mfrg->m_len = sizeof(struct ip6_frag);
1436 *frghdrp = mtod(mfrg, struct ip6_frag *);
1437 mlast->m_next = mfrg;
1444 * Calculates IPv6 path mtu for destination @dst.
1445 * Resulting MTU is stored in @mtup.
1447 * Returns 0 on success.
1450 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1452 struct epoch_tracker et;
1453 struct nhop_object *nh;
1454 struct in6_addr kdst;
1458 in6_splitscope(dst, &kdst, &scopeid);
1460 NET_EPOCH_ENTER(et);
1461 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1463 error = ip6_calcmtu(nh->nh_ifp, dst, nh->nh_mtu, mtup, NULL, 0);
1465 error = EHOSTUNREACH;
1472 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1473 * and cached data in @ro_pmtu.
1474 * MTU from (successful) route lookup is saved (along with dst)
1475 * inside @ro_pmtu to avoid subsequent route lookups after packet
1476 * filter processing.
1478 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1479 * Returns 0 on success.
1482 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1483 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1484 int *alwaysfragp, u_int fibnum, u_int proto)
1486 struct nhop_object *nh;
1487 struct in6_addr kdst;
1489 struct sockaddr_in6 *sa6_dst, sin6;
1495 if (ro_pmtu == NULL || do_lookup) {
1497 * Here ro_pmtu has final destination address, while
1498 * ro might represent immediate destination.
1499 * Use ro_pmtu destination since mtu might differ.
1501 if (ro_pmtu != NULL) {
1502 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1503 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1504 ro_pmtu->ro_mtu = 0;
1508 if (ro_pmtu == NULL || ro_pmtu->ro_mtu == 0) {
1509 bzero(sa6_dst, sizeof(*sa6_dst));
1510 sa6_dst->sin6_family = AF_INET6;
1511 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1512 sa6_dst->sin6_addr = *dst;
1514 in6_splitscope(dst, &kdst, &scopeid);
1515 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1518 if (ro_pmtu != NULL)
1519 ro_pmtu->ro_mtu = mtu;
1522 mtu = ro_pmtu->ro_mtu;
1525 if (ro_pmtu != NULL && ro_pmtu->ro_nh != NULL)
1526 mtu = ro_pmtu->ro_nh->nh_mtu;
1528 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1532 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1533 * hostcache data for @dst.
1534 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1536 * Returns 0 on success.
1539 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1540 u_long *mtup, int *alwaysfragp, u_int proto)
1548 struct in_conninfo inc;
1550 bzero(&inc, sizeof(inc));
1551 inc.inc_flags |= INC_ISIPV6;
1552 inc.inc6_faddr = *dst;
1554 ifmtu = IN6_LINKMTU(ifp);
1556 /* TCP is known to react to pmtu changes so skip hc */
1557 if (proto != IPPROTO_TCP)
1558 mtu = tcp_hc_getmtu(&inc);
1561 mtu = min(mtu, rt_mtu);
1566 else if (mtu < IPV6_MMTU) {
1568 * RFC2460 section 5, last paragraph:
1569 * if we record ICMPv6 too big message with
1570 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1571 * or smaller, with framgent header attached.
1572 * (fragment header is needed regardless from the
1573 * packet size, for translators to identify packets)
1579 mtu = IN6_LINKMTU(ifp);
1581 error = EHOSTUNREACH; /* XXX */
1585 *alwaysfragp = alwaysfrag;
1590 * IP6 socket option processing.
1593 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1595 int optdatalen, uproto;
1597 struct inpcb *inp = sotoinpcb(so);
1599 int level, op, optname;
1603 uint32_t rss_bucket;
1608 * Don't use more than a quarter of mbuf clusters. N.B.:
1609 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1610 * on LP64 architectures, so cast to u_long to avoid undefined
1611 * behavior. ILP32 architectures cannot have nmbclusters
1612 * large enough to overflow for other reasons.
1614 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1616 level = sopt->sopt_level;
1617 op = sopt->sopt_dir;
1618 optname = sopt->sopt_name;
1619 optlen = sopt->sopt_valsize;
1623 uproto = (int)so->so_proto->pr_protocol;
1625 if (level != IPPROTO_IPV6) {
1628 if (sopt->sopt_level == SOL_SOCKET &&
1629 sopt->sopt_dir == SOPT_SET) {
1630 switch (sopt->sopt_name) {
1633 if ((so->so_options & SO_REUSEADDR) != 0)
1634 inp->inp_flags2 |= INP_REUSEADDR;
1636 inp->inp_flags2 &= ~INP_REUSEADDR;
1642 if ((so->so_options & SO_REUSEPORT) != 0)
1643 inp->inp_flags2 |= INP_REUSEPORT;
1645 inp->inp_flags2 &= ~INP_REUSEPORT;
1649 case SO_REUSEPORT_LB:
1651 if ((so->so_options & SO_REUSEPORT_LB) != 0)
1652 inp->inp_flags2 |= INP_REUSEPORT_LB;
1654 inp->inp_flags2 &= ~INP_REUSEPORT_LB;
1660 inp->inp_inc.inc_fibnum = so->so_fibnum;
1664 case SO_MAX_PACING_RATE:
1667 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1678 } else { /* level == IPPROTO_IPV6 */
1682 case IPV6_2292PKTOPTIONS:
1683 #ifdef IPV6_PKTOPTIONS
1684 case IPV6_PKTOPTIONS:
1689 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1690 printf("ip6_ctloutput: mbuf limit hit\n");
1695 error = soopt_getm(sopt, &m); /* XXX */
1698 error = soopt_mcopyin(sopt, m); /* XXX */
1702 error = ip6_pcbopts(&inp->in6p_outputopts, m,
1705 m_freem(m); /* XXX */
1710 * Use of some Hop-by-Hop options or some
1711 * Destination options, might require special
1712 * privilege. That is, normal applications
1713 * (without special privilege) might be forbidden
1714 * from setting certain options in outgoing packets,
1715 * and might never see certain options in received
1716 * packets. [RFC 2292 Section 6]
1717 * KAME specific note:
1718 * KAME prevents non-privileged users from sending or
1719 * receiving ANY hbh/dst options in order to avoid
1720 * overhead of parsing options in the kernel.
1722 case IPV6_RECVHOPOPTS:
1723 case IPV6_RECVDSTOPTS:
1724 case IPV6_RECVRTHDRDSTOPTS:
1726 error = priv_check(td,
1727 PRIV_NETINET_SETHDROPTS);
1732 case IPV6_UNICAST_HOPS:
1735 case IPV6_RECVPKTINFO:
1736 case IPV6_RECVHOPLIMIT:
1737 case IPV6_RECVRTHDR:
1738 case IPV6_RECVPATHMTU:
1739 case IPV6_RECVTCLASS:
1740 case IPV6_RECVFLOWID:
1742 case IPV6_RECVRSSBUCKETID:
1745 case IPV6_AUTOFLOWLABEL:
1746 case IPV6_ORIGDSTADDR:
1748 case IPV6_BINDMULTI:
1750 case IPV6_RSS_LISTEN_BUCKET:
1753 if (optname == IPV6_BINDANY && td != NULL) {
1754 error = priv_check(td,
1755 PRIV_NETINET_BINDANY);
1760 if (optlen != sizeof(int)) {
1764 error = sooptcopyin(sopt, &optval,
1765 sizeof optval, sizeof optval);
1769 case IPV6_UNICAST_HOPS:
1770 if (optval < -1 || optval >= 256)
1773 /* -1 = kernel default */
1774 inp->in6p_hops = optval;
1775 if ((inp->inp_vflag &
1777 inp->inp_ip_ttl = optval;
1780 #define OPTSET(bit) \
1784 inp->inp_flags |= (bit); \
1786 inp->inp_flags &= ~(bit); \
1788 } while (/*CONSTCOND*/ 0)
1789 #define OPTSET2292(bit) \
1792 inp->inp_flags |= IN6P_RFC2292; \
1794 inp->inp_flags |= (bit); \
1796 inp->inp_flags &= ~(bit); \
1798 } while (/*CONSTCOND*/ 0)
1799 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0)
1801 #define OPTSET2_N(bit, val) do { \
1803 inp->inp_flags2 |= bit; \
1805 inp->inp_flags2 &= ~bit; \
1807 #define OPTSET2(bit, val) do { \
1809 OPTSET2_N(bit, val); \
1812 #define OPTBIT2(bit) (inp->inp_flags2 & (bit) ? 1 : 0)
1813 #define OPTSET2292_EXCLUSIVE(bit) \
1816 if (OPTBIT(IN6P_RFC2292)) { \
1820 inp->inp_flags |= (bit); \
1822 inp->inp_flags &= ~(bit); \
1825 } while (/*CONSTCOND*/ 0)
1827 case IPV6_RECVPKTINFO:
1828 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1833 struct ip6_pktopts **optp;
1835 /* cannot mix with RFC2292 */
1836 if (OPTBIT(IN6P_RFC2292)) {
1841 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1843 return (ECONNRESET);
1845 optp = &inp->in6p_outputopts;
1846 error = ip6_pcbopt(IPV6_HOPLIMIT,
1847 (u_char *)&optval, sizeof(optval),
1848 optp, (td != NULL) ? td->td_ucred :
1854 case IPV6_RECVHOPLIMIT:
1855 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1858 case IPV6_RECVHOPOPTS:
1859 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1862 case IPV6_RECVDSTOPTS:
1863 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1866 case IPV6_RECVRTHDRDSTOPTS:
1867 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1870 case IPV6_RECVRTHDR:
1871 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1874 case IPV6_RECVPATHMTU:
1876 * We ignore this option for TCP
1878 * (RFC3542 leaves this case
1881 if (uproto != IPPROTO_TCP)
1885 case IPV6_RECVFLOWID:
1886 OPTSET2(INP_RECVFLOWID, optval);
1890 case IPV6_RECVRSSBUCKETID:
1891 OPTSET2(INP_RECVRSSBUCKETID, optval);
1897 if (inp->inp_lport ||
1898 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1900 * The socket is already bound.
1907 inp->inp_flags |= IN6P_IPV6_V6ONLY;
1908 inp->inp_vflag &= ~INP_IPV4;
1910 inp->inp_flags &= ~IN6P_IPV6_V6ONLY;
1911 inp->inp_vflag |= INP_IPV4;
1915 case IPV6_RECVTCLASS:
1916 /* cannot mix with RFC2292 XXX */
1917 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1919 case IPV6_AUTOFLOWLABEL:
1920 OPTSET(IN6P_AUTOFLOWLABEL);
1923 case IPV6_ORIGDSTADDR:
1924 OPTSET2(INP_ORIGDSTADDR, optval);
1927 OPTSET(INP_BINDANY);
1930 case IPV6_BINDMULTI:
1931 OPTSET2(INP_BINDMULTI, optval);
1934 case IPV6_RSS_LISTEN_BUCKET:
1935 if ((optval >= 0) &&
1936 (optval < rss_getnumbuckets())) {
1938 inp->inp_rss_listen_bucket = optval;
1939 OPTSET2_N(INP_RSS_BUCKET_SET, 1);
1947 if ((optval >= -1) && (optval <=
1948 (INP_2PCP_MASK >> INP_2PCP_SHIFT))) {
1974 case IPV6_USE_MIN_MTU:
1975 case IPV6_PREFER_TEMPADDR:
1976 if (optlen != sizeof(optval)) {
1980 error = sooptcopyin(sopt, &optval,
1981 sizeof optval, sizeof optval);
1985 struct ip6_pktopts **optp;
1987 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1989 return (ECONNRESET);
1991 optp = &inp->in6p_outputopts;
1992 error = ip6_pcbopt(optname,
1993 (u_char *)&optval, sizeof(optval),
1994 optp, (td != NULL) ? td->td_ucred :
2000 case IPV6_2292PKTINFO:
2001 case IPV6_2292HOPLIMIT:
2002 case IPV6_2292HOPOPTS:
2003 case IPV6_2292DSTOPTS:
2004 case IPV6_2292RTHDR:
2006 if (optlen != sizeof(int)) {
2010 error = sooptcopyin(sopt, &optval,
2011 sizeof optval, sizeof optval);
2015 case IPV6_2292PKTINFO:
2016 OPTSET2292(IN6P_PKTINFO);
2018 case IPV6_2292HOPLIMIT:
2019 OPTSET2292(IN6P_HOPLIMIT);
2021 case IPV6_2292HOPOPTS:
2023 * Check super-user privilege.
2024 * See comments for IPV6_RECVHOPOPTS.
2027 error = priv_check(td,
2028 PRIV_NETINET_SETHDROPTS);
2032 OPTSET2292(IN6P_HOPOPTS);
2034 case IPV6_2292DSTOPTS:
2036 error = priv_check(td,
2037 PRIV_NETINET_SETHDROPTS);
2041 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
2043 case IPV6_2292RTHDR:
2044 OPTSET2292(IN6P_RTHDR);
2052 case IPV6_RTHDRDSTOPTS:
2055 /* new advanced API (RFC3542) */
2057 u_char optbuf_storage[MCLBYTES];
2059 struct ip6_pktopts **optp;
2061 /* cannot mix with RFC2292 */
2062 if (OPTBIT(IN6P_RFC2292)) {
2068 * We only ensure valsize is not too large
2069 * here. Further validation will be done
2072 error = sooptcopyin(sopt, optbuf_storage,
2073 sizeof(optbuf_storage), 0);
2076 optlen = sopt->sopt_valsize;
2077 optbuf = optbuf_storage;
2079 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2081 return (ECONNRESET);
2083 optp = &inp->in6p_outputopts;
2084 error = ip6_pcbopt(optname, optbuf, optlen,
2085 optp, (td != NULL) ? td->td_ucred : NULL,
2092 case IPV6_MULTICAST_IF:
2093 case IPV6_MULTICAST_HOPS:
2094 case IPV6_MULTICAST_LOOP:
2095 case IPV6_JOIN_GROUP:
2096 case IPV6_LEAVE_GROUP:
2098 case MCAST_BLOCK_SOURCE:
2099 case MCAST_UNBLOCK_SOURCE:
2100 case MCAST_JOIN_GROUP:
2101 case MCAST_LEAVE_GROUP:
2102 case MCAST_JOIN_SOURCE_GROUP:
2103 case MCAST_LEAVE_SOURCE_GROUP:
2104 error = ip6_setmoptions(inp, sopt);
2107 case IPV6_PORTRANGE:
2108 error = sooptcopyin(sopt, &optval,
2109 sizeof optval, sizeof optval);
2115 case IPV6_PORTRANGE_DEFAULT:
2116 inp->inp_flags &= ~(INP_LOWPORT);
2117 inp->inp_flags &= ~(INP_HIGHPORT);
2120 case IPV6_PORTRANGE_HIGH:
2121 inp->inp_flags &= ~(INP_LOWPORT);
2122 inp->inp_flags |= INP_HIGHPORT;
2125 case IPV6_PORTRANGE_LOW:
2126 inp->inp_flags &= ~(INP_HIGHPORT);
2127 inp->inp_flags |= INP_LOWPORT;
2137 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2138 case IPV6_IPSEC_POLICY:
2139 if (IPSEC_ENABLED(ipv6)) {
2140 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2147 error = ENOPROTOOPT;
2154 case IPV6_2292PKTOPTIONS:
2155 #ifdef IPV6_PKTOPTIONS
2156 case IPV6_PKTOPTIONS:
2159 * RFC3542 (effectively) deprecated the
2160 * semantics of the 2292-style pktoptions.
2161 * Since it was not reliable in nature (i.e.,
2162 * applications had to expect the lack of some
2163 * information after all), it would make sense
2164 * to simplify this part by always returning
2167 sopt->sopt_valsize = 0;
2170 case IPV6_RECVHOPOPTS:
2171 case IPV6_RECVDSTOPTS:
2172 case IPV6_RECVRTHDRDSTOPTS:
2173 case IPV6_UNICAST_HOPS:
2174 case IPV6_RECVPKTINFO:
2175 case IPV6_RECVHOPLIMIT:
2176 case IPV6_RECVRTHDR:
2177 case IPV6_RECVPATHMTU:
2180 case IPV6_PORTRANGE:
2181 case IPV6_RECVTCLASS:
2182 case IPV6_AUTOFLOWLABEL:
2186 case IPV6_RECVFLOWID:
2188 case IPV6_RSSBUCKETID:
2189 case IPV6_RECVRSSBUCKETID:
2191 case IPV6_BINDMULTI:
2194 case IPV6_RECVHOPOPTS:
2195 optval = OPTBIT(IN6P_HOPOPTS);
2198 case IPV6_RECVDSTOPTS:
2199 optval = OPTBIT(IN6P_DSTOPTS);
2202 case IPV6_RECVRTHDRDSTOPTS:
2203 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2206 case IPV6_UNICAST_HOPS:
2207 optval = inp->in6p_hops;
2210 case IPV6_RECVPKTINFO:
2211 optval = OPTBIT(IN6P_PKTINFO);
2214 case IPV6_RECVHOPLIMIT:
2215 optval = OPTBIT(IN6P_HOPLIMIT);
2218 case IPV6_RECVRTHDR:
2219 optval = OPTBIT(IN6P_RTHDR);
2222 case IPV6_RECVPATHMTU:
2223 optval = OPTBIT(IN6P_MTU);
2227 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2230 case IPV6_PORTRANGE:
2233 flags = inp->inp_flags;
2234 if (flags & INP_HIGHPORT)
2235 optval = IPV6_PORTRANGE_HIGH;
2236 else if (flags & INP_LOWPORT)
2237 optval = IPV6_PORTRANGE_LOW;
2242 case IPV6_RECVTCLASS:
2243 optval = OPTBIT(IN6P_TCLASS);
2246 case IPV6_AUTOFLOWLABEL:
2247 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2250 case IPV6_ORIGDSTADDR:
2251 optval = OPTBIT2(INP_ORIGDSTADDR);
2255 optval = OPTBIT(INP_BINDANY);
2259 optval = inp->inp_flowid;
2263 optval = inp->inp_flowtype;
2266 case IPV6_RECVFLOWID:
2267 optval = OPTBIT2(INP_RECVFLOWID);
2270 case IPV6_RSSBUCKETID:
2272 rss_hash2bucket(inp->inp_flowid,
2276 optval = rss_bucket;
2281 case IPV6_RECVRSSBUCKETID:
2282 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2286 case IPV6_BINDMULTI:
2287 optval = OPTBIT2(INP_BINDMULTI);
2291 if (OPTBIT2(INP_2PCP_SET)) {
2292 optval = (inp->inp_flags2 &
2303 error = sooptcopyout(sopt, &optval,
2310 struct ip6_mtuinfo mtuinfo;
2311 struct in6_addr addr;
2313 if (!(so->so_state & SS_ISCONNECTED))
2316 * XXX: we dot not consider the case of source
2317 * routing, or optional information to specify
2318 * the outgoing interface.
2319 * Copy faddr out of inp to avoid holding lock
2320 * on inp during route lookup.
2323 bcopy(&inp->in6p_faddr, &addr, sizeof(addr));
2325 error = ip6_getpmtu_ctl(so->so_fibnum,
2329 if (pmtu > IPV6_MAXPACKET)
2330 pmtu = IPV6_MAXPACKET;
2332 bzero(&mtuinfo, sizeof(mtuinfo));
2333 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2334 optdata = (void *)&mtuinfo;
2335 optdatalen = sizeof(mtuinfo);
2336 error = sooptcopyout(sopt, optdata,
2341 case IPV6_2292PKTINFO:
2342 case IPV6_2292HOPLIMIT:
2343 case IPV6_2292HOPOPTS:
2344 case IPV6_2292RTHDR:
2345 case IPV6_2292DSTOPTS:
2347 case IPV6_2292PKTINFO:
2348 optval = OPTBIT(IN6P_PKTINFO);
2350 case IPV6_2292HOPLIMIT:
2351 optval = OPTBIT(IN6P_HOPLIMIT);
2353 case IPV6_2292HOPOPTS:
2354 optval = OPTBIT(IN6P_HOPOPTS);
2356 case IPV6_2292RTHDR:
2357 optval = OPTBIT(IN6P_RTHDR);
2359 case IPV6_2292DSTOPTS:
2360 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2363 error = sooptcopyout(sopt, &optval,
2370 case IPV6_RTHDRDSTOPTS:
2374 case IPV6_USE_MIN_MTU:
2375 case IPV6_PREFER_TEMPADDR:
2376 error = ip6_getpcbopt(inp, optname, sopt);
2379 case IPV6_MULTICAST_IF:
2380 case IPV6_MULTICAST_HOPS:
2381 case IPV6_MULTICAST_LOOP:
2383 error = ip6_getmoptions(inp, sopt);
2386 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2387 case IPV6_IPSEC_POLICY:
2388 if (IPSEC_ENABLED(ipv6)) {
2389 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2395 error = ENOPROTOOPT;
2405 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2407 int error = 0, optval, optlen;
2408 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2409 struct inpcb *inp = sotoinpcb(so);
2410 int level, op, optname;
2412 level = sopt->sopt_level;
2413 op = sopt->sopt_dir;
2414 optname = sopt->sopt_name;
2415 optlen = sopt->sopt_valsize;
2417 if (level != IPPROTO_IPV6) {
2424 * For ICMPv6 sockets, no modification allowed for checksum
2425 * offset, permit "no change" values to help existing apps.
2427 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2428 * for an ICMPv6 socket will fail."
2429 * The current behavior does not meet RFC3542.
2433 if (optlen != sizeof(int)) {
2437 error = sooptcopyin(sopt, &optval, sizeof(optval),
2441 if (optval < -1 || (optval % 2) != 0) {
2443 * The API assumes non-negative even offset
2444 * values or -1 as a special value.
2447 } else if (so->so_proto->pr_protocol ==
2449 if (optval != icmp6off)
2452 inp->in6p_cksum = optval;
2456 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2459 optval = inp->in6p_cksum;
2461 error = sooptcopyout(sopt, &optval, sizeof(optval));
2471 error = ENOPROTOOPT;
2479 * Set up IP6 options in pcb for insertion in output packets or
2480 * specifying behavior of outgoing packets.
2483 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2484 struct socket *so, struct sockopt *sopt)
2486 struct ip6_pktopts *opt = *pktopt;
2488 struct thread *td = sopt->sopt_td;
2489 struct epoch_tracker et;
2491 /* turn off any old options. */
2494 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2495 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2496 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2497 printf("ip6_pcbopts: all specified options are cleared.\n");
2499 ip6_clearpktopts(opt, -1);
2501 opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT);
2507 if (!m || m->m_len == 0) {
2509 * Only turning off any previous options, regardless of
2510 * whether the opt is just created or given.
2512 free(opt, M_IP6OPT);
2516 /* set options specified by user. */
2517 NET_EPOCH_ENTER(et);
2518 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2519 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2520 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2521 free(opt, M_IP6OPT);
2531 * initialize ip6_pktopts. beware that there are non-zero default values in
2535 ip6_initpktopts(struct ip6_pktopts *opt)
2538 bzero(opt, sizeof(*opt));
2539 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2540 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2541 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2542 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2546 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2547 struct ucred *cred, int uproto)
2549 struct epoch_tracker et;
2550 struct ip6_pktopts *opt;
2553 if (*pktopt == NULL) {
2554 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2556 if (*pktopt == NULL)
2558 ip6_initpktopts(*pktopt);
2562 NET_EPOCH_ENTER(et);
2563 ret = ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto);
2569 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2570 if (pktopt && pktopt->field) { \
2572 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2573 malloc_optdata = true; \
2575 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
2577 free(optdata, M_TEMP); \
2578 return (ECONNRESET); \
2580 pktopt = inp->in6p_outputopts; \
2581 if (pktopt && pktopt->field) { \
2582 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2583 bcopy(pktopt->field, optdata, optdatalen); \
2585 free(optdata, M_TEMP); \
2587 malloc_optdata = false; \
2592 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2593 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2595 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2596 pktopt->field->sa_len)
2599 ip6_getpcbopt(struct inpcb *inp, int optname, struct sockopt *sopt)
2601 void *optdata = NULL;
2602 bool malloc_optdata = false;
2605 struct in6_pktinfo null_pktinfo;
2606 int deftclass = 0, on;
2607 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2608 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2609 struct ip6_pktopts *pktopt;
2612 pktopt = inp->in6p_outputopts;
2616 optdata = (void *)&null_pktinfo;
2617 if (pktopt && pktopt->ip6po_pktinfo) {
2618 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2619 sizeof(null_pktinfo));
2620 in6_clearscope(&null_pktinfo.ipi6_addr);
2622 /* XXX: we don't have to do this every time... */
2623 bzero(&null_pktinfo, sizeof(null_pktinfo));
2625 optdatalen = sizeof(struct in6_pktinfo);
2628 if (pktopt && pktopt->ip6po_tclass >= 0)
2629 deftclass = pktopt->ip6po_tclass;
2630 optdata = (void *)&deftclass;
2631 optdatalen = sizeof(int);
2634 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2637 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2639 case IPV6_RTHDRDSTOPTS:
2640 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2643 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2646 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2648 case IPV6_USE_MIN_MTU:
2650 defminmtu = pktopt->ip6po_minmtu;
2651 optdata = (void *)&defminmtu;
2652 optdatalen = sizeof(int);
2655 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2659 optdata = (void *)&on;
2660 optdatalen = sizeof(on);
2662 case IPV6_PREFER_TEMPADDR:
2664 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2665 optdata = (void *)&defpreftemp;
2666 optdatalen = sizeof(int);
2668 default: /* should not happen */
2670 panic("ip6_getpcbopt: unexpected option\n");
2673 return (ENOPROTOOPT);
2677 error = sooptcopyout(sopt, optdata, optdatalen);
2679 free(optdata, M_TEMP);
2685 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2690 if (optname == -1 || optname == IPV6_PKTINFO) {
2691 if (pktopt->ip6po_pktinfo)
2692 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2693 pktopt->ip6po_pktinfo = NULL;
2695 if (optname == -1 || optname == IPV6_HOPLIMIT)
2696 pktopt->ip6po_hlim = -1;
2697 if (optname == -1 || optname == IPV6_TCLASS)
2698 pktopt->ip6po_tclass = -1;
2699 if (optname == -1 || optname == IPV6_NEXTHOP) {
2700 if (pktopt->ip6po_nextroute.ro_nh) {
2701 NH_FREE(pktopt->ip6po_nextroute.ro_nh);
2702 pktopt->ip6po_nextroute.ro_nh = NULL;
2704 if (pktopt->ip6po_nexthop)
2705 free(pktopt->ip6po_nexthop, M_IP6OPT);
2706 pktopt->ip6po_nexthop = NULL;
2708 if (optname == -1 || optname == IPV6_HOPOPTS) {
2709 if (pktopt->ip6po_hbh)
2710 free(pktopt->ip6po_hbh, M_IP6OPT);
2711 pktopt->ip6po_hbh = NULL;
2713 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2714 if (pktopt->ip6po_dest1)
2715 free(pktopt->ip6po_dest1, M_IP6OPT);
2716 pktopt->ip6po_dest1 = NULL;
2718 if (optname == -1 || optname == IPV6_RTHDR) {
2719 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2720 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2721 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2722 if (pktopt->ip6po_route.ro_nh) {
2723 NH_FREE(pktopt->ip6po_route.ro_nh);
2724 pktopt->ip6po_route.ro_nh = NULL;
2727 if (optname == -1 || optname == IPV6_DSTOPTS) {
2728 if (pktopt->ip6po_dest2)
2729 free(pktopt->ip6po_dest2, M_IP6OPT);
2730 pktopt->ip6po_dest2 = NULL;
2734 #define PKTOPT_EXTHDRCPY(type) \
2737 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2738 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2739 if (dst->type == NULL)\
2741 bcopy(src->type, dst->type, hlen);\
2743 } while (/*CONSTCOND*/ 0)
2746 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2748 if (dst == NULL || src == NULL) {
2749 printf("ip6_clearpktopts: invalid argument\n");
2753 dst->ip6po_hlim = src->ip6po_hlim;
2754 dst->ip6po_tclass = src->ip6po_tclass;
2755 dst->ip6po_flags = src->ip6po_flags;
2756 dst->ip6po_minmtu = src->ip6po_minmtu;
2757 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2758 if (src->ip6po_pktinfo) {
2759 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2761 if (dst->ip6po_pktinfo == NULL)
2763 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2765 if (src->ip6po_nexthop) {
2766 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2768 if (dst->ip6po_nexthop == NULL)
2770 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2771 src->ip6po_nexthop->sa_len);
2773 PKTOPT_EXTHDRCPY(ip6po_hbh);
2774 PKTOPT_EXTHDRCPY(ip6po_dest1);
2775 PKTOPT_EXTHDRCPY(ip6po_dest2);
2776 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2780 ip6_clearpktopts(dst, -1);
2783 #undef PKTOPT_EXTHDRCPY
2785 struct ip6_pktopts *
2786 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2789 struct ip6_pktopts *dst;
2791 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2794 ip6_initpktopts(dst);
2796 if ((error = copypktopts(dst, src, canwait)) != 0) {
2797 free(dst, M_IP6OPT);
2805 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2810 ip6_clearpktopts(pktopt, -1);
2812 free(pktopt, M_IP6OPT);
2816 * Set IPv6 outgoing packet options based on advanced API.
2819 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2820 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2822 struct cmsghdr *cm = NULL;
2824 if (control == NULL || opt == NULL)
2828 * ip6_setpktopt can call ifnet_byindex(), so it's imperative that we
2829 * are in the network epoch here.
2833 ip6_initpktopts(opt);
2838 * If stickyopt is provided, make a local copy of the options
2839 * for this particular packet, then override them by ancillary
2841 * XXX: copypktopts() does not copy the cached route to a next
2842 * hop (if any). This is not very good in terms of efficiency,
2843 * but we can allow this since this option should be rarely
2846 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2851 * XXX: Currently, we assume all the optional information is stored
2854 if (control->m_next)
2857 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2858 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2861 if (control->m_len < CMSG_LEN(0))
2864 cm = mtod(control, struct cmsghdr *);
2865 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2867 if (cm->cmsg_level != IPPROTO_IPV6)
2870 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2871 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2880 * Set a particular packet option, as a sticky option or an ancillary data
2881 * item. "len" can be 0 only when it's a sticky option.
2882 * We have 4 cases of combination of "sticky" and "cmsg":
2883 * "sticky=0, cmsg=0": impossible
2884 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2885 * "sticky=1, cmsg=0": RFC3542 socket option
2886 * "sticky=1, cmsg=1": RFC2292 socket option
2889 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2890 struct ucred *cred, int sticky, int cmsg, int uproto)
2892 int minmtupolicy, preftemp;
2897 if (!sticky && !cmsg) {
2899 printf("ip6_setpktopt: impossible case\n");
2905 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2906 * not be specified in the context of RFC3542. Conversely,
2907 * RFC3542 types should not be specified in the context of RFC2292.
2911 case IPV6_2292PKTINFO:
2912 case IPV6_2292HOPLIMIT:
2913 case IPV6_2292NEXTHOP:
2914 case IPV6_2292HOPOPTS:
2915 case IPV6_2292DSTOPTS:
2916 case IPV6_2292RTHDR:
2917 case IPV6_2292PKTOPTIONS:
2918 return (ENOPROTOOPT);
2921 if (sticky && cmsg) {
2928 case IPV6_RTHDRDSTOPTS:
2930 case IPV6_USE_MIN_MTU:
2933 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2934 return (ENOPROTOOPT);
2939 case IPV6_2292PKTINFO:
2942 struct ifnet *ifp = NULL;
2943 struct in6_pktinfo *pktinfo;
2945 if (len != sizeof(struct in6_pktinfo))
2948 pktinfo = (struct in6_pktinfo *)buf;
2951 * An application can clear any sticky IPV6_PKTINFO option by
2952 * doing a "regular" setsockopt with ipi6_addr being
2953 * in6addr_any and ipi6_ifindex being zero.
2954 * [RFC 3542, Section 6]
2956 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2957 pktinfo->ipi6_ifindex == 0 &&
2958 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2959 ip6_clearpktopts(opt, optname);
2963 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2964 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2967 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2969 /* validate the interface index if specified. */
2970 if (pktinfo->ipi6_ifindex) {
2971 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2975 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2976 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2980 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2981 struct in6_ifaddr *ia;
2983 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2984 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2986 return (EADDRNOTAVAIL);
2987 ifa_free(&ia->ia_ifa);
2990 * We store the address anyway, and let in6_selectsrc()
2991 * validate the specified address. This is because ipi6_addr
2992 * may not have enough information about its scope zone, and
2993 * we may need additional information (such as outgoing
2994 * interface or the scope zone of a destination address) to
2995 * disambiguate the scope.
2996 * XXX: the delay of the validation may confuse the
2997 * application when it is used as a sticky option.
2999 if (opt->ip6po_pktinfo == NULL) {
3000 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
3001 M_IP6OPT, M_NOWAIT);
3002 if (opt->ip6po_pktinfo == NULL)
3005 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
3009 case IPV6_2292HOPLIMIT:
3015 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3016 * to simplify the ordering among hoplimit options.
3018 if (optname == IPV6_HOPLIMIT && sticky)
3019 return (ENOPROTOOPT);
3021 if (len != sizeof(int))
3024 if (*hlimp < -1 || *hlimp > 255)
3027 opt->ip6po_hlim = *hlimp;
3035 if (len != sizeof(int))
3037 tclass = *(int *)buf;
3038 if (tclass < -1 || tclass > 255)
3041 opt->ip6po_tclass = tclass;
3045 case IPV6_2292NEXTHOP:
3048 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3053 if (len == 0) { /* just remove the option */
3054 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3058 /* check if cmsg_len is large enough for sa_len */
3059 if (len < sizeof(struct sockaddr) || len < *buf)
3062 switch (((struct sockaddr *)buf)->sa_family) {
3065 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3068 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3071 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3072 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3075 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
3081 case AF_LINK: /* should eventually be supported */
3083 return (EAFNOSUPPORT);
3086 /* turn off the previous option, then set the new option. */
3087 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3088 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3089 if (opt->ip6po_nexthop == NULL)
3091 bcopy(buf, opt->ip6po_nexthop, *buf);
3094 case IPV6_2292HOPOPTS:
3097 struct ip6_hbh *hbh;
3101 * XXX: We don't allow a non-privileged user to set ANY HbH
3102 * options, since per-option restriction has too much
3106 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3112 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3113 break; /* just remove the option */
3116 /* message length validation */
3117 if (len < sizeof(struct ip6_hbh))
3119 hbh = (struct ip6_hbh *)buf;
3120 hbhlen = (hbh->ip6h_len + 1) << 3;
3124 /* turn off the previous option, then set the new option. */
3125 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3126 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3127 if (opt->ip6po_hbh == NULL)
3129 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3134 case IPV6_2292DSTOPTS:
3136 case IPV6_RTHDRDSTOPTS:
3138 struct ip6_dest *dest, **newdest = NULL;
3141 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
3142 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3148 ip6_clearpktopts(opt, optname);
3149 break; /* just remove the option */
3152 /* message length validation */
3153 if (len < sizeof(struct ip6_dest))
3155 dest = (struct ip6_dest *)buf;
3156 destlen = (dest->ip6d_len + 1) << 3;
3161 * Determine the position that the destination options header
3162 * should be inserted; before or after the routing header.
3165 case IPV6_2292DSTOPTS:
3167 * The old advacned API is ambiguous on this point.
3168 * Our approach is to determine the position based
3169 * according to the existence of a routing header.
3170 * Note, however, that this depends on the order of the
3171 * extension headers in the ancillary data; the 1st
3172 * part of the destination options header must appear
3173 * before the routing header in the ancillary data,
3175 * RFC3542 solved the ambiguity by introducing
3176 * separate ancillary data or option types.
3178 if (opt->ip6po_rthdr == NULL)
3179 newdest = &opt->ip6po_dest1;
3181 newdest = &opt->ip6po_dest2;
3183 case IPV6_RTHDRDSTOPTS:
3184 newdest = &opt->ip6po_dest1;
3187 newdest = &opt->ip6po_dest2;
3191 /* turn off the previous option, then set the new option. */
3192 ip6_clearpktopts(opt, optname);
3193 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3194 if (*newdest == NULL)
3196 bcopy(dest, *newdest, destlen);
3201 case IPV6_2292RTHDR:
3204 struct ip6_rthdr *rth;
3208 ip6_clearpktopts(opt, IPV6_RTHDR);
3209 break; /* just remove the option */
3212 /* message length validation */
3213 if (len < sizeof(struct ip6_rthdr))
3215 rth = (struct ip6_rthdr *)buf;
3216 rthlen = (rth->ip6r_len + 1) << 3;
3220 switch (rth->ip6r_type) {
3221 case IPV6_RTHDR_TYPE_0:
3222 if (rth->ip6r_len == 0) /* must contain one addr */
3224 if (rth->ip6r_len % 2) /* length must be even */
3226 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3230 return (EINVAL); /* not supported */
3233 /* turn off the previous option */
3234 ip6_clearpktopts(opt, IPV6_RTHDR);
3235 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3236 if (opt->ip6po_rthdr == NULL)
3238 bcopy(rth, opt->ip6po_rthdr, rthlen);
3243 case IPV6_USE_MIN_MTU:
3244 if (len != sizeof(int))
3246 minmtupolicy = *(int *)buf;
3247 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3248 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3249 minmtupolicy != IP6PO_MINMTU_ALL) {
3252 opt->ip6po_minmtu = minmtupolicy;
3256 if (len != sizeof(int))
3259 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3261 * we ignore this option for TCP sockets.
3262 * (RFC3542 leaves this case unspecified.)
3264 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3266 opt->ip6po_flags |= IP6PO_DONTFRAG;
3269 case IPV6_PREFER_TEMPADDR:
3270 if (len != sizeof(int))
3272 preftemp = *(int *)buf;
3273 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3274 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3275 preftemp != IP6PO_TEMPADDR_PREFER) {
3278 opt->ip6po_prefer_tempaddr = preftemp;
3282 return (ENOPROTOOPT);
3283 } /* end of switch */
3289 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3290 * packet to the input queue of a specified interface. Note that this
3291 * calls the output routine of the loopback "driver", but with an interface
3292 * pointer that might NOT be &loif -- easier than replicating that code here.
3295 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3298 struct ip6_hdr *ip6;
3300 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3305 * Make sure to deep-copy IPv6 header portion in case the data
3306 * is in an mbuf cluster, so that we can safely override the IPv6
3307 * header portion later.
3309 if (!M_WRITABLE(copym) ||
3310 copym->m_len < sizeof(struct ip6_hdr)) {
3311 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3315 ip6 = mtod(copym, struct ip6_hdr *);
3317 * clear embedded scope identifiers if necessary.
3318 * in6_clearscope will touch the addresses only when necessary.
3320 in6_clearscope(&ip6->ip6_src);
3321 in6_clearscope(&ip6->ip6_dst);
3322 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3323 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3325 copym->m_pkthdr.csum_data = 0xffff;
3327 if_simloop(ifp, copym, AF_INET6, 0);
3331 * Chop IPv6 header off from the payload.
3334 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3337 struct ip6_hdr *ip6;
3339 ip6 = mtod(m, struct ip6_hdr *);
3340 if (m->m_len > sizeof(*ip6)) {
3341 mh = m_gethdr(M_NOWAIT, MT_DATA);
3346 m_move_pkthdr(mh, m);
3347 M_ALIGN(mh, sizeof(*ip6));
3348 m->m_len -= sizeof(*ip6);
3349 m->m_data += sizeof(*ip6);
3352 m->m_len = sizeof(*ip6);
3353 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3355 exthdrs->ip6e_ip6 = m;
3360 * Compute IPv6 extension header length.
3363 ip6_optlen(struct inpcb *inp)
3367 if (!inp->in6p_outputopts)
3372 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3374 len += elen(inp->in6p_outputopts->ip6po_hbh);
3375 if (inp->in6p_outputopts->ip6po_rthdr)
3376 /* dest1 is valid with rthdr only */
3377 len += elen(inp->in6p_outputopts->ip6po_dest1);
3378 len += elen(inp->in6p_outputopts->ip6po_rthdr);
3379 len += elen(inp->in6p_outputopts->ip6po_dest2);