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
344 * Always stamp tags that include NIC ktls.
350 if (inp != NULL && mst == NULL) {
351 if ((inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) != 0 ||
352 (inp->inp_snd_tag != NULL &&
353 inp->inp_snd_tag->ifp != ifp))
354 in_pcboutput_txrtlmt(inp, ifp, m);
356 if (inp->inp_snd_tag != NULL)
357 mst = inp->inp_snd_tag;
360 if (stamp_tag && mst != NULL) {
361 KASSERT(m->m_pkthdr.rcvif == NULL,
362 ("trying to add a send tag to a forwarded packet"));
363 if (mst->ifp != ifp) {
369 /* stamp send tag on mbuf */
370 m->m_pkthdr.snd_tag = m_snd_tag_ref(mst);
371 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
374 error = nd6_output_ifp(ifp, origifp, m, dst, (struct route *)ro);
377 /* Check for route change invalidating send tags. */
381 error = ktls_output_eagain(inp, tls);
387 in_pcboutput_eagain(inp);
394 * The packet in mbuf chain m contains a skeletal IP6 header (with pri, len,
395 * nxt, hlim, src, dst).
396 * This function may modify ver and hlim only.
397 * The mbuf chain containing the packet will be freed.
398 * The mbuf opt, if present, will not be freed.
399 * If route_in6 ro is present and has ro_nh initialized, route lookup would be
400 * skipped and ro->ro_nh would be used. If ro is present but ro->ro_nh is NULL,
401 * then result of route lookup is stored in ro->ro_nh.
403 * Type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and nd_ifinfo.linkmtu
404 * is uint32_t. So we use u_long to hold largest one, which is rt_mtu.
406 * ifpp - XXX: just for statistics
409 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
410 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
411 struct ifnet **ifpp, struct inpcb *inp)
414 struct ifnet *ifp, *origifp;
417 struct route_in6 *ro_pmtu;
418 struct nhop_object *nh;
419 struct sockaddr_in6 *dst, sin6, src_sa, dst_sa;
420 struct in6_addr odst;
425 struct in6_ifaddr *ia = NULL;
427 int alwaysfrag, dontfrag;
428 u_int32_t optlen, plen = 0, unfragpartlen;
429 struct ip6_exthdrs exthdrs;
430 struct in6_addr src0, dst0;
436 struct m_tag *fwd_tag = NULL;
442 INP_LOCK_ASSERT(inp);
443 M_SETFIB(m, inp->inp_inc.inc_fibnum);
444 if ((flags & IP_NODEFAULTFLOWID) == 0) {
445 /* Unconditionally set flowid. */
446 m->m_pkthdr.flowid = inp->inp_flowid;
447 M_HASHTYPE_SET(m, inp->inp_flowtype);
449 if ((inp->inp_flags2 & INP_2PCP_SET) != 0)
450 vlan_pcp = (inp->inp_flags2 & INP_2PCP_MASK) >>
453 m->m_pkthdr.numa_domain = inp->inp_numa_domain;
457 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
459 * IPSec checking which handles several cases.
460 * FAST IPSEC: We re-injected the packet.
461 * XXX: need scope argument.
463 if (IPSEC_ENABLED(ipv6)) {
464 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
465 if (error == EINPROGRESS)
472 /* Source address validation. */
473 ip6 = mtod(m, struct ip6_hdr *);
474 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
475 (flags & IPV6_UNSPECSRC) == 0) {
477 IP6STAT_INC(ip6s_badscope);
480 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
482 IP6STAT_INC(ip6s_badscope);
487 * If we are given packet options to add extension headers prepare them.
488 * Calculate the total length of the extension header chain.
489 * Keep the length of the unfragmentable part for fragmentation.
491 bzero(&exthdrs, sizeof(exthdrs));
493 unfragpartlen = sizeof(struct ip6_hdr);
495 /* Hop-by-Hop options header. */
496 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh, optlen);
498 /* Destination options header (1st part). */
499 if (opt->ip6po_rthdr) {
500 #ifndef RTHDR_SUPPORT_IMPLEMENTED
502 * If there is a routing header, discard the packet
503 * right away here. RH0/1 are obsolete and we do not
504 * currently support RH2/3/4.
505 * People trying to use RH253/254 may want to disable
507 * The moment we do support any routing header (again)
508 * this block should check the routing type more
516 * Destination options header (1st part).
517 * This only makes sense with a routing header.
518 * See Section 9.2 of RFC 3542.
519 * Disabling this part just for MIP6 convenience is
520 * a bad idea. We need to think carefully about a
521 * way to make the advanced API coexist with MIP6
522 * options, which might automatically be inserted in
525 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1,
528 /* Routing header. */
529 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr, optlen);
531 unfragpartlen += optlen;
534 * NOTE: we don't add AH/ESP length here (done in
535 * ip6_ipsec_output()).
538 /* Destination options header (2nd part). */
539 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2, optlen);
543 * If there is at least one extension header,
544 * separate IP6 header from the payload.
548 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
552 m = exthdrs.ip6e_ip6;
553 ip6 = mtod(m, struct ip6_hdr *);
557 /* Adjust mbuf packet header length. */
558 m->m_pkthdr.len += optlen;
559 plen = m->m_pkthdr.len - sizeof(*ip6);
561 /* If this is a jumbo payload, insert a jumbo payload option. */
562 if (plen > IPV6_MAXPACKET) {
564 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
568 m = exthdrs.ip6e_ip6;
569 ip6 = mtod(m, struct ip6_hdr *);
572 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
576 ip6->ip6_plen = htons(plen);
577 nexthdrp = &ip6->ip6_nxt;
581 * Concatenate headers and fill in next header fields.
582 * Here we have, on "m"
584 * and we insert headers accordingly.
585 * Finally, we should be getting:
586 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload].
588 * During the header composing process "m" points to IPv6
589 * header. "mprev" points to an extension header prior to esp.
594 * We treat dest2 specially. This makes IPsec processing
595 * much easier. The goal here is to make mprev point the
596 * mbuf prior to dest2.
598 * Result: IPv6 dest2 payload.
599 * m and mprev will point to IPv6 header.
601 if (exthdrs.ip6e_dest2) {
603 panic("%s:%d: assumption failed: "
604 "hdr not split: hdrsplit %d exthdrs %p",
605 __func__, __LINE__, hdrsplit, &exthdrs);
606 exthdrs.ip6e_dest2->m_next = m->m_next;
607 m->m_next = exthdrs.ip6e_dest2;
608 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
609 ip6->ip6_nxt = IPPROTO_DSTOPTS;
613 * Result: IPv6 hbh dest1 rthdr dest2 payload.
614 * m will point to IPv6 header. mprev will point to the
615 * extension header prior to dest2 (rthdr in the above case).
617 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
618 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
620 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
624 IP6STAT_INC(ip6s_localout);
628 if (opt && opt->ip6po_rthdr)
629 ro = &opt->ip6po_route;
631 dst = (struct sockaddr_in6 *)&ro->ro_dst;
634 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
638 * If specified, try to fill in the traffic class field.
639 * Do not override if a non-zero value is already set.
640 * We check the diffserv field and the ECN field separately.
642 if (opt && opt->ip6po_tclass >= 0) {
645 if (IPV6_DSCP(ip6) == 0)
647 if (IPV6_ECN(ip6) == 0)
650 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
653 /* Fill in or override the hop limit field, if necessary. */
654 if (opt && opt->ip6po_hlim != -1)
655 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
656 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
658 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
660 ip6->ip6_hlim = V_ip6_defmcasthlim;
663 if (ro == NULL || ro->ro_nh == NULL) {
664 bzero(dst, sizeof(*dst));
665 dst->sin6_family = AF_INET6;
666 dst->sin6_len = sizeof(*dst);
667 dst->sin6_addr = ip6->ip6_dst;
670 * Validate route against routing table changes.
671 * Make sure that the address family is set in route.
677 if (ro->ro_nh != NULL && inp != NULL) {
678 ro->ro_dst.sin6_family = AF_INET6; /* XXX KASSERT? */
679 NH_VALIDATE((struct route *)ro, &inp->inp_rt_cookie,
682 if (ro->ro_nh != NULL && fwd_tag == NULL &&
683 (!NH_IS_VALID(ro->ro_nh) ||
684 ro->ro_dst.sin6_family != AF_INET6 ||
685 !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)))
686 RO_INVALIDATE_CACHE(ro);
688 if (ro->ro_nh != NULL && fwd_tag == NULL &&
689 ro->ro_dst.sin6_family == AF_INET6 &&
690 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
695 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
697 if (fwd_tag == NULL) {
698 bzero(&dst_sa, sizeof(dst_sa));
699 dst_sa.sin6_family = AF_INET6;
700 dst_sa.sin6_len = sizeof(dst_sa);
701 dst_sa.sin6_addr = ip6->ip6_dst;
703 error = in6_selectroute(&dst_sa, opt, im6o, ro, &ifp,
704 &nh, fibnum, m->m_pkthdr.flowid);
706 IP6STAT_INC(ip6s_noroute);
708 in6_ifstat_inc(ifp, ifs6_out_discard);
716 * If in6_selectroute() does not return a nexthop
717 * dst may not have been updated.
719 *dst = dst_sa; /* XXX */
721 if (nh->nh_flags & NHF_HOST)
723 ia = (struct in6_ifaddr *)(nh->nh_ifa);
724 counter_u64_add(nh->nh_pksent, 1);
727 struct nhop_object *nh;
728 struct in6_addr kdst;
731 if (fwd_tag == NULL) {
732 bzero(&dst_sa, sizeof(dst_sa));
733 dst_sa.sin6_family = AF_INET6;
734 dst_sa.sin6_len = sizeof(dst_sa);
735 dst_sa.sin6_addr = ip6->ip6_dst;
738 if (IN6_IS_ADDR_MULTICAST(&dst_sa.sin6_addr) &&
740 (ifp = im6o->im6o_multicast_ifp) != NULL) {
741 /* We do not need a route lookup. */
742 *dst = dst_sa; /* XXX */
746 in6_splitscope(&dst_sa.sin6_addr, &kdst, &scopeid);
748 if (IN6_IS_ADDR_MC_LINKLOCAL(&dst_sa.sin6_addr) ||
749 IN6_IS_ADDR_MC_NODELOCAL(&dst_sa.sin6_addr)) {
751 ifp = in6_getlinkifnet(scopeid);
753 error = EHOSTUNREACH;
756 *dst = dst_sa; /* XXX */
761 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE,
764 IP6STAT_INC(ip6s_noroute);
765 /* No ifp in6_ifstat_inc(ifp, ifs6_out_discard); */
766 error = EHOSTUNREACH;;
772 ia = ifatoia6(nh->nh_ifa);
773 if (nh->nh_flags & NHF_GATEWAY)
774 dst->sin6_addr = nh->gw6_sa.sin6_addr;
779 /* Then nh (for unicast) and ifp must be non-NULL valid values. */
780 if ((flags & IPV6_FORWARDING) == 0) {
781 /* XXX: the FORWARDING flag can be set for mrouting. */
782 in6_ifstat_inc(ifp, ifs6_out_request);
785 /* Setup data structures for scope ID checks. */
787 bzero(&src_sa, sizeof(src_sa));
788 src_sa.sin6_family = AF_INET6;
789 src_sa.sin6_len = sizeof(src_sa);
790 src_sa.sin6_addr = ip6->ip6_src;
793 /* Re-initialize to be sure. */
794 bzero(&dst_sa, sizeof(dst_sa));
795 dst_sa.sin6_family = AF_INET6;
796 dst_sa.sin6_len = sizeof(dst_sa);
797 dst_sa.sin6_addr = ip6->ip6_dst;
799 /* Check for valid scope ID. */
800 if (in6_setscope(&src0, ifp, &zone) == 0 &&
801 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
802 in6_setscope(&dst0, ifp, &zone) == 0 &&
803 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
805 * The outgoing interface is in the zone of the source
806 * and destination addresses.
808 * Because the loopback interface cannot receive
809 * packets with a different scope ID than its own,
810 * there is a trick to pretend the outgoing packet
811 * was received by the real network interface, by
812 * setting "origifp" different from "ifp". This is
813 * only allowed when "ifp" is a loopback network
814 * interface. Refer to code in nd6_output_ifp() for
820 * We should use ia_ifp to support the case of sending
821 * packets to an address of our own.
823 if (ia != NULL && ia->ia_ifp)
826 } else if ((ifp->if_flags & IFF_LOOPBACK) == 0 ||
827 sa6_recoverscope(&src_sa) != 0 ||
828 sa6_recoverscope(&dst_sa) != 0 ||
829 dst_sa.sin6_scope_id == 0 ||
830 (src_sa.sin6_scope_id != 0 &&
831 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
832 (origifp = ifnet_byindex(dst_sa.sin6_scope_id)) == NULL) {
834 * If the destination network interface is not a
835 * loopback interface, or the destination network
836 * address has no scope ID, or the source address has
837 * a scope ID set which is different from the
838 * destination address one, or there is no network
839 * interface representing this scope ID, the address
840 * pair is considered invalid.
842 IP6STAT_INC(ip6s_badscope);
843 in6_ifstat_inc(ifp, ifs6_out_discard);
845 error = EHOSTUNREACH; /* XXX */
848 /* All scope ID checks are successful. */
850 if (nh && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
851 if (opt && opt->ip6po_nextroute.ro_nh) {
853 * The nexthop is explicitly specified by the
854 * application. We assume the next hop is an IPv6
857 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
859 else if ((nh->nh_flags & NHF_GATEWAY))
863 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
864 m->m_flags &= ~(M_BCAST | M_MCAST); /* Just in case. */
866 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
867 in6_ifstat_inc(ifp, ifs6_out_mcast);
869 /* Confirm that the outgoing interface supports multicast. */
870 if (!(ifp->if_flags & IFF_MULTICAST)) {
871 IP6STAT_INC(ip6s_noroute);
872 in6_ifstat_inc(ifp, ifs6_out_discard);
876 if ((im6o == NULL && in6_mcast_loop) ||
877 (im6o && im6o->im6o_multicast_loop)) {
879 * Loop back multicast datagram if not expressly
880 * forbidden to do so, even if we have not joined
881 * the address; protocols will filter it later,
882 * thus deferring a hash lookup and lock acquisition
883 * at the expense of an m_copym().
885 ip6_mloopback(ifp, m);
888 * If we are acting as a multicast router, perform
889 * multicast forwarding as if the packet had just
890 * arrived on the interface to which we are about
891 * to send. The multicast forwarding function
892 * recursively calls this function, using the
893 * IPV6_FORWARDING flag to prevent infinite recursion.
895 * Multicasts that are looped back by ip6_mloopback(),
896 * above, will be forwarded by the ip6_input() routine,
899 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
901 * XXX: ip6_mforward expects that rcvif is NULL
902 * when it is called from the originating path.
903 * However, it may not always be the case.
905 m->m_pkthdr.rcvif = NULL;
906 if (ip6_mforward(ip6, ifp, m) != 0) {
913 * Multicasts with a hoplimit of zero may be looped back,
914 * above, but must not be transmitted on a network.
915 * Also, multicasts addressed to the loopback interface
916 * are not sent -- the above call to ip6_mloopback() will
917 * loop back a copy if this host actually belongs to the
918 * destination group on the loopback interface.
920 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
921 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
928 * Fill the outgoing inteface to tell the upper layer
929 * to increment per-interface statistics.
934 /* Determine path MTU. */
935 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
936 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
938 KASSERT(mtu > 0, ("%s:%d: mtu %ld, ro_pmtu %p ro %p ifp %p "
939 "alwaysfrag %d fibnum %u\n", __func__, __LINE__, mtu, ro_pmtu, ro,
940 ifp, alwaysfrag, fibnum));
943 * The caller of this function may specify to use the minimum MTU
945 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
946 * setting. The logic is a bit complicated; by default, unicast
947 * packets will follow path MTU while multicast packets will be sent at
948 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
949 * including unicast ones will be sent at the minimum MTU. Multicast
950 * packets will always be sent at the minimum MTU unless
951 * IP6PO_MINMTU_DISABLE is explicitly specified.
952 * See RFC 3542 for more details.
954 if (mtu > IPV6_MMTU) {
955 if ((flags & IPV6_MINMTU))
957 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
959 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
961 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
967 * Clear embedded scope identifiers if necessary.
968 * in6_clearscope() will touch the addresses only when necessary.
970 in6_clearscope(&ip6->ip6_src);
971 in6_clearscope(&ip6->ip6_dst);
974 * If the outgoing packet contains a hop-by-hop options header,
975 * it must be examined and processed even by the source node.
976 * (RFC 2460, section 4.)
978 if (exthdrs.ip6e_hbh) {
979 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
980 u_int32_t dummy; /* XXX unused */
981 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
984 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
985 panic("ip6e_hbh is not contiguous");
988 * XXX: if we have to send an ICMPv6 error to the sender,
989 * we need the M_LOOP flag since icmp6_error() expects
990 * the IPv6 and the hop-by-hop options header are
991 * contiguous unless the flag is set.
993 m->m_flags |= M_LOOP;
994 m->m_pkthdr.rcvif = ifp;
995 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
996 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
997 &dummy, &plen) < 0) {
998 /* m was already freed at this point. */
999 error = EINVAL;/* better error? */
1002 m->m_flags &= ~M_LOOP; /* XXX */
1003 m->m_pkthdr.rcvif = NULL;
1006 /* Jump over all PFIL processing if hooks are not active. */
1007 if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
1010 odst = ip6->ip6_dst;
1011 /* Run through list of hooks for output packets. */
1012 switch (pfil_run_hooks(V_inet6_pfil_head, &m, ifp, PFIL_OUT, inp)) {
1014 ip6 = mtod(m, struct ip6_hdr *);
1024 /* See if destination IP address was changed by packet filter. */
1025 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
1026 m->m_flags |= M_SKIP_FIREWALL;
1027 /* If destination is now ourself drop to ip6_input(). */
1028 if (in6_localip(&ip6->ip6_dst)) {
1029 m->m_flags |= M_FASTFWD_OURS;
1030 if (m->m_pkthdr.rcvif == NULL)
1031 m->m_pkthdr.rcvif = V_loif;
1032 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1033 m->m_pkthdr.csum_flags |=
1034 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1035 m->m_pkthdr.csum_data = 0xffff;
1037 #if defined(SCTP) || defined(SCTP_SUPPORT)
1038 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1039 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1041 error = netisr_queue(NETISR_IPV6, m);
1045 RO_INVALIDATE_CACHE(ro);
1046 needfiblookup = 1; /* Redo the routing table lookup. */
1049 /* See if fib was changed by packet filter. */
1050 if (fibnum != M_GETFIB(m)) {
1051 m->m_flags |= M_SKIP_FIREWALL;
1052 fibnum = M_GETFIB(m);
1054 RO_INVALIDATE_CACHE(ro);
1060 /* See if local, if yes, send it to netisr. */
1061 if (m->m_flags & M_FASTFWD_OURS) {
1062 if (m->m_pkthdr.rcvif == NULL)
1063 m->m_pkthdr.rcvif = V_loif;
1064 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1065 m->m_pkthdr.csum_flags |=
1066 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1067 m->m_pkthdr.csum_data = 0xffff;
1069 #if defined(SCTP) || defined(SCTP_SUPPORT)
1070 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1071 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1073 error = netisr_queue(NETISR_IPV6, m);
1076 /* Or forward to some other address? */
1077 if ((m->m_flags & M_IP6_NEXTHOP) &&
1078 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
1080 dst = (struct sockaddr_in6 *)&ro->ro_dst;
1083 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
1084 m->m_flags |= M_SKIP_FIREWALL;
1085 m->m_flags &= ~M_IP6_NEXTHOP;
1086 m_tag_delete(m, fwd_tag);
1092 EVL_APPLY_PRI(m, vlan_pcp);
1094 /* Ensure the packet data is mapped if the interface requires it. */
1095 if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) {
1096 m = mb_unmapped_to_ext(m);
1098 IP6STAT_INC(ip6s_odropped);
1104 * Send the packet to the outgoing interface.
1105 * If necessary, do IPv6 fragmentation before sending.
1107 * The logic here is rather complex:
1108 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
1109 * 1-a: send as is if tlen <= path mtu
1110 * 1-b: fragment if tlen > path mtu
1112 * 2: if user asks us not to fragment (dontfrag == 1)
1113 * 2-a: send as is if tlen <= interface mtu
1114 * 2-b: error if tlen > interface mtu
1116 * 3: if we always need to attach fragment header (alwaysfrag == 1)
1119 * 4: if dontfrag == 1 && alwaysfrag == 1
1120 * error, as we cannot handle this conflicting request.
1122 sw_csum = m->m_pkthdr.csum_flags;
1124 tso = ((sw_csum & ifp->if_hwassist &
1125 (CSUM_TSO | CSUM_INNER_TSO)) != 0) ? 1 : 0;
1126 sw_csum &= ~ifp->if_hwassist;
1130 * If we added extension headers, we will not do TSO and calculate the
1131 * checksums ourselves for now.
1132 * XXX-BZ Need a framework to know when the NIC can handle it, even
1135 ip6_output_delayed_csum(m, ifp, sw_csum, plen, optlen);
1136 /* XXX-BZ m->m_pkthdr.csum_flags &= ~ifp->if_hwassist; */
1137 tlen = m->m_pkthdr.len;
1139 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
1143 if (dontfrag && alwaysfrag) { /* Case 4. */
1144 /* Conflicting request - can't transmit. */
1148 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* Case 2-b. */
1150 * Even if the DONTFRAG option is specified, we cannot send the
1151 * packet when the data length is larger than the MTU of the
1152 * outgoing interface.
1153 * Notify the error by sending IPV6_PATHMTU ancillary data if
1154 * application wanted to know the MTU value. Also return an
1155 * error code (this is not described in the API spec).
1158 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
1163 /* Transmit packet without fragmentation. */
1164 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* Cases 1-a and 2-a. */
1165 struct in6_ifaddr *ia6;
1167 ip6 = mtod(m, struct ip6_hdr *);
1168 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1170 /* Record statistics for this interface address. */
1171 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
1172 counter_u64_add(ia6->ia_ifa.ifa_obytes,
1175 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1176 (flags & IP_NO_SND_TAG_RL) ? false : true);
1180 /* Try to fragment the packet. Cases 1-b and 3. */
1181 if (mtu < IPV6_MMTU) {
1182 /* Path MTU cannot be less than IPV6_MMTU. */
1184 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1186 } else if (ip6->ip6_plen == 0) {
1187 /* Jumbo payload cannot be fragmented. */
1189 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1195 * Too large for the destination or interface;
1196 * fragment if possible.
1197 * Must be able to put at least 8 bytes per fragment.
1199 if (mtu > IPV6_MAXPACKET)
1200 mtu = IPV6_MAXPACKET;
1202 len = (mtu - unfragpartlen - sizeof(struct ip6_frag)) & ~7;
1205 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1210 * If the interface will not calculate checksums on
1211 * fragmented packets, then do it here.
1212 * XXX-BZ handle the hw offloading case. Need flags.
1214 ip6_output_delayed_csum(m, ifp, m->m_pkthdr.csum_flags, plen,
1218 * Change the next header field of the last header in the
1219 * unfragmentable part.
1221 if (exthdrs.ip6e_rthdr) {
1222 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1223 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1224 } else if (exthdrs.ip6e_dest1) {
1225 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1226 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1227 } else if (exthdrs.ip6e_hbh) {
1228 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1229 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1231 ip6 = mtod(m, struct ip6_hdr *);
1232 nextproto = ip6->ip6_nxt;
1233 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1237 * Loop through length of segment after first fragment,
1238 * make new header and copy data of each part and link onto
1242 id = htonl(ip6_randomid());
1243 error = ip6_fragment(ifp, m, unfragpartlen, nextproto,len, id);
1247 in6_ifstat_inc(ifp, ifs6_out_fragok);
1250 /* Remove leading garbage. */
1259 /* Record statistics for this interface address. */
1261 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1262 counter_u64_add(ia->ia_ifa.ifa_obytes,
1266 EVL_APPLY_PRI(m, vlan_pcp);
1267 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1274 IP6STAT_INC(ip6s_fragmented);
1280 m_freem(exthdrs.ip6e_hbh); /* m_freem() checks if mbuf is NULL. */
1281 m_freem(exthdrs.ip6e_dest1);
1282 m_freem(exthdrs.ip6e_rthdr);
1283 m_freem(exthdrs.ip6e_dest2);
1292 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1296 if (hlen > MCLBYTES)
1297 return (ENOBUFS); /* XXX */
1300 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1302 m = m_get(M_NOWAIT, MT_DATA);
1307 bcopy(hdr, mtod(m, caddr_t), hlen);
1314 * Insert jumbo payload option.
1317 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1323 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1326 * If there is no hop-by-hop options header, allocate new one.
1327 * If there is one but it doesn't have enough space to store the
1328 * jumbo payload option, allocate a cluster to store the whole options.
1329 * Otherwise, use it to store the options.
1331 if (exthdrs->ip6e_hbh == NULL) {
1332 mopt = m_get(M_NOWAIT, MT_DATA);
1335 mopt->m_len = JUMBOOPTLEN;
1336 optbuf = mtod(mopt, u_char *);
1337 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1338 exthdrs->ip6e_hbh = mopt;
1340 struct ip6_hbh *hbh;
1342 mopt = exthdrs->ip6e_hbh;
1343 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1346 * - exthdrs->ip6e_hbh is not referenced from places
1347 * other than exthdrs.
1348 * - exthdrs->ip6e_hbh is not an mbuf chain.
1350 int oldoptlen = mopt->m_len;
1354 * XXX: give up if the whole (new) hbh header does
1355 * not fit even in an mbuf cluster.
1357 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1361 * As a consequence, we must always prepare a cluster
1364 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1367 n->m_len = oldoptlen + JUMBOOPTLEN;
1368 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1370 optbuf = mtod(n, caddr_t) + oldoptlen;
1372 mopt = exthdrs->ip6e_hbh = n;
1374 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1375 mopt->m_len += JUMBOOPTLEN;
1377 optbuf[0] = IP6OPT_PADN;
1381 * Adjust the header length according to the pad and
1382 * the jumbo payload option.
1384 hbh = mtod(mopt, struct ip6_hbh *);
1385 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1388 /* fill in the option. */
1389 optbuf[2] = IP6OPT_JUMBO;
1391 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1392 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1394 /* finally, adjust the packet header length */
1395 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1402 * Insert fragment header and copy unfragmentable header portions.
1405 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1406 struct ip6_frag **frghdrp)
1408 struct mbuf *n, *mlast;
1410 if (hlen > sizeof(struct ip6_hdr)) {
1411 n = m_copym(m0, sizeof(struct ip6_hdr),
1412 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1419 /* Search for the last mbuf of unfragmentable part. */
1420 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1423 if (M_WRITABLE(mlast) &&
1424 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1425 /* use the trailing space of the last mbuf for the fragment hdr */
1426 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1428 mlast->m_len += sizeof(struct ip6_frag);
1429 m->m_pkthdr.len += sizeof(struct ip6_frag);
1431 /* allocate a new mbuf for the fragment header */
1434 mfrg = m_get(M_NOWAIT, MT_DATA);
1437 mfrg->m_len = sizeof(struct ip6_frag);
1438 *frghdrp = mtod(mfrg, struct ip6_frag *);
1439 mlast->m_next = mfrg;
1446 * Calculates IPv6 path mtu for destination @dst.
1447 * Resulting MTU is stored in @mtup.
1449 * Returns 0 on success.
1452 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1454 struct epoch_tracker et;
1455 struct nhop_object *nh;
1456 struct in6_addr kdst;
1460 in6_splitscope(dst, &kdst, &scopeid);
1462 NET_EPOCH_ENTER(et);
1463 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1465 error = ip6_calcmtu(nh->nh_ifp, dst, nh->nh_mtu, mtup, NULL, 0);
1467 error = EHOSTUNREACH;
1474 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1475 * and cached data in @ro_pmtu.
1476 * MTU from (successful) route lookup is saved (along with dst)
1477 * inside @ro_pmtu to avoid subsequent route lookups after packet
1478 * filter processing.
1480 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1481 * Returns 0 on success.
1484 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1485 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1486 int *alwaysfragp, u_int fibnum, u_int proto)
1488 struct nhop_object *nh;
1489 struct in6_addr kdst;
1491 struct sockaddr_in6 *sa6_dst, sin6;
1497 if (ro_pmtu == NULL || do_lookup) {
1499 * Here ro_pmtu has final destination address, while
1500 * ro might represent immediate destination.
1501 * Use ro_pmtu destination since mtu might differ.
1503 if (ro_pmtu != NULL) {
1504 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1505 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1506 ro_pmtu->ro_mtu = 0;
1510 if (ro_pmtu == NULL || ro_pmtu->ro_mtu == 0) {
1511 bzero(sa6_dst, sizeof(*sa6_dst));
1512 sa6_dst->sin6_family = AF_INET6;
1513 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1514 sa6_dst->sin6_addr = *dst;
1516 in6_splitscope(dst, &kdst, &scopeid);
1517 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1520 if (ro_pmtu != NULL)
1521 ro_pmtu->ro_mtu = mtu;
1524 mtu = ro_pmtu->ro_mtu;
1527 if (ro_pmtu != NULL && ro_pmtu->ro_nh != NULL)
1528 mtu = ro_pmtu->ro_nh->nh_mtu;
1530 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1534 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1535 * hostcache data for @dst.
1536 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1538 * Returns 0 on success.
1541 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1542 u_long *mtup, int *alwaysfragp, u_int proto)
1550 struct in_conninfo inc;
1552 bzero(&inc, sizeof(inc));
1553 inc.inc_flags |= INC_ISIPV6;
1554 inc.inc6_faddr = *dst;
1556 ifmtu = IN6_LINKMTU(ifp);
1558 /* TCP is known to react to pmtu changes so skip hc */
1559 if (proto != IPPROTO_TCP)
1560 mtu = tcp_hc_getmtu(&inc);
1563 mtu = min(mtu, rt_mtu);
1568 else if (mtu < IPV6_MMTU) {
1570 * RFC2460 section 5, last paragraph:
1571 * if we record ICMPv6 too big message with
1572 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1573 * or smaller, with framgent header attached.
1574 * (fragment header is needed regardless from the
1575 * packet size, for translators to identify packets)
1581 mtu = IN6_LINKMTU(ifp);
1583 error = EHOSTUNREACH; /* XXX */
1587 *alwaysfragp = alwaysfrag;
1592 * IP6 socket option processing.
1595 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1597 int optdatalen, uproto;
1599 struct inpcb *inp = sotoinpcb(so);
1601 int level, op, optname;
1605 uint32_t rss_bucket;
1610 * Don't use more than a quarter of mbuf clusters. N.B.:
1611 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1612 * on LP64 architectures, so cast to u_long to avoid undefined
1613 * behavior. ILP32 architectures cannot have nmbclusters
1614 * large enough to overflow for other reasons.
1616 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1618 level = sopt->sopt_level;
1619 op = sopt->sopt_dir;
1620 optname = sopt->sopt_name;
1621 optlen = sopt->sopt_valsize;
1625 uproto = (int)so->so_proto->pr_protocol;
1627 if (level != IPPROTO_IPV6) {
1630 if (sopt->sopt_level == SOL_SOCKET &&
1631 sopt->sopt_dir == SOPT_SET) {
1632 switch (sopt->sopt_name) {
1635 if ((so->so_options & SO_REUSEADDR) != 0)
1636 inp->inp_flags2 |= INP_REUSEADDR;
1638 inp->inp_flags2 &= ~INP_REUSEADDR;
1644 if ((so->so_options & SO_REUSEPORT) != 0)
1645 inp->inp_flags2 |= INP_REUSEPORT;
1647 inp->inp_flags2 &= ~INP_REUSEPORT;
1651 case SO_REUSEPORT_LB:
1653 if ((so->so_options & SO_REUSEPORT_LB) != 0)
1654 inp->inp_flags2 |= INP_REUSEPORT_LB;
1656 inp->inp_flags2 &= ~INP_REUSEPORT_LB;
1662 inp->inp_inc.inc_fibnum = so->so_fibnum;
1666 case SO_MAX_PACING_RATE:
1669 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1680 } else { /* level == IPPROTO_IPV6 */
1684 case IPV6_2292PKTOPTIONS:
1685 #ifdef IPV6_PKTOPTIONS
1686 case IPV6_PKTOPTIONS:
1691 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1692 printf("ip6_ctloutput: mbuf limit hit\n");
1697 error = soopt_getm(sopt, &m); /* XXX */
1700 error = soopt_mcopyin(sopt, m); /* XXX */
1704 error = ip6_pcbopts(&inp->in6p_outputopts, m,
1707 m_freem(m); /* XXX */
1712 * Use of some Hop-by-Hop options or some
1713 * Destination options, might require special
1714 * privilege. That is, normal applications
1715 * (without special privilege) might be forbidden
1716 * from setting certain options in outgoing packets,
1717 * and might never see certain options in received
1718 * packets. [RFC 2292 Section 6]
1719 * KAME specific note:
1720 * KAME prevents non-privileged users from sending or
1721 * receiving ANY hbh/dst options in order to avoid
1722 * overhead of parsing options in the kernel.
1724 case IPV6_RECVHOPOPTS:
1725 case IPV6_RECVDSTOPTS:
1726 case IPV6_RECVRTHDRDSTOPTS:
1728 error = priv_check(td,
1729 PRIV_NETINET_SETHDROPTS);
1734 case IPV6_UNICAST_HOPS:
1737 case IPV6_RECVPKTINFO:
1738 case IPV6_RECVHOPLIMIT:
1739 case IPV6_RECVRTHDR:
1740 case IPV6_RECVPATHMTU:
1741 case IPV6_RECVTCLASS:
1742 case IPV6_RECVFLOWID:
1744 case IPV6_RECVRSSBUCKETID:
1747 case IPV6_AUTOFLOWLABEL:
1748 case IPV6_ORIGDSTADDR:
1750 case IPV6_BINDMULTI:
1752 case IPV6_RSS_LISTEN_BUCKET:
1755 if (optname == IPV6_BINDANY && td != NULL) {
1756 error = priv_check(td,
1757 PRIV_NETINET_BINDANY);
1762 if (optlen != sizeof(int)) {
1766 error = sooptcopyin(sopt, &optval,
1767 sizeof optval, sizeof optval);
1771 case IPV6_UNICAST_HOPS:
1772 if (optval < -1 || optval >= 256)
1775 /* -1 = kernel default */
1776 inp->in6p_hops = optval;
1777 if ((inp->inp_vflag &
1779 inp->inp_ip_ttl = optval;
1782 #define OPTSET(bit) \
1786 inp->inp_flags |= (bit); \
1788 inp->inp_flags &= ~(bit); \
1790 } while (/*CONSTCOND*/ 0)
1791 #define OPTSET2292(bit) \
1794 inp->inp_flags |= IN6P_RFC2292; \
1796 inp->inp_flags |= (bit); \
1798 inp->inp_flags &= ~(bit); \
1800 } while (/*CONSTCOND*/ 0)
1801 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0)
1803 #define OPTSET2_N(bit, val) do { \
1805 inp->inp_flags2 |= bit; \
1807 inp->inp_flags2 &= ~bit; \
1809 #define OPTSET2(bit, val) do { \
1811 OPTSET2_N(bit, val); \
1814 #define OPTBIT2(bit) (inp->inp_flags2 & (bit) ? 1 : 0)
1815 #define OPTSET2292_EXCLUSIVE(bit) \
1818 if (OPTBIT(IN6P_RFC2292)) { \
1822 inp->inp_flags |= (bit); \
1824 inp->inp_flags &= ~(bit); \
1827 } while (/*CONSTCOND*/ 0)
1829 case IPV6_RECVPKTINFO:
1830 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1835 struct ip6_pktopts **optp;
1837 /* cannot mix with RFC2292 */
1838 if (OPTBIT(IN6P_RFC2292)) {
1843 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1845 return (ECONNRESET);
1847 optp = &inp->in6p_outputopts;
1848 error = ip6_pcbopt(IPV6_HOPLIMIT,
1849 (u_char *)&optval, sizeof(optval),
1850 optp, (td != NULL) ? td->td_ucred :
1856 case IPV6_RECVHOPLIMIT:
1857 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1860 case IPV6_RECVHOPOPTS:
1861 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1864 case IPV6_RECVDSTOPTS:
1865 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1868 case IPV6_RECVRTHDRDSTOPTS:
1869 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1872 case IPV6_RECVRTHDR:
1873 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1876 case IPV6_RECVPATHMTU:
1878 * We ignore this option for TCP
1880 * (RFC3542 leaves this case
1883 if (uproto != IPPROTO_TCP)
1887 case IPV6_RECVFLOWID:
1888 OPTSET2(INP_RECVFLOWID, optval);
1892 case IPV6_RECVRSSBUCKETID:
1893 OPTSET2(INP_RECVRSSBUCKETID, optval);
1899 if (inp->inp_lport ||
1900 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1902 * The socket is already bound.
1909 inp->inp_flags |= IN6P_IPV6_V6ONLY;
1910 inp->inp_vflag &= ~INP_IPV4;
1912 inp->inp_flags &= ~IN6P_IPV6_V6ONLY;
1913 inp->inp_vflag |= INP_IPV4;
1917 case IPV6_RECVTCLASS:
1918 /* cannot mix with RFC2292 XXX */
1919 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1921 case IPV6_AUTOFLOWLABEL:
1922 OPTSET(IN6P_AUTOFLOWLABEL);
1925 case IPV6_ORIGDSTADDR:
1926 OPTSET2(INP_ORIGDSTADDR, optval);
1929 OPTSET(INP_BINDANY);
1932 case IPV6_BINDMULTI:
1933 OPTSET2(INP_BINDMULTI, optval);
1936 case IPV6_RSS_LISTEN_BUCKET:
1937 if ((optval >= 0) &&
1938 (optval < rss_getnumbuckets())) {
1940 inp->inp_rss_listen_bucket = optval;
1941 OPTSET2_N(INP_RSS_BUCKET_SET, 1);
1949 if ((optval >= -1) && (optval <=
1950 (INP_2PCP_MASK >> INP_2PCP_SHIFT))) {
1976 case IPV6_USE_MIN_MTU:
1977 case IPV6_PREFER_TEMPADDR:
1978 if (optlen != sizeof(optval)) {
1982 error = sooptcopyin(sopt, &optval,
1983 sizeof optval, sizeof optval);
1987 struct ip6_pktopts **optp;
1989 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1991 return (ECONNRESET);
1993 optp = &inp->in6p_outputopts;
1994 error = ip6_pcbopt(optname,
1995 (u_char *)&optval, sizeof(optval),
1996 optp, (td != NULL) ? td->td_ucred :
2002 case IPV6_2292PKTINFO:
2003 case IPV6_2292HOPLIMIT:
2004 case IPV6_2292HOPOPTS:
2005 case IPV6_2292DSTOPTS:
2006 case IPV6_2292RTHDR:
2008 if (optlen != sizeof(int)) {
2012 error = sooptcopyin(sopt, &optval,
2013 sizeof optval, sizeof optval);
2017 case IPV6_2292PKTINFO:
2018 OPTSET2292(IN6P_PKTINFO);
2020 case IPV6_2292HOPLIMIT:
2021 OPTSET2292(IN6P_HOPLIMIT);
2023 case IPV6_2292HOPOPTS:
2025 * Check super-user privilege.
2026 * See comments for IPV6_RECVHOPOPTS.
2029 error = priv_check(td,
2030 PRIV_NETINET_SETHDROPTS);
2034 OPTSET2292(IN6P_HOPOPTS);
2036 case IPV6_2292DSTOPTS:
2038 error = priv_check(td,
2039 PRIV_NETINET_SETHDROPTS);
2043 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
2045 case IPV6_2292RTHDR:
2046 OPTSET2292(IN6P_RTHDR);
2054 case IPV6_RTHDRDSTOPTS:
2057 /* new advanced API (RFC3542) */
2059 u_char optbuf_storage[MCLBYTES];
2061 struct ip6_pktopts **optp;
2063 /* cannot mix with RFC2292 */
2064 if (OPTBIT(IN6P_RFC2292)) {
2070 * We only ensure valsize is not too large
2071 * here. Further validation will be done
2074 error = sooptcopyin(sopt, optbuf_storage,
2075 sizeof(optbuf_storage), 0);
2078 optlen = sopt->sopt_valsize;
2079 optbuf = optbuf_storage;
2081 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2083 return (ECONNRESET);
2085 optp = &inp->in6p_outputopts;
2086 error = ip6_pcbopt(optname, optbuf, optlen,
2087 optp, (td != NULL) ? td->td_ucred : NULL,
2094 case IPV6_MULTICAST_IF:
2095 case IPV6_MULTICAST_HOPS:
2096 case IPV6_MULTICAST_LOOP:
2097 case IPV6_JOIN_GROUP:
2098 case IPV6_LEAVE_GROUP:
2100 case MCAST_BLOCK_SOURCE:
2101 case MCAST_UNBLOCK_SOURCE:
2102 case MCAST_JOIN_GROUP:
2103 case MCAST_LEAVE_GROUP:
2104 case MCAST_JOIN_SOURCE_GROUP:
2105 case MCAST_LEAVE_SOURCE_GROUP:
2106 error = ip6_setmoptions(inp, sopt);
2109 case IPV6_PORTRANGE:
2110 error = sooptcopyin(sopt, &optval,
2111 sizeof optval, sizeof optval);
2117 case IPV6_PORTRANGE_DEFAULT:
2118 inp->inp_flags &= ~(INP_LOWPORT);
2119 inp->inp_flags &= ~(INP_HIGHPORT);
2122 case IPV6_PORTRANGE_HIGH:
2123 inp->inp_flags &= ~(INP_LOWPORT);
2124 inp->inp_flags |= INP_HIGHPORT;
2127 case IPV6_PORTRANGE_LOW:
2128 inp->inp_flags &= ~(INP_HIGHPORT);
2129 inp->inp_flags |= INP_LOWPORT;
2139 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2140 case IPV6_IPSEC_POLICY:
2141 if (IPSEC_ENABLED(ipv6)) {
2142 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2149 error = ENOPROTOOPT;
2156 case IPV6_2292PKTOPTIONS:
2157 #ifdef IPV6_PKTOPTIONS
2158 case IPV6_PKTOPTIONS:
2161 * RFC3542 (effectively) deprecated the
2162 * semantics of the 2292-style pktoptions.
2163 * Since it was not reliable in nature (i.e.,
2164 * applications had to expect the lack of some
2165 * information after all), it would make sense
2166 * to simplify this part by always returning
2169 sopt->sopt_valsize = 0;
2172 case IPV6_RECVHOPOPTS:
2173 case IPV6_RECVDSTOPTS:
2174 case IPV6_RECVRTHDRDSTOPTS:
2175 case IPV6_UNICAST_HOPS:
2176 case IPV6_RECVPKTINFO:
2177 case IPV6_RECVHOPLIMIT:
2178 case IPV6_RECVRTHDR:
2179 case IPV6_RECVPATHMTU:
2182 case IPV6_PORTRANGE:
2183 case IPV6_RECVTCLASS:
2184 case IPV6_AUTOFLOWLABEL:
2188 case IPV6_RECVFLOWID:
2190 case IPV6_RSSBUCKETID:
2191 case IPV6_RECVRSSBUCKETID:
2193 case IPV6_BINDMULTI:
2196 case IPV6_RECVHOPOPTS:
2197 optval = OPTBIT(IN6P_HOPOPTS);
2200 case IPV6_RECVDSTOPTS:
2201 optval = OPTBIT(IN6P_DSTOPTS);
2204 case IPV6_RECVRTHDRDSTOPTS:
2205 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2208 case IPV6_UNICAST_HOPS:
2209 optval = inp->in6p_hops;
2212 case IPV6_RECVPKTINFO:
2213 optval = OPTBIT(IN6P_PKTINFO);
2216 case IPV6_RECVHOPLIMIT:
2217 optval = OPTBIT(IN6P_HOPLIMIT);
2220 case IPV6_RECVRTHDR:
2221 optval = OPTBIT(IN6P_RTHDR);
2224 case IPV6_RECVPATHMTU:
2225 optval = OPTBIT(IN6P_MTU);
2229 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2232 case IPV6_PORTRANGE:
2235 flags = inp->inp_flags;
2236 if (flags & INP_HIGHPORT)
2237 optval = IPV6_PORTRANGE_HIGH;
2238 else if (flags & INP_LOWPORT)
2239 optval = IPV6_PORTRANGE_LOW;
2244 case IPV6_RECVTCLASS:
2245 optval = OPTBIT(IN6P_TCLASS);
2248 case IPV6_AUTOFLOWLABEL:
2249 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2252 case IPV6_ORIGDSTADDR:
2253 optval = OPTBIT2(INP_ORIGDSTADDR);
2257 optval = OPTBIT(INP_BINDANY);
2261 optval = inp->inp_flowid;
2265 optval = inp->inp_flowtype;
2268 case IPV6_RECVFLOWID:
2269 optval = OPTBIT2(INP_RECVFLOWID);
2272 case IPV6_RSSBUCKETID:
2274 rss_hash2bucket(inp->inp_flowid,
2278 optval = rss_bucket;
2283 case IPV6_RECVRSSBUCKETID:
2284 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2288 case IPV6_BINDMULTI:
2289 optval = OPTBIT2(INP_BINDMULTI);
2293 if (OPTBIT2(INP_2PCP_SET)) {
2294 optval = (inp->inp_flags2 &
2305 error = sooptcopyout(sopt, &optval,
2312 struct ip6_mtuinfo mtuinfo;
2313 struct in6_addr addr;
2315 if (!(so->so_state & SS_ISCONNECTED))
2318 * XXX: we dot not consider the case of source
2319 * routing, or optional information to specify
2320 * the outgoing interface.
2321 * Copy faddr out of inp to avoid holding lock
2322 * on inp during route lookup.
2325 bcopy(&inp->in6p_faddr, &addr, sizeof(addr));
2327 error = ip6_getpmtu_ctl(so->so_fibnum,
2331 if (pmtu > IPV6_MAXPACKET)
2332 pmtu = IPV6_MAXPACKET;
2334 bzero(&mtuinfo, sizeof(mtuinfo));
2335 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2336 optdata = (void *)&mtuinfo;
2337 optdatalen = sizeof(mtuinfo);
2338 error = sooptcopyout(sopt, optdata,
2343 case IPV6_2292PKTINFO:
2344 case IPV6_2292HOPLIMIT:
2345 case IPV6_2292HOPOPTS:
2346 case IPV6_2292RTHDR:
2347 case IPV6_2292DSTOPTS:
2349 case IPV6_2292PKTINFO:
2350 optval = OPTBIT(IN6P_PKTINFO);
2352 case IPV6_2292HOPLIMIT:
2353 optval = OPTBIT(IN6P_HOPLIMIT);
2355 case IPV6_2292HOPOPTS:
2356 optval = OPTBIT(IN6P_HOPOPTS);
2358 case IPV6_2292RTHDR:
2359 optval = OPTBIT(IN6P_RTHDR);
2361 case IPV6_2292DSTOPTS:
2362 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2365 error = sooptcopyout(sopt, &optval,
2372 case IPV6_RTHDRDSTOPTS:
2376 case IPV6_USE_MIN_MTU:
2377 case IPV6_PREFER_TEMPADDR:
2378 error = ip6_getpcbopt(inp, optname, sopt);
2381 case IPV6_MULTICAST_IF:
2382 case IPV6_MULTICAST_HOPS:
2383 case IPV6_MULTICAST_LOOP:
2385 error = ip6_getmoptions(inp, sopt);
2388 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2389 case IPV6_IPSEC_POLICY:
2390 if (IPSEC_ENABLED(ipv6)) {
2391 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2397 error = ENOPROTOOPT;
2407 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2409 int error = 0, optval, optlen;
2410 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2411 struct inpcb *inp = sotoinpcb(so);
2412 int level, op, optname;
2414 level = sopt->sopt_level;
2415 op = sopt->sopt_dir;
2416 optname = sopt->sopt_name;
2417 optlen = sopt->sopt_valsize;
2419 if (level != IPPROTO_IPV6) {
2426 * For ICMPv6 sockets, no modification allowed for checksum
2427 * offset, permit "no change" values to help existing apps.
2429 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2430 * for an ICMPv6 socket will fail."
2431 * The current behavior does not meet RFC3542.
2435 if (optlen != sizeof(int)) {
2439 error = sooptcopyin(sopt, &optval, sizeof(optval),
2443 if (optval < -1 || (optval % 2) != 0) {
2445 * The API assumes non-negative even offset
2446 * values or -1 as a special value.
2449 } else if (so->so_proto->pr_protocol ==
2451 if (optval != icmp6off)
2454 inp->in6p_cksum = optval;
2458 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2461 optval = inp->in6p_cksum;
2463 error = sooptcopyout(sopt, &optval, sizeof(optval));
2473 error = ENOPROTOOPT;
2481 * Set up IP6 options in pcb for insertion in output packets or
2482 * specifying behavior of outgoing packets.
2485 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2486 struct socket *so, struct sockopt *sopt)
2488 struct ip6_pktopts *opt = *pktopt;
2490 struct thread *td = sopt->sopt_td;
2491 struct epoch_tracker et;
2493 /* turn off any old options. */
2496 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2497 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2498 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2499 printf("ip6_pcbopts: all specified options are cleared.\n");
2501 ip6_clearpktopts(opt, -1);
2503 opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT);
2509 if (!m || m->m_len == 0) {
2511 * Only turning off any previous options, regardless of
2512 * whether the opt is just created or given.
2514 free(opt, M_IP6OPT);
2518 /* set options specified by user. */
2519 NET_EPOCH_ENTER(et);
2520 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2521 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2522 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2523 free(opt, M_IP6OPT);
2533 * initialize ip6_pktopts. beware that there are non-zero default values in
2537 ip6_initpktopts(struct ip6_pktopts *opt)
2540 bzero(opt, sizeof(*opt));
2541 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2542 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2543 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2544 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2548 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2549 struct ucred *cred, int uproto)
2551 struct epoch_tracker et;
2552 struct ip6_pktopts *opt;
2555 if (*pktopt == NULL) {
2556 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2558 if (*pktopt == NULL)
2560 ip6_initpktopts(*pktopt);
2564 NET_EPOCH_ENTER(et);
2565 ret = ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto);
2571 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2572 if (pktopt && pktopt->field) { \
2574 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2575 malloc_optdata = true; \
2577 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
2579 free(optdata, M_TEMP); \
2580 return (ECONNRESET); \
2582 pktopt = inp->in6p_outputopts; \
2583 if (pktopt && pktopt->field) { \
2584 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2585 bcopy(pktopt->field, optdata, optdatalen); \
2587 free(optdata, M_TEMP); \
2589 malloc_optdata = false; \
2594 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2595 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2597 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2598 pktopt->field->sa_len)
2601 ip6_getpcbopt(struct inpcb *inp, int optname, struct sockopt *sopt)
2603 void *optdata = NULL;
2604 bool malloc_optdata = false;
2607 struct in6_pktinfo null_pktinfo;
2608 int deftclass = 0, on;
2609 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2610 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2611 struct ip6_pktopts *pktopt;
2614 pktopt = inp->in6p_outputopts;
2618 optdata = (void *)&null_pktinfo;
2619 if (pktopt && pktopt->ip6po_pktinfo) {
2620 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2621 sizeof(null_pktinfo));
2622 in6_clearscope(&null_pktinfo.ipi6_addr);
2624 /* XXX: we don't have to do this every time... */
2625 bzero(&null_pktinfo, sizeof(null_pktinfo));
2627 optdatalen = sizeof(struct in6_pktinfo);
2630 if (pktopt && pktopt->ip6po_tclass >= 0)
2631 deftclass = pktopt->ip6po_tclass;
2632 optdata = (void *)&deftclass;
2633 optdatalen = sizeof(int);
2636 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2639 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2641 case IPV6_RTHDRDSTOPTS:
2642 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2645 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2648 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2650 case IPV6_USE_MIN_MTU:
2652 defminmtu = pktopt->ip6po_minmtu;
2653 optdata = (void *)&defminmtu;
2654 optdatalen = sizeof(int);
2657 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2661 optdata = (void *)&on;
2662 optdatalen = sizeof(on);
2664 case IPV6_PREFER_TEMPADDR:
2666 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2667 optdata = (void *)&defpreftemp;
2668 optdatalen = sizeof(int);
2670 default: /* should not happen */
2672 panic("ip6_getpcbopt: unexpected option\n");
2675 return (ENOPROTOOPT);
2679 error = sooptcopyout(sopt, optdata, optdatalen);
2681 free(optdata, M_TEMP);
2687 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2692 if (optname == -1 || optname == IPV6_PKTINFO) {
2693 if (pktopt->ip6po_pktinfo)
2694 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2695 pktopt->ip6po_pktinfo = NULL;
2697 if (optname == -1 || optname == IPV6_HOPLIMIT)
2698 pktopt->ip6po_hlim = -1;
2699 if (optname == -1 || optname == IPV6_TCLASS)
2700 pktopt->ip6po_tclass = -1;
2701 if (optname == -1 || optname == IPV6_NEXTHOP) {
2702 if (pktopt->ip6po_nextroute.ro_nh) {
2703 NH_FREE(pktopt->ip6po_nextroute.ro_nh);
2704 pktopt->ip6po_nextroute.ro_nh = NULL;
2706 if (pktopt->ip6po_nexthop)
2707 free(pktopt->ip6po_nexthop, M_IP6OPT);
2708 pktopt->ip6po_nexthop = NULL;
2710 if (optname == -1 || optname == IPV6_HOPOPTS) {
2711 if (pktopt->ip6po_hbh)
2712 free(pktopt->ip6po_hbh, M_IP6OPT);
2713 pktopt->ip6po_hbh = NULL;
2715 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2716 if (pktopt->ip6po_dest1)
2717 free(pktopt->ip6po_dest1, M_IP6OPT);
2718 pktopt->ip6po_dest1 = NULL;
2720 if (optname == -1 || optname == IPV6_RTHDR) {
2721 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2722 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2723 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2724 if (pktopt->ip6po_route.ro_nh) {
2725 NH_FREE(pktopt->ip6po_route.ro_nh);
2726 pktopt->ip6po_route.ro_nh = NULL;
2729 if (optname == -1 || optname == IPV6_DSTOPTS) {
2730 if (pktopt->ip6po_dest2)
2731 free(pktopt->ip6po_dest2, M_IP6OPT);
2732 pktopt->ip6po_dest2 = NULL;
2736 #define PKTOPT_EXTHDRCPY(type) \
2739 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2740 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2741 if (dst->type == NULL)\
2743 bcopy(src->type, dst->type, hlen);\
2745 } while (/*CONSTCOND*/ 0)
2748 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2750 if (dst == NULL || src == NULL) {
2751 printf("ip6_clearpktopts: invalid argument\n");
2755 dst->ip6po_hlim = src->ip6po_hlim;
2756 dst->ip6po_tclass = src->ip6po_tclass;
2757 dst->ip6po_flags = src->ip6po_flags;
2758 dst->ip6po_minmtu = src->ip6po_minmtu;
2759 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2760 if (src->ip6po_pktinfo) {
2761 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2763 if (dst->ip6po_pktinfo == NULL)
2765 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2767 if (src->ip6po_nexthop) {
2768 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2770 if (dst->ip6po_nexthop == NULL)
2772 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2773 src->ip6po_nexthop->sa_len);
2775 PKTOPT_EXTHDRCPY(ip6po_hbh);
2776 PKTOPT_EXTHDRCPY(ip6po_dest1);
2777 PKTOPT_EXTHDRCPY(ip6po_dest2);
2778 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2782 ip6_clearpktopts(dst, -1);
2785 #undef PKTOPT_EXTHDRCPY
2787 struct ip6_pktopts *
2788 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2791 struct ip6_pktopts *dst;
2793 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2796 ip6_initpktopts(dst);
2798 if ((error = copypktopts(dst, src, canwait)) != 0) {
2799 free(dst, M_IP6OPT);
2807 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2812 ip6_clearpktopts(pktopt, -1);
2814 free(pktopt, M_IP6OPT);
2818 * Set IPv6 outgoing packet options based on advanced API.
2821 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2822 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2824 struct cmsghdr *cm = NULL;
2826 if (control == NULL || opt == NULL)
2830 * ip6_setpktopt can call ifnet_byindex(), so it's imperative that we
2831 * are in the network epoch here.
2835 ip6_initpktopts(opt);
2840 * If stickyopt is provided, make a local copy of the options
2841 * for this particular packet, then override them by ancillary
2843 * XXX: copypktopts() does not copy the cached route to a next
2844 * hop (if any). This is not very good in terms of efficiency,
2845 * but we can allow this since this option should be rarely
2848 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2853 * XXX: Currently, we assume all the optional information is stored
2856 if (control->m_next)
2859 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2860 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2863 if (control->m_len < CMSG_LEN(0))
2866 cm = mtod(control, struct cmsghdr *);
2867 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2869 if (cm->cmsg_level != IPPROTO_IPV6)
2872 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2873 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2882 * Set a particular packet option, as a sticky option or an ancillary data
2883 * item. "len" can be 0 only when it's a sticky option.
2884 * We have 4 cases of combination of "sticky" and "cmsg":
2885 * "sticky=0, cmsg=0": impossible
2886 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2887 * "sticky=1, cmsg=0": RFC3542 socket option
2888 * "sticky=1, cmsg=1": RFC2292 socket option
2891 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2892 struct ucred *cred, int sticky, int cmsg, int uproto)
2894 int minmtupolicy, preftemp;
2899 if (!sticky && !cmsg) {
2901 printf("ip6_setpktopt: impossible case\n");
2907 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2908 * not be specified in the context of RFC3542. Conversely,
2909 * RFC3542 types should not be specified in the context of RFC2292.
2913 case IPV6_2292PKTINFO:
2914 case IPV6_2292HOPLIMIT:
2915 case IPV6_2292NEXTHOP:
2916 case IPV6_2292HOPOPTS:
2917 case IPV6_2292DSTOPTS:
2918 case IPV6_2292RTHDR:
2919 case IPV6_2292PKTOPTIONS:
2920 return (ENOPROTOOPT);
2923 if (sticky && cmsg) {
2930 case IPV6_RTHDRDSTOPTS:
2932 case IPV6_USE_MIN_MTU:
2935 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2936 return (ENOPROTOOPT);
2941 case IPV6_2292PKTINFO:
2944 struct ifnet *ifp = NULL;
2945 struct in6_pktinfo *pktinfo;
2947 if (len != sizeof(struct in6_pktinfo))
2950 pktinfo = (struct in6_pktinfo *)buf;
2953 * An application can clear any sticky IPV6_PKTINFO option by
2954 * doing a "regular" setsockopt with ipi6_addr being
2955 * in6addr_any and ipi6_ifindex being zero.
2956 * [RFC 3542, Section 6]
2958 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2959 pktinfo->ipi6_ifindex == 0 &&
2960 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2961 ip6_clearpktopts(opt, optname);
2965 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2966 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2969 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2971 /* validate the interface index if specified. */
2972 if (pktinfo->ipi6_ifindex) {
2973 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2977 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2978 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2982 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2983 struct in6_ifaddr *ia;
2985 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2986 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2988 return (EADDRNOTAVAIL);
2989 ifa_free(&ia->ia_ifa);
2992 * We store the address anyway, and let in6_selectsrc()
2993 * validate the specified address. This is because ipi6_addr
2994 * may not have enough information about its scope zone, and
2995 * we may need additional information (such as outgoing
2996 * interface or the scope zone of a destination address) to
2997 * disambiguate the scope.
2998 * XXX: the delay of the validation may confuse the
2999 * application when it is used as a sticky option.
3001 if (opt->ip6po_pktinfo == NULL) {
3002 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
3003 M_IP6OPT, M_NOWAIT);
3004 if (opt->ip6po_pktinfo == NULL)
3007 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
3011 case IPV6_2292HOPLIMIT:
3017 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3018 * to simplify the ordering among hoplimit options.
3020 if (optname == IPV6_HOPLIMIT && sticky)
3021 return (ENOPROTOOPT);
3023 if (len != sizeof(int))
3026 if (*hlimp < -1 || *hlimp > 255)
3029 opt->ip6po_hlim = *hlimp;
3037 if (len != sizeof(int))
3039 tclass = *(int *)buf;
3040 if (tclass < -1 || tclass > 255)
3043 opt->ip6po_tclass = tclass;
3047 case IPV6_2292NEXTHOP:
3050 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3055 if (len == 0) { /* just remove the option */
3056 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3060 /* check if cmsg_len is large enough for sa_len */
3061 if (len < sizeof(struct sockaddr) || len < *buf)
3064 switch (((struct sockaddr *)buf)->sa_family) {
3067 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3070 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3073 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3074 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3077 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
3083 case AF_LINK: /* should eventually be supported */
3085 return (EAFNOSUPPORT);
3088 /* turn off the previous option, then set the new option. */
3089 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3090 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3091 if (opt->ip6po_nexthop == NULL)
3093 bcopy(buf, opt->ip6po_nexthop, *buf);
3096 case IPV6_2292HOPOPTS:
3099 struct ip6_hbh *hbh;
3103 * XXX: We don't allow a non-privileged user to set ANY HbH
3104 * options, since per-option restriction has too much
3108 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3114 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3115 break; /* just remove the option */
3118 /* message length validation */
3119 if (len < sizeof(struct ip6_hbh))
3121 hbh = (struct ip6_hbh *)buf;
3122 hbhlen = (hbh->ip6h_len + 1) << 3;
3126 /* turn off the previous option, then set the new option. */
3127 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3128 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3129 if (opt->ip6po_hbh == NULL)
3131 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3136 case IPV6_2292DSTOPTS:
3138 case IPV6_RTHDRDSTOPTS:
3140 struct ip6_dest *dest, **newdest = NULL;
3143 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
3144 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3150 ip6_clearpktopts(opt, optname);
3151 break; /* just remove the option */
3154 /* message length validation */
3155 if (len < sizeof(struct ip6_dest))
3157 dest = (struct ip6_dest *)buf;
3158 destlen = (dest->ip6d_len + 1) << 3;
3163 * Determine the position that the destination options header
3164 * should be inserted; before or after the routing header.
3167 case IPV6_2292DSTOPTS:
3169 * The old advacned API is ambiguous on this point.
3170 * Our approach is to determine the position based
3171 * according to the existence of a routing header.
3172 * Note, however, that this depends on the order of the
3173 * extension headers in the ancillary data; the 1st
3174 * part of the destination options header must appear
3175 * before the routing header in the ancillary data,
3177 * RFC3542 solved the ambiguity by introducing
3178 * separate ancillary data or option types.
3180 if (opt->ip6po_rthdr == NULL)
3181 newdest = &opt->ip6po_dest1;
3183 newdest = &opt->ip6po_dest2;
3185 case IPV6_RTHDRDSTOPTS:
3186 newdest = &opt->ip6po_dest1;
3189 newdest = &opt->ip6po_dest2;
3193 /* turn off the previous option, then set the new option. */
3194 ip6_clearpktopts(opt, optname);
3195 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3196 if (*newdest == NULL)
3198 bcopy(dest, *newdest, destlen);
3203 case IPV6_2292RTHDR:
3206 struct ip6_rthdr *rth;
3210 ip6_clearpktopts(opt, IPV6_RTHDR);
3211 break; /* just remove the option */
3214 /* message length validation */
3215 if (len < sizeof(struct ip6_rthdr))
3217 rth = (struct ip6_rthdr *)buf;
3218 rthlen = (rth->ip6r_len + 1) << 3;
3222 switch (rth->ip6r_type) {
3223 case IPV6_RTHDR_TYPE_0:
3224 if (rth->ip6r_len == 0) /* must contain one addr */
3226 if (rth->ip6r_len % 2) /* length must be even */
3228 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3232 return (EINVAL); /* not supported */
3235 /* turn off the previous option */
3236 ip6_clearpktopts(opt, IPV6_RTHDR);
3237 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3238 if (opt->ip6po_rthdr == NULL)
3240 bcopy(rth, opt->ip6po_rthdr, rthlen);
3245 case IPV6_USE_MIN_MTU:
3246 if (len != sizeof(int))
3248 minmtupolicy = *(int *)buf;
3249 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3250 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3251 minmtupolicy != IP6PO_MINMTU_ALL) {
3254 opt->ip6po_minmtu = minmtupolicy;
3258 if (len != sizeof(int))
3261 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3263 * we ignore this option for TCP sockets.
3264 * (RFC3542 leaves this case unspecified.)
3266 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3268 opt->ip6po_flags |= IP6PO_DONTFRAG;
3271 case IPV6_PREFER_TEMPADDR:
3272 if (len != sizeof(int))
3274 preftemp = *(int *)buf;
3275 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3276 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3277 preftemp != IP6PO_TEMPADDR_PREFER) {
3280 opt->ip6po_prefer_tempaddr = preftemp;
3284 return (ENOPROTOOPT);
3285 } /* end of switch */
3291 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3292 * packet to the input queue of a specified interface. Note that this
3293 * calls the output routine of the loopback "driver", but with an interface
3294 * pointer that might NOT be &loif -- easier than replicating that code here.
3297 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3300 struct ip6_hdr *ip6;
3302 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3307 * Make sure to deep-copy IPv6 header portion in case the data
3308 * is in an mbuf cluster, so that we can safely override the IPv6
3309 * header portion later.
3311 if (!M_WRITABLE(copym) ||
3312 copym->m_len < sizeof(struct ip6_hdr)) {
3313 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3317 ip6 = mtod(copym, struct ip6_hdr *);
3319 * clear embedded scope identifiers if necessary.
3320 * in6_clearscope will touch the addresses only when necessary.
3322 in6_clearscope(&ip6->ip6_src);
3323 in6_clearscope(&ip6->ip6_dst);
3324 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3325 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3327 copym->m_pkthdr.csum_data = 0xffff;
3329 if_simloop(ifp, copym, AF_INET6, 0);
3333 * Chop IPv6 header off from the payload.
3336 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3339 struct ip6_hdr *ip6;
3341 ip6 = mtod(m, struct ip6_hdr *);
3342 if (m->m_len > sizeof(*ip6)) {
3343 mh = m_gethdr(M_NOWAIT, MT_DATA);
3348 m_move_pkthdr(mh, m);
3349 M_ALIGN(mh, sizeof(*ip6));
3350 m->m_len -= sizeof(*ip6);
3351 m->m_data += sizeof(*ip6);
3354 m->m_len = sizeof(*ip6);
3355 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3357 exthdrs->ip6e_ip6 = m;
3362 * Compute IPv6 extension header length.
3365 ip6_optlen(struct inpcb *inp)
3369 if (!inp->in6p_outputopts)
3374 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3376 len += elen(inp->in6p_outputopts->ip6po_hbh);
3377 if (inp->in6p_outputopts->ip6po_rthdr)
3378 /* dest1 is valid with rthdr only */
3379 len += elen(inp->in6p_outputopts->ip6po_dest1);
3380 len += elen(inp->in6p_outputopts->ip6po_rthdr);
3381 len += elen(inp->in6p_outputopts->ip6po_dest2);