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 *);
549 if (ro->ro_rt && fwd_tag == NULL) {
551 ifp = ro->ro_rt->rt_ifp;
553 if (fwd_tag == NULL) {
554 bzero(&dst_sa, sizeof(dst_sa));
555 dst_sa.sin6_family = AF_INET6;
556 dst_sa.sin6_len = sizeof(dst_sa);
557 dst_sa.sin6_addr = ip6->ip6_dst;
559 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
563 in6_ifstat_inc(ifp, ifs6_out_discard);
569 * If in6_selectroute() does not return a route entry,
570 * dst may not have been updated.
572 *dst = dst_sa; /* XXX */
576 * then rt (for unicast) and ifp must be non-NULL valid values.
578 if ((flags & IPV6_FORWARDING) == 0) {
579 /* XXX: the FORWARDING flag can be set for mrouting. */
580 in6_ifstat_inc(ifp, ifs6_out_request);
583 ia = (struct in6_ifaddr *)(rt->rt_ifa);
584 counter_u64_add(rt->rt_pksent, 1);
589 * The outgoing interface must be in the zone of source and
590 * destination addresses.
595 if (in6_setscope(&src0, origifp, &zone))
597 bzero(&src_sa, sizeof(src_sa));
598 src_sa.sin6_family = AF_INET6;
599 src_sa.sin6_len = sizeof(src_sa);
600 src_sa.sin6_addr = ip6->ip6_src;
601 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
605 if (in6_setscope(&dst0, origifp, &zone))
607 /* re-initialize to be sure */
608 bzero(&dst_sa, sizeof(dst_sa));
609 dst_sa.sin6_family = AF_INET6;
610 dst_sa.sin6_len = sizeof(dst_sa);
611 dst_sa.sin6_addr = ip6->ip6_dst;
612 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
616 /* We should use ia_ifp to support the case of
617 * sending packets to an address of our own.
619 if (ia != NULL && ia->ia_ifp)
622 /* scope check is done. */
626 IP6STAT_INC(ip6s_badscope);
627 in6_ifstat_inc(origifp, ifs6_out_discard);
629 error = EHOSTUNREACH; /* XXX */
633 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
634 if (opt && opt->ip6po_nextroute.ro_rt) {
636 * The nexthop is explicitly specified by the
637 * application. We assume the next hop is an IPv6
640 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
642 else if ((rt->rt_flags & RTF_GATEWAY))
643 dst = (struct sockaddr_in6 *)rt->rt_gateway;
646 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
647 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
649 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
650 in6_ifstat_inc(ifp, ifs6_out_mcast);
652 * Confirm that the outgoing interface supports multicast.
654 if (!(ifp->if_flags & IFF_MULTICAST)) {
655 IP6STAT_INC(ip6s_noroute);
656 in6_ifstat_inc(ifp, ifs6_out_discard);
660 if ((im6o == NULL && in6_mcast_loop) ||
661 (im6o && im6o->im6o_multicast_loop)) {
663 * Loop back multicast datagram if not expressly
664 * forbidden to do so, even if we have not joined
665 * the address; protocols will filter it later,
666 * thus deferring a hash lookup and lock acquisition
667 * at the expense of an m_copym().
669 ip6_mloopback(ifp, m);
672 * If we are acting as a multicast router, perform
673 * multicast forwarding as if the packet had just
674 * arrived on the interface to which we are about
675 * to send. The multicast forwarding function
676 * recursively calls this function, using the
677 * IPV6_FORWARDING flag to prevent infinite recursion.
679 * Multicasts that are looped back by ip6_mloopback(),
680 * above, will be forwarded by the ip6_input() routine,
683 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
685 * XXX: ip6_mforward expects that rcvif is NULL
686 * when it is called from the originating path.
687 * However, it may not always be the case.
689 m->m_pkthdr.rcvif = NULL;
690 if (ip6_mforward(ip6, ifp, m) != 0) {
697 * Multicasts with a hoplimit of zero may be looped back,
698 * above, but must not be transmitted on a network.
699 * Also, multicasts addressed to the loopback interface
700 * are not sent -- the above call to ip6_mloopback() will
701 * loop back a copy if this host actually belongs to the
702 * destination group on the loopback interface.
704 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
705 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
712 * Fill the outgoing inteface to tell the upper layer
713 * to increment per-interface statistics.
718 /* Determine path MTU. */
719 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &finaldst, &mtu,
720 &alwaysfrag, fibnum)) != 0)
724 * The caller of this function may specify to use the minimum MTU
726 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
727 * setting. The logic is a bit complicated; by default, unicast
728 * packets will follow path MTU while multicast packets will be sent at
729 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
730 * including unicast ones will be sent at the minimum MTU. Multicast
731 * packets will always be sent at the minimum MTU unless
732 * IP6PO_MINMTU_DISABLE is explicitly specified.
733 * See RFC 3542 for more details.
735 if (mtu > IPV6_MMTU) {
736 if ((flags & IPV6_MINMTU))
738 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
740 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
742 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
748 * clear embedded scope identifiers if necessary.
749 * in6_clearscope will touch the addresses only when necessary.
751 in6_clearscope(&ip6->ip6_src);
752 in6_clearscope(&ip6->ip6_dst);
755 * If the outgoing packet contains a hop-by-hop options header,
756 * it must be examined and processed even by the source node.
757 * (RFC 2460, section 4.)
759 if (exthdrs.ip6e_hbh) {
760 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
761 u_int32_t dummy; /* XXX unused */
762 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
765 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
766 panic("ip6e_hbh is not contiguous");
769 * XXX: if we have to send an ICMPv6 error to the sender,
770 * we need the M_LOOP flag since icmp6_error() expects
771 * the IPv6 and the hop-by-hop options header are
772 * contiguous unless the flag is set.
774 m->m_flags |= M_LOOP;
775 m->m_pkthdr.rcvif = ifp;
776 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
777 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
778 &dummy, &plen) < 0) {
779 /* m was already freed at this point */
780 error = EINVAL;/* better error? */
783 m->m_flags &= ~M_LOOP; /* XXX */
784 m->m_pkthdr.rcvif = NULL;
787 /* Jump over all PFIL processing if hooks are not active. */
788 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
792 /* Run through list of hooks for output packets. */
793 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
794 if (error != 0 || m == NULL)
796 ip6 = mtod(m, struct ip6_hdr *);
799 /* See if destination IP address was changed by packet filter. */
800 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
801 m->m_flags |= M_SKIP_FIREWALL;
802 /* If destination is now ourself drop to ip6_input(). */
803 if (in6_localip(&ip6->ip6_dst)) {
804 m->m_flags |= M_FASTFWD_OURS;
805 if (m->m_pkthdr.rcvif == NULL)
806 m->m_pkthdr.rcvif = V_loif;
807 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
808 m->m_pkthdr.csum_flags |=
809 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
810 m->m_pkthdr.csum_data = 0xffff;
813 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
814 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
816 error = netisr_queue(NETISR_IPV6, m);
819 needfiblookup = 1; /* Redo the routing table lookup. */
821 /* See if fib was changed by packet filter. */
822 if (fibnum != M_GETFIB(m)) {
823 m->m_flags |= M_SKIP_FIREWALL;
824 fibnum = M_GETFIB(m);
831 /* See if local, if yes, send it to netisr. */
832 if (m->m_flags & M_FASTFWD_OURS) {
833 if (m->m_pkthdr.rcvif == NULL)
834 m->m_pkthdr.rcvif = V_loif;
835 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
836 m->m_pkthdr.csum_flags |=
837 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
838 m->m_pkthdr.csum_data = 0xffff;
841 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
842 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
844 error = netisr_queue(NETISR_IPV6, m);
847 /* Or forward to some other address? */
848 if ((m->m_flags & M_IP6_NEXTHOP) &&
849 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
850 dst = (struct sockaddr_in6 *)&ro->ro_dst;
851 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
852 m->m_flags |= M_SKIP_FIREWALL;
853 m->m_flags &= ~M_IP6_NEXTHOP;
854 m_tag_delete(m, fwd_tag);
860 * Send the packet to the outgoing interface.
861 * If necessary, do IPv6 fragmentation before sending.
863 * the logic here is rather complex:
864 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
865 * 1-a: send as is if tlen <= path mtu
866 * 1-b: fragment if tlen > path mtu
868 * 2: if user asks us not to fragment (dontfrag == 1)
869 * 2-a: send as is if tlen <= interface mtu
870 * 2-b: error if tlen > interface mtu
872 * 3: if we always need to attach fragment header (alwaysfrag == 1)
875 * 4: if dontfrag == 1 && alwaysfrag == 1
876 * error, as we cannot handle this conflicting request
878 sw_csum = m->m_pkthdr.csum_flags;
880 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
881 sw_csum &= ~ifp->if_hwassist;
885 * If we added extension headers, we will not do TSO and calculate the
886 * checksums ourselves for now.
887 * XXX-BZ Need a framework to know when the NIC can handle it, even
890 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
891 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
892 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
895 if (sw_csum & CSUM_SCTP_IPV6) {
896 sw_csum &= ~CSUM_SCTP_IPV6;
897 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
900 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
901 tlen = m->m_pkthdr.len;
903 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
907 if (dontfrag && alwaysfrag) { /* case 4 */
908 /* conflicting request - can't transmit */
912 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
914 * Even if the DONTFRAG option is specified, we cannot send the
915 * packet when the data length is larger than the MTU of the
916 * outgoing interface.
917 * Notify the error by sending IPV6_PATHMTU ancillary data if
918 * application wanted to know the MTU value. Also return an
919 * error code (this is not described in the API spec).
922 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
928 * transmit packet without fragmentation
930 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
931 struct in6_ifaddr *ia6;
933 ip6 = mtod(m, struct ip6_hdr *);
934 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
936 /* Record statistics for this interface address. */
937 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
938 counter_u64_add(ia6->ia_ifa.ifa_obytes,
940 ifa_free(&ia6->ia_ifa);
942 error = nd6_output_ifp(ifp, origifp, m, dst, NULL);
947 * try to fragment the packet. case 1-b and 3
949 if (mtu < IPV6_MMTU) {
950 /* path MTU cannot be less than IPV6_MMTU */
952 in6_ifstat_inc(ifp, ifs6_out_fragfail);
954 } else if (ip6->ip6_plen == 0) {
955 /* jumbo payload cannot be fragmented */
957 in6_ifstat_inc(ifp, ifs6_out_fragfail);
963 * Too large for the destination or interface;
964 * fragment if possible.
965 * Must be able to put at least 8 bytes per fragment.
967 hlen = unfragpartlen;
968 if (mtu > IPV6_MAXPACKET)
969 mtu = IPV6_MAXPACKET;
971 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
974 in6_ifstat_inc(ifp, ifs6_out_fragfail);
979 * If the interface will not calculate checksums on
980 * fragmented packets, then do it here.
981 * XXX-BZ handle the hw offloading case. Need flags.
983 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
984 in6_delayed_cksum(m, plen, hlen);
985 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
988 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
989 sctp_delayed_cksum(m, hlen);
990 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
994 * Change the next header field of the last header in the
995 * unfragmentable part.
997 if (exthdrs.ip6e_rthdr) {
998 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
999 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1000 } else if (exthdrs.ip6e_dest1) {
1001 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1002 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1003 } else if (exthdrs.ip6e_hbh) {
1004 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1005 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1007 nextproto = ip6->ip6_nxt;
1008 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1012 * Loop through length of segment after first fragment,
1013 * make new header and copy data of each part and link onto
1017 id = htonl(ip6_randomid());
1018 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1021 in6_ifstat_inc(ifp, ifs6_out_fragok);
1025 * Remove leading garbages.
1031 for (m0 = m; m; m = m0) {
1035 /* Record statistics for this interface address. */
1037 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1038 counter_u64_add(ia->ia_ifa.ifa_obytes,
1041 error = nd6_output_ifp(ifp, origifp, m, dst, NULL);
1047 IP6STAT_INC(ip6s_fragmented);
1050 if (ro == &ip6route)
1055 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1056 m_freem(exthdrs.ip6e_dest1);
1057 m_freem(exthdrs.ip6e_rthdr);
1058 m_freem(exthdrs.ip6e_dest2);
1067 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1071 if (hlen > MCLBYTES)
1072 return (ENOBUFS); /* XXX */
1075 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1077 m = m_get(M_NOWAIT, MT_DATA);
1082 bcopy(hdr, mtod(m, caddr_t), hlen);
1089 * Insert jumbo payload option.
1092 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1098 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1101 * If there is no hop-by-hop options header, allocate new one.
1102 * If there is one but it doesn't have enough space to store the
1103 * jumbo payload option, allocate a cluster to store the whole options.
1104 * Otherwise, use it to store the options.
1106 if (exthdrs->ip6e_hbh == 0) {
1107 mopt = m_get(M_NOWAIT, MT_DATA);
1110 mopt->m_len = JUMBOOPTLEN;
1111 optbuf = mtod(mopt, u_char *);
1112 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1113 exthdrs->ip6e_hbh = mopt;
1115 struct ip6_hbh *hbh;
1117 mopt = exthdrs->ip6e_hbh;
1118 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1121 * - exthdrs->ip6e_hbh is not referenced from places
1122 * other than exthdrs.
1123 * - exthdrs->ip6e_hbh is not an mbuf chain.
1125 int oldoptlen = mopt->m_len;
1129 * XXX: give up if the whole (new) hbh header does
1130 * not fit even in an mbuf cluster.
1132 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1136 * As a consequence, we must always prepare a cluster
1139 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1142 n->m_len = oldoptlen + JUMBOOPTLEN;
1143 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1145 optbuf = mtod(n, caddr_t) + oldoptlen;
1147 mopt = exthdrs->ip6e_hbh = n;
1149 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1150 mopt->m_len += JUMBOOPTLEN;
1152 optbuf[0] = IP6OPT_PADN;
1156 * Adjust the header length according to the pad and
1157 * the jumbo payload option.
1159 hbh = mtod(mopt, struct ip6_hbh *);
1160 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1163 /* fill in the option. */
1164 optbuf[2] = IP6OPT_JUMBO;
1166 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1167 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1169 /* finally, adjust the packet header length */
1170 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1177 * Insert fragment header and copy unfragmentable header portions.
1180 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1181 struct ip6_frag **frghdrp)
1183 struct mbuf *n, *mlast;
1185 if (hlen > sizeof(struct ip6_hdr)) {
1186 n = m_copym(m0, sizeof(struct ip6_hdr),
1187 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1194 /* Search for the last mbuf of unfragmentable part. */
1195 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1198 if (M_WRITABLE(mlast) &&
1199 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1200 /* use the trailing space of the last mbuf for the fragment hdr */
1201 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1203 mlast->m_len += sizeof(struct ip6_frag);
1204 m->m_pkthdr.len += sizeof(struct ip6_frag);
1206 /* allocate a new mbuf for the fragment header */
1209 mfrg = m_get(M_NOWAIT, MT_DATA);
1212 mfrg->m_len = sizeof(struct ip6_frag);
1213 *frghdrp = mtod(mfrg, struct ip6_frag *);
1214 mlast->m_next = mfrg;
1221 * Calculates IPv6 path mtu for destination @dst.
1222 * Resulting MTU is stored in @mtup.
1224 * Returns 0 on success.
1227 ip6_getpmtu_ctl(u_int fibnum, struct in6_addr *dst, u_long *mtup)
1229 struct nhop6_extended nh6;
1230 struct in6_addr kdst;
1236 in6_splitscope(dst, &kdst, &scopeid);
1237 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1238 return (EHOSTUNREACH);
1243 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL);
1244 fib6_free_nh_ext(fibnum, &nh6);
1250 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1251 * and cached data in @ro_pmtu.
1252 * MTU from (successful) route lookup is saved (along with dst)
1253 * inside @ro_pmtu to avoid subsequent route lookups after packet
1254 * filter processing.
1256 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1257 * Returns 0 on success.
1260 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1261 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1262 int *alwaysfragp, u_int fibnum)
1264 struct nhop6_basic nh6;
1265 struct in6_addr kdst;
1267 struct sockaddr_in6 *sa6_dst;
1274 * Here ro_pmtu has final destination address, while
1275 * ro might represent immediate destination.
1276 * Use ro_pmtu destination since mtu might differ.
1278 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1279 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1280 ro_pmtu->ro_mtu = 0;
1282 if (ro_pmtu->ro_mtu == 0) {
1283 bzero(sa6_dst, sizeof(*sa6_dst));
1284 sa6_dst->sin6_family = AF_INET6;
1285 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1286 sa6_dst->sin6_addr = *dst;
1288 in6_splitscope(dst, &kdst, &scopeid);
1289 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1291 ro_pmtu->ro_mtu = nh6.nh_mtu;
1294 mtu = ro_pmtu->ro_mtu;
1298 mtu = ro_pmtu->ro_rt->rt_mtu;
1300 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp));
1304 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1305 * hostcache data for @dst.
1306 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1308 * Returns 0 on success.
1311 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1312 u_long *mtup, int *alwaysfragp)
1320 struct in_conninfo inc;
1322 bzero(&inc, sizeof(inc));
1323 inc.inc_flags |= INC_ISIPV6;
1324 inc.inc6_faddr = *dst;
1326 ifmtu = IN6_LINKMTU(ifp);
1327 mtu = tcp_hc_getmtu(&inc);
1329 mtu = min(mtu, rt_mtu);
1334 else if (mtu < IPV6_MMTU) {
1336 * RFC2460 section 5, last paragraph:
1337 * if we record ICMPv6 too big message with
1338 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1339 * or smaller, with framgent header attached.
1340 * (fragment header is needed regardless from the
1341 * packet size, for translators to identify packets)
1347 mtu = IN6_LINKMTU(ifp);
1349 error = EHOSTUNREACH; /* XXX */
1353 *alwaysfragp = alwaysfrag;
1358 * IP6 socket option processing.
1361 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1363 int optdatalen, uproto;
1365 struct inpcb *in6p = sotoinpcb(so);
1367 int level, op, optname;
1371 uint32_t rss_bucket;
1375 level = sopt->sopt_level;
1376 op = sopt->sopt_dir;
1377 optname = sopt->sopt_name;
1378 optlen = sopt->sopt_valsize;
1382 uproto = (int)so->so_proto->pr_protocol;
1384 if (level != IPPROTO_IPV6) {
1387 if (sopt->sopt_level == SOL_SOCKET &&
1388 sopt->sopt_dir == SOPT_SET) {
1389 switch (sopt->sopt_name) {
1392 if ((so->so_options & SO_REUSEADDR) != 0)
1393 in6p->inp_flags2 |= INP_REUSEADDR;
1395 in6p->inp_flags2 &= ~INP_REUSEADDR;
1401 if ((so->so_options & SO_REUSEPORT) != 0)
1402 in6p->inp_flags2 |= INP_REUSEPORT;
1404 in6p->inp_flags2 &= ~INP_REUSEPORT;
1410 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1418 } else { /* level == IPPROTO_IPV6 */
1423 case IPV6_2292PKTOPTIONS:
1424 #ifdef IPV6_PKTOPTIONS
1425 case IPV6_PKTOPTIONS:
1430 error = soopt_getm(sopt, &m); /* XXX */
1433 error = soopt_mcopyin(sopt, m); /* XXX */
1436 error = ip6_pcbopts(&in6p->in6p_outputopts,
1438 m_freem(m); /* XXX */
1443 * Use of some Hop-by-Hop options or some
1444 * Destination options, might require special
1445 * privilege. That is, normal applications
1446 * (without special privilege) might be forbidden
1447 * from setting certain options in outgoing packets,
1448 * and might never see certain options in received
1449 * packets. [RFC 2292 Section 6]
1450 * KAME specific note:
1451 * KAME prevents non-privileged users from sending or
1452 * receiving ANY hbh/dst options in order to avoid
1453 * overhead of parsing options in the kernel.
1455 case IPV6_RECVHOPOPTS:
1456 case IPV6_RECVDSTOPTS:
1457 case IPV6_RECVRTHDRDSTOPTS:
1459 error = priv_check(td,
1460 PRIV_NETINET_SETHDROPTS);
1465 case IPV6_UNICAST_HOPS:
1468 case IPV6_RECVPKTINFO:
1469 case IPV6_RECVHOPLIMIT:
1470 case IPV6_RECVRTHDR:
1471 case IPV6_RECVPATHMTU:
1472 case IPV6_RECVTCLASS:
1473 case IPV6_RECVFLOWID:
1475 case IPV6_RECVRSSBUCKETID:
1478 case IPV6_AUTOFLOWLABEL:
1480 case IPV6_BINDMULTI:
1482 case IPV6_RSS_LISTEN_BUCKET:
1484 if (optname == IPV6_BINDANY && td != NULL) {
1485 error = priv_check(td,
1486 PRIV_NETINET_BINDANY);
1491 if (optlen != sizeof(int)) {
1495 error = sooptcopyin(sopt, &optval,
1496 sizeof optval, sizeof optval);
1501 case IPV6_UNICAST_HOPS:
1502 if (optval < -1 || optval >= 256)
1505 /* -1 = kernel default */
1506 in6p->in6p_hops = optval;
1507 if ((in6p->inp_vflag &
1509 in6p->inp_ip_ttl = optval;
1512 #define OPTSET(bit) \
1516 in6p->inp_flags |= (bit); \
1518 in6p->inp_flags &= ~(bit); \
1519 INP_WUNLOCK(in6p); \
1520 } while (/*CONSTCOND*/ 0)
1521 #define OPTSET2292(bit) \
1524 in6p->inp_flags |= IN6P_RFC2292; \
1526 in6p->inp_flags |= (bit); \
1528 in6p->inp_flags &= ~(bit); \
1529 INP_WUNLOCK(in6p); \
1530 } while (/*CONSTCOND*/ 0)
1531 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1533 #define OPTSET2(bit, val) do { \
1536 in6p->inp_flags2 |= bit; \
1538 in6p->inp_flags2 &= ~bit; \
1539 INP_WUNLOCK(in6p); \
1541 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1543 case IPV6_RECVPKTINFO:
1544 /* cannot mix with RFC2292 */
1545 if (OPTBIT(IN6P_RFC2292)) {
1549 OPTSET(IN6P_PKTINFO);
1554 struct ip6_pktopts **optp;
1556 /* cannot mix with RFC2292 */
1557 if (OPTBIT(IN6P_RFC2292)) {
1561 optp = &in6p->in6p_outputopts;
1562 error = ip6_pcbopt(IPV6_HOPLIMIT,
1563 (u_char *)&optval, sizeof(optval),
1564 optp, (td != NULL) ? td->td_ucred :
1569 case IPV6_RECVHOPLIMIT:
1570 /* cannot mix with RFC2292 */
1571 if (OPTBIT(IN6P_RFC2292)) {
1575 OPTSET(IN6P_HOPLIMIT);
1578 case IPV6_RECVHOPOPTS:
1579 /* cannot mix with RFC2292 */
1580 if (OPTBIT(IN6P_RFC2292)) {
1584 OPTSET(IN6P_HOPOPTS);
1587 case IPV6_RECVDSTOPTS:
1588 /* cannot mix with RFC2292 */
1589 if (OPTBIT(IN6P_RFC2292)) {
1593 OPTSET(IN6P_DSTOPTS);
1596 case IPV6_RECVRTHDRDSTOPTS:
1597 /* cannot mix with RFC2292 */
1598 if (OPTBIT(IN6P_RFC2292)) {
1602 OPTSET(IN6P_RTHDRDSTOPTS);
1605 case IPV6_RECVRTHDR:
1606 /* cannot mix with RFC2292 */
1607 if (OPTBIT(IN6P_RFC2292)) {
1614 case IPV6_RECVPATHMTU:
1616 * We ignore this option for TCP
1618 * (RFC3542 leaves this case
1621 if (uproto != IPPROTO_TCP)
1625 case IPV6_RECVFLOWID:
1626 OPTSET2(INP_RECVFLOWID, optval);
1630 case IPV6_RECVRSSBUCKETID:
1631 OPTSET2(INP_RECVRSSBUCKETID, optval);
1637 * make setsockopt(IPV6_V6ONLY)
1638 * available only prior to bind(2).
1639 * see ipng mailing list, Jun 22 2001.
1641 if (in6p->inp_lport ||
1642 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1646 OPTSET(IN6P_IPV6_V6ONLY);
1648 in6p->inp_vflag &= ~INP_IPV4;
1650 in6p->inp_vflag |= INP_IPV4;
1652 case IPV6_RECVTCLASS:
1653 /* cannot mix with RFC2292 XXX */
1654 if (OPTBIT(IN6P_RFC2292)) {
1658 OPTSET(IN6P_TCLASS);
1660 case IPV6_AUTOFLOWLABEL:
1661 OPTSET(IN6P_AUTOFLOWLABEL);
1665 OPTSET(INP_BINDANY);
1668 case IPV6_BINDMULTI:
1669 OPTSET2(INP_BINDMULTI, optval);
1672 case IPV6_RSS_LISTEN_BUCKET:
1673 if ((optval >= 0) &&
1674 (optval < rss_getnumbuckets())) {
1675 in6p->inp_rss_listen_bucket = optval;
1676 OPTSET2(INP_RSS_BUCKET_SET, 1);
1687 case IPV6_USE_MIN_MTU:
1688 case IPV6_PREFER_TEMPADDR:
1689 if (optlen != sizeof(optval)) {
1693 error = sooptcopyin(sopt, &optval,
1694 sizeof optval, sizeof optval);
1698 struct ip6_pktopts **optp;
1699 optp = &in6p->in6p_outputopts;
1700 error = ip6_pcbopt(optname,
1701 (u_char *)&optval, sizeof(optval),
1702 optp, (td != NULL) ? td->td_ucred :
1707 case IPV6_2292PKTINFO:
1708 case IPV6_2292HOPLIMIT:
1709 case IPV6_2292HOPOPTS:
1710 case IPV6_2292DSTOPTS:
1711 case IPV6_2292RTHDR:
1713 if (optlen != sizeof(int)) {
1717 error = sooptcopyin(sopt, &optval,
1718 sizeof optval, sizeof optval);
1722 case IPV6_2292PKTINFO:
1723 OPTSET2292(IN6P_PKTINFO);
1725 case IPV6_2292HOPLIMIT:
1726 OPTSET2292(IN6P_HOPLIMIT);
1728 case IPV6_2292HOPOPTS:
1730 * Check super-user privilege.
1731 * See comments for IPV6_RECVHOPOPTS.
1734 error = priv_check(td,
1735 PRIV_NETINET_SETHDROPTS);
1739 OPTSET2292(IN6P_HOPOPTS);
1741 case IPV6_2292DSTOPTS:
1743 error = priv_check(td,
1744 PRIV_NETINET_SETHDROPTS);
1748 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1750 case IPV6_2292RTHDR:
1751 OPTSET2292(IN6P_RTHDR);
1759 case IPV6_RTHDRDSTOPTS:
1762 /* new advanced API (RFC3542) */
1764 u_char optbuf_storage[MCLBYTES];
1766 struct ip6_pktopts **optp;
1768 /* cannot mix with RFC2292 */
1769 if (OPTBIT(IN6P_RFC2292)) {
1775 * We only ensure valsize is not too large
1776 * here. Further validation will be done
1779 error = sooptcopyin(sopt, optbuf_storage,
1780 sizeof(optbuf_storage), 0);
1783 optlen = sopt->sopt_valsize;
1784 optbuf = optbuf_storage;
1785 optp = &in6p->in6p_outputopts;
1786 error = ip6_pcbopt(optname, optbuf, optlen,
1787 optp, (td != NULL) ? td->td_ucred : NULL,
1793 case IPV6_MULTICAST_IF:
1794 case IPV6_MULTICAST_HOPS:
1795 case IPV6_MULTICAST_LOOP:
1796 case IPV6_JOIN_GROUP:
1797 case IPV6_LEAVE_GROUP:
1799 case MCAST_BLOCK_SOURCE:
1800 case MCAST_UNBLOCK_SOURCE:
1801 case MCAST_JOIN_GROUP:
1802 case MCAST_LEAVE_GROUP:
1803 case MCAST_JOIN_SOURCE_GROUP:
1804 case MCAST_LEAVE_SOURCE_GROUP:
1805 error = ip6_setmoptions(in6p, sopt);
1808 case IPV6_PORTRANGE:
1809 error = sooptcopyin(sopt, &optval,
1810 sizeof optval, sizeof optval);
1816 case IPV6_PORTRANGE_DEFAULT:
1817 in6p->inp_flags &= ~(INP_LOWPORT);
1818 in6p->inp_flags &= ~(INP_HIGHPORT);
1821 case IPV6_PORTRANGE_HIGH:
1822 in6p->inp_flags &= ~(INP_LOWPORT);
1823 in6p->inp_flags |= INP_HIGHPORT;
1826 case IPV6_PORTRANGE_LOW:
1827 in6p->inp_flags &= ~(INP_HIGHPORT);
1828 in6p->inp_flags |= INP_LOWPORT;
1839 case IPV6_IPSEC_POLICY:
1844 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1846 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1848 req = mtod(m, caddr_t);
1849 error = ipsec_set_policy(in6p, optname, req,
1850 m->m_len, (sopt->sopt_td != NULL) ?
1851 sopt->sopt_td->td_ucred : NULL);
1858 error = ENOPROTOOPT;
1866 case IPV6_2292PKTOPTIONS:
1867 #ifdef IPV6_PKTOPTIONS
1868 case IPV6_PKTOPTIONS:
1871 * RFC3542 (effectively) deprecated the
1872 * semantics of the 2292-style pktoptions.
1873 * Since it was not reliable in nature (i.e.,
1874 * applications had to expect the lack of some
1875 * information after all), it would make sense
1876 * to simplify this part by always returning
1879 sopt->sopt_valsize = 0;
1882 case IPV6_RECVHOPOPTS:
1883 case IPV6_RECVDSTOPTS:
1884 case IPV6_RECVRTHDRDSTOPTS:
1885 case IPV6_UNICAST_HOPS:
1886 case IPV6_RECVPKTINFO:
1887 case IPV6_RECVHOPLIMIT:
1888 case IPV6_RECVRTHDR:
1889 case IPV6_RECVPATHMTU:
1892 case IPV6_PORTRANGE:
1893 case IPV6_RECVTCLASS:
1894 case IPV6_AUTOFLOWLABEL:
1898 case IPV6_RECVFLOWID:
1900 case IPV6_RSSBUCKETID:
1901 case IPV6_RECVRSSBUCKETID:
1903 case IPV6_BINDMULTI:
1906 case IPV6_RECVHOPOPTS:
1907 optval = OPTBIT(IN6P_HOPOPTS);
1910 case IPV6_RECVDSTOPTS:
1911 optval = OPTBIT(IN6P_DSTOPTS);
1914 case IPV6_RECVRTHDRDSTOPTS:
1915 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1918 case IPV6_UNICAST_HOPS:
1919 optval = in6p->in6p_hops;
1922 case IPV6_RECVPKTINFO:
1923 optval = OPTBIT(IN6P_PKTINFO);
1926 case IPV6_RECVHOPLIMIT:
1927 optval = OPTBIT(IN6P_HOPLIMIT);
1930 case IPV6_RECVRTHDR:
1931 optval = OPTBIT(IN6P_RTHDR);
1934 case IPV6_RECVPATHMTU:
1935 optval = OPTBIT(IN6P_MTU);
1939 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1942 case IPV6_PORTRANGE:
1945 flags = in6p->inp_flags;
1946 if (flags & INP_HIGHPORT)
1947 optval = IPV6_PORTRANGE_HIGH;
1948 else if (flags & INP_LOWPORT)
1949 optval = IPV6_PORTRANGE_LOW;
1954 case IPV6_RECVTCLASS:
1955 optval = OPTBIT(IN6P_TCLASS);
1958 case IPV6_AUTOFLOWLABEL:
1959 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1963 optval = OPTBIT(INP_BINDANY);
1967 optval = in6p->inp_flowid;
1971 optval = in6p->inp_flowtype;
1974 case IPV6_RECVFLOWID:
1975 optval = OPTBIT2(INP_RECVFLOWID);
1978 case IPV6_RSSBUCKETID:
1980 rss_hash2bucket(in6p->inp_flowid,
1984 optval = rss_bucket;
1989 case IPV6_RECVRSSBUCKETID:
1990 optval = OPTBIT2(INP_RECVRSSBUCKETID);
1994 case IPV6_BINDMULTI:
1995 optval = OPTBIT2(INP_BINDMULTI);
2001 error = sooptcopyout(sopt, &optval,
2008 struct ip6_mtuinfo mtuinfo;
2010 if (!(so->so_state & SS_ISCONNECTED))
2013 * XXX: we dot not consider the case of source
2014 * routing, or optional information to specify
2015 * the outgoing interface.
2017 error = ip6_getpmtu_ctl(so->so_fibnum,
2018 &in6p->in6p_faddr, &pmtu);
2021 if (pmtu > IPV6_MAXPACKET)
2022 pmtu = IPV6_MAXPACKET;
2024 bzero(&mtuinfo, sizeof(mtuinfo));
2025 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2026 optdata = (void *)&mtuinfo;
2027 optdatalen = sizeof(mtuinfo);
2028 error = sooptcopyout(sopt, optdata,
2033 case IPV6_2292PKTINFO:
2034 case IPV6_2292HOPLIMIT:
2035 case IPV6_2292HOPOPTS:
2036 case IPV6_2292RTHDR:
2037 case IPV6_2292DSTOPTS:
2039 case IPV6_2292PKTINFO:
2040 optval = OPTBIT(IN6P_PKTINFO);
2042 case IPV6_2292HOPLIMIT:
2043 optval = OPTBIT(IN6P_HOPLIMIT);
2045 case IPV6_2292HOPOPTS:
2046 optval = OPTBIT(IN6P_HOPOPTS);
2048 case IPV6_2292RTHDR:
2049 optval = OPTBIT(IN6P_RTHDR);
2051 case IPV6_2292DSTOPTS:
2052 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2055 error = sooptcopyout(sopt, &optval,
2062 case IPV6_RTHDRDSTOPTS:
2066 case IPV6_USE_MIN_MTU:
2067 case IPV6_PREFER_TEMPADDR:
2068 error = ip6_getpcbopt(in6p->in6p_outputopts,
2072 case IPV6_MULTICAST_IF:
2073 case IPV6_MULTICAST_HOPS:
2074 case IPV6_MULTICAST_LOOP:
2076 error = ip6_getmoptions(in6p, sopt);
2080 case IPV6_IPSEC_POLICY:
2084 struct mbuf *m = NULL;
2085 struct mbuf **mp = &m;
2086 size_t ovalsize = sopt->sopt_valsize;
2087 caddr_t oval = (caddr_t)sopt->sopt_val;
2089 error = soopt_getm(sopt, &m); /* XXX */
2092 error = soopt_mcopyin(sopt, m); /* XXX */
2095 sopt->sopt_valsize = ovalsize;
2096 sopt->sopt_val = oval;
2098 req = mtod(m, caddr_t);
2101 error = ipsec_get_policy(in6p, req, len, mp);
2103 error = soopt_mcopyout(sopt, m); /* XXX */
2104 if (error == 0 && m)
2111 error = ENOPROTOOPT;
2121 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2123 int error = 0, optval, optlen;
2124 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2125 struct inpcb *in6p = sotoinpcb(so);
2126 int level, op, optname;
2128 level = sopt->sopt_level;
2129 op = sopt->sopt_dir;
2130 optname = sopt->sopt_name;
2131 optlen = sopt->sopt_valsize;
2133 if (level != IPPROTO_IPV6) {
2140 * For ICMPv6 sockets, no modification allowed for checksum
2141 * offset, permit "no change" values to help existing apps.
2143 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2144 * for an ICMPv6 socket will fail."
2145 * The current behavior does not meet RFC3542.
2149 if (optlen != sizeof(int)) {
2153 error = sooptcopyin(sopt, &optval, sizeof(optval),
2157 if ((optval % 2) != 0) {
2158 /* the API assumes even offset values */
2160 } else if (so->so_proto->pr_protocol ==
2162 if (optval != icmp6off)
2165 in6p->in6p_cksum = optval;
2169 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2172 optval = in6p->in6p_cksum;
2174 error = sooptcopyout(sopt, &optval, sizeof(optval));
2184 error = ENOPROTOOPT;
2192 * Set up IP6 options in pcb for insertion in output packets or
2193 * specifying behavior of outgoing packets.
2196 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2197 struct socket *so, struct sockopt *sopt)
2199 struct ip6_pktopts *opt = *pktopt;
2201 struct thread *td = sopt->sopt_td;
2203 /* turn off any old options. */
2206 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2207 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2208 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2209 printf("ip6_pcbopts: all specified options are cleared.\n");
2211 ip6_clearpktopts(opt, -1);
2213 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2216 if (!m || m->m_len == 0) {
2218 * Only turning off any previous options, regardless of
2219 * whether the opt is just created or given.
2221 free(opt, M_IP6OPT);
2225 /* set options specified by user. */
2226 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2227 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2228 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2229 free(opt, M_IP6OPT);
2237 * initialize ip6_pktopts. beware that there are non-zero default values in
2241 ip6_initpktopts(struct ip6_pktopts *opt)
2244 bzero(opt, sizeof(*opt));
2245 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2246 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2247 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2248 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2252 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2253 struct ucred *cred, int uproto)
2255 struct ip6_pktopts *opt;
2257 if (*pktopt == NULL) {
2258 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2260 ip6_initpktopts(*pktopt);
2264 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2268 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2270 void *optdata = NULL;
2272 struct ip6_ext *ip6e;
2274 struct in6_pktinfo null_pktinfo;
2275 int deftclass = 0, on;
2276 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2277 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2281 optdata = (void *)&null_pktinfo;
2282 if (pktopt && pktopt->ip6po_pktinfo) {
2283 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2284 sizeof(null_pktinfo));
2285 in6_clearscope(&null_pktinfo.ipi6_addr);
2287 /* XXX: we don't have to do this every time... */
2288 bzero(&null_pktinfo, sizeof(null_pktinfo));
2290 optdatalen = sizeof(struct in6_pktinfo);
2293 if (pktopt && pktopt->ip6po_tclass >= 0)
2294 optdata = (void *)&pktopt->ip6po_tclass;
2296 optdata = (void *)&deftclass;
2297 optdatalen = sizeof(int);
2300 if (pktopt && pktopt->ip6po_hbh) {
2301 optdata = (void *)pktopt->ip6po_hbh;
2302 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2303 optdatalen = (ip6e->ip6e_len + 1) << 3;
2307 if (pktopt && pktopt->ip6po_rthdr) {
2308 optdata = (void *)pktopt->ip6po_rthdr;
2309 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2310 optdatalen = (ip6e->ip6e_len + 1) << 3;
2313 case IPV6_RTHDRDSTOPTS:
2314 if (pktopt && pktopt->ip6po_dest1) {
2315 optdata = (void *)pktopt->ip6po_dest1;
2316 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2317 optdatalen = (ip6e->ip6e_len + 1) << 3;
2321 if (pktopt && pktopt->ip6po_dest2) {
2322 optdata = (void *)pktopt->ip6po_dest2;
2323 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2324 optdatalen = (ip6e->ip6e_len + 1) << 3;
2328 if (pktopt && pktopt->ip6po_nexthop) {
2329 optdata = (void *)pktopt->ip6po_nexthop;
2330 optdatalen = pktopt->ip6po_nexthop->sa_len;
2333 case IPV6_USE_MIN_MTU:
2335 optdata = (void *)&pktopt->ip6po_minmtu;
2337 optdata = (void *)&defminmtu;
2338 optdatalen = sizeof(int);
2341 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2345 optdata = (void *)&on;
2346 optdatalen = sizeof(on);
2348 case IPV6_PREFER_TEMPADDR:
2350 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2352 optdata = (void *)&defpreftemp;
2353 optdatalen = sizeof(int);
2355 default: /* should not happen */
2357 panic("ip6_getpcbopt: unexpected option\n");
2359 return (ENOPROTOOPT);
2362 error = sooptcopyout(sopt, optdata, optdatalen);
2368 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2373 if (optname == -1 || optname == IPV6_PKTINFO) {
2374 if (pktopt->ip6po_pktinfo)
2375 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2376 pktopt->ip6po_pktinfo = NULL;
2378 if (optname == -1 || optname == IPV6_HOPLIMIT)
2379 pktopt->ip6po_hlim = -1;
2380 if (optname == -1 || optname == IPV6_TCLASS)
2381 pktopt->ip6po_tclass = -1;
2382 if (optname == -1 || optname == IPV6_NEXTHOP) {
2383 if (pktopt->ip6po_nextroute.ro_rt) {
2384 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2385 pktopt->ip6po_nextroute.ro_rt = NULL;
2387 if (pktopt->ip6po_nexthop)
2388 free(pktopt->ip6po_nexthop, M_IP6OPT);
2389 pktopt->ip6po_nexthop = NULL;
2391 if (optname == -1 || optname == IPV6_HOPOPTS) {
2392 if (pktopt->ip6po_hbh)
2393 free(pktopt->ip6po_hbh, M_IP6OPT);
2394 pktopt->ip6po_hbh = NULL;
2396 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2397 if (pktopt->ip6po_dest1)
2398 free(pktopt->ip6po_dest1, M_IP6OPT);
2399 pktopt->ip6po_dest1 = NULL;
2401 if (optname == -1 || optname == IPV6_RTHDR) {
2402 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2403 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2404 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2405 if (pktopt->ip6po_route.ro_rt) {
2406 RTFREE(pktopt->ip6po_route.ro_rt);
2407 pktopt->ip6po_route.ro_rt = NULL;
2410 if (optname == -1 || optname == IPV6_DSTOPTS) {
2411 if (pktopt->ip6po_dest2)
2412 free(pktopt->ip6po_dest2, M_IP6OPT);
2413 pktopt->ip6po_dest2 = NULL;
2417 #define PKTOPT_EXTHDRCPY(type) \
2420 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2421 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2422 if (dst->type == NULL && canwait == M_NOWAIT)\
2424 bcopy(src->type, dst->type, hlen);\
2426 } while (/*CONSTCOND*/ 0)
2429 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2431 if (dst == NULL || src == NULL) {
2432 printf("ip6_clearpktopts: invalid argument\n");
2436 dst->ip6po_hlim = src->ip6po_hlim;
2437 dst->ip6po_tclass = src->ip6po_tclass;
2438 dst->ip6po_flags = src->ip6po_flags;
2439 dst->ip6po_minmtu = src->ip6po_minmtu;
2440 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2441 if (src->ip6po_pktinfo) {
2442 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2444 if (dst->ip6po_pktinfo == NULL)
2446 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2448 if (src->ip6po_nexthop) {
2449 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2451 if (dst->ip6po_nexthop == NULL)
2453 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2454 src->ip6po_nexthop->sa_len);
2456 PKTOPT_EXTHDRCPY(ip6po_hbh);
2457 PKTOPT_EXTHDRCPY(ip6po_dest1);
2458 PKTOPT_EXTHDRCPY(ip6po_dest2);
2459 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2463 ip6_clearpktopts(dst, -1);
2466 #undef PKTOPT_EXTHDRCPY
2468 struct ip6_pktopts *
2469 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2472 struct ip6_pktopts *dst;
2474 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2477 ip6_initpktopts(dst);
2479 if ((error = copypktopts(dst, src, canwait)) != 0) {
2480 free(dst, M_IP6OPT);
2488 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2493 ip6_clearpktopts(pktopt, -1);
2495 free(pktopt, M_IP6OPT);
2499 * Set IPv6 outgoing packet options based on advanced API.
2502 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2503 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2505 struct cmsghdr *cm = 0;
2507 if (control == NULL || opt == NULL)
2510 ip6_initpktopts(opt);
2515 * If stickyopt is provided, make a local copy of the options
2516 * for this particular packet, then override them by ancillary
2518 * XXX: copypktopts() does not copy the cached route to a next
2519 * hop (if any). This is not very good in terms of efficiency,
2520 * but we can allow this since this option should be rarely
2523 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2528 * XXX: Currently, we assume all the optional information is stored
2531 if (control->m_next)
2534 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2535 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2538 if (control->m_len < CMSG_LEN(0))
2541 cm = mtod(control, struct cmsghdr *);
2542 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2544 if (cm->cmsg_level != IPPROTO_IPV6)
2547 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2548 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2557 * Set a particular packet option, as a sticky option or an ancillary data
2558 * item. "len" can be 0 only when it's a sticky option.
2559 * We have 4 cases of combination of "sticky" and "cmsg":
2560 * "sticky=0, cmsg=0": impossible
2561 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2562 * "sticky=1, cmsg=0": RFC3542 socket option
2563 * "sticky=1, cmsg=1": RFC2292 socket option
2566 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2567 struct ucred *cred, int sticky, int cmsg, int uproto)
2569 int minmtupolicy, preftemp;
2572 if (!sticky && !cmsg) {
2574 printf("ip6_setpktopt: impossible case\n");
2580 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2581 * not be specified in the context of RFC3542. Conversely,
2582 * RFC3542 types should not be specified in the context of RFC2292.
2586 case IPV6_2292PKTINFO:
2587 case IPV6_2292HOPLIMIT:
2588 case IPV6_2292NEXTHOP:
2589 case IPV6_2292HOPOPTS:
2590 case IPV6_2292DSTOPTS:
2591 case IPV6_2292RTHDR:
2592 case IPV6_2292PKTOPTIONS:
2593 return (ENOPROTOOPT);
2596 if (sticky && cmsg) {
2603 case IPV6_RTHDRDSTOPTS:
2605 case IPV6_USE_MIN_MTU:
2608 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2609 return (ENOPROTOOPT);
2614 case IPV6_2292PKTINFO:
2617 struct ifnet *ifp = NULL;
2618 struct in6_pktinfo *pktinfo;
2620 if (len != sizeof(struct in6_pktinfo))
2623 pktinfo = (struct in6_pktinfo *)buf;
2626 * An application can clear any sticky IPV6_PKTINFO option by
2627 * doing a "regular" setsockopt with ipi6_addr being
2628 * in6addr_any and ipi6_ifindex being zero.
2629 * [RFC 3542, Section 6]
2631 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2632 pktinfo->ipi6_ifindex == 0 &&
2633 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2634 ip6_clearpktopts(opt, optname);
2638 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2639 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2642 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2644 /* validate the interface index if specified. */
2645 if (pktinfo->ipi6_ifindex > V_if_index)
2647 if (pktinfo->ipi6_ifindex) {
2648 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2652 if (ifp != NULL && (
2653 ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED))
2657 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2658 struct in6_ifaddr *ia;
2660 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2661 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2663 return (EADDRNOTAVAIL);
2664 ifa_free(&ia->ia_ifa);
2667 * We store the address anyway, and let in6_selectsrc()
2668 * validate the specified address. This is because ipi6_addr
2669 * may not have enough information about its scope zone, and
2670 * we may need additional information (such as outgoing
2671 * interface or the scope zone of a destination address) to
2672 * disambiguate the scope.
2673 * XXX: the delay of the validation may confuse the
2674 * application when it is used as a sticky option.
2676 if (opt->ip6po_pktinfo == NULL) {
2677 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2678 M_IP6OPT, M_NOWAIT);
2679 if (opt->ip6po_pktinfo == NULL)
2682 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2686 case IPV6_2292HOPLIMIT:
2692 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2693 * to simplify the ordering among hoplimit options.
2695 if (optname == IPV6_HOPLIMIT && sticky)
2696 return (ENOPROTOOPT);
2698 if (len != sizeof(int))
2701 if (*hlimp < -1 || *hlimp > 255)
2704 opt->ip6po_hlim = *hlimp;
2712 if (len != sizeof(int))
2714 tclass = *(int *)buf;
2715 if (tclass < -1 || tclass > 255)
2718 opt->ip6po_tclass = tclass;
2722 case IPV6_2292NEXTHOP:
2725 error = priv_check_cred(cred,
2726 PRIV_NETINET_SETHDROPTS, 0);
2731 if (len == 0) { /* just remove the option */
2732 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2736 /* check if cmsg_len is large enough for sa_len */
2737 if (len < sizeof(struct sockaddr) || len < *buf)
2740 switch (((struct sockaddr *)buf)->sa_family) {
2743 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2746 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2749 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2750 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2753 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2759 case AF_LINK: /* should eventually be supported */
2761 return (EAFNOSUPPORT);
2764 /* turn off the previous option, then set the new option. */
2765 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2766 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2767 if (opt->ip6po_nexthop == NULL)
2769 bcopy(buf, opt->ip6po_nexthop, *buf);
2772 case IPV6_2292HOPOPTS:
2775 struct ip6_hbh *hbh;
2779 * XXX: We don't allow a non-privileged user to set ANY HbH
2780 * options, since per-option restriction has too much
2784 error = priv_check_cred(cred,
2785 PRIV_NETINET_SETHDROPTS, 0);
2791 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2792 break; /* just remove the option */
2795 /* message length validation */
2796 if (len < sizeof(struct ip6_hbh))
2798 hbh = (struct ip6_hbh *)buf;
2799 hbhlen = (hbh->ip6h_len + 1) << 3;
2803 /* turn off the previous option, then set the new option. */
2804 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2805 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2806 if (opt->ip6po_hbh == NULL)
2808 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2813 case IPV6_2292DSTOPTS:
2815 case IPV6_RTHDRDSTOPTS:
2817 struct ip6_dest *dest, **newdest = NULL;
2820 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2821 error = priv_check_cred(cred,
2822 PRIV_NETINET_SETHDROPTS, 0);
2828 ip6_clearpktopts(opt, optname);
2829 break; /* just remove the option */
2832 /* message length validation */
2833 if (len < sizeof(struct ip6_dest))
2835 dest = (struct ip6_dest *)buf;
2836 destlen = (dest->ip6d_len + 1) << 3;
2841 * Determine the position that the destination options header
2842 * should be inserted; before or after the routing header.
2845 case IPV6_2292DSTOPTS:
2847 * The old advacned API is ambiguous on this point.
2848 * Our approach is to determine the position based
2849 * according to the existence of a routing header.
2850 * Note, however, that this depends on the order of the
2851 * extension headers in the ancillary data; the 1st
2852 * part of the destination options header must appear
2853 * before the routing header in the ancillary data,
2855 * RFC3542 solved the ambiguity by introducing
2856 * separate ancillary data or option types.
2858 if (opt->ip6po_rthdr == NULL)
2859 newdest = &opt->ip6po_dest1;
2861 newdest = &opt->ip6po_dest2;
2863 case IPV6_RTHDRDSTOPTS:
2864 newdest = &opt->ip6po_dest1;
2867 newdest = &opt->ip6po_dest2;
2871 /* turn off the previous option, then set the new option. */
2872 ip6_clearpktopts(opt, optname);
2873 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2874 if (*newdest == NULL)
2876 bcopy(dest, *newdest, destlen);
2881 case IPV6_2292RTHDR:
2884 struct ip6_rthdr *rth;
2888 ip6_clearpktopts(opt, IPV6_RTHDR);
2889 break; /* just remove the option */
2892 /* message length validation */
2893 if (len < sizeof(struct ip6_rthdr))
2895 rth = (struct ip6_rthdr *)buf;
2896 rthlen = (rth->ip6r_len + 1) << 3;
2900 switch (rth->ip6r_type) {
2901 case IPV6_RTHDR_TYPE_0:
2902 if (rth->ip6r_len == 0) /* must contain one addr */
2904 if (rth->ip6r_len % 2) /* length must be even */
2906 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2910 return (EINVAL); /* not supported */
2913 /* turn off the previous option */
2914 ip6_clearpktopts(opt, IPV6_RTHDR);
2915 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2916 if (opt->ip6po_rthdr == NULL)
2918 bcopy(rth, opt->ip6po_rthdr, rthlen);
2923 case IPV6_USE_MIN_MTU:
2924 if (len != sizeof(int))
2926 minmtupolicy = *(int *)buf;
2927 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2928 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2929 minmtupolicy != IP6PO_MINMTU_ALL) {
2932 opt->ip6po_minmtu = minmtupolicy;
2936 if (len != sizeof(int))
2939 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2941 * we ignore this option for TCP sockets.
2942 * (RFC3542 leaves this case unspecified.)
2944 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2946 opt->ip6po_flags |= IP6PO_DONTFRAG;
2949 case IPV6_PREFER_TEMPADDR:
2950 if (len != sizeof(int))
2952 preftemp = *(int *)buf;
2953 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2954 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2955 preftemp != IP6PO_TEMPADDR_PREFER) {
2958 opt->ip6po_prefer_tempaddr = preftemp;
2962 return (ENOPROTOOPT);
2963 } /* end of switch */
2969 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2970 * packet to the input queue of a specified interface. Note that this
2971 * calls the output routine of the loopback "driver", but with an interface
2972 * pointer that might NOT be &loif -- easier than replicating that code here.
2975 ip6_mloopback(struct ifnet *ifp, const struct mbuf *m)
2978 struct ip6_hdr *ip6;
2980 copym = m_copy(m, 0, M_COPYALL);
2985 * Make sure to deep-copy IPv6 header portion in case the data
2986 * is in an mbuf cluster, so that we can safely override the IPv6
2987 * header portion later.
2989 if (!M_WRITABLE(copym) ||
2990 copym->m_len < sizeof(struct ip6_hdr)) {
2991 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2995 ip6 = mtod(copym, struct ip6_hdr *);
2997 * clear embedded scope identifiers if necessary.
2998 * in6_clearscope will touch the addresses only when necessary.
3000 in6_clearscope(&ip6->ip6_src);
3001 in6_clearscope(&ip6->ip6_dst);
3002 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3003 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3005 copym->m_pkthdr.csum_data = 0xffff;
3007 if_simloop(ifp, copym, AF_INET6, 0);
3011 * Chop IPv6 header off from the payload.
3014 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3017 struct ip6_hdr *ip6;
3019 ip6 = mtod(m, struct ip6_hdr *);
3020 if (m->m_len > sizeof(*ip6)) {
3021 mh = m_gethdr(M_NOWAIT, MT_DATA);
3026 m_move_pkthdr(mh, m);
3027 M_ALIGN(mh, sizeof(*ip6));
3028 m->m_len -= sizeof(*ip6);
3029 m->m_data += sizeof(*ip6);
3032 m->m_len = sizeof(*ip6);
3033 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3035 exthdrs->ip6e_ip6 = m;
3040 * Compute IPv6 extension header length.
3043 ip6_optlen(struct inpcb *in6p)
3047 if (!in6p->in6p_outputopts)
3052 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3054 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3055 if (in6p->in6p_outputopts->ip6po_rthdr)
3056 /* dest1 is valid with rthdr only */
3057 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3058 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3059 len += elen(in6p->in6p_outputopts->ip6po_dest2);