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_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
211 int mtu, uint32_t id)
213 struct mbuf *m, **mnext, *m_frgpart;
214 struct ip6_hdr *ip6, *mhip6;
215 struct ip6_frag *ip6f;
218 int tlen = m0->m_pkthdr.len;
220 KASSERT(( mtu % 8 == 0), ("Fragment length must be a multiple of 8"));
223 ip6 = mtod(m, struct ip6_hdr *);
224 mnext = &m->m_nextpkt;
226 for (off = hlen; off < tlen; off += mtu) {
227 m = m_gethdr(M_NOWAIT, MT_DATA);
229 IP6STAT_INC(ip6s_odropped);
232 m->m_flags = m0->m_flags & M_COPYFLAGS;
234 mnext = &m->m_nextpkt;
235 m->m_data += max_linkhdr;
236 mhip6 = mtod(m, struct ip6_hdr *);
238 m->m_len = sizeof(*mhip6);
239 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
241 IP6STAT_INC(ip6s_odropped);
244 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
245 if (off + mtu >= tlen)
248 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
249 mhip6->ip6_plen = htons((u_short)(mtu + hlen +
250 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
251 if ((m_frgpart = m_copy(m0, off, mtu)) == NULL) {
252 IP6STAT_INC(ip6s_odropped);
256 m->m_pkthdr.len = mtu + hlen + sizeof(*ip6f);
257 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
258 m->m_pkthdr.rcvif = NULL;
259 ip6f->ip6f_reserved = 0;
260 ip6f->ip6f_ident = id;
261 ip6f->ip6f_nxt = nextproto;
262 IP6STAT_INC(ip6s_ofragments);
263 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
270 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
271 * header (with pri, len, nxt, hlim, src, dst).
272 * This function may modify ver and hlim only.
273 * The mbuf chain containing the packet will be freed.
274 * The mbuf opt, if present, will not be freed.
275 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
276 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
277 * then result of route lookup is stored in ro->ro_rt.
279 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
280 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
283 * ifpp - XXX: just for statistics
286 * XXX TODO: no flowid is assigned for outbound flows?
289 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
290 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
291 struct ifnet **ifpp, struct inpcb *inp)
294 struct ifnet *ifp, *origifp;
296 struct mbuf *mprev = NULL;
298 struct route_in6 ip6route;
299 struct rtentry *rt = NULL;
300 struct sockaddr_in6 *dst, src_sa, dst_sa;
301 struct in6_addr odst;
303 struct in6_ifaddr *ia = NULL;
305 int alwaysfrag, dontfrag;
306 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
307 struct ip6_exthdrs exthdrs;
308 struct in6_addr src0, dst0;
310 struct route_in6 *ro_pmtu = NULL;
315 struct m_tag *fwd_tag = NULL;
319 INP_LOCK_ASSERT(inp);
320 M_SETFIB(m, inp->inp_inc.inc_fibnum);
321 if ((flags & IP_NODEFAULTFLOWID) == 0) {
322 /* unconditionally set flowid */
323 m->m_pkthdr.flowid = inp->inp_flowid;
324 M_HASHTYPE_SET(m, inp->inp_flowtype);
328 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
330 * IPSec checking which handles several cases.
331 * FAST IPSEC: We re-injected the packet.
332 * XXX: need scope argument.
334 if (IPSEC_ENABLED(ipv6)) {
335 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
336 if (error == EINPROGRESS)
343 bzero(&exthdrs, sizeof(exthdrs));
345 /* Hop-by-Hop options header */
346 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
347 /* Destination options header(1st part) */
348 if (opt->ip6po_rthdr) {
350 * Destination options header(1st part)
351 * This only makes sense with a routing header.
352 * See Section 9.2 of RFC 3542.
353 * Disabling this part just for MIP6 convenience is
354 * a bad idea. We need to think carefully about a
355 * way to make the advanced API coexist with MIP6
356 * options, which might automatically be inserted in
359 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
362 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
363 /* Destination options header(2nd part) */
364 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
368 * Calculate the total length of the extension header chain.
369 * Keep the length of the unfragmentable part for fragmentation.
372 if (exthdrs.ip6e_hbh)
373 optlen += exthdrs.ip6e_hbh->m_len;
374 if (exthdrs.ip6e_dest1)
375 optlen += exthdrs.ip6e_dest1->m_len;
376 if (exthdrs.ip6e_rthdr)
377 optlen += exthdrs.ip6e_rthdr->m_len;
378 unfragpartlen = optlen + sizeof(struct ip6_hdr);
380 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
381 if (exthdrs.ip6e_dest2)
382 optlen += exthdrs.ip6e_dest2->m_len;
385 * If there is at least one extension header,
386 * separate IP6 header from the payload.
388 if (optlen && !hdrsplit) {
389 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
393 m = exthdrs.ip6e_ip6;
397 ip6 = mtod(m, struct ip6_hdr *);
399 /* adjust mbuf packet header length */
400 m->m_pkthdr.len += optlen;
401 plen = m->m_pkthdr.len - sizeof(*ip6);
403 /* If this is a jumbo payload, insert a jumbo payload option. */
404 if (plen > IPV6_MAXPACKET) {
406 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
410 m = exthdrs.ip6e_ip6;
414 ip6 = mtod(m, struct ip6_hdr *);
415 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
419 ip6->ip6_plen = htons(plen);
422 * Concatenate headers and fill in next header fields.
423 * Here we have, on "m"
425 * and we insert headers accordingly. Finally, we should be getting:
426 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
428 * during the header composing process, "m" points to IPv6 header.
429 * "mprev" points to an extension header prior to esp.
431 u_char *nexthdrp = &ip6->ip6_nxt;
435 * we treat dest2 specially. this makes IPsec processing
436 * much easier. the goal here is to make mprev point the
437 * mbuf prior to dest2.
439 * result: IPv6 dest2 payload
440 * m and mprev will point to IPv6 header.
442 if (exthdrs.ip6e_dest2) {
444 panic("assumption failed: hdr not split");
445 exthdrs.ip6e_dest2->m_next = m->m_next;
446 m->m_next = exthdrs.ip6e_dest2;
447 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
448 ip6->ip6_nxt = IPPROTO_DSTOPTS;
452 * result: IPv6 hbh dest1 rthdr dest2 payload
453 * m will point to IPv6 header. mprev will point to the
454 * extension header prior to dest2 (rthdr in the above case).
456 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
457 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
459 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
463 * If there is a routing header, discard the packet.
465 if (exthdrs.ip6e_rthdr) {
470 /* Source address validation */
471 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
472 (flags & IPV6_UNSPECSRC) == 0) {
474 IP6STAT_INC(ip6s_badscope);
477 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
479 IP6STAT_INC(ip6s_badscope);
483 IP6STAT_INC(ip6s_localout);
490 bzero((caddr_t)ro, sizeof(*ro));
493 if (opt && opt->ip6po_rthdr)
494 ro = &opt->ip6po_route;
495 dst = (struct sockaddr_in6 *)&ro->ro_dst;
497 if (ro->ro_rt == NULL)
498 (void )flowtable_lookup(AF_INET6, m, (struct route *)ro);
500 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
503 * if specified, try to fill in the traffic class field.
504 * do not override if a non-zero value is already set.
505 * we check the diffserv field and the ecn field separately.
507 if (opt && opt->ip6po_tclass >= 0) {
510 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
512 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
515 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
518 /* fill in or override the hop limit field, if necessary. */
519 if (opt && opt->ip6po_hlim != -1)
520 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
521 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
523 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
525 ip6->ip6_hlim = V_ip6_defmcasthlim;
528 * Validate route against routing table additions;
529 * a better/more specific route might have been added.
530 * Make sure address family is set in route.
533 ro->ro_dst.sin6_family = AF_INET6;
534 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
536 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
537 ro->ro_dst.sin6_family == AF_INET6 &&
538 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
540 ifp = ro->ro_rt->rt_ifp;
543 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
545 if (fwd_tag == NULL) {
546 bzero(&dst_sa, sizeof(dst_sa));
547 dst_sa.sin6_family = AF_INET6;
548 dst_sa.sin6_len = sizeof(dst_sa);
549 dst_sa.sin6_addr = ip6->ip6_dst;
551 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
555 in6_ifstat_inc(ifp, ifs6_out_discard);
561 * If in6_selectroute() does not return a route entry,
562 * dst may not have been updated.
564 *dst = dst_sa; /* XXX */
568 * then rt (for unicast) and ifp must be non-NULL valid values.
570 if ((flags & IPV6_FORWARDING) == 0) {
571 /* XXX: the FORWARDING flag can be set for mrouting. */
572 in6_ifstat_inc(ifp, ifs6_out_request);
575 ia = (struct in6_ifaddr *)(rt->rt_ifa);
576 counter_u64_add(rt->rt_pksent, 1);
579 /* Setup data structures for scope ID checks. */
581 bzero(&src_sa, sizeof(src_sa));
582 src_sa.sin6_family = AF_INET6;
583 src_sa.sin6_len = sizeof(src_sa);
584 src_sa.sin6_addr = ip6->ip6_src;
587 /* re-initialize to be sure */
588 bzero(&dst_sa, sizeof(dst_sa));
589 dst_sa.sin6_family = AF_INET6;
590 dst_sa.sin6_len = sizeof(dst_sa);
591 dst_sa.sin6_addr = ip6->ip6_dst;
593 /* Check for valid scope ID. */
594 if (in6_setscope(&src0, ifp, &zone) == 0 &&
595 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
596 in6_setscope(&dst0, ifp, &zone) == 0 &&
597 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
599 * The outgoing interface is in the zone of the source
600 * and destination addresses.
602 * Because the loopback interface cannot receive
603 * packets with a different scope ID than its own,
604 * there is a trick is to pretend the outgoing packet
605 * was received by the real network interface, by
606 * setting "origifp" different from "ifp". This is
607 * only allowed when "ifp" is a loopback network
608 * interface. Refer to code in nd6_output_ifp() for
614 * We should use ia_ifp to support the case of sending
615 * packets to an address of our own.
617 if (ia != NULL && ia->ia_ifp)
620 } else if ((ifp->if_flags & IFF_LOOPBACK) == 0 ||
621 sa6_recoverscope(&src_sa) != 0 ||
622 sa6_recoverscope(&dst_sa) != 0 ||
623 dst_sa.sin6_scope_id == 0 ||
624 (src_sa.sin6_scope_id != 0 &&
625 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
626 (origifp = ifnet_byindex(dst_sa.sin6_scope_id)) == NULL) {
628 * If the destination network interface is not a
629 * loopback interface, or the destination network
630 * address has no scope ID, or the source address has
631 * a scope ID set which is different from the
632 * destination address one, or there is no network
633 * interface representing this scope ID, the address
634 * pair is considered invalid.
636 IP6STAT_INC(ip6s_badscope);
637 in6_ifstat_inc(ifp, ifs6_out_discard);
639 error = EHOSTUNREACH; /* XXX */
643 /* All scope ID checks are successful. */
645 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
646 if (opt && opt->ip6po_nextroute.ro_rt) {
648 * The nexthop is explicitly specified by the
649 * application. We assume the next hop is an IPv6
652 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
654 else if ((rt->rt_flags & RTF_GATEWAY))
655 dst = (struct sockaddr_in6 *)rt->rt_gateway;
658 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
659 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
661 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
662 in6_ifstat_inc(ifp, ifs6_out_mcast);
664 * Confirm that the outgoing interface supports multicast.
666 if (!(ifp->if_flags & IFF_MULTICAST)) {
667 IP6STAT_INC(ip6s_noroute);
668 in6_ifstat_inc(ifp, ifs6_out_discard);
672 if ((im6o == NULL && in6_mcast_loop) ||
673 (im6o && im6o->im6o_multicast_loop)) {
675 * Loop back multicast datagram if not expressly
676 * forbidden to do so, even if we have not joined
677 * the address; protocols will filter it later,
678 * thus deferring a hash lookup and lock acquisition
679 * at the expense of an m_copym().
681 ip6_mloopback(ifp, m);
684 * If we are acting as a multicast router, perform
685 * multicast forwarding as if the packet had just
686 * arrived on the interface to which we are about
687 * to send. The multicast forwarding function
688 * recursively calls this function, using the
689 * IPV6_FORWARDING flag to prevent infinite recursion.
691 * Multicasts that are looped back by ip6_mloopback(),
692 * above, will be forwarded by the ip6_input() routine,
695 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
697 * XXX: ip6_mforward expects that rcvif is NULL
698 * when it is called from the originating path.
699 * However, it may not always be the case.
701 m->m_pkthdr.rcvif = NULL;
702 if (ip6_mforward(ip6, ifp, m) != 0) {
709 * Multicasts with a hoplimit of zero may be looped back,
710 * above, but must not be transmitted on a network.
711 * Also, multicasts addressed to the loopback interface
712 * are not sent -- the above call to ip6_mloopback() will
713 * loop back a copy if this host actually belongs to the
714 * destination group on the loopback interface.
716 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
717 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
724 * Fill the outgoing inteface to tell the upper layer
725 * to increment per-interface statistics.
730 /* Determine path MTU. */
731 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
732 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
736 * The caller of this function may specify to use the minimum MTU
738 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
739 * setting. The logic is a bit complicated; by default, unicast
740 * packets will follow path MTU while multicast packets will be sent at
741 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
742 * including unicast ones will be sent at the minimum MTU. Multicast
743 * packets will always be sent at the minimum MTU unless
744 * IP6PO_MINMTU_DISABLE is explicitly specified.
745 * See RFC 3542 for more details.
747 if (mtu > IPV6_MMTU) {
748 if ((flags & IPV6_MINMTU))
750 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
752 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
754 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
760 * clear embedded scope identifiers if necessary.
761 * in6_clearscope will touch the addresses only when necessary.
763 in6_clearscope(&ip6->ip6_src);
764 in6_clearscope(&ip6->ip6_dst);
767 * If the outgoing packet contains a hop-by-hop options header,
768 * it must be examined and processed even by the source node.
769 * (RFC 2460, section 4.)
771 if (exthdrs.ip6e_hbh) {
772 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
773 u_int32_t dummy; /* XXX unused */
774 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
777 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
778 panic("ip6e_hbh is not contiguous");
781 * XXX: if we have to send an ICMPv6 error to the sender,
782 * we need the M_LOOP flag since icmp6_error() expects
783 * the IPv6 and the hop-by-hop options header are
784 * contiguous unless the flag is set.
786 m->m_flags |= M_LOOP;
787 m->m_pkthdr.rcvif = ifp;
788 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
789 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
790 &dummy, &plen) < 0) {
791 /* m was already freed at this point */
792 error = EINVAL;/* better error? */
795 m->m_flags &= ~M_LOOP; /* XXX */
796 m->m_pkthdr.rcvif = NULL;
799 /* Jump over all PFIL processing if hooks are not active. */
800 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
804 /* Run through list of hooks for output packets. */
805 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, 0, inp);
806 if (error != 0 || m == NULL)
809 ip6 = mtod(m, struct ip6_hdr *);
812 /* See if destination IP address was changed by packet filter. */
813 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
814 m->m_flags |= M_SKIP_FIREWALL;
815 /* If destination is now ourself drop to ip6_input(). */
816 if (in6_localip(&ip6->ip6_dst)) {
817 m->m_flags |= M_FASTFWD_OURS;
818 if (m->m_pkthdr.rcvif == NULL)
819 m->m_pkthdr.rcvif = V_loif;
820 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
821 m->m_pkthdr.csum_flags |=
822 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
823 m->m_pkthdr.csum_data = 0xffff;
826 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
827 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
829 error = netisr_queue(NETISR_IPV6, m);
833 needfiblookup = 1; /* Redo the routing table lookup. */
835 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
839 /* See if fib was changed by packet filter. */
840 if (fibnum != M_GETFIB(m)) {
841 m->m_flags |= M_SKIP_FIREWALL;
842 fibnum = M_GETFIB(m);
846 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
852 /* See if local, if yes, send it to netisr. */
853 if (m->m_flags & M_FASTFWD_OURS) {
854 if (m->m_pkthdr.rcvif == NULL)
855 m->m_pkthdr.rcvif = V_loif;
856 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
857 m->m_pkthdr.csum_flags |=
858 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
859 m->m_pkthdr.csum_data = 0xffff;
862 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
863 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
865 error = netisr_queue(NETISR_IPV6, m);
868 /* Or forward to some other address? */
869 if ((m->m_flags & M_IP6_NEXTHOP) &&
870 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
871 dst = (struct sockaddr_in6 *)&ro->ro_dst;
872 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
873 m->m_flags |= M_SKIP_FIREWALL;
874 m->m_flags &= ~M_IP6_NEXTHOP;
875 m_tag_delete(m, fwd_tag);
881 * Send the packet to the outgoing interface.
882 * If necessary, do IPv6 fragmentation before sending.
884 * the logic here is rather complex:
885 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
886 * 1-a: send as is if tlen <= path mtu
887 * 1-b: fragment if tlen > path mtu
889 * 2: if user asks us not to fragment (dontfrag == 1)
890 * 2-a: send as is if tlen <= interface mtu
891 * 2-b: error if tlen > interface mtu
893 * 3: if we always need to attach fragment header (alwaysfrag == 1)
896 * 4: if dontfrag == 1 && alwaysfrag == 1
897 * error, as we cannot handle this conflicting request
899 sw_csum = m->m_pkthdr.csum_flags;
901 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
902 sw_csum &= ~ifp->if_hwassist;
906 * If we added extension headers, we will not do TSO and calculate the
907 * checksums ourselves for now.
908 * XXX-BZ Need a framework to know when the NIC can handle it, even
911 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
912 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
913 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
916 if (sw_csum & CSUM_SCTP_IPV6) {
917 sw_csum &= ~CSUM_SCTP_IPV6;
918 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
921 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
922 tlen = m->m_pkthdr.len;
924 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
928 if (dontfrag && alwaysfrag) { /* case 4 */
929 /* conflicting request - can't transmit */
933 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
935 * Even if the DONTFRAG option is specified, we cannot send the
936 * packet when the data length is larger than the MTU of the
937 * outgoing interface.
938 * Notify the error by sending IPV6_PATHMTU ancillary data if
939 * application wanted to know the MTU value. Also return an
940 * error code (this is not described in the API spec).
943 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
949 * transmit packet without fragmentation
951 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
952 struct in6_ifaddr *ia6;
954 ip6 = mtod(m, struct ip6_hdr *);
955 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
957 /* Record statistics for this interface address. */
958 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
959 counter_u64_add(ia6->ia_ifa.ifa_obytes,
961 ifa_free(&ia6->ia_ifa);
963 error = nd6_output_ifp(ifp, origifp, m, dst,
969 * try to fragment the packet. case 1-b and 3
971 if (mtu < IPV6_MMTU) {
972 /* path MTU cannot be less than IPV6_MMTU */
974 in6_ifstat_inc(ifp, ifs6_out_fragfail);
976 } else if (ip6->ip6_plen == 0) {
977 /* jumbo payload cannot be fragmented */
979 in6_ifstat_inc(ifp, ifs6_out_fragfail);
985 * Too large for the destination or interface;
986 * fragment if possible.
987 * Must be able to put at least 8 bytes per fragment.
989 hlen = unfragpartlen;
990 if (mtu > IPV6_MAXPACKET)
991 mtu = IPV6_MAXPACKET;
993 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
996 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1001 * If the interface will not calculate checksums on
1002 * fragmented packets, then do it here.
1003 * XXX-BZ handle the hw offloading case. Need flags.
1005 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1006 in6_delayed_cksum(m, plen, hlen);
1007 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1010 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1011 sctp_delayed_cksum(m, hlen);
1012 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1016 * Change the next header field of the last header in the
1017 * unfragmentable part.
1019 if (exthdrs.ip6e_rthdr) {
1020 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1021 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1022 } else if (exthdrs.ip6e_dest1) {
1023 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1024 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1025 } else if (exthdrs.ip6e_hbh) {
1026 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1027 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1029 nextproto = ip6->ip6_nxt;
1030 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1034 * Loop through length of segment after first fragment,
1035 * make new header and copy data of each part and link onto
1039 id = htonl(ip6_randomid());
1040 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1043 in6_ifstat_inc(ifp, ifs6_out_fragok);
1047 * Remove leading garbages.
1057 /* Record statistics for this interface address. */
1059 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1060 counter_u64_add(ia->ia_ifa.ifa_obytes,
1063 error = nd6_output_ifp(ifp, origifp, m, dst,
1064 (struct route *)ro);
1070 IP6STAT_INC(ip6s_fragmented);
1074 * Release the route if using our private route, or if
1075 * (with flowtable) we don't have our own reference.
1077 if (ro == &ip6route ||
1078 (ro != NULL && ro->ro_flags & RT_NORTREF))
1083 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1084 m_freem(exthdrs.ip6e_dest1);
1085 m_freem(exthdrs.ip6e_rthdr);
1086 m_freem(exthdrs.ip6e_dest2);
1095 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1099 if (hlen > MCLBYTES)
1100 return (ENOBUFS); /* XXX */
1103 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1105 m = m_get(M_NOWAIT, MT_DATA);
1110 bcopy(hdr, mtod(m, caddr_t), hlen);
1117 * Insert jumbo payload option.
1120 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1126 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1129 * If there is no hop-by-hop options header, allocate new one.
1130 * If there is one but it doesn't have enough space to store the
1131 * jumbo payload option, allocate a cluster to store the whole options.
1132 * Otherwise, use it to store the options.
1134 if (exthdrs->ip6e_hbh == NULL) {
1135 mopt = m_get(M_NOWAIT, MT_DATA);
1138 mopt->m_len = JUMBOOPTLEN;
1139 optbuf = mtod(mopt, u_char *);
1140 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1141 exthdrs->ip6e_hbh = mopt;
1143 struct ip6_hbh *hbh;
1145 mopt = exthdrs->ip6e_hbh;
1146 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1149 * - exthdrs->ip6e_hbh is not referenced from places
1150 * other than exthdrs.
1151 * - exthdrs->ip6e_hbh is not an mbuf chain.
1153 int oldoptlen = mopt->m_len;
1157 * XXX: give up if the whole (new) hbh header does
1158 * not fit even in an mbuf cluster.
1160 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1164 * As a consequence, we must always prepare a cluster
1167 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1170 n->m_len = oldoptlen + JUMBOOPTLEN;
1171 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1173 optbuf = mtod(n, caddr_t) + oldoptlen;
1175 mopt = exthdrs->ip6e_hbh = n;
1177 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1178 mopt->m_len += JUMBOOPTLEN;
1180 optbuf[0] = IP6OPT_PADN;
1184 * Adjust the header length according to the pad and
1185 * the jumbo payload option.
1187 hbh = mtod(mopt, struct ip6_hbh *);
1188 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1191 /* fill in the option. */
1192 optbuf[2] = IP6OPT_JUMBO;
1194 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1195 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1197 /* finally, adjust the packet header length */
1198 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1205 * Insert fragment header and copy unfragmentable header portions.
1208 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1209 struct ip6_frag **frghdrp)
1211 struct mbuf *n, *mlast;
1213 if (hlen > sizeof(struct ip6_hdr)) {
1214 n = m_copym(m0, sizeof(struct ip6_hdr),
1215 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1222 /* Search for the last mbuf of unfragmentable part. */
1223 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1226 if (M_WRITABLE(mlast) &&
1227 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1228 /* use the trailing space of the last mbuf for the fragment hdr */
1229 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1231 mlast->m_len += sizeof(struct ip6_frag);
1232 m->m_pkthdr.len += sizeof(struct ip6_frag);
1234 /* allocate a new mbuf for the fragment header */
1237 mfrg = m_get(M_NOWAIT, MT_DATA);
1240 mfrg->m_len = sizeof(struct ip6_frag);
1241 *frghdrp = mtod(mfrg, struct ip6_frag *);
1242 mlast->m_next = mfrg;
1249 * Calculates IPv6 path mtu for destination @dst.
1250 * Resulting MTU is stored in @mtup.
1252 * Returns 0 on success.
1255 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1257 struct nhop6_extended nh6;
1258 struct in6_addr kdst;
1264 in6_splitscope(dst, &kdst, &scopeid);
1265 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1266 return (EHOSTUNREACH);
1271 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1272 fib6_free_nh_ext(fibnum, &nh6);
1278 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1279 * and cached data in @ro_pmtu.
1280 * MTU from (successful) route lookup is saved (along with dst)
1281 * inside @ro_pmtu to avoid subsequent route lookups after packet
1282 * filter processing.
1284 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1285 * Returns 0 on success.
1288 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1289 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1290 int *alwaysfragp, u_int fibnum, u_int proto)
1292 struct nhop6_basic nh6;
1293 struct in6_addr kdst;
1295 struct sockaddr_in6 *sa6_dst;
1302 * Here ro_pmtu has final destination address, while
1303 * ro might represent immediate destination.
1304 * Use ro_pmtu destination since mtu might differ.
1306 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1307 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1308 ro_pmtu->ro_mtu = 0;
1310 if (ro_pmtu->ro_mtu == 0) {
1311 bzero(sa6_dst, sizeof(*sa6_dst));
1312 sa6_dst->sin6_family = AF_INET6;
1313 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1314 sa6_dst->sin6_addr = *dst;
1316 in6_splitscope(dst, &kdst, &scopeid);
1317 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1319 ro_pmtu->ro_mtu = nh6.nh_mtu;
1322 mtu = ro_pmtu->ro_mtu;
1326 mtu = ro_pmtu->ro_rt->rt_mtu;
1328 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1332 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1333 * hostcache data for @dst.
1334 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1336 * Returns 0 on success.
1339 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1340 u_long *mtup, int *alwaysfragp, u_int proto)
1348 struct in_conninfo inc;
1350 bzero(&inc, sizeof(inc));
1351 inc.inc_flags |= INC_ISIPV6;
1352 inc.inc6_faddr = *dst;
1354 ifmtu = IN6_LINKMTU(ifp);
1356 /* TCP is known to react to pmtu changes so skip hc */
1357 if (proto != IPPROTO_TCP)
1358 mtu = tcp_hc_getmtu(&inc);
1361 mtu = min(mtu, rt_mtu);
1366 else if (mtu < IPV6_MMTU) {
1368 * RFC2460 section 5, last paragraph:
1369 * if we record ICMPv6 too big message with
1370 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1371 * or smaller, with framgent header attached.
1372 * (fragment header is needed regardless from the
1373 * packet size, for translators to identify packets)
1379 mtu = IN6_LINKMTU(ifp);
1381 error = EHOSTUNREACH; /* XXX */
1385 *alwaysfragp = alwaysfrag;
1390 * IP6 socket option processing.
1393 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1395 int optdatalen, uproto;
1397 struct inpcb *in6p = sotoinpcb(so);
1399 int level, op, optname;
1403 uint32_t rss_bucket;
1408 * Don't use more than a quarter of mbuf clusters. N.B.:
1409 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1410 * on LP64 architectures, so cast to u_long to avoid undefined
1411 * behavior. ILP32 architectures cannot have nmbclusters
1412 * large enough to overflow for other reasons.
1414 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1416 level = sopt->sopt_level;
1417 op = sopt->sopt_dir;
1418 optname = sopt->sopt_name;
1419 optlen = sopt->sopt_valsize;
1423 uproto = (int)so->so_proto->pr_protocol;
1425 if (level != IPPROTO_IPV6) {
1428 if (sopt->sopt_level == SOL_SOCKET &&
1429 sopt->sopt_dir == SOPT_SET) {
1430 switch (sopt->sopt_name) {
1433 if ((so->so_options & SO_REUSEADDR) != 0)
1434 in6p->inp_flags2 |= INP_REUSEADDR;
1436 in6p->inp_flags2 &= ~INP_REUSEADDR;
1442 if ((so->so_options & SO_REUSEPORT) != 0)
1443 in6p->inp_flags2 |= INP_REUSEPORT;
1445 in6p->inp_flags2 &= ~INP_REUSEPORT;
1451 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1459 } else { /* level == IPPROTO_IPV6 */
1464 case IPV6_2292PKTOPTIONS:
1465 #ifdef IPV6_PKTOPTIONS
1466 case IPV6_PKTOPTIONS:
1471 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1472 printf("ip6_ctloutput: mbuf limit hit\n");
1477 error = soopt_getm(sopt, &m); /* XXX */
1480 error = soopt_mcopyin(sopt, m); /* XXX */
1483 error = ip6_pcbopts(&in6p->in6p_outputopts,
1485 m_freem(m); /* XXX */
1490 * Use of some Hop-by-Hop options or some
1491 * Destination options, might require special
1492 * privilege. That is, normal applications
1493 * (without special privilege) might be forbidden
1494 * from setting certain options in outgoing packets,
1495 * and might never see certain options in received
1496 * packets. [RFC 2292 Section 6]
1497 * KAME specific note:
1498 * KAME prevents non-privileged users from sending or
1499 * receiving ANY hbh/dst options in order to avoid
1500 * overhead of parsing options in the kernel.
1502 case IPV6_RECVHOPOPTS:
1503 case IPV6_RECVDSTOPTS:
1504 case IPV6_RECVRTHDRDSTOPTS:
1506 error = priv_check(td,
1507 PRIV_NETINET_SETHDROPTS);
1512 case IPV6_UNICAST_HOPS:
1515 case IPV6_RECVPKTINFO:
1516 case IPV6_RECVHOPLIMIT:
1517 case IPV6_RECVRTHDR:
1518 case IPV6_RECVPATHMTU:
1519 case IPV6_RECVTCLASS:
1520 case IPV6_RECVFLOWID:
1522 case IPV6_RECVRSSBUCKETID:
1525 case IPV6_AUTOFLOWLABEL:
1527 case IPV6_BINDMULTI:
1529 case IPV6_RSS_LISTEN_BUCKET:
1531 if (optname == IPV6_BINDANY && td != NULL) {
1532 error = priv_check(td,
1533 PRIV_NETINET_BINDANY);
1538 if (optlen != sizeof(int)) {
1542 error = sooptcopyin(sopt, &optval,
1543 sizeof optval, sizeof optval);
1548 case IPV6_UNICAST_HOPS:
1549 if (optval < -1 || optval >= 256)
1552 /* -1 = kernel default */
1553 in6p->in6p_hops = optval;
1554 if ((in6p->inp_vflag &
1556 in6p->inp_ip_ttl = optval;
1559 #define OPTSET(bit) \
1563 in6p->inp_flags |= (bit); \
1565 in6p->inp_flags &= ~(bit); \
1566 INP_WUNLOCK(in6p); \
1567 } while (/*CONSTCOND*/ 0)
1568 #define OPTSET2292(bit) \
1571 in6p->inp_flags |= IN6P_RFC2292; \
1573 in6p->inp_flags |= (bit); \
1575 in6p->inp_flags &= ~(bit); \
1576 INP_WUNLOCK(in6p); \
1577 } while (/*CONSTCOND*/ 0)
1578 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1580 #define OPTSET2(bit, val) do { \
1583 in6p->inp_flags2 |= bit; \
1585 in6p->inp_flags2 &= ~bit; \
1586 INP_WUNLOCK(in6p); \
1588 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1590 case IPV6_RECVPKTINFO:
1591 /* cannot mix with RFC2292 */
1592 if (OPTBIT(IN6P_RFC2292)) {
1596 OPTSET(IN6P_PKTINFO);
1601 struct ip6_pktopts **optp;
1603 /* cannot mix with RFC2292 */
1604 if (OPTBIT(IN6P_RFC2292)) {
1608 optp = &in6p->in6p_outputopts;
1609 error = ip6_pcbopt(IPV6_HOPLIMIT,
1610 (u_char *)&optval, sizeof(optval),
1611 optp, (td != NULL) ? td->td_ucred :
1616 case IPV6_RECVHOPLIMIT:
1617 /* cannot mix with RFC2292 */
1618 if (OPTBIT(IN6P_RFC2292)) {
1622 OPTSET(IN6P_HOPLIMIT);
1625 case IPV6_RECVHOPOPTS:
1626 /* cannot mix with RFC2292 */
1627 if (OPTBIT(IN6P_RFC2292)) {
1631 OPTSET(IN6P_HOPOPTS);
1634 case IPV6_RECVDSTOPTS:
1635 /* cannot mix with RFC2292 */
1636 if (OPTBIT(IN6P_RFC2292)) {
1640 OPTSET(IN6P_DSTOPTS);
1643 case IPV6_RECVRTHDRDSTOPTS:
1644 /* cannot mix with RFC2292 */
1645 if (OPTBIT(IN6P_RFC2292)) {
1649 OPTSET(IN6P_RTHDRDSTOPTS);
1652 case IPV6_RECVRTHDR:
1653 /* cannot mix with RFC2292 */
1654 if (OPTBIT(IN6P_RFC2292)) {
1661 case IPV6_RECVPATHMTU:
1663 * We ignore this option for TCP
1665 * (RFC3542 leaves this case
1668 if (uproto != IPPROTO_TCP)
1672 case IPV6_RECVFLOWID:
1673 OPTSET2(INP_RECVFLOWID, optval);
1677 case IPV6_RECVRSSBUCKETID:
1678 OPTSET2(INP_RECVRSSBUCKETID, optval);
1684 * make setsockopt(IPV6_V6ONLY)
1685 * available only prior to bind(2).
1686 * see ipng mailing list, Jun 22 2001.
1688 if (in6p->inp_lport ||
1689 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1693 OPTSET(IN6P_IPV6_V6ONLY);
1695 in6p->inp_vflag &= ~INP_IPV4;
1697 in6p->inp_vflag |= INP_IPV4;
1699 case IPV6_RECVTCLASS:
1700 /* cannot mix with RFC2292 XXX */
1701 if (OPTBIT(IN6P_RFC2292)) {
1705 OPTSET(IN6P_TCLASS);
1707 case IPV6_AUTOFLOWLABEL:
1708 OPTSET(IN6P_AUTOFLOWLABEL);
1712 OPTSET(INP_BINDANY);
1715 case IPV6_BINDMULTI:
1716 OPTSET2(INP_BINDMULTI, optval);
1719 case IPV6_RSS_LISTEN_BUCKET:
1720 if ((optval >= 0) &&
1721 (optval < rss_getnumbuckets())) {
1722 in6p->inp_rss_listen_bucket = optval;
1723 OPTSET2(INP_RSS_BUCKET_SET, 1);
1734 case IPV6_USE_MIN_MTU:
1735 case IPV6_PREFER_TEMPADDR:
1736 if (optlen != sizeof(optval)) {
1740 error = sooptcopyin(sopt, &optval,
1741 sizeof optval, sizeof optval);
1745 struct ip6_pktopts **optp;
1746 optp = &in6p->in6p_outputopts;
1747 error = ip6_pcbopt(optname,
1748 (u_char *)&optval, sizeof(optval),
1749 optp, (td != NULL) ? td->td_ucred :
1754 case IPV6_2292PKTINFO:
1755 case IPV6_2292HOPLIMIT:
1756 case IPV6_2292HOPOPTS:
1757 case IPV6_2292DSTOPTS:
1758 case IPV6_2292RTHDR:
1760 if (optlen != sizeof(int)) {
1764 error = sooptcopyin(sopt, &optval,
1765 sizeof optval, sizeof optval);
1769 case IPV6_2292PKTINFO:
1770 OPTSET2292(IN6P_PKTINFO);
1772 case IPV6_2292HOPLIMIT:
1773 OPTSET2292(IN6P_HOPLIMIT);
1775 case IPV6_2292HOPOPTS:
1777 * Check super-user privilege.
1778 * See comments for IPV6_RECVHOPOPTS.
1781 error = priv_check(td,
1782 PRIV_NETINET_SETHDROPTS);
1786 OPTSET2292(IN6P_HOPOPTS);
1788 case IPV6_2292DSTOPTS:
1790 error = priv_check(td,
1791 PRIV_NETINET_SETHDROPTS);
1795 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1797 case IPV6_2292RTHDR:
1798 OPTSET2292(IN6P_RTHDR);
1806 case IPV6_RTHDRDSTOPTS:
1809 /* new advanced API (RFC3542) */
1811 u_char optbuf_storage[MCLBYTES];
1813 struct ip6_pktopts **optp;
1815 /* cannot mix with RFC2292 */
1816 if (OPTBIT(IN6P_RFC2292)) {
1822 * We only ensure valsize is not too large
1823 * here. Further validation will be done
1826 error = sooptcopyin(sopt, optbuf_storage,
1827 sizeof(optbuf_storage), 0);
1830 optlen = sopt->sopt_valsize;
1831 optbuf = optbuf_storage;
1832 optp = &in6p->in6p_outputopts;
1833 error = ip6_pcbopt(optname, optbuf, optlen,
1834 optp, (td != NULL) ? td->td_ucred : NULL,
1840 case IPV6_MULTICAST_IF:
1841 case IPV6_MULTICAST_HOPS:
1842 case IPV6_MULTICAST_LOOP:
1843 case IPV6_JOIN_GROUP:
1844 case IPV6_LEAVE_GROUP:
1846 case MCAST_BLOCK_SOURCE:
1847 case MCAST_UNBLOCK_SOURCE:
1848 case MCAST_JOIN_GROUP:
1849 case MCAST_LEAVE_GROUP:
1850 case MCAST_JOIN_SOURCE_GROUP:
1851 case MCAST_LEAVE_SOURCE_GROUP:
1852 error = ip6_setmoptions(in6p, sopt);
1855 case IPV6_PORTRANGE:
1856 error = sooptcopyin(sopt, &optval,
1857 sizeof optval, sizeof optval);
1863 case IPV6_PORTRANGE_DEFAULT:
1864 in6p->inp_flags &= ~(INP_LOWPORT);
1865 in6p->inp_flags &= ~(INP_HIGHPORT);
1868 case IPV6_PORTRANGE_HIGH:
1869 in6p->inp_flags &= ~(INP_LOWPORT);
1870 in6p->inp_flags |= INP_HIGHPORT;
1873 case IPV6_PORTRANGE_LOW:
1874 in6p->inp_flags &= ~(INP_HIGHPORT);
1875 in6p->inp_flags |= INP_LOWPORT;
1885 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1886 case IPV6_IPSEC_POLICY:
1887 if (IPSEC_ENABLED(ipv6)) {
1888 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
1895 error = ENOPROTOOPT;
1903 case IPV6_2292PKTOPTIONS:
1904 #ifdef IPV6_PKTOPTIONS
1905 case IPV6_PKTOPTIONS:
1908 * RFC3542 (effectively) deprecated the
1909 * semantics of the 2292-style pktoptions.
1910 * Since it was not reliable in nature (i.e.,
1911 * applications had to expect the lack of some
1912 * information after all), it would make sense
1913 * to simplify this part by always returning
1916 sopt->sopt_valsize = 0;
1919 case IPV6_RECVHOPOPTS:
1920 case IPV6_RECVDSTOPTS:
1921 case IPV6_RECVRTHDRDSTOPTS:
1922 case IPV6_UNICAST_HOPS:
1923 case IPV6_RECVPKTINFO:
1924 case IPV6_RECVHOPLIMIT:
1925 case IPV6_RECVRTHDR:
1926 case IPV6_RECVPATHMTU:
1929 case IPV6_PORTRANGE:
1930 case IPV6_RECVTCLASS:
1931 case IPV6_AUTOFLOWLABEL:
1935 case IPV6_RECVFLOWID:
1937 case IPV6_RSSBUCKETID:
1938 case IPV6_RECVRSSBUCKETID:
1940 case IPV6_BINDMULTI:
1943 case IPV6_RECVHOPOPTS:
1944 optval = OPTBIT(IN6P_HOPOPTS);
1947 case IPV6_RECVDSTOPTS:
1948 optval = OPTBIT(IN6P_DSTOPTS);
1951 case IPV6_RECVRTHDRDSTOPTS:
1952 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1955 case IPV6_UNICAST_HOPS:
1956 optval = in6p->in6p_hops;
1959 case IPV6_RECVPKTINFO:
1960 optval = OPTBIT(IN6P_PKTINFO);
1963 case IPV6_RECVHOPLIMIT:
1964 optval = OPTBIT(IN6P_HOPLIMIT);
1967 case IPV6_RECVRTHDR:
1968 optval = OPTBIT(IN6P_RTHDR);
1971 case IPV6_RECVPATHMTU:
1972 optval = OPTBIT(IN6P_MTU);
1976 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1979 case IPV6_PORTRANGE:
1982 flags = in6p->inp_flags;
1983 if (flags & INP_HIGHPORT)
1984 optval = IPV6_PORTRANGE_HIGH;
1985 else if (flags & INP_LOWPORT)
1986 optval = IPV6_PORTRANGE_LOW;
1991 case IPV6_RECVTCLASS:
1992 optval = OPTBIT(IN6P_TCLASS);
1995 case IPV6_AUTOFLOWLABEL:
1996 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2000 optval = OPTBIT(INP_BINDANY);
2004 optval = in6p->inp_flowid;
2008 optval = in6p->inp_flowtype;
2011 case IPV6_RECVFLOWID:
2012 optval = OPTBIT2(INP_RECVFLOWID);
2015 case IPV6_RSSBUCKETID:
2017 rss_hash2bucket(in6p->inp_flowid,
2021 optval = rss_bucket;
2026 case IPV6_RECVRSSBUCKETID:
2027 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2031 case IPV6_BINDMULTI:
2032 optval = OPTBIT2(INP_BINDMULTI);
2038 error = sooptcopyout(sopt, &optval,
2045 struct ip6_mtuinfo mtuinfo;
2047 if (!(so->so_state & SS_ISCONNECTED))
2050 * XXX: we dot not consider the case of source
2051 * routing, or optional information to specify
2052 * the outgoing interface.
2054 error = ip6_getpmtu_ctl(so->so_fibnum,
2055 &in6p->in6p_faddr, &pmtu);
2058 if (pmtu > IPV6_MAXPACKET)
2059 pmtu = IPV6_MAXPACKET;
2061 bzero(&mtuinfo, sizeof(mtuinfo));
2062 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2063 optdata = (void *)&mtuinfo;
2064 optdatalen = sizeof(mtuinfo);
2065 error = sooptcopyout(sopt, optdata,
2070 case IPV6_2292PKTINFO:
2071 case IPV6_2292HOPLIMIT:
2072 case IPV6_2292HOPOPTS:
2073 case IPV6_2292RTHDR:
2074 case IPV6_2292DSTOPTS:
2076 case IPV6_2292PKTINFO:
2077 optval = OPTBIT(IN6P_PKTINFO);
2079 case IPV6_2292HOPLIMIT:
2080 optval = OPTBIT(IN6P_HOPLIMIT);
2082 case IPV6_2292HOPOPTS:
2083 optval = OPTBIT(IN6P_HOPOPTS);
2085 case IPV6_2292RTHDR:
2086 optval = OPTBIT(IN6P_RTHDR);
2088 case IPV6_2292DSTOPTS:
2089 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2092 error = sooptcopyout(sopt, &optval,
2099 case IPV6_RTHDRDSTOPTS:
2103 case IPV6_USE_MIN_MTU:
2104 case IPV6_PREFER_TEMPADDR:
2105 error = ip6_getpcbopt(in6p->in6p_outputopts,
2109 case IPV6_MULTICAST_IF:
2110 case IPV6_MULTICAST_HOPS:
2111 case IPV6_MULTICAST_LOOP:
2113 error = ip6_getmoptions(in6p, sopt);
2116 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2117 case IPV6_IPSEC_POLICY:
2118 if (IPSEC_ENABLED(ipv6)) {
2119 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
2125 error = ENOPROTOOPT;
2135 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2137 int error = 0, optval, optlen;
2138 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2139 struct inpcb *in6p = sotoinpcb(so);
2140 int level, op, optname;
2142 level = sopt->sopt_level;
2143 op = sopt->sopt_dir;
2144 optname = sopt->sopt_name;
2145 optlen = sopt->sopt_valsize;
2147 if (level != IPPROTO_IPV6) {
2154 * For ICMPv6 sockets, no modification allowed for checksum
2155 * offset, permit "no change" values to help existing apps.
2157 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2158 * for an ICMPv6 socket will fail."
2159 * The current behavior does not meet RFC3542.
2163 if (optlen != sizeof(int)) {
2167 error = sooptcopyin(sopt, &optval, sizeof(optval),
2171 if ((optval % 2) != 0) {
2172 /* the API assumes even offset values */
2174 } else if (so->so_proto->pr_protocol ==
2176 if (optval != icmp6off)
2179 in6p->in6p_cksum = optval;
2183 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2186 optval = in6p->in6p_cksum;
2188 error = sooptcopyout(sopt, &optval, sizeof(optval));
2198 error = ENOPROTOOPT;
2206 * Set up IP6 options in pcb for insertion in output packets or
2207 * specifying behavior of outgoing packets.
2210 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2211 struct socket *so, struct sockopt *sopt)
2213 struct ip6_pktopts *opt = *pktopt;
2215 struct thread *td = sopt->sopt_td;
2217 /* turn off any old options. */
2220 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2221 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2222 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2223 printf("ip6_pcbopts: all specified options are cleared.\n");
2225 ip6_clearpktopts(opt, -1);
2227 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2230 if (!m || m->m_len == 0) {
2232 * Only turning off any previous options, regardless of
2233 * whether the opt is just created or given.
2235 free(opt, M_IP6OPT);
2239 /* set options specified by user. */
2240 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2241 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2242 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2243 free(opt, M_IP6OPT);
2251 * initialize ip6_pktopts. beware that there are non-zero default values in
2255 ip6_initpktopts(struct ip6_pktopts *opt)
2258 bzero(opt, sizeof(*opt));
2259 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2260 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2261 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2262 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2266 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2267 struct ucred *cred, int uproto)
2269 struct ip6_pktopts *opt;
2271 if (*pktopt == NULL) {
2272 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2274 ip6_initpktopts(*pktopt);
2278 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2282 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2284 void *optdata = NULL;
2286 struct ip6_ext *ip6e;
2288 struct in6_pktinfo null_pktinfo;
2289 int deftclass = 0, on;
2290 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2291 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2295 optdata = (void *)&null_pktinfo;
2296 if (pktopt && pktopt->ip6po_pktinfo) {
2297 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2298 sizeof(null_pktinfo));
2299 in6_clearscope(&null_pktinfo.ipi6_addr);
2301 /* XXX: we don't have to do this every time... */
2302 bzero(&null_pktinfo, sizeof(null_pktinfo));
2304 optdatalen = sizeof(struct in6_pktinfo);
2307 if (pktopt && pktopt->ip6po_tclass >= 0)
2308 optdata = (void *)&pktopt->ip6po_tclass;
2310 optdata = (void *)&deftclass;
2311 optdatalen = sizeof(int);
2314 if (pktopt && pktopt->ip6po_hbh) {
2315 optdata = (void *)pktopt->ip6po_hbh;
2316 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2317 optdatalen = (ip6e->ip6e_len + 1) << 3;
2321 if (pktopt && pktopt->ip6po_rthdr) {
2322 optdata = (void *)pktopt->ip6po_rthdr;
2323 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2324 optdatalen = (ip6e->ip6e_len + 1) << 3;
2327 case IPV6_RTHDRDSTOPTS:
2328 if (pktopt && pktopt->ip6po_dest1) {
2329 optdata = (void *)pktopt->ip6po_dest1;
2330 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2331 optdatalen = (ip6e->ip6e_len + 1) << 3;
2335 if (pktopt && pktopt->ip6po_dest2) {
2336 optdata = (void *)pktopt->ip6po_dest2;
2337 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2338 optdatalen = (ip6e->ip6e_len + 1) << 3;
2342 if (pktopt && pktopt->ip6po_nexthop) {
2343 optdata = (void *)pktopt->ip6po_nexthop;
2344 optdatalen = pktopt->ip6po_nexthop->sa_len;
2347 case IPV6_USE_MIN_MTU:
2349 optdata = (void *)&pktopt->ip6po_minmtu;
2351 optdata = (void *)&defminmtu;
2352 optdatalen = sizeof(int);
2355 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2359 optdata = (void *)&on;
2360 optdatalen = sizeof(on);
2362 case IPV6_PREFER_TEMPADDR:
2364 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2366 optdata = (void *)&defpreftemp;
2367 optdatalen = sizeof(int);
2369 default: /* should not happen */
2371 panic("ip6_getpcbopt: unexpected option\n");
2373 return (ENOPROTOOPT);
2376 error = sooptcopyout(sopt, optdata, optdatalen);
2382 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2387 if (optname == -1 || optname == IPV6_PKTINFO) {
2388 if (pktopt->ip6po_pktinfo)
2389 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2390 pktopt->ip6po_pktinfo = NULL;
2392 if (optname == -1 || optname == IPV6_HOPLIMIT)
2393 pktopt->ip6po_hlim = -1;
2394 if (optname == -1 || optname == IPV6_TCLASS)
2395 pktopt->ip6po_tclass = -1;
2396 if (optname == -1 || optname == IPV6_NEXTHOP) {
2397 if (pktopt->ip6po_nextroute.ro_rt) {
2398 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2399 pktopt->ip6po_nextroute.ro_rt = NULL;
2401 if (pktopt->ip6po_nexthop)
2402 free(pktopt->ip6po_nexthop, M_IP6OPT);
2403 pktopt->ip6po_nexthop = NULL;
2405 if (optname == -1 || optname == IPV6_HOPOPTS) {
2406 if (pktopt->ip6po_hbh)
2407 free(pktopt->ip6po_hbh, M_IP6OPT);
2408 pktopt->ip6po_hbh = NULL;
2410 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2411 if (pktopt->ip6po_dest1)
2412 free(pktopt->ip6po_dest1, M_IP6OPT);
2413 pktopt->ip6po_dest1 = NULL;
2415 if (optname == -1 || optname == IPV6_RTHDR) {
2416 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2417 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2418 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2419 if (pktopt->ip6po_route.ro_rt) {
2420 RTFREE(pktopt->ip6po_route.ro_rt);
2421 pktopt->ip6po_route.ro_rt = NULL;
2424 if (optname == -1 || optname == IPV6_DSTOPTS) {
2425 if (pktopt->ip6po_dest2)
2426 free(pktopt->ip6po_dest2, M_IP6OPT);
2427 pktopt->ip6po_dest2 = NULL;
2431 #define PKTOPT_EXTHDRCPY(type) \
2434 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2435 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2436 if (dst->type == NULL)\
2438 bcopy(src->type, dst->type, hlen);\
2440 } while (/*CONSTCOND*/ 0)
2443 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2445 if (dst == NULL || src == NULL) {
2446 printf("ip6_clearpktopts: invalid argument\n");
2450 dst->ip6po_hlim = src->ip6po_hlim;
2451 dst->ip6po_tclass = src->ip6po_tclass;
2452 dst->ip6po_flags = src->ip6po_flags;
2453 dst->ip6po_minmtu = src->ip6po_minmtu;
2454 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2455 if (src->ip6po_pktinfo) {
2456 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2458 if (dst->ip6po_pktinfo == NULL)
2460 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2462 if (src->ip6po_nexthop) {
2463 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2465 if (dst->ip6po_nexthop == NULL)
2467 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2468 src->ip6po_nexthop->sa_len);
2470 PKTOPT_EXTHDRCPY(ip6po_hbh);
2471 PKTOPT_EXTHDRCPY(ip6po_dest1);
2472 PKTOPT_EXTHDRCPY(ip6po_dest2);
2473 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2477 ip6_clearpktopts(dst, -1);
2480 #undef PKTOPT_EXTHDRCPY
2482 struct ip6_pktopts *
2483 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2486 struct ip6_pktopts *dst;
2488 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2491 ip6_initpktopts(dst);
2493 if ((error = copypktopts(dst, src, canwait)) != 0) {
2494 free(dst, M_IP6OPT);
2502 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2507 ip6_clearpktopts(pktopt, -1);
2509 free(pktopt, M_IP6OPT);
2513 * Set IPv6 outgoing packet options based on advanced API.
2516 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2517 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2519 struct cmsghdr *cm = NULL;
2521 if (control == NULL || opt == NULL)
2524 ip6_initpktopts(opt);
2529 * If stickyopt is provided, make a local copy of the options
2530 * for this particular packet, then override them by ancillary
2532 * XXX: copypktopts() does not copy the cached route to a next
2533 * hop (if any). This is not very good in terms of efficiency,
2534 * but we can allow this since this option should be rarely
2537 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2542 * XXX: Currently, we assume all the optional information is stored
2545 if (control->m_next)
2548 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2549 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2552 if (control->m_len < CMSG_LEN(0))
2555 cm = mtod(control, struct cmsghdr *);
2556 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2558 if (cm->cmsg_level != IPPROTO_IPV6)
2561 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2562 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2571 * Set a particular packet option, as a sticky option or an ancillary data
2572 * item. "len" can be 0 only when it's a sticky option.
2573 * We have 4 cases of combination of "sticky" and "cmsg":
2574 * "sticky=0, cmsg=0": impossible
2575 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2576 * "sticky=1, cmsg=0": RFC3542 socket option
2577 * "sticky=1, cmsg=1": RFC2292 socket option
2580 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2581 struct ucred *cred, int sticky, int cmsg, int uproto)
2583 int minmtupolicy, preftemp;
2586 if (!sticky && !cmsg) {
2588 printf("ip6_setpktopt: impossible case\n");
2594 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2595 * not be specified in the context of RFC3542. Conversely,
2596 * RFC3542 types should not be specified in the context of RFC2292.
2600 case IPV6_2292PKTINFO:
2601 case IPV6_2292HOPLIMIT:
2602 case IPV6_2292NEXTHOP:
2603 case IPV6_2292HOPOPTS:
2604 case IPV6_2292DSTOPTS:
2605 case IPV6_2292RTHDR:
2606 case IPV6_2292PKTOPTIONS:
2607 return (ENOPROTOOPT);
2610 if (sticky && cmsg) {
2617 case IPV6_RTHDRDSTOPTS:
2619 case IPV6_USE_MIN_MTU:
2622 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2623 return (ENOPROTOOPT);
2628 case IPV6_2292PKTINFO:
2631 struct ifnet *ifp = NULL;
2632 struct in6_pktinfo *pktinfo;
2634 if (len != sizeof(struct in6_pktinfo))
2637 pktinfo = (struct in6_pktinfo *)buf;
2640 * An application can clear any sticky IPV6_PKTINFO option by
2641 * doing a "regular" setsockopt with ipi6_addr being
2642 * in6addr_any and ipi6_ifindex being zero.
2643 * [RFC 3542, Section 6]
2645 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2646 pktinfo->ipi6_ifindex == 0 &&
2647 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2648 ip6_clearpktopts(opt, optname);
2652 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2653 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2656 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2658 /* validate the interface index if specified. */
2659 if (pktinfo->ipi6_ifindex > V_if_index)
2661 if (pktinfo->ipi6_ifindex) {
2662 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2666 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2667 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2671 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2672 struct in6_ifaddr *ia;
2674 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2675 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2677 return (EADDRNOTAVAIL);
2678 ifa_free(&ia->ia_ifa);
2681 * We store the address anyway, and let in6_selectsrc()
2682 * validate the specified address. This is because ipi6_addr
2683 * may not have enough information about its scope zone, and
2684 * we may need additional information (such as outgoing
2685 * interface or the scope zone of a destination address) to
2686 * disambiguate the scope.
2687 * XXX: the delay of the validation may confuse the
2688 * application when it is used as a sticky option.
2690 if (opt->ip6po_pktinfo == NULL) {
2691 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2692 M_IP6OPT, M_NOWAIT);
2693 if (opt->ip6po_pktinfo == NULL)
2696 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2700 case IPV6_2292HOPLIMIT:
2706 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2707 * to simplify the ordering among hoplimit options.
2709 if (optname == IPV6_HOPLIMIT && sticky)
2710 return (ENOPROTOOPT);
2712 if (len != sizeof(int))
2715 if (*hlimp < -1 || *hlimp > 255)
2718 opt->ip6po_hlim = *hlimp;
2726 if (len != sizeof(int))
2728 tclass = *(int *)buf;
2729 if (tclass < -1 || tclass > 255)
2732 opt->ip6po_tclass = tclass;
2736 case IPV6_2292NEXTHOP:
2739 error = priv_check_cred(cred,
2740 PRIV_NETINET_SETHDROPTS, 0);
2745 if (len == 0) { /* just remove the option */
2746 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2750 /* check if cmsg_len is large enough for sa_len */
2751 if (len < sizeof(struct sockaddr) || len < *buf)
2754 switch (((struct sockaddr *)buf)->sa_family) {
2757 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2760 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2763 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2764 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2767 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2773 case AF_LINK: /* should eventually be supported */
2775 return (EAFNOSUPPORT);
2778 /* turn off the previous option, then set the new option. */
2779 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2780 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2781 if (opt->ip6po_nexthop == NULL)
2783 bcopy(buf, opt->ip6po_nexthop, *buf);
2786 case IPV6_2292HOPOPTS:
2789 struct ip6_hbh *hbh;
2793 * XXX: We don't allow a non-privileged user to set ANY HbH
2794 * options, since per-option restriction has too much
2798 error = priv_check_cred(cred,
2799 PRIV_NETINET_SETHDROPTS, 0);
2805 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2806 break; /* just remove the option */
2809 /* message length validation */
2810 if (len < sizeof(struct ip6_hbh))
2812 hbh = (struct ip6_hbh *)buf;
2813 hbhlen = (hbh->ip6h_len + 1) << 3;
2817 /* turn off the previous option, then set the new option. */
2818 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2819 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2820 if (opt->ip6po_hbh == NULL)
2822 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2827 case IPV6_2292DSTOPTS:
2829 case IPV6_RTHDRDSTOPTS:
2831 struct ip6_dest *dest, **newdest = NULL;
2834 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2835 error = priv_check_cred(cred,
2836 PRIV_NETINET_SETHDROPTS, 0);
2842 ip6_clearpktopts(opt, optname);
2843 break; /* just remove the option */
2846 /* message length validation */
2847 if (len < sizeof(struct ip6_dest))
2849 dest = (struct ip6_dest *)buf;
2850 destlen = (dest->ip6d_len + 1) << 3;
2855 * Determine the position that the destination options header
2856 * should be inserted; before or after the routing header.
2859 case IPV6_2292DSTOPTS:
2861 * The old advacned API is ambiguous on this point.
2862 * Our approach is to determine the position based
2863 * according to the existence of a routing header.
2864 * Note, however, that this depends on the order of the
2865 * extension headers in the ancillary data; the 1st
2866 * part of the destination options header must appear
2867 * before the routing header in the ancillary data,
2869 * RFC3542 solved the ambiguity by introducing
2870 * separate ancillary data or option types.
2872 if (opt->ip6po_rthdr == NULL)
2873 newdest = &opt->ip6po_dest1;
2875 newdest = &opt->ip6po_dest2;
2877 case IPV6_RTHDRDSTOPTS:
2878 newdest = &opt->ip6po_dest1;
2881 newdest = &opt->ip6po_dest2;
2885 /* turn off the previous option, then set the new option. */
2886 ip6_clearpktopts(opt, optname);
2887 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2888 if (*newdest == NULL)
2890 bcopy(dest, *newdest, destlen);
2895 case IPV6_2292RTHDR:
2898 struct ip6_rthdr *rth;
2902 ip6_clearpktopts(opt, IPV6_RTHDR);
2903 break; /* just remove the option */
2906 /* message length validation */
2907 if (len < sizeof(struct ip6_rthdr))
2909 rth = (struct ip6_rthdr *)buf;
2910 rthlen = (rth->ip6r_len + 1) << 3;
2914 switch (rth->ip6r_type) {
2915 case IPV6_RTHDR_TYPE_0:
2916 if (rth->ip6r_len == 0) /* must contain one addr */
2918 if (rth->ip6r_len % 2) /* length must be even */
2920 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2924 return (EINVAL); /* not supported */
2927 /* turn off the previous option */
2928 ip6_clearpktopts(opt, IPV6_RTHDR);
2929 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2930 if (opt->ip6po_rthdr == NULL)
2932 bcopy(rth, opt->ip6po_rthdr, rthlen);
2937 case IPV6_USE_MIN_MTU:
2938 if (len != sizeof(int))
2940 minmtupolicy = *(int *)buf;
2941 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2942 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2943 minmtupolicy != IP6PO_MINMTU_ALL) {
2946 opt->ip6po_minmtu = minmtupolicy;
2950 if (len != sizeof(int))
2953 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2955 * we ignore this option for TCP sockets.
2956 * (RFC3542 leaves this case unspecified.)
2958 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2960 opt->ip6po_flags |= IP6PO_DONTFRAG;
2963 case IPV6_PREFER_TEMPADDR:
2964 if (len != sizeof(int))
2966 preftemp = *(int *)buf;
2967 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2968 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2969 preftemp != IP6PO_TEMPADDR_PREFER) {
2972 opt->ip6po_prefer_tempaddr = preftemp;
2976 return (ENOPROTOOPT);
2977 } /* end of switch */
2983 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2984 * packet to the input queue of a specified interface. Note that this
2985 * calls the output routine of the loopback "driver", but with an interface
2986 * pointer that might NOT be &loif -- easier than replicating that code here.
2989 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
2992 struct ip6_hdr *ip6;
2994 copym = m_copy(m, 0, M_COPYALL);
2999 * Make sure to deep-copy IPv6 header portion in case the data
3000 * is in an mbuf cluster, so that we can safely override the IPv6
3001 * header portion later.
3003 if (!M_WRITABLE(copym) ||
3004 copym->m_len < sizeof(struct ip6_hdr)) {
3005 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3009 ip6 = mtod(copym, struct ip6_hdr *);
3011 * clear embedded scope identifiers if necessary.
3012 * in6_clearscope will touch the addresses only when necessary.
3014 in6_clearscope(&ip6->ip6_src);
3015 in6_clearscope(&ip6->ip6_dst);
3016 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3017 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3019 copym->m_pkthdr.csum_data = 0xffff;
3021 if_simloop(ifp, copym, AF_INET6, 0);
3025 * Chop IPv6 header off from the payload.
3028 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3031 struct ip6_hdr *ip6;
3033 ip6 = mtod(m, struct ip6_hdr *);
3034 if (m->m_len > sizeof(*ip6)) {
3035 mh = m_gethdr(M_NOWAIT, MT_DATA);
3040 m_move_pkthdr(mh, m);
3041 M_ALIGN(mh, sizeof(*ip6));
3042 m->m_len -= sizeof(*ip6);
3043 m->m_data += sizeof(*ip6);
3046 m->m_len = sizeof(*ip6);
3047 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3049 exthdrs->ip6e_ip6 = m;
3054 * Compute IPv6 extension header length.
3057 ip6_optlen(struct inpcb *in6p)
3061 if (!in6p->in6p_outputopts)
3066 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3068 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3069 if (in6p->in6p_outputopts->ip6po_rthdr)
3070 /* dest1 is valid with rthdr only */
3071 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3072 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3073 len += elen(in6p->in6p_outputopts->ip6po_dest2);