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);
327 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
329 * IPSec checking which handles several cases.
330 * FAST IPSEC: We re-injected the packet.
331 * XXX: need scope argument.
333 if (IPSEC_ENABLED(ipv6)) {
334 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
335 if (error == EINPROGRESS)
342 bzero(&exthdrs, sizeof(exthdrs));
344 /* Hop-by-Hop options header */
345 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
346 /* Destination options header(1st part) */
347 if (opt->ip6po_rthdr) {
349 * Destination options header(1st part)
350 * This only makes sense with a routing header.
351 * See Section 9.2 of RFC 3542.
352 * Disabling this part just for MIP6 convenience is
353 * a bad idea. We need to think carefully about a
354 * way to make the advanced API coexist with MIP6
355 * options, which might automatically be inserted in
358 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
361 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
362 /* Destination options header(2nd part) */
363 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
367 * Calculate the total length of the extension header chain.
368 * Keep the length of the unfragmentable part for fragmentation.
371 if (exthdrs.ip6e_hbh)
372 optlen += exthdrs.ip6e_hbh->m_len;
373 if (exthdrs.ip6e_dest1)
374 optlen += exthdrs.ip6e_dest1->m_len;
375 if (exthdrs.ip6e_rthdr)
376 optlen += exthdrs.ip6e_rthdr->m_len;
377 unfragpartlen = optlen + sizeof(struct ip6_hdr);
379 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
380 if (exthdrs.ip6e_dest2)
381 optlen += exthdrs.ip6e_dest2->m_len;
384 * If there is at least one extension header,
385 * separate IP6 header from the payload.
387 if (optlen && !hdrsplit) {
388 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
392 m = exthdrs.ip6e_ip6;
396 ip6 = mtod(m, struct ip6_hdr *);
398 /* adjust mbuf packet header length */
399 m->m_pkthdr.len += optlen;
400 plen = m->m_pkthdr.len - sizeof(*ip6);
402 /* If this is a jumbo payload, insert a jumbo payload option. */
403 if (plen > IPV6_MAXPACKET) {
405 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
409 m = exthdrs.ip6e_ip6;
413 ip6 = mtod(m, struct ip6_hdr *);
414 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
418 ip6->ip6_plen = htons(plen);
421 * Concatenate headers and fill in next header fields.
422 * Here we have, on "m"
424 * and we insert headers accordingly. Finally, we should be getting:
425 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
427 * during the header composing process, "m" points to IPv6 header.
428 * "mprev" points to an extension header prior to esp.
430 u_char *nexthdrp = &ip6->ip6_nxt;
434 * we treat dest2 specially. this makes IPsec processing
435 * much easier. the goal here is to make mprev point the
436 * mbuf prior to dest2.
438 * result: IPv6 dest2 payload
439 * m and mprev will point to IPv6 header.
441 if (exthdrs.ip6e_dest2) {
443 panic("assumption failed: hdr not split");
444 exthdrs.ip6e_dest2->m_next = m->m_next;
445 m->m_next = exthdrs.ip6e_dest2;
446 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
447 ip6->ip6_nxt = IPPROTO_DSTOPTS;
451 * result: IPv6 hbh dest1 rthdr dest2 payload
452 * m will point to IPv6 header. mprev will point to the
453 * extension header prior to dest2 (rthdr in the above case).
455 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
456 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
458 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
462 * If there is a routing header, discard the packet.
464 if (exthdrs.ip6e_rthdr) {
469 /* Source address validation */
470 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
471 (flags & IPV6_UNSPECSRC) == 0) {
473 IP6STAT_INC(ip6s_badscope);
476 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
478 IP6STAT_INC(ip6s_badscope);
482 IP6STAT_INC(ip6s_localout);
489 bzero((caddr_t)ro, sizeof(*ro));
492 if (opt && opt->ip6po_rthdr)
493 ro = &opt->ip6po_route;
494 dst = (struct sockaddr_in6 *)&ro->ro_dst;
495 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
498 * if specified, try to fill in the traffic class field.
499 * do not override if a non-zero value is already set.
500 * we check the diffserv field and the ecn field separately.
502 if (opt && opt->ip6po_tclass >= 0) {
505 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
507 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
510 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
513 /* fill in or override the hop limit field, if necessary. */
514 if (opt && opt->ip6po_hlim != -1)
515 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
516 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
518 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
520 ip6->ip6_hlim = V_ip6_defmcasthlim;
523 * Validate route against routing table additions;
524 * a better/more specific route might have been added.
525 * Make sure address family is set in route.
528 ro->ro_dst.sin6_family = AF_INET6;
529 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
531 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
532 ro->ro_dst.sin6_family == AF_INET6 &&
533 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
535 ifp = ro->ro_rt->rt_ifp;
538 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
540 if (fwd_tag == NULL) {
541 bzero(&dst_sa, sizeof(dst_sa));
542 dst_sa.sin6_family = AF_INET6;
543 dst_sa.sin6_len = sizeof(dst_sa);
544 dst_sa.sin6_addr = ip6->ip6_dst;
546 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
550 in6_ifstat_inc(ifp, ifs6_out_discard);
556 * If in6_selectroute() does not return a route entry,
557 * dst may not have been updated.
559 *dst = dst_sa; /* XXX */
563 * then rt (for unicast) and ifp must be non-NULL valid values.
565 if ((flags & IPV6_FORWARDING) == 0) {
566 /* XXX: the FORWARDING flag can be set for mrouting. */
567 in6_ifstat_inc(ifp, ifs6_out_request);
570 ia = (struct in6_ifaddr *)(rt->rt_ifa);
571 counter_u64_add(rt->rt_pksent, 1);
576 * The outgoing interface must be in the zone of source and
577 * destination addresses.
582 if (in6_setscope(&src0, origifp, &zone))
584 bzero(&src_sa, sizeof(src_sa));
585 src_sa.sin6_family = AF_INET6;
586 src_sa.sin6_len = sizeof(src_sa);
587 src_sa.sin6_addr = ip6->ip6_src;
588 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
592 if (in6_setscope(&dst0, origifp, &zone))
594 /* re-initialize to be sure */
595 bzero(&dst_sa, sizeof(dst_sa));
596 dst_sa.sin6_family = AF_INET6;
597 dst_sa.sin6_len = sizeof(dst_sa);
598 dst_sa.sin6_addr = ip6->ip6_dst;
599 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
603 /* We should use ia_ifp to support the case of
604 * sending packets to an address of our own.
606 if (ia != NULL && ia->ia_ifp)
609 /* scope check is done. */
613 IP6STAT_INC(ip6s_badscope);
614 in6_ifstat_inc(origifp, ifs6_out_discard);
616 error = EHOSTUNREACH; /* XXX */
620 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
621 if (opt && opt->ip6po_nextroute.ro_rt) {
623 * The nexthop is explicitly specified by the
624 * application. We assume the next hop is an IPv6
627 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
629 else if ((rt->rt_flags & RTF_GATEWAY))
630 dst = (struct sockaddr_in6 *)rt->rt_gateway;
633 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
634 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
636 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
637 in6_ifstat_inc(ifp, ifs6_out_mcast);
639 * Confirm that the outgoing interface supports multicast.
641 if (!(ifp->if_flags & IFF_MULTICAST)) {
642 IP6STAT_INC(ip6s_noroute);
643 in6_ifstat_inc(ifp, ifs6_out_discard);
647 if ((im6o == NULL && in6_mcast_loop) ||
648 (im6o && im6o->im6o_multicast_loop)) {
650 * Loop back multicast datagram if not expressly
651 * forbidden to do so, even if we have not joined
652 * the address; protocols will filter it later,
653 * thus deferring a hash lookup and lock acquisition
654 * at the expense of an m_copym().
656 ip6_mloopback(ifp, m);
659 * If we are acting as a multicast router, perform
660 * multicast forwarding as if the packet had just
661 * arrived on the interface to which we are about
662 * to send. The multicast forwarding function
663 * recursively calls this function, using the
664 * IPV6_FORWARDING flag to prevent infinite recursion.
666 * Multicasts that are looped back by ip6_mloopback(),
667 * above, will be forwarded by the ip6_input() routine,
670 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
672 * XXX: ip6_mforward expects that rcvif is NULL
673 * when it is called from the originating path.
674 * However, it may not always be the case.
676 m->m_pkthdr.rcvif = NULL;
677 if (ip6_mforward(ip6, ifp, m) != 0) {
684 * Multicasts with a hoplimit of zero may be looped back,
685 * above, but must not be transmitted on a network.
686 * Also, multicasts addressed to the loopback interface
687 * are not sent -- the above call to ip6_mloopback() will
688 * loop back a copy if this host actually belongs to the
689 * destination group on the loopback interface.
691 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
692 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
699 * Fill the outgoing inteface to tell the upper layer
700 * to increment per-interface statistics.
705 /* Determine path MTU. */
706 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
707 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
711 * The caller of this function may specify to use the minimum MTU
713 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
714 * setting. The logic is a bit complicated; by default, unicast
715 * packets will follow path MTU while multicast packets will be sent at
716 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
717 * including unicast ones will be sent at the minimum MTU. Multicast
718 * packets will always be sent at the minimum MTU unless
719 * IP6PO_MINMTU_DISABLE is explicitly specified.
720 * See RFC 3542 for more details.
722 if (mtu > IPV6_MMTU) {
723 if ((flags & IPV6_MINMTU))
725 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
727 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
729 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
735 * clear embedded scope identifiers if necessary.
736 * in6_clearscope will touch the addresses only when necessary.
738 in6_clearscope(&ip6->ip6_src);
739 in6_clearscope(&ip6->ip6_dst);
742 * If the outgoing packet contains a hop-by-hop options header,
743 * it must be examined and processed even by the source node.
744 * (RFC 2460, section 4.)
746 if (exthdrs.ip6e_hbh) {
747 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
748 u_int32_t dummy; /* XXX unused */
749 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
752 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
753 panic("ip6e_hbh is not contiguous");
756 * XXX: if we have to send an ICMPv6 error to the sender,
757 * we need the M_LOOP flag since icmp6_error() expects
758 * the IPv6 and the hop-by-hop options header are
759 * contiguous unless the flag is set.
761 m->m_flags |= M_LOOP;
762 m->m_pkthdr.rcvif = ifp;
763 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
764 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
765 &dummy, &plen) < 0) {
766 /* m was already freed at this point */
767 error = EINVAL;/* better error? */
770 m->m_flags &= ~M_LOOP; /* XXX */
771 m->m_pkthdr.rcvif = NULL;
774 /* Jump over all PFIL processing if hooks are not active. */
775 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
779 /* Run through list of hooks for output packets. */
780 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, 0, inp);
781 if (error != 0 || m == NULL)
784 ip6 = mtod(m, struct ip6_hdr *);
787 /* See if destination IP address was changed by packet filter. */
788 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
789 m->m_flags |= M_SKIP_FIREWALL;
790 /* If destination is now ourself drop to ip6_input(). */
791 if (in6_localip(&ip6->ip6_dst)) {
792 m->m_flags |= M_FASTFWD_OURS;
793 if (m->m_pkthdr.rcvif == NULL)
794 m->m_pkthdr.rcvif = V_loif;
795 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
796 m->m_pkthdr.csum_flags |=
797 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
798 m->m_pkthdr.csum_data = 0xffff;
801 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
802 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
804 error = netisr_queue(NETISR_IPV6, m);
808 needfiblookup = 1; /* Redo the routing table lookup. */
810 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
814 /* See if fib was changed by packet filter. */
815 if (fibnum != M_GETFIB(m)) {
816 m->m_flags |= M_SKIP_FIREWALL;
817 fibnum = M_GETFIB(m);
821 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
827 /* See if local, if yes, send it to netisr. */
828 if (m->m_flags & M_FASTFWD_OURS) {
829 if (m->m_pkthdr.rcvif == NULL)
830 m->m_pkthdr.rcvif = V_loif;
831 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
832 m->m_pkthdr.csum_flags |=
833 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
834 m->m_pkthdr.csum_data = 0xffff;
837 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
838 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
840 error = netisr_queue(NETISR_IPV6, m);
843 /* Or forward to some other address? */
844 if ((m->m_flags & M_IP6_NEXTHOP) &&
845 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
846 dst = (struct sockaddr_in6 *)&ro->ro_dst;
847 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
848 m->m_flags |= M_SKIP_FIREWALL;
849 m->m_flags &= ~M_IP6_NEXTHOP;
850 m_tag_delete(m, fwd_tag);
856 * Send the packet to the outgoing interface.
857 * If necessary, do IPv6 fragmentation before sending.
859 * the logic here is rather complex:
860 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
861 * 1-a: send as is if tlen <= path mtu
862 * 1-b: fragment if tlen > path mtu
864 * 2: if user asks us not to fragment (dontfrag == 1)
865 * 2-a: send as is if tlen <= interface mtu
866 * 2-b: error if tlen > interface mtu
868 * 3: if we always need to attach fragment header (alwaysfrag == 1)
871 * 4: if dontfrag == 1 && alwaysfrag == 1
872 * error, as we cannot handle this conflicting request
874 sw_csum = m->m_pkthdr.csum_flags;
876 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
877 sw_csum &= ~ifp->if_hwassist;
881 * If we added extension headers, we will not do TSO and calculate the
882 * checksums ourselves for now.
883 * XXX-BZ Need a framework to know when the NIC can handle it, even
886 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
887 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
888 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
891 if (sw_csum & CSUM_SCTP_IPV6) {
892 sw_csum &= ~CSUM_SCTP_IPV6;
893 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
896 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
897 tlen = m->m_pkthdr.len;
899 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
903 if (dontfrag && alwaysfrag) { /* case 4 */
904 /* conflicting request - can't transmit */
908 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
910 * Even if the DONTFRAG option is specified, we cannot send the
911 * packet when the data length is larger than the MTU of the
912 * outgoing interface.
913 * Notify the error by sending IPV6_PATHMTU ancillary data if
914 * application wanted to know the MTU value. Also return an
915 * error code (this is not described in the API spec).
918 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
924 * transmit packet without fragmentation
926 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
927 struct in6_ifaddr *ia6;
929 ip6 = mtod(m, struct ip6_hdr *);
930 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
932 /* Record statistics for this interface address. */
933 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
934 counter_u64_add(ia6->ia_ifa.ifa_obytes,
936 ifa_free(&ia6->ia_ifa);
940 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
941 in_pcboutput_txrtlmt(inp, ifp, m);
942 /* stamp send tag on mbuf */
943 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
945 m->m_pkthdr.snd_tag = NULL;
948 error = nd6_output_ifp(ifp, origifp, m, dst,
951 /* check for route change */
953 in_pcboutput_eagain(inp);
959 * try to fragment the packet. case 1-b and 3
961 if (mtu < IPV6_MMTU) {
962 /* path MTU cannot be less than IPV6_MMTU */
964 in6_ifstat_inc(ifp, ifs6_out_fragfail);
966 } else if (ip6->ip6_plen == 0) {
967 /* jumbo payload cannot be fragmented */
969 in6_ifstat_inc(ifp, ifs6_out_fragfail);
975 * Too large for the destination or interface;
976 * fragment if possible.
977 * Must be able to put at least 8 bytes per fragment.
979 hlen = unfragpartlen;
980 if (mtu > IPV6_MAXPACKET)
981 mtu = IPV6_MAXPACKET;
983 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
986 in6_ifstat_inc(ifp, ifs6_out_fragfail);
991 * If the interface will not calculate checksums on
992 * fragmented packets, then do it here.
993 * XXX-BZ handle the hw offloading case. Need flags.
995 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
996 in6_delayed_cksum(m, plen, hlen);
997 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1000 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1001 sctp_delayed_cksum(m, hlen);
1002 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1006 * Change the next header field of the last header in the
1007 * unfragmentable part.
1009 if (exthdrs.ip6e_rthdr) {
1010 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1011 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1012 } else if (exthdrs.ip6e_dest1) {
1013 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1014 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1015 } else if (exthdrs.ip6e_hbh) {
1016 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1017 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1019 nextproto = ip6->ip6_nxt;
1020 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1024 * Loop through length of segment after first fragment,
1025 * make new header and copy data of each part and link onto
1029 id = htonl(ip6_randomid());
1030 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1033 in6_ifstat_inc(ifp, ifs6_out_fragok);
1037 * Remove leading garbages.
1047 /* Record statistics for this interface address. */
1049 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1050 counter_u64_add(ia->ia_ifa.ifa_obytes,
1055 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
1056 in_pcboutput_txrtlmt(inp, ifp, m);
1057 /* stamp send tag on mbuf */
1058 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
1060 m->m_pkthdr.snd_tag = NULL;
1063 error = nd6_output_ifp(ifp, origifp, m, dst,
1064 (struct route *)ro);
1066 /* check for route change */
1067 if (error == EAGAIN)
1068 in_pcboutput_eagain(inp);
1075 IP6STAT_INC(ip6s_fragmented);
1078 if (ro == &ip6route)
1083 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1084 m_freem(exthdrs.ip6e_dest1);
1085 m_freem(exthdrs.ip6e_rthdr);
1086 m_freem(exthdrs.ip6e_dest2);
1095 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1099 if (hlen > MCLBYTES)
1100 return (ENOBUFS); /* XXX */
1103 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1105 m = m_get(M_NOWAIT, MT_DATA);
1110 bcopy(hdr, mtod(m, caddr_t), hlen);
1117 * Insert jumbo payload option.
1120 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1126 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1129 * If there is no hop-by-hop options header, allocate new one.
1130 * If there is one but it doesn't have enough space to store the
1131 * jumbo payload option, allocate a cluster to store the whole options.
1132 * Otherwise, use it to store the options.
1134 if (exthdrs->ip6e_hbh == NULL) {
1135 mopt = m_get(M_NOWAIT, MT_DATA);
1138 mopt->m_len = JUMBOOPTLEN;
1139 optbuf = mtod(mopt, u_char *);
1140 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1141 exthdrs->ip6e_hbh = mopt;
1143 struct ip6_hbh *hbh;
1145 mopt = exthdrs->ip6e_hbh;
1146 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1149 * - exthdrs->ip6e_hbh is not referenced from places
1150 * other than exthdrs.
1151 * - exthdrs->ip6e_hbh is not an mbuf chain.
1153 int oldoptlen = mopt->m_len;
1157 * XXX: give up if the whole (new) hbh header does
1158 * not fit even in an mbuf cluster.
1160 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1164 * As a consequence, we must always prepare a cluster
1167 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1170 n->m_len = oldoptlen + JUMBOOPTLEN;
1171 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1173 optbuf = mtod(n, caddr_t) + oldoptlen;
1175 mopt = exthdrs->ip6e_hbh = n;
1177 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1178 mopt->m_len += JUMBOOPTLEN;
1180 optbuf[0] = IP6OPT_PADN;
1184 * Adjust the header length according to the pad and
1185 * the jumbo payload option.
1187 hbh = mtod(mopt, struct ip6_hbh *);
1188 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1191 /* fill in the option. */
1192 optbuf[2] = IP6OPT_JUMBO;
1194 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1195 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1197 /* finally, adjust the packet header length */
1198 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1205 * Insert fragment header and copy unfragmentable header portions.
1208 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1209 struct ip6_frag **frghdrp)
1211 struct mbuf *n, *mlast;
1213 if (hlen > sizeof(struct ip6_hdr)) {
1214 n = m_copym(m0, sizeof(struct ip6_hdr),
1215 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1222 /* Search for the last mbuf of unfragmentable part. */
1223 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1226 if (M_WRITABLE(mlast) &&
1227 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1228 /* use the trailing space of the last mbuf for the fragment hdr */
1229 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1231 mlast->m_len += sizeof(struct ip6_frag);
1232 m->m_pkthdr.len += sizeof(struct ip6_frag);
1234 /* allocate a new mbuf for the fragment header */
1237 mfrg = m_get(M_NOWAIT, MT_DATA);
1240 mfrg->m_len = sizeof(struct ip6_frag);
1241 *frghdrp = mtod(mfrg, struct ip6_frag *);
1242 mlast->m_next = mfrg;
1249 * Calculates IPv6 path mtu for destination @dst.
1250 * Resulting MTU is stored in @mtup.
1252 * Returns 0 on success.
1255 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1257 struct nhop6_extended nh6;
1258 struct in6_addr kdst;
1264 in6_splitscope(dst, &kdst, &scopeid);
1265 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1266 return (EHOSTUNREACH);
1271 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1272 fib6_free_nh_ext(fibnum, &nh6);
1278 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1279 * and cached data in @ro_pmtu.
1280 * MTU from (successful) route lookup is saved (along with dst)
1281 * inside @ro_pmtu to avoid subsequent route lookups after packet
1282 * filter processing.
1284 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1285 * Returns 0 on success.
1288 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1289 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1290 int *alwaysfragp, u_int fibnum, u_int proto)
1292 struct nhop6_basic nh6;
1293 struct in6_addr kdst;
1295 struct sockaddr_in6 *sa6_dst;
1302 * Here ro_pmtu has final destination address, while
1303 * ro might represent immediate destination.
1304 * Use ro_pmtu destination since mtu might differ.
1306 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1307 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1308 ro_pmtu->ro_mtu = 0;
1310 if (ro_pmtu->ro_mtu == 0) {
1311 bzero(sa6_dst, sizeof(*sa6_dst));
1312 sa6_dst->sin6_family = AF_INET6;
1313 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1314 sa6_dst->sin6_addr = *dst;
1316 in6_splitscope(dst, &kdst, &scopeid);
1317 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1319 ro_pmtu->ro_mtu = nh6.nh_mtu;
1322 mtu = ro_pmtu->ro_mtu;
1326 mtu = ro_pmtu->ro_rt->rt_mtu;
1328 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1332 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1333 * hostcache data for @dst.
1334 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1336 * Returns 0 on success.
1339 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1340 u_long *mtup, int *alwaysfragp, u_int proto)
1348 struct in_conninfo inc;
1350 bzero(&inc, sizeof(inc));
1351 inc.inc_flags |= INC_ISIPV6;
1352 inc.inc6_faddr = *dst;
1354 ifmtu = IN6_LINKMTU(ifp);
1356 /* TCP is known to react to pmtu changes so skip hc */
1357 if (proto != IPPROTO_TCP)
1358 mtu = tcp_hc_getmtu(&inc);
1361 mtu = min(mtu, rt_mtu);
1366 else if (mtu < IPV6_MMTU) {
1368 * RFC2460 section 5, last paragraph:
1369 * if we record ICMPv6 too big message with
1370 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1371 * or smaller, with framgent header attached.
1372 * (fragment header is needed regardless from the
1373 * packet size, for translators to identify packets)
1379 mtu = IN6_LINKMTU(ifp);
1381 error = EHOSTUNREACH; /* XXX */
1385 *alwaysfragp = alwaysfrag;
1390 * IP6 socket option processing.
1393 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1395 int optdatalen, uproto;
1397 struct inpcb *in6p = sotoinpcb(so);
1399 int level, op, optname;
1403 uint32_t rss_bucket;
1408 * Don't use more than a quarter of mbuf clusters. N.B.:
1409 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1410 * on LP64 architectures, so cast to u_long to avoid undefined
1411 * behavior. ILP32 architectures cannot have nmbclusters
1412 * large enough to overflow for other reasons.
1414 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1416 level = sopt->sopt_level;
1417 op = sopt->sopt_dir;
1418 optname = sopt->sopt_name;
1419 optlen = sopt->sopt_valsize;
1423 uproto = (int)so->so_proto->pr_protocol;
1425 if (level != IPPROTO_IPV6) {
1428 if (sopt->sopt_level == SOL_SOCKET &&
1429 sopt->sopt_dir == SOPT_SET) {
1430 switch (sopt->sopt_name) {
1433 if ((so->so_options & SO_REUSEADDR) != 0)
1434 in6p->inp_flags2 |= INP_REUSEADDR;
1436 in6p->inp_flags2 &= ~INP_REUSEADDR;
1442 if ((so->so_options & SO_REUSEPORT) != 0)
1443 in6p->inp_flags2 |= INP_REUSEPORT;
1445 in6p->inp_flags2 &= ~INP_REUSEPORT;
1449 case SO_REUSEPORT_LB:
1451 if ((so->so_options & SO_REUSEPORT_LB) != 0)
1452 in6p->inp_flags2 |= INP_REUSEPORT_LB;
1454 in6p->inp_flags2 &= ~INP_REUSEPORT_LB;
1460 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1464 case SO_MAX_PACING_RATE:
1467 in6p->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1478 } else { /* level == IPPROTO_IPV6 */
1483 case IPV6_2292PKTOPTIONS:
1484 #ifdef IPV6_PKTOPTIONS
1485 case IPV6_PKTOPTIONS:
1490 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1491 printf("ip6_ctloutput: mbuf limit hit\n");
1496 error = soopt_getm(sopt, &m); /* XXX */
1499 error = soopt_mcopyin(sopt, m); /* XXX */
1502 error = ip6_pcbopts(&in6p->in6p_outputopts,
1504 m_freem(m); /* XXX */
1509 * Use of some Hop-by-Hop options or some
1510 * Destination options, might require special
1511 * privilege. That is, normal applications
1512 * (without special privilege) might be forbidden
1513 * from setting certain options in outgoing packets,
1514 * and might never see certain options in received
1515 * packets. [RFC 2292 Section 6]
1516 * KAME specific note:
1517 * KAME prevents non-privileged users from sending or
1518 * receiving ANY hbh/dst options in order to avoid
1519 * overhead of parsing options in the kernel.
1521 case IPV6_RECVHOPOPTS:
1522 case IPV6_RECVDSTOPTS:
1523 case IPV6_RECVRTHDRDSTOPTS:
1525 error = priv_check(td,
1526 PRIV_NETINET_SETHDROPTS);
1531 case IPV6_UNICAST_HOPS:
1534 case IPV6_RECVPKTINFO:
1535 case IPV6_RECVHOPLIMIT:
1536 case IPV6_RECVRTHDR:
1537 case IPV6_RECVPATHMTU:
1538 case IPV6_RECVTCLASS:
1539 case IPV6_RECVFLOWID:
1541 case IPV6_RECVRSSBUCKETID:
1544 case IPV6_AUTOFLOWLABEL:
1545 case IPV6_ORIGDSTADDR:
1547 case IPV6_BINDMULTI:
1549 case IPV6_RSS_LISTEN_BUCKET:
1551 if (optname == IPV6_BINDANY && td != NULL) {
1552 error = priv_check(td,
1553 PRIV_NETINET_BINDANY);
1558 if (optlen != sizeof(int)) {
1562 error = sooptcopyin(sopt, &optval,
1563 sizeof optval, sizeof optval);
1568 case IPV6_UNICAST_HOPS:
1569 if (optval < -1 || optval >= 256)
1572 /* -1 = kernel default */
1573 in6p->in6p_hops = optval;
1574 if ((in6p->inp_vflag &
1576 in6p->inp_ip_ttl = optval;
1579 #define OPTSET(bit) \
1583 in6p->inp_flags |= (bit); \
1585 in6p->inp_flags &= ~(bit); \
1586 INP_WUNLOCK(in6p); \
1587 } while (/*CONSTCOND*/ 0)
1588 #define OPTSET2292(bit) \
1591 in6p->inp_flags |= IN6P_RFC2292; \
1593 in6p->inp_flags |= (bit); \
1595 in6p->inp_flags &= ~(bit); \
1596 INP_WUNLOCK(in6p); \
1597 } while (/*CONSTCOND*/ 0)
1598 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1600 #define OPTSET2_N(bit, val) do { \
1602 in6p->inp_flags2 |= bit; \
1604 in6p->inp_flags2 &= ~bit; \
1606 #define OPTSET2(bit, val) do { \
1608 OPTSET2_N(bit, val); \
1609 INP_WUNLOCK(in6p); \
1611 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1612 #define OPTSET2292_EXCLUSIVE(bit) \
1615 if (OPTBIT(IN6P_RFC2292)) { \
1619 in6p->inp_flags |= (bit); \
1621 in6p->inp_flags &= ~(bit); \
1623 INP_WUNLOCK(in6p); \
1624 } while (/*CONSTCOND*/ 0)
1626 case IPV6_RECVPKTINFO:
1627 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1632 struct ip6_pktopts **optp;
1634 /* cannot mix with RFC2292 */
1635 if (OPTBIT(IN6P_RFC2292)) {
1639 optp = &in6p->in6p_outputopts;
1640 error = ip6_pcbopt(IPV6_HOPLIMIT,
1641 (u_char *)&optval, sizeof(optval),
1642 optp, (td != NULL) ? td->td_ucred :
1647 case IPV6_RECVHOPLIMIT:
1648 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1651 case IPV6_RECVHOPOPTS:
1652 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1655 case IPV6_RECVDSTOPTS:
1656 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1659 case IPV6_RECVRTHDRDSTOPTS:
1660 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1663 case IPV6_RECVRTHDR:
1664 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1667 case IPV6_RECVPATHMTU:
1669 * We ignore this option for TCP
1671 * (RFC3542 leaves this case
1674 if (uproto != IPPROTO_TCP)
1678 case IPV6_RECVFLOWID:
1679 OPTSET2(INP_RECVFLOWID, optval);
1683 case IPV6_RECVRSSBUCKETID:
1684 OPTSET2(INP_RECVRSSBUCKETID, optval);
1690 * make setsockopt(IPV6_V6ONLY)
1691 * available only prior to bind(2).
1692 * see ipng mailing list, Jun 22 2001.
1694 if (in6p->inp_lport ||
1695 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1699 OPTSET(IN6P_IPV6_V6ONLY);
1701 in6p->inp_vflag &= ~INP_IPV4;
1703 in6p->inp_vflag |= INP_IPV4;
1705 case IPV6_RECVTCLASS:
1706 /* cannot mix with RFC2292 XXX */
1707 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1709 case IPV6_AUTOFLOWLABEL:
1710 OPTSET(IN6P_AUTOFLOWLABEL);
1713 case IPV6_ORIGDSTADDR:
1714 OPTSET2(INP_ORIGDSTADDR, optval);
1717 OPTSET(INP_BINDANY);
1720 case IPV6_BINDMULTI:
1721 OPTSET2(INP_BINDMULTI, optval);
1724 case IPV6_RSS_LISTEN_BUCKET:
1725 if ((optval >= 0) &&
1726 (optval < rss_getnumbuckets())) {
1728 in6p->inp_rss_listen_bucket = optval;
1729 OPTSET2_N(INP_RSS_BUCKET_SET, 1);
1741 case IPV6_USE_MIN_MTU:
1742 case IPV6_PREFER_TEMPADDR:
1743 if (optlen != sizeof(optval)) {
1747 error = sooptcopyin(sopt, &optval,
1748 sizeof optval, sizeof optval);
1752 struct ip6_pktopts **optp;
1753 optp = &in6p->in6p_outputopts;
1754 error = ip6_pcbopt(optname,
1755 (u_char *)&optval, sizeof(optval),
1756 optp, (td != NULL) ? td->td_ucred :
1761 case IPV6_2292PKTINFO:
1762 case IPV6_2292HOPLIMIT:
1763 case IPV6_2292HOPOPTS:
1764 case IPV6_2292DSTOPTS:
1765 case IPV6_2292RTHDR:
1767 if (optlen != sizeof(int)) {
1771 error = sooptcopyin(sopt, &optval,
1772 sizeof optval, sizeof optval);
1776 case IPV6_2292PKTINFO:
1777 OPTSET2292(IN6P_PKTINFO);
1779 case IPV6_2292HOPLIMIT:
1780 OPTSET2292(IN6P_HOPLIMIT);
1782 case IPV6_2292HOPOPTS:
1784 * Check super-user privilege.
1785 * See comments for IPV6_RECVHOPOPTS.
1788 error = priv_check(td,
1789 PRIV_NETINET_SETHDROPTS);
1793 OPTSET2292(IN6P_HOPOPTS);
1795 case IPV6_2292DSTOPTS:
1797 error = priv_check(td,
1798 PRIV_NETINET_SETHDROPTS);
1802 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1804 case IPV6_2292RTHDR:
1805 OPTSET2292(IN6P_RTHDR);
1813 case IPV6_RTHDRDSTOPTS:
1816 /* new advanced API (RFC3542) */
1818 u_char optbuf_storage[MCLBYTES];
1820 struct ip6_pktopts **optp;
1822 /* cannot mix with RFC2292 */
1823 if (OPTBIT(IN6P_RFC2292)) {
1829 * We only ensure valsize is not too large
1830 * here. Further validation will be done
1833 error = sooptcopyin(sopt, optbuf_storage,
1834 sizeof(optbuf_storage), 0);
1837 optlen = sopt->sopt_valsize;
1838 optbuf = optbuf_storage;
1839 optp = &in6p->in6p_outputopts;
1840 error = ip6_pcbopt(optname, optbuf, optlen,
1841 optp, (td != NULL) ? td->td_ucred : NULL,
1847 case IPV6_MULTICAST_IF:
1848 case IPV6_MULTICAST_HOPS:
1849 case IPV6_MULTICAST_LOOP:
1850 case IPV6_JOIN_GROUP:
1851 case IPV6_LEAVE_GROUP:
1853 case MCAST_BLOCK_SOURCE:
1854 case MCAST_UNBLOCK_SOURCE:
1855 case MCAST_JOIN_GROUP:
1856 case MCAST_LEAVE_GROUP:
1857 case MCAST_JOIN_SOURCE_GROUP:
1858 case MCAST_LEAVE_SOURCE_GROUP:
1859 error = ip6_setmoptions(in6p, sopt);
1862 case IPV6_PORTRANGE:
1863 error = sooptcopyin(sopt, &optval,
1864 sizeof optval, sizeof optval);
1870 case IPV6_PORTRANGE_DEFAULT:
1871 in6p->inp_flags &= ~(INP_LOWPORT);
1872 in6p->inp_flags &= ~(INP_HIGHPORT);
1875 case IPV6_PORTRANGE_HIGH:
1876 in6p->inp_flags &= ~(INP_LOWPORT);
1877 in6p->inp_flags |= INP_HIGHPORT;
1880 case IPV6_PORTRANGE_LOW:
1881 in6p->inp_flags &= ~(INP_HIGHPORT);
1882 in6p->inp_flags |= INP_LOWPORT;
1892 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1893 case IPV6_IPSEC_POLICY:
1894 if (IPSEC_ENABLED(ipv6)) {
1895 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
1902 error = ENOPROTOOPT;
1910 case IPV6_2292PKTOPTIONS:
1911 #ifdef IPV6_PKTOPTIONS
1912 case IPV6_PKTOPTIONS:
1915 * RFC3542 (effectively) deprecated the
1916 * semantics of the 2292-style pktoptions.
1917 * Since it was not reliable in nature (i.e.,
1918 * applications had to expect the lack of some
1919 * information after all), it would make sense
1920 * to simplify this part by always returning
1923 sopt->sopt_valsize = 0;
1926 case IPV6_RECVHOPOPTS:
1927 case IPV6_RECVDSTOPTS:
1928 case IPV6_RECVRTHDRDSTOPTS:
1929 case IPV6_UNICAST_HOPS:
1930 case IPV6_RECVPKTINFO:
1931 case IPV6_RECVHOPLIMIT:
1932 case IPV6_RECVRTHDR:
1933 case IPV6_RECVPATHMTU:
1936 case IPV6_PORTRANGE:
1937 case IPV6_RECVTCLASS:
1938 case IPV6_AUTOFLOWLABEL:
1942 case IPV6_RECVFLOWID:
1944 case IPV6_RSSBUCKETID:
1945 case IPV6_RECVRSSBUCKETID:
1947 case IPV6_BINDMULTI:
1950 case IPV6_RECVHOPOPTS:
1951 optval = OPTBIT(IN6P_HOPOPTS);
1954 case IPV6_RECVDSTOPTS:
1955 optval = OPTBIT(IN6P_DSTOPTS);
1958 case IPV6_RECVRTHDRDSTOPTS:
1959 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1962 case IPV6_UNICAST_HOPS:
1963 optval = in6p->in6p_hops;
1966 case IPV6_RECVPKTINFO:
1967 optval = OPTBIT(IN6P_PKTINFO);
1970 case IPV6_RECVHOPLIMIT:
1971 optval = OPTBIT(IN6P_HOPLIMIT);
1974 case IPV6_RECVRTHDR:
1975 optval = OPTBIT(IN6P_RTHDR);
1978 case IPV6_RECVPATHMTU:
1979 optval = OPTBIT(IN6P_MTU);
1983 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1986 case IPV6_PORTRANGE:
1989 flags = in6p->inp_flags;
1990 if (flags & INP_HIGHPORT)
1991 optval = IPV6_PORTRANGE_HIGH;
1992 else if (flags & INP_LOWPORT)
1993 optval = IPV6_PORTRANGE_LOW;
1998 case IPV6_RECVTCLASS:
1999 optval = OPTBIT(IN6P_TCLASS);
2002 case IPV6_AUTOFLOWLABEL:
2003 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2006 case IPV6_ORIGDSTADDR:
2007 optval = OPTBIT2(INP_ORIGDSTADDR);
2011 optval = OPTBIT(INP_BINDANY);
2015 optval = in6p->inp_flowid;
2019 optval = in6p->inp_flowtype;
2022 case IPV6_RECVFLOWID:
2023 optval = OPTBIT2(INP_RECVFLOWID);
2026 case IPV6_RSSBUCKETID:
2028 rss_hash2bucket(in6p->inp_flowid,
2032 optval = rss_bucket;
2037 case IPV6_RECVRSSBUCKETID:
2038 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2042 case IPV6_BINDMULTI:
2043 optval = OPTBIT2(INP_BINDMULTI);
2049 error = sooptcopyout(sopt, &optval,
2056 struct ip6_mtuinfo mtuinfo;
2057 struct in6_addr addr;
2059 if (!(so->so_state & SS_ISCONNECTED))
2062 * XXX: we dot not consider the case of source
2063 * routing, or optional information to specify
2064 * the outgoing interface.
2065 * Copy faddr out of in6p to avoid holding lock
2066 * on inp during route lookup.
2069 bcopy(&in6p->in6p_faddr, &addr, sizeof(addr));
2071 error = ip6_getpmtu_ctl(so->so_fibnum,
2075 if (pmtu > IPV6_MAXPACKET)
2076 pmtu = IPV6_MAXPACKET;
2078 bzero(&mtuinfo, sizeof(mtuinfo));
2079 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2080 optdata = (void *)&mtuinfo;
2081 optdatalen = sizeof(mtuinfo);
2082 error = sooptcopyout(sopt, optdata,
2087 case IPV6_2292PKTINFO:
2088 case IPV6_2292HOPLIMIT:
2089 case IPV6_2292HOPOPTS:
2090 case IPV6_2292RTHDR:
2091 case IPV6_2292DSTOPTS:
2093 case IPV6_2292PKTINFO:
2094 optval = OPTBIT(IN6P_PKTINFO);
2096 case IPV6_2292HOPLIMIT:
2097 optval = OPTBIT(IN6P_HOPLIMIT);
2099 case IPV6_2292HOPOPTS:
2100 optval = OPTBIT(IN6P_HOPOPTS);
2102 case IPV6_2292RTHDR:
2103 optval = OPTBIT(IN6P_RTHDR);
2105 case IPV6_2292DSTOPTS:
2106 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2109 error = sooptcopyout(sopt, &optval,
2116 case IPV6_RTHDRDSTOPTS:
2120 case IPV6_USE_MIN_MTU:
2121 case IPV6_PREFER_TEMPADDR:
2122 error = ip6_getpcbopt(in6p, optname, sopt);
2125 case IPV6_MULTICAST_IF:
2126 case IPV6_MULTICAST_HOPS:
2127 case IPV6_MULTICAST_LOOP:
2129 error = ip6_getmoptions(in6p, sopt);
2132 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2133 case IPV6_IPSEC_POLICY:
2134 if (IPSEC_ENABLED(ipv6)) {
2135 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
2141 error = ENOPROTOOPT;
2151 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2153 int error = 0, optval, optlen;
2154 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2155 struct inpcb *in6p = sotoinpcb(so);
2156 int level, op, optname;
2158 level = sopt->sopt_level;
2159 op = sopt->sopt_dir;
2160 optname = sopt->sopt_name;
2161 optlen = sopt->sopt_valsize;
2163 if (level != IPPROTO_IPV6) {
2170 * For ICMPv6 sockets, no modification allowed for checksum
2171 * offset, permit "no change" values to help existing apps.
2173 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2174 * for an ICMPv6 socket will fail."
2175 * The current behavior does not meet RFC3542.
2179 if (optlen != sizeof(int)) {
2183 error = sooptcopyin(sopt, &optval, sizeof(optval),
2187 if ((optval % 2) != 0) {
2188 /* the API assumes even offset values */
2190 } else if (so->so_proto->pr_protocol ==
2192 if (optval != icmp6off)
2195 in6p->in6p_cksum = optval;
2199 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2202 optval = in6p->in6p_cksum;
2204 error = sooptcopyout(sopt, &optval, sizeof(optval));
2214 error = ENOPROTOOPT;
2222 * Set up IP6 options in pcb for insertion in output packets or
2223 * specifying behavior of outgoing packets.
2226 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2227 struct socket *so, struct sockopt *sopt)
2229 struct ip6_pktopts *opt = *pktopt;
2231 struct thread *td = sopt->sopt_td;
2233 /* turn off any old options. */
2236 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2237 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2238 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2239 printf("ip6_pcbopts: all specified options are cleared.\n");
2241 ip6_clearpktopts(opt, -1);
2243 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2246 if (!m || m->m_len == 0) {
2248 * Only turning off any previous options, regardless of
2249 * whether the opt is just created or given.
2251 free(opt, M_IP6OPT);
2255 /* set options specified by user. */
2256 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2257 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2258 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2259 free(opt, M_IP6OPT);
2267 * initialize ip6_pktopts. beware that there are non-zero default values in
2271 ip6_initpktopts(struct ip6_pktopts *opt)
2274 bzero(opt, sizeof(*opt));
2275 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2276 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2277 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2278 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2282 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2283 struct ucred *cred, int uproto)
2285 struct ip6_pktopts *opt;
2287 if (*pktopt == NULL) {
2288 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2290 ip6_initpktopts(*pktopt);
2294 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2297 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2298 if (pktopt && pktopt->field) { \
2299 INP_RUNLOCK(in6p); \
2300 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2301 malloc_optdata = true; \
2303 if (in6p->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
2304 INP_RUNLOCK(in6p); \
2305 free(optdata, M_TEMP); \
2306 return (ECONNRESET); \
2308 pktopt = in6p->in6p_outputopts; \
2309 if (pktopt && pktopt->field) { \
2310 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2311 bcopy(&pktopt->field, optdata, optdatalen); \
2313 free(optdata, M_TEMP); \
2315 malloc_optdata = false; \
2320 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2321 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2323 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2324 pktopt->field->sa_len)
2327 ip6_getpcbopt(struct inpcb *in6p, int optname, struct sockopt *sopt)
2329 void *optdata = NULL;
2330 bool malloc_optdata = false;
2333 struct in6_pktinfo null_pktinfo;
2334 int deftclass = 0, on;
2335 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2336 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2337 struct ip6_pktopts *pktopt;
2340 pktopt = in6p->in6p_outputopts;
2344 optdata = (void *)&null_pktinfo;
2345 if (pktopt && pktopt->ip6po_pktinfo) {
2346 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2347 sizeof(null_pktinfo));
2348 in6_clearscope(&null_pktinfo.ipi6_addr);
2350 /* XXX: we don't have to do this every time... */
2351 bzero(&null_pktinfo, sizeof(null_pktinfo));
2353 optdatalen = sizeof(struct in6_pktinfo);
2356 if (pktopt && pktopt->ip6po_tclass >= 0)
2357 deftclass = pktopt->ip6po_tclass;
2358 optdata = (void *)&deftclass;
2359 optdatalen = sizeof(int);
2362 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2365 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2367 case IPV6_RTHDRDSTOPTS:
2368 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2371 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2374 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2376 case IPV6_USE_MIN_MTU:
2378 defminmtu = pktopt->ip6po_minmtu;
2379 optdata = (void *)&defminmtu;
2380 optdatalen = sizeof(int);
2383 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2387 optdata = (void *)&on;
2388 optdatalen = sizeof(on);
2390 case IPV6_PREFER_TEMPADDR:
2392 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2393 optdata = (void *)&defpreftemp;
2394 optdatalen = sizeof(int);
2396 default: /* should not happen */
2398 panic("ip6_getpcbopt: unexpected option\n");
2401 return (ENOPROTOOPT);
2405 error = sooptcopyout(sopt, optdata, optdatalen);
2407 free(optdata, M_TEMP);
2413 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2418 if (optname == -1 || optname == IPV6_PKTINFO) {
2419 if (pktopt->ip6po_pktinfo)
2420 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2421 pktopt->ip6po_pktinfo = NULL;
2423 if (optname == -1 || optname == IPV6_HOPLIMIT)
2424 pktopt->ip6po_hlim = -1;
2425 if (optname == -1 || optname == IPV6_TCLASS)
2426 pktopt->ip6po_tclass = -1;
2427 if (optname == -1 || optname == IPV6_NEXTHOP) {
2428 if (pktopt->ip6po_nextroute.ro_rt) {
2429 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2430 pktopt->ip6po_nextroute.ro_rt = NULL;
2432 if (pktopt->ip6po_nexthop)
2433 free(pktopt->ip6po_nexthop, M_IP6OPT);
2434 pktopt->ip6po_nexthop = NULL;
2436 if (optname == -1 || optname == IPV6_HOPOPTS) {
2437 if (pktopt->ip6po_hbh)
2438 free(pktopt->ip6po_hbh, M_IP6OPT);
2439 pktopt->ip6po_hbh = NULL;
2441 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2442 if (pktopt->ip6po_dest1)
2443 free(pktopt->ip6po_dest1, M_IP6OPT);
2444 pktopt->ip6po_dest1 = NULL;
2446 if (optname == -1 || optname == IPV6_RTHDR) {
2447 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2448 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2449 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2450 if (pktopt->ip6po_route.ro_rt) {
2451 RTFREE(pktopt->ip6po_route.ro_rt);
2452 pktopt->ip6po_route.ro_rt = NULL;
2455 if (optname == -1 || optname == IPV6_DSTOPTS) {
2456 if (pktopt->ip6po_dest2)
2457 free(pktopt->ip6po_dest2, M_IP6OPT);
2458 pktopt->ip6po_dest2 = NULL;
2462 #define PKTOPT_EXTHDRCPY(type) \
2465 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2466 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2467 if (dst->type == NULL)\
2469 bcopy(src->type, dst->type, hlen);\
2471 } while (/*CONSTCOND*/ 0)
2474 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2476 if (dst == NULL || src == NULL) {
2477 printf("ip6_clearpktopts: invalid argument\n");
2481 dst->ip6po_hlim = src->ip6po_hlim;
2482 dst->ip6po_tclass = src->ip6po_tclass;
2483 dst->ip6po_flags = src->ip6po_flags;
2484 dst->ip6po_minmtu = src->ip6po_minmtu;
2485 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2486 if (src->ip6po_pktinfo) {
2487 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2489 if (dst->ip6po_pktinfo == NULL)
2491 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2493 if (src->ip6po_nexthop) {
2494 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2496 if (dst->ip6po_nexthop == NULL)
2498 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2499 src->ip6po_nexthop->sa_len);
2501 PKTOPT_EXTHDRCPY(ip6po_hbh);
2502 PKTOPT_EXTHDRCPY(ip6po_dest1);
2503 PKTOPT_EXTHDRCPY(ip6po_dest2);
2504 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2508 ip6_clearpktopts(dst, -1);
2511 #undef PKTOPT_EXTHDRCPY
2513 struct ip6_pktopts *
2514 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2517 struct ip6_pktopts *dst;
2519 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2522 ip6_initpktopts(dst);
2524 if ((error = copypktopts(dst, src, canwait)) != 0) {
2525 free(dst, M_IP6OPT);
2533 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2538 ip6_clearpktopts(pktopt, -1);
2540 free(pktopt, M_IP6OPT);
2544 * Set IPv6 outgoing packet options based on advanced API.
2547 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2548 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2550 struct cmsghdr *cm = NULL;
2552 if (control == NULL || opt == NULL)
2555 ip6_initpktopts(opt);
2560 * If stickyopt is provided, make a local copy of the options
2561 * for this particular packet, then override them by ancillary
2563 * XXX: copypktopts() does not copy the cached route to a next
2564 * hop (if any). This is not very good in terms of efficiency,
2565 * but we can allow this since this option should be rarely
2568 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2573 * XXX: Currently, we assume all the optional information is stored
2576 if (control->m_next)
2579 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2580 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2583 if (control->m_len < CMSG_LEN(0))
2586 cm = mtod(control, struct cmsghdr *);
2587 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2589 if (cm->cmsg_level != IPPROTO_IPV6)
2592 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2593 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2602 * Set a particular packet option, as a sticky option or an ancillary data
2603 * item. "len" can be 0 only when it's a sticky option.
2604 * We have 4 cases of combination of "sticky" and "cmsg":
2605 * "sticky=0, cmsg=0": impossible
2606 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2607 * "sticky=1, cmsg=0": RFC3542 socket option
2608 * "sticky=1, cmsg=1": RFC2292 socket option
2611 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2612 struct ucred *cred, int sticky, int cmsg, int uproto)
2614 int minmtupolicy, preftemp;
2617 if (!sticky && !cmsg) {
2619 printf("ip6_setpktopt: impossible case\n");
2625 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2626 * not be specified in the context of RFC3542. Conversely,
2627 * RFC3542 types should not be specified in the context of RFC2292.
2631 case IPV6_2292PKTINFO:
2632 case IPV6_2292HOPLIMIT:
2633 case IPV6_2292NEXTHOP:
2634 case IPV6_2292HOPOPTS:
2635 case IPV6_2292DSTOPTS:
2636 case IPV6_2292RTHDR:
2637 case IPV6_2292PKTOPTIONS:
2638 return (ENOPROTOOPT);
2641 if (sticky && cmsg) {
2648 case IPV6_RTHDRDSTOPTS:
2650 case IPV6_USE_MIN_MTU:
2653 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2654 return (ENOPROTOOPT);
2659 case IPV6_2292PKTINFO:
2662 struct ifnet *ifp = NULL;
2663 struct in6_pktinfo *pktinfo;
2665 if (len != sizeof(struct in6_pktinfo))
2668 pktinfo = (struct in6_pktinfo *)buf;
2671 * An application can clear any sticky IPV6_PKTINFO option by
2672 * doing a "regular" setsockopt with ipi6_addr being
2673 * in6addr_any and ipi6_ifindex being zero.
2674 * [RFC 3542, Section 6]
2676 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2677 pktinfo->ipi6_ifindex == 0 &&
2678 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2679 ip6_clearpktopts(opt, optname);
2683 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2684 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2687 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2689 /* validate the interface index if specified. */
2690 if (pktinfo->ipi6_ifindex > V_if_index)
2692 if (pktinfo->ipi6_ifindex) {
2693 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2697 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2698 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2702 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2703 struct in6_ifaddr *ia;
2705 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2706 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2708 return (EADDRNOTAVAIL);
2709 ifa_free(&ia->ia_ifa);
2712 * We store the address anyway, and let in6_selectsrc()
2713 * validate the specified address. This is because ipi6_addr
2714 * may not have enough information about its scope zone, and
2715 * we may need additional information (such as outgoing
2716 * interface or the scope zone of a destination address) to
2717 * disambiguate the scope.
2718 * XXX: the delay of the validation may confuse the
2719 * application when it is used as a sticky option.
2721 if (opt->ip6po_pktinfo == NULL) {
2722 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2723 M_IP6OPT, M_NOWAIT);
2724 if (opt->ip6po_pktinfo == NULL)
2727 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2731 case IPV6_2292HOPLIMIT:
2737 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2738 * to simplify the ordering among hoplimit options.
2740 if (optname == IPV6_HOPLIMIT && sticky)
2741 return (ENOPROTOOPT);
2743 if (len != sizeof(int))
2746 if (*hlimp < -1 || *hlimp > 255)
2749 opt->ip6po_hlim = *hlimp;
2757 if (len != sizeof(int))
2759 tclass = *(int *)buf;
2760 if (tclass < -1 || tclass > 255)
2763 opt->ip6po_tclass = tclass;
2767 case IPV6_2292NEXTHOP:
2770 error = priv_check_cred(cred,
2771 PRIV_NETINET_SETHDROPTS, 0);
2776 if (len == 0) { /* just remove the option */
2777 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2781 /* check if cmsg_len is large enough for sa_len */
2782 if (len < sizeof(struct sockaddr) || len < *buf)
2785 switch (((struct sockaddr *)buf)->sa_family) {
2788 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2791 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2794 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2795 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2798 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2804 case AF_LINK: /* should eventually be supported */
2806 return (EAFNOSUPPORT);
2809 /* turn off the previous option, then set the new option. */
2810 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2811 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2812 if (opt->ip6po_nexthop == NULL)
2814 bcopy(buf, opt->ip6po_nexthop, *buf);
2817 case IPV6_2292HOPOPTS:
2820 struct ip6_hbh *hbh;
2824 * XXX: We don't allow a non-privileged user to set ANY HbH
2825 * options, since per-option restriction has too much
2829 error = priv_check_cred(cred,
2830 PRIV_NETINET_SETHDROPTS, 0);
2836 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2837 break; /* just remove the option */
2840 /* message length validation */
2841 if (len < sizeof(struct ip6_hbh))
2843 hbh = (struct ip6_hbh *)buf;
2844 hbhlen = (hbh->ip6h_len + 1) << 3;
2848 /* turn off the previous option, then set the new option. */
2849 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2850 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2851 if (opt->ip6po_hbh == NULL)
2853 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2858 case IPV6_2292DSTOPTS:
2860 case IPV6_RTHDRDSTOPTS:
2862 struct ip6_dest *dest, **newdest = NULL;
2865 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2866 error = priv_check_cred(cred,
2867 PRIV_NETINET_SETHDROPTS, 0);
2873 ip6_clearpktopts(opt, optname);
2874 break; /* just remove the option */
2877 /* message length validation */
2878 if (len < sizeof(struct ip6_dest))
2880 dest = (struct ip6_dest *)buf;
2881 destlen = (dest->ip6d_len + 1) << 3;
2886 * Determine the position that the destination options header
2887 * should be inserted; before or after the routing header.
2890 case IPV6_2292DSTOPTS:
2892 * The old advacned API is ambiguous on this point.
2893 * Our approach is to determine the position based
2894 * according to the existence of a routing header.
2895 * Note, however, that this depends on the order of the
2896 * extension headers in the ancillary data; the 1st
2897 * part of the destination options header must appear
2898 * before the routing header in the ancillary data,
2900 * RFC3542 solved the ambiguity by introducing
2901 * separate ancillary data or option types.
2903 if (opt->ip6po_rthdr == NULL)
2904 newdest = &opt->ip6po_dest1;
2906 newdest = &opt->ip6po_dest2;
2908 case IPV6_RTHDRDSTOPTS:
2909 newdest = &opt->ip6po_dest1;
2912 newdest = &opt->ip6po_dest2;
2916 /* turn off the previous option, then set the new option. */
2917 ip6_clearpktopts(opt, optname);
2918 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2919 if (*newdest == NULL)
2921 bcopy(dest, *newdest, destlen);
2926 case IPV6_2292RTHDR:
2929 struct ip6_rthdr *rth;
2933 ip6_clearpktopts(opt, IPV6_RTHDR);
2934 break; /* just remove the option */
2937 /* message length validation */
2938 if (len < sizeof(struct ip6_rthdr))
2940 rth = (struct ip6_rthdr *)buf;
2941 rthlen = (rth->ip6r_len + 1) << 3;
2945 switch (rth->ip6r_type) {
2946 case IPV6_RTHDR_TYPE_0:
2947 if (rth->ip6r_len == 0) /* must contain one addr */
2949 if (rth->ip6r_len % 2) /* length must be even */
2951 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2955 return (EINVAL); /* not supported */
2958 /* turn off the previous option */
2959 ip6_clearpktopts(opt, IPV6_RTHDR);
2960 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2961 if (opt->ip6po_rthdr == NULL)
2963 bcopy(rth, opt->ip6po_rthdr, rthlen);
2968 case IPV6_USE_MIN_MTU:
2969 if (len != sizeof(int))
2971 minmtupolicy = *(int *)buf;
2972 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2973 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2974 minmtupolicy != IP6PO_MINMTU_ALL) {
2977 opt->ip6po_minmtu = minmtupolicy;
2981 if (len != sizeof(int))
2984 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2986 * we ignore this option for TCP sockets.
2987 * (RFC3542 leaves this case unspecified.)
2989 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2991 opt->ip6po_flags |= IP6PO_DONTFRAG;
2994 case IPV6_PREFER_TEMPADDR:
2995 if (len != sizeof(int))
2997 preftemp = *(int *)buf;
2998 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2999 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3000 preftemp != IP6PO_TEMPADDR_PREFER) {
3003 opt->ip6po_prefer_tempaddr = preftemp;
3007 return (ENOPROTOOPT);
3008 } /* end of switch */
3014 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3015 * packet to the input queue of a specified interface. Note that this
3016 * calls the output routine of the loopback "driver", but with an interface
3017 * pointer that might NOT be &loif -- easier than replicating that code here.
3020 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3023 struct ip6_hdr *ip6;
3025 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3030 * Make sure to deep-copy IPv6 header portion in case the data
3031 * is in an mbuf cluster, so that we can safely override the IPv6
3032 * header portion later.
3034 if (!M_WRITABLE(copym) ||
3035 copym->m_len < sizeof(struct ip6_hdr)) {
3036 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3040 ip6 = mtod(copym, struct ip6_hdr *);
3042 * clear embedded scope identifiers if necessary.
3043 * in6_clearscope will touch the addresses only when necessary.
3045 in6_clearscope(&ip6->ip6_src);
3046 in6_clearscope(&ip6->ip6_dst);
3047 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3048 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3050 copym->m_pkthdr.csum_data = 0xffff;
3052 if_simloop(ifp, copym, AF_INET6, 0);
3056 * Chop IPv6 header off from the payload.
3059 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3062 struct ip6_hdr *ip6;
3064 ip6 = mtod(m, struct ip6_hdr *);
3065 if (m->m_len > sizeof(*ip6)) {
3066 mh = m_gethdr(M_NOWAIT, MT_DATA);
3071 m_move_pkthdr(mh, m);
3072 M_ALIGN(mh, sizeof(*ip6));
3073 m->m_len -= sizeof(*ip6);
3074 m->m_data += sizeof(*ip6);
3077 m->m_len = sizeof(*ip6);
3078 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3080 exthdrs->ip6e_ip6 = m;
3085 * Compute IPv6 extension header length.
3088 ip6_optlen(struct inpcb *in6p)
3092 if (!in6p->in6p_outputopts)
3097 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3099 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3100 if (in6p->in6p_outputopts->ip6po_rthdr)
3101 /* dest1 is valid with rthdr only */
3102 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3103 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3104 len += elen(in6p->in6p_outputopts->ip6po_dest2);