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_ratelimit.h"
71 #include "opt_ipsec.h"
73 #include "opt_route.h"
76 #include <sys/param.h>
77 #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_llatbl.h>
94 #include <net/netisr.h>
95 #include <net/route.h>
97 #include <net/rss_config.h>
100 #include <netinet/in.h>
101 #include <netinet/in_var.h>
102 #include <netinet/ip_var.h>
103 #include <netinet6/in6_fib.h>
104 #include <netinet6/in6_var.h>
105 #include <netinet/ip6.h>
106 #include <netinet/icmp6.h>
107 #include <netinet6/ip6_var.h>
108 #include <netinet/in_pcb.h>
109 #include <netinet/tcp_var.h>
110 #include <netinet6/nd6.h>
111 #include <netinet6/in6_rss.h>
113 #include <netipsec/ipsec_support.h>
115 #include <netinet/sctp.h>
116 #include <netinet/sctp_crc32.h>
119 #include <netinet6/ip6protosw.h>
120 #include <netinet6/scope6_var.h>
122 extern int in6_mcast_loop;
125 struct mbuf *ip6e_ip6;
126 struct mbuf *ip6e_hbh;
127 struct mbuf *ip6e_dest1;
128 struct mbuf *ip6e_rthdr;
129 struct mbuf *ip6e_dest2;
132 static MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
134 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
135 struct ucred *, int);
136 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
137 struct socket *, struct sockopt *);
138 static int ip6_getpcbopt(struct inpcb *, int, struct sockopt *);
139 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
140 struct ucred *, int, int, int);
142 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
143 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
145 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
146 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
147 static int ip6_getpmtu(struct route_in6 *, int,
148 struct ifnet *, const struct in6_addr *, u_long *, int *, u_int,
150 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
151 u_long *, int *, u_int);
152 static int ip6_getpmtu_ctl(u_int, const struct in6_addr *, u_long *);
153 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
157 * Make an extension header from option data. hp is the source, and
158 * mp is the destination.
160 #define MAKE_EXTHDR(hp, mp) \
163 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
164 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
165 ((eh)->ip6e_len + 1) << 3); \
169 } while (/*CONSTCOND*/ 0)
172 * Form a chain of extension headers.
173 * m is the extension header mbuf
174 * mp is the previous mbuf in the chain
175 * p is the next header
176 * i is the type of option.
178 #define MAKE_CHAIN(m, mp, p, i)\
182 panic("assumption failed: hdr not split"); \
183 *mtod((m), u_char *) = *(p);\
185 p = mtod((m), u_char *);\
186 (m)->m_next = (mp)->m_next;\
190 } while (/*CONSTCOND*/ 0)
193 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
197 csum = in_cksum_skip(m, offset + plen, offset);
198 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
200 offset += m->m_pkthdr.csum_data; /* checksum offset */
202 if (offset + sizeof(csum) > m->m_len)
203 m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
205 *(u_short *)mtodo(m, offset) = csum;
209 ip6_output_delayed_csum(struct mbuf *m, struct ifnet *ifp, int csum_flags,
210 int plen, int optlen, bool frag __unused)
213 KASSERT((plen >= optlen), ("%s:%d: plen %d < optlen %d, m %p, ifp %p "
214 "csum_flags %#x frag %d\n",
215 __func__, __LINE__, plen, optlen, m, ifp, csum_flags, frag));
217 if ((csum_flags & CSUM_DELAY_DATA_IPV6) ||
219 (csum_flags & CSUM_SCTP_IPV6) ||
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;
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;
239 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
240 int fraglen , uint32_t id)
242 struct mbuf *m, **mnext, *m_frgpart;
243 struct ip6_hdr *ip6, *mhip6;
244 struct ip6_frag *ip6f;
247 int tlen = m0->m_pkthdr.len;
249 KASSERT((fraglen % 8 == 0), ("Fragment length must be a multiple of 8"));
252 ip6 = mtod(m, struct ip6_hdr *);
253 mnext = &m->m_nextpkt;
255 for (off = hlen; off < tlen; off += fraglen) {
256 m = m_gethdr(M_NOWAIT, MT_DATA);
258 IP6STAT_INC(ip6s_odropped);
263 * Make sure the complete packet header gets copied
264 * from the originating mbuf to the newly created
265 * mbuf. This also ensures that existing firewall
266 * classification(s), VLAN tags and so on get copied
267 * to the resulting fragmented packet(s):
269 if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
271 IP6STAT_INC(ip6s_odropped);
276 mnext = &m->m_nextpkt;
277 m->m_data += max_linkhdr;
278 mhip6 = mtod(m, struct ip6_hdr *);
280 m->m_len = sizeof(*mhip6);
281 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
283 IP6STAT_INC(ip6s_odropped);
286 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
287 if (off + fraglen >= tlen)
288 fraglen = tlen - off;
290 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
291 mhip6->ip6_plen = htons((u_short)(fraglen + hlen +
292 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
293 if ((m_frgpart = m_copym(m0, off, fraglen, M_NOWAIT)) == NULL) {
294 IP6STAT_INC(ip6s_odropped);
298 m->m_pkthdr.len = fraglen + hlen + sizeof(*ip6f);
299 ip6f->ip6f_reserved = 0;
300 ip6f->ip6f_ident = id;
301 ip6f->ip6f_nxt = nextproto;
302 IP6STAT_INC(ip6s_ofragments);
303 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
310 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
311 * header (with pri, len, nxt, hlim, src, dst).
312 * This function may modify ver and hlim only.
313 * The mbuf chain containing the packet will be freed.
314 * The mbuf opt, if present, will not be freed.
315 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
316 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
317 * then result of route lookup is stored in ro->ro_rt.
319 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
320 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
323 * ifpp - XXX: just for statistics
326 * XXX TODO: no flowid is assigned for outbound flows?
329 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
330 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
331 struct ifnet **ifpp, struct inpcb *inp)
334 struct ifnet *ifp, *origifp;
336 struct mbuf *mprev = NULL;
338 struct route_in6 ip6route;
339 struct rtentry *rt = NULL;
340 struct sockaddr_in6 *dst, src_sa, dst_sa;
341 struct in6_addr odst;
343 struct in6_ifaddr *ia = NULL;
345 int alwaysfrag, dontfrag;
346 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
347 struct ip6_exthdrs exthdrs;
348 struct in6_addr src0, dst0;
350 struct route_in6 *ro_pmtu = NULL;
355 struct m_tag *fwd_tag = NULL;
359 INP_LOCK_ASSERT(inp);
360 M_SETFIB(m, inp->inp_inc.inc_fibnum);
361 if ((flags & IP_NODEFAULTFLOWID) == 0) {
362 /* unconditionally set flowid */
363 m->m_pkthdr.flowid = inp->inp_flowid;
364 M_HASHTYPE_SET(m, inp->inp_flowtype);
368 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
370 * IPSec checking which handles several cases.
371 * FAST IPSEC: We re-injected the packet.
372 * XXX: need scope argument.
374 if (IPSEC_ENABLED(ipv6)) {
375 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
376 if (error == EINPROGRESS)
383 bzero(&exthdrs, sizeof(exthdrs));
385 /* Hop-by-Hop options header */
386 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
387 /* Destination options header(1st part) */
388 if (opt->ip6po_rthdr) {
390 * Destination options header(1st part)
391 * This only makes sense with a routing header.
392 * See Section 9.2 of RFC 3542.
393 * Disabling this part just for MIP6 convenience is
394 * a bad idea. We need to think carefully about a
395 * way to make the advanced API coexist with MIP6
396 * options, which might automatically be inserted in
399 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
402 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
403 /* Destination options header(2nd part) */
404 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
408 * Calculate the total length of the extension header chain.
409 * Keep the length of the unfragmentable part for fragmentation.
412 if (exthdrs.ip6e_hbh)
413 optlen += exthdrs.ip6e_hbh->m_len;
414 if (exthdrs.ip6e_dest1)
415 optlen += exthdrs.ip6e_dest1->m_len;
416 if (exthdrs.ip6e_rthdr)
417 optlen += exthdrs.ip6e_rthdr->m_len;
418 unfragpartlen = optlen + sizeof(struct ip6_hdr);
420 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
421 if (exthdrs.ip6e_dest2)
422 optlen += exthdrs.ip6e_dest2->m_len;
425 * If there is at least one extension header,
426 * separate IP6 header from the payload.
428 if (optlen && !hdrsplit) {
429 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
433 m = exthdrs.ip6e_ip6;
437 ip6 = mtod(m, struct ip6_hdr *);
439 /* adjust mbuf packet header length */
440 m->m_pkthdr.len += optlen;
441 plen = m->m_pkthdr.len - sizeof(*ip6);
443 /* If this is a jumbo payload, insert a jumbo payload option. */
444 if (plen > IPV6_MAXPACKET) {
446 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
450 m = exthdrs.ip6e_ip6;
454 ip6 = mtod(m, struct ip6_hdr *);
455 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
459 ip6->ip6_plen = htons(plen);
462 * Concatenate headers and fill in next header fields.
463 * Here we have, on "m"
465 * and we insert headers accordingly. Finally, we should be getting:
466 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
468 * during the header composing process, "m" points to IPv6 header.
469 * "mprev" points to an extension header prior to esp.
471 u_char *nexthdrp = &ip6->ip6_nxt;
475 * we treat dest2 specially. this makes IPsec processing
476 * much easier. the goal here is to make mprev point the
477 * mbuf prior to dest2.
479 * result: IPv6 dest2 payload
480 * m and mprev will point to IPv6 header.
482 if (exthdrs.ip6e_dest2) {
484 panic("assumption failed: hdr not split");
485 exthdrs.ip6e_dest2->m_next = m->m_next;
486 m->m_next = exthdrs.ip6e_dest2;
487 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
488 ip6->ip6_nxt = IPPROTO_DSTOPTS;
492 * result: IPv6 hbh dest1 rthdr dest2 payload
493 * m will point to IPv6 header. mprev will point to the
494 * extension header prior to dest2 (rthdr in the above case).
496 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
497 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
499 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
503 * If there is a routing header, discard the packet.
505 if (exthdrs.ip6e_rthdr) {
510 /* Source address validation */
511 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
512 (flags & IPV6_UNSPECSRC) == 0) {
514 IP6STAT_INC(ip6s_badscope);
517 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
519 IP6STAT_INC(ip6s_badscope);
523 IP6STAT_INC(ip6s_localout);
530 bzero((caddr_t)ro, sizeof(*ro));
533 if (opt && opt->ip6po_rthdr)
534 ro = &opt->ip6po_route;
535 dst = (struct sockaddr_in6 *)&ro->ro_dst;
536 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
539 * if specified, try to fill in the traffic class field.
540 * do not override if a non-zero value is already set.
541 * we check the diffserv field and the ecn field separately.
543 if (opt && opt->ip6po_tclass >= 0) {
546 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
548 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
551 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
554 /* fill in or override the hop limit field, if necessary. */
555 if (opt && opt->ip6po_hlim != -1)
556 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
557 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
559 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
561 ip6->ip6_hlim = V_ip6_defmcasthlim;
564 * Validate route against routing table additions;
565 * a better/more specific route might have been added.
566 * Make sure address family is set in route.
569 ro->ro_dst.sin6_family = AF_INET6;
570 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
572 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
573 ro->ro_dst.sin6_family == AF_INET6 &&
574 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
576 ifp = ro->ro_rt->rt_ifp;
579 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
581 if (fwd_tag == NULL) {
582 bzero(&dst_sa, sizeof(dst_sa));
583 dst_sa.sin6_family = AF_INET6;
584 dst_sa.sin6_len = sizeof(dst_sa);
585 dst_sa.sin6_addr = ip6->ip6_dst;
587 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
591 in6_ifstat_inc(ifp, ifs6_out_discard);
597 * If in6_selectroute() does not return a route entry,
598 * dst may not have been updated.
600 *dst = dst_sa; /* XXX */
604 * then rt (for unicast) and ifp must be non-NULL valid values.
606 if ((flags & IPV6_FORWARDING) == 0) {
607 /* XXX: the FORWARDING flag can be set for mrouting. */
608 in6_ifstat_inc(ifp, ifs6_out_request);
611 ia = (struct in6_ifaddr *)(rt->rt_ifa);
612 counter_u64_add(rt->rt_pksent, 1);
615 /* Setup data structures for scope ID checks. */
617 bzero(&src_sa, sizeof(src_sa));
618 src_sa.sin6_family = AF_INET6;
619 src_sa.sin6_len = sizeof(src_sa);
620 src_sa.sin6_addr = ip6->ip6_src;
623 /* re-initialize to be sure */
624 bzero(&dst_sa, sizeof(dst_sa));
625 dst_sa.sin6_family = AF_INET6;
626 dst_sa.sin6_len = sizeof(dst_sa);
627 dst_sa.sin6_addr = ip6->ip6_dst;
629 /* Check for valid scope ID. */
630 if (in6_setscope(&src0, ifp, &zone) == 0 &&
631 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
632 in6_setscope(&dst0, ifp, &zone) == 0 &&
633 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
635 * The outgoing interface is in the zone of the source
636 * and destination addresses.
638 * Because the loopback interface cannot receive
639 * packets with a different scope ID than its own,
640 * there is a trick is to pretend the outgoing packet
641 * was received by the real network interface, by
642 * setting "origifp" different from "ifp". This is
643 * only allowed when "ifp" is a loopback network
644 * interface. Refer to code in nd6_output_ifp() for
650 * We should use ia_ifp to support the case of sending
651 * packets to an address of our own.
653 if (ia != NULL && ia->ia_ifp)
656 } else if ((ifp->if_flags & IFF_LOOPBACK) == 0 ||
657 sa6_recoverscope(&src_sa) != 0 ||
658 sa6_recoverscope(&dst_sa) != 0 ||
659 dst_sa.sin6_scope_id == 0 ||
660 (src_sa.sin6_scope_id != 0 &&
661 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
662 (origifp = ifnet_byindex(dst_sa.sin6_scope_id)) == NULL) {
664 * If the destination network interface is not a
665 * loopback interface, or the destination network
666 * address has no scope ID, or the source address has
667 * a scope ID set which is different from the
668 * destination address one, or there is no network
669 * interface representing this scope ID, the address
670 * pair is considered invalid.
672 IP6STAT_INC(ip6s_badscope);
673 in6_ifstat_inc(ifp, ifs6_out_discard);
675 error = EHOSTUNREACH; /* XXX */
679 /* All scope ID checks are successful. */
681 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
682 if (opt && opt->ip6po_nextroute.ro_rt) {
684 * The nexthop is explicitly specified by the
685 * application. We assume the next hop is an IPv6
688 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
690 else if ((rt->rt_flags & RTF_GATEWAY))
691 dst = (struct sockaddr_in6 *)rt->rt_gateway;
694 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
695 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
697 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
698 in6_ifstat_inc(ifp, ifs6_out_mcast);
700 * Confirm that the outgoing interface supports multicast.
702 if (!(ifp->if_flags & IFF_MULTICAST)) {
703 IP6STAT_INC(ip6s_noroute);
704 in6_ifstat_inc(ifp, ifs6_out_discard);
708 if ((im6o == NULL && in6_mcast_loop) ||
709 (im6o && im6o->im6o_multicast_loop)) {
711 * Loop back multicast datagram if not expressly
712 * forbidden to do so, even if we have not joined
713 * the address; protocols will filter it later,
714 * thus deferring a hash lookup and lock acquisition
715 * at the expense of an m_copym().
717 ip6_mloopback(ifp, m);
720 * If we are acting as a multicast router, perform
721 * multicast forwarding as if the packet had just
722 * arrived on the interface to which we are about
723 * to send. The multicast forwarding function
724 * recursively calls this function, using the
725 * IPV6_FORWARDING flag to prevent infinite recursion.
727 * Multicasts that are looped back by ip6_mloopback(),
728 * above, will be forwarded by the ip6_input() routine,
731 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
733 * XXX: ip6_mforward expects that rcvif is NULL
734 * when it is called from the originating path.
735 * However, it may not always be the case.
737 m->m_pkthdr.rcvif = NULL;
738 if (ip6_mforward(ip6, ifp, m) != 0) {
745 * Multicasts with a hoplimit of zero may be looped back,
746 * above, but must not be transmitted on a network.
747 * Also, multicasts addressed to the loopback interface
748 * are not sent -- the above call to ip6_mloopback() will
749 * loop back a copy if this host actually belongs to the
750 * destination group on the loopback interface.
752 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
753 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
760 * Fill the outgoing inteface to tell the upper layer
761 * to increment per-interface statistics.
766 /* Determine path MTU. */
767 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
768 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
772 * The caller of this function may specify to use the minimum MTU
774 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
775 * setting. The logic is a bit complicated; by default, unicast
776 * packets will follow path MTU while multicast packets will be sent at
777 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
778 * including unicast ones will be sent at the minimum MTU. Multicast
779 * packets will always be sent at the minimum MTU unless
780 * IP6PO_MINMTU_DISABLE is explicitly specified.
781 * See RFC 3542 for more details.
783 if (mtu > IPV6_MMTU) {
784 if ((flags & IPV6_MINMTU))
786 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
788 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
790 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
796 * clear embedded scope identifiers if necessary.
797 * in6_clearscope will touch the addresses only when necessary.
799 in6_clearscope(&ip6->ip6_src);
800 in6_clearscope(&ip6->ip6_dst);
803 * If the outgoing packet contains a hop-by-hop options header,
804 * it must be examined and processed even by the source node.
805 * (RFC 2460, section 4.)
807 if (exthdrs.ip6e_hbh) {
808 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
809 u_int32_t dummy; /* XXX unused */
810 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
813 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
814 panic("ip6e_hbh is not contiguous");
817 * XXX: if we have to send an ICMPv6 error to the sender,
818 * we need the M_LOOP flag since icmp6_error() expects
819 * the IPv6 and the hop-by-hop options header are
820 * contiguous unless the flag is set.
822 m->m_flags |= M_LOOP;
823 m->m_pkthdr.rcvif = ifp;
824 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
825 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
826 &dummy, &plen) < 0) {
827 /* m was already freed at this point */
828 error = EINVAL;/* better error? */
831 m->m_flags &= ~M_LOOP; /* XXX */
832 m->m_pkthdr.rcvif = NULL;
835 /* Jump over all PFIL processing if hooks are not active. */
836 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
840 /* Run through list of hooks for output packets. */
841 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, 0, inp);
842 if (error != 0 || m == NULL)
845 ip6 = mtod(m, struct ip6_hdr *);
848 /* See if destination IP address was changed by packet filter. */
849 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
850 m->m_flags |= M_SKIP_FIREWALL;
851 /* If destination is now ourself drop to ip6_input(). */
852 if (in6_localip(&ip6->ip6_dst)) {
853 m->m_flags |= M_FASTFWD_OURS;
854 if (m->m_pkthdr.rcvif == NULL)
855 m->m_pkthdr.rcvif = V_loif;
856 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
857 m->m_pkthdr.csum_flags |=
858 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
859 m->m_pkthdr.csum_data = 0xffff;
862 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
863 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
865 error = netisr_queue(NETISR_IPV6, m);
868 RO_INVALIDATE_CACHE(ro);
869 needfiblookup = 1; /* Redo the routing table lookup. */
872 /* See if fib was changed by packet filter. */
873 if (fibnum != M_GETFIB(m)) {
874 m->m_flags |= M_SKIP_FIREWALL;
875 fibnum = M_GETFIB(m);
876 RO_INVALIDATE_CACHE(ro);
882 /* See if local, if yes, send it to netisr. */
883 if (m->m_flags & M_FASTFWD_OURS) {
884 if (m->m_pkthdr.rcvif == NULL)
885 m->m_pkthdr.rcvif = V_loif;
886 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
887 m->m_pkthdr.csum_flags |=
888 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
889 m->m_pkthdr.csum_data = 0xffff;
892 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
893 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
895 error = netisr_queue(NETISR_IPV6, m);
898 /* Or forward to some other address? */
899 if ((m->m_flags & M_IP6_NEXTHOP) &&
900 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
901 dst = (struct sockaddr_in6 *)&ro->ro_dst;
902 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
903 m->m_flags |= M_SKIP_FIREWALL;
904 m->m_flags &= ~M_IP6_NEXTHOP;
905 m_tag_delete(m, fwd_tag);
911 * Send the packet to the outgoing interface.
912 * If necessary, do IPv6 fragmentation before sending.
914 * the logic here is rather complex:
915 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
916 * 1-a: send as is if tlen <= path mtu
917 * 1-b: fragment if tlen > path mtu
919 * 2: if user asks us not to fragment (dontfrag == 1)
920 * 2-a: send as is if tlen <= interface mtu
921 * 2-b: error if tlen > interface mtu
923 * 3: if we always need to attach fragment header (alwaysfrag == 1)
926 * 4: if dontfrag == 1 && alwaysfrag == 1
927 * error, as we cannot handle this conflicting request
929 sw_csum = m->m_pkthdr.csum_flags;
931 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
932 sw_csum &= ~ifp->if_hwassist;
936 * If we added extension headers, we will not do TSO and calculate the
937 * checksums ourselves for now.
938 * XXX-BZ Need a framework to know when the NIC can handle it, even
941 error = ip6_output_delayed_csum(m, ifp, sw_csum, plen, optlen, false);
944 /* XXX-BZ m->m_pkthdr.csum_flags &= ~ifp->if_hwassist; */
945 tlen = m->m_pkthdr.len;
947 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
951 if (dontfrag && alwaysfrag) { /* case 4 */
952 /* conflicting request - can't transmit */
956 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
958 * Even if the DONTFRAG option is specified, we cannot send the
959 * packet when the data length is larger than the MTU of the
960 * outgoing interface.
961 * Notify the error by sending IPV6_PATHMTU ancillary data if
962 * application wanted to know the MTU value. Also return an
963 * error code (this is not described in the API spec).
966 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
972 * transmit packet without fragmentation
974 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
975 struct in6_ifaddr *ia6;
977 ip6 = mtod(m, struct ip6_hdr *);
978 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
980 /* Record statistics for this interface address. */
981 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
982 counter_u64_add(ia6->ia_ifa.ifa_obytes,
984 ifa_free(&ia6->ia_ifa);
988 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
989 in_pcboutput_txrtlmt(inp, ifp, m);
990 /* stamp send tag on mbuf */
991 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
992 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
994 m->m_pkthdr.snd_tag = NULL;
997 error = nd6_output_ifp(ifp, origifp, m, dst,
1000 /* check for route change */
1001 if (error == EAGAIN)
1002 in_pcboutput_eagain(inp);
1008 * try to fragment the packet. case 1-b and 3
1010 if (mtu < IPV6_MMTU) {
1011 /* path MTU cannot be less than IPV6_MMTU */
1013 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1015 } else if (ip6->ip6_plen == 0) {
1016 /* jumbo payload cannot be fragmented */
1018 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1024 * Too large for the destination or interface;
1025 * fragment if possible.
1026 * Must be able to put at least 8 bytes per fragment.
1028 hlen = unfragpartlen;
1029 if (mtu > IPV6_MAXPACKET)
1030 mtu = IPV6_MAXPACKET;
1032 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1035 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1040 * If the interface will not calculate checksums on
1041 * fragmented packets, then do it here.
1042 * XXX-BZ handle the hw offloading case. Need flags.
1044 error = ip6_output_delayed_csum(m, ifp, m->m_pkthdr.csum_flags,
1045 plen, optlen, true);
1050 * Change the next header field of the last header in the
1051 * unfragmentable part.
1053 if (exthdrs.ip6e_rthdr) {
1054 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1055 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1056 } else if (exthdrs.ip6e_dest1) {
1057 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1058 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1059 } else if (exthdrs.ip6e_hbh) {
1060 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1061 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1063 nextproto = ip6->ip6_nxt;
1064 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1068 * Loop through length of segment after first fragment,
1069 * make new header and copy data of each part and link onto
1073 id = htonl(ip6_randomid());
1074 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1077 in6_ifstat_inc(ifp, ifs6_out_fragok);
1081 * Remove leading garbages.
1091 /* Record statistics for this interface address. */
1093 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1094 counter_u64_add(ia->ia_ifa.ifa_obytes,
1099 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
1100 in_pcboutput_txrtlmt(inp, ifp, m);
1101 /* stamp send tag on mbuf */
1102 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
1103 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
1105 m->m_pkthdr.snd_tag = NULL;
1108 error = nd6_output_ifp(ifp, origifp, m, dst,
1109 (struct route *)ro);
1111 /* check for route change */
1112 if (error == EAGAIN)
1113 in_pcboutput_eagain(inp);
1120 IP6STAT_INC(ip6s_fragmented);
1123 if (ro == &ip6route)
1128 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1129 m_freem(exthdrs.ip6e_dest1);
1130 m_freem(exthdrs.ip6e_rthdr);
1131 m_freem(exthdrs.ip6e_dest2);
1140 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1144 if (hlen > MCLBYTES)
1145 return (ENOBUFS); /* XXX */
1148 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1150 m = m_get(M_NOWAIT, MT_DATA);
1155 bcopy(hdr, mtod(m, caddr_t), hlen);
1162 * Insert jumbo payload option.
1165 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1171 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1174 * If there is no hop-by-hop options header, allocate new one.
1175 * If there is one but it doesn't have enough space to store the
1176 * jumbo payload option, allocate a cluster to store the whole options.
1177 * Otherwise, use it to store the options.
1179 if (exthdrs->ip6e_hbh == NULL) {
1180 mopt = m_get(M_NOWAIT, MT_DATA);
1183 mopt->m_len = JUMBOOPTLEN;
1184 optbuf = mtod(mopt, u_char *);
1185 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1186 exthdrs->ip6e_hbh = mopt;
1188 struct ip6_hbh *hbh;
1190 mopt = exthdrs->ip6e_hbh;
1191 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1194 * - exthdrs->ip6e_hbh is not referenced from places
1195 * other than exthdrs.
1196 * - exthdrs->ip6e_hbh is not an mbuf chain.
1198 int oldoptlen = mopt->m_len;
1202 * XXX: give up if the whole (new) hbh header does
1203 * not fit even in an mbuf cluster.
1205 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1209 * As a consequence, we must always prepare a cluster
1212 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1215 n->m_len = oldoptlen + JUMBOOPTLEN;
1216 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1218 optbuf = mtod(n, caddr_t) + oldoptlen;
1220 mopt = exthdrs->ip6e_hbh = n;
1222 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1223 mopt->m_len += JUMBOOPTLEN;
1225 optbuf[0] = IP6OPT_PADN;
1229 * Adjust the header length according to the pad and
1230 * the jumbo payload option.
1232 hbh = mtod(mopt, struct ip6_hbh *);
1233 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1236 /* fill in the option. */
1237 optbuf[2] = IP6OPT_JUMBO;
1239 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1240 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1242 /* finally, adjust the packet header length */
1243 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1250 * Insert fragment header and copy unfragmentable header portions.
1253 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1254 struct ip6_frag **frghdrp)
1256 struct mbuf *n, *mlast;
1258 if (hlen > sizeof(struct ip6_hdr)) {
1259 n = m_copym(m0, sizeof(struct ip6_hdr),
1260 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1267 /* Search for the last mbuf of unfragmentable part. */
1268 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1271 if (M_WRITABLE(mlast) &&
1272 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1273 /* use the trailing space of the last mbuf for the fragment hdr */
1274 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1276 mlast->m_len += sizeof(struct ip6_frag);
1277 m->m_pkthdr.len += sizeof(struct ip6_frag);
1279 /* allocate a new mbuf for the fragment header */
1282 mfrg = m_get(M_NOWAIT, MT_DATA);
1285 mfrg->m_len = sizeof(struct ip6_frag);
1286 *frghdrp = mtod(mfrg, struct ip6_frag *);
1287 mlast->m_next = mfrg;
1294 * Calculates IPv6 path mtu for destination @dst.
1295 * Resulting MTU is stored in @mtup.
1297 * Returns 0 on success.
1300 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1302 struct nhop6_extended nh6;
1303 struct in6_addr kdst;
1309 in6_splitscope(dst, &kdst, &scopeid);
1310 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1311 return (EHOSTUNREACH);
1316 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1317 fib6_free_nh_ext(fibnum, &nh6);
1323 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1324 * and cached data in @ro_pmtu.
1325 * MTU from (successful) route lookup is saved (along with dst)
1326 * inside @ro_pmtu to avoid subsequent route lookups after packet
1327 * filter processing.
1329 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1330 * Returns 0 on success.
1333 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1334 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1335 int *alwaysfragp, u_int fibnum, u_int proto)
1337 struct nhop6_basic nh6;
1338 struct in6_addr kdst;
1340 struct sockaddr_in6 *sa6_dst;
1347 * Here ro_pmtu has final destination address, while
1348 * ro might represent immediate destination.
1349 * Use ro_pmtu destination since mtu might differ.
1351 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1352 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1353 ro_pmtu->ro_mtu = 0;
1355 if (ro_pmtu->ro_mtu == 0) {
1356 bzero(sa6_dst, sizeof(*sa6_dst));
1357 sa6_dst->sin6_family = AF_INET6;
1358 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1359 sa6_dst->sin6_addr = *dst;
1361 in6_splitscope(dst, &kdst, &scopeid);
1362 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1364 ro_pmtu->ro_mtu = nh6.nh_mtu;
1367 mtu = ro_pmtu->ro_mtu;
1371 mtu = ro_pmtu->ro_rt->rt_mtu;
1373 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1377 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1378 * hostcache data for @dst.
1379 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1381 * Returns 0 on success.
1384 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1385 u_long *mtup, int *alwaysfragp, u_int proto)
1393 struct in_conninfo inc;
1395 bzero(&inc, sizeof(inc));
1396 inc.inc_flags |= INC_ISIPV6;
1397 inc.inc6_faddr = *dst;
1399 ifmtu = IN6_LINKMTU(ifp);
1401 /* TCP is known to react to pmtu changes so skip hc */
1402 if (proto != IPPROTO_TCP)
1403 mtu = tcp_hc_getmtu(&inc);
1406 mtu = min(mtu, rt_mtu);
1411 else if (mtu < IPV6_MMTU) {
1413 * RFC2460 section 5, last paragraph:
1414 * if we record ICMPv6 too big message with
1415 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1416 * or smaller, with framgent header attached.
1417 * (fragment header is needed regardless from the
1418 * packet size, for translators to identify packets)
1424 mtu = IN6_LINKMTU(ifp);
1426 error = EHOSTUNREACH; /* XXX */
1430 *alwaysfragp = alwaysfrag;
1435 * IP6 socket option processing.
1438 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1440 int optdatalen, uproto;
1442 struct inpcb *in6p = sotoinpcb(so);
1444 int level, op, optname;
1448 uint32_t rss_bucket;
1453 * Don't use more than a quarter of mbuf clusters. N.B.:
1454 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1455 * on LP64 architectures, so cast to u_long to avoid undefined
1456 * behavior. ILP32 architectures cannot have nmbclusters
1457 * large enough to overflow for other reasons.
1459 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1461 level = sopt->sopt_level;
1462 op = sopt->sopt_dir;
1463 optname = sopt->sopt_name;
1464 optlen = sopt->sopt_valsize;
1468 uproto = (int)so->so_proto->pr_protocol;
1470 if (level != IPPROTO_IPV6) {
1473 if (sopt->sopt_level == SOL_SOCKET &&
1474 sopt->sopt_dir == SOPT_SET) {
1475 switch (sopt->sopt_name) {
1478 if ((so->so_options & SO_REUSEADDR) != 0)
1479 in6p->inp_flags2 |= INP_REUSEADDR;
1481 in6p->inp_flags2 &= ~INP_REUSEADDR;
1487 if ((so->so_options & SO_REUSEPORT) != 0)
1488 in6p->inp_flags2 |= INP_REUSEPORT;
1490 in6p->inp_flags2 &= ~INP_REUSEPORT;
1494 case SO_REUSEPORT_LB:
1496 if ((so->so_options & SO_REUSEPORT_LB) != 0)
1497 in6p->inp_flags2 |= INP_REUSEPORT_LB;
1499 in6p->inp_flags2 &= ~INP_REUSEPORT_LB;
1505 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1509 case SO_MAX_PACING_RATE:
1512 in6p->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1523 } else { /* level == IPPROTO_IPV6 */
1528 case IPV6_2292PKTOPTIONS:
1529 #ifdef IPV6_PKTOPTIONS
1530 case IPV6_PKTOPTIONS:
1535 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1536 printf("ip6_ctloutput: mbuf limit hit\n");
1541 error = soopt_getm(sopt, &m); /* XXX */
1544 error = soopt_mcopyin(sopt, m); /* XXX */
1548 error = ip6_pcbopts(&in6p->in6p_outputopts,
1551 m_freem(m); /* XXX */
1556 * Use of some Hop-by-Hop options or some
1557 * Destination options, might require special
1558 * privilege. That is, normal applications
1559 * (without special privilege) might be forbidden
1560 * from setting certain options in outgoing packets,
1561 * and might never see certain options in received
1562 * packets. [RFC 2292 Section 6]
1563 * KAME specific note:
1564 * KAME prevents non-privileged users from sending or
1565 * receiving ANY hbh/dst options in order to avoid
1566 * overhead of parsing options in the kernel.
1568 case IPV6_RECVHOPOPTS:
1569 case IPV6_RECVDSTOPTS:
1570 case IPV6_RECVRTHDRDSTOPTS:
1572 error = priv_check(td,
1573 PRIV_NETINET_SETHDROPTS);
1578 case IPV6_UNICAST_HOPS:
1581 case IPV6_RECVPKTINFO:
1582 case IPV6_RECVHOPLIMIT:
1583 case IPV6_RECVRTHDR:
1584 case IPV6_RECVPATHMTU:
1585 case IPV6_RECVTCLASS:
1586 case IPV6_RECVFLOWID:
1588 case IPV6_RECVRSSBUCKETID:
1591 case IPV6_AUTOFLOWLABEL:
1592 case IPV6_ORIGDSTADDR:
1594 case IPV6_BINDMULTI:
1596 case IPV6_RSS_LISTEN_BUCKET:
1598 if (optname == IPV6_BINDANY && td != NULL) {
1599 error = priv_check(td,
1600 PRIV_NETINET_BINDANY);
1605 if (optlen != sizeof(int)) {
1609 error = sooptcopyin(sopt, &optval,
1610 sizeof optval, sizeof optval);
1615 case IPV6_UNICAST_HOPS:
1616 if (optval < -1 || optval >= 256)
1619 /* -1 = kernel default */
1620 in6p->in6p_hops = optval;
1621 if ((in6p->inp_vflag &
1623 in6p->inp_ip_ttl = optval;
1626 #define OPTSET(bit) \
1630 in6p->inp_flags |= (bit); \
1632 in6p->inp_flags &= ~(bit); \
1633 INP_WUNLOCK(in6p); \
1634 } while (/*CONSTCOND*/ 0)
1635 #define OPTSET2292(bit) \
1638 in6p->inp_flags |= IN6P_RFC2292; \
1640 in6p->inp_flags |= (bit); \
1642 in6p->inp_flags &= ~(bit); \
1643 INP_WUNLOCK(in6p); \
1644 } while (/*CONSTCOND*/ 0)
1645 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1647 #define OPTSET2_N(bit, val) do { \
1649 in6p->inp_flags2 |= bit; \
1651 in6p->inp_flags2 &= ~bit; \
1653 #define OPTSET2(bit, val) do { \
1655 OPTSET2_N(bit, val); \
1656 INP_WUNLOCK(in6p); \
1658 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1659 #define OPTSET2292_EXCLUSIVE(bit) \
1662 if (OPTBIT(IN6P_RFC2292)) { \
1666 in6p->inp_flags |= (bit); \
1668 in6p->inp_flags &= ~(bit); \
1670 INP_WUNLOCK(in6p); \
1671 } while (/*CONSTCOND*/ 0)
1673 case IPV6_RECVPKTINFO:
1674 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1679 struct ip6_pktopts **optp;
1681 /* cannot mix with RFC2292 */
1682 if (OPTBIT(IN6P_RFC2292)) {
1687 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1689 return (ECONNRESET);
1691 optp = &in6p->in6p_outputopts;
1692 error = ip6_pcbopt(IPV6_HOPLIMIT,
1693 (u_char *)&optval, sizeof(optval),
1694 optp, (td != NULL) ? td->td_ucred :
1700 case IPV6_RECVHOPLIMIT:
1701 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1704 case IPV6_RECVHOPOPTS:
1705 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1708 case IPV6_RECVDSTOPTS:
1709 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1712 case IPV6_RECVRTHDRDSTOPTS:
1713 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1716 case IPV6_RECVRTHDR:
1717 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1720 case IPV6_RECVPATHMTU:
1722 * We ignore this option for TCP
1724 * (RFC3542 leaves this case
1727 if (uproto != IPPROTO_TCP)
1731 case IPV6_RECVFLOWID:
1732 OPTSET2(INP_RECVFLOWID, optval);
1736 case IPV6_RECVRSSBUCKETID:
1737 OPTSET2(INP_RECVRSSBUCKETID, optval);
1743 * make setsockopt(IPV6_V6ONLY)
1744 * available only prior to bind(2).
1745 * see ipng mailing list, Jun 22 2001.
1747 if (in6p->inp_lport ||
1748 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1752 OPTSET(IN6P_IPV6_V6ONLY);
1754 in6p->inp_vflag &= ~INP_IPV4;
1756 in6p->inp_vflag |= INP_IPV4;
1758 case IPV6_RECVTCLASS:
1759 /* cannot mix with RFC2292 XXX */
1760 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1762 case IPV6_AUTOFLOWLABEL:
1763 OPTSET(IN6P_AUTOFLOWLABEL);
1766 case IPV6_ORIGDSTADDR:
1767 OPTSET2(INP_ORIGDSTADDR, optval);
1770 OPTSET(INP_BINDANY);
1773 case IPV6_BINDMULTI:
1774 OPTSET2(INP_BINDMULTI, optval);
1777 case IPV6_RSS_LISTEN_BUCKET:
1778 if ((optval >= 0) &&
1779 (optval < rss_getnumbuckets())) {
1781 in6p->inp_rss_listen_bucket = optval;
1782 OPTSET2_N(INP_RSS_BUCKET_SET, 1);
1794 case IPV6_USE_MIN_MTU:
1795 case IPV6_PREFER_TEMPADDR:
1796 if (optlen != sizeof(optval)) {
1800 error = sooptcopyin(sopt, &optval,
1801 sizeof optval, sizeof optval);
1805 struct ip6_pktopts **optp;
1807 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1809 return (ECONNRESET);
1811 optp = &in6p->in6p_outputopts;
1812 error = ip6_pcbopt(optname,
1813 (u_char *)&optval, sizeof(optval),
1814 optp, (td != NULL) ? td->td_ucred :
1820 case IPV6_2292PKTINFO:
1821 case IPV6_2292HOPLIMIT:
1822 case IPV6_2292HOPOPTS:
1823 case IPV6_2292DSTOPTS:
1824 case IPV6_2292RTHDR:
1826 if (optlen != sizeof(int)) {
1830 error = sooptcopyin(sopt, &optval,
1831 sizeof optval, sizeof optval);
1835 case IPV6_2292PKTINFO:
1836 OPTSET2292(IN6P_PKTINFO);
1838 case IPV6_2292HOPLIMIT:
1839 OPTSET2292(IN6P_HOPLIMIT);
1841 case IPV6_2292HOPOPTS:
1843 * Check super-user privilege.
1844 * See comments for IPV6_RECVHOPOPTS.
1847 error = priv_check(td,
1848 PRIV_NETINET_SETHDROPTS);
1852 OPTSET2292(IN6P_HOPOPTS);
1854 case IPV6_2292DSTOPTS:
1856 error = priv_check(td,
1857 PRIV_NETINET_SETHDROPTS);
1861 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1863 case IPV6_2292RTHDR:
1864 OPTSET2292(IN6P_RTHDR);
1872 case IPV6_RTHDRDSTOPTS:
1875 /* new advanced API (RFC3542) */
1877 u_char optbuf_storage[MCLBYTES];
1879 struct ip6_pktopts **optp;
1881 /* cannot mix with RFC2292 */
1882 if (OPTBIT(IN6P_RFC2292)) {
1888 * We only ensure valsize is not too large
1889 * here. Further validation will be done
1892 error = sooptcopyin(sopt, optbuf_storage,
1893 sizeof(optbuf_storage), 0);
1896 optlen = sopt->sopt_valsize;
1897 optbuf = optbuf_storage;
1899 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1901 return (ECONNRESET);
1903 optp = &in6p->in6p_outputopts;
1904 error = ip6_pcbopt(optname, optbuf, optlen,
1905 optp, (td != NULL) ? td->td_ucred : NULL,
1912 case IPV6_MULTICAST_IF:
1913 case IPV6_MULTICAST_HOPS:
1914 case IPV6_MULTICAST_LOOP:
1915 case IPV6_JOIN_GROUP:
1916 case IPV6_LEAVE_GROUP:
1918 case MCAST_BLOCK_SOURCE:
1919 case MCAST_UNBLOCK_SOURCE:
1920 case MCAST_JOIN_GROUP:
1921 case MCAST_LEAVE_GROUP:
1922 case MCAST_JOIN_SOURCE_GROUP:
1923 case MCAST_LEAVE_SOURCE_GROUP:
1924 error = ip6_setmoptions(in6p, sopt);
1927 case IPV6_PORTRANGE:
1928 error = sooptcopyin(sopt, &optval,
1929 sizeof optval, sizeof optval);
1935 case IPV6_PORTRANGE_DEFAULT:
1936 in6p->inp_flags &= ~(INP_LOWPORT);
1937 in6p->inp_flags &= ~(INP_HIGHPORT);
1940 case IPV6_PORTRANGE_HIGH:
1941 in6p->inp_flags &= ~(INP_LOWPORT);
1942 in6p->inp_flags |= INP_HIGHPORT;
1945 case IPV6_PORTRANGE_LOW:
1946 in6p->inp_flags &= ~(INP_HIGHPORT);
1947 in6p->inp_flags |= INP_LOWPORT;
1957 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1958 case IPV6_IPSEC_POLICY:
1959 if (IPSEC_ENABLED(ipv6)) {
1960 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
1967 error = ENOPROTOOPT;
1975 case IPV6_2292PKTOPTIONS:
1976 #ifdef IPV6_PKTOPTIONS
1977 case IPV6_PKTOPTIONS:
1980 * RFC3542 (effectively) deprecated the
1981 * semantics of the 2292-style pktoptions.
1982 * Since it was not reliable in nature (i.e.,
1983 * applications had to expect the lack of some
1984 * information after all), it would make sense
1985 * to simplify this part by always returning
1988 sopt->sopt_valsize = 0;
1991 case IPV6_RECVHOPOPTS:
1992 case IPV6_RECVDSTOPTS:
1993 case IPV6_RECVRTHDRDSTOPTS:
1994 case IPV6_UNICAST_HOPS:
1995 case IPV6_RECVPKTINFO:
1996 case IPV6_RECVHOPLIMIT:
1997 case IPV6_RECVRTHDR:
1998 case IPV6_RECVPATHMTU:
2001 case IPV6_PORTRANGE:
2002 case IPV6_RECVTCLASS:
2003 case IPV6_AUTOFLOWLABEL:
2007 case IPV6_RECVFLOWID:
2009 case IPV6_RSSBUCKETID:
2010 case IPV6_RECVRSSBUCKETID:
2012 case IPV6_BINDMULTI:
2015 case IPV6_RECVHOPOPTS:
2016 optval = OPTBIT(IN6P_HOPOPTS);
2019 case IPV6_RECVDSTOPTS:
2020 optval = OPTBIT(IN6P_DSTOPTS);
2023 case IPV6_RECVRTHDRDSTOPTS:
2024 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2027 case IPV6_UNICAST_HOPS:
2028 optval = in6p->in6p_hops;
2031 case IPV6_RECVPKTINFO:
2032 optval = OPTBIT(IN6P_PKTINFO);
2035 case IPV6_RECVHOPLIMIT:
2036 optval = OPTBIT(IN6P_HOPLIMIT);
2039 case IPV6_RECVRTHDR:
2040 optval = OPTBIT(IN6P_RTHDR);
2043 case IPV6_RECVPATHMTU:
2044 optval = OPTBIT(IN6P_MTU);
2048 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2051 case IPV6_PORTRANGE:
2054 flags = in6p->inp_flags;
2055 if (flags & INP_HIGHPORT)
2056 optval = IPV6_PORTRANGE_HIGH;
2057 else if (flags & INP_LOWPORT)
2058 optval = IPV6_PORTRANGE_LOW;
2063 case IPV6_RECVTCLASS:
2064 optval = OPTBIT(IN6P_TCLASS);
2067 case IPV6_AUTOFLOWLABEL:
2068 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2071 case IPV6_ORIGDSTADDR:
2072 optval = OPTBIT2(INP_ORIGDSTADDR);
2076 optval = OPTBIT(INP_BINDANY);
2080 optval = in6p->inp_flowid;
2084 optval = in6p->inp_flowtype;
2087 case IPV6_RECVFLOWID:
2088 optval = OPTBIT2(INP_RECVFLOWID);
2091 case IPV6_RSSBUCKETID:
2093 rss_hash2bucket(in6p->inp_flowid,
2097 optval = rss_bucket;
2102 case IPV6_RECVRSSBUCKETID:
2103 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2107 case IPV6_BINDMULTI:
2108 optval = OPTBIT2(INP_BINDMULTI);
2114 error = sooptcopyout(sopt, &optval,
2121 struct ip6_mtuinfo mtuinfo;
2122 struct in6_addr addr;
2124 if (!(so->so_state & SS_ISCONNECTED))
2127 * XXX: we dot not consider the case of source
2128 * routing, or optional information to specify
2129 * the outgoing interface.
2130 * Copy faddr out of in6p to avoid holding lock
2131 * on inp during route lookup.
2134 bcopy(&in6p->in6p_faddr, &addr, sizeof(addr));
2136 error = ip6_getpmtu_ctl(so->so_fibnum,
2140 if (pmtu > IPV6_MAXPACKET)
2141 pmtu = IPV6_MAXPACKET;
2143 bzero(&mtuinfo, sizeof(mtuinfo));
2144 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2145 optdata = (void *)&mtuinfo;
2146 optdatalen = sizeof(mtuinfo);
2147 error = sooptcopyout(sopt, optdata,
2152 case IPV6_2292PKTINFO:
2153 case IPV6_2292HOPLIMIT:
2154 case IPV6_2292HOPOPTS:
2155 case IPV6_2292RTHDR:
2156 case IPV6_2292DSTOPTS:
2158 case IPV6_2292PKTINFO:
2159 optval = OPTBIT(IN6P_PKTINFO);
2161 case IPV6_2292HOPLIMIT:
2162 optval = OPTBIT(IN6P_HOPLIMIT);
2164 case IPV6_2292HOPOPTS:
2165 optval = OPTBIT(IN6P_HOPOPTS);
2167 case IPV6_2292RTHDR:
2168 optval = OPTBIT(IN6P_RTHDR);
2170 case IPV6_2292DSTOPTS:
2171 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2174 error = sooptcopyout(sopt, &optval,
2181 case IPV6_RTHDRDSTOPTS:
2185 case IPV6_USE_MIN_MTU:
2186 case IPV6_PREFER_TEMPADDR:
2187 error = ip6_getpcbopt(in6p, optname, sopt);
2190 case IPV6_MULTICAST_IF:
2191 case IPV6_MULTICAST_HOPS:
2192 case IPV6_MULTICAST_LOOP:
2194 error = ip6_getmoptions(in6p, sopt);
2197 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2198 case IPV6_IPSEC_POLICY:
2199 if (IPSEC_ENABLED(ipv6)) {
2200 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
2206 error = ENOPROTOOPT;
2216 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2218 int error = 0, optval, optlen;
2219 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2220 struct inpcb *in6p = sotoinpcb(so);
2221 int level, op, optname;
2223 level = sopt->sopt_level;
2224 op = sopt->sopt_dir;
2225 optname = sopt->sopt_name;
2226 optlen = sopt->sopt_valsize;
2228 if (level != IPPROTO_IPV6) {
2235 * For ICMPv6 sockets, no modification allowed for checksum
2236 * offset, permit "no change" values to help existing apps.
2238 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2239 * for an ICMPv6 socket will fail."
2240 * The current behavior does not meet RFC3542.
2244 if (optlen != sizeof(int)) {
2248 error = sooptcopyin(sopt, &optval, sizeof(optval),
2252 if (optval < -1 || (optval % 2) != 0) {
2254 * The API assumes non-negative even offset
2255 * values or -1 as a special value.
2258 } else if (so->so_proto->pr_protocol ==
2260 if (optval != icmp6off)
2263 in6p->in6p_cksum = optval;
2267 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2270 optval = in6p->in6p_cksum;
2272 error = sooptcopyout(sopt, &optval, sizeof(optval));
2282 error = ENOPROTOOPT;
2290 * Set up IP6 options in pcb for insertion in output packets or
2291 * specifying behavior of outgoing packets.
2294 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2295 struct socket *so, struct sockopt *sopt)
2297 struct ip6_pktopts *opt = *pktopt;
2299 struct thread *td = sopt->sopt_td;
2301 /* turn off any old options. */
2304 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2305 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2306 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2307 printf("ip6_pcbopts: all specified options are cleared.\n");
2309 ip6_clearpktopts(opt, -1);
2311 opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT);
2317 if (!m || m->m_len == 0) {
2319 * Only turning off any previous options, regardless of
2320 * whether the opt is just created or given.
2322 free(opt, M_IP6OPT);
2326 /* set options specified by user. */
2327 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2328 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2329 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2330 free(opt, M_IP6OPT);
2338 * initialize ip6_pktopts. beware that there are non-zero default values in
2342 ip6_initpktopts(struct ip6_pktopts *opt)
2345 bzero(opt, sizeof(*opt));
2346 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2347 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2348 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2349 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2353 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2354 struct ucred *cred, int uproto)
2356 struct ip6_pktopts *opt;
2358 if (*pktopt == NULL) {
2359 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2361 if (*pktopt == NULL)
2363 ip6_initpktopts(*pktopt);
2367 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2370 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2371 if (pktopt && pktopt->field) { \
2372 INP_RUNLOCK(in6p); \
2373 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2374 malloc_optdata = true; \
2376 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
2377 INP_RUNLOCK(in6p); \
2378 free(optdata, M_TEMP); \
2379 return (ECONNRESET); \
2381 pktopt = in6p->in6p_outputopts; \
2382 if (pktopt && pktopt->field) { \
2383 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2384 bcopy(&pktopt->field, optdata, optdatalen); \
2386 free(optdata, M_TEMP); \
2388 malloc_optdata = false; \
2393 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2394 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2396 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2397 pktopt->field->sa_len)
2400 ip6_getpcbopt(struct inpcb *in6p, int optname, struct sockopt *sopt)
2402 void *optdata = NULL;
2403 bool malloc_optdata = false;
2406 struct in6_pktinfo null_pktinfo;
2407 int deftclass = 0, on;
2408 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2409 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2410 struct ip6_pktopts *pktopt;
2413 pktopt = in6p->in6p_outputopts;
2417 optdata = (void *)&null_pktinfo;
2418 if (pktopt && pktopt->ip6po_pktinfo) {
2419 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2420 sizeof(null_pktinfo));
2421 in6_clearscope(&null_pktinfo.ipi6_addr);
2423 /* XXX: we don't have to do this every time... */
2424 bzero(&null_pktinfo, sizeof(null_pktinfo));
2426 optdatalen = sizeof(struct in6_pktinfo);
2429 if (pktopt && pktopt->ip6po_tclass >= 0)
2430 deftclass = pktopt->ip6po_tclass;
2431 optdata = (void *)&deftclass;
2432 optdatalen = sizeof(int);
2435 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2438 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2440 case IPV6_RTHDRDSTOPTS:
2441 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2444 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2447 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2449 case IPV6_USE_MIN_MTU:
2451 defminmtu = pktopt->ip6po_minmtu;
2452 optdata = (void *)&defminmtu;
2453 optdatalen = sizeof(int);
2456 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2460 optdata = (void *)&on;
2461 optdatalen = sizeof(on);
2463 case IPV6_PREFER_TEMPADDR:
2465 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2466 optdata = (void *)&defpreftemp;
2467 optdatalen = sizeof(int);
2469 default: /* should not happen */
2471 panic("ip6_getpcbopt: unexpected option\n");
2474 return (ENOPROTOOPT);
2478 error = sooptcopyout(sopt, optdata, optdatalen);
2480 free(optdata, M_TEMP);
2486 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2491 if (optname == -1 || optname == IPV6_PKTINFO) {
2492 if (pktopt->ip6po_pktinfo)
2493 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2494 pktopt->ip6po_pktinfo = NULL;
2496 if (optname == -1 || optname == IPV6_HOPLIMIT)
2497 pktopt->ip6po_hlim = -1;
2498 if (optname == -1 || optname == IPV6_TCLASS)
2499 pktopt->ip6po_tclass = -1;
2500 if (optname == -1 || optname == IPV6_NEXTHOP) {
2501 if (pktopt->ip6po_nextroute.ro_rt) {
2502 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2503 pktopt->ip6po_nextroute.ro_rt = NULL;
2505 if (pktopt->ip6po_nexthop)
2506 free(pktopt->ip6po_nexthop, M_IP6OPT);
2507 pktopt->ip6po_nexthop = NULL;
2509 if (optname == -1 || optname == IPV6_HOPOPTS) {
2510 if (pktopt->ip6po_hbh)
2511 free(pktopt->ip6po_hbh, M_IP6OPT);
2512 pktopt->ip6po_hbh = NULL;
2514 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2515 if (pktopt->ip6po_dest1)
2516 free(pktopt->ip6po_dest1, M_IP6OPT);
2517 pktopt->ip6po_dest1 = NULL;
2519 if (optname == -1 || optname == IPV6_RTHDR) {
2520 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2521 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2522 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2523 if (pktopt->ip6po_route.ro_rt) {
2524 RTFREE(pktopt->ip6po_route.ro_rt);
2525 pktopt->ip6po_route.ro_rt = NULL;
2528 if (optname == -1 || optname == IPV6_DSTOPTS) {
2529 if (pktopt->ip6po_dest2)
2530 free(pktopt->ip6po_dest2, M_IP6OPT);
2531 pktopt->ip6po_dest2 = NULL;
2535 #define PKTOPT_EXTHDRCPY(type) \
2538 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2539 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2540 if (dst->type == NULL)\
2542 bcopy(src->type, dst->type, hlen);\
2544 } while (/*CONSTCOND*/ 0)
2547 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2549 if (dst == NULL || src == NULL) {
2550 printf("ip6_clearpktopts: invalid argument\n");
2554 dst->ip6po_hlim = src->ip6po_hlim;
2555 dst->ip6po_tclass = src->ip6po_tclass;
2556 dst->ip6po_flags = src->ip6po_flags;
2557 dst->ip6po_minmtu = src->ip6po_minmtu;
2558 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2559 if (src->ip6po_pktinfo) {
2560 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2562 if (dst->ip6po_pktinfo == NULL)
2564 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2566 if (src->ip6po_nexthop) {
2567 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2569 if (dst->ip6po_nexthop == NULL)
2571 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2572 src->ip6po_nexthop->sa_len);
2574 PKTOPT_EXTHDRCPY(ip6po_hbh);
2575 PKTOPT_EXTHDRCPY(ip6po_dest1);
2576 PKTOPT_EXTHDRCPY(ip6po_dest2);
2577 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2581 ip6_clearpktopts(dst, -1);
2584 #undef PKTOPT_EXTHDRCPY
2586 struct ip6_pktopts *
2587 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2590 struct ip6_pktopts *dst;
2592 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2595 ip6_initpktopts(dst);
2597 if ((error = copypktopts(dst, src, canwait)) != 0) {
2598 free(dst, M_IP6OPT);
2606 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2611 ip6_clearpktopts(pktopt, -1);
2613 free(pktopt, M_IP6OPT);
2617 * Set IPv6 outgoing packet options based on advanced API.
2620 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2621 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2623 struct cmsghdr *cm = NULL;
2625 if (control == NULL || opt == NULL)
2628 ip6_initpktopts(opt);
2633 * If stickyopt is provided, make a local copy of the options
2634 * for this particular packet, then override them by ancillary
2636 * XXX: copypktopts() does not copy the cached route to a next
2637 * hop (if any). This is not very good in terms of efficiency,
2638 * but we can allow this since this option should be rarely
2641 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2646 * XXX: Currently, we assume all the optional information is stored
2649 if (control->m_next)
2652 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2653 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2656 if (control->m_len < CMSG_LEN(0))
2659 cm = mtod(control, struct cmsghdr *);
2660 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2662 if (cm->cmsg_level != IPPROTO_IPV6)
2665 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2666 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2675 * Set a particular packet option, as a sticky option or an ancillary data
2676 * item. "len" can be 0 only when it's a sticky option.
2677 * We have 4 cases of combination of "sticky" and "cmsg":
2678 * "sticky=0, cmsg=0": impossible
2679 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2680 * "sticky=1, cmsg=0": RFC3542 socket option
2681 * "sticky=1, cmsg=1": RFC2292 socket option
2684 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2685 struct ucred *cred, int sticky, int cmsg, int uproto)
2687 int minmtupolicy, preftemp;
2690 if (!sticky && !cmsg) {
2692 printf("ip6_setpktopt: impossible case\n");
2698 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2699 * not be specified in the context of RFC3542. Conversely,
2700 * RFC3542 types should not be specified in the context of RFC2292.
2704 case IPV6_2292PKTINFO:
2705 case IPV6_2292HOPLIMIT:
2706 case IPV6_2292NEXTHOP:
2707 case IPV6_2292HOPOPTS:
2708 case IPV6_2292DSTOPTS:
2709 case IPV6_2292RTHDR:
2710 case IPV6_2292PKTOPTIONS:
2711 return (ENOPROTOOPT);
2714 if (sticky && cmsg) {
2721 case IPV6_RTHDRDSTOPTS:
2723 case IPV6_USE_MIN_MTU:
2726 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2727 return (ENOPROTOOPT);
2732 case IPV6_2292PKTINFO:
2735 struct ifnet *ifp = NULL;
2736 struct in6_pktinfo *pktinfo;
2738 if (len != sizeof(struct in6_pktinfo))
2741 pktinfo = (struct in6_pktinfo *)buf;
2744 * An application can clear any sticky IPV6_PKTINFO option by
2745 * doing a "regular" setsockopt with ipi6_addr being
2746 * in6addr_any and ipi6_ifindex being zero.
2747 * [RFC 3542, Section 6]
2749 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2750 pktinfo->ipi6_ifindex == 0 &&
2751 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2752 ip6_clearpktopts(opt, optname);
2756 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2757 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2760 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2762 /* validate the interface index if specified. */
2763 if (pktinfo->ipi6_ifindex > V_if_index)
2765 if (pktinfo->ipi6_ifindex) {
2766 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2770 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2771 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2775 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2776 struct in6_ifaddr *ia;
2778 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2779 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2781 return (EADDRNOTAVAIL);
2782 ifa_free(&ia->ia_ifa);
2785 * We store the address anyway, and let in6_selectsrc()
2786 * validate the specified address. This is because ipi6_addr
2787 * may not have enough information about its scope zone, and
2788 * we may need additional information (such as outgoing
2789 * interface or the scope zone of a destination address) to
2790 * disambiguate the scope.
2791 * XXX: the delay of the validation may confuse the
2792 * application when it is used as a sticky option.
2794 if (opt->ip6po_pktinfo == NULL) {
2795 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2796 M_IP6OPT, M_NOWAIT);
2797 if (opt->ip6po_pktinfo == NULL)
2800 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2804 case IPV6_2292HOPLIMIT:
2810 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2811 * to simplify the ordering among hoplimit options.
2813 if (optname == IPV6_HOPLIMIT && sticky)
2814 return (ENOPROTOOPT);
2816 if (len != sizeof(int))
2819 if (*hlimp < -1 || *hlimp > 255)
2822 opt->ip6po_hlim = *hlimp;
2830 if (len != sizeof(int))
2832 tclass = *(int *)buf;
2833 if (tclass < -1 || tclass > 255)
2836 opt->ip6po_tclass = tclass;
2840 case IPV6_2292NEXTHOP:
2843 error = priv_check_cred(cred,
2844 PRIV_NETINET_SETHDROPTS, 0);
2849 if (len == 0) { /* just remove the option */
2850 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2854 /* check if cmsg_len is large enough for sa_len */
2855 if (len < sizeof(struct sockaddr) || len < *buf)
2858 switch (((struct sockaddr *)buf)->sa_family) {
2861 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2864 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2867 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2868 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2871 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2877 case AF_LINK: /* should eventually be supported */
2879 return (EAFNOSUPPORT);
2882 /* turn off the previous option, then set the new option. */
2883 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2884 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2885 if (opt->ip6po_nexthop == NULL)
2887 bcopy(buf, opt->ip6po_nexthop, *buf);
2890 case IPV6_2292HOPOPTS:
2893 struct ip6_hbh *hbh;
2897 * XXX: We don't allow a non-privileged user to set ANY HbH
2898 * options, since per-option restriction has too much
2902 error = priv_check_cred(cred,
2903 PRIV_NETINET_SETHDROPTS, 0);
2909 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2910 break; /* just remove the option */
2913 /* message length validation */
2914 if (len < sizeof(struct ip6_hbh))
2916 hbh = (struct ip6_hbh *)buf;
2917 hbhlen = (hbh->ip6h_len + 1) << 3;
2921 /* turn off the previous option, then set the new option. */
2922 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2923 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2924 if (opt->ip6po_hbh == NULL)
2926 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2931 case IPV6_2292DSTOPTS:
2933 case IPV6_RTHDRDSTOPTS:
2935 struct ip6_dest *dest, **newdest = NULL;
2938 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2939 error = priv_check_cred(cred,
2940 PRIV_NETINET_SETHDROPTS, 0);
2946 ip6_clearpktopts(opt, optname);
2947 break; /* just remove the option */
2950 /* message length validation */
2951 if (len < sizeof(struct ip6_dest))
2953 dest = (struct ip6_dest *)buf;
2954 destlen = (dest->ip6d_len + 1) << 3;
2959 * Determine the position that the destination options header
2960 * should be inserted; before or after the routing header.
2963 case IPV6_2292DSTOPTS:
2965 * The old advacned API is ambiguous on this point.
2966 * Our approach is to determine the position based
2967 * according to the existence of a routing header.
2968 * Note, however, that this depends on the order of the
2969 * extension headers in the ancillary data; the 1st
2970 * part of the destination options header must appear
2971 * before the routing header in the ancillary data,
2973 * RFC3542 solved the ambiguity by introducing
2974 * separate ancillary data or option types.
2976 if (opt->ip6po_rthdr == NULL)
2977 newdest = &opt->ip6po_dest1;
2979 newdest = &opt->ip6po_dest2;
2981 case IPV6_RTHDRDSTOPTS:
2982 newdest = &opt->ip6po_dest1;
2985 newdest = &opt->ip6po_dest2;
2989 /* turn off the previous option, then set the new option. */
2990 ip6_clearpktopts(opt, optname);
2991 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2992 if (*newdest == NULL)
2994 bcopy(dest, *newdest, destlen);
2999 case IPV6_2292RTHDR:
3002 struct ip6_rthdr *rth;
3006 ip6_clearpktopts(opt, IPV6_RTHDR);
3007 break; /* just remove the option */
3010 /* message length validation */
3011 if (len < sizeof(struct ip6_rthdr))
3013 rth = (struct ip6_rthdr *)buf;
3014 rthlen = (rth->ip6r_len + 1) << 3;
3018 switch (rth->ip6r_type) {
3019 case IPV6_RTHDR_TYPE_0:
3020 if (rth->ip6r_len == 0) /* must contain one addr */
3022 if (rth->ip6r_len % 2) /* length must be even */
3024 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3028 return (EINVAL); /* not supported */
3031 /* turn off the previous option */
3032 ip6_clearpktopts(opt, IPV6_RTHDR);
3033 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3034 if (opt->ip6po_rthdr == NULL)
3036 bcopy(rth, opt->ip6po_rthdr, rthlen);
3041 case IPV6_USE_MIN_MTU:
3042 if (len != sizeof(int))
3044 minmtupolicy = *(int *)buf;
3045 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3046 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3047 minmtupolicy != IP6PO_MINMTU_ALL) {
3050 opt->ip6po_minmtu = minmtupolicy;
3054 if (len != sizeof(int))
3057 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3059 * we ignore this option for TCP sockets.
3060 * (RFC3542 leaves this case unspecified.)
3062 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3064 opt->ip6po_flags |= IP6PO_DONTFRAG;
3067 case IPV6_PREFER_TEMPADDR:
3068 if (len != sizeof(int))
3070 preftemp = *(int *)buf;
3071 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3072 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3073 preftemp != IP6PO_TEMPADDR_PREFER) {
3076 opt->ip6po_prefer_tempaddr = preftemp;
3080 return (ENOPROTOOPT);
3081 } /* end of switch */
3087 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3088 * packet to the input queue of a specified interface. Note that this
3089 * calls the output routine of the loopback "driver", but with an interface
3090 * pointer that might NOT be &loif -- easier than replicating that code here.
3093 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3096 struct ip6_hdr *ip6;
3098 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3103 * Make sure to deep-copy IPv6 header portion in case the data
3104 * is in an mbuf cluster, so that we can safely override the IPv6
3105 * header portion later.
3107 if (!M_WRITABLE(copym) ||
3108 copym->m_len < sizeof(struct ip6_hdr)) {
3109 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3113 ip6 = mtod(copym, struct ip6_hdr *);
3115 * clear embedded scope identifiers if necessary.
3116 * in6_clearscope will touch the addresses only when necessary.
3118 in6_clearscope(&ip6->ip6_src);
3119 in6_clearscope(&ip6->ip6_dst);
3120 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3121 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3123 copym->m_pkthdr.csum_data = 0xffff;
3125 if_simloop(ifp, copym, AF_INET6, 0);
3129 * Chop IPv6 header off from the payload.
3132 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3135 struct ip6_hdr *ip6;
3137 ip6 = mtod(m, struct ip6_hdr *);
3138 if (m->m_len > sizeof(*ip6)) {
3139 mh = m_gethdr(M_NOWAIT, MT_DATA);
3144 m_move_pkthdr(mh, m);
3145 M_ALIGN(mh, sizeof(*ip6));
3146 m->m_len -= sizeof(*ip6);
3147 m->m_data += sizeof(*ip6);
3150 m->m_len = sizeof(*ip6);
3151 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3153 exthdrs->ip6e_ip6 = m;
3158 * Compute IPv6 extension header length.
3161 ip6_optlen(struct inpcb *in6p)
3165 if (!in6p->in6p_outputopts)
3170 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3172 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3173 if (in6p->in6p_outputopts->ip6po_rthdr)
3174 /* dest1 is valid with rthdr only */
3175 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3176 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3177 len += elen(in6p->in6p_outputopts->ip6po_dest2);