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 m = mb_unmapped_to_ext(m);
466 IP6STAT_INC(ip6s_odropped);
470 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
471 if (error == EINPROGRESS)
478 /* Source address validation. */
479 ip6 = mtod(m, struct ip6_hdr *);
480 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
481 (flags & IPV6_UNSPECSRC) == 0) {
483 IP6STAT_INC(ip6s_badscope);
486 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
488 IP6STAT_INC(ip6s_badscope);
493 * If we are given packet options to add extension headers prepare them.
494 * Calculate the total length of the extension header chain.
495 * Keep the length of the unfragmentable part for fragmentation.
497 bzero(&exthdrs, sizeof(exthdrs));
499 unfragpartlen = sizeof(struct ip6_hdr);
501 /* Hop-by-Hop options header. */
502 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh, optlen);
504 /* Destination options header (1st part). */
505 if (opt->ip6po_rthdr) {
506 #ifndef RTHDR_SUPPORT_IMPLEMENTED
508 * If there is a routing header, discard the packet
509 * right away here. RH0/1 are obsolete and we do not
510 * currently support RH2/3/4.
511 * People trying to use RH253/254 may want to disable
513 * The moment we do support any routing header (again)
514 * this block should check the routing type more
522 * Destination options header (1st part).
523 * This only makes sense with a routing header.
524 * See Section 9.2 of RFC 3542.
525 * Disabling this part just for MIP6 convenience is
526 * a bad idea. We need to think carefully about a
527 * way to make the advanced API coexist with MIP6
528 * options, which might automatically be inserted in
531 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1,
534 /* Routing header. */
535 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr, optlen);
537 unfragpartlen += optlen;
540 * NOTE: we don't add AH/ESP length here (done in
541 * ip6_ipsec_output()).
544 /* Destination options header (2nd part). */
545 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2, optlen);
549 * If there is at least one extension header,
550 * separate IP6 header from the payload.
554 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
558 m = exthdrs.ip6e_ip6;
559 ip6 = mtod(m, struct ip6_hdr *);
563 /* Adjust mbuf packet header length. */
564 m->m_pkthdr.len += optlen;
565 plen = m->m_pkthdr.len - sizeof(*ip6);
567 /* If this is a jumbo payload, insert a jumbo payload option. */
568 if (plen > IPV6_MAXPACKET) {
570 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
574 m = exthdrs.ip6e_ip6;
575 ip6 = mtod(m, struct ip6_hdr *);
578 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
582 ip6->ip6_plen = htons(plen);
583 nexthdrp = &ip6->ip6_nxt;
587 * Concatenate headers and fill in next header fields.
588 * Here we have, on "m"
590 * and we insert headers accordingly.
591 * Finally, we should be getting:
592 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload].
594 * During the header composing process "m" points to IPv6
595 * header. "mprev" points to an extension header prior to esp.
600 * We treat dest2 specially. This makes IPsec processing
601 * much easier. The goal here is to make mprev point the
602 * mbuf prior to dest2.
604 * Result: IPv6 dest2 payload.
605 * m and mprev will point to IPv6 header.
607 if (exthdrs.ip6e_dest2) {
609 panic("%s:%d: assumption failed: "
610 "hdr not split: hdrsplit %d exthdrs %p",
611 __func__, __LINE__, hdrsplit, &exthdrs);
612 exthdrs.ip6e_dest2->m_next = m->m_next;
613 m->m_next = exthdrs.ip6e_dest2;
614 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
615 ip6->ip6_nxt = IPPROTO_DSTOPTS;
619 * Result: IPv6 hbh dest1 rthdr dest2 payload.
620 * m will point to IPv6 header. mprev will point to the
621 * extension header prior to dest2 (rthdr in the above case).
623 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
624 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
626 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
630 IP6STAT_INC(ip6s_localout);
634 if (opt && opt->ip6po_rthdr)
635 ro = &opt->ip6po_route;
637 dst = (struct sockaddr_in6 *)&ro->ro_dst;
640 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
644 * If specified, try to fill in the traffic class field.
645 * Do not override if a non-zero value is already set.
646 * We check the diffserv field and the ECN field separately.
648 if (opt && opt->ip6po_tclass >= 0) {
651 if (IPV6_DSCP(ip6) == 0)
653 if (IPV6_ECN(ip6) == 0)
656 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
659 /* Fill in or override the hop limit field, if necessary. */
660 if (opt && opt->ip6po_hlim != -1)
661 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
662 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
664 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
666 ip6->ip6_hlim = V_ip6_defmcasthlim;
669 if (ro == NULL || ro->ro_nh == NULL) {
670 bzero(dst, sizeof(*dst));
671 dst->sin6_family = AF_INET6;
672 dst->sin6_len = sizeof(*dst);
673 dst->sin6_addr = ip6->ip6_dst;
676 * Validate route against routing table changes.
677 * Make sure that the address family is set in route.
683 if (ro->ro_nh != NULL && inp != NULL) {
684 ro->ro_dst.sin6_family = AF_INET6; /* XXX KASSERT? */
685 NH_VALIDATE((struct route *)ro, &inp->inp_rt_cookie,
688 if (ro->ro_nh != NULL && fwd_tag == NULL &&
689 (!NH_IS_VALID(ro->ro_nh) ||
690 ro->ro_dst.sin6_family != AF_INET6 ||
691 !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)))
692 RO_INVALIDATE_CACHE(ro);
694 if (ro->ro_nh != NULL && fwd_tag == NULL &&
695 ro->ro_dst.sin6_family == AF_INET6 &&
696 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
697 /* Nexthop is valid and contains valid ifp */
701 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
703 if (fwd_tag == NULL) {
704 bzero(&dst_sa, sizeof(dst_sa));
705 dst_sa.sin6_family = AF_INET6;
706 dst_sa.sin6_len = sizeof(dst_sa);
707 dst_sa.sin6_addr = ip6->ip6_dst;
709 error = in6_selectroute(&dst_sa, opt, im6o, ro, &ifp,
710 &nh, fibnum, m->m_pkthdr.flowid);
712 IP6STAT_INC(ip6s_noroute);
714 in6_ifstat_inc(ifp, ifs6_out_discard);
718 * At this point at least @ifp is not NULL
719 * Can be the case when dst is multicast, link-local or
720 * interface is explicitly specificed by the caller.
725 * If in6_selectroute() does not return a nexthop
726 * dst may not have been updated.
728 *dst = dst_sa; /* XXX */
733 origifp = nh->nh_aifp;
734 ia = (struct in6_ifaddr *)(nh->nh_ifa);
735 counter_u64_add(nh->nh_pksent, 1);
738 struct nhop_object *nh;
739 struct in6_addr kdst;
742 if (fwd_tag == NULL) {
743 bzero(&dst_sa, sizeof(dst_sa));
744 dst_sa.sin6_family = AF_INET6;
745 dst_sa.sin6_len = sizeof(dst_sa);
746 dst_sa.sin6_addr = ip6->ip6_dst;
749 if (IN6_IS_ADDR_MULTICAST(&dst_sa.sin6_addr) &&
751 (ifp = im6o->im6o_multicast_ifp) != NULL) {
752 /* We do not need a route lookup. */
753 *dst = dst_sa; /* XXX */
758 in6_splitscope(&dst_sa.sin6_addr, &kdst, &scopeid);
760 if (IN6_IS_ADDR_MC_LINKLOCAL(&dst_sa.sin6_addr) ||
761 IN6_IS_ADDR_MC_NODELOCAL(&dst_sa.sin6_addr)) {
763 ifp = in6_getlinkifnet(scopeid);
765 error = EHOSTUNREACH;
768 *dst = dst_sa; /* XXX */
774 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE,
777 IP6STAT_INC(ip6s_noroute);
778 /* No ifp in6_ifstat_inc(ifp, ifs6_out_discard); */
779 error = EHOSTUNREACH;
784 origifp = nh->nh_aifp;
785 ia = ifatoia6(nh->nh_ifa);
786 if (nh->nh_flags & NHF_GATEWAY)
787 dst->sin6_addr = nh->gw6_sa.sin6_addr;
788 else if (fwd_tag != NULL)
789 dst->sin6_addr = dst_sa.sin6_addr;
794 * At this point ifp MUST be pointing to the valid transmit ifp.
795 * origifp MUST be valid and pointing to either the same ifp or,
796 * in case of loopback output, to the interface which ip6_src
799 * fe80::1%em0 -> fe80::2%em0 -> ifp=em0, origifp=em0
800 * fe80::1%em0 -> fe80::1%em0 -> ifp=lo0, origifp=em0
801 * ::1 -> ::1 -> ifp=lo0, origifp=lo0
803 * mtu can be 0 and will be refined later.
805 KASSERT((ifp != NULL), ("output interface must not be NULL"));
806 KASSERT((origifp != NULL), ("output address interface must not be NULL"));
808 if ((flags & IPV6_FORWARDING) == 0) {
809 /* XXX: the FORWARDING flag can be set for mrouting. */
810 in6_ifstat_inc(ifp, ifs6_out_request);
813 /* Setup data structures for scope ID checks. */
815 bzero(&src_sa, sizeof(src_sa));
816 src_sa.sin6_family = AF_INET6;
817 src_sa.sin6_len = sizeof(src_sa);
818 src_sa.sin6_addr = ip6->ip6_src;
821 /* Re-initialize to be sure. */
822 bzero(&dst_sa, sizeof(dst_sa));
823 dst_sa.sin6_family = AF_INET6;
824 dst_sa.sin6_len = sizeof(dst_sa);
825 dst_sa.sin6_addr = ip6->ip6_dst;
827 /* Check for valid scope ID. */
828 if (in6_setscope(&src0, origifp, &zone) == 0 &&
829 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
830 in6_setscope(&dst0, origifp, &zone) == 0 &&
831 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
833 * The outgoing interface is in the zone of the source
834 * and destination addresses.
837 } else if ((origifp->if_flags & IFF_LOOPBACK) == 0 ||
838 sa6_recoverscope(&src_sa) != 0 ||
839 sa6_recoverscope(&dst_sa) != 0 ||
840 dst_sa.sin6_scope_id == 0 ||
841 (src_sa.sin6_scope_id != 0 &&
842 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
843 ifnet_byindex(dst_sa.sin6_scope_id) == NULL) {
845 * If the destination network interface is not a
846 * loopback interface, or the destination network
847 * address has no scope ID, or the source address has
848 * a scope ID set which is different from the
849 * destination address one, or there is no network
850 * interface representing this scope ID, the address
851 * pair is considered invalid.
853 IP6STAT_INC(ip6s_badscope);
854 in6_ifstat_inc(origifp, ifs6_out_discard);
856 error = EHOSTUNREACH; /* XXX */
859 /* All scope ID checks are successful. */
861 if (nh && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
862 if (opt && opt->ip6po_nextroute.ro_nh) {
864 * The nexthop is explicitly specified by the
865 * application. We assume the next hop is an IPv6
868 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
870 else if ((nh->nh_flags & NHF_GATEWAY))
874 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
875 m->m_flags &= ~(M_BCAST | M_MCAST); /* Just in case. */
877 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
878 in6_ifstat_inc(ifp, ifs6_out_mcast);
880 /* Confirm that the outgoing interface supports multicast. */
881 if (!(ifp->if_flags & IFF_MULTICAST)) {
882 IP6STAT_INC(ip6s_noroute);
883 in6_ifstat_inc(ifp, ifs6_out_discard);
887 if ((im6o == NULL && in6_mcast_loop) ||
888 (im6o && im6o->im6o_multicast_loop)) {
890 * Loop back multicast datagram if not expressly
891 * forbidden to do so, even if we have not joined
892 * the address; protocols will filter it later,
893 * thus deferring a hash lookup and lock acquisition
894 * at the expense of an m_copym().
896 ip6_mloopback(ifp, m);
899 * If we are acting as a multicast router, perform
900 * multicast forwarding as if the packet had just
901 * arrived on the interface to which we are about
902 * to send. The multicast forwarding function
903 * recursively calls this function, using the
904 * IPV6_FORWARDING flag to prevent infinite recursion.
906 * Multicasts that are looped back by ip6_mloopback(),
907 * above, will be forwarded by the ip6_input() routine,
910 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
912 * XXX: ip6_mforward expects that rcvif is NULL
913 * when it is called from the originating path.
914 * However, it may not always be the case.
916 m->m_pkthdr.rcvif = NULL;
917 if (ip6_mforward(ip6, ifp, m) != 0) {
924 * Multicasts with a hoplimit of zero may be looped back,
925 * above, but must not be transmitted on a network.
926 * Also, multicasts addressed to the loopback interface
927 * are not sent -- the above call to ip6_mloopback() will
928 * loop back a copy if this host actually belongs to the
929 * destination group on the loopback interface.
931 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
932 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
939 * Fill the outgoing inteface to tell the upper layer
940 * to increment per-interface statistics.
945 /* Determine path MTU. */
946 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
947 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
949 KASSERT(mtu > 0, ("%s:%d: mtu %ld, ro_pmtu %p ro %p ifp %p "
950 "alwaysfrag %d fibnum %u\n", __func__, __LINE__, mtu, ro_pmtu, ro,
951 ifp, alwaysfrag, fibnum));
954 * The caller of this function may specify to use the minimum MTU
956 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
957 * setting. The logic is a bit complicated; by default, unicast
958 * packets will follow path MTU while multicast packets will be sent at
959 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
960 * including unicast ones will be sent at the minimum MTU. Multicast
961 * packets will always be sent at the minimum MTU unless
962 * IP6PO_MINMTU_DISABLE is explicitly specified.
963 * See RFC 3542 for more details.
965 if (mtu > IPV6_MMTU) {
966 if ((flags & IPV6_MINMTU))
968 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
970 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
972 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
978 * Clear embedded scope identifiers if necessary.
979 * in6_clearscope() will touch the addresses only when necessary.
981 in6_clearscope(&ip6->ip6_src);
982 in6_clearscope(&ip6->ip6_dst);
985 * If the outgoing packet contains a hop-by-hop options header,
986 * it must be examined and processed even by the source node.
987 * (RFC 2460, section 4.)
989 if (exthdrs.ip6e_hbh) {
990 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
991 u_int32_t dummy; /* XXX unused */
992 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
995 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
996 panic("ip6e_hbh is not contiguous");
999 * XXX: if we have to send an ICMPv6 error to the sender,
1000 * we need the M_LOOP flag since icmp6_error() expects
1001 * the IPv6 and the hop-by-hop options header are
1002 * contiguous unless the flag is set.
1004 m->m_flags |= M_LOOP;
1005 m->m_pkthdr.rcvif = ifp;
1006 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
1007 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
1008 &dummy, &plen) < 0) {
1009 /* m was already freed at this point. */
1010 error = EINVAL;/* better error? */
1013 m->m_flags &= ~M_LOOP; /* XXX */
1014 m->m_pkthdr.rcvif = NULL;
1017 /* Jump over all PFIL processing if hooks are not active. */
1018 if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
1021 odst = ip6->ip6_dst;
1022 /* Run through list of hooks for output packets. */
1023 switch (pfil_mbuf_out(V_inet6_pfil_head, &m, ifp, inp)) {
1025 ip6 = mtod(m, struct ip6_hdr *);
1035 /* See if destination IP address was changed by packet filter. */
1036 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
1037 m->m_flags |= M_SKIP_FIREWALL;
1038 /* If destination is now ourself drop to ip6_input(). */
1039 if (in6_localip(&ip6->ip6_dst)) {
1040 m->m_flags |= M_FASTFWD_OURS;
1041 if (m->m_pkthdr.rcvif == NULL)
1042 m->m_pkthdr.rcvif = V_loif;
1043 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1044 m->m_pkthdr.csum_flags |=
1045 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1046 m->m_pkthdr.csum_data = 0xffff;
1048 #if defined(SCTP) || defined(SCTP_SUPPORT)
1049 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1050 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1052 error = netisr_queue(NETISR_IPV6, m);
1056 RO_INVALIDATE_CACHE(ro);
1057 needfiblookup = 1; /* Redo the routing table lookup. */
1060 /* See if fib was changed by packet filter. */
1061 if (fibnum != M_GETFIB(m)) {
1062 m->m_flags |= M_SKIP_FIREWALL;
1063 fibnum = M_GETFIB(m);
1065 RO_INVALIDATE_CACHE(ro);
1071 /* See if local, if yes, send it to netisr. */
1072 if (m->m_flags & M_FASTFWD_OURS) {
1073 if (m->m_pkthdr.rcvif == NULL)
1074 m->m_pkthdr.rcvif = V_loif;
1075 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1076 m->m_pkthdr.csum_flags |=
1077 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1078 m->m_pkthdr.csum_data = 0xffff;
1080 #if defined(SCTP) || defined(SCTP_SUPPORT)
1081 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1082 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1084 error = netisr_queue(NETISR_IPV6, m);
1087 /* Or forward to some other address? */
1088 if ((m->m_flags & M_IP6_NEXTHOP) &&
1089 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
1091 dst = (struct sockaddr_in6 *)&ro->ro_dst;
1094 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
1095 m->m_flags |= M_SKIP_FIREWALL;
1096 m->m_flags &= ~M_IP6_NEXTHOP;
1097 m_tag_delete(m, fwd_tag);
1103 EVL_APPLY_PRI(m, vlan_pcp);
1105 /* Ensure the packet data is mapped if the interface requires it. */
1106 if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) {
1107 m = mb_unmapped_to_ext(m);
1109 IP6STAT_INC(ip6s_odropped);
1115 * Send the packet to the outgoing interface.
1116 * If necessary, do IPv6 fragmentation before sending.
1118 * The logic here is rather complex:
1119 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
1120 * 1-a: send as is if tlen <= path mtu
1121 * 1-b: fragment if tlen > path mtu
1123 * 2: if user asks us not to fragment (dontfrag == 1)
1124 * 2-a: send as is if tlen <= interface mtu
1125 * 2-b: error if tlen > interface mtu
1127 * 3: if we always need to attach fragment header (alwaysfrag == 1)
1130 * 4: if dontfrag == 1 && alwaysfrag == 1
1131 * error, as we cannot handle this conflicting request.
1133 sw_csum = m->m_pkthdr.csum_flags;
1135 tso = ((sw_csum & ifp->if_hwassist &
1136 (CSUM_TSO | CSUM_INNER_TSO)) != 0) ? 1 : 0;
1137 sw_csum &= ~ifp->if_hwassist;
1141 * If we added extension headers, we will not do TSO and calculate the
1142 * checksums ourselves for now.
1143 * XXX-BZ Need a framework to know when the NIC can handle it, even
1146 ip6_output_delayed_csum(m, ifp, sw_csum, plen, optlen);
1147 /* XXX-BZ m->m_pkthdr.csum_flags &= ~ifp->if_hwassist; */
1148 tlen = m->m_pkthdr.len;
1150 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
1154 if (dontfrag && alwaysfrag) { /* Case 4. */
1155 /* Conflicting request - can't transmit. */
1159 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* Case 2-b. */
1161 * Even if the DONTFRAG option is specified, we cannot send the
1162 * packet when the data length is larger than the MTU of the
1163 * outgoing interface.
1164 * Notify the error by sending IPV6_PATHMTU ancillary data if
1165 * application wanted to know the MTU value. Also return an
1166 * error code (this is not described in the API spec).
1169 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
1174 /* Transmit packet without fragmentation. */
1175 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* Cases 1-a and 2-a. */
1176 struct in6_ifaddr *ia6;
1178 ip6 = mtod(m, struct ip6_hdr *);
1179 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1181 /* Record statistics for this interface address. */
1182 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
1183 counter_u64_add(ia6->ia_ifa.ifa_obytes,
1186 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1187 (flags & IP_NO_SND_TAG_RL) ? false : true);
1191 /* Try to fragment the packet. Cases 1-b and 3. */
1192 if (mtu < IPV6_MMTU) {
1193 /* Path MTU cannot be less than IPV6_MMTU. */
1195 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1197 } else if (ip6->ip6_plen == 0) {
1198 /* Jumbo payload cannot be fragmented. */
1200 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1206 * Too large for the destination or interface;
1207 * fragment if possible.
1208 * Must be able to put at least 8 bytes per fragment.
1210 if (mtu > IPV6_MAXPACKET)
1211 mtu = IPV6_MAXPACKET;
1213 len = (mtu - unfragpartlen - sizeof(struct ip6_frag)) & ~7;
1216 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1221 * If the interface will not calculate checksums on
1222 * fragmented packets, then do it here.
1223 * XXX-BZ handle the hw offloading case. Need flags.
1225 ip6_output_delayed_csum(m, ifp, m->m_pkthdr.csum_flags, plen,
1229 * Change the next header field of the last header in the
1230 * unfragmentable part.
1232 if (exthdrs.ip6e_rthdr) {
1233 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1234 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1235 } else if (exthdrs.ip6e_dest1) {
1236 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1237 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1238 } else if (exthdrs.ip6e_hbh) {
1239 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1240 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1242 ip6 = mtod(m, struct ip6_hdr *);
1243 nextproto = ip6->ip6_nxt;
1244 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1248 * Loop through length of segment after first fragment,
1249 * make new header and copy data of each part and link onto
1253 id = htonl(ip6_randomid());
1254 error = ip6_fragment(ifp, m, unfragpartlen, nextproto,len, id);
1258 in6_ifstat_inc(ifp, ifs6_out_fragok);
1261 /* Remove leading garbage. */
1270 /* Record statistics for this interface address. */
1272 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1273 counter_u64_add(ia->ia_ifa.ifa_obytes,
1277 EVL_APPLY_PRI(m, vlan_pcp);
1278 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1285 IP6STAT_INC(ip6s_fragmented);
1291 m_freem(exthdrs.ip6e_hbh); /* m_freem() checks if mbuf is NULL. */
1292 m_freem(exthdrs.ip6e_dest1);
1293 m_freem(exthdrs.ip6e_rthdr);
1294 m_freem(exthdrs.ip6e_dest2);
1303 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1307 if (hlen > MCLBYTES)
1308 return (ENOBUFS); /* XXX */
1311 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1313 m = m_get(M_NOWAIT, MT_DATA);
1318 bcopy(hdr, mtod(m, caddr_t), hlen);
1325 * Insert jumbo payload option.
1328 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1334 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1337 * If there is no hop-by-hop options header, allocate new one.
1338 * If there is one but it doesn't have enough space to store the
1339 * jumbo payload option, allocate a cluster to store the whole options.
1340 * Otherwise, use it to store the options.
1342 if (exthdrs->ip6e_hbh == NULL) {
1343 mopt = m_get(M_NOWAIT, MT_DATA);
1346 mopt->m_len = JUMBOOPTLEN;
1347 optbuf = mtod(mopt, u_char *);
1348 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1349 exthdrs->ip6e_hbh = mopt;
1351 struct ip6_hbh *hbh;
1353 mopt = exthdrs->ip6e_hbh;
1354 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1357 * - exthdrs->ip6e_hbh is not referenced from places
1358 * other than exthdrs.
1359 * - exthdrs->ip6e_hbh is not an mbuf chain.
1361 int oldoptlen = mopt->m_len;
1365 * XXX: give up if the whole (new) hbh header does
1366 * not fit even in an mbuf cluster.
1368 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1372 * As a consequence, we must always prepare a cluster
1375 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1378 n->m_len = oldoptlen + JUMBOOPTLEN;
1379 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1381 optbuf = mtod(n, caddr_t) + oldoptlen;
1383 mopt = exthdrs->ip6e_hbh = n;
1385 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1386 mopt->m_len += JUMBOOPTLEN;
1388 optbuf[0] = IP6OPT_PADN;
1392 * Adjust the header length according to the pad and
1393 * the jumbo payload option.
1395 hbh = mtod(mopt, struct ip6_hbh *);
1396 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1399 /* fill in the option. */
1400 optbuf[2] = IP6OPT_JUMBO;
1402 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1403 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1405 /* finally, adjust the packet header length */
1406 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1413 * Insert fragment header and copy unfragmentable header portions.
1416 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1417 struct ip6_frag **frghdrp)
1419 struct mbuf *n, *mlast;
1421 if (hlen > sizeof(struct ip6_hdr)) {
1422 n = m_copym(m0, sizeof(struct ip6_hdr),
1423 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1430 /* Search for the last mbuf of unfragmentable part. */
1431 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1434 if (M_WRITABLE(mlast) &&
1435 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1436 /* use the trailing space of the last mbuf for the fragment hdr */
1437 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1439 mlast->m_len += sizeof(struct ip6_frag);
1440 m->m_pkthdr.len += sizeof(struct ip6_frag);
1442 /* allocate a new mbuf for the fragment header */
1445 mfrg = m_get(M_NOWAIT, MT_DATA);
1448 mfrg->m_len = sizeof(struct ip6_frag);
1449 *frghdrp = mtod(mfrg, struct ip6_frag *);
1450 mlast->m_next = mfrg;
1457 * Calculates IPv6 path mtu for destination @dst.
1458 * Resulting MTU is stored in @mtup.
1460 * Returns 0 on success.
1463 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1465 struct epoch_tracker et;
1466 struct nhop_object *nh;
1467 struct in6_addr kdst;
1471 in6_splitscope(dst, &kdst, &scopeid);
1473 NET_EPOCH_ENTER(et);
1474 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1476 error = ip6_calcmtu(nh->nh_ifp, dst, nh->nh_mtu, mtup, NULL, 0);
1478 error = EHOSTUNREACH;
1485 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1486 * and cached data in @ro_pmtu.
1487 * MTU from (successful) route lookup is saved (along with dst)
1488 * inside @ro_pmtu to avoid subsequent route lookups after packet
1489 * filter processing.
1491 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1492 * Returns 0 on success.
1495 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1496 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1497 int *alwaysfragp, u_int fibnum, u_int proto)
1499 struct nhop_object *nh;
1500 struct in6_addr kdst;
1502 struct sockaddr_in6 *sa6_dst, sin6;
1508 if (ro_pmtu == NULL || do_lookup) {
1510 * Here ro_pmtu has final destination address, while
1511 * ro might represent immediate destination.
1512 * Use ro_pmtu destination since mtu might differ.
1514 if (ro_pmtu != NULL) {
1515 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1516 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1517 ro_pmtu->ro_mtu = 0;
1521 if (ro_pmtu == NULL || ro_pmtu->ro_mtu == 0) {
1522 bzero(sa6_dst, sizeof(*sa6_dst));
1523 sa6_dst->sin6_family = AF_INET6;
1524 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1525 sa6_dst->sin6_addr = *dst;
1527 in6_splitscope(dst, &kdst, &scopeid);
1528 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1531 if (ro_pmtu != NULL)
1532 ro_pmtu->ro_mtu = mtu;
1535 mtu = ro_pmtu->ro_mtu;
1538 if (ro_pmtu != NULL && ro_pmtu->ro_nh != NULL)
1539 mtu = ro_pmtu->ro_nh->nh_mtu;
1541 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1545 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1546 * hostcache data for @dst.
1547 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1549 * Returns 0 on success.
1552 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1553 u_long *mtup, int *alwaysfragp, u_int proto)
1561 struct in_conninfo inc;
1563 bzero(&inc, sizeof(inc));
1564 inc.inc_flags |= INC_ISIPV6;
1565 inc.inc6_faddr = *dst;
1567 ifmtu = IN6_LINKMTU(ifp);
1569 /* TCP is known to react to pmtu changes so skip hc */
1570 if (proto != IPPROTO_TCP)
1571 mtu = tcp_hc_getmtu(&inc);
1574 mtu = min(mtu, rt_mtu);
1579 else if (mtu < IPV6_MMTU) {
1581 * RFC2460 section 5, last paragraph:
1582 * if we record ICMPv6 too big message with
1583 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1584 * or smaller, with framgent header attached.
1585 * (fragment header is needed regardless from the
1586 * packet size, for translators to identify packets)
1592 mtu = IN6_LINKMTU(ifp);
1594 error = EHOSTUNREACH; /* XXX */
1598 *alwaysfragp = alwaysfrag;
1603 * IP6 socket option processing.
1606 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1608 int optdatalen, uproto;
1610 struct inpcb *inp = sotoinpcb(so);
1612 int level, op, optname;
1616 uint32_t rss_bucket;
1621 * Don't use more than a quarter of mbuf clusters. N.B.:
1622 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1623 * on LP64 architectures, so cast to u_long to avoid undefined
1624 * behavior. ILP32 architectures cannot have nmbclusters
1625 * large enough to overflow for other reasons.
1627 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1629 level = sopt->sopt_level;
1630 op = sopt->sopt_dir;
1631 optname = sopt->sopt_name;
1632 optlen = sopt->sopt_valsize;
1636 uproto = (int)so->so_proto->pr_protocol;
1638 if (level != IPPROTO_IPV6) {
1641 if (sopt->sopt_level == SOL_SOCKET &&
1642 sopt->sopt_dir == SOPT_SET) {
1643 switch (sopt->sopt_name) {
1646 inp->inp_inc.inc_fibnum = so->so_fibnum;
1650 case SO_MAX_PACING_RATE:
1653 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1664 } else { /* level == IPPROTO_IPV6 */
1668 case IPV6_2292PKTOPTIONS:
1669 #ifdef IPV6_PKTOPTIONS
1670 case IPV6_PKTOPTIONS:
1675 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1676 printf("ip6_ctloutput: mbuf limit hit\n");
1681 error = soopt_getm(sopt, &m); /* XXX */
1684 error = soopt_mcopyin(sopt, m); /* XXX */
1688 error = ip6_pcbopts(&inp->in6p_outputopts, m,
1691 m_freem(m); /* XXX */
1696 * Use of some Hop-by-Hop options or some
1697 * Destination options, might require special
1698 * privilege. That is, normal applications
1699 * (without special privilege) might be forbidden
1700 * from setting certain options in outgoing packets,
1701 * and might never see certain options in received
1702 * packets. [RFC 2292 Section 6]
1703 * KAME specific note:
1704 * KAME prevents non-privileged users from sending or
1705 * receiving ANY hbh/dst options in order to avoid
1706 * overhead of parsing options in the kernel.
1708 case IPV6_RECVHOPOPTS:
1709 case IPV6_RECVDSTOPTS:
1710 case IPV6_RECVRTHDRDSTOPTS:
1712 error = priv_check(td,
1713 PRIV_NETINET_SETHDROPTS);
1718 case IPV6_UNICAST_HOPS:
1721 case IPV6_RECVPKTINFO:
1722 case IPV6_RECVHOPLIMIT:
1723 case IPV6_RECVRTHDR:
1724 case IPV6_RECVPATHMTU:
1725 case IPV6_RECVTCLASS:
1726 case IPV6_RECVFLOWID:
1728 case IPV6_RECVRSSBUCKETID:
1731 case IPV6_AUTOFLOWLABEL:
1732 case IPV6_ORIGDSTADDR:
1735 if (optname == IPV6_BINDANY && td != NULL) {
1736 error = priv_check(td,
1737 PRIV_NETINET_BINDANY);
1742 if (optlen != sizeof(int)) {
1746 error = sooptcopyin(sopt, &optval,
1747 sizeof optval, sizeof optval);
1751 case IPV6_UNICAST_HOPS:
1752 if (optval < -1 || optval >= 256)
1755 /* -1 = kernel default */
1756 inp->in6p_hops = optval;
1757 if ((inp->inp_vflag &
1759 inp->inp_ip_ttl = optval;
1762 #define OPTSET(bit) \
1766 inp->inp_flags |= (bit); \
1768 inp->inp_flags &= ~(bit); \
1770 } while (/*CONSTCOND*/ 0)
1771 #define OPTSET2292(bit) \
1774 inp->inp_flags |= IN6P_RFC2292; \
1776 inp->inp_flags |= (bit); \
1778 inp->inp_flags &= ~(bit); \
1780 } while (/*CONSTCOND*/ 0)
1781 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0)
1783 #define OPTSET2_N(bit, val) do { \
1785 inp->inp_flags2 |= bit; \
1787 inp->inp_flags2 &= ~bit; \
1789 #define OPTSET2(bit, val) do { \
1791 OPTSET2_N(bit, val); \
1794 #define OPTBIT2(bit) (inp->inp_flags2 & (bit) ? 1 : 0)
1795 #define OPTSET2292_EXCLUSIVE(bit) \
1798 if (OPTBIT(IN6P_RFC2292)) { \
1802 inp->inp_flags |= (bit); \
1804 inp->inp_flags &= ~(bit); \
1807 } while (/*CONSTCOND*/ 0)
1809 case IPV6_RECVPKTINFO:
1810 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1815 struct ip6_pktopts **optp;
1817 /* cannot mix with RFC2292 */
1818 if (OPTBIT(IN6P_RFC2292)) {
1823 if (inp->inp_flags & INP_DROPPED) {
1825 return (ECONNRESET);
1827 optp = &inp->in6p_outputopts;
1828 error = ip6_pcbopt(IPV6_HOPLIMIT,
1829 (u_char *)&optval, sizeof(optval),
1830 optp, (td != NULL) ? td->td_ucred :
1836 case IPV6_RECVHOPLIMIT:
1837 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1840 case IPV6_RECVHOPOPTS:
1841 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1844 case IPV6_RECVDSTOPTS:
1845 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1848 case IPV6_RECVRTHDRDSTOPTS:
1849 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1852 case IPV6_RECVRTHDR:
1853 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1856 case IPV6_RECVPATHMTU:
1858 * We ignore this option for TCP
1860 * (RFC3542 leaves this case
1863 if (uproto != IPPROTO_TCP)
1867 case IPV6_RECVFLOWID:
1868 OPTSET2(INP_RECVFLOWID, optval);
1872 case IPV6_RECVRSSBUCKETID:
1873 OPTSET2(INP_RECVRSSBUCKETID, optval);
1879 if (inp->inp_lport ||
1880 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1882 * The socket is already bound.
1889 inp->inp_flags |= IN6P_IPV6_V6ONLY;
1890 inp->inp_vflag &= ~INP_IPV4;
1892 inp->inp_flags &= ~IN6P_IPV6_V6ONLY;
1893 inp->inp_vflag |= INP_IPV4;
1897 case IPV6_RECVTCLASS:
1898 /* cannot mix with RFC2292 XXX */
1899 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1901 case IPV6_AUTOFLOWLABEL:
1902 OPTSET(IN6P_AUTOFLOWLABEL);
1905 case IPV6_ORIGDSTADDR:
1906 OPTSET2(INP_ORIGDSTADDR, optval);
1909 OPTSET(INP_BINDANY);
1912 if ((optval >= -1) && (optval <=
1913 (INP_2PCP_MASK >> INP_2PCP_SHIFT))) {
1939 case IPV6_USE_MIN_MTU:
1940 case IPV6_PREFER_TEMPADDR:
1941 if (optlen != sizeof(optval)) {
1945 error = sooptcopyin(sopt, &optval,
1946 sizeof optval, sizeof optval);
1950 struct ip6_pktopts **optp;
1952 if (inp->inp_flags & INP_DROPPED) {
1954 return (ECONNRESET);
1956 optp = &inp->in6p_outputopts;
1957 error = ip6_pcbopt(optname,
1958 (u_char *)&optval, sizeof(optval),
1959 optp, (td != NULL) ? td->td_ucred :
1965 case IPV6_2292PKTINFO:
1966 case IPV6_2292HOPLIMIT:
1967 case IPV6_2292HOPOPTS:
1968 case IPV6_2292DSTOPTS:
1969 case IPV6_2292RTHDR:
1971 if (optlen != sizeof(int)) {
1975 error = sooptcopyin(sopt, &optval,
1976 sizeof optval, sizeof optval);
1980 case IPV6_2292PKTINFO:
1981 OPTSET2292(IN6P_PKTINFO);
1983 case IPV6_2292HOPLIMIT:
1984 OPTSET2292(IN6P_HOPLIMIT);
1986 case IPV6_2292HOPOPTS:
1988 * Check super-user privilege.
1989 * See comments for IPV6_RECVHOPOPTS.
1992 error = priv_check(td,
1993 PRIV_NETINET_SETHDROPTS);
1997 OPTSET2292(IN6P_HOPOPTS);
1999 case IPV6_2292DSTOPTS:
2001 error = priv_check(td,
2002 PRIV_NETINET_SETHDROPTS);
2006 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
2008 case IPV6_2292RTHDR:
2009 OPTSET2292(IN6P_RTHDR);
2017 case IPV6_RTHDRDSTOPTS:
2020 /* new advanced API (RFC3542) */
2022 u_char optbuf_storage[MCLBYTES];
2024 struct ip6_pktopts **optp;
2026 /* cannot mix with RFC2292 */
2027 if (OPTBIT(IN6P_RFC2292)) {
2033 * We only ensure valsize is not too large
2034 * here. Further validation will be done
2037 error = sooptcopyin(sopt, optbuf_storage,
2038 sizeof(optbuf_storage), 0);
2041 optlen = sopt->sopt_valsize;
2042 optbuf = optbuf_storage;
2044 if (inp->inp_flags & INP_DROPPED) {
2046 return (ECONNRESET);
2048 optp = &inp->in6p_outputopts;
2049 error = ip6_pcbopt(optname, optbuf, optlen,
2050 optp, (td != NULL) ? td->td_ucred : NULL,
2057 case IPV6_MULTICAST_IF:
2058 case IPV6_MULTICAST_HOPS:
2059 case IPV6_MULTICAST_LOOP:
2060 case IPV6_JOIN_GROUP:
2061 case IPV6_LEAVE_GROUP:
2063 case MCAST_BLOCK_SOURCE:
2064 case MCAST_UNBLOCK_SOURCE:
2065 case MCAST_JOIN_GROUP:
2066 case MCAST_LEAVE_GROUP:
2067 case MCAST_JOIN_SOURCE_GROUP:
2068 case MCAST_LEAVE_SOURCE_GROUP:
2069 error = ip6_setmoptions(inp, sopt);
2072 case IPV6_PORTRANGE:
2073 error = sooptcopyin(sopt, &optval,
2074 sizeof optval, sizeof optval);
2080 case IPV6_PORTRANGE_DEFAULT:
2081 inp->inp_flags &= ~(INP_LOWPORT);
2082 inp->inp_flags &= ~(INP_HIGHPORT);
2085 case IPV6_PORTRANGE_HIGH:
2086 inp->inp_flags &= ~(INP_LOWPORT);
2087 inp->inp_flags |= INP_HIGHPORT;
2090 case IPV6_PORTRANGE_LOW:
2091 inp->inp_flags &= ~(INP_HIGHPORT);
2092 inp->inp_flags |= INP_LOWPORT;
2102 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2103 case IPV6_IPSEC_POLICY:
2104 if (IPSEC_ENABLED(ipv6)) {
2105 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2112 error = ENOPROTOOPT;
2119 case IPV6_2292PKTOPTIONS:
2120 #ifdef IPV6_PKTOPTIONS
2121 case IPV6_PKTOPTIONS:
2124 * RFC3542 (effectively) deprecated the
2125 * semantics of the 2292-style pktoptions.
2126 * Since it was not reliable in nature (i.e.,
2127 * applications had to expect the lack of some
2128 * information after all), it would make sense
2129 * to simplify this part by always returning
2132 sopt->sopt_valsize = 0;
2135 case IPV6_RECVHOPOPTS:
2136 case IPV6_RECVDSTOPTS:
2137 case IPV6_RECVRTHDRDSTOPTS:
2138 case IPV6_UNICAST_HOPS:
2139 case IPV6_RECVPKTINFO:
2140 case IPV6_RECVHOPLIMIT:
2141 case IPV6_RECVRTHDR:
2142 case IPV6_RECVPATHMTU:
2145 case IPV6_PORTRANGE:
2146 case IPV6_RECVTCLASS:
2147 case IPV6_AUTOFLOWLABEL:
2151 case IPV6_RECVFLOWID:
2153 case IPV6_RSSBUCKETID:
2154 case IPV6_RECVRSSBUCKETID:
2158 case IPV6_RECVHOPOPTS:
2159 optval = OPTBIT(IN6P_HOPOPTS);
2162 case IPV6_RECVDSTOPTS:
2163 optval = OPTBIT(IN6P_DSTOPTS);
2166 case IPV6_RECVRTHDRDSTOPTS:
2167 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2170 case IPV6_UNICAST_HOPS:
2171 optval = inp->in6p_hops;
2174 case IPV6_RECVPKTINFO:
2175 optval = OPTBIT(IN6P_PKTINFO);
2178 case IPV6_RECVHOPLIMIT:
2179 optval = OPTBIT(IN6P_HOPLIMIT);
2182 case IPV6_RECVRTHDR:
2183 optval = OPTBIT(IN6P_RTHDR);
2186 case IPV6_RECVPATHMTU:
2187 optval = OPTBIT(IN6P_MTU);
2191 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2194 case IPV6_PORTRANGE:
2197 flags = inp->inp_flags;
2198 if (flags & INP_HIGHPORT)
2199 optval = IPV6_PORTRANGE_HIGH;
2200 else if (flags & INP_LOWPORT)
2201 optval = IPV6_PORTRANGE_LOW;
2206 case IPV6_RECVTCLASS:
2207 optval = OPTBIT(IN6P_TCLASS);
2210 case IPV6_AUTOFLOWLABEL:
2211 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2214 case IPV6_ORIGDSTADDR:
2215 optval = OPTBIT2(INP_ORIGDSTADDR);
2219 optval = OPTBIT(INP_BINDANY);
2223 optval = inp->inp_flowid;
2227 optval = inp->inp_flowtype;
2230 case IPV6_RECVFLOWID:
2231 optval = OPTBIT2(INP_RECVFLOWID);
2234 case IPV6_RSSBUCKETID:
2236 rss_hash2bucket(inp->inp_flowid,
2240 optval = rss_bucket;
2245 case IPV6_RECVRSSBUCKETID:
2246 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2252 if (OPTBIT2(INP_2PCP_SET)) {
2253 optval = (inp->inp_flags2 &
2264 error = sooptcopyout(sopt, &optval,
2271 struct ip6_mtuinfo mtuinfo;
2272 struct in6_addr addr;
2274 if (!(so->so_state & SS_ISCONNECTED))
2277 * XXX: we dot not consider the case of source
2278 * routing, or optional information to specify
2279 * the outgoing interface.
2280 * Copy faddr out of inp to avoid holding lock
2281 * on inp during route lookup.
2284 bcopy(&inp->in6p_faddr, &addr, sizeof(addr));
2286 error = ip6_getpmtu_ctl(so->so_fibnum,
2290 if (pmtu > IPV6_MAXPACKET)
2291 pmtu = IPV6_MAXPACKET;
2293 bzero(&mtuinfo, sizeof(mtuinfo));
2294 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2295 optdata = (void *)&mtuinfo;
2296 optdatalen = sizeof(mtuinfo);
2297 error = sooptcopyout(sopt, optdata,
2302 case IPV6_2292PKTINFO:
2303 case IPV6_2292HOPLIMIT:
2304 case IPV6_2292HOPOPTS:
2305 case IPV6_2292RTHDR:
2306 case IPV6_2292DSTOPTS:
2308 case IPV6_2292PKTINFO:
2309 optval = OPTBIT(IN6P_PKTINFO);
2311 case IPV6_2292HOPLIMIT:
2312 optval = OPTBIT(IN6P_HOPLIMIT);
2314 case IPV6_2292HOPOPTS:
2315 optval = OPTBIT(IN6P_HOPOPTS);
2317 case IPV6_2292RTHDR:
2318 optval = OPTBIT(IN6P_RTHDR);
2320 case IPV6_2292DSTOPTS:
2321 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2324 error = sooptcopyout(sopt, &optval,
2331 case IPV6_RTHDRDSTOPTS:
2335 case IPV6_USE_MIN_MTU:
2336 case IPV6_PREFER_TEMPADDR:
2337 error = ip6_getpcbopt(inp, optname, sopt);
2340 case IPV6_MULTICAST_IF:
2341 case IPV6_MULTICAST_HOPS:
2342 case IPV6_MULTICAST_LOOP:
2344 error = ip6_getmoptions(inp, sopt);
2347 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2348 case IPV6_IPSEC_POLICY:
2349 if (IPSEC_ENABLED(ipv6)) {
2350 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2356 error = ENOPROTOOPT;
2366 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2368 int error = 0, optval, optlen;
2369 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2370 struct inpcb *inp = sotoinpcb(so);
2371 int level, op, optname;
2373 level = sopt->sopt_level;
2374 op = sopt->sopt_dir;
2375 optname = sopt->sopt_name;
2376 optlen = sopt->sopt_valsize;
2378 if (level != IPPROTO_IPV6) {
2385 * For ICMPv6 sockets, no modification allowed for checksum
2386 * offset, permit "no change" values to help existing apps.
2388 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2389 * for an ICMPv6 socket will fail."
2390 * The current behavior does not meet RFC3542.
2394 if (optlen != sizeof(int)) {
2398 error = sooptcopyin(sopt, &optval, sizeof(optval),
2402 if (optval < -1 || (optval % 2) != 0) {
2404 * The API assumes non-negative even offset
2405 * values or -1 as a special value.
2408 } else if (inp->inp_ip_p == IPPROTO_ICMPV6) {
2409 if (optval != icmp6off)
2412 inp->in6p_cksum = optval;
2416 if (inp->inp_ip_p == IPPROTO_ICMPV6)
2419 optval = inp->in6p_cksum;
2421 error = sooptcopyout(sopt, &optval, sizeof(optval));
2431 error = ENOPROTOOPT;
2439 * Set up IP6 options in pcb for insertion in output packets or
2440 * specifying behavior of outgoing packets.
2443 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2444 struct socket *so, struct sockopt *sopt)
2446 struct ip6_pktopts *opt = *pktopt;
2448 struct thread *td = sopt->sopt_td;
2449 struct epoch_tracker et;
2451 /* turn off any old options. */
2454 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2455 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2456 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2457 printf("ip6_pcbopts: all specified options are cleared.\n");
2459 ip6_clearpktopts(opt, -1);
2461 opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT);
2467 if (!m || m->m_len == 0) {
2469 * Only turning off any previous options, regardless of
2470 * whether the opt is just created or given.
2472 free(opt, M_IP6OPT);
2476 /* set options specified by user. */
2477 NET_EPOCH_ENTER(et);
2478 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2479 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2480 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2481 free(opt, M_IP6OPT);
2491 * initialize ip6_pktopts. beware that there are non-zero default values in
2495 ip6_initpktopts(struct ip6_pktopts *opt)
2498 bzero(opt, sizeof(*opt));
2499 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2500 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2501 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2502 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2506 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2507 struct ucred *cred, int uproto)
2509 struct epoch_tracker et;
2510 struct ip6_pktopts *opt;
2513 if (*pktopt == NULL) {
2514 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2516 if (*pktopt == NULL)
2518 ip6_initpktopts(*pktopt);
2522 NET_EPOCH_ENTER(et);
2523 ret = ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto);
2529 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2530 if (pktopt && pktopt->field) { \
2532 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2533 malloc_optdata = true; \
2535 if (inp->inp_flags & INP_DROPPED) { \
2537 free(optdata, M_TEMP); \
2538 return (ECONNRESET); \
2540 pktopt = inp->in6p_outputopts; \
2541 if (pktopt && pktopt->field) { \
2542 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2543 bcopy(pktopt->field, optdata, optdatalen); \
2545 free(optdata, M_TEMP); \
2547 malloc_optdata = false; \
2552 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2553 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2555 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2556 pktopt->field->sa_len)
2559 ip6_getpcbopt(struct inpcb *inp, int optname, struct sockopt *sopt)
2561 void *optdata = NULL;
2562 bool malloc_optdata = false;
2565 struct in6_pktinfo null_pktinfo;
2566 int deftclass = 0, on;
2567 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2568 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2569 struct ip6_pktopts *pktopt;
2572 pktopt = inp->in6p_outputopts;
2576 optdata = (void *)&null_pktinfo;
2577 if (pktopt && pktopt->ip6po_pktinfo) {
2578 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2579 sizeof(null_pktinfo));
2580 in6_clearscope(&null_pktinfo.ipi6_addr);
2582 /* XXX: we don't have to do this every time... */
2583 bzero(&null_pktinfo, sizeof(null_pktinfo));
2585 optdatalen = sizeof(struct in6_pktinfo);
2588 if (pktopt && pktopt->ip6po_tclass >= 0)
2589 deftclass = pktopt->ip6po_tclass;
2590 optdata = (void *)&deftclass;
2591 optdatalen = sizeof(int);
2594 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2597 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2599 case IPV6_RTHDRDSTOPTS:
2600 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2603 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2606 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2608 case IPV6_USE_MIN_MTU:
2610 defminmtu = pktopt->ip6po_minmtu;
2611 optdata = (void *)&defminmtu;
2612 optdatalen = sizeof(int);
2615 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2619 optdata = (void *)&on;
2620 optdatalen = sizeof(on);
2622 case IPV6_PREFER_TEMPADDR:
2624 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2625 optdata = (void *)&defpreftemp;
2626 optdatalen = sizeof(int);
2628 default: /* should not happen */
2630 panic("ip6_getpcbopt: unexpected option\n");
2633 return (ENOPROTOOPT);
2637 error = sooptcopyout(sopt, optdata, optdatalen);
2639 free(optdata, M_TEMP);
2645 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2650 if (optname == -1 || optname == IPV6_PKTINFO) {
2651 if (pktopt->ip6po_pktinfo)
2652 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2653 pktopt->ip6po_pktinfo = NULL;
2655 if (optname == -1 || optname == IPV6_HOPLIMIT)
2656 pktopt->ip6po_hlim = -1;
2657 if (optname == -1 || optname == IPV6_TCLASS)
2658 pktopt->ip6po_tclass = -1;
2659 if (optname == -1 || optname == IPV6_NEXTHOP) {
2660 if (pktopt->ip6po_nextroute.ro_nh) {
2661 NH_FREE(pktopt->ip6po_nextroute.ro_nh);
2662 pktopt->ip6po_nextroute.ro_nh = NULL;
2664 if (pktopt->ip6po_nexthop)
2665 free(pktopt->ip6po_nexthop, M_IP6OPT);
2666 pktopt->ip6po_nexthop = NULL;
2668 if (optname == -1 || optname == IPV6_HOPOPTS) {
2669 if (pktopt->ip6po_hbh)
2670 free(pktopt->ip6po_hbh, M_IP6OPT);
2671 pktopt->ip6po_hbh = NULL;
2673 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2674 if (pktopt->ip6po_dest1)
2675 free(pktopt->ip6po_dest1, M_IP6OPT);
2676 pktopt->ip6po_dest1 = NULL;
2678 if (optname == -1 || optname == IPV6_RTHDR) {
2679 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2680 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2681 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2682 if (pktopt->ip6po_route.ro_nh) {
2683 NH_FREE(pktopt->ip6po_route.ro_nh);
2684 pktopt->ip6po_route.ro_nh = NULL;
2687 if (optname == -1 || optname == IPV6_DSTOPTS) {
2688 if (pktopt->ip6po_dest2)
2689 free(pktopt->ip6po_dest2, M_IP6OPT);
2690 pktopt->ip6po_dest2 = NULL;
2694 #define PKTOPT_EXTHDRCPY(type) \
2697 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2698 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2699 if (dst->type == NULL)\
2701 bcopy(src->type, dst->type, hlen);\
2703 } while (/*CONSTCOND*/ 0)
2706 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2708 if (dst == NULL || src == NULL) {
2709 printf("ip6_clearpktopts: invalid argument\n");
2713 dst->ip6po_hlim = src->ip6po_hlim;
2714 dst->ip6po_tclass = src->ip6po_tclass;
2715 dst->ip6po_flags = src->ip6po_flags;
2716 dst->ip6po_minmtu = src->ip6po_minmtu;
2717 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2718 if (src->ip6po_pktinfo) {
2719 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2721 if (dst->ip6po_pktinfo == NULL)
2723 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2725 if (src->ip6po_nexthop) {
2726 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2728 if (dst->ip6po_nexthop == NULL)
2730 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2731 src->ip6po_nexthop->sa_len);
2733 PKTOPT_EXTHDRCPY(ip6po_hbh);
2734 PKTOPT_EXTHDRCPY(ip6po_dest1);
2735 PKTOPT_EXTHDRCPY(ip6po_dest2);
2736 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2740 ip6_clearpktopts(dst, -1);
2743 #undef PKTOPT_EXTHDRCPY
2745 struct ip6_pktopts *
2746 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2749 struct ip6_pktopts *dst;
2751 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2754 ip6_initpktopts(dst);
2756 if ((error = copypktopts(dst, src, canwait)) != 0) {
2757 free(dst, M_IP6OPT);
2765 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2770 ip6_clearpktopts(pktopt, -1);
2772 free(pktopt, M_IP6OPT);
2776 * Set IPv6 outgoing packet options based on advanced API.
2779 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2780 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2782 struct cmsghdr *cm = NULL;
2784 if (control == NULL || opt == NULL)
2788 * ip6_setpktopt can call ifnet_byindex(), so it's imperative that we
2789 * are in the network epoch here.
2793 ip6_initpktopts(opt);
2798 * If stickyopt is provided, make a local copy of the options
2799 * for this particular packet, then override them by ancillary
2801 * XXX: copypktopts() does not copy the cached route to a next
2802 * hop (if any). This is not very good in terms of efficiency,
2803 * but we can allow this since this option should be rarely
2806 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2811 * XXX: Currently, we assume all the optional information is stored
2814 if (control->m_next)
2817 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2818 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2821 if (control->m_len < CMSG_LEN(0))
2824 cm = mtod(control, struct cmsghdr *);
2825 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2827 if (cm->cmsg_level != IPPROTO_IPV6)
2830 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2831 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2840 * Set a particular packet option, as a sticky option or an ancillary data
2841 * item. "len" can be 0 only when it's a sticky option.
2842 * We have 4 cases of combination of "sticky" and "cmsg":
2843 * "sticky=0, cmsg=0": impossible
2844 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2845 * "sticky=1, cmsg=0": RFC3542 socket option
2846 * "sticky=1, cmsg=1": RFC2292 socket option
2849 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2850 struct ucred *cred, int sticky, int cmsg, int uproto)
2852 int minmtupolicy, preftemp;
2857 if (!sticky && !cmsg) {
2859 printf("ip6_setpktopt: impossible case\n");
2865 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2866 * not be specified in the context of RFC3542. Conversely,
2867 * RFC3542 types should not be specified in the context of RFC2292.
2871 case IPV6_2292PKTINFO:
2872 case IPV6_2292HOPLIMIT:
2873 case IPV6_2292NEXTHOP:
2874 case IPV6_2292HOPOPTS:
2875 case IPV6_2292DSTOPTS:
2876 case IPV6_2292RTHDR:
2877 case IPV6_2292PKTOPTIONS:
2878 return (ENOPROTOOPT);
2881 if (sticky && cmsg) {
2888 case IPV6_RTHDRDSTOPTS:
2890 case IPV6_USE_MIN_MTU:
2893 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2894 return (ENOPROTOOPT);
2899 case IPV6_2292PKTINFO:
2902 struct ifnet *ifp = NULL;
2903 struct in6_pktinfo *pktinfo;
2905 if (len != sizeof(struct in6_pktinfo))
2908 pktinfo = (struct in6_pktinfo *)buf;
2911 * An application can clear any sticky IPV6_PKTINFO option by
2912 * doing a "regular" setsockopt with ipi6_addr being
2913 * in6addr_any and ipi6_ifindex being zero.
2914 * [RFC 3542, Section 6]
2916 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2917 pktinfo->ipi6_ifindex == 0 &&
2918 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2919 ip6_clearpktopts(opt, optname);
2923 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2924 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2927 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2929 /* validate the interface index if specified. */
2930 if (pktinfo->ipi6_ifindex) {
2931 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2935 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2936 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2940 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2941 struct in6_ifaddr *ia;
2943 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2944 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2946 return (EADDRNOTAVAIL);
2947 ifa_free(&ia->ia_ifa);
2950 * We store the address anyway, and let in6_selectsrc()
2951 * validate the specified address. This is because ipi6_addr
2952 * may not have enough information about its scope zone, and
2953 * we may need additional information (such as outgoing
2954 * interface or the scope zone of a destination address) to
2955 * disambiguate the scope.
2956 * XXX: the delay of the validation may confuse the
2957 * application when it is used as a sticky option.
2959 if (opt->ip6po_pktinfo == NULL) {
2960 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2961 M_IP6OPT, M_NOWAIT);
2962 if (opt->ip6po_pktinfo == NULL)
2965 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2969 case IPV6_2292HOPLIMIT:
2975 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2976 * to simplify the ordering among hoplimit options.
2978 if (optname == IPV6_HOPLIMIT && sticky)
2979 return (ENOPROTOOPT);
2981 if (len != sizeof(int))
2984 if (*hlimp < -1 || *hlimp > 255)
2987 opt->ip6po_hlim = *hlimp;
2995 if (len != sizeof(int))
2997 tclass = *(int *)buf;
2998 if (tclass < -1 || tclass > 255)
3001 opt->ip6po_tclass = tclass;
3005 case IPV6_2292NEXTHOP:
3008 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3013 if (len == 0) { /* just remove the option */
3014 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3018 /* check if cmsg_len is large enough for sa_len */
3019 if (len < sizeof(struct sockaddr) || len < *buf)
3022 switch (((struct sockaddr *)buf)->sa_family) {
3025 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3028 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3031 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3032 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3035 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
3041 case AF_LINK: /* should eventually be supported */
3043 return (EAFNOSUPPORT);
3046 /* turn off the previous option, then set the new option. */
3047 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3048 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3049 if (opt->ip6po_nexthop == NULL)
3051 bcopy(buf, opt->ip6po_nexthop, *buf);
3054 case IPV6_2292HOPOPTS:
3057 struct ip6_hbh *hbh;
3061 * XXX: We don't allow a non-privileged user to set ANY HbH
3062 * options, since per-option restriction has too much
3066 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3072 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3073 break; /* just remove the option */
3076 /* message length validation */
3077 if (len < sizeof(struct ip6_hbh))
3079 hbh = (struct ip6_hbh *)buf;
3080 hbhlen = (hbh->ip6h_len + 1) << 3;
3084 /* turn off the previous option, then set the new option. */
3085 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3086 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3087 if (opt->ip6po_hbh == NULL)
3089 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3094 case IPV6_2292DSTOPTS:
3096 case IPV6_RTHDRDSTOPTS:
3098 struct ip6_dest *dest, **newdest = NULL;
3101 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
3102 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3108 ip6_clearpktopts(opt, optname);
3109 break; /* just remove the option */
3112 /* message length validation */
3113 if (len < sizeof(struct ip6_dest))
3115 dest = (struct ip6_dest *)buf;
3116 destlen = (dest->ip6d_len + 1) << 3;
3121 * Determine the position that the destination options header
3122 * should be inserted; before or after the routing header.
3125 case IPV6_2292DSTOPTS:
3127 * The old advacned API is ambiguous on this point.
3128 * Our approach is to determine the position based
3129 * according to the existence of a routing header.
3130 * Note, however, that this depends on the order of the
3131 * extension headers in the ancillary data; the 1st
3132 * part of the destination options header must appear
3133 * before the routing header in the ancillary data,
3135 * RFC3542 solved the ambiguity by introducing
3136 * separate ancillary data or option types.
3138 if (opt->ip6po_rthdr == NULL)
3139 newdest = &opt->ip6po_dest1;
3141 newdest = &opt->ip6po_dest2;
3143 case IPV6_RTHDRDSTOPTS:
3144 newdest = &opt->ip6po_dest1;
3147 newdest = &opt->ip6po_dest2;
3151 /* turn off the previous option, then set the new option. */
3152 ip6_clearpktopts(opt, optname);
3153 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3154 if (*newdest == NULL)
3156 bcopy(dest, *newdest, destlen);
3161 case IPV6_2292RTHDR:
3164 struct ip6_rthdr *rth;
3168 ip6_clearpktopts(opt, IPV6_RTHDR);
3169 break; /* just remove the option */
3172 /* message length validation */
3173 if (len < sizeof(struct ip6_rthdr))
3175 rth = (struct ip6_rthdr *)buf;
3176 rthlen = (rth->ip6r_len + 1) << 3;
3180 switch (rth->ip6r_type) {
3181 case IPV6_RTHDR_TYPE_0:
3182 if (rth->ip6r_len == 0) /* must contain one addr */
3184 if (rth->ip6r_len % 2) /* length must be even */
3186 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3190 return (EINVAL); /* not supported */
3193 /* turn off the previous option */
3194 ip6_clearpktopts(opt, IPV6_RTHDR);
3195 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3196 if (opt->ip6po_rthdr == NULL)
3198 bcopy(rth, opt->ip6po_rthdr, rthlen);
3203 case IPV6_USE_MIN_MTU:
3204 if (len != sizeof(int))
3206 minmtupolicy = *(int *)buf;
3207 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3208 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3209 minmtupolicy != IP6PO_MINMTU_ALL) {
3212 opt->ip6po_minmtu = minmtupolicy;
3216 if (len != sizeof(int))
3219 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3221 * we ignore this option for TCP sockets.
3222 * (RFC3542 leaves this case unspecified.)
3224 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3226 opt->ip6po_flags |= IP6PO_DONTFRAG;
3229 case IPV6_PREFER_TEMPADDR:
3230 if (len != sizeof(int))
3232 preftemp = *(int *)buf;
3233 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3234 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3235 preftemp != IP6PO_TEMPADDR_PREFER) {
3238 opt->ip6po_prefer_tempaddr = preftemp;
3242 return (ENOPROTOOPT);
3243 } /* end of switch */
3249 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3250 * packet to the input queue of a specified interface. Note that this
3251 * calls the output routine of the loopback "driver", but with an interface
3252 * pointer that might NOT be &loif -- easier than replicating that code here.
3255 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3258 struct ip6_hdr *ip6;
3260 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3265 * Make sure to deep-copy IPv6 header portion in case the data
3266 * is in an mbuf cluster, so that we can safely override the IPv6
3267 * header portion later.
3269 if (!M_WRITABLE(copym) ||
3270 copym->m_len < sizeof(struct ip6_hdr)) {
3271 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3275 ip6 = mtod(copym, struct ip6_hdr *);
3277 * clear embedded scope identifiers if necessary.
3278 * in6_clearscope will touch the addresses only when necessary.
3280 in6_clearscope(&ip6->ip6_src);
3281 in6_clearscope(&ip6->ip6_dst);
3282 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3283 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3285 copym->m_pkthdr.csum_data = 0xffff;
3287 if_simloop(ifp, copym, AF_INET6, 0);
3291 * Chop IPv6 header off from the payload.
3294 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3297 struct ip6_hdr *ip6;
3299 ip6 = mtod(m, struct ip6_hdr *);
3300 if (m->m_len > sizeof(*ip6)) {
3301 mh = m_gethdr(M_NOWAIT, MT_DATA);
3306 m_move_pkthdr(mh, m);
3307 M_ALIGN(mh, sizeof(*ip6));
3308 m->m_len -= sizeof(*ip6);
3309 m->m_data += sizeof(*ip6);
3312 m->m_len = sizeof(*ip6);
3313 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3315 exthdrs->ip6e_ip6 = m;
3320 * Compute IPv6 extension header length.
3323 ip6_optlen(struct inpcb *inp)
3327 if (!inp->in6p_outputopts)
3332 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3334 len += elen(inp->in6p_outputopts->ip6po_hbh);
3335 if (inp->in6p_outputopts->ip6po_rthdr)
3336 /* dest1 is valid with rthdr only */
3337 len += elen(inp->in6p_outputopts->ip6po_dest1);
3338 len += elen(inp->in6p_outputopts->ip6po_rthdr);
3339 len += elen(inp->in6p_outputopts->ip6po_dest2);