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_var.h>
101 #include <netinet/ip6.h>
102 #include <netinet/icmp6.h>
103 #include <netinet6/ip6_var.h>
104 #include <netinet/in_pcb.h>
105 #include <netinet/tcp_var.h>
106 #include <netinet6/nd6.h>
107 #include <netinet6/in6_rss.h>
110 #include <netipsec/ipsec.h>
111 #include <netipsec/ipsec6.h>
112 #include <netipsec/key.h>
113 #include <netinet6/ip6_ipsec.h>
116 #include <netinet/sctp.h>
117 #include <netinet/sctp_crc32.h>
120 #include <netinet6/ip6protosw.h>
121 #include <netinet6/scope6_var.h>
124 #include <net/flowtable.h>
127 extern int in6_mcast_loop;
130 struct mbuf *ip6e_ip6;
131 struct mbuf *ip6e_hbh;
132 struct mbuf *ip6e_dest1;
133 struct mbuf *ip6e_rthdr;
134 struct mbuf *ip6e_dest2;
137 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
138 struct ucred *, int);
139 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
140 struct socket *, struct sockopt *);
141 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
142 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
143 struct ucred *, int, int, int);
145 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
146 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
148 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
149 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
150 static int ip6_getpmtu(struct route_in6 *, struct route_in6 *,
151 struct ifnet *, struct in6_addr *, u_long *, int *, u_int);
152 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
156 * Make an extension header from option data. hp is the source, and
157 * mp is the destination.
159 #define MAKE_EXTHDR(hp, mp) \
162 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
163 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
164 ((eh)->ip6e_len + 1) << 3); \
168 } while (/*CONSTCOND*/ 0)
171 * Form a chain of extension headers.
172 * m is the extension header mbuf
173 * mp is the previous mbuf in the chain
174 * p is the next header
175 * i is the type of option.
177 #define MAKE_CHAIN(m, mp, p, i)\
181 panic("assumption failed: hdr not split"); \
182 *mtod((m), u_char *) = *(p);\
184 p = mtod((m), u_char *);\
185 (m)->m_next = (mp)->m_next;\
189 } while (/*CONSTCOND*/ 0)
192 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
196 csum = in_cksum_skip(m, offset + plen, offset);
197 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
199 offset += m->m_pkthdr.csum_data; /* checksum offset */
201 if (offset + sizeof(u_short) > m->m_len) {
202 printf("%s: delayed m_pullup, m->len: %d plen %u off %u "
203 "csum_flags=%b\n", __func__, m->m_len, plen, offset,
204 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
206 * XXX this should not happen, but if it does, the correct
207 * behavior may be to insert the checksum in the appropriate
208 * next mbuf in the chain.
212 *(u_short *)(m->m_data + offset) = csum;
216 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
217 int mtu, uint32_t id)
219 struct mbuf *m, **mnext, *m_frgpart;
220 struct ip6_hdr *ip6, *mhip6;
221 struct ip6_frag *ip6f;
224 int tlen = m0->m_pkthdr.len;
227 ip6 = mtod(m, struct ip6_hdr *);
228 mnext = &m->m_nextpkt;
230 for (off = hlen; off < tlen; off += mtu) {
231 m = m_gethdr(M_NOWAIT, MT_DATA);
233 IP6STAT_INC(ip6s_odropped);
236 m->m_flags = m0->m_flags & M_COPYFLAGS;
238 mnext = &m->m_nextpkt;
239 m->m_data += max_linkhdr;
240 mhip6 = mtod(m, struct ip6_hdr *);
242 m->m_len = sizeof(*mhip6);
243 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
245 IP6STAT_INC(ip6s_odropped);
248 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
249 if (off + mtu >= tlen)
252 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
253 mhip6->ip6_plen = htons((u_short)(mtu + hlen +
254 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
255 if ((m_frgpart = m_copy(m0, off, mtu)) == 0) {
256 IP6STAT_INC(ip6s_odropped);
260 m->m_pkthdr.len = mtu + hlen + sizeof(*ip6f);
261 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
262 m->m_pkthdr.rcvif = NULL;
263 ip6f->ip6f_reserved = 0;
264 ip6f->ip6f_ident = id;
265 ip6f->ip6f_nxt = nextproto;
266 IP6STAT_INC(ip6s_ofragments);
267 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
274 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
275 * header (with pri, len, nxt, hlim, src, dst).
276 * This function may modify ver and hlim only.
277 * The mbuf chain containing the packet will be freed.
278 * The mbuf opt, if present, will not be freed.
279 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
280 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
281 * then result of route lookup is stored in ro->ro_rt.
283 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
284 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
287 * ifpp - XXX: just for statistics
290 * XXX TODO: no flowid is assigned for outbound flows?
293 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
294 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
295 struct ifnet **ifpp, struct inpcb *inp)
298 struct ifnet *ifp, *origifp;
300 struct mbuf *mprev = NULL;
302 struct route_in6 ip6route;
303 struct rtentry *rt = NULL;
304 struct sockaddr_in6 *dst, src_sa, dst_sa;
305 struct in6_addr odst;
307 struct in6_ifaddr *ia = NULL;
309 int alwaysfrag, dontfrag;
310 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
311 struct ip6_exthdrs exthdrs;
312 struct in6_addr finaldst, src0, dst0;
314 struct route_in6 *ro_pmtu = NULL;
319 struct m_tag *fwd_tag = NULL;
322 ip6 = mtod(m, struct ip6_hdr *);
324 printf ("ip6 is 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 finaldst = ip6->ip6_dst;
338 bzero(&exthdrs, sizeof(exthdrs));
340 /* Hop-by-Hop options header */
341 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
342 /* Destination options header(1st part) */
343 if (opt->ip6po_rthdr) {
345 * Destination options header(1st part)
346 * This only makes sense with a routing header.
347 * See Section 9.2 of RFC 3542.
348 * Disabling this part just for MIP6 convenience is
349 * a bad idea. We need to think carefully about a
350 * way to make the advanced API coexist with MIP6
351 * options, which might automatically be inserted in
354 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
357 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
358 /* Destination options header(2nd part) */
359 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
364 * IPSec checking which handles several cases.
365 * FAST IPSEC: We re-injected the packet.
366 * XXX: need scope argument.
368 switch(ip6_ipsec_output(&m, inp, &error))
370 case 1: /* Bad packet */
372 case -1: /* IPSec done */
374 case 0: /* No IPSec */
381 * Calculate the total length of the extension header chain.
382 * Keep the length of the unfragmentable part for fragmentation.
385 if (exthdrs.ip6e_hbh)
386 optlen += exthdrs.ip6e_hbh->m_len;
387 if (exthdrs.ip6e_dest1)
388 optlen += exthdrs.ip6e_dest1->m_len;
389 if (exthdrs.ip6e_rthdr)
390 optlen += exthdrs.ip6e_rthdr->m_len;
391 unfragpartlen = optlen + sizeof(struct ip6_hdr);
393 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
394 if (exthdrs.ip6e_dest2)
395 optlen += exthdrs.ip6e_dest2->m_len;
398 * If there is at least one extension header,
399 * separate IP6 header from the payload.
401 if (optlen && !hdrsplit) {
402 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
406 m = exthdrs.ip6e_ip6;
411 ip6 = mtod(m, struct ip6_hdr *);
413 /* adjust mbuf packet header length */
414 m->m_pkthdr.len += optlen;
415 plen = m->m_pkthdr.len - sizeof(*ip6);
417 /* If this is a jumbo payload, insert a jumbo payload option. */
418 if (plen > IPV6_MAXPACKET) {
420 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
424 m = exthdrs.ip6e_ip6;
428 ip6 = mtod(m, struct ip6_hdr *);
429 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
433 ip6->ip6_plen = htons(plen);
436 * Concatenate headers and fill in next header fields.
437 * Here we have, on "m"
439 * and we insert headers accordingly. Finally, we should be getting:
440 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
442 * during the header composing process, "m" points to IPv6 header.
443 * "mprev" points to an extension header prior to esp.
445 u_char *nexthdrp = &ip6->ip6_nxt;
449 * we treat dest2 specially. this makes IPsec processing
450 * much easier. the goal here is to make mprev point the
451 * mbuf prior to dest2.
453 * result: IPv6 dest2 payload
454 * m and mprev will point to IPv6 header.
456 if (exthdrs.ip6e_dest2) {
458 panic("assumption failed: hdr not split");
459 exthdrs.ip6e_dest2->m_next = m->m_next;
460 m->m_next = exthdrs.ip6e_dest2;
461 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
462 ip6->ip6_nxt = IPPROTO_DSTOPTS;
466 * result: IPv6 hbh dest1 rthdr dest2 payload
467 * m will point to IPv6 header. mprev will point to the
468 * extension header prior to dest2 (rthdr in the above case).
470 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
471 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
473 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
477 * If there is a routing header, discard the packet.
479 if (exthdrs.ip6e_rthdr) {
484 /* Source address validation */
485 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
486 (flags & IPV6_UNSPECSRC) == 0) {
488 IP6STAT_INC(ip6s_badscope);
491 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
493 IP6STAT_INC(ip6s_badscope);
497 IP6STAT_INC(ip6s_localout);
504 bzero((caddr_t)ro, sizeof(*ro));
507 if (opt && opt->ip6po_rthdr)
508 ro = &opt->ip6po_route;
509 dst = (struct sockaddr_in6 *)&ro->ro_dst;
511 if (ro->ro_rt == NULL)
512 (void )flowtable_lookup(AF_INET6, m, (struct route *)ro);
514 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
517 * if specified, try to fill in the traffic class field.
518 * do not override if a non-zero value is already set.
519 * we check the diffserv field and the ecn field separately.
521 if (opt && opt->ip6po_tclass >= 0) {
524 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
526 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
529 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
532 /* fill in or override the hop limit field, if necessary. */
533 if (opt && opt->ip6po_hlim != -1)
534 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
535 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
537 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
539 ip6->ip6_hlim = V_ip6_defmcasthlim;
543 ip6 = mtod(m, struct ip6_hdr *);
545 if (ro->ro_rt && fwd_tag == NULL) {
547 ifp = ro->ro_rt->rt_ifp;
549 if (fwd_tag == NULL) {
550 bzero(&dst_sa, sizeof(dst_sa));
551 dst_sa.sin6_family = AF_INET6;
552 dst_sa.sin6_len = sizeof(dst_sa);
553 dst_sa.sin6_addr = ip6->ip6_dst;
555 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
559 in6_ifstat_inc(ifp, ifs6_out_discard);
565 * If in6_selectroute() does not return a route entry,
566 * dst may not have been updated.
568 *dst = dst_sa; /* XXX */
572 * then rt (for unicast) and ifp must be non-NULL valid values.
574 if ((flags & IPV6_FORWARDING) == 0) {
575 /* XXX: the FORWARDING flag can be set for mrouting. */
576 in6_ifstat_inc(ifp, ifs6_out_request);
579 ia = (struct in6_ifaddr *)(rt->rt_ifa);
580 counter_u64_add(rt->rt_pksent, 1);
585 * The outgoing interface must be in the zone of source and
586 * destination addresses.
591 if (in6_setscope(&src0, origifp, &zone))
593 bzero(&src_sa, sizeof(src_sa));
594 src_sa.sin6_family = AF_INET6;
595 src_sa.sin6_len = sizeof(src_sa);
596 src_sa.sin6_addr = ip6->ip6_src;
597 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
601 if (in6_setscope(&dst0, origifp, &zone))
603 /* re-initialize to be sure */
604 bzero(&dst_sa, sizeof(dst_sa));
605 dst_sa.sin6_family = AF_INET6;
606 dst_sa.sin6_len = sizeof(dst_sa);
607 dst_sa.sin6_addr = ip6->ip6_dst;
608 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
612 /* We should use ia_ifp to support the case of
613 * sending packets to an address of our own.
615 if (ia != NULL && ia->ia_ifp)
618 /* scope check is done. */
622 IP6STAT_INC(ip6s_badscope);
623 in6_ifstat_inc(origifp, ifs6_out_discard);
625 error = EHOSTUNREACH; /* XXX */
629 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
630 if (opt && opt->ip6po_nextroute.ro_rt) {
632 * The nexthop is explicitly specified by the
633 * application. We assume the next hop is an IPv6
636 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
638 else if ((rt->rt_flags & RTF_GATEWAY))
639 dst = (struct sockaddr_in6 *)rt->rt_gateway;
642 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
643 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
645 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
646 in6_ifstat_inc(ifp, ifs6_out_mcast);
648 * Confirm that the outgoing interface supports multicast.
650 if (!(ifp->if_flags & IFF_MULTICAST)) {
651 IP6STAT_INC(ip6s_noroute);
652 in6_ifstat_inc(ifp, ifs6_out_discard);
656 if ((im6o == NULL && in6_mcast_loop) ||
657 (im6o && im6o->im6o_multicast_loop)) {
659 * Loop back multicast datagram if not expressly
660 * forbidden to do so, even if we have not joined
661 * the address; protocols will filter it later,
662 * thus deferring a hash lookup and lock acquisition
663 * at the expense of an m_copym().
665 ip6_mloopback(ifp, m);
668 * If we are acting as a multicast router, perform
669 * multicast forwarding as if the packet had just
670 * arrived on the interface to which we are about
671 * to send. The multicast forwarding function
672 * recursively calls this function, using the
673 * IPV6_FORWARDING flag to prevent infinite recursion.
675 * Multicasts that are looped back by ip6_mloopback(),
676 * above, will be forwarded by the ip6_input() routine,
679 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
681 * XXX: ip6_mforward expects that rcvif is NULL
682 * when it is called from the originating path.
683 * However, it may not always be the case.
685 m->m_pkthdr.rcvif = NULL;
686 if (ip6_mforward(ip6, ifp, m) != 0) {
693 * Multicasts with a hoplimit of zero may be looped back,
694 * above, but must not be transmitted on a network.
695 * Also, multicasts addressed to the loopback interface
696 * are not sent -- the above call to ip6_mloopback() will
697 * loop back a copy if this host actually belongs to the
698 * destination group on the loopback interface.
700 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
701 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
708 * Fill the outgoing inteface to tell the upper layer
709 * to increment per-interface statistics.
714 /* Determine path MTU. */
715 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
716 &alwaysfrag, fibnum)) != 0)
720 * The caller of this function may specify to use the minimum MTU
722 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
723 * setting. The logic is a bit complicated; by default, unicast
724 * packets will follow path MTU while multicast packets will be sent at
725 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
726 * including unicast ones will be sent at the minimum MTU. Multicast
727 * packets will always be sent at the minimum MTU unless
728 * IP6PO_MINMTU_DISABLE is explicitly specified.
729 * See RFC 3542 for more details.
731 if (mtu > IPV6_MMTU) {
732 if ((flags & IPV6_MINMTU))
734 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
736 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
738 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
744 * clear embedded scope identifiers if necessary.
745 * in6_clearscope will touch the addresses only when necessary.
747 in6_clearscope(&ip6->ip6_src);
748 in6_clearscope(&ip6->ip6_dst);
751 * If the outgoing packet contains a hop-by-hop options header,
752 * it must be examined and processed even by the source node.
753 * (RFC 2460, section 4.)
755 if (exthdrs.ip6e_hbh) {
756 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
757 u_int32_t dummy; /* XXX unused */
758 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
761 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
762 panic("ip6e_hbh is not contiguous");
765 * XXX: if we have to send an ICMPv6 error to the sender,
766 * we need the M_LOOP flag since icmp6_error() expects
767 * the IPv6 and the hop-by-hop options header are
768 * contiguous unless the flag is set.
770 m->m_flags |= M_LOOP;
771 m->m_pkthdr.rcvif = ifp;
772 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
773 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
774 &dummy, &plen) < 0) {
775 /* m was already freed at this point */
776 error = EINVAL;/* better error? */
779 m->m_flags &= ~M_LOOP; /* XXX */
780 m->m_pkthdr.rcvif = NULL;
783 /* Jump over all PFIL processing if hooks are not active. */
784 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
788 /* Run through list of hooks for output packets. */
789 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
790 if (error != 0 || m == NULL)
792 ip6 = mtod(m, struct ip6_hdr *);
795 /* See if destination IP address was changed by packet filter. */
796 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
797 m->m_flags |= M_SKIP_FIREWALL;
798 /* If destination is now ourself drop to ip6_input(). */
799 if (in6_localip(&ip6->ip6_dst)) {
800 m->m_flags |= M_FASTFWD_OURS;
801 if (m->m_pkthdr.rcvif == NULL)
802 m->m_pkthdr.rcvif = V_loif;
803 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
804 m->m_pkthdr.csum_flags |=
805 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
806 m->m_pkthdr.csum_data = 0xffff;
809 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
810 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
812 error = netisr_queue(NETISR_IPV6, m);
815 needfiblookup = 1; /* Redo the routing table lookup. */
817 /* See if fib was changed by packet filter. */
818 if (fibnum != M_GETFIB(m)) {
819 m->m_flags |= M_SKIP_FIREWALL;
820 fibnum = M_GETFIB(m);
827 /* See if local, if yes, send it to netisr. */
828 if (m->m_flags & M_FASTFWD_OURS) {
829 if (m->m_pkthdr.rcvif == NULL)
830 m->m_pkthdr.rcvif = V_loif;
831 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
832 m->m_pkthdr.csum_flags |=
833 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
834 m->m_pkthdr.csum_data = 0xffff;
837 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
838 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
840 error = netisr_queue(NETISR_IPV6, m);
843 /* Or forward to some other address? */
844 if ((m->m_flags & M_IP6_NEXTHOP) &&
845 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
846 dst = (struct sockaddr_in6 *)&ro->ro_dst;
847 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
848 m->m_flags |= M_SKIP_FIREWALL;
849 m->m_flags &= ~M_IP6_NEXTHOP;
850 m_tag_delete(m, fwd_tag);
856 * Send the packet to the outgoing interface.
857 * If necessary, do IPv6 fragmentation before sending.
859 * the logic here is rather complex:
860 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
861 * 1-a: send as is if tlen <= path mtu
862 * 1-b: fragment if tlen > path mtu
864 * 2: if user asks us not to fragment (dontfrag == 1)
865 * 2-a: send as is if tlen <= interface mtu
866 * 2-b: error if tlen > interface mtu
868 * 3: if we always need to attach fragment header (alwaysfrag == 1)
871 * 4: if dontfrag == 1 && alwaysfrag == 1
872 * error, as we cannot handle this conflicting request
874 sw_csum = m->m_pkthdr.csum_flags;
876 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
877 sw_csum &= ~ifp->if_hwassist;
881 * If we added extension headers, we will not do TSO and calculate the
882 * checksums ourselves for now.
883 * XXX-BZ Need a framework to know when the NIC can handle it, even
886 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
887 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
888 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
891 if (sw_csum & CSUM_SCTP_IPV6) {
892 sw_csum &= ~CSUM_SCTP_IPV6;
893 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
896 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
897 tlen = m->m_pkthdr.len;
899 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
903 if (dontfrag && alwaysfrag) { /* case 4 */
904 /* conflicting request - can't transmit */
908 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
910 * Even if the DONTFRAG option is specified, we cannot send the
911 * packet when the data length is larger than the MTU of the
912 * outgoing interface.
913 * Notify the error by sending IPV6_PATHMTU ancillary data if
914 * application wanted to know the MTU value. Also return an
915 * error code (this is not described in the API spec).
918 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
924 * transmit packet without fragmentation
926 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
927 struct in6_ifaddr *ia6;
929 ip6 = mtod(m, struct ip6_hdr *);
930 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
932 /* Record statistics for this interface address. */
933 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
934 counter_u64_add(ia6->ia_ifa.ifa_obytes,
936 ifa_free(&ia6->ia_ifa);
938 error = nd6_output_ifp(ifp, origifp, m, dst);
943 * try to fragment the packet. case 1-b and 3
945 if (mtu < IPV6_MMTU) {
946 /* path MTU cannot be less than IPV6_MMTU */
948 in6_ifstat_inc(ifp, ifs6_out_fragfail);
950 } else if (ip6->ip6_plen == 0) {
951 /* jumbo payload cannot be fragmented */
953 in6_ifstat_inc(ifp, ifs6_out_fragfail);
959 * Too large for the destination or interface;
960 * fragment if possible.
961 * Must be able to put at least 8 bytes per fragment.
963 hlen = unfragpartlen;
964 if (mtu > IPV6_MAXPACKET)
965 mtu = IPV6_MAXPACKET;
967 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
970 in6_ifstat_inc(ifp, ifs6_out_fragfail);
975 * If the interface will not calculate checksums on
976 * fragmented packets, then do it here.
977 * XXX-BZ handle the hw offloading case. Need flags.
979 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
980 in6_delayed_cksum(m, plen, hlen);
981 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
984 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
985 sctp_delayed_cksum(m, hlen);
986 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
990 * Change the next header field of the last header in the
991 * unfragmentable part.
993 if (exthdrs.ip6e_rthdr) {
994 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
995 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
996 } else if (exthdrs.ip6e_dest1) {
997 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
998 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
999 } else if (exthdrs.ip6e_hbh) {
1000 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1001 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1003 nextproto = ip6->ip6_nxt;
1004 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1008 * Loop through length of segment after first fragment,
1009 * make new header and copy data of each part and link onto
1013 id = htonl(ip6_randomid());
1014 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1017 in6_ifstat_inc(ifp, ifs6_out_fragok);
1021 * Remove leading garbages.
1027 for (m0 = m; m; m = m0) {
1031 /* Record statistics for this interface address. */
1033 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1034 counter_u64_add(ia->ia_ifa.ifa_obytes,
1037 error = nd6_output_ifp(ifp, origifp, m, dst);
1043 IP6STAT_INC(ip6s_fragmented);
1046 if (ro == &ip6route)
1048 if (ro_pmtu == &ip6route)
1053 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1054 m_freem(exthdrs.ip6e_dest1);
1055 m_freem(exthdrs.ip6e_rthdr);
1056 m_freem(exthdrs.ip6e_dest2);
1065 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1069 if (hlen > MCLBYTES)
1070 return (ENOBUFS); /* XXX */
1073 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1075 m = m_get(M_NOWAIT, MT_DATA);
1080 bcopy(hdr, mtod(m, caddr_t), hlen);
1087 * Insert jumbo payload option.
1090 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1096 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1099 * If there is no hop-by-hop options header, allocate new one.
1100 * If there is one but it doesn't have enough space to store the
1101 * jumbo payload option, allocate a cluster to store the whole options.
1102 * Otherwise, use it to store the options.
1104 if (exthdrs->ip6e_hbh == 0) {
1105 mopt = m_get(M_NOWAIT, MT_DATA);
1108 mopt->m_len = JUMBOOPTLEN;
1109 optbuf = mtod(mopt, u_char *);
1110 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1111 exthdrs->ip6e_hbh = mopt;
1113 struct ip6_hbh *hbh;
1115 mopt = exthdrs->ip6e_hbh;
1116 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1119 * - exthdrs->ip6e_hbh is not referenced from places
1120 * other than exthdrs.
1121 * - exthdrs->ip6e_hbh is not an mbuf chain.
1123 int oldoptlen = mopt->m_len;
1127 * XXX: give up if the whole (new) hbh header does
1128 * not fit even in an mbuf cluster.
1130 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1134 * As a consequence, we must always prepare a cluster
1137 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1140 n->m_len = oldoptlen + JUMBOOPTLEN;
1141 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1143 optbuf = mtod(n, caddr_t) + oldoptlen;
1145 mopt = exthdrs->ip6e_hbh = n;
1147 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1148 mopt->m_len += JUMBOOPTLEN;
1150 optbuf[0] = IP6OPT_PADN;
1154 * Adjust the header length according to the pad and
1155 * the jumbo payload option.
1157 hbh = mtod(mopt, struct ip6_hbh *);
1158 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1161 /* fill in the option. */
1162 optbuf[2] = IP6OPT_JUMBO;
1164 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1165 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1167 /* finally, adjust the packet header length */
1168 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1175 * Insert fragment header and copy unfragmentable header portions.
1178 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1179 struct ip6_frag **frghdrp)
1181 struct mbuf *n, *mlast;
1183 if (hlen > sizeof(struct ip6_hdr)) {
1184 n = m_copym(m0, sizeof(struct ip6_hdr),
1185 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1192 /* Search for the last mbuf of unfragmentable part. */
1193 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1196 if (M_WRITABLE(mlast) &&
1197 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1198 /* use the trailing space of the last mbuf for the fragment hdr */
1199 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1201 mlast->m_len += sizeof(struct ip6_frag);
1202 m->m_pkthdr.len += sizeof(struct ip6_frag);
1204 /* allocate a new mbuf for the fragment header */
1207 mfrg = m_get(M_NOWAIT, MT_DATA);
1210 mfrg->m_len = sizeof(struct ip6_frag);
1211 *frghdrp = mtod(mfrg, struct ip6_frag *);
1212 mlast->m_next = mfrg;
1219 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1220 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1221 int *alwaysfragp, u_int fibnum)
1227 if (ro_pmtu != ro) {
1228 /* The first hop and the final destination may differ. */
1229 struct sockaddr_in6 *sa6_dst =
1230 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1231 if (ro_pmtu->ro_rt &&
1232 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1233 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1234 RTFREE(ro_pmtu->ro_rt);
1235 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1237 if (ro_pmtu->ro_rt == NULL) {
1238 bzero(sa6_dst, sizeof(*sa6_dst));
1239 sa6_dst->sin6_family = AF_INET6;
1240 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1241 sa6_dst->sin6_addr = *dst;
1243 in6_rtalloc(ro_pmtu, fibnum);
1246 if (ro_pmtu->ro_rt) {
1248 struct in_conninfo inc;
1250 bzero(&inc, sizeof(inc));
1251 inc.inc_flags |= INC_ISIPV6;
1252 inc.inc6_faddr = *dst;
1255 ifp = ro_pmtu->ro_rt->rt_ifp;
1256 ifmtu = IN6_LINKMTU(ifp);
1257 mtu = tcp_hc_getmtu(&inc);
1259 mtu = min(mtu, ro_pmtu->ro_rt->rt_mtu);
1261 mtu = ro_pmtu->ro_rt->rt_mtu;
1264 else if (mtu < IPV6_MMTU) {
1266 * RFC2460 section 5, last paragraph:
1267 * if we record ICMPv6 too big message with
1268 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1269 * or smaller, with framgent header attached.
1270 * (fragment header is needed regardless from the
1271 * packet size, for translators to identify packets)
1277 mtu = IN6_LINKMTU(ifp);
1279 error = EHOSTUNREACH; /* XXX */
1283 *alwaysfragp = alwaysfrag;
1288 * IP6 socket option processing.
1291 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1293 int optdatalen, uproto;
1295 struct inpcb *in6p = sotoinpcb(so);
1297 int level, op, optname;
1301 uint32_t rss_bucket;
1305 level = sopt->sopt_level;
1306 op = sopt->sopt_dir;
1307 optname = sopt->sopt_name;
1308 optlen = sopt->sopt_valsize;
1312 uproto = (int)so->so_proto->pr_protocol;
1314 if (level != IPPROTO_IPV6) {
1317 if (sopt->sopt_level == SOL_SOCKET &&
1318 sopt->sopt_dir == SOPT_SET) {
1319 switch (sopt->sopt_name) {
1322 if ((so->so_options & SO_REUSEADDR) != 0)
1323 in6p->inp_flags2 |= INP_REUSEADDR;
1325 in6p->inp_flags2 &= ~INP_REUSEADDR;
1331 if ((so->so_options & SO_REUSEPORT) != 0)
1332 in6p->inp_flags2 |= INP_REUSEPORT;
1334 in6p->inp_flags2 &= ~INP_REUSEPORT;
1340 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1348 } else { /* level == IPPROTO_IPV6 */
1353 case IPV6_2292PKTOPTIONS:
1354 #ifdef IPV6_PKTOPTIONS
1355 case IPV6_PKTOPTIONS:
1360 error = soopt_getm(sopt, &m); /* XXX */
1363 error = soopt_mcopyin(sopt, m); /* XXX */
1366 error = ip6_pcbopts(&in6p->in6p_outputopts,
1368 m_freem(m); /* XXX */
1373 * Use of some Hop-by-Hop options or some
1374 * Destination options, might require special
1375 * privilege. That is, normal applications
1376 * (without special privilege) might be forbidden
1377 * from setting certain options in outgoing packets,
1378 * and might never see certain options in received
1379 * packets. [RFC 2292 Section 6]
1380 * KAME specific note:
1381 * KAME prevents non-privileged users from sending or
1382 * receiving ANY hbh/dst options in order to avoid
1383 * overhead of parsing options in the kernel.
1385 case IPV6_RECVHOPOPTS:
1386 case IPV6_RECVDSTOPTS:
1387 case IPV6_RECVRTHDRDSTOPTS:
1389 error = priv_check(td,
1390 PRIV_NETINET_SETHDROPTS);
1395 case IPV6_UNICAST_HOPS:
1398 case IPV6_RECVPKTINFO:
1399 case IPV6_RECVHOPLIMIT:
1400 case IPV6_RECVRTHDR:
1401 case IPV6_RECVPATHMTU:
1402 case IPV6_RECVTCLASS:
1403 case IPV6_RECVFLOWID:
1405 case IPV6_RECVRSSBUCKETID:
1408 case IPV6_AUTOFLOWLABEL:
1410 case IPV6_BINDMULTI:
1412 case IPV6_RSS_LISTEN_BUCKET:
1414 if (optname == IPV6_BINDANY && td != NULL) {
1415 error = priv_check(td,
1416 PRIV_NETINET_BINDANY);
1421 if (optlen != sizeof(int)) {
1425 error = sooptcopyin(sopt, &optval,
1426 sizeof optval, sizeof optval);
1431 case IPV6_UNICAST_HOPS:
1432 if (optval < -1 || optval >= 256)
1435 /* -1 = kernel default */
1436 in6p->in6p_hops = optval;
1437 if ((in6p->inp_vflag &
1439 in6p->inp_ip_ttl = optval;
1442 #define OPTSET(bit) \
1446 in6p->inp_flags |= (bit); \
1448 in6p->inp_flags &= ~(bit); \
1449 INP_WUNLOCK(in6p); \
1450 } while (/*CONSTCOND*/ 0)
1451 #define OPTSET2292(bit) \
1454 in6p->inp_flags |= IN6P_RFC2292; \
1456 in6p->inp_flags |= (bit); \
1458 in6p->inp_flags &= ~(bit); \
1459 INP_WUNLOCK(in6p); \
1460 } while (/*CONSTCOND*/ 0)
1461 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1463 #define OPTSET2(bit, val) do { \
1466 in6p->inp_flags2 |= bit; \
1468 in6p->inp_flags2 &= ~bit; \
1469 INP_WUNLOCK(in6p); \
1471 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1473 case IPV6_RECVPKTINFO:
1474 /* cannot mix with RFC2292 */
1475 if (OPTBIT(IN6P_RFC2292)) {
1479 OPTSET(IN6P_PKTINFO);
1484 struct ip6_pktopts **optp;
1486 /* cannot mix with RFC2292 */
1487 if (OPTBIT(IN6P_RFC2292)) {
1491 optp = &in6p->in6p_outputopts;
1492 error = ip6_pcbopt(IPV6_HOPLIMIT,
1493 (u_char *)&optval, sizeof(optval),
1494 optp, (td != NULL) ? td->td_ucred :
1499 case IPV6_RECVHOPLIMIT:
1500 /* cannot mix with RFC2292 */
1501 if (OPTBIT(IN6P_RFC2292)) {
1505 OPTSET(IN6P_HOPLIMIT);
1508 case IPV6_RECVHOPOPTS:
1509 /* cannot mix with RFC2292 */
1510 if (OPTBIT(IN6P_RFC2292)) {
1514 OPTSET(IN6P_HOPOPTS);
1517 case IPV6_RECVDSTOPTS:
1518 /* cannot mix with RFC2292 */
1519 if (OPTBIT(IN6P_RFC2292)) {
1523 OPTSET(IN6P_DSTOPTS);
1526 case IPV6_RECVRTHDRDSTOPTS:
1527 /* cannot mix with RFC2292 */
1528 if (OPTBIT(IN6P_RFC2292)) {
1532 OPTSET(IN6P_RTHDRDSTOPTS);
1535 case IPV6_RECVRTHDR:
1536 /* cannot mix with RFC2292 */
1537 if (OPTBIT(IN6P_RFC2292)) {
1544 case IPV6_RECVPATHMTU:
1546 * We ignore this option for TCP
1548 * (RFC3542 leaves this case
1551 if (uproto != IPPROTO_TCP)
1555 case IPV6_RECVFLOWID:
1556 OPTSET2(INP_RECVFLOWID, optval);
1560 case IPV6_RECVRSSBUCKETID:
1561 OPTSET2(INP_RECVRSSBUCKETID, optval);
1567 * make setsockopt(IPV6_V6ONLY)
1568 * available only prior to bind(2).
1569 * see ipng mailing list, Jun 22 2001.
1571 if (in6p->inp_lport ||
1572 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1576 OPTSET(IN6P_IPV6_V6ONLY);
1578 in6p->inp_vflag &= ~INP_IPV4;
1580 in6p->inp_vflag |= INP_IPV4;
1582 case IPV6_RECVTCLASS:
1583 /* cannot mix with RFC2292 XXX */
1584 if (OPTBIT(IN6P_RFC2292)) {
1588 OPTSET(IN6P_TCLASS);
1590 case IPV6_AUTOFLOWLABEL:
1591 OPTSET(IN6P_AUTOFLOWLABEL);
1595 OPTSET(INP_BINDANY);
1598 case IPV6_BINDMULTI:
1599 OPTSET2(INP_BINDMULTI, optval);
1602 case IPV6_RSS_LISTEN_BUCKET:
1603 if ((optval >= 0) &&
1604 (optval < rss_getnumbuckets())) {
1605 in6p->inp_rss_listen_bucket = optval;
1606 OPTSET2(INP_RSS_BUCKET_SET, 1);
1617 case IPV6_USE_MIN_MTU:
1618 case IPV6_PREFER_TEMPADDR:
1619 if (optlen != sizeof(optval)) {
1623 error = sooptcopyin(sopt, &optval,
1624 sizeof optval, sizeof optval);
1628 struct ip6_pktopts **optp;
1629 optp = &in6p->in6p_outputopts;
1630 error = ip6_pcbopt(optname,
1631 (u_char *)&optval, sizeof(optval),
1632 optp, (td != NULL) ? td->td_ucred :
1637 case IPV6_2292PKTINFO:
1638 case IPV6_2292HOPLIMIT:
1639 case IPV6_2292HOPOPTS:
1640 case IPV6_2292DSTOPTS:
1641 case IPV6_2292RTHDR:
1643 if (optlen != sizeof(int)) {
1647 error = sooptcopyin(sopt, &optval,
1648 sizeof optval, sizeof optval);
1652 case IPV6_2292PKTINFO:
1653 OPTSET2292(IN6P_PKTINFO);
1655 case IPV6_2292HOPLIMIT:
1656 OPTSET2292(IN6P_HOPLIMIT);
1658 case IPV6_2292HOPOPTS:
1660 * Check super-user privilege.
1661 * See comments for IPV6_RECVHOPOPTS.
1664 error = priv_check(td,
1665 PRIV_NETINET_SETHDROPTS);
1669 OPTSET2292(IN6P_HOPOPTS);
1671 case IPV6_2292DSTOPTS:
1673 error = priv_check(td,
1674 PRIV_NETINET_SETHDROPTS);
1678 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1680 case IPV6_2292RTHDR:
1681 OPTSET2292(IN6P_RTHDR);
1689 case IPV6_RTHDRDSTOPTS:
1692 /* new advanced API (RFC3542) */
1694 u_char optbuf_storage[MCLBYTES];
1696 struct ip6_pktopts **optp;
1698 /* cannot mix with RFC2292 */
1699 if (OPTBIT(IN6P_RFC2292)) {
1705 * We only ensure valsize is not too large
1706 * here. Further validation will be done
1709 error = sooptcopyin(sopt, optbuf_storage,
1710 sizeof(optbuf_storage), 0);
1713 optlen = sopt->sopt_valsize;
1714 optbuf = optbuf_storage;
1715 optp = &in6p->in6p_outputopts;
1716 error = ip6_pcbopt(optname, optbuf, optlen,
1717 optp, (td != NULL) ? td->td_ucred : NULL,
1723 case IPV6_MULTICAST_IF:
1724 case IPV6_MULTICAST_HOPS:
1725 case IPV6_MULTICAST_LOOP:
1726 case IPV6_JOIN_GROUP:
1727 case IPV6_LEAVE_GROUP:
1729 case MCAST_BLOCK_SOURCE:
1730 case MCAST_UNBLOCK_SOURCE:
1731 case MCAST_JOIN_GROUP:
1732 case MCAST_LEAVE_GROUP:
1733 case MCAST_JOIN_SOURCE_GROUP:
1734 case MCAST_LEAVE_SOURCE_GROUP:
1735 error = ip6_setmoptions(in6p, sopt);
1738 case IPV6_PORTRANGE:
1739 error = sooptcopyin(sopt, &optval,
1740 sizeof optval, sizeof optval);
1746 case IPV6_PORTRANGE_DEFAULT:
1747 in6p->inp_flags &= ~(INP_LOWPORT);
1748 in6p->inp_flags &= ~(INP_HIGHPORT);
1751 case IPV6_PORTRANGE_HIGH:
1752 in6p->inp_flags &= ~(INP_LOWPORT);
1753 in6p->inp_flags |= INP_HIGHPORT;
1756 case IPV6_PORTRANGE_LOW:
1757 in6p->inp_flags &= ~(INP_HIGHPORT);
1758 in6p->inp_flags |= INP_LOWPORT;
1769 case IPV6_IPSEC_POLICY:
1774 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1776 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1778 req = mtod(m, caddr_t);
1779 error = ipsec_set_policy(in6p, optname, req,
1780 m->m_len, (sopt->sopt_td != NULL) ?
1781 sopt->sopt_td->td_ucred : NULL);
1788 error = ENOPROTOOPT;
1796 case IPV6_2292PKTOPTIONS:
1797 #ifdef IPV6_PKTOPTIONS
1798 case IPV6_PKTOPTIONS:
1801 * RFC3542 (effectively) deprecated the
1802 * semantics of the 2292-style pktoptions.
1803 * Since it was not reliable in nature (i.e.,
1804 * applications had to expect the lack of some
1805 * information after all), it would make sense
1806 * to simplify this part by always returning
1809 sopt->sopt_valsize = 0;
1812 case IPV6_RECVHOPOPTS:
1813 case IPV6_RECVDSTOPTS:
1814 case IPV6_RECVRTHDRDSTOPTS:
1815 case IPV6_UNICAST_HOPS:
1816 case IPV6_RECVPKTINFO:
1817 case IPV6_RECVHOPLIMIT:
1818 case IPV6_RECVRTHDR:
1819 case IPV6_RECVPATHMTU:
1822 case IPV6_PORTRANGE:
1823 case IPV6_RECVTCLASS:
1824 case IPV6_AUTOFLOWLABEL:
1828 case IPV6_RECVFLOWID:
1830 case IPV6_RSSBUCKETID:
1831 case IPV6_RECVRSSBUCKETID:
1835 case IPV6_RECVHOPOPTS:
1836 optval = OPTBIT(IN6P_HOPOPTS);
1839 case IPV6_RECVDSTOPTS:
1840 optval = OPTBIT(IN6P_DSTOPTS);
1843 case IPV6_RECVRTHDRDSTOPTS:
1844 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1847 case IPV6_UNICAST_HOPS:
1848 optval = in6p->in6p_hops;
1851 case IPV6_RECVPKTINFO:
1852 optval = OPTBIT(IN6P_PKTINFO);
1855 case IPV6_RECVHOPLIMIT:
1856 optval = OPTBIT(IN6P_HOPLIMIT);
1859 case IPV6_RECVRTHDR:
1860 optval = OPTBIT(IN6P_RTHDR);
1863 case IPV6_RECVPATHMTU:
1864 optval = OPTBIT(IN6P_MTU);
1868 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1871 case IPV6_PORTRANGE:
1874 flags = in6p->inp_flags;
1875 if (flags & INP_HIGHPORT)
1876 optval = IPV6_PORTRANGE_HIGH;
1877 else if (flags & INP_LOWPORT)
1878 optval = IPV6_PORTRANGE_LOW;
1883 case IPV6_RECVTCLASS:
1884 optval = OPTBIT(IN6P_TCLASS);
1887 case IPV6_AUTOFLOWLABEL:
1888 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1892 optval = OPTBIT(INP_BINDANY);
1896 optval = in6p->inp_flowid;
1900 optval = in6p->inp_flowtype;
1903 case IPV6_RECVFLOWID:
1904 optval = OPTBIT2(INP_RECVFLOWID);
1907 case IPV6_RSSBUCKETID:
1909 rss_hash2bucket(in6p->inp_flowid,
1913 optval = rss_bucket;
1918 case IPV6_RECVRSSBUCKETID:
1919 optval = OPTBIT2(INP_RECVRSSBUCKETID);
1923 case IPV6_BINDMULTI:
1924 optval = OPTBIT2(INP_BINDMULTI);
1930 error = sooptcopyout(sopt, &optval,
1937 struct ip6_mtuinfo mtuinfo;
1938 struct route_in6 sro;
1940 bzero(&sro, sizeof(sro));
1942 if (!(so->so_state & SS_ISCONNECTED))
1945 * XXX: we dot not consider the case of source
1946 * routing, or optional information to specify
1947 * the outgoing interface.
1949 error = ip6_getpmtu(&sro, NULL, NULL,
1950 &in6p->in6p_faddr, &pmtu, NULL,
1956 if (pmtu > IPV6_MAXPACKET)
1957 pmtu = IPV6_MAXPACKET;
1959 bzero(&mtuinfo, sizeof(mtuinfo));
1960 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1961 optdata = (void *)&mtuinfo;
1962 optdatalen = sizeof(mtuinfo);
1963 error = sooptcopyout(sopt, optdata,
1968 case IPV6_2292PKTINFO:
1969 case IPV6_2292HOPLIMIT:
1970 case IPV6_2292HOPOPTS:
1971 case IPV6_2292RTHDR:
1972 case IPV6_2292DSTOPTS:
1974 case IPV6_2292PKTINFO:
1975 optval = OPTBIT(IN6P_PKTINFO);
1977 case IPV6_2292HOPLIMIT:
1978 optval = OPTBIT(IN6P_HOPLIMIT);
1980 case IPV6_2292HOPOPTS:
1981 optval = OPTBIT(IN6P_HOPOPTS);
1983 case IPV6_2292RTHDR:
1984 optval = OPTBIT(IN6P_RTHDR);
1986 case IPV6_2292DSTOPTS:
1987 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1990 error = sooptcopyout(sopt, &optval,
1997 case IPV6_RTHDRDSTOPTS:
2001 case IPV6_USE_MIN_MTU:
2002 case IPV6_PREFER_TEMPADDR:
2003 error = ip6_getpcbopt(in6p->in6p_outputopts,
2007 case IPV6_MULTICAST_IF:
2008 case IPV6_MULTICAST_HOPS:
2009 case IPV6_MULTICAST_LOOP:
2011 error = ip6_getmoptions(in6p, sopt);
2015 case IPV6_IPSEC_POLICY:
2019 struct mbuf *m = NULL;
2020 struct mbuf **mp = &m;
2021 size_t ovalsize = sopt->sopt_valsize;
2022 caddr_t oval = (caddr_t)sopt->sopt_val;
2024 error = soopt_getm(sopt, &m); /* XXX */
2027 error = soopt_mcopyin(sopt, m); /* XXX */
2030 sopt->sopt_valsize = ovalsize;
2031 sopt->sopt_val = oval;
2033 req = mtod(m, caddr_t);
2036 error = ipsec_get_policy(in6p, req, len, mp);
2038 error = soopt_mcopyout(sopt, m); /* XXX */
2039 if (error == 0 && m)
2046 error = ENOPROTOOPT;
2056 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2058 int error = 0, optval, optlen;
2059 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2060 struct inpcb *in6p = sotoinpcb(so);
2061 int level, op, optname;
2063 level = sopt->sopt_level;
2064 op = sopt->sopt_dir;
2065 optname = sopt->sopt_name;
2066 optlen = sopt->sopt_valsize;
2068 if (level != IPPROTO_IPV6) {
2075 * For ICMPv6 sockets, no modification allowed for checksum
2076 * offset, permit "no change" values to help existing apps.
2078 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2079 * for an ICMPv6 socket will fail."
2080 * The current behavior does not meet RFC3542.
2084 if (optlen != sizeof(int)) {
2088 error = sooptcopyin(sopt, &optval, sizeof(optval),
2092 if ((optval % 2) != 0) {
2093 /* the API assumes even offset values */
2095 } else if (so->so_proto->pr_protocol ==
2097 if (optval != icmp6off)
2100 in6p->in6p_cksum = optval;
2104 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2107 optval = in6p->in6p_cksum;
2109 error = sooptcopyout(sopt, &optval, sizeof(optval));
2119 error = ENOPROTOOPT;
2127 * Set up IP6 options in pcb for insertion in output packets or
2128 * specifying behavior of outgoing packets.
2131 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2132 struct socket *so, struct sockopt *sopt)
2134 struct ip6_pktopts *opt = *pktopt;
2136 struct thread *td = sopt->sopt_td;
2138 /* turn off any old options. */
2141 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2142 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2143 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2144 printf("ip6_pcbopts: all specified options are cleared.\n");
2146 ip6_clearpktopts(opt, -1);
2148 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2151 if (!m || m->m_len == 0) {
2153 * Only turning off any previous options, regardless of
2154 * whether the opt is just created or given.
2156 free(opt, M_IP6OPT);
2160 /* set options specified by user. */
2161 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2162 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2163 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2164 free(opt, M_IP6OPT);
2172 * initialize ip6_pktopts. beware that there are non-zero default values in
2176 ip6_initpktopts(struct ip6_pktopts *opt)
2179 bzero(opt, sizeof(*opt));
2180 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2181 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2182 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2183 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2187 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2188 struct ucred *cred, int uproto)
2190 struct ip6_pktopts *opt;
2192 if (*pktopt == NULL) {
2193 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2195 ip6_initpktopts(*pktopt);
2199 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2203 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2205 void *optdata = NULL;
2207 struct ip6_ext *ip6e;
2209 struct in6_pktinfo null_pktinfo;
2210 int deftclass = 0, on;
2211 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2212 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2216 optdata = (void *)&null_pktinfo;
2217 if (pktopt && pktopt->ip6po_pktinfo) {
2218 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2219 sizeof(null_pktinfo));
2220 in6_clearscope(&null_pktinfo.ipi6_addr);
2222 /* XXX: we don't have to do this every time... */
2223 bzero(&null_pktinfo, sizeof(null_pktinfo));
2225 optdatalen = sizeof(struct in6_pktinfo);
2228 if (pktopt && pktopt->ip6po_tclass >= 0)
2229 optdata = (void *)&pktopt->ip6po_tclass;
2231 optdata = (void *)&deftclass;
2232 optdatalen = sizeof(int);
2235 if (pktopt && pktopt->ip6po_hbh) {
2236 optdata = (void *)pktopt->ip6po_hbh;
2237 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2238 optdatalen = (ip6e->ip6e_len + 1) << 3;
2242 if (pktopt && pktopt->ip6po_rthdr) {
2243 optdata = (void *)pktopt->ip6po_rthdr;
2244 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2245 optdatalen = (ip6e->ip6e_len + 1) << 3;
2248 case IPV6_RTHDRDSTOPTS:
2249 if (pktopt && pktopt->ip6po_dest1) {
2250 optdata = (void *)pktopt->ip6po_dest1;
2251 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2252 optdatalen = (ip6e->ip6e_len + 1) << 3;
2256 if (pktopt && pktopt->ip6po_dest2) {
2257 optdata = (void *)pktopt->ip6po_dest2;
2258 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2259 optdatalen = (ip6e->ip6e_len + 1) << 3;
2263 if (pktopt && pktopt->ip6po_nexthop) {
2264 optdata = (void *)pktopt->ip6po_nexthop;
2265 optdatalen = pktopt->ip6po_nexthop->sa_len;
2268 case IPV6_USE_MIN_MTU:
2270 optdata = (void *)&pktopt->ip6po_minmtu;
2272 optdata = (void *)&defminmtu;
2273 optdatalen = sizeof(int);
2276 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2280 optdata = (void *)&on;
2281 optdatalen = sizeof(on);
2283 case IPV6_PREFER_TEMPADDR:
2285 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2287 optdata = (void *)&defpreftemp;
2288 optdatalen = sizeof(int);
2290 default: /* should not happen */
2292 panic("ip6_getpcbopt: unexpected option\n");
2294 return (ENOPROTOOPT);
2297 error = sooptcopyout(sopt, optdata, optdatalen);
2303 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2308 if (optname == -1 || optname == IPV6_PKTINFO) {
2309 if (pktopt->ip6po_pktinfo)
2310 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2311 pktopt->ip6po_pktinfo = NULL;
2313 if (optname == -1 || optname == IPV6_HOPLIMIT)
2314 pktopt->ip6po_hlim = -1;
2315 if (optname == -1 || optname == IPV6_TCLASS)
2316 pktopt->ip6po_tclass = -1;
2317 if (optname == -1 || optname == IPV6_NEXTHOP) {
2318 if (pktopt->ip6po_nextroute.ro_rt) {
2319 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2320 pktopt->ip6po_nextroute.ro_rt = NULL;
2322 if (pktopt->ip6po_nexthop)
2323 free(pktopt->ip6po_nexthop, M_IP6OPT);
2324 pktopt->ip6po_nexthop = NULL;
2326 if (optname == -1 || optname == IPV6_HOPOPTS) {
2327 if (pktopt->ip6po_hbh)
2328 free(pktopt->ip6po_hbh, M_IP6OPT);
2329 pktopt->ip6po_hbh = NULL;
2331 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2332 if (pktopt->ip6po_dest1)
2333 free(pktopt->ip6po_dest1, M_IP6OPT);
2334 pktopt->ip6po_dest1 = NULL;
2336 if (optname == -1 || optname == IPV6_RTHDR) {
2337 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2338 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2339 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2340 if (pktopt->ip6po_route.ro_rt) {
2341 RTFREE(pktopt->ip6po_route.ro_rt);
2342 pktopt->ip6po_route.ro_rt = NULL;
2345 if (optname == -1 || optname == IPV6_DSTOPTS) {
2346 if (pktopt->ip6po_dest2)
2347 free(pktopt->ip6po_dest2, M_IP6OPT);
2348 pktopt->ip6po_dest2 = NULL;
2352 #define PKTOPT_EXTHDRCPY(type) \
2355 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2356 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2357 if (dst->type == NULL && canwait == M_NOWAIT)\
2359 bcopy(src->type, dst->type, hlen);\
2361 } while (/*CONSTCOND*/ 0)
2364 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2366 if (dst == NULL || src == NULL) {
2367 printf("ip6_clearpktopts: invalid argument\n");
2371 dst->ip6po_hlim = src->ip6po_hlim;
2372 dst->ip6po_tclass = src->ip6po_tclass;
2373 dst->ip6po_flags = src->ip6po_flags;
2374 dst->ip6po_minmtu = src->ip6po_minmtu;
2375 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2376 if (src->ip6po_pktinfo) {
2377 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2379 if (dst->ip6po_pktinfo == NULL)
2381 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2383 if (src->ip6po_nexthop) {
2384 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2386 if (dst->ip6po_nexthop == NULL)
2388 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2389 src->ip6po_nexthop->sa_len);
2391 PKTOPT_EXTHDRCPY(ip6po_hbh);
2392 PKTOPT_EXTHDRCPY(ip6po_dest1);
2393 PKTOPT_EXTHDRCPY(ip6po_dest2);
2394 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2398 ip6_clearpktopts(dst, -1);
2401 #undef PKTOPT_EXTHDRCPY
2403 struct ip6_pktopts *
2404 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2407 struct ip6_pktopts *dst;
2409 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2412 ip6_initpktopts(dst);
2414 if ((error = copypktopts(dst, src, canwait)) != 0) {
2415 free(dst, M_IP6OPT);
2423 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2428 ip6_clearpktopts(pktopt, -1);
2430 free(pktopt, M_IP6OPT);
2434 * Set IPv6 outgoing packet options based on advanced API.
2437 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2438 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2440 struct cmsghdr *cm = 0;
2442 if (control == NULL || opt == NULL)
2445 ip6_initpktopts(opt);
2450 * If stickyopt is provided, make a local copy of the options
2451 * for this particular packet, then override them by ancillary
2453 * XXX: copypktopts() does not copy the cached route to a next
2454 * hop (if any). This is not very good in terms of efficiency,
2455 * but we can allow this since this option should be rarely
2458 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2463 * XXX: Currently, we assume all the optional information is stored
2466 if (control->m_next)
2469 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2470 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2473 if (control->m_len < CMSG_LEN(0))
2476 cm = mtod(control, struct cmsghdr *);
2477 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2479 if (cm->cmsg_level != IPPROTO_IPV6)
2482 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2483 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2492 * Set a particular packet option, as a sticky option or an ancillary data
2493 * item. "len" can be 0 only when it's a sticky option.
2494 * We have 4 cases of combination of "sticky" and "cmsg":
2495 * "sticky=0, cmsg=0": impossible
2496 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2497 * "sticky=1, cmsg=0": RFC3542 socket option
2498 * "sticky=1, cmsg=1": RFC2292 socket option
2501 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2502 struct ucred *cred, int sticky, int cmsg, int uproto)
2504 int minmtupolicy, preftemp;
2507 if (!sticky && !cmsg) {
2509 printf("ip6_setpktopt: impossible case\n");
2515 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2516 * not be specified in the context of RFC3542. Conversely,
2517 * RFC3542 types should not be specified in the context of RFC2292.
2521 case IPV6_2292PKTINFO:
2522 case IPV6_2292HOPLIMIT:
2523 case IPV6_2292NEXTHOP:
2524 case IPV6_2292HOPOPTS:
2525 case IPV6_2292DSTOPTS:
2526 case IPV6_2292RTHDR:
2527 case IPV6_2292PKTOPTIONS:
2528 return (ENOPROTOOPT);
2531 if (sticky && cmsg) {
2538 case IPV6_RTHDRDSTOPTS:
2540 case IPV6_USE_MIN_MTU:
2543 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2544 return (ENOPROTOOPT);
2549 case IPV6_2292PKTINFO:
2552 struct ifnet *ifp = NULL;
2553 struct in6_pktinfo *pktinfo;
2555 if (len != sizeof(struct in6_pktinfo))
2558 pktinfo = (struct in6_pktinfo *)buf;
2561 * An application can clear any sticky IPV6_PKTINFO option by
2562 * doing a "regular" setsockopt with ipi6_addr being
2563 * in6addr_any and ipi6_ifindex being zero.
2564 * [RFC 3542, Section 6]
2566 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2567 pktinfo->ipi6_ifindex == 0 &&
2568 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2569 ip6_clearpktopts(opt, optname);
2573 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2574 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2577 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2579 /* validate the interface index if specified. */
2580 if (pktinfo->ipi6_ifindex > V_if_index)
2582 if (pktinfo->ipi6_ifindex) {
2583 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2587 if (ifp != NULL && (
2588 ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED))
2592 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2593 struct in6_ifaddr *ia;
2595 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2596 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2598 return (EADDRNOTAVAIL);
2599 ifa_free(&ia->ia_ifa);
2602 * We store the address anyway, and let in6_selectsrc()
2603 * validate the specified address. This is because ipi6_addr
2604 * may not have enough information about its scope zone, and
2605 * we may need additional information (such as outgoing
2606 * interface or the scope zone of a destination address) to
2607 * disambiguate the scope.
2608 * XXX: the delay of the validation may confuse the
2609 * application when it is used as a sticky option.
2611 if (opt->ip6po_pktinfo == NULL) {
2612 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2613 M_IP6OPT, M_NOWAIT);
2614 if (opt->ip6po_pktinfo == NULL)
2617 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2621 case IPV6_2292HOPLIMIT:
2627 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2628 * to simplify the ordering among hoplimit options.
2630 if (optname == IPV6_HOPLIMIT && sticky)
2631 return (ENOPROTOOPT);
2633 if (len != sizeof(int))
2636 if (*hlimp < -1 || *hlimp > 255)
2639 opt->ip6po_hlim = *hlimp;
2647 if (len != sizeof(int))
2649 tclass = *(int *)buf;
2650 if (tclass < -1 || tclass > 255)
2653 opt->ip6po_tclass = tclass;
2657 case IPV6_2292NEXTHOP:
2660 error = priv_check_cred(cred,
2661 PRIV_NETINET_SETHDROPTS, 0);
2666 if (len == 0) { /* just remove the option */
2667 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2671 /* check if cmsg_len is large enough for sa_len */
2672 if (len < sizeof(struct sockaddr) || len < *buf)
2675 switch (((struct sockaddr *)buf)->sa_family) {
2678 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2681 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2684 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2685 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2688 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2694 case AF_LINK: /* should eventually be supported */
2696 return (EAFNOSUPPORT);
2699 /* turn off the previous option, then set the new option. */
2700 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2701 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2702 if (opt->ip6po_nexthop == NULL)
2704 bcopy(buf, opt->ip6po_nexthop, *buf);
2707 case IPV6_2292HOPOPTS:
2710 struct ip6_hbh *hbh;
2714 * XXX: We don't allow a non-privileged user to set ANY HbH
2715 * options, since per-option restriction has too much
2719 error = priv_check_cred(cred,
2720 PRIV_NETINET_SETHDROPTS, 0);
2726 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2727 break; /* just remove the option */
2730 /* message length validation */
2731 if (len < sizeof(struct ip6_hbh))
2733 hbh = (struct ip6_hbh *)buf;
2734 hbhlen = (hbh->ip6h_len + 1) << 3;
2738 /* turn off the previous option, then set the new option. */
2739 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2740 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2741 if (opt->ip6po_hbh == NULL)
2743 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2748 case IPV6_2292DSTOPTS:
2750 case IPV6_RTHDRDSTOPTS:
2752 struct ip6_dest *dest, **newdest = NULL;
2755 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2756 error = priv_check_cred(cred,
2757 PRIV_NETINET_SETHDROPTS, 0);
2763 ip6_clearpktopts(opt, optname);
2764 break; /* just remove the option */
2767 /* message length validation */
2768 if (len < sizeof(struct ip6_dest))
2770 dest = (struct ip6_dest *)buf;
2771 destlen = (dest->ip6d_len + 1) << 3;
2776 * Determine the position that the destination options header
2777 * should be inserted; before or after the routing header.
2780 case IPV6_2292DSTOPTS:
2782 * The old advacned API is ambiguous on this point.
2783 * Our approach is to determine the position based
2784 * according to the existence of a routing header.
2785 * Note, however, that this depends on the order of the
2786 * extension headers in the ancillary data; the 1st
2787 * part of the destination options header must appear
2788 * before the routing header in the ancillary data,
2790 * RFC3542 solved the ambiguity by introducing
2791 * separate ancillary data or option types.
2793 if (opt->ip6po_rthdr == NULL)
2794 newdest = &opt->ip6po_dest1;
2796 newdest = &opt->ip6po_dest2;
2798 case IPV6_RTHDRDSTOPTS:
2799 newdest = &opt->ip6po_dest1;
2802 newdest = &opt->ip6po_dest2;
2806 /* turn off the previous option, then set the new option. */
2807 ip6_clearpktopts(opt, optname);
2808 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2809 if (*newdest == NULL)
2811 bcopy(dest, *newdest, destlen);
2816 case IPV6_2292RTHDR:
2819 struct ip6_rthdr *rth;
2823 ip6_clearpktopts(opt, IPV6_RTHDR);
2824 break; /* just remove the option */
2827 /* message length validation */
2828 if (len < sizeof(struct ip6_rthdr))
2830 rth = (struct ip6_rthdr *)buf;
2831 rthlen = (rth->ip6r_len + 1) << 3;
2835 switch (rth->ip6r_type) {
2836 case IPV6_RTHDR_TYPE_0:
2837 if (rth->ip6r_len == 0) /* must contain one addr */
2839 if (rth->ip6r_len % 2) /* length must be even */
2841 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2845 return (EINVAL); /* not supported */
2848 /* turn off the previous option */
2849 ip6_clearpktopts(opt, IPV6_RTHDR);
2850 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2851 if (opt->ip6po_rthdr == NULL)
2853 bcopy(rth, opt->ip6po_rthdr, rthlen);
2858 case IPV6_USE_MIN_MTU:
2859 if (len != sizeof(int))
2861 minmtupolicy = *(int *)buf;
2862 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2863 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2864 minmtupolicy != IP6PO_MINMTU_ALL) {
2867 opt->ip6po_minmtu = minmtupolicy;
2871 if (len != sizeof(int))
2874 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2876 * we ignore this option for TCP sockets.
2877 * (RFC3542 leaves this case unspecified.)
2879 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2881 opt->ip6po_flags |= IP6PO_DONTFRAG;
2884 case IPV6_PREFER_TEMPADDR:
2885 if (len != sizeof(int))
2887 preftemp = *(int *)buf;
2888 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2889 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2890 preftemp != IP6PO_TEMPADDR_PREFER) {
2893 opt->ip6po_prefer_tempaddr = preftemp;
2897 return (ENOPROTOOPT);
2898 } /* end of switch */
2904 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2905 * packet to the input queue of a specified interface. Note that this
2906 * calls the output routine of the loopback "driver", but with an interface
2907 * pointer that might NOT be &loif -- easier than replicating that code here.
2910 ip6_mloopback(struct ifnet *ifp, const struct mbuf *m)
2913 struct ip6_hdr *ip6;
2915 copym = m_copy(m, 0, M_COPYALL);
2920 * Make sure to deep-copy IPv6 header portion in case the data
2921 * is in an mbuf cluster, so that we can safely override the IPv6
2922 * header portion later.
2924 if (!M_WRITABLE(copym) ||
2925 copym->m_len < sizeof(struct ip6_hdr)) {
2926 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2930 ip6 = mtod(copym, struct ip6_hdr *);
2932 * clear embedded scope identifiers if necessary.
2933 * in6_clearscope will touch the addresses only when necessary.
2935 in6_clearscope(&ip6->ip6_src);
2936 in6_clearscope(&ip6->ip6_dst);
2937 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
2938 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
2940 copym->m_pkthdr.csum_data = 0xffff;
2942 if_simloop(ifp, copym, AF_INET6, 0);
2946 * Chop IPv6 header off from the payload.
2949 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
2952 struct ip6_hdr *ip6;
2954 ip6 = mtod(m, struct ip6_hdr *);
2955 if (m->m_len > sizeof(*ip6)) {
2956 mh = m_gethdr(M_NOWAIT, MT_DATA);
2961 m_move_pkthdr(mh, m);
2962 M_ALIGN(mh, sizeof(*ip6));
2963 m->m_len -= sizeof(*ip6);
2964 m->m_data += sizeof(*ip6);
2967 m->m_len = sizeof(*ip6);
2968 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2970 exthdrs->ip6e_ip6 = m;
2975 * Compute IPv6 extension header length.
2978 ip6_optlen(struct inpcb *in6p)
2982 if (!in6p->in6p_outputopts)
2987 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2989 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2990 if (in6p->in6p_outputopts->ip6po_rthdr)
2991 /* dest1 is valid with rthdr only */
2992 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2993 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2994 len += elen(in6p->in6p_outputopts->ip6po_dest2);