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);
231 m->m_flags = m0->m_flags & M_COPYFLAGS;
233 mnext = &m->m_nextpkt;
234 m->m_data += max_linkhdr;
235 mhip6 = mtod(m, struct ip6_hdr *);
237 m->m_len = sizeof(*mhip6);
238 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
240 IP6STAT_INC(ip6s_odropped);
243 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
244 if (off + fraglen >= tlen)
245 fraglen = tlen - off;
247 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
248 mhip6->ip6_plen = htons((u_short)(fraglen + hlen +
249 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
250 if ((m_frgpart = m_copym(m0, off, fraglen, M_NOWAIT)) == NULL) {
251 IP6STAT_INC(ip6s_odropped);
255 m->m_pkthdr.len = fraglen + hlen + sizeof(*ip6f);
256 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
257 m->m_pkthdr.rcvif = NULL;
258 ip6f->ip6f_reserved = 0;
259 ip6f->ip6f_ident = id;
260 ip6f->ip6f_nxt = nextproto;
261 IP6STAT_INC(ip6s_ofragments);
262 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
269 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
270 * header (with pri, len, nxt, hlim, src, dst).
271 * This function may modify ver and hlim only.
272 * The mbuf chain containing the packet will be freed.
273 * The mbuf opt, if present, will not be freed.
274 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
275 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
276 * then result of route lookup is stored in ro->ro_rt.
278 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
279 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
282 * ifpp - XXX: just for statistics
285 * XXX TODO: no flowid is assigned for outbound flows?
288 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
289 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
290 struct ifnet **ifpp, struct inpcb *inp)
293 struct ifnet *ifp, *origifp;
295 struct mbuf *mprev = NULL;
297 struct route_in6 ip6route;
298 struct rtentry *rt = NULL;
299 struct sockaddr_in6 *dst, src_sa, dst_sa;
300 struct in6_addr odst;
302 struct in6_ifaddr *ia = NULL;
304 int alwaysfrag, dontfrag;
305 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
306 struct ip6_exthdrs exthdrs;
307 struct in6_addr src0, dst0;
309 struct route_in6 *ro_pmtu = NULL;
314 struct m_tag *fwd_tag = NULL;
318 INP_LOCK_ASSERT(inp);
319 M_SETFIB(m, inp->inp_inc.inc_fibnum);
320 if ((flags & IP_NODEFAULTFLOWID) == 0) {
321 /* unconditionally set flowid */
322 m->m_pkthdr.flowid = inp->inp_flowid;
323 M_HASHTYPE_SET(m, inp->inp_flowtype);
326 m->m_pkthdr.numa_domain = inp->inp_numa_domain;
330 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
332 * IPSec checking which handles several cases.
333 * FAST IPSEC: We re-injected the packet.
334 * XXX: need scope argument.
336 if (IPSEC_ENABLED(ipv6)) {
337 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
338 if (error == EINPROGRESS)
345 bzero(&exthdrs, sizeof(exthdrs));
347 /* Hop-by-Hop options header */
348 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
349 /* Destination options header(1st part) */
350 if (opt->ip6po_rthdr) {
352 * Destination options header(1st part)
353 * This only makes sense with a routing header.
354 * See Section 9.2 of RFC 3542.
355 * Disabling this part just for MIP6 convenience is
356 * a bad idea. We need to think carefully about a
357 * way to make the advanced API coexist with MIP6
358 * options, which might automatically be inserted in
361 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
364 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
365 /* Destination options header(2nd part) */
366 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
370 * Calculate the total length of the extension header chain.
371 * Keep the length of the unfragmentable part for fragmentation.
374 if (exthdrs.ip6e_hbh)
375 optlen += exthdrs.ip6e_hbh->m_len;
376 if (exthdrs.ip6e_dest1)
377 optlen += exthdrs.ip6e_dest1->m_len;
378 if (exthdrs.ip6e_rthdr)
379 optlen += exthdrs.ip6e_rthdr->m_len;
380 unfragpartlen = optlen + sizeof(struct ip6_hdr);
382 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
383 if (exthdrs.ip6e_dest2)
384 optlen += exthdrs.ip6e_dest2->m_len;
387 * If there is at least one extension header,
388 * separate IP6 header from the payload.
390 if (optlen && !hdrsplit) {
391 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
395 m = exthdrs.ip6e_ip6;
399 ip6 = mtod(m, struct ip6_hdr *);
401 /* adjust mbuf packet header length */
402 m->m_pkthdr.len += optlen;
403 plen = m->m_pkthdr.len - sizeof(*ip6);
405 /* If this is a jumbo payload, insert a jumbo payload option. */
406 if (plen > IPV6_MAXPACKET) {
408 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
412 m = exthdrs.ip6e_ip6;
416 ip6 = mtod(m, struct ip6_hdr *);
417 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
421 ip6->ip6_plen = htons(plen);
424 * Concatenate headers and fill in next header fields.
425 * Here we have, on "m"
427 * and we insert headers accordingly. Finally, we should be getting:
428 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
430 * during the header composing process, "m" points to IPv6 header.
431 * "mprev" points to an extension header prior to esp.
433 u_char *nexthdrp = &ip6->ip6_nxt;
437 * we treat dest2 specially. this makes IPsec processing
438 * much easier. the goal here is to make mprev point the
439 * mbuf prior to dest2.
441 * result: IPv6 dest2 payload
442 * m and mprev will point to IPv6 header.
444 if (exthdrs.ip6e_dest2) {
446 panic("assumption failed: hdr not split");
447 exthdrs.ip6e_dest2->m_next = m->m_next;
448 m->m_next = exthdrs.ip6e_dest2;
449 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
450 ip6->ip6_nxt = IPPROTO_DSTOPTS;
454 * result: IPv6 hbh dest1 rthdr dest2 payload
455 * m will point to IPv6 header. mprev will point to the
456 * extension header prior to dest2 (rthdr in the above case).
458 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
459 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
461 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
465 * If there is a routing header, discard the packet.
467 if (exthdrs.ip6e_rthdr) {
472 /* Source address validation */
473 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
474 (flags & IPV6_UNSPECSRC) == 0) {
476 IP6STAT_INC(ip6s_badscope);
479 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
481 IP6STAT_INC(ip6s_badscope);
485 IP6STAT_INC(ip6s_localout);
492 bzero((caddr_t)ro, sizeof(*ro));
495 if (opt && opt->ip6po_rthdr)
496 ro = &opt->ip6po_route;
497 dst = (struct sockaddr_in6 *)&ro->ro_dst;
498 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
501 * if specified, try to fill in the traffic class field.
502 * do not override if a non-zero value is already set.
503 * we check the diffserv field and the ecn field separately.
505 if (opt && opt->ip6po_tclass >= 0) {
508 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
510 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
513 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
516 /* fill in or override the hop limit field, if necessary. */
517 if (opt && opt->ip6po_hlim != -1)
518 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
519 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
521 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
523 ip6->ip6_hlim = V_ip6_defmcasthlim;
526 * Validate route against routing table additions;
527 * a better/more specific route might have been added.
528 * Make sure address family is set in route.
531 ro->ro_dst.sin6_family = AF_INET6;
532 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
534 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
535 ro->ro_dst.sin6_family == AF_INET6 &&
536 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
538 ifp = ro->ro_rt->rt_ifp;
541 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
543 if (fwd_tag == NULL) {
544 bzero(&dst_sa, sizeof(dst_sa));
545 dst_sa.sin6_family = AF_INET6;
546 dst_sa.sin6_len = sizeof(dst_sa);
547 dst_sa.sin6_addr = ip6->ip6_dst;
549 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
553 in6_ifstat_inc(ifp, ifs6_out_discard);
559 * If in6_selectroute() does not return a route entry,
560 * dst may not have been updated.
562 *dst = dst_sa; /* XXX */
566 * then rt (for unicast) and ifp must be non-NULL valid values.
568 if ((flags & IPV6_FORWARDING) == 0) {
569 /* XXX: the FORWARDING flag can be set for mrouting. */
570 in6_ifstat_inc(ifp, ifs6_out_request);
573 ia = (struct in6_ifaddr *)(rt->rt_ifa);
574 counter_u64_add(rt->rt_pksent, 1);
577 /* Setup data structures for scope ID checks. */
579 bzero(&src_sa, sizeof(src_sa));
580 src_sa.sin6_family = AF_INET6;
581 src_sa.sin6_len = sizeof(src_sa);
582 src_sa.sin6_addr = ip6->ip6_src;
585 /* re-initialize to be sure */
586 bzero(&dst_sa, sizeof(dst_sa));
587 dst_sa.sin6_family = AF_INET6;
588 dst_sa.sin6_len = sizeof(dst_sa);
589 dst_sa.sin6_addr = ip6->ip6_dst;
591 /* Check for valid scope ID. */
592 if (in6_setscope(&src0, ifp, &zone) == 0 &&
593 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
594 in6_setscope(&dst0, ifp, &zone) == 0 &&
595 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
597 * The outgoing interface is in the zone of the source
598 * and destination addresses.
600 * Because the loopback interface cannot receive
601 * packets with a different scope ID than its own,
602 * there is a trick is to pretend the outgoing packet
603 * was received by the real network interface, by
604 * setting "origifp" different from "ifp". This is
605 * only allowed when "ifp" is a loopback network
606 * interface. Refer to code in nd6_output_ifp() for
612 * We should use ia_ifp to support the case of sending
613 * packets to an address of our own.
615 if (ia != NULL && ia->ia_ifp)
618 } else if ((ifp->if_flags & IFF_LOOPBACK) == 0 ||
619 sa6_recoverscope(&src_sa) != 0 ||
620 sa6_recoverscope(&dst_sa) != 0 ||
621 dst_sa.sin6_scope_id == 0 ||
622 (src_sa.sin6_scope_id != 0 &&
623 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
624 (origifp = ifnet_byindex(dst_sa.sin6_scope_id)) == NULL) {
626 * If the destination network interface is not a
627 * loopback interface, or the destination network
628 * address has no scope ID, or the source address has
629 * a scope ID set which is different from the
630 * destination address one, or there is no network
631 * interface representing this scope ID, the address
632 * pair is considered invalid.
634 IP6STAT_INC(ip6s_badscope);
635 in6_ifstat_inc(ifp, ifs6_out_discard);
637 error = EHOSTUNREACH; /* XXX */
641 /* All scope ID checks are successful. */
643 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
644 if (opt && opt->ip6po_nextroute.ro_rt) {
646 * The nexthop is explicitly specified by the
647 * application. We assume the next hop is an IPv6
650 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
652 else if ((rt->rt_flags & RTF_GATEWAY))
653 dst = (struct sockaddr_in6 *)rt->rt_gateway;
656 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
657 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
659 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
660 in6_ifstat_inc(ifp, ifs6_out_mcast);
662 * Confirm that the outgoing interface supports multicast.
664 if (!(ifp->if_flags & IFF_MULTICAST)) {
665 IP6STAT_INC(ip6s_noroute);
666 in6_ifstat_inc(ifp, ifs6_out_discard);
670 if ((im6o == NULL && in6_mcast_loop) ||
671 (im6o && im6o->im6o_multicast_loop)) {
673 * Loop back multicast datagram if not expressly
674 * forbidden to do so, even if we have not joined
675 * the address; protocols will filter it later,
676 * thus deferring a hash lookup and lock acquisition
677 * at the expense of an m_copym().
679 ip6_mloopback(ifp, m);
682 * If we are acting as a multicast router, perform
683 * multicast forwarding as if the packet had just
684 * arrived on the interface to which we are about
685 * to send. The multicast forwarding function
686 * recursively calls this function, using the
687 * IPV6_FORWARDING flag to prevent infinite recursion.
689 * Multicasts that are looped back by ip6_mloopback(),
690 * above, will be forwarded by the ip6_input() routine,
693 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
695 * XXX: ip6_mforward expects that rcvif is NULL
696 * when it is called from the originating path.
697 * However, it may not always be the case.
699 m->m_pkthdr.rcvif = NULL;
700 if (ip6_mforward(ip6, ifp, m) != 0) {
707 * Multicasts with a hoplimit of zero may be looped back,
708 * above, but must not be transmitted on a network.
709 * Also, multicasts addressed to the loopback interface
710 * are not sent -- the above call to ip6_mloopback() will
711 * loop back a copy if this host actually belongs to the
712 * destination group on the loopback interface.
714 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
715 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
722 * Fill the outgoing inteface to tell the upper layer
723 * to increment per-interface statistics.
728 /* Determine path MTU. */
729 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
730 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
734 * The caller of this function may specify to use the minimum MTU
736 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
737 * setting. The logic is a bit complicated; by default, unicast
738 * packets will follow path MTU while multicast packets will be sent at
739 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
740 * including unicast ones will be sent at the minimum MTU. Multicast
741 * packets will always be sent at the minimum MTU unless
742 * IP6PO_MINMTU_DISABLE is explicitly specified.
743 * See RFC 3542 for more details.
745 if (mtu > IPV6_MMTU) {
746 if ((flags & IPV6_MINMTU))
748 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
750 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
752 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
758 * clear embedded scope identifiers if necessary.
759 * in6_clearscope will touch the addresses only when necessary.
761 in6_clearscope(&ip6->ip6_src);
762 in6_clearscope(&ip6->ip6_dst);
765 * If the outgoing packet contains a hop-by-hop options header,
766 * it must be examined and processed even by the source node.
767 * (RFC 2460, section 4.)
769 if (exthdrs.ip6e_hbh) {
770 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
771 u_int32_t dummy; /* XXX unused */
772 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
775 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
776 panic("ip6e_hbh is not contiguous");
779 * XXX: if we have to send an ICMPv6 error to the sender,
780 * we need the M_LOOP flag since icmp6_error() expects
781 * the IPv6 and the hop-by-hop options header are
782 * contiguous unless the flag is set.
784 m->m_flags |= M_LOOP;
785 m->m_pkthdr.rcvif = ifp;
786 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
787 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
788 &dummy, &plen) < 0) {
789 /* m was already freed at this point */
790 error = EINVAL;/* better error? */
793 m->m_flags &= ~M_LOOP; /* XXX */
794 m->m_pkthdr.rcvif = NULL;
797 /* Jump over all PFIL processing if hooks are not active. */
798 if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
802 /* Run through list of hooks for output packets. */
803 switch (pfil_run_hooks(V_inet6_pfil_head, &m, ifp, PFIL_OUT, inp)) {
805 ip6 = mtod(m, struct ip6_hdr *);
815 /* See if destination IP address was changed by packet filter. */
816 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
817 m->m_flags |= M_SKIP_FIREWALL;
818 /* If destination is now ourself drop to ip6_input(). */
819 if (in6_localip(&ip6->ip6_dst)) {
820 m->m_flags |= M_FASTFWD_OURS;
821 if (m->m_pkthdr.rcvif == NULL)
822 m->m_pkthdr.rcvif = V_loif;
823 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
824 m->m_pkthdr.csum_flags |=
825 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
826 m->m_pkthdr.csum_data = 0xffff;
829 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
830 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
832 error = netisr_queue(NETISR_IPV6, m);
835 RO_INVALIDATE_CACHE(ro);
836 needfiblookup = 1; /* Redo the routing table lookup. */
839 /* See if fib was changed by packet filter. */
840 if (fibnum != M_GETFIB(m)) {
841 m->m_flags |= M_SKIP_FIREWALL;
842 fibnum = M_GETFIB(m);
843 RO_INVALIDATE_CACHE(ro);
849 /* See if local, if yes, send it to netisr. */
850 if (m->m_flags & M_FASTFWD_OURS) {
851 if (m->m_pkthdr.rcvif == NULL)
852 m->m_pkthdr.rcvif = V_loif;
853 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
854 m->m_pkthdr.csum_flags |=
855 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
856 m->m_pkthdr.csum_data = 0xffff;
859 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
860 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
862 error = netisr_queue(NETISR_IPV6, m);
865 /* Or forward to some other address? */
866 if ((m->m_flags & M_IP6_NEXTHOP) &&
867 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
868 dst = (struct sockaddr_in6 *)&ro->ro_dst;
869 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
870 m->m_flags |= M_SKIP_FIREWALL;
871 m->m_flags &= ~M_IP6_NEXTHOP;
872 m_tag_delete(m, fwd_tag);
878 * Send the packet to the outgoing interface.
879 * If necessary, do IPv6 fragmentation before sending.
881 * the logic here is rather complex:
882 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
883 * 1-a: send as is if tlen <= path mtu
884 * 1-b: fragment if tlen > path mtu
886 * 2: if user asks us not to fragment (dontfrag == 1)
887 * 2-a: send as is if tlen <= interface mtu
888 * 2-b: error if tlen > interface mtu
890 * 3: if we always need to attach fragment header (alwaysfrag == 1)
893 * 4: if dontfrag == 1 && alwaysfrag == 1
894 * error, as we cannot handle this conflicting request
896 sw_csum = m->m_pkthdr.csum_flags;
898 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
899 sw_csum &= ~ifp->if_hwassist;
903 * If we added extension headers, we will not do TSO and calculate the
904 * checksums ourselves for now.
905 * XXX-BZ Need a framework to know when the NIC can handle it, even
908 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
909 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
910 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
913 if (sw_csum & CSUM_SCTP_IPV6) {
914 sw_csum &= ~CSUM_SCTP_IPV6;
915 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
918 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
919 tlen = m->m_pkthdr.len;
921 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
925 if (dontfrag && alwaysfrag) { /* case 4 */
926 /* conflicting request - can't transmit */
930 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
932 * Even if the DONTFRAG option is specified, we cannot send the
933 * packet when the data length is larger than the MTU of the
934 * outgoing interface.
935 * Notify the error by sending IPV6_PATHMTU ancillary data if
936 * application wanted to know the MTU value. Also return an
937 * error code (this is not described in the API spec).
940 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
946 * transmit packet without fragmentation
948 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
949 struct in6_ifaddr *ia6;
951 ip6 = mtod(m, struct ip6_hdr *);
952 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
954 /* Record statistics for this interface address. */
955 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
956 counter_u64_add(ia6->ia_ifa.ifa_obytes,
958 ifa_free(&ia6->ia_ifa);
962 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
963 in_pcboutput_txrtlmt(inp, ifp, m);
964 /* stamp send tag on mbuf */
965 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
967 m->m_pkthdr.snd_tag = NULL;
970 error = nd6_output_ifp(ifp, origifp, m, dst,
973 /* check for route change */
975 in_pcboutput_eagain(inp);
981 * try to fragment the packet. case 1-b and 3
983 if (mtu < IPV6_MMTU) {
984 /* path MTU cannot be less than IPV6_MMTU */
986 in6_ifstat_inc(ifp, ifs6_out_fragfail);
988 } else if (ip6->ip6_plen == 0) {
989 /* jumbo payload cannot be fragmented */
991 in6_ifstat_inc(ifp, ifs6_out_fragfail);
997 * Too large for the destination or interface;
998 * fragment if possible.
999 * Must be able to put at least 8 bytes per fragment.
1001 hlen = unfragpartlen;
1002 if (mtu > IPV6_MAXPACKET)
1003 mtu = IPV6_MAXPACKET;
1005 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1008 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1013 * If the interface will not calculate checksums on
1014 * fragmented packets, then do it here.
1015 * XXX-BZ handle the hw offloading case. Need flags.
1017 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1018 in6_delayed_cksum(m, plen, hlen);
1019 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1022 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1023 sctp_delayed_cksum(m, hlen);
1024 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1028 * Change the next header field of the last header in the
1029 * unfragmentable part.
1031 if (exthdrs.ip6e_rthdr) {
1032 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1033 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1034 } else if (exthdrs.ip6e_dest1) {
1035 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1036 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1037 } else if (exthdrs.ip6e_hbh) {
1038 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1039 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1041 nextproto = ip6->ip6_nxt;
1042 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1046 * Loop through length of segment after first fragment,
1047 * make new header and copy data of each part and link onto
1051 id = htonl(ip6_randomid());
1052 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1055 in6_ifstat_inc(ifp, ifs6_out_fragok);
1059 * Remove leading garbages.
1069 /* Record statistics for this interface address. */
1071 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1072 counter_u64_add(ia->ia_ifa.ifa_obytes,
1077 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
1078 in_pcboutput_txrtlmt(inp, ifp, m);
1079 /* stamp send tag on mbuf */
1080 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
1082 m->m_pkthdr.snd_tag = NULL;
1085 error = nd6_output_ifp(ifp, origifp, m, dst,
1086 (struct route *)ro);
1088 /* check for route change */
1089 if (error == EAGAIN)
1090 in_pcboutput_eagain(inp);
1097 IP6STAT_INC(ip6s_fragmented);
1100 if (ro == &ip6route)
1105 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1106 m_freem(exthdrs.ip6e_dest1);
1107 m_freem(exthdrs.ip6e_rthdr);
1108 m_freem(exthdrs.ip6e_dest2);
1117 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1121 if (hlen > MCLBYTES)
1122 return (ENOBUFS); /* XXX */
1125 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1127 m = m_get(M_NOWAIT, MT_DATA);
1132 bcopy(hdr, mtod(m, caddr_t), hlen);
1139 * Insert jumbo payload option.
1142 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1148 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1151 * If there is no hop-by-hop options header, allocate new one.
1152 * If there is one but it doesn't have enough space to store the
1153 * jumbo payload option, allocate a cluster to store the whole options.
1154 * Otherwise, use it to store the options.
1156 if (exthdrs->ip6e_hbh == NULL) {
1157 mopt = m_get(M_NOWAIT, MT_DATA);
1160 mopt->m_len = JUMBOOPTLEN;
1161 optbuf = mtod(mopt, u_char *);
1162 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1163 exthdrs->ip6e_hbh = mopt;
1165 struct ip6_hbh *hbh;
1167 mopt = exthdrs->ip6e_hbh;
1168 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1171 * - exthdrs->ip6e_hbh is not referenced from places
1172 * other than exthdrs.
1173 * - exthdrs->ip6e_hbh is not an mbuf chain.
1175 int oldoptlen = mopt->m_len;
1179 * XXX: give up if the whole (new) hbh header does
1180 * not fit even in an mbuf cluster.
1182 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1186 * As a consequence, we must always prepare a cluster
1189 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1192 n->m_len = oldoptlen + JUMBOOPTLEN;
1193 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1195 optbuf = mtod(n, caddr_t) + oldoptlen;
1197 mopt = exthdrs->ip6e_hbh = n;
1199 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1200 mopt->m_len += JUMBOOPTLEN;
1202 optbuf[0] = IP6OPT_PADN;
1206 * Adjust the header length according to the pad and
1207 * the jumbo payload option.
1209 hbh = mtod(mopt, struct ip6_hbh *);
1210 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1213 /* fill in the option. */
1214 optbuf[2] = IP6OPT_JUMBO;
1216 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1217 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1219 /* finally, adjust the packet header length */
1220 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1227 * Insert fragment header and copy unfragmentable header portions.
1230 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1231 struct ip6_frag **frghdrp)
1233 struct mbuf *n, *mlast;
1235 if (hlen > sizeof(struct ip6_hdr)) {
1236 n = m_copym(m0, sizeof(struct ip6_hdr),
1237 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1244 /* Search for the last mbuf of unfragmentable part. */
1245 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1248 if (M_WRITABLE(mlast) &&
1249 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1250 /* use the trailing space of the last mbuf for the fragment hdr */
1251 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1253 mlast->m_len += sizeof(struct ip6_frag);
1254 m->m_pkthdr.len += sizeof(struct ip6_frag);
1256 /* allocate a new mbuf for the fragment header */
1259 mfrg = m_get(M_NOWAIT, MT_DATA);
1262 mfrg->m_len = sizeof(struct ip6_frag);
1263 *frghdrp = mtod(mfrg, struct ip6_frag *);
1264 mlast->m_next = mfrg;
1271 * Calculates IPv6 path mtu for destination @dst.
1272 * Resulting MTU is stored in @mtup.
1274 * Returns 0 on success.
1277 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1279 struct nhop6_extended nh6;
1280 struct in6_addr kdst;
1286 in6_splitscope(dst, &kdst, &scopeid);
1287 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1288 return (EHOSTUNREACH);
1293 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1294 fib6_free_nh_ext(fibnum, &nh6);
1300 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1301 * and cached data in @ro_pmtu.
1302 * MTU from (successful) route lookup is saved (along with dst)
1303 * inside @ro_pmtu to avoid subsequent route lookups after packet
1304 * filter processing.
1306 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1307 * Returns 0 on success.
1310 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1311 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1312 int *alwaysfragp, u_int fibnum, u_int proto)
1314 struct nhop6_basic nh6;
1315 struct in6_addr kdst;
1317 struct sockaddr_in6 *sa6_dst;
1324 * Here ro_pmtu has final destination address, while
1325 * ro might represent immediate destination.
1326 * Use ro_pmtu destination since mtu might differ.
1328 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1329 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1330 ro_pmtu->ro_mtu = 0;
1332 if (ro_pmtu->ro_mtu == 0) {
1333 bzero(sa6_dst, sizeof(*sa6_dst));
1334 sa6_dst->sin6_family = AF_INET6;
1335 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1336 sa6_dst->sin6_addr = *dst;
1338 in6_splitscope(dst, &kdst, &scopeid);
1339 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1341 ro_pmtu->ro_mtu = nh6.nh_mtu;
1344 mtu = ro_pmtu->ro_mtu;
1348 mtu = ro_pmtu->ro_rt->rt_mtu;
1350 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1354 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1355 * hostcache data for @dst.
1356 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1358 * Returns 0 on success.
1361 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1362 u_long *mtup, int *alwaysfragp, u_int proto)
1370 struct in_conninfo inc;
1372 bzero(&inc, sizeof(inc));
1373 inc.inc_flags |= INC_ISIPV6;
1374 inc.inc6_faddr = *dst;
1376 ifmtu = IN6_LINKMTU(ifp);
1378 /* TCP is known to react to pmtu changes so skip hc */
1379 if (proto != IPPROTO_TCP)
1380 mtu = tcp_hc_getmtu(&inc);
1383 mtu = min(mtu, rt_mtu);
1388 else if (mtu < IPV6_MMTU) {
1390 * RFC2460 section 5, last paragraph:
1391 * if we record ICMPv6 too big message with
1392 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1393 * or smaller, with framgent header attached.
1394 * (fragment header is needed regardless from the
1395 * packet size, for translators to identify packets)
1401 mtu = IN6_LINKMTU(ifp);
1403 error = EHOSTUNREACH; /* XXX */
1407 *alwaysfragp = alwaysfrag;
1412 * IP6 socket option processing.
1415 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1417 int optdatalen, uproto;
1419 struct inpcb *in6p = sotoinpcb(so);
1421 int level, op, optname;
1425 uint32_t rss_bucket;
1430 * Don't use more than a quarter of mbuf clusters. N.B.:
1431 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1432 * on LP64 architectures, so cast to u_long to avoid undefined
1433 * behavior. ILP32 architectures cannot have nmbclusters
1434 * large enough to overflow for other reasons.
1436 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1438 level = sopt->sopt_level;
1439 op = sopt->sopt_dir;
1440 optname = sopt->sopt_name;
1441 optlen = sopt->sopt_valsize;
1445 uproto = (int)so->so_proto->pr_protocol;
1447 if (level != IPPROTO_IPV6) {
1450 if (sopt->sopt_level == SOL_SOCKET &&
1451 sopt->sopt_dir == SOPT_SET) {
1452 switch (sopt->sopt_name) {
1455 if ((so->so_options & SO_REUSEADDR) != 0)
1456 in6p->inp_flags2 |= INP_REUSEADDR;
1458 in6p->inp_flags2 &= ~INP_REUSEADDR;
1464 if ((so->so_options & SO_REUSEPORT) != 0)
1465 in6p->inp_flags2 |= INP_REUSEPORT;
1467 in6p->inp_flags2 &= ~INP_REUSEPORT;
1471 case SO_REUSEPORT_LB:
1473 if ((so->so_options & SO_REUSEPORT_LB) != 0)
1474 in6p->inp_flags2 |= INP_REUSEPORT_LB;
1476 in6p->inp_flags2 &= ~INP_REUSEPORT_LB;
1482 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1486 case SO_MAX_PACING_RATE:
1489 in6p->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1500 } else { /* level == IPPROTO_IPV6 */
1505 case IPV6_2292PKTOPTIONS:
1506 #ifdef IPV6_PKTOPTIONS
1507 case IPV6_PKTOPTIONS:
1512 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1513 printf("ip6_ctloutput: mbuf limit hit\n");
1518 error = soopt_getm(sopt, &m); /* XXX */
1521 error = soopt_mcopyin(sopt, m); /* XXX */
1524 error = ip6_pcbopts(&in6p->in6p_outputopts,
1526 m_freem(m); /* XXX */
1531 * Use of some Hop-by-Hop options or some
1532 * Destination options, might require special
1533 * privilege. That is, normal applications
1534 * (without special privilege) might be forbidden
1535 * from setting certain options in outgoing packets,
1536 * and might never see certain options in received
1537 * packets. [RFC 2292 Section 6]
1538 * KAME specific note:
1539 * KAME prevents non-privileged users from sending or
1540 * receiving ANY hbh/dst options in order to avoid
1541 * overhead of parsing options in the kernel.
1543 case IPV6_RECVHOPOPTS:
1544 case IPV6_RECVDSTOPTS:
1545 case IPV6_RECVRTHDRDSTOPTS:
1547 error = priv_check(td,
1548 PRIV_NETINET_SETHDROPTS);
1553 case IPV6_UNICAST_HOPS:
1556 case IPV6_RECVPKTINFO:
1557 case IPV6_RECVHOPLIMIT:
1558 case IPV6_RECVRTHDR:
1559 case IPV6_RECVPATHMTU:
1560 case IPV6_RECVTCLASS:
1561 case IPV6_RECVFLOWID:
1563 case IPV6_RECVRSSBUCKETID:
1566 case IPV6_AUTOFLOWLABEL:
1567 case IPV6_ORIGDSTADDR:
1569 case IPV6_BINDMULTI:
1571 case IPV6_RSS_LISTEN_BUCKET:
1573 if (optname == IPV6_BINDANY && td != NULL) {
1574 error = priv_check(td,
1575 PRIV_NETINET_BINDANY);
1580 if (optlen != sizeof(int)) {
1584 error = sooptcopyin(sopt, &optval,
1585 sizeof optval, sizeof optval);
1590 case IPV6_UNICAST_HOPS:
1591 if (optval < -1 || optval >= 256)
1594 /* -1 = kernel default */
1595 in6p->in6p_hops = optval;
1596 if ((in6p->inp_vflag &
1598 in6p->inp_ip_ttl = optval;
1601 #define OPTSET(bit) \
1605 in6p->inp_flags |= (bit); \
1607 in6p->inp_flags &= ~(bit); \
1608 INP_WUNLOCK(in6p); \
1609 } while (/*CONSTCOND*/ 0)
1610 #define OPTSET2292(bit) \
1613 in6p->inp_flags |= IN6P_RFC2292; \
1615 in6p->inp_flags |= (bit); \
1617 in6p->inp_flags &= ~(bit); \
1618 INP_WUNLOCK(in6p); \
1619 } while (/*CONSTCOND*/ 0)
1620 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1622 #define OPTSET2_N(bit, val) do { \
1624 in6p->inp_flags2 |= bit; \
1626 in6p->inp_flags2 &= ~bit; \
1628 #define OPTSET2(bit, val) do { \
1630 OPTSET2_N(bit, val); \
1631 INP_WUNLOCK(in6p); \
1633 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1634 #define OPTSET2292_EXCLUSIVE(bit) \
1637 if (OPTBIT(IN6P_RFC2292)) { \
1641 in6p->inp_flags |= (bit); \
1643 in6p->inp_flags &= ~(bit); \
1645 INP_WUNLOCK(in6p); \
1646 } while (/*CONSTCOND*/ 0)
1648 case IPV6_RECVPKTINFO:
1649 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1654 struct ip6_pktopts **optp;
1656 /* cannot mix with RFC2292 */
1657 if (OPTBIT(IN6P_RFC2292)) {
1662 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1664 return (ECONNRESET);
1666 optp = &in6p->in6p_outputopts;
1667 error = ip6_pcbopt(IPV6_HOPLIMIT,
1668 (u_char *)&optval, sizeof(optval),
1669 optp, (td != NULL) ? td->td_ucred :
1675 case IPV6_RECVHOPLIMIT:
1676 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1679 case IPV6_RECVHOPOPTS:
1680 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1683 case IPV6_RECVDSTOPTS:
1684 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1687 case IPV6_RECVRTHDRDSTOPTS:
1688 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1691 case IPV6_RECVRTHDR:
1692 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1695 case IPV6_RECVPATHMTU:
1697 * We ignore this option for TCP
1699 * (RFC3542 leaves this case
1702 if (uproto != IPPROTO_TCP)
1706 case IPV6_RECVFLOWID:
1707 OPTSET2(INP_RECVFLOWID, optval);
1711 case IPV6_RECVRSSBUCKETID:
1712 OPTSET2(INP_RECVRSSBUCKETID, optval);
1718 * make setsockopt(IPV6_V6ONLY)
1719 * available only prior to bind(2).
1720 * see ipng mailing list, Jun 22 2001.
1722 if (in6p->inp_lport ||
1723 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1727 OPTSET(IN6P_IPV6_V6ONLY);
1729 in6p->inp_vflag &= ~INP_IPV4;
1731 in6p->inp_vflag |= INP_IPV4;
1733 case IPV6_RECVTCLASS:
1734 /* cannot mix with RFC2292 XXX */
1735 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1737 case IPV6_AUTOFLOWLABEL:
1738 OPTSET(IN6P_AUTOFLOWLABEL);
1741 case IPV6_ORIGDSTADDR:
1742 OPTSET2(INP_ORIGDSTADDR, optval);
1745 OPTSET(INP_BINDANY);
1748 case IPV6_BINDMULTI:
1749 OPTSET2(INP_BINDMULTI, optval);
1752 case IPV6_RSS_LISTEN_BUCKET:
1753 if ((optval >= 0) &&
1754 (optval < rss_getnumbuckets())) {
1756 in6p->inp_rss_listen_bucket = optval;
1757 OPTSET2_N(INP_RSS_BUCKET_SET, 1);
1769 case IPV6_USE_MIN_MTU:
1770 case IPV6_PREFER_TEMPADDR:
1771 if (optlen != sizeof(optval)) {
1775 error = sooptcopyin(sopt, &optval,
1776 sizeof optval, sizeof optval);
1780 struct ip6_pktopts **optp;
1782 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1784 return (ECONNRESET);
1786 optp = &in6p->in6p_outputopts;
1787 error = ip6_pcbopt(optname,
1788 (u_char *)&optval, sizeof(optval),
1789 optp, (td != NULL) ? td->td_ucred :
1795 case IPV6_2292PKTINFO:
1796 case IPV6_2292HOPLIMIT:
1797 case IPV6_2292HOPOPTS:
1798 case IPV6_2292DSTOPTS:
1799 case IPV6_2292RTHDR:
1801 if (optlen != sizeof(int)) {
1805 error = sooptcopyin(sopt, &optval,
1806 sizeof optval, sizeof optval);
1810 case IPV6_2292PKTINFO:
1811 OPTSET2292(IN6P_PKTINFO);
1813 case IPV6_2292HOPLIMIT:
1814 OPTSET2292(IN6P_HOPLIMIT);
1816 case IPV6_2292HOPOPTS:
1818 * Check super-user privilege.
1819 * See comments for IPV6_RECVHOPOPTS.
1822 error = priv_check(td,
1823 PRIV_NETINET_SETHDROPTS);
1827 OPTSET2292(IN6P_HOPOPTS);
1829 case IPV6_2292DSTOPTS:
1831 error = priv_check(td,
1832 PRIV_NETINET_SETHDROPTS);
1836 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1838 case IPV6_2292RTHDR:
1839 OPTSET2292(IN6P_RTHDR);
1847 case IPV6_RTHDRDSTOPTS:
1850 /* new advanced API (RFC3542) */
1852 u_char optbuf_storage[MCLBYTES];
1854 struct ip6_pktopts **optp;
1856 /* cannot mix with RFC2292 */
1857 if (OPTBIT(IN6P_RFC2292)) {
1863 * We only ensure valsize is not too large
1864 * here. Further validation will be done
1867 error = sooptcopyin(sopt, optbuf_storage,
1868 sizeof(optbuf_storage), 0);
1871 optlen = sopt->sopt_valsize;
1872 optbuf = optbuf_storage;
1874 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1876 return (ECONNRESET);
1878 optp = &in6p->in6p_outputopts;
1879 error = ip6_pcbopt(optname, optbuf, optlen,
1880 optp, (td != NULL) ? td->td_ucred : NULL,
1887 case IPV6_MULTICAST_IF:
1888 case IPV6_MULTICAST_HOPS:
1889 case IPV6_MULTICAST_LOOP:
1890 case IPV6_JOIN_GROUP:
1891 case IPV6_LEAVE_GROUP:
1893 case MCAST_BLOCK_SOURCE:
1894 case MCAST_UNBLOCK_SOURCE:
1895 case MCAST_JOIN_GROUP:
1896 case MCAST_LEAVE_GROUP:
1897 case MCAST_JOIN_SOURCE_GROUP:
1898 case MCAST_LEAVE_SOURCE_GROUP:
1899 error = ip6_setmoptions(in6p, sopt);
1902 case IPV6_PORTRANGE:
1903 error = sooptcopyin(sopt, &optval,
1904 sizeof optval, sizeof optval);
1910 case IPV6_PORTRANGE_DEFAULT:
1911 in6p->inp_flags &= ~(INP_LOWPORT);
1912 in6p->inp_flags &= ~(INP_HIGHPORT);
1915 case IPV6_PORTRANGE_HIGH:
1916 in6p->inp_flags &= ~(INP_LOWPORT);
1917 in6p->inp_flags |= INP_HIGHPORT;
1920 case IPV6_PORTRANGE_LOW:
1921 in6p->inp_flags &= ~(INP_HIGHPORT);
1922 in6p->inp_flags |= INP_LOWPORT;
1932 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1933 case IPV6_IPSEC_POLICY:
1934 if (IPSEC_ENABLED(ipv6)) {
1935 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
1942 error = ENOPROTOOPT;
1950 case IPV6_2292PKTOPTIONS:
1951 #ifdef IPV6_PKTOPTIONS
1952 case IPV6_PKTOPTIONS:
1955 * RFC3542 (effectively) deprecated the
1956 * semantics of the 2292-style pktoptions.
1957 * Since it was not reliable in nature (i.e.,
1958 * applications had to expect the lack of some
1959 * information after all), it would make sense
1960 * to simplify this part by always returning
1963 sopt->sopt_valsize = 0;
1966 case IPV6_RECVHOPOPTS:
1967 case IPV6_RECVDSTOPTS:
1968 case IPV6_RECVRTHDRDSTOPTS:
1969 case IPV6_UNICAST_HOPS:
1970 case IPV6_RECVPKTINFO:
1971 case IPV6_RECVHOPLIMIT:
1972 case IPV6_RECVRTHDR:
1973 case IPV6_RECVPATHMTU:
1976 case IPV6_PORTRANGE:
1977 case IPV6_RECVTCLASS:
1978 case IPV6_AUTOFLOWLABEL:
1982 case IPV6_RECVFLOWID:
1984 case IPV6_RSSBUCKETID:
1985 case IPV6_RECVRSSBUCKETID:
1987 case IPV6_BINDMULTI:
1990 case IPV6_RECVHOPOPTS:
1991 optval = OPTBIT(IN6P_HOPOPTS);
1994 case IPV6_RECVDSTOPTS:
1995 optval = OPTBIT(IN6P_DSTOPTS);
1998 case IPV6_RECVRTHDRDSTOPTS:
1999 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2002 case IPV6_UNICAST_HOPS:
2003 optval = in6p->in6p_hops;
2006 case IPV6_RECVPKTINFO:
2007 optval = OPTBIT(IN6P_PKTINFO);
2010 case IPV6_RECVHOPLIMIT:
2011 optval = OPTBIT(IN6P_HOPLIMIT);
2014 case IPV6_RECVRTHDR:
2015 optval = OPTBIT(IN6P_RTHDR);
2018 case IPV6_RECVPATHMTU:
2019 optval = OPTBIT(IN6P_MTU);
2023 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2026 case IPV6_PORTRANGE:
2029 flags = in6p->inp_flags;
2030 if (flags & INP_HIGHPORT)
2031 optval = IPV6_PORTRANGE_HIGH;
2032 else if (flags & INP_LOWPORT)
2033 optval = IPV6_PORTRANGE_LOW;
2038 case IPV6_RECVTCLASS:
2039 optval = OPTBIT(IN6P_TCLASS);
2042 case IPV6_AUTOFLOWLABEL:
2043 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2046 case IPV6_ORIGDSTADDR:
2047 optval = OPTBIT2(INP_ORIGDSTADDR);
2051 optval = OPTBIT(INP_BINDANY);
2055 optval = in6p->inp_flowid;
2059 optval = in6p->inp_flowtype;
2062 case IPV6_RECVFLOWID:
2063 optval = OPTBIT2(INP_RECVFLOWID);
2066 case IPV6_RSSBUCKETID:
2068 rss_hash2bucket(in6p->inp_flowid,
2072 optval = rss_bucket;
2077 case IPV6_RECVRSSBUCKETID:
2078 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2082 case IPV6_BINDMULTI:
2083 optval = OPTBIT2(INP_BINDMULTI);
2089 error = sooptcopyout(sopt, &optval,
2096 struct ip6_mtuinfo mtuinfo;
2097 struct in6_addr addr;
2099 if (!(so->so_state & SS_ISCONNECTED))
2102 * XXX: we dot not consider the case of source
2103 * routing, or optional information to specify
2104 * the outgoing interface.
2105 * Copy faddr out of in6p to avoid holding lock
2106 * on inp during route lookup.
2109 bcopy(&in6p->in6p_faddr, &addr, sizeof(addr));
2111 error = ip6_getpmtu_ctl(so->so_fibnum,
2115 if (pmtu > IPV6_MAXPACKET)
2116 pmtu = IPV6_MAXPACKET;
2118 bzero(&mtuinfo, sizeof(mtuinfo));
2119 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2120 optdata = (void *)&mtuinfo;
2121 optdatalen = sizeof(mtuinfo);
2122 error = sooptcopyout(sopt, optdata,
2127 case IPV6_2292PKTINFO:
2128 case IPV6_2292HOPLIMIT:
2129 case IPV6_2292HOPOPTS:
2130 case IPV6_2292RTHDR:
2131 case IPV6_2292DSTOPTS:
2133 case IPV6_2292PKTINFO:
2134 optval = OPTBIT(IN6P_PKTINFO);
2136 case IPV6_2292HOPLIMIT:
2137 optval = OPTBIT(IN6P_HOPLIMIT);
2139 case IPV6_2292HOPOPTS:
2140 optval = OPTBIT(IN6P_HOPOPTS);
2142 case IPV6_2292RTHDR:
2143 optval = OPTBIT(IN6P_RTHDR);
2145 case IPV6_2292DSTOPTS:
2146 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2149 error = sooptcopyout(sopt, &optval,
2156 case IPV6_RTHDRDSTOPTS:
2160 case IPV6_USE_MIN_MTU:
2161 case IPV6_PREFER_TEMPADDR:
2162 error = ip6_getpcbopt(in6p, optname, sopt);
2165 case IPV6_MULTICAST_IF:
2166 case IPV6_MULTICAST_HOPS:
2167 case IPV6_MULTICAST_LOOP:
2169 error = ip6_getmoptions(in6p, sopt);
2172 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2173 case IPV6_IPSEC_POLICY:
2174 if (IPSEC_ENABLED(ipv6)) {
2175 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
2181 error = ENOPROTOOPT;
2191 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2193 int error = 0, optval, optlen;
2194 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2195 struct inpcb *in6p = sotoinpcb(so);
2196 int level, op, optname;
2198 level = sopt->sopt_level;
2199 op = sopt->sopt_dir;
2200 optname = sopt->sopt_name;
2201 optlen = sopt->sopt_valsize;
2203 if (level != IPPROTO_IPV6) {
2210 * For ICMPv6 sockets, no modification allowed for checksum
2211 * offset, permit "no change" values to help existing apps.
2213 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2214 * for an ICMPv6 socket will fail."
2215 * The current behavior does not meet RFC3542.
2219 if (optlen != sizeof(int)) {
2223 error = sooptcopyin(sopt, &optval, sizeof(optval),
2227 if (optval < -1 || (optval % 2) != 0) {
2229 * The API assumes non-negative even offset
2230 * values or -1 as a special value.
2233 } else if (so->so_proto->pr_protocol ==
2235 if (optval != icmp6off)
2238 in6p->in6p_cksum = optval;
2242 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2245 optval = in6p->in6p_cksum;
2247 error = sooptcopyout(sopt, &optval, sizeof(optval));
2257 error = ENOPROTOOPT;
2265 * Set up IP6 options in pcb for insertion in output packets or
2266 * specifying behavior of outgoing packets.
2269 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2270 struct socket *so, struct sockopt *sopt)
2272 struct ip6_pktopts *opt = *pktopt;
2274 struct thread *td = sopt->sopt_td;
2276 /* turn off any old options. */
2279 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2280 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2281 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2282 printf("ip6_pcbopts: all specified options are cleared.\n");
2284 ip6_clearpktopts(opt, -1);
2286 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2289 if (!m || m->m_len == 0) {
2291 * Only turning off any previous options, regardless of
2292 * whether the opt is just created or given.
2294 free(opt, M_IP6OPT);
2298 /* set options specified by user. */
2299 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2300 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2301 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2302 free(opt, M_IP6OPT);
2310 * initialize ip6_pktopts. beware that there are non-zero default values in
2314 ip6_initpktopts(struct ip6_pktopts *opt)
2317 bzero(opt, sizeof(*opt));
2318 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2319 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2320 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2321 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2325 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2326 struct ucred *cred, int uproto)
2328 struct ip6_pktopts *opt;
2330 if (*pktopt == NULL) {
2331 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2333 if (*pktopt == NULL)
2335 ip6_initpktopts(*pktopt);
2339 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2342 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2343 if (pktopt && pktopt->field) { \
2344 INP_RUNLOCK(in6p); \
2345 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2346 malloc_optdata = true; \
2348 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
2349 INP_RUNLOCK(in6p); \
2350 free(optdata, M_TEMP); \
2351 return (ECONNRESET); \
2353 pktopt = in6p->in6p_outputopts; \
2354 if (pktopt && pktopt->field) { \
2355 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2356 bcopy(&pktopt->field, optdata, optdatalen); \
2358 free(optdata, M_TEMP); \
2360 malloc_optdata = false; \
2365 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2366 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2368 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2369 pktopt->field->sa_len)
2372 ip6_getpcbopt(struct inpcb *in6p, int optname, struct sockopt *sopt)
2374 void *optdata = NULL;
2375 bool malloc_optdata = false;
2378 struct in6_pktinfo null_pktinfo;
2379 int deftclass = 0, on;
2380 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2381 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2382 struct ip6_pktopts *pktopt;
2385 pktopt = in6p->in6p_outputopts;
2389 optdata = (void *)&null_pktinfo;
2390 if (pktopt && pktopt->ip6po_pktinfo) {
2391 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2392 sizeof(null_pktinfo));
2393 in6_clearscope(&null_pktinfo.ipi6_addr);
2395 /* XXX: we don't have to do this every time... */
2396 bzero(&null_pktinfo, sizeof(null_pktinfo));
2398 optdatalen = sizeof(struct in6_pktinfo);
2401 if (pktopt && pktopt->ip6po_tclass >= 0)
2402 deftclass = pktopt->ip6po_tclass;
2403 optdata = (void *)&deftclass;
2404 optdatalen = sizeof(int);
2407 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2410 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2412 case IPV6_RTHDRDSTOPTS:
2413 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2416 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2419 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2421 case IPV6_USE_MIN_MTU:
2423 defminmtu = pktopt->ip6po_minmtu;
2424 optdata = (void *)&defminmtu;
2425 optdatalen = sizeof(int);
2428 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2432 optdata = (void *)&on;
2433 optdatalen = sizeof(on);
2435 case IPV6_PREFER_TEMPADDR:
2437 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2438 optdata = (void *)&defpreftemp;
2439 optdatalen = sizeof(int);
2441 default: /* should not happen */
2443 panic("ip6_getpcbopt: unexpected option\n");
2446 return (ENOPROTOOPT);
2450 error = sooptcopyout(sopt, optdata, optdatalen);
2452 free(optdata, M_TEMP);
2458 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2463 if (optname == -1 || optname == IPV6_PKTINFO) {
2464 if (pktopt->ip6po_pktinfo)
2465 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2466 pktopt->ip6po_pktinfo = NULL;
2468 if (optname == -1 || optname == IPV6_HOPLIMIT)
2469 pktopt->ip6po_hlim = -1;
2470 if (optname == -1 || optname == IPV6_TCLASS)
2471 pktopt->ip6po_tclass = -1;
2472 if (optname == -1 || optname == IPV6_NEXTHOP) {
2473 if (pktopt->ip6po_nextroute.ro_rt) {
2474 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2475 pktopt->ip6po_nextroute.ro_rt = NULL;
2477 if (pktopt->ip6po_nexthop)
2478 free(pktopt->ip6po_nexthop, M_IP6OPT);
2479 pktopt->ip6po_nexthop = NULL;
2481 if (optname == -1 || optname == IPV6_HOPOPTS) {
2482 if (pktopt->ip6po_hbh)
2483 free(pktopt->ip6po_hbh, M_IP6OPT);
2484 pktopt->ip6po_hbh = NULL;
2486 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2487 if (pktopt->ip6po_dest1)
2488 free(pktopt->ip6po_dest1, M_IP6OPT);
2489 pktopt->ip6po_dest1 = NULL;
2491 if (optname == -1 || optname == IPV6_RTHDR) {
2492 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2493 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2494 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2495 if (pktopt->ip6po_route.ro_rt) {
2496 RTFREE(pktopt->ip6po_route.ro_rt);
2497 pktopt->ip6po_route.ro_rt = NULL;
2500 if (optname == -1 || optname == IPV6_DSTOPTS) {
2501 if (pktopt->ip6po_dest2)
2502 free(pktopt->ip6po_dest2, M_IP6OPT);
2503 pktopt->ip6po_dest2 = NULL;
2507 #define PKTOPT_EXTHDRCPY(type) \
2510 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2511 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2512 if (dst->type == NULL)\
2514 bcopy(src->type, dst->type, hlen);\
2516 } while (/*CONSTCOND*/ 0)
2519 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2521 if (dst == NULL || src == NULL) {
2522 printf("ip6_clearpktopts: invalid argument\n");
2526 dst->ip6po_hlim = src->ip6po_hlim;
2527 dst->ip6po_tclass = src->ip6po_tclass;
2528 dst->ip6po_flags = src->ip6po_flags;
2529 dst->ip6po_minmtu = src->ip6po_minmtu;
2530 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2531 if (src->ip6po_pktinfo) {
2532 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2534 if (dst->ip6po_pktinfo == NULL)
2536 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2538 if (src->ip6po_nexthop) {
2539 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2541 if (dst->ip6po_nexthop == NULL)
2543 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2544 src->ip6po_nexthop->sa_len);
2546 PKTOPT_EXTHDRCPY(ip6po_hbh);
2547 PKTOPT_EXTHDRCPY(ip6po_dest1);
2548 PKTOPT_EXTHDRCPY(ip6po_dest2);
2549 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2553 ip6_clearpktopts(dst, -1);
2556 #undef PKTOPT_EXTHDRCPY
2558 struct ip6_pktopts *
2559 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2562 struct ip6_pktopts *dst;
2564 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2567 ip6_initpktopts(dst);
2569 if ((error = copypktopts(dst, src, canwait)) != 0) {
2570 free(dst, M_IP6OPT);
2578 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2583 ip6_clearpktopts(pktopt, -1);
2585 free(pktopt, M_IP6OPT);
2589 * Set IPv6 outgoing packet options based on advanced API.
2592 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2593 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2595 struct cmsghdr *cm = NULL;
2597 if (control == NULL || opt == NULL)
2600 ip6_initpktopts(opt);
2605 * If stickyopt is provided, make a local copy of the options
2606 * for this particular packet, then override them by ancillary
2608 * XXX: copypktopts() does not copy the cached route to a next
2609 * hop (if any). This is not very good in terms of efficiency,
2610 * but we can allow this since this option should be rarely
2613 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2618 * XXX: Currently, we assume all the optional information is stored
2621 if (control->m_next)
2624 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2625 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2628 if (control->m_len < CMSG_LEN(0))
2631 cm = mtod(control, struct cmsghdr *);
2632 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2634 if (cm->cmsg_level != IPPROTO_IPV6)
2637 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2638 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2647 * Set a particular packet option, as a sticky option or an ancillary data
2648 * item. "len" can be 0 only when it's a sticky option.
2649 * We have 4 cases of combination of "sticky" and "cmsg":
2650 * "sticky=0, cmsg=0": impossible
2651 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2652 * "sticky=1, cmsg=0": RFC3542 socket option
2653 * "sticky=1, cmsg=1": RFC2292 socket option
2656 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2657 struct ucred *cred, int sticky, int cmsg, int uproto)
2659 int minmtupolicy, preftemp;
2662 if (!sticky && !cmsg) {
2664 printf("ip6_setpktopt: impossible case\n");
2670 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2671 * not be specified in the context of RFC3542. Conversely,
2672 * RFC3542 types should not be specified in the context of RFC2292.
2676 case IPV6_2292PKTINFO:
2677 case IPV6_2292HOPLIMIT:
2678 case IPV6_2292NEXTHOP:
2679 case IPV6_2292HOPOPTS:
2680 case IPV6_2292DSTOPTS:
2681 case IPV6_2292RTHDR:
2682 case IPV6_2292PKTOPTIONS:
2683 return (ENOPROTOOPT);
2686 if (sticky && cmsg) {
2693 case IPV6_RTHDRDSTOPTS:
2695 case IPV6_USE_MIN_MTU:
2698 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2699 return (ENOPROTOOPT);
2704 case IPV6_2292PKTINFO:
2707 struct ifnet *ifp = NULL;
2708 struct in6_pktinfo *pktinfo;
2710 if (len != sizeof(struct in6_pktinfo))
2713 pktinfo = (struct in6_pktinfo *)buf;
2716 * An application can clear any sticky IPV6_PKTINFO option by
2717 * doing a "regular" setsockopt with ipi6_addr being
2718 * in6addr_any and ipi6_ifindex being zero.
2719 * [RFC 3542, Section 6]
2721 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2722 pktinfo->ipi6_ifindex == 0 &&
2723 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2724 ip6_clearpktopts(opt, optname);
2728 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2729 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2732 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2734 /* validate the interface index if specified. */
2735 if (pktinfo->ipi6_ifindex > V_if_index)
2737 if (pktinfo->ipi6_ifindex) {
2738 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2742 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2743 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2747 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2748 struct in6_ifaddr *ia;
2750 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2751 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2753 return (EADDRNOTAVAIL);
2754 ifa_free(&ia->ia_ifa);
2757 * We store the address anyway, and let in6_selectsrc()
2758 * validate the specified address. This is because ipi6_addr
2759 * may not have enough information about its scope zone, and
2760 * we may need additional information (such as outgoing
2761 * interface or the scope zone of a destination address) to
2762 * disambiguate the scope.
2763 * XXX: the delay of the validation may confuse the
2764 * application when it is used as a sticky option.
2766 if (opt->ip6po_pktinfo == NULL) {
2767 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2768 M_IP6OPT, M_NOWAIT);
2769 if (opt->ip6po_pktinfo == NULL)
2772 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2776 case IPV6_2292HOPLIMIT:
2782 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2783 * to simplify the ordering among hoplimit options.
2785 if (optname == IPV6_HOPLIMIT && sticky)
2786 return (ENOPROTOOPT);
2788 if (len != sizeof(int))
2791 if (*hlimp < -1 || *hlimp > 255)
2794 opt->ip6po_hlim = *hlimp;
2802 if (len != sizeof(int))
2804 tclass = *(int *)buf;
2805 if (tclass < -1 || tclass > 255)
2808 opt->ip6po_tclass = tclass;
2812 case IPV6_2292NEXTHOP:
2815 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
2820 if (len == 0) { /* just remove the option */
2821 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2825 /* check if cmsg_len is large enough for sa_len */
2826 if (len < sizeof(struct sockaddr) || len < *buf)
2829 switch (((struct sockaddr *)buf)->sa_family) {
2832 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2835 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2838 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2839 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2842 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2848 case AF_LINK: /* should eventually be supported */
2850 return (EAFNOSUPPORT);
2853 /* turn off the previous option, then set the new option. */
2854 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2855 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2856 if (opt->ip6po_nexthop == NULL)
2858 bcopy(buf, opt->ip6po_nexthop, *buf);
2861 case IPV6_2292HOPOPTS:
2864 struct ip6_hbh *hbh;
2868 * XXX: We don't allow a non-privileged user to set ANY HbH
2869 * options, since per-option restriction has too much
2873 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
2879 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2880 break; /* just remove the option */
2883 /* message length validation */
2884 if (len < sizeof(struct ip6_hbh))
2886 hbh = (struct ip6_hbh *)buf;
2887 hbhlen = (hbh->ip6h_len + 1) << 3;
2891 /* turn off the previous option, then set the new option. */
2892 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2893 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2894 if (opt->ip6po_hbh == NULL)
2896 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2901 case IPV6_2292DSTOPTS:
2903 case IPV6_RTHDRDSTOPTS:
2905 struct ip6_dest *dest, **newdest = NULL;
2908 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2909 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
2915 ip6_clearpktopts(opt, optname);
2916 break; /* just remove the option */
2919 /* message length validation */
2920 if (len < sizeof(struct ip6_dest))
2922 dest = (struct ip6_dest *)buf;
2923 destlen = (dest->ip6d_len + 1) << 3;
2928 * Determine the position that the destination options header
2929 * should be inserted; before or after the routing header.
2932 case IPV6_2292DSTOPTS:
2934 * The old advacned API is ambiguous on this point.
2935 * Our approach is to determine the position based
2936 * according to the existence of a routing header.
2937 * Note, however, that this depends on the order of the
2938 * extension headers in the ancillary data; the 1st
2939 * part of the destination options header must appear
2940 * before the routing header in the ancillary data,
2942 * RFC3542 solved the ambiguity by introducing
2943 * separate ancillary data or option types.
2945 if (opt->ip6po_rthdr == NULL)
2946 newdest = &opt->ip6po_dest1;
2948 newdest = &opt->ip6po_dest2;
2950 case IPV6_RTHDRDSTOPTS:
2951 newdest = &opt->ip6po_dest1;
2954 newdest = &opt->ip6po_dest2;
2958 /* turn off the previous option, then set the new option. */
2959 ip6_clearpktopts(opt, optname);
2960 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2961 if (*newdest == NULL)
2963 bcopy(dest, *newdest, destlen);
2968 case IPV6_2292RTHDR:
2971 struct ip6_rthdr *rth;
2975 ip6_clearpktopts(opt, IPV6_RTHDR);
2976 break; /* just remove the option */
2979 /* message length validation */
2980 if (len < sizeof(struct ip6_rthdr))
2982 rth = (struct ip6_rthdr *)buf;
2983 rthlen = (rth->ip6r_len + 1) << 3;
2987 switch (rth->ip6r_type) {
2988 case IPV6_RTHDR_TYPE_0:
2989 if (rth->ip6r_len == 0) /* must contain one addr */
2991 if (rth->ip6r_len % 2) /* length must be even */
2993 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2997 return (EINVAL); /* not supported */
3000 /* turn off the previous option */
3001 ip6_clearpktopts(opt, IPV6_RTHDR);
3002 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3003 if (opt->ip6po_rthdr == NULL)
3005 bcopy(rth, opt->ip6po_rthdr, rthlen);
3010 case IPV6_USE_MIN_MTU:
3011 if (len != sizeof(int))
3013 minmtupolicy = *(int *)buf;
3014 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3015 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3016 minmtupolicy != IP6PO_MINMTU_ALL) {
3019 opt->ip6po_minmtu = minmtupolicy;
3023 if (len != sizeof(int))
3026 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3028 * we ignore this option for TCP sockets.
3029 * (RFC3542 leaves this case unspecified.)
3031 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3033 opt->ip6po_flags |= IP6PO_DONTFRAG;
3036 case IPV6_PREFER_TEMPADDR:
3037 if (len != sizeof(int))
3039 preftemp = *(int *)buf;
3040 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3041 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3042 preftemp != IP6PO_TEMPADDR_PREFER) {
3045 opt->ip6po_prefer_tempaddr = preftemp;
3049 return (ENOPROTOOPT);
3050 } /* end of switch */
3056 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3057 * packet to the input queue of a specified interface. Note that this
3058 * calls the output routine of the loopback "driver", but with an interface
3059 * pointer that might NOT be &loif -- easier than replicating that code here.
3062 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3065 struct ip6_hdr *ip6;
3067 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3072 * Make sure to deep-copy IPv6 header portion in case the data
3073 * is in an mbuf cluster, so that we can safely override the IPv6
3074 * header portion later.
3076 if (!M_WRITABLE(copym) ||
3077 copym->m_len < sizeof(struct ip6_hdr)) {
3078 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3082 ip6 = mtod(copym, struct ip6_hdr *);
3084 * clear embedded scope identifiers if necessary.
3085 * in6_clearscope will touch the addresses only when necessary.
3087 in6_clearscope(&ip6->ip6_src);
3088 in6_clearscope(&ip6->ip6_dst);
3089 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3090 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3092 copym->m_pkthdr.csum_data = 0xffff;
3094 if_simloop(ifp, copym, AF_INET6, 0);
3098 * Chop IPv6 header off from the payload.
3101 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3104 struct ip6_hdr *ip6;
3106 ip6 = mtod(m, struct ip6_hdr *);
3107 if (m->m_len > sizeof(*ip6)) {
3108 mh = m_gethdr(M_NOWAIT, MT_DATA);
3113 m_move_pkthdr(mh, m);
3114 M_ALIGN(mh, sizeof(*ip6));
3115 m->m_len -= sizeof(*ip6);
3116 m->m_data += sizeof(*ip6);
3119 m->m_len = sizeof(*ip6);
3120 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3122 exthdrs->ip6e_ip6 = m;
3127 * Compute IPv6 extension header length.
3130 ip6_optlen(struct inpcb *in6p)
3134 if (!in6p->in6p_outputopts)
3139 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3141 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3142 if (in6p->in6p_outputopts->ip6po_rthdr)
3143 /* dest1 is valid with rthdr only */
3144 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3145 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3146 len += elen(in6p->in6p_outputopts->ip6po_dest2);