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_private.h>
96 #include <net/if_vlan_var.h>
97 #include <net/if_llatbl.h>
98 #include <net/ethernet.h>
99 #include <net/netisr.h>
100 #include <net/route.h>
101 #include <net/route/nhop.h>
102 #include <net/pfil.h>
103 #include <net/rss_config.h>
104 #include <net/vnet.h>
106 #include <netinet/in.h>
107 #include <netinet/in_var.h>
108 #include <netinet/ip_var.h>
109 #include <netinet6/in6_fib.h>
110 #include <netinet6/in6_var.h>
111 #include <netinet/ip6.h>
112 #include <netinet/icmp6.h>
113 #include <netinet6/ip6_var.h>
114 #include <netinet/in_pcb.h>
115 #include <netinet/tcp_var.h>
116 #include <netinet6/nd6.h>
117 #include <netinet6/in6_rss.h>
119 #include <netipsec/ipsec_support.h>
120 #if defined(SCTP) || defined(SCTP_SUPPORT)
121 #include <netinet/sctp.h>
122 #include <netinet/sctp_crc32.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)) {
691 /* Nexthop is valid and contains valid ifp */
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);
712 * At this point at least @ifp is not NULL
713 * Can be the case when dst is multicast, link-local or
714 * interface is explicitly specificed by the caller.
719 * If in6_selectroute() does not return a nexthop
720 * dst may not have been updated.
722 *dst = dst_sa; /* XXX */
727 origifp = nh->nh_aifp;
728 ia = (struct in6_ifaddr *)(nh->nh_ifa);
729 counter_u64_add(nh->nh_pksent, 1);
732 struct nhop_object *nh;
733 struct in6_addr kdst;
736 if (fwd_tag == NULL) {
737 bzero(&dst_sa, sizeof(dst_sa));
738 dst_sa.sin6_family = AF_INET6;
739 dst_sa.sin6_len = sizeof(dst_sa);
740 dst_sa.sin6_addr = ip6->ip6_dst;
743 if (IN6_IS_ADDR_MULTICAST(&dst_sa.sin6_addr) &&
745 (ifp = im6o->im6o_multicast_ifp) != NULL) {
746 /* We do not need a route lookup. */
747 *dst = dst_sa; /* XXX */
752 in6_splitscope(&dst_sa.sin6_addr, &kdst, &scopeid);
754 if (IN6_IS_ADDR_MC_LINKLOCAL(&dst_sa.sin6_addr) ||
755 IN6_IS_ADDR_MC_NODELOCAL(&dst_sa.sin6_addr)) {
757 ifp = in6_getlinkifnet(scopeid);
759 error = EHOSTUNREACH;
762 *dst = dst_sa; /* XXX */
768 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE,
771 IP6STAT_INC(ip6s_noroute);
772 /* No ifp in6_ifstat_inc(ifp, ifs6_out_discard); */
773 error = EHOSTUNREACH;
778 origifp = nh->nh_aifp;
779 ia = ifatoia6(nh->nh_ifa);
780 if (nh->nh_flags & NHF_GATEWAY)
781 dst->sin6_addr = nh->gw6_sa.sin6_addr;
782 else if (fwd_tag != NULL)
783 dst->sin6_addr = dst_sa.sin6_addr;
788 * At this point ifp MUST be pointing to the valid transmit ifp.
789 * origifp MUST be valid and pointing to either the same ifp or,
790 * in case of loopback output, to the interface which ip6_src
793 * fe80::1%em0 -> fe80::2%em0 -> ifp=em0, origifp=em0
794 * fe80::1%em0 -> fe80::1%em0 -> ifp=lo0, origifp=em0
795 * ::1 -> ::1 -> ifp=lo0, origifp=lo0
797 * mtu can be 0 and will be refined later.
799 KASSERT((ifp != NULL), ("output interface must not be NULL"));
800 KASSERT((origifp != NULL), ("output address interface must not be NULL"));
802 if ((flags & IPV6_FORWARDING) == 0) {
803 /* XXX: the FORWARDING flag can be set for mrouting. */
804 in6_ifstat_inc(ifp, ifs6_out_request);
807 /* Setup data structures for scope ID checks. */
809 bzero(&src_sa, sizeof(src_sa));
810 src_sa.sin6_family = AF_INET6;
811 src_sa.sin6_len = sizeof(src_sa);
812 src_sa.sin6_addr = ip6->ip6_src;
815 /* Re-initialize to be sure. */
816 bzero(&dst_sa, sizeof(dst_sa));
817 dst_sa.sin6_family = AF_INET6;
818 dst_sa.sin6_len = sizeof(dst_sa);
819 dst_sa.sin6_addr = ip6->ip6_dst;
821 /* Check for valid scope ID. */
822 if (in6_setscope(&src0, origifp, &zone) == 0 &&
823 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
824 in6_setscope(&dst0, origifp, &zone) == 0 &&
825 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
827 * The outgoing interface is in the zone of the source
828 * and destination addresses.
831 } else if ((origifp->if_flags & IFF_LOOPBACK) == 0 ||
832 sa6_recoverscope(&src_sa) != 0 ||
833 sa6_recoverscope(&dst_sa) != 0 ||
834 dst_sa.sin6_scope_id == 0 ||
835 (src_sa.sin6_scope_id != 0 &&
836 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
837 ifnet_byindex(dst_sa.sin6_scope_id) == NULL) {
839 * If the destination network interface is not a
840 * loopback interface, or the destination network
841 * address has no scope ID, or the source address has
842 * a scope ID set which is different from the
843 * destination address one, or there is no network
844 * interface representing this scope ID, the address
845 * pair is considered invalid.
847 IP6STAT_INC(ip6s_badscope);
848 in6_ifstat_inc(origifp, ifs6_out_discard);
850 error = EHOSTUNREACH; /* XXX */
853 /* All scope ID checks are successful. */
855 if (nh && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
856 if (opt && opt->ip6po_nextroute.ro_nh) {
858 * The nexthop is explicitly specified by the
859 * application. We assume the next hop is an IPv6
862 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
864 else if ((nh->nh_flags & NHF_GATEWAY))
868 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
869 m->m_flags &= ~(M_BCAST | M_MCAST); /* Just in case. */
871 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
872 in6_ifstat_inc(ifp, ifs6_out_mcast);
874 /* Confirm that the outgoing interface supports multicast. */
875 if (!(ifp->if_flags & IFF_MULTICAST)) {
876 IP6STAT_INC(ip6s_noroute);
877 in6_ifstat_inc(ifp, ifs6_out_discard);
881 if ((im6o == NULL && in6_mcast_loop) ||
882 (im6o && im6o->im6o_multicast_loop)) {
884 * Loop back multicast datagram if not expressly
885 * forbidden to do so, even if we have not joined
886 * the address; protocols will filter it later,
887 * thus deferring a hash lookup and lock acquisition
888 * at the expense of an m_copym().
890 ip6_mloopback(ifp, m);
893 * If we are acting as a multicast router, perform
894 * multicast forwarding as if the packet had just
895 * arrived on the interface to which we are about
896 * to send. The multicast forwarding function
897 * recursively calls this function, using the
898 * IPV6_FORWARDING flag to prevent infinite recursion.
900 * Multicasts that are looped back by ip6_mloopback(),
901 * above, will be forwarded by the ip6_input() routine,
904 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
906 * XXX: ip6_mforward expects that rcvif is NULL
907 * when it is called from the originating path.
908 * However, it may not always be the case.
910 m->m_pkthdr.rcvif = NULL;
911 if (ip6_mforward(ip6, ifp, m) != 0) {
918 * Multicasts with a hoplimit of zero may be looped back,
919 * above, but must not be transmitted on a network.
920 * Also, multicasts addressed to the loopback interface
921 * are not sent -- the above call to ip6_mloopback() will
922 * loop back a copy if this host actually belongs to the
923 * destination group on the loopback interface.
925 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
926 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
933 * Fill the outgoing inteface to tell the upper layer
934 * to increment per-interface statistics.
939 /* Determine path MTU. */
940 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
941 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
943 KASSERT(mtu > 0, ("%s:%d: mtu %ld, ro_pmtu %p ro %p ifp %p "
944 "alwaysfrag %d fibnum %u\n", __func__, __LINE__, mtu, ro_pmtu, ro,
945 ifp, alwaysfrag, fibnum));
948 * The caller of this function may specify to use the minimum MTU
950 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
951 * setting. The logic is a bit complicated; by default, unicast
952 * packets will follow path MTU while multicast packets will be sent at
953 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
954 * including unicast ones will be sent at the minimum MTU. Multicast
955 * packets will always be sent at the minimum MTU unless
956 * IP6PO_MINMTU_DISABLE is explicitly specified.
957 * See RFC 3542 for more details.
959 if (mtu > IPV6_MMTU) {
960 if ((flags & IPV6_MINMTU))
962 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
964 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
966 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
972 * Clear embedded scope identifiers if necessary.
973 * in6_clearscope() will touch the addresses only when necessary.
975 in6_clearscope(&ip6->ip6_src);
976 in6_clearscope(&ip6->ip6_dst);
979 * If the outgoing packet contains a hop-by-hop options header,
980 * it must be examined and processed even by the source node.
981 * (RFC 2460, section 4.)
983 if (exthdrs.ip6e_hbh) {
984 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
985 u_int32_t dummy; /* XXX unused */
986 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
989 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
990 panic("ip6e_hbh is not contiguous");
993 * XXX: if we have to send an ICMPv6 error to the sender,
994 * we need the M_LOOP flag since icmp6_error() expects
995 * the IPv6 and the hop-by-hop options header are
996 * contiguous unless the flag is set.
998 m->m_flags |= M_LOOP;
999 m->m_pkthdr.rcvif = ifp;
1000 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
1001 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
1002 &dummy, &plen) < 0) {
1003 /* m was already freed at this point. */
1004 error = EINVAL;/* better error? */
1007 m->m_flags &= ~M_LOOP; /* XXX */
1008 m->m_pkthdr.rcvif = NULL;
1011 /* Jump over all PFIL processing if hooks are not active. */
1012 if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
1015 odst = ip6->ip6_dst;
1016 /* Run through list of hooks for output packets. */
1017 switch (pfil_mbuf_out(V_inet6_pfil_head, &m, ifp, inp)) {
1019 ip6 = mtod(m, struct ip6_hdr *);
1029 /* See if destination IP address was changed by packet filter. */
1030 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
1031 m->m_flags |= M_SKIP_FIREWALL;
1032 /* If destination is now ourself drop to ip6_input(). */
1033 if (in6_localip(&ip6->ip6_dst)) {
1034 m->m_flags |= M_FASTFWD_OURS;
1035 if (m->m_pkthdr.rcvif == NULL)
1036 m->m_pkthdr.rcvif = V_loif;
1037 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1038 m->m_pkthdr.csum_flags |=
1039 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1040 m->m_pkthdr.csum_data = 0xffff;
1042 #if defined(SCTP) || defined(SCTP_SUPPORT)
1043 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1044 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1046 error = netisr_queue(NETISR_IPV6, m);
1050 RO_INVALIDATE_CACHE(ro);
1051 needfiblookup = 1; /* Redo the routing table lookup. */
1054 /* See if fib was changed by packet filter. */
1055 if (fibnum != M_GETFIB(m)) {
1056 m->m_flags |= M_SKIP_FIREWALL;
1057 fibnum = M_GETFIB(m);
1059 RO_INVALIDATE_CACHE(ro);
1065 /* See if local, if yes, send it to netisr. */
1066 if (m->m_flags & M_FASTFWD_OURS) {
1067 if (m->m_pkthdr.rcvif == NULL)
1068 m->m_pkthdr.rcvif = V_loif;
1069 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1070 m->m_pkthdr.csum_flags |=
1071 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1072 m->m_pkthdr.csum_data = 0xffff;
1074 #if defined(SCTP) || defined(SCTP_SUPPORT)
1075 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1076 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1078 error = netisr_queue(NETISR_IPV6, m);
1081 /* Or forward to some other address? */
1082 if ((m->m_flags & M_IP6_NEXTHOP) &&
1083 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
1085 dst = (struct sockaddr_in6 *)&ro->ro_dst;
1088 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
1089 m->m_flags |= M_SKIP_FIREWALL;
1090 m->m_flags &= ~M_IP6_NEXTHOP;
1091 m_tag_delete(m, fwd_tag);
1097 EVL_APPLY_PRI(m, vlan_pcp);
1099 /* Ensure the packet data is mapped if the interface requires it. */
1100 if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) {
1101 m = mb_unmapped_to_ext(m);
1103 IP6STAT_INC(ip6s_odropped);
1109 * Send the packet to the outgoing interface.
1110 * If necessary, do IPv6 fragmentation before sending.
1112 * The logic here is rather complex:
1113 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
1114 * 1-a: send as is if tlen <= path mtu
1115 * 1-b: fragment if tlen > path mtu
1117 * 2: if user asks us not to fragment (dontfrag == 1)
1118 * 2-a: send as is if tlen <= interface mtu
1119 * 2-b: error if tlen > interface mtu
1121 * 3: if we always need to attach fragment header (alwaysfrag == 1)
1124 * 4: if dontfrag == 1 && alwaysfrag == 1
1125 * error, as we cannot handle this conflicting request.
1127 sw_csum = m->m_pkthdr.csum_flags;
1129 tso = ((sw_csum & ifp->if_hwassist &
1130 (CSUM_TSO | CSUM_INNER_TSO)) != 0) ? 1 : 0;
1131 sw_csum &= ~ifp->if_hwassist;
1135 * If we added extension headers, we will not do TSO and calculate the
1136 * checksums ourselves for now.
1137 * XXX-BZ Need a framework to know when the NIC can handle it, even
1140 ip6_output_delayed_csum(m, ifp, sw_csum, plen, optlen);
1141 /* XXX-BZ m->m_pkthdr.csum_flags &= ~ifp->if_hwassist; */
1142 tlen = m->m_pkthdr.len;
1144 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
1148 if (dontfrag && alwaysfrag) { /* Case 4. */
1149 /* Conflicting request - can't transmit. */
1153 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* Case 2-b. */
1155 * Even if the DONTFRAG option is specified, we cannot send the
1156 * packet when the data length is larger than the MTU of the
1157 * outgoing interface.
1158 * Notify the error by sending IPV6_PATHMTU ancillary data if
1159 * application wanted to know the MTU value. Also return an
1160 * error code (this is not described in the API spec).
1163 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
1168 /* Transmit packet without fragmentation. */
1169 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* Cases 1-a and 2-a. */
1170 struct in6_ifaddr *ia6;
1172 ip6 = mtod(m, struct ip6_hdr *);
1173 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1175 /* Record statistics for this interface address. */
1176 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
1177 counter_u64_add(ia6->ia_ifa.ifa_obytes,
1180 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1181 (flags & IP_NO_SND_TAG_RL) ? false : true);
1185 /* Try to fragment the packet. Cases 1-b and 3. */
1186 if (mtu < IPV6_MMTU) {
1187 /* Path MTU cannot be less than IPV6_MMTU. */
1189 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1191 } else if (ip6->ip6_plen == 0) {
1192 /* Jumbo payload cannot be fragmented. */
1194 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1200 * Too large for the destination or interface;
1201 * fragment if possible.
1202 * Must be able to put at least 8 bytes per fragment.
1204 if (mtu > IPV6_MAXPACKET)
1205 mtu = IPV6_MAXPACKET;
1207 len = (mtu - unfragpartlen - sizeof(struct ip6_frag)) & ~7;
1210 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1215 * If the interface will not calculate checksums on
1216 * fragmented packets, then do it here.
1217 * XXX-BZ handle the hw offloading case. Need flags.
1219 ip6_output_delayed_csum(m, ifp, m->m_pkthdr.csum_flags, plen,
1223 * Change the next header field of the last header in the
1224 * unfragmentable part.
1226 if (exthdrs.ip6e_rthdr) {
1227 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1228 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1229 } else if (exthdrs.ip6e_dest1) {
1230 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1231 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1232 } else if (exthdrs.ip6e_hbh) {
1233 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1234 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1236 ip6 = mtod(m, struct ip6_hdr *);
1237 nextproto = ip6->ip6_nxt;
1238 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1242 * Loop through length of segment after first fragment,
1243 * make new header and copy data of each part and link onto
1247 id = htonl(ip6_randomid());
1248 error = ip6_fragment(ifp, m, unfragpartlen, nextproto,len, id);
1252 in6_ifstat_inc(ifp, ifs6_out_fragok);
1255 /* Remove leading garbage. */
1264 /* Record statistics for this interface address. */
1266 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1267 counter_u64_add(ia->ia_ifa.ifa_obytes,
1271 EVL_APPLY_PRI(m, vlan_pcp);
1272 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1279 IP6STAT_INC(ip6s_fragmented);
1285 m_freem(exthdrs.ip6e_hbh); /* m_freem() checks if mbuf is NULL. */
1286 m_freem(exthdrs.ip6e_dest1);
1287 m_freem(exthdrs.ip6e_rthdr);
1288 m_freem(exthdrs.ip6e_dest2);
1297 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1301 if (hlen > MCLBYTES)
1302 return (ENOBUFS); /* XXX */
1305 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1307 m = m_get(M_NOWAIT, MT_DATA);
1312 bcopy(hdr, mtod(m, caddr_t), hlen);
1319 * Insert jumbo payload option.
1322 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1328 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1331 * If there is no hop-by-hop options header, allocate new one.
1332 * If there is one but it doesn't have enough space to store the
1333 * jumbo payload option, allocate a cluster to store the whole options.
1334 * Otherwise, use it to store the options.
1336 if (exthdrs->ip6e_hbh == NULL) {
1337 mopt = m_get(M_NOWAIT, MT_DATA);
1340 mopt->m_len = JUMBOOPTLEN;
1341 optbuf = mtod(mopt, u_char *);
1342 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1343 exthdrs->ip6e_hbh = mopt;
1345 struct ip6_hbh *hbh;
1347 mopt = exthdrs->ip6e_hbh;
1348 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1351 * - exthdrs->ip6e_hbh is not referenced from places
1352 * other than exthdrs.
1353 * - exthdrs->ip6e_hbh is not an mbuf chain.
1355 int oldoptlen = mopt->m_len;
1359 * XXX: give up if the whole (new) hbh header does
1360 * not fit even in an mbuf cluster.
1362 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1366 * As a consequence, we must always prepare a cluster
1369 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1372 n->m_len = oldoptlen + JUMBOOPTLEN;
1373 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1375 optbuf = mtod(n, caddr_t) + oldoptlen;
1377 mopt = exthdrs->ip6e_hbh = n;
1379 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1380 mopt->m_len += JUMBOOPTLEN;
1382 optbuf[0] = IP6OPT_PADN;
1386 * Adjust the header length according to the pad and
1387 * the jumbo payload option.
1389 hbh = mtod(mopt, struct ip6_hbh *);
1390 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1393 /* fill in the option. */
1394 optbuf[2] = IP6OPT_JUMBO;
1396 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1397 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1399 /* finally, adjust the packet header length */
1400 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1407 * Insert fragment header and copy unfragmentable header portions.
1410 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1411 struct ip6_frag **frghdrp)
1413 struct mbuf *n, *mlast;
1415 if (hlen > sizeof(struct ip6_hdr)) {
1416 n = m_copym(m0, sizeof(struct ip6_hdr),
1417 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1424 /* Search for the last mbuf of unfragmentable part. */
1425 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1428 if (M_WRITABLE(mlast) &&
1429 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1430 /* use the trailing space of the last mbuf for the fragment hdr */
1431 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1433 mlast->m_len += sizeof(struct ip6_frag);
1434 m->m_pkthdr.len += sizeof(struct ip6_frag);
1436 /* allocate a new mbuf for the fragment header */
1439 mfrg = m_get(M_NOWAIT, MT_DATA);
1442 mfrg->m_len = sizeof(struct ip6_frag);
1443 *frghdrp = mtod(mfrg, struct ip6_frag *);
1444 mlast->m_next = mfrg;
1451 * Calculates IPv6 path mtu for destination @dst.
1452 * Resulting MTU is stored in @mtup.
1454 * Returns 0 on success.
1457 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1459 struct epoch_tracker et;
1460 struct nhop_object *nh;
1461 struct in6_addr kdst;
1465 in6_splitscope(dst, &kdst, &scopeid);
1467 NET_EPOCH_ENTER(et);
1468 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1470 error = ip6_calcmtu(nh->nh_ifp, dst, nh->nh_mtu, mtup, NULL, 0);
1472 error = EHOSTUNREACH;
1479 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1480 * and cached data in @ro_pmtu.
1481 * MTU from (successful) route lookup is saved (along with dst)
1482 * inside @ro_pmtu to avoid subsequent route lookups after packet
1483 * filter processing.
1485 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1486 * Returns 0 on success.
1489 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1490 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1491 int *alwaysfragp, u_int fibnum, u_int proto)
1493 struct nhop_object *nh;
1494 struct in6_addr kdst;
1496 struct sockaddr_in6 *sa6_dst, sin6;
1502 if (ro_pmtu == NULL || do_lookup) {
1504 * Here ro_pmtu has final destination address, while
1505 * ro might represent immediate destination.
1506 * Use ro_pmtu destination since mtu might differ.
1508 if (ro_pmtu != NULL) {
1509 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1510 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1511 ro_pmtu->ro_mtu = 0;
1515 if (ro_pmtu == NULL || ro_pmtu->ro_mtu == 0) {
1516 bzero(sa6_dst, sizeof(*sa6_dst));
1517 sa6_dst->sin6_family = AF_INET6;
1518 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1519 sa6_dst->sin6_addr = *dst;
1521 in6_splitscope(dst, &kdst, &scopeid);
1522 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1525 if (ro_pmtu != NULL)
1526 ro_pmtu->ro_mtu = mtu;
1529 mtu = ro_pmtu->ro_mtu;
1532 if (ro_pmtu != NULL && ro_pmtu->ro_nh != NULL)
1533 mtu = ro_pmtu->ro_nh->nh_mtu;
1535 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1539 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1540 * hostcache data for @dst.
1541 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1543 * Returns 0 on success.
1546 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1547 u_long *mtup, int *alwaysfragp, u_int proto)
1555 struct in_conninfo inc;
1557 bzero(&inc, sizeof(inc));
1558 inc.inc_flags |= INC_ISIPV6;
1559 inc.inc6_faddr = *dst;
1561 ifmtu = IN6_LINKMTU(ifp);
1563 /* TCP is known to react to pmtu changes so skip hc */
1564 if (proto != IPPROTO_TCP)
1565 mtu = tcp_hc_getmtu(&inc);
1568 mtu = min(mtu, rt_mtu);
1573 else if (mtu < IPV6_MMTU) {
1575 * RFC2460 section 5, last paragraph:
1576 * if we record ICMPv6 too big message with
1577 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1578 * or smaller, with framgent header attached.
1579 * (fragment header is needed regardless from the
1580 * packet size, for translators to identify packets)
1586 mtu = IN6_LINKMTU(ifp);
1588 error = EHOSTUNREACH; /* XXX */
1592 *alwaysfragp = alwaysfrag;
1597 * IP6 socket option processing.
1600 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1602 int optdatalen, uproto;
1604 struct inpcb *inp = sotoinpcb(so);
1606 int level, op, optname;
1610 uint32_t rss_bucket;
1615 * Don't use more than a quarter of mbuf clusters. N.B.:
1616 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1617 * on LP64 architectures, so cast to u_long to avoid undefined
1618 * behavior. ILP32 architectures cannot have nmbclusters
1619 * large enough to overflow for other reasons.
1621 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1623 level = sopt->sopt_level;
1624 op = sopt->sopt_dir;
1625 optname = sopt->sopt_name;
1626 optlen = sopt->sopt_valsize;
1630 uproto = (int)so->so_proto->pr_protocol;
1632 if (level != IPPROTO_IPV6) {
1635 if (sopt->sopt_level == SOL_SOCKET &&
1636 sopt->sopt_dir == SOPT_SET) {
1637 switch (sopt->sopt_name) {
1640 if ((so->so_options & SO_REUSEADDR) != 0)
1641 inp->inp_flags2 |= INP_REUSEADDR;
1643 inp->inp_flags2 &= ~INP_REUSEADDR;
1649 if ((so->so_options & SO_REUSEPORT) != 0)
1650 inp->inp_flags2 |= INP_REUSEPORT;
1652 inp->inp_flags2 &= ~INP_REUSEPORT;
1656 case SO_REUSEPORT_LB:
1658 if ((so->so_options & SO_REUSEPORT_LB) != 0)
1659 inp->inp_flags2 |= INP_REUSEPORT_LB;
1661 inp->inp_flags2 &= ~INP_REUSEPORT_LB;
1667 inp->inp_inc.inc_fibnum = so->so_fibnum;
1671 case SO_MAX_PACING_RATE:
1674 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1685 } else { /* level == IPPROTO_IPV6 */
1689 case IPV6_2292PKTOPTIONS:
1690 #ifdef IPV6_PKTOPTIONS
1691 case IPV6_PKTOPTIONS:
1696 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1697 printf("ip6_ctloutput: mbuf limit hit\n");
1702 error = soopt_getm(sopt, &m); /* XXX */
1705 error = soopt_mcopyin(sopt, m); /* XXX */
1709 error = ip6_pcbopts(&inp->in6p_outputopts, m,
1712 m_freem(m); /* XXX */
1717 * Use of some Hop-by-Hop options or some
1718 * Destination options, might require special
1719 * privilege. That is, normal applications
1720 * (without special privilege) might be forbidden
1721 * from setting certain options in outgoing packets,
1722 * and might never see certain options in received
1723 * packets. [RFC 2292 Section 6]
1724 * KAME specific note:
1725 * KAME prevents non-privileged users from sending or
1726 * receiving ANY hbh/dst options in order to avoid
1727 * overhead of parsing options in the kernel.
1729 case IPV6_RECVHOPOPTS:
1730 case IPV6_RECVDSTOPTS:
1731 case IPV6_RECVRTHDRDSTOPTS:
1733 error = priv_check(td,
1734 PRIV_NETINET_SETHDROPTS);
1739 case IPV6_UNICAST_HOPS:
1742 case IPV6_RECVPKTINFO:
1743 case IPV6_RECVHOPLIMIT:
1744 case IPV6_RECVRTHDR:
1745 case IPV6_RECVPATHMTU:
1746 case IPV6_RECVTCLASS:
1747 case IPV6_RECVFLOWID:
1749 case IPV6_RECVRSSBUCKETID:
1752 case IPV6_AUTOFLOWLABEL:
1753 case IPV6_ORIGDSTADDR:
1756 if (optname == IPV6_BINDANY && td != NULL) {
1757 error = priv_check(td,
1758 PRIV_NETINET_BINDANY);
1763 if (optlen != sizeof(int)) {
1767 error = sooptcopyin(sopt, &optval,
1768 sizeof optval, sizeof optval);
1772 case IPV6_UNICAST_HOPS:
1773 if (optval < -1 || optval >= 256)
1776 /* -1 = kernel default */
1777 inp->in6p_hops = optval;
1778 if ((inp->inp_vflag &
1780 inp->inp_ip_ttl = optval;
1783 #define OPTSET(bit) \
1787 inp->inp_flags |= (bit); \
1789 inp->inp_flags &= ~(bit); \
1791 } while (/*CONSTCOND*/ 0)
1792 #define OPTSET2292(bit) \
1795 inp->inp_flags |= IN6P_RFC2292; \
1797 inp->inp_flags |= (bit); \
1799 inp->inp_flags &= ~(bit); \
1801 } while (/*CONSTCOND*/ 0)
1802 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0)
1804 #define OPTSET2_N(bit, val) do { \
1806 inp->inp_flags2 |= bit; \
1808 inp->inp_flags2 &= ~bit; \
1810 #define OPTSET2(bit, val) do { \
1812 OPTSET2_N(bit, val); \
1815 #define OPTBIT2(bit) (inp->inp_flags2 & (bit) ? 1 : 0)
1816 #define OPTSET2292_EXCLUSIVE(bit) \
1819 if (OPTBIT(IN6P_RFC2292)) { \
1823 inp->inp_flags |= (bit); \
1825 inp->inp_flags &= ~(bit); \
1828 } while (/*CONSTCOND*/ 0)
1830 case IPV6_RECVPKTINFO:
1831 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1836 struct ip6_pktopts **optp;
1838 /* cannot mix with RFC2292 */
1839 if (OPTBIT(IN6P_RFC2292)) {
1844 if (inp->inp_flags & INP_DROPPED) {
1846 return (ECONNRESET);
1848 optp = &inp->in6p_outputopts;
1849 error = ip6_pcbopt(IPV6_HOPLIMIT,
1850 (u_char *)&optval, sizeof(optval),
1851 optp, (td != NULL) ? td->td_ucred :
1857 case IPV6_RECVHOPLIMIT:
1858 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1861 case IPV6_RECVHOPOPTS:
1862 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1865 case IPV6_RECVDSTOPTS:
1866 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1869 case IPV6_RECVRTHDRDSTOPTS:
1870 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1873 case IPV6_RECVRTHDR:
1874 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1877 case IPV6_RECVPATHMTU:
1879 * We ignore this option for TCP
1881 * (RFC3542 leaves this case
1884 if (uproto != IPPROTO_TCP)
1888 case IPV6_RECVFLOWID:
1889 OPTSET2(INP_RECVFLOWID, optval);
1893 case IPV6_RECVRSSBUCKETID:
1894 OPTSET2(INP_RECVRSSBUCKETID, optval);
1900 if (inp->inp_lport ||
1901 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1903 * The socket is already bound.
1910 inp->inp_flags |= IN6P_IPV6_V6ONLY;
1911 inp->inp_vflag &= ~INP_IPV4;
1913 inp->inp_flags &= ~IN6P_IPV6_V6ONLY;
1914 inp->inp_vflag |= INP_IPV4;
1918 case IPV6_RECVTCLASS:
1919 /* cannot mix with RFC2292 XXX */
1920 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1922 case IPV6_AUTOFLOWLABEL:
1923 OPTSET(IN6P_AUTOFLOWLABEL);
1926 case IPV6_ORIGDSTADDR:
1927 OPTSET2(INP_ORIGDSTADDR, optval);
1930 OPTSET(INP_BINDANY);
1933 if ((optval >= -1) && (optval <=
1934 (INP_2PCP_MASK >> INP_2PCP_SHIFT))) {
1960 case IPV6_USE_MIN_MTU:
1961 case IPV6_PREFER_TEMPADDR:
1962 if (optlen != sizeof(optval)) {
1966 error = sooptcopyin(sopt, &optval,
1967 sizeof optval, sizeof optval);
1971 struct ip6_pktopts **optp;
1973 if (inp->inp_flags & INP_DROPPED) {
1975 return (ECONNRESET);
1977 optp = &inp->in6p_outputopts;
1978 error = ip6_pcbopt(optname,
1979 (u_char *)&optval, sizeof(optval),
1980 optp, (td != NULL) ? td->td_ucred :
1986 case IPV6_2292PKTINFO:
1987 case IPV6_2292HOPLIMIT:
1988 case IPV6_2292HOPOPTS:
1989 case IPV6_2292DSTOPTS:
1990 case IPV6_2292RTHDR:
1992 if (optlen != sizeof(int)) {
1996 error = sooptcopyin(sopt, &optval,
1997 sizeof optval, sizeof optval);
2001 case IPV6_2292PKTINFO:
2002 OPTSET2292(IN6P_PKTINFO);
2004 case IPV6_2292HOPLIMIT:
2005 OPTSET2292(IN6P_HOPLIMIT);
2007 case IPV6_2292HOPOPTS:
2009 * Check super-user privilege.
2010 * See comments for IPV6_RECVHOPOPTS.
2013 error = priv_check(td,
2014 PRIV_NETINET_SETHDROPTS);
2018 OPTSET2292(IN6P_HOPOPTS);
2020 case IPV6_2292DSTOPTS:
2022 error = priv_check(td,
2023 PRIV_NETINET_SETHDROPTS);
2027 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
2029 case IPV6_2292RTHDR:
2030 OPTSET2292(IN6P_RTHDR);
2038 case IPV6_RTHDRDSTOPTS:
2041 /* new advanced API (RFC3542) */
2043 u_char optbuf_storage[MCLBYTES];
2045 struct ip6_pktopts **optp;
2047 /* cannot mix with RFC2292 */
2048 if (OPTBIT(IN6P_RFC2292)) {
2054 * We only ensure valsize is not too large
2055 * here. Further validation will be done
2058 error = sooptcopyin(sopt, optbuf_storage,
2059 sizeof(optbuf_storage), 0);
2062 optlen = sopt->sopt_valsize;
2063 optbuf = optbuf_storage;
2065 if (inp->inp_flags & INP_DROPPED) {
2067 return (ECONNRESET);
2069 optp = &inp->in6p_outputopts;
2070 error = ip6_pcbopt(optname, optbuf, optlen,
2071 optp, (td != NULL) ? td->td_ucred : NULL,
2078 case IPV6_MULTICAST_IF:
2079 case IPV6_MULTICAST_HOPS:
2080 case IPV6_MULTICAST_LOOP:
2081 case IPV6_JOIN_GROUP:
2082 case IPV6_LEAVE_GROUP:
2084 case MCAST_BLOCK_SOURCE:
2085 case MCAST_UNBLOCK_SOURCE:
2086 case MCAST_JOIN_GROUP:
2087 case MCAST_LEAVE_GROUP:
2088 case MCAST_JOIN_SOURCE_GROUP:
2089 case MCAST_LEAVE_SOURCE_GROUP:
2090 error = ip6_setmoptions(inp, sopt);
2093 case IPV6_PORTRANGE:
2094 error = sooptcopyin(sopt, &optval,
2095 sizeof optval, sizeof optval);
2101 case IPV6_PORTRANGE_DEFAULT:
2102 inp->inp_flags &= ~(INP_LOWPORT);
2103 inp->inp_flags &= ~(INP_HIGHPORT);
2106 case IPV6_PORTRANGE_HIGH:
2107 inp->inp_flags &= ~(INP_LOWPORT);
2108 inp->inp_flags |= INP_HIGHPORT;
2111 case IPV6_PORTRANGE_LOW:
2112 inp->inp_flags &= ~(INP_HIGHPORT);
2113 inp->inp_flags |= INP_LOWPORT;
2123 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2124 case IPV6_IPSEC_POLICY:
2125 if (IPSEC_ENABLED(ipv6)) {
2126 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2133 error = ENOPROTOOPT;
2140 case IPV6_2292PKTOPTIONS:
2141 #ifdef IPV6_PKTOPTIONS
2142 case IPV6_PKTOPTIONS:
2145 * RFC3542 (effectively) deprecated the
2146 * semantics of the 2292-style pktoptions.
2147 * Since it was not reliable in nature (i.e.,
2148 * applications had to expect the lack of some
2149 * information after all), it would make sense
2150 * to simplify this part by always returning
2153 sopt->sopt_valsize = 0;
2156 case IPV6_RECVHOPOPTS:
2157 case IPV6_RECVDSTOPTS:
2158 case IPV6_RECVRTHDRDSTOPTS:
2159 case IPV6_UNICAST_HOPS:
2160 case IPV6_RECVPKTINFO:
2161 case IPV6_RECVHOPLIMIT:
2162 case IPV6_RECVRTHDR:
2163 case IPV6_RECVPATHMTU:
2166 case IPV6_PORTRANGE:
2167 case IPV6_RECVTCLASS:
2168 case IPV6_AUTOFLOWLABEL:
2172 case IPV6_RECVFLOWID:
2174 case IPV6_RSSBUCKETID:
2175 case IPV6_RECVRSSBUCKETID:
2179 case IPV6_RECVHOPOPTS:
2180 optval = OPTBIT(IN6P_HOPOPTS);
2183 case IPV6_RECVDSTOPTS:
2184 optval = OPTBIT(IN6P_DSTOPTS);
2187 case IPV6_RECVRTHDRDSTOPTS:
2188 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2191 case IPV6_UNICAST_HOPS:
2192 optval = inp->in6p_hops;
2195 case IPV6_RECVPKTINFO:
2196 optval = OPTBIT(IN6P_PKTINFO);
2199 case IPV6_RECVHOPLIMIT:
2200 optval = OPTBIT(IN6P_HOPLIMIT);
2203 case IPV6_RECVRTHDR:
2204 optval = OPTBIT(IN6P_RTHDR);
2207 case IPV6_RECVPATHMTU:
2208 optval = OPTBIT(IN6P_MTU);
2212 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2215 case IPV6_PORTRANGE:
2218 flags = inp->inp_flags;
2219 if (flags & INP_HIGHPORT)
2220 optval = IPV6_PORTRANGE_HIGH;
2221 else if (flags & INP_LOWPORT)
2222 optval = IPV6_PORTRANGE_LOW;
2227 case IPV6_RECVTCLASS:
2228 optval = OPTBIT(IN6P_TCLASS);
2231 case IPV6_AUTOFLOWLABEL:
2232 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2235 case IPV6_ORIGDSTADDR:
2236 optval = OPTBIT2(INP_ORIGDSTADDR);
2240 optval = OPTBIT(INP_BINDANY);
2244 optval = inp->inp_flowid;
2248 optval = inp->inp_flowtype;
2251 case IPV6_RECVFLOWID:
2252 optval = OPTBIT2(INP_RECVFLOWID);
2255 case IPV6_RSSBUCKETID:
2257 rss_hash2bucket(inp->inp_flowid,
2261 optval = rss_bucket;
2266 case IPV6_RECVRSSBUCKETID:
2267 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2273 if (OPTBIT2(INP_2PCP_SET)) {
2274 optval = (inp->inp_flags2 &
2285 error = sooptcopyout(sopt, &optval,
2292 struct ip6_mtuinfo mtuinfo;
2293 struct in6_addr addr;
2295 if (!(so->so_state & SS_ISCONNECTED))
2298 * XXX: we dot not consider the case of source
2299 * routing, or optional information to specify
2300 * the outgoing interface.
2301 * Copy faddr out of inp to avoid holding lock
2302 * on inp during route lookup.
2305 bcopy(&inp->in6p_faddr, &addr, sizeof(addr));
2307 error = ip6_getpmtu_ctl(so->so_fibnum,
2311 if (pmtu > IPV6_MAXPACKET)
2312 pmtu = IPV6_MAXPACKET;
2314 bzero(&mtuinfo, sizeof(mtuinfo));
2315 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2316 optdata = (void *)&mtuinfo;
2317 optdatalen = sizeof(mtuinfo);
2318 error = sooptcopyout(sopt, optdata,
2323 case IPV6_2292PKTINFO:
2324 case IPV6_2292HOPLIMIT:
2325 case IPV6_2292HOPOPTS:
2326 case IPV6_2292RTHDR:
2327 case IPV6_2292DSTOPTS:
2329 case IPV6_2292PKTINFO:
2330 optval = OPTBIT(IN6P_PKTINFO);
2332 case IPV6_2292HOPLIMIT:
2333 optval = OPTBIT(IN6P_HOPLIMIT);
2335 case IPV6_2292HOPOPTS:
2336 optval = OPTBIT(IN6P_HOPOPTS);
2338 case IPV6_2292RTHDR:
2339 optval = OPTBIT(IN6P_RTHDR);
2341 case IPV6_2292DSTOPTS:
2342 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2345 error = sooptcopyout(sopt, &optval,
2352 case IPV6_RTHDRDSTOPTS:
2356 case IPV6_USE_MIN_MTU:
2357 case IPV6_PREFER_TEMPADDR:
2358 error = ip6_getpcbopt(inp, optname, sopt);
2361 case IPV6_MULTICAST_IF:
2362 case IPV6_MULTICAST_HOPS:
2363 case IPV6_MULTICAST_LOOP:
2365 error = ip6_getmoptions(inp, sopt);
2368 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2369 case IPV6_IPSEC_POLICY:
2370 if (IPSEC_ENABLED(ipv6)) {
2371 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2377 error = ENOPROTOOPT;
2387 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2389 int error = 0, optval, optlen;
2390 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2391 struct inpcb *inp = sotoinpcb(so);
2392 int level, op, optname;
2394 level = sopt->sopt_level;
2395 op = sopt->sopt_dir;
2396 optname = sopt->sopt_name;
2397 optlen = sopt->sopt_valsize;
2399 if (level != IPPROTO_IPV6) {
2406 * For ICMPv6 sockets, no modification allowed for checksum
2407 * offset, permit "no change" values to help existing apps.
2409 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2410 * for an ICMPv6 socket will fail."
2411 * The current behavior does not meet RFC3542.
2415 if (optlen != sizeof(int)) {
2419 error = sooptcopyin(sopt, &optval, sizeof(optval),
2423 if (optval < -1 || (optval % 2) != 0) {
2425 * The API assumes non-negative even offset
2426 * values or -1 as a special value.
2429 } else if (inp->inp_ip_p == IPPROTO_ICMPV6) {
2430 if (optval != icmp6off)
2433 inp->in6p_cksum = optval;
2437 if (inp->inp_ip_p == IPPROTO_ICMPV6)
2440 optval = inp->in6p_cksum;
2442 error = sooptcopyout(sopt, &optval, sizeof(optval));
2452 error = ENOPROTOOPT;
2460 * Set up IP6 options in pcb for insertion in output packets or
2461 * specifying behavior of outgoing packets.
2464 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2465 struct socket *so, struct sockopt *sopt)
2467 struct ip6_pktopts *opt = *pktopt;
2469 struct thread *td = sopt->sopt_td;
2470 struct epoch_tracker et;
2472 /* turn off any old options. */
2475 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2476 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2477 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2478 printf("ip6_pcbopts: all specified options are cleared.\n");
2480 ip6_clearpktopts(opt, -1);
2482 opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT);
2488 if (!m || m->m_len == 0) {
2490 * Only turning off any previous options, regardless of
2491 * whether the opt is just created or given.
2493 free(opt, M_IP6OPT);
2497 /* set options specified by user. */
2498 NET_EPOCH_ENTER(et);
2499 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2500 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2501 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2502 free(opt, M_IP6OPT);
2512 * initialize ip6_pktopts. beware that there are non-zero default values in
2516 ip6_initpktopts(struct ip6_pktopts *opt)
2519 bzero(opt, sizeof(*opt));
2520 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2521 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2522 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2523 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2527 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2528 struct ucred *cred, int uproto)
2530 struct epoch_tracker et;
2531 struct ip6_pktopts *opt;
2534 if (*pktopt == NULL) {
2535 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2537 if (*pktopt == NULL)
2539 ip6_initpktopts(*pktopt);
2543 NET_EPOCH_ENTER(et);
2544 ret = ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto);
2550 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2551 if (pktopt && pktopt->field) { \
2553 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2554 malloc_optdata = true; \
2556 if (inp->inp_flags & INP_DROPPED) { \
2558 free(optdata, M_TEMP); \
2559 return (ECONNRESET); \
2561 pktopt = inp->in6p_outputopts; \
2562 if (pktopt && pktopt->field) { \
2563 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2564 bcopy(pktopt->field, optdata, optdatalen); \
2566 free(optdata, M_TEMP); \
2568 malloc_optdata = false; \
2573 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2574 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2576 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2577 pktopt->field->sa_len)
2580 ip6_getpcbopt(struct inpcb *inp, int optname, struct sockopt *sopt)
2582 void *optdata = NULL;
2583 bool malloc_optdata = false;
2586 struct in6_pktinfo null_pktinfo;
2587 int deftclass = 0, on;
2588 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2589 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2590 struct ip6_pktopts *pktopt;
2593 pktopt = inp->in6p_outputopts;
2597 optdata = (void *)&null_pktinfo;
2598 if (pktopt && pktopt->ip6po_pktinfo) {
2599 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2600 sizeof(null_pktinfo));
2601 in6_clearscope(&null_pktinfo.ipi6_addr);
2603 /* XXX: we don't have to do this every time... */
2604 bzero(&null_pktinfo, sizeof(null_pktinfo));
2606 optdatalen = sizeof(struct in6_pktinfo);
2609 if (pktopt && pktopt->ip6po_tclass >= 0)
2610 deftclass = pktopt->ip6po_tclass;
2611 optdata = (void *)&deftclass;
2612 optdatalen = sizeof(int);
2615 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2618 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2620 case IPV6_RTHDRDSTOPTS:
2621 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2624 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2627 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2629 case IPV6_USE_MIN_MTU:
2631 defminmtu = pktopt->ip6po_minmtu;
2632 optdata = (void *)&defminmtu;
2633 optdatalen = sizeof(int);
2636 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2640 optdata = (void *)&on;
2641 optdatalen = sizeof(on);
2643 case IPV6_PREFER_TEMPADDR:
2645 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2646 optdata = (void *)&defpreftemp;
2647 optdatalen = sizeof(int);
2649 default: /* should not happen */
2651 panic("ip6_getpcbopt: unexpected option\n");
2654 return (ENOPROTOOPT);
2658 error = sooptcopyout(sopt, optdata, optdatalen);
2660 free(optdata, M_TEMP);
2666 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2671 if (optname == -1 || optname == IPV6_PKTINFO) {
2672 if (pktopt->ip6po_pktinfo)
2673 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2674 pktopt->ip6po_pktinfo = NULL;
2676 if (optname == -1 || optname == IPV6_HOPLIMIT)
2677 pktopt->ip6po_hlim = -1;
2678 if (optname == -1 || optname == IPV6_TCLASS)
2679 pktopt->ip6po_tclass = -1;
2680 if (optname == -1 || optname == IPV6_NEXTHOP) {
2681 if (pktopt->ip6po_nextroute.ro_nh) {
2682 NH_FREE(pktopt->ip6po_nextroute.ro_nh);
2683 pktopt->ip6po_nextroute.ro_nh = NULL;
2685 if (pktopt->ip6po_nexthop)
2686 free(pktopt->ip6po_nexthop, M_IP6OPT);
2687 pktopt->ip6po_nexthop = NULL;
2689 if (optname == -1 || optname == IPV6_HOPOPTS) {
2690 if (pktopt->ip6po_hbh)
2691 free(pktopt->ip6po_hbh, M_IP6OPT);
2692 pktopt->ip6po_hbh = NULL;
2694 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2695 if (pktopt->ip6po_dest1)
2696 free(pktopt->ip6po_dest1, M_IP6OPT);
2697 pktopt->ip6po_dest1 = NULL;
2699 if (optname == -1 || optname == IPV6_RTHDR) {
2700 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2701 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2702 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2703 if (pktopt->ip6po_route.ro_nh) {
2704 NH_FREE(pktopt->ip6po_route.ro_nh);
2705 pktopt->ip6po_route.ro_nh = NULL;
2708 if (optname == -1 || optname == IPV6_DSTOPTS) {
2709 if (pktopt->ip6po_dest2)
2710 free(pktopt->ip6po_dest2, M_IP6OPT);
2711 pktopt->ip6po_dest2 = NULL;
2715 #define PKTOPT_EXTHDRCPY(type) \
2718 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2719 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2720 if (dst->type == NULL)\
2722 bcopy(src->type, dst->type, hlen);\
2724 } while (/*CONSTCOND*/ 0)
2727 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2729 if (dst == NULL || src == NULL) {
2730 printf("ip6_clearpktopts: invalid argument\n");
2734 dst->ip6po_hlim = src->ip6po_hlim;
2735 dst->ip6po_tclass = src->ip6po_tclass;
2736 dst->ip6po_flags = src->ip6po_flags;
2737 dst->ip6po_minmtu = src->ip6po_minmtu;
2738 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2739 if (src->ip6po_pktinfo) {
2740 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2742 if (dst->ip6po_pktinfo == NULL)
2744 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2746 if (src->ip6po_nexthop) {
2747 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2749 if (dst->ip6po_nexthop == NULL)
2751 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2752 src->ip6po_nexthop->sa_len);
2754 PKTOPT_EXTHDRCPY(ip6po_hbh);
2755 PKTOPT_EXTHDRCPY(ip6po_dest1);
2756 PKTOPT_EXTHDRCPY(ip6po_dest2);
2757 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2761 ip6_clearpktopts(dst, -1);
2764 #undef PKTOPT_EXTHDRCPY
2766 struct ip6_pktopts *
2767 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2770 struct ip6_pktopts *dst;
2772 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2775 ip6_initpktopts(dst);
2777 if ((error = copypktopts(dst, src, canwait)) != 0) {
2778 free(dst, M_IP6OPT);
2786 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2791 ip6_clearpktopts(pktopt, -1);
2793 free(pktopt, M_IP6OPT);
2797 * Set IPv6 outgoing packet options based on advanced API.
2800 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2801 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2803 struct cmsghdr *cm = NULL;
2805 if (control == NULL || opt == NULL)
2809 * ip6_setpktopt can call ifnet_byindex(), so it's imperative that we
2810 * are in the network epoch here.
2814 ip6_initpktopts(opt);
2819 * If stickyopt is provided, make a local copy of the options
2820 * for this particular packet, then override them by ancillary
2822 * XXX: copypktopts() does not copy the cached route to a next
2823 * hop (if any). This is not very good in terms of efficiency,
2824 * but we can allow this since this option should be rarely
2827 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2832 * XXX: Currently, we assume all the optional information is stored
2835 if (control->m_next)
2838 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2839 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2842 if (control->m_len < CMSG_LEN(0))
2845 cm = mtod(control, struct cmsghdr *);
2846 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2848 if (cm->cmsg_level != IPPROTO_IPV6)
2851 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2852 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2861 * Set a particular packet option, as a sticky option or an ancillary data
2862 * item. "len" can be 0 only when it's a sticky option.
2863 * We have 4 cases of combination of "sticky" and "cmsg":
2864 * "sticky=0, cmsg=0": impossible
2865 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2866 * "sticky=1, cmsg=0": RFC3542 socket option
2867 * "sticky=1, cmsg=1": RFC2292 socket option
2870 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2871 struct ucred *cred, int sticky, int cmsg, int uproto)
2873 int minmtupolicy, preftemp;
2878 if (!sticky && !cmsg) {
2880 printf("ip6_setpktopt: impossible case\n");
2886 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2887 * not be specified in the context of RFC3542. Conversely,
2888 * RFC3542 types should not be specified in the context of RFC2292.
2892 case IPV6_2292PKTINFO:
2893 case IPV6_2292HOPLIMIT:
2894 case IPV6_2292NEXTHOP:
2895 case IPV6_2292HOPOPTS:
2896 case IPV6_2292DSTOPTS:
2897 case IPV6_2292RTHDR:
2898 case IPV6_2292PKTOPTIONS:
2899 return (ENOPROTOOPT);
2902 if (sticky && cmsg) {
2909 case IPV6_RTHDRDSTOPTS:
2911 case IPV6_USE_MIN_MTU:
2914 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2915 return (ENOPROTOOPT);
2920 case IPV6_2292PKTINFO:
2923 struct ifnet *ifp = NULL;
2924 struct in6_pktinfo *pktinfo;
2926 if (len != sizeof(struct in6_pktinfo))
2929 pktinfo = (struct in6_pktinfo *)buf;
2932 * An application can clear any sticky IPV6_PKTINFO option by
2933 * doing a "regular" setsockopt with ipi6_addr being
2934 * in6addr_any and ipi6_ifindex being zero.
2935 * [RFC 3542, Section 6]
2937 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2938 pktinfo->ipi6_ifindex == 0 &&
2939 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2940 ip6_clearpktopts(opt, optname);
2944 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2945 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2948 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2950 /* validate the interface index if specified. */
2951 if (pktinfo->ipi6_ifindex) {
2952 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2956 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2957 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2961 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2962 struct in6_ifaddr *ia;
2964 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2965 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2967 return (EADDRNOTAVAIL);
2968 ifa_free(&ia->ia_ifa);
2971 * We store the address anyway, and let in6_selectsrc()
2972 * validate the specified address. This is because ipi6_addr
2973 * may not have enough information about its scope zone, and
2974 * we may need additional information (such as outgoing
2975 * interface or the scope zone of a destination address) to
2976 * disambiguate the scope.
2977 * XXX: the delay of the validation may confuse the
2978 * application when it is used as a sticky option.
2980 if (opt->ip6po_pktinfo == NULL) {
2981 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2982 M_IP6OPT, M_NOWAIT);
2983 if (opt->ip6po_pktinfo == NULL)
2986 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2990 case IPV6_2292HOPLIMIT:
2996 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2997 * to simplify the ordering among hoplimit options.
2999 if (optname == IPV6_HOPLIMIT && sticky)
3000 return (ENOPROTOOPT);
3002 if (len != sizeof(int))
3005 if (*hlimp < -1 || *hlimp > 255)
3008 opt->ip6po_hlim = *hlimp;
3016 if (len != sizeof(int))
3018 tclass = *(int *)buf;
3019 if (tclass < -1 || tclass > 255)
3022 opt->ip6po_tclass = tclass;
3026 case IPV6_2292NEXTHOP:
3029 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3034 if (len == 0) { /* just remove the option */
3035 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3039 /* check if cmsg_len is large enough for sa_len */
3040 if (len < sizeof(struct sockaddr) || len < *buf)
3043 switch (((struct sockaddr *)buf)->sa_family) {
3046 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3049 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3052 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3053 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3056 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
3062 case AF_LINK: /* should eventually be supported */
3064 return (EAFNOSUPPORT);
3067 /* turn off the previous option, then set the new option. */
3068 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3069 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3070 if (opt->ip6po_nexthop == NULL)
3072 bcopy(buf, opt->ip6po_nexthop, *buf);
3075 case IPV6_2292HOPOPTS:
3078 struct ip6_hbh *hbh;
3082 * XXX: We don't allow a non-privileged user to set ANY HbH
3083 * options, since per-option restriction has too much
3087 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3093 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3094 break; /* just remove the option */
3097 /* message length validation */
3098 if (len < sizeof(struct ip6_hbh))
3100 hbh = (struct ip6_hbh *)buf;
3101 hbhlen = (hbh->ip6h_len + 1) << 3;
3105 /* turn off the previous option, then set the new option. */
3106 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3107 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3108 if (opt->ip6po_hbh == NULL)
3110 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3115 case IPV6_2292DSTOPTS:
3117 case IPV6_RTHDRDSTOPTS:
3119 struct ip6_dest *dest, **newdest = NULL;
3122 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
3123 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3129 ip6_clearpktopts(opt, optname);
3130 break; /* just remove the option */
3133 /* message length validation */
3134 if (len < sizeof(struct ip6_dest))
3136 dest = (struct ip6_dest *)buf;
3137 destlen = (dest->ip6d_len + 1) << 3;
3142 * Determine the position that the destination options header
3143 * should be inserted; before or after the routing header.
3146 case IPV6_2292DSTOPTS:
3148 * The old advacned API is ambiguous on this point.
3149 * Our approach is to determine the position based
3150 * according to the existence of a routing header.
3151 * Note, however, that this depends on the order of the
3152 * extension headers in the ancillary data; the 1st
3153 * part of the destination options header must appear
3154 * before the routing header in the ancillary data,
3156 * RFC3542 solved the ambiguity by introducing
3157 * separate ancillary data or option types.
3159 if (opt->ip6po_rthdr == NULL)
3160 newdest = &opt->ip6po_dest1;
3162 newdest = &opt->ip6po_dest2;
3164 case IPV6_RTHDRDSTOPTS:
3165 newdest = &opt->ip6po_dest1;
3168 newdest = &opt->ip6po_dest2;
3172 /* turn off the previous option, then set the new option. */
3173 ip6_clearpktopts(opt, optname);
3174 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3175 if (*newdest == NULL)
3177 bcopy(dest, *newdest, destlen);
3182 case IPV6_2292RTHDR:
3185 struct ip6_rthdr *rth;
3189 ip6_clearpktopts(opt, IPV6_RTHDR);
3190 break; /* just remove the option */
3193 /* message length validation */
3194 if (len < sizeof(struct ip6_rthdr))
3196 rth = (struct ip6_rthdr *)buf;
3197 rthlen = (rth->ip6r_len + 1) << 3;
3201 switch (rth->ip6r_type) {
3202 case IPV6_RTHDR_TYPE_0:
3203 if (rth->ip6r_len == 0) /* must contain one addr */
3205 if (rth->ip6r_len % 2) /* length must be even */
3207 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3211 return (EINVAL); /* not supported */
3214 /* turn off the previous option */
3215 ip6_clearpktopts(opt, IPV6_RTHDR);
3216 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3217 if (opt->ip6po_rthdr == NULL)
3219 bcopy(rth, opt->ip6po_rthdr, rthlen);
3224 case IPV6_USE_MIN_MTU:
3225 if (len != sizeof(int))
3227 minmtupolicy = *(int *)buf;
3228 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3229 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3230 minmtupolicy != IP6PO_MINMTU_ALL) {
3233 opt->ip6po_minmtu = minmtupolicy;
3237 if (len != sizeof(int))
3240 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3242 * we ignore this option for TCP sockets.
3243 * (RFC3542 leaves this case unspecified.)
3245 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3247 opt->ip6po_flags |= IP6PO_DONTFRAG;
3250 case IPV6_PREFER_TEMPADDR:
3251 if (len != sizeof(int))
3253 preftemp = *(int *)buf;
3254 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3255 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3256 preftemp != IP6PO_TEMPADDR_PREFER) {
3259 opt->ip6po_prefer_tempaddr = preftemp;
3263 return (ENOPROTOOPT);
3264 } /* end of switch */
3270 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3271 * packet to the input queue of a specified interface. Note that this
3272 * calls the output routine of the loopback "driver", but with an interface
3273 * pointer that might NOT be &loif -- easier than replicating that code here.
3276 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3279 struct ip6_hdr *ip6;
3281 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3286 * Make sure to deep-copy IPv6 header portion in case the data
3287 * is in an mbuf cluster, so that we can safely override the IPv6
3288 * header portion later.
3290 if (!M_WRITABLE(copym) ||
3291 copym->m_len < sizeof(struct ip6_hdr)) {
3292 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3296 ip6 = mtod(copym, struct ip6_hdr *);
3298 * clear embedded scope identifiers if necessary.
3299 * in6_clearscope will touch the addresses only when necessary.
3301 in6_clearscope(&ip6->ip6_src);
3302 in6_clearscope(&ip6->ip6_dst);
3303 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3304 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3306 copym->m_pkthdr.csum_data = 0xffff;
3308 if_simloop(ifp, copym, AF_INET6, 0);
3312 * Chop IPv6 header off from the payload.
3315 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3318 struct ip6_hdr *ip6;
3320 ip6 = mtod(m, struct ip6_hdr *);
3321 if (m->m_len > sizeof(*ip6)) {
3322 mh = m_gethdr(M_NOWAIT, MT_DATA);
3327 m_move_pkthdr(mh, m);
3328 M_ALIGN(mh, sizeof(*ip6));
3329 m->m_len -= sizeof(*ip6);
3330 m->m_data += sizeof(*ip6);
3333 m->m_len = sizeof(*ip6);
3334 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3336 exthdrs->ip6e_ip6 = m;
3341 * Compute IPv6 extension header length.
3344 ip6_optlen(struct inpcb *inp)
3348 if (!inp->in6p_outputopts)
3353 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3355 len += elen(inp->in6p_outputopts->ip6po_hbh);
3356 if (inp->in6p_outputopts->ip6po_rthdr)
3357 /* dest1 is valid with rthdr only */
3358 len += elen(inp->in6p_outputopts->ip6po_dest1);
3359 len += elen(inp->in6p_outputopts->ip6po_rthdr);
3360 len += elen(inp->in6p_outputopts->ip6po_dest2);