2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
69 #include "opt_inet6.h"
70 #include "opt_ipsec.h"
71 #include "opt_kern_tls.h"
72 #include "opt_ratelimit.h"
73 #include "opt_route.h"
77 #include <sys/param.h>
78 #include <sys/kernel.h>
80 #include <sys/malloc.h>
82 #include <sys/errno.h>
85 #include <sys/protosw.h>
86 #include <sys/socket.h>
87 #include <sys/socketvar.h>
88 #include <sys/syslog.h>
89 #include <sys/ucred.h>
91 #include <machine/in_cksum.h>
94 #include <net/if_var.h>
95 #include <net/if_llatbl.h>
96 #include <net/netisr.h>
97 #include <net/route.h>
99 #include <net/rss_config.h>
100 #include <net/vnet.h>
102 #include <netinet/in.h>
103 #include <netinet/in_var.h>
104 #include <netinet/ip_var.h>
105 #include <netinet6/in6_fib.h>
106 #include <netinet6/in6_var.h>
107 #include <netinet/ip6.h>
108 #include <netinet/icmp6.h>
109 #include <netinet6/ip6_var.h>
110 #include <netinet/in_pcb.h>
111 #include <netinet/tcp_var.h>
112 #include <netinet6/nd6.h>
113 #include <netinet6/in6_rss.h>
115 #include <netipsec/ipsec_support.h>
117 #include <netinet/sctp.h>
118 #include <netinet/sctp_crc32.h>
121 #include <netinet6/ip6protosw.h>
122 #include <netinet6/scope6_var.h>
124 extern int in6_mcast_loop;
127 struct mbuf *ip6e_ip6;
128 struct mbuf *ip6e_hbh;
129 struct mbuf *ip6e_dest1;
130 struct mbuf *ip6e_rthdr;
131 struct mbuf *ip6e_dest2;
134 static MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
136 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
137 struct ucred *, int);
138 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
139 struct socket *, struct sockopt *);
140 static int ip6_getpcbopt(struct inpcb *, int, struct sockopt *);
141 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
142 struct ucred *, int, int, int);
144 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
145 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
147 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
148 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
149 static int ip6_getpmtu(struct route_in6 *, int,
150 struct ifnet *, const struct in6_addr *, u_long *, int *, u_int,
152 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
153 u_long *, int *, u_int);
154 static int ip6_getpmtu_ctl(u_int, const struct in6_addr *, u_long *);
155 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
159 * Make an extension header from option data. hp is the source, and
160 * mp is the destination.
162 #define MAKE_EXTHDR(hp, mp) \
165 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
166 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
167 ((eh)->ip6e_len + 1) << 3); \
171 } while (/*CONSTCOND*/ 0)
174 * Form a chain of extension headers.
175 * m is the extension header mbuf
176 * mp is the previous mbuf in the chain
177 * p is the next header
178 * i is the type of option.
180 #define MAKE_CHAIN(m, mp, p, i)\
184 panic("assumption failed: hdr not split"); \
185 *mtod((m), u_char *) = *(p);\
187 p = mtod((m), u_char *);\
188 (m)->m_next = (mp)->m_next;\
192 } while (/*CONSTCOND*/ 0)
195 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
199 csum = in_cksum_skip(m, offset + plen, offset);
200 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
202 offset += m->m_pkthdr.csum_data; /* checksum offset */
204 if (offset + sizeof(csum) > m->m_len)
205 m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
207 *(u_short *)mtodo(m, offset) = csum;
211 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
212 int fraglen , uint32_t id)
214 struct mbuf *m, **mnext, *m_frgpart;
215 struct ip6_hdr *ip6, *mhip6;
216 struct ip6_frag *ip6f;
219 int tlen = m0->m_pkthdr.len;
221 KASSERT((fraglen % 8 == 0), ("Fragment length must be a multiple of 8"));
224 ip6 = mtod(m, struct ip6_hdr *);
225 mnext = &m->m_nextpkt;
227 for (off = hlen; off < tlen; off += fraglen) {
228 m = m_gethdr(M_NOWAIT, MT_DATA);
230 IP6STAT_INC(ip6s_odropped);
235 * Make sure the complete packet header gets copied
236 * from the originating mbuf to the newly created
237 * mbuf. This also ensures that existing firewall
238 * classification(s), VLAN tags and so on get copied
239 * to the resulting fragmented packet(s):
241 if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
243 IP6STAT_INC(ip6s_odropped);
248 mnext = &m->m_nextpkt;
249 m->m_data += max_linkhdr;
250 mhip6 = mtod(m, struct ip6_hdr *);
252 m->m_len = sizeof(*mhip6);
253 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
255 IP6STAT_INC(ip6s_odropped);
258 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
259 if (off + fraglen >= tlen)
260 fraglen = tlen - off;
262 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
263 mhip6->ip6_plen = htons((u_short)(fraglen + hlen +
264 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
265 if ((m_frgpart = m_copym(m0, off, fraglen, M_NOWAIT)) == NULL) {
266 IP6STAT_INC(ip6s_odropped);
270 m->m_pkthdr.len = fraglen + hlen + sizeof(*ip6f);
271 ip6f->ip6f_reserved = 0;
272 ip6f->ip6f_ident = id;
273 ip6f->ip6f_nxt = nextproto;
274 IP6STAT_INC(ip6s_ofragments);
275 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
282 ip6_output_send(struct inpcb *inp, struct ifnet *ifp, struct ifnet *origifp,
283 struct mbuf *m, struct sockaddr_in6 *dst, struct route_in6 *ro)
286 struct ktls_session *tls = NULL;
288 struct m_snd_tag *mst;
291 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
296 * If this is an unencrypted TLS record, save a reference to
297 * the record. This local reference is used to call
298 * ktls_output_eagain after the mbuf has been freed (thus
299 * dropping the mbuf's reference) in if_output.
301 if (m->m_next != NULL && mbuf_has_tls_session(m->m_next)) {
302 tls = ktls_hold(m->m_next->m_ext.ext_pgs->tls);
306 * If a TLS session doesn't have a valid tag, it must
307 * have had an earlier ifp mismatch, so drop this
317 if (inp != NULL && mst == NULL) {
318 if ((inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) != 0 ||
319 (inp->inp_snd_tag != NULL &&
320 inp->inp_snd_tag->ifp != ifp))
321 in_pcboutput_txrtlmt(inp, ifp, m);
323 if (inp->inp_snd_tag != NULL)
324 mst = inp->inp_snd_tag;
328 KASSERT(m->m_pkthdr.rcvif == NULL,
329 ("trying to add a send tag to a forwarded packet"));
330 if (mst->ifp != ifp) {
335 /* stamp send tag on mbuf */
336 m->m_pkthdr.snd_tag = m_snd_tag_ref(mst);
337 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
340 error = nd6_output_ifp(ifp, origifp, m, dst, (struct route *)ro);
343 /* Check for route change invalidating send tags. */
347 error = ktls_output_eagain(inp, tls);
353 in_pcboutput_eagain(inp);
359 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
360 * header (with pri, len, nxt, hlim, src, dst).
361 * This function may modify ver and hlim only.
362 * The mbuf chain containing the packet will be freed.
363 * The mbuf opt, if present, will not be freed.
364 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
365 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
366 * then result of route lookup is stored in ro->ro_rt.
368 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
369 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
372 * ifpp - XXX: just for statistics
375 * XXX TODO: no flowid is assigned for outbound flows?
378 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
379 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
380 struct ifnet **ifpp, struct inpcb *inp)
383 struct ifnet *ifp, *origifp;
385 struct mbuf *mprev = NULL;
387 struct epoch_tracker et;
388 struct route_in6 ip6route;
389 struct rtentry *rt = NULL;
390 struct sockaddr_in6 *dst, src_sa, dst_sa;
391 struct in6_addr odst;
393 struct in6_ifaddr *ia = NULL;
395 int alwaysfrag, dontfrag;
396 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
397 struct ip6_exthdrs exthdrs;
398 struct in6_addr src0, dst0;
400 struct route_in6 *ro_pmtu = NULL;
405 struct m_tag *fwd_tag = NULL;
411 INP_LOCK_ASSERT(inp);
412 M_SETFIB(m, inp->inp_inc.inc_fibnum);
413 if ((flags & IP_NODEFAULTFLOWID) == 0) {
414 /* unconditionally set flowid */
415 m->m_pkthdr.flowid = inp->inp_flowid;
416 M_HASHTYPE_SET(m, inp->inp_flowtype);
419 m->m_pkthdr.numa_domain = inp->inp_numa_domain;
423 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
425 * IPSec checking which handles several cases.
426 * FAST IPSEC: We re-injected the packet.
427 * XXX: need scope argument.
429 if (IPSEC_ENABLED(ipv6)) {
430 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
431 if (error == EINPROGRESS)
438 bzero(&exthdrs, sizeof(exthdrs));
440 /* Hop-by-Hop options header */
441 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
442 /* Destination options header(1st part) */
443 if (opt->ip6po_rthdr) {
445 * Destination options header(1st part)
446 * This only makes sense with a routing header.
447 * See Section 9.2 of RFC 3542.
448 * Disabling this part just for MIP6 convenience is
449 * a bad idea. We need to think carefully about a
450 * way to make the advanced API coexist with MIP6
451 * options, which might automatically be inserted in
454 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
457 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
458 /* Destination options header(2nd part) */
459 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
463 * Calculate the total length of the extension header chain.
464 * Keep the length of the unfragmentable part for fragmentation.
467 if (exthdrs.ip6e_hbh)
468 optlen += exthdrs.ip6e_hbh->m_len;
469 if (exthdrs.ip6e_dest1)
470 optlen += exthdrs.ip6e_dest1->m_len;
471 if (exthdrs.ip6e_rthdr)
472 optlen += exthdrs.ip6e_rthdr->m_len;
473 unfragpartlen = optlen + sizeof(struct ip6_hdr);
475 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
476 if (exthdrs.ip6e_dest2)
477 optlen += exthdrs.ip6e_dest2->m_len;
480 * If there is at least one extension header,
481 * separate IP6 header from the payload.
483 if (optlen && !hdrsplit) {
484 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
488 m = exthdrs.ip6e_ip6;
492 ip6 = mtod(m, struct ip6_hdr *);
494 /* adjust mbuf packet header length */
495 m->m_pkthdr.len += optlen;
496 plen = m->m_pkthdr.len - sizeof(*ip6);
498 /* If this is a jumbo payload, insert a jumbo payload option. */
499 if (plen > IPV6_MAXPACKET) {
501 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
505 m = exthdrs.ip6e_ip6;
509 ip6 = mtod(m, struct ip6_hdr *);
510 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
514 ip6->ip6_plen = htons(plen);
517 * Concatenate headers and fill in next header fields.
518 * Here we have, on "m"
520 * and we insert headers accordingly. Finally, we should be getting:
521 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
523 * during the header composing process, "m" points to IPv6 header.
524 * "mprev" points to an extension header prior to esp.
526 u_char *nexthdrp = &ip6->ip6_nxt;
530 * we treat dest2 specially. this makes IPsec processing
531 * much easier. the goal here is to make mprev point the
532 * mbuf prior to dest2.
534 * result: IPv6 dest2 payload
535 * m and mprev will point to IPv6 header.
537 if (exthdrs.ip6e_dest2) {
539 panic("assumption failed: hdr not split");
540 exthdrs.ip6e_dest2->m_next = m->m_next;
541 m->m_next = exthdrs.ip6e_dest2;
542 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
543 ip6->ip6_nxt = IPPROTO_DSTOPTS;
547 * result: IPv6 hbh dest1 rthdr dest2 payload
548 * m will point to IPv6 header. mprev will point to the
549 * extension header prior to dest2 (rthdr in the above case).
551 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
552 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
554 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
558 * If there is a routing header, discard the packet.
560 if (exthdrs.ip6e_rthdr) {
565 /* Source address validation */
566 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
567 (flags & IPV6_UNSPECSRC) == 0) {
569 IP6STAT_INC(ip6s_badscope);
572 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
574 IP6STAT_INC(ip6s_badscope);
578 IP6STAT_INC(ip6s_localout);
585 bzero((caddr_t)ro, sizeof(*ro));
588 if (opt && opt->ip6po_rthdr)
589 ro = &opt->ip6po_route;
590 dst = (struct sockaddr_in6 *)&ro->ro_dst;
591 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
594 * if specified, try to fill in the traffic class field.
595 * do not override if a non-zero value is already set.
596 * we check the diffserv field and the ecn field separately.
598 if (opt && opt->ip6po_tclass >= 0) {
601 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
603 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
606 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
609 /* fill in or override the hop limit field, if necessary. */
610 if (opt && opt->ip6po_hlim != -1)
611 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
612 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
614 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
616 ip6->ip6_hlim = V_ip6_defmcasthlim;
619 * Validate route against routing table additions;
620 * a better/more specific route might have been added.
621 * Make sure address family is set in route.
624 ro->ro_dst.sin6_family = AF_INET6;
625 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
627 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
628 ro->ro_dst.sin6_family == AF_INET6 &&
629 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
631 ifp = ro->ro_rt->rt_ifp;
634 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
636 if (fwd_tag == NULL) {
637 bzero(&dst_sa, sizeof(dst_sa));
638 dst_sa.sin6_family = AF_INET6;
639 dst_sa.sin6_len = sizeof(dst_sa);
640 dst_sa.sin6_addr = ip6->ip6_dst;
642 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
646 in6_ifstat_inc(ifp, ifs6_out_discard);
652 * If in6_selectroute() does not return a route entry,
653 * dst may not have been updated.
655 *dst = dst_sa; /* XXX */
659 * then rt (for unicast) and ifp must be non-NULL valid values.
661 if ((flags & IPV6_FORWARDING) == 0) {
662 /* XXX: the FORWARDING flag can be set for mrouting. */
663 in6_ifstat_inc(ifp, ifs6_out_request);
666 ia = (struct in6_ifaddr *)(rt->rt_ifa);
667 counter_u64_add(rt->rt_pksent, 1);
670 /* Setup data structures for scope ID checks. */
672 bzero(&src_sa, sizeof(src_sa));
673 src_sa.sin6_family = AF_INET6;
674 src_sa.sin6_len = sizeof(src_sa);
675 src_sa.sin6_addr = ip6->ip6_src;
678 /* re-initialize to be sure */
679 bzero(&dst_sa, sizeof(dst_sa));
680 dst_sa.sin6_family = AF_INET6;
681 dst_sa.sin6_len = sizeof(dst_sa);
682 dst_sa.sin6_addr = ip6->ip6_dst;
684 /* Check for valid scope ID. */
685 if (in6_setscope(&src0, ifp, &zone) == 0 &&
686 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
687 in6_setscope(&dst0, ifp, &zone) == 0 &&
688 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
690 * The outgoing interface is in the zone of the source
691 * and destination addresses.
693 * Because the loopback interface cannot receive
694 * packets with a different scope ID than its own,
695 * there is a trick is to pretend the outgoing packet
696 * was received by the real network interface, by
697 * setting "origifp" different from "ifp". This is
698 * only allowed when "ifp" is a loopback network
699 * interface. Refer to code in nd6_output_ifp() for
705 * We should use ia_ifp to support the case of sending
706 * packets to an address of our own.
708 if (ia != NULL && ia->ia_ifp)
711 } else if ((ifp->if_flags & IFF_LOOPBACK) == 0 ||
712 sa6_recoverscope(&src_sa) != 0 ||
713 sa6_recoverscope(&dst_sa) != 0 ||
714 dst_sa.sin6_scope_id == 0 ||
715 (src_sa.sin6_scope_id != 0 &&
716 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
717 (origifp = ifnet_byindex(dst_sa.sin6_scope_id)) == NULL) {
719 * If the destination network interface is not a
720 * loopback interface, or the destination network
721 * address has no scope ID, or the source address has
722 * a scope ID set which is different from the
723 * destination address one, or there is no network
724 * interface representing this scope ID, the address
725 * pair is considered invalid.
727 IP6STAT_INC(ip6s_badscope);
728 in6_ifstat_inc(ifp, ifs6_out_discard);
730 error = EHOSTUNREACH; /* XXX */
734 /* All scope ID checks are successful. */
736 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
737 if (opt && opt->ip6po_nextroute.ro_rt) {
739 * The nexthop is explicitly specified by the
740 * application. We assume the next hop is an IPv6
743 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
745 else if ((rt->rt_flags & RTF_GATEWAY))
746 dst = (struct sockaddr_in6 *)rt->rt_gateway;
749 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
750 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
752 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
753 in6_ifstat_inc(ifp, ifs6_out_mcast);
755 * Confirm that the outgoing interface supports multicast.
757 if (!(ifp->if_flags & IFF_MULTICAST)) {
758 IP6STAT_INC(ip6s_noroute);
759 in6_ifstat_inc(ifp, ifs6_out_discard);
763 if ((im6o == NULL && in6_mcast_loop) ||
764 (im6o && im6o->im6o_multicast_loop)) {
766 * Loop back multicast datagram if not expressly
767 * forbidden to do so, even if we have not joined
768 * the address; protocols will filter it later,
769 * thus deferring a hash lookup and lock acquisition
770 * at the expense of an m_copym().
772 ip6_mloopback(ifp, m);
775 * If we are acting as a multicast router, perform
776 * multicast forwarding as if the packet had just
777 * arrived on the interface to which we are about
778 * to send. The multicast forwarding function
779 * recursively calls this function, using the
780 * IPV6_FORWARDING flag to prevent infinite recursion.
782 * Multicasts that are looped back by ip6_mloopback(),
783 * above, will be forwarded by the ip6_input() routine,
786 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
788 * XXX: ip6_mforward expects that rcvif is NULL
789 * when it is called from the originating path.
790 * However, it may not always be the case.
792 m->m_pkthdr.rcvif = NULL;
793 if (ip6_mforward(ip6, ifp, m) != 0) {
800 * Multicasts with a hoplimit of zero may be looped back,
801 * above, but must not be transmitted on a network.
802 * Also, multicasts addressed to the loopback interface
803 * are not sent -- the above call to ip6_mloopback() will
804 * loop back a copy if this host actually belongs to the
805 * destination group on the loopback interface.
807 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
808 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
815 * Fill the outgoing inteface to tell the upper layer
816 * to increment per-interface statistics.
821 /* Determine path MTU. */
822 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
823 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
827 * The caller of this function may specify to use the minimum MTU
829 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
830 * setting. The logic is a bit complicated; by default, unicast
831 * packets will follow path MTU while multicast packets will be sent at
832 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
833 * including unicast ones will be sent at the minimum MTU. Multicast
834 * packets will always be sent at the minimum MTU unless
835 * IP6PO_MINMTU_DISABLE is explicitly specified.
836 * See RFC 3542 for more details.
838 if (mtu > IPV6_MMTU) {
839 if ((flags & IPV6_MINMTU))
841 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
843 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
845 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
851 * clear embedded scope identifiers if necessary.
852 * in6_clearscope will touch the addresses only when necessary.
854 in6_clearscope(&ip6->ip6_src);
855 in6_clearscope(&ip6->ip6_dst);
858 * If the outgoing packet contains a hop-by-hop options header,
859 * it must be examined and processed even by the source node.
860 * (RFC 2460, section 4.)
862 if (exthdrs.ip6e_hbh) {
863 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
864 u_int32_t dummy; /* XXX unused */
865 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
868 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
869 panic("ip6e_hbh is not contiguous");
872 * XXX: if we have to send an ICMPv6 error to the sender,
873 * we need the M_LOOP flag since icmp6_error() expects
874 * the IPv6 and the hop-by-hop options header are
875 * contiguous unless the flag is set.
877 m->m_flags |= M_LOOP;
878 m->m_pkthdr.rcvif = ifp;
879 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
880 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
881 &dummy, &plen) < 0) {
882 /* m was already freed at this point */
883 error = EINVAL;/* better error? */
886 m->m_flags &= ~M_LOOP; /* XXX */
887 m->m_pkthdr.rcvif = NULL;
890 /* Jump over all PFIL processing if hooks are not active. */
891 if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
895 /* Run through list of hooks for output packets. */
896 switch (pfil_run_hooks(V_inet6_pfil_head, &m, ifp, PFIL_OUT, inp)) {
898 ip6 = mtod(m, struct ip6_hdr *);
908 /* See if destination IP address was changed by packet filter. */
909 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
910 m->m_flags |= M_SKIP_FIREWALL;
911 /* If destination is now ourself drop to ip6_input(). */
912 if (in6_localip(&ip6->ip6_dst)) {
913 m->m_flags |= M_FASTFWD_OURS;
914 if (m->m_pkthdr.rcvif == NULL)
915 m->m_pkthdr.rcvif = V_loif;
916 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
917 m->m_pkthdr.csum_flags |=
918 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
919 m->m_pkthdr.csum_data = 0xffff;
922 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
923 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
925 error = netisr_queue(NETISR_IPV6, m);
928 RO_INVALIDATE_CACHE(ro);
929 needfiblookup = 1; /* Redo the routing table lookup. */
932 /* See if fib was changed by packet filter. */
933 if (fibnum != M_GETFIB(m)) {
934 m->m_flags |= M_SKIP_FIREWALL;
935 fibnum = M_GETFIB(m);
936 RO_INVALIDATE_CACHE(ro);
942 /* See if local, if yes, send it to netisr. */
943 if (m->m_flags & M_FASTFWD_OURS) {
944 if (m->m_pkthdr.rcvif == NULL)
945 m->m_pkthdr.rcvif = V_loif;
946 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
947 m->m_pkthdr.csum_flags |=
948 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
949 m->m_pkthdr.csum_data = 0xffff;
952 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
953 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
955 error = netisr_queue(NETISR_IPV6, m);
958 /* Or forward to some other address? */
959 if ((m->m_flags & M_IP6_NEXTHOP) &&
960 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
961 dst = (struct sockaddr_in6 *)&ro->ro_dst;
962 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
963 m->m_flags |= M_SKIP_FIREWALL;
964 m->m_flags &= ~M_IP6_NEXTHOP;
965 m_tag_delete(m, fwd_tag);
971 * Send the packet to the outgoing interface.
972 * If necessary, do IPv6 fragmentation before sending.
974 * the logic here is rather complex:
975 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
976 * 1-a: send as is if tlen <= path mtu
977 * 1-b: fragment if tlen > path mtu
979 * 2: if user asks us not to fragment (dontfrag == 1)
980 * 2-a: send as is if tlen <= interface mtu
981 * 2-b: error if tlen > interface mtu
983 * 3: if we always need to attach fragment header (alwaysfrag == 1)
986 * 4: if dontfrag == 1 && alwaysfrag == 1
987 * error, as we cannot handle this conflicting request
989 sw_csum = m->m_pkthdr.csum_flags;
991 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
992 sw_csum &= ~ifp->if_hwassist;
996 * If we added extension headers, we will not do TSO and calculate the
997 * checksums ourselves for now.
998 * XXX-BZ Need a framework to know when the NIC can handle it, even
1001 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
1002 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
1003 m = mb_unmapped_to_ext(m);
1006 IP6STAT_INC(ip6s_odropped);
1009 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
1010 } else if ((ifp->if_capenable & IFCAP_NOMAP) == 0) {
1011 m = mb_unmapped_to_ext(m);
1014 IP6STAT_INC(ip6s_odropped);
1019 if (sw_csum & CSUM_SCTP_IPV6) {
1020 sw_csum &= ~CSUM_SCTP_IPV6;
1021 m = mb_unmapped_to_ext(m);
1024 IP6STAT_INC(ip6s_odropped);
1027 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
1030 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
1031 tlen = m->m_pkthdr.len;
1033 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
1037 if (dontfrag && alwaysfrag) { /* case 4 */
1038 /* conflicting request - can't transmit */
1042 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
1044 * Even if the DONTFRAG option is specified, we cannot send the
1045 * packet when the data length is larger than the MTU of the
1046 * outgoing interface.
1047 * Notify the error by sending IPV6_PATHMTU ancillary data if
1048 * application wanted to know the MTU value. Also return an
1049 * error code (this is not described in the API spec).
1052 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
1058 * transmit packet without fragmentation
1060 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
1061 struct in6_ifaddr *ia6;
1063 ip6 = mtod(m, struct ip6_hdr *);
1064 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1066 /* Record statistics for this interface address. */
1067 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
1068 counter_u64_add(ia6->ia_ifa.ifa_obytes,
1070 ifa_free(&ia6->ia_ifa);
1072 error = ip6_output_send(inp, ifp, origifp, m, dst, ro);
1077 * try to fragment the packet. case 1-b and 3
1079 if (mtu < IPV6_MMTU) {
1080 /* path MTU cannot be less than IPV6_MMTU */
1082 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1084 } else if (ip6->ip6_plen == 0) {
1085 /* jumbo payload cannot be fragmented */
1087 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1093 * Too large for the destination or interface;
1094 * fragment if possible.
1095 * Must be able to put at least 8 bytes per fragment.
1097 hlen = unfragpartlen;
1098 if (mtu > IPV6_MAXPACKET)
1099 mtu = IPV6_MAXPACKET;
1101 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1104 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1109 * If the interface will not calculate checksums on
1110 * fragmented packets, then do it here.
1111 * XXX-BZ handle the hw offloading case. Need flags.
1113 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1114 m = mb_unmapped_to_ext(m);
1116 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1120 in6_delayed_cksum(m, plen, hlen);
1121 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1124 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1125 m = mb_unmapped_to_ext(m);
1127 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1131 sctp_delayed_cksum(m, hlen);
1132 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1136 * Change the next header field of the last header in the
1137 * unfragmentable part.
1139 if (exthdrs.ip6e_rthdr) {
1140 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1141 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1142 } else if (exthdrs.ip6e_dest1) {
1143 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1144 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1145 } else if (exthdrs.ip6e_hbh) {
1146 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1147 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1149 nextproto = ip6->ip6_nxt;
1150 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1154 * Loop through length of segment after first fragment,
1155 * make new header and copy data of each part and link onto
1159 id = htonl(ip6_randomid());
1160 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1163 in6_ifstat_inc(ifp, ifs6_out_fragok);
1167 * Remove leading garbages.
1177 /* Record statistics for this interface address. */
1179 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1180 counter_u64_add(ia->ia_ifa.ifa_obytes,
1183 error = ip6_output_send(inp, ifp, origifp, m, dst, ro);
1189 IP6STAT_INC(ip6s_fragmented);
1193 if (ro == &ip6route)
1198 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1199 m_freem(exthdrs.ip6e_dest1);
1200 m_freem(exthdrs.ip6e_rthdr);
1201 m_freem(exthdrs.ip6e_dest2);
1210 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1214 if (hlen > MCLBYTES)
1215 return (ENOBUFS); /* XXX */
1218 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1220 m = m_get(M_NOWAIT, MT_DATA);
1225 bcopy(hdr, mtod(m, caddr_t), hlen);
1232 * Insert jumbo payload option.
1235 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1241 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1244 * If there is no hop-by-hop options header, allocate new one.
1245 * If there is one but it doesn't have enough space to store the
1246 * jumbo payload option, allocate a cluster to store the whole options.
1247 * Otherwise, use it to store the options.
1249 if (exthdrs->ip6e_hbh == NULL) {
1250 mopt = m_get(M_NOWAIT, MT_DATA);
1253 mopt->m_len = JUMBOOPTLEN;
1254 optbuf = mtod(mopt, u_char *);
1255 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1256 exthdrs->ip6e_hbh = mopt;
1258 struct ip6_hbh *hbh;
1260 mopt = exthdrs->ip6e_hbh;
1261 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1264 * - exthdrs->ip6e_hbh is not referenced from places
1265 * other than exthdrs.
1266 * - exthdrs->ip6e_hbh is not an mbuf chain.
1268 int oldoptlen = mopt->m_len;
1272 * XXX: give up if the whole (new) hbh header does
1273 * not fit even in an mbuf cluster.
1275 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1279 * As a consequence, we must always prepare a cluster
1282 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1285 n->m_len = oldoptlen + JUMBOOPTLEN;
1286 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1288 optbuf = mtod(n, caddr_t) + oldoptlen;
1290 mopt = exthdrs->ip6e_hbh = n;
1292 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1293 mopt->m_len += JUMBOOPTLEN;
1295 optbuf[0] = IP6OPT_PADN;
1299 * Adjust the header length according to the pad and
1300 * the jumbo payload option.
1302 hbh = mtod(mopt, struct ip6_hbh *);
1303 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1306 /* fill in the option. */
1307 optbuf[2] = IP6OPT_JUMBO;
1309 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1310 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1312 /* finally, adjust the packet header length */
1313 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1320 * Insert fragment header and copy unfragmentable header portions.
1323 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1324 struct ip6_frag **frghdrp)
1326 struct mbuf *n, *mlast;
1328 if (hlen > sizeof(struct ip6_hdr)) {
1329 n = m_copym(m0, sizeof(struct ip6_hdr),
1330 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1337 /* Search for the last mbuf of unfragmentable part. */
1338 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1341 if (M_WRITABLE(mlast) &&
1342 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1343 /* use the trailing space of the last mbuf for the fragment hdr */
1344 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1346 mlast->m_len += sizeof(struct ip6_frag);
1347 m->m_pkthdr.len += sizeof(struct ip6_frag);
1349 /* allocate a new mbuf for the fragment header */
1352 mfrg = m_get(M_NOWAIT, MT_DATA);
1355 mfrg->m_len = sizeof(struct ip6_frag);
1356 *frghdrp = mtod(mfrg, struct ip6_frag *);
1357 mlast->m_next = mfrg;
1364 * Calculates IPv6 path mtu for destination @dst.
1365 * Resulting MTU is stored in @mtup.
1367 * Returns 0 on success.
1370 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1372 struct nhop6_extended nh6;
1373 struct in6_addr kdst;
1379 in6_splitscope(dst, &kdst, &scopeid);
1380 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1381 return (EHOSTUNREACH);
1386 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1387 fib6_free_nh_ext(fibnum, &nh6);
1393 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1394 * and cached data in @ro_pmtu.
1395 * MTU from (successful) route lookup is saved (along with dst)
1396 * inside @ro_pmtu to avoid subsequent route lookups after packet
1397 * filter processing.
1399 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1400 * Returns 0 on success.
1403 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1404 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1405 int *alwaysfragp, u_int fibnum, u_int proto)
1407 struct nhop6_basic nh6;
1408 struct in6_addr kdst;
1410 struct sockaddr_in6 *sa6_dst;
1417 * Here ro_pmtu has final destination address, while
1418 * ro might represent immediate destination.
1419 * Use ro_pmtu destination since mtu might differ.
1421 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1422 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1423 ro_pmtu->ro_mtu = 0;
1425 if (ro_pmtu->ro_mtu == 0) {
1426 bzero(sa6_dst, sizeof(*sa6_dst));
1427 sa6_dst->sin6_family = AF_INET6;
1428 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1429 sa6_dst->sin6_addr = *dst;
1431 in6_splitscope(dst, &kdst, &scopeid);
1432 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1434 ro_pmtu->ro_mtu = nh6.nh_mtu;
1437 mtu = ro_pmtu->ro_mtu;
1441 mtu = ro_pmtu->ro_rt->rt_mtu;
1443 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1447 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1448 * hostcache data for @dst.
1449 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1451 * Returns 0 on success.
1454 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1455 u_long *mtup, int *alwaysfragp, u_int proto)
1463 struct in_conninfo inc;
1465 bzero(&inc, sizeof(inc));
1466 inc.inc_flags |= INC_ISIPV6;
1467 inc.inc6_faddr = *dst;
1469 ifmtu = IN6_LINKMTU(ifp);
1471 /* TCP is known to react to pmtu changes so skip hc */
1472 if (proto != IPPROTO_TCP)
1473 mtu = tcp_hc_getmtu(&inc);
1476 mtu = min(mtu, rt_mtu);
1481 else if (mtu < IPV6_MMTU) {
1483 * RFC2460 section 5, last paragraph:
1484 * if we record ICMPv6 too big message with
1485 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1486 * or smaller, with framgent header attached.
1487 * (fragment header is needed regardless from the
1488 * packet size, for translators to identify packets)
1494 mtu = IN6_LINKMTU(ifp);
1496 error = EHOSTUNREACH; /* XXX */
1500 *alwaysfragp = alwaysfrag;
1505 * IP6 socket option processing.
1508 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1510 int optdatalen, uproto;
1512 struct inpcb *inp = sotoinpcb(so);
1514 int level, op, optname;
1518 uint32_t rss_bucket;
1523 * Don't use more than a quarter of mbuf clusters. N.B.:
1524 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1525 * on LP64 architectures, so cast to u_long to avoid undefined
1526 * behavior. ILP32 architectures cannot have nmbclusters
1527 * large enough to overflow for other reasons.
1529 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1531 level = sopt->sopt_level;
1532 op = sopt->sopt_dir;
1533 optname = sopt->sopt_name;
1534 optlen = sopt->sopt_valsize;
1538 uproto = (int)so->so_proto->pr_protocol;
1540 if (level != IPPROTO_IPV6) {
1543 if (sopt->sopt_level == SOL_SOCKET &&
1544 sopt->sopt_dir == SOPT_SET) {
1545 switch (sopt->sopt_name) {
1548 if ((so->so_options & SO_REUSEADDR) != 0)
1549 inp->inp_flags2 |= INP_REUSEADDR;
1551 inp->inp_flags2 &= ~INP_REUSEADDR;
1557 if ((so->so_options & SO_REUSEPORT) != 0)
1558 inp->inp_flags2 |= INP_REUSEPORT;
1560 inp->inp_flags2 &= ~INP_REUSEPORT;
1564 case SO_REUSEPORT_LB:
1566 if ((so->so_options & SO_REUSEPORT_LB) != 0)
1567 inp->inp_flags2 |= INP_REUSEPORT_LB;
1569 inp->inp_flags2 &= ~INP_REUSEPORT_LB;
1575 inp->inp_inc.inc_fibnum = so->so_fibnum;
1579 case SO_MAX_PACING_RATE:
1582 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1593 } else { /* level == IPPROTO_IPV6 */
1598 case IPV6_2292PKTOPTIONS:
1599 #ifdef IPV6_PKTOPTIONS
1600 case IPV6_PKTOPTIONS:
1605 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1606 printf("ip6_ctloutput: mbuf limit hit\n");
1611 error = soopt_getm(sopt, &m); /* XXX */
1614 error = soopt_mcopyin(sopt, m); /* XXX */
1617 error = ip6_pcbopts(&inp->in6p_outputopts,
1619 m_freem(m); /* XXX */
1624 * Use of some Hop-by-Hop options or some
1625 * Destination options, might require special
1626 * privilege. That is, normal applications
1627 * (without special privilege) might be forbidden
1628 * from setting certain options in outgoing packets,
1629 * and might never see certain options in received
1630 * packets. [RFC 2292 Section 6]
1631 * KAME specific note:
1632 * KAME prevents non-privileged users from sending or
1633 * receiving ANY hbh/dst options in order to avoid
1634 * overhead of parsing options in the kernel.
1636 case IPV6_RECVHOPOPTS:
1637 case IPV6_RECVDSTOPTS:
1638 case IPV6_RECVRTHDRDSTOPTS:
1640 error = priv_check(td,
1641 PRIV_NETINET_SETHDROPTS);
1646 case IPV6_UNICAST_HOPS:
1649 case IPV6_RECVPKTINFO:
1650 case IPV6_RECVHOPLIMIT:
1651 case IPV6_RECVRTHDR:
1652 case IPV6_RECVPATHMTU:
1653 case IPV6_RECVTCLASS:
1654 case IPV6_RECVFLOWID:
1656 case IPV6_RECVRSSBUCKETID:
1659 case IPV6_AUTOFLOWLABEL:
1660 case IPV6_ORIGDSTADDR:
1662 case IPV6_BINDMULTI:
1664 case IPV6_RSS_LISTEN_BUCKET:
1666 if (optname == IPV6_BINDANY && td != NULL) {
1667 error = priv_check(td,
1668 PRIV_NETINET_BINDANY);
1673 if (optlen != sizeof(int)) {
1677 error = sooptcopyin(sopt, &optval,
1678 sizeof optval, sizeof optval);
1683 case IPV6_UNICAST_HOPS:
1684 if (optval < -1 || optval >= 256)
1687 /* -1 = kernel default */
1688 inp->in6p_hops = optval;
1689 if ((inp->inp_vflag &
1691 inp->inp_ip_ttl = optval;
1694 #define OPTSET(bit) \
1698 inp->inp_flags |= (bit); \
1700 inp->inp_flags &= ~(bit); \
1702 } while (/*CONSTCOND*/ 0)
1703 #define OPTSET2292(bit) \
1706 inp->inp_flags |= IN6P_RFC2292; \
1708 inp->inp_flags |= (bit); \
1710 inp->inp_flags &= ~(bit); \
1712 } while (/*CONSTCOND*/ 0)
1713 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0)
1715 #define OPTSET2_N(bit, val) do { \
1717 inp->inp_flags2 |= bit; \
1719 inp->inp_flags2 &= ~bit; \
1721 #define OPTSET2(bit, val) do { \
1723 OPTSET2_N(bit, val); \
1726 #define OPTBIT2(bit) (inp->inp_flags2 & (bit) ? 1 : 0)
1727 #define OPTSET2292_EXCLUSIVE(bit) \
1730 if (OPTBIT(IN6P_RFC2292)) { \
1734 inp->inp_flags |= (bit); \
1736 inp->inp_flags &= ~(bit); \
1739 } while (/*CONSTCOND*/ 0)
1741 case IPV6_RECVPKTINFO:
1742 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1747 struct ip6_pktopts **optp;
1749 /* cannot mix with RFC2292 */
1750 if (OPTBIT(IN6P_RFC2292)) {
1755 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1757 return (ECONNRESET);
1759 optp = &inp->in6p_outputopts;
1760 error = ip6_pcbopt(IPV6_HOPLIMIT,
1761 (u_char *)&optval, sizeof(optval),
1762 optp, (td != NULL) ? td->td_ucred :
1768 case IPV6_RECVHOPLIMIT:
1769 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1772 case IPV6_RECVHOPOPTS:
1773 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1776 case IPV6_RECVDSTOPTS:
1777 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1780 case IPV6_RECVRTHDRDSTOPTS:
1781 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1784 case IPV6_RECVRTHDR:
1785 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1788 case IPV6_RECVPATHMTU:
1790 * We ignore this option for TCP
1792 * (RFC3542 leaves this case
1795 if (uproto != IPPROTO_TCP)
1799 case IPV6_RECVFLOWID:
1800 OPTSET2(INP_RECVFLOWID, optval);
1804 case IPV6_RECVRSSBUCKETID:
1805 OPTSET2(INP_RECVRSSBUCKETID, optval);
1811 if (inp->inp_lport ||
1812 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1814 * The socket is already bound.
1821 inp->inp_flags |= IN6P_IPV6_V6ONLY;
1822 inp->inp_vflag &= ~INP_IPV4;
1824 inp->inp_flags &= ~IN6P_IPV6_V6ONLY;
1825 inp->inp_vflag |= INP_IPV4;
1829 case IPV6_RECVTCLASS:
1830 /* cannot mix with RFC2292 XXX */
1831 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1833 case IPV6_AUTOFLOWLABEL:
1834 OPTSET(IN6P_AUTOFLOWLABEL);
1837 case IPV6_ORIGDSTADDR:
1838 OPTSET2(INP_ORIGDSTADDR, optval);
1841 OPTSET(INP_BINDANY);
1844 case IPV6_BINDMULTI:
1845 OPTSET2(INP_BINDMULTI, optval);
1848 case IPV6_RSS_LISTEN_BUCKET:
1849 if ((optval >= 0) &&
1850 (optval < rss_getnumbuckets())) {
1852 inp->inp_rss_listen_bucket = optval;
1853 OPTSET2_N(INP_RSS_BUCKET_SET, 1);
1865 case IPV6_USE_MIN_MTU:
1866 case IPV6_PREFER_TEMPADDR:
1867 if (optlen != sizeof(optval)) {
1871 error = sooptcopyin(sopt, &optval,
1872 sizeof optval, sizeof optval);
1876 struct ip6_pktopts **optp;
1878 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1880 return (ECONNRESET);
1882 optp = &inp->in6p_outputopts;
1883 error = ip6_pcbopt(optname,
1884 (u_char *)&optval, sizeof(optval),
1885 optp, (td != NULL) ? td->td_ucred :
1891 case IPV6_2292PKTINFO:
1892 case IPV6_2292HOPLIMIT:
1893 case IPV6_2292HOPOPTS:
1894 case IPV6_2292DSTOPTS:
1895 case IPV6_2292RTHDR:
1897 if (optlen != sizeof(int)) {
1901 error = sooptcopyin(sopt, &optval,
1902 sizeof optval, sizeof optval);
1906 case IPV6_2292PKTINFO:
1907 OPTSET2292(IN6P_PKTINFO);
1909 case IPV6_2292HOPLIMIT:
1910 OPTSET2292(IN6P_HOPLIMIT);
1912 case IPV6_2292HOPOPTS:
1914 * Check super-user privilege.
1915 * See comments for IPV6_RECVHOPOPTS.
1918 error = priv_check(td,
1919 PRIV_NETINET_SETHDROPTS);
1923 OPTSET2292(IN6P_HOPOPTS);
1925 case IPV6_2292DSTOPTS:
1927 error = priv_check(td,
1928 PRIV_NETINET_SETHDROPTS);
1932 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1934 case IPV6_2292RTHDR:
1935 OPTSET2292(IN6P_RTHDR);
1943 case IPV6_RTHDRDSTOPTS:
1946 /* new advanced API (RFC3542) */
1948 u_char optbuf_storage[MCLBYTES];
1950 struct ip6_pktopts **optp;
1952 /* cannot mix with RFC2292 */
1953 if (OPTBIT(IN6P_RFC2292)) {
1959 * We only ensure valsize is not too large
1960 * here. Further validation will be done
1963 error = sooptcopyin(sopt, optbuf_storage,
1964 sizeof(optbuf_storage), 0);
1967 optlen = sopt->sopt_valsize;
1968 optbuf = optbuf_storage;
1970 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1972 return (ECONNRESET);
1974 optp = &inp->in6p_outputopts;
1975 error = ip6_pcbopt(optname, optbuf, optlen,
1976 optp, (td != NULL) ? td->td_ucred : NULL,
1983 case IPV6_MULTICAST_IF:
1984 case IPV6_MULTICAST_HOPS:
1985 case IPV6_MULTICAST_LOOP:
1986 case IPV6_JOIN_GROUP:
1987 case IPV6_LEAVE_GROUP:
1989 case MCAST_BLOCK_SOURCE:
1990 case MCAST_UNBLOCK_SOURCE:
1991 case MCAST_JOIN_GROUP:
1992 case MCAST_LEAVE_GROUP:
1993 case MCAST_JOIN_SOURCE_GROUP:
1994 case MCAST_LEAVE_SOURCE_GROUP:
1995 error = ip6_setmoptions(inp, sopt);
1998 case IPV6_PORTRANGE:
1999 error = sooptcopyin(sopt, &optval,
2000 sizeof optval, sizeof optval);
2006 case IPV6_PORTRANGE_DEFAULT:
2007 inp->inp_flags &= ~(INP_LOWPORT);
2008 inp->inp_flags &= ~(INP_HIGHPORT);
2011 case IPV6_PORTRANGE_HIGH:
2012 inp->inp_flags &= ~(INP_LOWPORT);
2013 inp->inp_flags |= INP_HIGHPORT;
2016 case IPV6_PORTRANGE_LOW:
2017 inp->inp_flags &= ~(INP_HIGHPORT);
2018 inp->inp_flags |= INP_LOWPORT;
2028 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2029 case IPV6_IPSEC_POLICY:
2030 if (IPSEC_ENABLED(ipv6)) {
2031 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2038 error = ENOPROTOOPT;
2046 case IPV6_2292PKTOPTIONS:
2047 #ifdef IPV6_PKTOPTIONS
2048 case IPV6_PKTOPTIONS:
2051 * RFC3542 (effectively) deprecated the
2052 * semantics of the 2292-style pktoptions.
2053 * Since it was not reliable in nature (i.e.,
2054 * applications had to expect the lack of some
2055 * information after all), it would make sense
2056 * to simplify this part by always returning
2059 sopt->sopt_valsize = 0;
2062 case IPV6_RECVHOPOPTS:
2063 case IPV6_RECVDSTOPTS:
2064 case IPV6_RECVRTHDRDSTOPTS:
2065 case IPV6_UNICAST_HOPS:
2066 case IPV6_RECVPKTINFO:
2067 case IPV6_RECVHOPLIMIT:
2068 case IPV6_RECVRTHDR:
2069 case IPV6_RECVPATHMTU:
2072 case IPV6_PORTRANGE:
2073 case IPV6_RECVTCLASS:
2074 case IPV6_AUTOFLOWLABEL:
2078 case IPV6_RECVFLOWID:
2080 case IPV6_RSSBUCKETID:
2081 case IPV6_RECVRSSBUCKETID:
2083 case IPV6_BINDMULTI:
2086 case IPV6_RECVHOPOPTS:
2087 optval = OPTBIT(IN6P_HOPOPTS);
2090 case IPV6_RECVDSTOPTS:
2091 optval = OPTBIT(IN6P_DSTOPTS);
2094 case IPV6_RECVRTHDRDSTOPTS:
2095 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2098 case IPV6_UNICAST_HOPS:
2099 optval = inp->in6p_hops;
2102 case IPV6_RECVPKTINFO:
2103 optval = OPTBIT(IN6P_PKTINFO);
2106 case IPV6_RECVHOPLIMIT:
2107 optval = OPTBIT(IN6P_HOPLIMIT);
2110 case IPV6_RECVRTHDR:
2111 optval = OPTBIT(IN6P_RTHDR);
2114 case IPV6_RECVPATHMTU:
2115 optval = OPTBIT(IN6P_MTU);
2119 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2122 case IPV6_PORTRANGE:
2125 flags = inp->inp_flags;
2126 if (flags & INP_HIGHPORT)
2127 optval = IPV6_PORTRANGE_HIGH;
2128 else if (flags & INP_LOWPORT)
2129 optval = IPV6_PORTRANGE_LOW;
2134 case IPV6_RECVTCLASS:
2135 optval = OPTBIT(IN6P_TCLASS);
2138 case IPV6_AUTOFLOWLABEL:
2139 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2142 case IPV6_ORIGDSTADDR:
2143 optval = OPTBIT2(INP_ORIGDSTADDR);
2147 optval = OPTBIT(INP_BINDANY);
2151 optval = inp->inp_flowid;
2155 optval = inp->inp_flowtype;
2158 case IPV6_RECVFLOWID:
2159 optval = OPTBIT2(INP_RECVFLOWID);
2162 case IPV6_RSSBUCKETID:
2164 rss_hash2bucket(inp->inp_flowid,
2168 optval = rss_bucket;
2173 case IPV6_RECVRSSBUCKETID:
2174 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2178 case IPV6_BINDMULTI:
2179 optval = OPTBIT2(INP_BINDMULTI);
2185 error = sooptcopyout(sopt, &optval,
2192 struct ip6_mtuinfo mtuinfo;
2193 struct in6_addr addr;
2195 if (!(so->so_state & SS_ISCONNECTED))
2198 * XXX: we dot not consider the case of source
2199 * routing, or optional information to specify
2200 * the outgoing interface.
2201 * Copy faddr out of inp to avoid holding lock
2202 * on inp during route lookup.
2205 bcopy(&inp->in6p_faddr, &addr, sizeof(addr));
2207 error = ip6_getpmtu_ctl(so->so_fibnum,
2211 if (pmtu > IPV6_MAXPACKET)
2212 pmtu = IPV6_MAXPACKET;
2214 bzero(&mtuinfo, sizeof(mtuinfo));
2215 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2216 optdata = (void *)&mtuinfo;
2217 optdatalen = sizeof(mtuinfo);
2218 error = sooptcopyout(sopt, optdata,
2223 case IPV6_2292PKTINFO:
2224 case IPV6_2292HOPLIMIT:
2225 case IPV6_2292HOPOPTS:
2226 case IPV6_2292RTHDR:
2227 case IPV6_2292DSTOPTS:
2229 case IPV6_2292PKTINFO:
2230 optval = OPTBIT(IN6P_PKTINFO);
2232 case IPV6_2292HOPLIMIT:
2233 optval = OPTBIT(IN6P_HOPLIMIT);
2235 case IPV6_2292HOPOPTS:
2236 optval = OPTBIT(IN6P_HOPOPTS);
2238 case IPV6_2292RTHDR:
2239 optval = OPTBIT(IN6P_RTHDR);
2241 case IPV6_2292DSTOPTS:
2242 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2245 error = sooptcopyout(sopt, &optval,
2252 case IPV6_RTHDRDSTOPTS:
2256 case IPV6_USE_MIN_MTU:
2257 case IPV6_PREFER_TEMPADDR:
2258 error = ip6_getpcbopt(inp, optname, sopt);
2261 case IPV6_MULTICAST_IF:
2262 case IPV6_MULTICAST_HOPS:
2263 case IPV6_MULTICAST_LOOP:
2265 error = ip6_getmoptions(inp, sopt);
2268 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2269 case IPV6_IPSEC_POLICY:
2270 if (IPSEC_ENABLED(ipv6)) {
2271 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2277 error = ENOPROTOOPT;
2287 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2289 int error = 0, optval, optlen;
2290 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2291 struct inpcb *inp = sotoinpcb(so);
2292 int level, op, optname;
2294 level = sopt->sopt_level;
2295 op = sopt->sopt_dir;
2296 optname = sopt->sopt_name;
2297 optlen = sopt->sopt_valsize;
2299 if (level != IPPROTO_IPV6) {
2306 * For ICMPv6 sockets, no modification allowed for checksum
2307 * offset, permit "no change" values to help existing apps.
2309 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2310 * for an ICMPv6 socket will fail."
2311 * The current behavior does not meet RFC3542.
2315 if (optlen != sizeof(int)) {
2319 error = sooptcopyin(sopt, &optval, sizeof(optval),
2323 if (optval < -1 || (optval % 2) != 0) {
2325 * The API assumes non-negative even offset
2326 * values or -1 as a special value.
2329 } else if (so->so_proto->pr_protocol ==
2331 if (optval != icmp6off)
2334 inp->in6p_cksum = optval;
2338 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2341 optval = inp->in6p_cksum;
2343 error = sooptcopyout(sopt, &optval, sizeof(optval));
2353 error = ENOPROTOOPT;
2361 * Set up IP6 options in pcb for insertion in output packets or
2362 * specifying behavior of outgoing packets.
2365 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2366 struct socket *so, struct sockopt *sopt)
2368 struct ip6_pktopts *opt = *pktopt;
2370 struct thread *td = sopt->sopt_td;
2372 /* turn off any old options. */
2375 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2376 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2377 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2378 printf("ip6_pcbopts: all specified options are cleared.\n");
2380 ip6_clearpktopts(opt, -1);
2382 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2385 if (!m || m->m_len == 0) {
2387 * Only turning off any previous options, regardless of
2388 * whether the opt is just created or given.
2390 free(opt, M_IP6OPT);
2394 /* set options specified by user. */
2395 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2396 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2397 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2398 free(opt, M_IP6OPT);
2406 * initialize ip6_pktopts. beware that there are non-zero default values in
2410 ip6_initpktopts(struct ip6_pktopts *opt)
2413 bzero(opt, sizeof(*opt));
2414 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2415 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2416 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2417 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2421 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2422 struct ucred *cred, int uproto)
2424 struct ip6_pktopts *opt;
2426 if (*pktopt == NULL) {
2427 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2429 if (*pktopt == NULL)
2431 ip6_initpktopts(*pktopt);
2435 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2438 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2439 if (pktopt && pktopt->field) { \
2441 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2442 malloc_optdata = true; \
2444 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
2446 free(optdata, M_TEMP); \
2447 return (ECONNRESET); \
2449 pktopt = inp->in6p_outputopts; \
2450 if (pktopt && pktopt->field) { \
2451 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2452 bcopy(&pktopt->field, optdata, optdatalen); \
2454 free(optdata, M_TEMP); \
2456 malloc_optdata = false; \
2461 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2462 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2464 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2465 pktopt->field->sa_len)
2468 ip6_getpcbopt(struct inpcb *inp, int optname, struct sockopt *sopt)
2470 void *optdata = NULL;
2471 bool malloc_optdata = false;
2474 struct in6_pktinfo null_pktinfo;
2475 int deftclass = 0, on;
2476 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2477 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2478 struct ip6_pktopts *pktopt;
2481 pktopt = inp->in6p_outputopts;
2485 optdata = (void *)&null_pktinfo;
2486 if (pktopt && pktopt->ip6po_pktinfo) {
2487 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2488 sizeof(null_pktinfo));
2489 in6_clearscope(&null_pktinfo.ipi6_addr);
2491 /* XXX: we don't have to do this every time... */
2492 bzero(&null_pktinfo, sizeof(null_pktinfo));
2494 optdatalen = sizeof(struct in6_pktinfo);
2497 if (pktopt && pktopt->ip6po_tclass >= 0)
2498 deftclass = pktopt->ip6po_tclass;
2499 optdata = (void *)&deftclass;
2500 optdatalen = sizeof(int);
2503 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2506 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2508 case IPV6_RTHDRDSTOPTS:
2509 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2512 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2515 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2517 case IPV6_USE_MIN_MTU:
2519 defminmtu = pktopt->ip6po_minmtu;
2520 optdata = (void *)&defminmtu;
2521 optdatalen = sizeof(int);
2524 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2528 optdata = (void *)&on;
2529 optdatalen = sizeof(on);
2531 case IPV6_PREFER_TEMPADDR:
2533 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2534 optdata = (void *)&defpreftemp;
2535 optdatalen = sizeof(int);
2537 default: /* should not happen */
2539 panic("ip6_getpcbopt: unexpected option\n");
2542 return (ENOPROTOOPT);
2546 error = sooptcopyout(sopt, optdata, optdatalen);
2548 free(optdata, M_TEMP);
2554 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2559 if (optname == -1 || optname == IPV6_PKTINFO) {
2560 if (pktopt->ip6po_pktinfo)
2561 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2562 pktopt->ip6po_pktinfo = NULL;
2564 if (optname == -1 || optname == IPV6_HOPLIMIT)
2565 pktopt->ip6po_hlim = -1;
2566 if (optname == -1 || optname == IPV6_TCLASS)
2567 pktopt->ip6po_tclass = -1;
2568 if (optname == -1 || optname == IPV6_NEXTHOP) {
2569 if (pktopt->ip6po_nextroute.ro_rt) {
2570 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2571 pktopt->ip6po_nextroute.ro_rt = NULL;
2573 if (pktopt->ip6po_nexthop)
2574 free(pktopt->ip6po_nexthop, M_IP6OPT);
2575 pktopt->ip6po_nexthop = NULL;
2577 if (optname == -1 || optname == IPV6_HOPOPTS) {
2578 if (pktopt->ip6po_hbh)
2579 free(pktopt->ip6po_hbh, M_IP6OPT);
2580 pktopt->ip6po_hbh = NULL;
2582 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2583 if (pktopt->ip6po_dest1)
2584 free(pktopt->ip6po_dest1, M_IP6OPT);
2585 pktopt->ip6po_dest1 = NULL;
2587 if (optname == -1 || optname == IPV6_RTHDR) {
2588 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2589 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2590 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2591 if (pktopt->ip6po_route.ro_rt) {
2592 RTFREE(pktopt->ip6po_route.ro_rt);
2593 pktopt->ip6po_route.ro_rt = NULL;
2596 if (optname == -1 || optname == IPV6_DSTOPTS) {
2597 if (pktopt->ip6po_dest2)
2598 free(pktopt->ip6po_dest2, M_IP6OPT);
2599 pktopt->ip6po_dest2 = NULL;
2603 #define PKTOPT_EXTHDRCPY(type) \
2606 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2607 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2608 if (dst->type == NULL)\
2610 bcopy(src->type, dst->type, hlen);\
2612 } while (/*CONSTCOND*/ 0)
2615 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2617 if (dst == NULL || src == NULL) {
2618 printf("ip6_clearpktopts: invalid argument\n");
2622 dst->ip6po_hlim = src->ip6po_hlim;
2623 dst->ip6po_tclass = src->ip6po_tclass;
2624 dst->ip6po_flags = src->ip6po_flags;
2625 dst->ip6po_minmtu = src->ip6po_minmtu;
2626 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2627 if (src->ip6po_pktinfo) {
2628 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2630 if (dst->ip6po_pktinfo == NULL)
2632 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2634 if (src->ip6po_nexthop) {
2635 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2637 if (dst->ip6po_nexthop == NULL)
2639 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2640 src->ip6po_nexthop->sa_len);
2642 PKTOPT_EXTHDRCPY(ip6po_hbh);
2643 PKTOPT_EXTHDRCPY(ip6po_dest1);
2644 PKTOPT_EXTHDRCPY(ip6po_dest2);
2645 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2649 ip6_clearpktopts(dst, -1);
2652 #undef PKTOPT_EXTHDRCPY
2654 struct ip6_pktopts *
2655 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2658 struct ip6_pktopts *dst;
2660 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2663 ip6_initpktopts(dst);
2665 if ((error = copypktopts(dst, src, canwait)) != 0) {
2666 free(dst, M_IP6OPT);
2674 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2679 ip6_clearpktopts(pktopt, -1);
2681 free(pktopt, M_IP6OPT);
2685 * Set IPv6 outgoing packet options based on advanced API.
2688 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2689 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2691 struct cmsghdr *cm = NULL;
2693 if (control == NULL || opt == NULL)
2696 ip6_initpktopts(opt);
2701 * If stickyopt is provided, make a local copy of the options
2702 * for this particular packet, then override them by ancillary
2704 * XXX: copypktopts() does not copy the cached route to a next
2705 * hop (if any). This is not very good in terms of efficiency,
2706 * but we can allow this since this option should be rarely
2709 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2714 * XXX: Currently, we assume all the optional information is stored
2717 if (control->m_next)
2720 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2721 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2724 if (control->m_len < CMSG_LEN(0))
2727 cm = mtod(control, struct cmsghdr *);
2728 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2730 if (cm->cmsg_level != IPPROTO_IPV6)
2733 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2734 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2743 * Set a particular packet option, as a sticky option or an ancillary data
2744 * item. "len" can be 0 only when it's a sticky option.
2745 * We have 4 cases of combination of "sticky" and "cmsg":
2746 * "sticky=0, cmsg=0": impossible
2747 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2748 * "sticky=1, cmsg=0": RFC3542 socket option
2749 * "sticky=1, cmsg=1": RFC2292 socket option
2752 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2753 struct ucred *cred, int sticky, int cmsg, int uproto)
2755 int minmtupolicy, preftemp;
2758 if (!sticky && !cmsg) {
2760 printf("ip6_setpktopt: impossible case\n");
2766 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2767 * not be specified in the context of RFC3542. Conversely,
2768 * RFC3542 types should not be specified in the context of RFC2292.
2772 case IPV6_2292PKTINFO:
2773 case IPV6_2292HOPLIMIT:
2774 case IPV6_2292NEXTHOP:
2775 case IPV6_2292HOPOPTS:
2776 case IPV6_2292DSTOPTS:
2777 case IPV6_2292RTHDR:
2778 case IPV6_2292PKTOPTIONS:
2779 return (ENOPROTOOPT);
2782 if (sticky && cmsg) {
2789 case IPV6_RTHDRDSTOPTS:
2791 case IPV6_USE_MIN_MTU:
2794 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2795 return (ENOPROTOOPT);
2800 case IPV6_2292PKTINFO:
2803 struct ifnet *ifp = NULL;
2804 struct in6_pktinfo *pktinfo;
2806 if (len != sizeof(struct in6_pktinfo))
2809 pktinfo = (struct in6_pktinfo *)buf;
2812 * An application can clear any sticky IPV6_PKTINFO option by
2813 * doing a "regular" setsockopt with ipi6_addr being
2814 * in6addr_any and ipi6_ifindex being zero.
2815 * [RFC 3542, Section 6]
2817 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2818 pktinfo->ipi6_ifindex == 0 &&
2819 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2820 ip6_clearpktopts(opt, optname);
2824 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2825 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2828 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2830 /* validate the interface index if specified. */
2831 if (pktinfo->ipi6_ifindex > V_if_index)
2833 if (pktinfo->ipi6_ifindex) {
2834 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2838 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2839 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2843 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2844 struct in6_ifaddr *ia;
2846 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2847 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2849 return (EADDRNOTAVAIL);
2850 ifa_free(&ia->ia_ifa);
2853 * We store the address anyway, and let in6_selectsrc()
2854 * validate the specified address. This is because ipi6_addr
2855 * may not have enough information about its scope zone, and
2856 * we may need additional information (such as outgoing
2857 * interface or the scope zone of a destination address) to
2858 * disambiguate the scope.
2859 * XXX: the delay of the validation may confuse the
2860 * application when it is used as a sticky option.
2862 if (opt->ip6po_pktinfo == NULL) {
2863 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2864 M_IP6OPT, M_NOWAIT);
2865 if (opt->ip6po_pktinfo == NULL)
2868 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2872 case IPV6_2292HOPLIMIT:
2878 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2879 * to simplify the ordering among hoplimit options.
2881 if (optname == IPV6_HOPLIMIT && sticky)
2882 return (ENOPROTOOPT);
2884 if (len != sizeof(int))
2887 if (*hlimp < -1 || *hlimp > 255)
2890 opt->ip6po_hlim = *hlimp;
2898 if (len != sizeof(int))
2900 tclass = *(int *)buf;
2901 if (tclass < -1 || tclass > 255)
2904 opt->ip6po_tclass = tclass;
2908 case IPV6_2292NEXTHOP:
2911 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
2916 if (len == 0) { /* just remove the option */
2917 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2921 /* check if cmsg_len is large enough for sa_len */
2922 if (len < sizeof(struct sockaddr) || len < *buf)
2925 switch (((struct sockaddr *)buf)->sa_family) {
2928 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2931 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2934 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2935 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2938 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2944 case AF_LINK: /* should eventually be supported */
2946 return (EAFNOSUPPORT);
2949 /* turn off the previous option, then set the new option. */
2950 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2951 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2952 if (opt->ip6po_nexthop == NULL)
2954 bcopy(buf, opt->ip6po_nexthop, *buf);
2957 case IPV6_2292HOPOPTS:
2960 struct ip6_hbh *hbh;
2964 * XXX: We don't allow a non-privileged user to set ANY HbH
2965 * options, since per-option restriction has too much
2969 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
2975 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2976 break; /* just remove the option */
2979 /* message length validation */
2980 if (len < sizeof(struct ip6_hbh))
2982 hbh = (struct ip6_hbh *)buf;
2983 hbhlen = (hbh->ip6h_len + 1) << 3;
2987 /* turn off the previous option, then set the new option. */
2988 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2989 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2990 if (opt->ip6po_hbh == NULL)
2992 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2997 case IPV6_2292DSTOPTS:
2999 case IPV6_RTHDRDSTOPTS:
3001 struct ip6_dest *dest, **newdest = NULL;
3004 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
3005 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3011 ip6_clearpktopts(opt, optname);
3012 break; /* just remove the option */
3015 /* message length validation */
3016 if (len < sizeof(struct ip6_dest))
3018 dest = (struct ip6_dest *)buf;
3019 destlen = (dest->ip6d_len + 1) << 3;
3024 * Determine the position that the destination options header
3025 * should be inserted; before or after the routing header.
3028 case IPV6_2292DSTOPTS:
3030 * The old advacned API is ambiguous on this point.
3031 * Our approach is to determine the position based
3032 * according to the existence of a routing header.
3033 * Note, however, that this depends on the order of the
3034 * extension headers in the ancillary data; the 1st
3035 * part of the destination options header must appear
3036 * before the routing header in the ancillary data,
3038 * RFC3542 solved the ambiguity by introducing
3039 * separate ancillary data or option types.
3041 if (opt->ip6po_rthdr == NULL)
3042 newdest = &opt->ip6po_dest1;
3044 newdest = &opt->ip6po_dest2;
3046 case IPV6_RTHDRDSTOPTS:
3047 newdest = &opt->ip6po_dest1;
3050 newdest = &opt->ip6po_dest2;
3054 /* turn off the previous option, then set the new option. */
3055 ip6_clearpktopts(opt, optname);
3056 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3057 if (*newdest == NULL)
3059 bcopy(dest, *newdest, destlen);
3064 case IPV6_2292RTHDR:
3067 struct ip6_rthdr *rth;
3071 ip6_clearpktopts(opt, IPV6_RTHDR);
3072 break; /* just remove the option */
3075 /* message length validation */
3076 if (len < sizeof(struct ip6_rthdr))
3078 rth = (struct ip6_rthdr *)buf;
3079 rthlen = (rth->ip6r_len + 1) << 3;
3083 switch (rth->ip6r_type) {
3084 case IPV6_RTHDR_TYPE_0:
3085 if (rth->ip6r_len == 0) /* must contain one addr */
3087 if (rth->ip6r_len % 2) /* length must be even */
3089 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3093 return (EINVAL); /* not supported */
3096 /* turn off the previous option */
3097 ip6_clearpktopts(opt, IPV6_RTHDR);
3098 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3099 if (opt->ip6po_rthdr == NULL)
3101 bcopy(rth, opt->ip6po_rthdr, rthlen);
3106 case IPV6_USE_MIN_MTU:
3107 if (len != sizeof(int))
3109 minmtupolicy = *(int *)buf;
3110 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3111 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3112 minmtupolicy != IP6PO_MINMTU_ALL) {
3115 opt->ip6po_minmtu = minmtupolicy;
3119 if (len != sizeof(int))
3122 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3124 * we ignore this option for TCP sockets.
3125 * (RFC3542 leaves this case unspecified.)
3127 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3129 opt->ip6po_flags |= IP6PO_DONTFRAG;
3132 case IPV6_PREFER_TEMPADDR:
3133 if (len != sizeof(int))
3135 preftemp = *(int *)buf;
3136 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3137 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3138 preftemp != IP6PO_TEMPADDR_PREFER) {
3141 opt->ip6po_prefer_tempaddr = preftemp;
3145 return (ENOPROTOOPT);
3146 } /* end of switch */
3152 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3153 * packet to the input queue of a specified interface. Note that this
3154 * calls the output routine of the loopback "driver", but with an interface
3155 * pointer that might NOT be &loif -- easier than replicating that code here.
3158 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3161 struct ip6_hdr *ip6;
3163 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3168 * Make sure to deep-copy IPv6 header portion in case the data
3169 * is in an mbuf cluster, so that we can safely override the IPv6
3170 * header portion later.
3172 if (!M_WRITABLE(copym) ||
3173 copym->m_len < sizeof(struct ip6_hdr)) {
3174 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3178 ip6 = mtod(copym, struct ip6_hdr *);
3180 * clear embedded scope identifiers if necessary.
3181 * in6_clearscope will touch the addresses only when necessary.
3183 in6_clearscope(&ip6->ip6_src);
3184 in6_clearscope(&ip6->ip6_dst);
3185 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3186 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3188 copym->m_pkthdr.csum_data = 0xffff;
3190 if_simloop(ifp, copym, AF_INET6, 0);
3194 * Chop IPv6 header off from the payload.
3197 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3200 struct ip6_hdr *ip6;
3202 ip6 = mtod(m, struct ip6_hdr *);
3203 if (m->m_len > sizeof(*ip6)) {
3204 mh = m_gethdr(M_NOWAIT, MT_DATA);
3209 m_move_pkthdr(mh, m);
3210 M_ALIGN(mh, sizeof(*ip6));
3211 m->m_len -= sizeof(*ip6);
3212 m->m_data += sizeof(*ip6);
3215 m->m_len = sizeof(*ip6);
3216 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3218 exthdrs->ip6e_ip6 = m;
3223 * Compute IPv6 extension header length.
3226 ip6_optlen(struct inpcb *inp)
3230 if (!inp->in6p_outputopts)
3235 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3237 len += elen(inp->in6p_outputopts->ip6po_hbh);
3238 if (inp->in6p_outputopts->ip6po_rthdr)
3239 /* dest1 is valid with rthdr only */
3240 len += elen(inp->in6p_outputopts->ip6po_dest1);
3241 len += elen(inp->in6p_outputopts->ip6po_rthdr);
3242 len += elen(inp->in6p_outputopts->ip6po_dest2);