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 ip6_pktopts *, 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(u_short) > m->m_len) {
203 printf("%s: delayed m_pullup, m->len: %d plen %u off %u "
204 "csum_flags=%b\n", __func__, m->m_len, plen, offset,
205 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
207 * XXX this should not happen, but if it does, the correct
208 * behavior may be to insert the checksum in the appropriate
209 * next mbuf in the chain.
213 *(u_short *)(m->m_data + offset) = csum;
217 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
218 int fraglen , uint32_t id)
220 struct mbuf *m, **mnext, *m_frgpart;
221 struct ip6_hdr *ip6, *mhip6;
222 struct ip6_frag *ip6f;
225 int tlen = m0->m_pkthdr.len;
227 KASSERT((fraglen % 8 == 0), ("Fragment length must be a multiple of 8"));
230 ip6 = mtod(m, struct ip6_hdr *);
231 mnext = &m->m_nextpkt;
233 for (off = hlen; off < tlen; off += fraglen) {
234 m = m_gethdr(M_NOWAIT, MT_DATA);
236 IP6STAT_INC(ip6s_odropped);
239 m->m_flags = m0->m_flags & M_COPYFLAGS;
241 mnext = &m->m_nextpkt;
242 m->m_data += max_linkhdr;
243 mhip6 = mtod(m, struct ip6_hdr *);
245 m->m_len = sizeof(*mhip6);
246 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
248 IP6STAT_INC(ip6s_odropped);
251 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
252 if (off + fraglen >= tlen)
253 fraglen = tlen - off;
255 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
256 mhip6->ip6_plen = htons((u_short)(fraglen + hlen +
257 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
258 if ((m_frgpart = m_copym(m0, off, fraglen, M_NOWAIT)) == NULL) {
259 IP6STAT_INC(ip6s_odropped);
263 m->m_pkthdr.len = fraglen + hlen + sizeof(*ip6f);
264 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
265 m->m_pkthdr.rcvif = NULL;
266 ip6f->ip6f_reserved = 0;
267 ip6f->ip6f_ident = id;
268 ip6f->ip6f_nxt = nextproto;
269 IP6STAT_INC(ip6s_ofragments);
270 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
277 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
278 * header (with pri, len, nxt, hlim, src, dst).
279 * This function may modify ver and hlim only.
280 * The mbuf chain containing the packet will be freed.
281 * The mbuf opt, if present, will not be freed.
282 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
283 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
284 * then result of route lookup is stored in ro->ro_rt.
286 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
287 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
290 * ifpp - XXX: just for statistics
293 * XXX TODO: no flowid is assigned for outbound flows?
296 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
297 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
298 struct ifnet **ifpp, struct inpcb *inp)
301 struct ifnet *ifp, *origifp;
303 struct mbuf *mprev = NULL;
305 struct route_in6 ip6route;
306 struct rtentry *rt = NULL;
307 struct sockaddr_in6 *dst, src_sa, dst_sa;
308 struct in6_addr odst;
310 struct in6_ifaddr *ia = NULL;
312 int alwaysfrag, dontfrag;
313 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
314 struct ip6_exthdrs exthdrs;
315 struct in6_addr src0, dst0;
317 struct route_in6 *ro_pmtu = NULL;
322 struct m_tag *fwd_tag = NULL;
326 INP_LOCK_ASSERT(inp);
327 M_SETFIB(m, inp->inp_inc.inc_fibnum);
328 if ((flags & IP_NODEFAULTFLOWID) == 0) {
329 /* unconditionally set flowid */
330 m->m_pkthdr.flowid = inp->inp_flowid;
331 M_HASHTYPE_SET(m, inp->inp_flowtype);
335 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
337 * IPSec checking which handles several cases.
338 * FAST IPSEC: We re-injected the packet.
339 * XXX: need scope argument.
341 if (IPSEC_ENABLED(ipv6)) {
342 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
343 if (error == EINPROGRESS)
350 bzero(&exthdrs, sizeof(exthdrs));
352 /* Hop-by-Hop options header */
353 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
354 /* Destination options header(1st part) */
355 if (opt->ip6po_rthdr) {
357 * Destination options header(1st part)
358 * This only makes sense with a routing header.
359 * See Section 9.2 of RFC 3542.
360 * Disabling this part just for MIP6 convenience is
361 * a bad idea. We need to think carefully about a
362 * way to make the advanced API coexist with MIP6
363 * options, which might automatically be inserted in
366 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
369 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
370 /* Destination options header(2nd part) */
371 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
375 * Calculate the total length of the extension header chain.
376 * Keep the length of the unfragmentable part for fragmentation.
379 if (exthdrs.ip6e_hbh)
380 optlen += exthdrs.ip6e_hbh->m_len;
381 if (exthdrs.ip6e_dest1)
382 optlen += exthdrs.ip6e_dest1->m_len;
383 if (exthdrs.ip6e_rthdr)
384 optlen += exthdrs.ip6e_rthdr->m_len;
385 unfragpartlen = optlen + sizeof(struct ip6_hdr);
387 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
388 if (exthdrs.ip6e_dest2)
389 optlen += exthdrs.ip6e_dest2->m_len;
392 * If there is at least one extension header,
393 * separate IP6 header from the payload.
395 if (optlen && !hdrsplit) {
396 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
400 m = exthdrs.ip6e_ip6;
404 ip6 = mtod(m, struct ip6_hdr *);
406 /* adjust mbuf packet header length */
407 m->m_pkthdr.len += optlen;
408 plen = m->m_pkthdr.len - sizeof(*ip6);
410 /* If this is a jumbo payload, insert a jumbo payload option. */
411 if (plen > IPV6_MAXPACKET) {
413 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
417 m = exthdrs.ip6e_ip6;
421 ip6 = mtod(m, struct ip6_hdr *);
422 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
426 ip6->ip6_plen = htons(plen);
429 * Concatenate headers and fill in next header fields.
430 * Here we have, on "m"
432 * and we insert headers accordingly. Finally, we should be getting:
433 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
435 * during the header composing process, "m" points to IPv6 header.
436 * "mprev" points to an extension header prior to esp.
438 u_char *nexthdrp = &ip6->ip6_nxt;
442 * we treat dest2 specially. this makes IPsec processing
443 * much easier. the goal here is to make mprev point the
444 * mbuf prior to dest2.
446 * result: IPv6 dest2 payload
447 * m and mprev will point to IPv6 header.
449 if (exthdrs.ip6e_dest2) {
451 panic("assumption failed: hdr not split");
452 exthdrs.ip6e_dest2->m_next = m->m_next;
453 m->m_next = exthdrs.ip6e_dest2;
454 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
455 ip6->ip6_nxt = IPPROTO_DSTOPTS;
459 * result: IPv6 hbh dest1 rthdr dest2 payload
460 * m will point to IPv6 header. mprev will point to the
461 * extension header prior to dest2 (rthdr in the above case).
463 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
464 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
466 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
470 * If there is a routing header, discard the packet.
472 if (exthdrs.ip6e_rthdr) {
477 /* Source address validation */
478 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
479 (flags & IPV6_UNSPECSRC) == 0) {
481 IP6STAT_INC(ip6s_badscope);
484 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
486 IP6STAT_INC(ip6s_badscope);
490 IP6STAT_INC(ip6s_localout);
497 bzero((caddr_t)ro, sizeof(*ro));
500 if (opt && opt->ip6po_rthdr)
501 ro = &opt->ip6po_route;
502 dst = (struct sockaddr_in6 *)&ro->ro_dst;
503 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
506 * if specified, try to fill in the traffic class field.
507 * do not override if a non-zero value is already set.
508 * we check the diffserv field and the ecn field separately.
510 if (opt && opt->ip6po_tclass >= 0) {
513 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
515 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
518 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
521 /* fill in or override the hop limit field, if necessary. */
522 if (opt && opt->ip6po_hlim != -1)
523 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
524 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
526 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
528 ip6->ip6_hlim = V_ip6_defmcasthlim;
531 * Validate route against routing table additions;
532 * a better/more specific route might have been added.
533 * Make sure address family is set in route.
536 ro->ro_dst.sin6_family = AF_INET6;
537 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
539 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
540 ro->ro_dst.sin6_family == AF_INET6 &&
541 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
543 ifp = ro->ro_rt->rt_ifp;
546 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
548 if (fwd_tag == NULL) {
549 bzero(&dst_sa, sizeof(dst_sa));
550 dst_sa.sin6_family = AF_INET6;
551 dst_sa.sin6_len = sizeof(dst_sa);
552 dst_sa.sin6_addr = ip6->ip6_dst;
554 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
558 in6_ifstat_inc(ifp, ifs6_out_discard);
564 * If in6_selectroute() does not return a route entry,
565 * dst may not have been updated.
567 *dst = dst_sa; /* XXX */
571 * then rt (for unicast) and ifp must be non-NULL valid values.
573 if ((flags & IPV6_FORWARDING) == 0) {
574 /* XXX: the FORWARDING flag can be set for mrouting. */
575 in6_ifstat_inc(ifp, ifs6_out_request);
578 ia = (struct in6_ifaddr *)(rt->rt_ifa);
579 counter_u64_add(rt->rt_pksent, 1);
584 * The outgoing interface must be in the zone of source and
585 * destination addresses.
590 if (in6_setscope(&src0, origifp, &zone))
592 bzero(&src_sa, sizeof(src_sa));
593 src_sa.sin6_family = AF_INET6;
594 src_sa.sin6_len = sizeof(src_sa);
595 src_sa.sin6_addr = ip6->ip6_src;
596 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
600 if (in6_setscope(&dst0, origifp, &zone))
602 /* re-initialize to be sure */
603 bzero(&dst_sa, sizeof(dst_sa));
604 dst_sa.sin6_family = AF_INET6;
605 dst_sa.sin6_len = sizeof(dst_sa);
606 dst_sa.sin6_addr = ip6->ip6_dst;
607 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
611 /* We should use ia_ifp to support the case of
612 * sending packets to an address of our own.
614 if (ia != NULL && ia->ia_ifp)
617 /* scope check is done. */
621 IP6STAT_INC(ip6s_badscope);
622 in6_ifstat_inc(origifp, ifs6_out_discard);
624 error = EHOSTUNREACH; /* XXX */
628 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
629 if (opt && opt->ip6po_nextroute.ro_rt) {
631 * The nexthop is explicitly specified by the
632 * application. We assume the next hop is an IPv6
635 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
637 else if ((rt->rt_flags & RTF_GATEWAY))
638 dst = (struct sockaddr_in6 *)rt->rt_gateway;
641 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
642 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
644 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
645 in6_ifstat_inc(ifp, ifs6_out_mcast);
647 * Confirm that the outgoing interface supports multicast.
649 if (!(ifp->if_flags & IFF_MULTICAST)) {
650 IP6STAT_INC(ip6s_noroute);
651 in6_ifstat_inc(ifp, ifs6_out_discard);
655 if ((im6o == NULL && in6_mcast_loop) ||
656 (im6o && im6o->im6o_multicast_loop)) {
658 * Loop back multicast datagram if not expressly
659 * forbidden to do so, even if we have not joined
660 * the address; protocols will filter it later,
661 * thus deferring a hash lookup and lock acquisition
662 * at the expense of an m_copym().
664 ip6_mloopback(ifp, m);
667 * If we are acting as a multicast router, perform
668 * multicast forwarding as if the packet had just
669 * arrived on the interface to which we are about
670 * to send. The multicast forwarding function
671 * recursively calls this function, using the
672 * IPV6_FORWARDING flag to prevent infinite recursion.
674 * Multicasts that are looped back by ip6_mloopback(),
675 * above, will be forwarded by the ip6_input() routine,
678 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
680 * XXX: ip6_mforward expects that rcvif is NULL
681 * when it is called from the originating path.
682 * However, it may not always be the case.
684 m->m_pkthdr.rcvif = NULL;
685 if (ip6_mforward(ip6, ifp, m) != 0) {
692 * Multicasts with a hoplimit of zero may be looped back,
693 * above, but must not be transmitted on a network.
694 * Also, multicasts addressed to the loopback interface
695 * are not sent -- the above call to ip6_mloopback() will
696 * loop back a copy if this host actually belongs to the
697 * destination group on the loopback interface.
699 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
700 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
707 * Fill the outgoing inteface to tell the upper layer
708 * to increment per-interface statistics.
713 /* Determine path MTU. */
714 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
715 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
719 * The caller of this function may specify to use the minimum MTU
721 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
722 * setting. The logic is a bit complicated; by default, unicast
723 * packets will follow path MTU while multicast packets will be sent at
724 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
725 * including unicast ones will be sent at the minimum MTU. Multicast
726 * packets will always be sent at the minimum MTU unless
727 * IP6PO_MINMTU_DISABLE is explicitly specified.
728 * See RFC 3542 for more details.
730 if (mtu > IPV6_MMTU) {
731 if ((flags & IPV6_MINMTU))
733 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
735 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
737 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
743 * clear embedded scope identifiers if necessary.
744 * in6_clearscope will touch the addresses only when necessary.
746 in6_clearscope(&ip6->ip6_src);
747 in6_clearscope(&ip6->ip6_dst);
750 * If the outgoing packet contains a hop-by-hop options header,
751 * it must be examined and processed even by the source node.
752 * (RFC 2460, section 4.)
754 if (exthdrs.ip6e_hbh) {
755 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
756 u_int32_t dummy; /* XXX unused */
757 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
760 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
761 panic("ip6e_hbh is not contiguous");
764 * XXX: if we have to send an ICMPv6 error to the sender,
765 * we need the M_LOOP flag since icmp6_error() expects
766 * the IPv6 and the hop-by-hop options header are
767 * contiguous unless the flag is set.
769 m->m_flags |= M_LOOP;
770 m->m_pkthdr.rcvif = ifp;
771 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
772 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
773 &dummy, &plen) < 0) {
774 /* m was already freed at this point */
775 error = EINVAL;/* better error? */
778 m->m_flags &= ~M_LOOP; /* XXX */
779 m->m_pkthdr.rcvif = NULL;
782 /* Jump over all PFIL processing if hooks are not active. */
783 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
787 /* Run through list of hooks for output packets. */
788 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
789 if (error != 0 || m == NULL)
792 ip6 = mtod(m, struct ip6_hdr *);
795 /* See if destination IP address was changed by packet filter. */
796 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
797 m->m_flags |= M_SKIP_FIREWALL;
798 /* If destination is now ourself drop to ip6_input(). */
799 if (in6_localip(&ip6->ip6_dst)) {
800 m->m_flags |= M_FASTFWD_OURS;
801 if (m->m_pkthdr.rcvif == NULL)
802 m->m_pkthdr.rcvif = V_loif;
803 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
804 m->m_pkthdr.csum_flags |=
805 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
806 m->m_pkthdr.csum_data = 0xffff;
809 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
810 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
812 error = netisr_queue(NETISR_IPV6, m);
816 needfiblookup = 1; /* Redo the routing table lookup. */
818 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
822 /* See if fib was changed by packet filter. */
823 if (fibnum != M_GETFIB(m)) {
824 m->m_flags |= M_SKIP_FIREWALL;
825 fibnum = M_GETFIB(m);
829 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
835 /* See if local, if yes, send it to netisr. */
836 if (m->m_flags & M_FASTFWD_OURS) {
837 if (m->m_pkthdr.rcvif == NULL)
838 m->m_pkthdr.rcvif = V_loif;
839 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
840 m->m_pkthdr.csum_flags |=
841 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
842 m->m_pkthdr.csum_data = 0xffff;
845 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
846 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
848 error = netisr_queue(NETISR_IPV6, m);
851 /* Or forward to some other address? */
852 if ((m->m_flags & M_IP6_NEXTHOP) &&
853 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
854 dst = (struct sockaddr_in6 *)&ro->ro_dst;
855 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
856 m->m_flags |= M_SKIP_FIREWALL;
857 m->m_flags &= ~M_IP6_NEXTHOP;
858 m_tag_delete(m, fwd_tag);
864 * Send the packet to the outgoing interface.
865 * If necessary, do IPv6 fragmentation before sending.
867 * the logic here is rather complex:
868 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
869 * 1-a: send as is if tlen <= path mtu
870 * 1-b: fragment if tlen > path mtu
872 * 2: if user asks us not to fragment (dontfrag == 1)
873 * 2-a: send as is if tlen <= interface mtu
874 * 2-b: error if tlen > interface mtu
876 * 3: if we always need to attach fragment header (alwaysfrag == 1)
879 * 4: if dontfrag == 1 && alwaysfrag == 1
880 * error, as we cannot handle this conflicting request
882 sw_csum = m->m_pkthdr.csum_flags;
884 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
885 sw_csum &= ~ifp->if_hwassist;
889 * If we added extension headers, we will not do TSO and calculate the
890 * checksums ourselves for now.
891 * XXX-BZ Need a framework to know when the NIC can handle it, even
894 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
895 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
896 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
899 if (sw_csum & CSUM_SCTP_IPV6) {
900 sw_csum &= ~CSUM_SCTP_IPV6;
901 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
904 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
905 tlen = m->m_pkthdr.len;
907 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
911 if (dontfrag && alwaysfrag) { /* case 4 */
912 /* conflicting request - can't transmit */
916 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
918 * Even if the DONTFRAG option is specified, we cannot send the
919 * packet when the data length is larger than the MTU of the
920 * outgoing interface.
921 * Notify the error by sending IPV6_PATHMTU ancillary data if
922 * application wanted to know the MTU value. Also return an
923 * error code (this is not described in the API spec).
926 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
932 * transmit packet without fragmentation
934 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
935 struct in6_ifaddr *ia6;
937 ip6 = mtod(m, struct ip6_hdr *);
938 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
940 /* Record statistics for this interface address. */
941 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
942 counter_u64_add(ia6->ia_ifa.ifa_obytes,
944 ifa_free(&ia6->ia_ifa);
948 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
949 in_pcboutput_txrtlmt(inp, ifp, m);
950 /* stamp send tag on mbuf */
951 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
953 m->m_pkthdr.snd_tag = NULL;
956 error = nd6_output_ifp(ifp, origifp, m, dst,
959 /* check for route change */
961 in_pcboutput_eagain(inp);
967 * try to fragment the packet. case 1-b and 3
969 if (mtu < IPV6_MMTU) {
970 /* path MTU cannot be less than IPV6_MMTU */
972 in6_ifstat_inc(ifp, ifs6_out_fragfail);
974 } else if (ip6->ip6_plen == 0) {
975 /* jumbo payload cannot be fragmented */
977 in6_ifstat_inc(ifp, ifs6_out_fragfail);
983 * Too large for the destination or interface;
984 * fragment if possible.
985 * Must be able to put at least 8 bytes per fragment.
987 hlen = unfragpartlen;
988 if (mtu > IPV6_MAXPACKET)
989 mtu = IPV6_MAXPACKET;
991 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
994 in6_ifstat_inc(ifp, ifs6_out_fragfail);
999 * If the interface will not calculate checksums on
1000 * fragmented packets, then do it here.
1001 * XXX-BZ handle the hw offloading case. Need flags.
1003 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1004 in6_delayed_cksum(m, plen, hlen);
1005 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1008 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1009 sctp_delayed_cksum(m, hlen);
1010 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1014 * Change the next header field of the last header in the
1015 * unfragmentable part.
1017 if (exthdrs.ip6e_rthdr) {
1018 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1019 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1020 } else if (exthdrs.ip6e_dest1) {
1021 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1022 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1023 } else if (exthdrs.ip6e_hbh) {
1024 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1025 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1027 nextproto = ip6->ip6_nxt;
1028 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1032 * Loop through length of segment after first fragment,
1033 * make new header and copy data of each part and link onto
1037 id = htonl(ip6_randomid());
1038 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1041 in6_ifstat_inc(ifp, ifs6_out_fragok);
1045 * Remove leading garbages.
1051 for (m0 = m; m; m = m0) {
1055 /* Record statistics for this interface address. */
1057 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1058 counter_u64_add(ia->ia_ifa.ifa_obytes,
1063 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
1064 in_pcboutput_txrtlmt(inp, ifp, m);
1065 /* stamp send tag on mbuf */
1066 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
1068 m->m_pkthdr.snd_tag = NULL;
1071 error = nd6_output_ifp(ifp, origifp, m, dst,
1072 (struct route *)ro);
1074 /* check for route change */
1075 if (error == EAGAIN)
1076 in_pcboutput_eagain(inp);
1083 IP6STAT_INC(ip6s_fragmented);
1086 if (ro == &ip6route)
1091 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1092 m_freem(exthdrs.ip6e_dest1);
1093 m_freem(exthdrs.ip6e_rthdr);
1094 m_freem(exthdrs.ip6e_dest2);
1103 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1107 if (hlen > MCLBYTES)
1108 return (ENOBUFS); /* XXX */
1111 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1113 m = m_get(M_NOWAIT, MT_DATA);
1118 bcopy(hdr, mtod(m, caddr_t), hlen);
1125 * Insert jumbo payload option.
1128 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1134 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1137 * If there is no hop-by-hop options header, allocate new one.
1138 * If there is one but it doesn't have enough space to store the
1139 * jumbo payload option, allocate a cluster to store the whole options.
1140 * Otherwise, use it to store the options.
1142 if (exthdrs->ip6e_hbh == NULL) {
1143 mopt = m_get(M_NOWAIT, MT_DATA);
1146 mopt->m_len = JUMBOOPTLEN;
1147 optbuf = mtod(mopt, u_char *);
1148 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1149 exthdrs->ip6e_hbh = mopt;
1151 struct ip6_hbh *hbh;
1153 mopt = exthdrs->ip6e_hbh;
1154 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1157 * - exthdrs->ip6e_hbh is not referenced from places
1158 * other than exthdrs.
1159 * - exthdrs->ip6e_hbh is not an mbuf chain.
1161 int oldoptlen = mopt->m_len;
1165 * XXX: give up if the whole (new) hbh header does
1166 * not fit even in an mbuf cluster.
1168 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1172 * As a consequence, we must always prepare a cluster
1175 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1178 n->m_len = oldoptlen + JUMBOOPTLEN;
1179 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1181 optbuf = mtod(n, caddr_t) + oldoptlen;
1183 mopt = exthdrs->ip6e_hbh = n;
1185 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1186 mopt->m_len += JUMBOOPTLEN;
1188 optbuf[0] = IP6OPT_PADN;
1192 * Adjust the header length according to the pad and
1193 * the jumbo payload option.
1195 hbh = mtod(mopt, struct ip6_hbh *);
1196 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1199 /* fill in the option. */
1200 optbuf[2] = IP6OPT_JUMBO;
1202 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1203 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1205 /* finally, adjust the packet header length */
1206 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1213 * Insert fragment header and copy unfragmentable header portions.
1216 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1217 struct ip6_frag **frghdrp)
1219 struct mbuf *n, *mlast;
1221 if (hlen > sizeof(struct ip6_hdr)) {
1222 n = m_copym(m0, sizeof(struct ip6_hdr),
1223 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1230 /* Search for the last mbuf of unfragmentable part. */
1231 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1234 if (M_WRITABLE(mlast) &&
1235 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1236 /* use the trailing space of the last mbuf for the fragment hdr */
1237 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1239 mlast->m_len += sizeof(struct ip6_frag);
1240 m->m_pkthdr.len += sizeof(struct ip6_frag);
1242 /* allocate a new mbuf for the fragment header */
1245 mfrg = m_get(M_NOWAIT, MT_DATA);
1248 mfrg->m_len = sizeof(struct ip6_frag);
1249 *frghdrp = mtod(mfrg, struct ip6_frag *);
1250 mlast->m_next = mfrg;
1257 * Calculates IPv6 path mtu for destination @dst.
1258 * Resulting MTU is stored in @mtup.
1260 * Returns 0 on success.
1263 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1265 struct nhop6_extended nh6;
1266 struct in6_addr kdst;
1272 in6_splitscope(dst, &kdst, &scopeid);
1273 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1274 return (EHOSTUNREACH);
1279 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1280 fib6_free_nh_ext(fibnum, &nh6);
1286 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1287 * and cached data in @ro_pmtu.
1288 * MTU from (successful) route lookup is saved (along with dst)
1289 * inside @ro_pmtu to avoid subsequent route lookups after packet
1290 * filter processing.
1292 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1293 * Returns 0 on success.
1296 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1297 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1298 int *alwaysfragp, u_int fibnum, u_int proto)
1300 struct nhop6_basic nh6;
1301 struct in6_addr kdst;
1303 struct sockaddr_in6 *sa6_dst;
1310 * Here ro_pmtu has final destination address, while
1311 * ro might represent immediate destination.
1312 * Use ro_pmtu destination since mtu might differ.
1314 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1315 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1316 ro_pmtu->ro_mtu = 0;
1318 if (ro_pmtu->ro_mtu == 0) {
1319 bzero(sa6_dst, sizeof(*sa6_dst));
1320 sa6_dst->sin6_family = AF_INET6;
1321 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1322 sa6_dst->sin6_addr = *dst;
1324 in6_splitscope(dst, &kdst, &scopeid);
1325 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1327 ro_pmtu->ro_mtu = nh6.nh_mtu;
1330 mtu = ro_pmtu->ro_mtu;
1334 mtu = ro_pmtu->ro_rt->rt_mtu;
1336 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1340 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1341 * hostcache data for @dst.
1342 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1344 * Returns 0 on success.
1347 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1348 u_long *mtup, int *alwaysfragp, u_int proto)
1356 struct in_conninfo inc;
1358 bzero(&inc, sizeof(inc));
1359 inc.inc_flags |= INC_ISIPV6;
1360 inc.inc6_faddr = *dst;
1362 ifmtu = IN6_LINKMTU(ifp);
1364 /* TCP is known to react to pmtu changes so skip hc */
1365 if (proto != IPPROTO_TCP)
1366 mtu = tcp_hc_getmtu(&inc);
1369 mtu = min(mtu, rt_mtu);
1374 else if (mtu < IPV6_MMTU) {
1376 * RFC2460 section 5, last paragraph:
1377 * if we record ICMPv6 too big message with
1378 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1379 * or smaller, with framgent header attached.
1380 * (fragment header is needed regardless from the
1381 * packet size, for translators to identify packets)
1387 mtu = IN6_LINKMTU(ifp);
1389 error = EHOSTUNREACH; /* XXX */
1393 *alwaysfragp = alwaysfrag;
1398 * IP6 socket option processing.
1401 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1403 int optdatalen, uproto;
1405 struct inpcb *in6p = sotoinpcb(so);
1407 int level, op, optname;
1411 uint32_t rss_bucket;
1416 * Don't use more than a quarter of mbuf clusters. N.B.:
1417 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1418 * on LP64 architectures, so cast to u_long to avoid undefined
1419 * behavior. ILP32 architectures cannot have nmbclusters
1420 * large enough to overflow for other reasons.
1422 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1424 level = sopt->sopt_level;
1425 op = sopt->sopt_dir;
1426 optname = sopt->sopt_name;
1427 optlen = sopt->sopt_valsize;
1431 uproto = (int)so->so_proto->pr_protocol;
1433 if (level != IPPROTO_IPV6) {
1436 if (sopt->sopt_level == SOL_SOCKET &&
1437 sopt->sopt_dir == SOPT_SET) {
1438 switch (sopt->sopt_name) {
1441 if ((so->so_options & SO_REUSEADDR) != 0)
1442 in6p->inp_flags2 |= INP_REUSEADDR;
1444 in6p->inp_flags2 &= ~INP_REUSEADDR;
1450 if ((so->so_options & SO_REUSEPORT) != 0)
1451 in6p->inp_flags2 |= INP_REUSEPORT;
1453 in6p->inp_flags2 &= ~INP_REUSEPORT;
1459 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1463 case SO_MAX_PACING_RATE:
1466 in6p->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1477 } else { /* level == IPPROTO_IPV6 */
1482 case IPV6_2292PKTOPTIONS:
1483 #ifdef IPV6_PKTOPTIONS
1484 case IPV6_PKTOPTIONS:
1489 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1490 printf("ip6_ctloutput: mbuf limit hit\n");
1495 error = soopt_getm(sopt, &m); /* XXX */
1498 error = soopt_mcopyin(sopt, m); /* XXX */
1501 error = ip6_pcbopts(&in6p->in6p_outputopts,
1503 m_freem(m); /* XXX */
1508 * Use of some Hop-by-Hop options or some
1509 * Destination options, might require special
1510 * privilege. That is, normal applications
1511 * (without special privilege) might be forbidden
1512 * from setting certain options in outgoing packets,
1513 * and might never see certain options in received
1514 * packets. [RFC 2292 Section 6]
1515 * KAME specific note:
1516 * KAME prevents non-privileged users from sending or
1517 * receiving ANY hbh/dst options in order to avoid
1518 * overhead of parsing options in the kernel.
1520 case IPV6_RECVHOPOPTS:
1521 case IPV6_RECVDSTOPTS:
1522 case IPV6_RECVRTHDRDSTOPTS:
1524 error = priv_check(td,
1525 PRIV_NETINET_SETHDROPTS);
1530 case IPV6_UNICAST_HOPS:
1533 case IPV6_RECVPKTINFO:
1534 case IPV6_RECVHOPLIMIT:
1535 case IPV6_RECVRTHDR:
1536 case IPV6_RECVPATHMTU:
1537 case IPV6_RECVTCLASS:
1538 case IPV6_RECVFLOWID:
1540 case IPV6_RECVRSSBUCKETID:
1543 case IPV6_AUTOFLOWLABEL:
1544 case IPV6_ORIGDSTADDR:
1546 case IPV6_BINDMULTI:
1548 case IPV6_RSS_LISTEN_BUCKET:
1550 if (optname == IPV6_BINDANY && td != NULL) {
1551 error = priv_check(td,
1552 PRIV_NETINET_BINDANY);
1557 if (optlen != sizeof(int)) {
1561 error = sooptcopyin(sopt, &optval,
1562 sizeof optval, sizeof optval);
1567 case IPV6_UNICAST_HOPS:
1568 if (optval < -1 || optval >= 256)
1571 /* -1 = kernel default */
1572 in6p->in6p_hops = optval;
1573 if ((in6p->inp_vflag &
1575 in6p->inp_ip_ttl = optval;
1578 #define OPTSET(bit) \
1582 in6p->inp_flags |= (bit); \
1584 in6p->inp_flags &= ~(bit); \
1585 INP_WUNLOCK(in6p); \
1586 } while (/*CONSTCOND*/ 0)
1587 #define OPTSET2292(bit) \
1590 in6p->inp_flags |= IN6P_RFC2292; \
1592 in6p->inp_flags |= (bit); \
1594 in6p->inp_flags &= ~(bit); \
1595 INP_WUNLOCK(in6p); \
1596 } while (/*CONSTCOND*/ 0)
1597 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1599 #define OPTSET2(bit, val) do { \
1602 in6p->inp_flags2 |= bit; \
1604 in6p->inp_flags2 &= ~bit; \
1605 INP_WUNLOCK(in6p); \
1607 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1609 case IPV6_RECVPKTINFO:
1610 /* cannot mix with RFC2292 */
1611 if (OPTBIT(IN6P_RFC2292)) {
1615 OPTSET(IN6P_PKTINFO);
1620 struct ip6_pktopts **optp;
1622 /* cannot mix with RFC2292 */
1623 if (OPTBIT(IN6P_RFC2292)) {
1627 optp = &in6p->in6p_outputopts;
1628 error = ip6_pcbopt(IPV6_HOPLIMIT,
1629 (u_char *)&optval, sizeof(optval),
1630 optp, (td != NULL) ? td->td_ucred :
1635 case IPV6_RECVHOPLIMIT:
1636 /* cannot mix with RFC2292 */
1637 if (OPTBIT(IN6P_RFC2292)) {
1641 OPTSET(IN6P_HOPLIMIT);
1644 case IPV6_RECVHOPOPTS:
1645 /* cannot mix with RFC2292 */
1646 if (OPTBIT(IN6P_RFC2292)) {
1650 OPTSET(IN6P_HOPOPTS);
1653 case IPV6_RECVDSTOPTS:
1654 /* cannot mix with RFC2292 */
1655 if (OPTBIT(IN6P_RFC2292)) {
1659 OPTSET(IN6P_DSTOPTS);
1662 case IPV6_RECVRTHDRDSTOPTS:
1663 /* cannot mix with RFC2292 */
1664 if (OPTBIT(IN6P_RFC2292)) {
1668 OPTSET(IN6P_RTHDRDSTOPTS);
1671 case IPV6_RECVRTHDR:
1672 /* cannot mix with RFC2292 */
1673 if (OPTBIT(IN6P_RFC2292)) {
1680 case IPV6_RECVPATHMTU:
1682 * We ignore this option for TCP
1684 * (RFC3542 leaves this case
1687 if (uproto != IPPROTO_TCP)
1691 case IPV6_RECVFLOWID:
1692 OPTSET2(INP_RECVFLOWID, optval);
1696 case IPV6_RECVRSSBUCKETID:
1697 OPTSET2(INP_RECVRSSBUCKETID, optval);
1703 * make setsockopt(IPV6_V6ONLY)
1704 * available only prior to bind(2).
1705 * see ipng mailing list, Jun 22 2001.
1707 if (in6p->inp_lport ||
1708 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1712 OPTSET(IN6P_IPV6_V6ONLY);
1714 in6p->inp_vflag &= ~INP_IPV4;
1716 in6p->inp_vflag |= INP_IPV4;
1718 case IPV6_RECVTCLASS:
1719 /* cannot mix with RFC2292 XXX */
1720 if (OPTBIT(IN6P_RFC2292)) {
1724 OPTSET(IN6P_TCLASS);
1726 case IPV6_AUTOFLOWLABEL:
1727 OPTSET(IN6P_AUTOFLOWLABEL);
1730 case IPV6_ORIGDSTADDR:
1731 OPTSET2(INP_ORIGDSTADDR, optval);
1734 OPTSET(INP_BINDANY);
1737 case IPV6_BINDMULTI:
1738 OPTSET2(INP_BINDMULTI, optval);
1741 case IPV6_RSS_LISTEN_BUCKET:
1742 if ((optval >= 0) &&
1743 (optval < rss_getnumbuckets())) {
1744 in6p->inp_rss_listen_bucket = optval;
1745 OPTSET2(INP_RSS_BUCKET_SET, 1);
1756 case IPV6_USE_MIN_MTU:
1757 case IPV6_PREFER_TEMPADDR:
1758 if (optlen != sizeof(optval)) {
1762 error = sooptcopyin(sopt, &optval,
1763 sizeof optval, sizeof optval);
1767 struct ip6_pktopts **optp;
1768 optp = &in6p->in6p_outputopts;
1769 error = ip6_pcbopt(optname,
1770 (u_char *)&optval, sizeof(optval),
1771 optp, (td != NULL) ? td->td_ucred :
1776 case IPV6_2292PKTINFO:
1777 case IPV6_2292HOPLIMIT:
1778 case IPV6_2292HOPOPTS:
1779 case IPV6_2292DSTOPTS:
1780 case IPV6_2292RTHDR:
1782 if (optlen != sizeof(int)) {
1786 error = sooptcopyin(sopt, &optval,
1787 sizeof optval, sizeof optval);
1791 case IPV6_2292PKTINFO:
1792 OPTSET2292(IN6P_PKTINFO);
1794 case IPV6_2292HOPLIMIT:
1795 OPTSET2292(IN6P_HOPLIMIT);
1797 case IPV6_2292HOPOPTS:
1799 * Check super-user privilege.
1800 * See comments for IPV6_RECVHOPOPTS.
1803 error = priv_check(td,
1804 PRIV_NETINET_SETHDROPTS);
1808 OPTSET2292(IN6P_HOPOPTS);
1810 case IPV6_2292DSTOPTS:
1812 error = priv_check(td,
1813 PRIV_NETINET_SETHDROPTS);
1817 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1819 case IPV6_2292RTHDR:
1820 OPTSET2292(IN6P_RTHDR);
1828 case IPV6_RTHDRDSTOPTS:
1831 /* new advanced API (RFC3542) */
1833 u_char optbuf_storage[MCLBYTES];
1835 struct ip6_pktopts **optp;
1837 /* cannot mix with RFC2292 */
1838 if (OPTBIT(IN6P_RFC2292)) {
1844 * We only ensure valsize is not too large
1845 * here. Further validation will be done
1848 error = sooptcopyin(sopt, optbuf_storage,
1849 sizeof(optbuf_storage), 0);
1852 optlen = sopt->sopt_valsize;
1853 optbuf = optbuf_storage;
1854 optp = &in6p->in6p_outputopts;
1855 error = ip6_pcbopt(optname, optbuf, optlen,
1856 optp, (td != NULL) ? td->td_ucred : NULL,
1862 case IPV6_MULTICAST_IF:
1863 case IPV6_MULTICAST_HOPS:
1864 case IPV6_MULTICAST_LOOP:
1865 case IPV6_JOIN_GROUP:
1866 case IPV6_LEAVE_GROUP:
1868 case MCAST_BLOCK_SOURCE:
1869 case MCAST_UNBLOCK_SOURCE:
1870 case MCAST_JOIN_GROUP:
1871 case MCAST_LEAVE_GROUP:
1872 case MCAST_JOIN_SOURCE_GROUP:
1873 case MCAST_LEAVE_SOURCE_GROUP:
1874 error = ip6_setmoptions(in6p, sopt);
1877 case IPV6_PORTRANGE:
1878 error = sooptcopyin(sopt, &optval,
1879 sizeof optval, sizeof optval);
1885 case IPV6_PORTRANGE_DEFAULT:
1886 in6p->inp_flags &= ~(INP_LOWPORT);
1887 in6p->inp_flags &= ~(INP_HIGHPORT);
1890 case IPV6_PORTRANGE_HIGH:
1891 in6p->inp_flags &= ~(INP_LOWPORT);
1892 in6p->inp_flags |= INP_HIGHPORT;
1895 case IPV6_PORTRANGE_LOW:
1896 in6p->inp_flags &= ~(INP_HIGHPORT);
1897 in6p->inp_flags |= INP_LOWPORT;
1907 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1908 case IPV6_IPSEC_POLICY:
1909 if (IPSEC_ENABLED(ipv6)) {
1910 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
1917 error = ENOPROTOOPT;
1925 case IPV6_2292PKTOPTIONS:
1926 #ifdef IPV6_PKTOPTIONS
1927 case IPV6_PKTOPTIONS:
1930 * RFC3542 (effectively) deprecated the
1931 * semantics of the 2292-style pktoptions.
1932 * Since it was not reliable in nature (i.e.,
1933 * applications had to expect the lack of some
1934 * information after all), it would make sense
1935 * to simplify this part by always returning
1938 sopt->sopt_valsize = 0;
1941 case IPV6_RECVHOPOPTS:
1942 case IPV6_RECVDSTOPTS:
1943 case IPV6_RECVRTHDRDSTOPTS:
1944 case IPV6_UNICAST_HOPS:
1945 case IPV6_RECVPKTINFO:
1946 case IPV6_RECVHOPLIMIT:
1947 case IPV6_RECVRTHDR:
1948 case IPV6_RECVPATHMTU:
1951 case IPV6_PORTRANGE:
1952 case IPV6_RECVTCLASS:
1953 case IPV6_AUTOFLOWLABEL:
1957 case IPV6_RECVFLOWID:
1959 case IPV6_RSSBUCKETID:
1960 case IPV6_RECVRSSBUCKETID:
1962 case IPV6_BINDMULTI:
1965 case IPV6_RECVHOPOPTS:
1966 optval = OPTBIT(IN6P_HOPOPTS);
1969 case IPV6_RECVDSTOPTS:
1970 optval = OPTBIT(IN6P_DSTOPTS);
1973 case IPV6_RECVRTHDRDSTOPTS:
1974 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1977 case IPV6_UNICAST_HOPS:
1978 optval = in6p->in6p_hops;
1981 case IPV6_RECVPKTINFO:
1982 optval = OPTBIT(IN6P_PKTINFO);
1985 case IPV6_RECVHOPLIMIT:
1986 optval = OPTBIT(IN6P_HOPLIMIT);
1989 case IPV6_RECVRTHDR:
1990 optval = OPTBIT(IN6P_RTHDR);
1993 case IPV6_RECVPATHMTU:
1994 optval = OPTBIT(IN6P_MTU);
1998 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2001 case IPV6_PORTRANGE:
2004 flags = in6p->inp_flags;
2005 if (flags & INP_HIGHPORT)
2006 optval = IPV6_PORTRANGE_HIGH;
2007 else if (flags & INP_LOWPORT)
2008 optval = IPV6_PORTRANGE_LOW;
2013 case IPV6_RECVTCLASS:
2014 optval = OPTBIT(IN6P_TCLASS);
2017 case IPV6_AUTOFLOWLABEL:
2018 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2021 case IPV6_ORIGDSTADDR:
2022 optval = OPTBIT2(INP_ORIGDSTADDR);
2026 optval = OPTBIT(INP_BINDANY);
2030 optval = in6p->inp_flowid;
2034 optval = in6p->inp_flowtype;
2037 case IPV6_RECVFLOWID:
2038 optval = OPTBIT2(INP_RECVFLOWID);
2041 case IPV6_RSSBUCKETID:
2043 rss_hash2bucket(in6p->inp_flowid,
2047 optval = rss_bucket;
2052 case IPV6_RECVRSSBUCKETID:
2053 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2057 case IPV6_BINDMULTI:
2058 optval = OPTBIT2(INP_BINDMULTI);
2064 error = sooptcopyout(sopt, &optval,
2071 struct ip6_mtuinfo mtuinfo;
2073 if (!(so->so_state & SS_ISCONNECTED))
2076 * XXX: we dot not consider the case of source
2077 * routing, or optional information to specify
2078 * the outgoing interface.
2080 error = ip6_getpmtu_ctl(so->so_fibnum,
2081 &in6p->in6p_faddr, &pmtu);
2084 if (pmtu > IPV6_MAXPACKET)
2085 pmtu = IPV6_MAXPACKET;
2087 bzero(&mtuinfo, sizeof(mtuinfo));
2088 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2089 optdata = (void *)&mtuinfo;
2090 optdatalen = sizeof(mtuinfo);
2091 error = sooptcopyout(sopt, optdata,
2096 case IPV6_2292PKTINFO:
2097 case IPV6_2292HOPLIMIT:
2098 case IPV6_2292HOPOPTS:
2099 case IPV6_2292RTHDR:
2100 case IPV6_2292DSTOPTS:
2102 case IPV6_2292PKTINFO:
2103 optval = OPTBIT(IN6P_PKTINFO);
2105 case IPV6_2292HOPLIMIT:
2106 optval = OPTBIT(IN6P_HOPLIMIT);
2108 case IPV6_2292HOPOPTS:
2109 optval = OPTBIT(IN6P_HOPOPTS);
2111 case IPV6_2292RTHDR:
2112 optval = OPTBIT(IN6P_RTHDR);
2114 case IPV6_2292DSTOPTS:
2115 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2118 error = sooptcopyout(sopt, &optval,
2125 case IPV6_RTHDRDSTOPTS:
2129 case IPV6_USE_MIN_MTU:
2130 case IPV6_PREFER_TEMPADDR:
2131 error = ip6_getpcbopt(in6p->in6p_outputopts,
2135 case IPV6_MULTICAST_IF:
2136 case IPV6_MULTICAST_HOPS:
2137 case IPV6_MULTICAST_LOOP:
2139 error = ip6_getmoptions(in6p, sopt);
2142 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2143 case IPV6_IPSEC_POLICY:
2144 if (IPSEC_ENABLED(ipv6)) {
2145 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
2151 error = ENOPROTOOPT;
2161 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2163 int error = 0, optval, optlen;
2164 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2165 struct inpcb *in6p = sotoinpcb(so);
2166 int level, op, optname;
2168 level = sopt->sopt_level;
2169 op = sopt->sopt_dir;
2170 optname = sopt->sopt_name;
2171 optlen = sopt->sopt_valsize;
2173 if (level != IPPROTO_IPV6) {
2180 * For ICMPv6 sockets, no modification allowed for checksum
2181 * offset, permit "no change" values to help existing apps.
2183 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2184 * for an ICMPv6 socket will fail."
2185 * The current behavior does not meet RFC3542.
2189 if (optlen != sizeof(int)) {
2193 error = sooptcopyin(sopt, &optval, sizeof(optval),
2197 if ((optval % 2) != 0) {
2198 /* the API assumes even offset values */
2200 } else if (so->so_proto->pr_protocol ==
2202 if (optval != icmp6off)
2205 in6p->in6p_cksum = optval;
2209 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2212 optval = in6p->in6p_cksum;
2214 error = sooptcopyout(sopt, &optval, sizeof(optval));
2224 error = ENOPROTOOPT;
2232 * Set up IP6 options in pcb for insertion in output packets or
2233 * specifying behavior of outgoing packets.
2236 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2237 struct socket *so, struct sockopt *sopt)
2239 struct ip6_pktopts *opt = *pktopt;
2241 struct thread *td = sopt->sopt_td;
2243 /* turn off any old options. */
2246 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2247 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2248 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2249 printf("ip6_pcbopts: all specified options are cleared.\n");
2251 ip6_clearpktopts(opt, -1);
2253 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2256 if (!m || m->m_len == 0) {
2258 * Only turning off any previous options, regardless of
2259 * whether the opt is just created or given.
2261 free(opt, M_IP6OPT);
2265 /* set options specified by user. */
2266 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2267 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2268 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2269 free(opt, M_IP6OPT);
2277 * initialize ip6_pktopts. beware that there are non-zero default values in
2281 ip6_initpktopts(struct ip6_pktopts *opt)
2284 bzero(opt, sizeof(*opt));
2285 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2286 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2287 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2288 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2292 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2293 struct ucred *cred, int uproto)
2295 struct ip6_pktopts *opt;
2297 if (*pktopt == NULL) {
2298 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2300 ip6_initpktopts(*pktopt);
2304 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2308 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2310 void *optdata = NULL;
2312 struct ip6_ext *ip6e;
2314 struct in6_pktinfo null_pktinfo;
2315 int deftclass = 0, on;
2316 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2317 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2321 optdata = (void *)&null_pktinfo;
2322 if (pktopt && pktopt->ip6po_pktinfo) {
2323 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2324 sizeof(null_pktinfo));
2325 in6_clearscope(&null_pktinfo.ipi6_addr);
2327 /* XXX: we don't have to do this every time... */
2328 bzero(&null_pktinfo, sizeof(null_pktinfo));
2330 optdatalen = sizeof(struct in6_pktinfo);
2333 if (pktopt && pktopt->ip6po_tclass >= 0)
2334 optdata = (void *)&pktopt->ip6po_tclass;
2336 optdata = (void *)&deftclass;
2337 optdatalen = sizeof(int);
2340 if (pktopt && pktopt->ip6po_hbh) {
2341 optdata = (void *)pktopt->ip6po_hbh;
2342 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2343 optdatalen = (ip6e->ip6e_len + 1) << 3;
2347 if (pktopt && pktopt->ip6po_rthdr) {
2348 optdata = (void *)pktopt->ip6po_rthdr;
2349 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2350 optdatalen = (ip6e->ip6e_len + 1) << 3;
2353 case IPV6_RTHDRDSTOPTS:
2354 if (pktopt && pktopt->ip6po_dest1) {
2355 optdata = (void *)pktopt->ip6po_dest1;
2356 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2357 optdatalen = (ip6e->ip6e_len + 1) << 3;
2361 if (pktopt && pktopt->ip6po_dest2) {
2362 optdata = (void *)pktopt->ip6po_dest2;
2363 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2364 optdatalen = (ip6e->ip6e_len + 1) << 3;
2368 if (pktopt && pktopt->ip6po_nexthop) {
2369 optdata = (void *)pktopt->ip6po_nexthop;
2370 optdatalen = pktopt->ip6po_nexthop->sa_len;
2373 case IPV6_USE_MIN_MTU:
2375 optdata = (void *)&pktopt->ip6po_minmtu;
2377 optdata = (void *)&defminmtu;
2378 optdatalen = sizeof(int);
2381 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2385 optdata = (void *)&on;
2386 optdatalen = sizeof(on);
2388 case IPV6_PREFER_TEMPADDR:
2390 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2392 optdata = (void *)&defpreftemp;
2393 optdatalen = sizeof(int);
2395 default: /* should not happen */
2397 panic("ip6_getpcbopt: unexpected option\n");
2399 return (ENOPROTOOPT);
2402 error = sooptcopyout(sopt, optdata, optdatalen);
2408 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2413 if (optname == -1 || optname == IPV6_PKTINFO) {
2414 if (pktopt->ip6po_pktinfo)
2415 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2416 pktopt->ip6po_pktinfo = NULL;
2418 if (optname == -1 || optname == IPV6_HOPLIMIT)
2419 pktopt->ip6po_hlim = -1;
2420 if (optname == -1 || optname == IPV6_TCLASS)
2421 pktopt->ip6po_tclass = -1;
2422 if (optname == -1 || optname == IPV6_NEXTHOP) {
2423 if (pktopt->ip6po_nextroute.ro_rt) {
2424 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2425 pktopt->ip6po_nextroute.ro_rt = NULL;
2427 if (pktopt->ip6po_nexthop)
2428 free(pktopt->ip6po_nexthop, M_IP6OPT);
2429 pktopt->ip6po_nexthop = NULL;
2431 if (optname == -1 || optname == IPV6_HOPOPTS) {
2432 if (pktopt->ip6po_hbh)
2433 free(pktopt->ip6po_hbh, M_IP6OPT);
2434 pktopt->ip6po_hbh = NULL;
2436 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2437 if (pktopt->ip6po_dest1)
2438 free(pktopt->ip6po_dest1, M_IP6OPT);
2439 pktopt->ip6po_dest1 = NULL;
2441 if (optname == -1 || optname == IPV6_RTHDR) {
2442 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2443 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2444 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2445 if (pktopt->ip6po_route.ro_rt) {
2446 RTFREE(pktopt->ip6po_route.ro_rt);
2447 pktopt->ip6po_route.ro_rt = NULL;
2450 if (optname == -1 || optname == IPV6_DSTOPTS) {
2451 if (pktopt->ip6po_dest2)
2452 free(pktopt->ip6po_dest2, M_IP6OPT);
2453 pktopt->ip6po_dest2 = NULL;
2457 #define PKTOPT_EXTHDRCPY(type) \
2460 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2461 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2462 if (dst->type == NULL)\
2464 bcopy(src->type, dst->type, hlen);\
2466 } while (/*CONSTCOND*/ 0)
2469 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2471 if (dst == NULL || src == NULL) {
2472 printf("ip6_clearpktopts: invalid argument\n");
2476 dst->ip6po_hlim = src->ip6po_hlim;
2477 dst->ip6po_tclass = src->ip6po_tclass;
2478 dst->ip6po_flags = src->ip6po_flags;
2479 dst->ip6po_minmtu = src->ip6po_minmtu;
2480 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2481 if (src->ip6po_pktinfo) {
2482 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2484 if (dst->ip6po_pktinfo == NULL)
2486 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2488 if (src->ip6po_nexthop) {
2489 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2491 if (dst->ip6po_nexthop == NULL)
2493 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2494 src->ip6po_nexthop->sa_len);
2496 PKTOPT_EXTHDRCPY(ip6po_hbh);
2497 PKTOPT_EXTHDRCPY(ip6po_dest1);
2498 PKTOPT_EXTHDRCPY(ip6po_dest2);
2499 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2503 ip6_clearpktopts(dst, -1);
2506 #undef PKTOPT_EXTHDRCPY
2508 struct ip6_pktopts *
2509 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2512 struct ip6_pktopts *dst;
2514 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2517 ip6_initpktopts(dst);
2519 if ((error = copypktopts(dst, src, canwait)) != 0) {
2520 free(dst, M_IP6OPT);
2528 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2533 ip6_clearpktopts(pktopt, -1);
2535 free(pktopt, M_IP6OPT);
2539 * Set IPv6 outgoing packet options based on advanced API.
2542 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2543 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2545 struct cmsghdr *cm = NULL;
2547 if (control == NULL || opt == NULL)
2550 ip6_initpktopts(opt);
2555 * If stickyopt is provided, make a local copy of the options
2556 * for this particular packet, then override them by ancillary
2558 * XXX: copypktopts() does not copy the cached route to a next
2559 * hop (if any). This is not very good in terms of efficiency,
2560 * but we can allow this since this option should be rarely
2563 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2568 * XXX: Currently, we assume all the optional information is stored
2571 if (control->m_next)
2574 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2575 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2578 if (control->m_len < CMSG_LEN(0))
2581 cm = mtod(control, struct cmsghdr *);
2582 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2584 if (cm->cmsg_level != IPPROTO_IPV6)
2587 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2588 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2597 * Set a particular packet option, as a sticky option or an ancillary data
2598 * item. "len" can be 0 only when it's a sticky option.
2599 * We have 4 cases of combination of "sticky" and "cmsg":
2600 * "sticky=0, cmsg=0": impossible
2601 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2602 * "sticky=1, cmsg=0": RFC3542 socket option
2603 * "sticky=1, cmsg=1": RFC2292 socket option
2606 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2607 struct ucred *cred, int sticky, int cmsg, int uproto)
2609 int minmtupolicy, preftemp;
2612 if (!sticky && !cmsg) {
2614 printf("ip6_setpktopt: impossible case\n");
2620 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2621 * not be specified in the context of RFC3542. Conversely,
2622 * RFC3542 types should not be specified in the context of RFC2292.
2626 case IPV6_2292PKTINFO:
2627 case IPV6_2292HOPLIMIT:
2628 case IPV6_2292NEXTHOP:
2629 case IPV6_2292HOPOPTS:
2630 case IPV6_2292DSTOPTS:
2631 case IPV6_2292RTHDR:
2632 case IPV6_2292PKTOPTIONS:
2633 return (ENOPROTOOPT);
2636 if (sticky && cmsg) {
2643 case IPV6_RTHDRDSTOPTS:
2645 case IPV6_USE_MIN_MTU:
2648 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2649 return (ENOPROTOOPT);
2654 case IPV6_2292PKTINFO:
2657 struct ifnet *ifp = NULL;
2658 struct in6_pktinfo *pktinfo;
2660 if (len != sizeof(struct in6_pktinfo))
2663 pktinfo = (struct in6_pktinfo *)buf;
2666 * An application can clear any sticky IPV6_PKTINFO option by
2667 * doing a "regular" setsockopt with ipi6_addr being
2668 * in6addr_any and ipi6_ifindex being zero.
2669 * [RFC 3542, Section 6]
2671 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2672 pktinfo->ipi6_ifindex == 0 &&
2673 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2674 ip6_clearpktopts(opt, optname);
2678 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2679 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2682 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2684 /* validate the interface index if specified. */
2685 if (pktinfo->ipi6_ifindex > V_if_index)
2687 if (pktinfo->ipi6_ifindex) {
2688 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2692 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2693 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2697 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2698 struct in6_ifaddr *ia;
2700 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2701 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2703 return (EADDRNOTAVAIL);
2704 ifa_free(&ia->ia_ifa);
2707 * We store the address anyway, and let in6_selectsrc()
2708 * validate the specified address. This is because ipi6_addr
2709 * may not have enough information about its scope zone, and
2710 * we may need additional information (such as outgoing
2711 * interface or the scope zone of a destination address) to
2712 * disambiguate the scope.
2713 * XXX: the delay of the validation may confuse the
2714 * application when it is used as a sticky option.
2716 if (opt->ip6po_pktinfo == NULL) {
2717 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2718 M_IP6OPT, M_NOWAIT);
2719 if (opt->ip6po_pktinfo == NULL)
2722 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2726 case IPV6_2292HOPLIMIT:
2732 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2733 * to simplify the ordering among hoplimit options.
2735 if (optname == IPV6_HOPLIMIT && sticky)
2736 return (ENOPROTOOPT);
2738 if (len != sizeof(int))
2741 if (*hlimp < -1 || *hlimp > 255)
2744 opt->ip6po_hlim = *hlimp;
2752 if (len != sizeof(int))
2754 tclass = *(int *)buf;
2755 if (tclass < -1 || tclass > 255)
2758 opt->ip6po_tclass = tclass;
2762 case IPV6_2292NEXTHOP:
2765 error = priv_check_cred(cred,
2766 PRIV_NETINET_SETHDROPTS, 0);
2771 if (len == 0) { /* just remove the option */
2772 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2776 /* check if cmsg_len is large enough for sa_len */
2777 if (len < sizeof(struct sockaddr) || len < *buf)
2780 switch (((struct sockaddr *)buf)->sa_family) {
2783 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2786 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2789 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2790 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2793 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2799 case AF_LINK: /* should eventually be supported */
2801 return (EAFNOSUPPORT);
2804 /* turn off the previous option, then set the new option. */
2805 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2806 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2807 if (opt->ip6po_nexthop == NULL)
2809 bcopy(buf, opt->ip6po_nexthop, *buf);
2812 case IPV6_2292HOPOPTS:
2815 struct ip6_hbh *hbh;
2819 * XXX: We don't allow a non-privileged user to set ANY HbH
2820 * options, since per-option restriction has too much
2824 error = priv_check_cred(cred,
2825 PRIV_NETINET_SETHDROPTS, 0);
2831 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2832 break; /* just remove the option */
2835 /* message length validation */
2836 if (len < sizeof(struct ip6_hbh))
2838 hbh = (struct ip6_hbh *)buf;
2839 hbhlen = (hbh->ip6h_len + 1) << 3;
2843 /* turn off the previous option, then set the new option. */
2844 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2845 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2846 if (opt->ip6po_hbh == NULL)
2848 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2853 case IPV6_2292DSTOPTS:
2855 case IPV6_RTHDRDSTOPTS:
2857 struct ip6_dest *dest, **newdest = NULL;
2860 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2861 error = priv_check_cred(cred,
2862 PRIV_NETINET_SETHDROPTS, 0);
2868 ip6_clearpktopts(opt, optname);
2869 break; /* just remove the option */
2872 /* message length validation */
2873 if (len < sizeof(struct ip6_dest))
2875 dest = (struct ip6_dest *)buf;
2876 destlen = (dest->ip6d_len + 1) << 3;
2881 * Determine the position that the destination options header
2882 * should be inserted; before or after the routing header.
2885 case IPV6_2292DSTOPTS:
2887 * The old advacned API is ambiguous on this point.
2888 * Our approach is to determine the position based
2889 * according to the existence of a routing header.
2890 * Note, however, that this depends on the order of the
2891 * extension headers in the ancillary data; the 1st
2892 * part of the destination options header must appear
2893 * before the routing header in the ancillary data,
2895 * RFC3542 solved the ambiguity by introducing
2896 * separate ancillary data or option types.
2898 if (opt->ip6po_rthdr == NULL)
2899 newdest = &opt->ip6po_dest1;
2901 newdest = &opt->ip6po_dest2;
2903 case IPV6_RTHDRDSTOPTS:
2904 newdest = &opt->ip6po_dest1;
2907 newdest = &opt->ip6po_dest2;
2911 /* turn off the previous option, then set the new option. */
2912 ip6_clearpktopts(opt, optname);
2913 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2914 if (*newdest == NULL)
2916 bcopy(dest, *newdest, destlen);
2921 case IPV6_2292RTHDR:
2924 struct ip6_rthdr *rth;
2928 ip6_clearpktopts(opt, IPV6_RTHDR);
2929 break; /* just remove the option */
2932 /* message length validation */
2933 if (len < sizeof(struct ip6_rthdr))
2935 rth = (struct ip6_rthdr *)buf;
2936 rthlen = (rth->ip6r_len + 1) << 3;
2940 switch (rth->ip6r_type) {
2941 case IPV6_RTHDR_TYPE_0:
2942 if (rth->ip6r_len == 0) /* must contain one addr */
2944 if (rth->ip6r_len % 2) /* length must be even */
2946 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2950 return (EINVAL); /* not supported */
2953 /* turn off the previous option */
2954 ip6_clearpktopts(opt, IPV6_RTHDR);
2955 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2956 if (opt->ip6po_rthdr == NULL)
2958 bcopy(rth, opt->ip6po_rthdr, rthlen);
2963 case IPV6_USE_MIN_MTU:
2964 if (len != sizeof(int))
2966 minmtupolicy = *(int *)buf;
2967 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2968 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2969 minmtupolicy != IP6PO_MINMTU_ALL) {
2972 opt->ip6po_minmtu = minmtupolicy;
2976 if (len != sizeof(int))
2979 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2981 * we ignore this option for TCP sockets.
2982 * (RFC3542 leaves this case unspecified.)
2984 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2986 opt->ip6po_flags |= IP6PO_DONTFRAG;
2989 case IPV6_PREFER_TEMPADDR:
2990 if (len != sizeof(int))
2992 preftemp = *(int *)buf;
2993 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2994 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2995 preftemp != IP6PO_TEMPADDR_PREFER) {
2998 opt->ip6po_prefer_tempaddr = preftemp;
3002 return (ENOPROTOOPT);
3003 } /* end of switch */
3009 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3010 * packet to the input queue of a specified interface. Note that this
3011 * calls the output routine of the loopback "driver", but with an interface
3012 * pointer that might NOT be &loif -- easier than replicating that code here.
3015 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3018 struct ip6_hdr *ip6;
3020 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3025 * Make sure to deep-copy IPv6 header portion in case the data
3026 * is in an mbuf cluster, so that we can safely override the IPv6
3027 * header portion later.
3029 if (!M_WRITABLE(copym) ||
3030 copym->m_len < sizeof(struct ip6_hdr)) {
3031 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3035 ip6 = mtod(copym, struct ip6_hdr *);
3037 * clear embedded scope identifiers if necessary.
3038 * in6_clearscope will touch the addresses only when necessary.
3040 in6_clearscope(&ip6->ip6_src);
3041 in6_clearscope(&ip6->ip6_dst);
3042 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3043 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3045 copym->m_pkthdr.csum_data = 0xffff;
3047 if_simloop(ifp, copym, AF_INET6, 0);
3051 * Chop IPv6 header off from the payload.
3054 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3057 struct ip6_hdr *ip6;
3059 ip6 = mtod(m, struct ip6_hdr *);
3060 if (m->m_len > sizeof(*ip6)) {
3061 mh = m_gethdr(M_NOWAIT, MT_DATA);
3066 m_move_pkthdr(mh, m);
3067 M_ALIGN(mh, sizeof(*ip6));
3068 m->m_len -= sizeof(*ip6);
3069 m->m_data += sizeof(*ip6);
3072 m->m_len = sizeof(*ip6);
3073 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3075 exthdrs->ip6e_ip6 = m;
3080 * Compute IPv6 extension header length.
3083 ip6_optlen(struct inpcb *in6p)
3087 if (!in6p->in6p_outputopts)
3092 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3094 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3095 if (in6p->in6p_outputopts->ip6po_rthdr)
3096 /* dest1 is valid with rthdr only */
3097 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3098 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3099 len += elen(in6p->in6p_outputopts->ip6po_dest2);