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>
67 #include "opt_inet6.h"
68 #include "opt_ipsec.h"
69 #include "opt_kern_tls.h"
70 #include "opt_ratelimit.h"
71 #include "opt_route.h"
75 #include <sys/param.h>
76 #include <sys/kernel.h>
78 #include <sys/malloc.h>
80 #include <sys/errno.h>
83 #include <sys/protosw.h>
84 #include <sys/socket.h>
85 #include <sys/socketvar.h>
86 #include <sys/syslog.h>
87 #include <sys/ucred.h>
89 #include <machine/in_cksum.h>
92 #include <net/if_var.h>
93 #include <net/if_private.h>
94 #include <net/if_vlan_var.h>
95 #include <net/if_llatbl.h>
96 #include <net/ethernet.h>
97 #include <net/netisr.h>
98 #include <net/route.h>
99 #include <net/route/nhop.h>
100 #include <net/pfil.h>
101 #include <net/rss_config.h>
102 #include <net/vnet.h>
104 #include <netinet/in.h>
105 #include <netinet/in_var.h>
106 #include <netinet/ip_var.h>
107 #include <netinet6/in6_fib.h>
108 #include <netinet6/in6_var.h>
109 #include <netinet/ip6.h>
110 #include <netinet/icmp6.h>
111 #include <netinet6/ip6_var.h>
112 #include <netinet/in_pcb.h>
113 #include <netinet/tcp_var.h>
114 #include <netinet6/nd6.h>
115 #include <netinet6/in6_rss.h>
117 #include <netipsec/ipsec_support.h>
118 #if defined(SCTP) || defined(SCTP_SUPPORT)
119 #include <netinet/sctp.h>
120 #include <netinet/sctp_crc32.h>
123 #include <netinet6/scope6_var.h>
125 extern int in6_mcast_loop;
128 struct mbuf *ip6e_ip6;
129 struct mbuf *ip6e_hbh;
130 struct mbuf *ip6e_dest1;
131 struct mbuf *ip6e_rthdr;
132 struct mbuf *ip6e_dest2;
135 static MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
137 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
138 struct ucred *, int);
139 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
140 struct socket *, struct sockopt *);
141 static int ip6_getpcbopt(struct inpcb *, int, struct sockopt *);
142 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
143 struct ucred *, int, int, int);
145 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
146 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
148 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
149 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
150 static int ip6_getpmtu(struct route_in6 *, int,
151 struct ifnet *, const struct in6_addr *, u_long *, int *, u_int,
153 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
154 u_long *, int *, u_int);
155 static int ip6_getpmtu_ctl(u_int, const struct in6_addr *, u_long *);
156 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
159 * Make an extension header from option data. hp is the source,
160 * mp is the destination, and _ol is the optlen.
162 #define MAKE_EXTHDR(hp, mp, _ol) \
165 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
166 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
167 ((eh)->ip6e_len + 1) << 3); \
170 (_ol) += (*(mp))->m_len; \
172 } while (/*CONSTCOND*/ 0)
175 * Form a chain of extension headers.
176 * m is the extension header mbuf
177 * mp is the previous mbuf in the chain
178 * p is the next header
179 * i is the type of option.
181 #define MAKE_CHAIN(m, mp, p, i)\
185 panic("%s:%d: assumption failed: "\
186 "hdr not split: hdrsplit %d exthdrs %p",\
187 __func__, __LINE__, hdrsplit, &exthdrs);\
188 *mtod((m), u_char *) = *(p);\
190 p = mtod((m), u_char *);\
191 (m)->m_next = (mp)->m_next;\
195 } while (/*CONSTCOND*/ 0)
198 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
202 csum = in_cksum_skip(m, offset + plen, offset);
203 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
205 offset += m->m_pkthdr.csum_data; /* checksum offset */
207 if (offset + sizeof(csum) > m->m_len)
208 m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
210 *(u_short *)mtodo(m, offset) = csum;
214 ip6_output_delayed_csum(struct mbuf *m, struct ifnet *ifp, int csum_flags,
215 int plen, int optlen)
218 KASSERT((plen >= optlen), ("%s:%d: plen %d < optlen %d, m %p, ifp %p "
220 __func__, __LINE__, plen, optlen, m, ifp, csum_flags));
222 if (csum_flags & CSUM_DELAY_DATA_IPV6) {
223 in6_delayed_cksum(m, plen - optlen,
224 sizeof(struct ip6_hdr) + optlen);
225 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
227 #if defined(SCTP) || defined(SCTP_SUPPORT)
228 if (csum_flags & CSUM_SCTP_IPV6) {
229 sctp_delayed_cksum(m, sizeof(struct ip6_hdr) + optlen);
230 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
236 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
237 int fraglen , uint32_t id)
239 struct mbuf *m, **mnext, *m_frgpart;
240 struct ip6_hdr *ip6, *mhip6;
241 struct ip6_frag *ip6f;
244 int tlen = m0->m_pkthdr.len;
246 KASSERT((fraglen % 8 == 0), ("Fragment length must be a multiple of 8"));
249 ip6 = mtod(m, struct ip6_hdr *);
250 mnext = &m->m_nextpkt;
252 for (off = hlen; off < tlen; off += fraglen) {
253 m = m_gethdr(M_NOWAIT, MT_DATA);
255 IP6STAT_INC(ip6s_odropped);
260 * Make sure the complete packet header gets copied
261 * from the originating mbuf to the newly created
262 * mbuf. This also ensures that existing firewall
263 * classification(s), VLAN tags and so on get copied
264 * to the resulting fragmented packet(s):
266 if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
268 IP6STAT_INC(ip6s_odropped);
273 mnext = &m->m_nextpkt;
274 m->m_data += max_linkhdr;
275 mhip6 = mtod(m, struct ip6_hdr *);
277 m->m_len = sizeof(*mhip6);
278 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
280 IP6STAT_INC(ip6s_odropped);
283 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
284 if (off + fraglen >= tlen)
285 fraglen = tlen - off;
287 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
288 mhip6->ip6_plen = htons((u_short)(fraglen + hlen +
289 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
290 if ((m_frgpart = m_copym(m0, off, fraglen, M_NOWAIT)) == NULL) {
291 IP6STAT_INC(ip6s_odropped);
295 m->m_pkthdr.len = fraglen + hlen + sizeof(*ip6f);
296 ip6f->ip6f_reserved = 0;
297 ip6f->ip6f_ident = id;
298 ip6f->ip6f_nxt = nextproto;
299 IP6STAT_INC(ip6s_ofragments);
300 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
307 ip6_output_send(struct inpcb *inp, struct ifnet *ifp, struct ifnet *origifp,
308 struct mbuf *m, struct sockaddr_in6 *dst, struct route_in6 *ro,
312 struct ktls_session *tls = NULL;
314 struct m_snd_tag *mst;
317 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
322 * If this is an unencrypted TLS record, save a reference to
323 * the record. This local reference is used to call
324 * ktls_output_eagain after the mbuf has been freed (thus
325 * dropping the mbuf's reference) in if_output.
327 if (m->m_next != NULL && mbuf_has_tls_session(m->m_next)) {
328 tls = ktls_hold(m->m_next->m_epg_tls);
332 * If a TLS session doesn't have a valid tag, it must
333 * have had an earlier ifp mismatch, so drop this
342 * Always stamp tags that include NIC ktls.
348 if (inp != NULL && mst == NULL) {
349 if ((inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) != 0 ||
350 (inp->inp_snd_tag != NULL &&
351 inp->inp_snd_tag->ifp != ifp))
352 in_pcboutput_txrtlmt(inp, ifp, m);
354 if (inp->inp_snd_tag != NULL)
355 mst = inp->inp_snd_tag;
358 if (stamp_tag && mst != NULL) {
359 KASSERT(m->m_pkthdr.rcvif == NULL,
360 ("trying to add a send tag to a forwarded packet"));
361 if (mst->ifp != ifp) {
367 /* stamp send tag on mbuf */
368 m->m_pkthdr.snd_tag = m_snd_tag_ref(mst);
369 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
372 error = nd6_output_ifp(ifp, origifp, m, dst, (struct route *)ro);
375 /* Check for route change invalidating send tags. */
379 error = ktls_output_eagain(inp, tls);
385 in_pcboutput_eagain(inp);
392 * The packet in mbuf chain m contains a skeletal IP6 header (with pri, len,
393 * nxt, hlim, src, dst).
394 * This function may modify ver and hlim only.
395 * The mbuf chain containing the packet will be freed.
396 * The mbuf opt, if present, will not be freed.
397 * If route_in6 ro is present and has ro_nh initialized, route lookup would be
398 * skipped and ro->ro_nh would be used. If ro is present but ro->ro_nh is NULL,
399 * then result of route lookup is stored in ro->ro_nh.
401 * Type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and nd_ifinfo.linkmtu
402 * is uint32_t. So we use u_long to hold largest one, which is rt_mtu.
404 * ifpp - XXX: just for statistics
407 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
408 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
409 struct ifnet **ifpp, struct inpcb *inp)
412 struct ifnet *ifp, *origifp;
415 struct route_in6 *ro_pmtu;
416 struct nhop_object *nh;
417 struct sockaddr_in6 *dst, sin6, src_sa, dst_sa;
418 struct in6_addr odst;
423 struct in6_ifaddr *ia = NULL;
425 int alwaysfrag, dontfrag;
426 u_int32_t optlen, plen = 0, unfragpartlen;
427 struct ip6_exthdrs exthdrs;
428 struct in6_addr src0, dst0;
434 struct m_tag *fwd_tag = NULL;
440 INP_LOCK_ASSERT(inp);
441 M_SETFIB(m, inp->inp_inc.inc_fibnum);
442 if ((flags & IP_NODEFAULTFLOWID) == 0) {
443 /* Unconditionally set flowid. */
444 m->m_pkthdr.flowid = inp->inp_flowid;
445 M_HASHTYPE_SET(m, inp->inp_flowtype);
447 if ((inp->inp_flags2 & INP_2PCP_SET) != 0)
448 vlan_pcp = (inp->inp_flags2 & INP_2PCP_MASK) >>
451 m->m_pkthdr.numa_domain = inp->inp_numa_domain;
455 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
457 * IPSec checking which handles several cases.
458 * FAST IPSEC: We re-injected the packet.
459 * XXX: need scope argument.
461 if (IPSEC_ENABLED(ipv6)) {
462 m = mb_unmapped_to_ext(m);
464 IP6STAT_INC(ip6s_odropped);
468 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
469 if (error == EINPROGRESS)
476 /* Source address validation. */
477 ip6 = mtod(m, struct ip6_hdr *);
478 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
479 (flags & IPV6_UNSPECSRC) == 0) {
481 IP6STAT_INC(ip6s_badscope);
484 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
486 IP6STAT_INC(ip6s_badscope);
491 * If we are given packet options to add extension headers prepare them.
492 * Calculate the total length of the extension header chain.
493 * Keep the length of the unfragmentable part for fragmentation.
495 bzero(&exthdrs, sizeof(exthdrs));
497 unfragpartlen = sizeof(struct ip6_hdr);
499 /* Hop-by-Hop options header. */
500 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh, optlen);
502 /* Destination options header (1st part). */
503 if (opt->ip6po_rthdr) {
504 #ifndef RTHDR_SUPPORT_IMPLEMENTED
506 * If there is a routing header, discard the packet
507 * right away here. RH0/1 are obsolete and we do not
508 * currently support RH2/3/4.
509 * People trying to use RH253/254 may want to disable
511 * The moment we do support any routing header (again)
512 * this block should check the routing type more
520 * Destination options header (1st part).
521 * This only makes sense with a routing header.
522 * See Section 9.2 of RFC 3542.
523 * Disabling this part just for MIP6 convenience is
524 * a bad idea. We need to think carefully about a
525 * way to make the advanced API coexist with MIP6
526 * options, which might automatically be inserted in
529 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1,
532 /* Routing header. */
533 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr, optlen);
535 unfragpartlen += optlen;
538 * NOTE: we don't add AH/ESP length here (done in
539 * ip6_ipsec_output()).
542 /* Destination options header (2nd part). */
543 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2, optlen);
547 * If there is at least one extension header,
548 * separate IP6 header from the payload.
552 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
556 m = exthdrs.ip6e_ip6;
557 ip6 = mtod(m, struct ip6_hdr *);
561 /* Adjust mbuf packet header length. */
562 m->m_pkthdr.len += optlen;
563 plen = m->m_pkthdr.len - sizeof(*ip6);
565 /* If this is a jumbo payload, insert a jumbo payload option. */
566 if (plen > IPV6_MAXPACKET) {
568 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
572 m = exthdrs.ip6e_ip6;
573 ip6 = mtod(m, struct ip6_hdr *);
576 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
580 ip6->ip6_plen = htons(plen);
581 nexthdrp = &ip6->ip6_nxt;
585 * Concatenate headers and fill in next header fields.
586 * Here we have, on "m"
588 * and we insert headers accordingly.
589 * Finally, we should be getting:
590 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload].
592 * During the header composing process "m" points to IPv6
593 * header. "mprev" points to an extension header prior to esp.
598 * We treat dest2 specially. This makes IPsec processing
599 * much easier. The goal here is to make mprev point the
600 * mbuf prior to dest2.
602 * Result: IPv6 dest2 payload.
603 * m and mprev will point to IPv6 header.
605 if (exthdrs.ip6e_dest2) {
607 panic("%s:%d: assumption failed: "
608 "hdr not split: hdrsplit %d exthdrs %p",
609 __func__, __LINE__, hdrsplit, &exthdrs);
610 exthdrs.ip6e_dest2->m_next = m->m_next;
611 m->m_next = exthdrs.ip6e_dest2;
612 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
613 ip6->ip6_nxt = IPPROTO_DSTOPTS;
617 * Result: IPv6 hbh dest1 rthdr dest2 payload.
618 * m will point to IPv6 header. mprev will point to the
619 * extension header prior to dest2 (rthdr in the above case).
621 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
622 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
624 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
628 IP6STAT_INC(ip6s_localout);
632 if (opt && opt->ip6po_rthdr)
633 ro = &opt->ip6po_route;
635 dst = (struct sockaddr_in6 *)&ro->ro_dst;
638 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
642 * If specified, try to fill in the traffic class field.
643 * Do not override if a non-zero value is already set.
644 * We check the diffserv field and the ECN field separately.
646 if (opt && opt->ip6po_tclass >= 0) {
649 if (IPV6_DSCP(ip6) == 0)
651 if (IPV6_ECN(ip6) == 0)
654 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
657 /* Fill in or override the hop limit field, if necessary. */
658 if (opt && opt->ip6po_hlim != -1)
659 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
660 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
662 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
664 ip6->ip6_hlim = V_ip6_defmcasthlim;
667 if (ro == NULL || ro->ro_nh == NULL) {
668 bzero(dst, sizeof(*dst));
669 dst->sin6_family = AF_INET6;
670 dst->sin6_len = sizeof(*dst);
671 dst->sin6_addr = ip6->ip6_dst;
674 * Validate route against routing table changes.
675 * Make sure that the address family is set in route.
681 if (ro->ro_nh != NULL && inp != NULL) {
682 ro->ro_dst.sin6_family = AF_INET6; /* XXX KASSERT? */
683 NH_VALIDATE((struct route *)ro, &inp->inp_rt_cookie,
686 if (ro->ro_nh != NULL && fwd_tag == NULL &&
687 (!NH_IS_VALID(ro->ro_nh) ||
688 ro->ro_dst.sin6_family != AF_INET6 ||
689 !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)))
690 RO_INVALIDATE_CACHE(ro);
692 if (ro->ro_nh != NULL && fwd_tag == NULL &&
693 ro->ro_dst.sin6_family == AF_INET6 &&
694 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
695 /* Nexthop is valid and contains valid ifp */
699 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
701 if (fwd_tag == NULL) {
702 bzero(&dst_sa, sizeof(dst_sa));
703 dst_sa.sin6_family = AF_INET6;
704 dst_sa.sin6_len = sizeof(dst_sa);
705 dst_sa.sin6_addr = ip6->ip6_dst;
707 error = in6_selectroute(&dst_sa, opt, im6o, ro, &ifp,
708 &nh, fibnum, m->m_pkthdr.flowid);
710 IP6STAT_INC(ip6s_noroute);
712 in6_ifstat_inc(ifp, ifs6_out_discard);
716 * At this point at least @ifp is not NULL
717 * Can be the case when dst is multicast, link-local or
718 * interface is explicitly specificed by the caller.
723 * If in6_selectroute() does not return a nexthop
724 * dst may not have been updated.
726 *dst = dst_sa; /* XXX */
731 origifp = nh->nh_aifp;
732 ia = (struct in6_ifaddr *)(nh->nh_ifa);
733 counter_u64_add(nh->nh_pksent, 1);
736 struct nhop_object *nh;
737 struct in6_addr kdst;
740 if (fwd_tag == NULL) {
741 bzero(&dst_sa, sizeof(dst_sa));
742 dst_sa.sin6_family = AF_INET6;
743 dst_sa.sin6_len = sizeof(dst_sa);
744 dst_sa.sin6_addr = ip6->ip6_dst;
747 if (IN6_IS_ADDR_MULTICAST(&dst_sa.sin6_addr) &&
749 (ifp = im6o->im6o_multicast_ifp) != NULL) {
750 /* We do not need a route lookup. */
751 *dst = dst_sa; /* XXX */
756 in6_splitscope(&dst_sa.sin6_addr, &kdst, &scopeid);
758 if (IN6_IS_ADDR_MC_LINKLOCAL(&dst_sa.sin6_addr) ||
759 IN6_IS_ADDR_MC_NODELOCAL(&dst_sa.sin6_addr)) {
761 ifp = in6_getlinkifnet(scopeid);
763 error = EHOSTUNREACH;
766 *dst = dst_sa; /* XXX */
772 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE,
775 IP6STAT_INC(ip6s_noroute);
776 /* No ifp in6_ifstat_inc(ifp, ifs6_out_discard); */
777 error = EHOSTUNREACH;
782 origifp = nh->nh_aifp;
783 ia = ifatoia6(nh->nh_ifa);
784 if (nh->nh_flags & NHF_GATEWAY)
785 dst->sin6_addr = nh->gw6_sa.sin6_addr;
786 else if (fwd_tag != NULL)
787 dst->sin6_addr = dst_sa.sin6_addr;
792 * At this point ifp MUST be pointing to the valid transmit ifp.
793 * origifp MUST be valid and pointing to either the same ifp or,
794 * in case of loopback output, to the interface which ip6_src
797 * fe80::1%em0 -> fe80::2%em0 -> ifp=em0, origifp=em0
798 * fe80::1%em0 -> fe80::1%em0 -> ifp=lo0, origifp=em0
799 * ::1 -> ::1 -> ifp=lo0, origifp=lo0
801 * mtu can be 0 and will be refined later.
803 KASSERT((ifp != NULL), ("output interface must not be NULL"));
804 KASSERT((origifp != NULL), ("output address interface must not be NULL"));
806 if ((flags & IPV6_FORWARDING) == 0) {
807 /* XXX: the FORWARDING flag can be set for mrouting. */
808 in6_ifstat_inc(ifp, ifs6_out_request);
811 /* Setup data structures for scope ID checks. */
813 bzero(&src_sa, sizeof(src_sa));
814 src_sa.sin6_family = AF_INET6;
815 src_sa.sin6_len = sizeof(src_sa);
816 src_sa.sin6_addr = ip6->ip6_src;
819 /* Re-initialize to be sure. */
820 bzero(&dst_sa, sizeof(dst_sa));
821 dst_sa.sin6_family = AF_INET6;
822 dst_sa.sin6_len = sizeof(dst_sa);
823 dst_sa.sin6_addr = ip6->ip6_dst;
825 /* Check for valid scope ID. */
826 if (in6_setscope(&src0, origifp, &zone) == 0 &&
827 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
828 in6_setscope(&dst0, origifp, &zone) == 0 &&
829 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
831 * The outgoing interface is in the zone of the source
832 * and destination addresses.
835 } else if ((origifp->if_flags & IFF_LOOPBACK) == 0 ||
836 sa6_recoverscope(&src_sa) != 0 ||
837 sa6_recoverscope(&dst_sa) != 0 ||
838 dst_sa.sin6_scope_id == 0 ||
839 (src_sa.sin6_scope_id != 0 &&
840 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
841 ifnet_byindex(dst_sa.sin6_scope_id) == NULL) {
843 * If the destination network interface is not a
844 * loopback interface, or the destination network
845 * address has no scope ID, or the source address has
846 * a scope ID set which is different from the
847 * destination address one, or there is no network
848 * interface representing this scope ID, the address
849 * pair is considered invalid.
851 IP6STAT_INC(ip6s_badscope);
852 in6_ifstat_inc(origifp, ifs6_out_discard);
854 error = EHOSTUNREACH; /* XXX */
857 /* All scope ID checks are successful. */
859 if (nh && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
860 if (opt && opt->ip6po_nextroute.ro_nh) {
862 * The nexthop is explicitly specified by the
863 * application. We assume the next hop is an IPv6
866 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
868 else if ((nh->nh_flags & NHF_GATEWAY))
872 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
873 m->m_flags &= ~(M_BCAST | M_MCAST); /* Just in case. */
875 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
876 in6_ifstat_inc(ifp, ifs6_out_mcast);
878 /* Confirm that the outgoing interface supports multicast. */
879 if (!(ifp->if_flags & IFF_MULTICAST)) {
880 IP6STAT_INC(ip6s_noroute);
881 in6_ifstat_inc(ifp, ifs6_out_discard);
885 if ((im6o == NULL && in6_mcast_loop) ||
886 (im6o && im6o->im6o_multicast_loop)) {
888 * Loop back multicast datagram if not expressly
889 * forbidden to do so, even if we have not joined
890 * the address; protocols will filter it later,
891 * thus deferring a hash lookup and lock acquisition
892 * at the expense of an m_copym().
894 ip6_mloopback(ifp, m);
897 * If we are acting as a multicast router, perform
898 * multicast forwarding as if the packet had just
899 * arrived on the interface to which we are about
900 * to send. The multicast forwarding function
901 * recursively calls this function, using the
902 * IPV6_FORWARDING flag to prevent infinite recursion.
904 * Multicasts that are looped back by ip6_mloopback(),
905 * above, will be forwarded by the ip6_input() routine,
908 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
910 * XXX: ip6_mforward expects that rcvif is NULL
911 * when it is called from the originating path.
912 * However, it may not always be the case.
914 m->m_pkthdr.rcvif = NULL;
915 if (ip6_mforward(ip6, ifp, m) != 0) {
922 * Multicasts with a hoplimit of zero may be looped back,
923 * above, but must not be transmitted on a network.
924 * Also, multicasts addressed to the loopback interface
925 * are not sent -- the above call to ip6_mloopback() will
926 * loop back a copy if this host actually belongs to the
927 * destination group on the loopback interface.
929 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
930 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
937 * Fill the outgoing inteface to tell the upper layer
938 * to increment per-interface statistics.
943 /* Determine path MTU. */
944 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
945 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
947 KASSERT(mtu > 0, ("%s:%d: mtu %ld, ro_pmtu %p ro %p ifp %p "
948 "alwaysfrag %d fibnum %u\n", __func__, __LINE__, mtu, ro_pmtu, ro,
949 ifp, alwaysfrag, fibnum));
952 * The caller of this function may specify to use the minimum MTU
954 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
955 * setting. The logic is a bit complicated; by default, unicast
956 * packets will follow path MTU while multicast packets will be sent at
957 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
958 * including unicast ones will be sent at the minimum MTU. Multicast
959 * packets will always be sent at the minimum MTU unless
960 * IP6PO_MINMTU_DISABLE is explicitly specified.
961 * See RFC 3542 for more details.
963 if (mtu > IPV6_MMTU) {
964 if ((flags & IPV6_MINMTU))
966 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
968 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
970 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
976 * Clear embedded scope identifiers if necessary.
977 * in6_clearscope() will touch the addresses only when necessary.
979 in6_clearscope(&ip6->ip6_src);
980 in6_clearscope(&ip6->ip6_dst);
983 * If the outgoing packet contains a hop-by-hop options header,
984 * it must be examined and processed even by the source node.
985 * (RFC 2460, section 4.)
987 if (exthdrs.ip6e_hbh) {
988 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
989 u_int32_t dummy; /* XXX unused */
990 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
993 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
994 panic("ip6e_hbh is not contiguous");
997 * XXX: if we have to send an ICMPv6 error to the sender,
998 * we need the M_LOOP flag since icmp6_error() expects
999 * the IPv6 and the hop-by-hop options header are
1000 * contiguous unless the flag is set.
1002 m->m_flags |= M_LOOP;
1003 m->m_pkthdr.rcvif = ifp;
1004 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
1005 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
1006 &dummy, &plen) < 0) {
1007 /* m was already freed at this point. */
1008 error = EINVAL;/* better error? */
1011 m->m_flags &= ~M_LOOP; /* XXX */
1012 m->m_pkthdr.rcvif = NULL;
1015 /* Jump over all PFIL processing if hooks are not active. */
1016 if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
1019 odst = ip6->ip6_dst;
1020 /* Run through list of hooks for output packets. */
1021 switch (pfil_mbuf_out(V_inet6_pfil_head, &m, ifp, inp)) {
1023 ip6 = mtod(m, struct ip6_hdr *);
1033 /* See if destination IP address was changed by packet filter. */
1034 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
1035 m->m_flags |= M_SKIP_FIREWALL;
1036 /* If destination is now ourself drop to ip6_input(). */
1037 if (in6_localip(&ip6->ip6_dst)) {
1038 m->m_flags |= M_FASTFWD_OURS;
1039 if (m->m_pkthdr.rcvif == NULL)
1040 m->m_pkthdr.rcvif = V_loif;
1041 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1042 m->m_pkthdr.csum_flags |=
1043 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1044 m->m_pkthdr.csum_data = 0xffff;
1046 #if defined(SCTP) || defined(SCTP_SUPPORT)
1047 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1048 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1050 error = netisr_queue(NETISR_IPV6, m);
1054 RO_INVALIDATE_CACHE(ro);
1055 needfiblookup = 1; /* Redo the routing table lookup. */
1058 /* See if fib was changed by packet filter. */
1059 if (fibnum != M_GETFIB(m)) {
1060 m->m_flags |= M_SKIP_FIREWALL;
1061 fibnum = M_GETFIB(m);
1063 RO_INVALIDATE_CACHE(ro);
1069 /* See if local, if yes, send it to netisr. */
1070 if (m->m_flags & M_FASTFWD_OURS) {
1071 if (m->m_pkthdr.rcvif == NULL)
1072 m->m_pkthdr.rcvif = V_loif;
1073 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1074 m->m_pkthdr.csum_flags |=
1075 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1076 m->m_pkthdr.csum_data = 0xffff;
1078 #if defined(SCTP) || defined(SCTP_SUPPORT)
1079 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1080 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1082 error = netisr_queue(NETISR_IPV6, m);
1085 /* Or forward to some other address? */
1086 if ((m->m_flags & M_IP6_NEXTHOP) &&
1087 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
1089 dst = (struct sockaddr_in6 *)&ro->ro_dst;
1092 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
1093 m->m_flags |= M_SKIP_FIREWALL;
1094 m->m_flags &= ~M_IP6_NEXTHOP;
1095 m_tag_delete(m, fwd_tag);
1101 EVL_APPLY_PRI(m, vlan_pcp);
1103 /* Ensure the packet data is mapped if the interface requires it. */
1104 if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) {
1105 m = mb_unmapped_to_ext(m);
1107 IP6STAT_INC(ip6s_odropped);
1113 * Send the packet to the outgoing interface.
1114 * If necessary, do IPv6 fragmentation before sending.
1116 * The logic here is rather complex:
1117 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
1118 * 1-a: send as is if tlen <= path mtu
1119 * 1-b: fragment if tlen > path mtu
1121 * 2: if user asks us not to fragment (dontfrag == 1)
1122 * 2-a: send as is if tlen <= interface mtu
1123 * 2-b: error if tlen > interface mtu
1125 * 3: if we always need to attach fragment header (alwaysfrag == 1)
1128 * 4: if dontfrag == 1 && alwaysfrag == 1
1129 * error, as we cannot handle this conflicting request.
1131 sw_csum = m->m_pkthdr.csum_flags;
1133 tso = ((sw_csum & ifp->if_hwassist &
1134 (CSUM_TSO | CSUM_INNER_TSO)) != 0) ? 1 : 0;
1135 sw_csum &= ~ifp->if_hwassist;
1139 * If we added extension headers, we will not do TSO and calculate the
1140 * checksums ourselves for now.
1141 * XXX-BZ Need a framework to know when the NIC can handle it, even
1144 ip6_output_delayed_csum(m, ifp, sw_csum, plen, optlen);
1145 /* XXX-BZ m->m_pkthdr.csum_flags &= ~ifp->if_hwassist; */
1146 tlen = m->m_pkthdr.len;
1148 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
1152 if (dontfrag && alwaysfrag) { /* Case 4. */
1153 /* Conflicting request - can't transmit. */
1157 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* Case 2-b. */
1159 * Even if the DONTFRAG option is specified, we cannot send the
1160 * packet when the data length is larger than the MTU of the
1161 * outgoing interface.
1162 * Notify the error by sending IPV6_PATHMTU ancillary data if
1163 * application wanted to know the MTU value. Also return an
1164 * error code (this is not described in the API spec).
1167 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
1172 /* Transmit packet without fragmentation. */
1173 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* Cases 1-a and 2-a. */
1174 struct in6_ifaddr *ia6;
1176 ip6 = mtod(m, struct ip6_hdr *);
1177 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1179 /* Record statistics for this interface address. */
1180 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
1181 counter_u64_add(ia6->ia_ifa.ifa_obytes,
1184 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1185 (flags & IP_NO_SND_TAG_RL) ? false : true);
1189 /* Try to fragment the packet. Cases 1-b and 3. */
1190 if (mtu < IPV6_MMTU) {
1191 /* Path MTU cannot be less than IPV6_MMTU. */
1193 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1195 } else if (ip6->ip6_plen == 0) {
1196 /* Jumbo payload cannot be fragmented. */
1198 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1204 * Too large for the destination or interface;
1205 * fragment if possible.
1206 * Must be able to put at least 8 bytes per fragment.
1208 if (mtu > IPV6_MAXPACKET)
1209 mtu = IPV6_MAXPACKET;
1211 len = (mtu - unfragpartlen - sizeof(struct ip6_frag)) & ~7;
1214 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1219 * If the interface will not calculate checksums on
1220 * fragmented packets, then do it here.
1221 * XXX-BZ handle the hw offloading case. Need flags.
1223 ip6_output_delayed_csum(m, ifp, m->m_pkthdr.csum_flags, plen,
1227 * Change the next header field of the last header in the
1228 * unfragmentable part.
1230 if (exthdrs.ip6e_rthdr) {
1231 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1232 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1233 } else if (exthdrs.ip6e_dest1) {
1234 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1235 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1236 } else if (exthdrs.ip6e_hbh) {
1237 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1238 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1240 ip6 = mtod(m, struct ip6_hdr *);
1241 nextproto = ip6->ip6_nxt;
1242 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1246 * Loop through length of segment after first fragment,
1247 * make new header and copy data of each part and link onto
1251 id = htonl(ip6_randomid());
1252 error = ip6_fragment(ifp, m, unfragpartlen, nextproto,len, id);
1256 in6_ifstat_inc(ifp, ifs6_out_fragok);
1259 /* Remove leading garbage. */
1268 /* Record statistics for this interface address. */
1270 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1271 counter_u64_add(ia->ia_ifa.ifa_obytes,
1275 EVL_APPLY_PRI(m, vlan_pcp);
1276 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1283 IP6STAT_INC(ip6s_fragmented);
1289 m_freem(exthdrs.ip6e_hbh); /* m_freem() checks if mbuf is NULL. */
1290 m_freem(exthdrs.ip6e_dest1);
1291 m_freem(exthdrs.ip6e_rthdr);
1292 m_freem(exthdrs.ip6e_dest2);
1301 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1305 if (hlen > MCLBYTES)
1306 return (ENOBUFS); /* XXX */
1309 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1311 m = m_get(M_NOWAIT, MT_DATA);
1316 bcopy(hdr, mtod(m, caddr_t), hlen);
1323 * Insert jumbo payload option.
1326 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1332 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1335 * If there is no hop-by-hop options header, allocate new one.
1336 * If there is one but it doesn't have enough space to store the
1337 * jumbo payload option, allocate a cluster to store the whole options.
1338 * Otherwise, use it to store the options.
1340 if (exthdrs->ip6e_hbh == NULL) {
1341 mopt = m_get(M_NOWAIT, MT_DATA);
1344 mopt->m_len = JUMBOOPTLEN;
1345 optbuf = mtod(mopt, u_char *);
1346 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1347 exthdrs->ip6e_hbh = mopt;
1349 struct ip6_hbh *hbh;
1351 mopt = exthdrs->ip6e_hbh;
1352 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1355 * - exthdrs->ip6e_hbh is not referenced from places
1356 * other than exthdrs.
1357 * - exthdrs->ip6e_hbh is not an mbuf chain.
1359 int oldoptlen = mopt->m_len;
1363 * XXX: give up if the whole (new) hbh header does
1364 * not fit even in an mbuf cluster.
1366 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1370 * As a consequence, we must always prepare a cluster
1373 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1376 n->m_len = oldoptlen + JUMBOOPTLEN;
1377 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1379 optbuf = mtod(n, caddr_t) + oldoptlen;
1381 mopt = exthdrs->ip6e_hbh = n;
1383 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1384 mopt->m_len += JUMBOOPTLEN;
1386 optbuf[0] = IP6OPT_PADN;
1390 * Adjust the header length according to the pad and
1391 * the jumbo payload option.
1393 hbh = mtod(mopt, struct ip6_hbh *);
1394 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1397 /* fill in the option. */
1398 optbuf[2] = IP6OPT_JUMBO;
1400 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1401 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1403 /* finally, adjust the packet header length */
1404 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1411 * Insert fragment header and copy unfragmentable header portions.
1414 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1415 struct ip6_frag **frghdrp)
1417 struct mbuf *n, *mlast;
1419 if (hlen > sizeof(struct ip6_hdr)) {
1420 n = m_copym(m0, sizeof(struct ip6_hdr),
1421 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1428 /* Search for the last mbuf of unfragmentable part. */
1429 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1432 if (M_WRITABLE(mlast) &&
1433 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1434 /* use the trailing space of the last mbuf for the fragment hdr */
1435 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1437 mlast->m_len += sizeof(struct ip6_frag);
1438 m->m_pkthdr.len += sizeof(struct ip6_frag);
1440 /* allocate a new mbuf for the fragment header */
1443 mfrg = m_get(M_NOWAIT, MT_DATA);
1446 mfrg->m_len = sizeof(struct ip6_frag);
1447 *frghdrp = mtod(mfrg, struct ip6_frag *);
1448 mlast->m_next = mfrg;
1455 * Calculates IPv6 path mtu for destination @dst.
1456 * Resulting MTU is stored in @mtup.
1458 * Returns 0 on success.
1461 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1463 struct epoch_tracker et;
1464 struct nhop_object *nh;
1465 struct in6_addr kdst;
1469 in6_splitscope(dst, &kdst, &scopeid);
1471 NET_EPOCH_ENTER(et);
1472 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1474 error = ip6_calcmtu(nh->nh_ifp, dst, nh->nh_mtu, mtup, NULL, 0);
1476 error = EHOSTUNREACH;
1483 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1484 * and cached data in @ro_pmtu.
1485 * MTU from (successful) route lookup is saved (along with dst)
1486 * inside @ro_pmtu to avoid subsequent route lookups after packet
1487 * filter processing.
1489 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1490 * Returns 0 on success.
1493 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1494 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1495 int *alwaysfragp, u_int fibnum, u_int proto)
1497 struct nhop_object *nh;
1498 struct in6_addr kdst;
1500 struct sockaddr_in6 *sa6_dst, sin6;
1506 if (ro_pmtu == NULL || do_lookup) {
1508 * Here ro_pmtu has final destination address, while
1509 * ro might represent immediate destination.
1510 * Use ro_pmtu destination since mtu might differ.
1512 if (ro_pmtu != NULL) {
1513 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1514 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1515 ro_pmtu->ro_mtu = 0;
1519 if (ro_pmtu == NULL || ro_pmtu->ro_mtu == 0) {
1520 bzero(sa6_dst, sizeof(*sa6_dst));
1521 sa6_dst->sin6_family = AF_INET6;
1522 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1523 sa6_dst->sin6_addr = *dst;
1525 in6_splitscope(dst, &kdst, &scopeid);
1526 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1529 if (ro_pmtu != NULL)
1530 ro_pmtu->ro_mtu = mtu;
1533 mtu = ro_pmtu->ro_mtu;
1536 if (ro_pmtu != NULL && ro_pmtu->ro_nh != NULL)
1537 mtu = ro_pmtu->ro_nh->nh_mtu;
1539 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1543 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1544 * hostcache data for @dst.
1545 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1547 * Returns 0 on success.
1550 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1551 u_long *mtup, int *alwaysfragp, u_int proto)
1559 struct in_conninfo inc;
1561 bzero(&inc, sizeof(inc));
1562 inc.inc_flags |= INC_ISIPV6;
1563 inc.inc6_faddr = *dst;
1565 ifmtu = IN6_LINKMTU(ifp);
1567 /* TCP is known to react to pmtu changes so skip hc */
1568 if (proto != IPPROTO_TCP)
1569 mtu = tcp_hc_getmtu(&inc);
1572 mtu = min(mtu, rt_mtu);
1577 else if (mtu < IPV6_MMTU) {
1579 * RFC2460 section 5, last paragraph:
1580 * if we record ICMPv6 too big message with
1581 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1582 * or smaller, with framgent header attached.
1583 * (fragment header is needed regardless from the
1584 * packet size, for translators to identify packets)
1590 mtu = IN6_LINKMTU(ifp);
1592 error = EHOSTUNREACH; /* XXX */
1596 *alwaysfragp = alwaysfrag;
1601 * IP6 socket option processing.
1604 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1606 int optdatalen, uproto;
1608 struct inpcb *inp = sotoinpcb(so);
1610 int level, op, optname;
1614 uint32_t rss_bucket;
1619 * Don't use more than a quarter of mbuf clusters. N.B.:
1620 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1621 * on LP64 architectures, so cast to u_long to avoid undefined
1622 * behavior. ILP32 architectures cannot have nmbclusters
1623 * large enough to overflow for other reasons.
1625 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1627 level = sopt->sopt_level;
1628 op = sopt->sopt_dir;
1629 optname = sopt->sopt_name;
1630 optlen = sopt->sopt_valsize;
1634 uproto = (int)so->so_proto->pr_protocol;
1636 if (level != IPPROTO_IPV6) {
1639 if (sopt->sopt_level == SOL_SOCKET &&
1640 sopt->sopt_dir == SOPT_SET) {
1641 switch (sopt->sopt_name) {
1644 inp->inp_inc.inc_fibnum = so->so_fibnum;
1648 case SO_MAX_PACING_RATE:
1651 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1662 } else { /* level == IPPROTO_IPV6 */
1666 case IPV6_2292PKTOPTIONS:
1667 #ifdef IPV6_PKTOPTIONS
1668 case IPV6_PKTOPTIONS:
1673 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1674 printf("ip6_ctloutput: mbuf limit hit\n");
1679 error = soopt_getm(sopt, &m); /* XXX */
1682 error = soopt_mcopyin(sopt, m); /* XXX */
1686 error = ip6_pcbopts(&inp->in6p_outputopts, m,
1689 m_freem(m); /* XXX */
1694 * Use of some Hop-by-Hop options or some
1695 * Destination options, might require special
1696 * privilege. That is, normal applications
1697 * (without special privilege) might be forbidden
1698 * from setting certain options in outgoing packets,
1699 * and might never see certain options in received
1700 * packets. [RFC 2292 Section 6]
1701 * KAME specific note:
1702 * KAME prevents non-privileged users from sending or
1703 * receiving ANY hbh/dst options in order to avoid
1704 * overhead of parsing options in the kernel.
1706 case IPV6_RECVHOPOPTS:
1707 case IPV6_RECVDSTOPTS:
1708 case IPV6_RECVRTHDRDSTOPTS:
1710 error = priv_check(td,
1711 PRIV_NETINET_SETHDROPTS);
1716 case IPV6_UNICAST_HOPS:
1719 case IPV6_RECVPKTINFO:
1720 case IPV6_RECVHOPLIMIT:
1721 case IPV6_RECVRTHDR:
1722 case IPV6_RECVPATHMTU:
1723 case IPV6_RECVTCLASS:
1724 case IPV6_RECVFLOWID:
1726 case IPV6_RECVRSSBUCKETID:
1729 case IPV6_AUTOFLOWLABEL:
1730 case IPV6_ORIGDSTADDR:
1733 if (optname == IPV6_BINDANY && td != NULL) {
1734 error = priv_check(td,
1735 PRIV_NETINET_BINDANY);
1740 if (optlen != sizeof(int)) {
1744 error = sooptcopyin(sopt, &optval,
1745 sizeof optval, sizeof optval);
1749 case IPV6_UNICAST_HOPS:
1750 if (optval < -1 || optval >= 256)
1753 /* -1 = kernel default */
1754 inp->in6p_hops = optval;
1755 if ((inp->inp_vflag &
1757 inp->inp_ip_ttl = optval;
1760 #define OPTSET(bit) \
1764 inp->inp_flags |= (bit); \
1766 inp->inp_flags &= ~(bit); \
1768 } while (/*CONSTCOND*/ 0)
1769 #define OPTSET2292(bit) \
1772 inp->inp_flags |= IN6P_RFC2292; \
1774 inp->inp_flags |= (bit); \
1776 inp->inp_flags &= ~(bit); \
1778 } while (/*CONSTCOND*/ 0)
1779 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0)
1781 #define OPTSET2_N(bit, val) do { \
1783 inp->inp_flags2 |= bit; \
1785 inp->inp_flags2 &= ~bit; \
1787 #define OPTSET2(bit, val) do { \
1789 OPTSET2_N(bit, val); \
1792 #define OPTBIT2(bit) (inp->inp_flags2 & (bit) ? 1 : 0)
1793 #define OPTSET2292_EXCLUSIVE(bit) \
1796 if (OPTBIT(IN6P_RFC2292)) { \
1800 inp->inp_flags |= (bit); \
1802 inp->inp_flags &= ~(bit); \
1805 } while (/*CONSTCOND*/ 0)
1807 case IPV6_RECVPKTINFO:
1808 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1813 struct ip6_pktopts **optp;
1815 /* cannot mix with RFC2292 */
1816 if (OPTBIT(IN6P_RFC2292)) {
1821 if (inp->inp_flags & INP_DROPPED) {
1823 return (ECONNRESET);
1825 optp = &inp->in6p_outputopts;
1826 error = ip6_pcbopt(IPV6_HOPLIMIT,
1827 (u_char *)&optval, sizeof(optval),
1828 optp, (td != NULL) ? td->td_ucred :
1834 case IPV6_RECVHOPLIMIT:
1835 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1838 case IPV6_RECVHOPOPTS:
1839 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1842 case IPV6_RECVDSTOPTS:
1843 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1846 case IPV6_RECVRTHDRDSTOPTS:
1847 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1850 case IPV6_RECVRTHDR:
1851 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1854 case IPV6_RECVPATHMTU:
1856 * We ignore this option for TCP
1858 * (RFC3542 leaves this case
1861 if (uproto != IPPROTO_TCP)
1865 case IPV6_RECVFLOWID:
1866 OPTSET2(INP_RECVFLOWID, optval);
1870 case IPV6_RECVRSSBUCKETID:
1871 OPTSET2(INP_RECVRSSBUCKETID, optval);
1877 if (inp->inp_lport ||
1878 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1880 * The socket is already bound.
1887 inp->inp_flags |= IN6P_IPV6_V6ONLY;
1888 inp->inp_vflag &= ~INP_IPV4;
1890 inp->inp_flags &= ~IN6P_IPV6_V6ONLY;
1891 inp->inp_vflag |= INP_IPV4;
1895 case IPV6_RECVTCLASS:
1896 /* cannot mix with RFC2292 XXX */
1897 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1899 case IPV6_AUTOFLOWLABEL:
1900 OPTSET(IN6P_AUTOFLOWLABEL);
1903 case IPV6_ORIGDSTADDR:
1904 OPTSET2(INP_ORIGDSTADDR, optval);
1907 OPTSET(INP_BINDANY);
1910 if ((optval >= -1) && (optval <=
1911 (INP_2PCP_MASK >> INP_2PCP_SHIFT))) {
1937 case IPV6_USE_MIN_MTU:
1938 case IPV6_PREFER_TEMPADDR:
1939 if (optlen != sizeof(optval)) {
1943 error = sooptcopyin(sopt, &optval,
1944 sizeof optval, sizeof optval);
1948 struct ip6_pktopts **optp;
1950 if (inp->inp_flags & INP_DROPPED) {
1952 return (ECONNRESET);
1954 optp = &inp->in6p_outputopts;
1955 error = ip6_pcbopt(optname,
1956 (u_char *)&optval, sizeof(optval),
1957 optp, (td != NULL) ? td->td_ucred :
1963 case IPV6_2292PKTINFO:
1964 case IPV6_2292HOPLIMIT:
1965 case IPV6_2292HOPOPTS:
1966 case IPV6_2292DSTOPTS:
1967 case IPV6_2292RTHDR:
1969 if (optlen != sizeof(int)) {
1973 error = sooptcopyin(sopt, &optval,
1974 sizeof optval, sizeof optval);
1978 case IPV6_2292PKTINFO:
1979 OPTSET2292(IN6P_PKTINFO);
1981 case IPV6_2292HOPLIMIT:
1982 OPTSET2292(IN6P_HOPLIMIT);
1984 case IPV6_2292HOPOPTS:
1986 * Check super-user privilege.
1987 * See comments for IPV6_RECVHOPOPTS.
1990 error = priv_check(td,
1991 PRIV_NETINET_SETHDROPTS);
1995 OPTSET2292(IN6P_HOPOPTS);
1997 case IPV6_2292DSTOPTS:
1999 error = priv_check(td,
2000 PRIV_NETINET_SETHDROPTS);
2004 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
2006 case IPV6_2292RTHDR:
2007 OPTSET2292(IN6P_RTHDR);
2015 case IPV6_RTHDRDSTOPTS:
2018 /* new advanced API (RFC3542) */
2020 u_char optbuf_storage[MCLBYTES];
2022 struct ip6_pktopts **optp;
2024 /* cannot mix with RFC2292 */
2025 if (OPTBIT(IN6P_RFC2292)) {
2031 * We only ensure valsize is not too large
2032 * here. Further validation will be done
2035 error = sooptcopyin(sopt, optbuf_storage,
2036 sizeof(optbuf_storage), 0);
2039 optlen = sopt->sopt_valsize;
2040 optbuf = optbuf_storage;
2042 if (inp->inp_flags & INP_DROPPED) {
2044 return (ECONNRESET);
2046 optp = &inp->in6p_outputopts;
2047 error = ip6_pcbopt(optname, optbuf, optlen,
2048 optp, (td != NULL) ? td->td_ucred : NULL,
2055 case IPV6_MULTICAST_IF:
2056 case IPV6_MULTICAST_HOPS:
2057 case IPV6_MULTICAST_LOOP:
2058 case IPV6_JOIN_GROUP:
2059 case IPV6_LEAVE_GROUP:
2061 case MCAST_BLOCK_SOURCE:
2062 case MCAST_UNBLOCK_SOURCE:
2063 case MCAST_JOIN_GROUP:
2064 case MCAST_LEAVE_GROUP:
2065 case MCAST_JOIN_SOURCE_GROUP:
2066 case MCAST_LEAVE_SOURCE_GROUP:
2067 error = ip6_setmoptions(inp, sopt);
2070 case IPV6_PORTRANGE:
2071 error = sooptcopyin(sopt, &optval,
2072 sizeof optval, sizeof optval);
2078 case IPV6_PORTRANGE_DEFAULT:
2079 inp->inp_flags &= ~(INP_LOWPORT);
2080 inp->inp_flags &= ~(INP_HIGHPORT);
2083 case IPV6_PORTRANGE_HIGH:
2084 inp->inp_flags &= ~(INP_LOWPORT);
2085 inp->inp_flags |= INP_HIGHPORT;
2088 case IPV6_PORTRANGE_LOW:
2089 inp->inp_flags &= ~(INP_HIGHPORT);
2090 inp->inp_flags |= INP_LOWPORT;
2100 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2101 case IPV6_IPSEC_POLICY:
2102 if (IPSEC_ENABLED(ipv6)) {
2103 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2110 error = ENOPROTOOPT;
2117 case IPV6_2292PKTOPTIONS:
2118 #ifdef IPV6_PKTOPTIONS
2119 case IPV6_PKTOPTIONS:
2122 * RFC3542 (effectively) deprecated the
2123 * semantics of the 2292-style pktoptions.
2124 * Since it was not reliable in nature (i.e.,
2125 * applications had to expect the lack of some
2126 * information after all), it would make sense
2127 * to simplify this part by always returning
2130 sopt->sopt_valsize = 0;
2133 case IPV6_RECVHOPOPTS:
2134 case IPV6_RECVDSTOPTS:
2135 case IPV6_RECVRTHDRDSTOPTS:
2136 case IPV6_UNICAST_HOPS:
2137 case IPV6_RECVPKTINFO:
2138 case IPV6_RECVHOPLIMIT:
2139 case IPV6_RECVRTHDR:
2140 case IPV6_RECVPATHMTU:
2143 case IPV6_PORTRANGE:
2144 case IPV6_RECVTCLASS:
2145 case IPV6_AUTOFLOWLABEL:
2149 case IPV6_RECVFLOWID:
2151 case IPV6_RSSBUCKETID:
2152 case IPV6_RECVRSSBUCKETID:
2156 case IPV6_RECVHOPOPTS:
2157 optval = OPTBIT(IN6P_HOPOPTS);
2160 case IPV6_RECVDSTOPTS:
2161 optval = OPTBIT(IN6P_DSTOPTS);
2164 case IPV6_RECVRTHDRDSTOPTS:
2165 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2168 case IPV6_UNICAST_HOPS:
2169 optval = inp->in6p_hops;
2172 case IPV6_RECVPKTINFO:
2173 optval = OPTBIT(IN6P_PKTINFO);
2176 case IPV6_RECVHOPLIMIT:
2177 optval = OPTBIT(IN6P_HOPLIMIT);
2180 case IPV6_RECVRTHDR:
2181 optval = OPTBIT(IN6P_RTHDR);
2184 case IPV6_RECVPATHMTU:
2185 optval = OPTBIT(IN6P_MTU);
2189 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2192 case IPV6_PORTRANGE:
2195 flags = inp->inp_flags;
2196 if (flags & INP_HIGHPORT)
2197 optval = IPV6_PORTRANGE_HIGH;
2198 else if (flags & INP_LOWPORT)
2199 optval = IPV6_PORTRANGE_LOW;
2204 case IPV6_RECVTCLASS:
2205 optval = OPTBIT(IN6P_TCLASS);
2208 case IPV6_AUTOFLOWLABEL:
2209 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2212 case IPV6_ORIGDSTADDR:
2213 optval = OPTBIT2(INP_ORIGDSTADDR);
2217 optval = OPTBIT(INP_BINDANY);
2221 optval = inp->inp_flowid;
2225 optval = inp->inp_flowtype;
2228 case IPV6_RECVFLOWID:
2229 optval = OPTBIT2(INP_RECVFLOWID);
2232 case IPV6_RSSBUCKETID:
2234 rss_hash2bucket(inp->inp_flowid,
2238 optval = rss_bucket;
2243 case IPV6_RECVRSSBUCKETID:
2244 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2250 if (OPTBIT2(INP_2PCP_SET)) {
2251 optval = (inp->inp_flags2 &
2262 error = sooptcopyout(sopt, &optval,
2269 struct ip6_mtuinfo mtuinfo;
2270 struct in6_addr addr;
2272 if (!(so->so_state & SS_ISCONNECTED))
2275 * XXX: we dot not consider the case of source
2276 * routing, or optional information to specify
2277 * the outgoing interface.
2278 * Copy faddr out of inp to avoid holding lock
2279 * on inp during route lookup.
2282 bcopy(&inp->in6p_faddr, &addr, sizeof(addr));
2284 error = ip6_getpmtu_ctl(so->so_fibnum,
2288 if (pmtu > IPV6_MAXPACKET)
2289 pmtu = IPV6_MAXPACKET;
2291 bzero(&mtuinfo, sizeof(mtuinfo));
2292 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2293 optdata = (void *)&mtuinfo;
2294 optdatalen = sizeof(mtuinfo);
2295 error = sooptcopyout(sopt, optdata,
2300 case IPV6_2292PKTINFO:
2301 case IPV6_2292HOPLIMIT:
2302 case IPV6_2292HOPOPTS:
2303 case IPV6_2292RTHDR:
2304 case IPV6_2292DSTOPTS:
2306 case IPV6_2292PKTINFO:
2307 optval = OPTBIT(IN6P_PKTINFO);
2309 case IPV6_2292HOPLIMIT:
2310 optval = OPTBIT(IN6P_HOPLIMIT);
2312 case IPV6_2292HOPOPTS:
2313 optval = OPTBIT(IN6P_HOPOPTS);
2315 case IPV6_2292RTHDR:
2316 optval = OPTBIT(IN6P_RTHDR);
2318 case IPV6_2292DSTOPTS:
2319 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2322 error = sooptcopyout(sopt, &optval,
2329 case IPV6_RTHDRDSTOPTS:
2333 case IPV6_USE_MIN_MTU:
2334 case IPV6_PREFER_TEMPADDR:
2335 error = ip6_getpcbopt(inp, optname, sopt);
2338 case IPV6_MULTICAST_IF:
2339 case IPV6_MULTICAST_HOPS:
2340 case IPV6_MULTICAST_LOOP:
2342 error = ip6_getmoptions(inp, sopt);
2345 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2346 case IPV6_IPSEC_POLICY:
2347 if (IPSEC_ENABLED(ipv6)) {
2348 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2354 error = ENOPROTOOPT;
2364 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2366 int error = 0, optval, optlen;
2367 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2368 struct inpcb *inp = sotoinpcb(so);
2369 int level, op, optname;
2371 level = sopt->sopt_level;
2372 op = sopt->sopt_dir;
2373 optname = sopt->sopt_name;
2374 optlen = sopt->sopt_valsize;
2376 if (level != IPPROTO_IPV6) {
2383 * For ICMPv6 sockets, no modification allowed for checksum
2384 * offset, permit "no change" values to help existing apps.
2386 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2387 * for an ICMPv6 socket will fail."
2388 * The current behavior does not meet RFC3542.
2392 if (optlen != sizeof(int)) {
2396 error = sooptcopyin(sopt, &optval, sizeof(optval),
2400 if (optval < -1 || (optval % 2) != 0) {
2402 * The API assumes non-negative even offset
2403 * values or -1 as a special value.
2406 } else if (inp->inp_ip_p == IPPROTO_ICMPV6) {
2407 if (optval != icmp6off)
2410 inp->in6p_cksum = optval;
2414 if (inp->inp_ip_p == IPPROTO_ICMPV6)
2417 optval = inp->in6p_cksum;
2419 error = sooptcopyout(sopt, &optval, sizeof(optval));
2429 error = ENOPROTOOPT;
2437 * Set up IP6 options in pcb for insertion in output packets or
2438 * specifying behavior of outgoing packets.
2441 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2442 struct socket *so, struct sockopt *sopt)
2444 struct ip6_pktopts *opt = *pktopt;
2446 struct thread *td = sopt->sopt_td;
2447 struct epoch_tracker et;
2449 /* turn off any old options. */
2452 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2453 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2454 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2455 printf("ip6_pcbopts: all specified options are cleared.\n");
2457 ip6_clearpktopts(opt, -1);
2459 opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT);
2465 if (!m || m->m_len == 0) {
2467 * Only turning off any previous options, regardless of
2468 * whether the opt is just created or given.
2470 free(opt, M_IP6OPT);
2474 /* set options specified by user. */
2475 NET_EPOCH_ENTER(et);
2476 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2477 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2478 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2479 free(opt, M_IP6OPT);
2489 * initialize ip6_pktopts. beware that there are non-zero default values in
2493 ip6_initpktopts(struct ip6_pktopts *opt)
2496 bzero(opt, sizeof(*opt));
2497 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2498 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2499 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2500 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2504 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2505 struct ucred *cred, int uproto)
2507 struct epoch_tracker et;
2508 struct ip6_pktopts *opt;
2511 if (*pktopt == NULL) {
2512 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2514 if (*pktopt == NULL)
2516 ip6_initpktopts(*pktopt);
2520 NET_EPOCH_ENTER(et);
2521 ret = ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto);
2527 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2528 if (pktopt && pktopt->field) { \
2530 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2531 malloc_optdata = true; \
2533 if (inp->inp_flags & INP_DROPPED) { \
2535 free(optdata, M_TEMP); \
2536 return (ECONNRESET); \
2538 pktopt = inp->in6p_outputopts; \
2539 if (pktopt && pktopt->field) { \
2540 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2541 bcopy(pktopt->field, optdata, optdatalen); \
2543 free(optdata, M_TEMP); \
2545 malloc_optdata = false; \
2550 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2551 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2553 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2554 pktopt->field->sa_len)
2557 ip6_getpcbopt(struct inpcb *inp, int optname, struct sockopt *sopt)
2559 void *optdata = NULL;
2560 bool malloc_optdata = false;
2563 struct in6_pktinfo null_pktinfo;
2564 int deftclass = 0, on;
2565 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2566 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2567 struct ip6_pktopts *pktopt;
2570 pktopt = inp->in6p_outputopts;
2574 optdata = (void *)&null_pktinfo;
2575 if (pktopt && pktopt->ip6po_pktinfo) {
2576 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2577 sizeof(null_pktinfo));
2578 in6_clearscope(&null_pktinfo.ipi6_addr);
2580 /* XXX: we don't have to do this every time... */
2581 bzero(&null_pktinfo, sizeof(null_pktinfo));
2583 optdatalen = sizeof(struct in6_pktinfo);
2586 if (pktopt && pktopt->ip6po_tclass >= 0)
2587 deftclass = pktopt->ip6po_tclass;
2588 optdata = (void *)&deftclass;
2589 optdatalen = sizeof(int);
2592 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2595 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2597 case IPV6_RTHDRDSTOPTS:
2598 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2601 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2604 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2606 case IPV6_USE_MIN_MTU:
2608 defminmtu = pktopt->ip6po_minmtu;
2609 optdata = (void *)&defminmtu;
2610 optdatalen = sizeof(int);
2613 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2617 optdata = (void *)&on;
2618 optdatalen = sizeof(on);
2620 case IPV6_PREFER_TEMPADDR:
2622 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2623 optdata = (void *)&defpreftemp;
2624 optdatalen = sizeof(int);
2626 default: /* should not happen */
2628 panic("ip6_getpcbopt: unexpected option\n");
2631 return (ENOPROTOOPT);
2635 error = sooptcopyout(sopt, optdata, optdatalen);
2637 free(optdata, M_TEMP);
2643 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2648 if (optname == -1 || optname == IPV6_PKTINFO) {
2649 if (pktopt->ip6po_pktinfo)
2650 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2651 pktopt->ip6po_pktinfo = NULL;
2653 if (optname == -1 || optname == IPV6_HOPLIMIT)
2654 pktopt->ip6po_hlim = -1;
2655 if (optname == -1 || optname == IPV6_TCLASS)
2656 pktopt->ip6po_tclass = -1;
2657 if (optname == -1 || optname == IPV6_NEXTHOP) {
2658 if (pktopt->ip6po_nextroute.ro_nh) {
2659 NH_FREE(pktopt->ip6po_nextroute.ro_nh);
2660 pktopt->ip6po_nextroute.ro_nh = NULL;
2662 if (pktopt->ip6po_nexthop)
2663 free(pktopt->ip6po_nexthop, M_IP6OPT);
2664 pktopt->ip6po_nexthop = NULL;
2666 if (optname == -1 || optname == IPV6_HOPOPTS) {
2667 if (pktopt->ip6po_hbh)
2668 free(pktopt->ip6po_hbh, M_IP6OPT);
2669 pktopt->ip6po_hbh = NULL;
2671 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2672 if (pktopt->ip6po_dest1)
2673 free(pktopt->ip6po_dest1, M_IP6OPT);
2674 pktopt->ip6po_dest1 = NULL;
2676 if (optname == -1 || optname == IPV6_RTHDR) {
2677 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2678 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2679 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2680 if (pktopt->ip6po_route.ro_nh) {
2681 NH_FREE(pktopt->ip6po_route.ro_nh);
2682 pktopt->ip6po_route.ro_nh = NULL;
2685 if (optname == -1 || optname == IPV6_DSTOPTS) {
2686 if (pktopt->ip6po_dest2)
2687 free(pktopt->ip6po_dest2, M_IP6OPT);
2688 pktopt->ip6po_dest2 = NULL;
2692 #define PKTOPT_EXTHDRCPY(type) \
2695 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2696 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2697 if (dst->type == NULL)\
2699 bcopy(src->type, dst->type, hlen);\
2701 } while (/*CONSTCOND*/ 0)
2704 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2706 if (dst == NULL || src == NULL) {
2707 printf("ip6_clearpktopts: invalid argument\n");
2711 dst->ip6po_hlim = src->ip6po_hlim;
2712 dst->ip6po_tclass = src->ip6po_tclass;
2713 dst->ip6po_flags = src->ip6po_flags;
2714 dst->ip6po_minmtu = src->ip6po_minmtu;
2715 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2716 if (src->ip6po_pktinfo) {
2717 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2719 if (dst->ip6po_pktinfo == NULL)
2721 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2723 if (src->ip6po_nexthop) {
2724 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2726 if (dst->ip6po_nexthop == NULL)
2728 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2729 src->ip6po_nexthop->sa_len);
2731 PKTOPT_EXTHDRCPY(ip6po_hbh);
2732 PKTOPT_EXTHDRCPY(ip6po_dest1);
2733 PKTOPT_EXTHDRCPY(ip6po_dest2);
2734 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2738 ip6_clearpktopts(dst, -1);
2741 #undef PKTOPT_EXTHDRCPY
2743 struct ip6_pktopts *
2744 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2747 struct ip6_pktopts *dst;
2749 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2752 ip6_initpktopts(dst);
2754 if ((error = copypktopts(dst, src, canwait)) != 0) {
2755 free(dst, M_IP6OPT);
2763 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2768 ip6_clearpktopts(pktopt, -1);
2770 free(pktopt, M_IP6OPT);
2774 * Set IPv6 outgoing packet options based on advanced API.
2777 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2778 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2780 struct cmsghdr *cm = NULL;
2782 if (control == NULL || opt == NULL)
2786 * ip6_setpktopt can call ifnet_byindex(), so it's imperative that we
2787 * are in the network epoch here.
2791 ip6_initpktopts(opt);
2796 * If stickyopt is provided, make a local copy of the options
2797 * for this particular packet, then override them by ancillary
2799 * XXX: copypktopts() does not copy the cached route to a next
2800 * hop (if any). This is not very good in terms of efficiency,
2801 * but we can allow this since this option should be rarely
2804 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2809 * XXX: Currently, we assume all the optional information is stored
2812 if (control->m_next)
2815 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2816 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2819 if (control->m_len < CMSG_LEN(0))
2822 cm = mtod(control, struct cmsghdr *);
2823 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2825 if (cm->cmsg_level != IPPROTO_IPV6)
2828 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2829 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2838 * Set a particular packet option, as a sticky option or an ancillary data
2839 * item. "len" can be 0 only when it's a sticky option.
2840 * We have 4 cases of combination of "sticky" and "cmsg":
2841 * "sticky=0, cmsg=0": impossible
2842 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2843 * "sticky=1, cmsg=0": RFC3542 socket option
2844 * "sticky=1, cmsg=1": RFC2292 socket option
2847 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2848 struct ucred *cred, int sticky, int cmsg, int uproto)
2850 int minmtupolicy, preftemp;
2855 if (!sticky && !cmsg) {
2857 printf("ip6_setpktopt: impossible case\n");
2863 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2864 * not be specified in the context of RFC3542. Conversely,
2865 * RFC3542 types should not be specified in the context of RFC2292.
2869 case IPV6_2292PKTINFO:
2870 case IPV6_2292HOPLIMIT:
2871 case IPV6_2292NEXTHOP:
2872 case IPV6_2292HOPOPTS:
2873 case IPV6_2292DSTOPTS:
2874 case IPV6_2292RTHDR:
2875 case IPV6_2292PKTOPTIONS:
2876 return (ENOPROTOOPT);
2879 if (sticky && cmsg) {
2886 case IPV6_RTHDRDSTOPTS:
2888 case IPV6_USE_MIN_MTU:
2891 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2892 return (ENOPROTOOPT);
2897 case IPV6_2292PKTINFO:
2900 struct ifnet *ifp = NULL;
2901 struct in6_pktinfo *pktinfo;
2903 if (len != sizeof(struct in6_pktinfo))
2906 pktinfo = (struct in6_pktinfo *)buf;
2909 * An application can clear any sticky IPV6_PKTINFO option by
2910 * doing a "regular" setsockopt with ipi6_addr being
2911 * in6addr_any and ipi6_ifindex being zero.
2912 * [RFC 3542, Section 6]
2914 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2915 pktinfo->ipi6_ifindex == 0 &&
2916 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2917 ip6_clearpktopts(opt, optname);
2921 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2922 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2925 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2927 /* validate the interface index if specified. */
2928 if (pktinfo->ipi6_ifindex) {
2929 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2933 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2934 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2938 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2939 struct in6_ifaddr *ia;
2941 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2942 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2944 return (EADDRNOTAVAIL);
2945 ifa_free(&ia->ia_ifa);
2948 * We store the address anyway, and let in6_selectsrc()
2949 * validate the specified address. This is because ipi6_addr
2950 * may not have enough information about its scope zone, and
2951 * we may need additional information (such as outgoing
2952 * interface or the scope zone of a destination address) to
2953 * disambiguate the scope.
2954 * XXX: the delay of the validation may confuse the
2955 * application when it is used as a sticky option.
2957 if (opt->ip6po_pktinfo == NULL) {
2958 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2959 M_IP6OPT, M_NOWAIT);
2960 if (opt->ip6po_pktinfo == NULL)
2963 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2967 case IPV6_2292HOPLIMIT:
2973 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2974 * to simplify the ordering among hoplimit options.
2976 if (optname == IPV6_HOPLIMIT && sticky)
2977 return (ENOPROTOOPT);
2979 if (len != sizeof(int))
2982 if (*hlimp < -1 || *hlimp > 255)
2985 opt->ip6po_hlim = *hlimp;
2993 if (len != sizeof(int))
2995 tclass = *(int *)buf;
2996 if (tclass < -1 || tclass > 255)
2999 opt->ip6po_tclass = tclass;
3003 case IPV6_2292NEXTHOP:
3006 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3011 if (len == 0) { /* just remove the option */
3012 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3016 /* check if cmsg_len is large enough for sa_len */
3017 if (len < sizeof(struct sockaddr) || len < *buf)
3020 switch (((struct sockaddr *)buf)->sa_family) {
3023 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3026 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3029 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3030 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3033 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
3039 case AF_LINK: /* should eventually be supported */
3041 return (EAFNOSUPPORT);
3044 /* turn off the previous option, then set the new option. */
3045 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3046 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3047 if (opt->ip6po_nexthop == NULL)
3049 bcopy(buf, opt->ip6po_nexthop, *buf);
3052 case IPV6_2292HOPOPTS:
3055 struct ip6_hbh *hbh;
3059 * XXX: We don't allow a non-privileged user to set ANY HbH
3060 * options, since per-option restriction has too much
3064 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3070 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3071 break; /* just remove the option */
3074 /* message length validation */
3075 if (len < sizeof(struct ip6_hbh))
3077 hbh = (struct ip6_hbh *)buf;
3078 hbhlen = (hbh->ip6h_len + 1) << 3;
3082 /* turn off the previous option, then set the new option. */
3083 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3084 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3085 if (opt->ip6po_hbh == NULL)
3087 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3092 case IPV6_2292DSTOPTS:
3094 case IPV6_RTHDRDSTOPTS:
3096 struct ip6_dest *dest, **newdest = NULL;
3099 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
3100 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3106 ip6_clearpktopts(opt, optname);
3107 break; /* just remove the option */
3110 /* message length validation */
3111 if (len < sizeof(struct ip6_dest))
3113 dest = (struct ip6_dest *)buf;
3114 destlen = (dest->ip6d_len + 1) << 3;
3119 * Determine the position that the destination options header
3120 * should be inserted; before or after the routing header.
3123 case IPV6_2292DSTOPTS:
3125 * The old advacned API is ambiguous on this point.
3126 * Our approach is to determine the position based
3127 * according to the existence of a routing header.
3128 * Note, however, that this depends on the order of the
3129 * extension headers in the ancillary data; the 1st
3130 * part of the destination options header must appear
3131 * before the routing header in the ancillary data,
3133 * RFC3542 solved the ambiguity by introducing
3134 * separate ancillary data or option types.
3136 if (opt->ip6po_rthdr == NULL)
3137 newdest = &opt->ip6po_dest1;
3139 newdest = &opt->ip6po_dest2;
3141 case IPV6_RTHDRDSTOPTS:
3142 newdest = &opt->ip6po_dest1;
3145 newdest = &opt->ip6po_dest2;
3149 /* turn off the previous option, then set the new option. */
3150 ip6_clearpktopts(opt, optname);
3151 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3152 if (*newdest == NULL)
3154 bcopy(dest, *newdest, destlen);
3159 case IPV6_2292RTHDR:
3162 struct ip6_rthdr *rth;
3166 ip6_clearpktopts(opt, IPV6_RTHDR);
3167 break; /* just remove the option */
3170 /* message length validation */
3171 if (len < sizeof(struct ip6_rthdr))
3173 rth = (struct ip6_rthdr *)buf;
3174 rthlen = (rth->ip6r_len + 1) << 3;
3178 switch (rth->ip6r_type) {
3179 case IPV6_RTHDR_TYPE_0:
3180 if (rth->ip6r_len == 0) /* must contain one addr */
3182 if (rth->ip6r_len % 2) /* length must be even */
3184 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3188 return (EINVAL); /* not supported */
3191 /* turn off the previous option */
3192 ip6_clearpktopts(opt, IPV6_RTHDR);
3193 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3194 if (opt->ip6po_rthdr == NULL)
3196 bcopy(rth, opt->ip6po_rthdr, rthlen);
3201 case IPV6_USE_MIN_MTU:
3202 if (len != sizeof(int))
3204 minmtupolicy = *(int *)buf;
3205 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3206 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3207 minmtupolicy != IP6PO_MINMTU_ALL) {
3210 opt->ip6po_minmtu = minmtupolicy;
3214 if (len != sizeof(int))
3217 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3219 * we ignore this option for TCP sockets.
3220 * (RFC3542 leaves this case unspecified.)
3222 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3224 opt->ip6po_flags |= IP6PO_DONTFRAG;
3227 case IPV6_PREFER_TEMPADDR:
3228 if (len != sizeof(int))
3230 preftemp = *(int *)buf;
3231 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3232 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3233 preftemp != IP6PO_TEMPADDR_PREFER) {
3236 opt->ip6po_prefer_tempaddr = preftemp;
3240 return (ENOPROTOOPT);
3241 } /* end of switch */
3247 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3248 * packet to the input queue of a specified interface. Note that this
3249 * calls the output routine of the loopback "driver", but with an interface
3250 * pointer that might NOT be &loif -- easier than replicating that code here.
3253 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3256 struct ip6_hdr *ip6;
3258 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3263 * Make sure to deep-copy IPv6 header portion in case the data
3264 * is in an mbuf cluster, so that we can safely override the IPv6
3265 * header portion later.
3267 if (!M_WRITABLE(copym) ||
3268 copym->m_len < sizeof(struct ip6_hdr)) {
3269 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3273 ip6 = mtod(copym, struct ip6_hdr *);
3275 * clear embedded scope identifiers if necessary.
3276 * in6_clearscope will touch the addresses only when necessary.
3278 in6_clearscope(&ip6->ip6_src);
3279 in6_clearscope(&ip6->ip6_dst);
3280 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3281 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3283 copym->m_pkthdr.csum_data = 0xffff;
3285 if_simloop(ifp, copym, AF_INET6, 0);
3289 * Chop IPv6 header off from the payload.
3292 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3295 struct ip6_hdr *ip6;
3297 ip6 = mtod(m, struct ip6_hdr *);
3298 if (m->m_len > sizeof(*ip6)) {
3299 mh = m_gethdr(M_NOWAIT, MT_DATA);
3304 m_move_pkthdr(mh, m);
3305 M_ALIGN(mh, sizeof(*ip6));
3306 m->m_len -= sizeof(*ip6);
3307 m->m_data += sizeof(*ip6);
3310 m->m_len = sizeof(*ip6);
3311 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3313 exthdrs->ip6e_ip6 = m;
3318 * Compute IPv6 extension header length.
3321 ip6_optlen(struct inpcb *inp)
3325 if (!inp->in6p_outputopts)
3330 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3332 len += elen(inp->in6p_outputopts->ip6po_hbh);
3333 if (inp->in6p_outputopts->ip6po_rthdr)
3334 /* dest1 is valid with rthdr only */
3335 len += elen(inp->in6p_outputopts->ip6po_dest1);
3336 len += elen(inp->in6p_outputopts->ip6po_rthdr);
3337 len += elen(inp->in6p_outputopts->ip6po_dest2);