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 */
1502 error = ip6_pcbopts(&in6p->in6p_outputopts,
1505 m_freem(m); /* XXX */
1510 * Use of some Hop-by-Hop options or some
1511 * Destination options, might require special
1512 * privilege. That is, normal applications
1513 * (without special privilege) might be forbidden
1514 * from setting certain options in outgoing packets,
1515 * and might never see certain options in received
1516 * packets. [RFC 2292 Section 6]
1517 * KAME specific note:
1518 * KAME prevents non-privileged users from sending or
1519 * receiving ANY hbh/dst options in order to avoid
1520 * overhead of parsing options in the kernel.
1522 case IPV6_RECVHOPOPTS:
1523 case IPV6_RECVDSTOPTS:
1524 case IPV6_RECVRTHDRDSTOPTS:
1526 error = priv_check(td,
1527 PRIV_NETINET_SETHDROPTS);
1532 case IPV6_UNICAST_HOPS:
1535 case IPV6_RECVPKTINFO:
1536 case IPV6_RECVHOPLIMIT:
1537 case IPV6_RECVRTHDR:
1538 case IPV6_RECVPATHMTU:
1539 case IPV6_RECVTCLASS:
1540 case IPV6_RECVFLOWID:
1542 case IPV6_RECVRSSBUCKETID:
1545 case IPV6_AUTOFLOWLABEL:
1547 case IPV6_BINDMULTI:
1549 case IPV6_RSS_LISTEN_BUCKET:
1551 if (optname == IPV6_BINDANY && td != NULL) {
1552 error = priv_check(td,
1553 PRIV_NETINET_BINDANY);
1558 if (optlen != sizeof(int)) {
1562 error = sooptcopyin(sopt, &optval,
1563 sizeof optval, sizeof optval);
1568 case IPV6_UNICAST_HOPS:
1569 if (optval < -1 || optval >= 256)
1572 /* -1 = kernel default */
1573 in6p->in6p_hops = optval;
1574 if ((in6p->inp_vflag &
1576 in6p->inp_ip_ttl = optval;
1579 #define OPTSET(bit) \
1583 in6p->inp_flags |= (bit); \
1585 in6p->inp_flags &= ~(bit); \
1586 INP_WUNLOCK(in6p); \
1587 } while (/*CONSTCOND*/ 0)
1588 #define OPTSET2292(bit) \
1591 in6p->inp_flags |= IN6P_RFC2292; \
1593 in6p->inp_flags |= (bit); \
1595 in6p->inp_flags &= ~(bit); \
1596 INP_WUNLOCK(in6p); \
1597 } while (/*CONSTCOND*/ 0)
1598 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1600 #define OPTSET2(bit, val) do { \
1603 in6p->inp_flags2 |= bit; \
1605 in6p->inp_flags2 &= ~bit; \
1606 INP_WUNLOCK(in6p); \
1608 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1610 case IPV6_RECVPKTINFO:
1611 /* cannot mix with RFC2292 */
1612 if (OPTBIT(IN6P_RFC2292)) {
1616 OPTSET(IN6P_PKTINFO);
1621 struct ip6_pktopts **optp;
1623 /* cannot mix with RFC2292 */
1624 if (OPTBIT(IN6P_RFC2292)) {
1628 optp = &in6p->in6p_outputopts;
1629 error = ip6_pcbopt(IPV6_HOPLIMIT,
1630 (u_char *)&optval, sizeof(optval),
1631 optp, (td != NULL) ? td->td_ucred :
1636 case IPV6_RECVHOPLIMIT:
1637 /* cannot mix with RFC2292 */
1638 if (OPTBIT(IN6P_RFC2292)) {
1642 OPTSET(IN6P_HOPLIMIT);
1645 case IPV6_RECVHOPOPTS:
1646 /* cannot mix with RFC2292 */
1647 if (OPTBIT(IN6P_RFC2292)) {
1651 OPTSET(IN6P_HOPOPTS);
1654 case IPV6_RECVDSTOPTS:
1655 /* cannot mix with RFC2292 */
1656 if (OPTBIT(IN6P_RFC2292)) {
1660 OPTSET(IN6P_DSTOPTS);
1663 case IPV6_RECVRTHDRDSTOPTS:
1664 /* cannot mix with RFC2292 */
1665 if (OPTBIT(IN6P_RFC2292)) {
1669 OPTSET(IN6P_RTHDRDSTOPTS);
1672 case IPV6_RECVRTHDR:
1673 /* cannot mix with RFC2292 */
1674 if (OPTBIT(IN6P_RFC2292)) {
1681 case IPV6_RECVPATHMTU:
1683 * We ignore this option for TCP
1685 * (RFC3542 leaves this case
1688 if (uproto != IPPROTO_TCP)
1692 case IPV6_RECVFLOWID:
1693 OPTSET2(INP_RECVFLOWID, optval);
1697 case IPV6_RECVRSSBUCKETID:
1698 OPTSET2(INP_RECVRSSBUCKETID, optval);
1704 * make setsockopt(IPV6_V6ONLY)
1705 * available only prior to bind(2).
1706 * see ipng mailing list, Jun 22 2001.
1708 if (in6p->inp_lport ||
1709 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1713 OPTSET(IN6P_IPV6_V6ONLY);
1715 in6p->inp_vflag &= ~INP_IPV4;
1717 in6p->inp_vflag |= INP_IPV4;
1719 case IPV6_RECVTCLASS:
1720 /* cannot mix with RFC2292 XXX */
1721 if (OPTBIT(IN6P_RFC2292)) {
1725 OPTSET(IN6P_TCLASS);
1727 case IPV6_AUTOFLOWLABEL:
1728 OPTSET(IN6P_AUTOFLOWLABEL);
1732 OPTSET(INP_BINDANY);
1735 case IPV6_BINDMULTI:
1736 OPTSET2(INP_BINDMULTI, optval);
1739 case IPV6_RSS_LISTEN_BUCKET:
1740 if ((optval >= 0) &&
1741 (optval < rss_getnumbuckets())) {
1742 in6p->inp_rss_listen_bucket = optval;
1743 OPTSET2(INP_RSS_BUCKET_SET, 1);
1754 case IPV6_USE_MIN_MTU:
1755 case IPV6_PREFER_TEMPADDR:
1756 if (optlen != sizeof(optval)) {
1760 error = sooptcopyin(sopt, &optval,
1761 sizeof optval, sizeof optval);
1765 struct ip6_pktopts **optp;
1766 optp = &in6p->in6p_outputopts;
1767 error = ip6_pcbopt(optname,
1768 (u_char *)&optval, sizeof(optval),
1769 optp, (td != NULL) ? td->td_ucred :
1774 case IPV6_2292PKTINFO:
1775 case IPV6_2292HOPLIMIT:
1776 case IPV6_2292HOPOPTS:
1777 case IPV6_2292DSTOPTS:
1778 case IPV6_2292RTHDR:
1780 if (optlen != sizeof(int)) {
1784 error = sooptcopyin(sopt, &optval,
1785 sizeof optval, sizeof optval);
1789 case IPV6_2292PKTINFO:
1790 OPTSET2292(IN6P_PKTINFO);
1792 case IPV6_2292HOPLIMIT:
1793 OPTSET2292(IN6P_HOPLIMIT);
1795 case IPV6_2292HOPOPTS:
1797 * Check super-user privilege.
1798 * See comments for IPV6_RECVHOPOPTS.
1801 error = priv_check(td,
1802 PRIV_NETINET_SETHDROPTS);
1806 OPTSET2292(IN6P_HOPOPTS);
1808 case IPV6_2292DSTOPTS:
1810 error = priv_check(td,
1811 PRIV_NETINET_SETHDROPTS);
1815 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1817 case IPV6_2292RTHDR:
1818 OPTSET2292(IN6P_RTHDR);
1826 case IPV6_RTHDRDSTOPTS:
1829 /* new advanced API (RFC3542) */
1831 u_char optbuf_storage[MCLBYTES];
1833 struct ip6_pktopts **optp;
1835 /* cannot mix with RFC2292 */
1836 if (OPTBIT(IN6P_RFC2292)) {
1842 * We only ensure valsize is not too large
1843 * here. Further validation will be done
1846 error = sooptcopyin(sopt, optbuf_storage,
1847 sizeof(optbuf_storage), 0);
1850 optlen = sopt->sopt_valsize;
1851 optbuf = optbuf_storage;
1852 optp = &in6p->in6p_outputopts;
1853 error = ip6_pcbopt(optname, optbuf, optlen,
1854 optp, (td != NULL) ? td->td_ucred : NULL,
1860 case IPV6_MULTICAST_IF:
1861 case IPV6_MULTICAST_HOPS:
1862 case IPV6_MULTICAST_LOOP:
1863 case IPV6_JOIN_GROUP:
1864 case IPV6_LEAVE_GROUP:
1866 case MCAST_BLOCK_SOURCE:
1867 case MCAST_UNBLOCK_SOURCE:
1868 case MCAST_JOIN_GROUP:
1869 case MCAST_LEAVE_GROUP:
1870 case MCAST_JOIN_SOURCE_GROUP:
1871 case MCAST_LEAVE_SOURCE_GROUP:
1872 error = ip6_setmoptions(in6p, sopt);
1875 case IPV6_PORTRANGE:
1876 error = sooptcopyin(sopt, &optval,
1877 sizeof optval, sizeof optval);
1883 case IPV6_PORTRANGE_DEFAULT:
1884 in6p->inp_flags &= ~(INP_LOWPORT);
1885 in6p->inp_flags &= ~(INP_HIGHPORT);
1888 case IPV6_PORTRANGE_HIGH:
1889 in6p->inp_flags &= ~(INP_LOWPORT);
1890 in6p->inp_flags |= INP_HIGHPORT;
1893 case IPV6_PORTRANGE_LOW:
1894 in6p->inp_flags &= ~(INP_HIGHPORT);
1895 in6p->inp_flags |= INP_LOWPORT;
1905 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1906 case IPV6_IPSEC_POLICY:
1907 if (IPSEC_ENABLED(ipv6)) {
1908 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
1915 error = ENOPROTOOPT;
1923 case IPV6_2292PKTOPTIONS:
1924 #ifdef IPV6_PKTOPTIONS
1925 case IPV6_PKTOPTIONS:
1928 * RFC3542 (effectively) deprecated the
1929 * semantics of the 2292-style pktoptions.
1930 * Since it was not reliable in nature (i.e.,
1931 * applications had to expect the lack of some
1932 * information after all), it would make sense
1933 * to simplify this part by always returning
1936 sopt->sopt_valsize = 0;
1939 case IPV6_RECVHOPOPTS:
1940 case IPV6_RECVDSTOPTS:
1941 case IPV6_RECVRTHDRDSTOPTS:
1942 case IPV6_UNICAST_HOPS:
1943 case IPV6_RECVPKTINFO:
1944 case IPV6_RECVHOPLIMIT:
1945 case IPV6_RECVRTHDR:
1946 case IPV6_RECVPATHMTU:
1949 case IPV6_PORTRANGE:
1950 case IPV6_RECVTCLASS:
1951 case IPV6_AUTOFLOWLABEL:
1955 case IPV6_RECVFLOWID:
1957 case IPV6_RSSBUCKETID:
1958 case IPV6_RECVRSSBUCKETID:
1960 case IPV6_BINDMULTI:
1963 case IPV6_RECVHOPOPTS:
1964 optval = OPTBIT(IN6P_HOPOPTS);
1967 case IPV6_RECVDSTOPTS:
1968 optval = OPTBIT(IN6P_DSTOPTS);
1971 case IPV6_RECVRTHDRDSTOPTS:
1972 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1975 case IPV6_UNICAST_HOPS:
1976 optval = in6p->in6p_hops;
1979 case IPV6_RECVPKTINFO:
1980 optval = OPTBIT(IN6P_PKTINFO);
1983 case IPV6_RECVHOPLIMIT:
1984 optval = OPTBIT(IN6P_HOPLIMIT);
1987 case IPV6_RECVRTHDR:
1988 optval = OPTBIT(IN6P_RTHDR);
1991 case IPV6_RECVPATHMTU:
1992 optval = OPTBIT(IN6P_MTU);
1996 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1999 case IPV6_PORTRANGE:
2002 flags = in6p->inp_flags;
2003 if (flags & INP_HIGHPORT)
2004 optval = IPV6_PORTRANGE_HIGH;
2005 else if (flags & INP_LOWPORT)
2006 optval = IPV6_PORTRANGE_LOW;
2011 case IPV6_RECVTCLASS:
2012 optval = OPTBIT(IN6P_TCLASS);
2015 case IPV6_AUTOFLOWLABEL:
2016 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2020 optval = OPTBIT(INP_BINDANY);
2024 optval = in6p->inp_flowid;
2028 optval = in6p->inp_flowtype;
2031 case IPV6_RECVFLOWID:
2032 optval = OPTBIT2(INP_RECVFLOWID);
2035 case IPV6_RSSBUCKETID:
2037 rss_hash2bucket(in6p->inp_flowid,
2041 optval = rss_bucket;
2046 case IPV6_RECVRSSBUCKETID:
2047 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2051 case IPV6_BINDMULTI:
2052 optval = OPTBIT2(INP_BINDMULTI);
2058 error = sooptcopyout(sopt, &optval,
2065 struct ip6_mtuinfo mtuinfo;
2067 if (!(so->so_state & SS_ISCONNECTED))
2070 * XXX: we dot not consider the case of source
2071 * routing, or optional information to specify
2072 * the outgoing interface.
2074 error = ip6_getpmtu_ctl(so->so_fibnum,
2075 &in6p->in6p_faddr, &pmtu);
2078 if (pmtu > IPV6_MAXPACKET)
2079 pmtu = IPV6_MAXPACKET;
2081 bzero(&mtuinfo, sizeof(mtuinfo));
2082 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2083 optdata = (void *)&mtuinfo;
2084 optdatalen = sizeof(mtuinfo);
2085 error = sooptcopyout(sopt, optdata,
2090 case IPV6_2292PKTINFO:
2091 case IPV6_2292HOPLIMIT:
2092 case IPV6_2292HOPOPTS:
2093 case IPV6_2292RTHDR:
2094 case IPV6_2292DSTOPTS:
2096 case IPV6_2292PKTINFO:
2097 optval = OPTBIT(IN6P_PKTINFO);
2099 case IPV6_2292HOPLIMIT:
2100 optval = OPTBIT(IN6P_HOPLIMIT);
2102 case IPV6_2292HOPOPTS:
2103 optval = OPTBIT(IN6P_HOPOPTS);
2105 case IPV6_2292RTHDR:
2106 optval = OPTBIT(IN6P_RTHDR);
2108 case IPV6_2292DSTOPTS:
2109 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2112 error = sooptcopyout(sopt, &optval,
2119 case IPV6_RTHDRDSTOPTS:
2123 case IPV6_USE_MIN_MTU:
2124 case IPV6_PREFER_TEMPADDR:
2125 error = ip6_getpcbopt(in6p->in6p_outputopts,
2129 case IPV6_MULTICAST_IF:
2130 case IPV6_MULTICAST_HOPS:
2131 case IPV6_MULTICAST_LOOP:
2133 error = ip6_getmoptions(in6p, sopt);
2136 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2137 case IPV6_IPSEC_POLICY:
2138 if (IPSEC_ENABLED(ipv6)) {
2139 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
2145 error = ENOPROTOOPT;
2155 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2157 int error = 0, optval, optlen;
2158 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2159 struct inpcb *in6p = sotoinpcb(so);
2160 int level, op, optname;
2162 level = sopt->sopt_level;
2163 op = sopt->sopt_dir;
2164 optname = sopt->sopt_name;
2165 optlen = sopt->sopt_valsize;
2167 if (level != IPPROTO_IPV6) {
2174 * For ICMPv6 sockets, no modification allowed for checksum
2175 * offset, permit "no change" values to help existing apps.
2177 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2178 * for an ICMPv6 socket will fail."
2179 * The current behavior does not meet RFC3542.
2183 if (optlen != sizeof(int)) {
2187 error = sooptcopyin(sopt, &optval, sizeof(optval),
2191 if (optval < -1 || (optval % 2) != 0) {
2193 * The API assumes non-negative even offset
2194 * values or -1 as a special value.
2197 } else if (so->so_proto->pr_protocol ==
2199 if (optval != icmp6off)
2202 in6p->in6p_cksum = optval;
2206 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2209 optval = in6p->in6p_cksum;
2211 error = sooptcopyout(sopt, &optval, sizeof(optval));
2221 error = ENOPROTOOPT;
2229 * Set up IP6 options in pcb for insertion in output packets or
2230 * specifying behavior of outgoing packets.
2233 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2234 struct socket *so, struct sockopt *sopt)
2236 struct ip6_pktopts *opt = *pktopt;
2238 struct thread *td = sopt->sopt_td;
2240 /* turn off any old options. */
2243 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2244 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2245 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2246 printf("ip6_pcbopts: all specified options are cleared.\n");
2248 ip6_clearpktopts(opt, -1);
2250 opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT);
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_copy(m, 0, M_COPYALL);
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);