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 MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
140 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
141 struct ucred *, int);
142 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
143 struct socket *, struct sockopt *);
144 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
145 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
146 struct ucred *, int, int, int);
148 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
149 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
151 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
152 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
153 static int ip6_getpmtu(struct route_in6 *, int,
154 struct ifnet *, const struct in6_addr *, u_long *, int *, u_int);
155 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
157 static int ip6_getpmtu_ctl(u_int, const struct in6_addr *, u_long *);
158 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
162 * Make an extension header from option data. hp is the source, and
163 * mp is the destination.
165 #define MAKE_EXTHDR(hp, mp) \
168 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
169 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
170 ((eh)->ip6e_len + 1) << 3); \
174 } while (/*CONSTCOND*/ 0)
177 * Form a chain of extension headers.
178 * m is the extension header mbuf
179 * mp is the previous mbuf in the chain
180 * p is the next header
181 * i is the type of option.
183 #define MAKE_CHAIN(m, mp, p, i)\
187 panic("assumption failed: hdr not split"); \
188 *mtod((m), u_char *) = *(p);\
190 p = mtod((m), u_char *);\
191 (m)->m_next = (mp)->m_next;\
195 } while (/*CONSTCOND*/ 0)
198 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
202 csum = in_cksum_skip(m, offset + plen, offset);
203 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
205 offset += m->m_pkthdr.csum_data; /* checksum offset */
207 if (offset + sizeof(u_short) > m->m_len) {
208 printf("%s: delayed m_pullup, m->len: %d plen %u off %u "
209 "csum_flags=%b\n", __func__, m->m_len, plen, offset,
210 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
212 * XXX this should not happen, but if it does, the correct
213 * behavior may be to insert the checksum in the appropriate
214 * next mbuf in the chain.
218 *(u_short *)(m->m_data + offset) = csum;
222 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
223 int mtu, uint32_t id)
225 struct mbuf *m, **mnext, *m_frgpart;
226 struct ip6_hdr *ip6, *mhip6;
227 struct ip6_frag *ip6f;
230 int tlen = m0->m_pkthdr.len;
233 ip6 = mtod(m, struct ip6_hdr *);
234 mnext = &m->m_nextpkt;
236 for (off = hlen; off < tlen; off += mtu) {
237 m = m_gethdr(M_NOWAIT, MT_DATA);
239 IP6STAT_INC(ip6s_odropped);
242 m->m_flags = m0->m_flags & M_COPYFLAGS;
244 mnext = &m->m_nextpkt;
245 m->m_data += max_linkhdr;
246 mhip6 = mtod(m, struct ip6_hdr *);
248 m->m_len = sizeof(*mhip6);
249 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
251 IP6STAT_INC(ip6s_odropped);
254 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
255 if (off + mtu >= tlen)
258 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
259 mhip6->ip6_plen = htons((u_short)(mtu + hlen +
260 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
261 if ((m_frgpart = m_copy(m0, off, mtu)) == NULL) {
262 IP6STAT_INC(ip6s_odropped);
266 m->m_pkthdr.len = mtu + hlen + sizeof(*ip6f);
267 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
268 m->m_pkthdr.rcvif = NULL;
269 ip6f->ip6f_reserved = 0;
270 ip6f->ip6f_ident = id;
271 ip6f->ip6f_nxt = nextproto;
272 IP6STAT_INC(ip6s_ofragments);
273 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
280 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
281 * header (with pri, len, nxt, hlim, src, dst).
282 * This function may modify ver and hlim only.
283 * The mbuf chain containing the packet will be freed.
284 * The mbuf opt, if present, will not be freed.
285 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
286 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
287 * then result of route lookup is stored in ro->ro_rt.
289 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
290 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
293 * ifpp - XXX: just for statistics
296 * XXX TODO: no flowid is assigned for outbound flows?
299 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
300 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
301 struct ifnet **ifpp, struct inpcb *inp)
304 struct ifnet *ifp, *origifp;
306 struct mbuf *mprev = NULL;
308 struct route_in6 ip6route;
309 struct rtentry *rt = NULL;
310 struct sockaddr_in6 *dst, src_sa, dst_sa;
311 struct in6_addr odst;
313 struct in6_ifaddr *ia = NULL;
315 int alwaysfrag, dontfrag;
316 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
317 struct ip6_exthdrs exthdrs;
318 struct in6_addr src0, dst0;
320 struct route_in6 *ro_pmtu = NULL;
325 struct m_tag *fwd_tag = NULL;
329 M_SETFIB(m, inp->inp_inc.inc_fibnum);
330 if ((flags & IP_NODEFAULTFLOWID) == 0) {
331 /* unconditionally set flowid */
332 m->m_pkthdr.flowid = inp->inp_flowid;
333 M_HASHTYPE_SET(m, inp->inp_flowtype);
337 bzero(&exthdrs, sizeof(exthdrs));
339 /* Hop-by-Hop options header */
340 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
341 /* Destination options header(1st part) */
342 if (opt->ip6po_rthdr) {
344 * Destination options header(1st part)
345 * This only makes sense with a routing header.
346 * See Section 9.2 of RFC 3542.
347 * Disabling this part just for MIP6 convenience is
348 * a bad idea. We need to think carefully about a
349 * way to make the advanced API coexist with MIP6
350 * options, which might automatically be inserted in
353 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
356 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
357 /* Destination options header(2nd part) */
358 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
363 * IPSec checking which handles several cases.
364 * FAST IPSEC: We re-injected the packet.
365 * XXX: need scope argument.
367 switch(ip6_ipsec_output(&m, inp, &error))
369 case 1: /* Bad packet */
371 case -1: /* IPSec done */
373 case 0: /* No IPSec */
380 * Calculate the total length of the extension header chain.
381 * Keep the length of the unfragmentable part for fragmentation.
384 if (exthdrs.ip6e_hbh)
385 optlen += exthdrs.ip6e_hbh->m_len;
386 if (exthdrs.ip6e_dest1)
387 optlen += exthdrs.ip6e_dest1->m_len;
388 if (exthdrs.ip6e_rthdr)
389 optlen += exthdrs.ip6e_rthdr->m_len;
390 unfragpartlen = optlen + sizeof(struct ip6_hdr);
392 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
393 if (exthdrs.ip6e_dest2)
394 optlen += exthdrs.ip6e_dest2->m_len;
397 * If there is at least one extension header,
398 * separate IP6 header from the payload.
400 if (optlen && !hdrsplit) {
401 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
405 m = exthdrs.ip6e_ip6;
409 ip6 = mtod(m, struct ip6_hdr *);
411 /* adjust mbuf packet header length */
412 m->m_pkthdr.len += optlen;
413 plen = m->m_pkthdr.len - sizeof(*ip6);
415 /* If this is a jumbo payload, insert a jumbo payload option. */
416 if (plen > IPV6_MAXPACKET) {
418 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
422 m = exthdrs.ip6e_ip6;
426 ip6 = mtod(m, struct ip6_hdr *);
427 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
431 ip6->ip6_plen = htons(plen);
434 * Concatenate headers and fill in next header fields.
435 * Here we have, on "m"
437 * and we insert headers accordingly. Finally, we should be getting:
438 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
440 * during the header composing process, "m" points to IPv6 header.
441 * "mprev" points to an extension header prior to esp.
443 u_char *nexthdrp = &ip6->ip6_nxt;
447 * we treat dest2 specially. this makes IPsec processing
448 * much easier. the goal here is to make mprev point the
449 * mbuf prior to dest2.
451 * result: IPv6 dest2 payload
452 * m and mprev will point to IPv6 header.
454 if (exthdrs.ip6e_dest2) {
456 panic("assumption failed: hdr not split");
457 exthdrs.ip6e_dest2->m_next = m->m_next;
458 m->m_next = exthdrs.ip6e_dest2;
459 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
460 ip6->ip6_nxt = IPPROTO_DSTOPTS;
464 * result: IPv6 hbh dest1 rthdr dest2 payload
465 * m will point to IPv6 header. mprev will point to the
466 * extension header prior to dest2 (rthdr in the above case).
468 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
469 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
471 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
475 * If there is a routing header, discard the packet.
477 if (exthdrs.ip6e_rthdr) {
482 /* Source address validation */
483 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
484 (flags & IPV6_UNSPECSRC) == 0) {
486 IP6STAT_INC(ip6s_badscope);
489 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
491 IP6STAT_INC(ip6s_badscope);
495 IP6STAT_INC(ip6s_localout);
502 bzero((caddr_t)ro, sizeof(*ro));
504 ro->ro_flags |= RT_LLE_CACHE;
506 if (opt && opt->ip6po_rthdr)
507 ro = &opt->ip6po_route;
508 dst = (struct sockaddr_in6 *)&ro->ro_dst;
510 if (ro->ro_rt == NULL)
511 (void )flowtable_lookup(AF_INET6, m, (struct route *)ro);
513 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
516 * if specified, try to fill in the traffic class field.
517 * do not override if a non-zero value is already set.
518 * we check the diffserv field and the ecn field separately.
520 if (opt && opt->ip6po_tclass >= 0) {
523 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
525 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
528 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
531 /* fill in or override the hop limit field, if necessary. */
532 if (opt && opt->ip6po_hlim != -1)
533 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
534 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
536 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
538 ip6->ip6_hlim = V_ip6_defmcasthlim;
541 * Validate route against routing table additions;
542 * a better/more specific route might have been added.
543 * Make sure address family is set in route.
546 ro->ro_dst.sin6_family = AF_INET6;
547 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
549 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
550 ro->ro_dst.sin6_family == AF_INET6 &&
551 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
553 ifp = ro->ro_rt->rt_ifp;
555 if (fwd_tag == NULL) {
556 bzero(&dst_sa, sizeof(dst_sa));
557 dst_sa.sin6_family = AF_INET6;
558 dst_sa.sin6_len = sizeof(dst_sa);
559 dst_sa.sin6_addr = ip6->ip6_dst;
561 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
565 in6_ifstat_inc(ifp, ifs6_out_discard);
571 * If in6_selectroute() does not return a route entry,
572 * dst may not have been updated.
574 *dst = dst_sa; /* XXX */
578 * then rt (for unicast) and ifp must be non-NULL valid values.
580 if ((flags & IPV6_FORWARDING) == 0) {
581 /* XXX: the FORWARDING flag can be set for mrouting. */
582 in6_ifstat_inc(ifp, ifs6_out_request);
585 ia = (struct in6_ifaddr *)(rt->rt_ifa);
586 counter_u64_add(rt->rt_pksent, 1);
591 * The outgoing interface must be in the zone of source and
592 * destination addresses.
597 if (in6_setscope(&src0, origifp, &zone))
599 bzero(&src_sa, sizeof(src_sa));
600 src_sa.sin6_family = AF_INET6;
601 src_sa.sin6_len = sizeof(src_sa);
602 src_sa.sin6_addr = ip6->ip6_src;
603 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
607 if (in6_setscope(&dst0, origifp, &zone))
609 /* re-initialize to be sure */
610 bzero(&dst_sa, sizeof(dst_sa));
611 dst_sa.sin6_family = AF_INET6;
612 dst_sa.sin6_len = sizeof(dst_sa);
613 dst_sa.sin6_addr = ip6->ip6_dst;
614 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
618 /* We should use ia_ifp to support the case of
619 * sending packets to an address of our own.
621 if (ia != NULL && ia->ia_ifp)
624 /* scope check is done. */
628 IP6STAT_INC(ip6s_badscope);
629 in6_ifstat_inc(origifp, ifs6_out_discard);
631 error = EHOSTUNREACH; /* XXX */
635 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
636 if (opt && opt->ip6po_nextroute.ro_rt) {
638 * The nexthop is explicitly specified by the
639 * application. We assume the next hop is an IPv6
642 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
644 else if ((rt->rt_flags & RTF_GATEWAY))
645 dst = (struct sockaddr_in6 *)rt->rt_gateway;
648 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
649 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
651 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
652 in6_ifstat_inc(ifp, ifs6_out_mcast);
654 * Confirm that the outgoing interface supports multicast.
656 if (!(ifp->if_flags & IFF_MULTICAST)) {
657 IP6STAT_INC(ip6s_noroute);
658 in6_ifstat_inc(ifp, ifs6_out_discard);
662 if ((im6o == NULL && in6_mcast_loop) ||
663 (im6o && im6o->im6o_multicast_loop)) {
665 * Loop back multicast datagram if not expressly
666 * forbidden to do so, even if we have not joined
667 * the address; protocols will filter it later,
668 * thus deferring a hash lookup and lock acquisition
669 * at the expense of an m_copym().
671 ip6_mloopback(ifp, m);
674 * If we are acting as a multicast router, perform
675 * multicast forwarding as if the packet had just
676 * arrived on the interface to which we are about
677 * to send. The multicast forwarding function
678 * recursively calls this function, using the
679 * IPV6_FORWARDING flag to prevent infinite recursion.
681 * Multicasts that are looped back by ip6_mloopback(),
682 * above, will be forwarded by the ip6_input() routine,
685 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
687 * XXX: ip6_mforward expects that rcvif is NULL
688 * when it is called from the originating path.
689 * However, it may not always be the case.
691 m->m_pkthdr.rcvif = NULL;
692 if (ip6_mforward(ip6, ifp, m) != 0) {
699 * Multicasts with a hoplimit of zero may be looped back,
700 * above, but must not be transmitted on a network.
701 * Also, multicasts addressed to the loopback interface
702 * are not sent -- the above call to ip6_mloopback() will
703 * loop back a copy if this host actually belongs to the
704 * destination group on the loopback interface.
706 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
707 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
714 * Fill the outgoing inteface to tell the upper layer
715 * to increment per-interface statistics.
720 /* Determine path MTU. */
721 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
722 &mtu, &alwaysfrag, fibnum)) != 0)
726 * The caller of this function may specify to use the minimum MTU
728 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
729 * setting. The logic is a bit complicated; by default, unicast
730 * packets will follow path MTU while multicast packets will be sent at
731 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
732 * including unicast ones will be sent at the minimum MTU. Multicast
733 * packets will always be sent at the minimum MTU unless
734 * IP6PO_MINMTU_DISABLE is explicitly specified.
735 * See RFC 3542 for more details.
737 if (mtu > IPV6_MMTU) {
738 if ((flags & IPV6_MINMTU))
740 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
742 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
744 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
750 * clear embedded scope identifiers if necessary.
751 * in6_clearscope will touch the addresses only when necessary.
753 in6_clearscope(&ip6->ip6_src);
754 in6_clearscope(&ip6->ip6_dst);
757 * If the outgoing packet contains a hop-by-hop options header,
758 * it must be examined and processed even by the source node.
759 * (RFC 2460, section 4.)
761 if (exthdrs.ip6e_hbh) {
762 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
763 u_int32_t dummy; /* XXX unused */
764 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
767 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
768 panic("ip6e_hbh is not contiguous");
771 * XXX: if we have to send an ICMPv6 error to the sender,
772 * we need the M_LOOP flag since icmp6_error() expects
773 * the IPv6 and the hop-by-hop options header are
774 * contiguous unless the flag is set.
776 m->m_flags |= M_LOOP;
777 m->m_pkthdr.rcvif = ifp;
778 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
779 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
780 &dummy, &plen) < 0) {
781 /* m was already freed at this point */
782 error = EINVAL;/* better error? */
785 m->m_flags &= ~M_LOOP; /* XXX */
786 m->m_pkthdr.rcvif = NULL;
789 /* Jump over all PFIL processing if hooks are not active. */
790 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
794 /* Run through list of hooks for output packets. */
795 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
796 if (error != 0 || m == NULL)
799 ip6 = mtod(m, struct ip6_hdr *);
802 /* See if destination IP address was changed by packet filter. */
803 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
804 m->m_flags |= M_SKIP_FIREWALL;
805 /* If destination is now ourself drop to ip6_input(). */
806 if (in6_localip(&ip6->ip6_dst)) {
807 m->m_flags |= M_FASTFWD_OURS;
808 if (m->m_pkthdr.rcvif == NULL)
809 m->m_pkthdr.rcvif = V_loif;
810 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
811 m->m_pkthdr.csum_flags |=
812 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
813 m->m_pkthdr.csum_data = 0xffff;
816 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
817 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
819 error = netisr_queue(NETISR_IPV6, m);
823 needfiblookup = 1; /* Redo the routing table lookup. */
826 /* See if fib was changed by packet filter. */
827 if (fibnum != M_GETFIB(m)) {
828 m->m_flags |= M_SKIP_FIREWALL;
829 fibnum = M_GETFIB(m);
836 /* See if local, if yes, send it to netisr. */
837 if (m->m_flags & M_FASTFWD_OURS) {
838 if (m->m_pkthdr.rcvif == NULL)
839 m->m_pkthdr.rcvif = V_loif;
840 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
841 m->m_pkthdr.csum_flags |=
842 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
843 m->m_pkthdr.csum_data = 0xffff;
846 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
847 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
849 error = netisr_queue(NETISR_IPV6, m);
852 /* Or forward to some other address? */
853 if ((m->m_flags & M_IP6_NEXTHOP) &&
854 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
855 dst = (struct sockaddr_in6 *)&ro->ro_dst;
856 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
857 m->m_flags |= M_SKIP_FIREWALL;
858 m->m_flags &= ~M_IP6_NEXTHOP;
859 m_tag_delete(m, fwd_tag);
865 * Send the packet to the outgoing interface.
866 * If necessary, do IPv6 fragmentation before sending.
868 * the logic here is rather complex:
869 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
870 * 1-a: send as is if tlen <= path mtu
871 * 1-b: fragment if tlen > path mtu
873 * 2: if user asks us not to fragment (dontfrag == 1)
874 * 2-a: send as is if tlen <= interface mtu
875 * 2-b: error if tlen > interface mtu
877 * 3: if we always need to attach fragment header (alwaysfrag == 1)
880 * 4: if dontfrag == 1 && alwaysfrag == 1
881 * error, as we cannot handle this conflicting request
883 sw_csum = m->m_pkthdr.csum_flags;
885 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
886 sw_csum &= ~ifp->if_hwassist;
890 * If we added extension headers, we will not do TSO and calculate the
891 * checksums ourselves for now.
892 * XXX-BZ Need a framework to know when the NIC can handle it, even
895 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
896 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
897 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
900 if (sw_csum & CSUM_SCTP_IPV6) {
901 sw_csum &= ~CSUM_SCTP_IPV6;
902 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
905 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
906 tlen = m->m_pkthdr.len;
908 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
912 if (dontfrag && alwaysfrag) { /* case 4 */
913 /* conflicting request - can't transmit */
917 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
919 * Even if the DONTFRAG option is specified, we cannot send the
920 * packet when the data length is larger than the MTU of the
921 * outgoing interface.
922 * Notify the error by sending IPV6_PATHMTU ancillary data if
923 * application wanted to know the MTU value. Also return an
924 * error code (this is not described in the API spec).
927 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
933 * transmit packet without fragmentation
935 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
936 struct in6_ifaddr *ia6;
938 ip6 = mtod(m, struct ip6_hdr *);
939 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
941 /* Record statistics for this interface address. */
942 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
943 counter_u64_add(ia6->ia_ifa.ifa_obytes,
945 ifa_free(&ia6->ia_ifa);
947 error = nd6_output_ifp(ifp, origifp, m, dst,
953 * try to fragment the packet. case 1-b and 3
955 if (mtu < IPV6_MMTU) {
956 /* path MTU cannot be less than IPV6_MMTU */
958 in6_ifstat_inc(ifp, ifs6_out_fragfail);
960 } else if (ip6->ip6_plen == 0) {
961 /* jumbo payload cannot be fragmented */
963 in6_ifstat_inc(ifp, ifs6_out_fragfail);
969 * Too large for the destination or interface;
970 * fragment if possible.
971 * Must be able to put at least 8 bytes per fragment.
973 hlen = unfragpartlen;
974 if (mtu > IPV6_MAXPACKET)
975 mtu = IPV6_MAXPACKET;
977 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
980 in6_ifstat_inc(ifp, ifs6_out_fragfail);
985 * If the interface will not calculate checksums on
986 * fragmented packets, then do it here.
987 * XXX-BZ handle the hw offloading case. Need flags.
989 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
990 in6_delayed_cksum(m, plen, hlen);
991 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
994 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
995 sctp_delayed_cksum(m, hlen);
996 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1000 * Change the next header field of the last header in the
1001 * unfragmentable part.
1003 if (exthdrs.ip6e_rthdr) {
1004 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1005 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1006 } else if (exthdrs.ip6e_dest1) {
1007 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1008 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1009 } else if (exthdrs.ip6e_hbh) {
1010 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1011 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1013 nextproto = ip6->ip6_nxt;
1014 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1018 * Loop through length of segment after first fragment,
1019 * make new header and copy data of each part and link onto
1023 id = htonl(ip6_randomid());
1024 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1027 in6_ifstat_inc(ifp, ifs6_out_fragok);
1031 * Remove leading garbages.
1037 for (m0 = m; m; m = m0) {
1041 /* Record statistics for this interface address. */
1043 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1044 counter_u64_add(ia->ia_ifa.ifa_obytes,
1047 error = nd6_output_ifp(ifp, origifp, m, dst,
1048 (struct route *)ro);
1054 IP6STAT_INC(ip6s_fragmented);
1058 * Release the route if using our private route, or if
1059 * (with flowtable) we don't have our own reference.
1061 if (ro == &ip6route || ro->ro_flags & RT_NORTREF)
1066 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1067 m_freem(exthdrs.ip6e_dest1);
1068 m_freem(exthdrs.ip6e_rthdr);
1069 m_freem(exthdrs.ip6e_dest2);
1078 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1082 if (hlen > MCLBYTES)
1083 return (ENOBUFS); /* XXX */
1086 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1088 m = m_get(M_NOWAIT, MT_DATA);
1093 bcopy(hdr, mtod(m, caddr_t), hlen);
1100 * Insert jumbo payload option.
1103 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1109 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1112 * If there is no hop-by-hop options header, allocate new one.
1113 * If there is one but it doesn't have enough space to store the
1114 * jumbo payload option, allocate a cluster to store the whole options.
1115 * Otherwise, use it to store the options.
1117 if (exthdrs->ip6e_hbh == NULL) {
1118 mopt = m_get(M_NOWAIT, MT_DATA);
1121 mopt->m_len = JUMBOOPTLEN;
1122 optbuf = mtod(mopt, u_char *);
1123 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1124 exthdrs->ip6e_hbh = mopt;
1126 struct ip6_hbh *hbh;
1128 mopt = exthdrs->ip6e_hbh;
1129 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1132 * - exthdrs->ip6e_hbh is not referenced from places
1133 * other than exthdrs.
1134 * - exthdrs->ip6e_hbh is not an mbuf chain.
1136 int oldoptlen = mopt->m_len;
1140 * XXX: give up if the whole (new) hbh header does
1141 * not fit even in an mbuf cluster.
1143 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1147 * As a consequence, we must always prepare a cluster
1150 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1153 n->m_len = oldoptlen + JUMBOOPTLEN;
1154 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1156 optbuf = mtod(n, caddr_t) + oldoptlen;
1158 mopt = exthdrs->ip6e_hbh = n;
1160 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1161 mopt->m_len += JUMBOOPTLEN;
1163 optbuf[0] = IP6OPT_PADN;
1167 * Adjust the header length according to the pad and
1168 * the jumbo payload option.
1170 hbh = mtod(mopt, struct ip6_hbh *);
1171 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1174 /* fill in the option. */
1175 optbuf[2] = IP6OPT_JUMBO;
1177 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1178 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1180 /* finally, adjust the packet header length */
1181 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1188 * Insert fragment header and copy unfragmentable header portions.
1191 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1192 struct ip6_frag **frghdrp)
1194 struct mbuf *n, *mlast;
1196 if (hlen > sizeof(struct ip6_hdr)) {
1197 n = m_copym(m0, sizeof(struct ip6_hdr),
1198 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1205 /* Search for the last mbuf of unfragmentable part. */
1206 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1209 if (M_WRITABLE(mlast) &&
1210 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1211 /* use the trailing space of the last mbuf for the fragment hdr */
1212 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1214 mlast->m_len += sizeof(struct ip6_frag);
1215 m->m_pkthdr.len += sizeof(struct ip6_frag);
1217 /* allocate a new mbuf for the fragment header */
1220 mfrg = m_get(M_NOWAIT, MT_DATA);
1223 mfrg->m_len = sizeof(struct ip6_frag);
1224 *frghdrp = mtod(mfrg, struct ip6_frag *);
1225 mlast->m_next = mfrg;
1232 * Calculates IPv6 path mtu for destination @dst.
1233 * Resulting MTU is stored in @mtup.
1235 * Returns 0 on success.
1238 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1240 struct nhop6_extended nh6;
1241 struct in6_addr kdst;
1247 in6_splitscope(dst, &kdst, &scopeid);
1248 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1249 return (EHOSTUNREACH);
1254 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL);
1255 fib6_free_nh_ext(fibnum, &nh6);
1261 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1262 * and cached data in @ro_pmtu.
1263 * MTU from (successful) route lookup is saved (along with dst)
1264 * inside @ro_pmtu to avoid subsequent route lookups after packet
1265 * filter processing.
1267 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1268 * Returns 0 on success.
1271 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1272 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1273 int *alwaysfragp, u_int fibnum)
1275 struct nhop6_basic nh6;
1276 struct in6_addr kdst;
1278 struct sockaddr_in6 *sa6_dst;
1285 * Here ro_pmtu has final destination address, while
1286 * ro might represent immediate destination.
1287 * Use ro_pmtu destination since mtu might differ.
1289 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1290 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1291 ro_pmtu->ro_mtu = 0;
1293 if (ro_pmtu->ro_mtu == 0) {
1294 bzero(sa6_dst, sizeof(*sa6_dst));
1295 sa6_dst->sin6_family = AF_INET6;
1296 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1297 sa6_dst->sin6_addr = *dst;
1299 in6_splitscope(dst, &kdst, &scopeid);
1300 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1302 ro_pmtu->ro_mtu = nh6.nh_mtu;
1305 mtu = ro_pmtu->ro_mtu;
1309 mtu = ro_pmtu->ro_rt->rt_mtu;
1311 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp));
1315 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1316 * hostcache data for @dst.
1317 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1319 * Returns 0 on success.
1322 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1323 u_long *mtup, int *alwaysfragp)
1331 struct in_conninfo inc;
1333 bzero(&inc, sizeof(inc));
1334 inc.inc_flags |= INC_ISIPV6;
1335 inc.inc6_faddr = *dst;
1337 ifmtu = IN6_LINKMTU(ifp);
1338 mtu = tcp_hc_getmtu(&inc);
1340 mtu = min(mtu, rt_mtu);
1345 else if (mtu < IPV6_MMTU) {
1347 * RFC2460 section 5, last paragraph:
1348 * if we record ICMPv6 too big message with
1349 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1350 * or smaller, with framgent header attached.
1351 * (fragment header is needed regardless from the
1352 * packet size, for translators to identify packets)
1358 mtu = IN6_LINKMTU(ifp);
1360 error = EHOSTUNREACH; /* XXX */
1364 *alwaysfragp = alwaysfrag;
1369 * IP6 socket option processing.
1372 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1374 int optdatalen, uproto;
1376 struct inpcb *in6p = sotoinpcb(so);
1378 int level, op, optname;
1382 uint32_t rss_bucket;
1386 level = sopt->sopt_level;
1387 op = sopt->sopt_dir;
1388 optname = sopt->sopt_name;
1389 optlen = sopt->sopt_valsize;
1393 uproto = (int)so->so_proto->pr_protocol;
1395 if (level != IPPROTO_IPV6) {
1398 if (sopt->sopt_level == SOL_SOCKET &&
1399 sopt->sopt_dir == SOPT_SET) {
1400 switch (sopt->sopt_name) {
1403 if ((so->so_options & SO_REUSEADDR) != 0)
1404 in6p->inp_flags2 |= INP_REUSEADDR;
1406 in6p->inp_flags2 &= ~INP_REUSEADDR;
1412 if ((so->so_options & SO_REUSEPORT) != 0)
1413 in6p->inp_flags2 |= INP_REUSEPORT;
1415 in6p->inp_flags2 &= ~INP_REUSEPORT;
1421 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1429 } else { /* level == IPPROTO_IPV6 */
1434 case IPV6_2292PKTOPTIONS:
1435 #ifdef IPV6_PKTOPTIONS
1436 case IPV6_PKTOPTIONS:
1441 error = soopt_getm(sopt, &m); /* XXX */
1444 error = soopt_mcopyin(sopt, m); /* XXX */
1447 error = ip6_pcbopts(&in6p->in6p_outputopts,
1449 m_freem(m); /* XXX */
1454 * Use of some Hop-by-Hop options or some
1455 * Destination options, might require special
1456 * privilege. That is, normal applications
1457 * (without special privilege) might be forbidden
1458 * from setting certain options in outgoing packets,
1459 * and might never see certain options in received
1460 * packets. [RFC 2292 Section 6]
1461 * KAME specific note:
1462 * KAME prevents non-privileged users from sending or
1463 * receiving ANY hbh/dst options in order to avoid
1464 * overhead of parsing options in the kernel.
1466 case IPV6_RECVHOPOPTS:
1467 case IPV6_RECVDSTOPTS:
1468 case IPV6_RECVRTHDRDSTOPTS:
1470 error = priv_check(td,
1471 PRIV_NETINET_SETHDROPTS);
1476 case IPV6_UNICAST_HOPS:
1479 case IPV6_RECVPKTINFO:
1480 case IPV6_RECVHOPLIMIT:
1481 case IPV6_RECVRTHDR:
1482 case IPV6_RECVPATHMTU:
1483 case IPV6_RECVTCLASS:
1484 case IPV6_RECVFLOWID:
1486 case IPV6_RECVRSSBUCKETID:
1489 case IPV6_AUTOFLOWLABEL:
1491 case IPV6_BINDMULTI:
1493 case IPV6_RSS_LISTEN_BUCKET:
1495 if (optname == IPV6_BINDANY && td != NULL) {
1496 error = priv_check(td,
1497 PRIV_NETINET_BINDANY);
1502 if (optlen != sizeof(int)) {
1506 error = sooptcopyin(sopt, &optval,
1507 sizeof optval, sizeof optval);
1512 case IPV6_UNICAST_HOPS:
1513 if (optval < -1 || optval >= 256)
1516 /* -1 = kernel default */
1517 in6p->in6p_hops = optval;
1518 if ((in6p->inp_vflag &
1520 in6p->inp_ip_ttl = optval;
1523 #define OPTSET(bit) \
1527 in6p->inp_flags |= (bit); \
1529 in6p->inp_flags &= ~(bit); \
1530 INP_WUNLOCK(in6p); \
1531 } while (/*CONSTCOND*/ 0)
1532 #define OPTSET2292(bit) \
1535 in6p->inp_flags |= IN6P_RFC2292; \
1537 in6p->inp_flags |= (bit); \
1539 in6p->inp_flags &= ~(bit); \
1540 INP_WUNLOCK(in6p); \
1541 } while (/*CONSTCOND*/ 0)
1542 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1544 #define OPTSET2(bit, val) do { \
1547 in6p->inp_flags2 |= bit; \
1549 in6p->inp_flags2 &= ~bit; \
1550 INP_WUNLOCK(in6p); \
1552 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1554 case IPV6_RECVPKTINFO:
1555 /* cannot mix with RFC2292 */
1556 if (OPTBIT(IN6P_RFC2292)) {
1560 OPTSET(IN6P_PKTINFO);
1565 struct ip6_pktopts **optp;
1567 /* cannot mix with RFC2292 */
1568 if (OPTBIT(IN6P_RFC2292)) {
1572 optp = &in6p->in6p_outputopts;
1573 error = ip6_pcbopt(IPV6_HOPLIMIT,
1574 (u_char *)&optval, sizeof(optval),
1575 optp, (td != NULL) ? td->td_ucred :
1580 case IPV6_RECVHOPLIMIT:
1581 /* cannot mix with RFC2292 */
1582 if (OPTBIT(IN6P_RFC2292)) {
1586 OPTSET(IN6P_HOPLIMIT);
1589 case IPV6_RECVHOPOPTS:
1590 /* cannot mix with RFC2292 */
1591 if (OPTBIT(IN6P_RFC2292)) {
1595 OPTSET(IN6P_HOPOPTS);
1598 case IPV6_RECVDSTOPTS:
1599 /* cannot mix with RFC2292 */
1600 if (OPTBIT(IN6P_RFC2292)) {
1604 OPTSET(IN6P_DSTOPTS);
1607 case IPV6_RECVRTHDRDSTOPTS:
1608 /* cannot mix with RFC2292 */
1609 if (OPTBIT(IN6P_RFC2292)) {
1613 OPTSET(IN6P_RTHDRDSTOPTS);
1616 case IPV6_RECVRTHDR:
1617 /* cannot mix with RFC2292 */
1618 if (OPTBIT(IN6P_RFC2292)) {
1625 case IPV6_RECVPATHMTU:
1627 * We ignore this option for TCP
1629 * (RFC3542 leaves this case
1632 if (uproto != IPPROTO_TCP)
1636 case IPV6_RECVFLOWID:
1637 OPTSET2(INP_RECVFLOWID, optval);
1641 case IPV6_RECVRSSBUCKETID:
1642 OPTSET2(INP_RECVRSSBUCKETID, optval);
1648 * make setsockopt(IPV6_V6ONLY)
1649 * available only prior to bind(2).
1650 * see ipng mailing list, Jun 22 2001.
1652 if (in6p->inp_lport ||
1653 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1657 OPTSET(IN6P_IPV6_V6ONLY);
1659 in6p->inp_vflag &= ~INP_IPV4;
1661 in6p->inp_vflag |= INP_IPV4;
1663 case IPV6_RECVTCLASS:
1664 /* cannot mix with RFC2292 XXX */
1665 if (OPTBIT(IN6P_RFC2292)) {
1669 OPTSET(IN6P_TCLASS);
1671 case IPV6_AUTOFLOWLABEL:
1672 OPTSET(IN6P_AUTOFLOWLABEL);
1676 OPTSET(INP_BINDANY);
1679 case IPV6_BINDMULTI:
1680 OPTSET2(INP_BINDMULTI, optval);
1683 case IPV6_RSS_LISTEN_BUCKET:
1684 if ((optval >= 0) &&
1685 (optval < rss_getnumbuckets())) {
1686 in6p->inp_rss_listen_bucket = optval;
1687 OPTSET2(INP_RSS_BUCKET_SET, 1);
1698 case IPV6_USE_MIN_MTU:
1699 case IPV6_PREFER_TEMPADDR:
1700 if (optlen != sizeof(optval)) {
1704 error = sooptcopyin(sopt, &optval,
1705 sizeof optval, sizeof optval);
1709 struct ip6_pktopts **optp;
1710 optp = &in6p->in6p_outputopts;
1711 error = ip6_pcbopt(optname,
1712 (u_char *)&optval, sizeof(optval),
1713 optp, (td != NULL) ? td->td_ucred :
1718 case IPV6_2292PKTINFO:
1719 case IPV6_2292HOPLIMIT:
1720 case IPV6_2292HOPOPTS:
1721 case IPV6_2292DSTOPTS:
1722 case IPV6_2292RTHDR:
1724 if (optlen != sizeof(int)) {
1728 error = sooptcopyin(sopt, &optval,
1729 sizeof optval, sizeof optval);
1733 case IPV6_2292PKTINFO:
1734 OPTSET2292(IN6P_PKTINFO);
1736 case IPV6_2292HOPLIMIT:
1737 OPTSET2292(IN6P_HOPLIMIT);
1739 case IPV6_2292HOPOPTS:
1741 * Check super-user privilege.
1742 * See comments for IPV6_RECVHOPOPTS.
1745 error = priv_check(td,
1746 PRIV_NETINET_SETHDROPTS);
1750 OPTSET2292(IN6P_HOPOPTS);
1752 case IPV6_2292DSTOPTS:
1754 error = priv_check(td,
1755 PRIV_NETINET_SETHDROPTS);
1759 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1761 case IPV6_2292RTHDR:
1762 OPTSET2292(IN6P_RTHDR);
1770 case IPV6_RTHDRDSTOPTS:
1773 /* new advanced API (RFC3542) */
1775 u_char optbuf_storage[MCLBYTES];
1777 struct ip6_pktopts **optp;
1779 /* cannot mix with RFC2292 */
1780 if (OPTBIT(IN6P_RFC2292)) {
1786 * We only ensure valsize is not too large
1787 * here. Further validation will be done
1790 error = sooptcopyin(sopt, optbuf_storage,
1791 sizeof(optbuf_storage), 0);
1794 optlen = sopt->sopt_valsize;
1795 optbuf = optbuf_storage;
1796 optp = &in6p->in6p_outputopts;
1797 error = ip6_pcbopt(optname, optbuf, optlen,
1798 optp, (td != NULL) ? td->td_ucred : NULL,
1804 case IPV6_MULTICAST_IF:
1805 case IPV6_MULTICAST_HOPS:
1806 case IPV6_MULTICAST_LOOP:
1807 case IPV6_JOIN_GROUP:
1808 case IPV6_LEAVE_GROUP:
1810 case MCAST_BLOCK_SOURCE:
1811 case MCAST_UNBLOCK_SOURCE:
1812 case MCAST_JOIN_GROUP:
1813 case MCAST_LEAVE_GROUP:
1814 case MCAST_JOIN_SOURCE_GROUP:
1815 case MCAST_LEAVE_SOURCE_GROUP:
1816 error = ip6_setmoptions(in6p, sopt);
1819 case IPV6_PORTRANGE:
1820 error = sooptcopyin(sopt, &optval,
1821 sizeof optval, sizeof optval);
1827 case IPV6_PORTRANGE_DEFAULT:
1828 in6p->inp_flags &= ~(INP_LOWPORT);
1829 in6p->inp_flags &= ~(INP_HIGHPORT);
1832 case IPV6_PORTRANGE_HIGH:
1833 in6p->inp_flags &= ~(INP_LOWPORT);
1834 in6p->inp_flags |= INP_HIGHPORT;
1837 case IPV6_PORTRANGE_LOW:
1838 in6p->inp_flags &= ~(INP_HIGHPORT);
1839 in6p->inp_flags |= INP_LOWPORT;
1850 case IPV6_IPSEC_POLICY:
1855 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1857 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1859 req = mtod(m, caddr_t);
1860 error = ipsec_set_policy(in6p, optname, req,
1861 m->m_len, (sopt->sopt_td != NULL) ?
1862 sopt->sopt_td->td_ucred : NULL);
1869 error = ENOPROTOOPT;
1877 case IPV6_2292PKTOPTIONS:
1878 #ifdef IPV6_PKTOPTIONS
1879 case IPV6_PKTOPTIONS:
1882 * RFC3542 (effectively) deprecated the
1883 * semantics of the 2292-style pktoptions.
1884 * Since it was not reliable in nature (i.e.,
1885 * applications had to expect the lack of some
1886 * information after all), it would make sense
1887 * to simplify this part by always returning
1890 sopt->sopt_valsize = 0;
1893 case IPV6_RECVHOPOPTS:
1894 case IPV6_RECVDSTOPTS:
1895 case IPV6_RECVRTHDRDSTOPTS:
1896 case IPV6_UNICAST_HOPS:
1897 case IPV6_RECVPKTINFO:
1898 case IPV6_RECVHOPLIMIT:
1899 case IPV6_RECVRTHDR:
1900 case IPV6_RECVPATHMTU:
1903 case IPV6_PORTRANGE:
1904 case IPV6_RECVTCLASS:
1905 case IPV6_AUTOFLOWLABEL:
1909 case IPV6_RECVFLOWID:
1911 case IPV6_RSSBUCKETID:
1912 case IPV6_RECVRSSBUCKETID:
1914 case IPV6_BINDMULTI:
1917 case IPV6_RECVHOPOPTS:
1918 optval = OPTBIT(IN6P_HOPOPTS);
1921 case IPV6_RECVDSTOPTS:
1922 optval = OPTBIT(IN6P_DSTOPTS);
1925 case IPV6_RECVRTHDRDSTOPTS:
1926 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1929 case IPV6_UNICAST_HOPS:
1930 optval = in6p->in6p_hops;
1933 case IPV6_RECVPKTINFO:
1934 optval = OPTBIT(IN6P_PKTINFO);
1937 case IPV6_RECVHOPLIMIT:
1938 optval = OPTBIT(IN6P_HOPLIMIT);
1941 case IPV6_RECVRTHDR:
1942 optval = OPTBIT(IN6P_RTHDR);
1945 case IPV6_RECVPATHMTU:
1946 optval = OPTBIT(IN6P_MTU);
1950 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1953 case IPV6_PORTRANGE:
1956 flags = in6p->inp_flags;
1957 if (flags & INP_HIGHPORT)
1958 optval = IPV6_PORTRANGE_HIGH;
1959 else if (flags & INP_LOWPORT)
1960 optval = IPV6_PORTRANGE_LOW;
1965 case IPV6_RECVTCLASS:
1966 optval = OPTBIT(IN6P_TCLASS);
1969 case IPV6_AUTOFLOWLABEL:
1970 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1974 optval = OPTBIT(INP_BINDANY);
1978 optval = in6p->inp_flowid;
1982 optval = in6p->inp_flowtype;
1985 case IPV6_RECVFLOWID:
1986 optval = OPTBIT2(INP_RECVFLOWID);
1989 case IPV6_RSSBUCKETID:
1991 rss_hash2bucket(in6p->inp_flowid,
1995 optval = rss_bucket;
2000 case IPV6_RECVRSSBUCKETID:
2001 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2005 case IPV6_BINDMULTI:
2006 optval = OPTBIT2(INP_BINDMULTI);
2012 error = sooptcopyout(sopt, &optval,
2019 struct ip6_mtuinfo mtuinfo;
2021 if (!(so->so_state & SS_ISCONNECTED))
2024 * XXX: we dot not consider the case of source
2025 * routing, or optional information to specify
2026 * the outgoing interface.
2028 error = ip6_getpmtu_ctl(so->so_fibnum,
2029 &in6p->in6p_faddr, &pmtu);
2032 if (pmtu > IPV6_MAXPACKET)
2033 pmtu = IPV6_MAXPACKET;
2035 bzero(&mtuinfo, sizeof(mtuinfo));
2036 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2037 optdata = (void *)&mtuinfo;
2038 optdatalen = sizeof(mtuinfo);
2039 error = sooptcopyout(sopt, optdata,
2044 case IPV6_2292PKTINFO:
2045 case IPV6_2292HOPLIMIT:
2046 case IPV6_2292HOPOPTS:
2047 case IPV6_2292RTHDR:
2048 case IPV6_2292DSTOPTS:
2050 case IPV6_2292PKTINFO:
2051 optval = OPTBIT(IN6P_PKTINFO);
2053 case IPV6_2292HOPLIMIT:
2054 optval = OPTBIT(IN6P_HOPLIMIT);
2056 case IPV6_2292HOPOPTS:
2057 optval = OPTBIT(IN6P_HOPOPTS);
2059 case IPV6_2292RTHDR:
2060 optval = OPTBIT(IN6P_RTHDR);
2062 case IPV6_2292DSTOPTS:
2063 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2066 error = sooptcopyout(sopt, &optval,
2073 case IPV6_RTHDRDSTOPTS:
2077 case IPV6_USE_MIN_MTU:
2078 case IPV6_PREFER_TEMPADDR:
2079 error = ip6_getpcbopt(in6p->in6p_outputopts,
2083 case IPV6_MULTICAST_IF:
2084 case IPV6_MULTICAST_HOPS:
2085 case IPV6_MULTICAST_LOOP:
2087 error = ip6_getmoptions(in6p, sopt);
2091 case IPV6_IPSEC_POLICY:
2095 struct mbuf *m = NULL;
2096 struct mbuf **mp = &m;
2097 size_t ovalsize = sopt->sopt_valsize;
2098 caddr_t oval = (caddr_t)sopt->sopt_val;
2100 error = soopt_getm(sopt, &m); /* XXX */
2103 error = soopt_mcopyin(sopt, m); /* XXX */
2106 sopt->sopt_valsize = ovalsize;
2107 sopt->sopt_val = oval;
2109 req = mtod(m, caddr_t);
2112 error = ipsec_get_policy(in6p, req, len, mp);
2114 error = soopt_mcopyout(sopt, m); /* XXX */
2115 if (error == 0 && m)
2122 error = ENOPROTOOPT;
2132 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2134 int error = 0, optval, optlen;
2135 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2136 struct inpcb *in6p = sotoinpcb(so);
2137 int level, op, optname;
2139 level = sopt->sopt_level;
2140 op = sopt->sopt_dir;
2141 optname = sopt->sopt_name;
2142 optlen = sopt->sopt_valsize;
2144 if (level != IPPROTO_IPV6) {
2151 * For ICMPv6 sockets, no modification allowed for checksum
2152 * offset, permit "no change" values to help existing apps.
2154 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2155 * for an ICMPv6 socket will fail."
2156 * The current behavior does not meet RFC3542.
2160 if (optlen != sizeof(int)) {
2164 error = sooptcopyin(sopt, &optval, sizeof(optval),
2168 if ((optval % 2) != 0) {
2169 /* the API assumes even offset values */
2171 } else if (so->so_proto->pr_protocol ==
2173 if (optval != icmp6off)
2176 in6p->in6p_cksum = optval;
2180 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2183 optval = in6p->in6p_cksum;
2185 error = sooptcopyout(sopt, &optval, sizeof(optval));
2195 error = ENOPROTOOPT;
2203 * Set up IP6 options in pcb for insertion in output packets or
2204 * specifying behavior of outgoing packets.
2207 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2208 struct socket *so, struct sockopt *sopt)
2210 struct ip6_pktopts *opt = *pktopt;
2212 struct thread *td = sopt->sopt_td;
2214 /* turn off any old options. */
2217 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2218 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2219 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2220 printf("ip6_pcbopts: all specified options are cleared.\n");
2222 ip6_clearpktopts(opt, -1);
2224 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2227 if (!m || m->m_len == 0) {
2229 * Only turning off any previous options, regardless of
2230 * whether the opt is just created or given.
2232 free(opt, M_IP6OPT);
2236 /* set options specified by user. */
2237 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2238 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2239 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2240 free(opt, M_IP6OPT);
2248 * initialize ip6_pktopts. beware that there are non-zero default values in
2252 ip6_initpktopts(struct ip6_pktopts *opt)
2255 bzero(opt, sizeof(*opt));
2256 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2257 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2258 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2259 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2263 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2264 struct ucred *cred, int uproto)
2266 struct ip6_pktopts *opt;
2268 if (*pktopt == NULL) {
2269 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2271 ip6_initpktopts(*pktopt);
2275 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2279 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2281 void *optdata = NULL;
2283 struct ip6_ext *ip6e;
2285 struct in6_pktinfo null_pktinfo;
2286 int deftclass = 0, on;
2287 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2288 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2292 optdata = (void *)&null_pktinfo;
2293 if (pktopt && pktopt->ip6po_pktinfo) {
2294 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2295 sizeof(null_pktinfo));
2296 in6_clearscope(&null_pktinfo.ipi6_addr);
2298 /* XXX: we don't have to do this every time... */
2299 bzero(&null_pktinfo, sizeof(null_pktinfo));
2301 optdatalen = sizeof(struct in6_pktinfo);
2304 if (pktopt && pktopt->ip6po_tclass >= 0)
2305 optdata = (void *)&pktopt->ip6po_tclass;
2307 optdata = (void *)&deftclass;
2308 optdatalen = sizeof(int);
2311 if (pktopt && pktopt->ip6po_hbh) {
2312 optdata = (void *)pktopt->ip6po_hbh;
2313 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2314 optdatalen = (ip6e->ip6e_len + 1) << 3;
2318 if (pktopt && pktopt->ip6po_rthdr) {
2319 optdata = (void *)pktopt->ip6po_rthdr;
2320 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2321 optdatalen = (ip6e->ip6e_len + 1) << 3;
2324 case IPV6_RTHDRDSTOPTS:
2325 if (pktopt && pktopt->ip6po_dest1) {
2326 optdata = (void *)pktopt->ip6po_dest1;
2327 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2328 optdatalen = (ip6e->ip6e_len + 1) << 3;
2332 if (pktopt && pktopt->ip6po_dest2) {
2333 optdata = (void *)pktopt->ip6po_dest2;
2334 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2335 optdatalen = (ip6e->ip6e_len + 1) << 3;
2339 if (pktopt && pktopt->ip6po_nexthop) {
2340 optdata = (void *)pktopt->ip6po_nexthop;
2341 optdatalen = pktopt->ip6po_nexthop->sa_len;
2344 case IPV6_USE_MIN_MTU:
2346 optdata = (void *)&pktopt->ip6po_minmtu;
2348 optdata = (void *)&defminmtu;
2349 optdatalen = sizeof(int);
2352 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2356 optdata = (void *)&on;
2357 optdatalen = sizeof(on);
2359 case IPV6_PREFER_TEMPADDR:
2361 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2363 optdata = (void *)&defpreftemp;
2364 optdatalen = sizeof(int);
2366 default: /* should not happen */
2368 panic("ip6_getpcbopt: unexpected option\n");
2370 return (ENOPROTOOPT);
2373 error = sooptcopyout(sopt, optdata, optdatalen);
2379 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2384 if (optname == -1 || optname == IPV6_PKTINFO) {
2385 if (pktopt->ip6po_pktinfo)
2386 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2387 pktopt->ip6po_pktinfo = NULL;
2389 if (optname == -1 || optname == IPV6_HOPLIMIT)
2390 pktopt->ip6po_hlim = -1;
2391 if (optname == -1 || optname == IPV6_TCLASS)
2392 pktopt->ip6po_tclass = -1;
2393 if (optname == -1 || optname == IPV6_NEXTHOP) {
2394 if (pktopt->ip6po_nextroute.ro_rt) {
2395 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2396 pktopt->ip6po_nextroute.ro_rt = NULL;
2398 if (pktopt->ip6po_nexthop)
2399 free(pktopt->ip6po_nexthop, M_IP6OPT);
2400 pktopt->ip6po_nexthop = NULL;
2402 if (optname == -1 || optname == IPV6_HOPOPTS) {
2403 if (pktopt->ip6po_hbh)
2404 free(pktopt->ip6po_hbh, M_IP6OPT);
2405 pktopt->ip6po_hbh = NULL;
2407 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2408 if (pktopt->ip6po_dest1)
2409 free(pktopt->ip6po_dest1, M_IP6OPT);
2410 pktopt->ip6po_dest1 = NULL;
2412 if (optname == -1 || optname == IPV6_RTHDR) {
2413 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2414 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2415 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2416 if (pktopt->ip6po_route.ro_rt) {
2417 RTFREE(pktopt->ip6po_route.ro_rt);
2418 pktopt->ip6po_route.ro_rt = NULL;
2421 if (optname == -1 || optname == IPV6_DSTOPTS) {
2422 if (pktopt->ip6po_dest2)
2423 free(pktopt->ip6po_dest2, M_IP6OPT);
2424 pktopt->ip6po_dest2 = NULL;
2428 #define PKTOPT_EXTHDRCPY(type) \
2431 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2432 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2433 if (dst->type == NULL && canwait == M_NOWAIT)\
2435 bcopy(src->type, dst->type, hlen);\
2437 } while (/*CONSTCOND*/ 0)
2440 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2442 if (dst == NULL || src == NULL) {
2443 printf("ip6_clearpktopts: invalid argument\n");
2447 dst->ip6po_hlim = src->ip6po_hlim;
2448 dst->ip6po_tclass = src->ip6po_tclass;
2449 dst->ip6po_flags = src->ip6po_flags;
2450 dst->ip6po_minmtu = src->ip6po_minmtu;
2451 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2452 if (src->ip6po_pktinfo) {
2453 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2455 if (dst->ip6po_pktinfo == NULL)
2457 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2459 if (src->ip6po_nexthop) {
2460 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2462 if (dst->ip6po_nexthop == NULL)
2464 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2465 src->ip6po_nexthop->sa_len);
2467 PKTOPT_EXTHDRCPY(ip6po_hbh);
2468 PKTOPT_EXTHDRCPY(ip6po_dest1);
2469 PKTOPT_EXTHDRCPY(ip6po_dest2);
2470 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2474 ip6_clearpktopts(dst, -1);
2477 #undef PKTOPT_EXTHDRCPY
2479 struct ip6_pktopts *
2480 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2483 struct ip6_pktopts *dst;
2485 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2488 ip6_initpktopts(dst);
2490 if ((error = copypktopts(dst, src, canwait)) != 0) {
2491 free(dst, M_IP6OPT);
2499 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2504 ip6_clearpktopts(pktopt, -1);
2506 free(pktopt, M_IP6OPT);
2510 * Set IPv6 outgoing packet options based on advanced API.
2513 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2514 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2516 struct cmsghdr *cm = NULL;
2518 if (control == NULL || opt == NULL)
2521 ip6_initpktopts(opt);
2526 * If stickyopt is provided, make a local copy of the options
2527 * for this particular packet, then override them by ancillary
2529 * XXX: copypktopts() does not copy the cached route to a next
2530 * hop (if any). This is not very good in terms of efficiency,
2531 * but we can allow this since this option should be rarely
2534 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2539 * XXX: Currently, we assume all the optional information is stored
2542 if (control->m_next)
2545 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2546 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2549 if (control->m_len < CMSG_LEN(0))
2552 cm = mtod(control, struct cmsghdr *);
2553 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2555 if (cm->cmsg_level != IPPROTO_IPV6)
2558 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2559 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2568 * Set a particular packet option, as a sticky option or an ancillary data
2569 * item. "len" can be 0 only when it's a sticky option.
2570 * We have 4 cases of combination of "sticky" and "cmsg":
2571 * "sticky=0, cmsg=0": impossible
2572 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2573 * "sticky=1, cmsg=0": RFC3542 socket option
2574 * "sticky=1, cmsg=1": RFC2292 socket option
2577 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2578 struct ucred *cred, int sticky, int cmsg, int uproto)
2580 int minmtupolicy, preftemp;
2583 if (!sticky && !cmsg) {
2585 printf("ip6_setpktopt: impossible case\n");
2591 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2592 * not be specified in the context of RFC3542. Conversely,
2593 * RFC3542 types should not be specified in the context of RFC2292.
2597 case IPV6_2292PKTINFO:
2598 case IPV6_2292HOPLIMIT:
2599 case IPV6_2292NEXTHOP:
2600 case IPV6_2292HOPOPTS:
2601 case IPV6_2292DSTOPTS:
2602 case IPV6_2292RTHDR:
2603 case IPV6_2292PKTOPTIONS:
2604 return (ENOPROTOOPT);
2607 if (sticky && cmsg) {
2614 case IPV6_RTHDRDSTOPTS:
2616 case IPV6_USE_MIN_MTU:
2619 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2620 return (ENOPROTOOPT);
2625 case IPV6_2292PKTINFO:
2628 struct ifnet *ifp = NULL;
2629 struct in6_pktinfo *pktinfo;
2631 if (len != sizeof(struct in6_pktinfo))
2634 pktinfo = (struct in6_pktinfo *)buf;
2637 * An application can clear any sticky IPV6_PKTINFO option by
2638 * doing a "regular" setsockopt with ipi6_addr being
2639 * in6addr_any and ipi6_ifindex being zero.
2640 * [RFC 3542, Section 6]
2642 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2643 pktinfo->ipi6_ifindex == 0 &&
2644 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2645 ip6_clearpktopts(opt, optname);
2649 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2650 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2653 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2655 /* validate the interface index if specified. */
2656 if (pktinfo->ipi6_ifindex > V_if_index)
2658 if (pktinfo->ipi6_ifindex) {
2659 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2663 if (ifp != NULL && (
2664 ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED))
2668 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2669 struct in6_ifaddr *ia;
2671 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2672 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2674 return (EADDRNOTAVAIL);
2675 ifa_free(&ia->ia_ifa);
2678 * We store the address anyway, and let in6_selectsrc()
2679 * validate the specified address. This is because ipi6_addr
2680 * may not have enough information about its scope zone, and
2681 * we may need additional information (such as outgoing
2682 * interface or the scope zone of a destination address) to
2683 * disambiguate the scope.
2684 * XXX: the delay of the validation may confuse the
2685 * application when it is used as a sticky option.
2687 if (opt->ip6po_pktinfo == NULL) {
2688 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2689 M_IP6OPT, M_NOWAIT);
2690 if (opt->ip6po_pktinfo == NULL)
2693 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2697 case IPV6_2292HOPLIMIT:
2703 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2704 * to simplify the ordering among hoplimit options.
2706 if (optname == IPV6_HOPLIMIT && sticky)
2707 return (ENOPROTOOPT);
2709 if (len != sizeof(int))
2712 if (*hlimp < -1 || *hlimp > 255)
2715 opt->ip6po_hlim = *hlimp;
2723 if (len != sizeof(int))
2725 tclass = *(int *)buf;
2726 if (tclass < -1 || tclass > 255)
2729 opt->ip6po_tclass = tclass;
2733 case IPV6_2292NEXTHOP:
2736 error = priv_check_cred(cred,
2737 PRIV_NETINET_SETHDROPTS, 0);
2742 if (len == 0) { /* just remove the option */
2743 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2747 /* check if cmsg_len is large enough for sa_len */
2748 if (len < sizeof(struct sockaddr) || len < *buf)
2751 switch (((struct sockaddr *)buf)->sa_family) {
2754 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2757 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2760 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2761 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2764 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2770 case AF_LINK: /* should eventually be supported */
2772 return (EAFNOSUPPORT);
2775 /* turn off the previous option, then set the new option. */
2776 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2777 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2778 if (opt->ip6po_nexthop == NULL)
2780 bcopy(buf, opt->ip6po_nexthop, *buf);
2783 case IPV6_2292HOPOPTS:
2786 struct ip6_hbh *hbh;
2790 * XXX: We don't allow a non-privileged user to set ANY HbH
2791 * options, since per-option restriction has too much
2795 error = priv_check_cred(cred,
2796 PRIV_NETINET_SETHDROPTS, 0);
2802 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2803 break; /* just remove the option */
2806 /* message length validation */
2807 if (len < sizeof(struct ip6_hbh))
2809 hbh = (struct ip6_hbh *)buf;
2810 hbhlen = (hbh->ip6h_len + 1) << 3;
2814 /* turn off the previous option, then set the new option. */
2815 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2816 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2817 if (opt->ip6po_hbh == NULL)
2819 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2824 case IPV6_2292DSTOPTS:
2826 case IPV6_RTHDRDSTOPTS:
2828 struct ip6_dest *dest, **newdest = NULL;
2831 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2832 error = priv_check_cred(cred,
2833 PRIV_NETINET_SETHDROPTS, 0);
2839 ip6_clearpktopts(opt, optname);
2840 break; /* just remove the option */
2843 /* message length validation */
2844 if (len < sizeof(struct ip6_dest))
2846 dest = (struct ip6_dest *)buf;
2847 destlen = (dest->ip6d_len + 1) << 3;
2852 * Determine the position that the destination options header
2853 * should be inserted; before or after the routing header.
2856 case IPV6_2292DSTOPTS:
2858 * The old advacned API is ambiguous on this point.
2859 * Our approach is to determine the position based
2860 * according to the existence of a routing header.
2861 * Note, however, that this depends on the order of the
2862 * extension headers in the ancillary data; the 1st
2863 * part of the destination options header must appear
2864 * before the routing header in the ancillary data,
2866 * RFC3542 solved the ambiguity by introducing
2867 * separate ancillary data or option types.
2869 if (opt->ip6po_rthdr == NULL)
2870 newdest = &opt->ip6po_dest1;
2872 newdest = &opt->ip6po_dest2;
2874 case IPV6_RTHDRDSTOPTS:
2875 newdest = &opt->ip6po_dest1;
2878 newdest = &opt->ip6po_dest2;
2882 /* turn off the previous option, then set the new option. */
2883 ip6_clearpktopts(opt, optname);
2884 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2885 if (*newdest == NULL)
2887 bcopy(dest, *newdest, destlen);
2892 case IPV6_2292RTHDR:
2895 struct ip6_rthdr *rth;
2899 ip6_clearpktopts(opt, IPV6_RTHDR);
2900 break; /* just remove the option */
2903 /* message length validation */
2904 if (len < sizeof(struct ip6_rthdr))
2906 rth = (struct ip6_rthdr *)buf;
2907 rthlen = (rth->ip6r_len + 1) << 3;
2911 switch (rth->ip6r_type) {
2912 case IPV6_RTHDR_TYPE_0:
2913 if (rth->ip6r_len == 0) /* must contain one addr */
2915 if (rth->ip6r_len % 2) /* length must be even */
2917 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2921 return (EINVAL); /* not supported */
2924 /* turn off the previous option */
2925 ip6_clearpktopts(opt, IPV6_RTHDR);
2926 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2927 if (opt->ip6po_rthdr == NULL)
2929 bcopy(rth, opt->ip6po_rthdr, rthlen);
2934 case IPV6_USE_MIN_MTU:
2935 if (len != sizeof(int))
2937 minmtupolicy = *(int *)buf;
2938 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2939 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2940 minmtupolicy != IP6PO_MINMTU_ALL) {
2943 opt->ip6po_minmtu = minmtupolicy;
2947 if (len != sizeof(int))
2950 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2952 * we ignore this option for TCP sockets.
2953 * (RFC3542 leaves this case unspecified.)
2955 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2957 opt->ip6po_flags |= IP6PO_DONTFRAG;
2960 case IPV6_PREFER_TEMPADDR:
2961 if (len != sizeof(int))
2963 preftemp = *(int *)buf;
2964 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2965 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2966 preftemp != IP6PO_TEMPADDR_PREFER) {
2969 opt->ip6po_prefer_tempaddr = preftemp;
2973 return (ENOPROTOOPT);
2974 } /* end of switch */
2980 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2981 * packet to the input queue of a specified interface. Note that this
2982 * calls the output routine of the loopback "driver", but with an interface
2983 * pointer that might NOT be &loif -- easier than replicating that code here.
2986 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
2989 struct ip6_hdr *ip6;
2991 copym = m_copy(m, 0, M_COPYALL);
2996 * Make sure to deep-copy IPv6 header portion in case the data
2997 * is in an mbuf cluster, so that we can safely override the IPv6
2998 * header portion later.
3000 if (!M_WRITABLE(copym) ||
3001 copym->m_len < sizeof(struct ip6_hdr)) {
3002 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3006 ip6 = mtod(copym, struct ip6_hdr *);
3008 * clear embedded scope identifiers if necessary.
3009 * in6_clearscope will touch the addresses only when necessary.
3011 in6_clearscope(&ip6->ip6_src);
3012 in6_clearscope(&ip6->ip6_dst);
3013 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3014 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3016 copym->m_pkthdr.csum_data = 0xffff;
3018 if_simloop(ifp, copym, AF_INET6, 0);
3022 * Chop IPv6 header off from the payload.
3025 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3028 struct ip6_hdr *ip6;
3030 ip6 = mtod(m, struct ip6_hdr *);
3031 if (m->m_len > sizeof(*ip6)) {
3032 mh = m_gethdr(M_NOWAIT, MT_DATA);
3037 m_move_pkthdr(mh, m);
3038 M_ALIGN(mh, sizeof(*ip6));
3039 m->m_len -= sizeof(*ip6);
3040 m->m_data += sizeof(*ip6);
3043 m->m_len = sizeof(*ip6);
3044 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3046 exthdrs->ip6e_ip6 = m;
3051 * Compute IPv6 extension header length.
3054 ip6_optlen(struct inpcb *in6p)
3058 if (!in6p->in6p_outputopts)
3063 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3065 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3066 if (in6p->in6p_outputopts->ip6po_rthdr)
3067 /* dest1 is valid with rthdr only */
3068 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3069 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3070 len += elen(in6p->in6p_outputopts->ip6po_dest2);