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,
158 * mp is the destination, and _ol is the optlen.
160 #define MAKE_EXTHDR(hp, mp, _ol) \
163 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
164 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
165 ((eh)->ip6e_len + 1) << 3); \
168 (_ol) += (*(mp))->m_len; \
170 } while (/*CONSTCOND*/ 0)
173 * Form a chain of extension headers.
174 * m is the extension header mbuf
175 * mp is the previous mbuf in the chain
176 * p is the next header
177 * i is the type of option.
179 #define MAKE_CHAIN(m, mp, p, i)\
183 panic("%s:%d: assumption failed: "\
184 "hdr not split: hdrsplit %d exthdrs %p",\
185 __func__, __LINE__, hdrsplit, &exthdrs);\
186 *mtod((m), u_char *) = *(p);\
188 p = mtod((m), u_char *);\
189 (m)->m_next = (mp)->m_next;\
193 } while (/*CONSTCOND*/ 0)
196 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
200 csum = in_cksum_skip(m, offset + plen, offset);
201 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
203 offset += m->m_pkthdr.csum_data; /* checksum offset */
205 if (offset + sizeof(csum) > m->m_len)
206 m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
208 *(u_short *)mtodo(m, offset) = csum;
212 ip6_output_delayed_csum(struct mbuf *m, struct ifnet *ifp, int csum_flags,
213 int plen, int optlen, bool frag __unused)
216 KASSERT((plen >= optlen), ("%s:%d: plen %d < optlen %d, m %p, ifp %p "
217 "csum_flags %#x frag %d\n",
218 __func__, __LINE__, plen, optlen, m, ifp, csum_flags, frag));
220 if ((csum_flags & CSUM_DELAY_DATA_IPV6) ||
222 (csum_flags & CSUM_SCTP_IPV6) ||
225 if (csum_flags & CSUM_DELAY_DATA_IPV6) {
226 in6_delayed_cksum(m, plen - optlen,
227 sizeof(struct ip6_hdr) + optlen);
228 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
231 if (csum_flags & CSUM_SCTP_IPV6) {
232 sctp_delayed_cksum(m, sizeof(struct ip6_hdr) + optlen);
233 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
242 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
243 int fraglen , uint32_t id)
245 struct mbuf *m, **mnext, *m_frgpart;
246 struct ip6_hdr *ip6, *mhip6;
247 struct ip6_frag *ip6f;
250 int tlen = m0->m_pkthdr.len;
252 KASSERT((fraglen % 8 == 0), ("Fragment length must be a multiple of 8"));
255 ip6 = mtod(m, struct ip6_hdr *);
256 mnext = &m->m_nextpkt;
258 for (off = hlen; off < tlen; off += fraglen) {
259 m = m_gethdr(M_NOWAIT, MT_DATA);
261 IP6STAT_INC(ip6s_odropped);
266 * Make sure the complete packet header gets copied
267 * from the originating mbuf to the newly created
268 * mbuf. This also ensures that existing firewall
269 * classification(s), VLAN tags and so on get copied
270 * to the resulting fragmented packet(s):
272 if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
274 IP6STAT_INC(ip6s_odropped);
279 mnext = &m->m_nextpkt;
280 m->m_data += max_linkhdr;
281 mhip6 = mtod(m, struct ip6_hdr *);
283 m->m_len = sizeof(*mhip6);
284 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
286 IP6STAT_INC(ip6s_odropped);
289 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
290 if (off + fraglen >= tlen)
291 fraglen = tlen - off;
293 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
294 mhip6->ip6_plen = htons((u_short)(fraglen + hlen +
295 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
296 if ((m_frgpart = m_copym(m0, off, fraglen, M_NOWAIT)) == NULL) {
297 IP6STAT_INC(ip6s_odropped);
301 m->m_pkthdr.len = fraglen + hlen + sizeof(*ip6f);
302 ip6f->ip6f_reserved = 0;
303 ip6f->ip6f_ident = id;
304 ip6f->ip6f_nxt = nextproto;
305 IP6STAT_INC(ip6s_ofragments);
306 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
314 * The packet in mbuf chain m contains a skeletal IP6 header (with pri, len,
315 * nxt, hlim, src, dst).
316 * This function may modify ver and hlim only.
317 * The mbuf chain containing the packet will be freed.
318 * The mbuf opt, if present, will not be freed.
319 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
320 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
321 * then result of route lookup is stored in ro->ro_rt.
323 * Type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and nd_ifinfo.linkmtu
324 * is uint32_t. So we use u_long to hold largest one, which is rt_mtu.
326 * ifpp - XXX: just for statistics
329 * XXX TODO: no flowid is assigned for outbound flows?
332 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
333 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
334 struct ifnet **ifpp, struct inpcb *inp)
337 struct ifnet *ifp, *origifp;
341 struct route_in6 ip6route;
342 struct rtentry *rt = NULL;
343 struct sockaddr_in6 *dst, src_sa, dst_sa;
344 struct in6_addr odst;
347 struct in6_ifaddr *ia = NULL;
349 int alwaysfrag, dontfrag;
350 u_int32_t optlen, plen = 0, unfragpartlen;
351 struct ip6_exthdrs exthdrs;
352 struct in6_addr src0, dst0;
354 struct route_in6 *ro_pmtu = NULL;
359 struct m_tag *fwd_tag = NULL;
363 INP_LOCK_ASSERT(inp);
364 M_SETFIB(m, inp->inp_inc.inc_fibnum);
365 if ((flags & IP_NODEFAULTFLOWID) == 0) {
366 /* Unconditionally set flowid. */
367 m->m_pkthdr.flowid = inp->inp_flowid;
368 M_HASHTYPE_SET(m, inp->inp_flowtype);
372 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
374 * IPSec checking which handles several cases.
375 * FAST IPSEC: We re-injected the packet.
376 * XXX: need scope argument.
378 if (IPSEC_ENABLED(ipv6)) {
379 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
380 if (error == EINPROGRESS)
387 /* Source address validation. */
388 ip6 = mtod(m, struct ip6_hdr *);
389 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
390 (flags & IPV6_UNSPECSRC) == 0) {
392 IP6STAT_INC(ip6s_badscope);
395 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
397 IP6STAT_INC(ip6s_badscope);
402 * If we are given packet options to add extension headers prepare them.
403 * Calculate the total length of the extension header chain.
404 * Keep the length of the unfragmentable part for fragmentation.
406 bzero(&exthdrs, sizeof(exthdrs));
408 unfragpartlen = sizeof(struct ip6_hdr);
410 /* Hop-by-Hop options header. */
411 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh, optlen);
413 /* Destination options header (1st part). */
414 if (opt->ip6po_rthdr) {
415 #ifndef RTHDR_SUPPORT_IMPLEMENTED
417 * If there is a routing header, discard the packet
418 * right away here. RH0/1 are obsolete and we do not
419 * currently support RH2/3/4.
420 * People trying to use RH253/254 may want to disable
422 * The moment we do support any routing header (again)
423 * this block should check the routing type more
431 * Destination options header (1st part).
432 * This only makes sense with a routing header.
433 * See Section 9.2 of RFC 3542.
434 * Disabling this part just for MIP6 convenience is
435 * a bad idea. We need to think carefully about a
436 * way to make the advanced API coexist with MIP6
437 * options, which might automatically be inserted in
440 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1,
443 /* Routing header. */
444 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr, optlen);
446 unfragpartlen += optlen;
449 * NOTE: we don't add AH/ESP length here (done in
450 * ip6_ipsec_output()).
453 /* Destination options header (2nd part). */
454 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2, optlen);
458 * If there is at least one extension header,
459 * separate IP6 header from the payload.
463 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
467 m = exthdrs.ip6e_ip6;
468 ip6 = mtod(m, struct ip6_hdr *);
472 /* Adjust mbuf packet header length. */
473 m->m_pkthdr.len += optlen;
474 plen = m->m_pkthdr.len - sizeof(*ip6);
476 /* If this is a jumbo payload, insert a jumbo payload option. */
477 if (plen > IPV6_MAXPACKET) {
479 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
483 m = exthdrs.ip6e_ip6;
484 ip6 = mtod(m, struct ip6_hdr *);
487 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
491 ip6->ip6_plen = htons(plen);
492 nexthdrp = &ip6->ip6_nxt;
496 * Concatenate headers and fill in next header fields.
497 * Here we have, on "m"
499 * and we insert headers accordingly.
500 * Finally, we should be getting:
501 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload].
503 * During the header composing process "m" points to IPv6
504 * header. "mprev" points to an extension header prior to esp.
509 * We treat dest2 specially. This makes IPsec processing
510 * much easier. The goal here is to make mprev point the
511 * mbuf prior to dest2.
513 * Result: IPv6 dest2 payload.
514 * m and mprev will point to IPv6 header.
516 if (exthdrs.ip6e_dest2) {
518 panic("%s:%d: assumption failed: "
519 "hdr not split: hdrsplit %d exthdrs %p",
520 __func__, __LINE__, hdrsplit, &exthdrs);
521 exthdrs.ip6e_dest2->m_next = m->m_next;
522 m->m_next = exthdrs.ip6e_dest2;
523 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
524 ip6->ip6_nxt = IPPROTO_DSTOPTS;
528 * Result: IPv6 hbh dest1 rthdr dest2 payload.
529 * m will point to IPv6 header. mprev will point to the
530 * extension header prior to dest2 (rthdr in the above case).
532 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
533 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
535 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
539 IP6STAT_INC(ip6s_localout);
544 bzero((caddr_t)ro, sizeof(*ro));
547 if (opt && opt->ip6po_rthdr)
548 ro = &opt->ip6po_route;
549 dst = (struct sockaddr_in6 *)&ro->ro_dst;
550 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
553 * If specified, try to fill in the traffic class field.
554 * Do not override if a non-zero value is already set.
555 * We check the diffserv field and the ECN field separately.
557 if (opt && opt->ip6po_tclass >= 0) {
560 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
562 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
565 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
568 /* Fill in or override the hop limit field, if necessary. */
569 if (opt && opt->ip6po_hlim != -1)
570 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
571 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
573 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
575 ip6->ip6_hlim = V_ip6_defmcasthlim;
578 * Validate route against routing table additions;
579 * a better/more specific route might have been added.
580 * Make sure that the address family is set in route.
583 ro->ro_dst.sin6_family = AF_INET6;
584 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
586 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
587 ro->ro_dst.sin6_family == AF_INET6 &&
588 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
590 ifp = ro->ro_rt->rt_ifp;
593 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
595 if (fwd_tag == NULL) {
596 bzero(&dst_sa, sizeof(dst_sa));
597 dst_sa.sin6_family = AF_INET6;
598 dst_sa.sin6_len = sizeof(dst_sa);
599 dst_sa.sin6_addr = ip6->ip6_dst;
601 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
605 in6_ifstat_inc(ifp, ifs6_out_discard);
611 * If in6_selectroute() does not return a route entry
612 * dst may not have been updated.
614 *dst = dst_sa; /* XXX */
617 /* Then rt (for unicast) and ifp must be non-NULL valid values. */
618 if ((flags & IPV6_FORWARDING) == 0) {
619 /* XXX: the FORWARDING flag can be set for mrouting. */
620 in6_ifstat_inc(ifp, ifs6_out_request);
623 ia = (struct in6_ifaddr *)(rt->rt_ifa);
624 counter_u64_add(rt->rt_pksent, 1);
627 /* Setup data structures for scope ID checks. */
629 bzero(&src_sa, sizeof(src_sa));
630 src_sa.sin6_family = AF_INET6;
631 src_sa.sin6_len = sizeof(src_sa);
632 src_sa.sin6_addr = ip6->ip6_src;
635 /* Re-initialize to be sure. */
636 bzero(&dst_sa, sizeof(dst_sa));
637 dst_sa.sin6_family = AF_INET6;
638 dst_sa.sin6_len = sizeof(dst_sa);
639 dst_sa.sin6_addr = ip6->ip6_dst;
641 /* Check for valid scope ID. */
642 if (in6_setscope(&src0, ifp, &zone) == 0 &&
643 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
644 in6_setscope(&dst0, ifp, &zone) == 0 &&
645 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
647 * The outgoing interface is in the zone of the source
648 * and destination addresses.
650 * Because the loopback interface cannot receive
651 * packets with a different scope ID than its own,
652 * there is a trick to pretend the outgoing packet
653 * was received by the real network interface, by
654 * setting "origifp" different from "ifp". This is
655 * only allowed when "ifp" is a loopback network
656 * interface. Refer to code in nd6_output_ifp() for
662 * We should use ia_ifp to support the case of sending
663 * packets to an address of our own.
665 if (ia != NULL && ia->ia_ifp)
668 } else if ((ifp->if_flags & IFF_LOOPBACK) == 0 ||
669 sa6_recoverscope(&src_sa) != 0 ||
670 sa6_recoverscope(&dst_sa) != 0 ||
671 dst_sa.sin6_scope_id == 0 ||
672 (src_sa.sin6_scope_id != 0 &&
673 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
674 (origifp = ifnet_byindex(dst_sa.sin6_scope_id)) == NULL) {
676 * If the destination network interface is not a
677 * loopback interface, or the destination network
678 * address has no scope ID, or the source address has
679 * a scope ID set which is different from the
680 * destination address one, or there is no network
681 * interface representing this scope ID, the address
682 * pair is considered invalid.
684 IP6STAT_INC(ip6s_badscope);
685 in6_ifstat_inc(ifp, ifs6_out_discard);
687 error = EHOSTUNREACH; /* XXX */
690 /* All scope ID checks are successful. */
692 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
693 if (opt && opt->ip6po_nextroute.ro_rt) {
695 * The nexthop is explicitly specified by the
696 * application. We assume the next hop is an IPv6
699 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
701 else if ((rt->rt_flags & RTF_GATEWAY))
702 dst = (struct sockaddr_in6 *)rt->rt_gateway;
705 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
706 m->m_flags &= ~(M_BCAST | M_MCAST); /* Just in case. */
708 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
709 in6_ifstat_inc(ifp, ifs6_out_mcast);
711 /* Confirm that the outgoing interface supports multicast. */
712 if (!(ifp->if_flags & IFF_MULTICAST)) {
713 IP6STAT_INC(ip6s_noroute);
714 in6_ifstat_inc(ifp, ifs6_out_discard);
718 if ((im6o == NULL && in6_mcast_loop) ||
719 (im6o && im6o->im6o_multicast_loop)) {
721 * Loop back multicast datagram if not expressly
722 * forbidden to do so, even if we have not joined
723 * the address; protocols will filter it later,
724 * thus deferring a hash lookup and lock acquisition
725 * at the expense of an m_copym().
727 ip6_mloopback(ifp, m);
730 * If we are acting as a multicast router, perform
731 * multicast forwarding as if the packet had just
732 * arrived on the interface to which we are about
733 * to send. The multicast forwarding function
734 * recursively calls this function, using the
735 * IPV6_FORWARDING flag to prevent infinite recursion.
737 * Multicasts that are looped back by ip6_mloopback(),
738 * above, will be forwarded by the ip6_input() routine,
741 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
743 * XXX: ip6_mforward expects that rcvif is NULL
744 * when it is called from the originating path.
745 * However, it may not always be the case.
747 m->m_pkthdr.rcvif = NULL;
748 if (ip6_mforward(ip6, ifp, m) != 0) {
755 * Multicasts with a hoplimit of zero may be looped back,
756 * above, but must not be transmitted on a network.
757 * Also, multicasts addressed to the loopback interface
758 * are not sent -- the above call to ip6_mloopback() will
759 * loop back a copy if this host actually belongs to the
760 * destination group on the loopback interface.
762 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
763 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
770 * Fill the outgoing inteface to tell the upper layer
771 * to increment per-interface statistics.
776 /* Determine path MTU. */
777 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
778 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
782 * The caller of this function may specify to use the minimum MTU
784 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
785 * setting. The logic is a bit complicated; by default, unicast
786 * packets will follow path MTU while multicast packets will be sent at
787 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
788 * including unicast ones will be sent at the minimum MTU. Multicast
789 * packets will always be sent at the minimum MTU unless
790 * IP6PO_MINMTU_DISABLE is explicitly specified.
791 * See RFC 3542 for more details.
793 if (mtu > IPV6_MMTU) {
794 if ((flags & IPV6_MINMTU))
796 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
798 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
800 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
806 * Clear embedded scope identifiers if necessary.
807 * in6_clearscope() will touch the addresses only when necessary.
809 in6_clearscope(&ip6->ip6_src);
810 in6_clearscope(&ip6->ip6_dst);
813 * If the outgoing packet contains a hop-by-hop options header,
814 * it must be examined and processed even by the source node.
815 * (RFC 2460, section 4.)
817 if (exthdrs.ip6e_hbh) {
818 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
819 u_int32_t dummy; /* XXX unused */
820 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
823 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
824 panic("ip6e_hbh is not contiguous");
827 * XXX: if we have to send an ICMPv6 error to the sender,
828 * we need the M_LOOP flag since icmp6_error() expects
829 * the IPv6 and the hop-by-hop options header are
830 * contiguous unless the flag is set.
832 m->m_flags |= M_LOOP;
833 m->m_pkthdr.rcvif = ifp;
834 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
835 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
836 &dummy, &plen) < 0) {
837 /* m was already freed at this point. */
838 error = EINVAL;/* better error? */
841 m->m_flags &= ~M_LOOP; /* XXX */
842 m->m_pkthdr.rcvif = NULL;
845 /* Jump over all PFIL processing if hooks are not active. */
846 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
850 /* Run through list of hooks for output packets. */
851 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, 0, inp);
852 if (error != 0 || m == NULL)
855 ip6 = mtod(m, struct ip6_hdr *);
858 /* See if destination IP address was changed by packet filter. */
859 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
860 m->m_flags |= M_SKIP_FIREWALL;
861 /* If destination is now ourself drop to ip6_input(). */
862 if (in6_localip(&ip6->ip6_dst)) {
863 m->m_flags |= M_FASTFWD_OURS;
864 if (m->m_pkthdr.rcvif == NULL)
865 m->m_pkthdr.rcvif = V_loif;
866 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
867 m->m_pkthdr.csum_flags |=
868 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
869 m->m_pkthdr.csum_data = 0xffff;
872 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
873 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
875 error = netisr_queue(NETISR_IPV6, m);
878 RO_INVALIDATE_CACHE(ro);
879 needfiblookup = 1; /* Redo the routing table lookup. */
882 /* See if fib was changed by packet filter. */
883 if (fibnum != M_GETFIB(m)) {
884 m->m_flags |= M_SKIP_FIREWALL;
885 fibnum = M_GETFIB(m);
886 RO_INVALIDATE_CACHE(ro);
892 /* See if local, if yes, send it to netisr. */
893 if (m->m_flags & M_FASTFWD_OURS) {
894 if (m->m_pkthdr.rcvif == NULL)
895 m->m_pkthdr.rcvif = V_loif;
896 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
897 m->m_pkthdr.csum_flags |=
898 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
899 m->m_pkthdr.csum_data = 0xffff;
902 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
903 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
905 error = netisr_queue(NETISR_IPV6, m);
908 /* Or forward to some other address? */
909 if ((m->m_flags & M_IP6_NEXTHOP) &&
910 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
911 dst = (struct sockaddr_in6 *)&ro->ro_dst;
912 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
913 m->m_flags |= M_SKIP_FIREWALL;
914 m->m_flags &= ~M_IP6_NEXTHOP;
915 m_tag_delete(m, fwd_tag);
921 * Send the packet to the outgoing interface.
922 * If necessary, do IPv6 fragmentation before sending.
924 * The logic here is rather complex:
925 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
926 * 1-a: send as is if tlen <= path mtu
927 * 1-b: fragment if tlen > path mtu
929 * 2: if user asks us not to fragment (dontfrag == 1)
930 * 2-a: send as is if tlen <= interface mtu
931 * 2-b: error if tlen > interface mtu
933 * 3: if we always need to attach fragment header (alwaysfrag == 1)
936 * 4: if dontfrag == 1 && alwaysfrag == 1
937 * error, as we cannot handle this conflicting request.
939 sw_csum = m->m_pkthdr.csum_flags;
941 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
942 sw_csum &= ~ifp->if_hwassist;
946 * If we added extension headers, we will not do TSO and calculate the
947 * checksums ourselves for now.
948 * XXX-BZ Need a framework to know when the NIC can handle it, even
951 error = ip6_output_delayed_csum(m, ifp, sw_csum, plen, optlen, false);
954 /* XXX-BZ m->m_pkthdr.csum_flags &= ~ifp->if_hwassist; */
955 tlen = m->m_pkthdr.len;
957 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
961 if (dontfrag && alwaysfrag) { /* Case 4. */
962 /* Conflicting request - can't transmit. */
966 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* Case 2-b. */
968 * Even if the DONTFRAG option is specified, we cannot send the
969 * packet when the data length is larger than the MTU of the
970 * outgoing interface.
971 * Notify the error by sending IPV6_PATHMTU ancillary data if
972 * application wanted to know the MTU value. Also return an
973 * error code (this is not described in the API spec).
976 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
981 /* Transmit packet without fragmentation. */
982 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* Cases 1-a and 2-a. */
983 struct in6_ifaddr *ia6;
985 ip6 = mtod(m, struct ip6_hdr *);
986 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
988 /* Record statistics for this interface address. */
989 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
990 counter_u64_add(ia6->ia_ifa.ifa_obytes,
992 ifa_free(&ia6->ia_ifa);
996 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
997 in_pcboutput_txrtlmt(inp, ifp, m);
998 /* stamp send tag on mbuf */
999 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
1000 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
1002 m->m_pkthdr.snd_tag = NULL;
1005 error = nd6_output_ifp(ifp, origifp, m, dst,
1006 (struct route *)ro);
1008 /* check for route change */
1009 if (error == EAGAIN)
1010 in_pcboutput_eagain(inp);
1015 /* Try to fragment the packet. Cases 1-b and 3. */
1016 if (mtu < IPV6_MMTU) {
1017 /* Path MTU cannot be less than IPV6_MMTU. */
1019 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1021 } else if (ip6->ip6_plen == 0) {
1022 /* Jumbo payload cannot be fragmented. */
1024 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1030 * Too large for the destination or interface;
1031 * fragment if possible.
1032 * Must be able to put at least 8 bytes per fragment.
1034 if (mtu > IPV6_MAXPACKET)
1035 mtu = IPV6_MAXPACKET;
1037 len = (mtu - unfragpartlen - sizeof(struct ip6_frag)) & ~7;
1040 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1045 * If the interface will not calculate checksums on
1046 * fragmented packets, then do it here.
1047 * XXX-BZ handle the hw offloading case. Need flags.
1049 error = ip6_output_delayed_csum(m, ifp, m->m_pkthdr.csum_flags,
1050 plen, optlen, true);
1055 * Change the next header field of the last header in the
1056 * unfragmentable part.
1058 if (exthdrs.ip6e_rthdr) {
1059 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1060 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1061 } else if (exthdrs.ip6e_dest1) {
1062 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1063 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1064 } else if (exthdrs.ip6e_hbh) {
1065 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1066 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1068 ip6 = mtod(m, struct ip6_hdr *);
1069 nextproto = ip6->ip6_nxt;
1070 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1074 * Loop through length of segment after first fragment,
1075 * make new header and copy data of each part and link onto
1079 id = htonl(ip6_randomid());
1080 error = ip6_fragment(ifp, m, unfragpartlen, nextproto,len, id);
1084 in6_ifstat_inc(ifp, ifs6_out_fragok);
1087 /* Remove leading garbage. */
1096 /* Record statistics for this interface address. */
1098 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1099 counter_u64_add(ia->ia_ifa.ifa_obytes,
1104 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
1105 in_pcboutput_txrtlmt(inp, ifp, m);
1106 /* stamp send tag on mbuf */
1107 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
1108 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
1110 m->m_pkthdr.snd_tag = NULL;
1113 error = nd6_output_ifp(ifp, origifp, m, dst,
1114 (struct route *)ro);
1116 /* check for route change */
1117 if (error == EAGAIN)
1118 in_pcboutput_eagain(inp);
1125 IP6STAT_INC(ip6s_fragmented);
1128 if (ro == &ip6route)
1133 m_freem(exthdrs.ip6e_hbh); /* m_freem() checks if mbuf is NULL. */
1134 m_freem(exthdrs.ip6e_dest1);
1135 m_freem(exthdrs.ip6e_rthdr);
1136 m_freem(exthdrs.ip6e_dest2);
1145 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1149 if (hlen > MCLBYTES)
1150 return (ENOBUFS); /* XXX */
1153 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1155 m = m_get(M_NOWAIT, MT_DATA);
1160 bcopy(hdr, mtod(m, caddr_t), hlen);
1167 * Insert jumbo payload option.
1170 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1176 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1179 * If there is no hop-by-hop options header, allocate new one.
1180 * If there is one but it doesn't have enough space to store the
1181 * jumbo payload option, allocate a cluster to store the whole options.
1182 * Otherwise, use it to store the options.
1184 if (exthdrs->ip6e_hbh == NULL) {
1185 mopt = m_get(M_NOWAIT, MT_DATA);
1188 mopt->m_len = JUMBOOPTLEN;
1189 optbuf = mtod(mopt, u_char *);
1190 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1191 exthdrs->ip6e_hbh = mopt;
1193 struct ip6_hbh *hbh;
1195 mopt = exthdrs->ip6e_hbh;
1196 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1199 * - exthdrs->ip6e_hbh is not referenced from places
1200 * other than exthdrs.
1201 * - exthdrs->ip6e_hbh is not an mbuf chain.
1203 int oldoptlen = mopt->m_len;
1207 * XXX: give up if the whole (new) hbh header does
1208 * not fit even in an mbuf cluster.
1210 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1214 * As a consequence, we must always prepare a cluster
1217 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1220 n->m_len = oldoptlen + JUMBOOPTLEN;
1221 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1223 optbuf = mtod(n, caddr_t) + oldoptlen;
1225 mopt = exthdrs->ip6e_hbh = n;
1227 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1228 mopt->m_len += JUMBOOPTLEN;
1230 optbuf[0] = IP6OPT_PADN;
1234 * Adjust the header length according to the pad and
1235 * the jumbo payload option.
1237 hbh = mtod(mopt, struct ip6_hbh *);
1238 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1241 /* fill in the option. */
1242 optbuf[2] = IP6OPT_JUMBO;
1244 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1245 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1247 /* finally, adjust the packet header length */
1248 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1255 * Insert fragment header and copy unfragmentable header portions.
1258 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1259 struct ip6_frag **frghdrp)
1261 struct mbuf *n, *mlast;
1263 if (hlen > sizeof(struct ip6_hdr)) {
1264 n = m_copym(m0, sizeof(struct ip6_hdr),
1265 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1272 /* Search for the last mbuf of unfragmentable part. */
1273 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1276 if (M_WRITABLE(mlast) &&
1277 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1278 /* use the trailing space of the last mbuf for the fragment hdr */
1279 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1281 mlast->m_len += sizeof(struct ip6_frag);
1282 m->m_pkthdr.len += sizeof(struct ip6_frag);
1284 /* allocate a new mbuf for the fragment header */
1287 mfrg = m_get(M_NOWAIT, MT_DATA);
1290 mfrg->m_len = sizeof(struct ip6_frag);
1291 *frghdrp = mtod(mfrg, struct ip6_frag *);
1292 mlast->m_next = mfrg;
1299 * Calculates IPv6 path mtu for destination @dst.
1300 * Resulting MTU is stored in @mtup.
1302 * Returns 0 on success.
1305 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1307 struct nhop6_extended nh6;
1308 struct in6_addr kdst;
1314 in6_splitscope(dst, &kdst, &scopeid);
1315 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1316 return (EHOSTUNREACH);
1321 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1322 fib6_free_nh_ext(fibnum, &nh6);
1328 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1329 * and cached data in @ro_pmtu.
1330 * MTU from (successful) route lookup is saved (along with dst)
1331 * inside @ro_pmtu to avoid subsequent route lookups after packet
1332 * filter processing.
1334 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1335 * Returns 0 on success.
1338 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1339 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1340 int *alwaysfragp, u_int fibnum, u_int proto)
1342 struct nhop6_basic nh6;
1343 struct in6_addr kdst;
1345 struct sockaddr_in6 *sa6_dst;
1352 * Here ro_pmtu has final destination address, while
1353 * ro might represent immediate destination.
1354 * Use ro_pmtu destination since mtu might differ.
1356 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1357 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1358 ro_pmtu->ro_mtu = 0;
1360 if (ro_pmtu->ro_mtu == 0) {
1361 bzero(sa6_dst, sizeof(*sa6_dst));
1362 sa6_dst->sin6_family = AF_INET6;
1363 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1364 sa6_dst->sin6_addr = *dst;
1366 in6_splitscope(dst, &kdst, &scopeid);
1367 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1369 ro_pmtu->ro_mtu = nh6.nh_mtu;
1372 mtu = ro_pmtu->ro_mtu;
1376 mtu = ro_pmtu->ro_rt->rt_mtu;
1378 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1382 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1383 * hostcache data for @dst.
1384 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1386 * Returns 0 on success.
1389 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1390 u_long *mtup, int *alwaysfragp, u_int proto)
1398 struct in_conninfo inc;
1400 bzero(&inc, sizeof(inc));
1401 inc.inc_flags |= INC_ISIPV6;
1402 inc.inc6_faddr = *dst;
1404 ifmtu = IN6_LINKMTU(ifp);
1406 /* TCP is known to react to pmtu changes so skip hc */
1407 if (proto != IPPROTO_TCP)
1408 mtu = tcp_hc_getmtu(&inc);
1411 mtu = min(mtu, rt_mtu);
1416 else if (mtu < IPV6_MMTU) {
1418 * RFC2460 section 5, last paragraph:
1419 * if we record ICMPv6 too big message with
1420 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1421 * or smaller, with framgent header attached.
1422 * (fragment header is needed regardless from the
1423 * packet size, for translators to identify packets)
1429 mtu = IN6_LINKMTU(ifp);
1431 error = EHOSTUNREACH; /* XXX */
1435 *alwaysfragp = alwaysfrag;
1440 * IP6 socket option processing.
1443 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1445 int optdatalen, uproto;
1447 struct inpcb *inp = sotoinpcb(so);
1449 int level, op, optname;
1453 uint32_t rss_bucket;
1458 * Don't use more than a quarter of mbuf clusters. N.B.:
1459 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1460 * on LP64 architectures, so cast to u_long to avoid undefined
1461 * behavior. ILP32 architectures cannot have nmbclusters
1462 * large enough to overflow for other reasons.
1464 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1466 level = sopt->sopt_level;
1467 op = sopt->sopt_dir;
1468 optname = sopt->sopt_name;
1469 optlen = sopt->sopt_valsize;
1473 uproto = (int)so->so_proto->pr_protocol;
1475 if (level != IPPROTO_IPV6) {
1478 if (sopt->sopt_level == SOL_SOCKET &&
1479 sopt->sopt_dir == SOPT_SET) {
1480 switch (sopt->sopt_name) {
1483 if ((so->so_options & SO_REUSEADDR) != 0)
1484 inp->inp_flags2 |= INP_REUSEADDR;
1486 inp->inp_flags2 &= ~INP_REUSEADDR;
1492 if ((so->so_options & SO_REUSEPORT) != 0)
1493 inp->inp_flags2 |= INP_REUSEPORT;
1495 inp->inp_flags2 &= ~INP_REUSEPORT;
1499 case SO_REUSEPORT_LB:
1501 if ((so->so_options & SO_REUSEPORT_LB) != 0)
1502 inp->inp_flags2 |= INP_REUSEPORT_LB;
1504 inp->inp_flags2 &= ~INP_REUSEPORT_LB;
1510 inp->inp_inc.inc_fibnum = so->so_fibnum;
1514 case SO_MAX_PACING_RATE:
1517 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1528 } else { /* level == IPPROTO_IPV6 */
1533 case IPV6_2292PKTOPTIONS:
1534 #ifdef IPV6_PKTOPTIONS
1535 case IPV6_PKTOPTIONS:
1540 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1541 printf("ip6_ctloutput: mbuf limit hit\n");
1546 error = soopt_getm(sopt, &m); /* XXX */
1549 error = soopt_mcopyin(sopt, m); /* XXX */
1552 error = ip6_pcbopts(&inp->in6p_outputopts,
1554 m_freem(m); /* XXX */
1559 * Use of some Hop-by-Hop options or some
1560 * Destination options, might require special
1561 * privilege. That is, normal applications
1562 * (without special privilege) might be forbidden
1563 * from setting certain options in outgoing packets,
1564 * and might never see certain options in received
1565 * packets. [RFC 2292 Section 6]
1566 * KAME specific note:
1567 * KAME prevents non-privileged users from sending or
1568 * receiving ANY hbh/dst options in order to avoid
1569 * overhead of parsing options in the kernel.
1571 case IPV6_RECVHOPOPTS:
1572 case IPV6_RECVDSTOPTS:
1573 case IPV6_RECVRTHDRDSTOPTS:
1575 error = priv_check(td,
1576 PRIV_NETINET_SETHDROPTS);
1581 case IPV6_UNICAST_HOPS:
1584 case IPV6_RECVPKTINFO:
1585 case IPV6_RECVHOPLIMIT:
1586 case IPV6_RECVRTHDR:
1587 case IPV6_RECVPATHMTU:
1588 case IPV6_RECVTCLASS:
1589 case IPV6_RECVFLOWID:
1591 case IPV6_RECVRSSBUCKETID:
1594 case IPV6_AUTOFLOWLABEL:
1595 case IPV6_ORIGDSTADDR:
1597 case IPV6_BINDMULTI:
1599 case IPV6_RSS_LISTEN_BUCKET:
1601 if (optname == IPV6_BINDANY && td != NULL) {
1602 error = priv_check(td,
1603 PRIV_NETINET_BINDANY);
1608 if (optlen != sizeof(int)) {
1612 error = sooptcopyin(sopt, &optval,
1613 sizeof optval, sizeof optval);
1618 case IPV6_UNICAST_HOPS:
1619 if (optval < -1 || optval >= 256)
1622 /* -1 = kernel default */
1623 inp->in6p_hops = optval;
1624 if ((inp->inp_vflag &
1626 inp->inp_ip_ttl = optval;
1629 #define OPTSET(bit) \
1633 inp->inp_flags |= (bit); \
1635 inp->inp_flags &= ~(bit); \
1637 } while (/*CONSTCOND*/ 0)
1638 #define OPTSET2292(bit) \
1641 inp->inp_flags |= IN6P_RFC2292; \
1643 inp->inp_flags |= (bit); \
1645 inp->inp_flags &= ~(bit); \
1647 } while (/*CONSTCOND*/ 0)
1648 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0)
1650 #define OPTSET2_N(bit, val) do { \
1652 inp->inp_flags2 |= bit; \
1654 inp->inp_flags2 &= ~bit; \
1656 #define OPTSET2(bit, val) do { \
1658 OPTSET2_N(bit, val); \
1661 #define OPTBIT2(bit) (inp->inp_flags2 & (bit) ? 1 : 0)
1662 #define OPTSET2292_EXCLUSIVE(bit) \
1665 if (OPTBIT(IN6P_RFC2292)) { \
1669 inp->inp_flags |= (bit); \
1671 inp->inp_flags &= ~(bit); \
1674 } while (/*CONSTCOND*/ 0)
1676 case IPV6_RECVPKTINFO:
1677 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1682 struct ip6_pktopts **optp;
1684 /* cannot mix with RFC2292 */
1685 if (OPTBIT(IN6P_RFC2292)) {
1690 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1692 return (ECONNRESET);
1694 optp = &inp->in6p_outputopts;
1695 error = ip6_pcbopt(IPV6_HOPLIMIT,
1696 (u_char *)&optval, sizeof(optval),
1697 optp, (td != NULL) ? td->td_ucred :
1703 case IPV6_RECVHOPLIMIT:
1704 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1707 case IPV6_RECVHOPOPTS:
1708 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1711 case IPV6_RECVDSTOPTS:
1712 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1715 case IPV6_RECVRTHDRDSTOPTS:
1716 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1719 case IPV6_RECVRTHDR:
1720 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1723 case IPV6_RECVPATHMTU:
1725 * We ignore this option for TCP
1727 * (RFC3542 leaves this case
1730 if (uproto != IPPROTO_TCP)
1734 case IPV6_RECVFLOWID:
1735 OPTSET2(INP_RECVFLOWID, optval);
1739 case IPV6_RECVRSSBUCKETID:
1740 OPTSET2(INP_RECVRSSBUCKETID, optval);
1746 * make setsockopt(IPV6_V6ONLY)
1747 * available only prior to bind(2).
1748 * see ipng mailing list, Jun 22 2001.
1750 if (inp->inp_lport ||
1751 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1755 OPTSET(IN6P_IPV6_V6ONLY);
1757 inp->inp_vflag &= ~INP_IPV4;
1759 inp->inp_vflag |= INP_IPV4;
1761 case IPV6_RECVTCLASS:
1762 /* cannot mix with RFC2292 XXX */
1763 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1765 case IPV6_AUTOFLOWLABEL:
1766 OPTSET(IN6P_AUTOFLOWLABEL);
1769 case IPV6_ORIGDSTADDR:
1770 OPTSET2(INP_ORIGDSTADDR, optval);
1773 OPTSET(INP_BINDANY);
1776 case IPV6_BINDMULTI:
1777 OPTSET2(INP_BINDMULTI, optval);
1780 case IPV6_RSS_LISTEN_BUCKET:
1781 if ((optval >= 0) &&
1782 (optval < rss_getnumbuckets())) {
1784 inp->inp_rss_listen_bucket = optval;
1785 OPTSET2_N(INP_RSS_BUCKET_SET, 1);
1797 case IPV6_USE_MIN_MTU:
1798 case IPV6_PREFER_TEMPADDR:
1799 if (optlen != sizeof(optval)) {
1803 error = sooptcopyin(sopt, &optval,
1804 sizeof optval, sizeof optval);
1808 struct ip6_pktopts **optp;
1810 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1812 return (ECONNRESET);
1814 optp = &inp->in6p_outputopts;
1815 error = ip6_pcbopt(optname,
1816 (u_char *)&optval, sizeof(optval),
1817 optp, (td != NULL) ? td->td_ucred :
1823 case IPV6_2292PKTINFO:
1824 case IPV6_2292HOPLIMIT:
1825 case IPV6_2292HOPOPTS:
1826 case IPV6_2292DSTOPTS:
1827 case IPV6_2292RTHDR:
1829 if (optlen != sizeof(int)) {
1833 error = sooptcopyin(sopt, &optval,
1834 sizeof optval, sizeof optval);
1838 case IPV6_2292PKTINFO:
1839 OPTSET2292(IN6P_PKTINFO);
1841 case IPV6_2292HOPLIMIT:
1842 OPTSET2292(IN6P_HOPLIMIT);
1844 case IPV6_2292HOPOPTS:
1846 * Check super-user privilege.
1847 * See comments for IPV6_RECVHOPOPTS.
1850 error = priv_check(td,
1851 PRIV_NETINET_SETHDROPTS);
1855 OPTSET2292(IN6P_HOPOPTS);
1857 case IPV6_2292DSTOPTS:
1859 error = priv_check(td,
1860 PRIV_NETINET_SETHDROPTS);
1864 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1866 case IPV6_2292RTHDR:
1867 OPTSET2292(IN6P_RTHDR);
1875 case IPV6_RTHDRDSTOPTS:
1878 /* new advanced API (RFC3542) */
1880 u_char optbuf_storage[MCLBYTES];
1882 struct ip6_pktopts **optp;
1884 /* cannot mix with RFC2292 */
1885 if (OPTBIT(IN6P_RFC2292)) {
1891 * We only ensure valsize is not too large
1892 * here. Further validation will be done
1895 error = sooptcopyin(sopt, optbuf_storage,
1896 sizeof(optbuf_storage), 0);
1899 optlen = sopt->sopt_valsize;
1900 optbuf = optbuf_storage;
1902 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1904 return (ECONNRESET);
1906 optp = &inp->in6p_outputopts;
1907 error = ip6_pcbopt(optname, optbuf, optlen,
1908 optp, (td != NULL) ? td->td_ucred : NULL,
1915 case IPV6_MULTICAST_IF:
1916 case IPV6_MULTICAST_HOPS:
1917 case IPV6_MULTICAST_LOOP:
1918 case IPV6_JOIN_GROUP:
1919 case IPV6_LEAVE_GROUP:
1921 case MCAST_BLOCK_SOURCE:
1922 case MCAST_UNBLOCK_SOURCE:
1923 case MCAST_JOIN_GROUP:
1924 case MCAST_LEAVE_GROUP:
1925 case MCAST_JOIN_SOURCE_GROUP:
1926 case MCAST_LEAVE_SOURCE_GROUP:
1927 error = ip6_setmoptions(inp, sopt);
1930 case IPV6_PORTRANGE:
1931 error = sooptcopyin(sopt, &optval,
1932 sizeof optval, sizeof optval);
1938 case IPV6_PORTRANGE_DEFAULT:
1939 inp->inp_flags &= ~(INP_LOWPORT);
1940 inp->inp_flags &= ~(INP_HIGHPORT);
1943 case IPV6_PORTRANGE_HIGH:
1944 inp->inp_flags &= ~(INP_LOWPORT);
1945 inp->inp_flags |= INP_HIGHPORT;
1948 case IPV6_PORTRANGE_LOW:
1949 inp->inp_flags &= ~(INP_HIGHPORT);
1950 inp->inp_flags |= INP_LOWPORT;
1960 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1961 case IPV6_IPSEC_POLICY:
1962 if (IPSEC_ENABLED(ipv6)) {
1963 error = IPSEC_PCBCTL(ipv6, inp, sopt);
1970 error = ENOPROTOOPT;
1978 case IPV6_2292PKTOPTIONS:
1979 #ifdef IPV6_PKTOPTIONS
1980 case IPV6_PKTOPTIONS:
1983 * RFC3542 (effectively) deprecated the
1984 * semantics of the 2292-style pktoptions.
1985 * Since it was not reliable in nature (i.e.,
1986 * applications had to expect the lack of some
1987 * information after all), it would make sense
1988 * to simplify this part by always returning
1991 sopt->sopt_valsize = 0;
1994 case IPV6_RECVHOPOPTS:
1995 case IPV6_RECVDSTOPTS:
1996 case IPV6_RECVRTHDRDSTOPTS:
1997 case IPV6_UNICAST_HOPS:
1998 case IPV6_RECVPKTINFO:
1999 case IPV6_RECVHOPLIMIT:
2000 case IPV6_RECVRTHDR:
2001 case IPV6_RECVPATHMTU:
2004 case IPV6_PORTRANGE:
2005 case IPV6_RECVTCLASS:
2006 case IPV6_AUTOFLOWLABEL:
2010 case IPV6_RECVFLOWID:
2012 case IPV6_RSSBUCKETID:
2013 case IPV6_RECVRSSBUCKETID:
2015 case IPV6_BINDMULTI:
2018 case IPV6_RECVHOPOPTS:
2019 optval = OPTBIT(IN6P_HOPOPTS);
2022 case IPV6_RECVDSTOPTS:
2023 optval = OPTBIT(IN6P_DSTOPTS);
2026 case IPV6_RECVRTHDRDSTOPTS:
2027 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2030 case IPV6_UNICAST_HOPS:
2031 optval = inp->in6p_hops;
2034 case IPV6_RECVPKTINFO:
2035 optval = OPTBIT(IN6P_PKTINFO);
2038 case IPV6_RECVHOPLIMIT:
2039 optval = OPTBIT(IN6P_HOPLIMIT);
2042 case IPV6_RECVRTHDR:
2043 optval = OPTBIT(IN6P_RTHDR);
2046 case IPV6_RECVPATHMTU:
2047 optval = OPTBIT(IN6P_MTU);
2051 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2054 case IPV6_PORTRANGE:
2057 flags = inp->inp_flags;
2058 if (flags & INP_HIGHPORT)
2059 optval = IPV6_PORTRANGE_HIGH;
2060 else if (flags & INP_LOWPORT)
2061 optval = IPV6_PORTRANGE_LOW;
2066 case IPV6_RECVTCLASS:
2067 optval = OPTBIT(IN6P_TCLASS);
2070 case IPV6_AUTOFLOWLABEL:
2071 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2074 case IPV6_ORIGDSTADDR:
2075 optval = OPTBIT2(INP_ORIGDSTADDR);
2079 optval = OPTBIT(INP_BINDANY);
2083 optval = inp->inp_flowid;
2087 optval = inp->inp_flowtype;
2090 case IPV6_RECVFLOWID:
2091 optval = OPTBIT2(INP_RECVFLOWID);
2094 case IPV6_RSSBUCKETID:
2096 rss_hash2bucket(inp->inp_flowid,
2100 optval = rss_bucket;
2105 case IPV6_RECVRSSBUCKETID:
2106 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2110 case IPV6_BINDMULTI:
2111 optval = OPTBIT2(INP_BINDMULTI);
2117 error = sooptcopyout(sopt, &optval,
2124 struct ip6_mtuinfo mtuinfo;
2125 struct in6_addr addr;
2127 if (!(so->so_state & SS_ISCONNECTED))
2130 * XXX: we dot not consider the case of source
2131 * routing, or optional information to specify
2132 * the outgoing interface.
2133 * Copy faddr out of inp to avoid holding lock
2134 * on inp during route lookup.
2137 bcopy(&inp->in6p_faddr, &addr, sizeof(addr));
2139 error = ip6_getpmtu_ctl(so->so_fibnum,
2143 if (pmtu > IPV6_MAXPACKET)
2144 pmtu = IPV6_MAXPACKET;
2146 bzero(&mtuinfo, sizeof(mtuinfo));
2147 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2148 optdata = (void *)&mtuinfo;
2149 optdatalen = sizeof(mtuinfo);
2150 error = sooptcopyout(sopt, optdata,
2155 case IPV6_2292PKTINFO:
2156 case IPV6_2292HOPLIMIT:
2157 case IPV6_2292HOPOPTS:
2158 case IPV6_2292RTHDR:
2159 case IPV6_2292DSTOPTS:
2161 case IPV6_2292PKTINFO:
2162 optval = OPTBIT(IN6P_PKTINFO);
2164 case IPV6_2292HOPLIMIT:
2165 optval = OPTBIT(IN6P_HOPLIMIT);
2167 case IPV6_2292HOPOPTS:
2168 optval = OPTBIT(IN6P_HOPOPTS);
2170 case IPV6_2292RTHDR:
2171 optval = OPTBIT(IN6P_RTHDR);
2173 case IPV6_2292DSTOPTS:
2174 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2177 error = sooptcopyout(sopt, &optval,
2184 case IPV6_RTHDRDSTOPTS:
2188 case IPV6_USE_MIN_MTU:
2189 case IPV6_PREFER_TEMPADDR:
2190 error = ip6_getpcbopt(inp, optname, sopt);
2193 case IPV6_MULTICAST_IF:
2194 case IPV6_MULTICAST_HOPS:
2195 case IPV6_MULTICAST_LOOP:
2197 error = ip6_getmoptions(inp, sopt);
2200 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2201 case IPV6_IPSEC_POLICY:
2202 if (IPSEC_ENABLED(ipv6)) {
2203 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2209 error = ENOPROTOOPT;
2219 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2221 int error = 0, optval, optlen;
2222 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2223 struct inpcb *inp = sotoinpcb(so);
2224 int level, op, optname;
2226 level = sopt->sopt_level;
2227 op = sopt->sopt_dir;
2228 optname = sopt->sopt_name;
2229 optlen = sopt->sopt_valsize;
2231 if (level != IPPROTO_IPV6) {
2238 * For ICMPv6 sockets, no modification allowed for checksum
2239 * offset, permit "no change" values to help existing apps.
2241 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2242 * for an ICMPv6 socket will fail."
2243 * The current behavior does not meet RFC3542.
2247 if (optlen != sizeof(int)) {
2251 error = sooptcopyin(sopt, &optval, sizeof(optval),
2255 if (optval < -1 || (optval % 2) != 0) {
2257 * The API assumes non-negative even offset
2258 * values or -1 as a special value.
2261 } else if (so->so_proto->pr_protocol ==
2263 if (optval != icmp6off)
2266 inp->in6p_cksum = optval;
2270 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2273 optval = inp->in6p_cksum;
2275 error = sooptcopyout(sopt, &optval, sizeof(optval));
2285 error = ENOPROTOOPT;
2293 * Set up IP6 options in pcb for insertion in output packets or
2294 * specifying behavior of outgoing packets.
2297 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2298 struct socket *so, struct sockopt *sopt)
2300 struct ip6_pktopts *opt = *pktopt;
2302 struct thread *td = sopt->sopt_td;
2304 /* turn off any old options. */
2307 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2308 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2309 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2310 printf("ip6_pcbopts: all specified options are cleared.\n");
2312 ip6_clearpktopts(opt, -1);
2314 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
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) { \
2373 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2374 malloc_optdata = true; \
2376 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
2378 free(optdata, M_TEMP); \
2379 return (ECONNRESET); \
2381 pktopt = inp->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 *inp, 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 = inp->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 *inp)
3165 if (!inp->in6p_outputopts)
3170 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3172 len += elen(inp->in6p_outputopts->ip6po_hbh);
3173 if (inp->in6p_outputopts->ip6po_rthdr)
3174 /* dest1 is valid with rthdr only */
3175 len += elen(inp->in6p_outputopts->ip6po_dest1);
3176 len += elen(inp->in6p_outputopts->ip6po_rthdr);
3177 len += elen(inp->in6p_outputopts->ip6po_dest2);