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;
970 m->m_pkthdr.snd_tag = NULL;
973 error = nd6_output_ifp(ifp, origifp, m, dst,
976 /* check for route change */
978 in_pcboutput_eagain(inp);
984 * try to fragment the packet. case 1-b and 3
986 if (mtu < IPV6_MMTU) {
987 /* path MTU cannot be less than IPV6_MMTU */
989 in6_ifstat_inc(ifp, ifs6_out_fragfail);
991 } else if (ip6->ip6_plen == 0) {
992 /* jumbo payload cannot be fragmented */
994 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1000 * Too large for the destination or interface;
1001 * fragment if possible.
1002 * Must be able to put at least 8 bytes per fragment.
1004 hlen = unfragpartlen;
1005 if (mtu > IPV6_MAXPACKET)
1006 mtu = IPV6_MAXPACKET;
1008 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1011 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1016 * If the interface will not calculate checksums on
1017 * fragmented packets, then do it here.
1018 * XXX-BZ handle the hw offloading case. Need flags.
1020 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1021 in6_delayed_cksum(m, plen, hlen);
1022 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1025 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1026 sctp_delayed_cksum(m, hlen);
1027 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1031 * Change the next header field of the last header in the
1032 * unfragmentable part.
1034 if (exthdrs.ip6e_rthdr) {
1035 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1036 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1037 } else if (exthdrs.ip6e_dest1) {
1038 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1039 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1040 } else if (exthdrs.ip6e_hbh) {
1041 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1042 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1044 nextproto = ip6->ip6_nxt;
1045 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1049 * Loop through length of segment after first fragment,
1050 * make new header and copy data of each part and link onto
1054 id = htonl(ip6_randomid());
1055 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1058 in6_ifstat_inc(ifp, ifs6_out_fragok);
1062 * Remove leading garbages.
1072 /* Record statistics for this interface address. */
1074 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1075 counter_u64_add(ia->ia_ifa.ifa_obytes,
1080 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
1081 in_pcboutput_txrtlmt(inp, ifp, m);
1082 /* stamp send tag on mbuf */
1083 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
1085 m->m_pkthdr.snd_tag = NULL;
1088 error = nd6_output_ifp(ifp, origifp, m, dst,
1089 (struct route *)ro);
1091 /* check for route change */
1092 if (error == EAGAIN)
1093 in_pcboutput_eagain(inp);
1100 IP6STAT_INC(ip6s_fragmented);
1103 if (ro == &ip6route)
1108 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1109 m_freem(exthdrs.ip6e_dest1);
1110 m_freem(exthdrs.ip6e_rthdr);
1111 m_freem(exthdrs.ip6e_dest2);
1120 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1124 if (hlen > MCLBYTES)
1125 return (ENOBUFS); /* XXX */
1128 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1130 m = m_get(M_NOWAIT, MT_DATA);
1135 bcopy(hdr, mtod(m, caddr_t), hlen);
1142 * Insert jumbo payload option.
1145 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1151 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1154 * If there is no hop-by-hop options header, allocate new one.
1155 * If there is one but it doesn't have enough space to store the
1156 * jumbo payload option, allocate a cluster to store the whole options.
1157 * Otherwise, use it to store the options.
1159 if (exthdrs->ip6e_hbh == NULL) {
1160 mopt = m_get(M_NOWAIT, MT_DATA);
1163 mopt->m_len = JUMBOOPTLEN;
1164 optbuf = mtod(mopt, u_char *);
1165 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1166 exthdrs->ip6e_hbh = mopt;
1168 struct ip6_hbh *hbh;
1170 mopt = exthdrs->ip6e_hbh;
1171 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1174 * - exthdrs->ip6e_hbh is not referenced from places
1175 * other than exthdrs.
1176 * - exthdrs->ip6e_hbh is not an mbuf chain.
1178 int oldoptlen = mopt->m_len;
1182 * XXX: give up if the whole (new) hbh header does
1183 * not fit even in an mbuf cluster.
1185 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1189 * As a consequence, we must always prepare a cluster
1192 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1195 n->m_len = oldoptlen + JUMBOOPTLEN;
1196 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1198 optbuf = mtod(n, caddr_t) + oldoptlen;
1200 mopt = exthdrs->ip6e_hbh = n;
1202 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1203 mopt->m_len += JUMBOOPTLEN;
1205 optbuf[0] = IP6OPT_PADN;
1209 * Adjust the header length according to the pad and
1210 * the jumbo payload option.
1212 hbh = mtod(mopt, struct ip6_hbh *);
1213 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1216 /* fill in the option. */
1217 optbuf[2] = IP6OPT_JUMBO;
1219 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1220 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1222 /* finally, adjust the packet header length */
1223 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1230 * Insert fragment header and copy unfragmentable header portions.
1233 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1234 struct ip6_frag **frghdrp)
1236 struct mbuf *n, *mlast;
1238 if (hlen > sizeof(struct ip6_hdr)) {
1239 n = m_copym(m0, sizeof(struct ip6_hdr),
1240 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1247 /* Search for the last mbuf of unfragmentable part. */
1248 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1251 if (M_WRITABLE(mlast) &&
1252 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1253 /* use the trailing space of the last mbuf for the fragment hdr */
1254 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1256 mlast->m_len += sizeof(struct ip6_frag);
1257 m->m_pkthdr.len += sizeof(struct ip6_frag);
1259 /* allocate a new mbuf for the fragment header */
1262 mfrg = m_get(M_NOWAIT, MT_DATA);
1265 mfrg->m_len = sizeof(struct ip6_frag);
1266 *frghdrp = mtod(mfrg, struct ip6_frag *);
1267 mlast->m_next = mfrg;
1274 * Calculates IPv6 path mtu for destination @dst.
1275 * Resulting MTU is stored in @mtup.
1277 * Returns 0 on success.
1280 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1282 struct nhop6_extended nh6;
1283 struct in6_addr kdst;
1289 in6_splitscope(dst, &kdst, &scopeid);
1290 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1291 return (EHOSTUNREACH);
1296 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1297 fib6_free_nh_ext(fibnum, &nh6);
1303 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1304 * and cached data in @ro_pmtu.
1305 * MTU from (successful) route lookup is saved (along with dst)
1306 * inside @ro_pmtu to avoid subsequent route lookups after packet
1307 * filter processing.
1309 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1310 * Returns 0 on success.
1313 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1314 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1315 int *alwaysfragp, u_int fibnum, u_int proto)
1317 struct nhop6_basic nh6;
1318 struct in6_addr kdst;
1320 struct sockaddr_in6 *sa6_dst;
1327 * Here ro_pmtu has final destination address, while
1328 * ro might represent immediate destination.
1329 * Use ro_pmtu destination since mtu might differ.
1331 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1332 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1333 ro_pmtu->ro_mtu = 0;
1335 if (ro_pmtu->ro_mtu == 0) {
1336 bzero(sa6_dst, sizeof(*sa6_dst));
1337 sa6_dst->sin6_family = AF_INET6;
1338 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1339 sa6_dst->sin6_addr = *dst;
1341 in6_splitscope(dst, &kdst, &scopeid);
1342 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1344 ro_pmtu->ro_mtu = nh6.nh_mtu;
1347 mtu = ro_pmtu->ro_mtu;
1351 mtu = ro_pmtu->ro_rt->rt_mtu;
1353 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1357 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1358 * hostcache data for @dst.
1359 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1361 * Returns 0 on success.
1364 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1365 u_long *mtup, int *alwaysfragp, u_int proto)
1373 struct in_conninfo inc;
1375 bzero(&inc, sizeof(inc));
1376 inc.inc_flags |= INC_ISIPV6;
1377 inc.inc6_faddr = *dst;
1379 ifmtu = IN6_LINKMTU(ifp);
1381 /* TCP is known to react to pmtu changes so skip hc */
1382 if (proto != IPPROTO_TCP)
1383 mtu = tcp_hc_getmtu(&inc);
1386 mtu = min(mtu, rt_mtu);
1391 else if (mtu < IPV6_MMTU) {
1393 * RFC2460 section 5, last paragraph:
1394 * if we record ICMPv6 too big message with
1395 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1396 * or smaller, with framgent header attached.
1397 * (fragment header is needed regardless from the
1398 * packet size, for translators to identify packets)
1404 mtu = IN6_LINKMTU(ifp);
1406 error = EHOSTUNREACH; /* XXX */
1410 *alwaysfragp = alwaysfrag;
1415 * IP6 socket option processing.
1418 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1420 int optdatalen, uproto;
1422 struct inpcb *in6p = sotoinpcb(so);
1424 int level, op, optname;
1428 uint32_t rss_bucket;
1433 * Don't use more than a quarter of mbuf clusters. N.B.:
1434 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1435 * on LP64 architectures, so cast to u_long to avoid undefined
1436 * behavior. ILP32 architectures cannot have nmbclusters
1437 * large enough to overflow for other reasons.
1439 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1441 level = sopt->sopt_level;
1442 op = sopt->sopt_dir;
1443 optname = sopt->sopt_name;
1444 optlen = sopt->sopt_valsize;
1448 uproto = (int)so->so_proto->pr_protocol;
1450 if (level != IPPROTO_IPV6) {
1453 if (sopt->sopt_level == SOL_SOCKET &&
1454 sopt->sopt_dir == SOPT_SET) {
1455 switch (sopt->sopt_name) {
1458 if ((so->so_options & SO_REUSEADDR) != 0)
1459 in6p->inp_flags2 |= INP_REUSEADDR;
1461 in6p->inp_flags2 &= ~INP_REUSEADDR;
1467 if ((so->so_options & SO_REUSEPORT) != 0)
1468 in6p->inp_flags2 |= INP_REUSEPORT;
1470 in6p->inp_flags2 &= ~INP_REUSEPORT;
1474 case SO_REUSEPORT_LB:
1476 if ((so->so_options & SO_REUSEPORT_LB) != 0)
1477 in6p->inp_flags2 |= INP_REUSEPORT_LB;
1479 in6p->inp_flags2 &= ~INP_REUSEPORT_LB;
1485 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1489 case SO_MAX_PACING_RATE:
1492 in6p->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1503 } else { /* level == IPPROTO_IPV6 */
1508 case IPV6_2292PKTOPTIONS:
1509 #ifdef IPV6_PKTOPTIONS
1510 case IPV6_PKTOPTIONS:
1515 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1516 printf("ip6_ctloutput: mbuf limit hit\n");
1521 error = soopt_getm(sopt, &m); /* XXX */
1524 error = soopt_mcopyin(sopt, m); /* XXX */
1527 error = ip6_pcbopts(&in6p->in6p_outputopts,
1529 m_freem(m); /* XXX */
1534 * Use of some Hop-by-Hop options or some
1535 * Destination options, might require special
1536 * privilege. That is, normal applications
1537 * (without special privilege) might be forbidden
1538 * from setting certain options in outgoing packets,
1539 * and might never see certain options in received
1540 * packets. [RFC 2292 Section 6]
1541 * KAME specific note:
1542 * KAME prevents non-privileged users from sending or
1543 * receiving ANY hbh/dst options in order to avoid
1544 * overhead of parsing options in the kernel.
1546 case IPV6_RECVHOPOPTS:
1547 case IPV6_RECVDSTOPTS:
1548 case IPV6_RECVRTHDRDSTOPTS:
1550 error = priv_check(td,
1551 PRIV_NETINET_SETHDROPTS);
1556 case IPV6_UNICAST_HOPS:
1559 case IPV6_RECVPKTINFO:
1560 case IPV6_RECVHOPLIMIT:
1561 case IPV6_RECVRTHDR:
1562 case IPV6_RECVPATHMTU:
1563 case IPV6_RECVTCLASS:
1564 case IPV6_RECVFLOWID:
1566 case IPV6_RECVRSSBUCKETID:
1569 case IPV6_AUTOFLOWLABEL:
1570 case IPV6_ORIGDSTADDR:
1572 case IPV6_BINDMULTI:
1574 case IPV6_RSS_LISTEN_BUCKET:
1576 if (optname == IPV6_BINDANY && td != NULL) {
1577 error = priv_check(td,
1578 PRIV_NETINET_BINDANY);
1583 if (optlen != sizeof(int)) {
1587 error = sooptcopyin(sopt, &optval,
1588 sizeof optval, sizeof optval);
1593 case IPV6_UNICAST_HOPS:
1594 if (optval < -1 || optval >= 256)
1597 /* -1 = kernel default */
1598 in6p->in6p_hops = optval;
1599 if ((in6p->inp_vflag &
1601 in6p->inp_ip_ttl = optval;
1604 #define OPTSET(bit) \
1608 in6p->inp_flags |= (bit); \
1610 in6p->inp_flags &= ~(bit); \
1611 INP_WUNLOCK(in6p); \
1612 } while (/*CONSTCOND*/ 0)
1613 #define OPTSET2292(bit) \
1616 in6p->inp_flags |= IN6P_RFC2292; \
1618 in6p->inp_flags |= (bit); \
1620 in6p->inp_flags &= ~(bit); \
1621 INP_WUNLOCK(in6p); \
1622 } while (/*CONSTCOND*/ 0)
1623 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1625 #define OPTSET2_N(bit, val) do { \
1627 in6p->inp_flags2 |= bit; \
1629 in6p->inp_flags2 &= ~bit; \
1631 #define OPTSET2(bit, val) do { \
1633 OPTSET2_N(bit, val); \
1634 INP_WUNLOCK(in6p); \
1636 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1637 #define OPTSET2292_EXCLUSIVE(bit) \
1640 if (OPTBIT(IN6P_RFC2292)) { \
1644 in6p->inp_flags |= (bit); \
1646 in6p->inp_flags &= ~(bit); \
1648 INP_WUNLOCK(in6p); \
1649 } while (/*CONSTCOND*/ 0)
1651 case IPV6_RECVPKTINFO:
1652 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1657 struct ip6_pktopts **optp;
1659 /* cannot mix with RFC2292 */
1660 if (OPTBIT(IN6P_RFC2292)) {
1665 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1667 return (ECONNRESET);
1669 optp = &in6p->in6p_outputopts;
1670 error = ip6_pcbopt(IPV6_HOPLIMIT,
1671 (u_char *)&optval, sizeof(optval),
1672 optp, (td != NULL) ? td->td_ucred :
1678 case IPV6_RECVHOPLIMIT:
1679 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1682 case IPV6_RECVHOPOPTS:
1683 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1686 case IPV6_RECVDSTOPTS:
1687 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1690 case IPV6_RECVRTHDRDSTOPTS:
1691 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1694 case IPV6_RECVRTHDR:
1695 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1698 case IPV6_RECVPATHMTU:
1700 * We ignore this option for TCP
1702 * (RFC3542 leaves this case
1705 if (uproto != IPPROTO_TCP)
1709 case IPV6_RECVFLOWID:
1710 OPTSET2(INP_RECVFLOWID, optval);
1714 case IPV6_RECVRSSBUCKETID:
1715 OPTSET2(INP_RECVRSSBUCKETID, optval);
1721 * make setsockopt(IPV6_V6ONLY)
1722 * available only prior to bind(2).
1723 * see ipng mailing list, Jun 22 2001.
1725 if (in6p->inp_lport ||
1726 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1730 OPTSET(IN6P_IPV6_V6ONLY);
1732 in6p->inp_vflag &= ~INP_IPV4;
1734 in6p->inp_vflag |= INP_IPV4;
1736 case IPV6_RECVTCLASS:
1737 /* cannot mix with RFC2292 XXX */
1738 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1740 case IPV6_AUTOFLOWLABEL:
1741 OPTSET(IN6P_AUTOFLOWLABEL);
1744 case IPV6_ORIGDSTADDR:
1745 OPTSET2(INP_ORIGDSTADDR, optval);
1748 OPTSET(INP_BINDANY);
1751 case IPV6_BINDMULTI:
1752 OPTSET2(INP_BINDMULTI, optval);
1755 case IPV6_RSS_LISTEN_BUCKET:
1756 if ((optval >= 0) &&
1757 (optval < rss_getnumbuckets())) {
1759 in6p->inp_rss_listen_bucket = optval;
1760 OPTSET2_N(INP_RSS_BUCKET_SET, 1);
1772 case IPV6_USE_MIN_MTU:
1773 case IPV6_PREFER_TEMPADDR:
1774 if (optlen != sizeof(optval)) {
1778 error = sooptcopyin(sopt, &optval,
1779 sizeof optval, sizeof optval);
1783 struct ip6_pktopts **optp;
1785 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1787 return (ECONNRESET);
1789 optp = &in6p->in6p_outputopts;
1790 error = ip6_pcbopt(optname,
1791 (u_char *)&optval, sizeof(optval),
1792 optp, (td != NULL) ? td->td_ucred :
1798 case IPV6_2292PKTINFO:
1799 case IPV6_2292HOPLIMIT:
1800 case IPV6_2292HOPOPTS:
1801 case IPV6_2292DSTOPTS:
1802 case IPV6_2292RTHDR:
1804 if (optlen != sizeof(int)) {
1808 error = sooptcopyin(sopt, &optval,
1809 sizeof optval, sizeof optval);
1813 case IPV6_2292PKTINFO:
1814 OPTSET2292(IN6P_PKTINFO);
1816 case IPV6_2292HOPLIMIT:
1817 OPTSET2292(IN6P_HOPLIMIT);
1819 case IPV6_2292HOPOPTS:
1821 * Check super-user privilege.
1822 * See comments for IPV6_RECVHOPOPTS.
1825 error = priv_check(td,
1826 PRIV_NETINET_SETHDROPTS);
1830 OPTSET2292(IN6P_HOPOPTS);
1832 case IPV6_2292DSTOPTS:
1834 error = priv_check(td,
1835 PRIV_NETINET_SETHDROPTS);
1839 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1841 case IPV6_2292RTHDR:
1842 OPTSET2292(IN6P_RTHDR);
1850 case IPV6_RTHDRDSTOPTS:
1853 /* new advanced API (RFC3542) */
1855 u_char optbuf_storage[MCLBYTES];
1857 struct ip6_pktopts **optp;
1859 /* cannot mix with RFC2292 */
1860 if (OPTBIT(IN6P_RFC2292)) {
1866 * We only ensure valsize is not too large
1867 * here. Further validation will be done
1870 error = sooptcopyin(sopt, optbuf_storage,
1871 sizeof(optbuf_storage), 0);
1874 optlen = sopt->sopt_valsize;
1875 optbuf = optbuf_storage;
1877 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1879 return (ECONNRESET);
1881 optp = &in6p->in6p_outputopts;
1882 error = ip6_pcbopt(optname, optbuf, optlen,
1883 optp, (td != NULL) ? td->td_ucred : NULL,
1890 case IPV6_MULTICAST_IF:
1891 case IPV6_MULTICAST_HOPS:
1892 case IPV6_MULTICAST_LOOP:
1893 case IPV6_JOIN_GROUP:
1894 case IPV6_LEAVE_GROUP:
1896 case MCAST_BLOCK_SOURCE:
1897 case MCAST_UNBLOCK_SOURCE:
1898 case MCAST_JOIN_GROUP:
1899 case MCAST_LEAVE_GROUP:
1900 case MCAST_JOIN_SOURCE_GROUP:
1901 case MCAST_LEAVE_SOURCE_GROUP:
1902 error = ip6_setmoptions(in6p, sopt);
1905 case IPV6_PORTRANGE:
1906 error = sooptcopyin(sopt, &optval,
1907 sizeof optval, sizeof optval);
1913 case IPV6_PORTRANGE_DEFAULT:
1914 in6p->inp_flags &= ~(INP_LOWPORT);
1915 in6p->inp_flags &= ~(INP_HIGHPORT);
1918 case IPV6_PORTRANGE_HIGH:
1919 in6p->inp_flags &= ~(INP_LOWPORT);
1920 in6p->inp_flags |= INP_HIGHPORT;
1923 case IPV6_PORTRANGE_LOW:
1924 in6p->inp_flags &= ~(INP_HIGHPORT);
1925 in6p->inp_flags |= INP_LOWPORT;
1935 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1936 case IPV6_IPSEC_POLICY:
1937 if (IPSEC_ENABLED(ipv6)) {
1938 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
1945 error = ENOPROTOOPT;
1953 case IPV6_2292PKTOPTIONS:
1954 #ifdef IPV6_PKTOPTIONS
1955 case IPV6_PKTOPTIONS:
1958 * RFC3542 (effectively) deprecated the
1959 * semantics of the 2292-style pktoptions.
1960 * Since it was not reliable in nature (i.e.,
1961 * applications had to expect the lack of some
1962 * information after all), it would make sense
1963 * to simplify this part by always returning
1966 sopt->sopt_valsize = 0;
1969 case IPV6_RECVHOPOPTS:
1970 case IPV6_RECVDSTOPTS:
1971 case IPV6_RECVRTHDRDSTOPTS:
1972 case IPV6_UNICAST_HOPS:
1973 case IPV6_RECVPKTINFO:
1974 case IPV6_RECVHOPLIMIT:
1975 case IPV6_RECVRTHDR:
1976 case IPV6_RECVPATHMTU:
1979 case IPV6_PORTRANGE:
1980 case IPV6_RECVTCLASS:
1981 case IPV6_AUTOFLOWLABEL:
1985 case IPV6_RECVFLOWID:
1987 case IPV6_RSSBUCKETID:
1988 case IPV6_RECVRSSBUCKETID:
1990 case IPV6_BINDMULTI:
1993 case IPV6_RECVHOPOPTS:
1994 optval = OPTBIT(IN6P_HOPOPTS);
1997 case IPV6_RECVDSTOPTS:
1998 optval = OPTBIT(IN6P_DSTOPTS);
2001 case IPV6_RECVRTHDRDSTOPTS:
2002 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2005 case IPV6_UNICAST_HOPS:
2006 optval = in6p->in6p_hops;
2009 case IPV6_RECVPKTINFO:
2010 optval = OPTBIT(IN6P_PKTINFO);
2013 case IPV6_RECVHOPLIMIT:
2014 optval = OPTBIT(IN6P_HOPLIMIT);
2017 case IPV6_RECVRTHDR:
2018 optval = OPTBIT(IN6P_RTHDR);
2021 case IPV6_RECVPATHMTU:
2022 optval = OPTBIT(IN6P_MTU);
2026 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2029 case IPV6_PORTRANGE:
2032 flags = in6p->inp_flags;
2033 if (flags & INP_HIGHPORT)
2034 optval = IPV6_PORTRANGE_HIGH;
2035 else if (flags & INP_LOWPORT)
2036 optval = IPV6_PORTRANGE_LOW;
2041 case IPV6_RECVTCLASS:
2042 optval = OPTBIT(IN6P_TCLASS);
2045 case IPV6_AUTOFLOWLABEL:
2046 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2049 case IPV6_ORIGDSTADDR:
2050 optval = OPTBIT2(INP_ORIGDSTADDR);
2054 optval = OPTBIT(INP_BINDANY);
2058 optval = in6p->inp_flowid;
2062 optval = in6p->inp_flowtype;
2065 case IPV6_RECVFLOWID:
2066 optval = OPTBIT2(INP_RECVFLOWID);
2069 case IPV6_RSSBUCKETID:
2071 rss_hash2bucket(in6p->inp_flowid,
2075 optval = rss_bucket;
2080 case IPV6_RECVRSSBUCKETID:
2081 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2085 case IPV6_BINDMULTI:
2086 optval = OPTBIT2(INP_BINDMULTI);
2092 error = sooptcopyout(sopt, &optval,
2099 struct ip6_mtuinfo mtuinfo;
2100 struct in6_addr addr;
2102 if (!(so->so_state & SS_ISCONNECTED))
2105 * XXX: we dot not consider the case of source
2106 * routing, or optional information to specify
2107 * the outgoing interface.
2108 * Copy faddr out of in6p to avoid holding lock
2109 * on inp during route lookup.
2112 bcopy(&in6p->in6p_faddr, &addr, sizeof(addr));
2114 error = ip6_getpmtu_ctl(so->so_fibnum,
2118 if (pmtu > IPV6_MAXPACKET)
2119 pmtu = IPV6_MAXPACKET;
2121 bzero(&mtuinfo, sizeof(mtuinfo));
2122 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2123 optdata = (void *)&mtuinfo;
2124 optdatalen = sizeof(mtuinfo);
2125 error = sooptcopyout(sopt, optdata,
2130 case IPV6_2292PKTINFO:
2131 case IPV6_2292HOPLIMIT:
2132 case IPV6_2292HOPOPTS:
2133 case IPV6_2292RTHDR:
2134 case IPV6_2292DSTOPTS:
2136 case IPV6_2292PKTINFO:
2137 optval = OPTBIT(IN6P_PKTINFO);
2139 case IPV6_2292HOPLIMIT:
2140 optval = OPTBIT(IN6P_HOPLIMIT);
2142 case IPV6_2292HOPOPTS:
2143 optval = OPTBIT(IN6P_HOPOPTS);
2145 case IPV6_2292RTHDR:
2146 optval = OPTBIT(IN6P_RTHDR);
2148 case IPV6_2292DSTOPTS:
2149 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2152 error = sooptcopyout(sopt, &optval,
2159 case IPV6_RTHDRDSTOPTS:
2163 case IPV6_USE_MIN_MTU:
2164 case IPV6_PREFER_TEMPADDR:
2165 error = ip6_getpcbopt(in6p, optname, sopt);
2168 case IPV6_MULTICAST_IF:
2169 case IPV6_MULTICAST_HOPS:
2170 case IPV6_MULTICAST_LOOP:
2172 error = ip6_getmoptions(in6p, sopt);
2175 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2176 case IPV6_IPSEC_POLICY:
2177 if (IPSEC_ENABLED(ipv6)) {
2178 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
2184 error = ENOPROTOOPT;
2194 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2196 int error = 0, optval, optlen;
2197 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2198 struct inpcb *in6p = sotoinpcb(so);
2199 int level, op, optname;
2201 level = sopt->sopt_level;
2202 op = sopt->sopt_dir;
2203 optname = sopt->sopt_name;
2204 optlen = sopt->sopt_valsize;
2206 if (level != IPPROTO_IPV6) {
2213 * For ICMPv6 sockets, no modification allowed for checksum
2214 * offset, permit "no change" values to help existing apps.
2216 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2217 * for an ICMPv6 socket will fail."
2218 * The current behavior does not meet RFC3542.
2222 if (optlen != sizeof(int)) {
2226 error = sooptcopyin(sopt, &optval, sizeof(optval),
2230 if (optval < -1 || (optval % 2) != 0) {
2232 * The API assumes non-negative even offset
2233 * values or -1 as a special value.
2236 } else if (so->so_proto->pr_protocol ==
2238 if (optval != icmp6off)
2241 in6p->in6p_cksum = optval;
2245 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2248 optval = in6p->in6p_cksum;
2250 error = sooptcopyout(sopt, &optval, sizeof(optval));
2260 error = ENOPROTOOPT;
2268 * Set up IP6 options in pcb for insertion in output packets or
2269 * specifying behavior of outgoing packets.
2272 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2273 struct socket *so, struct sockopt *sopt)
2275 struct ip6_pktopts *opt = *pktopt;
2277 struct thread *td = sopt->sopt_td;
2279 /* turn off any old options. */
2282 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2283 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2284 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2285 printf("ip6_pcbopts: all specified options are cleared.\n");
2287 ip6_clearpktopts(opt, -1);
2289 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2292 if (!m || m->m_len == 0) {
2294 * Only turning off any previous options, regardless of
2295 * whether the opt is just created or given.
2297 free(opt, M_IP6OPT);
2301 /* set options specified by user. */
2302 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2303 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2304 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2305 free(opt, M_IP6OPT);
2313 * initialize ip6_pktopts. beware that there are non-zero default values in
2317 ip6_initpktopts(struct ip6_pktopts *opt)
2320 bzero(opt, sizeof(*opt));
2321 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2322 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2323 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2324 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2328 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2329 struct ucred *cred, int uproto)
2331 struct ip6_pktopts *opt;
2333 if (*pktopt == NULL) {
2334 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2336 if (*pktopt == NULL)
2338 ip6_initpktopts(*pktopt);
2342 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2345 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2346 if (pktopt && pktopt->field) { \
2347 INP_RUNLOCK(in6p); \
2348 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2349 malloc_optdata = true; \
2351 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
2352 INP_RUNLOCK(in6p); \
2353 free(optdata, M_TEMP); \
2354 return (ECONNRESET); \
2356 pktopt = in6p->in6p_outputopts; \
2357 if (pktopt && pktopt->field) { \
2358 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2359 bcopy(&pktopt->field, optdata, optdatalen); \
2361 free(optdata, M_TEMP); \
2363 malloc_optdata = false; \
2368 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2369 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2371 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2372 pktopt->field->sa_len)
2375 ip6_getpcbopt(struct inpcb *in6p, int optname, struct sockopt *sopt)
2377 void *optdata = NULL;
2378 bool malloc_optdata = false;
2381 struct in6_pktinfo null_pktinfo;
2382 int deftclass = 0, on;
2383 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2384 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2385 struct ip6_pktopts *pktopt;
2388 pktopt = in6p->in6p_outputopts;
2392 optdata = (void *)&null_pktinfo;
2393 if (pktopt && pktopt->ip6po_pktinfo) {
2394 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2395 sizeof(null_pktinfo));
2396 in6_clearscope(&null_pktinfo.ipi6_addr);
2398 /* XXX: we don't have to do this every time... */
2399 bzero(&null_pktinfo, sizeof(null_pktinfo));
2401 optdatalen = sizeof(struct in6_pktinfo);
2404 if (pktopt && pktopt->ip6po_tclass >= 0)
2405 deftclass = pktopt->ip6po_tclass;
2406 optdata = (void *)&deftclass;
2407 optdatalen = sizeof(int);
2410 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2413 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2415 case IPV6_RTHDRDSTOPTS:
2416 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2419 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2422 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2424 case IPV6_USE_MIN_MTU:
2426 defminmtu = pktopt->ip6po_minmtu;
2427 optdata = (void *)&defminmtu;
2428 optdatalen = sizeof(int);
2431 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2435 optdata = (void *)&on;
2436 optdatalen = sizeof(on);
2438 case IPV6_PREFER_TEMPADDR:
2440 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2441 optdata = (void *)&defpreftemp;
2442 optdatalen = sizeof(int);
2444 default: /* should not happen */
2446 panic("ip6_getpcbopt: unexpected option\n");
2449 return (ENOPROTOOPT);
2453 error = sooptcopyout(sopt, optdata, optdatalen);
2455 free(optdata, M_TEMP);
2461 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2466 if (optname == -1 || optname == IPV6_PKTINFO) {
2467 if (pktopt->ip6po_pktinfo)
2468 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2469 pktopt->ip6po_pktinfo = NULL;
2471 if (optname == -1 || optname == IPV6_HOPLIMIT)
2472 pktopt->ip6po_hlim = -1;
2473 if (optname == -1 || optname == IPV6_TCLASS)
2474 pktopt->ip6po_tclass = -1;
2475 if (optname == -1 || optname == IPV6_NEXTHOP) {
2476 if (pktopt->ip6po_nextroute.ro_rt) {
2477 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2478 pktopt->ip6po_nextroute.ro_rt = NULL;
2480 if (pktopt->ip6po_nexthop)
2481 free(pktopt->ip6po_nexthop, M_IP6OPT);
2482 pktopt->ip6po_nexthop = NULL;
2484 if (optname == -1 || optname == IPV6_HOPOPTS) {
2485 if (pktopt->ip6po_hbh)
2486 free(pktopt->ip6po_hbh, M_IP6OPT);
2487 pktopt->ip6po_hbh = NULL;
2489 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2490 if (pktopt->ip6po_dest1)
2491 free(pktopt->ip6po_dest1, M_IP6OPT);
2492 pktopt->ip6po_dest1 = NULL;
2494 if (optname == -1 || optname == IPV6_RTHDR) {
2495 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2496 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2497 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2498 if (pktopt->ip6po_route.ro_rt) {
2499 RTFREE(pktopt->ip6po_route.ro_rt);
2500 pktopt->ip6po_route.ro_rt = NULL;
2503 if (optname == -1 || optname == IPV6_DSTOPTS) {
2504 if (pktopt->ip6po_dest2)
2505 free(pktopt->ip6po_dest2, M_IP6OPT);
2506 pktopt->ip6po_dest2 = NULL;
2510 #define PKTOPT_EXTHDRCPY(type) \
2513 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2514 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2515 if (dst->type == NULL)\
2517 bcopy(src->type, dst->type, hlen);\
2519 } while (/*CONSTCOND*/ 0)
2522 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2524 if (dst == NULL || src == NULL) {
2525 printf("ip6_clearpktopts: invalid argument\n");
2529 dst->ip6po_hlim = src->ip6po_hlim;
2530 dst->ip6po_tclass = src->ip6po_tclass;
2531 dst->ip6po_flags = src->ip6po_flags;
2532 dst->ip6po_minmtu = src->ip6po_minmtu;
2533 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2534 if (src->ip6po_pktinfo) {
2535 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2537 if (dst->ip6po_pktinfo == NULL)
2539 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2541 if (src->ip6po_nexthop) {
2542 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2544 if (dst->ip6po_nexthop == NULL)
2546 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2547 src->ip6po_nexthop->sa_len);
2549 PKTOPT_EXTHDRCPY(ip6po_hbh);
2550 PKTOPT_EXTHDRCPY(ip6po_dest1);
2551 PKTOPT_EXTHDRCPY(ip6po_dest2);
2552 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2556 ip6_clearpktopts(dst, -1);
2559 #undef PKTOPT_EXTHDRCPY
2561 struct ip6_pktopts *
2562 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2565 struct ip6_pktopts *dst;
2567 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2570 ip6_initpktopts(dst);
2572 if ((error = copypktopts(dst, src, canwait)) != 0) {
2573 free(dst, M_IP6OPT);
2581 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2586 ip6_clearpktopts(pktopt, -1);
2588 free(pktopt, M_IP6OPT);
2592 * Set IPv6 outgoing packet options based on advanced API.
2595 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2596 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2598 struct cmsghdr *cm = NULL;
2600 if (control == NULL || opt == NULL)
2603 ip6_initpktopts(opt);
2608 * If stickyopt is provided, make a local copy of the options
2609 * for this particular packet, then override them by ancillary
2611 * XXX: copypktopts() does not copy the cached route to a next
2612 * hop (if any). This is not very good in terms of efficiency,
2613 * but we can allow this since this option should be rarely
2616 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2621 * XXX: Currently, we assume all the optional information is stored
2624 if (control->m_next)
2627 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2628 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2631 if (control->m_len < CMSG_LEN(0))
2634 cm = mtod(control, struct cmsghdr *);
2635 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2637 if (cm->cmsg_level != IPPROTO_IPV6)
2640 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2641 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2650 * Set a particular packet option, as a sticky option or an ancillary data
2651 * item. "len" can be 0 only when it's a sticky option.
2652 * We have 4 cases of combination of "sticky" and "cmsg":
2653 * "sticky=0, cmsg=0": impossible
2654 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2655 * "sticky=1, cmsg=0": RFC3542 socket option
2656 * "sticky=1, cmsg=1": RFC2292 socket option
2659 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2660 struct ucred *cred, int sticky, int cmsg, int uproto)
2662 int minmtupolicy, preftemp;
2665 if (!sticky && !cmsg) {
2667 printf("ip6_setpktopt: impossible case\n");
2673 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2674 * not be specified in the context of RFC3542. Conversely,
2675 * RFC3542 types should not be specified in the context of RFC2292.
2679 case IPV6_2292PKTINFO:
2680 case IPV6_2292HOPLIMIT:
2681 case IPV6_2292NEXTHOP:
2682 case IPV6_2292HOPOPTS:
2683 case IPV6_2292DSTOPTS:
2684 case IPV6_2292RTHDR:
2685 case IPV6_2292PKTOPTIONS:
2686 return (ENOPROTOOPT);
2689 if (sticky && cmsg) {
2696 case IPV6_RTHDRDSTOPTS:
2698 case IPV6_USE_MIN_MTU:
2701 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2702 return (ENOPROTOOPT);
2707 case IPV6_2292PKTINFO:
2710 struct ifnet *ifp = NULL;
2711 struct in6_pktinfo *pktinfo;
2713 if (len != sizeof(struct in6_pktinfo))
2716 pktinfo = (struct in6_pktinfo *)buf;
2719 * An application can clear any sticky IPV6_PKTINFO option by
2720 * doing a "regular" setsockopt with ipi6_addr being
2721 * in6addr_any and ipi6_ifindex being zero.
2722 * [RFC 3542, Section 6]
2724 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2725 pktinfo->ipi6_ifindex == 0 &&
2726 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2727 ip6_clearpktopts(opt, optname);
2731 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2732 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2735 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2737 /* validate the interface index if specified. */
2738 if (pktinfo->ipi6_ifindex > V_if_index)
2740 if (pktinfo->ipi6_ifindex) {
2741 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2745 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2746 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2750 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2751 struct in6_ifaddr *ia;
2753 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2754 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2756 return (EADDRNOTAVAIL);
2757 ifa_free(&ia->ia_ifa);
2760 * We store the address anyway, and let in6_selectsrc()
2761 * validate the specified address. This is because ipi6_addr
2762 * may not have enough information about its scope zone, and
2763 * we may need additional information (such as outgoing
2764 * interface or the scope zone of a destination address) to
2765 * disambiguate the scope.
2766 * XXX: the delay of the validation may confuse the
2767 * application when it is used as a sticky option.
2769 if (opt->ip6po_pktinfo == NULL) {
2770 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2771 M_IP6OPT, M_NOWAIT);
2772 if (opt->ip6po_pktinfo == NULL)
2775 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2779 case IPV6_2292HOPLIMIT:
2785 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2786 * to simplify the ordering among hoplimit options.
2788 if (optname == IPV6_HOPLIMIT && sticky)
2789 return (ENOPROTOOPT);
2791 if (len != sizeof(int))
2794 if (*hlimp < -1 || *hlimp > 255)
2797 opt->ip6po_hlim = *hlimp;
2805 if (len != sizeof(int))
2807 tclass = *(int *)buf;
2808 if (tclass < -1 || tclass > 255)
2811 opt->ip6po_tclass = tclass;
2815 case IPV6_2292NEXTHOP:
2818 error = priv_check_cred(cred,
2819 PRIV_NETINET_SETHDROPTS, 0);
2824 if (len == 0) { /* just remove the option */
2825 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2829 /* check if cmsg_len is large enough for sa_len */
2830 if (len < sizeof(struct sockaddr) || len < *buf)
2833 switch (((struct sockaddr *)buf)->sa_family) {
2836 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2839 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2842 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2843 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2846 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2852 case AF_LINK: /* should eventually be supported */
2854 return (EAFNOSUPPORT);
2857 /* turn off the previous option, then set the new option. */
2858 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2859 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2860 if (opt->ip6po_nexthop == NULL)
2862 bcopy(buf, opt->ip6po_nexthop, *buf);
2865 case IPV6_2292HOPOPTS:
2868 struct ip6_hbh *hbh;
2872 * XXX: We don't allow a non-privileged user to set ANY HbH
2873 * options, since per-option restriction has too much
2877 error = priv_check_cred(cred,
2878 PRIV_NETINET_SETHDROPTS, 0);
2884 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2885 break; /* just remove the option */
2888 /* message length validation */
2889 if (len < sizeof(struct ip6_hbh))
2891 hbh = (struct ip6_hbh *)buf;
2892 hbhlen = (hbh->ip6h_len + 1) << 3;
2896 /* turn off the previous option, then set the new option. */
2897 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2898 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2899 if (opt->ip6po_hbh == NULL)
2901 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2906 case IPV6_2292DSTOPTS:
2908 case IPV6_RTHDRDSTOPTS:
2910 struct ip6_dest *dest, **newdest = NULL;
2913 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2914 error = priv_check_cred(cred,
2915 PRIV_NETINET_SETHDROPTS, 0);
2921 ip6_clearpktopts(opt, optname);
2922 break; /* just remove the option */
2925 /* message length validation */
2926 if (len < sizeof(struct ip6_dest))
2928 dest = (struct ip6_dest *)buf;
2929 destlen = (dest->ip6d_len + 1) << 3;
2934 * Determine the position that the destination options header
2935 * should be inserted; before or after the routing header.
2938 case IPV6_2292DSTOPTS:
2940 * The old advacned API is ambiguous on this point.
2941 * Our approach is to determine the position based
2942 * according to the existence of a routing header.
2943 * Note, however, that this depends on the order of the
2944 * extension headers in the ancillary data; the 1st
2945 * part of the destination options header must appear
2946 * before the routing header in the ancillary data,
2948 * RFC3542 solved the ambiguity by introducing
2949 * separate ancillary data or option types.
2951 if (opt->ip6po_rthdr == NULL)
2952 newdest = &opt->ip6po_dest1;
2954 newdest = &opt->ip6po_dest2;
2956 case IPV6_RTHDRDSTOPTS:
2957 newdest = &opt->ip6po_dest1;
2960 newdest = &opt->ip6po_dest2;
2964 /* turn off the previous option, then set the new option. */
2965 ip6_clearpktopts(opt, optname);
2966 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2967 if (*newdest == NULL)
2969 bcopy(dest, *newdest, destlen);
2974 case IPV6_2292RTHDR:
2977 struct ip6_rthdr *rth;
2981 ip6_clearpktopts(opt, IPV6_RTHDR);
2982 break; /* just remove the option */
2985 /* message length validation */
2986 if (len < sizeof(struct ip6_rthdr))
2988 rth = (struct ip6_rthdr *)buf;
2989 rthlen = (rth->ip6r_len + 1) << 3;
2993 switch (rth->ip6r_type) {
2994 case IPV6_RTHDR_TYPE_0:
2995 if (rth->ip6r_len == 0) /* must contain one addr */
2997 if (rth->ip6r_len % 2) /* length must be even */
2999 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3003 return (EINVAL); /* not supported */
3006 /* turn off the previous option */
3007 ip6_clearpktopts(opt, IPV6_RTHDR);
3008 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3009 if (opt->ip6po_rthdr == NULL)
3011 bcopy(rth, opt->ip6po_rthdr, rthlen);
3016 case IPV6_USE_MIN_MTU:
3017 if (len != sizeof(int))
3019 minmtupolicy = *(int *)buf;
3020 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3021 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3022 minmtupolicy != IP6PO_MINMTU_ALL) {
3025 opt->ip6po_minmtu = minmtupolicy;
3029 if (len != sizeof(int))
3032 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3034 * we ignore this option for TCP sockets.
3035 * (RFC3542 leaves this case unspecified.)
3037 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3039 opt->ip6po_flags |= IP6PO_DONTFRAG;
3042 case IPV6_PREFER_TEMPADDR:
3043 if (len != sizeof(int))
3045 preftemp = *(int *)buf;
3046 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3047 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3048 preftemp != IP6PO_TEMPADDR_PREFER) {
3051 opt->ip6po_prefer_tempaddr = preftemp;
3055 return (ENOPROTOOPT);
3056 } /* end of switch */
3062 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3063 * packet to the input queue of a specified interface. Note that this
3064 * calls the output routine of the loopback "driver", but with an interface
3065 * pointer that might NOT be &loif -- easier than replicating that code here.
3068 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3071 struct ip6_hdr *ip6;
3073 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3078 * Make sure to deep-copy IPv6 header portion in case the data
3079 * is in an mbuf cluster, so that we can safely override the IPv6
3080 * header portion later.
3082 if (!M_WRITABLE(copym) ||
3083 copym->m_len < sizeof(struct ip6_hdr)) {
3084 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3088 ip6 = mtod(copym, struct ip6_hdr *);
3090 * clear embedded scope identifiers if necessary.
3091 * in6_clearscope will touch the addresses only when necessary.
3093 in6_clearscope(&ip6->ip6_src);
3094 in6_clearscope(&ip6->ip6_dst);
3095 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3096 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3098 copym->m_pkthdr.csum_data = 0xffff;
3100 if_simloop(ifp, copym, AF_INET6, 0);
3104 * Chop IPv6 header off from the payload.
3107 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3110 struct ip6_hdr *ip6;
3112 ip6 = mtod(m, struct ip6_hdr *);
3113 if (m->m_len > sizeof(*ip6)) {
3114 mh = m_gethdr(M_NOWAIT, MT_DATA);
3119 m_move_pkthdr(mh, m);
3120 M_ALIGN(mh, sizeof(*ip6));
3121 m->m_len -= sizeof(*ip6);
3122 m->m_data += sizeof(*ip6);
3125 m->m_len = sizeof(*ip6);
3126 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3128 exthdrs->ip6e_ip6 = m;
3133 * Compute IPv6 extension header length.
3136 ip6_optlen(struct inpcb *in6p)
3140 if (!in6p->in6p_outputopts)
3145 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3147 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3148 if (in6p->in6p_outputopts->ip6po_rthdr)
3149 /* dest1 is valid with rthdr only */
3150 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3151 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3152 len += elen(in6p->in6p_outputopts->ip6po_dest2);