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 route_in6 ip6route;
388 struct rtentry *rt = NULL;
389 struct sockaddr_in6 *dst, src_sa, dst_sa;
390 struct in6_addr odst;
392 struct in6_ifaddr *ia = NULL;
394 int alwaysfrag, dontfrag;
395 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
396 struct ip6_exthdrs exthdrs;
397 struct in6_addr src0, dst0;
399 struct route_in6 *ro_pmtu = NULL;
404 struct m_tag *fwd_tag = NULL;
408 INP_LOCK_ASSERT(inp);
409 M_SETFIB(m, inp->inp_inc.inc_fibnum);
410 if ((flags & IP_NODEFAULTFLOWID) == 0) {
411 /* unconditionally set flowid */
412 m->m_pkthdr.flowid = inp->inp_flowid;
413 M_HASHTYPE_SET(m, inp->inp_flowtype);
416 m->m_pkthdr.numa_domain = inp->inp_numa_domain;
420 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
422 * IPSec checking which handles several cases.
423 * FAST IPSEC: We re-injected the packet.
424 * XXX: need scope argument.
426 if (IPSEC_ENABLED(ipv6)) {
427 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
428 if (error == EINPROGRESS)
435 bzero(&exthdrs, sizeof(exthdrs));
437 /* Hop-by-Hop options header */
438 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
439 /* Destination options header(1st part) */
440 if (opt->ip6po_rthdr) {
442 * Destination options header(1st part)
443 * This only makes sense with a routing header.
444 * See Section 9.2 of RFC 3542.
445 * Disabling this part just for MIP6 convenience is
446 * a bad idea. We need to think carefully about a
447 * way to make the advanced API coexist with MIP6
448 * options, which might automatically be inserted in
451 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
454 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
455 /* Destination options header(2nd part) */
456 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
460 * Calculate the total length of the extension header chain.
461 * Keep the length of the unfragmentable part for fragmentation.
464 if (exthdrs.ip6e_hbh)
465 optlen += exthdrs.ip6e_hbh->m_len;
466 if (exthdrs.ip6e_dest1)
467 optlen += exthdrs.ip6e_dest1->m_len;
468 if (exthdrs.ip6e_rthdr)
469 optlen += exthdrs.ip6e_rthdr->m_len;
470 unfragpartlen = optlen + sizeof(struct ip6_hdr);
472 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
473 if (exthdrs.ip6e_dest2)
474 optlen += exthdrs.ip6e_dest2->m_len;
477 * If there is at least one extension header,
478 * separate IP6 header from the payload.
480 if (optlen && !hdrsplit) {
481 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
485 m = exthdrs.ip6e_ip6;
489 ip6 = mtod(m, struct ip6_hdr *);
491 /* adjust mbuf packet header length */
492 m->m_pkthdr.len += optlen;
493 plen = m->m_pkthdr.len - sizeof(*ip6);
495 /* If this is a jumbo payload, insert a jumbo payload option. */
496 if (plen > IPV6_MAXPACKET) {
498 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
502 m = exthdrs.ip6e_ip6;
506 ip6 = mtod(m, struct ip6_hdr *);
507 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
511 ip6->ip6_plen = htons(plen);
514 * Concatenate headers and fill in next header fields.
515 * Here we have, on "m"
517 * and we insert headers accordingly. Finally, we should be getting:
518 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
520 * during the header composing process, "m" points to IPv6 header.
521 * "mprev" points to an extension header prior to esp.
523 u_char *nexthdrp = &ip6->ip6_nxt;
527 * we treat dest2 specially. this makes IPsec processing
528 * much easier. the goal here is to make mprev point the
529 * mbuf prior to dest2.
531 * result: IPv6 dest2 payload
532 * m and mprev will point to IPv6 header.
534 if (exthdrs.ip6e_dest2) {
536 panic("assumption failed: hdr not split");
537 exthdrs.ip6e_dest2->m_next = m->m_next;
538 m->m_next = exthdrs.ip6e_dest2;
539 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
540 ip6->ip6_nxt = IPPROTO_DSTOPTS;
544 * result: IPv6 hbh dest1 rthdr dest2 payload
545 * m will point to IPv6 header. mprev will point to the
546 * extension header prior to dest2 (rthdr in the above case).
548 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
549 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
551 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
555 * If there is a routing header, discard the packet.
557 if (exthdrs.ip6e_rthdr) {
562 /* Source address validation */
563 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
564 (flags & IPV6_UNSPECSRC) == 0) {
566 IP6STAT_INC(ip6s_badscope);
569 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
571 IP6STAT_INC(ip6s_badscope);
575 IP6STAT_INC(ip6s_localout);
582 bzero((caddr_t)ro, sizeof(*ro));
585 if (opt && opt->ip6po_rthdr)
586 ro = &opt->ip6po_route;
587 dst = (struct sockaddr_in6 *)&ro->ro_dst;
588 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
591 * if specified, try to fill in the traffic class field.
592 * do not override if a non-zero value is already set.
593 * we check the diffserv field and the ecn field separately.
595 if (opt && opt->ip6po_tclass >= 0) {
598 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
600 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
603 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
606 /* fill in or override the hop limit field, if necessary. */
607 if (opt && opt->ip6po_hlim != -1)
608 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
609 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
611 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
613 ip6->ip6_hlim = V_ip6_defmcasthlim;
616 * Validate route against routing table additions;
617 * a better/more specific route might have been added.
618 * Make sure address family is set in route.
621 ro->ro_dst.sin6_family = AF_INET6;
622 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
624 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
625 ro->ro_dst.sin6_family == AF_INET6 &&
626 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
628 ifp = ro->ro_rt->rt_ifp;
631 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
633 if (fwd_tag == NULL) {
634 bzero(&dst_sa, sizeof(dst_sa));
635 dst_sa.sin6_family = AF_INET6;
636 dst_sa.sin6_len = sizeof(dst_sa);
637 dst_sa.sin6_addr = ip6->ip6_dst;
639 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
643 in6_ifstat_inc(ifp, ifs6_out_discard);
649 * If in6_selectroute() does not return a route entry,
650 * dst may not have been updated.
652 *dst = dst_sa; /* XXX */
656 * then rt (for unicast) and ifp must be non-NULL valid values.
658 if ((flags & IPV6_FORWARDING) == 0) {
659 /* XXX: the FORWARDING flag can be set for mrouting. */
660 in6_ifstat_inc(ifp, ifs6_out_request);
663 ia = (struct in6_ifaddr *)(rt->rt_ifa);
664 counter_u64_add(rt->rt_pksent, 1);
667 /* Setup data structures for scope ID checks. */
669 bzero(&src_sa, sizeof(src_sa));
670 src_sa.sin6_family = AF_INET6;
671 src_sa.sin6_len = sizeof(src_sa);
672 src_sa.sin6_addr = ip6->ip6_src;
675 /* re-initialize to be sure */
676 bzero(&dst_sa, sizeof(dst_sa));
677 dst_sa.sin6_family = AF_INET6;
678 dst_sa.sin6_len = sizeof(dst_sa);
679 dst_sa.sin6_addr = ip6->ip6_dst;
681 /* Check for valid scope ID. */
682 if (in6_setscope(&src0, ifp, &zone) == 0 &&
683 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
684 in6_setscope(&dst0, ifp, &zone) == 0 &&
685 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
687 * The outgoing interface is in the zone of the source
688 * and destination addresses.
690 * Because the loopback interface cannot receive
691 * packets with a different scope ID than its own,
692 * there is a trick is to pretend the outgoing packet
693 * was received by the real network interface, by
694 * setting "origifp" different from "ifp". This is
695 * only allowed when "ifp" is a loopback network
696 * interface. Refer to code in nd6_output_ifp() for
702 * We should use ia_ifp to support the case of sending
703 * packets to an address of our own.
705 if (ia != NULL && ia->ia_ifp)
708 } else if ((ifp->if_flags & IFF_LOOPBACK) == 0 ||
709 sa6_recoverscope(&src_sa) != 0 ||
710 sa6_recoverscope(&dst_sa) != 0 ||
711 dst_sa.sin6_scope_id == 0 ||
712 (src_sa.sin6_scope_id != 0 &&
713 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
714 (origifp = ifnet_byindex(dst_sa.sin6_scope_id)) == NULL) {
716 * If the destination network interface is not a
717 * loopback interface, or the destination network
718 * address has no scope ID, or the source address has
719 * a scope ID set which is different from the
720 * destination address one, or there is no network
721 * interface representing this scope ID, the address
722 * pair is considered invalid.
724 IP6STAT_INC(ip6s_badscope);
725 in6_ifstat_inc(ifp, ifs6_out_discard);
727 error = EHOSTUNREACH; /* XXX */
731 /* All scope ID checks are successful. */
733 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
734 if (opt && opt->ip6po_nextroute.ro_rt) {
736 * The nexthop is explicitly specified by the
737 * application. We assume the next hop is an IPv6
740 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
742 else if ((rt->rt_flags & RTF_GATEWAY))
743 dst = (struct sockaddr_in6 *)rt->rt_gateway;
746 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
747 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
749 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
750 in6_ifstat_inc(ifp, ifs6_out_mcast);
752 * Confirm that the outgoing interface supports multicast.
754 if (!(ifp->if_flags & IFF_MULTICAST)) {
755 IP6STAT_INC(ip6s_noroute);
756 in6_ifstat_inc(ifp, ifs6_out_discard);
760 if ((im6o == NULL && in6_mcast_loop) ||
761 (im6o && im6o->im6o_multicast_loop)) {
763 * Loop back multicast datagram if not expressly
764 * forbidden to do so, even if we have not joined
765 * the address; protocols will filter it later,
766 * thus deferring a hash lookup and lock acquisition
767 * at the expense of an m_copym().
769 ip6_mloopback(ifp, m);
772 * If we are acting as a multicast router, perform
773 * multicast forwarding as if the packet had just
774 * arrived on the interface to which we are about
775 * to send. The multicast forwarding function
776 * recursively calls this function, using the
777 * IPV6_FORWARDING flag to prevent infinite recursion.
779 * Multicasts that are looped back by ip6_mloopback(),
780 * above, will be forwarded by the ip6_input() routine,
783 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
785 * XXX: ip6_mforward expects that rcvif is NULL
786 * when it is called from the originating path.
787 * However, it may not always be the case.
789 m->m_pkthdr.rcvif = NULL;
790 if (ip6_mforward(ip6, ifp, m) != 0) {
797 * Multicasts with a hoplimit of zero may be looped back,
798 * above, but must not be transmitted on a network.
799 * Also, multicasts addressed to the loopback interface
800 * are not sent -- the above call to ip6_mloopback() will
801 * loop back a copy if this host actually belongs to the
802 * destination group on the loopback interface.
804 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
805 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
812 * Fill the outgoing inteface to tell the upper layer
813 * to increment per-interface statistics.
818 /* Determine path MTU. */
819 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
820 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
824 * The caller of this function may specify to use the minimum MTU
826 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
827 * setting. The logic is a bit complicated; by default, unicast
828 * packets will follow path MTU while multicast packets will be sent at
829 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
830 * including unicast ones will be sent at the minimum MTU. Multicast
831 * packets will always be sent at the minimum MTU unless
832 * IP6PO_MINMTU_DISABLE is explicitly specified.
833 * See RFC 3542 for more details.
835 if (mtu > IPV6_MMTU) {
836 if ((flags & IPV6_MINMTU))
838 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
840 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
842 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
848 * clear embedded scope identifiers if necessary.
849 * in6_clearscope will touch the addresses only when necessary.
851 in6_clearscope(&ip6->ip6_src);
852 in6_clearscope(&ip6->ip6_dst);
855 * If the outgoing packet contains a hop-by-hop options header,
856 * it must be examined and processed even by the source node.
857 * (RFC 2460, section 4.)
859 if (exthdrs.ip6e_hbh) {
860 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
861 u_int32_t dummy; /* XXX unused */
862 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
865 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
866 panic("ip6e_hbh is not contiguous");
869 * XXX: if we have to send an ICMPv6 error to the sender,
870 * we need the M_LOOP flag since icmp6_error() expects
871 * the IPv6 and the hop-by-hop options header are
872 * contiguous unless the flag is set.
874 m->m_flags |= M_LOOP;
875 m->m_pkthdr.rcvif = ifp;
876 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
877 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
878 &dummy, &plen) < 0) {
879 /* m was already freed at this point */
880 error = EINVAL;/* better error? */
883 m->m_flags &= ~M_LOOP; /* XXX */
884 m->m_pkthdr.rcvif = NULL;
887 /* Jump over all PFIL processing if hooks are not active. */
888 if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
892 /* Run through list of hooks for output packets. */
893 switch (pfil_run_hooks(V_inet6_pfil_head, &m, ifp, PFIL_OUT, inp)) {
895 ip6 = mtod(m, struct ip6_hdr *);
905 /* See if destination IP address was changed by packet filter. */
906 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
907 m->m_flags |= M_SKIP_FIREWALL;
908 /* If destination is now ourself drop to ip6_input(). */
909 if (in6_localip(&ip6->ip6_dst)) {
910 m->m_flags |= M_FASTFWD_OURS;
911 if (m->m_pkthdr.rcvif == NULL)
912 m->m_pkthdr.rcvif = V_loif;
913 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
914 m->m_pkthdr.csum_flags |=
915 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
916 m->m_pkthdr.csum_data = 0xffff;
919 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
920 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
922 error = netisr_queue(NETISR_IPV6, m);
925 RO_INVALIDATE_CACHE(ro);
926 needfiblookup = 1; /* Redo the routing table lookup. */
929 /* See if fib was changed by packet filter. */
930 if (fibnum != M_GETFIB(m)) {
931 m->m_flags |= M_SKIP_FIREWALL;
932 fibnum = M_GETFIB(m);
933 RO_INVALIDATE_CACHE(ro);
939 /* See if local, if yes, send it to netisr. */
940 if (m->m_flags & M_FASTFWD_OURS) {
941 if (m->m_pkthdr.rcvif == NULL)
942 m->m_pkthdr.rcvif = V_loif;
943 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
944 m->m_pkthdr.csum_flags |=
945 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
946 m->m_pkthdr.csum_data = 0xffff;
949 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
950 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
952 error = netisr_queue(NETISR_IPV6, m);
955 /* Or forward to some other address? */
956 if ((m->m_flags & M_IP6_NEXTHOP) &&
957 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
958 dst = (struct sockaddr_in6 *)&ro->ro_dst;
959 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
960 m->m_flags |= M_SKIP_FIREWALL;
961 m->m_flags &= ~M_IP6_NEXTHOP;
962 m_tag_delete(m, fwd_tag);
968 * Send the packet to the outgoing interface.
969 * If necessary, do IPv6 fragmentation before sending.
971 * the logic here is rather complex:
972 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
973 * 1-a: send as is if tlen <= path mtu
974 * 1-b: fragment if tlen > path mtu
976 * 2: if user asks us not to fragment (dontfrag == 1)
977 * 2-a: send as is if tlen <= interface mtu
978 * 2-b: error if tlen > interface mtu
980 * 3: if we always need to attach fragment header (alwaysfrag == 1)
983 * 4: if dontfrag == 1 && alwaysfrag == 1
984 * error, as we cannot handle this conflicting request
986 sw_csum = m->m_pkthdr.csum_flags;
988 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
989 sw_csum &= ~ifp->if_hwassist;
993 * If we added extension headers, we will not do TSO and calculate the
994 * checksums ourselves for now.
995 * XXX-BZ Need a framework to know when the NIC can handle it, even
998 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
999 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
1000 m = mb_unmapped_to_ext(m);
1003 IP6STAT_INC(ip6s_odropped);
1006 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
1007 } else if ((ifp->if_capenable & IFCAP_NOMAP) == 0) {
1008 m = mb_unmapped_to_ext(m);
1011 IP6STAT_INC(ip6s_odropped);
1016 if (sw_csum & CSUM_SCTP_IPV6) {
1017 sw_csum &= ~CSUM_SCTP_IPV6;
1018 m = mb_unmapped_to_ext(m);
1021 IP6STAT_INC(ip6s_odropped);
1024 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
1027 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
1028 tlen = m->m_pkthdr.len;
1030 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
1034 if (dontfrag && alwaysfrag) { /* case 4 */
1035 /* conflicting request - can't transmit */
1039 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
1041 * Even if the DONTFRAG option is specified, we cannot send the
1042 * packet when the data length is larger than the MTU of the
1043 * outgoing interface.
1044 * Notify the error by sending IPV6_PATHMTU ancillary data if
1045 * application wanted to know the MTU value. Also return an
1046 * error code (this is not described in the API spec).
1049 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
1055 * transmit packet without fragmentation
1057 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
1058 struct in6_ifaddr *ia6;
1060 ip6 = mtod(m, struct ip6_hdr *);
1061 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1063 /* Record statistics for this interface address. */
1064 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
1065 counter_u64_add(ia6->ia_ifa.ifa_obytes,
1067 ifa_free(&ia6->ia_ifa);
1069 error = ip6_output_send(inp, ifp, origifp, m, dst, ro);
1074 * try to fragment the packet. case 1-b and 3
1076 if (mtu < IPV6_MMTU) {
1077 /* path MTU cannot be less than IPV6_MMTU */
1079 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1081 } else if (ip6->ip6_plen == 0) {
1082 /* jumbo payload cannot be fragmented */
1084 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1090 * Too large for the destination or interface;
1091 * fragment if possible.
1092 * Must be able to put at least 8 bytes per fragment.
1094 hlen = unfragpartlen;
1095 if (mtu > IPV6_MAXPACKET)
1096 mtu = IPV6_MAXPACKET;
1098 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1101 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1106 * If the interface will not calculate checksums on
1107 * fragmented packets, then do it here.
1108 * XXX-BZ handle the hw offloading case. Need flags.
1110 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1111 m = mb_unmapped_to_ext(m);
1113 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1117 in6_delayed_cksum(m, plen, hlen);
1118 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1121 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1122 m = mb_unmapped_to_ext(m);
1124 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1128 sctp_delayed_cksum(m, hlen);
1129 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1133 * Change the next header field of the last header in the
1134 * unfragmentable part.
1136 if (exthdrs.ip6e_rthdr) {
1137 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1138 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1139 } else if (exthdrs.ip6e_dest1) {
1140 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1141 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1142 } else if (exthdrs.ip6e_hbh) {
1143 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1144 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1146 nextproto = ip6->ip6_nxt;
1147 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1151 * Loop through length of segment after first fragment,
1152 * make new header and copy data of each part and link onto
1156 id = htonl(ip6_randomid());
1157 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1160 in6_ifstat_inc(ifp, ifs6_out_fragok);
1164 * Remove leading garbages.
1174 /* Record statistics for this interface address. */
1176 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1177 counter_u64_add(ia->ia_ifa.ifa_obytes,
1180 error = ip6_output_send(inp, ifp, origifp, m, dst, ro);
1186 IP6STAT_INC(ip6s_fragmented);
1189 if (ro == &ip6route)
1194 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1195 m_freem(exthdrs.ip6e_dest1);
1196 m_freem(exthdrs.ip6e_rthdr);
1197 m_freem(exthdrs.ip6e_dest2);
1206 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1210 if (hlen > MCLBYTES)
1211 return (ENOBUFS); /* XXX */
1214 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1216 m = m_get(M_NOWAIT, MT_DATA);
1221 bcopy(hdr, mtod(m, caddr_t), hlen);
1228 * Insert jumbo payload option.
1231 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1237 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1240 * If there is no hop-by-hop options header, allocate new one.
1241 * If there is one but it doesn't have enough space to store the
1242 * jumbo payload option, allocate a cluster to store the whole options.
1243 * Otherwise, use it to store the options.
1245 if (exthdrs->ip6e_hbh == NULL) {
1246 mopt = m_get(M_NOWAIT, MT_DATA);
1249 mopt->m_len = JUMBOOPTLEN;
1250 optbuf = mtod(mopt, u_char *);
1251 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1252 exthdrs->ip6e_hbh = mopt;
1254 struct ip6_hbh *hbh;
1256 mopt = exthdrs->ip6e_hbh;
1257 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1260 * - exthdrs->ip6e_hbh is not referenced from places
1261 * other than exthdrs.
1262 * - exthdrs->ip6e_hbh is not an mbuf chain.
1264 int oldoptlen = mopt->m_len;
1268 * XXX: give up if the whole (new) hbh header does
1269 * not fit even in an mbuf cluster.
1271 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1275 * As a consequence, we must always prepare a cluster
1278 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1281 n->m_len = oldoptlen + JUMBOOPTLEN;
1282 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1284 optbuf = mtod(n, caddr_t) + oldoptlen;
1286 mopt = exthdrs->ip6e_hbh = n;
1288 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1289 mopt->m_len += JUMBOOPTLEN;
1291 optbuf[0] = IP6OPT_PADN;
1295 * Adjust the header length according to the pad and
1296 * the jumbo payload option.
1298 hbh = mtod(mopt, struct ip6_hbh *);
1299 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1302 /* fill in the option. */
1303 optbuf[2] = IP6OPT_JUMBO;
1305 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1306 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1308 /* finally, adjust the packet header length */
1309 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1316 * Insert fragment header and copy unfragmentable header portions.
1319 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1320 struct ip6_frag **frghdrp)
1322 struct mbuf *n, *mlast;
1324 if (hlen > sizeof(struct ip6_hdr)) {
1325 n = m_copym(m0, sizeof(struct ip6_hdr),
1326 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1333 /* Search for the last mbuf of unfragmentable part. */
1334 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1337 if (M_WRITABLE(mlast) &&
1338 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1339 /* use the trailing space of the last mbuf for the fragment hdr */
1340 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1342 mlast->m_len += sizeof(struct ip6_frag);
1343 m->m_pkthdr.len += sizeof(struct ip6_frag);
1345 /* allocate a new mbuf for the fragment header */
1348 mfrg = m_get(M_NOWAIT, MT_DATA);
1351 mfrg->m_len = sizeof(struct ip6_frag);
1352 *frghdrp = mtod(mfrg, struct ip6_frag *);
1353 mlast->m_next = mfrg;
1360 * Calculates IPv6 path mtu for destination @dst.
1361 * Resulting MTU is stored in @mtup.
1363 * Returns 0 on success.
1366 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1368 struct nhop6_extended nh6;
1369 struct in6_addr kdst;
1375 in6_splitscope(dst, &kdst, &scopeid);
1376 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1377 return (EHOSTUNREACH);
1382 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1383 fib6_free_nh_ext(fibnum, &nh6);
1389 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1390 * and cached data in @ro_pmtu.
1391 * MTU from (successful) route lookup is saved (along with dst)
1392 * inside @ro_pmtu to avoid subsequent route lookups after packet
1393 * filter processing.
1395 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1396 * Returns 0 on success.
1399 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1400 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1401 int *alwaysfragp, u_int fibnum, u_int proto)
1403 struct nhop6_basic nh6;
1404 struct in6_addr kdst;
1406 struct sockaddr_in6 *sa6_dst;
1413 * Here ro_pmtu has final destination address, while
1414 * ro might represent immediate destination.
1415 * Use ro_pmtu destination since mtu might differ.
1417 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1418 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1419 ro_pmtu->ro_mtu = 0;
1421 if (ro_pmtu->ro_mtu == 0) {
1422 bzero(sa6_dst, sizeof(*sa6_dst));
1423 sa6_dst->sin6_family = AF_INET6;
1424 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1425 sa6_dst->sin6_addr = *dst;
1427 in6_splitscope(dst, &kdst, &scopeid);
1428 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1430 ro_pmtu->ro_mtu = nh6.nh_mtu;
1433 mtu = ro_pmtu->ro_mtu;
1437 mtu = ro_pmtu->ro_rt->rt_mtu;
1439 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1443 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1444 * hostcache data for @dst.
1445 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1447 * Returns 0 on success.
1450 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1451 u_long *mtup, int *alwaysfragp, u_int proto)
1459 struct in_conninfo inc;
1461 bzero(&inc, sizeof(inc));
1462 inc.inc_flags |= INC_ISIPV6;
1463 inc.inc6_faddr = *dst;
1465 ifmtu = IN6_LINKMTU(ifp);
1467 /* TCP is known to react to pmtu changes so skip hc */
1468 if (proto != IPPROTO_TCP)
1469 mtu = tcp_hc_getmtu(&inc);
1472 mtu = min(mtu, rt_mtu);
1477 else if (mtu < IPV6_MMTU) {
1479 * RFC2460 section 5, last paragraph:
1480 * if we record ICMPv6 too big message with
1481 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1482 * or smaller, with framgent header attached.
1483 * (fragment header is needed regardless from the
1484 * packet size, for translators to identify packets)
1490 mtu = IN6_LINKMTU(ifp);
1492 error = EHOSTUNREACH; /* XXX */
1496 *alwaysfragp = alwaysfrag;
1501 * IP6 socket option processing.
1504 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1506 int optdatalen, uproto;
1508 struct inpcb *inp = sotoinpcb(so);
1510 int level, op, optname;
1514 uint32_t rss_bucket;
1519 * Don't use more than a quarter of mbuf clusters. N.B.:
1520 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1521 * on LP64 architectures, so cast to u_long to avoid undefined
1522 * behavior. ILP32 architectures cannot have nmbclusters
1523 * large enough to overflow for other reasons.
1525 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1527 level = sopt->sopt_level;
1528 op = sopt->sopt_dir;
1529 optname = sopt->sopt_name;
1530 optlen = sopt->sopt_valsize;
1534 uproto = (int)so->so_proto->pr_protocol;
1536 if (level != IPPROTO_IPV6) {
1539 if (sopt->sopt_level == SOL_SOCKET &&
1540 sopt->sopt_dir == SOPT_SET) {
1541 switch (sopt->sopt_name) {
1544 if ((so->so_options & SO_REUSEADDR) != 0)
1545 inp->inp_flags2 |= INP_REUSEADDR;
1547 inp->inp_flags2 &= ~INP_REUSEADDR;
1553 if ((so->so_options & SO_REUSEPORT) != 0)
1554 inp->inp_flags2 |= INP_REUSEPORT;
1556 inp->inp_flags2 &= ~INP_REUSEPORT;
1560 case SO_REUSEPORT_LB:
1562 if ((so->so_options & SO_REUSEPORT_LB) != 0)
1563 inp->inp_flags2 |= INP_REUSEPORT_LB;
1565 inp->inp_flags2 &= ~INP_REUSEPORT_LB;
1571 inp->inp_inc.inc_fibnum = so->so_fibnum;
1575 case SO_MAX_PACING_RATE:
1578 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1589 } else { /* level == IPPROTO_IPV6 */
1594 case IPV6_2292PKTOPTIONS:
1595 #ifdef IPV6_PKTOPTIONS
1596 case IPV6_PKTOPTIONS:
1601 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1602 printf("ip6_ctloutput: mbuf limit hit\n");
1607 error = soopt_getm(sopt, &m); /* XXX */
1610 error = soopt_mcopyin(sopt, m); /* XXX */
1613 error = ip6_pcbopts(&inp->in6p_outputopts,
1615 m_freem(m); /* XXX */
1620 * Use of some Hop-by-Hop options or some
1621 * Destination options, might require special
1622 * privilege. That is, normal applications
1623 * (without special privilege) might be forbidden
1624 * from setting certain options in outgoing packets,
1625 * and might never see certain options in received
1626 * packets. [RFC 2292 Section 6]
1627 * KAME specific note:
1628 * KAME prevents non-privileged users from sending or
1629 * receiving ANY hbh/dst options in order to avoid
1630 * overhead of parsing options in the kernel.
1632 case IPV6_RECVHOPOPTS:
1633 case IPV6_RECVDSTOPTS:
1634 case IPV6_RECVRTHDRDSTOPTS:
1636 error = priv_check(td,
1637 PRIV_NETINET_SETHDROPTS);
1642 case IPV6_UNICAST_HOPS:
1645 case IPV6_RECVPKTINFO:
1646 case IPV6_RECVHOPLIMIT:
1647 case IPV6_RECVRTHDR:
1648 case IPV6_RECVPATHMTU:
1649 case IPV6_RECVTCLASS:
1650 case IPV6_RECVFLOWID:
1652 case IPV6_RECVRSSBUCKETID:
1655 case IPV6_AUTOFLOWLABEL:
1656 case IPV6_ORIGDSTADDR:
1658 case IPV6_BINDMULTI:
1660 case IPV6_RSS_LISTEN_BUCKET:
1662 if (optname == IPV6_BINDANY && td != NULL) {
1663 error = priv_check(td,
1664 PRIV_NETINET_BINDANY);
1669 if (optlen != sizeof(int)) {
1673 error = sooptcopyin(sopt, &optval,
1674 sizeof optval, sizeof optval);
1679 case IPV6_UNICAST_HOPS:
1680 if (optval < -1 || optval >= 256)
1683 /* -1 = kernel default */
1684 inp->in6p_hops = optval;
1685 if ((inp->inp_vflag &
1687 inp->inp_ip_ttl = optval;
1690 #define OPTSET(bit) \
1694 inp->inp_flags |= (bit); \
1696 inp->inp_flags &= ~(bit); \
1698 } while (/*CONSTCOND*/ 0)
1699 #define OPTSET2292(bit) \
1702 inp->inp_flags |= IN6P_RFC2292; \
1704 inp->inp_flags |= (bit); \
1706 inp->inp_flags &= ~(bit); \
1708 } while (/*CONSTCOND*/ 0)
1709 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0)
1711 #define OPTSET2_N(bit, val) do { \
1713 inp->inp_flags2 |= bit; \
1715 inp->inp_flags2 &= ~bit; \
1717 #define OPTSET2(bit, val) do { \
1719 OPTSET2_N(bit, val); \
1722 #define OPTBIT2(bit) (inp->inp_flags2 & (bit) ? 1 : 0)
1723 #define OPTSET2292_EXCLUSIVE(bit) \
1726 if (OPTBIT(IN6P_RFC2292)) { \
1730 inp->inp_flags |= (bit); \
1732 inp->inp_flags &= ~(bit); \
1735 } while (/*CONSTCOND*/ 0)
1737 case IPV6_RECVPKTINFO:
1738 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1743 struct ip6_pktopts **optp;
1745 /* cannot mix with RFC2292 */
1746 if (OPTBIT(IN6P_RFC2292)) {
1751 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1753 return (ECONNRESET);
1755 optp = &inp->in6p_outputopts;
1756 error = ip6_pcbopt(IPV6_HOPLIMIT,
1757 (u_char *)&optval, sizeof(optval),
1758 optp, (td != NULL) ? td->td_ucred :
1764 case IPV6_RECVHOPLIMIT:
1765 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1768 case IPV6_RECVHOPOPTS:
1769 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1772 case IPV6_RECVDSTOPTS:
1773 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1776 case IPV6_RECVRTHDRDSTOPTS:
1777 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1780 case IPV6_RECVRTHDR:
1781 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1784 case IPV6_RECVPATHMTU:
1786 * We ignore this option for TCP
1788 * (RFC3542 leaves this case
1791 if (uproto != IPPROTO_TCP)
1795 case IPV6_RECVFLOWID:
1796 OPTSET2(INP_RECVFLOWID, optval);
1800 case IPV6_RECVRSSBUCKETID:
1801 OPTSET2(INP_RECVRSSBUCKETID, optval);
1807 * make setsockopt(IPV6_V6ONLY)
1808 * available only prior to bind(2).
1809 * see ipng mailing list, Jun 22 2001.
1811 if (inp->inp_lport ||
1812 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1816 OPTSET(IN6P_IPV6_V6ONLY);
1818 inp->inp_vflag &= ~INP_IPV4;
1820 inp->inp_vflag |= INP_IPV4;
1822 case IPV6_RECVTCLASS:
1823 /* cannot mix with RFC2292 XXX */
1824 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1826 case IPV6_AUTOFLOWLABEL:
1827 OPTSET(IN6P_AUTOFLOWLABEL);
1830 case IPV6_ORIGDSTADDR:
1831 OPTSET2(INP_ORIGDSTADDR, optval);
1834 OPTSET(INP_BINDANY);
1837 case IPV6_BINDMULTI:
1838 OPTSET2(INP_BINDMULTI, optval);
1841 case IPV6_RSS_LISTEN_BUCKET:
1842 if ((optval >= 0) &&
1843 (optval < rss_getnumbuckets())) {
1845 inp->inp_rss_listen_bucket = optval;
1846 OPTSET2_N(INP_RSS_BUCKET_SET, 1);
1858 case IPV6_USE_MIN_MTU:
1859 case IPV6_PREFER_TEMPADDR:
1860 if (optlen != sizeof(optval)) {
1864 error = sooptcopyin(sopt, &optval,
1865 sizeof optval, sizeof optval);
1869 struct ip6_pktopts **optp;
1871 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1873 return (ECONNRESET);
1875 optp = &inp->in6p_outputopts;
1876 error = ip6_pcbopt(optname,
1877 (u_char *)&optval, sizeof(optval),
1878 optp, (td != NULL) ? td->td_ucred :
1884 case IPV6_2292PKTINFO:
1885 case IPV6_2292HOPLIMIT:
1886 case IPV6_2292HOPOPTS:
1887 case IPV6_2292DSTOPTS:
1888 case IPV6_2292RTHDR:
1890 if (optlen != sizeof(int)) {
1894 error = sooptcopyin(sopt, &optval,
1895 sizeof optval, sizeof optval);
1899 case IPV6_2292PKTINFO:
1900 OPTSET2292(IN6P_PKTINFO);
1902 case IPV6_2292HOPLIMIT:
1903 OPTSET2292(IN6P_HOPLIMIT);
1905 case IPV6_2292HOPOPTS:
1907 * Check super-user privilege.
1908 * See comments for IPV6_RECVHOPOPTS.
1911 error = priv_check(td,
1912 PRIV_NETINET_SETHDROPTS);
1916 OPTSET2292(IN6P_HOPOPTS);
1918 case IPV6_2292DSTOPTS:
1920 error = priv_check(td,
1921 PRIV_NETINET_SETHDROPTS);
1925 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1927 case IPV6_2292RTHDR:
1928 OPTSET2292(IN6P_RTHDR);
1936 case IPV6_RTHDRDSTOPTS:
1939 /* new advanced API (RFC3542) */
1941 u_char optbuf_storage[MCLBYTES];
1943 struct ip6_pktopts **optp;
1945 /* cannot mix with RFC2292 */
1946 if (OPTBIT(IN6P_RFC2292)) {
1952 * We only ensure valsize is not too large
1953 * here. Further validation will be done
1956 error = sooptcopyin(sopt, optbuf_storage,
1957 sizeof(optbuf_storage), 0);
1960 optlen = sopt->sopt_valsize;
1961 optbuf = optbuf_storage;
1963 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1965 return (ECONNRESET);
1967 optp = &inp->in6p_outputopts;
1968 error = ip6_pcbopt(optname, optbuf, optlen,
1969 optp, (td != NULL) ? td->td_ucred : NULL,
1976 case IPV6_MULTICAST_IF:
1977 case IPV6_MULTICAST_HOPS:
1978 case IPV6_MULTICAST_LOOP:
1979 case IPV6_JOIN_GROUP:
1980 case IPV6_LEAVE_GROUP:
1982 case MCAST_BLOCK_SOURCE:
1983 case MCAST_UNBLOCK_SOURCE:
1984 case MCAST_JOIN_GROUP:
1985 case MCAST_LEAVE_GROUP:
1986 case MCAST_JOIN_SOURCE_GROUP:
1987 case MCAST_LEAVE_SOURCE_GROUP:
1988 error = ip6_setmoptions(inp, sopt);
1991 case IPV6_PORTRANGE:
1992 error = sooptcopyin(sopt, &optval,
1993 sizeof optval, sizeof optval);
1999 case IPV6_PORTRANGE_DEFAULT:
2000 inp->inp_flags &= ~(INP_LOWPORT);
2001 inp->inp_flags &= ~(INP_HIGHPORT);
2004 case IPV6_PORTRANGE_HIGH:
2005 inp->inp_flags &= ~(INP_LOWPORT);
2006 inp->inp_flags |= INP_HIGHPORT;
2009 case IPV6_PORTRANGE_LOW:
2010 inp->inp_flags &= ~(INP_HIGHPORT);
2011 inp->inp_flags |= INP_LOWPORT;
2021 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2022 case IPV6_IPSEC_POLICY:
2023 if (IPSEC_ENABLED(ipv6)) {
2024 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2031 error = ENOPROTOOPT;
2039 case IPV6_2292PKTOPTIONS:
2040 #ifdef IPV6_PKTOPTIONS
2041 case IPV6_PKTOPTIONS:
2044 * RFC3542 (effectively) deprecated the
2045 * semantics of the 2292-style pktoptions.
2046 * Since it was not reliable in nature (i.e.,
2047 * applications had to expect the lack of some
2048 * information after all), it would make sense
2049 * to simplify this part by always returning
2052 sopt->sopt_valsize = 0;
2055 case IPV6_RECVHOPOPTS:
2056 case IPV6_RECVDSTOPTS:
2057 case IPV6_RECVRTHDRDSTOPTS:
2058 case IPV6_UNICAST_HOPS:
2059 case IPV6_RECVPKTINFO:
2060 case IPV6_RECVHOPLIMIT:
2061 case IPV6_RECVRTHDR:
2062 case IPV6_RECVPATHMTU:
2065 case IPV6_PORTRANGE:
2066 case IPV6_RECVTCLASS:
2067 case IPV6_AUTOFLOWLABEL:
2071 case IPV6_RECVFLOWID:
2073 case IPV6_RSSBUCKETID:
2074 case IPV6_RECVRSSBUCKETID:
2076 case IPV6_BINDMULTI:
2079 case IPV6_RECVHOPOPTS:
2080 optval = OPTBIT(IN6P_HOPOPTS);
2083 case IPV6_RECVDSTOPTS:
2084 optval = OPTBIT(IN6P_DSTOPTS);
2087 case IPV6_RECVRTHDRDSTOPTS:
2088 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2091 case IPV6_UNICAST_HOPS:
2092 optval = inp->in6p_hops;
2095 case IPV6_RECVPKTINFO:
2096 optval = OPTBIT(IN6P_PKTINFO);
2099 case IPV6_RECVHOPLIMIT:
2100 optval = OPTBIT(IN6P_HOPLIMIT);
2103 case IPV6_RECVRTHDR:
2104 optval = OPTBIT(IN6P_RTHDR);
2107 case IPV6_RECVPATHMTU:
2108 optval = OPTBIT(IN6P_MTU);
2112 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2115 case IPV6_PORTRANGE:
2118 flags = inp->inp_flags;
2119 if (flags & INP_HIGHPORT)
2120 optval = IPV6_PORTRANGE_HIGH;
2121 else if (flags & INP_LOWPORT)
2122 optval = IPV6_PORTRANGE_LOW;
2127 case IPV6_RECVTCLASS:
2128 optval = OPTBIT(IN6P_TCLASS);
2131 case IPV6_AUTOFLOWLABEL:
2132 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2135 case IPV6_ORIGDSTADDR:
2136 optval = OPTBIT2(INP_ORIGDSTADDR);
2140 optval = OPTBIT(INP_BINDANY);
2144 optval = inp->inp_flowid;
2148 optval = inp->inp_flowtype;
2151 case IPV6_RECVFLOWID:
2152 optval = OPTBIT2(INP_RECVFLOWID);
2155 case IPV6_RSSBUCKETID:
2157 rss_hash2bucket(inp->inp_flowid,
2161 optval = rss_bucket;
2166 case IPV6_RECVRSSBUCKETID:
2167 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2171 case IPV6_BINDMULTI:
2172 optval = OPTBIT2(INP_BINDMULTI);
2178 error = sooptcopyout(sopt, &optval,
2185 struct ip6_mtuinfo mtuinfo;
2186 struct in6_addr addr;
2188 if (!(so->so_state & SS_ISCONNECTED))
2191 * XXX: we dot not consider the case of source
2192 * routing, or optional information to specify
2193 * the outgoing interface.
2194 * Copy faddr out of inp to avoid holding lock
2195 * on inp during route lookup.
2198 bcopy(&inp->in6p_faddr, &addr, sizeof(addr));
2200 error = ip6_getpmtu_ctl(so->so_fibnum,
2204 if (pmtu > IPV6_MAXPACKET)
2205 pmtu = IPV6_MAXPACKET;
2207 bzero(&mtuinfo, sizeof(mtuinfo));
2208 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2209 optdata = (void *)&mtuinfo;
2210 optdatalen = sizeof(mtuinfo);
2211 error = sooptcopyout(sopt, optdata,
2216 case IPV6_2292PKTINFO:
2217 case IPV6_2292HOPLIMIT:
2218 case IPV6_2292HOPOPTS:
2219 case IPV6_2292RTHDR:
2220 case IPV6_2292DSTOPTS:
2222 case IPV6_2292PKTINFO:
2223 optval = OPTBIT(IN6P_PKTINFO);
2225 case IPV6_2292HOPLIMIT:
2226 optval = OPTBIT(IN6P_HOPLIMIT);
2228 case IPV6_2292HOPOPTS:
2229 optval = OPTBIT(IN6P_HOPOPTS);
2231 case IPV6_2292RTHDR:
2232 optval = OPTBIT(IN6P_RTHDR);
2234 case IPV6_2292DSTOPTS:
2235 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2238 error = sooptcopyout(sopt, &optval,
2245 case IPV6_RTHDRDSTOPTS:
2249 case IPV6_USE_MIN_MTU:
2250 case IPV6_PREFER_TEMPADDR:
2251 error = ip6_getpcbopt(inp, optname, sopt);
2254 case IPV6_MULTICAST_IF:
2255 case IPV6_MULTICAST_HOPS:
2256 case IPV6_MULTICAST_LOOP:
2258 error = ip6_getmoptions(inp, sopt);
2261 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2262 case IPV6_IPSEC_POLICY:
2263 if (IPSEC_ENABLED(ipv6)) {
2264 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2270 error = ENOPROTOOPT;
2280 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2282 int error = 0, optval, optlen;
2283 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2284 struct inpcb *inp = sotoinpcb(so);
2285 int level, op, optname;
2287 level = sopt->sopt_level;
2288 op = sopt->sopt_dir;
2289 optname = sopt->sopt_name;
2290 optlen = sopt->sopt_valsize;
2292 if (level != IPPROTO_IPV6) {
2299 * For ICMPv6 sockets, no modification allowed for checksum
2300 * offset, permit "no change" values to help existing apps.
2302 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2303 * for an ICMPv6 socket will fail."
2304 * The current behavior does not meet RFC3542.
2308 if (optlen != sizeof(int)) {
2312 error = sooptcopyin(sopt, &optval, sizeof(optval),
2316 if (optval < -1 || (optval % 2) != 0) {
2318 * The API assumes non-negative even offset
2319 * values or -1 as a special value.
2322 } else if (so->so_proto->pr_protocol ==
2324 if (optval != icmp6off)
2327 inp->in6p_cksum = optval;
2331 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2334 optval = inp->in6p_cksum;
2336 error = sooptcopyout(sopt, &optval, sizeof(optval));
2346 error = ENOPROTOOPT;
2354 * Set up IP6 options in pcb for insertion in output packets or
2355 * specifying behavior of outgoing packets.
2358 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2359 struct socket *so, struct sockopt *sopt)
2361 struct ip6_pktopts *opt = *pktopt;
2363 struct thread *td = sopt->sopt_td;
2365 /* turn off any old options. */
2368 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2369 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2370 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2371 printf("ip6_pcbopts: all specified options are cleared.\n");
2373 ip6_clearpktopts(opt, -1);
2375 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2378 if (!m || m->m_len == 0) {
2380 * Only turning off any previous options, regardless of
2381 * whether the opt is just created or given.
2383 free(opt, M_IP6OPT);
2387 /* set options specified by user. */
2388 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2389 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2390 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2391 free(opt, M_IP6OPT);
2399 * initialize ip6_pktopts. beware that there are non-zero default values in
2403 ip6_initpktopts(struct ip6_pktopts *opt)
2406 bzero(opt, sizeof(*opt));
2407 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2408 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2409 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2410 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2414 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2415 struct ucred *cred, int uproto)
2417 struct ip6_pktopts *opt;
2419 if (*pktopt == NULL) {
2420 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2422 if (*pktopt == NULL)
2424 ip6_initpktopts(*pktopt);
2428 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2431 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2432 if (pktopt && pktopt->field) { \
2434 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2435 malloc_optdata = true; \
2437 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
2439 free(optdata, M_TEMP); \
2440 return (ECONNRESET); \
2442 pktopt = inp->in6p_outputopts; \
2443 if (pktopt && pktopt->field) { \
2444 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2445 bcopy(&pktopt->field, optdata, optdatalen); \
2447 free(optdata, M_TEMP); \
2449 malloc_optdata = false; \
2454 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2455 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2457 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2458 pktopt->field->sa_len)
2461 ip6_getpcbopt(struct inpcb *inp, int optname, struct sockopt *sopt)
2463 void *optdata = NULL;
2464 bool malloc_optdata = false;
2467 struct in6_pktinfo null_pktinfo;
2468 int deftclass = 0, on;
2469 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2470 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2471 struct ip6_pktopts *pktopt;
2474 pktopt = inp->in6p_outputopts;
2478 optdata = (void *)&null_pktinfo;
2479 if (pktopt && pktopt->ip6po_pktinfo) {
2480 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2481 sizeof(null_pktinfo));
2482 in6_clearscope(&null_pktinfo.ipi6_addr);
2484 /* XXX: we don't have to do this every time... */
2485 bzero(&null_pktinfo, sizeof(null_pktinfo));
2487 optdatalen = sizeof(struct in6_pktinfo);
2490 if (pktopt && pktopt->ip6po_tclass >= 0)
2491 deftclass = pktopt->ip6po_tclass;
2492 optdata = (void *)&deftclass;
2493 optdatalen = sizeof(int);
2496 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2499 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2501 case IPV6_RTHDRDSTOPTS:
2502 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2505 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2508 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2510 case IPV6_USE_MIN_MTU:
2512 defminmtu = pktopt->ip6po_minmtu;
2513 optdata = (void *)&defminmtu;
2514 optdatalen = sizeof(int);
2517 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2521 optdata = (void *)&on;
2522 optdatalen = sizeof(on);
2524 case IPV6_PREFER_TEMPADDR:
2526 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2527 optdata = (void *)&defpreftemp;
2528 optdatalen = sizeof(int);
2530 default: /* should not happen */
2532 panic("ip6_getpcbopt: unexpected option\n");
2535 return (ENOPROTOOPT);
2539 error = sooptcopyout(sopt, optdata, optdatalen);
2541 free(optdata, M_TEMP);
2547 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2552 if (optname == -1 || optname == IPV6_PKTINFO) {
2553 if (pktopt->ip6po_pktinfo)
2554 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2555 pktopt->ip6po_pktinfo = NULL;
2557 if (optname == -1 || optname == IPV6_HOPLIMIT)
2558 pktopt->ip6po_hlim = -1;
2559 if (optname == -1 || optname == IPV6_TCLASS)
2560 pktopt->ip6po_tclass = -1;
2561 if (optname == -1 || optname == IPV6_NEXTHOP) {
2562 if (pktopt->ip6po_nextroute.ro_rt) {
2563 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2564 pktopt->ip6po_nextroute.ro_rt = NULL;
2566 if (pktopt->ip6po_nexthop)
2567 free(pktopt->ip6po_nexthop, M_IP6OPT);
2568 pktopt->ip6po_nexthop = NULL;
2570 if (optname == -1 || optname == IPV6_HOPOPTS) {
2571 if (pktopt->ip6po_hbh)
2572 free(pktopt->ip6po_hbh, M_IP6OPT);
2573 pktopt->ip6po_hbh = NULL;
2575 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2576 if (pktopt->ip6po_dest1)
2577 free(pktopt->ip6po_dest1, M_IP6OPT);
2578 pktopt->ip6po_dest1 = NULL;
2580 if (optname == -1 || optname == IPV6_RTHDR) {
2581 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2582 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2583 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2584 if (pktopt->ip6po_route.ro_rt) {
2585 RTFREE(pktopt->ip6po_route.ro_rt);
2586 pktopt->ip6po_route.ro_rt = NULL;
2589 if (optname == -1 || optname == IPV6_DSTOPTS) {
2590 if (pktopt->ip6po_dest2)
2591 free(pktopt->ip6po_dest2, M_IP6OPT);
2592 pktopt->ip6po_dest2 = NULL;
2596 #define PKTOPT_EXTHDRCPY(type) \
2599 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2600 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2601 if (dst->type == NULL)\
2603 bcopy(src->type, dst->type, hlen);\
2605 } while (/*CONSTCOND*/ 0)
2608 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2610 if (dst == NULL || src == NULL) {
2611 printf("ip6_clearpktopts: invalid argument\n");
2615 dst->ip6po_hlim = src->ip6po_hlim;
2616 dst->ip6po_tclass = src->ip6po_tclass;
2617 dst->ip6po_flags = src->ip6po_flags;
2618 dst->ip6po_minmtu = src->ip6po_minmtu;
2619 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2620 if (src->ip6po_pktinfo) {
2621 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2623 if (dst->ip6po_pktinfo == NULL)
2625 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2627 if (src->ip6po_nexthop) {
2628 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2630 if (dst->ip6po_nexthop == NULL)
2632 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2633 src->ip6po_nexthop->sa_len);
2635 PKTOPT_EXTHDRCPY(ip6po_hbh);
2636 PKTOPT_EXTHDRCPY(ip6po_dest1);
2637 PKTOPT_EXTHDRCPY(ip6po_dest2);
2638 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2642 ip6_clearpktopts(dst, -1);
2645 #undef PKTOPT_EXTHDRCPY
2647 struct ip6_pktopts *
2648 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2651 struct ip6_pktopts *dst;
2653 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2656 ip6_initpktopts(dst);
2658 if ((error = copypktopts(dst, src, canwait)) != 0) {
2659 free(dst, M_IP6OPT);
2667 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2672 ip6_clearpktopts(pktopt, -1);
2674 free(pktopt, M_IP6OPT);
2678 * Set IPv6 outgoing packet options based on advanced API.
2681 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2682 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2684 struct cmsghdr *cm = NULL;
2686 if (control == NULL || opt == NULL)
2689 ip6_initpktopts(opt);
2694 * If stickyopt is provided, make a local copy of the options
2695 * for this particular packet, then override them by ancillary
2697 * XXX: copypktopts() does not copy the cached route to a next
2698 * hop (if any). This is not very good in terms of efficiency,
2699 * but we can allow this since this option should be rarely
2702 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2707 * XXX: Currently, we assume all the optional information is stored
2710 if (control->m_next)
2713 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2714 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2717 if (control->m_len < CMSG_LEN(0))
2720 cm = mtod(control, struct cmsghdr *);
2721 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2723 if (cm->cmsg_level != IPPROTO_IPV6)
2726 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2727 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2736 * Set a particular packet option, as a sticky option or an ancillary data
2737 * item. "len" can be 0 only when it's a sticky option.
2738 * We have 4 cases of combination of "sticky" and "cmsg":
2739 * "sticky=0, cmsg=0": impossible
2740 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2741 * "sticky=1, cmsg=0": RFC3542 socket option
2742 * "sticky=1, cmsg=1": RFC2292 socket option
2745 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2746 struct ucred *cred, int sticky, int cmsg, int uproto)
2748 int minmtupolicy, preftemp;
2751 if (!sticky && !cmsg) {
2753 printf("ip6_setpktopt: impossible case\n");
2759 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2760 * not be specified in the context of RFC3542. Conversely,
2761 * RFC3542 types should not be specified in the context of RFC2292.
2765 case IPV6_2292PKTINFO:
2766 case IPV6_2292HOPLIMIT:
2767 case IPV6_2292NEXTHOP:
2768 case IPV6_2292HOPOPTS:
2769 case IPV6_2292DSTOPTS:
2770 case IPV6_2292RTHDR:
2771 case IPV6_2292PKTOPTIONS:
2772 return (ENOPROTOOPT);
2775 if (sticky && cmsg) {
2782 case IPV6_RTHDRDSTOPTS:
2784 case IPV6_USE_MIN_MTU:
2787 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2788 return (ENOPROTOOPT);
2793 case IPV6_2292PKTINFO:
2796 struct ifnet *ifp = NULL;
2797 struct in6_pktinfo *pktinfo;
2799 if (len != sizeof(struct in6_pktinfo))
2802 pktinfo = (struct in6_pktinfo *)buf;
2805 * An application can clear any sticky IPV6_PKTINFO option by
2806 * doing a "regular" setsockopt with ipi6_addr being
2807 * in6addr_any and ipi6_ifindex being zero.
2808 * [RFC 3542, Section 6]
2810 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2811 pktinfo->ipi6_ifindex == 0 &&
2812 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2813 ip6_clearpktopts(opt, optname);
2817 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2818 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2821 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2823 /* validate the interface index if specified. */
2824 if (pktinfo->ipi6_ifindex > V_if_index)
2826 if (pktinfo->ipi6_ifindex) {
2827 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2831 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2832 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2836 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2837 struct in6_ifaddr *ia;
2839 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2840 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2842 return (EADDRNOTAVAIL);
2843 ifa_free(&ia->ia_ifa);
2846 * We store the address anyway, and let in6_selectsrc()
2847 * validate the specified address. This is because ipi6_addr
2848 * may not have enough information about its scope zone, and
2849 * we may need additional information (such as outgoing
2850 * interface or the scope zone of a destination address) to
2851 * disambiguate the scope.
2852 * XXX: the delay of the validation may confuse the
2853 * application when it is used as a sticky option.
2855 if (opt->ip6po_pktinfo == NULL) {
2856 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2857 M_IP6OPT, M_NOWAIT);
2858 if (opt->ip6po_pktinfo == NULL)
2861 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2865 case IPV6_2292HOPLIMIT:
2871 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2872 * to simplify the ordering among hoplimit options.
2874 if (optname == IPV6_HOPLIMIT && sticky)
2875 return (ENOPROTOOPT);
2877 if (len != sizeof(int))
2880 if (*hlimp < -1 || *hlimp > 255)
2883 opt->ip6po_hlim = *hlimp;
2891 if (len != sizeof(int))
2893 tclass = *(int *)buf;
2894 if (tclass < -1 || tclass > 255)
2897 opt->ip6po_tclass = tclass;
2901 case IPV6_2292NEXTHOP:
2904 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
2909 if (len == 0) { /* just remove the option */
2910 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2914 /* check if cmsg_len is large enough for sa_len */
2915 if (len < sizeof(struct sockaddr) || len < *buf)
2918 switch (((struct sockaddr *)buf)->sa_family) {
2921 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2924 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2927 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2928 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2931 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2937 case AF_LINK: /* should eventually be supported */
2939 return (EAFNOSUPPORT);
2942 /* turn off the previous option, then set the new option. */
2943 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2944 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2945 if (opt->ip6po_nexthop == NULL)
2947 bcopy(buf, opt->ip6po_nexthop, *buf);
2950 case IPV6_2292HOPOPTS:
2953 struct ip6_hbh *hbh;
2957 * XXX: We don't allow a non-privileged user to set ANY HbH
2958 * options, since per-option restriction has too much
2962 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
2968 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2969 break; /* just remove the option */
2972 /* message length validation */
2973 if (len < sizeof(struct ip6_hbh))
2975 hbh = (struct ip6_hbh *)buf;
2976 hbhlen = (hbh->ip6h_len + 1) << 3;
2980 /* turn off the previous option, then set the new option. */
2981 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2982 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2983 if (opt->ip6po_hbh == NULL)
2985 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2990 case IPV6_2292DSTOPTS:
2992 case IPV6_RTHDRDSTOPTS:
2994 struct ip6_dest *dest, **newdest = NULL;
2997 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2998 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3004 ip6_clearpktopts(opt, optname);
3005 break; /* just remove the option */
3008 /* message length validation */
3009 if (len < sizeof(struct ip6_dest))
3011 dest = (struct ip6_dest *)buf;
3012 destlen = (dest->ip6d_len + 1) << 3;
3017 * Determine the position that the destination options header
3018 * should be inserted; before or after the routing header.
3021 case IPV6_2292DSTOPTS:
3023 * The old advacned API is ambiguous on this point.
3024 * Our approach is to determine the position based
3025 * according to the existence of a routing header.
3026 * Note, however, that this depends on the order of the
3027 * extension headers in the ancillary data; the 1st
3028 * part of the destination options header must appear
3029 * before the routing header in the ancillary data,
3031 * RFC3542 solved the ambiguity by introducing
3032 * separate ancillary data or option types.
3034 if (opt->ip6po_rthdr == NULL)
3035 newdest = &opt->ip6po_dest1;
3037 newdest = &opt->ip6po_dest2;
3039 case IPV6_RTHDRDSTOPTS:
3040 newdest = &opt->ip6po_dest1;
3043 newdest = &opt->ip6po_dest2;
3047 /* turn off the previous option, then set the new option. */
3048 ip6_clearpktopts(opt, optname);
3049 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3050 if (*newdest == NULL)
3052 bcopy(dest, *newdest, destlen);
3057 case IPV6_2292RTHDR:
3060 struct ip6_rthdr *rth;
3064 ip6_clearpktopts(opt, IPV6_RTHDR);
3065 break; /* just remove the option */
3068 /* message length validation */
3069 if (len < sizeof(struct ip6_rthdr))
3071 rth = (struct ip6_rthdr *)buf;
3072 rthlen = (rth->ip6r_len + 1) << 3;
3076 switch (rth->ip6r_type) {
3077 case IPV6_RTHDR_TYPE_0:
3078 if (rth->ip6r_len == 0) /* must contain one addr */
3080 if (rth->ip6r_len % 2) /* length must be even */
3082 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3086 return (EINVAL); /* not supported */
3089 /* turn off the previous option */
3090 ip6_clearpktopts(opt, IPV6_RTHDR);
3091 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3092 if (opt->ip6po_rthdr == NULL)
3094 bcopy(rth, opt->ip6po_rthdr, rthlen);
3099 case IPV6_USE_MIN_MTU:
3100 if (len != sizeof(int))
3102 minmtupolicy = *(int *)buf;
3103 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3104 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3105 minmtupolicy != IP6PO_MINMTU_ALL) {
3108 opt->ip6po_minmtu = minmtupolicy;
3112 if (len != sizeof(int))
3115 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3117 * we ignore this option for TCP sockets.
3118 * (RFC3542 leaves this case unspecified.)
3120 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3122 opt->ip6po_flags |= IP6PO_DONTFRAG;
3125 case IPV6_PREFER_TEMPADDR:
3126 if (len != sizeof(int))
3128 preftemp = *(int *)buf;
3129 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3130 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3131 preftemp != IP6PO_TEMPADDR_PREFER) {
3134 opt->ip6po_prefer_tempaddr = preftemp;
3138 return (ENOPROTOOPT);
3139 } /* end of switch */
3145 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3146 * packet to the input queue of a specified interface. Note that this
3147 * calls the output routine of the loopback "driver", but with an interface
3148 * pointer that might NOT be &loif -- easier than replicating that code here.
3151 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3154 struct ip6_hdr *ip6;
3156 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3161 * Make sure to deep-copy IPv6 header portion in case the data
3162 * is in an mbuf cluster, so that we can safely override the IPv6
3163 * header portion later.
3165 if (!M_WRITABLE(copym) ||
3166 copym->m_len < sizeof(struct ip6_hdr)) {
3167 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3171 ip6 = mtod(copym, struct ip6_hdr *);
3173 * clear embedded scope identifiers if necessary.
3174 * in6_clearscope will touch the addresses only when necessary.
3176 in6_clearscope(&ip6->ip6_src);
3177 in6_clearscope(&ip6->ip6_dst);
3178 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3179 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3181 copym->m_pkthdr.csum_data = 0xffff;
3183 if_simloop(ifp, copym, AF_INET6, 0);
3187 * Chop IPv6 header off from the payload.
3190 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3193 struct ip6_hdr *ip6;
3195 ip6 = mtod(m, struct ip6_hdr *);
3196 if (m->m_len > sizeof(*ip6)) {
3197 mh = m_gethdr(M_NOWAIT, MT_DATA);
3202 m_move_pkthdr(mh, m);
3203 M_ALIGN(mh, sizeof(*ip6));
3204 m->m_len -= sizeof(*ip6);
3205 m->m_data += sizeof(*ip6);
3208 m->m_len = sizeof(*ip6);
3209 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3211 exthdrs->ip6e_ip6 = m;
3216 * Compute IPv6 extension header length.
3219 ip6_optlen(struct inpcb *inp)
3223 if (!inp->in6p_outputopts)
3228 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3230 len += elen(inp->in6p_outputopts->ip6po_hbh);
3231 if (inp->in6p_outputopts->ip6po_rthdr)
3232 /* dest1 is valid with rthdr only */
3233 len += elen(inp->in6p_outputopts->ip6po_dest1);
3234 len += elen(inp->in6p_outputopts->ip6po_rthdr);
3235 len += elen(inp->in6p_outputopts->ip6po_dest2);