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/netisr.h>
91 #include <net/route.h>
93 #include <net/rss_config.h>
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <netinet/ip_var.h>
99 #include <netinet6/in6_fib.h>
100 #include <netinet6/in6_var.h>
101 #include <netinet/ip6.h>
102 #include <netinet/icmp6.h>
103 #include <netinet6/ip6_var.h>
104 #include <netinet/in_pcb.h>
105 #include <netinet/tcp_var.h>
106 #include <netinet6/nd6.h>
107 #include <netinet6/in6_rss.h>
110 #include <netipsec/ipsec.h>
111 #include <netipsec/ipsec6.h>
112 #include <netipsec/key.h>
113 #include <netinet6/ip6_ipsec.h>
116 #include <netinet/sctp.h>
117 #include <netinet/sctp_crc32.h>
120 #include <netinet6/ip6protosw.h>
121 #include <netinet6/scope6_var.h>
124 #include <net/flowtable.h>
127 extern int in6_mcast_loop;
130 struct mbuf *ip6e_ip6;
131 struct mbuf *ip6e_hbh;
132 struct mbuf *ip6e_dest1;
133 struct mbuf *ip6e_rthdr;
134 struct mbuf *ip6e_dest2;
137 static MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
139 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
140 struct ucred *, int);
141 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
142 struct socket *, struct sockopt *);
143 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
144 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
145 struct ucred *, int, int, int);
147 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
148 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
150 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
151 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
152 static int ip6_getpmtu(struct route_in6 *, int,
153 struct ifnet *, const struct in6_addr *, u_long *, int *, u_int);
154 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
156 static int ip6_getpmtu_ctl(u_int, const struct in6_addr *, u_long *);
157 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
161 * Make an extension header from option data. hp is the source, and
162 * mp is the destination.
164 #define MAKE_EXTHDR(hp, mp) \
167 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
168 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
169 ((eh)->ip6e_len + 1) << 3); \
173 } while (/*CONSTCOND*/ 0)
176 * Form a chain of extension headers.
177 * m is the extension header mbuf
178 * mp is the previous mbuf in the chain
179 * p is the next header
180 * i is the type of option.
182 #define MAKE_CHAIN(m, mp, p, i)\
186 panic("assumption failed: hdr not split"); \
187 *mtod((m), u_char *) = *(p);\
189 p = mtod((m), u_char *);\
190 (m)->m_next = (mp)->m_next;\
194 } while (/*CONSTCOND*/ 0)
197 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
201 csum = in_cksum_skip(m, offset + plen, offset);
202 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
204 offset += m->m_pkthdr.csum_data; /* checksum offset */
206 if (offset + sizeof(u_short) > m->m_len) {
207 printf("%s: delayed m_pullup, m->len: %d plen %u off %u "
208 "csum_flags=%b\n", __func__, m->m_len, plen, offset,
209 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
211 * XXX this should not happen, but if it does, the correct
212 * behavior may be to insert the checksum in the appropriate
213 * next mbuf in the chain.
217 *(u_short *)(m->m_data + offset) = csum;
221 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
222 int mtu, uint32_t id)
224 struct mbuf *m, **mnext, *m_frgpart;
225 struct ip6_hdr *ip6, *mhip6;
226 struct ip6_frag *ip6f;
229 int tlen = m0->m_pkthdr.len;
232 ip6 = mtod(m, struct ip6_hdr *);
233 mnext = &m->m_nextpkt;
235 for (off = hlen; off < tlen; off += mtu) {
236 m = m_gethdr(M_NOWAIT, MT_DATA);
238 IP6STAT_INC(ip6s_odropped);
241 m->m_flags = m0->m_flags & M_COPYFLAGS;
243 mnext = &m->m_nextpkt;
244 m->m_data += max_linkhdr;
245 mhip6 = mtod(m, struct ip6_hdr *);
247 m->m_len = sizeof(*mhip6);
248 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
250 IP6STAT_INC(ip6s_odropped);
253 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
254 if (off + mtu >= tlen)
257 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
258 mhip6->ip6_plen = htons((u_short)(mtu + hlen +
259 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
260 if ((m_frgpart = m_copy(m0, off, mtu)) == NULL) {
261 IP6STAT_INC(ip6s_odropped);
265 m->m_pkthdr.len = mtu + hlen + sizeof(*ip6f);
266 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
267 m->m_pkthdr.rcvif = NULL;
268 ip6f->ip6f_reserved = 0;
269 ip6f->ip6f_ident = id;
270 ip6f->ip6f_nxt = nextproto;
271 IP6STAT_INC(ip6s_ofragments);
272 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
279 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
280 * header (with pri, len, nxt, hlim, src, dst).
281 * This function may modify ver and hlim only.
282 * The mbuf chain containing the packet will be freed.
283 * The mbuf opt, if present, will not be freed.
284 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
285 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
286 * then result of route lookup is stored in ro->ro_rt.
288 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
289 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
292 * ifpp - XXX: just for statistics
295 * XXX TODO: no flowid is assigned for outbound flows?
298 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
299 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
300 struct ifnet **ifpp, struct inpcb *inp)
303 struct ifnet *ifp, *origifp;
305 struct mbuf *mprev = NULL;
307 struct route_in6 ip6route;
308 struct rtentry *rt = NULL;
309 struct sockaddr_in6 *dst, src_sa, dst_sa;
310 struct in6_addr odst;
312 struct in6_ifaddr *ia = NULL;
314 int alwaysfrag, dontfrag;
315 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
316 struct ip6_exthdrs exthdrs;
317 struct in6_addr src0, dst0;
319 struct route_in6 *ro_pmtu = NULL;
324 struct m_tag *fwd_tag = NULL;
328 M_SETFIB(m, inp->inp_inc.inc_fibnum);
329 if ((flags & IP_NODEFAULTFLOWID) == 0) {
330 /* unconditionally set flowid */
331 m->m_pkthdr.flowid = inp->inp_flowid;
332 M_HASHTYPE_SET(m, inp->inp_flowtype);
336 bzero(&exthdrs, sizeof(exthdrs));
338 /* Hop-by-Hop options header */
339 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
340 /* Destination options header(1st part) */
341 if (opt->ip6po_rthdr) {
343 * Destination options header(1st part)
344 * This only makes sense with a routing header.
345 * See Section 9.2 of RFC 3542.
346 * Disabling this part just for MIP6 convenience is
347 * a bad idea. We need to think carefully about a
348 * way to make the advanced API coexist with MIP6
349 * options, which might automatically be inserted in
352 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
355 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
356 /* Destination options header(2nd part) */
357 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
362 * IPSec checking which handles several cases.
363 * FAST IPSEC: We re-injected the packet.
364 * XXX: need scope argument.
366 switch(ip6_ipsec_output(&m, inp, &error))
368 case 1: /* Bad packet */
370 case -1: /* IPSec done */
372 case 0: /* No IPSec */
379 * Calculate the total length of the extension header chain.
380 * Keep the length of the unfragmentable part for fragmentation.
383 if (exthdrs.ip6e_hbh)
384 optlen += exthdrs.ip6e_hbh->m_len;
385 if (exthdrs.ip6e_dest1)
386 optlen += exthdrs.ip6e_dest1->m_len;
387 if (exthdrs.ip6e_rthdr)
388 optlen += exthdrs.ip6e_rthdr->m_len;
389 unfragpartlen = optlen + sizeof(struct ip6_hdr);
391 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
392 if (exthdrs.ip6e_dest2)
393 optlen += exthdrs.ip6e_dest2->m_len;
396 * If there is at least one extension header,
397 * separate IP6 header from the payload.
399 if (optlen && !hdrsplit) {
400 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
404 m = exthdrs.ip6e_ip6;
408 ip6 = mtod(m, struct ip6_hdr *);
410 /* adjust mbuf packet header length */
411 m->m_pkthdr.len += optlen;
412 plen = m->m_pkthdr.len - sizeof(*ip6);
414 /* If this is a jumbo payload, insert a jumbo payload option. */
415 if (plen > IPV6_MAXPACKET) {
417 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
421 m = exthdrs.ip6e_ip6;
425 ip6 = mtod(m, struct ip6_hdr *);
426 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
430 ip6->ip6_plen = htons(plen);
433 * Concatenate headers and fill in next header fields.
434 * Here we have, on "m"
436 * and we insert headers accordingly. Finally, we should be getting:
437 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
439 * during the header composing process, "m" points to IPv6 header.
440 * "mprev" points to an extension header prior to esp.
442 u_char *nexthdrp = &ip6->ip6_nxt;
446 * we treat dest2 specially. this makes IPsec processing
447 * much easier. the goal here is to make mprev point the
448 * mbuf prior to dest2.
450 * result: IPv6 dest2 payload
451 * m and mprev will point to IPv6 header.
453 if (exthdrs.ip6e_dest2) {
455 panic("assumption failed: hdr not split");
456 exthdrs.ip6e_dest2->m_next = m->m_next;
457 m->m_next = exthdrs.ip6e_dest2;
458 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
459 ip6->ip6_nxt = IPPROTO_DSTOPTS;
463 * result: IPv6 hbh dest1 rthdr dest2 payload
464 * m will point to IPv6 header. mprev will point to the
465 * extension header prior to dest2 (rthdr in the above case).
467 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
468 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
470 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
474 * If there is a routing header, discard the packet.
476 if (exthdrs.ip6e_rthdr) {
481 /* Source address validation */
482 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
483 (flags & IPV6_UNSPECSRC) == 0) {
485 IP6STAT_INC(ip6s_badscope);
488 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
490 IP6STAT_INC(ip6s_badscope);
494 IP6STAT_INC(ip6s_localout);
501 bzero((caddr_t)ro, sizeof(*ro));
503 ro->ro_flags |= RT_LLE_CACHE;
505 if (opt && opt->ip6po_rthdr)
506 ro = &opt->ip6po_route;
507 dst = (struct sockaddr_in6 *)&ro->ro_dst;
509 if (ro->ro_rt == NULL)
510 (void )flowtable_lookup(AF_INET6, m, (struct route *)ro);
512 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
515 * if specified, try to fill in the traffic class field.
516 * do not override if a non-zero value is already set.
517 * we check the diffserv field and the ecn field separately.
519 if (opt && opt->ip6po_tclass >= 0) {
522 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
524 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
527 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
530 /* fill in or override the hop limit field, if necessary. */
531 if (opt && opt->ip6po_hlim != -1)
532 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
533 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
535 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
537 ip6->ip6_hlim = V_ip6_defmcasthlim;
540 * Validate route against routing table additions;
541 * a better/more specific route might have been added.
542 * Make sure address family is set in route.
545 ro->ro_dst.sin6_family = AF_INET6;
546 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
548 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
549 ro->ro_dst.sin6_family == AF_INET6 &&
550 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
552 ifp = ro->ro_rt->rt_ifp;
554 if (fwd_tag == NULL) {
555 bzero(&dst_sa, sizeof(dst_sa));
556 dst_sa.sin6_family = AF_INET6;
557 dst_sa.sin6_len = sizeof(dst_sa);
558 dst_sa.sin6_addr = ip6->ip6_dst;
560 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
564 in6_ifstat_inc(ifp, ifs6_out_discard);
570 * If in6_selectroute() does not return a route entry,
571 * dst may not have been updated.
573 *dst = dst_sa; /* XXX */
577 * then rt (for unicast) and ifp must be non-NULL valid values.
579 if ((flags & IPV6_FORWARDING) == 0) {
580 /* XXX: the FORWARDING flag can be set for mrouting. */
581 in6_ifstat_inc(ifp, ifs6_out_request);
584 ia = (struct in6_ifaddr *)(rt->rt_ifa);
585 counter_u64_add(rt->rt_pksent, 1);
590 * The outgoing interface must be in the zone of source and
591 * destination addresses.
596 if (in6_setscope(&src0, origifp, &zone))
598 bzero(&src_sa, sizeof(src_sa));
599 src_sa.sin6_family = AF_INET6;
600 src_sa.sin6_len = sizeof(src_sa);
601 src_sa.sin6_addr = ip6->ip6_src;
602 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
606 if (in6_setscope(&dst0, origifp, &zone))
608 /* re-initialize to be sure */
609 bzero(&dst_sa, sizeof(dst_sa));
610 dst_sa.sin6_family = AF_INET6;
611 dst_sa.sin6_len = sizeof(dst_sa);
612 dst_sa.sin6_addr = ip6->ip6_dst;
613 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
617 /* We should use ia_ifp to support the case of
618 * sending packets to an address of our own.
620 if (ia != NULL && ia->ia_ifp)
623 /* scope check is done. */
627 IP6STAT_INC(ip6s_badscope);
628 in6_ifstat_inc(origifp, ifs6_out_discard);
630 error = EHOSTUNREACH; /* XXX */
634 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
635 if (opt && opt->ip6po_nextroute.ro_rt) {
637 * The nexthop is explicitly specified by the
638 * application. We assume the next hop is an IPv6
641 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
643 else if ((rt->rt_flags & RTF_GATEWAY))
644 dst = (struct sockaddr_in6 *)rt->rt_gateway;
647 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
648 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
650 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
651 in6_ifstat_inc(ifp, ifs6_out_mcast);
653 * Confirm that the outgoing interface supports multicast.
655 if (!(ifp->if_flags & IFF_MULTICAST)) {
656 IP6STAT_INC(ip6s_noroute);
657 in6_ifstat_inc(ifp, ifs6_out_discard);
661 if ((im6o == NULL && in6_mcast_loop) ||
662 (im6o && im6o->im6o_multicast_loop)) {
664 * Loop back multicast datagram if not expressly
665 * forbidden to do so, even if we have not joined
666 * the address; protocols will filter it later,
667 * thus deferring a hash lookup and lock acquisition
668 * at the expense of an m_copym().
670 ip6_mloopback(ifp, m);
673 * If we are acting as a multicast router, perform
674 * multicast forwarding as if the packet had just
675 * arrived on the interface to which we are about
676 * to send. The multicast forwarding function
677 * recursively calls this function, using the
678 * IPV6_FORWARDING flag to prevent infinite recursion.
680 * Multicasts that are looped back by ip6_mloopback(),
681 * above, will be forwarded by the ip6_input() routine,
684 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
686 * XXX: ip6_mforward expects that rcvif is NULL
687 * when it is called from the originating path.
688 * However, it may not always be the case.
690 m->m_pkthdr.rcvif = NULL;
691 if (ip6_mforward(ip6, ifp, m) != 0) {
698 * Multicasts with a hoplimit of zero may be looped back,
699 * above, but must not be transmitted on a network.
700 * Also, multicasts addressed to the loopback interface
701 * are not sent -- the above call to ip6_mloopback() will
702 * loop back a copy if this host actually belongs to the
703 * destination group on the loopback interface.
705 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
706 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
713 * Fill the outgoing inteface to tell the upper layer
714 * to increment per-interface statistics.
719 /* Determine path MTU. */
720 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
721 &mtu, &alwaysfrag, fibnum)) != 0)
725 * The caller of this function may specify to use the minimum MTU
727 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
728 * setting. The logic is a bit complicated; by default, unicast
729 * packets will follow path MTU while multicast packets will be sent at
730 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
731 * including unicast ones will be sent at the minimum MTU. Multicast
732 * packets will always be sent at the minimum MTU unless
733 * IP6PO_MINMTU_DISABLE is explicitly specified.
734 * See RFC 3542 for more details.
736 if (mtu > IPV6_MMTU) {
737 if ((flags & IPV6_MINMTU))
739 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
741 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
743 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
749 * clear embedded scope identifiers if necessary.
750 * in6_clearscope will touch the addresses only when necessary.
752 in6_clearscope(&ip6->ip6_src);
753 in6_clearscope(&ip6->ip6_dst);
756 * If the outgoing packet contains a hop-by-hop options header,
757 * it must be examined and processed even by the source node.
758 * (RFC 2460, section 4.)
760 if (exthdrs.ip6e_hbh) {
761 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
762 u_int32_t dummy; /* XXX unused */
763 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
766 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
767 panic("ip6e_hbh is not contiguous");
770 * XXX: if we have to send an ICMPv6 error to the sender,
771 * we need the M_LOOP flag since icmp6_error() expects
772 * the IPv6 and the hop-by-hop options header are
773 * contiguous unless the flag is set.
775 m->m_flags |= M_LOOP;
776 m->m_pkthdr.rcvif = ifp;
777 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
778 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
779 &dummy, &plen) < 0) {
780 /* m was already freed at this point */
781 error = EINVAL;/* better error? */
784 m->m_flags &= ~M_LOOP; /* XXX */
785 m->m_pkthdr.rcvif = NULL;
788 /* Jump over all PFIL processing if hooks are not active. */
789 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
793 /* Run through list of hooks for output packets. */
794 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
795 if (error != 0 || m == NULL)
798 ip6 = mtod(m, struct ip6_hdr *);
801 /* See if destination IP address was changed by packet filter. */
802 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
803 m->m_flags |= M_SKIP_FIREWALL;
804 /* If destination is now ourself drop to ip6_input(). */
805 if (in6_localip(&ip6->ip6_dst)) {
806 m->m_flags |= M_FASTFWD_OURS;
807 if (m->m_pkthdr.rcvif == NULL)
808 m->m_pkthdr.rcvif = V_loif;
809 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
810 m->m_pkthdr.csum_flags |=
811 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
812 m->m_pkthdr.csum_data = 0xffff;
815 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
816 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
818 error = netisr_queue(NETISR_IPV6, m);
822 needfiblookup = 1; /* Redo the routing table lookup. */
825 /* See if fib was changed by packet filter. */
826 if (fibnum != M_GETFIB(m)) {
827 m->m_flags |= M_SKIP_FIREWALL;
828 fibnum = M_GETFIB(m);
835 /* See if local, if yes, send it to netisr. */
836 if (m->m_flags & M_FASTFWD_OURS) {
837 if (m->m_pkthdr.rcvif == NULL)
838 m->m_pkthdr.rcvif = V_loif;
839 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
840 m->m_pkthdr.csum_flags |=
841 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
842 m->m_pkthdr.csum_data = 0xffff;
845 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
846 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
848 error = netisr_queue(NETISR_IPV6, m);
851 /* Or forward to some other address? */
852 if ((m->m_flags & M_IP6_NEXTHOP) &&
853 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
854 dst = (struct sockaddr_in6 *)&ro->ro_dst;
855 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
856 m->m_flags |= M_SKIP_FIREWALL;
857 m->m_flags &= ~M_IP6_NEXTHOP;
858 m_tag_delete(m, fwd_tag);
864 * Send the packet to the outgoing interface.
865 * If necessary, do IPv6 fragmentation before sending.
867 * the logic here is rather complex:
868 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
869 * 1-a: send as is if tlen <= path mtu
870 * 1-b: fragment if tlen > path mtu
872 * 2: if user asks us not to fragment (dontfrag == 1)
873 * 2-a: send as is if tlen <= interface mtu
874 * 2-b: error if tlen > interface mtu
876 * 3: if we always need to attach fragment header (alwaysfrag == 1)
879 * 4: if dontfrag == 1 && alwaysfrag == 1
880 * error, as we cannot handle this conflicting request
882 sw_csum = m->m_pkthdr.csum_flags;
884 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
885 sw_csum &= ~ifp->if_hwassist;
889 * If we added extension headers, we will not do TSO and calculate the
890 * checksums ourselves for now.
891 * XXX-BZ Need a framework to know when the NIC can handle it, even
894 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
895 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
896 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
899 if (sw_csum & CSUM_SCTP_IPV6) {
900 sw_csum &= ~CSUM_SCTP_IPV6;
901 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
904 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
905 tlen = m->m_pkthdr.len;
907 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
911 if (dontfrag && alwaysfrag) { /* case 4 */
912 /* conflicting request - can't transmit */
916 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
918 * Even if the DONTFRAG option is specified, we cannot send the
919 * packet when the data length is larger than the MTU of the
920 * outgoing interface.
921 * Notify the error by sending IPV6_PATHMTU ancillary data if
922 * application wanted to know the MTU value. Also return an
923 * error code (this is not described in the API spec).
926 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
932 * transmit packet without fragmentation
934 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
935 struct in6_ifaddr *ia6;
937 ip6 = mtod(m, struct ip6_hdr *);
938 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
940 /* Record statistics for this interface address. */
941 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
942 counter_u64_add(ia6->ia_ifa.ifa_obytes,
944 ifa_free(&ia6->ia_ifa);
946 error = nd6_output_ifp(ifp, origifp, m, dst,
952 * try to fragment the packet. case 1-b and 3
954 if (mtu < IPV6_MMTU) {
955 /* path MTU cannot be less than IPV6_MMTU */
957 in6_ifstat_inc(ifp, ifs6_out_fragfail);
959 } else if (ip6->ip6_plen == 0) {
960 /* jumbo payload cannot be fragmented */
962 in6_ifstat_inc(ifp, ifs6_out_fragfail);
968 * Too large for the destination or interface;
969 * fragment if possible.
970 * Must be able to put at least 8 bytes per fragment.
972 hlen = unfragpartlen;
973 if (mtu > IPV6_MAXPACKET)
974 mtu = IPV6_MAXPACKET;
976 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
979 in6_ifstat_inc(ifp, ifs6_out_fragfail);
984 * If the interface will not calculate checksums on
985 * fragmented packets, then do it here.
986 * XXX-BZ handle the hw offloading case. Need flags.
988 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
989 in6_delayed_cksum(m, plen, hlen);
990 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
993 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
994 sctp_delayed_cksum(m, hlen);
995 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
999 * Change the next header field of the last header in the
1000 * unfragmentable part.
1002 if (exthdrs.ip6e_rthdr) {
1003 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1004 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1005 } else if (exthdrs.ip6e_dest1) {
1006 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1007 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1008 } else if (exthdrs.ip6e_hbh) {
1009 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1010 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1012 nextproto = ip6->ip6_nxt;
1013 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1017 * Loop through length of segment after first fragment,
1018 * make new header and copy data of each part and link onto
1022 id = htonl(ip6_randomid());
1023 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1026 in6_ifstat_inc(ifp, ifs6_out_fragok);
1030 * Remove leading garbages.
1036 for (m0 = m; m; m = m0) {
1040 /* Record statistics for this interface address. */
1042 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1043 counter_u64_add(ia->ia_ifa.ifa_obytes,
1046 error = nd6_output_ifp(ifp, origifp, m, dst,
1047 (struct route *)ro);
1053 IP6STAT_INC(ip6s_fragmented);
1057 * Release the route if using our private route, or if
1058 * (with flowtable) we don't have our own reference.
1060 if (ro == &ip6route || ro->ro_flags & RT_NORTREF)
1065 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1066 m_freem(exthdrs.ip6e_dest1);
1067 m_freem(exthdrs.ip6e_rthdr);
1068 m_freem(exthdrs.ip6e_dest2);
1077 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1081 if (hlen > MCLBYTES)
1082 return (ENOBUFS); /* XXX */
1085 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1087 m = m_get(M_NOWAIT, MT_DATA);
1092 bcopy(hdr, mtod(m, caddr_t), hlen);
1099 * Insert jumbo payload option.
1102 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1108 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1111 * If there is no hop-by-hop options header, allocate new one.
1112 * If there is one but it doesn't have enough space to store the
1113 * jumbo payload option, allocate a cluster to store the whole options.
1114 * Otherwise, use it to store the options.
1116 if (exthdrs->ip6e_hbh == NULL) {
1117 mopt = m_get(M_NOWAIT, MT_DATA);
1120 mopt->m_len = JUMBOOPTLEN;
1121 optbuf = mtod(mopt, u_char *);
1122 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1123 exthdrs->ip6e_hbh = mopt;
1125 struct ip6_hbh *hbh;
1127 mopt = exthdrs->ip6e_hbh;
1128 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1131 * - exthdrs->ip6e_hbh is not referenced from places
1132 * other than exthdrs.
1133 * - exthdrs->ip6e_hbh is not an mbuf chain.
1135 int oldoptlen = mopt->m_len;
1139 * XXX: give up if the whole (new) hbh header does
1140 * not fit even in an mbuf cluster.
1142 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1146 * As a consequence, we must always prepare a cluster
1149 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1152 n->m_len = oldoptlen + JUMBOOPTLEN;
1153 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1155 optbuf = mtod(n, caddr_t) + oldoptlen;
1157 mopt = exthdrs->ip6e_hbh = n;
1159 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1160 mopt->m_len += JUMBOOPTLEN;
1162 optbuf[0] = IP6OPT_PADN;
1166 * Adjust the header length according to the pad and
1167 * the jumbo payload option.
1169 hbh = mtod(mopt, struct ip6_hbh *);
1170 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1173 /* fill in the option. */
1174 optbuf[2] = IP6OPT_JUMBO;
1176 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1177 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1179 /* finally, adjust the packet header length */
1180 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1187 * Insert fragment header and copy unfragmentable header portions.
1190 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1191 struct ip6_frag **frghdrp)
1193 struct mbuf *n, *mlast;
1195 if (hlen > sizeof(struct ip6_hdr)) {
1196 n = m_copym(m0, sizeof(struct ip6_hdr),
1197 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1204 /* Search for the last mbuf of unfragmentable part. */
1205 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1208 if (M_WRITABLE(mlast) &&
1209 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1210 /* use the trailing space of the last mbuf for the fragment hdr */
1211 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1213 mlast->m_len += sizeof(struct ip6_frag);
1214 m->m_pkthdr.len += sizeof(struct ip6_frag);
1216 /* allocate a new mbuf for the fragment header */
1219 mfrg = m_get(M_NOWAIT, MT_DATA);
1222 mfrg->m_len = sizeof(struct ip6_frag);
1223 *frghdrp = mtod(mfrg, struct ip6_frag *);
1224 mlast->m_next = mfrg;
1231 * Calculates IPv6 path mtu for destination @dst.
1232 * Resulting MTU is stored in @mtup.
1234 * Returns 0 on success.
1237 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1239 struct nhop6_extended nh6;
1240 struct in6_addr kdst;
1246 in6_splitscope(dst, &kdst, &scopeid);
1247 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1248 return (EHOSTUNREACH);
1253 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL);
1254 fib6_free_nh_ext(fibnum, &nh6);
1260 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1261 * and cached data in @ro_pmtu.
1262 * MTU from (successful) route lookup is saved (along with dst)
1263 * inside @ro_pmtu to avoid subsequent route lookups after packet
1264 * filter processing.
1266 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1267 * Returns 0 on success.
1270 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1271 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1272 int *alwaysfragp, u_int fibnum)
1274 struct nhop6_basic nh6;
1275 struct in6_addr kdst;
1277 struct sockaddr_in6 *sa6_dst;
1284 * Here ro_pmtu has final destination address, while
1285 * ro might represent immediate destination.
1286 * Use ro_pmtu destination since mtu might differ.
1288 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1289 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1290 ro_pmtu->ro_mtu = 0;
1292 if (ro_pmtu->ro_mtu == 0) {
1293 bzero(sa6_dst, sizeof(*sa6_dst));
1294 sa6_dst->sin6_family = AF_INET6;
1295 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1296 sa6_dst->sin6_addr = *dst;
1298 in6_splitscope(dst, &kdst, &scopeid);
1299 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1301 ro_pmtu->ro_mtu = nh6.nh_mtu;
1304 mtu = ro_pmtu->ro_mtu;
1308 mtu = ro_pmtu->ro_rt->rt_mtu;
1310 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp));
1314 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1315 * hostcache data for @dst.
1316 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1318 * Returns 0 on success.
1321 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1322 u_long *mtup, int *alwaysfragp)
1330 struct in_conninfo inc;
1332 bzero(&inc, sizeof(inc));
1333 inc.inc_flags |= INC_ISIPV6;
1334 inc.inc6_faddr = *dst;
1336 ifmtu = IN6_LINKMTU(ifp);
1337 mtu = tcp_hc_getmtu(&inc);
1339 mtu = min(mtu, rt_mtu);
1344 else if (mtu < IPV6_MMTU) {
1346 * RFC2460 section 5, last paragraph:
1347 * if we record ICMPv6 too big message with
1348 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1349 * or smaller, with framgent header attached.
1350 * (fragment header is needed regardless from the
1351 * packet size, for translators to identify packets)
1357 mtu = IN6_LINKMTU(ifp);
1359 error = EHOSTUNREACH; /* XXX */
1363 *alwaysfragp = alwaysfrag;
1368 * IP6 socket option processing.
1371 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1373 int optdatalen, uproto;
1375 struct inpcb *in6p = sotoinpcb(so);
1377 int level, op, optname;
1381 uint32_t rss_bucket;
1385 level = sopt->sopt_level;
1386 op = sopt->sopt_dir;
1387 optname = sopt->sopt_name;
1388 optlen = sopt->sopt_valsize;
1392 uproto = (int)so->so_proto->pr_protocol;
1394 if (level != IPPROTO_IPV6) {
1397 if (sopt->sopt_level == SOL_SOCKET &&
1398 sopt->sopt_dir == SOPT_SET) {
1399 switch (sopt->sopt_name) {
1402 if ((so->so_options & SO_REUSEADDR) != 0)
1403 in6p->inp_flags2 |= INP_REUSEADDR;
1405 in6p->inp_flags2 &= ~INP_REUSEADDR;
1411 if ((so->so_options & SO_REUSEPORT) != 0)
1412 in6p->inp_flags2 |= INP_REUSEPORT;
1414 in6p->inp_flags2 &= ~INP_REUSEPORT;
1420 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1428 } else { /* level == IPPROTO_IPV6 */
1433 case IPV6_2292PKTOPTIONS:
1434 #ifdef IPV6_PKTOPTIONS
1435 case IPV6_PKTOPTIONS:
1440 error = soopt_getm(sopt, &m); /* XXX */
1443 error = soopt_mcopyin(sopt, m); /* XXX */
1446 error = ip6_pcbopts(&in6p->in6p_outputopts,
1448 m_freem(m); /* XXX */
1453 * Use of some Hop-by-Hop options or some
1454 * Destination options, might require special
1455 * privilege. That is, normal applications
1456 * (without special privilege) might be forbidden
1457 * from setting certain options in outgoing packets,
1458 * and might never see certain options in received
1459 * packets. [RFC 2292 Section 6]
1460 * KAME specific note:
1461 * KAME prevents non-privileged users from sending or
1462 * receiving ANY hbh/dst options in order to avoid
1463 * overhead of parsing options in the kernel.
1465 case IPV6_RECVHOPOPTS:
1466 case IPV6_RECVDSTOPTS:
1467 case IPV6_RECVRTHDRDSTOPTS:
1469 error = priv_check(td,
1470 PRIV_NETINET_SETHDROPTS);
1475 case IPV6_UNICAST_HOPS:
1478 case IPV6_RECVPKTINFO:
1479 case IPV6_RECVHOPLIMIT:
1480 case IPV6_RECVRTHDR:
1481 case IPV6_RECVPATHMTU:
1482 case IPV6_RECVTCLASS:
1483 case IPV6_RECVFLOWID:
1485 case IPV6_RECVRSSBUCKETID:
1488 case IPV6_AUTOFLOWLABEL:
1490 case IPV6_BINDMULTI:
1492 case IPV6_RSS_LISTEN_BUCKET:
1494 if (optname == IPV6_BINDANY && td != NULL) {
1495 error = priv_check(td,
1496 PRIV_NETINET_BINDANY);
1501 if (optlen != sizeof(int)) {
1505 error = sooptcopyin(sopt, &optval,
1506 sizeof optval, sizeof optval);
1511 case IPV6_UNICAST_HOPS:
1512 if (optval < -1 || optval >= 256)
1515 /* -1 = kernel default */
1516 in6p->in6p_hops = optval;
1517 if ((in6p->inp_vflag &
1519 in6p->inp_ip_ttl = optval;
1522 #define OPTSET(bit) \
1526 in6p->inp_flags |= (bit); \
1528 in6p->inp_flags &= ~(bit); \
1529 INP_WUNLOCK(in6p); \
1530 } while (/*CONSTCOND*/ 0)
1531 #define OPTSET2292(bit) \
1534 in6p->inp_flags |= IN6P_RFC2292; \
1536 in6p->inp_flags |= (bit); \
1538 in6p->inp_flags &= ~(bit); \
1539 INP_WUNLOCK(in6p); \
1540 } while (/*CONSTCOND*/ 0)
1541 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1543 #define OPTSET2(bit, val) do { \
1546 in6p->inp_flags2 |= bit; \
1548 in6p->inp_flags2 &= ~bit; \
1549 INP_WUNLOCK(in6p); \
1551 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1553 case IPV6_RECVPKTINFO:
1554 /* cannot mix with RFC2292 */
1555 if (OPTBIT(IN6P_RFC2292)) {
1559 OPTSET(IN6P_PKTINFO);
1564 struct ip6_pktopts **optp;
1566 /* cannot mix with RFC2292 */
1567 if (OPTBIT(IN6P_RFC2292)) {
1571 optp = &in6p->in6p_outputopts;
1572 error = ip6_pcbopt(IPV6_HOPLIMIT,
1573 (u_char *)&optval, sizeof(optval),
1574 optp, (td != NULL) ? td->td_ucred :
1579 case IPV6_RECVHOPLIMIT:
1580 /* cannot mix with RFC2292 */
1581 if (OPTBIT(IN6P_RFC2292)) {
1585 OPTSET(IN6P_HOPLIMIT);
1588 case IPV6_RECVHOPOPTS:
1589 /* cannot mix with RFC2292 */
1590 if (OPTBIT(IN6P_RFC2292)) {
1594 OPTSET(IN6P_HOPOPTS);
1597 case IPV6_RECVDSTOPTS:
1598 /* cannot mix with RFC2292 */
1599 if (OPTBIT(IN6P_RFC2292)) {
1603 OPTSET(IN6P_DSTOPTS);
1606 case IPV6_RECVRTHDRDSTOPTS:
1607 /* cannot mix with RFC2292 */
1608 if (OPTBIT(IN6P_RFC2292)) {
1612 OPTSET(IN6P_RTHDRDSTOPTS);
1615 case IPV6_RECVRTHDR:
1616 /* cannot mix with RFC2292 */
1617 if (OPTBIT(IN6P_RFC2292)) {
1624 case IPV6_RECVPATHMTU:
1626 * We ignore this option for TCP
1628 * (RFC3542 leaves this case
1631 if (uproto != IPPROTO_TCP)
1635 case IPV6_RECVFLOWID:
1636 OPTSET2(INP_RECVFLOWID, optval);
1640 case IPV6_RECVRSSBUCKETID:
1641 OPTSET2(INP_RECVRSSBUCKETID, optval);
1647 * make setsockopt(IPV6_V6ONLY)
1648 * available only prior to bind(2).
1649 * see ipng mailing list, Jun 22 2001.
1651 if (in6p->inp_lport ||
1652 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1656 OPTSET(IN6P_IPV6_V6ONLY);
1658 in6p->inp_vflag &= ~INP_IPV4;
1660 in6p->inp_vflag |= INP_IPV4;
1662 case IPV6_RECVTCLASS:
1663 /* cannot mix with RFC2292 XXX */
1664 if (OPTBIT(IN6P_RFC2292)) {
1668 OPTSET(IN6P_TCLASS);
1670 case IPV6_AUTOFLOWLABEL:
1671 OPTSET(IN6P_AUTOFLOWLABEL);
1675 OPTSET(INP_BINDANY);
1678 case IPV6_BINDMULTI:
1679 OPTSET2(INP_BINDMULTI, optval);
1682 case IPV6_RSS_LISTEN_BUCKET:
1683 if ((optval >= 0) &&
1684 (optval < rss_getnumbuckets())) {
1685 in6p->inp_rss_listen_bucket = optval;
1686 OPTSET2(INP_RSS_BUCKET_SET, 1);
1697 case IPV6_USE_MIN_MTU:
1698 case IPV6_PREFER_TEMPADDR:
1699 if (optlen != sizeof(optval)) {
1703 error = sooptcopyin(sopt, &optval,
1704 sizeof optval, sizeof optval);
1708 struct ip6_pktopts **optp;
1709 optp = &in6p->in6p_outputopts;
1710 error = ip6_pcbopt(optname,
1711 (u_char *)&optval, sizeof(optval),
1712 optp, (td != NULL) ? td->td_ucred :
1717 case IPV6_2292PKTINFO:
1718 case IPV6_2292HOPLIMIT:
1719 case IPV6_2292HOPOPTS:
1720 case IPV6_2292DSTOPTS:
1721 case IPV6_2292RTHDR:
1723 if (optlen != sizeof(int)) {
1727 error = sooptcopyin(sopt, &optval,
1728 sizeof optval, sizeof optval);
1732 case IPV6_2292PKTINFO:
1733 OPTSET2292(IN6P_PKTINFO);
1735 case IPV6_2292HOPLIMIT:
1736 OPTSET2292(IN6P_HOPLIMIT);
1738 case IPV6_2292HOPOPTS:
1740 * Check super-user privilege.
1741 * See comments for IPV6_RECVHOPOPTS.
1744 error = priv_check(td,
1745 PRIV_NETINET_SETHDROPTS);
1749 OPTSET2292(IN6P_HOPOPTS);
1751 case IPV6_2292DSTOPTS:
1753 error = priv_check(td,
1754 PRIV_NETINET_SETHDROPTS);
1758 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1760 case IPV6_2292RTHDR:
1761 OPTSET2292(IN6P_RTHDR);
1769 case IPV6_RTHDRDSTOPTS:
1772 /* new advanced API (RFC3542) */
1774 u_char optbuf_storage[MCLBYTES];
1776 struct ip6_pktopts **optp;
1778 /* cannot mix with RFC2292 */
1779 if (OPTBIT(IN6P_RFC2292)) {
1785 * We only ensure valsize is not too large
1786 * here. Further validation will be done
1789 error = sooptcopyin(sopt, optbuf_storage,
1790 sizeof(optbuf_storage), 0);
1793 optlen = sopt->sopt_valsize;
1794 optbuf = optbuf_storage;
1795 optp = &in6p->in6p_outputopts;
1796 error = ip6_pcbopt(optname, optbuf, optlen,
1797 optp, (td != NULL) ? td->td_ucred : NULL,
1803 case IPV6_MULTICAST_IF:
1804 case IPV6_MULTICAST_HOPS:
1805 case IPV6_MULTICAST_LOOP:
1806 case IPV6_JOIN_GROUP:
1807 case IPV6_LEAVE_GROUP:
1809 case MCAST_BLOCK_SOURCE:
1810 case MCAST_UNBLOCK_SOURCE:
1811 case MCAST_JOIN_GROUP:
1812 case MCAST_LEAVE_GROUP:
1813 case MCAST_JOIN_SOURCE_GROUP:
1814 case MCAST_LEAVE_SOURCE_GROUP:
1815 error = ip6_setmoptions(in6p, sopt);
1818 case IPV6_PORTRANGE:
1819 error = sooptcopyin(sopt, &optval,
1820 sizeof optval, sizeof optval);
1826 case IPV6_PORTRANGE_DEFAULT:
1827 in6p->inp_flags &= ~(INP_LOWPORT);
1828 in6p->inp_flags &= ~(INP_HIGHPORT);
1831 case IPV6_PORTRANGE_HIGH:
1832 in6p->inp_flags &= ~(INP_LOWPORT);
1833 in6p->inp_flags |= INP_HIGHPORT;
1836 case IPV6_PORTRANGE_LOW:
1837 in6p->inp_flags &= ~(INP_HIGHPORT);
1838 in6p->inp_flags |= INP_LOWPORT;
1849 case IPV6_IPSEC_POLICY:
1854 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1856 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1858 req = mtod(m, caddr_t);
1859 error = ipsec_set_policy(in6p, optname, req,
1860 m->m_len, (sopt->sopt_td != NULL) ?
1861 sopt->sopt_td->td_ucred : NULL);
1868 error = ENOPROTOOPT;
1876 case IPV6_2292PKTOPTIONS:
1877 #ifdef IPV6_PKTOPTIONS
1878 case IPV6_PKTOPTIONS:
1881 * RFC3542 (effectively) deprecated the
1882 * semantics of the 2292-style pktoptions.
1883 * Since it was not reliable in nature (i.e.,
1884 * applications had to expect the lack of some
1885 * information after all), it would make sense
1886 * to simplify this part by always returning
1889 sopt->sopt_valsize = 0;
1892 case IPV6_RECVHOPOPTS:
1893 case IPV6_RECVDSTOPTS:
1894 case IPV6_RECVRTHDRDSTOPTS:
1895 case IPV6_UNICAST_HOPS:
1896 case IPV6_RECVPKTINFO:
1897 case IPV6_RECVHOPLIMIT:
1898 case IPV6_RECVRTHDR:
1899 case IPV6_RECVPATHMTU:
1902 case IPV6_PORTRANGE:
1903 case IPV6_RECVTCLASS:
1904 case IPV6_AUTOFLOWLABEL:
1908 case IPV6_RECVFLOWID:
1910 case IPV6_RSSBUCKETID:
1911 case IPV6_RECVRSSBUCKETID:
1913 case IPV6_BINDMULTI:
1916 case IPV6_RECVHOPOPTS:
1917 optval = OPTBIT(IN6P_HOPOPTS);
1920 case IPV6_RECVDSTOPTS:
1921 optval = OPTBIT(IN6P_DSTOPTS);
1924 case IPV6_RECVRTHDRDSTOPTS:
1925 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1928 case IPV6_UNICAST_HOPS:
1929 optval = in6p->in6p_hops;
1932 case IPV6_RECVPKTINFO:
1933 optval = OPTBIT(IN6P_PKTINFO);
1936 case IPV6_RECVHOPLIMIT:
1937 optval = OPTBIT(IN6P_HOPLIMIT);
1940 case IPV6_RECVRTHDR:
1941 optval = OPTBIT(IN6P_RTHDR);
1944 case IPV6_RECVPATHMTU:
1945 optval = OPTBIT(IN6P_MTU);
1949 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1952 case IPV6_PORTRANGE:
1955 flags = in6p->inp_flags;
1956 if (flags & INP_HIGHPORT)
1957 optval = IPV6_PORTRANGE_HIGH;
1958 else if (flags & INP_LOWPORT)
1959 optval = IPV6_PORTRANGE_LOW;
1964 case IPV6_RECVTCLASS:
1965 optval = OPTBIT(IN6P_TCLASS);
1968 case IPV6_AUTOFLOWLABEL:
1969 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1973 optval = OPTBIT(INP_BINDANY);
1977 optval = in6p->inp_flowid;
1981 optval = in6p->inp_flowtype;
1984 case IPV6_RECVFLOWID:
1985 optval = OPTBIT2(INP_RECVFLOWID);
1988 case IPV6_RSSBUCKETID:
1990 rss_hash2bucket(in6p->inp_flowid,
1994 optval = rss_bucket;
1999 case IPV6_RECVRSSBUCKETID:
2000 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2004 case IPV6_BINDMULTI:
2005 optval = OPTBIT2(INP_BINDMULTI);
2011 error = sooptcopyout(sopt, &optval,
2018 struct ip6_mtuinfo mtuinfo;
2020 if (!(so->so_state & SS_ISCONNECTED))
2023 * XXX: we dot not consider the case of source
2024 * routing, or optional information to specify
2025 * the outgoing interface.
2027 error = ip6_getpmtu_ctl(so->so_fibnum,
2028 &in6p->in6p_faddr, &pmtu);
2031 if (pmtu > IPV6_MAXPACKET)
2032 pmtu = IPV6_MAXPACKET;
2034 bzero(&mtuinfo, sizeof(mtuinfo));
2035 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2036 optdata = (void *)&mtuinfo;
2037 optdatalen = sizeof(mtuinfo);
2038 error = sooptcopyout(sopt, optdata,
2043 case IPV6_2292PKTINFO:
2044 case IPV6_2292HOPLIMIT:
2045 case IPV6_2292HOPOPTS:
2046 case IPV6_2292RTHDR:
2047 case IPV6_2292DSTOPTS:
2049 case IPV6_2292PKTINFO:
2050 optval = OPTBIT(IN6P_PKTINFO);
2052 case IPV6_2292HOPLIMIT:
2053 optval = OPTBIT(IN6P_HOPLIMIT);
2055 case IPV6_2292HOPOPTS:
2056 optval = OPTBIT(IN6P_HOPOPTS);
2058 case IPV6_2292RTHDR:
2059 optval = OPTBIT(IN6P_RTHDR);
2061 case IPV6_2292DSTOPTS:
2062 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2065 error = sooptcopyout(sopt, &optval,
2072 case IPV6_RTHDRDSTOPTS:
2076 case IPV6_USE_MIN_MTU:
2077 case IPV6_PREFER_TEMPADDR:
2078 error = ip6_getpcbopt(in6p->in6p_outputopts,
2082 case IPV6_MULTICAST_IF:
2083 case IPV6_MULTICAST_HOPS:
2084 case IPV6_MULTICAST_LOOP:
2086 error = ip6_getmoptions(in6p, sopt);
2090 case IPV6_IPSEC_POLICY:
2094 struct mbuf *m = NULL;
2095 struct mbuf **mp = &m;
2096 size_t ovalsize = sopt->sopt_valsize;
2097 caddr_t oval = (caddr_t)sopt->sopt_val;
2099 error = soopt_getm(sopt, &m); /* XXX */
2102 error = soopt_mcopyin(sopt, m); /* XXX */
2105 sopt->sopt_valsize = ovalsize;
2106 sopt->sopt_val = oval;
2108 req = mtod(m, caddr_t);
2111 error = ipsec_get_policy(in6p, req, len, mp);
2113 error = soopt_mcopyout(sopt, m); /* XXX */
2114 if (error == 0 && m)
2121 error = ENOPROTOOPT;
2131 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2133 int error = 0, optval, optlen;
2134 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2135 struct inpcb *in6p = sotoinpcb(so);
2136 int level, op, optname;
2138 level = sopt->sopt_level;
2139 op = sopt->sopt_dir;
2140 optname = sopt->sopt_name;
2141 optlen = sopt->sopt_valsize;
2143 if (level != IPPROTO_IPV6) {
2150 * For ICMPv6 sockets, no modification allowed for checksum
2151 * offset, permit "no change" values to help existing apps.
2153 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2154 * for an ICMPv6 socket will fail."
2155 * The current behavior does not meet RFC3542.
2159 if (optlen != sizeof(int)) {
2163 error = sooptcopyin(sopt, &optval, sizeof(optval),
2167 if ((optval % 2) != 0) {
2168 /* the API assumes even offset values */
2170 } else if (so->so_proto->pr_protocol ==
2172 if (optval != icmp6off)
2175 in6p->in6p_cksum = optval;
2179 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2182 optval = in6p->in6p_cksum;
2184 error = sooptcopyout(sopt, &optval, sizeof(optval));
2194 error = ENOPROTOOPT;
2202 * Set up IP6 options in pcb for insertion in output packets or
2203 * specifying behavior of outgoing packets.
2206 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2207 struct socket *so, struct sockopt *sopt)
2209 struct ip6_pktopts *opt = *pktopt;
2211 struct thread *td = sopt->sopt_td;
2213 /* turn off any old options. */
2216 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2217 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2218 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2219 printf("ip6_pcbopts: all specified options are cleared.\n");
2221 ip6_clearpktopts(opt, -1);
2223 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2226 if (!m || m->m_len == 0) {
2228 * Only turning off any previous options, regardless of
2229 * whether the opt is just created or given.
2231 free(opt, M_IP6OPT);
2235 /* set options specified by user. */
2236 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2237 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2238 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2239 free(opt, M_IP6OPT);
2247 * initialize ip6_pktopts. beware that there are non-zero default values in
2251 ip6_initpktopts(struct ip6_pktopts *opt)
2254 bzero(opt, sizeof(*opt));
2255 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2256 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2257 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2258 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2262 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2263 struct ucred *cred, int uproto)
2265 struct ip6_pktopts *opt;
2267 if (*pktopt == NULL) {
2268 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2270 ip6_initpktopts(*pktopt);
2274 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2278 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2280 void *optdata = NULL;
2282 struct ip6_ext *ip6e;
2284 struct in6_pktinfo null_pktinfo;
2285 int deftclass = 0, on;
2286 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2287 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2291 optdata = (void *)&null_pktinfo;
2292 if (pktopt && pktopt->ip6po_pktinfo) {
2293 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2294 sizeof(null_pktinfo));
2295 in6_clearscope(&null_pktinfo.ipi6_addr);
2297 /* XXX: we don't have to do this every time... */
2298 bzero(&null_pktinfo, sizeof(null_pktinfo));
2300 optdatalen = sizeof(struct in6_pktinfo);
2303 if (pktopt && pktopt->ip6po_tclass >= 0)
2304 optdata = (void *)&pktopt->ip6po_tclass;
2306 optdata = (void *)&deftclass;
2307 optdatalen = sizeof(int);
2310 if (pktopt && pktopt->ip6po_hbh) {
2311 optdata = (void *)pktopt->ip6po_hbh;
2312 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2313 optdatalen = (ip6e->ip6e_len + 1) << 3;
2317 if (pktopt && pktopt->ip6po_rthdr) {
2318 optdata = (void *)pktopt->ip6po_rthdr;
2319 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2320 optdatalen = (ip6e->ip6e_len + 1) << 3;
2323 case IPV6_RTHDRDSTOPTS:
2324 if (pktopt && pktopt->ip6po_dest1) {
2325 optdata = (void *)pktopt->ip6po_dest1;
2326 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2327 optdatalen = (ip6e->ip6e_len + 1) << 3;
2331 if (pktopt && pktopt->ip6po_dest2) {
2332 optdata = (void *)pktopt->ip6po_dest2;
2333 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2334 optdatalen = (ip6e->ip6e_len + 1) << 3;
2338 if (pktopt && pktopt->ip6po_nexthop) {
2339 optdata = (void *)pktopt->ip6po_nexthop;
2340 optdatalen = pktopt->ip6po_nexthop->sa_len;
2343 case IPV6_USE_MIN_MTU:
2345 optdata = (void *)&pktopt->ip6po_minmtu;
2347 optdata = (void *)&defminmtu;
2348 optdatalen = sizeof(int);
2351 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2355 optdata = (void *)&on;
2356 optdatalen = sizeof(on);
2358 case IPV6_PREFER_TEMPADDR:
2360 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2362 optdata = (void *)&defpreftemp;
2363 optdatalen = sizeof(int);
2365 default: /* should not happen */
2367 panic("ip6_getpcbopt: unexpected option\n");
2369 return (ENOPROTOOPT);
2372 error = sooptcopyout(sopt, optdata, optdatalen);
2378 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2383 if (optname == -1 || optname == IPV6_PKTINFO) {
2384 if (pktopt->ip6po_pktinfo)
2385 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2386 pktopt->ip6po_pktinfo = NULL;
2388 if (optname == -1 || optname == IPV6_HOPLIMIT)
2389 pktopt->ip6po_hlim = -1;
2390 if (optname == -1 || optname == IPV6_TCLASS)
2391 pktopt->ip6po_tclass = -1;
2392 if (optname == -1 || optname == IPV6_NEXTHOP) {
2393 if (pktopt->ip6po_nextroute.ro_rt) {
2394 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2395 pktopt->ip6po_nextroute.ro_rt = NULL;
2397 if (pktopt->ip6po_nexthop)
2398 free(pktopt->ip6po_nexthop, M_IP6OPT);
2399 pktopt->ip6po_nexthop = NULL;
2401 if (optname == -1 || optname == IPV6_HOPOPTS) {
2402 if (pktopt->ip6po_hbh)
2403 free(pktopt->ip6po_hbh, M_IP6OPT);
2404 pktopt->ip6po_hbh = NULL;
2406 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2407 if (pktopt->ip6po_dest1)
2408 free(pktopt->ip6po_dest1, M_IP6OPT);
2409 pktopt->ip6po_dest1 = NULL;
2411 if (optname == -1 || optname == IPV6_RTHDR) {
2412 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2413 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2414 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2415 if (pktopt->ip6po_route.ro_rt) {
2416 RTFREE(pktopt->ip6po_route.ro_rt);
2417 pktopt->ip6po_route.ro_rt = NULL;
2420 if (optname == -1 || optname == IPV6_DSTOPTS) {
2421 if (pktopt->ip6po_dest2)
2422 free(pktopt->ip6po_dest2, M_IP6OPT);
2423 pktopt->ip6po_dest2 = NULL;
2427 #define PKTOPT_EXTHDRCPY(type) \
2430 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2431 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2432 if (dst->type == NULL && canwait == M_NOWAIT)\
2434 bcopy(src->type, dst->type, hlen);\
2436 } while (/*CONSTCOND*/ 0)
2439 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2441 if (dst == NULL || src == NULL) {
2442 printf("ip6_clearpktopts: invalid argument\n");
2446 dst->ip6po_hlim = src->ip6po_hlim;
2447 dst->ip6po_tclass = src->ip6po_tclass;
2448 dst->ip6po_flags = src->ip6po_flags;
2449 dst->ip6po_minmtu = src->ip6po_minmtu;
2450 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2451 if (src->ip6po_pktinfo) {
2452 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2454 if (dst->ip6po_pktinfo == NULL)
2456 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2458 if (src->ip6po_nexthop) {
2459 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2461 if (dst->ip6po_nexthop == NULL)
2463 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2464 src->ip6po_nexthop->sa_len);
2466 PKTOPT_EXTHDRCPY(ip6po_hbh);
2467 PKTOPT_EXTHDRCPY(ip6po_dest1);
2468 PKTOPT_EXTHDRCPY(ip6po_dest2);
2469 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2473 ip6_clearpktopts(dst, -1);
2476 #undef PKTOPT_EXTHDRCPY
2478 struct ip6_pktopts *
2479 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2482 struct ip6_pktopts *dst;
2484 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2487 ip6_initpktopts(dst);
2489 if ((error = copypktopts(dst, src, canwait)) != 0) {
2490 free(dst, M_IP6OPT);
2498 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2503 ip6_clearpktopts(pktopt, -1);
2505 free(pktopt, M_IP6OPT);
2509 * Set IPv6 outgoing packet options based on advanced API.
2512 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2513 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2515 struct cmsghdr *cm = NULL;
2517 if (control == NULL || opt == NULL)
2520 ip6_initpktopts(opt);
2525 * If stickyopt is provided, make a local copy of the options
2526 * for this particular packet, then override them by ancillary
2528 * XXX: copypktopts() does not copy the cached route to a next
2529 * hop (if any). This is not very good in terms of efficiency,
2530 * but we can allow this since this option should be rarely
2533 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2538 * XXX: Currently, we assume all the optional information is stored
2541 if (control->m_next)
2544 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2545 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2548 if (control->m_len < CMSG_LEN(0))
2551 cm = mtod(control, struct cmsghdr *);
2552 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2554 if (cm->cmsg_level != IPPROTO_IPV6)
2557 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2558 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2567 * Set a particular packet option, as a sticky option or an ancillary data
2568 * item. "len" can be 0 only when it's a sticky option.
2569 * We have 4 cases of combination of "sticky" and "cmsg":
2570 * "sticky=0, cmsg=0": impossible
2571 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2572 * "sticky=1, cmsg=0": RFC3542 socket option
2573 * "sticky=1, cmsg=1": RFC2292 socket option
2576 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2577 struct ucred *cred, int sticky, int cmsg, int uproto)
2579 int minmtupolicy, preftemp;
2582 if (!sticky && !cmsg) {
2584 printf("ip6_setpktopt: impossible case\n");
2590 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2591 * not be specified in the context of RFC3542. Conversely,
2592 * RFC3542 types should not be specified in the context of RFC2292.
2596 case IPV6_2292PKTINFO:
2597 case IPV6_2292HOPLIMIT:
2598 case IPV6_2292NEXTHOP:
2599 case IPV6_2292HOPOPTS:
2600 case IPV6_2292DSTOPTS:
2601 case IPV6_2292RTHDR:
2602 case IPV6_2292PKTOPTIONS:
2603 return (ENOPROTOOPT);
2606 if (sticky && cmsg) {
2613 case IPV6_RTHDRDSTOPTS:
2615 case IPV6_USE_MIN_MTU:
2618 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2619 return (ENOPROTOOPT);
2624 case IPV6_2292PKTINFO:
2627 struct ifnet *ifp = NULL;
2628 struct in6_pktinfo *pktinfo;
2630 if (len != sizeof(struct in6_pktinfo))
2633 pktinfo = (struct in6_pktinfo *)buf;
2636 * An application can clear any sticky IPV6_PKTINFO option by
2637 * doing a "regular" setsockopt with ipi6_addr being
2638 * in6addr_any and ipi6_ifindex being zero.
2639 * [RFC 3542, Section 6]
2641 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2642 pktinfo->ipi6_ifindex == 0 &&
2643 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2644 ip6_clearpktopts(opt, optname);
2648 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2649 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2652 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2654 /* validate the interface index if specified. */
2655 if (pktinfo->ipi6_ifindex > V_if_index)
2657 if (pktinfo->ipi6_ifindex) {
2658 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2662 if (ifp != NULL && (
2663 ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED))
2667 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2668 struct in6_ifaddr *ia;
2670 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2671 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2673 return (EADDRNOTAVAIL);
2674 ifa_free(&ia->ia_ifa);
2677 * We store the address anyway, and let in6_selectsrc()
2678 * validate the specified address. This is because ipi6_addr
2679 * may not have enough information about its scope zone, and
2680 * we may need additional information (such as outgoing
2681 * interface or the scope zone of a destination address) to
2682 * disambiguate the scope.
2683 * XXX: the delay of the validation may confuse the
2684 * application when it is used as a sticky option.
2686 if (opt->ip6po_pktinfo == NULL) {
2687 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2688 M_IP6OPT, M_NOWAIT);
2689 if (opt->ip6po_pktinfo == NULL)
2692 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2696 case IPV6_2292HOPLIMIT:
2702 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2703 * to simplify the ordering among hoplimit options.
2705 if (optname == IPV6_HOPLIMIT && sticky)
2706 return (ENOPROTOOPT);
2708 if (len != sizeof(int))
2711 if (*hlimp < -1 || *hlimp > 255)
2714 opt->ip6po_hlim = *hlimp;
2722 if (len != sizeof(int))
2724 tclass = *(int *)buf;
2725 if (tclass < -1 || tclass > 255)
2728 opt->ip6po_tclass = tclass;
2732 case IPV6_2292NEXTHOP:
2735 error = priv_check_cred(cred,
2736 PRIV_NETINET_SETHDROPTS, 0);
2741 if (len == 0) { /* just remove the option */
2742 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2746 /* check if cmsg_len is large enough for sa_len */
2747 if (len < sizeof(struct sockaddr) || len < *buf)
2750 switch (((struct sockaddr *)buf)->sa_family) {
2753 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2756 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2759 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2760 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2763 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2769 case AF_LINK: /* should eventually be supported */
2771 return (EAFNOSUPPORT);
2774 /* turn off the previous option, then set the new option. */
2775 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2776 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2777 if (opt->ip6po_nexthop == NULL)
2779 bcopy(buf, opt->ip6po_nexthop, *buf);
2782 case IPV6_2292HOPOPTS:
2785 struct ip6_hbh *hbh;
2789 * XXX: We don't allow a non-privileged user to set ANY HbH
2790 * options, since per-option restriction has too much
2794 error = priv_check_cred(cred,
2795 PRIV_NETINET_SETHDROPTS, 0);
2801 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2802 break; /* just remove the option */
2805 /* message length validation */
2806 if (len < sizeof(struct ip6_hbh))
2808 hbh = (struct ip6_hbh *)buf;
2809 hbhlen = (hbh->ip6h_len + 1) << 3;
2813 /* turn off the previous option, then set the new option. */
2814 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2815 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2816 if (opt->ip6po_hbh == NULL)
2818 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2823 case IPV6_2292DSTOPTS:
2825 case IPV6_RTHDRDSTOPTS:
2827 struct ip6_dest *dest, **newdest = NULL;
2830 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2831 error = priv_check_cred(cred,
2832 PRIV_NETINET_SETHDROPTS, 0);
2838 ip6_clearpktopts(opt, optname);
2839 break; /* just remove the option */
2842 /* message length validation */
2843 if (len < sizeof(struct ip6_dest))
2845 dest = (struct ip6_dest *)buf;
2846 destlen = (dest->ip6d_len + 1) << 3;
2851 * Determine the position that the destination options header
2852 * should be inserted; before or after the routing header.
2855 case IPV6_2292DSTOPTS:
2857 * The old advacned API is ambiguous on this point.
2858 * Our approach is to determine the position based
2859 * according to the existence of a routing header.
2860 * Note, however, that this depends on the order of the
2861 * extension headers in the ancillary data; the 1st
2862 * part of the destination options header must appear
2863 * before the routing header in the ancillary data,
2865 * RFC3542 solved the ambiguity by introducing
2866 * separate ancillary data or option types.
2868 if (opt->ip6po_rthdr == NULL)
2869 newdest = &opt->ip6po_dest1;
2871 newdest = &opt->ip6po_dest2;
2873 case IPV6_RTHDRDSTOPTS:
2874 newdest = &opt->ip6po_dest1;
2877 newdest = &opt->ip6po_dest2;
2881 /* turn off the previous option, then set the new option. */
2882 ip6_clearpktopts(opt, optname);
2883 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2884 if (*newdest == NULL)
2886 bcopy(dest, *newdest, destlen);
2891 case IPV6_2292RTHDR:
2894 struct ip6_rthdr *rth;
2898 ip6_clearpktopts(opt, IPV6_RTHDR);
2899 break; /* just remove the option */
2902 /* message length validation */
2903 if (len < sizeof(struct ip6_rthdr))
2905 rth = (struct ip6_rthdr *)buf;
2906 rthlen = (rth->ip6r_len + 1) << 3;
2910 switch (rth->ip6r_type) {
2911 case IPV6_RTHDR_TYPE_0:
2912 if (rth->ip6r_len == 0) /* must contain one addr */
2914 if (rth->ip6r_len % 2) /* length must be even */
2916 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2920 return (EINVAL); /* not supported */
2923 /* turn off the previous option */
2924 ip6_clearpktopts(opt, IPV6_RTHDR);
2925 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2926 if (opt->ip6po_rthdr == NULL)
2928 bcopy(rth, opt->ip6po_rthdr, rthlen);
2933 case IPV6_USE_MIN_MTU:
2934 if (len != sizeof(int))
2936 minmtupolicy = *(int *)buf;
2937 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2938 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2939 minmtupolicy != IP6PO_MINMTU_ALL) {
2942 opt->ip6po_minmtu = minmtupolicy;
2946 if (len != sizeof(int))
2949 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2951 * we ignore this option for TCP sockets.
2952 * (RFC3542 leaves this case unspecified.)
2954 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2956 opt->ip6po_flags |= IP6PO_DONTFRAG;
2959 case IPV6_PREFER_TEMPADDR:
2960 if (len != sizeof(int))
2962 preftemp = *(int *)buf;
2963 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2964 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2965 preftemp != IP6PO_TEMPADDR_PREFER) {
2968 opt->ip6po_prefer_tempaddr = preftemp;
2972 return (ENOPROTOOPT);
2973 } /* end of switch */
2979 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2980 * packet to the input queue of a specified interface. Note that this
2981 * calls the output routine of the loopback "driver", but with an interface
2982 * pointer that might NOT be &loif -- easier than replicating that code here.
2985 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
2988 struct ip6_hdr *ip6;
2990 copym = m_copy(m, 0, M_COPYALL);
2995 * Make sure to deep-copy IPv6 header portion in case the data
2996 * is in an mbuf cluster, so that we can safely override the IPv6
2997 * header portion later.
2999 if (!M_WRITABLE(copym) ||
3000 copym->m_len < sizeof(struct ip6_hdr)) {
3001 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3005 ip6 = mtod(copym, struct ip6_hdr *);
3007 * clear embedded scope identifiers if necessary.
3008 * in6_clearscope will touch the addresses only when necessary.
3010 in6_clearscope(&ip6->ip6_src);
3011 in6_clearscope(&ip6->ip6_dst);
3012 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3013 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3015 copym->m_pkthdr.csum_data = 0xffff;
3017 if_simloop(ifp, copym, AF_INET6, 0);
3021 * Chop IPv6 header off from the payload.
3024 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3027 struct ip6_hdr *ip6;
3029 ip6 = mtod(m, struct ip6_hdr *);
3030 if (m->m_len > sizeof(*ip6)) {
3031 mh = m_gethdr(M_NOWAIT, MT_DATA);
3036 m_move_pkthdr(mh, m);
3037 M_ALIGN(mh, sizeof(*ip6));
3038 m->m_len -= sizeof(*ip6);
3039 m->m_data += sizeof(*ip6);
3042 m->m_len = sizeof(*ip6);
3043 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3045 exthdrs->ip6e_ip6 = m;
3050 * Compute IPv6 extension header length.
3053 ip6_optlen(struct inpcb *in6p)
3057 if (!in6p->in6p_outputopts)
3062 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3064 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3065 if (in6p->in6p_outputopts->ip6po_rthdr)
3066 /* dest1 is valid with rthdr only */
3067 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3068 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3069 len += elen(in6p->in6p_outputopts->ip6po_dest2);