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"
69 #include "opt_ipsec.h"
71 #include "opt_route.h"
74 #include <sys/param.h>
75 #include <sys/kernel.h>
76 #include <sys/malloc.h>
78 #include <sys/errno.h>
81 #include <sys/protosw.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/syslog.h>
85 #include <sys/ucred.h>
87 #include <machine/in_cksum.h>
90 #include <net/if_var.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>
111 #include <netipsec/ipsec.h>
112 #include <netipsec/ipsec6.h>
113 #include <netipsec/key.h>
114 #include <netinet6/ip6_ipsec.h>
117 #include <netinet/sctp.h>
118 #include <netinet/sctp_crc32.h>
121 #include <netinet6/ip6protosw.h>
122 #include <netinet6/scope6_var.h>
125 #include <net/flowtable.h>
128 extern int in6_mcast_loop;
131 struct mbuf *ip6e_ip6;
132 struct mbuf *ip6e_hbh;
133 struct mbuf *ip6e_dest1;
134 struct mbuf *ip6e_rthdr;
135 struct mbuf *ip6e_dest2;
138 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
139 struct ucred *, int);
140 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
141 struct socket *, struct sockopt *);
142 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
143 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
144 struct ucred *, int, int, int);
146 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
147 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
149 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
150 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
151 static int ip6_getpmtu(struct route_in6 *, int,
152 struct ifnet *, struct in6_addr *, u_long *, int *, u_int);
153 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
155 static int ip6_getpmtu_ctl(u_int, struct in6_addr *, u_long *);
156 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
160 * Make an extension header from option data. hp is the source, and
161 * mp is the destination.
163 #define MAKE_EXTHDR(hp, mp) \
166 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
167 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
168 ((eh)->ip6e_len + 1) << 3); \
172 } while (/*CONSTCOND*/ 0)
175 * Form a chain of extension headers.
176 * m is the extension header mbuf
177 * mp is the previous mbuf in the chain
178 * p is the next header
179 * i is the type of option.
181 #define MAKE_CHAIN(m, mp, p, i)\
185 panic("assumption failed: hdr not split"); \
186 *mtod((m), u_char *) = *(p);\
188 p = mtod((m), u_char *);\
189 (m)->m_next = (mp)->m_next;\
193 } while (/*CONSTCOND*/ 0)
196 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
200 csum = in_cksum_skip(m, offset + plen, offset);
201 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
203 offset += m->m_pkthdr.csum_data; /* checksum offset */
205 if (offset + sizeof(u_short) > m->m_len) {
206 printf("%s: delayed m_pullup, m->len: %d plen %u off %u "
207 "csum_flags=%b\n", __func__, m->m_len, plen, offset,
208 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
210 * XXX this should not happen, but if it does, the correct
211 * behavior may be to insert the checksum in the appropriate
212 * next mbuf in the chain.
216 *(u_short *)(m->m_data + offset) = csum;
220 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
221 int mtu, uint32_t id)
223 struct mbuf *m, **mnext, *m_frgpart;
224 struct ip6_hdr *ip6, *mhip6;
225 struct ip6_frag *ip6f;
228 int tlen = m0->m_pkthdr.len;
231 ip6 = mtod(m, struct ip6_hdr *);
232 mnext = &m->m_nextpkt;
234 for (off = hlen; off < tlen; off += mtu) {
235 m = m_gethdr(M_NOWAIT, MT_DATA);
237 IP6STAT_INC(ip6s_odropped);
240 m->m_flags = m0->m_flags & M_COPYFLAGS;
242 mnext = &m->m_nextpkt;
243 m->m_data += max_linkhdr;
244 mhip6 = mtod(m, struct ip6_hdr *);
246 m->m_len = sizeof(*mhip6);
247 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
249 IP6STAT_INC(ip6s_odropped);
252 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
253 if (off + mtu >= tlen)
256 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
257 mhip6->ip6_plen = htons((u_short)(mtu + hlen +
258 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
259 if ((m_frgpart = m_copy(m0, off, mtu)) == 0) {
260 IP6STAT_INC(ip6s_odropped);
264 m->m_pkthdr.len = mtu + hlen + sizeof(*ip6f);
265 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
266 m->m_pkthdr.rcvif = NULL;
267 ip6f->ip6f_reserved = 0;
268 ip6f->ip6f_ident = id;
269 ip6f->ip6f_nxt = nextproto;
270 IP6STAT_INC(ip6s_ofragments);
271 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
278 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
279 * header (with pri, len, nxt, hlim, src, dst).
280 * This function may modify ver and hlim only.
281 * The mbuf chain containing the packet will be freed.
282 * The mbuf opt, if present, will not be freed.
283 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
284 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
285 * then result of route lookup is stored in ro->ro_rt.
287 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
288 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
291 * ifpp - XXX: just for statistics
294 * XXX TODO: no flowid is assigned for outbound flows?
297 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
298 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
299 struct ifnet **ifpp, struct inpcb *inp)
302 struct ifnet *ifp, *origifp;
304 struct mbuf *mprev = NULL;
306 struct route_in6 ip6route;
307 struct rtentry *rt = NULL;
308 struct sockaddr_in6 *dst, src_sa, dst_sa;
309 struct in6_addr odst;
311 struct in6_ifaddr *ia = NULL;
313 int alwaysfrag, dontfrag;
314 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
315 struct ip6_exthdrs exthdrs;
316 struct in6_addr finaldst, src0, dst0;
318 struct route_in6 *ro_pmtu = NULL;
323 struct m_tag *fwd_tag = NULL;
326 ip6 = mtod(m, struct ip6_hdr *);
328 printf ("ip6 is NULL");
333 M_SETFIB(m, inp->inp_inc.inc_fibnum);
334 if ((flags & IP_NODEFAULTFLOWID) == 0) {
335 /* unconditionally set flowid */
336 m->m_pkthdr.flowid = inp->inp_flowid;
337 M_HASHTYPE_SET(m, inp->inp_flowtype);
341 finaldst = ip6->ip6_dst;
342 bzero(&exthdrs, sizeof(exthdrs));
344 /* Hop-by-Hop options header */
345 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
346 /* Destination options header(1st part) */
347 if (opt->ip6po_rthdr) {
349 * Destination options header(1st part)
350 * This only makes sense with a routing header.
351 * See Section 9.2 of RFC 3542.
352 * Disabling this part just for MIP6 convenience is
353 * a bad idea. We need to think carefully about a
354 * way to make the advanced API coexist with MIP6
355 * options, which might automatically be inserted in
358 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
361 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
362 /* Destination options header(2nd part) */
363 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
368 * IPSec checking which handles several cases.
369 * FAST IPSEC: We re-injected the packet.
370 * XXX: need scope argument.
372 switch(ip6_ipsec_output(&m, inp, &error))
374 case 1: /* Bad packet */
376 case -1: /* IPSec done */
378 case 0: /* No IPSec */
385 * Calculate the total length of the extension header chain.
386 * Keep the length of the unfragmentable part for fragmentation.
389 if (exthdrs.ip6e_hbh)
390 optlen += exthdrs.ip6e_hbh->m_len;
391 if (exthdrs.ip6e_dest1)
392 optlen += exthdrs.ip6e_dest1->m_len;
393 if (exthdrs.ip6e_rthdr)
394 optlen += exthdrs.ip6e_rthdr->m_len;
395 unfragpartlen = optlen + sizeof(struct ip6_hdr);
397 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
398 if (exthdrs.ip6e_dest2)
399 optlen += exthdrs.ip6e_dest2->m_len;
402 * If there is at least one extension header,
403 * separate IP6 header from the payload.
405 if (optlen && !hdrsplit) {
406 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
410 m = exthdrs.ip6e_ip6;
415 ip6 = mtod(m, struct ip6_hdr *);
417 /* adjust mbuf packet header length */
418 m->m_pkthdr.len += optlen;
419 plen = m->m_pkthdr.len - sizeof(*ip6);
421 /* If this is a jumbo payload, insert a jumbo payload option. */
422 if (plen > IPV6_MAXPACKET) {
424 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
428 m = exthdrs.ip6e_ip6;
432 ip6 = mtod(m, struct ip6_hdr *);
433 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
437 ip6->ip6_plen = htons(plen);
440 * Concatenate headers and fill in next header fields.
441 * Here we have, on "m"
443 * and we insert headers accordingly. Finally, we should be getting:
444 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
446 * during the header composing process, "m" points to IPv6 header.
447 * "mprev" points to an extension header prior to esp.
449 u_char *nexthdrp = &ip6->ip6_nxt;
453 * we treat dest2 specially. this makes IPsec processing
454 * much easier. the goal here is to make mprev point the
455 * mbuf prior to dest2.
457 * result: IPv6 dest2 payload
458 * m and mprev will point to IPv6 header.
460 if (exthdrs.ip6e_dest2) {
462 panic("assumption failed: hdr not split");
463 exthdrs.ip6e_dest2->m_next = m->m_next;
464 m->m_next = exthdrs.ip6e_dest2;
465 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
466 ip6->ip6_nxt = IPPROTO_DSTOPTS;
470 * result: IPv6 hbh dest1 rthdr dest2 payload
471 * m will point to IPv6 header. mprev will point to the
472 * extension header prior to dest2 (rthdr in the above case).
474 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
475 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
477 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
481 * If there is a routing header, discard the packet.
483 if (exthdrs.ip6e_rthdr) {
488 /* Source address validation */
489 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
490 (flags & IPV6_UNSPECSRC) == 0) {
492 IP6STAT_INC(ip6s_badscope);
495 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
497 IP6STAT_INC(ip6s_badscope);
501 IP6STAT_INC(ip6s_localout);
508 bzero((caddr_t)ro, sizeof(*ro));
511 if (opt && opt->ip6po_rthdr)
512 ro = &opt->ip6po_route;
513 dst = (struct sockaddr_in6 *)&ro->ro_dst;
515 if (ro->ro_rt == NULL)
516 (void )flowtable_lookup(AF_INET6, m, (struct route *)ro);
518 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
521 * if specified, try to fill in the traffic class field.
522 * do not override if a non-zero value is already set.
523 * we check the diffserv field and the ecn field separately.
525 if (opt && opt->ip6po_tclass >= 0) {
528 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
530 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
533 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
536 /* fill in or override the hop limit field, if necessary. */
537 if (opt && opt->ip6po_hlim != -1)
538 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
539 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
541 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
543 ip6->ip6_hlim = V_ip6_defmcasthlim;
547 ip6 = mtod(m, struct ip6_hdr *);
550 * Validate route against routing table additions;
551 * a better/more specific route might have been added.
552 * Make sure address family is set in route.
555 ro->ro_dst.sin6_family = AF_INET6;
556 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
558 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
559 ro->ro_dst.sin6_family == AF_INET6 &&
560 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
562 ifp = ro->ro_rt->rt_ifp;
564 if (fwd_tag == NULL) {
565 bzero(&dst_sa, sizeof(dst_sa));
566 dst_sa.sin6_family = AF_INET6;
567 dst_sa.sin6_len = sizeof(dst_sa);
568 dst_sa.sin6_addr = ip6->ip6_dst;
570 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
574 in6_ifstat_inc(ifp, ifs6_out_discard);
580 * If in6_selectroute() does not return a route entry,
581 * dst may not have been updated.
583 *dst = dst_sa; /* XXX */
587 * then rt (for unicast) and ifp must be non-NULL valid values.
589 if ((flags & IPV6_FORWARDING) == 0) {
590 /* XXX: the FORWARDING flag can be set for mrouting. */
591 in6_ifstat_inc(ifp, ifs6_out_request);
594 ia = (struct in6_ifaddr *)(rt->rt_ifa);
595 counter_u64_add(rt->rt_pksent, 1);
600 * The outgoing interface must be in the zone of source and
601 * destination addresses.
606 if (in6_setscope(&src0, origifp, &zone))
608 bzero(&src_sa, sizeof(src_sa));
609 src_sa.sin6_family = AF_INET6;
610 src_sa.sin6_len = sizeof(src_sa);
611 src_sa.sin6_addr = ip6->ip6_src;
612 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
616 if (in6_setscope(&dst0, origifp, &zone))
618 /* re-initialize to be sure */
619 bzero(&dst_sa, sizeof(dst_sa));
620 dst_sa.sin6_family = AF_INET6;
621 dst_sa.sin6_len = sizeof(dst_sa);
622 dst_sa.sin6_addr = ip6->ip6_dst;
623 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
627 /* We should use ia_ifp to support the case of
628 * sending packets to an address of our own.
630 if (ia != NULL && ia->ia_ifp)
633 /* scope check is done. */
637 IP6STAT_INC(ip6s_badscope);
638 in6_ifstat_inc(origifp, ifs6_out_discard);
640 error = EHOSTUNREACH; /* XXX */
644 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
645 if (opt && opt->ip6po_nextroute.ro_rt) {
647 * The nexthop is explicitly specified by the
648 * application. We assume the next hop is an IPv6
651 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
653 else if ((rt->rt_flags & RTF_GATEWAY))
654 dst = (struct sockaddr_in6 *)rt->rt_gateway;
657 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
658 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
660 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
661 in6_ifstat_inc(ifp, ifs6_out_mcast);
663 * Confirm that the outgoing interface supports multicast.
665 if (!(ifp->if_flags & IFF_MULTICAST)) {
666 IP6STAT_INC(ip6s_noroute);
667 in6_ifstat_inc(ifp, ifs6_out_discard);
671 if ((im6o == NULL && in6_mcast_loop) ||
672 (im6o && im6o->im6o_multicast_loop)) {
674 * Loop back multicast datagram if not expressly
675 * forbidden to do so, even if we have not joined
676 * the address; protocols will filter it later,
677 * thus deferring a hash lookup and lock acquisition
678 * at the expense of an m_copym().
680 ip6_mloopback(ifp, m);
683 * If we are acting as a multicast router, perform
684 * multicast forwarding as if the packet had just
685 * arrived on the interface to which we are about
686 * to send. The multicast forwarding function
687 * recursively calls this function, using the
688 * IPV6_FORWARDING flag to prevent infinite recursion.
690 * Multicasts that are looped back by ip6_mloopback(),
691 * above, will be forwarded by the ip6_input() routine,
694 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
696 * XXX: ip6_mforward expects that rcvif is NULL
697 * when it is called from the originating path.
698 * However, it may not always be the case.
700 m->m_pkthdr.rcvif = NULL;
701 if (ip6_mforward(ip6, ifp, m) != 0) {
708 * Multicasts with a hoplimit of zero may be looped back,
709 * above, but must not be transmitted on a network.
710 * Also, multicasts addressed to the loopback interface
711 * are not sent -- the above call to ip6_mloopback() will
712 * loop back a copy if this host actually belongs to the
713 * destination group on the loopback interface.
715 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
716 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
723 * Fill the outgoing inteface to tell the upper layer
724 * to increment per-interface statistics.
729 /* Determine path MTU. */
730 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &finaldst, &mtu,
731 &alwaysfrag, fibnum)) != 0)
735 * The caller of this function may specify to use the minimum MTU
737 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
738 * setting. The logic is a bit complicated; by default, unicast
739 * packets will follow path MTU while multicast packets will be sent at
740 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
741 * including unicast ones will be sent at the minimum MTU. Multicast
742 * packets will always be sent at the minimum MTU unless
743 * IP6PO_MINMTU_DISABLE is explicitly specified.
744 * See RFC 3542 for more details.
746 if (mtu > IPV6_MMTU) {
747 if ((flags & IPV6_MINMTU))
749 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
751 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
753 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
759 * clear embedded scope identifiers if necessary.
760 * in6_clearscope will touch the addresses only when necessary.
762 in6_clearscope(&ip6->ip6_src);
763 in6_clearscope(&ip6->ip6_dst);
766 * If the outgoing packet contains a hop-by-hop options header,
767 * it must be examined and processed even by the source node.
768 * (RFC 2460, section 4.)
770 if (exthdrs.ip6e_hbh) {
771 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
772 u_int32_t dummy; /* XXX unused */
773 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
776 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
777 panic("ip6e_hbh is not contiguous");
780 * XXX: if we have to send an ICMPv6 error to the sender,
781 * we need the M_LOOP flag since icmp6_error() expects
782 * the IPv6 and the hop-by-hop options header are
783 * contiguous unless the flag is set.
785 m->m_flags |= M_LOOP;
786 m->m_pkthdr.rcvif = ifp;
787 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
788 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
789 &dummy, &plen) < 0) {
790 /* m was already freed at this point */
791 error = EINVAL;/* better error? */
794 m->m_flags &= ~M_LOOP; /* XXX */
795 m->m_pkthdr.rcvif = NULL;
798 /* Jump over all PFIL processing if hooks are not active. */
799 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
803 /* Run through list of hooks for output packets. */
804 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
805 if (error != 0 || m == NULL)
807 ip6 = mtod(m, struct ip6_hdr *);
810 /* See if destination IP address was changed by packet filter. */
811 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
812 m->m_flags |= M_SKIP_FIREWALL;
813 /* If destination is now ourself drop to ip6_input(). */
814 if (in6_localip(&ip6->ip6_dst)) {
815 m->m_flags |= M_FASTFWD_OURS;
816 if (m->m_pkthdr.rcvif == NULL)
817 m->m_pkthdr.rcvif = V_loif;
818 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
819 m->m_pkthdr.csum_flags |=
820 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
821 m->m_pkthdr.csum_data = 0xffff;
824 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
825 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
827 error = netisr_queue(NETISR_IPV6, m);
830 needfiblookup = 1; /* Redo the routing table lookup. */
832 /* See if fib was changed by packet filter. */
833 if (fibnum != M_GETFIB(m)) {
834 m->m_flags |= M_SKIP_FIREWALL;
835 fibnum = M_GETFIB(m);
842 /* See if local, if yes, send it to netisr. */
843 if (m->m_flags & M_FASTFWD_OURS) {
844 if (m->m_pkthdr.rcvif == NULL)
845 m->m_pkthdr.rcvif = V_loif;
846 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
847 m->m_pkthdr.csum_flags |=
848 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
849 m->m_pkthdr.csum_data = 0xffff;
852 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
853 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
855 error = netisr_queue(NETISR_IPV6, m);
858 /* Or forward to some other address? */
859 if ((m->m_flags & M_IP6_NEXTHOP) &&
860 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
861 dst = (struct sockaddr_in6 *)&ro->ro_dst;
862 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
863 m->m_flags |= M_SKIP_FIREWALL;
864 m->m_flags &= ~M_IP6_NEXTHOP;
865 m_tag_delete(m, fwd_tag);
871 * Send the packet to the outgoing interface.
872 * If necessary, do IPv6 fragmentation before sending.
874 * the logic here is rather complex:
875 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
876 * 1-a: send as is if tlen <= path mtu
877 * 1-b: fragment if tlen > path mtu
879 * 2: if user asks us not to fragment (dontfrag == 1)
880 * 2-a: send as is if tlen <= interface mtu
881 * 2-b: error if tlen > interface mtu
883 * 3: if we always need to attach fragment header (alwaysfrag == 1)
886 * 4: if dontfrag == 1 && alwaysfrag == 1
887 * error, as we cannot handle this conflicting request
889 sw_csum = m->m_pkthdr.csum_flags;
891 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
892 sw_csum &= ~ifp->if_hwassist;
896 * If we added extension headers, we will not do TSO and calculate the
897 * checksums ourselves for now.
898 * XXX-BZ Need a framework to know when the NIC can handle it, even
901 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
902 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
903 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
906 if (sw_csum & CSUM_SCTP_IPV6) {
907 sw_csum &= ~CSUM_SCTP_IPV6;
908 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
911 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
912 tlen = m->m_pkthdr.len;
914 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
918 if (dontfrag && alwaysfrag) { /* case 4 */
919 /* conflicting request - can't transmit */
923 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
925 * Even if the DONTFRAG option is specified, we cannot send the
926 * packet when the data length is larger than the MTU of the
927 * outgoing interface.
928 * Notify the error by sending IPV6_PATHMTU ancillary data if
929 * application wanted to know the MTU value. Also return an
930 * error code (this is not described in the API spec).
933 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
939 * transmit packet without fragmentation
941 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
942 struct in6_ifaddr *ia6;
944 ip6 = mtod(m, struct ip6_hdr *);
945 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
947 /* Record statistics for this interface address. */
948 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
949 counter_u64_add(ia6->ia_ifa.ifa_obytes,
951 ifa_free(&ia6->ia_ifa);
953 error = nd6_output_ifp(ifp, origifp, m, dst,
959 * try to fragment the packet. case 1-b and 3
961 if (mtu < IPV6_MMTU) {
962 /* path MTU cannot be less than IPV6_MMTU */
964 in6_ifstat_inc(ifp, ifs6_out_fragfail);
966 } else if (ip6->ip6_plen == 0) {
967 /* jumbo payload cannot be fragmented */
969 in6_ifstat_inc(ifp, ifs6_out_fragfail);
975 * Too large for the destination or interface;
976 * fragment if possible.
977 * Must be able to put at least 8 bytes per fragment.
979 hlen = unfragpartlen;
980 if (mtu > IPV6_MAXPACKET)
981 mtu = IPV6_MAXPACKET;
983 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
986 in6_ifstat_inc(ifp, ifs6_out_fragfail);
991 * If the interface will not calculate checksums on
992 * fragmented packets, then do it here.
993 * XXX-BZ handle the hw offloading case. Need flags.
995 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
996 in6_delayed_cksum(m, plen, hlen);
997 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1000 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1001 sctp_delayed_cksum(m, hlen);
1002 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1006 * Change the next header field of the last header in the
1007 * unfragmentable part.
1009 if (exthdrs.ip6e_rthdr) {
1010 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1011 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1012 } else if (exthdrs.ip6e_dest1) {
1013 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1014 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1015 } else if (exthdrs.ip6e_hbh) {
1016 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1017 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1019 nextproto = ip6->ip6_nxt;
1020 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1024 * Loop through length of segment after first fragment,
1025 * make new header and copy data of each part and link onto
1029 id = htonl(ip6_randomid());
1030 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1033 in6_ifstat_inc(ifp, ifs6_out_fragok);
1037 * Remove leading garbages.
1043 for (m0 = m; m; m = m0) {
1047 /* Record statistics for this interface address. */
1049 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1050 counter_u64_add(ia->ia_ifa.ifa_obytes,
1053 error = nd6_output_ifp(ifp, origifp, m, dst,
1054 (struct route *)ro);
1060 IP6STAT_INC(ip6s_fragmented);
1063 if (ro == &ip6route)
1068 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1069 m_freem(exthdrs.ip6e_dest1);
1070 m_freem(exthdrs.ip6e_rthdr);
1071 m_freem(exthdrs.ip6e_dest2);
1080 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1084 if (hlen > MCLBYTES)
1085 return (ENOBUFS); /* XXX */
1088 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1090 m = m_get(M_NOWAIT, MT_DATA);
1095 bcopy(hdr, mtod(m, caddr_t), hlen);
1102 * Insert jumbo payload option.
1105 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1111 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1114 * If there is no hop-by-hop options header, allocate new one.
1115 * If there is one but it doesn't have enough space to store the
1116 * jumbo payload option, allocate a cluster to store the whole options.
1117 * Otherwise, use it to store the options.
1119 if (exthdrs->ip6e_hbh == 0) {
1120 mopt = m_get(M_NOWAIT, MT_DATA);
1123 mopt->m_len = JUMBOOPTLEN;
1124 optbuf = mtod(mopt, u_char *);
1125 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1126 exthdrs->ip6e_hbh = mopt;
1128 struct ip6_hbh *hbh;
1130 mopt = exthdrs->ip6e_hbh;
1131 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1134 * - exthdrs->ip6e_hbh is not referenced from places
1135 * other than exthdrs.
1136 * - exthdrs->ip6e_hbh is not an mbuf chain.
1138 int oldoptlen = mopt->m_len;
1142 * XXX: give up if the whole (new) hbh header does
1143 * not fit even in an mbuf cluster.
1145 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1149 * As a consequence, we must always prepare a cluster
1152 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1155 n->m_len = oldoptlen + JUMBOOPTLEN;
1156 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1158 optbuf = mtod(n, caddr_t) + oldoptlen;
1160 mopt = exthdrs->ip6e_hbh = n;
1162 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1163 mopt->m_len += JUMBOOPTLEN;
1165 optbuf[0] = IP6OPT_PADN;
1169 * Adjust the header length according to the pad and
1170 * the jumbo payload option.
1172 hbh = mtod(mopt, struct ip6_hbh *);
1173 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1176 /* fill in the option. */
1177 optbuf[2] = IP6OPT_JUMBO;
1179 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1180 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1182 /* finally, adjust the packet header length */
1183 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1190 * Insert fragment header and copy unfragmentable header portions.
1193 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1194 struct ip6_frag **frghdrp)
1196 struct mbuf *n, *mlast;
1198 if (hlen > sizeof(struct ip6_hdr)) {
1199 n = m_copym(m0, sizeof(struct ip6_hdr),
1200 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1207 /* Search for the last mbuf of unfragmentable part. */
1208 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1211 if (M_WRITABLE(mlast) &&
1212 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1213 /* use the trailing space of the last mbuf for the fragment hdr */
1214 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1216 mlast->m_len += sizeof(struct ip6_frag);
1217 m->m_pkthdr.len += sizeof(struct ip6_frag);
1219 /* allocate a new mbuf for the fragment header */
1222 mfrg = m_get(M_NOWAIT, MT_DATA);
1225 mfrg->m_len = sizeof(struct ip6_frag);
1226 *frghdrp = mtod(mfrg, struct ip6_frag *);
1227 mlast->m_next = mfrg;
1234 * Calculates IPv6 path mtu for destination @dst.
1235 * Resulting MTU is stored in @mtup.
1237 * Returns 0 on success.
1240 ip6_getpmtu_ctl(u_int fibnum, struct in6_addr *dst, u_long *mtup)
1242 struct nhop6_extended nh6;
1243 struct in6_addr kdst;
1249 in6_splitscope(dst, &kdst, &scopeid);
1250 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1251 return (EHOSTUNREACH);
1256 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL);
1257 fib6_free_nh_ext(fibnum, &nh6);
1263 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1264 * and cached data in @ro_pmtu.
1265 * MTU from (successful) route lookup is saved (along with dst)
1266 * inside @ro_pmtu to avoid subsequent route lookups after packet
1267 * filter processing.
1269 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1270 * Returns 0 on success.
1273 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1274 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1275 int *alwaysfragp, u_int fibnum)
1277 struct nhop6_basic nh6;
1278 struct in6_addr kdst;
1280 struct sockaddr_in6 *sa6_dst;
1287 * Here ro_pmtu has final destination address, while
1288 * ro might represent immediate destination.
1289 * Use ro_pmtu destination since mtu might differ.
1291 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1292 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1293 ro_pmtu->ro_mtu = 0;
1295 if (ro_pmtu->ro_mtu == 0) {
1296 bzero(sa6_dst, sizeof(*sa6_dst));
1297 sa6_dst->sin6_family = AF_INET6;
1298 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1299 sa6_dst->sin6_addr = *dst;
1301 in6_splitscope(dst, &kdst, &scopeid);
1302 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1304 ro_pmtu->ro_mtu = nh6.nh_mtu;
1307 mtu = ro_pmtu->ro_mtu;
1311 mtu = ro_pmtu->ro_rt->rt_mtu;
1313 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp));
1317 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1318 * hostcache data for @dst.
1319 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1321 * Returns 0 on success.
1324 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1325 u_long *mtup, int *alwaysfragp)
1333 struct in_conninfo inc;
1335 bzero(&inc, sizeof(inc));
1336 inc.inc_flags |= INC_ISIPV6;
1337 inc.inc6_faddr = *dst;
1339 ifmtu = IN6_LINKMTU(ifp);
1340 mtu = tcp_hc_getmtu(&inc);
1342 mtu = min(mtu, rt_mtu);
1347 else if (mtu < IPV6_MMTU) {
1349 * RFC2460 section 5, last paragraph:
1350 * if we record ICMPv6 too big message with
1351 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1352 * or smaller, with framgent header attached.
1353 * (fragment header is needed regardless from the
1354 * packet size, for translators to identify packets)
1360 mtu = IN6_LINKMTU(ifp);
1362 error = EHOSTUNREACH; /* XXX */
1366 *alwaysfragp = alwaysfrag;
1371 * IP6 socket option processing.
1374 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1376 int optdatalen, uproto;
1378 struct inpcb *in6p = sotoinpcb(so);
1380 int level, op, optname;
1384 uint32_t rss_bucket;
1388 level = sopt->sopt_level;
1389 op = sopt->sopt_dir;
1390 optname = sopt->sopt_name;
1391 optlen = sopt->sopt_valsize;
1395 uproto = (int)so->so_proto->pr_protocol;
1397 if (level != IPPROTO_IPV6) {
1400 if (sopt->sopt_level == SOL_SOCKET &&
1401 sopt->sopt_dir == SOPT_SET) {
1402 switch (sopt->sopt_name) {
1405 if ((so->so_options & SO_REUSEADDR) != 0)
1406 in6p->inp_flags2 |= INP_REUSEADDR;
1408 in6p->inp_flags2 &= ~INP_REUSEADDR;
1414 if ((so->so_options & SO_REUSEPORT) != 0)
1415 in6p->inp_flags2 |= INP_REUSEPORT;
1417 in6p->inp_flags2 &= ~INP_REUSEPORT;
1423 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1431 } else { /* level == IPPROTO_IPV6 */
1436 case IPV6_2292PKTOPTIONS:
1437 #ifdef IPV6_PKTOPTIONS
1438 case IPV6_PKTOPTIONS:
1443 error = soopt_getm(sopt, &m); /* XXX */
1446 error = soopt_mcopyin(sopt, m); /* XXX */
1449 error = ip6_pcbopts(&in6p->in6p_outputopts,
1451 m_freem(m); /* XXX */
1456 * Use of some Hop-by-Hop options or some
1457 * Destination options, might require special
1458 * privilege. That is, normal applications
1459 * (without special privilege) might be forbidden
1460 * from setting certain options in outgoing packets,
1461 * and might never see certain options in received
1462 * packets. [RFC 2292 Section 6]
1463 * KAME specific note:
1464 * KAME prevents non-privileged users from sending or
1465 * receiving ANY hbh/dst options in order to avoid
1466 * overhead of parsing options in the kernel.
1468 case IPV6_RECVHOPOPTS:
1469 case IPV6_RECVDSTOPTS:
1470 case IPV6_RECVRTHDRDSTOPTS:
1472 error = priv_check(td,
1473 PRIV_NETINET_SETHDROPTS);
1478 case IPV6_UNICAST_HOPS:
1481 case IPV6_RECVPKTINFO:
1482 case IPV6_RECVHOPLIMIT:
1483 case IPV6_RECVRTHDR:
1484 case IPV6_RECVPATHMTU:
1485 case IPV6_RECVTCLASS:
1486 case IPV6_RECVFLOWID:
1488 case IPV6_RECVRSSBUCKETID:
1491 case IPV6_AUTOFLOWLABEL:
1493 case IPV6_BINDMULTI:
1495 case IPV6_RSS_LISTEN_BUCKET:
1497 if (optname == IPV6_BINDANY && td != NULL) {
1498 error = priv_check(td,
1499 PRIV_NETINET_BINDANY);
1504 if (optlen != sizeof(int)) {
1508 error = sooptcopyin(sopt, &optval,
1509 sizeof optval, sizeof optval);
1514 case IPV6_UNICAST_HOPS:
1515 if (optval < -1 || optval >= 256)
1518 /* -1 = kernel default */
1519 in6p->in6p_hops = optval;
1520 if ((in6p->inp_vflag &
1522 in6p->inp_ip_ttl = optval;
1525 #define OPTSET(bit) \
1529 in6p->inp_flags |= (bit); \
1531 in6p->inp_flags &= ~(bit); \
1532 INP_WUNLOCK(in6p); \
1533 } while (/*CONSTCOND*/ 0)
1534 #define OPTSET2292(bit) \
1537 in6p->inp_flags |= IN6P_RFC2292; \
1539 in6p->inp_flags |= (bit); \
1541 in6p->inp_flags &= ~(bit); \
1542 INP_WUNLOCK(in6p); \
1543 } while (/*CONSTCOND*/ 0)
1544 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1546 #define OPTSET2(bit, val) do { \
1549 in6p->inp_flags2 |= bit; \
1551 in6p->inp_flags2 &= ~bit; \
1552 INP_WUNLOCK(in6p); \
1554 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1556 case IPV6_RECVPKTINFO:
1557 /* cannot mix with RFC2292 */
1558 if (OPTBIT(IN6P_RFC2292)) {
1562 OPTSET(IN6P_PKTINFO);
1567 struct ip6_pktopts **optp;
1569 /* cannot mix with RFC2292 */
1570 if (OPTBIT(IN6P_RFC2292)) {
1574 optp = &in6p->in6p_outputopts;
1575 error = ip6_pcbopt(IPV6_HOPLIMIT,
1576 (u_char *)&optval, sizeof(optval),
1577 optp, (td != NULL) ? td->td_ucred :
1582 case IPV6_RECVHOPLIMIT:
1583 /* cannot mix with RFC2292 */
1584 if (OPTBIT(IN6P_RFC2292)) {
1588 OPTSET(IN6P_HOPLIMIT);
1591 case IPV6_RECVHOPOPTS:
1592 /* cannot mix with RFC2292 */
1593 if (OPTBIT(IN6P_RFC2292)) {
1597 OPTSET(IN6P_HOPOPTS);
1600 case IPV6_RECVDSTOPTS:
1601 /* cannot mix with RFC2292 */
1602 if (OPTBIT(IN6P_RFC2292)) {
1606 OPTSET(IN6P_DSTOPTS);
1609 case IPV6_RECVRTHDRDSTOPTS:
1610 /* cannot mix with RFC2292 */
1611 if (OPTBIT(IN6P_RFC2292)) {
1615 OPTSET(IN6P_RTHDRDSTOPTS);
1618 case IPV6_RECVRTHDR:
1619 /* cannot mix with RFC2292 */
1620 if (OPTBIT(IN6P_RFC2292)) {
1627 case IPV6_RECVPATHMTU:
1629 * We ignore this option for TCP
1631 * (RFC3542 leaves this case
1634 if (uproto != IPPROTO_TCP)
1638 case IPV6_RECVFLOWID:
1639 OPTSET2(INP_RECVFLOWID, optval);
1643 case IPV6_RECVRSSBUCKETID:
1644 OPTSET2(INP_RECVRSSBUCKETID, optval);
1650 * make setsockopt(IPV6_V6ONLY)
1651 * available only prior to bind(2).
1652 * see ipng mailing list, Jun 22 2001.
1654 if (in6p->inp_lport ||
1655 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1659 OPTSET(IN6P_IPV6_V6ONLY);
1661 in6p->inp_vflag &= ~INP_IPV4;
1663 in6p->inp_vflag |= INP_IPV4;
1665 case IPV6_RECVTCLASS:
1666 /* cannot mix with RFC2292 XXX */
1667 if (OPTBIT(IN6P_RFC2292)) {
1671 OPTSET(IN6P_TCLASS);
1673 case IPV6_AUTOFLOWLABEL:
1674 OPTSET(IN6P_AUTOFLOWLABEL);
1678 OPTSET(INP_BINDANY);
1681 case IPV6_BINDMULTI:
1682 OPTSET2(INP_BINDMULTI, optval);
1685 case IPV6_RSS_LISTEN_BUCKET:
1686 if ((optval >= 0) &&
1687 (optval < rss_getnumbuckets())) {
1688 in6p->inp_rss_listen_bucket = optval;
1689 OPTSET2(INP_RSS_BUCKET_SET, 1);
1700 case IPV6_USE_MIN_MTU:
1701 case IPV6_PREFER_TEMPADDR:
1702 if (optlen != sizeof(optval)) {
1706 error = sooptcopyin(sopt, &optval,
1707 sizeof optval, sizeof optval);
1711 struct ip6_pktopts **optp;
1712 optp = &in6p->in6p_outputopts;
1713 error = ip6_pcbopt(optname,
1714 (u_char *)&optval, sizeof(optval),
1715 optp, (td != NULL) ? td->td_ucred :
1720 case IPV6_2292PKTINFO:
1721 case IPV6_2292HOPLIMIT:
1722 case IPV6_2292HOPOPTS:
1723 case IPV6_2292DSTOPTS:
1724 case IPV6_2292RTHDR:
1726 if (optlen != sizeof(int)) {
1730 error = sooptcopyin(sopt, &optval,
1731 sizeof optval, sizeof optval);
1735 case IPV6_2292PKTINFO:
1736 OPTSET2292(IN6P_PKTINFO);
1738 case IPV6_2292HOPLIMIT:
1739 OPTSET2292(IN6P_HOPLIMIT);
1741 case IPV6_2292HOPOPTS:
1743 * Check super-user privilege.
1744 * See comments for IPV6_RECVHOPOPTS.
1747 error = priv_check(td,
1748 PRIV_NETINET_SETHDROPTS);
1752 OPTSET2292(IN6P_HOPOPTS);
1754 case IPV6_2292DSTOPTS:
1756 error = priv_check(td,
1757 PRIV_NETINET_SETHDROPTS);
1761 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1763 case IPV6_2292RTHDR:
1764 OPTSET2292(IN6P_RTHDR);
1772 case IPV6_RTHDRDSTOPTS:
1775 /* new advanced API (RFC3542) */
1777 u_char optbuf_storage[MCLBYTES];
1779 struct ip6_pktopts **optp;
1781 /* cannot mix with RFC2292 */
1782 if (OPTBIT(IN6P_RFC2292)) {
1788 * We only ensure valsize is not too large
1789 * here. Further validation will be done
1792 error = sooptcopyin(sopt, optbuf_storage,
1793 sizeof(optbuf_storage), 0);
1796 optlen = sopt->sopt_valsize;
1797 optbuf = optbuf_storage;
1798 optp = &in6p->in6p_outputopts;
1799 error = ip6_pcbopt(optname, optbuf, optlen,
1800 optp, (td != NULL) ? td->td_ucred : NULL,
1806 case IPV6_MULTICAST_IF:
1807 case IPV6_MULTICAST_HOPS:
1808 case IPV6_MULTICAST_LOOP:
1809 case IPV6_JOIN_GROUP:
1810 case IPV6_LEAVE_GROUP:
1812 case MCAST_BLOCK_SOURCE:
1813 case MCAST_UNBLOCK_SOURCE:
1814 case MCAST_JOIN_GROUP:
1815 case MCAST_LEAVE_GROUP:
1816 case MCAST_JOIN_SOURCE_GROUP:
1817 case MCAST_LEAVE_SOURCE_GROUP:
1818 error = ip6_setmoptions(in6p, sopt);
1821 case IPV6_PORTRANGE:
1822 error = sooptcopyin(sopt, &optval,
1823 sizeof optval, sizeof optval);
1829 case IPV6_PORTRANGE_DEFAULT:
1830 in6p->inp_flags &= ~(INP_LOWPORT);
1831 in6p->inp_flags &= ~(INP_HIGHPORT);
1834 case IPV6_PORTRANGE_HIGH:
1835 in6p->inp_flags &= ~(INP_LOWPORT);
1836 in6p->inp_flags |= INP_HIGHPORT;
1839 case IPV6_PORTRANGE_LOW:
1840 in6p->inp_flags &= ~(INP_HIGHPORT);
1841 in6p->inp_flags |= INP_LOWPORT;
1852 case IPV6_IPSEC_POLICY:
1857 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1859 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1861 req = mtod(m, caddr_t);
1862 error = ipsec_set_policy(in6p, optname, req,
1863 m->m_len, (sopt->sopt_td != NULL) ?
1864 sopt->sopt_td->td_ucred : NULL);
1871 error = ENOPROTOOPT;
1879 case IPV6_2292PKTOPTIONS:
1880 #ifdef IPV6_PKTOPTIONS
1881 case IPV6_PKTOPTIONS:
1884 * RFC3542 (effectively) deprecated the
1885 * semantics of the 2292-style pktoptions.
1886 * Since it was not reliable in nature (i.e.,
1887 * applications had to expect the lack of some
1888 * information after all), it would make sense
1889 * to simplify this part by always returning
1892 sopt->sopt_valsize = 0;
1895 case IPV6_RECVHOPOPTS:
1896 case IPV6_RECVDSTOPTS:
1897 case IPV6_RECVRTHDRDSTOPTS:
1898 case IPV6_UNICAST_HOPS:
1899 case IPV6_RECVPKTINFO:
1900 case IPV6_RECVHOPLIMIT:
1901 case IPV6_RECVRTHDR:
1902 case IPV6_RECVPATHMTU:
1905 case IPV6_PORTRANGE:
1906 case IPV6_RECVTCLASS:
1907 case IPV6_AUTOFLOWLABEL:
1911 case IPV6_RECVFLOWID:
1913 case IPV6_RSSBUCKETID:
1914 case IPV6_RECVRSSBUCKETID:
1916 case IPV6_BINDMULTI:
1919 case IPV6_RECVHOPOPTS:
1920 optval = OPTBIT(IN6P_HOPOPTS);
1923 case IPV6_RECVDSTOPTS:
1924 optval = OPTBIT(IN6P_DSTOPTS);
1927 case IPV6_RECVRTHDRDSTOPTS:
1928 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1931 case IPV6_UNICAST_HOPS:
1932 optval = in6p->in6p_hops;
1935 case IPV6_RECVPKTINFO:
1936 optval = OPTBIT(IN6P_PKTINFO);
1939 case IPV6_RECVHOPLIMIT:
1940 optval = OPTBIT(IN6P_HOPLIMIT);
1943 case IPV6_RECVRTHDR:
1944 optval = OPTBIT(IN6P_RTHDR);
1947 case IPV6_RECVPATHMTU:
1948 optval = OPTBIT(IN6P_MTU);
1952 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1955 case IPV6_PORTRANGE:
1958 flags = in6p->inp_flags;
1959 if (flags & INP_HIGHPORT)
1960 optval = IPV6_PORTRANGE_HIGH;
1961 else if (flags & INP_LOWPORT)
1962 optval = IPV6_PORTRANGE_LOW;
1967 case IPV6_RECVTCLASS:
1968 optval = OPTBIT(IN6P_TCLASS);
1971 case IPV6_AUTOFLOWLABEL:
1972 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1976 optval = OPTBIT(INP_BINDANY);
1980 optval = in6p->inp_flowid;
1984 optval = in6p->inp_flowtype;
1987 case IPV6_RECVFLOWID:
1988 optval = OPTBIT2(INP_RECVFLOWID);
1991 case IPV6_RSSBUCKETID:
1993 rss_hash2bucket(in6p->inp_flowid,
1997 optval = rss_bucket;
2002 case IPV6_RECVRSSBUCKETID:
2003 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2007 case IPV6_BINDMULTI:
2008 optval = OPTBIT2(INP_BINDMULTI);
2014 error = sooptcopyout(sopt, &optval,
2021 struct ip6_mtuinfo mtuinfo;
2023 if (!(so->so_state & SS_ISCONNECTED))
2026 * XXX: we dot not consider the case of source
2027 * routing, or optional information to specify
2028 * the outgoing interface.
2030 error = ip6_getpmtu_ctl(so->so_fibnum,
2031 &in6p->in6p_faddr, &pmtu);
2034 if (pmtu > IPV6_MAXPACKET)
2035 pmtu = IPV6_MAXPACKET;
2037 bzero(&mtuinfo, sizeof(mtuinfo));
2038 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2039 optdata = (void *)&mtuinfo;
2040 optdatalen = sizeof(mtuinfo);
2041 error = sooptcopyout(sopt, optdata,
2046 case IPV6_2292PKTINFO:
2047 case IPV6_2292HOPLIMIT:
2048 case IPV6_2292HOPOPTS:
2049 case IPV6_2292RTHDR:
2050 case IPV6_2292DSTOPTS:
2052 case IPV6_2292PKTINFO:
2053 optval = OPTBIT(IN6P_PKTINFO);
2055 case IPV6_2292HOPLIMIT:
2056 optval = OPTBIT(IN6P_HOPLIMIT);
2058 case IPV6_2292HOPOPTS:
2059 optval = OPTBIT(IN6P_HOPOPTS);
2061 case IPV6_2292RTHDR:
2062 optval = OPTBIT(IN6P_RTHDR);
2064 case IPV6_2292DSTOPTS:
2065 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2068 error = sooptcopyout(sopt, &optval,
2075 case IPV6_RTHDRDSTOPTS:
2079 case IPV6_USE_MIN_MTU:
2080 case IPV6_PREFER_TEMPADDR:
2081 error = ip6_getpcbopt(in6p->in6p_outputopts,
2085 case IPV6_MULTICAST_IF:
2086 case IPV6_MULTICAST_HOPS:
2087 case IPV6_MULTICAST_LOOP:
2089 error = ip6_getmoptions(in6p, sopt);
2093 case IPV6_IPSEC_POLICY:
2097 struct mbuf *m = NULL;
2098 struct mbuf **mp = &m;
2099 size_t ovalsize = sopt->sopt_valsize;
2100 caddr_t oval = (caddr_t)sopt->sopt_val;
2102 error = soopt_getm(sopt, &m); /* XXX */
2105 error = soopt_mcopyin(sopt, m); /* XXX */
2108 sopt->sopt_valsize = ovalsize;
2109 sopt->sopt_val = oval;
2111 req = mtod(m, caddr_t);
2114 error = ipsec_get_policy(in6p, req, len, mp);
2116 error = soopt_mcopyout(sopt, m); /* XXX */
2117 if (error == 0 && m)
2124 error = ENOPROTOOPT;
2134 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2136 int error = 0, optval, optlen;
2137 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2138 struct inpcb *in6p = sotoinpcb(so);
2139 int level, op, optname;
2141 level = sopt->sopt_level;
2142 op = sopt->sopt_dir;
2143 optname = sopt->sopt_name;
2144 optlen = sopt->sopt_valsize;
2146 if (level != IPPROTO_IPV6) {
2153 * For ICMPv6 sockets, no modification allowed for checksum
2154 * offset, permit "no change" values to help existing apps.
2156 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2157 * for an ICMPv6 socket will fail."
2158 * The current behavior does not meet RFC3542.
2162 if (optlen != sizeof(int)) {
2166 error = sooptcopyin(sopt, &optval, sizeof(optval),
2170 if ((optval % 2) != 0) {
2171 /* the API assumes even offset values */
2173 } else if (so->so_proto->pr_protocol ==
2175 if (optval != icmp6off)
2178 in6p->in6p_cksum = optval;
2182 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2185 optval = in6p->in6p_cksum;
2187 error = sooptcopyout(sopt, &optval, sizeof(optval));
2197 error = ENOPROTOOPT;
2205 * Set up IP6 options in pcb for insertion in output packets or
2206 * specifying behavior of outgoing packets.
2209 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2210 struct socket *so, struct sockopt *sopt)
2212 struct ip6_pktopts *opt = *pktopt;
2214 struct thread *td = sopt->sopt_td;
2216 /* turn off any old options. */
2219 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2220 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2221 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2222 printf("ip6_pcbopts: all specified options are cleared.\n");
2224 ip6_clearpktopts(opt, -1);
2226 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2229 if (!m || m->m_len == 0) {
2231 * Only turning off any previous options, regardless of
2232 * whether the opt is just created or given.
2234 free(opt, M_IP6OPT);
2238 /* set options specified by user. */
2239 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2240 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2241 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2242 free(opt, M_IP6OPT);
2250 * initialize ip6_pktopts. beware that there are non-zero default values in
2254 ip6_initpktopts(struct ip6_pktopts *opt)
2257 bzero(opt, sizeof(*opt));
2258 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2259 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2260 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2261 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2265 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2266 struct ucred *cred, int uproto)
2268 struct ip6_pktopts *opt;
2270 if (*pktopt == NULL) {
2271 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2273 ip6_initpktopts(*pktopt);
2277 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2281 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2283 void *optdata = NULL;
2285 struct ip6_ext *ip6e;
2287 struct in6_pktinfo null_pktinfo;
2288 int deftclass = 0, on;
2289 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2290 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2294 optdata = (void *)&null_pktinfo;
2295 if (pktopt && pktopt->ip6po_pktinfo) {
2296 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2297 sizeof(null_pktinfo));
2298 in6_clearscope(&null_pktinfo.ipi6_addr);
2300 /* XXX: we don't have to do this every time... */
2301 bzero(&null_pktinfo, sizeof(null_pktinfo));
2303 optdatalen = sizeof(struct in6_pktinfo);
2306 if (pktopt && pktopt->ip6po_tclass >= 0)
2307 optdata = (void *)&pktopt->ip6po_tclass;
2309 optdata = (void *)&deftclass;
2310 optdatalen = sizeof(int);
2313 if (pktopt && pktopt->ip6po_hbh) {
2314 optdata = (void *)pktopt->ip6po_hbh;
2315 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2316 optdatalen = (ip6e->ip6e_len + 1) << 3;
2320 if (pktopt && pktopt->ip6po_rthdr) {
2321 optdata = (void *)pktopt->ip6po_rthdr;
2322 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2323 optdatalen = (ip6e->ip6e_len + 1) << 3;
2326 case IPV6_RTHDRDSTOPTS:
2327 if (pktopt && pktopt->ip6po_dest1) {
2328 optdata = (void *)pktopt->ip6po_dest1;
2329 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2330 optdatalen = (ip6e->ip6e_len + 1) << 3;
2334 if (pktopt && pktopt->ip6po_dest2) {
2335 optdata = (void *)pktopt->ip6po_dest2;
2336 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2337 optdatalen = (ip6e->ip6e_len + 1) << 3;
2341 if (pktopt && pktopt->ip6po_nexthop) {
2342 optdata = (void *)pktopt->ip6po_nexthop;
2343 optdatalen = pktopt->ip6po_nexthop->sa_len;
2346 case IPV6_USE_MIN_MTU:
2348 optdata = (void *)&pktopt->ip6po_minmtu;
2350 optdata = (void *)&defminmtu;
2351 optdatalen = sizeof(int);
2354 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2358 optdata = (void *)&on;
2359 optdatalen = sizeof(on);
2361 case IPV6_PREFER_TEMPADDR:
2363 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2365 optdata = (void *)&defpreftemp;
2366 optdatalen = sizeof(int);
2368 default: /* should not happen */
2370 panic("ip6_getpcbopt: unexpected option\n");
2372 return (ENOPROTOOPT);
2375 error = sooptcopyout(sopt, optdata, optdatalen);
2381 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2386 if (optname == -1 || optname == IPV6_PKTINFO) {
2387 if (pktopt->ip6po_pktinfo)
2388 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2389 pktopt->ip6po_pktinfo = NULL;
2391 if (optname == -1 || optname == IPV6_HOPLIMIT)
2392 pktopt->ip6po_hlim = -1;
2393 if (optname == -1 || optname == IPV6_TCLASS)
2394 pktopt->ip6po_tclass = -1;
2395 if (optname == -1 || optname == IPV6_NEXTHOP) {
2396 if (pktopt->ip6po_nextroute.ro_rt) {
2397 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2398 pktopt->ip6po_nextroute.ro_rt = NULL;
2400 if (pktopt->ip6po_nexthop)
2401 free(pktopt->ip6po_nexthop, M_IP6OPT);
2402 pktopt->ip6po_nexthop = NULL;
2404 if (optname == -1 || optname == IPV6_HOPOPTS) {
2405 if (pktopt->ip6po_hbh)
2406 free(pktopt->ip6po_hbh, M_IP6OPT);
2407 pktopt->ip6po_hbh = NULL;
2409 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2410 if (pktopt->ip6po_dest1)
2411 free(pktopt->ip6po_dest1, M_IP6OPT);
2412 pktopt->ip6po_dest1 = NULL;
2414 if (optname == -1 || optname == IPV6_RTHDR) {
2415 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2416 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2417 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2418 if (pktopt->ip6po_route.ro_rt) {
2419 RTFREE(pktopt->ip6po_route.ro_rt);
2420 pktopt->ip6po_route.ro_rt = NULL;
2423 if (optname == -1 || optname == IPV6_DSTOPTS) {
2424 if (pktopt->ip6po_dest2)
2425 free(pktopt->ip6po_dest2, M_IP6OPT);
2426 pktopt->ip6po_dest2 = NULL;
2430 #define PKTOPT_EXTHDRCPY(type) \
2433 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2434 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2435 if (dst->type == NULL && canwait == M_NOWAIT)\
2437 bcopy(src->type, dst->type, hlen);\
2439 } while (/*CONSTCOND*/ 0)
2442 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2444 if (dst == NULL || src == NULL) {
2445 printf("ip6_clearpktopts: invalid argument\n");
2449 dst->ip6po_hlim = src->ip6po_hlim;
2450 dst->ip6po_tclass = src->ip6po_tclass;
2451 dst->ip6po_flags = src->ip6po_flags;
2452 dst->ip6po_minmtu = src->ip6po_minmtu;
2453 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2454 if (src->ip6po_pktinfo) {
2455 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2457 if (dst->ip6po_pktinfo == NULL)
2459 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2461 if (src->ip6po_nexthop) {
2462 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2464 if (dst->ip6po_nexthop == NULL)
2466 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2467 src->ip6po_nexthop->sa_len);
2469 PKTOPT_EXTHDRCPY(ip6po_hbh);
2470 PKTOPT_EXTHDRCPY(ip6po_dest1);
2471 PKTOPT_EXTHDRCPY(ip6po_dest2);
2472 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2476 ip6_clearpktopts(dst, -1);
2479 #undef PKTOPT_EXTHDRCPY
2481 struct ip6_pktopts *
2482 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2485 struct ip6_pktopts *dst;
2487 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2490 ip6_initpktopts(dst);
2492 if ((error = copypktopts(dst, src, canwait)) != 0) {
2493 free(dst, M_IP6OPT);
2501 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2506 ip6_clearpktopts(pktopt, -1);
2508 free(pktopt, M_IP6OPT);
2512 * Set IPv6 outgoing packet options based on advanced API.
2515 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2516 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2518 struct cmsghdr *cm = 0;
2520 if (control == NULL || opt == NULL)
2523 ip6_initpktopts(opt);
2528 * If stickyopt is provided, make a local copy of the options
2529 * for this particular packet, then override them by ancillary
2531 * XXX: copypktopts() does not copy the cached route to a next
2532 * hop (if any). This is not very good in terms of efficiency,
2533 * but we can allow this since this option should be rarely
2536 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2541 * XXX: Currently, we assume all the optional information is stored
2544 if (control->m_next)
2547 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2548 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2551 if (control->m_len < CMSG_LEN(0))
2554 cm = mtod(control, struct cmsghdr *);
2555 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2557 if (cm->cmsg_level != IPPROTO_IPV6)
2560 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2561 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2570 * Set a particular packet option, as a sticky option or an ancillary data
2571 * item. "len" can be 0 only when it's a sticky option.
2572 * We have 4 cases of combination of "sticky" and "cmsg":
2573 * "sticky=0, cmsg=0": impossible
2574 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2575 * "sticky=1, cmsg=0": RFC3542 socket option
2576 * "sticky=1, cmsg=1": RFC2292 socket option
2579 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2580 struct ucred *cred, int sticky, int cmsg, int uproto)
2582 int minmtupolicy, preftemp;
2585 if (!sticky && !cmsg) {
2587 printf("ip6_setpktopt: impossible case\n");
2593 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2594 * not be specified in the context of RFC3542. Conversely,
2595 * RFC3542 types should not be specified in the context of RFC2292.
2599 case IPV6_2292PKTINFO:
2600 case IPV6_2292HOPLIMIT:
2601 case IPV6_2292NEXTHOP:
2602 case IPV6_2292HOPOPTS:
2603 case IPV6_2292DSTOPTS:
2604 case IPV6_2292RTHDR:
2605 case IPV6_2292PKTOPTIONS:
2606 return (ENOPROTOOPT);
2609 if (sticky && cmsg) {
2616 case IPV6_RTHDRDSTOPTS:
2618 case IPV6_USE_MIN_MTU:
2621 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2622 return (ENOPROTOOPT);
2627 case IPV6_2292PKTINFO:
2630 struct ifnet *ifp = NULL;
2631 struct in6_pktinfo *pktinfo;
2633 if (len != sizeof(struct in6_pktinfo))
2636 pktinfo = (struct in6_pktinfo *)buf;
2639 * An application can clear any sticky IPV6_PKTINFO option by
2640 * doing a "regular" setsockopt with ipi6_addr being
2641 * in6addr_any and ipi6_ifindex being zero.
2642 * [RFC 3542, Section 6]
2644 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2645 pktinfo->ipi6_ifindex == 0 &&
2646 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2647 ip6_clearpktopts(opt, optname);
2651 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2652 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2655 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2657 /* validate the interface index if specified. */
2658 if (pktinfo->ipi6_ifindex > V_if_index)
2660 if (pktinfo->ipi6_ifindex) {
2661 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2665 if (ifp != NULL && (
2666 ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED))
2670 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2671 struct in6_ifaddr *ia;
2673 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2674 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2676 return (EADDRNOTAVAIL);
2677 ifa_free(&ia->ia_ifa);
2680 * We store the address anyway, and let in6_selectsrc()
2681 * validate the specified address. This is because ipi6_addr
2682 * may not have enough information about its scope zone, and
2683 * we may need additional information (such as outgoing
2684 * interface or the scope zone of a destination address) to
2685 * disambiguate the scope.
2686 * XXX: the delay of the validation may confuse the
2687 * application when it is used as a sticky option.
2689 if (opt->ip6po_pktinfo == NULL) {
2690 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2691 M_IP6OPT, M_NOWAIT);
2692 if (opt->ip6po_pktinfo == NULL)
2695 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2699 case IPV6_2292HOPLIMIT:
2705 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2706 * to simplify the ordering among hoplimit options.
2708 if (optname == IPV6_HOPLIMIT && sticky)
2709 return (ENOPROTOOPT);
2711 if (len != sizeof(int))
2714 if (*hlimp < -1 || *hlimp > 255)
2717 opt->ip6po_hlim = *hlimp;
2725 if (len != sizeof(int))
2727 tclass = *(int *)buf;
2728 if (tclass < -1 || tclass > 255)
2731 opt->ip6po_tclass = tclass;
2735 case IPV6_2292NEXTHOP:
2738 error = priv_check_cred(cred,
2739 PRIV_NETINET_SETHDROPTS, 0);
2744 if (len == 0) { /* just remove the option */
2745 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2749 /* check if cmsg_len is large enough for sa_len */
2750 if (len < sizeof(struct sockaddr) || len < *buf)
2753 switch (((struct sockaddr *)buf)->sa_family) {
2756 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2759 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2762 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2763 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2766 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2772 case AF_LINK: /* should eventually be supported */
2774 return (EAFNOSUPPORT);
2777 /* turn off the previous option, then set the new option. */
2778 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2779 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2780 if (opt->ip6po_nexthop == NULL)
2782 bcopy(buf, opt->ip6po_nexthop, *buf);
2785 case IPV6_2292HOPOPTS:
2788 struct ip6_hbh *hbh;
2792 * XXX: We don't allow a non-privileged user to set ANY HbH
2793 * options, since per-option restriction has too much
2797 error = priv_check_cred(cred,
2798 PRIV_NETINET_SETHDROPTS, 0);
2804 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2805 break; /* just remove the option */
2808 /* message length validation */
2809 if (len < sizeof(struct ip6_hbh))
2811 hbh = (struct ip6_hbh *)buf;
2812 hbhlen = (hbh->ip6h_len + 1) << 3;
2816 /* turn off the previous option, then set the new option. */
2817 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2818 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2819 if (opt->ip6po_hbh == NULL)
2821 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2826 case IPV6_2292DSTOPTS:
2828 case IPV6_RTHDRDSTOPTS:
2830 struct ip6_dest *dest, **newdest = NULL;
2833 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2834 error = priv_check_cred(cred,
2835 PRIV_NETINET_SETHDROPTS, 0);
2841 ip6_clearpktopts(opt, optname);
2842 break; /* just remove the option */
2845 /* message length validation */
2846 if (len < sizeof(struct ip6_dest))
2848 dest = (struct ip6_dest *)buf;
2849 destlen = (dest->ip6d_len + 1) << 3;
2854 * Determine the position that the destination options header
2855 * should be inserted; before or after the routing header.
2858 case IPV6_2292DSTOPTS:
2860 * The old advacned API is ambiguous on this point.
2861 * Our approach is to determine the position based
2862 * according to the existence of a routing header.
2863 * Note, however, that this depends on the order of the
2864 * extension headers in the ancillary data; the 1st
2865 * part of the destination options header must appear
2866 * before the routing header in the ancillary data,
2868 * RFC3542 solved the ambiguity by introducing
2869 * separate ancillary data or option types.
2871 if (opt->ip6po_rthdr == NULL)
2872 newdest = &opt->ip6po_dest1;
2874 newdest = &opt->ip6po_dest2;
2876 case IPV6_RTHDRDSTOPTS:
2877 newdest = &opt->ip6po_dest1;
2880 newdest = &opt->ip6po_dest2;
2884 /* turn off the previous option, then set the new option. */
2885 ip6_clearpktopts(opt, optname);
2886 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2887 if (*newdest == NULL)
2889 bcopy(dest, *newdest, destlen);
2894 case IPV6_2292RTHDR:
2897 struct ip6_rthdr *rth;
2901 ip6_clearpktopts(opt, IPV6_RTHDR);
2902 break; /* just remove the option */
2905 /* message length validation */
2906 if (len < sizeof(struct ip6_rthdr))
2908 rth = (struct ip6_rthdr *)buf;
2909 rthlen = (rth->ip6r_len + 1) << 3;
2913 switch (rth->ip6r_type) {
2914 case IPV6_RTHDR_TYPE_0:
2915 if (rth->ip6r_len == 0) /* must contain one addr */
2917 if (rth->ip6r_len % 2) /* length must be even */
2919 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2923 return (EINVAL); /* not supported */
2926 /* turn off the previous option */
2927 ip6_clearpktopts(opt, IPV6_RTHDR);
2928 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2929 if (opt->ip6po_rthdr == NULL)
2931 bcopy(rth, opt->ip6po_rthdr, rthlen);
2936 case IPV6_USE_MIN_MTU:
2937 if (len != sizeof(int))
2939 minmtupolicy = *(int *)buf;
2940 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2941 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2942 minmtupolicy != IP6PO_MINMTU_ALL) {
2945 opt->ip6po_minmtu = minmtupolicy;
2949 if (len != sizeof(int))
2952 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2954 * we ignore this option for TCP sockets.
2955 * (RFC3542 leaves this case unspecified.)
2957 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2959 opt->ip6po_flags |= IP6PO_DONTFRAG;
2962 case IPV6_PREFER_TEMPADDR:
2963 if (len != sizeof(int))
2965 preftemp = *(int *)buf;
2966 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2967 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2968 preftemp != IP6PO_TEMPADDR_PREFER) {
2971 opt->ip6po_prefer_tempaddr = preftemp;
2975 return (ENOPROTOOPT);
2976 } /* end of switch */
2982 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2983 * packet to the input queue of a specified interface. Note that this
2984 * calls the output routine of the loopback "driver", but with an interface
2985 * pointer that might NOT be &loif -- easier than replicating that code here.
2988 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
2991 struct ip6_hdr *ip6;
2993 copym = m_copy(m, 0, M_COPYALL);
2998 * Make sure to deep-copy IPv6 header portion in case the data
2999 * is in an mbuf cluster, so that we can safely override the IPv6
3000 * header portion later.
3002 if (!M_WRITABLE(copym) ||
3003 copym->m_len < sizeof(struct ip6_hdr)) {
3004 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3008 ip6 = mtod(copym, struct ip6_hdr *);
3010 * clear embedded scope identifiers if necessary.
3011 * in6_clearscope will touch the addresses only when necessary.
3013 in6_clearscope(&ip6->ip6_src);
3014 in6_clearscope(&ip6->ip6_dst);
3015 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3016 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3018 copym->m_pkthdr.csum_data = 0xffff;
3020 if_simloop(ifp, copym, AF_INET6, 0);
3024 * Chop IPv6 header off from the payload.
3027 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3030 struct ip6_hdr *ip6;
3032 ip6 = mtod(m, struct ip6_hdr *);
3033 if (m->m_len > sizeof(*ip6)) {
3034 mh = m_gethdr(M_NOWAIT, MT_DATA);
3039 m_move_pkthdr(mh, m);
3040 M_ALIGN(mh, sizeof(*ip6));
3041 m->m_len -= sizeof(*ip6);
3042 m->m_data += sizeof(*ip6);
3045 m->m_len = sizeof(*ip6);
3046 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3048 exthdrs->ip6e_ip6 = m;
3053 * Compute IPv6 extension header length.
3056 ip6_optlen(struct inpcb *in6p)
3060 if (!in6p->in6p_outputopts)
3065 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3067 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3068 if (in6p->in6p_outputopts->ip6po_rthdr)
3069 /* dest1 is valid with rthdr only */
3070 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3071 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3072 len += elen(in6p->in6p_outputopts->ip6po_dest2);