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 *, const 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, const 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)) == NULL) {
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 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 bzero(&exthdrs, sizeof(exthdrs));
343 /* Hop-by-Hop options header */
344 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
345 /* Destination options header(1st part) */
346 if (opt->ip6po_rthdr) {
348 * Destination options header(1st part)
349 * This only makes sense with a routing header.
350 * See Section 9.2 of RFC 3542.
351 * Disabling this part just for MIP6 convenience is
352 * a bad idea. We need to think carefully about a
353 * way to make the advanced API coexist with MIP6
354 * options, which might automatically be inserted in
357 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
360 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
361 /* Destination options header(2nd part) */
362 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
367 * IPSec checking which handles several cases.
368 * FAST IPSEC: We re-injected the packet.
369 * XXX: need scope argument.
371 switch(ip6_ipsec_output(&m, inp, &error))
373 case 1: /* Bad packet */
375 case -1: /* IPSec done */
377 case 0: /* No IPSec */
384 * Calculate the total length of the extension header chain.
385 * Keep the length of the unfragmentable part for fragmentation.
388 if (exthdrs.ip6e_hbh)
389 optlen += exthdrs.ip6e_hbh->m_len;
390 if (exthdrs.ip6e_dest1)
391 optlen += exthdrs.ip6e_dest1->m_len;
392 if (exthdrs.ip6e_rthdr)
393 optlen += exthdrs.ip6e_rthdr->m_len;
394 unfragpartlen = optlen + sizeof(struct ip6_hdr);
396 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
397 if (exthdrs.ip6e_dest2)
398 optlen += exthdrs.ip6e_dest2->m_len;
401 * If there is at least one extension header,
402 * separate IP6 header from the payload.
404 if (optlen && !hdrsplit) {
405 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
409 m = exthdrs.ip6e_ip6;
414 ip6 = mtod(m, struct ip6_hdr *);
416 /* adjust mbuf packet header length */
417 m->m_pkthdr.len += optlen;
418 plen = m->m_pkthdr.len - sizeof(*ip6);
420 /* If this is a jumbo payload, insert a jumbo payload option. */
421 if (plen > IPV6_MAXPACKET) {
423 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
427 m = exthdrs.ip6e_ip6;
431 ip6 = mtod(m, struct ip6_hdr *);
432 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
436 ip6->ip6_plen = htons(plen);
439 * Concatenate headers and fill in next header fields.
440 * Here we have, on "m"
442 * and we insert headers accordingly. Finally, we should be getting:
443 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
445 * during the header composing process, "m" points to IPv6 header.
446 * "mprev" points to an extension header prior to esp.
448 u_char *nexthdrp = &ip6->ip6_nxt;
452 * we treat dest2 specially. this makes IPsec processing
453 * much easier. the goal here is to make mprev point the
454 * mbuf prior to dest2.
456 * result: IPv6 dest2 payload
457 * m and mprev will point to IPv6 header.
459 if (exthdrs.ip6e_dest2) {
461 panic("assumption failed: hdr not split");
462 exthdrs.ip6e_dest2->m_next = m->m_next;
463 m->m_next = exthdrs.ip6e_dest2;
464 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
465 ip6->ip6_nxt = IPPROTO_DSTOPTS;
469 * result: IPv6 hbh dest1 rthdr dest2 payload
470 * m will point to IPv6 header. mprev will point to the
471 * extension header prior to dest2 (rthdr in the above case).
473 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
474 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
476 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
480 * If there is a routing header, discard the packet.
482 if (exthdrs.ip6e_rthdr) {
487 /* Source address validation */
488 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
489 (flags & IPV6_UNSPECSRC) == 0) {
491 IP6STAT_INC(ip6s_badscope);
494 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
496 IP6STAT_INC(ip6s_badscope);
500 IP6STAT_INC(ip6s_localout);
507 bzero((caddr_t)ro, sizeof(*ro));
510 if (opt && opt->ip6po_rthdr)
511 ro = &opt->ip6po_route;
512 dst = (struct sockaddr_in6 *)&ro->ro_dst;
514 if (ro->ro_rt == NULL)
515 (void )flowtable_lookup(AF_INET6, m, (struct route *)ro);
517 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
520 * if specified, try to fill in the traffic class field.
521 * do not override if a non-zero value is already set.
522 * we check the diffserv field and the ecn field separately.
524 if (opt && opt->ip6po_tclass >= 0) {
527 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
529 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
532 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
535 /* fill in or override the hop limit field, if necessary. */
536 if (opt && opt->ip6po_hlim != -1)
537 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
538 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
540 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
542 ip6->ip6_hlim = V_ip6_defmcasthlim;
546 ip6 = mtod(m, struct ip6_hdr *);
549 * Validate route against routing table additions;
550 * a better/more specific route might have been added.
551 * Make sure address family is set in route.
554 ro->ro_dst.sin6_family = AF_INET6;
555 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
557 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
558 ro->ro_dst.sin6_family == AF_INET6 &&
559 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
561 ifp = ro->ro_rt->rt_ifp;
563 if (fwd_tag == NULL) {
564 bzero(&dst_sa, sizeof(dst_sa));
565 dst_sa.sin6_family = AF_INET6;
566 dst_sa.sin6_len = sizeof(dst_sa);
567 dst_sa.sin6_addr = ip6->ip6_dst;
569 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
573 in6_ifstat_inc(ifp, ifs6_out_discard);
579 * If in6_selectroute() does not return a route entry,
580 * dst may not have been updated.
582 *dst = dst_sa; /* XXX */
586 * then rt (for unicast) and ifp must be non-NULL valid values.
588 if ((flags & IPV6_FORWARDING) == 0) {
589 /* XXX: the FORWARDING flag can be set for mrouting. */
590 in6_ifstat_inc(ifp, ifs6_out_request);
593 ia = (struct in6_ifaddr *)(rt->rt_ifa);
594 counter_u64_add(rt->rt_pksent, 1);
599 * The outgoing interface must be in the zone of source and
600 * destination addresses.
605 if (in6_setscope(&src0, origifp, &zone))
607 bzero(&src_sa, sizeof(src_sa));
608 src_sa.sin6_family = AF_INET6;
609 src_sa.sin6_len = sizeof(src_sa);
610 src_sa.sin6_addr = ip6->ip6_src;
611 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
615 if (in6_setscope(&dst0, origifp, &zone))
617 /* re-initialize to be sure */
618 bzero(&dst_sa, sizeof(dst_sa));
619 dst_sa.sin6_family = AF_INET6;
620 dst_sa.sin6_len = sizeof(dst_sa);
621 dst_sa.sin6_addr = ip6->ip6_dst;
622 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
626 /* We should use ia_ifp to support the case of
627 * sending packets to an address of our own.
629 if (ia != NULL && ia->ia_ifp)
632 /* scope check is done. */
636 IP6STAT_INC(ip6s_badscope);
637 in6_ifstat_inc(origifp, ifs6_out_discard);
639 error = EHOSTUNREACH; /* XXX */
643 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
644 if (opt && opt->ip6po_nextroute.ro_rt) {
646 * The nexthop is explicitly specified by the
647 * application. We assume the next hop is an IPv6
650 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
652 else if ((rt->rt_flags & RTF_GATEWAY))
653 dst = (struct sockaddr_in6 *)rt->rt_gateway;
656 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
657 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
659 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
660 in6_ifstat_inc(ifp, ifs6_out_mcast);
662 * Confirm that the outgoing interface supports multicast.
664 if (!(ifp->if_flags & IFF_MULTICAST)) {
665 IP6STAT_INC(ip6s_noroute);
666 in6_ifstat_inc(ifp, ifs6_out_discard);
670 if ((im6o == NULL && in6_mcast_loop) ||
671 (im6o && im6o->im6o_multicast_loop)) {
673 * Loop back multicast datagram if not expressly
674 * forbidden to do so, even if we have not joined
675 * the address; protocols will filter it later,
676 * thus deferring a hash lookup and lock acquisition
677 * at the expense of an m_copym().
679 ip6_mloopback(ifp, m);
682 * If we are acting as a multicast router, perform
683 * multicast forwarding as if the packet had just
684 * arrived on the interface to which we are about
685 * to send. The multicast forwarding function
686 * recursively calls this function, using the
687 * IPV6_FORWARDING flag to prevent infinite recursion.
689 * Multicasts that are looped back by ip6_mloopback(),
690 * above, will be forwarded by the ip6_input() routine,
693 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
695 * XXX: ip6_mforward expects that rcvif is NULL
696 * when it is called from the originating path.
697 * However, it may not always be the case.
699 m->m_pkthdr.rcvif = NULL;
700 if (ip6_mforward(ip6, ifp, m) != 0) {
707 * Multicasts with a hoplimit of zero may be looped back,
708 * above, but must not be transmitted on a network.
709 * Also, multicasts addressed to the loopback interface
710 * are not sent -- the above call to ip6_mloopback() will
711 * loop back a copy if this host actually belongs to the
712 * destination group on the loopback interface.
714 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
715 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
722 * Fill the outgoing inteface to tell the upper layer
723 * to increment per-interface statistics.
728 /* Determine path MTU. */
729 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
730 &mtu, &alwaysfrag, fibnum)) != 0)
734 * The caller of this function may specify to use the minimum MTU
736 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
737 * setting. The logic is a bit complicated; by default, unicast
738 * packets will follow path MTU while multicast packets will be sent at
739 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
740 * including unicast ones will be sent at the minimum MTU. Multicast
741 * packets will always be sent at the minimum MTU unless
742 * IP6PO_MINMTU_DISABLE is explicitly specified.
743 * See RFC 3542 for more details.
745 if (mtu > IPV6_MMTU) {
746 if ((flags & IPV6_MINMTU))
748 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
750 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
752 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
758 * clear embedded scope identifiers if necessary.
759 * in6_clearscope will touch the addresses only when necessary.
761 in6_clearscope(&ip6->ip6_src);
762 in6_clearscope(&ip6->ip6_dst);
765 * If the outgoing packet contains a hop-by-hop options header,
766 * it must be examined and processed even by the source node.
767 * (RFC 2460, section 4.)
769 if (exthdrs.ip6e_hbh) {
770 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
771 u_int32_t dummy; /* XXX unused */
772 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
775 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
776 panic("ip6e_hbh is not contiguous");
779 * XXX: if we have to send an ICMPv6 error to the sender,
780 * we need the M_LOOP flag since icmp6_error() expects
781 * the IPv6 and the hop-by-hop options header are
782 * contiguous unless the flag is set.
784 m->m_flags |= M_LOOP;
785 m->m_pkthdr.rcvif = ifp;
786 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
787 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
788 &dummy, &plen) < 0) {
789 /* m was already freed at this point */
790 error = EINVAL;/* better error? */
793 m->m_flags &= ~M_LOOP; /* XXX */
794 m->m_pkthdr.rcvif = NULL;
797 /* Jump over all PFIL processing if hooks are not active. */
798 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
802 /* Run through list of hooks for output packets. */
803 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
804 if (error != 0 || m == NULL)
806 ip6 = mtod(m, struct ip6_hdr *);
809 /* See if destination IP address was changed by packet filter. */
810 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
811 m->m_flags |= M_SKIP_FIREWALL;
812 /* If destination is now ourself drop to ip6_input(). */
813 if (in6_localip(&ip6->ip6_dst)) {
814 m->m_flags |= M_FASTFWD_OURS;
815 if (m->m_pkthdr.rcvif == NULL)
816 m->m_pkthdr.rcvif = V_loif;
817 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
818 m->m_pkthdr.csum_flags |=
819 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
820 m->m_pkthdr.csum_data = 0xffff;
823 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
824 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
826 error = netisr_queue(NETISR_IPV6, m);
830 needfiblookup = 1; /* Redo the routing table lookup. */
833 /* See if fib was changed by packet filter. */
834 if (fibnum != M_GETFIB(m)) {
835 m->m_flags |= M_SKIP_FIREWALL;
836 fibnum = M_GETFIB(m);
843 /* See if local, if yes, send it to netisr. */
844 if (m->m_flags & M_FASTFWD_OURS) {
845 if (m->m_pkthdr.rcvif == NULL)
846 m->m_pkthdr.rcvif = V_loif;
847 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
848 m->m_pkthdr.csum_flags |=
849 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
850 m->m_pkthdr.csum_data = 0xffff;
853 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
854 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
856 error = netisr_queue(NETISR_IPV6, m);
859 /* Or forward to some other address? */
860 if ((m->m_flags & M_IP6_NEXTHOP) &&
861 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
862 dst = (struct sockaddr_in6 *)&ro->ro_dst;
863 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
864 m->m_flags |= M_SKIP_FIREWALL;
865 m->m_flags &= ~M_IP6_NEXTHOP;
866 m_tag_delete(m, fwd_tag);
872 * Send the packet to the outgoing interface.
873 * If necessary, do IPv6 fragmentation before sending.
875 * the logic here is rather complex:
876 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
877 * 1-a: send as is if tlen <= path mtu
878 * 1-b: fragment if tlen > path mtu
880 * 2: if user asks us not to fragment (dontfrag == 1)
881 * 2-a: send as is if tlen <= interface mtu
882 * 2-b: error if tlen > interface mtu
884 * 3: if we always need to attach fragment header (alwaysfrag == 1)
887 * 4: if dontfrag == 1 && alwaysfrag == 1
888 * error, as we cannot handle this conflicting request
890 sw_csum = m->m_pkthdr.csum_flags;
892 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
893 sw_csum &= ~ifp->if_hwassist;
897 * If we added extension headers, we will not do TSO and calculate the
898 * checksums ourselves for now.
899 * XXX-BZ Need a framework to know when the NIC can handle it, even
902 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
903 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
904 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
907 if (sw_csum & CSUM_SCTP_IPV6) {
908 sw_csum &= ~CSUM_SCTP_IPV6;
909 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
912 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
913 tlen = m->m_pkthdr.len;
915 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
919 if (dontfrag && alwaysfrag) { /* case 4 */
920 /* conflicting request - can't transmit */
924 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
926 * Even if the DONTFRAG option is specified, we cannot send the
927 * packet when the data length is larger than the MTU of the
928 * outgoing interface.
929 * Notify the error by sending IPV6_PATHMTU ancillary data if
930 * application wanted to know the MTU value. Also return an
931 * error code (this is not described in the API spec).
934 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
940 * transmit packet without fragmentation
942 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
943 struct in6_ifaddr *ia6;
945 ip6 = mtod(m, struct ip6_hdr *);
946 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
948 /* Record statistics for this interface address. */
949 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
950 counter_u64_add(ia6->ia_ifa.ifa_obytes,
952 ifa_free(&ia6->ia_ifa);
954 error = nd6_output_ifp(ifp, origifp, m, dst,
960 * try to fragment the packet. case 1-b and 3
962 if (mtu < IPV6_MMTU) {
963 /* path MTU cannot be less than IPV6_MMTU */
965 in6_ifstat_inc(ifp, ifs6_out_fragfail);
967 } else if (ip6->ip6_plen == 0) {
968 /* jumbo payload cannot be fragmented */
970 in6_ifstat_inc(ifp, ifs6_out_fragfail);
976 * Too large for the destination or interface;
977 * fragment if possible.
978 * Must be able to put at least 8 bytes per fragment.
980 hlen = unfragpartlen;
981 if (mtu > IPV6_MAXPACKET)
982 mtu = IPV6_MAXPACKET;
984 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
987 in6_ifstat_inc(ifp, ifs6_out_fragfail);
992 * If the interface will not calculate checksums on
993 * fragmented packets, then do it here.
994 * XXX-BZ handle the hw offloading case. Need flags.
996 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
997 in6_delayed_cksum(m, plen, hlen);
998 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1001 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1002 sctp_delayed_cksum(m, hlen);
1003 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1007 * Change the next header field of the last header in the
1008 * unfragmentable part.
1010 if (exthdrs.ip6e_rthdr) {
1011 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1012 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1013 } else if (exthdrs.ip6e_dest1) {
1014 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1015 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1016 } else if (exthdrs.ip6e_hbh) {
1017 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1018 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1020 nextproto = ip6->ip6_nxt;
1021 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1025 * Loop through length of segment after first fragment,
1026 * make new header and copy data of each part and link onto
1030 id = htonl(ip6_randomid());
1031 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1034 in6_ifstat_inc(ifp, ifs6_out_fragok);
1038 * Remove leading garbages.
1044 for (m0 = m; m; m = m0) {
1048 /* Record statistics for this interface address. */
1050 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1051 counter_u64_add(ia->ia_ifa.ifa_obytes,
1054 error = nd6_output_ifp(ifp, origifp, m, dst,
1055 (struct route *)ro);
1061 IP6STAT_INC(ip6s_fragmented);
1064 if (ro == &ip6route)
1069 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1070 m_freem(exthdrs.ip6e_dest1);
1071 m_freem(exthdrs.ip6e_rthdr);
1072 m_freem(exthdrs.ip6e_dest2);
1081 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1085 if (hlen > MCLBYTES)
1086 return (ENOBUFS); /* XXX */
1089 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1091 m = m_get(M_NOWAIT, MT_DATA);
1096 bcopy(hdr, mtod(m, caddr_t), hlen);
1103 * Insert jumbo payload option.
1106 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1112 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1115 * If there is no hop-by-hop options header, allocate new one.
1116 * If there is one but it doesn't have enough space to store the
1117 * jumbo payload option, allocate a cluster to store the whole options.
1118 * Otherwise, use it to store the options.
1120 if (exthdrs->ip6e_hbh == NULL) {
1121 mopt = m_get(M_NOWAIT, MT_DATA);
1124 mopt->m_len = JUMBOOPTLEN;
1125 optbuf = mtod(mopt, u_char *);
1126 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1127 exthdrs->ip6e_hbh = mopt;
1129 struct ip6_hbh *hbh;
1131 mopt = exthdrs->ip6e_hbh;
1132 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1135 * - exthdrs->ip6e_hbh is not referenced from places
1136 * other than exthdrs.
1137 * - exthdrs->ip6e_hbh is not an mbuf chain.
1139 int oldoptlen = mopt->m_len;
1143 * XXX: give up if the whole (new) hbh header does
1144 * not fit even in an mbuf cluster.
1146 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1150 * As a consequence, we must always prepare a cluster
1153 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1156 n->m_len = oldoptlen + JUMBOOPTLEN;
1157 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1159 optbuf = mtod(n, caddr_t) + oldoptlen;
1161 mopt = exthdrs->ip6e_hbh = n;
1163 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1164 mopt->m_len += JUMBOOPTLEN;
1166 optbuf[0] = IP6OPT_PADN;
1170 * Adjust the header length according to the pad and
1171 * the jumbo payload option.
1173 hbh = mtod(mopt, struct ip6_hbh *);
1174 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1177 /* fill in the option. */
1178 optbuf[2] = IP6OPT_JUMBO;
1180 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1181 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1183 /* finally, adjust the packet header length */
1184 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1191 * Insert fragment header and copy unfragmentable header portions.
1194 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1195 struct ip6_frag **frghdrp)
1197 struct mbuf *n, *mlast;
1199 if (hlen > sizeof(struct ip6_hdr)) {
1200 n = m_copym(m0, sizeof(struct ip6_hdr),
1201 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1208 /* Search for the last mbuf of unfragmentable part. */
1209 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1212 if (M_WRITABLE(mlast) &&
1213 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1214 /* use the trailing space of the last mbuf for the fragment hdr */
1215 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1217 mlast->m_len += sizeof(struct ip6_frag);
1218 m->m_pkthdr.len += sizeof(struct ip6_frag);
1220 /* allocate a new mbuf for the fragment header */
1223 mfrg = m_get(M_NOWAIT, MT_DATA);
1226 mfrg->m_len = sizeof(struct ip6_frag);
1227 *frghdrp = mtod(mfrg, struct ip6_frag *);
1228 mlast->m_next = mfrg;
1235 * Calculates IPv6 path mtu for destination @dst.
1236 * Resulting MTU is stored in @mtup.
1238 * Returns 0 on success.
1241 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1243 struct nhop6_extended nh6;
1244 struct in6_addr kdst;
1250 in6_splitscope(dst, &kdst, &scopeid);
1251 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1252 return (EHOSTUNREACH);
1257 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL);
1258 fib6_free_nh_ext(fibnum, &nh6);
1264 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1265 * and cached data in @ro_pmtu.
1266 * MTU from (successful) route lookup is saved (along with dst)
1267 * inside @ro_pmtu to avoid subsequent route lookups after packet
1268 * filter processing.
1270 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1271 * Returns 0 on success.
1274 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1275 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1276 int *alwaysfragp, u_int fibnum)
1278 struct nhop6_basic nh6;
1279 struct in6_addr kdst;
1281 struct sockaddr_in6 *sa6_dst;
1288 * Here ro_pmtu has final destination address, while
1289 * ro might represent immediate destination.
1290 * Use ro_pmtu destination since mtu might differ.
1292 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1293 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1294 ro_pmtu->ro_mtu = 0;
1296 if (ro_pmtu->ro_mtu == 0) {
1297 bzero(sa6_dst, sizeof(*sa6_dst));
1298 sa6_dst->sin6_family = AF_INET6;
1299 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1300 sa6_dst->sin6_addr = *dst;
1302 in6_splitscope(dst, &kdst, &scopeid);
1303 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1305 ro_pmtu->ro_mtu = nh6.nh_mtu;
1308 mtu = ro_pmtu->ro_mtu;
1312 mtu = ro_pmtu->ro_rt->rt_mtu;
1314 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp));
1318 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1319 * hostcache data for @dst.
1320 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1322 * Returns 0 on success.
1325 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1326 u_long *mtup, int *alwaysfragp)
1334 struct in_conninfo inc;
1336 bzero(&inc, sizeof(inc));
1337 inc.inc_flags |= INC_ISIPV6;
1338 inc.inc6_faddr = *dst;
1340 ifmtu = IN6_LINKMTU(ifp);
1341 mtu = tcp_hc_getmtu(&inc);
1343 mtu = min(mtu, rt_mtu);
1348 else if (mtu < IPV6_MMTU) {
1350 * RFC2460 section 5, last paragraph:
1351 * if we record ICMPv6 too big message with
1352 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1353 * or smaller, with framgent header attached.
1354 * (fragment header is needed regardless from the
1355 * packet size, for translators to identify packets)
1361 mtu = IN6_LINKMTU(ifp);
1363 error = EHOSTUNREACH; /* XXX */
1367 *alwaysfragp = alwaysfrag;
1372 * IP6 socket option processing.
1375 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1377 int optdatalen, uproto;
1379 struct inpcb *in6p = sotoinpcb(so);
1381 int level, op, optname;
1385 uint32_t rss_bucket;
1389 level = sopt->sopt_level;
1390 op = sopt->sopt_dir;
1391 optname = sopt->sopt_name;
1392 optlen = sopt->sopt_valsize;
1396 uproto = (int)so->so_proto->pr_protocol;
1398 if (level != IPPROTO_IPV6) {
1401 if (sopt->sopt_level == SOL_SOCKET &&
1402 sopt->sopt_dir == SOPT_SET) {
1403 switch (sopt->sopt_name) {
1406 if ((so->so_options & SO_REUSEADDR) != 0)
1407 in6p->inp_flags2 |= INP_REUSEADDR;
1409 in6p->inp_flags2 &= ~INP_REUSEADDR;
1415 if ((so->so_options & SO_REUSEPORT) != 0)
1416 in6p->inp_flags2 |= INP_REUSEPORT;
1418 in6p->inp_flags2 &= ~INP_REUSEPORT;
1424 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1432 } else { /* level == IPPROTO_IPV6 */
1437 case IPV6_2292PKTOPTIONS:
1438 #ifdef IPV6_PKTOPTIONS
1439 case IPV6_PKTOPTIONS:
1444 error = soopt_getm(sopt, &m); /* XXX */
1447 error = soopt_mcopyin(sopt, m); /* XXX */
1450 error = ip6_pcbopts(&in6p->in6p_outputopts,
1452 m_freem(m); /* XXX */
1457 * Use of some Hop-by-Hop options or some
1458 * Destination options, might require special
1459 * privilege. That is, normal applications
1460 * (without special privilege) might be forbidden
1461 * from setting certain options in outgoing packets,
1462 * and might never see certain options in received
1463 * packets. [RFC 2292 Section 6]
1464 * KAME specific note:
1465 * KAME prevents non-privileged users from sending or
1466 * receiving ANY hbh/dst options in order to avoid
1467 * overhead of parsing options in the kernel.
1469 case IPV6_RECVHOPOPTS:
1470 case IPV6_RECVDSTOPTS:
1471 case IPV6_RECVRTHDRDSTOPTS:
1473 error = priv_check(td,
1474 PRIV_NETINET_SETHDROPTS);
1479 case IPV6_UNICAST_HOPS:
1482 case IPV6_RECVPKTINFO:
1483 case IPV6_RECVHOPLIMIT:
1484 case IPV6_RECVRTHDR:
1485 case IPV6_RECVPATHMTU:
1486 case IPV6_RECVTCLASS:
1487 case IPV6_RECVFLOWID:
1489 case IPV6_RECVRSSBUCKETID:
1492 case IPV6_AUTOFLOWLABEL:
1494 case IPV6_BINDMULTI:
1496 case IPV6_RSS_LISTEN_BUCKET:
1498 if (optname == IPV6_BINDANY && td != NULL) {
1499 error = priv_check(td,
1500 PRIV_NETINET_BINDANY);
1505 if (optlen != sizeof(int)) {
1509 error = sooptcopyin(sopt, &optval,
1510 sizeof optval, sizeof optval);
1515 case IPV6_UNICAST_HOPS:
1516 if (optval < -1 || optval >= 256)
1519 /* -1 = kernel default */
1520 in6p->in6p_hops = optval;
1521 if ((in6p->inp_vflag &
1523 in6p->inp_ip_ttl = optval;
1526 #define OPTSET(bit) \
1530 in6p->inp_flags |= (bit); \
1532 in6p->inp_flags &= ~(bit); \
1533 INP_WUNLOCK(in6p); \
1534 } while (/*CONSTCOND*/ 0)
1535 #define OPTSET2292(bit) \
1538 in6p->inp_flags |= IN6P_RFC2292; \
1540 in6p->inp_flags |= (bit); \
1542 in6p->inp_flags &= ~(bit); \
1543 INP_WUNLOCK(in6p); \
1544 } while (/*CONSTCOND*/ 0)
1545 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1547 #define OPTSET2(bit, val) do { \
1550 in6p->inp_flags2 |= bit; \
1552 in6p->inp_flags2 &= ~bit; \
1553 INP_WUNLOCK(in6p); \
1555 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1557 case IPV6_RECVPKTINFO:
1558 /* cannot mix with RFC2292 */
1559 if (OPTBIT(IN6P_RFC2292)) {
1563 OPTSET(IN6P_PKTINFO);
1568 struct ip6_pktopts **optp;
1570 /* cannot mix with RFC2292 */
1571 if (OPTBIT(IN6P_RFC2292)) {
1575 optp = &in6p->in6p_outputopts;
1576 error = ip6_pcbopt(IPV6_HOPLIMIT,
1577 (u_char *)&optval, sizeof(optval),
1578 optp, (td != NULL) ? td->td_ucred :
1583 case IPV6_RECVHOPLIMIT:
1584 /* cannot mix with RFC2292 */
1585 if (OPTBIT(IN6P_RFC2292)) {
1589 OPTSET(IN6P_HOPLIMIT);
1592 case IPV6_RECVHOPOPTS:
1593 /* cannot mix with RFC2292 */
1594 if (OPTBIT(IN6P_RFC2292)) {
1598 OPTSET(IN6P_HOPOPTS);
1601 case IPV6_RECVDSTOPTS:
1602 /* cannot mix with RFC2292 */
1603 if (OPTBIT(IN6P_RFC2292)) {
1607 OPTSET(IN6P_DSTOPTS);
1610 case IPV6_RECVRTHDRDSTOPTS:
1611 /* cannot mix with RFC2292 */
1612 if (OPTBIT(IN6P_RFC2292)) {
1616 OPTSET(IN6P_RTHDRDSTOPTS);
1619 case IPV6_RECVRTHDR:
1620 /* cannot mix with RFC2292 */
1621 if (OPTBIT(IN6P_RFC2292)) {
1628 case IPV6_RECVPATHMTU:
1630 * We ignore this option for TCP
1632 * (RFC3542 leaves this case
1635 if (uproto != IPPROTO_TCP)
1639 case IPV6_RECVFLOWID:
1640 OPTSET2(INP_RECVFLOWID, optval);
1644 case IPV6_RECVRSSBUCKETID:
1645 OPTSET2(INP_RECVRSSBUCKETID, optval);
1651 * make setsockopt(IPV6_V6ONLY)
1652 * available only prior to bind(2).
1653 * see ipng mailing list, Jun 22 2001.
1655 if (in6p->inp_lport ||
1656 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1660 OPTSET(IN6P_IPV6_V6ONLY);
1662 in6p->inp_vflag &= ~INP_IPV4;
1664 in6p->inp_vflag |= INP_IPV4;
1666 case IPV6_RECVTCLASS:
1667 /* cannot mix with RFC2292 XXX */
1668 if (OPTBIT(IN6P_RFC2292)) {
1672 OPTSET(IN6P_TCLASS);
1674 case IPV6_AUTOFLOWLABEL:
1675 OPTSET(IN6P_AUTOFLOWLABEL);
1679 OPTSET(INP_BINDANY);
1682 case IPV6_BINDMULTI:
1683 OPTSET2(INP_BINDMULTI, optval);
1686 case IPV6_RSS_LISTEN_BUCKET:
1687 if ((optval >= 0) &&
1688 (optval < rss_getnumbuckets())) {
1689 in6p->inp_rss_listen_bucket = optval;
1690 OPTSET2(INP_RSS_BUCKET_SET, 1);
1701 case IPV6_USE_MIN_MTU:
1702 case IPV6_PREFER_TEMPADDR:
1703 if (optlen != sizeof(optval)) {
1707 error = sooptcopyin(sopt, &optval,
1708 sizeof optval, sizeof optval);
1712 struct ip6_pktopts **optp;
1713 optp = &in6p->in6p_outputopts;
1714 error = ip6_pcbopt(optname,
1715 (u_char *)&optval, sizeof(optval),
1716 optp, (td != NULL) ? td->td_ucred :
1721 case IPV6_2292PKTINFO:
1722 case IPV6_2292HOPLIMIT:
1723 case IPV6_2292HOPOPTS:
1724 case IPV6_2292DSTOPTS:
1725 case IPV6_2292RTHDR:
1727 if (optlen != sizeof(int)) {
1731 error = sooptcopyin(sopt, &optval,
1732 sizeof optval, sizeof optval);
1736 case IPV6_2292PKTINFO:
1737 OPTSET2292(IN6P_PKTINFO);
1739 case IPV6_2292HOPLIMIT:
1740 OPTSET2292(IN6P_HOPLIMIT);
1742 case IPV6_2292HOPOPTS:
1744 * Check super-user privilege.
1745 * See comments for IPV6_RECVHOPOPTS.
1748 error = priv_check(td,
1749 PRIV_NETINET_SETHDROPTS);
1753 OPTSET2292(IN6P_HOPOPTS);
1755 case IPV6_2292DSTOPTS:
1757 error = priv_check(td,
1758 PRIV_NETINET_SETHDROPTS);
1762 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1764 case IPV6_2292RTHDR:
1765 OPTSET2292(IN6P_RTHDR);
1773 case IPV6_RTHDRDSTOPTS:
1776 /* new advanced API (RFC3542) */
1778 u_char optbuf_storage[MCLBYTES];
1780 struct ip6_pktopts **optp;
1782 /* cannot mix with RFC2292 */
1783 if (OPTBIT(IN6P_RFC2292)) {
1789 * We only ensure valsize is not too large
1790 * here. Further validation will be done
1793 error = sooptcopyin(sopt, optbuf_storage,
1794 sizeof(optbuf_storage), 0);
1797 optlen = sopt->sopt_valsize;
1798 optbuf = optbuf_storage;
1799 optp = &in6p->in6p_outputopts;
1800 error = ip6_pcbopt(optname, optbuf, optlen,
1801 optp, (td != NULL) ? td->td_ucred : NULL,
1807 case IPV6_MULTICAST_IF:
1808 case IPV6_MULTICAST_HOPS:
1809 case IPV6_MULTICAST_LOOP:
1810 case IPV6_JOIN_GROUP:
1811 case IPV6_LEAVE_GROUP:
1813 case MCAST_BLOCK_SOURCE:
1814 case MCAST_UNBLOCK_SOURCE:
1815 case MCAST_JOIN_GROUP:
1816 case MCAST_LEAVE_GROUP:
1817 case MCAST_JOIN_SOURCE_GROUP:
1818 case MCAST_LEAVE_SOURCE_GROUP:
1819 error = ip6_setmoptions(in6p, sopt);
1822 case IPV6_PORTRANGE:
1823 error = sooptcopyin(sopt, &optval,
1824 sizeof optval, sizeof optval);
1830 case IPV6_PORTRANGE_DEFAULT:
1831 in6p->inp_flags &= ~(INP_LOWPORT);
1832 in6p->inp_flags &= ~(INP_HIGHPORT);
1835 case IPV6_PORTRANGE_HIGH:
1836 in6p->inp_flags &= ~(INP_LOWPORT);
1837 in6p->inp_flags |= INP_HIGHPORT;
1840 case IPV6_PORTRANGE_LOW:
1841 in6p->inp_flags &= ~(INP_HIGHPORT);
1842 in6p->inp_flags |= INP_LOWPORT;
1853 case IPV6_IPSEC_POLICY:
1858 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1860 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1862 req = mtod(m, caddr_t);
1863 error = ipsec_set_policy(in6p, optname, req,
1864 m->m_len, (sopt->sopt_td != NULL) ?
1865 sopt->sopt_td->td_ucred : NULL);
1872 error = ENOPROTOOPT;
1880 case IPV6_2292PKTOPTIONS:
1881 #ifdef IPV6_PKTOPTIONS
1882 case IPV6_PKTOPTIONS:
1885 * RFC3542 (effectively) deprecated the
1886 * semantics of the 2292-style pktoptions.
1887 * Since it was not reliable in nature (i.e.,
1888 * applications had to expect the lack of some
1889 * information after all), it would make sense
1890 * to simplify this part by always returning
1893 sopt->sopt_valsize = 0;
1896 case IPV6_RECVHOPOPTS:
1897 case IPV6_RECVDSTOPTS:
1898 case IPV6_RECVRTHDRDSTOPTS:
1899 case IPV6_UNICAST_HOPS:
1900 case IPV6_RECVPKTINFO:
1901 case IPV6_RECVHOPLIMIT:
1902 case IPV6_RECVRTHDR:
1903 case IPV6_RECVPATHMTU:
1906 case IPV6_PORTRANGE:
1907 case IPV6_RECVTCLASS:
1908 case IPV6_AUTOFLOWLABEL:
1912 case IPV6_RECVFLOWID:
1914 case IPV6_RSSBUCKETID:
1915 case IPV6_RECVRSSBUCKETID:
1917 case IPV6_BINDMULTI:
1920 case IPV6_RECVHOPOPTS:
1921 optval = OPTBIT(IN6P_HOPOPTS);
1924 case IPV6_RECVDSTOPTS:
1925 optval = OPTBIT(IN6P_DSTOPTS);
1928 case IPV6_RECVRTHDRDSTOPTS:
1929 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1932 case IPV6_UNICAST_HOPS:
1933 optval = in6p->in6p_hops;
1936 case IPV6_RECVPKTINFO:
1937 optval = OPTBIT(IN6P_PKTINFO);
1940 case IPV6_RECVHOPLIMIT:
1941 optval = OPTBIT(IN6P_HOPLIMIT);
1944 case IPV6_RECVRTHDR:
1945 optval = OPTBIT(IN6P_RTHDR);
1948 case IPV6_RECVPATHMTU:
1949 optval = OPTBIT(IN6P_MTU);
1953 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1956 case IPV6_PORTRANGE:
1959 flags = in6p->inp_flags;
1960 if (flags & INP_HIGHPORT)
1961 optval = IPV6_PORTRANGE_HIGH;
1962 else if (flags & INP_LOWPORT)
1963 optval = IPV6_PORTRANGE_LOW;
1968 case IPV6_RECVTCLASS:
1969 optval = OPTBIT(IN6P_TCLASS);
1972 case IPV6_AUTOFLOWLABEL:
1973 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1977 optval = OPTBIT(INP_BINDANY);
1981 optval = in6p->inp_flowid;
1985 optval = in6p->inp_flowtype;
1988 case IPV6_RECVFLOWID:
1989 optval = OPTBIT2(INP_RECVFLOWID);
1992 case IPV6_RSSBUCKETID:
1994 rss_hash2bucket(in6p->inp_flowid,
1998 optval = rss_bucket;
2003 case IPV6_RECVRSSBUCKETID:
2004 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2008 case IPV6_BINDMULTI:
2009 optval = OPTBIT2(INP_BINDMULTI);
2015 error = sooptcopyout(sopt, &optval,
2022 struct ip6_mtuinfo mtuinfo;
2024 if (!(so->so_state & SS_ISCONNECTED))
2027 * XXX: we dot not consider the case of source
2028 * routing, or optional information to specify
2029 * the outgoing interface.
2031 error = ip6_getpmtu_ctl(so->so_fibnum,
2032 &in6p->in6p_faddr, &pmtu);
2035 if (pmtu > IPV6_MAXPACKET)
2036 pmtu = IPV6_MAXPACKET;
2038 bzero(&mtuinfo, sizeof(mtuinfo));
2039 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2040 optdata = (void *)&mtuinfo;
2041 optdatalen = sizeof(mtuinfo);
2042 error = sooptcopyout(sopt, optdata,
2047 case IPV6_2292PKTINFO:
2048 case IPV6_2292HOPLIMIT:
2049 case IPV6_2292HOPOPTS:
2050 case IPV6_2292RTHDR:
2051 case IPV6_2292DSTOPTS:
2053 case IPV6_2292PKTINFO:
2054 optval = OPTBIT(IN6P_PKTINFO);
2056 case IPV6_2292HOPLIMIT:
2057 optval = OPTBIT(IN6P_HOPLIMIT);
2059 case IPV6_2292HOPOPTS:
2060 optval = OPTBIT(IN6P_HOPOPTS);
2062 case IPV6_2292RTHDR:
2063 optval = OPTBIT(IN6P_RTHDR);
2065 case IPV6_2292DSTOPTS:
2066 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2069 error = sooptcopyout(sopt, &optval,
2076 case IPV6_RTHDRDSTOPTS:
2080 case IPV6_USE_MIN_MTU:
2081 case IPV6_PREFER_TEMPADDR:
2082 error = ip6_getpcbopt(in6p->in6p_outputopts,
2086 case IPV6_MULTICAST_IF:
2087 case IPV6_MULTICAST_HOPS:
2088 case IPV6_MULTICAST_LOOP:
2090 error = ip6_getmoptions(in6p, sopt);
2094 case IPV6_IPSEC_POLICY:
2098 struct mbuf *m = NULL;
2099 struct mbuf **mp = &m;
2100 size_t ovalsize = sopt->sopt_valsize;
2101 caddr_t oval = (caddr_t)sopt->sopt_val;
2103 error = soopt_getm(sopt, &m); /* XXX */
2106 error = soopt_mcopyin(sopt, m); /* XXX */
2109 sopt->sopt_valsize = ovalsize;
2110 sopt->sopt_val = oval;
2112 req = mtod(m, caddr_t);
2115 error = ipsec_get_policy(in6p, req, len, mp);
2117 error = soopt_mcopyout(sopt, m); /* XXX */
2118 if (error == 0 && m)
2125 error = ENOPROTOOPT;
2135 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2137 int error = 0, optval, optlen;
2138 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2139 struct inpcb *in6p = sotoinpcb(so);
2140 int level, op, optname;
2142 level = sopt->sopt_level;
2143 op = sopt->sopt_dir;
2144 optname = sopt->sopt_name;
2145 optlen = sopt->sopt_valsize;
2147 if (level != IPPROTO_IPV6) {
2154 * For ICMPv6 sockets, no modification allowed for checksum
2155 * offset, permit "no change" values to help existing apps.
2157 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2158 * for an ICMPv6 socket will fail."
2159 * The current behavior does not meet RFC3542.
2163 if (optlen != sizeof(int)) {
2167 error = sooptcopyin(sopt, &optval, sizeof(optval),
2171 if ((optval % 2) != 0) {
2172 /* the API assumes even offset values */
2174 } else if (so->so_proto->pr_protocol ==
2176 if (optval != icmp6off)
2179 in6p->in6p_cksum = optval;
2183 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2186 optval = in6p->in6p_cksum;
2188 error = sooptcopyout(sopt, &optval, sizeof(optval));
2198 error = ENOPROTOOPT;
2206 * Set up IP6 options in pcb for insertion in output packets or
2207 * specifying behavior of outgoing packets.
2210 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2211 struct socket *so, struct sockopt *sopt)
2213 struct ip6_pktopts *opt = *pktopt;
2215 struct thread *td = sopt->sopt_td;
2217 /* turn off any old options. */
2220 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2221 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2222 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2223 printf("ip6_pcbopts: all specified options are cleared.\n");
2225 ip6_clearpktopts(opt, -1);
2227 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2230 if (!m || m->m_len == 0) {
2232 * Only turning off any previous options, regardless of
2233 * whether the opt is just created or given.
2235 free(opt, M_IP6OPT);
2239 /* set options specified by user. */
2240 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2241 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2242 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2243 free(opt, M_IP6OPT);
2251 * initialize ip6_pktopts. beware that there are non-zero default values in
2255 ip6_initpktopts(struct ip6_pktopts *opt)
2258 bzero(opt, sizeof(*opt));
2259 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2260 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2261 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2262 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2266 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2267 struct ucred *cred, int uproto)
2269 struct ip6_pktopts *opt;
2271 if (*pktopt == NULL) {
2272 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2274 ip6_initpktopts(*pktopt);
2278 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2282 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2284 void *optdata = NULL;
2286 struct ip6_ext *ip6e;
2288 struct in6_pktinfo null_pktinfo;
2289 int deftclass = 0, on;
2290 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2291 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2295 optdata = (void *)&null_pktinfo;
2296 if (pktopt && pktopt->ip6po_pktinfo) {
2297 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2298 sizeof(null_pktinfo));
2299 in6_clearscope(&null_pktinfo.ipi6_addr);
2301 /* XXX: we don't have to do this every time... */
2302 bzero(&null_pktinfo, sizeof(null_pktinfo));
2304 optdatalen = sizeof(struct in6_pktinfo);
2307 if (pktopt && pktopt->ip6po_tclass >= 0)
2308 optdata = (void *)&pktopt->ip6po_tclass;
2310 optdata = (void *)&deftclass;
2311 optdatalen = sizeof(int);
2314 if (pktopt && pktopt->ip6po_hbh) {
2315 optdata = (void *)pktopt->ip6po_hbh;
2316 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2317 optdatalen = (ip6e->ip6e_len + 1) << 3;
2321 if (pktopt && pktopt->ip6po_rthdr) {
2322 optdata = (void *)pktopt->ip6po_rthdr;
2323 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2324 optdatalen = (ip6e->ip6e_len + 1) << 3;
2327 case IPV6_RTHDRDSTOPTS:
2328 if (pktopt && pktopt->ip6po_dest1) {
2329 optdata = (void *)pktopt->ip6po_dest1;
2330 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2331 optdatalen = (ip6e->ip6e_len + 1) << 3;
2335 if (pktopt && pktopt->ip6po_dest2) {
2336 optdata = (void *)pktopt->ip6po_dest2;
2337 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2338 optdatalen = (ip6e->ip6e_len + 1) << 3;
2342 if (pktopt && pktopt->ip6po_nexthop) {
2343 optdata = (void *)pktopt->ip6po_nexthop;
2344 optdatalen = pktopt->ip6po_nexthop->sa_len;
2347 case IPV6_USE_MIN_MTU:
2349 optdata = (void *)&pktopt->ip6po_minmtu;
2351 optdata = (void *)&defminmtu;
2352 optdatalen = sizeof(int);
2355 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2359 optdata = (void *)&on;
2360 optdatalen = sizeof(on);
2362 case IPV6_PREFER_TEMPADDR:
2364 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2366 optdata = (void *)&defpreftemp;
2367 optdatalen = sizeof(int);
2369 default: /* should not happen */
2371 panic("ip6_getpcbopt: unexpected option\n");
2373 return (ENOPROTOOPT);
2376 error = sooptcopyout(sopt, optdata, optdatalen);
2382 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2387 if (optname == -1 || optname == IPV6_PKTINFO) {
2388 if (pktopt->ip6po_pktinfo)
2389 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2390 pktopt->ip6po_pktinfo = NULL;
2392 if (optname == -1 || optname == IPV6_HOPLIMIT)
2393 pktopt->ip6po_hlim = -1;
2394 if (optname == -1 || optname == IPV6_TCLASS)
2395 pktopt->ip6po_tclass = -1;
2396 if (optname == -1 || optname == IPV6_NEXTHOP) {
2397 if (pktopt->ip6po_nextroute.ro_rt) {
2398 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2399 pktopt->ip6po_nextroute.ro_rt = NULL;
2401 if (pktopt->ip6po_nexthop)
2402 free(pktopt->ip6po_nexthop, M_IP6OPT);
2403 pktopt->ip6po_nexthop = NULL;
2405 if (optname == -1 || optname == IPV6_HOPOPTS) {
2406 if (pktopt->ip6po_hbh)
2407 free(pktopt->ip6po_hbh, M_IP6OPT);
2408 pktopt->ip6po_hbh = NULL;
2410 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2411 if (pktopt->ip6po_dest1)
2412 free(pktopt->ip6po_dest1, M_IP6OPT);
2413 pktopt->ip6po_dest1 = NULL;
2415 if (optname == -1 || optname == IPV6_RTHDR) {
2416 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2417 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2418 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2419 if (pktopt->ip6po_route.ro_rt) {
2420 RTFREE(pktopt->ip6po_route.ro_rt);
2421 pktopt->ip6po_route.ro_rt = NULL;
2424 if (optname == -1 || optname == IPV6_DSTOPTS) {
2425 if (pktopt->ip6po_dest2)
2426 free(pktopt->ip6po_dest2, M_IP6OPT);
2427 pktopt->ip6po_dest2 = NULL;
2431 #define PKTOPT_EXTHDRCPY(type) \
2434 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2435 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2436 if (dst->type == NULL && canwait == M_NOWAIT)\
2438 bcopy(src->type, dst->type, hlen);\
2440 } while (/*CONSTCOND*/ 0)
2443 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2445 if (dst == NULL || src == NULL) {
2446 printf("ip6_clearpktopts: invalid argument\n");
2450 dst->ip6po_hlim = src->ip6po_hlim;
2451 dst->ip6po_tclass = src->ip6po_tclass;
2452 dst->ip6po_flags = src->ip6po_flags;
2453 dst->ip6po_minmtu = src->ip6po_minmtu;
2454 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2455 if (src->ip6po_pktinfo) {
2456 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2458 if (dst->ip6po_pktinfo == NULL)
2460 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2462 if (src->ip6po_nexthop) {
2463 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2465 if (dst->ip6po_nexthop == NULL)
2467 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2468 src->ip6po_nexthop->sa_len);
2470 PKTOPT_EXTHDRCPY(ip6po_hbh);
2471 PKTOPT_EXTHDRCPY(ip6po_dest1);
2472 PKTOPT_EXTHDRCPY(ip6po_dest2);
2473 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2477 ip6_clearpktopts(dst, -1);
2480 #undef PKTOPT_EXTHDRCPY
2482 struct ip6_pktopts *
2483 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2486 struct ip6_pktopts *dst;
2488 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2491 ip6_initpktopts(dst);
2493 if ((error = copypktopts(dst, src, canwait)) != 0) {
2494 free(dst, M_IP6OPT);
2502 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2507 ip6_clearpktopts(pktopt, -1);
2509 free(pktopt, M_IP6OPT);
2513 * Set IPv6 outgoing packet options based on advanced API.
2516 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2517 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2519 struct cmsghdr *cm = NULL;
2521 if (control == NULL || opt == NULL)
2524 ip6_initpktopts(opt);
2529 * If stickyopt is provided, make a local copy of the options
2530 * for this particular packet, then override them by ancillary
2532 * XXX: copypktopts() does not copy the cached route to a next
2533 * hop (if any). This is not very good in terms of efficiency,
2534 * but we can allow this since this option should be rarely
2537 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2542 * XXX: Currently, we assume all the optional information is stored
2545 if (control->m_next)
2548 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2549 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2552 if (control->m_len < CMSG_LEN(0))
2555 cm = mtod(control, struct cmsghdr *);
2556 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2558 if (cm->cmsg_level != IPPROTO_IPV6)
2561 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2562 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2571 * Set a particular packet option, as a sticky option or an ancillary data
2572 * item. "len" can be 0 only when it's a sticky option.
2573 * We have 4 cases of combination of "sticky" and "cmsg":
2574 * "sticky=0, cmsg=0": impossible
2575 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2576 * "sticky=1, cmsg=0": RFC3542 socket option
2577 * "sticky=1, cmsg=1": RFC2292 socket option
2580 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2581 struct ucred *cred, int sticky, int cmsg, int uproto)
2583 int minmtupolicy, preftemp;
2586 if (!sticky && !cmsg) {
2588 printf("ip6_setpktopt: impossible case\n");
2594 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2595 * not be specified in the context of RFC3542. Conversely,
2596 * RFC3542 types should not be specified in the context of RFC2292.
2600 case IPV6_2292PKTINFO:
2601 case IPV6_2292HOPLIMIT:
2602 case IPV6_2292NEXTHOP:
2603 case IPV6_2292HOPOPTS:
2604 case IPV6_2292DSTOPTS:
2605 case IPV6_2292RTHDR:
2606 case IPV6_2292PKTOPTIONS:
2607 return (ENOPROTOOPT);
2610 if (sticky && cmsg) {
2617 case IPV6_RTHDRDSTOPTS:
2619 case IPV6_USE_MIN_MTU:
2622 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2623 return (ENOPROTOOPT);
2628 case IPV6_2292PKTINFO:
2631 struct ifnet *ifp = NULL;
2632 struct in6_pktinfo *pktinfo;
2634 if (len != sizeof(struct in6_pktinfo))
2637 pktinfo = (struct in6_pktinfo *)buf;
2640 * An application can clear any sticky IPV6_PKTINFO option by
2641 * doing a "regular" setsockopt with ipi6_addr being
2642 * in6addr_any and ipi6_ifindex being zero.
2643 * [RFC 3542, Section 6]
2645 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2646 pktinfo->ipi6_ifindex == 0 &&
2647 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2648 ip6_clearpktopts(opt, optname);
2652 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2653 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2656 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2658 /* validate the interface index if specified. */
2659 if (pktinfo->ipi6_ifindex > V_if_index)
2661 if (pktinfo->ipi6_ifindex) {
2662 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2666 if (ifp != NULL && (
2667 ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED))
2671 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2672 struct in6_ifaddr *ia;
2674 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2675 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2677 return (EADDRNOTAVAIL);
2678 ifa_free(&ia->ia_ifa);
2681 * We store the address anyway, and let in6_selectsrc()
2682 * validate the specified address. This is because ipi6_addr
2683 * may not have enough information about its scope zone, and
2684 * we may need additional information (such as outgoing
2685 * interface or the scope zone of a destination address) to
2686 * disambiguate the scope.
2687 * XXX: the delay of the validation may confuse the
2688 * application when it is used as a sticky option.
2690 if (opt->ip6po_pktinfo == NULL) {
2691 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2692 M_IP6OPT, M_NOWAIT);
2693 if (opt->ip6po_pktinfo == NULL)
2696 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2700 case IPV6_2292HOPLIMIT:
2706 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2707 * to simplify the ordering among hoplimit options.
2709 if (optname == IPV6_HOPLIMIT && sticky)
2710 return (ENOPROTOOPT);
2712 if (len != sizeof(int))
2715 if (*hlimp < -1 || *hlimp > 255)
2718 opt->ip6po_hlim = *hlimp;
2726 if (len != sizeof(int))
2728 tclass = *(int *)buf;
2729 if (tclass < -1 || tclass > 255)
2732 opt->ip6po_tclass = tclass;
2736 case IPV6_2292NEXTHOP:
2739 error = priv_check_cred(cred,
2740 PRIV_NETINET_SETHDROPTS, 0);
2745 if (len == 0) { /* just remove the option */
2746 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2750 /* check if cmsg_len is large enough for sa_len */
2751 if (len < sizeof(struct sockaddr) || len < *buf)
2754 switch (((struct sockaddr *)buf)->sa_family) {
2757 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2760 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2763 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2764 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2767 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2773 case AF_LINK: /* should eventually be supported */
2775 return (EAFNOSUPPORT);
2778 /* turn off the previous option, then set the new option. */
2779 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2780 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2781 if (opt->ip6po_nexthop == NULL)
2783 bcopy(buf, opt->ip6po_nexthop, *buf);
2786 case IPV6_2292HOPOPTS:
2789 struct ip6_hbh *hbh;
2793 * XXX: We don't allow a non-privileged user to set ANY HbH
2794 * options, since per-option restriction has too much
2798 error = priv_check_cred(cred,
2799 PRIV_NETINET_SETHDROPTS, 0);
2805 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2806 break; /* just remove the option */
2809 /* message length validation */
2810 if (len < sizeof(struct ip6_hbh))
2812 hbh = (struct ip6_hbh *)buf;
2813 hbhlen = (hbh->ip6h_len + 1) << 3;
2817 /* turn off the previous option, then set the new option. */
2818 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2819 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2820 if (opt->ip6po_hbh == NULL)
2822 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2827 case IPV6_2292DSTOPTS:
2829 case IPV6_RTHDRDSTOPTS:
2831 struct ip6_dest *dest, **newdest = NULL;
2834 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2835 error = priv_check_cred(cred,
2836 PRIV_NETINET_SETHDROPTS, 0);
2842 ip6_clearpktopts(opt, optname);
2843 break; /* just remove the option */
2846 /* message length validation */
2847 if (len < sizeof(struct ip6_dest))
2849 dest = (struct ip6_dest *)buf;
2850 destlen = (dest->ip6d_len + 1) << 3;
2855 * Determine the position that the destination options header
2856 * should be inserted; before or after the routing header.
2859 case IPV6_2292DSTOPTS:
2861 * The old advacned API is ambiguous on this point.
2862 * Our approach is to determine the position based
2863 * according to the existence of a routing header.
2864 * Note, however, that this depends on the order of the
2865 * extension headers in the ancillary data; the 1st
2866 * part of the destination options header must appear
2867 * before the routing header in the ancillary data,
2869 * RFC3542 solved the ambiguity by introducing
2870 * separate ancillary data or option types.
2872 if (opt->ip6po_rthdr == NULL)
2873 newdest = &opt->ip6po_dest1;
2875 newdest = &opt->ip6po_dest2;
2877 case IPV6_RTHDRDSTOPTS:
2878 newdest = &opt->ip6po_dest1;
2881 newdest = &opt->ip6po_dest2;
2885 /* turn off the previous option, then set the new option. */
2886 ip6_clearpktopts(opt, optname);
2887 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2888 if (*newdest == NULL)
2890 bcopy(dest, *newdest, destlen);
2895 case IPV6_2292RTHDR:
2898 struct ip6_rthdr *rth;
2902 ip6_clearpktopts(opt, IPV6_RTHDR);
2903 break; /* just remove the option */
2906 /* message length validation */
2907 if (len < sizeof(struct ip6_rthdr))
2909 rth = (struct ip6_rthdr *)buf;
2910 rthlen = (rth->ip6r_len + 1) << 3;
2914 switch (rth->ip6r_type) {
2915 case IPV6_RTHDR_TYPE_0:
2916 if (rth->ip6r_len == 0) /* must contain one addr */
2918 if (rth->ip6r_len % 2) /* length must be even */
2920 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2924 return (EINVAL); /* not supported */
2927 /* turn off the previous option */
2928 ip6_clearpktopts(opt, IPV6_RTHDR);
2929 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2930 if (opt->ip6po_rthdr == NULL)
2932 bcopy(rth, opt->ip6po_rthdr, rthlen);
2937 case IPV6_USE_MIN_MTU:
2938 if (len != sizeof(int))
2940 minmtupolicy = *(int *)buf;
2941 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2942 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2943 minmtupolicy != IP6PO_MINMTU_ALL) {
2946 opt->ip6po_minmtu = minmtupolicy;
2950 if (len != sizeof(int))
2953 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2955 * we ignore this option for TCP sockets.
2956 * (RFC3542 leaves this case unspecified.)
2958 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2960 opt->ip6po_flags |= IP6PO_DONTFRAG;
2963 case IPV6_PREFER_TEMPADDR:
2964 if (len != sizeof(int))
2966 preftemp = *(int *)buf;
2967 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2968 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2969 preftemp != IP6PO_TEMPADDR_PREFER) {
2972 opt->ip6po_prefer_tempaddr = preftemp;
2976 return (ENOPROTOOPT);
2977 } /* end of switch */
2983 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2984 * packet to the input queue of a specified interface. Note that this
2985 * calls the output routine of the loopback "driver", but with an interface
2986 * pointer that might NOT be &loif -- easier than replicating that code here.
2989 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
2992 struct ip6_hdr *ip6;
2994 copym = m_copy(m, 0, M_COPYALL);
2999 * Make sure to deep-copy IPv6 header portion in case the data
3000 * is in an mbuf cluster, so that we can safely override the IPv6
3001 * header portion later.
3003 if (!M_WRITABLE(copym) ||
3004 copym->m_len < sizeof(struct ip6_hdr)) {
3005 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3009 ip6 = mtod(copym, struct ip6_hdr *);
3011 * clear embedded scope identifiers if necessary.
3012 * in6_clearscope will touch the addresses only when necessary.
3014 in6_clearscope(&ip6->ip6_src);
3015 in6_clearscope(&ip6->ip6_dst);
3016 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3017 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3019 copym->m_pkthdr.csum_data = 0xffff;
3021 if_simloop(ifp, copym, AF_INET6, 0);
3025 * Chop IPv6 header off from the payload.
3028 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3031 struct ip6_hdr *ip6;
3033 ip6 = mtod(m, struct ip6_hdr *);
3034 if (m->m_len > sizeof(*ip6)) {
3035 mh = m_gethdr(M_NOWAIT, MT_DATA);
3040 m_move_pkthdr(mh, m);
3041 M_ALIGN(mh, sizeof(*ip6));
3042 m->m_len -= sizeof(*ip6);
3043 m->m_data += sizeof(*ip6);
3046 m->m_len = sizeof(*ip6);
3047 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3049 exthdrs->ip6e_ip6 = m;
3054 * Compute IPv6 extension header length.
3057 ip6_optlen(struct inpcb *in6p)
3061 if (!in6p->in6p_outputopts)
3066 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3068 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3069 if (in6p->in6p_outputopts->ip6po_rthdr)
3070 /* dest1 is valid with rthdr only */
3071 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3072 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3073 len += elen(in6p->in6p_outputopts->ip6po_dest2);