2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
69 #include "opt_inet6.h"
70 #include "opt_ratelimit.h"
71 #include "opt_ipsec.h"
73 #include "opt_route.h"
76 #include <sys/param.h>
77 #include <sys/kernel.h>
78 #include <sys/malloc.h>
80 #include <sys/errno.h>
83 #include <sys/protosw.h>
84 #include <sys/socket.h>
85 #include <sys/socketvar.h>
86 #include <sys/syslog.h>
87 #include <sys/ucred.h>
89 #include <machine/in_cksum.h>
92 #include <net/if_var.h>
93 #include <net/if_llatbl.h>
94 #include <net/netisr.h>
95 #include <net/route.h>
97 #include <net/rss_config.h>
100 #include <netinet/in.h>
101 #include <netinet/in_var.h>
102 #include <netinet/ip_var.h>
103 #include <netinet6/in6_fib.h>
104 #include <netinet6/in6_var.h>
105 #include <netinet/ip6.h>
106 #include <netinet/icmp6.h>
107 #include <netinet6/ip6_var.h>
108 #include <netinet/in_pcb.h>
109 #include <netinet/tcp_var.h>
110 #include <netinet6/nd6.h>
111 #include <netinet6/in6_rss.h>
113 #include <netipsec/ipsec_support.h>
115 #include <netinet/sctp.h>
116 #include <netinet/sctp_crc32.h>
119 #include <netinet6/ip6protosw.h>
120 #include <netinet6/scope6_var.h>
122 extern int in6_mcast_loop;
125 struct mbuf *ip6e_ip6;
126 struct mbuf *ip6e_hbh;
127 struct mbuf *ip6e_dest1;
128 struct mbuf *ip6e_rthdr;
129 struct mbuf *ip6e_dest2;
132 static MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
134 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
135 struct ucred *, int);
136 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
137 struct socket *, struct sockopt *);
138 static int ip6_getpcbopt(struct inpcb *, int, struct sockopt *);
139 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
140 struct ucred *, int, int, int);
142 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
143 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
145 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
146 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
147 static int ip6_getpmtu(struct route_in6 *, int,
148 struct ifnet *, const struct in6_addr *, u_long *, int *, u_int,
150 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
151 u_long *, int *, u_int);
152 static int ip6_getpmtu_ctl(u_int, const struct in6_addr *, u_long *);
153 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
157 * Make an extension header from option data. hp is the source, and
158 * mp is the destination.
160 #define MAKE_EXTHDR(hp, mp) \
163 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
164 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
165 ((eh)->ip6e_len + 1) << 3); \
169 } while (/*CONSTCOND*/ 0)
172 * Form a chain of extension headers.
173 * m is the extension header mbuf
174 * mp is the previous mbuf in the chain
175 * p is the next header
176 * i is the type of option.
178 #define MAKE_CHAIN(m, mp, p, i)\
182 panic("assumption failed: hdr not split"); \
183 *mtod((m), u_char *) = *(p);\
185 p = mtod((m), u_char *);\
186 (m)->m_next = (mp)->m_next;\
190 } while (/*CONSTCOND*/ 0)
193 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
197 csum = in_cksum_skip(m, offset + plen, offset);
198 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
200 offset += m->m_pkthdr.csum_data; /* checksum offset */
202 if (offset + sizeof(csum) > m->m_len)
203 m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
205 *(u_short *)mtodo(m, offset) = csum;
209 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
210 int fraglen , uint32_t id)
212 struct mbuf *m, **mnext, *m_frgpart;
213 struct ip6_hdr *ip6, *mhip6;
214 struct ip6_frag *ip6f;
217 int tlen = m0->m_pkthdr.len;
219 KASSERT((fraglen % 8 == 0), ("Fragment length must be a multiple of 8"));
222 ip6 = mtod(m, struct ip6_hdr *);
223 mnext = &m->m_nextpkt;
225 for (off = hlen; off < tlen; off += fraglen) {
226 m = m_gethdr(M_NOWAIT, MT_DATA);
228 IP6STAT_INC(ip6s_odropped);
233 * Make sure the complete packet header gets copied
234 * from the originating mbuf to the newly created
235 * mbuf. This also ensures that existing firewall
236 * classification(s), VLAN tags and so on get copied
237 * to the resulting fragmented packet(s):
239 if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
241 IP6STAT_INC(ip6s_odropped);
246 mnext = &m->m_nextpkt;
247 m->m_data += max_linkhdr;
248 mhip6 = mtod(m, struct ip6_hdr *);
250 m->m_len = sizeof(*mhip6);
251 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
253 IP6STAT_INC(ip6s_odropped);
256 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
257 if (off + fraglen >= tlen)
258 fraglen = tlen - off;
260 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
261 mhip6->ip6_plen = htons((u_short)(fraglen + hlen +
262 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
263 if ((m_frgpart = m_copym(m0, off, fraglen, M_NOWAIT)) == NULL) {
264 IP6STAT_INC(ip6s_odropped);
268 m->m_pkthdr.len = fraglen + hlen + sizeof(*ip6f);
269 ip6f->ip6f_reserved = 0;
270 ip6f->ip6f_ident = id;
271 ip6f->ip6f_nxt = nextproto;
272 IP6STAT_INC(ip6s_ofragments);
273 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
280 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
281 * header (with pri, len, nxt, hlim, src, dst).
282 * This function may modify ver and hlim only.
283 * The mbuf chain containing the packet will be freed.
284 * The mbuf opt, if present, will not be freed.
285 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
286 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
287 * then result of route lookup is stored in ro->ro_rt.
289 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
290 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
293 * ifpp - XXX: just for statistics
296 * XXX TODO: no flowid is assigned for outbound flows?
299 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
300 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
301 struct ifnet **ifpp, struct inpcb *inp)
304 struct ifnet *ifp, *origifp;
306 struct mbuf *mprev = NULL;
308 struct route_in6 ip6route;
309 struct rtentry *rt = NULL;
310 struct sockaddr_in6 *dst, src_sa, dst_sa;
311 struct in6_addr odst;
313 struct in6_ifaddr *ia = NULL;
315 int alwaysfrag, dontfrag;
316 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
317 struct ip6_exthdrs exthdrs;
318 struct in6_addr src0, dst0;
320 struct route_in6 *ro_pmtu = NULL;
325 struct m_tag *fwd_tag = NULL;
329 INP_LOCK_ASSERT(inp);
330 M_SETFIB(m, inp->inp_inc.inc_fibnum);
331 if ((flags & IP_NODEFAULTFLOWID) == 0) {
332 /* unconditionally set flowid */
333 m->m_pkthdr.flowid = inp->inp_flowid;
334 M_HASHTYPE_SET(m, inp->inp_flowtype);
338 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
340 * IPSec checking which handles several cases.
341 * FAST IPSEC: We re-injected the packet.
342 * XXX: need scope argument.
344 if (IPSEC_ENABLED(ipv6)) {
345 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
346 if (error == EINPROGRESS)
353 bzero(&exthdrs, sizeof(exthdrs));
355 /* Hop-by-Hop options header */
356 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
357 /* Destination options header(1st part) */
358 if (opt->ip6po_rthdr) {
360 * Destination options header(1st part)
361 * This only makes sense with a routing header.
362 * See Section 9.2 of RFC 3542.
363 * Disabling this part just for MIP6 convenience is
364 * a bad idea. We need to think carefully about a
365 * way to make the advanced API coexist with MIP6
366 * options, which might automatically be inserted in
369 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
372 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
373 /* Destination options header(2nd part) */
374 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
378 * Calculate the total length of the extension header chain.
379 * Keep the length of the unfragmentable part for fragmentation.
382 if (exthdrs.ip6e_hbh)
383 optlen += exthdrs.ip6e_hbh->m_len;
384 if (exthdrs.ip6e_dest1)
385 optlen += exthdrs.ip6e_dest1->m_len;
386 if (exthdrs.ip6e_rthdr)
387 optlen += exthdrs.ip6e_rthdr->m_len;
388 unfragpartlen = optlen + sizeof(struct ip6_hdr);
390 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
391 if (exthdrs.ip6e_dest2)
392 optlen += exthdrs.ip6e_dest2->m_len;
395 * If there is at least one extension header,
396 * separate IP6 header from the payload.
398 if (optlen && !hdrsplit) {
399 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
403 m = exthdrs.ip6e_ip6;
407 ip6 = mtod(m, struct ip6_hdr *);
409 /* adjust mbuf packet header length */
410 m->m_pkthdr.len += optlen;
411 plen = m->m_pkthdr.len - sizeof(*ip6);
413 /* If this is a jumbo payload, insert a jumbo payload option. */
414 if (plen > IPV6_MAXPACKET) {
416 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
420 m = exthdrs.ip6e_ip6;
424 ip6 = mtod(m, struct ip6_hdr *);
425 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
429 ip6->ip6_plen = htons(plen);
432 * Concatenate headers and fill in next header fields.
433 * Here we have, on "m"
435 * and we insert headers accordingly. Finally, we should be getting:
436 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
438 * during the header composing process, "m" points to IPv6 header.
439 * "mprev" points to an extension header prior to esp.
441 u_char *nexthdrp = &ip6->ip6_nxt;
445 * we treat dest2 specially. this makes IPsec processing
446 * much easier. the goal here is to make mprev point the
447 * mbuf prior to dest2.
449 * result: IPv6 dest2 payload
450 * m and mprev will point to IPv6 header.
452 if (exthdrs.ip6e_dest2) {
454 panic("assumption failed: hdr not split");
455 exthdrs.ip6e_dest2->m_next = m->m_next;
456 m->m_next = exthdrs.ip6e_dest2;
457 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
458 ip6->ip6_nxt = IPPROTO_DSTOPTS;
462 * result: IPv6 hbh dest1 rthdr dest2 payload
463 * m will point to IPv6 header. mprev will point to the
464 * extension header prior to dest2 (rthdr in the above case).
466 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
467 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
469 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
473 * If there is a routing header, discard the packet.
475 if (exthdrs.ip6e_rthdr) {
480 /* Source address validation */
481 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
482 (flags & IPV6_UNSPECSRC) == 0) {
484 IP6STAT_INC(ip6s_badscope);
487 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
489 IP6STAT_INC(ip6s_badscope);
493 IP6STAT_INC(ip6s_localout);
500 bzero((caddr_t)ro, sizeof(*ro));
503 if (opt && opt->ip6po_rthdr)
504 ro = &opt->ip6po_route;
505 dst = (struct sockaddr_in6 *)&ro->ro_dst;
506 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
509 * if specified, try to fill in the traffic class field.
510 * do not override if a non-zero value is already set.
511 * we check the diffserv field and the ecn field separately.
513 if (opt && opt->ip6po_tclass >= 0) {
516 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
518 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
521 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
524 /* fill in or override the hop limit field, if necessary. */
525 if (opt && opt->ip6po_hlim != -1)
526 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
527 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
529 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
531 ip6->ip6_hlim = V_ip6_defmcasthlim;
534 * Validate route against routing table additions;
535 * a better/more specific route might have been added.
536 * Make sure address family is set in route.
539 ro->ro_dst.sin6_family = AF_INET6;
540 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
542 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
543 ro->ro_dst.sin6_family == AF_INET6 &&
544 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
546 ifp = ro->ro_rt->rt_ifp;
549 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
551 if (fwd_tag == NULL) {
552 bzero(&dst_sa, sizeof(dst_sa));
553 dst_sa.sin6_family = AF_INET6;
554 dst_sa.sin6_len = sizeof(dst_sa);
555 dst_sa.sin6_addr = ip6->ip6_dst;
557 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
561 in6_ifstat_inc(ifp, ifs6_out_discard);
567 * If in6_selectroute() does not return a route entry,
568 * dst may not have been updated.
570 *dst = dst_sa; /* XXX */
574 * then rt (for unicast) and ifp must be non-NULL valid values.
576 if ((flags & IPV6_FORWARDING) == 0) {
577 /* XXX: the FORWARDING flag can be set for mrouting. */
578 in6_ifstat_inc(ifp, ifs6_out_request);
581 ia = (struct in6_ifaddr *)(rt->rt_ifa);
582 counter_u64_add(rt->rt_pksent, 1);
585 /* Setup data structures for scope ID checks. */
587 bzero(&src_sa, sizeof(src_sa));
588 src_sa.sin6_family = AF_INET6;
589 src_sa.sin6_len = sizeof(src_sa);
590 src_sa.sin6_addr = ip6->ip6_src;
593 /* re-initialize to be sure */
594 bzero(&dst_sa, sizeof(dst_sa));
595 dst_sa.sin6_family = AF_INET6;
596 dst_sa.sin6_len = sizeof(dst_sa);
597 dst_sa.sin6_addr = ip6->ip6_dst;
599 /* Check for valid scope ID. */
600 if (in6_setscope(&src0, ifp, &zone) == 0 &&
601 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
602 in6_setscope(&dst0, ifp, &zone) == 0 &&
603 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
605 * The outgoing interface is in the zone of the source
606 * and destination addresses.
608 * Because the loopback interface cannot receive
609 * packets with a different scope ID than its own,
610 * there is a trick is to pretend the outgoing packet
611 * was received by the real network interface, by
612 * setting "origifp" different from "ifp". This is
613 * only allowed when "ifp" is a loopback network
614 * interface. Refer to code in nd6_output_ifp() for
620 * We should use ia_ifp to support the case of sending
621 * packets to an address of our own.
623 if (ia != NULL && ia->ia_ifp)
626 } else if ((ifp->if_flags & IFF_LOOPBACK) == 0 ||
627 sa6_recoverscope(&src_sa) != 0 ||
628 sa6_recoverscope(&dst_sa) != 0 ||
629 dst_sa.sin6_scope_id == 0 ||
630 (src_sa.sin6_scope_id != 0 &&
631 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
632 (origifp = ifnet_byindex(dst_sa.sin6_scope_id)) == NULL) {
634 * If the destination network interface is not a
635 * loopback interface, or the destination network
636 * address has no scope ID, or the source address has
637 * a scope ID set which is different from the
638 * destination address one, or there is no network
639 * interface representing this scope ID, the address
640 * pair is considered invalid.
642 IP6STAT_INC(ip6s_badscope);
643 in6_ifstat_inc(ifp, ifs6_out_discard);
645 error = EHOSTUNREACH; /* XXX */
649 /* All scope ID checks are successful. */
651 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
652 if (opt && opt->ip6po_nextroute.ro_rt) {
654 * The nexthop is explicitly specified by the
655 * application. We assume the next hop is an IPv6
658 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
660 else if ((rt->rt_flags & RTF_GATEWAY))
661 dst = (struct sockaddr_in6 *)rt->rt_gateway;
664 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
665 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
667 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
668 in6_ifstat_inc(ifp, ifs6_out_mcast);
670 * Confirm that the outgoing interface supports multicast.
672 if (!(ifp->if_flags & IFF_MULTICAST)) {
673 IP6STAT_INC(ip6s_noroute);
674 in6_ifstat_inc(ifp, ifs6_out_discard);
678 if ((im6o == NULL && in6_mcast_loop) ||
679 (im6o && im6o->im6o_multicast_loop)) {
681 * Loop back multicast datagram if not expressly
682 * forbidden to do so, even if we have not joined
683 * the address; protocols will filter it later,
684 * thus deferring a hash lookup and lock acquisition
685 * at the expense of an m_copym().
687 ip6_mloopback(ifp, m);
690 * If we are acting as a multicast router, perform
691 * multicast forwarding as if the packet had just
692 * arrived on the interface to which we are about
693 * to send. The multicast forwarding function
694 * recursively calls this function, using the
695 * IPV6_FORWARDING flag to prevent infinite recursion.
697 * Multicasts that are looped back by ip6_mloopback(),
698 * above, will be forwarded by the ip6_input() routine,
701 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
703 * XXX: ip6_mforward expects that rcvif is NULL
704 * when it is called from the originating path.
705 * However, it may not always be the case.
707 m->m_pkthdr.rcvif = NULL;
708 if (ip6_mforward(ip6, ifp, m) != 0) {
715 * Multicasts with a hoplimit of zero may be looped back,
716 * above, but must not be transmitted on a network.
717 * Also, multicasts addressed to the loopback interface
718 * are not sent -- the above call to ip6_mloopback() will
719 * loop back a copy if this host actually belongs to the
720 * destination group on the loopback interface.
722 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
723 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
730 * Fill the outgoing inteface to tell the upper layer
731 * to increment per-interface statistics.
736 /* Determine path MTU. */
737 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
738 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
742 * The caller of this function may specify to use the minimum MTU
744 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
745 * setting. The logic is a bit complicated; by default, unicast
746 * packets will follow path MTU while multicast packets will be sent at
747 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
748 * including unicast ones will be sent at the minimum MTU. Multicast
749 * packets will always be sent at the minimum MTU unless
750 * IP6PO_MINMTU_DISABLE is explicitly specified.
751 * See RFC 3542 for more details.
753 if (mtu > IPV6_MMTU) {
754 if ((flags & IPV6_MINMTU))
756 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
758 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
760 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
766 * clear embedded scope identifiers if necessary.
767 * in6_clearscope will touch the addresses only when necessary.
769 in6_clearscope(&ip6->ip6_src);
770 in6_clearscope(&ip6->ip6_dst);
773 * If the outgoing packet contains a hop-by-hop options header,
774 * it must be examined and processed even by the source node.
775 * (RFC 2460, section 4.)
777 if (exthdrs.ip6e_hbh) {
778 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
779 u_int32_t dummy; /* XXX unused */
780 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
783 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
784 panic("ip6e_hbh is not contiguous");
787 * XXX: if we have to send an ICMPv6 error to the sender,
788 * we need the M_LOOP flag since icmp6_error() expects
789 * the IPv6 and the hop-by-hop options header are
790 * contiguous unless the flag is set.
792 m->m_flags |= M_LOOP;
793 m->m_pkthdr.rcvif = ifp;
794 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
795 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
796 &dummy, &plen) < 0) {
797 /* m was already freed at this point */
798 error = EINVAL;/* better error? */
801 m->m_flags &= ~M_LOOP; /* XXX */
802 m->m_pkthdr.rcvif = NULL;
805 /* Jump over all PFIL processing if hooks are not active. */
806 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
810 /* Run through list of hooks for output packets. */
811 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, 0, inp);
812 if (error != 0 || m == NULL)
815 ip6 = mtod(m, struct ip6_hdr *);
818 /* See if destination IP address was changed by packet filter. */
819 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
820 m->m_flags |= M_SKIP_FIREWALL;
821 /* If destination is now ourself drop to ip6_input(). */
822 if (in6_localip(&ip6->ip6_dst)) {
823 m->m_flags |= M_FASTFWD_OURS;
824 if (m->m_pkthdr.rcvif == NULL)
825 m->m_pkthdr.rcvif = V_loif;
826 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
827 m->m_pkthdr.csum_flags |=
828 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
829 m->m_pkthdr.csum_data = 0xffff;
832 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
833 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
835 error = netisr_queue(NETISR_IPV6, m);
838 RO_INVALIDATE_CACHE(ro);
839 needfiblookup = 1; /* Redo the routing table lookup. */
842 /* See if fib was changed by packet filter. */
843 if (fibnum != M_GETFIB(m)) {
844 m->m_flags |= M_SKIP_FIREWALL;
845 fibnum = M_GETFIB(m);
846 RO_INVALIDATE_CACHE(ro);
852 /* See if local, if yes, send it to netisr. */
853 if (m->m_flags & M_FASTFWD_OURS) {
854 if (m->m_pkthdr.rcvif == NULL)
855 m->m_pkthdr.rcvif = V_loif;
856 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
857 m->m_pkthdr.csum_flags |=
858 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
859 m->m_pkthdr.csum_data = 0xffff;
862 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
863 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
865 error = netisr_queue(NETISR_IPV6, m);
868 /* Or forward to some other address? */
869 if ((m->m_flags & M_IP6_NEXTHOP) &&
870 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
871 dst = (struct sockaddr_in6 *)&ro->ro_dst;
872 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
873 m->m_flags |= M_SKIP_FIREWALL;
874 m->m_flags &= ~M_IP6_NEXTHOP;
875 m_tag_delete(m, fwd_tag);
881 * Send the packet to the outgoing interface.
882 * If necessary, do IPv6 fragmentation before sending.
884 * the logic here is rather complex:
885 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
886 * 1-a: send as is if tlen <= path mtu
887 * 1-b: fragment if tlen > path mtu
889 * 2: if user asks us not to fragment (dontfrag == 1)
890 * 2-a: send as is if tlen <= interface mtu
891 * 2-b: error if tlen > interface mtu
893 * 3: if we always need to attach fragment header (alwaysfrag == 1)
896 * 4: if dontfrag == 1 && alwaysfrag == 1
897 * error, as we cannot handle this conflicting request
899 sw_csum = m->m_pkthdr.csum_flags;
901 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
902 sw_csum &= ~ifp->if_hwassist;
906 * If we added extension headers, we will not do TSO and calculate the
907 * checksums ourselves for now.
908 * XXX-BZ Need a framework to know when the NIC can handle it, even
911 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
912 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
913 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
916 if (sw_csum & CSUM_SCTP_IPV6) {
917 sw_csum &= ~CSUM_SCTP_IPV6;
918 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
921 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
922 tlen = m->m_pkthdr.len;
924 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
928 if (dontfrag && alwaysfrag) { /* case 4 */
929 /* conflicting request - can't transmit */
933 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
935 * Even if the DONTFRAG option is specified, we cannot send the
936 * packet when the data length is larger than the MTU of the
937 * outgoing interface.
938 * Notify the error by sending IPV6_PATHMTU ancillary data if
939 * application wanted to know the MTU value. Also return an
940 * error code (this is not described in the API spec).
943 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
949 * transmit packet without fragmentation
951 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
952 struct in6_ifaddr *ia6;
954 ip6 = mtod(m, struct ip6_hdr *);
955 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
957 /* Record statistics for this interface address. */
958 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
959 counter_u64_add(ia6->ia_ifa.ifa_obytes,
961 ifa_free(&ia6->ia_ifa);
965 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
966 in_pcboutput_txrtlmt(inp, ifp, m);
967 /* stamp send tag on mbuf */
968 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
969 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
971 m->m_pkthdr.snd_tag = NULL;
974 error = nd6_output_ifp(ifp, origifp, m, dst,
977 /* check for route change */
979 in_pcboutput_eagain(inp);
985 * try to fragment the packet. case 1-b and 3
987 if (mtu < IPV6_MMTU) {
988 /* path MTU cannot be less than IPV6_MMTU */
990 in6_ifstat_inc(ifp, ifs6_out_fragfail);
992 } else if (ip6->ip6_plen == 0) {
993 /* jumbo payload cannot be fragmented */
995 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1001 * Too large for the destination or interface;
1002 * fragment if possible.
1003 * Must be able to put at least 8 bytes per fragment.
1005 hlen = unfragpartlen;
1006 if (mtu > IPV6_MAXPACKET)
1007 mtu = IPV6_MAXPACKET;
1009 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1012 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1017 * If the interface will not calculate checksums on
1018 * fragmented packets, then do it here.
1019 * XXX-BZ handle the hw offloading case. Need flags.
1021 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1022 in6_delayed_cksum(m, plen, hlen);
1023 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1026 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1027 sctp_delayed_cksum(m, hlen);
1028 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1032 * Change the next header field of the last header in the
1033 * unfragmentable part.
1035 if (exthdrs.ip6e_rthdr) {
1036 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1037 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1038 } else if (exthdrs.ip6e_dest1) {
1039 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1040 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1041 } else if (exthdrs.ip6e_hbh) {
1042 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1043 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1045 nextproto = ip6->ip6_nxt;
1046 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1050 * Loop through length of segment after first fragment,
1051 * make new header and copy data of each part and link onto
1055 id = htonl(ip6_randomid());
1056 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1059 in6_ifstat_inc(ifp, ifs6_out_fragok);
1063 * Remove leading garbages.
1073 /* Record statistics for this interface address. */
1075 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1076 counter_u64_add(ia->ia_ifa.ifa_obytes,
1081 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
1082 in_pcboutput_txrtlmt(inp, ifp, m);
1083 /* stamp send tag on mbuf */
1084 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
1085 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
1087 m->m_pkthdr.snd_tag = NULL;
1090 error = nd6_output_ifp(ifp, origifp, m, dst,
1091 (struct route *)ro);
1093 /* check for route change */
1094 if (error == EAGAIN)
1095 in_pcboutput_eagain(inp);
1102 IP6STAT_INC(ip6s_fragmented);
1105 if (ro == &ip6route)
1110 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1111 m_freem(exthdrs.ip6e_dest1);
1112 m_freem(exthdrs.ip6e_rthdr);
1113 m_freem(exthdrs.ip6e_dest2);
1122 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1126 if (hlen > MCLBYTES)
1127 return (ENOBUFS); /* XXX */
1130 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1132 m = m_get(M_NOWAIT, MT_DATA);
1137 bcopy(hdr, mtod(m, caddr_t), hlen);
1144 * Insert jumbo payload option.
1147 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1153 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1156 * If there is no hop-by-hop options header, allocate new one.
1157 * If there is one but it doesn't have enough space to store the
1158 * jumbo payload option, allocate a cluster to store the whole options.
1159 * Otherwise, use it to store the options.
1161 if (exthdrs->ip6e_hbh == NULL) {
1162 mopt = m_get(M_NOWAIT, MT_DATA);
1165 mopt->m_len = JUMBOOPTLEN;
1166 optbuf = mtod(mopt, u_char *);
1167 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1168 exthdrs->ip6e_hbh = mopt;
1170 struct ip6_hbh *hbh;
1172 mopt = exthdrs->ip6e_hbh;
1173 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1176 * - exthdrs->ip6e_hbh is not referenced from places
1177 * other than exthdrs.
1178 * - exthdrs->ip6e_hbh is not an mbuf chain.
1180 int oldoptlen = mopt->m_len;
1184 * XXX: give up if the whole (new) hbh header does
1185 * not fit even in an mbuf cluster.
1187 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1191 * As a consequence, we must always prepare a cluster
1194 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1197 n->m_len = oldoptlen + JUMBOOPTLEN;
1198 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1200 optbuf = mtod(n, caddr_t) + oldoptlen;
1202 mopt = exthdrs->ip6e_hbh = n;
1204 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1205 mopt->m_len += JUMBOOPTLEN;
1207 optbuf[0] = IP6OPT_PADN;
1211 * Adjust the header length according to the pad and
1212 * the jumbo payload option.
1214 hbh = mtod(mopt, struct ip6_hbh *);
1215 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1218 /* fill in the option. */
1219 optbuf[2] = IP6OPT_JUMBO;
1221 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1222 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1224 /* finally, adjust the packet header length */
1225 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1232 * Insert fragment header and copy unfragmentable header portions.
1235 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1236 struct ip6_frag **frghdrp)
1238 struct mbuf *n, *mlast;
1240 if (hlen > sizeof(struct ip6_hdr)) {
1241 n = m_copym(m0, sizeof(struct ip6_hdr),
1242 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1249 /* Search for the last mbuf of unfragmentable part. */
1250 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1253 if (M_WRITABLE(mlast) &&
1254 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1255 /* use the trailing space of the last mbuf for the fragment hdr */
1256 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1258 mlast->m_len += sizeof(struct ip6_frag);
1259 m->m_pkthdr.len += sizeof(struct ip6_frag);
1261 /* allocate a new mbuf for the fragment header */
1264 mfrg = m_get(M_NOWAIT, MT_DATA);
1267 mfrg->m_len = sizeof(struct ip6_frag);
1268 *frghdrp = mtod(mfrg, struct ip6_frag *);
1269 mlast->m_next = mfrg;
1276 * Calculates IPv6 path mtu for destination @dst.
1277 * Resulting MTU is stored in @mtup.
1279 * Returns 0 on success.
1282 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1284 struct nhop6_extended nh6;
1285 struct in6_addr kdst;
1291 in6_splitscope(dst, &kdst, &scopeid);
1292 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1293 return (EHOSTUNREACH);
1298 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1299 fib6_free_nh_ext(fibnum, &nh6);
1305 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1306 * and cached data in @ro_pmtu.
1307 * MTU from (successful) route lookup is saved (along with dst)
1308 * inside @ro_pmtu to avoid subsequent route lookups after packet
1309 * filter processing.
1311 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1312 * Returns 0 on success.
1315 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1316 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1317 int *alwaysfragp, u_int fibnum, u_int proto)
1319 struct nhop6_basic nh6;
1320 struct in6_addr kdst;
1322 struct sockaddr_in6 *sa6_dst;
1329 * Here ro_pmtu has final destination address, while
1330 * ro might represent immediate destination.
1331 * Use ro_pmtu destination since mtu might differ.
1333 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1334 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1335 ro_pmtu->ro_mtu = 0;
1337 if (ro_pmtu->ro_mtu == 0) {
1338 bzero(sa6_dst, sizeof(*sa6_dst));
1339 sa6_dst->sin6_family = AF_INET6;
1340 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1341 sa6_dst->sin6_addr = *dst;
1343 in6_splitscope(dst, &kdst, &scopeid);
1344 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1346 ro_pmtu->ro_mtu = nh6.nh_mtu;
1349 mtu = ro_pmtu->ro_mtu;
1353 mtu = ro_pmtu->ro_rt->rt_mtu;
1355 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1359 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1360 * hostcache data for @dst.
1361 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1363 * Returns 0 on success.
1366 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1367 u_long *mtup, int *alwaysfragp, u_int proto)
1375 struct in_conninfo inc;
1377 bzero(&inc, sizeof(inc));
1378 inc.inc_flags |= INC_ISIPV6;
1379 inc.inc6_faddr = *dst;
1381 ifmtu = IN6_LINKMTU(ifp);
1383 /* TCP is known to react to pmtu changes so skip hc */
1384 if (proto != IPPROTO_TCP)
1385 mtu = tcp_hc_getmtu(&inc);
1388 mtu = min(mtu, rt_mtu);
1393 else if (mtu < IPV6_MMTU) {
1395 * RFC2460 section 5, last paragraph:
1396 * if we record ICMPv6 too big message with
1397 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1398 * or smaller, with framgent header attached.
1399 * (fragment header is needed regardless from the
1400 * packet size, for translators to identify packets)
1406 mtu = IN6_LINKMTU(ifp);
1408 error = EHOSTUNREACH; /* XXX */
1412 *alwaysfragp = alwaysfrag;
1417 * IP6 socket option processing.
1420 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1422 int optdatalen, uproto;
1424 struct inpcb *in6p = sotoinpcb(so);
1426 int level, op, optname;
1430 uint32_t rss_bucket;
1435 * Don't use more than a quarter of mbuf clusters. N.B.:
1436 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1437 * on LP64 architectures, so cast to u_long to avoid undefined
1438 * behavior. ILP32 architectures cannot have nmbclusters
1439 * large enough to overflow for other reasons.
1441 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1443 level = sopt->sopt_level;
1444 op = sopt->sopt_dir;
1445 optname = sopt->sopt_name;
1446 optlen = sopt->sopt_valsize;
1450 uproto = (int)so->so_proto->pr_protocol;
1452 if (level != IPPROTO_IPV6) {
1455 if (sopt->sopt_level == SOL_SOCKET &&
1456 sopt->sopt_dir == SOPT_SET) {
1457 switch (sopt->sopt_name) {
1460 if ((so->so_options & SO_REUSEADDR) != 0)
1461 in6p->inp_flags2 |= INP_REUSEADDR;
1463 in6p->inp_flags2 &= ~INP_REUSEADDR;
1469 if ((so->so_options & SO_REUSEPORT) != 0)
1470 in6p->inp_flags2 |= INP_REUSEPORT;
1472 in6p->inp_flags2 &= ~INP_REUSEPORT;
1476 case SO_REUSEPORT_LB:
1478 if ((so->so_options & SO_REUSEPORT_LB) != 0)
1479 in6p->inp_flags2 |= INP_REUSEPORT_LB;
1481 in6p->inp_flags2 &= ~INP_REUSEPORT_LB;
1487 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1491 case SO_MAX_PACING_RATE:
1494 in6p->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1505 } else { /* level == IPPROTO_IPV6 */
1510 case IPV6_2292PKTOPTIONS:
1511 #ifdef IPV6_PKTOPTIONS
1512 case IPV6_PKTOPTIONS:
1517 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1518 printf("ip6_ctloutput: mbuf limit hit\n");
1523 error = soopt_getm(sopt, &m); /* XXX */
1526 error = soopt_mcopyin(sopt, m); /* XXX */
1529 error = ip6_pcbopts(&in6p->in6p_outputopts,
1531 m_freem(m); /* XXX */
1536 * Use of some Hop-by-Hop options or some
1537 * Destination options, might require special
1538 * privilege. That is, normal applications
1539 * (without special privilege) might be forbidden
1540 * from setting certain options in outgoing packets,
1541 * and might never see certain options in received
1542 * packets. [RFC 2292 Section 6]
1543 * KAME specific note:
1544 * KAME prevents non-privileged users from sending or
1545 * receiving ANY hbh/dst options in order to avoid
1546 * overhead of parsing options in the kernel.
1548 case IPV6_RECVHOPOPTS:
1549 case IPV6_RECVDSTOPTS:
1550 case IPV6_RECVRTHDRDSTOPTS:
1552 error = priv_check(td,
1553 PRIV_NETINET_SETHDROPTS);
1558 case IPV6_UNICAST_HOPS:
1561 case IPV6_RECVPKTINFO:
1562 case IPV6_RECVHOPLIMIT:
1563 case IPV6_RECVRTHDR:
1564 case IPV6_RECVPATHMTU:
1565 case IPV6_RECVTCLASS:
1566 case IPV6_RECVFLOWID:
1568 case IPV6_RECVRSSBUCKETID:
1571 case IPV6_AUTOFLOWLABEL:
1572 case IPV6_ORIGDSTADDR:
1574 case IPV6_BINDMULTI:
1576 case IPV6_RSS_LISTEN_BUCKET:
1578 if (optname == IPV6_BINDANY && td != NULL) {
1579 error = priv_check(td,
1580 PRIV_NETINET_BINDANY);
1585 if (optlen != sizeof(int)) {
1589 error = sooptcopyin(sopt, &optval,
1590 sizeof optval, sizeof optval);
1595 case IPV6_UNICAST_HOPS:
1596 if (optval < -1 || optval >= 256)
1599 /* -1 = kernel default */
1600 in6p->in6p_hops = optval;
1601 if ((in6p->inp_vflag &
1603 in6p->inp_ip_ttl = optval;
1606 #define OPTSET(bit) \
1610 in6p->inp_flags |= (bit); \
1612 in6p->inp_flags &= ~(bit); \
1613 INP_WUNLOCK(in6p); \
1614 } while (/*CONSTCOND*/ 0)
1615 #define OPTSET2292(bit) \
1618 in6p->inp_flags |= IN6P_RFC2292; \
1620 in6p->inp_flags |= (bit); \
1622 in6p->inp_flags &= ~(bit); \
1623 INP_WUNLOCK(in6p); \
1624 } while (/*CONSTCOND*/ 0)
1625 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1627 #define OPTSET2_N(bit, val) do { \
1629 in6p->inp_flags2 |= bit; \
1631 in6p->inp_flags2 &= ~bit; \
1633 #define OPTSET2(bit, val) do { \
1635 OPTSET2_N(bit, val); \
1636 INP_WUNLOCK(in6p); \
1638 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1639 #define OPTSET2292_EXCLUSIVE(bit) \
1642 if (OPTBIT(IN6P_RFC2292)) { \
1646 in6p->inp_flags |= (bit); \
1648 in6p->inp_flags &= ~(bit); \
1650 INP_WUNLOCK(in6p); \
1651 } while (/*CONSTCOND*/ 0)
1653 case IPV6_RECVPKTINFO:
1654 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1659 struct ip6_pktopts **optp;
1661 /* cannot mix with RFC2292 */
1662 if (OPTBIT(IN6P_RFC2292)) {
1667 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1669 return (ECONNRESET);
1671 optp = &in6p->in6p_outputopts;
1672 error = ip6_pcbopt(IPV6_HOPLIMIT,
1673 (u_char *)&optval, sizeof(optval),
1674 optp, (td != NULL) ? td->td_ucred :
1680 case IPV6_RECVHOPLIMIT:
1681 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1684 case IPV6_RECVHOPOPTS:
1685 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1688 case IPV6_RECVDSTOPTS:
1689 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1692 case IPV6_RECVRTHDRDSTOPTS:
1693 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1696 case IPV6_RECVRTHDR:
1697 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1700 case IPV6_RECVPATHMTU:
1702 * We ignore this option for TCP
1704 * (RFC3542 leaves this case
1707 if (uproto != IPPROTO_TCP)
1711 case IPV6_RECVFLOWID:
1712 OPTSET2(INP_RECVFLOWID, optval);
1716 case IPV6_RECVRSSBUCKETID:
1717 OPTSET2(INP_RECVRSSBUCKETID, optval);
1723 * make setsockopt(IPV6_V6ONLY)
1724 * available only prior to bind(2).
1725 * see ipng mailing list, Jun 22 2001.
1727 if (in6p->inp_lport ||
1728 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1732 OPTSET(IN6P_IPV6_V6ONLY);
1734 in6p->inp_vflag &= ~INP_IPV4;
1736 in6p->inp_vflag |= INP_IPV4;
1738 case IPV6_RECVTCLASS:
1739 /* cannot mix with RFC2292 XXX */
1740 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1742 case IPV6_AUTOFLOWLABEL:
1743 OPTSET(IN6P_AUTOFLOWLABEL);
1746 case IPV6_ORIGDSTADDR:
1747 OPTSET2(INP_ORIGDSTADDR, optval);
1750 OPTSET(INP_BINDANY);
1753 case IPV6_BINDMULTI:
1754 OPTSET2(INP_BINDMULTI, optval);
1757 case IPV6_RSS_LISTEN_BUCKET:
1758 if ((optval >= 0) &&
1759 (optval < rss_getnumbuckets())) {
1761 in6p->inp_rss_listen_bucket = optval;
1762 OPTSET2_N(INP_RSS_BUCKET_SET, 1);
1774 case IPV6_USE_MIN_MTU:
1775 case IPV6_PREFER_TEMPADDR:
1776 if (optlen != sizeof(optval)) {
1780 error = sooptcopyin(sopt, &optval,
1781 sizeof optval, sizeof optval);
1785 struct ip6_pktopts **optp;
1787 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1789 return (ECONNRESET);
1791 optp = &in6p->in6p_outputopts;
1792 error = ip6_pcbopt(optname,
1793 (u_char *)&optval, sizeof(optval),
1794 optp, (td != NULL) ? td->td_ucred :
1800 case IPV6_2292PKTINFO:
1801 case IPV6_2292HOPLIMIT:
1802 case IPV6_2292HOPOPTS:
1803 case IPV6_2292DSTOPTS:
1804 case IPV6_2292RTHDR:
1806 if (optlen != sizeof(int)) {
1810 error = sooptcopyin(sopt, &optval,
1811 sizeof optval, sizeof optval);
1815 case IPV6_2292PKTINFO:
1816 OPTSET2292(IN6P_PKTINFO);
1818 case IPV6_2292HOPLIMIT:
1819 OPTSET2292(IN6P_HOPLIMIT);
1821 case IPV6_2292HOPOPTS:
1823 * Check super-user privilege.
1824 * See comments for IPV6_RECVHOPOPTS.
1827 error = priv_check(td,
1828 PRIV_NETINET_SETHDROPTS);
1832 OPTSET2292(IN6P_HOPOPTS);
1834 case IPV6_2292DSTOPTS:
1836 error = priv_check(td,
1837 PRIV_NETINET_SETHDROPTS);
1841 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1843 case IPV6_2292RTHDR:
1844 OPTSET2292(IN6P_RTHDR);
1852 case IPV6_RTHDRDSTOPTS:
1855 /* new advanced API (RFC3542) */
1857 u_char optbuf_storage[MCLBYTES];
1859 struct ip6_pktopts **optp;
1861 /* cannot mix with RFC2292 */
1862 if (OPTBIT(IN6P_RFC2292)) {
1868 * We only ensure valsize is not too large
1869 * here. Further validation will be done
1872 error = sooptcopyin(sopt, optbuf_storage,
1873 sizeof(optbuf_storage), 0);
1876 optlen = sopt->sopt_valsize;
1877 optbuf = optbuf_storage;
1879 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1881 return (ECONNRESET);
1883 optp = &in6p->in6p_outputopts;
1884 error = ip6_pcbopt(optname, optbuf, optlen,
1885 optp, (td != NULL) ? td->td_ucred : NULL,
1892 case IPV6_MULTICAST_IF:
1893 case IPV6_MULTICAST_HOPS:
1894 case IPV6_MULTICAST_LOOP:
1895 case IPV6_JOIN_GROUP:
1896 case IPV6_LEAVE_GROUP:
1898 case MCAST_BLOCK_SOURCE:
1899 case MCAST_UNBLOCK_SOURCE:
1900 case MCAST_JOIN_GROUP:
1901 case MCAST_LEAVE_GROUP:
1902 case MCAST_JOIN_SOURCE_GROUP:
1903 case MCAST_LEAVE_SOURCE_GROUP:
1904 error = ip6_setmoptions(in6p, sopt);
1907 case IPV6_PORTRANGE:
1908 error = sooptcopyin(sopt, &optval,
1909 sizeof optval, sizeof optval);
1915 case IPV6_PORTRANGE_DEFAULT:
1916 in6p->inp_flags &= ~(INP_LOWPORT);
1917 in6p->inp_flags &= ~(INP_HIGHPORT);
1920 case IPV6_PORTRANGE_HIGH:
1921 in6p->inp_flags &= ~(INP_LOWPORT);
1922 in6p->inp_flags |= INP_HIGHPORT;
1925 case IPV6_PORTRANGE_LOW:
1926 in6p->inp_flags &= ~(INP_HIGHPORT);
1927 in6p->inp_flags |= INP_LOWPORT;
1937 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1938 case IPV6_IPSEC_POLICY:
1939 if (IPSEC_ENABLED(ipv6)) {
1940 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
1947 error = ENOPROTOOPT;
1955 case IPV6_2292PKTOPTIONS:
1956 #ifdef IPV6_PKTOPTIONS
1957 case IPV6_PKTOPTIONS:
1960 * RFC3542 (effectively) deprecated the
1961 * semantics of the 2292-style pktoptions.
1962 * Since it was not reliable in nature (i.e.,
1963 * applications had to expect the lack of some
1964 * information after all), it would make sense
1965 * to simplify this part by always returning
1968 sopt->sopt_valsize = 0;
1971 case IPV6_RECVHOPOPTS:
1972 case IPV6_RECVDSTOPTS:
1973 case IPV6_RECVRTHDRDSTOPTS:
1974 case IPV6_UNICAST_HOPS:
1975 case IPV6_RECVPKTINFO:
1976 case IPV6_RECVHOPLIMIT:
1977 case IPV6_RECVRTHDR:
1978 case IPV6_RECVPATHMTU:
1981 case IPV6_PORTRANGE:
1982 case IPV6_RECVTCLASS:
1983 case IPV6_AUTOFLOWLABEL:
1987 case IPV6_RECVFLOWID:
1989 case IPV6_RSSBUCKETID:
1990 case IPV6_RECVRSSBUCKETID:
1992 case IPV6_BINDMULTI:
1995 case IPV6_RECVHOPOPTS:
1996 optval = OPTBIT(IN6P_HOPOPTS);
1999 case IPV6_RECVDSTOPTS:
2000 optval = OPTBIT(IN6P_DSTOPTS);
2003 case IPV6_RECVRTHDRDSTOPTS:
2004 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2007 case IPV6_UNICAST_HOPS:
2008 optval = in6p->in6p_hops;
2011 case IPV6_RECVPKTINFO:
2012 optval = OPTBIT(IN6P_PKTINFO);
2015 case IPV6_RECVHOPLIMIT:
2016 optval = OPTBIT(IN6P_HOPLIMIT);
2019 case IPV6_RECVRTHDR:
2020 optval = OPTBIT(IN6P_RTHDR);
2023 case IPV6_RECVPATHMTU:
2024 optval = OPTBIT(IN6P_MTU);
2028 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2031 case IPV6_PORTRANGE:
2034 flags = in6p->inp_flags;
2035 if (flags & INP_HIGHPORT)
2036 optval = IPV6_PORTRANGE_HIGH;
2037 else if (flags & INP_LOWPORT)
2038 optval = IPV6_PORTRANGE_LOW;
2043 case IPV6_RECVTCLASS:
2044 optval = OPTBIT(IN6P_TCLASS);
2047 case IPV6_AUTOFLOWLABEL:
2048 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2051 case IPV6_ORIGDSTADDR:
2052 optval = OPTBIT2(INP_ORIGDSTADDR);
2056 optval = OPTBIT(INP_BINDANY);
2060 optval = in6p->inp_flowid;
2064 optval = in6p->inp_flowtype;
2067 case IPV6_RECVFLOWID:
2068 optval = OPTBIT2(INP_RECVFLOWID);
2071 case IPV6_RSSBUCKETID:
2073 rss_hash2bucket(in6p->inp_flowid,
2077 optval = rss_bucket;
2082 case IPV6_RECVRSSBUCKETID:
2083 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2087 case IPV6_BINDMULTI:
2088 optval = OPTBIT2(INP_BINDMULTI);
2094 error = sooptcopyout(sopt, &optval,
2101 struct ip6_mtuinfo mtuinfo;
2102 struct in6_addr addr;
2104 if (!(so->so_state & SS_ISCONNECTED))
2107 * XXX: we dot not consider the case of source
2108 * routing, or optional information to specify
2109 * the outgoing interface.
2110 * Copy faddr out of in6p to avoid holding lock
2111 * on inp during route lookup.
2114 bcopy(&in6p->in6p_faddr, &addr, sizeof(addr));
2116 error = ip6_getpmtu_ctl(so->so_fibnum,
2120 if (pmtu > IPV6_MAXPACKET)
2121 pmtu = IPV6_MAXPACKET;
2123 bzero(&mtuinfo, sizeof(mtuinfo));
2124 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2125 optdata = (void *)&mtuinfo;
2126 optdatalen = sizeof(mtuinfo);
2127 error = sooptcopyout(sopt, optdata,
2132 case IPV6_2292PKTINFO:
2133 case IPV6_2292HOPLIMIT:
2134 case IPV6_2292HOPOPTS:
2135 case IPV6_2292RTHDR:
2136 case IPV6_2292DSTOPTS:
2138 case IPV6_2292PKTINFO:
2139 optval = OPTBIT(IN6P_PKTINFO);
2141 case IPV6_2292HOPLIMIT:
2142 optval = OPTBIT(IN6P_HOPLIMIT);
2144 case IPV6_2292HOPOPTS:
2145 optval = OPTBIT(IN6P_HOPOPTS);
2147 case IPV6_2292RTHDR:
2148 optval = OPTBIT(IN6P_RTHDR);
2150 case IPV6_2292DSTOPTS:
2151 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2154 error = sooptcopyout(sopt, &optval,
2161 case IPV6_RTHDRDSTOPTS:
2165 case IPV6_USE_MIN_MTU:
2166 case IPV6_PREFER_TEMPADDR:
2167 error = ip6_getpcbopt(in6p, optname, sopt);
2170 case IPV6_MULTICAST_IF:
2171 case IPV6_MULTICAST_HOPS:
2172 case IPV6_MULTICAST_LOOP:
2174 error = ip6_getmoptions(in6p, sopt);
2177 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2178 case IPV6_IPSEC_POLICY:
2179 if (IPSEC_ENABLED(ipv6)) {
2180 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
2186 error = ENOPROTOOPT;
2196 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2198 int error = 0, optval, optlen;
2199 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2200 struct inpcb *in6p = sotoinpcb(so);
2201 int level, op, optname;
2203 level = sopt->sopt_level;
2204 op = sopt->sopt_dir;
2205 optname = sopt->sopt_name;
2206 optlen = sopt->sopt_valsize;
2208 if (level != IPPROTO_IPV6) {
2215 * For ICMPv6 sockets, no modification allowed for checksum
2216 * offset, permit "no change" values to help existing apps.
2218 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2219 * for an ICMPv6 socket will fail."
2220 * The current behavior does not meet RFC3542.
2224 if (optlen != sizeof(int)) {
2228 error = sooptcopyin(sopt, &optval, sizeof(optval),
2232 if (optval < -1 || (optval % 2) != 0) {
2234 * The API assumes non-negative even offset
2235 * values or -1 as a special value.
2238 } else if (so->so_proto->pr_protocol ==
2240 if (optval != icmp6off)
2243 in6p->in6p_cksum = optval;
2247 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2250 optval = in6p->in6p_cksum;
2252 error = sooptcopyout(sopt, &optval, sizeof(optval));
2262 error = ENOPROTOOPT;
2270 * Set up IP6 options in pcb for insertion in output packets or
2271 * specifying behavior of outgoing packets.
2274 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2275 struct socket *so, struct sockopt *sopt)
2277 struct ip6_pktopts *opt = *pktopt;
2279 struct thread *td = sopt->sopt_td;
2281 /* turn off any old options. */
2284 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2285 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2286 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2287 printf("ip6_pcbopts: all specified options are cleared.\n");
2289 ip6_clearpktopts(opt, -1);
2291 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2294 if (!m || m->m_len == 0) {
2296 * Only turning off any previous options, regardless of
2297 * whether the opt is just created or given.
2299 free(opt, M_IP6OPT);
2303 /* set options specified by user. */
2304 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2305 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2306 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2307 free(opt, M_IP6OPT);
2315 * initialize ip6_pktopts. beware that there are non-zero default values in
2319 ip6_initpktopts(struct ip6_pktopts *opt)
2322 bzero(opt, sizeof(*opt));
2323 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2324 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2325 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2326 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2330 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2331 struct ucred *cred, int uproto)
2333 struct ip6_pktopts *opt;
2335 if (*pktopt == NULL) {
2336 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2338 if (*pktopt == NULL)
2340 ip6_initpktopts(*pktopt);
2344 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2347 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2348 if (pktopt && pktopt->field) { \
2349 INP_RUNLOCK(in6p); \
2350 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2351 malloc_optdata = true; \
2353 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
2354 INP_RUNLOCK(in6p); \
2355 free(optdata, M_TEMP); \
2356 return (ECONNRESET); \
2358 pktopt = in6p->in6p_outputopts; \
2359 if (pktopt && pktopt->field) { \
2360 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2361 bcopy(&pktopt->field, optdata, optdatalen); \
2363 free(optdata, M_TEMP); \
2365 malloc_optdata = false; \
2370 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2371 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2373 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2374 pktopt->field->sa_len)
2377 ip6_getpcbopt(struct inpcb *in6p, int optname, struct sockopt *sopt)
2379 void *optdata = NULL;
2380 bool malloc_optdata = false;
2383 struct in6_pktinfo null_pktinfo;
2384 int deftclass = 0, on;
2385 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2386 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2387 struct ip6_pktopts *pktopt;
2390 pktopt = in6p->in6p_outputopts;
2394 optdata = (void *)&null_pktinfo;
2395 if (pktopt && pktopt->ip6po_pktinfo) {
2396 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2397 sizeof(null_pktinfo));
2398 in6_clearscope(&null_pktinfo.ipi6_addr);
2400 /* XXX: we don't have to do this every time... */
2401 bzero(&null_pktinfo, sizeof(null_pktinfo));
2403 optdatalen = sizeof(struct in6_pktinfo);
2406 if (pktopt && pktopt->ip6po_tclass >= 0)
2407 deftclass = pktopt->ip6po_tclass;
2408 optdata = (void *)&deftclass;
2409 optdatalen = sizeof(int);
2412 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2415 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2417 case IPV6_RTHDRDSTOPTS:
2418 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2421 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2424 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2426 case IPV6_USE_MIN_MTU:
2428 defminmtu = pktopt->ip6po_minmtu;
2429 optdata = (void *)&defminmtu;
2430 optdatalen = sizeof(int);
2433 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2437 optdata = (void *)&on;
2438 optdatalen = sizeof(on);
2440 case IPV6_PREFER_TEMPADDR:
2442 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2443 optdata = (void *)&defpreftemp;
2444 optdatalen = sizeof(int);
2446 default: /* should not happen */
2448 panic("ip6_getpcbopt: unexpected option\n");
2451 return (ENOPROTOOPT);
2455 error = sooptcopyout(sopt, optdata, optdatalen);
2457 free(optdata, M_TEMP);
2463 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2468 if (optname == -1 || optname == IPV6_PKTINFO) {
2469 if (pktopt->ip6po_pktinfo)
2470 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2471 pktopt->ip6po_pktinfo = NULL;
2473 if (optname == -1 || optname == IPV6_HOPLIMIT)
2474 pktopt->ip6po_hlim = -1;
2475 if (optname == -1 || optname == IPV6_TCLASS)
2476 pktopt->ip6po_tclass = -1;
2477 if (optname == -1 || optname == IPV6_NEXTHOP) {
2478 if (pktopt->ip6po_nextroute.ro_rt) {
2479 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2480 pktopt->ip6po_nextroute.ro_rt = NULL;
2482 if (pktopt->ip6po_nexthop)
2483 free(pktopt->ip6po_nexthop, M_IP6OPT);
2484 pktopt->ip6po_nexthop = NULL;
2486 if (optname == -1 || optname == IPV6_HOPOPTS) {
2487 if (pktopt->ip6po_hbh)
2488 free(pktopt->ip6po_hbh, M_IP6OPT);
2489 pktopt->ip6po_hbh = NULL;
2491 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2492 if (pktopt->ip6po_dest1)
2493 free(pktopt->ip6po_dest1, M_IP6OPT);
2494 pktopt->ip6po_dest1 = NULL;
2496 if (optname == -1 || optname == IPV6_RTHDR) {
2497 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2498 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2499 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2500 if (pktopt->ip6po_route.ro_rt) {
2501 RTFREE(pktopt->ip6po_route.ro_rt);
2502 pktopt->ip6po_route.ro_rt = NULL;
2505 if (optname == -1 || optname == IPV6_DSTOPTS) {
2506 if (pktopt->ip6po_dest2)
2507 free(pktopt->ip6po_dest2, M_IP6OPT);
2508 pktopt->ip6po_dest2 = NULL;
2512 #define PKTOPT_EXTHDRCPY(type) \
2515 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2516 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2517 if (dst->type == NULL)\
2519 bcopy(src->type, dst->type, hlen);\
2521 } while (/*CONSTCOND*/ 0)
2524 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2526 if (dst == NULL || src == NULL) {
2527 printf("ip6_clearpktopts: invalid argument\n");
2531 dst->ip6po_hlim = src->ip6po_hlim;
2532 dst->ip6po_tclass = src->ip6po_tclass;
2533 dst->ip6po_flags = src->ip6po_flags;
2534 dst->ip6po_minmtu = src->ip6po_minmtu;
2535 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2536 if (src->ip6po_pktinfo) {
2537 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2539 if (dst->ip6po_pktinfo == NULL)
2541 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2543 if (src->ip6po_nexthop) {
2544 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2546 if (dst->ip6po_nexthop == NULL)
2548 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2549 src->ip6po_nexthop->sa_len);
2551 PKTOPT_EXTHDRCPY(ip6po_hbh);
2552 PKTOPT_EXTHDRCPY(ip6po_dest1);
2553 PKTOPT_EXTHDRCPY(ip6po_dest2);
2554 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2558 ip6_clearpktopts(dst, -1);
2561 #undef PKTOPT_EXTHDRCPY
2563 struct ip6_pktopts *
2564 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2567 struct ip6_pktopts *dst;
2569 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2572 ip6_initpktopts(dst);
2574 if ((error = copypktopts(dst, src, canwait)) != 0) {
2575 free(dst, M_IP6OPT);
2583 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2588 ip6_clearpktopts(pktopt, -1);
2590 free(pktopt, M_IP6OPT);
2594 * Set IPv6 outgoing packet options based on advanced API.
2597 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2598 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2600 struct cmsghdr *cm = NULL;
2602 if (control == NULL || opt == NULL)
2605 ip6_initpktopts(opt);
2610 * If stickyopt is provided, make a local copy of the options
2611 * for this particular packet, then override them by ancillary
2613 * XXX: copypktopts() does not copy the cached route to a next
2614 * hop (if any). This is not very good in terms of efficiency,
2615 * but we can allow this since this option should be rarely
2618 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2623 * XXX: Currently, we assume all the optional information is stored
2626 if (control->m_next)
2629 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2630 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2633 if (control->m_len < CMSG_LEN(0))
2636 cm = mtod(control, struct cmsghdr *);
2637 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2639 if (cm->cmsg_level != IPPROTO_IPV6)
2642 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2643 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2652 * Set a particular packet option, as a sticky option or an ancillary data
2653 * item. "len" can be 0 only when it's a sticky option.
2654 * We have 4 cases of combination of "sticky" and "cmsg":
2655 * "sticky=0, cmsg=0": impossible
2656 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2657 * "sticky=1, cmsg=0": RFC3542 socket option
2658 * "sticky=1, cmsg=1": RFC2292 socket option
2661 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2662 struct ucred *cred, int sticky, int cmsg, int uproto)
2664 int minmtupolicy, preftemp;
2667 if (!sticky && !cmsg) {
2669 printf("ip6_setpktopt: impossible case\n");
2675 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2676 * not be specified in the context of RFC3542. Conversely,
2677 * RFC3542 types should not be specified in the context of RFC2292.
2681 case IPV6_2292PKTINFO:
2682 case IPV6_2292HOPLIMIT:
2683 case IPV6_2292NEXTHOP:
2684 case IPV6_2292HOPOPTS:
2685 case IPV6_2292DSTOPTS:
2686 case IPV6_2292RTHDR:
2687 case IPV6_2292PKTOPTIONS:
2688 return (ENOPROTOOPT);
2691 if (sticky && cmsg) {
2698 case IPV6_RTHDRDSTOPTS:
2700 case IPV6_USE_MIN_MTU:
2703 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2704 return (ENOPROTOOPT);
2709 case IPV6_2292PKTINFO:
2712 struct ifnet *ifp = NULL;
2713 struct in6_pktinfo *pktinfo;
2715 if (len != sizeof(struct in6_pktinfo))
2718 pktinfo = (struct in6_pktinfo *)buf;
2721 * An application can clear any sticky IPV6_PKTINFO option by
2722 * doing a "regular" setsockopt with ipi6_addr being
2723 * in6addr_any and ipi6_ifindex being zero.
2724 * [RFC 3542, Section 6]
2726 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2727 pktinfo->ipi6_ifindex == 0 &&
2728 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2729 ip6_clearpktopts(opt, optname);
2733 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2734 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2737 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2739 /* validate the interface index if specified. */
2740 if (pktinfo->ipi6_ifindex > V_if_index)
2742 if (pktinfo->ipi6_ifindex) {
2743 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2747 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2748 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2752 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2753 struct in6_ifaddr *ia;
2755 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2756 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2758 return (EADDRNOTAVAIL);
2759 ifa_free(&ia->ia_ifa);
2762 * We store the address anyway, and let in6_selectsrc()
2763 * validate the specified address. This is because ipi6_addr
2764 * may not have enough information about its scope zone, and
2765 * we may need additional information (such as outgoing
2766 * interface or the scope zone of a destination address) to
2767 * disambiguate the scope.
2768 * XXX: the delay of the validation may confuse the
2769 * application when it is used as a sticky option.
2771 if (opt->ip6po_pktinfo == NULL) {
2772 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2773 M_IP6OPT, M_NOWAIT);
2774 if (opt->ip6po_pktinfo == NULL)
2777 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2781 case IPV6_2292HOPLIMIT:
2787 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2788 * to simplify the ordering among hoplimit options.
2790 if (optname == IPV6_HOPLIMIT && sticky)
2791 return (ENOPROTOOPT);
2793 if (len != sizeof(int))
2796 if (*hlimp < -1 || *hlimp > 255)
2799 opt->ip6po_hlim = *hlimp;
2807 if (len != sizeof(int))
2809 tclass = *(int *)buf;
2810 if (tclass < -1 || tclass > 255)
2813 opt->ip6po_tclass = tclass;
2817 case IPV6_2292NEXTHOP:
2820 error = priv_check_cred(cred,
2821 PRIV_NETINET_SETHDROPTS, 0);
2826 if (len == 0) { /* just remove the option */
2827 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2831 /* check if cmsg_len is large enough for sa_len */
2832 if (len < sizeof(struct sockaddr) || len < *buf)
2835 switch (((struct sockaddr *)buf)->sa_family) {
2838 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2841 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2844 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2845 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2848 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2854 case AF_LINK: /* should eventually be supported */
2856 return (EAFNOSUPPORT);
2859 /* turn off the previous option, then set the new option. */
2860 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2861 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2862 if (opt->ip6po_nexthop == NULL)
2864 bcopy(buf, opt->ip6po_nexthop, *buf);
2867 case IPV6_2292HOPOPTS:
2870 struct ip6_hbh *hbh;
2874 * XXX: We don't allow a non-privileged user to set ANY HbH
2875 * options, since per-option restriction has too much
2879 error = priv_check_cred(cred,
2880 PRIV_NETINET_SETHDROPTS, 0);
2886 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2887 break; /* just remove the option */
2890 /* message length validation */
2891 if (len < sizeof(struct ip6_hbh))
2893 hbh = (struct ip6_hbh *)buf;
2894 hbhlen = (hbh->ip6h_len + 1) << 3;
2898 /* turn off the previous option, then set the new option. */
2899 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2900 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2901 if (opt->ip6po_hbh == NULL)
2903 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2908 case IPV6_2292DSTOPTS:
2910 case IPV6_RTHDRDSTOPTS:
2912 struct ip6_dest *dest, **newdest = NULL;
2915 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2916 error = priv_check_cred(cred,
2917 PRIV_NETINET_SETHDROPTS, 0);
2923 ip6_clearpktopts(opt, optname);
2924 break; /* just remove the option */
2927 /* message length validation */
2928 if (len < sizeof(struct ip6_dest))
2930 dest = (struct ip6_dest *)buf;
2931 destlen = (dest->ip6d_len + 1) << 3;
2936 * Determine the position that the destination options header
2937 * should be inserted; before or after the routing header.
2940 case IPV6_2292DSTOPTS:
2942 * The old advacned API is ambiguous on this point.
2943 * Our approach is to determine the position based
2944 * according to the existence of a routing header.
2945 * Note, however, that this depends on the order of the
2946 * extension headers in the ancillary data; the 1st
2947 * part of the destination options header must appear
2948 * before the routing header in the ancillary data,
2950 * RFC3542 solved the ambiguity by introducing
2951 * separate ancillary data or option types.
2953 if (opt->ip6po_rthdr == NULL)
2954 newdest = &opt->ip6po_dest1;
2956 newdest = &opt->ip6po_dest2;
2958 case IPV6_RTHDRDSTOPTS:
2959 newdest = &opt->ip6po_dest1;
2962 newdest = &opt->ip6po_dest2;
2966 /* turn off the previous option, then set the new option. */
2967 ip6_clearpktopts(opt, optname);
2968 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2969 if (*newdest == NULL)
2971 bcopy(dest, *newdest, destlen);
2976 case IPV6_2292RTHDR:
2979 struct ip6_rthdr *rth;
2983 ip6_clearpktopts(opt, IPV6_RTHDR);
2984 break; /* just remove the option */
2987 /* message length validation */
2988 if (len < sizeof(struct ip6_rthdr))
2990 rth = (struct ip6_rthdr *)buf;
2991 rthlen = (rth->ip6r_len + 1) << 3;
2995 switch (rth->ip6r_type) {
2996 case IPV6_RTHDR_TYPE_0:
2997 if (rth->ip6r_len == 0) /* must contain one addr */
2999 if (rth->ip6r_len % 2) /* length must be even */
3001 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3005 return (EINVAL); /* not supported */
3008 /* turn off the previous option */
3009 ip6_clearpktopts(opt, IPV6_RTHDR);
3010 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3011 if (opt->ip6po_rthdr == NULL)
3013 bcopy(rth, opt->ip6po_rthdr, rthlen);
3018 case IPV6_USE_MIN_MTU:
3019 if (len != sizeof(int))
3021 minmtupolicy = *(int *)buf;
3022 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3023 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3024 minmtupolicy != IP6PO_MINMTU_ALL) {
3027 opt->ip6po_minmtu = minmtupolicy;
3031 if (len != sizeof(int))
3034 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3036 * we ignore this option for TCP sockets.
3037 * (RFC3542 leaves this case unspecified.)
3039 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3041 opt->ip6po_flags |= IP6PO_DONTFRAG;
3044 case IPV6_PREFER_TEMPADDR:
3045 if (len != sizeof(int))
3047 preftemp = *(int *)buf;
3048 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3049 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3050 preftemp != IP6PO_TEMPADDR_PREFER) {
3053 opt->ip6po_prefer_tempaddr = preftemp;
3057 return (ENOPROTOOPT);
3058 } /* end of switch */
3064 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3065 * packet to the input queue of a specified interface. Note that this
3066 * calls the output routine of the loopback "driver", but with an interface
3067 * pointer that might NOT be &loif -- easier than replicating that code here.
3070 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3073 struct ip6_hdr *ip6;
3075 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3080 * Make sure to deep-copy IPv6 header portion in case the data
3081 * is in an mbuf cluster, so that we can safely override the IPv6
3082 * header portion later.
3084 if (!M_WRITABLE(copym) ||
3085 copym->m_len < sizeof(struct ip6_hdr)) {
3086 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3090 ip6 = mtod(copym, struct ip6_hdr *);
3092 * clear embedded scope identifiers if necessary.
3093 * in6_clearscope will touch the addresses only when necessary.
3095 in6_clearscope(&ip6->ip6_src);
3096 in6_clearscope(&ip6->ip6_dst);
3097 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3098 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3100 copym->m_pkthdr.csum_data = 0xffff;
3102 if_simloop(ifp, copym, AF_INET6, 0);
3106 * Chop IPv6 header off from the payload.
3109 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3112 struct ip6_hdr *ip6;
3114 ip6 = mtod(m, struct ip6_hdr *);
3115 if (m->m_len > sizeof(*ip6)) {
3116 mh = m_gethdr(M_NOWAIT, MT_DATA);
3121 m_move_pkthdr(mh, m);
3122 M_ALIGN(mh, sizeof(*ip6));
3123 m->m_len -= sizeof(*ip6);
3124 m->m_data += sizeof(*ip6);
3127 m->m_len = sizeof(*ip6);
3128 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3130 exthdrs->ip6e_ip6 = m;
3135 * Compute IPv6 extension header length.
3138 ip6_optlen(struct inpcb *in6p)
3142 if (!in6p->in6p_outputopts)
3147 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3149 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3150 if (in6p->in6p_outputopts->ip6po_rthdr)
3151 /* dest1 is valid with rthdr only */
3152 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3153 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3154 len += elen(in6p->in6p_outputopts->ip6po_dest2);