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);
337 m->m_pkthdr.numa_domain = inp->inp_numa_domain;
341 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
343 * IPSec checking which handles several cases.
344 * FAST IPSEC: We re-injected the packet.
345 * XXX: need scope argument.
347 if (IPSEC_ENABLED(ipv6)) {
348 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
349 if (error == EINPROGRESS)
356 bzero(&exthdrs, sizeof(exthdrs));
358 /* Hop-by-Hop options header */
359 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
360 /* Destination options header(1st part) */
361 if (opt->ip6po_rthdr) {
363 * Destination options header(1st part)
364 * This only makes sense with a routing header.
365 * See Section 9.2 of RFC 3542.
366 * Disabling this part just for MIP6 convenience is
367 * a bad idea. We need to think carefully about a
368 * way to make the advanced API coexist with MIP6
369 * options, which might automatically be inserted in
372 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
375 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
376 /* Destination options header(2nd part) */
377 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
381 * Calculate the total length of the extension header chain.
382 * Keep the length of the unfragmentable part for fragmentation.
385 if (exthdrs.ip6e_hbh)
386 optlen += exthdrs.ip6e_hbh->m_len;
387 if (exthdrs.ip6e_dest1)
388 optlen += exthdrs.ip6e_dest1->m_len;
389 if (exthdrs.ip6e_rthdr)
390 optlen += exthdrs.ip6e_rthdr->m_len;
391 unfragpartlen = optlen + sizeof(struct ip6_hdr);
393 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
394 if (exthdrs.ip6e_dest2)
395 optlen += exthdrs.ip6e_dest2->m_len;
398 * If there is at least one extension header,
399 * separate IP6 header from the payload.
401 if (optlen && !hdrsplit) {
402 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
406 m = exthdrs.ip6e_ip6;
410 ip6 = mtod(m, struct ip6_hdr *);
412 /* adjust mbuf packet header length */
413 m->m_pkthdr.len += optlen;
414 plen = m->m_pkthdr.len - sizeof(*ip6);
416 /* If this is a jumbo payload, insert a jumbo payload option. */
417 if (plen > IPV6_MAXPACKET) {
419 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
423 m = exthdrs.ip6e_ip6;
427 ip6 = mtod(m, struct ip6_hdr *);
428 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
432 ip6->ip6_plen = htons(plen);
435 * Concatenate headers and fill in next header fields.
436 * Here we have, on "m"
438 * and we insert headers accordingly. Finally, we should be getting:
439 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
441 * during the header composing process, "m" points to IPv6 header.
442 * "mprev" points to an extension header prior to esp.
444 u_char *nexthdrp = &ip6->ip6_nxt;
448 * we treat dest2 specially. this makes IPsec processing
449 * much easier. the goal here is to make mprev point the
450 * mbuf prior to dest2.
452 * result: IPv6 dest2 payload
453 * m and mprev will point to IPv6 header.
455 if (exthdrs.ip6e_dest2) {
457 panic("assumption failed: hdr not split");
458 exthdrs.ip6e_dest2->m_next = m->m_next;
459 m->m_next = exthdrs.ip6e_dest2;
460 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
461 ip6->ip6_nxt = IPPROTO_DSTOPTS;
465 * result: IPv6 hbh dest1 rthdr dest2 payload
466 * m will point to IPv6 header. mprev will point to the
467 * extension header prior to dest2 (rthdr in the above case).
469 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
470 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
472 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
476 * If there is a routing header, discard the packet.
478 if (exthdrs.ip6e_rthdr) {
483 /* Source address validation */
484 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
485 (flags & IPV6_UNSPECSRC) == 0) {
487 IP6STAT_INC(ip6s_badscope);
490 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
492 IP6STAT_INC(ip6s_badscope);
496 IP6STAT_INC(ip6s_localout);
503 bzero((caddr_t)ro, sizeof(*ro));
506 if (opt && opt->ip6po_rthdr)
507 ro = &opt->ip6po_route;
508 dst = (struct sockaddr_in6 *)&ro->ro_dst;
509 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
512 * if specified, try to fill in the traffic class field.
513 * do not override if a non-zero value is already set.
514 * we check the diffserv field and the ecn field separately.
516 if (opt && opt->ip6po_tclass >= 0) {
519 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
521 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
524 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
527 /* fill in or override the hop limit field, if necessary. */
528 if (opt && opt->ip6po_hlim != -1)
529 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
530 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
532 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
534 ip6->ip6_hlim = V_ip6_defmcasthlim;
537 * Validate route against routing table additions;
538 * a better/more specific route might have been added.
539 * Make sure address family is set in route.
542 ro->ro_dst.sin6_family = AF_INET6;
543 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
545 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
546 ro->ro_dst.sin6_family == AF_INET6 &&
547 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
549 ifp = ro->ro_rt->rt_ifp;
552 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
554 if (fwd_tag == NULL) {
555 bzero(&dst_sa, sizeof(dst_sa));
556 dst_sa.sin6_family = AF_INET6;
557 dst_sa.sin6_len = sizeof(dst_sa);
558 dst_sa.sin6_addr = ip6->ip6_dst;
560 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
564 in6_ifstat_inc(ifp, ifs6_out_discard);
570 * If in6_selectroute() does not return a route entry,
571 * dst may not have been updated.
573 *dst = dst_sa; /* XXX */
577 * then rt (for unicast) and ifp must be non-NULL valid values.
579 if ((flags & IPV6_FORWARDING) == 0) {
580 /* XXX: the FORWARDING flag can be set for mrouting. */
581 in6_ifstat_inc(ifp, ifs6_out_request);
584 ia = (struct in6_ifaddr *)(rt->rt_ifa);
585 counter_u64_add(rt->rt_pksent, 1);
588 /* Setup data structures for scope ID checks. */
590 bzero(&src_sa, sizeof(src_sa));
591 src_sa.sin6_family = AF_INET6;
592 src_sa.sin6_len = sizeof(src_sa);
593 src_sa.sin6_addr = ip6->ip6_src;
596 /* re-initialize to be sure */
597 bzero(&dst_sa, sizeof(dst_sa));
598 dst_sa.sin6_family = AF_INET6;
599 dst_sa.sin6_len = sizeof(dst_sa);
600 dst_sa.sin6_addr = ip6->ip6_dst;
602 /* Check for valid scope ID. */
603 if (in6_setscope(&src0, ifp, &zone) == 0 &&
604 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
605 in6_setscope(&dst0, ifp, &zone) == 0 &&
606 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
608 * The outgoing interface is in the zone of the source
609 * and destination addresses.
611 * Because the loopback interface cannot receive
612 * packets with a different scope ID than its own,
613 * there is a trick is to pretend the outgoing packet
614 * was received by the real network interface, by
615 * setting "origifp" different from "ifp". This is
616 * only allowed when "ifp" is a loopback network
617 * interface. Refer to code in nd6_output_ifp() for
623 * We should use ia_ifp to support the case of sending
624 * packets to an address of our own.
626 if (ia != NULL && ia->ia_ifp)
629 } else if ((ifp->if_flags & IFF_LOOPBACK) == 0 ||
630 sa6_recoverscope(&src_sa) != 0 ||
631 sa6_recoverscope(&dst_sa) != 0 ||
632 dst_sa.sin6_scope_id == 0 ||
633 (src_sa.sin6_scope_id != 0 &&
634 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
635 (origifp = ifnet_byindex(dst_sa.sin6_scope_id)) == NULL) {
637 * If the destination network interface is not a
638 * loopback interface, or the destination network
639 * address has no scope ID, or the source address has
640 * a scope ID set which is different from the
641 * destination address one, or there is no network
642 * interface representing this scope ID, the address
643 * pair is considered invalid.
645 IP6STAT_INC(ip6s_badscope);
646 in6_ifstat_inc(ifp, ifs6_out_discard);
648 error = EHOSTUNREACH; /* XXX */
652 /* All scope ID checks are successful. */
654 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
655 if (opt && opt->ip6po_nextroute.ro_rt) {
657 * The nexthop is explicitly specified by the
658 * application. We assume the next hop is an IPv6
661 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
663 else if ((rt->rt_flags & RTF_GATEWAY))
664 dst = (struct sockaddr_in6 *)rt->rt_gateway;
667 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
668 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
670 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
671 in6_ifstat_inc(ifp, ifs6_out_mcast);
673 * Confirm that the outgoing interface supports multicast.
675 if (!(ifp->if_flags & IFF_MULTICAST)) {
676 IP6STAT_INC(ip6s_noroute);
677 in6_ifstat_inc(ifp, ifs6_out_discard);
681 if ((im6o == NULL && in6_mcast_loop) ||
682 (im6o && im6o->im6o_multicast_loop)) {
684 * Loop back multicast datagram if not expressly
685 * forbidden to do so, even if we have not joined
686 * the address; protocols will filter it later,
687 * thus deferring a hash lookup and lock acquisition
688 * at the expense of an m_copym().
690 ip6_mloopback(ifp, m);
693 * If we are acting as a multicast router, perform
694 * multicast forwarding as if the packet had just
695 * arrived on the interface to which we are about
696 * to send. The multicast forwarding function
697 * recursively calls this function, using the
698 * IPV6_FORWARDING flag to prevent infinite recursion.
700 * Multicasts that are looped back by ip6_mloopback(),
701 * above, will be forwarded by the ip6_input() routine,
704 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
706 * XXX: ip6_mforward expects that rcvif is NULL
707 * when it is called from the originating path.
708 * However, it may not always be the case.
710 m->m_pkthdr.rcvif = NULL;
711 if (ip6_mforward(ip6, ifp, m) != 0) {
718 * Multicasts with a hoplimit of zero may be looped back,
719 * above, but must not be transmitted on a network.
720 * Also, multicasts addressed to the loopback interface
721 * are not sent -- the above call to ip6_mloopback() will
722 * loop back a copy if this host actually belongs to the
723 * destination group on the loopback interface.
725 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
726 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
733 * Fill the outgoing inteface to tell the upper layer
734 * to increment per-interface statistics.
739 /* Determine path MTU. */
740 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
741 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
745 * The caller of this function may specify to use the minimum MTU
747 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
748 * setting. The logic is a bit complicated; by default, unicast
749 * packets will follow path MTU while multicast packets will be sent at
750 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
751 * including unicast ones will be sent at the minimum MTU. Multicast
752 * packets will always be sent at the minimum MTU unless
753 * IP6PO_MINMTU_DISABLE is explicitly specified.
754 * See RFC 3542 for more details.
756 if (mtu > IPV6_MMTU) {
757 if ((flags & IPV6_MINMTU))
759 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
761 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
763 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
769 * clear embedded scope identifiers if necessary.
770 * in6_clearscope will touch the addresses only when necessary.
772 in6_clearscope(&ip6->ip6_src);
773 in6_clearscope(&ip6->ip6_dst);
776 * If the outgoing packet contains a hop-by-hop options header,
777 * it must be examined and processed even by the source node.
778 * (RFC 2460, section 4.)
780 if (exthdrs.ip6e_hbh) {
781 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
782 u_int32_t dummy; /* XXX unused */
783 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
786 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
787 panic("ip6e_hbh is not contiguous");
790 * XXX: if we have to send an ICMPv6 error to the sender,
791 * we need the M_LOOP flag since icmp6_error() expects
792 * the IPv6 and the hop-by-hop options header are
793 * contiguous unless the flag is set.
795 m->m_flags |= M_LOOP;
796 m->m_pkthdr.rcvif = ifp;
797 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
798 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
799 &dummy, &plen) < 0) {
800 /* m was already freed at this point */
801 error = EINVAL;/* better error? */
804 m->m_flags &= ~M_LOOP; /* XXX */
805 m->m_pkthdr.rcvif = NULL;
808 /* Jump over all PFIL processing if hooks are not active. */
809 if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
813 /* Run through list of hooks for output packets. */
814 switch (pfil_run_hooks(V_inet6_pfil_head, &m, ifp, PFIL_OUT, inp)) {
816 ip6 = mtod(m, struct ip6_hdr *);
826 /* See if destination IP address was changed by packet filter. */
827 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
828 m->m_flags |= M_SKIP_FIREWALL;
829 /* If destination is now ourself drop to ip6_input(). */
830 if (in6_localip(&ip6->ip6_dst)) {
831 m->m_flags |= M_FASTFWD_OURS;
832 if (m->m_pkthdr.rcvif == NULL)
833 m->m_pkthdr.rcvif = V_loif;
834 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
835 m->m_pkthdr.csum_flags |=
836 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
837 m->m_pkthdr.csum_data = 0xffff;
840 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
841 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
843 error = netisr_queue(NETISR_IPV6, m);
846 RO_INVALIDATE_CACHE(ro);
847 needfiblookup = 1; /* Redo the routing table lookup. */
850 /* See if fib was changed by packet filter. */
851 if (fibnum != M_GETFIB(m)) {
852 m->m_flags |= M_SKIP_FIREWALL;
853 fibnum = M_GETFIB(m);
854 RO_INVALIDATE_CACHE(ro);
860 /* See if local, if yes, send it to netisr. */
861 if (m->m_flags & M_FASTFWD_OURS) {
862 if (m->m_pkthdr.rcvif == NULL)
863 m->m_pkthdr.rcvif = V_loif;
864 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
865 m->m_pkthdr.csum_flags |=
866 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
867 m->m_pkthdr.csum_data = 0xffff;
870 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
871 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
873 error = netisr_queue(NETISR_IPV6, m);
876 /* Or forward to some other address? */
877 if ((m->m_flags & M_IP6_NEXTHOP) &&
878 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
879 dst = (struct sockaddr_in6 *)&ro->ro_dst;
880 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
881 m->m_flags |= M_SKIP_FIREWALL;
882 m->m_flags &= ~M_IP6_NEXTHOP;
883 m_tag_delete(m, fwd_tag);
889 * Send the packet to the outgoing interface.
890 * If necessary, do IPv6 fragmentation before sending.
892 * the logic here is rather complex:
893 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
894 * 1-a: send as is if tlen <= path mtu
895 * 1-b: fragment if tlen > path mtu
897 * 2: if user asks us not to fragment (dontfrag == 1)
898 * 2-a: send as is if tlen <= interface mtu
899 * 2-b: error if tlen > interface mtu
901 * 3: if we always need to attach fragment header (alwaysfrag == 1)
904 * 4: if dontfrag == 1 && alwaysfrag == 1
905 * error, as we cannot handle this conflicting request
907 sw_csum = m->m_pkthdr.csum_flags;
909 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
910 sw_csum &= ~ifp->if_hwassist;
914 * If we added extension headers, we will not do TSO and calculate the
915 * checksums ourselves for now.
916 * XXX-BZ Need a framework to know when the NIC can handle it, even
919 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
920 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
921 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
924 if (sw_csum & CSUM_SCTP_IPV6) {
925 sw_csum &= ~CSUM_SCTP_IPV6;
926 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
929 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
930 tlen = m->m_pkthdr.len;
932 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
936 if (dontfrag && alwaysfrag) { /* case 4 */
937 /* conflicting request - can't transmit */
941 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
943 * Even if the DONTFRAG option is specified, we cannot send the
944 * packet when the data length is larger than the MTU of the
945 * outgoing interface.
946 * Notify the error by sending IPV6_PATHMTU ancillary data if
947 * application wanted to know the MTU value. Also return an
948 * error code (this is not described in the API spec).
951 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
957 * transmit packet without fragmentation
959 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
960 struct in6_ifaddr *ia6;
962 ip6 = mtod(m, struct ip6_hdr *);
963 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
965 /* Record statistics for this interface address. */
966 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
967 counter_u64_add(ia6->ia_ifa.ifa_obytes,
969 ifa_free(&ia6->ia_ifa);
973 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
974 in_pcboutput_txrtlmt(inp, ifp, m);
975 /* stamp send tag on mbuf */
976 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
978 m->m_pkthdr.snd_tag = NULL;
981 error = nd6_output_ifp(ifp, origifp, m, dst,
984 /* check for route change */
986 in_pcboutput_eagain(inp);
992 * try to fragment the packet. case 1-b and 3
994 if (mtu < IPV6_MMTU) {
995 /* path MTU cannot be less than IPV6_MMTU */
997 in6_ifstat_inc(ifp, ifs6_out_fragfail);
999 } else if (ip6->ip6_plen == 0) {
1000 /* jumbo payload cannot be fragmented */
1002 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1008 * Too large for the destination or interface;
1009 * fragment if possible.
1010 * Must be able to put at least 8 bytes per fragment.
1012 hlen = unfragpartlen;
1013 if (mtu > IPV6_MAXPACKET)
1014 mtu = IPV6_MAXPACKET;
1016 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1019 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1024 * If the interface will not calculate checksums on
1025 * fragmented packets, then do it here.
1026 * XXX-BZ handle the hw offloading case. Need flags.
1028 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1029 in6_delayed_cksum(m, plen, hlen);
1030 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1033 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1034 sctp_delayed_cksum(m, hlen);
1035 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1039 * Change the next header field of the last header in the
1040 * unfragmentable part.
1042 if (exthdrs.ip6e_rthdr) {
1043 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1044 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1045 } else if (exthdrs.ip6e_dest1) {
1046 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1047 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1048 } else if (exthdrs.ip6e_hbh) {
1049 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1050 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1052 nextproto = ip6->ip6_nxt;
1053 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1057 * Loop through length of segment after first fragment,
1058 * make new header and copy data of each part and link onto
1062 id = htonl(ip6_randomid());
1063 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1066 in6_ifstat_inc(ifp, ifs6_out_fragok);
1070 * Remove leading garbages.
1080 /* Record statistics for this interface address. */
1082 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1083 counter_u64_add(ia->ia_ifa.ifa_obytes,
1088 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
1089 in_pcboutput_txrtlmt(inp, ifp, m);
1090 /* stamp send tag on mbuf */
1091 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
1093 m->m_pkthdr.snd_tag = NULL;
1096 error = nd6_output_ifp(ifp, origifp, m, dst,
1097 (struct route *)ro);
1099 /* check for route change */
1100 if (error == EAGAIN)
1101 in_pcboutput_eagain(inp);
1108 IP6STAT_INC(ip6s_fragmented);
1111 if (ro == &ip6route)
1116 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1117 m_freem(exthdrs.ip6e_dest1);
1118 m_freem(exthdrs.ip6e_rthdr);
1119 m_freem(exthdrs.ip6e_dest2);
1128 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1132 if (hlen > MCLBYTES)
1133 return (ENOBUFS); /* XXX */
1136 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1138 m = m_get(M_NOWAIT, MT_DATA);
1143 bcopy(hdr, mtod(m, caddr_t), hlen);
1150 * Insert jumbo payload option.
1153 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1159 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1162 * If there is no hop-by-hop options header, allocate new one.
1163 * If there is one but it doesn't have enough space to store the
1164 * jumbo payload option, allocate a cluster to store the whole options.
1165 * Otherwise, use it to store the options.
1167 if (exthdrs->ip6e_hbh == NULL) {
1168 mopt = m_get(M_NOWAIT, MT_DATA);
1171 mopt->m_len = JUMBOOPTLEN;
1172 optbuf = mtod(mopt, u_char *);
1173 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1174 exthdrs->ip6e_hbh = mopt;
1176 struct ip6_hbh *hbh;
1178 mopt = exthdrs->ip6e_hbh;
1179 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1182 * - exthdrs->ip6e_hbh is not referenced from places
1183 * other than exthdrs.
1184 * - exthdrs->ip6e_hbh is not an mbuf chain.
1186 int oldoptlen = mopt->m_len;
1190 * XXX: give up if the whole (new) hbh header does
1191 * not fit even in an mbuf cluster.
1193 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1197 * As a consequence, we must always prepare a cluster
1200 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1203 n->m_len = oldoptlen + JUMBOOPTLEN;
1204 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1206 optbuf = mtod(n, caddr_t) + oldoptlen;
1208 mopt = exthdrs->ip6e_hbh = n;
1210 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1211 mopt->m_len += JUMBOOPTLEN;
1213 optbuf[0] = IP6OPT_PADN;
1217 * Adjust the header length according to the pad and
1218 * the jumbo payload option.
1220 hbh = mtod(mopt, struct ip6_hbh *);
1221 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1224 /* fill in the option. */
1225 optbuf[2] = IP6OPT_JUMBO;
1227 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1228 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1230 /* finally, adjust the packet header length */
1231 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1238 * Insert fragment header and copy unfragmentable header portions.
1241 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1242 struct ip6_frag **frghdrp)
1244 struct mbuf *n, *mlast;
1246 if (hlen > sizeof(struct ip6_hdr)) {
1247 n = m_copym(m0, sizeof(struct ip6_hdr),
1248 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1255 /* Search for the last mbuf of unfragmentable part. */
1256 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1259 if (M_WRITABLE(mlast) &&
1260 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1261 /* use the trailing space of the last mbuf for the fragment hdr */
1262 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1264 mlast->m_len += sizeof(struct ip6_frag);
1265 m->m_pkthdr.len += sizeof(struct ip6_frag);
1267 /* allocate a new mbuf for the fragment header */
1270 mfrg = m_get(M_NOWAIT, MT_DATA);
1273 mfrg->m_len = sizeof(struct ip6_frag);
1274 *frghdrp = mtod(mfrg, struct ip6_frag *);
1275 mlast->m_next = mfrg;
1282 * Calculates IPv6 path mtu for destination @dst.
1283 * Resulting MTU is stored in @mtup.
1285 * Returns 0 on success.
1288 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1290 struct nhop6_extended nh6;
1291 struct in6_addr kdst;
1297 in6_splitscope(dst, &kdst, &scopeid);
1298 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1299 return (EHOSTUNREACH);
1304 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1305 fib6_free_nh_ext(fibnum, &nh6);
1311 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1312 * and cached data in @ro_pmtu.
1313 * MTU from (successful) route lookup is saved (along with dst)
1314 * inside @ro_pmtu to avoid subsequent route lookups after packet
1315 * filter processing.
1317 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1318 * Returns 0 on success.
1321 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1322 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1323 int *alwaysfragp, u_int fibnum, u_int proto)
1325 struct nhop6_basic nh6;
1326 struct in6_addr kdst;
1328 struct sockaddr_in6 *sa6_dst;
1335 * Here ro_pmtu has final destination address, while
1336 * ro might represent immediate destination.
1337 * Use ro_pmtu destination since mtu might differ.
1339 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1340 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1341 ro_pmtu->ro_mtu = 0;
1343 if (ro_pmtu->ro_mtu == 0) {
1344 bzero(sa6_dst, sizeof(*sa6_dst));
1345 sa6_dst->sin6_family = AF_INET6;
1346 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1347 sa6_dst->sin6_addr = *dst;
1349 in6_splitscope(dst, &kdst, &scopeid);
1350 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1352 ro_pmtu->ro_mtu = nh6.nh_mtu;
1355 mtu = ro_pmtu->ro_mtu;
1359 mtu = ro_pmtu->ro_rt->rt_mtu;
1361 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1365 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1366 * hostcache data for @dst.
1367 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1369 * Returns 0 on success.
1372 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1373 u_long *mtup, int *alwaysfragp, u_int proto)
1381 struct in_conninfo inc;
1383 bzero(&inc, sizeof(inc));
1384 inc.inc_flags |= INC_ISIPV6;
1385 inc.inc6_faddr = *dst;
1387 ifmtu = IN6_LINKMTU(ifp);
1389 /* TCP is known to react to pmtu changes so skip hc */
1390 if (proto != IPPROTO_TCP)
1391 mtu = tcp_hc_getmtu(&inc);
1394 mtu = min(mtu, rt_mtu);
1399 else if (mtu < IPV6_MMTU) {
1401 * RFC2460 section 5, last paragraph:
1402 * if we record ICMPv6 too big message with
1403 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1404 * or smaller, with framgent header attached.
1405 * (fragment header is needed regardless from the
1406 * packet size, for translators to identify packets)
1412 mtu = IN6_LINKMTU(ifp);
1414 error = EHOSTUNREACH; /* XXX */
1418 *alwaysfragp = alwaysfrag;
1423 * IP6 socket option processing.
1426 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1428 int optdatalen, uproto;
1430 struct inpcb *in6p = sotoinpcb(so);
1432 int level, op, optname;
1436 uint32_t rss_bucket;
1441 * Don't use more than a quarter of mbuf clusters. N.B.:
1442 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1443 * on LP64 architectures, so cast to u_long to avoid undefined
1444 * behavior. ILP32 architectures cannot have nmbclusters
1445 * large enough to overflow for other reasons.
1447 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1449 level = sopt->sopt_level;
1450 op = sopt->sopt_dir;
1451 optname = sopt->sopt_name;
1452 optlen = sopt->sopt_valsize;
1456 uproto = (int)so->so_proto->pr_protocol;
1458 if (level != IPPROTO_IPV6) {
1461 if (sopt->sopt_level == SOL_SOCKET &&
1462 sopt->sopt_dir == SOPT_SET) {
1463 switch (sopt->sopt_name) {
1466 if ((so->so_options & SO_REUSEADDR) != 0)
1467 in6p->inp_flags2 |= INP_REUSEADDR;
1469 in6p->inp_flags2 &= ~INP_REUSEADDR;
1475 if ((so->so_options & SO_REUSEPORT) != 0)
1476 in6p->inp_flags2 |= INP_REUSEPORT;
1478 in6p->inp_flags2 &= ~INP_REUSEPORT;
1482 case SO_REUSEPORT_LB:
1484 if ((so->so_options & SO_REUSEPORT_LB) != 0)
1485 in6p->inp_flags2 |= INP_REUSEPORT_LB;
1487 in6p->inp_flags2 &= ~INP_REUSEPORT_LB;
1493 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1497 case SO_MAX_PACING_RATE:
1500 in6p->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1511 } else { /* level == IPPROTO_IPV6 */
1516 case IPV6_2292PKTOPTIONS:
1517 #ifdef IPV6_PKTOPTIONS
1518 case IPV6_PKTOPTIONS:
1523 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1524 printf("ip6_ctloutput: mbuf limit hit\n");
1529 error = soopt_getm(sopt, &m); /* XXX */
1532 error = soopt_mcopyin(sopt, m); /* XXX */
1535 error = ip6_pcbopts(&in6p->in6p_outputopts,
1537 m_freem(m); /* XXX */
1542 * Use of some Hop-by-Hop options or some
1543 * Destination options, might require special
1544 * privilege. That is, normal applications
1545 * (without special privilege) might be forbidden
1546 * from setting certain options in outgoing packets,
1547 * and might never see certain options in received
1548 * packets. [RFC 2292 Section 6]
1549 * KAME specific note:
1550 * KAME prevents non-privileged users from sending or
1551 * receiving ANY hbh/dst options in order to avoid
1552 * overhead of parsing options in the kernel.
1554 case IPV6_RECVHOPOPTS:
1555 case IPV6_RECVDSTOPTS:
1556 case IPV6_RECVRTHDRDSTOPTS:
1558 error = priv_check(td,
1559 PRIV_NETINET_SETHDROPTS);
1564 case IPV6_UNICAST_HOPS:
1567 case IPV6_RECVPKTINFO:
1568 case IPV6_RECVHOPLIMIT:
1569 case IPV6_RECVRTHDR:
1570 case IPV6_RECVPATHMTU:
1571 case IPV6_RECVTCLASS:
1572 case IPV6_RECVFLOWID:
1574 case IPV6_RECVRSSBUCKETID:
1577 case IPV6_AUTOFLOWLABEL:
1578 case IPV6_ORIGDSTADDR:
1580 case IPV6_BINDMULTI:
1582 case IPV6_RSS_LISTEN_BUCKET:
1584 if (optname == IPV6_BINDANY && td != NULL) {
1585 error = priv_check(td,
1586 PRIV_NETINET_BINDANY);
1591 if (optlen != sizeof(int)) {
1595 error = sooptcopyin(sopt, &optval,
1596 sizeof optval, sizeof optval);
1601 case IPV6_UNICAST_HOPS:
1602 if (optval < -1 || optval >= 256)
1605 /* -1 = kernel default */
1606 in6p->in6p_hops = optval;
1607 if ((in6p->inp_vflag &
1609 in6p->inp_ip_ttl = optval;
1612 #define OPTSET(bit) \
1616 in6p->inp_flags |= (bit); \
1618 in6p->inp_flags &= ~(bit); \
1619 INP_WUNLOCK(in6p); \
1620 } while (/*CONSTCOND*/ 0)
1621 #define OPTSET2292(bit) \
1624 in6p->inp_flags |= IN6P_RFC2292; \
1626 in6p->inp_flags |= (bit); \
1628 in6p->inp_flags &= ~(bit); \
1629 INP_WUNLOCK(in6p); \
1630 } while (/*CONSTCOND*/ 0)
1631 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1633 #define OPTSET2_N(bit, val) do { \
1635 in6p->inp_flags2 |= bit; \
1637 in6p->inp_flags2 &= ~bit; \
1639 #define OPTSET2(bit, val) do { \
1641 OPTSET2_N(bit, val); \
1642 INP_WUNLOCK(in6p); \
1644 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1645 #define OPTSET2292_EXCLUSIVE(bit) \
1648 if (OPTBIT(IN6P_RFC2292)) { \
1652 in6p->inp_flags |= (bit); \
1654 in6p->inp_flags &= ~(bit); \
1656 INP_WUNLOCK(in6p); \
1657 } while (/*CONSTCOND*/ 0)
1659 case IPV6_RECVPKTINFO:
1660 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1665 struct ip6_pktopts **optp;
1667 /* cannot mix with RFC2292 */
1668 if (OPTBIT(IN6P_RFC2292)) {
1673 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1675 return (ECONNRESET);
1677 optp = &in6p->in6p_outputopts;
1678 error = ip6_pcbopt(IPV6_HOPLIMIT,
1679 (u_char *)&optval, sizeof(optval),
1680 optp, (td != NULL) ? td->td_ucred :
1686 case IPV6_RECVHOPLIMIT:
1687 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1690 case IPV6_RECVHOPOPTS:
1691 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1694 case IPV6_RECVDSTOPTS:
1695 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1698 case IPV6_RECVRTHDRDSTOPTS:
1699 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1702 case IPV6_RECVRTHDR:
1703 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1706 case IPV6_RECVPATHMTU:
1708 * We ignore this option for TCP
1710 * (RFC3542 leaves this case
1713 if (uproto != IPPROTO_TCP)
1717 case IPV6_RECVFLOWID:
1718 OPTSET2(INP_RECVFLOWID, optval);
1722 case IPV6_RECVRSSBUCKETID:
1723 OPTSET2(INP_RECVRSSBUCKETID, optval);
1729 * make setsockopt(IPV6_V6ONLY)
1730 * available only prior to bind(2).
1731 * see ipng mailing list, Jun 22 2001.
1733 if (in6p->inp_lport ||
1734 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1738 OPTSET(IN6P_IPV6_V6ONLY);
1740 in6p->inp_vflag &= ~INP_IPV4;
1742 in6p->inp_vflag |= INP_IPV4;
1744 case IPV6_RECVTCLASS:
1745 /* cannot mix with RFC2292 XXX */
1746 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1748 case IPV6_AUTOFLOWLABEL:
1749 OPTSET(IN6P_AUTOFLOWLABEL);
1752 case IPV6_ORIGDSTADDR:
1753 OPTSET2(INP_ORIGDSTADDR, optval);
1756 OPTSET(INP_BINDANY);
1759 case IPV6_BINDMULTI:
1760 OPTSET2(INP_BINDMULTI, optval);
1763 case IPV6_RSS_LISTEN_BUCKET:
1764 if ((optval >= 0) &&
1765 (optval < rss_getnumbuckets())) {
1767 in6p->inp_rss_listen_bucket = optval;
1768 OPTSET2_N(INP_RSS_BUCKET_SET, 1);
1780 case IPV6_USE_MIN_MTU:
1781 case IPV6_PREFER_TEMPADDR:
1782 if (optlen != sizeof(optval)) {
1786 error = sooptcopyin(sopt, &optval,
1787 sizeof optval, sizeof optval);
1791 struct ip6_pktopts **optp;
1793 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1795 return (ECONNRESET);
1797 optp = &in6p->in6p_outputopts;
1798 error = ip6_pcbopt(optname,
1799 (u_char *)&optval, sizeof(optval),
1800 optp, (td != NULL) ? td->td_ucred :
1806 case IPV6_2292PKTINFO:
1807 case IPV6_2292HOPLIMIT:
1808 case IPV6_2292HOPOPTS:
1809 case IPV6_2292DSTOPTS:
1810 case IPV6_2292RTHDR:
1812 if (optlen != sizeof(int)) {
1816 error = sooptcopyin(sopt, &optval,
1817 sizeof optval, sizeof optval);
1821 case IPV6_2292PKTINFO:
1822 OPTSET2292(IN6P_PKTINFO);
1824 case IPV6_2292HOPLIMIT:
1825 OPTSET2292(IN6P_HOPLIMIT);
1827 case IPV6_2292HOPOPTS:
1829 * Check super-user privilege.
1830 * See comments for IPV6_RECVHOPOPTS.
1833 error = priv_check(td,
1834 PRIV_NETINET_SETHDROPTS);
1838 OPTSET2292(IN6P_HOPOPTS);
1840 case IPV6_2292DSTOPTS:
1842 error = priv_check(td,
1843 PRIV_NETINET_SETHDROPTS);
1847 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1849 case IPV6_2292RTHDR:
1850 OPTSET2292(IN6P_RTHDR);
1858 case IPV6_RTHDRDSTOPTS:
1861 /* new advanced API (RFC3542) */
1863 u_char optbuf_storage[MCLBYTES];
1865 struct ip6_pktopts **optp;
1867 /* cannot mix with RFC2292 */
1868 if (OPTBIT(IN6P_RFC2292)) {
1874 * We only ensure valsize is not too large
1875 * here. Further validation will be done
1878 error = sooptcopyin(sopt, optbuf_storage,
1879 sizeof(optbuf_storage), 0);
1882 optlen = sopt->sopt_valsize;
1883 optbuf = optbuf_storage;
1885 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1887 return (ECONNRESET);
1889 optp = &in6p->in6p_outputopts;
1890 error = ip6_pcbopt(optname, optbuf, optlen,
1891 optp, (td != NULL) ? td->td_ucred : NULL,
1898 case IPV6_MULTICAST_IF:
1899 case IPV6_MULTICAST_HOPS:
1900 case IPV6_MULTICAST_LOOP:
1901 case IPV6_JOIN_GROUP:
1902 case IPV6_LEAVE_GROUP:
1904 case MCAST_BLOCK_SOURCE:
1905 case MCAST_UNBLOCK_SOURCE:
1906 case MCAST_JOIN_GROUP:
1907 case MCAST_LEAVE_GROUP:
1908 case MCAST_JOIN_SOURCE_GROUP:
1909 case MCAST_LEAVE_SOURCE_GROUP:
1910 error = ip6_setmoptions(in6p, sopt);
1913 case IPV6_PORTRANGE:
1914 error = sooptcopyin(sopt, &optval,
1915 sizeof optval, sizeof optval);
1921 case IPV6_PORTRANGE_DEFAULT:
1922 in6p->inp_flags &= ~(INP_LOWPORT);
1923 in6p->inp_flags &= ~(INP_HIGHPORT);
1926 case IPV6_PORTRANGE_HIGH:
1927 in6p->inp_flags &= ~(INP_LOWPORT);
1928 in6p->inp_flags |= INP_HIGHPORT;
1931 case IPV6_PORTRANGE_LOW:
1932 in6p->inp_flags &= ~(INP_HIGHPORT);
1933 in6p->inp_flags |= INP_LOWPORT;
1943 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1944 case IPV6_IPSEC_POLICY:
1945 if (IPSEC_ENABLED(ipv6)) {
1946 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
1953 error = ENOPROTOOPT;
1961 case IPV6_2292PKTOPTIONS:
1962 #ifdef IPV6_PKTOPTIONS
1963 case IPV6_PKTOPTIONS:
1966 * RFC3542 (effectively) deprecated the
1967 * semantics of the 2292-style pktoptions.
1968 * Since it was not reliable in nature (i.e.,
1969 * applications had to expect the lack of some
1970 * information after all), it would make sense
1971 * to simplify this part by always returning
1974 sopt->sopt_valsize = 0;
1977 case IPV6_RECVHOPOPTS:
1978 case IPV6_RECVDSTOPTS:
1979 case IPV6_RECVRTHDRDSTOPTS:
1980 case IPV6_UNICAST_HOPS:
1981 case IPV6_RECVPKTINFO:
1982 case IPV6_RECVHOPLIMIT:
1983 case IPV6_RECVRTHDR:
1984 case IPV6_RECVPATHMTU:
1987 case IPV6_PORTRANGE:
1988 case IPV6_RECVTCLASS:
1989 case IPV6_AUTOFLOWLABEL:
1993 case IPV6_RECVFLOWID:
1995 case IPV6_RSSBUCKETID:
1996 case IPV6_RECVRSSBUCKETID:
1998 case IPV6_BINDMULTI:
2001 case IPV6_RECVHOPOPTS:
2002 optval = OPTBIT(IN6P_HOPOPTS);
2005 case IPV6_RECVDSTOPTS:
2006 optval = OPTBIT(IN6P_DSTOPTS);
2009 case IPV6_RECVRTHDRDSTOPTS:
2010 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2013 case IPV6_UNICAST_HOPS:
2014 optval = in6p->in6p_hops;
2017 case IPV6_RECVPKTINFO:
2018 optval = OPTBIT(IN6P_PKTINFO);
2021 case IPV6_RECVHOPLIMIT:
2022 optval = OPTBIT(IN6P_HOPLIMIT);
2025 case IPV6_RECVRTHDR:
2026 optval = OPTBIT(IN6P_RTHDR);
2029 case IPV6_RECVPATHMTU:
2030 optval = OPTBIT(IN6P_MTU);
2034 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2037 case IPV6_PORTRANGE:
2040 flags = in6p->inp_flags;
2041 if (flags & INP_HIGHPORT)
2042 optval = IPV6_PORTRANGE_HIGH;
2043 else if (flags & INP_LOWPORT)
2044 optval = IPV6_PORTRANGE_LOW;
2049 case IPV6_RECVTCLASS:
2050 optval = OPTBIT(IN6P_TCLASS);
2053 case IPV6_AUTOFLOWLABEL:
2054 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2057 case IPV6_ORIGDSTADDR:
2058 optval = OPTBIT2(INP_ORIGDSTADDR);
2062 optval = OPTBIT(INP_BINDANY);
2066 optval = in6p->inp_flowid;
2070 optval = in6p->inp_flowtype;
2073 case IPV6_RECVFLOWID:
2074 optval = OPTBIT2(INP_RECVFLOWID);
2077 case IPV6_RSSBUCKETID:
2079 rss_hash2bucket(in6p->inp_flowid,
2083 optval = rss_bucket;
2088 case IPV6_RECVRSSBUCKETID:
2089 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2093 case IPV6_BINDMULTI:
2094 optval = OPTBIT2(INP_BINDMULTI);
2100 error = sooptcopyout(sopt, &optval,
2107 struct ip6_mtuinfo mtuinfo;
2108 struct in6_addr addr;
2110 if (!(so->so_state & SS_ISCONNECTED))
2113 * XXX: we dot not consider the case of source
2114 * routing, or optional information to specify
2115 * the outgoing interface.
2116 * Copy faddr out of in6p to avoid holding lock
2117 * on inp during route lookup.
2120 bcopy(&in6p->in6p_faddr, &addr, sizeof(addr));
2122 error = ip6_getpmtu_ctl(so->so_fibnum,
2126 if (pmtu > IPV6_MAXPACKET)
2127 pmtu = IPV6_MAXPACKET;
2129 bzero(&mtuinfo, sizeof(mtuinfo));
2130 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2131 optdata = (void *)&mtuinfo;
2132 optdatalen = sizeof(mtuinfo);
2133 error = sooptcopyout(sopt, optdata,
2138 case IPV6_2292PKTINFO:
2139 case IPV6_2292HOPLIMIT:
2140 case IPV6_2292HOPOPTS:
2141 case IPV6_2292RTHDR:
2142 case IPV6_2292DSTOPTS:
2144 case IPV6_2292PKTINFO:
2145 optval = OPTBIT(IN6P_PKTINFO);
2147 case IPV6_2292HOPLIMIT:
2148 optval = OPTBIT(IN6P_HOPLIMIT);
2150 case IPV6_2292HOPOPTS:
2151 optval = OPTBIT(IN6P_HOPOPTS);
2153 case IPV6_2292RTHDR:
2154 optval = OPTBIT(IN6P_RTHDR);
2156 case IPV6_2292DSTOPTS:
2157 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2160 error = sooptcopyout(sopt, &optval,
2167 case IPV6_RTHDRDSTOPTS:
2171 case IPV6_USE_MIN_MTU:
2172 case IPV6_PREFER_TEMPADDR:
2173 error = ip6_getpcbopt(in6p, optname, sopt);
2176 case IPV6_MULTICAST_IF:
2177 case IPV6_MULTICAST_HOPS:
2178 case IPV6_MULTICAST_LOOP:
2180 error = ip6_getmoptions(in6p, sopt);
2183 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2184 case IPV6_IPSEC_POLICY:
2185 if (IPSEC_ENABLED(ipv6)) {
2186 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
2192 error = ENOPROTOOPT;
2202 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2204 int error = 0, optval, optlen;
2205 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2206 struct inpcb *in6p = sotoinpcb(so);
2207 int level, op, optname;
2209 level = sopt->sopt_level;
2210 op = sopt->sopt_dir;
2211 optname = sopt->sopt_name;
2212 optlen = sopt->sopt_valsize;
2214 if (level != IPPROTO_IPV6) {
2221 * For ICMPv6 sockets, no modification allowed for checksum
2222 * offset, permit "no change" values to help existing apps.
2224 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2225 * for an ICMPv6 socket will fail."
2226 * The current behavior does not meet RFC3542.
2230 if (optlen != sizeof(int)) {
2234 error = sooptcopyin(sopt, &optval, sizeof(optval),
2238 if (optval < -1 || (optval % 2) != 0) {
2240 * The API assumes non-negative even offset
2241 * values or -1 as a special value.
2244 } else if (so->so_proto->pr_protocol ==
2246 if (optval != icmp6off)
2249 in6p->in6p_cksum = optval;
2253 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2256 optval = in6p->in6p_cksum;
2258 error = sooptcopyout(sopt, &optval, sizeof(optval));
2268 error = ENOPROTOOPT;
2276 * Set up IP6 options in pcb for insertion in output packets or
2277 * specifying behavior of outgoing packets.
2280 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2281 struct socket *so, struct sockopt *sopt)
2283 struct ip6_pktopts *opt = *pktopt;
2285 struct thread *td = sopt->sopt_td;
2287 /* turn off any old options. */
2290 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2291 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2292 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2293 printf("ip6_pcbopts: all specified options are cleared.\n");
2295 ip6_clearpktopts(opt, -1);
2297 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2300 if (!m || m->m_len == 0) {
2302 * Only turning off any previous options, regardless of
2303 * whether the opt is just created or given.
2305 free(opt, M_IP6OPT);
2309 /* set options specified by user. */
2310 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2311 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2312 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2313 free(opt, M_IP6OPT);
2321 * initialize ip6_pktopts. beware that there are non-zero default values in
2325 ip6_initpktopts(struct ip6_pktopts *opt)
2328 bzero(opt, sizeof(*opt));
2329 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2330 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2331 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2332 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2336 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2337 struct ucred *cred, int uproto)
2339 struct ip6_pktopts *opt;
2341 if (*pktopt == NULL) {
2342 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2344 if (*pktopt == NULL)
2346 ip6_initpktopts(*pktopt);
2350 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2353 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2354 if (pktopt && pktopt->field) { \
2355 INP_RUNLOCK(in6p); \
2356 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2357 malloc_optdata = true; \
2359 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
2360 INP_RUNLOCK(in6p); \
2361 free(optdata, M_TEMP); \
2362 return (ECONNRESET); \
2364 pktopt = in6p->in6p_outputopts; \
2365 if (pktopt && pktopt->field) { \
2366 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2367 bcopy(&pktopt->field, optdata, optdatalen); \
2369 free(optdata, M_TEMP); \
2371 malloc_optdata = false; \
2376 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2377 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2379 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2380 pktopt->field->sa_len)
2383 ip6_getpcbopt(struct inpcb *in6p, int optname, struct sockopt *sopt)
2385 void *optdata = NULL;
2386 bool malloc_optdata = false;
2389 struct in6_pktinfo null_pktinfo;
2390 int deftclass = 0, on;
2391 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2392 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2393 struct ip6_pktopts *pktopt;
2396 pktopt = in6p->in6p_outputopts;
2400 optdata = (void *)&null_pktinfo;
2401 if (pktopt && pktopt->ip6po_pktinfo) {
2402 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2403 sizeof(null_pktinfo));
2404 in6_clearscope(&null_pktinfo.ipi6_addr);
2406 /* XXX: we don't have to do this every time... */
2407 bzero(&null_pktinfo, sizeof(null_pktinfo));
2409 optdatalen = sizeof(struct in6_pktinfo);
2412 if (pktopt && pktopt->ip6po_tclass >= 0)
2413 deftclass = pktopt->ip6po_tclass;
2414 optdata = (void *)&deftclass;
2415 optdatalen = sizeof(int);
2418 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2421 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2423 case IPV6_RTHDRDSTOPTS:
2424 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2427 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2430 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2432 case IPV6_USE_MIN_MTU:
2434 defminmtu = pktopt->ip6po_minmtu;
2435 optdata = (void *)&defminmtu;
2436 optdatalen = sizeof(int);
2439 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2443 optdata = (void *)&on;
2444 optdatalen = sizeof(on);
2446 case IPV6_PREFER_TEMPADDR:
2448 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2449 optdata = (void *)&defpreftemp;
2450 optdatalen = sizeof(int);
2452 default: /* should not happen */
2454 panic("ip6_getpcbopt: unexpected option\n");
2457 return (ENOPROTOOPT);
2461 error = sooptcopyout(sopt, optdata, optdatalen);
2463 free(optdata, M_TEMP);
2469 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2474 if (optname == -1 || optname == IPV6_PKTINFO) {
2475 if (pktopt->ip6po_pktinfo)
2476 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2477 pktopt->ip6po_pktinfo = NULL;
2479 if (optname == -1 || optname == IPV6_HOPLIMIT)
2480 pktopt->ip6po_hlim = -1;
2481 if (optname == -1 || optname == IPV6_TCLASS)
2482 pktopt->ip6po_tclass = -1;
2483 if (optname == -1 || optname == IPV6_NEXTHOP) {
2484 if (pktopt->ip6po_nextroute.ro_rt) {
2485 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2486 pktopt->ip6po_nextroute.ro_rt = NULL;
2488 if (pktopt->ip6po_nexthop)
2489 free(pktopt->ip6po_nexthop, M_IP6OPT);
2490 pktopt->ip6po_nexthop = NULL;
2492 if (optname == -1 || optname == IPV6_HOPOPTS) {
2493 if (pktopt->ip6po_hbh)
2494 free(pktopt->ip6po_hbh, M_IP6OPT);
2495 pktopt->ip6po_hbh = NULL;
2497 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2498 if (pktopt->ip6po_dest1)
2499 free(pktopt->ip6po_dest1, M_IP6OPT);
2500 pktopt->ip6po_dest1 = NULL;
2502 if (optname == -1 || optname == IPV6_RTHDR) {
2503 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2504 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2505 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2506 if (pktopt->ip6po_route.ro_rt) {
2507 RTFREE(pktopt->ip6po_route.ro_rt);
2508 pktopt->ip6po_route.ro_rt = NULL;
2511 if (optname == -1 || optname == IPV6_DSTOPTS) {
2512 if (pktopt->ip6po_dest2)
2513 free(pktopt->ip6po_dest2, M_IP6OPT);
2514 pktopt->ip6po_dest2 = NULL;
2518 #define PKTOPT_EXTHDRCPY(type) \
2521 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2522 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2523 if (dst->type == NULL)\
2525 bcopy(src->type, dst->type, hlen);\
2527 } while (/*CONSTCOND*/ 0)
2530 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2532 if (dst == NULL || src == NULL) {
2533 printf("ip6_clearpktopts: invalid argument\n");
2537 dst->ip6po_hlim = src->ip6po_hlim;
2538 dst->ip6po_tclass = src->ip6po_tclass;
2539 dst->ip6po_flags = src->ip6po_flags;
2540 dst->ip6po_minmtu = src->ip6po_minmtu;
2541 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2542 if (src->ip6po_pktinfo) {
2543 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2545 if (dst->ip6po_pktinfo == NULL)
2547 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2549 if (src->ip6po_nexthop) {
2550 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2552 if (dst->ip6po_nexthop == NULL)
2554 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2555 src->ip6po_nexthop->sa_len);
2557 PKTOPT_EXTHDRCPY(ip6po_hbh);
2558 PKTOPT_EXTHDRCPY(ip6po_dest1);
2559 PKTOPT_EXTHDRCPY(ip6po_dest2);
2560 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2564 ip6_clearpktopts(dst, -1);
2567 #undef PKTOPT_EXTHDRCPY
2569 struct ip6_pktopts *
2570 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2573 struct ip6_pktopts *dst;
2575 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2578 ip6_initpktopts(dst);
2580 if ((error = copypktopts(dst, src, canwait)) != 0) {
2581 free(dst, M_IP6OPT);
2589 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2594 ip6_clearpktopts(pktopt, -1);
2596 free(pktopt, M_IP6OPT);
2600 * Set IPv6 outgoing packet options based on advanced API.
2603 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2604 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2606 struct cmsghdr *cm = NULL;
2608 if (control == NULL || opt == NULL)
2611 ip6_initpktopts(opt);
2616 * If stickyopt is provided, make a local copy of the options
2617 * for this particular packet, then override them by ancillary
2619 * XXX: copypktopts() does not copy the cached route to a next
2620 * hop (if any). This is not very good in terms of efficiency,
2621 * but we can allow this since this option should be rarely
2624 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2629 * XXX: Currently, we assume all the optional information is stored
2632 if (control->m_next)
2635 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2636 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2639 if (control->m_len < CMSG_LEN(0))
2642 cm = mtod(control, struct cmsghdr *);
2643 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2645 if (cm->cmsg_level != IPPROTO_IPV6)
2648 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2649 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2658 * Set a particular packet option, as a sticky option or an ancillary data
2659 * item. "len" can be 0 only when it's a sticky option.
2660 * We have 4 cases of combination of "sticky" and "cmsg":
2661 * "sticky=0, cmsg=0": impossible
2662 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2663 * "sticky=1, cmsg=0": RFC3542 socket option
2664 * "sticky=1, cmsg=1": RFC2292 socket option
2667 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2668 struct ucred *cred, int sticky, int cmsg, int uproto)
2670 int minmtupolicy, preftemp;
2673 if (!sticky && !cmsg) {
2675 printf("ip6_setpktopt: impossible case\n");
2681 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2682 * not be specified in the context of RFC3542. Conversely,
2683 * RFC3542 types should not be specified in the context of RFC2292.
2687 case IPV6_2292PKTINFO:
2688 case IPV6_2292HOPLIMIT:
2689 case IPV6_2292NEXTHOP:
2690 case IPV6_2292HOPOPTS:
2691 case IPV6_2292DSTOPTS:
2692 case IPV6_2292RTHDR:
2693 case IPV6_2292PKTOPTIONS:
2694 return (ENOPROTOOPT);
2697 if (sticky && cmsg) {
2704 case IPV6_RTHDRDSTOPTS:
2706 case IPV6_USE_MIN_MTU:
2709 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2710 return (ENOPROTOOPT);
2715 case IPV6_2292PKTINFO:
2718 struct ifnet *ifp = NULL;
2719 struct in6_pktinfo *pktinfo;
2721 if (len != sizeof(struct in6_pktinfo))
2724 pktinfo = (struct in6_pktinfo *)buf;
2727 * An application can clear any sticky IPV6_PKTINFO option by
2728 * doing a "regular" setsockopt with ipi6_addr being
2729 * in6addr_any and ipi6_ifindex being zero.
2730 * [RFC 3542, Section 6]
2732 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2733 pktinfo->ipi6_ifindex == 0 &&
2734 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2735 ip6_clearpktopts(opt, optname);
2739 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2740 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2743 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2745 /* validate the interface index if specified. */
2746 if (pktinfo->ipi6_ifindex > V_if_index)
2748 if (pktinfo->ipi6_ifindex) {
2749 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2753 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2754 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2758 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2759 struct in6_ifaddr *ia;
2761 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2762 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2764 return (EADDRNOTAVAIL);
2765 ifa_free(&ia->ia_ifa);
2768 * We store the address anyway, and let in6_selectsrc()
2769 * validate the specified address. This is because ipi6_addr
2770 * may not have enough information about its scope zone, and
2771 * we may need additional information (such as outgoing
2772 * interface or the scope zone of a destination address) to
2773 * disambiguate the scope.
2774 * XXX: the delay of the validation may confuse the
2775 * application when it is used as a sticky option.
2777 if (opt->ip6po_pktinfo == NULL) {
2778 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2779 M_IP6OPT, M_NOWAIT);
2780 if (opt->ip6po_pktinfo == NULL)
2783 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2787 case IPV6_2292HOPLIMIT:
2793 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2794 * to simplify the ordering among hoplimit options.
2796 if (optname == IPV6_HOPLIMIT && sticky)
2797 return (ENOPROTOOPT);
2799 if (len != sizeof(int))
2802 if (*hlimp < -1 || *hlimp > 255)
2805 opt->ip6po_hlim = *hlimp;
2813 if (len != sizeof(int))
2815 tclass = *(int *)buf;
2816 if (tclass < -1 || tclass > 255)
2819 opt->ip6po_tclass = tclass;
2823 case IPV6_2292NEXTHOP:
2826 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
2831 if (len == 0) { /* just remove the option */
2832 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2836 /* check if cmsg_len is large enough for sa_len */
2837 if (len < sizeof(struct sockaddr) || len < *buf)
2840 switch (((struct sockaddr *)buf)->sa_family) {
2843 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2846 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2849 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2850 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2853 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2859 case AF_LINK: /* should eventually be supported */
2861 return (EAFNOSUPPORT);
2864 /* turn off the previous option, then set the new option. */
2865 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2866 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2867 if (opt->ip6po_nexthop == NULL)
2869 bcopy(buf, opt->ip6po_nexthop, *buf);
2872 case IPV6_2292HOPOPTS:
2875 struct ip6_hbh *hbh;
2879 * XXX: We don't allow a non-privileged user to set ANY HbH
2880 * options, since per-option restriction has too much
2884 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
2890 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2891 break; /* just remove the option */
2894 /* message length validation */
2895 if (len < sizeof(struct ip6_hbh))
2897 hbh = (struct ip6_hbh *)buf;
2898 hbhlen = (hbh->ip6h_len + 1) << 3;
2902 /* turn off the previous option, then set the new option. */
2903 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2904 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2905 if (opt->ip6po_hbh == NULL)
2907 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2912 case IPV6_2292DSTOPTS:
2914 case IPV6_RTHDRDSTOPTS:
2916 struct ip6_dest *dest, **newdest = NULL;
2919 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2920 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
2926 ip6_clearpktopts(opt, optname);
2927 break; /* just remove the option */
2930 /* message length validation */
2931 if (len < sizeof(struct ip6_dest))
2933 dest = (struct ip6_dest *)buf;
2934 destlen = (dest->ip6d_len + 1) << 3;
2939 * Determine the position that the destination options header
2940 * should be inserted; before or after the routing header.
2943 case IPV6_2292DSTOPTS:
2945 * The old advacned API is ambiguous on this point.
2946 * Our approach is to determine the position based
2947 * according to the existence of a routing header.
2948 * Note, however, that this depends on the order of the
2949 * extension headers in the ancillary data; the 1st
2950 * part of the destination options header must appear
2951 * before the routing header in the ancillary data,
2953 * RFC3542 solved the ambiguity by introducing
2954 * separate ancillary data or option types.
2956 if (opt->ip6po_rthdr == NULL)
2957 newdest = &opt->ip6po_dest1;
2959 newdest = &opt->ip6po_dest2;
2961 case IPV6_RTHDRDSTOPTS:
2962 newdest = &opt->ip6po_dest1;
2965 newdest = &opt->ip6po_dest2;
2969 /* turn off the previous option, then set the new option. */
2970 ip6_clearpktopts(opt, optname);
2971 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2972 if (*newdest == NULL)
2974 bcopy(dest, *newdest, destlen);
2979 case IPV6_2292RTHDR:
2982 struct ip6_rthdr *rth;
2986 ip6_clearpktopts(opt, IPV6_RTHDR);
2987 break; /* just remove the option */
2990 /* message length validation */
2991 if (len < sizeof(struct ip6_rthdr))
2993 rth = (struct ip6_rthdr *)buf;
2994 rthlen = (rth->ip6r_len + 1) << 3;
2998 switch (rth->ip6r_type) {
2999 case IPV6_RTHDR_TYPE_0:
3000 if (rth->ip6r_len == 0) /* must contain one addr */
3002 if (rth->ip6r_len % 2) /* length must be even */
3004 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3008 return (EINVAL); /* not supported */
3011 /* turn off the previous option */
3012 ip6_clearpktopts(opt, IPV6_RTHDR);
3013 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3014 if (opt->ip6po_rthdr == NULL)
3016 bcopy(rth, opt->ip6po_rthdr, rthlen);
3021 case IPV6_USE_MIN_MTU:
3022 if (len != sizeof(int))
3024 minmtupolicy = *(int *)buf;
3025 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3026 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3027 minmtupolicy != IP6PO_MINMTU_ALL) {
3030 opt->ip6po_minmtu = minmtupolicy;
3034 if (len != sizeof(int))
3037 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3039 * we ignore this option for TCP sockets.
3040 * (RFC3542 leaves this case unspecified.)
3042 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3044 opt->ip6po_flags |= IP6PO_DONTFRAG;
3047 case IPV6_PREFER_TEMPADDR:
3048 if (len != sizeof(int))
3050 preftemp = *(int *)buf;
3051 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3052 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3053 preftemp != IP6PO_TEMPADDR_PREFER) {
3056 opt->ip6po_prefer_tempaddr = preftemp;
3060 return (ENOPROTOOPT);
3061 } /* end of switch */
3067 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3068 * packet to the input queue of a specified interface. Note that this
3069 * calls the output routine of the loopback "driver", but with an interface
3070 * pointer that might NOT be &loif -- easier than replicating that code here.
3073 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3076 struct ip6_hdr *ip6;
3078 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3083 * Make sure to deep-copy IPv6 header portion in case the data
3084 * is in an mbuf cluster, so that we can safely override the IPv6
3085 * header portion later.
3087 if (!M_WRITABLE(copym) ||
3088 copym->m_len < sizeof(struct ip6_hdr)) {
3089 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3093 ip6 = mtod(copym, struct ip6_hdr *);
3095 * clear embedded scope identifiers if necessary.
3096 * in6_clearscope will touch the addresses only when necessary.
3098 in6_clearscope(&ip6->ip6_src);
3099 in6_clearscope(&ip6->ip6_dst);
3100 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3101 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3103 copym->m_pkthdr.csum_data = 0xffff;
3105 if_simloop(ifp, copym, AF_INET6, 0);
3109 * Chop IPv6 header off from the payload.
3112 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3115 struct ip6_hdr *ip6;
3117 ip6 = mtod(m, struct ip6_hdr *);
3118 if (m->m_len > sizeof(*ip6)) {
3119 mh = m_gethdr(M_NOWAIT, MT_DATA);
3124 m_move_pkthdr(mh, m);
3125 M_ALIGN(mh, sizeof(*ip6));
3126 m->m_len -= sizeof(*ip6);
3127 m->m_data += sizeof(*ip6);
3130 m->m_len = sizeof(*ip6);
3131 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3133 exthdrs->ip6e_ip6 = m;
3138 * Compute IPv6 extension header length.
3141 ip6_optlen(struct inpcb *in6p)
3145 if (!in6p->in6p_outputopts)
3150 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3152 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3153 if (in6p->in6p_outputopts->ip6po_rthdr)
3154 /* dest1 is valid with rthdr only */
3155 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3156 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3157 len += elen(in6p->in6p_outputopts->ip6po_dest2);