2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $
33 * Copyright (c) 1982, 1986, 1988, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
67 #include "opt_inet6.h"
68 #include "opt_ipsec.h"
70 #include "opt_route.h"
73 #include <sys/param.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.h>
77 #include <sys/errno.h>
80 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/syslog.h>
84 #include <sys/ucred.h>
86 #include <machine/in_cksum.h>
89 #include <net/if_var.h>
90 #include <net/if_llatbl.h>
91 #include <net/netisr.h>
92 #include <net/route.h>
94 #include <net/rss_config.h>
97 #include <netinet/in.h>
98 #include <netinet/in_var.h>
99 #include <netinet/ip_var.h>
100 #include <netinet6/in6_fib.h>
101 #include <netinet6/in6_var.h>
102 #include <netinet/ip6.h>
103 #include <netinet/icmp6.h>
104 #include <netinet6/ip6_var.h>
105 #include <netinet/in_pcb.h>
106 #include <netinet/tcp_var.h>
107 #include <netinet6/nd6.h>
108 #include <netinet6/in6_rss.h>
110 #include <netipsec/ipsec_support.h>
112 #include <netinet/sctp.h>
113 #include <netinet/sctp_crc32.h>
116 #include <netinet6/ip6protosw.h>
117 #include <netinet6/scope6_var.h>
120 #include <net/flowtable.h>
123 extern int in6_mcast_loop;
126 struct mbuf *ip6e_ip6;
127 struct mbuf *ip6e_hbh;
128 struct mbuf *ip6e_dest1;
129 struct mbuf *ip6e_rthdr;
130 struct mbuf *ip6e_dest2;
133 static MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
135 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
136 struct ucred *, int);
137 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
138 struct socket *, struct sockopt *);
139 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
140 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
141 struct ucred *, int, int, int);
143 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
144 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
146 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
147 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
148 static int ip6_getpmtu(struct route_in6 *, int,
149 struct ifnet *, const struct in6_addr *, u_long *, int *, u_int,
151 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
152 u_long *, int *, u_int);
153 static int ip6_getpmtu_ctl(u_int, const struct in6_addr *, u_long *);
154 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
158 * Make an extension header from option data. hp is the source, and
159 * mp is the destination.
161 #define MAKE_EXTHDR(hp, mp) \
164 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
165 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
166 ((eh)->ip6e_len + 1) << 3); \
170 } while (/*CONSTCOND*/ 0)
173 * Form a chain of extension headers.
174 * m is the extension header mbuf
175 * mp is the previous mbuf in the chain
176 * p is the next header
177 * i is the type of option.
179 #define MAKE_CHAIN(m, mp, p, i)\
183 panic("assumption failed: hdr not split"); \
184 *mtod((m), u_char *) = *(p);\
186 p = mtod((m), u_char *);\
187 (m)->m_next = (mp)->m_next;\
191 } while (/*CONSTCOND*/ 0)
194 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
198 csum = in_cksum_skip(m, offset + plen, offset);
199 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
201 offset += m->m_pkthdr.csum_data; /* checksum offset */
203 if (offset + sizeof(csum) > m->m_len)
204 m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
206 *(u_short *)mtodo(m, offset) = csum;
210 ip6_output_delayed_csum(struct mbuf *m, struct ifnet *ifp, int csum_flags,
211 int plen, int optlen, bool frag __unused)
214 KASSERT((plen >= optlen), ("%s:%d: plen %d < optlen %d, m %p, ifp %p "
215 "csum_flags %#x frag %d\n",
216 __func__, __LINE__, plen, optlen, m, ifp, csum_flags, frag));
218 if ((csum_flags & CSUM_DELAY_DATA_IPV6) ||
220 (csum_flags & CSUM_SCTP_IPV6) ||
223 if (csum_flags & CSUM_DELAY_DATA_IPV6) {
224 in6_delayed_cksum(m, plen - optlen,
225 sizeof(struct ip6_hdr) + optlen);
226 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
229 if (csum_flags & CSUM_SCTP_IPV6) {
230 sctp_delayed_cksum(m, sizeof(struct ip6_hdr) + optlen);
231 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
240 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
241 int mtu, uint32_t id)
243 struct mbuf *m, **mnext, *m_frgpart;
244 struct ip6_hdr *ip6, *mhip6;
245 struct ip6_frag *ip6f;
248 int tlen = m0->m_pkthdr.len;
250 KASSERT(( mtu % 8 == 0), ("Fragment length must be a multiple of 8"));
253 ip6 = mtod(m, struct ip6_hdr *);
254 mnext = &m->m_nextpkt;
256 for (off = hlen; off < tlen; off += mtu) {
257 m = m_gethdr(M_NOWAIT, MT_DATA);
259 IP6STAT_INC(ip6s_odropped);
262 m->m_flags = m0->m_flags & M_COPYFLAGS;
264 mnext = &m->m_nextpkt;
265 m->m_data += max_linkhdr;
266 mhip6 = mtod(m, struct ip6_hdr *);
268 m->m_len = sizeof(*mhip6);
269 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
271 IP6STAT_INC(ip6s_odropped);
274 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
275 if (off + mtu >= tlen)
278 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
279 mhip6->ip6_plen = htons((u_short)(mtu + hlen +
280 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
281 if ((m_frgpart = m_copy(m0, off, mtu)) == NULL) {
282 IP6STAT_INC(ip6s_odropped);
286 m->m_pkthdr.len = mtu + hlen + sizeof(*ip6f);
287 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
288 m->m_pkthdr.rcvif = NULL;
289 ip6f->ip6f_reserved = 0;
290 ip6f->ip6f_ident = id;
291 ip6f->ip6f_nxt = nextproto;
292 IP6STAT_INC(ip6s_ofragments);
293 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
300 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
301 * header (with pri, len, nxt, hlim, src, dst).
302 * This function may modify ver and hlim only.
303 * The mbuf chain containing the packet will be freed.
304 * The mbuf opt, if present, will not be freed.
305 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
306 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
307 * then result of route lookup is stored in ro->ro_rt.
309 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
310 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
313 * ifpp - XXX: just for statistics
316 * XXX TODO: no flowid is assigned for outbound flows?
319 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
320 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
321 struct ifnet **ifpp, struct inpcb *inp)
324 struct ifnet *ifp, *origifp;
326 struct mbuf *mprev = NULL;
328 struct route_in6 ip6route;
329 struct rtentry *rt = NULL;
330 struct sockaddr_in6 *dst, src_sa, dst_sa;
331 struct in6_addr odst;
333 struct in6_ifaddr *ia = NULL;
335 int alwaysfrag, dontfrag;
336 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
337 struct ip6_exthdrs exthdrs;
338 struct in6_addr src0, dst0;
340 struct route_in6 *ro_pmtu = NULL;
345 struct m_tag *fwd_tag = NULL;
349 INP_LOCK_ASSERT(inp);
350 M_SETFIB(m, inp->inp_inc.inc_fibnum);
351 if ((flags & IP_NODEFAULTFLOWID) == 0) {
352 /* unconditionally set flowid */
353 m->m_pkthdr.flowid = inp->inp_flowid;
354 M_HASHTYPE_SET(m, inp->inp_flowtype);
358 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
360 * IPSec checking which handles several cases.
361 * FAST IPSEC: We re-injected the packet.
362 * XXX: need scope argument.
364 if (IPSEC_ENABLED(ipv6)) {
365 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
366 if (error == EINPROGRESS)
373 bzero(&exthdrs, sizeof(exthdrs));
375 /* Hop-by-Hop options header */
376 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
377 /* Destination options header(1st part) */
378 if (opt->ip6po_rthdr) {
380 * Destination options header(1st part)
381 * This only makes sense with a routing header.
382 * See Section 9.2 of RFC 3542.
383 * Disabling this part just for MIP6 convenience is
384 * a bad idea. We need to think carefully about a
385 * way to make the advanced API coexist with MIP6
386 * options, which might automatically be inserted in
389 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
392 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
393 /* Destination options header(2nd part) */
394 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
398 * Calculate the total length of the extension header chain.
399 * Keep the length of the unfragmentable part for fragmentation.
402 if (exthdrs.ip6e_hbh)
403 optlen += exthdrs.ip6e_hbh->m_len;
404 if (exthdrs.ip6e_dest1)
405 optlen += exthdrs.ip6e_dest1->m_len;
406 if (exthdrs.ip6e_rthdr)
407 optlen += exthdrs.ip6e_rthdr->m_len;
408 unfragpartlen = optlen + sizeof(struct ip6_hdr);
410 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
411 if (exthdrs.ip6e_dest2)
412 optlen += exthdrs.ip6e_dest2->m_len;
415 * If there is at least one extension header,
416 * separate IP6 header from the payload.
418 if (optlen && !hdrsplit) {
419 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
423 m = exthdrs.ip6e_ip6;
427 ip6 = mtod(m, struct ip6_hdr *);
429 /* adjust mbuf packet header length */
430 m->m_pkthdr.len += optlen;
431 plen = m->m_pkthdr.len - sizeof(*ip6);
433 /* If this is a jumbo payload, insert a jumbo payload option. */
434 if (plen > IPV6_MAXPACKET) {
436 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
440 m = exthdrs.ip6e_ip6;
444 ip6 = mtod(m, struct ip6_hdr *);
445 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
449 ip6->ip6_plen = htons(plen);
452 * Concatenate headers and fill in next header fields.
453 * Here we have, on "m"
455 * and we insert headers accordingly. Finally, we should be getting:
456 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
458 * during the header composing process, "m" points to IPv6 header.
459 * "mprev" points to an extension header prior to esp.
461 u_char *nexthdrp = &ip6->ip6_nxt;
465 * we treat dest2 specially. this makes IPsec processing
466 * much easier. the goal here is to make mprev point the
467 * mbuf prior to dest2.
469 * result: IPv6 dest2 payload
470 * m and mprev will point to IPv6 header.
472 if (exthdrs.ip6e_dest2) {
474 panic("assumption failed: hdr not split");
475 exthdrs.ip6e_dest2->m_next = m->m_next;
476 m->m_next = exthdrs.ip6e_dest2;
477 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
478 ip6->ip6_nxt = IPPROTO_DSTOPTS;
482 * result: IPv6 hbh dest1 rthdr dest2 payload
483 * m will point to IPv6 header. mprev will point to the
484 * extension header prior to dest2 (rthdr in the above case).
486 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
487 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
489 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
493 * If there is a routing header, discard the packet.
495 if (exthdrs.ip6e_rthdr) {
500 /* Source address validation */
501 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
502 (flags & IPV6_UNSPECSRC) == 0) {
504 IP6STAT_INC(ip6s_badscope);
507 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
509 IP6STAT_INC(ip6s_badscope);
513 IP6STAT_INC(ip6s_localout);
520 bzero((caddr_t)ro, sizeof(*ro));
523 if (opt && opt->ip6po_rthdr)
524 ro = &opt->ip6po_route;
525 dst = (struct sockaddr_in6 *)&ro->ro_dst;
527 if (ro->ro_rt == NULL)
528 (void )flowtable_lookup(AF_INET6, m, (struct route *)ro);
530 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
533 * if specified, try to fill in the traffic class field.
534 * do not override if a non-zero value is already set.
535 * we check the diffserv field and the ecn field separately.
537 if (opt && opt->ip6po_tclass >= 0) {
540 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
542 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
545 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
548 /* fill in or override the hop limit field, if necessary. */
549 if (opt && opt->ip6po_hlim != -1)
550 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
551 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
553 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
555 ip6->ip6_hlim = V_ip6_defmcasthlim;
558 * Validate route against routing table additions;
559 * a better/more specific route might have been added.
560 * Make sure address family is set in route.
563 ro->ro_dst.sin6_family = AF_INET6;
564 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
566 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
567 ro->ro_dst.sin6_family == AF_INET6 &&
568 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
570 ifp = ro->ro_rt->rt_ifp;
573 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
575 if (fwd_tag == NULL) {
576 bzero(&dst_sa, sizeof(dst_sa));
577 dst_sa.sin6_family = AF_INET6;
578 dst_sa.sin6_len = sizeof(dst_sa);
579 dst_sa.sin6_addr = ip6->ip6_dst;
581 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
585 in6_ifstat_inc(ifp, ifs6_out_discard);
591 * If in6_selectroute() does not return a route entry,
592 * dst may not have been updated.
594 *dst = dst_sa; /* XXX */
598 * then rt (for unicast) and ifp must be non-NULL valid values.
600 if ((flags & IPV6_FORWARDING) == 0) {
601 /* XXX: the FORWARDING flag can be set for mrouting. */
602 in6_ifstat_inc(ifp, ifs6_out_request);
605 ia = (struct in6_ifaddr *)(rt->rt_ifa);
606 counter_u64_add(rt->rt_pksent, 1);
609 /* Setup data structures for scope ID checks. */
611 bzero(&src_sa, sizeof(src_sa));
612 src_sa.sin6_family = AF_INET6;
613 src_sa.sin6_len = sizeof(src_sa);
614 src_sa.sin6_addr = ip6->ip6_src;
617 /* re-initialize to be sure */
618 bzero(&dst_sa, sizeof(dst_sa));
619 dst_sa.sin6_family = AF_INET6;
620 dst_sa.sin6_len = sizeof(dst_sa);
621 dst_sa.sin6_addr = ip6->ip6_dst;
623 /* Check for valid scope ID. */
624 if (in6_setscope(&src0, ifp, &zone) == 0 &&
625 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
626 in6_setscope(&dst0, ifp, &zone) == 0 &&
627 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
629 * The outgoing interface is in the zone of the source
630 * and destination addresses.
632 * Because the loopback interface cannot receive
633 * packets with a different scope ID than its own,
634 * there is a trick is to pretend the outgoing packet
635 * was received by the real network interface, by
636 * setting "origifp" different from "ifp". This is
637 * only allowed when "ifp" is a loopback network
638 * interface. Refer to code in nd6_output_ifp() for
644 * We should use ia_ifp to support the case of sending
645 * packets to an address of our own.
647 if (ia != NULL && ia->ia_ifp)
650 } else if ((ifp->if_flags & IFF_LOOPBACK) == 0 ||
651 sa6_recoverscope(&src_sa) != 0 ||
652 sa6_recoverscope(&dst_sa) != 0 ||
653 dst_sa.sin6_scope_id == 0 ||
654 (src_sa.sin6_scope_id != 0 &&
655 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
656 (origifp = ifnet_byindex(dst_sa.sin6_scope_id)) == NULL) {
658 * If the destination network interface is not a
659 * loopback interface, or the destination network
660 * address has no scope ID, or the source address has
661 * a scope ID set which is different from the
662 * destination address one, or there is no network
663 * interface representing this scope ID, the address
664 * pair is considered invalid.
666 IP6STAT_INC(ip6s_badscope);
667 in6_ifstat_inc(ifp, ifs6_out_discard);
669 error = EHOSTUNREACH; /* XXX */
673 /* All scope ID checks are successful. */
675 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
676 if (opt && opt->ip6po_nextroute.ro_rt) {
678 * The nexthop is explicitly specified by the
679 * application. We assume the next hop is an IPv6
682 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
684 else if ((rt->rt_flags & RTF_GATEWAY))
685 dst = (struct sockaddr_in6 *)rt->rt_gateway;
688 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
689 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
691 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
692 in6_ifstat_inc(ifp, ifs6_out_mcast);
694 * Confirm that the outgoing interface supports multicast.
696 if (!(ifp->if_flags & IFF_MULTICAST)) {
697 IP6STAT_INC(ip6s_noroute);
698 in6_ifstat_inc(ifp, ifs6_out_discard);
702 if ((im6o == NULL && in6_mcast_loop) ||
703 (im6o && im6o->im6o_multicast_loop)) {
705 * Loop back multicast datagram if not expressly
706 * forbidden to do so, even if we have not joined
707 * the address; protocols will filter it later,
708 * thus deferring a hash lookup and lock acquisition
709 * at the expense of an m_copym().
711 ip6_mloopback(ifp, m);
714 * If we are acting as a multicast router, perform
715 * multicast forwarding as if the packet had just
716 * arrived on the interface to which we are about
717 * to send. The multicast forwarding function
718 * recursively calls this function, using the
719 * IPV6_FORWARDING flag to prevent infinite recursion.
721 * Multicasts that are looped back by ip6_mloopback(),
722 * above, will be forwarded by the ip6_input() routine,
725 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
727 * XXX: ip6_mforward expects that rcvif is NULL
728 * when it is called from the originating path.
729 * However, it may not always be the case.
731 m->m_pkthdr.rcvif = NULL;
732 if (ip6_mforward(ip6, ifp, m) != 0) {
739 * Multicasts with a hoplimit of zero may be looped back,
740 * above, but must not be transmitted on a network.
741 * Also, multicasts addressed to the loopback interface
742 * are not sent -- the above call to ip6_mloopback() will
743 * loop back a copy if this host actually belongs to the
744 * destination group on the loopback interface.
746 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
747 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
754 * Fill the outgoing inteface to tell the upper layer
755 * to increment per-interface statistics.
760 /* Determine path MTU. */
761 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
762 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
766 * The caller of this function may specify to use the minimum MTU
768 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
769 * setting. The logic is a bit complicated; by default, unicast
770 * packets will follow path MTU while multicast packets will be sent at
771 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
772 * including unicast ones will be sent at the minimum MTU. Multicast
773 * packets will always be sent at the minimum MTU unless
774 * IP6PO_MINMTU_DISABLE is explicitly specified.
775 * See RFC 3542 for more details.
777 if (mtu > IPV6_MMTU) {
778 if ((flags & IPV6_MINMTU))
780 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
782 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
784 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
790 * clear embedded scope identifiers if necessary.
791 * in6_clearscope will touch the addresses only when necessary.
793 in6_clearscope(&ip6->ip6_src);
794 in6_clearscope(&ip6->ip6_dst);
797 * If the outgoing packet contains a hop-by-hop options header,
798 * it must be examined and processed even by the source node.
799 * (RFC 2460, section 4.)
801 if (exthdrs.ip6e_hbh) {
802 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
803 u_int32_t dummy; /* XXX unused */
804 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
807 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
808 panic("ip6e_hbh is not contiguous");
811 * XXX: if we have to send an ICMPv6 error to the sender,
812 * we need the M_LOOP flag since icmp6_error() expects
813 * the IPv6 and the hop-by-hop options header are
814 * contiguous unless the flag is set.
816 m->m_flags |= M_LOOP;
817 m->m_pkthdr.rcvif = ifp;
818 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
819 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
820 &dummy, &plen) < 0) {
821 /* m was already freed at this point */
822 error = EINVAL;/* better error? */
825 m->m_flags &= ~M_LOOP; /* XXX */
826 m->m_pkthdr.rcvif = NULL;
829 /* Jump over all PFIL processing if hooks are not active. */
830 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
834 /* Run through list of hooks for output packets. */
835 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, 0, inp);
836 if (error != 0 || m == NULL)
839 ip6 = mtod(m, struct ip6_hdr *);
842 /* See if destination IP address was changed by packet filter. */
843 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
844 m->m_flags |= M_SKIP_FIREWALL;
845 /* If destination is now ourself drop to ip6_input(). */
846 if (in6_localip(&ip6->ip6_dst)) {
847 m->m_flags |= M_FASTFWD_OURS;
848 if (m->m_pkthdr.rcvif == NULL)
849 m->m_pkthdr.rcvif = V_loif;
850 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
851 m->m_pkthdr.csum_flags |=
852 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
853 m->m_pkthdr.csum_data = 0xffff;
856 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
857 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
859 error = netisr_queue(NETISR_IPV6, m);
863 needfiblookup = 1; /* Redo the routing table lookup. */
865 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
869 /* See if fib was changed by packet filter. */
870 if (fibnum != M_GETFIB(m)) {
871 m->m_flags |= M_SKIP_FIREWALL;
872 fibnum = M_GETFIB(m);
876 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
882 /* See if local, if yes, send it to netisr. */
883 if (m->m_flags & M_FASTFWD_OURS) {
884 if (m->m_pkthdr.rcvif == NULL)
885 m->m_pkthdr.rcvif = V_loif;
886 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
887 m->m_pkthdr.csum_flags |=
888 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
889 m->m_pkthdr.csum_data = 0xffff;
892 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
893 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
895 error = netisr_queue(NETISR_IPV6, m);
898 /* Or forward to some other address? */
899 if ((m->m_flags & M_IP6_NEXTHOP) &&
900 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
901 dst = (struct sockaddr_in6 *)&ro->ro_dst;
902 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
903 m->m_flags |= M_SKIP_FIREWALL;
904 m->m_flags &= ~M_IP6_NEXTHOP;
905 m_tag_delete(m, fwd_tag);
911 * Send the packet to the outgoing interface.
912 * If necessary, do IPv6 fragmentation before sending.
914 * the logic here is rather complex:
915 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
916 * 1-a: send as is if tlen <= path mtu
917 * 1-b: fragment if tlen > path mtu
919 * 2: if user asks us not to fragment (dontfrag == 1)
920 * 2-a: send as is if tlen <= interface mtu
921 * 2-b: error if tlen > interface mtu
923 * 3: if we always need to attach fragment header (alwaysfrag == 1)
926 * 4: if dontfrag == 1 && alwaysfrag == 1
927 * error, as we cannot handle this conflicting request
929 sw_csum = m->m_pkthdr.csum_flags;
931 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
932 sw_csum &= ~ifp->if_hwassist;
936 * If we added extension headers, we will not do TSO and calculate the
937 * checksums ourselves for now.
938 * XXX-BZ Need a framework to know when the NIC can handle it, even
941 error = ip6_output_delayed_csum(m, ifp, sw_csum, plen, optlen, false);
944 /* XXX-BZ m->m_pkthdr.csum_flags &= ~ifp->if_hwassist; */
945 tlen = m->m_pkthdr.len;
947 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
951 if (dontfrag && alwaysfrag) { /* case 4 */
952 /* conflicting request - can't transmit */
956 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
958 * Even if the DONTFRAG option is specified, we cannot send the
959 * packet when the data length is larger than the MTU of the
960 * outgoing interface.
961 * Notify the error by sending IPV6_PATHMTU ancillary data if
962 * application wanted to know the MTU value. Also return an
963 * error code (this is not described in the API spec).
966 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
972 * transmit packet without fragmentation
974 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
975 struct in6_ifaddr *ia6;
977 ip6 = mtod(m, struct ip6_hdr *);
978 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
980 /* Record statistics for this interface address. */
981 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
982 counter_u64_add(ia6->ia_ifa.ifa_obytes,
984 ifa_free(&ia6->ia_ifa);
986 error = nd6_output_ifp(ifp, origifp, m, dst,
992 * try to fragment the packet. case 1-b and 3
994 if (mtu < IPV6_MMTU) {
995 /* path MTU cannot be less than IPV6_MMTU */
997 in6_ifstat_inc(ifp, ifs6_out_fragfail);
999 } else if (ip6->ip6_plen == 0) {
1000 /* jumbo payload cannot be fragmented */
1002 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1008 * Too large for the destination or interface;
1009 * fragment if possible.
1010 * Must be able to put at least 8 bytes per fragment.
1012 hlen = unfragpartlen;
1013 if (mtu > IPV6_MAXPACKET)
1014 mtu = IPV6_MAXPACKET;
1016 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1019 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1024 * If the interface will not calculate checksums on
1025 * fragmented packets, then do it here.
1026 * XXX-BZ handle the hw offloading case. Need flags.
1028 error = ip6_output_delayed_csum(m, ifp, m->m_pkthdr.csum_flags,
1029 plen, optlen, true);
1034 * Change the next header field of the last header in the
1035 * unfragmentable part.
1037 if (exthdrs.ip6e_rthdr) {
1038 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1039 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1040 } else if (exthdrs.ip6e_dest1) {
1041 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1042 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1043 } else if (exthdrs.ip6e_hbh) {
1044 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1045 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1047 nextproto = ip6->ip6_nxt;
1048 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1052 * Loop through length of segment after first fragment,
1053 * make new header and copy data of each part and link onto
1057 id = htonl(ip6_randomid());
1058 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1061 in6_ifstat_inc(ifp, ifs6_out_fragok);
1065 * Remove leading garbages.
1075 /* Record statistics for this interface address. */
1077 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1078 counter_u64_add(ia->ia_ifa.ifa_obytes,
1081 error = nd6_output_ifp(ifp, origifp, m, dst,
1082 (struct route *)ro);
1088 IP6STAT_INC(ip6s_fragmented);
1092 * Release the route if using our private route, or if
1093 * (with flowtable) we don't have our own reference.
1095 if (ro == &ip6route ||
1096 (ro != NULL && ro->ro_flags & RT_NORTREF))
1101 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1102 m_freem(exthdrs.ip6e_dest1);
1103 m_freem(exthdrs.ip6e_rthdr);
1104 m_freem(exthdrs.ip6e_dest2);
1113 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1117 if (hlen > MCLBYTES)
1118 return (ENOBUFS); /* XXX */
1121 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1123 m = m_get(M_NOWAIT, MT_DATA);
1128 bcopy(hdr, mtod(m, caddr_t), hlen);
1135 * Insert jumbo payload option.
1138 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1144 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1147 * If there is no hop-by-hop options header, allocate new one.
1148 * If there is one but it doesn't have enough space to store the
1149 * jumbo payload option, allocate a cluster to store the whole options.
1150 * Otherwise, use it to store the options.
1152 if (exthdrs->ip6e_hbh == NULL) {
1153 mopt = m_get(M_NOWAIT, MT_DATA);
1156 mopt->m_len = JUMBOOPTLEN;
1157 optbuf = mtod(mopt, u_char *);
1158 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1159 exthdrs->ip6e_hbh = mopt;
1161 struct ip6_hbh *hbh;
1163 mopt = exthdrs->ip6e_hbh;
1164 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1167 * - exthdrs->ip6e_hbh is not referenced from places
1168 * other than exthdrs.
1169 * - exthdrs->ip6e_hbh is not an mbuf chain.
1171 int oldoptlen = mopt->m_len;
1175 * XXX: give up if the whole (new) hbh header does
1176 * not fit even in an mbuf cluster.
1178 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1182 * As a consequence, we must always prepare a cluster
1185 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1188 n->m_len = oldoptlen + JUMBOOPTLEN;
1189 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1191 optbuf = mtod(n, caddr_t) + oldoptlen;
1193 mopt = exthdrs->ip6e_hbh = n;
1195 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1196 mopt->m_len += JUMBOOPTLEN;
1198 optbuf[0] = IP6OPT_PADN;
1202 * Adjust the header length according to the pad and
1203 * the jumbo payload option.
1205 hbh = mtod(mopt, struct ip6_hbh *);
1206 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1209 /* fill in the option. */
1210 optbuf[2] = IP6OPT_JUMBO;
1212 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1213 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1215 /* finally, adjust the packet header length */
1216 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1223 * Insert fragment header and copy unfragmentable header portions.
1226 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1227 struct ip6_frag **frghdrp)
1229 struct mbuf *n, *mlast;
1231 if (hlen > sizeof(struct ip6_hdr)) {
1232 n = m_copym(m0, sizeof(struct ip6_hdr),
1233 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1240 /* Search for the last mbuf of unfragmentable part. */
1241 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1244 if (M_WRITABLE(mlast) &&
1245 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1246 /* use the trailing space of the last mbuf for the fragment hdr */
1247 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1249 mlast->m_len += sizeof(struct ip6_frag);
1250 m->m_pkthdr.len += sizeof(struct ip6_frag);
1252 /* allocate a new mbuf for the fragment header */
1255 mfrg = m_get(M_NOWAIT, MT_DATA);
1258 mfrg->m_len = sizeof(struct ip6_frag);
1259 *frghdrp = mtod(mfrg, struct ip6_frag *);
1260 mlast->m_next = mfrg;
1267 * Calculates IPv6 path mtu for destination @dst.
1268 * Resulting MTU is stored in @mtup.
1270 * Returns 0 on success.
1273 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1275 struct nhop6_extended nh6;
1276 struct in6_addr kdst;
1282 in6_splitscope(dst, &kdst, &scopeid);
1283 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1284 return (EHOSTUNREACH);
1289 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1290 fib6_free_nh_ext(fibnum, &nh6);
1296 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1297 * and cached data in @ro_pmtu.
1298 * MTU from (successful) route lookup is saved (along with dst)
1299 * inside @ro_pmtu to avoid subsequent route lookups after packet
1300 * filter processing.
1302 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1303 * Returns 0 on success.
1306 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1307 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1308 int *alwaysfragp, u_int fibnum, u_int proto)
1310 struct nhop6_basic nh6;
1311 struct in6_addr kdst;
1313 struct sockaddr_in6 *sa6_dst;
1320 * Here ro_pmtu has final destination address, while
1321 * ro might represent immediate destination.
1322 * Use ro_pmtu destination since mtu might differ.
1324 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1325 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1326 ro_pmtu->ro_mtu = 0;
1328 if (ro_pmtu->ro_mtu == 0) {
1329 bzero(sa6_dst, sizeof(*sa6_dst));
1330 sa6_dst->sin6_family = AF_INET6;
1331 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1332 sa6_dst->sin6_addr = *dst;
1334 in6_splitscope(dst, &kdst, &scopeid);
1335 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1337 ro_pmtu->ro_mtu = nh6.nh_mtu;
1340 mtu = ro_pmtu->ro_mtu;
1344 mtu = ro_pmtu->ro_rt->rt_mtu;
1346 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1350 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1351 * hostcache data for @dst.
1352 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1354 * Returns 0 on success.
1357 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1358 u_long *mtup, int *alwaysfragp, u_int proto)
1366 struct in_conninfo inc;
1368 bzero(&inc, sizeof(inc));
1369 inc.inc_flags |= INC_ISIPV6;
1370 inc.inc6_faddr = *dst;
1372 ifmtu = IN6_LINKMTU(ifp);
1374 /* TCP is known to react to pmtu changes so skip hc */
1375 if (proto != IPPROTO_TCP)
1376 mtu = tcp_hc_getmtu(&inc);
1379 mtu = min(mtu, rt_mtu);
1384 else if (mtu < IPV6_MMTU) {
1386 * RFC2460 section 5, last paragraph:
1387 * if we record ICMPv6 too big message with
1388 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1389 * or smaller, with framgent header attached.
1390 * (fragment header is needed regardless from the
1391 * packet size, for translators to identify packets)
1397 mtu = IN6_LINKMTU(ifp);
1399 error = EHOSTUNREACH; /* XXX */
1403 *alwaysfragp = alwaysfrag;
1408 * IP6 socket option processing.
1411 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1413 int optdatalen, uproto;
1415 struct inpcb *in6p = sotoinpcb(so);
1417 int level, op, optname;
1421 uint32_t rss_bucket;
1426 * Don't use more than a quarter of mbuf clusters. N.B.:
1427 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1428 * on LP64 architectures, so cast to u_long to avoid undefined
1429 * behavior. ILP32 architectures cannot have nmbclusters
1430 * large enough to overflow for other reasons.
1432 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1434 level = sopt->sopt_level;
1435 op = sopt->sopt_dir;
1436 optname = sopt->sopt_name;
1437 optlen = sopt->sopt_valsize;
1441 uproto = (int)so->so_proto->pr_protocol;
1443 if (level != IPPROTO_IPV6) {
1446 if (sopt->sopt_level == SOL_SOCKET &&
1447 sopt->sopt_dir == SOPT_SET) {
1448 switch (sopt->sopt_name) {
1451 if ((so->so_options & SO_REUSEADDR) != 0)
1452 in6p->inp_flags2 |= INP_REUSEADDR;
1454 in6p->inp_flags2 &= ~INP_REUSEADDR;
1460 if ((so->so_options & SO_REUSEPORT) != 0)
1461 in6p->inp_flags2 |= INP_REUSEPORT;
1463 in6p->inp_flags2 &= ~INP_REUSEPORT;
1469 in6p->inp_inc.inc_fibnum = so->so_fibnum;
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:
1545 case IPV6_BINDMULTI:
1547 case IPV6_RSS_LISTEN_BUCKET:
1549 if (optname == IPV6_BINDANY && td != NULL) {
1550 error = priv_check(td,
1551 PRIV_NETINET_BINDANY);
1556 if (optlen != sizeof(int)) {
1560 error = sooptcopyin(sopt, &optval,
1561 sizeof optval, sizeof optval);
1566 case IPV6_UNICAST_HOPS:
1567 if (optval < -1 || optval >= 256)
1570 /* -1 = kernel default */
1571 in6p->in6p_hops = optval;
1572 if ((in6p->inp_vflag &
1574 in6p->inp_ip_ttl = optval;
1577 #define OPTSET(bit) \
1581 in6p->inp_flags |= (bit); \
1583 in6p->inp_flags &= ~(bit); \
1584 INP_WUNLOCK(in6p); \
1585 } while (/*CONSTCOND*/ 0)
1586 #define OPTSET2292(bit) \
1589 in6p->inp_flags |= IN6P_RFC2292; \
1591 in6p->inp_flags |= (bit); \
1593 in6p->inp_flags &= ~(bit); \
1594 INP_WUNLOCK(in6p); \
1595 } while (/*CONSTCOND*/ 0)
1596 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1598 #define OPTSET2(bit, val) do { \
1601 in6p->inp_flags2 |= bit; \
1603 in6p->inp_flags2 &= ~bit; \
1604 INP_WUNLOCK(in6p); \
1606 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1608 case IPV6_RECVPKTINFO:
1609 /* cannot mix with RFC2292 */
1610 if (OPTBIT(IN6P_RFC2292)) {
1614 OPTSET(IN6P_PKTINFO);
1619 struct ip6_pktopts **optp;
1621 /* cannot mix with RFC2292 */
1622 if (OPTBIT(IN6P_RFC2292)) {
1626 optp = &in6p->in6p_outputopts;
1627 error = ip6_pcbopt(IPV6_HOPLIMIT,
1628 (u_char *)&optval, sizeof(optval),
1629 optp, (td != NULL) ? td->td_ucred :
1634 case IPV6_RECVHOPLIMIT:
1635 /* cannot mix with RFC2292 */
1636 if (OPTBIT(IN6P_RFC2292)) {
1640 OPTSET(IN6P_HOPLIMIT);
1643 case IPV6_RECVHOPOPTS:
1644 /* cannot mix with RFC2292 */
1645 if (OPTBIT(IN6P_RFC2292)) {
1649 OPTSET(IN6P_HOPOPTS);
1652 case IPV6_RECVDSTOPTS:
1653 /* cannot mix with RFC2292 */
1654 if (OPTBIT(IN6P_RFC2292)) {
1658 OPTSET(IN6P_DSTOPTS);
1661 case IPV6_RECVRTHDRDSTOPTS:
1662 /* cannot mix with RFC2292 */
1663 if (OPTBIT(IN6P_RFC2292)) {
1667 OPTSET(IN6P_RTHDRDSTOPTS);
1670 case IPV6_RECVRTHDR:
1671 /* cannot mix with RFC2292 */
1672 if (OPTBIT(IN6P_RFC2292)) {
1679 case IPV6_RECVPATHMTU:
1681 * We ignore this option for TCP
1683 * (RFC3542 leaves this case
1686 if (uproto != IPPROTO_TCP)
1690 case IPV6_RECVFLOWID:
1691 OPTSET2(INP_RECVFLOWID, optval);
1695 case IPV6_RECVRSSBUCKETID:
1696 OPTSET2(INP_RECVRSSBUCKETID, optval);
1702 * make setsockopt(IPV6_V6ONLY)
1703 * available only prior to bind(2).
1704 * see ipng mailing list, Jun 22 2001.
1706 if (in6p->inp_lport ||
1707 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1711 OPTSET(IN6P_IPV6_V6ONLY);
1713 in6p->inp_vflag &= ~INP_IPV4;
1715 in6p->inp_vflag |= INP_IPV4;
1717 case IPV6_RECVTCLASS:
1718 /* cannot mix with RFC2292 XXX */
1719 if (OPTBIT(IN6P_RFC2292)) {
1723 OPTSET(IN6P_TCLASS);
1725 case IPV6_AUTOFLOWLABEL:
1726 OPTSET(IN6P_AUTOFLOWLABEL);
1730 OPTSET(INP_BINDANY);
1733 case IPV6_BINDMULTI:
1734 OPTSET2(INP_BINDMULTI, optval);
1737 case IPV6_RSS_LISTEN_BUCKET:
1738 if ((optval >= 0) &&
1739 (optval < rss_getnumbuckets())) {
1740 in6p->inp_rss_listen_bucket = optval;
1741 OPTSET2(INP_RSS_BUCKET_SET, 1);
1752 case IPV6_USE_MIN_MTU:
1753 case IPV6_PREFER_TEMPADDR:
1754 if (optlen != sizeof(optval)) {
1758 error = sooptcopyin(sopt, &optval,
1759 sizeof optval, sizeof optval);
1763 struct ip6_pktopts **optp;
1764 optp = &in6p->in6p_outputopts;
1765 error = ip6_pcbopt(optname,
1766 (u_char *)&optval, sizeof(optval),
1767 optp, (td != NULL) ? td->td_ucred :
1772 case IPV6_2292PKTINFO:
1773 case IPV6_2292HOPLIMIT:
1774 case IPV6_2292HOPOPTS:
1775 case IPV6_2292DSTOPTS:
1776 case IPV6_2292RTHDR:
1778 if (optlen != sizeof(int)) {
1782 error = sooptcopyin(sopt, &optval,
1783 sizeof optval, sizeof optval);
1787 case IPV6_2292PKTINFO:
1788 OPTSET2292(IN6P_PKTINFO);
1790 case IPV6_2292HOPLIMIT:
1791 OPTSET2292(IN6P_HOPLIMIT);
1793 case IPV6_2292HOPOPTS:
1795 * Check super-user privilege.
1796 * See comments for IPV6_RECVHOPOPTS.
1799 error = priv_check(td,
1800 PRIV_NETINET_SETHDROPTS);
1804 OPTSET2292(IN6P_HOPOPTS);
1806 case IPV6_2292DSTOPTS:
1808 error = priv_check(td,
1809 PRIV_NETINET_SETHDROPTS);
1813 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1815 case IPV6_2292RTHDR:
1816 OPTSET2292(IN6P_RTHDR);
1824 case IPV6_RTHDRDSTOPTS:
1827 /* new advanced API (RFC3542) */
1829 u_char optbuf_storage[MCLBYTES];
1831 struct ip6_pktopts **optp;
1833 /* cannot mix with RFC2292 */
1834 if (OPTBIT(IN6P_RFC2292)) {
1840 * We only ensure valsize is not too large
1841 * here. Further validation will be done
1844 error = sooptcopyin(sopt, optbuf_storage,
1845 sizeof(optbuf_storage), 0);
1848 optlen = sopt->sopt_valsize;
1849 optbuf = optbuf_storage;
1850 optp = &in6p->in6p_outputopts;
1851 error = ip6_pcbopt(optname, optbuf, optlen,
1852 optp, (td != NULL) ? td->td_ucred : NULL,
1858 case IPV6_MULTICAST_IF:
1859 case IPV6_MULTICAST_HOPS:
1860 case IPV6_MULTICAST_LOOP:
1861 case IPV6_JOIN_GROUP:
1862 case IPV6_LEAVE_GROUP:
1864 case MCAST_BLOCK_SOURCE:
1865 case MCAST_UNBLOCK_SOURCE:
1866 case MCAST_JOIN_GROUP:
1867 case MCAST_LEAVE_GROUP:
1868 case MCAST_JOIN_SOURCE_GROUP:
1869 case MCAST_LEAVE_SOURCE_GROUP:
1870 error = ip6_setmoptions(in6p, sopt);
1873 case IPV6_PORTRANGE:
1874 error = sooptcopyin(sopt, &optval,
1875 sizeof optval, sizeof optval);
1881 case IPV6_PORTRANGE_DEFAULT:
1882 in6p->inp_flags &= ~(INP_LOWPORT);
1883 in6p->inp_flags &= ~(INP_HIGHPORT);
1886 case IPV6_PORTRANGE_HIGH:
1887 in6p->inp_flags &= ~(INP_LOWPORT);
1888 in6p->inp_flags |= INP_HIGHPORT;
1891 case IPV6_PORTRANGE_LOW:
1892 in6p->inp_flags &= ~(INP_HIGHPORT);
1893 in6p->inp_flags |= INP_LOWPORT;
1903 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1904 case IPV6_IPSEC_POLICY:
1905 if (IPSEC_ENABLED(ipv6)) {
1906 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
1913 error = ENOPROTOOPT;
1921 case IPV6_2292PKTOPTIONS:
1922 #ifdef IPV6_PKTOPTIONS
1923 case IPV6_PKTOPTIONS:
1926 * RFC3542 (effectively) deprecated the
1927 * semantics of the 2292-style pktoptions.
1928 * Since it was not reliable in nature (i.e.,
1929 * applications had to expect the lack of some
1930 * information after all), it would make sense
1931 * to simplify this part by always returning
1934 sopt->sopt_valsize = 0;
1937 case IPV6_RECVHOPOPTS:
1938 case IPV6_RECVDSTOPTS:
1939 case IPV6_RECVRTHDRDSTOPTS:
1940 case IPV6_UNICAST_HOPS:
1941 case IPV6_RECVPKTINFO:
1942 case IPV6_RECVHOPLIMIT:
1943 case IPV6_RECVRTHDR:
1944 case IPV6_RECVPATHMTU:
1947 case IPV6_PORTRANGE:
1948 case IPV6_RECVTCLASS:
1949 case IPV6_AUTOFLOWLABEL:
1953 case IPV6_RECVFLOWID:
1955 case IPV6_RSSBUCKETID:
1956 case IPV6_RECVRSSBUCKETID:
1958 case IPV6_BINDMULTI:
1961 case IPV6_RECVHOPOPTS:
1962 optval = OPTBIT(IN6P_HOPOPTS);
1965 case IPV6_RECVDSTOPTS:
1966 optval = OPTBIT(IN6P_DSTOPTS);
1969 case IPV6_RECVRTHDRDSTOPTS:
1970 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1973 case IPV6_UNICAST_HOPS:
1974 optval = in6p->in6p_hops;
1977 case IPV6_RECVPKTINFO:
1978 optval = OPTBIT(IN6P_PKTINFO);
1981 case IPV6_RECVHOPLIMIT:
1982 optval = OPTBIT(IN6P_HOPLIMIT);
1985 case IPV6_RECVRTHDR:
1986 optval = OPTBIT(IN6P_RTHDR);
1989 case IPV6_RECVPATHMTU:
1990 optval = OPTBIT(IN6P_MTU);
1994 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1997 case IPV6_PORTRANGE:
2000 flags = in6p->inp_flags;
2001 if (flags & INP_HIGHPORT)
2002 optval = IPV6_PORTRANGE_HIGH;
2003 else if (flags & INP_LOWPORT)
2004 optval = IPV6_PORTRANGE_LOW;
2009 case IPV6_RECVTCLASS:
2010 optval = OPTBIT(IN6P_TCLASS);
2013 case IPV6_AUTOFLOWLABEL:
2014 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2018 optval = OPTBIT(INP_BINDANY);
2022 optval = in6p->inp_flowid;
2026 optval = in6p->inp_flowtype;
2029 case IPV6_RECVFLOWID:
2030 optval = OPTBIT2(INP_RECVFLOWID);
2033 case IPV6_RSSBUCKETID:
2035 rss_hash2bucket(in6p->inp_flowid,
2039 optval = rss_bucket;
2044 case IPV6_RECVRSSBUCKETID:
2045 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2049 case IPV6_BINDMULTI:
2050 optval = OPTBIT2(INP_BINDMULTI);
2056 error = sooptcopyout(sopt, &optval,
2063 struct ip6_mtuinfo mtuinfo;
2065 if (!(so->so_state & SS_ISCONNECTED))
2068 * XXX: we dot not consider the case of source
2069 * routing, or optional information to specify
2070 * the outgoing interface.
2072 error = ip6_getpmtu_ctl(so->so_fibnum,
2073 &in6p->in6p_faddr, &pmtu);
2076 if (pmtu > IPV6_MAXPACKET)
2077 pmtu = IPV6_MAXPACKET;
2079 bzero(&mtuinfo, sizeof(mtuinfo));
2080 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2081 optdata = (void *)&mtuinfo;
2082 optdatalen = sizeof(mtuinfo);
2083 error = sooptcopyout(sopt, optdata,
2088 case IPV6_2292PKTINFO:
2089 case IPV6_2292HOPLIMIT:
2090 case IPV6_2292HOPOPTS:
2091 case IPV6_2292RTHDR:
2092 case IPV6_2292DSTOPTS:
2094 case IPV6_2292PKTINFO:
2095 optval = OPTBIT(IN6P_PKTINFO);
2097 case IPV6_2292HOPLIMIT:
2098 optval = OPTBIT(IN6P_HOPLIMIT);
2100 case IPV6_2292HOPOPTS:
2101 optval = OPTBIT(IN6P_HOPOPTS);
2103 case IPV6_2292RTHDR:
2104 optval = OPTBIT(IN6P_RTHDR);
2106 case IPV6_2292DSTOPTS:
2107 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2110 error = sooptcopyout(sopt, &optval,
2117 case IPV6_RTHDRDSTOPTS:
2121 case IPV6_USE_MIN_MTU:
2122 case IPV6_PREFER_TEMPADDR:
2123 error = ip6_getpcbopt(in6p->in6p_outputopts,
2127 case IPV6_MULTICAST_IF:
2128 case IPV6_MULTICAST_HOPS:
2129 case IPV6_MULTICAST_LOOP:
2131 error = ip6_getmoptions(in6p, sopt);
2134 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2135 case IPV6_IPSEC_POLICY:
2136 if (IPSEC_ENABLED(ipv6)) {
2137 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
2143 error = ENOPROTOOPT;
2153 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2155 int error = 0, optval, optlen;
2156 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2157 struct inpcb *in6p = sotoinpcb(so);
2158 int level, op, optname;
2160 level = sopt->sopt_level;
2161 op = sopt->sopt_dir;
2162 optname = sopt->sopt_name;
2163 optlen = sopt->sopt_valsize;
2165 if (level != IPPROTO_IPV6) {
2172 * For ICMPv6 sockets, no modification allowed for checksum
2173 * offset, permit "no change" values to help existing apps.
2175 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2176 * for an ICMPv6 socket will fail."
2177 * The current behavior does not meet RFC3542.
2181 if (optlen != sizeof(int)) {
2185 error = sooptcopyin(sopt, &optval, sizeof(optval),
2189 if (optval < -1 || (optval % 2) != 0) {
2191 * The API assumes non-negative even offset
2192 * values or -1 as a special value.
2195 } else if (so->so_proto->pr_protocol ==
2197 if (optval != icmp6off)
2200 in6p->in6p_cksum = optval;
2204 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2207 optval = in6p->in6p_cksum;
2209 error = sooptcopyout(sopt, &optval, sizeof(optval));
2219 error = ENOPROTOOPT;
2227 * Set up IP6 options in pcb for insertion in output packets or
2228 * specifying behavior of outgoing packets.
2231 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2232 struct socket *so, struct sockopt *sopt)
2234 struct ip6_pktopts *opt = *pktopt;
2236 struct thread *td = sopt->sopt_td;
2238 /* turn off any old options. */
2241 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2242 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2243 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2244 printf("ip6_pcbopts: all specified options are cleared.\n");
2246 ip6_clearpktopts(opt, -1);
2248 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2251 if (!m || m->m_len == 0) {
2253 * Only turning off any previous options, regardless of
2254 * whether the opt is just created or given.
2256 free(opt, M_IP6OPT);
2260 /* set options specified by user. */
2261 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2262 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2263 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2264 free(opt, M_IP6OPT);
2272 * initialize ip6_pktopts. beware that there are non-zero default values in
2276 ip6_initpktopts(struct ip6_pktopts *opt)
2279 bzero(opt, sizeof(*opt));
2280 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2281 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2282 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2283 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2287 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2288 struct ucred *cred, int uproto)
2290 struct ip6_pktopts *opt;
2292 if (*pktopt == NULL) {
2293 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2295 ip6_initpktopts(*pktopt);
2299 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2303 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2305 void *optdata = NULL;
2307 struct ip6_ext *ip6e;
2309 struct in6_pktinfo null_pktinfo;
2310 int deftclass = 0, on;
2311 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2312 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2316 optdata = (void *)&null_pktinfo;
2317 if (pktopt && pktopt->ip6po_pktinfo) {
2318 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2319 sizeof(null_pktinfo));
2320 in6_clearscope(&null_pktinfo.ipi6_addr);
2322 /* XXX: we don't have to do this every time... */
2323 bzero(&null_pktinfo, sizeof(null_pktinfo));
2325 optdatalen = sizeof(struct in6_pktinfo);
2328 if (pktopt && pktopt->ip6po_tclass >= 0)
2329 optdata = (void *)&pktopt->ip6po_tclass;
2331 optdata = (void *)&deftclass;
2332 optdatalen = sizeof(int);
2335 if (pktopt && pktopt->ip6po_hbh) {
2336 optdata = (void *)pktopt->ip6po_hbh;
2337 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2338 optdatalen = (ip6e->ip6e_len + 1) << 3;
2342 if (pktopt && pktopt->ip6po_rthdr) {
2343 optdata = (void *)pktopt->ip6po_rthdr;
2344 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2345 optdatalen = (ip6e->ip6e_len + 1) << 3;
2348 case IPV6_RTHDRDSTOPTS:
2349 if (pktopt && pktopt->ip6po_dest1) {
2350 optdata = (void *)pktopt->ip6po_dest1;
2351 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2352 optdatalen = (ip6e->ip6e_len + 1) << 3;
2356 if (pktopt && pktopt->ip6po_dest2) {
2357 optdata = (void *)pktopt->ip6po_dest2;
2358 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2359 optdatalen = (ip6e->ip6e_len + 1) << 3;
2363 if (pktopt && pktopt->ip6po_nexthop) {
2364 optdata = (void *)pktopt->ip6po_nexthop;
2365 optdatalen = pktopt->ip6po_nexthop->sa_len;
2368 case IPV6_USE_MIN_MTU:
2370 optdata = (void *)&pktopt->ip6po_minmtu;
2372 optdata = (void *)&defminmtu;
2373 optdatalen = sizeof(int);
2376 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2380 optdata = (void *)&on;
2381 optdatalen = sizeof(on);
2383 case IPV6_PREFER_TEMPADDR:
2385 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2387 optdata = (void *)&defpreftemp;
2388 optdatalen = sizeof(int);
2390 default: /* should not happen */
2392 panic("ip6_getpcbopt: unexpected option\n");
2394 return (ENOPROTOOPT);
2397 error = sooptcopyout(sopt, optdata, optdatalen);
2403 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2408 if (optname == -1 || optname == IPV6_PKTINFO) {
2409 if (pktopt->ip6po_pktinfo)
2410 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2411 pktopt->ip6po_pktinfo = NULL;
2413 if (optname == -1 || optname == IPV6_HOPLIMIT)
2414 pktopt->ip6po_hlim = -1;
2415 if (optname == -1 || optname == IPV6_TCLASS)
2416 pktopt->ip6po_tclass = -1;
2417 if (optname == -1 || optname == IPV6_NEXTHOP) {
2418 if (pktopt->ip6po_nextroute.ro_rt) {
2419 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2420 pktopt->ip6po_nextroute.ro_rt = NULL;
2422 if (pktopt->ip6po_nexthop)
2423 free(pktopt->ip6po_nexthop, M_IP6OPT);
2424 pktopt->ip6po_nexthop = NULL;
2426 if (optname == -1 || optname == IPV6_HOPOPTS) {
2427 if (pktopt->ip6po_hbh)
2428 free(pktopt->ip6po_hbh, M_IP6OPT);
2429 pktopt->ip6po_hbh = NULL;
2431 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2432 if (pktopt->ip6po_dest1)
2433 free(pktopt->ip6po_dest1, M_IP6OPT);
2434 pktopt->ip6po_dest1 = NULL;
2436 if (optname == -1 || optname == IPV6_RTHDR) {
2437 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2438 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2439 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2440 if (pktopt->ip6po_route.ro_rt) {
2441 RTFREE(pktopt->ip6po_route.ro_rt);
2442 pktopt->ip6po_route.ro_rt = NULL;
2445 if (optname == -1 || optname == IPV6_DSTOPTS) {
2446 if (pktopt->ip6po_dest2)
2447 free(pktopt->ip6po_dest2, M_IP6OPT);
2448 pktopt->ip6po_dest2 = NULL;
2452 #define PKTOPT_EXTHDRCPY(type) \
2455 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2456 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2457 if (dst->type == NULL)\
2459 bcopy(src->type, dst->type, hlen);\
2461 } while (/*CONSTCOND*/ 0)
2464 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2466 if (dst == NULL || src == NULL) {
2467 printf("ip6_clearpktopts: invalid argument\n");
2471 dst->ip6po_hlim = src->ip6po_hlim;
2472 dst->ip6po_tclass = src->ip6po_tclass;
2473 dst->ip6po_flags = src->ip6po_flags;
2474 dst->ip6po_minmtu = src->ip6po_minmtu;
2475 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2476 if (src->ip6po_pktinfo) {
2477 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2479 if (dst->ip6po_pktinfo == NULL)
2481 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2483 if (src->ip6po_nexthop) {
2484 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2486 if (dst->ip6po_nexthop == NULL)
2488 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2489 src->ip6po_nexthop->sa_len);
2491 PKTOPT_EXTHDRCPY(ip6po_hbh);
2492 PKTOPT_EXTHDRCPY(ip6po_dest1);
2493 PKTOPT_EXTHDRCPY(ip6po_dest2);
2494 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2498 ip6_clearpktopts(dst, -1);
2501 #undef PKTOPT_EXTHDRCPY
2503 struct ip6_pktopts *
2504 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2507 struct ip6_pktopts *dst;
2509 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2512 ip6_initpktopts(dst);
2514 if ((error = copypktopts(dst, src, canwait)) != 0) {
2515 free(dst, M_IP6OPT);
2523 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2528 ip6_clearpktopts(pktopt, -1);
2530 free(pktopt, M_IP6OPT);
2534 * Set IPv6 outgoing packet options based on advanced API.
2537 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2538 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2540 struct cmsghdr *cm = NULL;
2542 if (control == NULL || opt == NULL)
2545 ip6_initpktopts(opt);
2550 * If stickyopt is provided, make a local copy of the options
2551 * for this particular packet, then override them by ancillary
2553 * XXX: copypktopts() does not copy the cached route to a next
2554 * hop (if any). This is not very good in terms of efficiency,
2555 * but we can allow this since this option should be rarely
2558 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2563 * XXX: Currently, we assume all the optional information is stored
2566 if (control->m_next)
2569 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2570 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2573 if (control->m_len < CMSG_LEN(0))
2576 cm = mtod(control, struct cmsghdr *);
2577 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2579 if (cm->cmsg_level != IPPROTO_IPV6)
2582 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2583 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2592 * Set a particular packet option, as a sticky option or an ancillary data
2593 * item. "len" can be 0 only when it's a sticky option.
2594 * We have 4 cases of combination of "sticky" and "cmsg":
2595 * "sticky=0, cmsg=0": impossible
2596 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2597 * "sticky=1, cmsg=0": RFC3542 socket option
2598 * "sticky=1, cmsg=1": RFC2292 socket option
2601 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2602 struct ucred *cred, int sticky, int cmsg, int uproto)
2604 int minmtupolicy, preftemp;
2607 if (!sticky && !cmsg) {
2609 printf("ip6_setpktopt: impossible case\n");
2615 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2616 * not be specified in the context of RFC3542. Conversely,
2617 * RFC3542 types should not be specified in the context of RFC2292.
2621 case IPV6_2292PKTINFO:
2622 case IPV6_2292HOPLIMIT:
2623 case IPV6_2292NEXTHOP:
2624 case IPV6_2292HOPOPTS:
2625 case IPV6_2292DSTOPTS:
2626 case IPV6_2292RTHDR:
2627 case IPV6_2292PKTOPTIONS:
2628 return (ENOPROTOOPT);
2631 if (sticky && cmsg) {
2638 case IPV6_RTHDRDSTOPTS:
2640 case IPV6_USE_MIN_MTU:
2643 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2644 return (ENOPROTOOPT);
2649 case IPV6_2292PKTINFO:
2652 struct ifnet *ifp = NULL;
2653 struct in6_pktinfo *pktinfo;
2655 if (len != sizeof(struct in6_pktinfo))
2658 pktinfo = (struct in6_pktinfo *)buf;
2661 * An application can clear any sticky IPV6_PKTINFO option by
2662 * doing a "regular" setsockopt with ipi6_addr being
2663 * in6addr_any and ipi6_ifindex being zero.
2664 * [RFC 3542, Section 6]
2666 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2667 pktinfo->ipi6_ifindex == 0 &&
2668 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2669 ip6_clearpktopts(opt, optname);
2673 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2674 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2677 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2679 /* validate the interface index if specified. */
2680 if (pktinfo->ipi6_ifindex > V_if_index)
2682 if (pktinfo->ipi6_ifindex) {
2683 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2687 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2688 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2692 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2693 struct in6_ifaddr *ia;
2695 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2696 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2698 return (EADDRNOTAVAIL);
2699 ifa_free(&ia->ia_ifa);
2702 * We store the address anyway, and let in6_selectsrc()
2703 * validate the specified address. This is because ipi6_addr
2704 * may not have enough information about its scope zone, and
2705 * we may need additional information (such as outgoing
2706 * interface or the scope zone of a destination address) to
2707 * disambiguate the scope.
2708 * XXX: the delay of the validation may confuse the
2709 * application when it is used as a sticky option.
2711 if (opt->ip6po_pktinfo == NULL) {
2712 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2713 M_IP6OPT, M_NOWAIT);
2714 if (opt->ip6po_pktinfo == NULL)
2717 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2721 case IPV6_2292HOPLIMIT:
2727 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2728 * to simplify the ordering among hoplimit options.
2730 if (optname == IPV6_HOPLIMIT && sticky)
2731 return (ENOPROTOOPT);
2733 if (len != sizeof(int))
2736 if (*hlimp < -1 || *hlimp > 255)
2739 opt->ip6po_hlim = *hlimp;
2747 if (len != sizeof(int))
2749 tclass = *(int *)buf;
2750 if (tclass < -1 || tclass > 255)
2753 opt->ip6po_tclass = tclass;
2757 case IPV6_2292NEXTHOP:
2760 error = priv_check_cred(cred,
2761 PRIV_NETINET_SETHDROPTS, 0);
2766 if (len == 0) { /* just remove the option */
2767 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2771 /* check if cmsg_len is large enough for sa_len */
2772 if (len < sizeof(struct sockaddr) || len < *buf)
2775 switch (((struct sockaddr *)buf)->sa_family) {
2778 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2781 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2784 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2785 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2788 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2794 case AF_LINK: /* should eventually be supported */
2796 return (EAFNOSUPPORT);
2799 /* turn off the previous option, then set the new option. */
2800 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2801 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2802 if (opt->ip6po_nexthop == NULL)
2804 bcopy(buf, opt->ip6po_nexthop, *buf);
2807 case IPV6_2292HOPOPTS:
2810 struct ip6_hbh *hbh;
2814 * XXX: We don't allow a non-privileged user to set ANY HbH
2815 * options, since per-option restriction has too much
2819 error = priv_check_cred(cred,
2820 PRIV_NETINET_SETHDROPTS, 0);
2826 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2827 break; /* just remove the option */
2830 /* message length validation */
2831 if (len < sizeof(struct ip6_hbh))
2833 hbh = (struct ip6_hbh *)buf;
2834 hbhlen = (hbh->ip6h_len + 1) << 3;
2838 /* turn off the previous option, then set the new option. */
2839 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2840 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2841 if (opt->ip6po_hbh == NULL)
2843 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2848 case IPV6_2292DSTOPTS:
2850 case IPV6_RTHDRDSTOPTS:
2852 struct ip6_dest *dest, **newdest = NULL;
2855 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2856 error = priv_check_cred(cred,
2857 PRIV_NETINET_SETHDROPTS, 0);
2863 ip6_clearpktopts(opt, optname);
2864 break; /* just remove the option */
2867 /* message length validation */
2868 if (len < sizeof(struct ip6_dest))
2870 dest = (struct ip6_dest *)buf;
2871 destlen = (dest->ip6d_len + 1) << 3;
2876 * Determine the position that the destination options header
2877 * should be inserted; before or after the routing header.
2880 case IPV6_2292DSTOPTS:
2882 * The old advacned API is ambiguous on this point.
2883 * Our approach is to determine the position based
2884 * according to the existence of a routing header.
2885 * Note, however, that this depends on the order of the
2886 * extension headers in the ancillary data; the 1st
2887 * part of the destination options header must appear
2888 * before the routing header in the ancillary data,
2890 * RFC3542 solved the ambiguity by introducing
2891 * separate ancillary data or option types.
2893 if (opt->ip6po_rthdr == NULL)
2894 newdest = &opt->ip6po_dest1;
2896 newdest = &opt->ip6po_dest2;
2898 case IPV6_RTHDRDSTOPTS:
2899 newdest = &opt->ip6po_dest1;
2902 newdest = &opt->ip6po_dest2;
2906 /* turn off the previous option, then set the new option. */
2907 ip6_clearpktopts(opt, optname);
2908 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2909 if (*newdest == NULL)
2911 bcopy(dest, *newdest, destlen);
2916 case IPV6_2292RTHDR:
2919 struct ip6_rthdr *rth;
2923 ip6_clearpktopts(opt, IPV6_RTHDR);
2924 break; /* just remove the option */
2927 /* message length validation */
2928 if (len < sizeof(struct ip6_rthdr))
2930 rth = (struct ip6_rthdr *)buf;
2931 rthlen = (rth->ip6r_len + 1) << 3;
2935 switch (rth->ip6r_type) {
2936 case IPV6_RTHDR_TYPE_0:
2937 if (rth->ip6r_len == 0) /* must contain one addr */
2939 if (rth->ip6r_len % 2) /* length must be even */
2941 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2945 return (EINVAL); /* not supported */
2948 /* turn off the previous option */
2949 ip6_clearpktopts(opt, IPV6_RTHDR);
2950 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2951 if (opt->ip6po_rthdr == NULL)
2953 bcopy(rth, opt->ip6po_rthdr, rthlen);
2958 case IPV6_USE_MIN_MTU:
2959 if (len != sizeof(int))
2961 minmtupolicy = *(int *)buf;
2962 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2963 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2964 minmtupolicy != IP6PO_MINMTU_ALL) {
2967 opt->ip6po_minmtu = minmtupolicy;
2971 if (len != sizeof(int))
2974 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2976 * we ignore this option for TCP sockets.
2977 * (RFC3542 leaves this case unspecified.)
2979 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2981 opt->ip6po_flags |= IP6PO_DONTFRAG;
2984 case IPV6_PREFER_TEMPADDR:
2985 if (len != sizeof(int))
2987 preftemp = *(int *)buf;
2988 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2989 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2990 preftemp != IP6PO_TEMPADDR_PREFER) {
2993 opt->ip6po_prefer_tempaddr = preftemp;
2997 return (ENOPROTOOPT);
2998 } /* end of switch */
3004 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3005 * packet to the input queue of a specified interface. Note that this
3006 * calls the output routine of the loopback "driver", but with an interface
3007 * pointer that might NOT be &loif -- easier than replicating that code here.
3010 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3013 struct ip6_hdr *ip6;
3015 copym = m_copy(m, 0, M_COPYALL);
3020 * Make sure to deep-copy IPv6 header portion in case the data
3021 * is in an mbuf cluster, so that we can safely override the IPv6
3022 * header portion later.
3024 if (!M_WRITABLE(copym) ||
3025 copym->m_len < sizeof(struct ip6_hdr)) {
3026 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3030 ip6 = mtod(copym, struct ip6_hdr *);
3032 * clear embedded scope identifiers if necessary.
3033 * in6_clearscope will touch the addresses only when necessary.
3035 in6_clearscope(&ip6->ip6_src);
3036 in6_clearscope(&ip6->ip6_dst);
3037 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3038 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3040 copym->m_pkthdr.csum_data = 0xffff;
3042 if_simloop(ifp, copym, AF_INET6, 0);
3046 * Chop IPv6 header off from the payload.
3049 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3052 struct ip6_hdr *ip6;
3054 ip6 = mtod(m, struct ip6_hdr *);
3055 if (m->m_len > sizeof(*ip6)) {
3056 mh = m_gethdr(M_NOWAIT, MT_DATA);
3061 m_move_pkthdr(mh, m);
3062 M_ALIGN(mh, sizeof(*ip6));
3063 m->m_len -= sizeof(*ip6);
3064 m->m_data += sizeof(*ip6);
3067 m->m_len = sizeof(*ip6);
3068 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3070 exthdrs->ip6e_ip6 = m;
3075 * Compute IPv6 extension header length.
3078 ip6_optlen(struct inpcb *in6p)
3082 if (!in6p->in6p_outputopts)
3087 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3089 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3090 if (in6p->in6p_outputopts->ip6po_rthdr)
3091 /* dest1 is valid with rthdr only */
3092 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3093 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3094 len += elen(in6p->in6p_outputopts->ip6po_dest2);