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>
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <netinet/ip_var.h>
99 #include <netinet6/in6_var.h>
100 #include <netinet/ip6.h>
101 #include <netinet/icmp6.h>
102 #include <netinet6/ip6_var.h>
103 #include <netinet/in_pcb.h>
104 #include <netinet/tcp_var.h>
105 #include <netinet6/nd6.h>
106 #include <netinet/in_rss.h>
109 #include <netipsec/ipsec.h>
110 #include <netipsec/ipsec6.h>
111 #include <netipsec/key.h>
112 #include <netinet6/ip6_ipsec.h>
115 #include <netinet/sctp.h>
116 #include <netinet/sctp_crc32.h>
119 #include <netinet6/ip6protosw.h>
120 #include <netinet6/scope6_var.h>
123 #include <net/flowtable.h>
126 extern int in6_mcast_loop;
129 struct mbuf *ip6e_ip6;
130 struct mbuf *ip6e_hbh;
131 struct mbuf *ip6e_dest1;
132 struct mbuf *ip6e_rthdr;
133 struct mbuf *ip6e_dest2;
136 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
137 struct ucred *, int);
138 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
139 struct socket *, struct sockopt *);
140 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
141 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
142 struct ucred *, int, int, int);
144 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
145 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
147 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
148 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
149 static int ip6_getpmtu(struct route_in6 *, struct route_in6 *,
150 struct ifnet *, struct in6_addr *, u_long *, int *, u_int);
151 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
155 * Make an extension header from option data. hp is the source, and
156 * mp is the destination.
158 #define MAKE_EXTHDR(hp, mp) \
161 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
162 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
163 ((eh)->ip6e_len + 1) << 3); \
167 } while (/*CONSTCOND*/ 0)
170 * Form a chain of extension headers.
171 * m is the extension header mbuf
172 * mp is the previous mbuf in the chain
173 * p is the next header
174 * i is the type of option.
176 #define MAKE_CHAIN(m, mp, p, i)\
180 panic("assumption failed: hdr not split"); \
181 *mtod((m), u_char *) = *(p);\
183 p = mtod((m), u_char *);\
184 (m)->m_next = (mp)->m_next;\
188 } while (/*CONSTCOND*/ 0)
191 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
195 csum = in_cksum_skip(m, offset + plen, offset);
196 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
198 offset += m->m_pkthdr.csum_data; /* checksum offset */
200 if (offset + sizeof(u_short) > m->m_len) {
201 printf("%s: delayed m_pullup, m->len: %d plen %u off %u "
202 "csum_flags=%b\n", __func__, m->m_len, plen, offset,
203 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
205 * XXX this should not happen, but if it does, the correct
206 * behavior may be to insert the checksum in the appropriate
207 * next mbuf in the chain.
211 *(u_short *)(m->m_data + offset) = csum;
215 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
216 * header (with pri, len, nxt, hlim, src, dst).
217 * This function may modify ver and hlim only.
218 * The mbuf chain containing the packet will be freed.
219 * The mbuf opt, if present, will not be freed.
220 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
221 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
222 * then result of route lookup is stored in ro->ro_rt.
224 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
225 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
228 * ifpp - XXX: just for statistics
231 * XXX TODO: no flowid is assigned for outbound flows?
234 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
235 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
236 struct ifnet **ifpp, struct inpcb *inp)
238 struct ip6_hdr *ip6, *mhip6;
239 struct ifnet *ifp, *origifp;
241 struct mbuf *mprev = NULL;
242 int hlen, tlen, len, off;
243 struct route_in6 ip6route;
244 struct rtentry *rt = NULL;
245 struct sockaddr_in6 *dst, src_sa, dst_sa;
246 struct in6_addr odst;
248 struct in6_ifaddr *ia = NULL;
250 int alwaysfrag, dontfrag;
251 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
252 struct ip6_exthdrs exthdrs;
253 struct in6_addr finaldst, src0, dst0;
255 struct route_in6 *ro_pmtu = NULL;
260 struct m_tag *fwd_tag = NULL;
262 ip6 = mtod(m, struct ip6_hdr *);
264 printf ("ip6 is NULL");
269 M_SETFIB(m, inp->inp_inc.inc_fibnum);
270 if (((flags & IP_NODEFAULTFLOWID) == 0) &&
271 (inp->inp_flags & (INP_HW_FLOWID|INP_SW_FLOWID))) {
272 m->m_pkthdr.flowid = inp->inp_flowid;
273 m->m_flags |= M_FLOWID;
277 finaldst = ip6->ip6_dst;
278 bzero(&exthdrs, sizeof(exthdrs));
280 /* Hop-by-Hop options header */
281 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
282 /* Destination options header(1st part) */
283 if (opt->ip6po_rthdr) {
285 * Destination options header(1st part)
286 * This only makes sense with a routing header.
287 * See Section 9.2 of RFC 3542.
288 * Disabling this part just for MIP6 convenience is
289 * a bad idea. We need to think carefully about a
290 * way to make the advanced API coexist with MIP6
291 * options, which might automatically be inserted in
294 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
297 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
298 /* Destination options header(2nd part) */
299 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
304 * IPSec checking which handles several cases.
305 * FAST IPSEC: We re-injected the packet.
307 switch(ip6_ipsec_output(&m, inp, &flags, &error, &ifp))
309 case 1: /* Bad packet */
311 case -1: /* IPSec done */
313 case 0: /* No IPSec */
320 * Calculate the total length of the extension header chain.
321 * Keep the length of the unfragmentable part for fragmentation.
324 if (exthdrs.ip6e_hbh)
325 optlen += exthdrs.ip6e_hbh->m_len;
326 if (exthdrs.ip6e_dest1)
327 optlen += exthdrs.ip6e_dest1->m_len;
328 if (exthdrs.ip6e_rthdr)
329 optlen += exthdrs.ip6e_rthdr->m_len;
330 unfragpartlen = optlen + sizeof(struct ip6_hdr);
332 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
333 if (exthdrs.ip6e_dest2)
334 optlen += exthdrs.ip6e_dest2->m_len;
337 * If there is at least one extension header,
338 * separate IP6 header from the payload.
340 if (optlen && !hdrsplit) {
341 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
345 m = exthdrs.ip6e_ip6;
350 ip6 = mtod(m, struct ip6_hdr *);
352 /* adjust mbuf packet header length */
353 m->m_pkthdr.len += optlen;
354 plen = m->m_pkthdr.len - sizeof(*ip6);
356 /* If this is a jumbo payload, insert a jumbo payload option. */
357 if (plen > IPV6_MAXPACKET) {
359 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
363 m = exthdrs.ip6e_ip6;
367 ip6 = mtod(m, struct ip6_hdr *);
368 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
372 ip6->ip6_plen = htons(plen);
375 * Concatenate headers and fill in next header fields.
376 * Here we have, on "m"
378 * and we insert headers accordingly. Finally, we should be getting:
379 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
381 * during the header composing process, "m" points to IPv6 header.
382 * "mprev" points to an extension header prior to esp.
384 u_char *nexthdrp = &ip6->ip6_nxt;
388 * we treat dest2 specially. this makes IPsec processing
389 * much easier. the goal here is to make mprev point the
390 * mbuf prior to dest2.
392 * result: IPv6 dest2 payload
393 * m and mprev will point to IPv6 header.
395 if (exthdrs.ip6e_dest2) {
397 panic("assumption failed: hdr not split");
398 exthdrs.ip6e_dest2->m_next = m->m_next;
399 m->m_next = exthdrs.ip6e_dest2;
400 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
401 ip6->ip6_nxt = IPPROTO_DSTOPTS;
405 * result: IPv6 hbh dest1 rthdr dest2 payload
406 * m will point to IPv6 header. mprev will point to the
407 * extension header prior to dest2 (rthdr in the above case).
409 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
410 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
412 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
416 * If there is a routing header, discard the packet.
418 if (exthdrs.ip6e_rthdr) {
423 /* Source address validation */
424 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
425 (flags & IPV6_UNSPECSRC) == 0) {
427 IP6STAT_INC(ip6s_badscope);
430 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
432 IP6STAT_INC(ip6s_badscope);
436 IP6STAT_INC(ip6s_localout);
443 bzero((caddr_t)ro, sizeof(*ro));
446 if (opt && opt->ip6po_rthdr)
447 ro = &opt->ip6po_route;
448 dst = (struct sockaddr_in6 *)&ro->ro_dst;
450 if (ro->ro_rt == NULL)
451 (void )flowtable_lookup(AF_INET6, m, (struct route *)ro);
453 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
456 * if specified, try to fill in the traffic class field.
457 * do not override if a non-zero value is already set.
458 * we check the diffserv field and the ecn field separately.
460 if (opt && opt->ip6po_tclass >= 0) {
463 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
465 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
468 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
471 /* fill in or override the hop limit field, if necessary. */
472 if (opt && opt->ip6po_hlim != -1)
473 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
474 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
476 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
478 ip6->ip6_hlim = V_ip6_defmcasthlim;
482 ip6 = mtod(m, struct ip6_hdr *);
484 if (ro->ro_rt && fwd_tag == NULL) {
486 ifp = ro->ro_rt->rt_ifp;
488 if (fwd_tag == NULL) {
489 bzero(&dst_sa, sizeof(dst_sa));
490 dst_sa.sin6_family = AF_INET6;
491 dst_sa.sin6_len = sizeof(dst_sa);
492 dst_sa.sin6_addr = ip6->ip6_dst;
494 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
498 in6_ifstat_inc(ifp, ifs6_out_discard);
504 * If in6_selectroute() does not return a route entry,
505 * dst may not have been updated.
507 *dst = dst_sa; /* XXX */
511 * then rt (for unicast) and ifp must be non-NULL valid values.
513 if ((flags & IPV6_FORWARDING) == 0) {
514 /* XXX: the FORWARDING flag can be set for mrouting. */
515 in6_ifstat_inc(ifp, ifs6_out_request);
518 ia = (struct in6_ifaddr *)(rt->rt_ifa);
519 counter_u64_add(rt->rt_pksent, 1);
524 * The outgoing interface must be in the zone of source and
525 * destination addresses.
530 if (in6_setscope(&src0, origifp, &zone))
532 bzero(&src_sa, sizeof(src_sa));
533 src_sa.sin6_family = AF_INET6;
534 src_sa.sin6_len = sizeof(src_sa);
535 src_sa.sin6_addr = ip6->ip6_src;
536 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
540 if (in6_setscope(&dst0, origifp, &zone))
542 /* re-initialize to be sure */
543 bzero(&dst_sa, sizeof(dst_sa));
544 dst_sa.sin6_family = AF_INET6;
545 dst_sa.sin6_len = sizeof(dst_sa);
546 dst_sa.sin6_addr = ip6->ip6_dst;
547 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
551 /* We should use ia_ifp to support the case of
552 * sending packets to an address of our own.
554 if (ia != NULL && ia->ia_ifp)
557 /* scope check is done. */
561 IP6STAT_INC(ip6s_badscope);
562 in6_ifstat_inc(origifp, ifs6_out_discard);
564 error = EHOSTUNREACH; /* XXX */
568 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
569 if (opt && opt->ip6po_nextroute.ro_rt) {
571 * The nexthop is explicitly specified by the
572 * application. We assume the next hop is an IPv6
575 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
577 else if ((rt->rt_flags & RTF_GATEWAY))
578 dst = (struct sockaddr_in6 *)rt->rt_gateway;
581 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
582 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
584 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
585 in6_ifstat_inc(ifp, ifs6_out_mcast);
587 * Confirm that the outgoing interface supports multicast.
589 if (!(ifp->if_flags & IFF_MULTICAST)) {
590 IP6STAT_INC(ip6s_noroute);
591 in6_ifstat_inc(ifp, ifs6_out_discard);
595 if ((im6o == NULL && in6_mcast_loop) ||
596 (im6o && im6o->im6o_multicast_loop)) {
598 * Loop back multicast datagram if not expressly
599 * forbidden to do so, even if we have not joined
600 * the address; protocols will filter it later,
601 * thus deferring a hash lookup and lock acquisition
602 * at the expense of an m_copym().
604 ip6_mloopback(ifp, m, dst);
607 * If we are acting as a multicast router, perform
608 * multicast forwarding as if the packet had just
609 * arrived on the interface to which we are about
610 * to send. The multicast forwarding function
611 * recursively calls this function, using the
612 * IPV6_FORWARDING flag to prevent infinite recursion.
614 * Multicasts that are looped back by ip6_mloopback(),
615 * above, will be forwarded by the ip6_input() routine,
618 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
620 * XXX: ip6_mforward expects that rcvif is NULL
621 * when it is called from the originating path.
622 * However, it may not always be the case.
624 m->m_pkthdr.rcvif = NULL;
625 if (ip6_mforward(ip6, ifp, m) != 0) {
632 * Multicasts with a hoplimit of zero may be looped back,
633 * above, but must not be transmitted on a network.
634 * Also, multicasts addressed to the loopback interface
635 * are not sent -- the above call to ip6_mloopback() will
636 * loop back a copy if this host actually belongs to the
637 * destination group on the loopback interface.
639 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
640 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
647 * Fill the outgoing inteface to tell the upper layer
648 * to increment per-interface statistics.
653 /* Determine path MTU. */
654 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
655 &alwaysfrag, fibnum)) != 0)
659 * The caller of this function may specify to use the minimum MTU
661 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
662 * setting. The logic is a bit complicated; by default, unicast
663 * packets will follow path MTU while multicast packets will be sent at
664 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
665 * including unicast ones will be sent at the minimum MTU. Multicast
666 * packets will always be sent at the minimum MTU unless
667 * IP6PO_MINMTU_DISABLE is explicitly specified.
668 * See RFC 3542 for more details.
670 if (mtu > IPV6_MMTU) {
671 if ((flags & IPV6_MINMTU))
673 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
675 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
677 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
683 * clear embedded scope identifiers if necessary.
684 * in6_clearscope will touch the addresses only when necessary.
686 in6_clearscope(&ip6->ip6_src);
687 in6_clearscope(&ip6->ip6_dst);
690 * If the outgoing packet contains a hop-by-hop options header,
691 * it must be examined and processed even by the source node.
692 * (RFC 2460, section 4.)
694 if (exthdrs.ip6e_hbh) {
695 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
696 u_int32_t dummy; /* XXX unused */
697 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
700 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
701 panic("ip6e_hbh is not contiguous");
704 * XXX: if we have to send an ICMPv6 error to the sender,
705 * we need the M_LOOP flag since icmp6_error() expects
706 * the IPv6 and the hop-by-hop options header are
707 * contiguous unless the flag is set.
709 m->m_flags |= M_LOOP;
710 m->m_pkthdr.rcvif = ifp;
711 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
712 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
713 &dummy, &plen) < 0) {
714 /* m was already freed at this point */
715 error = EINVAL;/* better error? */
718 m->m_flags &= ~M_LOOP; /* XXX */
719 m->m_pkthdr.rcvif = NULL;
722 /* Jump over all PFIL processing if hooks are not active. */
723 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
727 /* Run through list of hooks for output packets. */
728 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
729 if (error != 0 || m == NULL)
731 ip6 = mtod(m, struct ip6_hdr *);
734 /* See if destination IP address was changed by packet filter. */
735 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
736 m->m_flags |= M_SKIP_FIREWALL;
737 /* If destination is now ourself drop to ip6_input(). */
738 if (in6_localip(&ip6->ip6_dst)) {
739 m->m_flags |= M_FASTFWD_OURS;
740 if (m->m_pkthdr.rcvif == NULL)
741 m->m_pkthdr.rcvif = V_loif;
742 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
743 m->m_pkthdr.csum_flags |=
744 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
745 m->m_pkthdr.csum_data = 0xffff;
748 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
749 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
751 error = netisr_queue(NETISR_IPV6, m);
754 needfiblookup = 1; /* Redo the routing table lookup. */
756 /* See if fib was changed by packet filter. */
757 if (fibnum != M_GETFIB(m)) {
758 m->m_flags |= M_SKIP_FIREWALL;
759 fibnum = M_GETFIB(m);
766 /* See if local, if yes, send it to netisr. */
767 if (m->m_flags & M_FASTFWD_OURS) {
768 if (m->m_pkthdr.rcvif == NULL)
769 m->m_pkthdr.rcvif = V_loif;
770 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
771 m->m_pkthdr.csum_flags |=
772 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
773 m->m_pkthdr.csum_data = 0xffff;
776 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
777 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
779 error = netisr_queue(NETISR_IPV6, m);
782 /* Or forward to some other address? */
783 if ((m->m_flags & M_IP6_NEXTHOP) &&
784 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
785 dst = (struct sockaddr_in6 *)&ro->ro_dst;
786 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
787 m->m_flags |= M_SKIP_FIREWALL;
788 m->m_flags &= ~M_IP6_NEXTHOP;
789 m_tag_delete(m, fwd_tag);
795 * Send the packet to the outgoing interface.
796 * If necessary, do IPv6 fragmentation before sending.
798 * the logic here is rather complex:
799 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
800 * 1-a: send as is if tlen <= path mtu
801 * 1-b: fragment if tlen > path mtu
803 * 2: if user asks us not to fragment (dontfrag == 1)
804 * 2-a: send as is if tlen <= interface mtu
805 * 2-b: error if tlen > interface mtu
807 * 3: if we always need to attach fragment header (alwaysfrag == 1)
810 * 4: if dontfrag == 1 && alwaysfrag == 1
811 * error, as we cannot handle this conflicting request
813 sw_csum = m->m_pkthdr.csum_flags;
815 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
816 sw_csum &= ~ifp->if_hwassist;
820 * If we added extension headers, we will not do TSO and calculate the
821 * checksums ourselves for now.
822 * XXX-BZ Need a framework to know when the NIC can handle it, even
825 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
826 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
827 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
830 if (sw_csum & CSUM_SCTP_IPV6) {
831 sw_csum &= ~CSUM_SCTP_IPV6;
832 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
835 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
836 tlen = m->m_pkthdr.len;
838 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
842 if (dontfrag && alwaysfrag) { /* case 4 */
843 /* conflicting request - can't transmit */
847 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
849 * Even if the DONTFRAG option is specified, we cannot send the
850 * packet when the data length is larger than the MTU of the
851 * outgoing interface.
852 * Notify the error by sending IPV6_PATHMTU ancillary data as
853 * well as returning an error code (the latter is not described
857 struct ip6ctlparam ip6cp;
859 mtu32 = (u_int32_t)mtu;
860 bzero(&ip6cp, sizeof(ip6cp));
861 ip6cp.ip6c_cmdarg = (void *)&mtu32;
862 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
870 * transmit packet without fragmentation
872 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
873 struct in6_ifaddr *ia6;
875 ip6 = mtod(m, struct ip6_hdr *);
876 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
878 /* Record statistics for this interface address. */
879 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
880 counter_u64_add(ia6->ia_ifa.ifa_obytes,
882 ifa_free(&ia6->ia_ifa);
884 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
889 * try to fragment the packet. case 1-b and 3
891 if (mtu < IPV6_MMTU) {
892 /* path MTU cannot be less than IPV6_MMTU */
894 in6_ifstat_inc(ifp, ifs6_out_fragfail);
896 } else if (ip6->ip6_plen == 0) {
897 /* jumbo payload cannot be fragmented */
899 in6_ifstat_inc(ifp, ifs6_out_fragfail);
902 struct mbuf **mnext, *m_frgpart;
903 struct ip6_frag *ip6f;
904 u_int32_t id = htonl(ip6_randomid());
907 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
910 * Too large for the destination or interface;
911 * fragment if possible.
912 * Must be able to put at least 8 bytes per fragment.
914 hlen = unfragpartlen;
915 if (mtu > IPV6_MAXPACKET)
916 mtu = IPV6_MAXPACKET;
918 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
921 in6_ifstat_inc(ifp, ifs6_out_fragfail);
926 * Verify that we have any chance at all of being able to queue
927 * the packet or packet fragments
929 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
930 < tlen /* - hlen */)) {
932 IP6STAT_INC(ip6s_odropped);
938 * If the interface will not calculate checksums on
939 * fragmented packets, then do it here.
940 * XXX-BZ handle the hw offloading case. Need flags.
942 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
943 in6_delayed_cksum(m, plen, hlen);
944 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
947 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
948 sctp_delayed_cksum(m, hlen);
949 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
952 mnext = &m->m_nextpkt;
955 * Change the next header field of the last header in the
956 * unfragmentable part.
958 if (exthdrs.ip6e_rthdr) {
959 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
960 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
961 } else if (exthdrs.ip6e_dest1) {
962 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
963 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
964 } else if (exthdrs.ip6e_hbh) {
965 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
966 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
968 nextproto = ip6->ip6_nxt;
969 ip6->ip6_nxt = IPPROTO_FRAGMENT;
973 * Loop through length of segment after first fragment,
974 * make new header and copy data of each part and link onto
978 for (off = hlen; off < tlen; off += len) {
979 m = m_gethdr(M_NOWAIT, MT_DATA);
982 IP6STAT_INC(ip6s_odropped);
985 m->m_flags = m0->m_flags & M_COPYFLAGS;
987 mnext = &m->m_nextpkt;
988 m->m_data += max_linkhdr;
989 mhip6 = mtod(m, struct ip6_hdr *);
991 m->m_len = sizeof(*mhip6);
992 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
994 IP6STAT_INC(ip6s_odropped);
997 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
998 if (off + len >= tlen)
1001 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1002 mhip6->ip6_plen = htons((u_short)(len + hlen +
1003 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1004 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1006 IP6STAT_INC(ip6s_odropped);
1009 m_cat(m, m_frgpart);
1010 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1011 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
1012 m->m_pkthdr.rcvif = NULL;
1013 ip6f->ip6f_reserved = 0;
1014 ip6f->ip6f_ident = id;
1015 ip6f->ip6f_nxt = nextproto;
1016 IP6STAT_INC(ip6s_ofragments);
1017 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1020 in6_ifstat_inc(ifp, ifs6_out_fragok);
1024 * Remove leading garbages.
1030 for (m0 = m; m; m = m0) {
1034 /* Record statistics for this interface address. */
1036 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1037 counter_u64_add(ia->ia_ifa.ifa_obytes,
1040 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1046 IP6STAT_INC(ip6s_fragmented);
1049 if (ro == &ip6route)
1051 if (ro_pmtu == &ip6route)
1056 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1057 m_freem(exthdrs.ip6e_dest1);
1058 m_freem(exthdrs.ip6e_rthdr);
1059 m_freem(exthdrs.ip6e_dest2);
1068 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1072 if (hlen > MCLBYTES)
1073 return (ENOBUFS); /* XXX */
1076 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1078 m = m_get(M_NOWAIT, MT_DATA);
1083 bcopy(hdr, mtod(m, caddr_t), hlen);
1090 * Insert jumbo payload option.
1093 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1099 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1102 * If there is no hop-by-hop options header, allocate new one.
1103 * If there is one but it doesn't have enough space to store the
1104 * jumbo payload option, allocate a cluster to store the whole options.
1105 * Otherwise, use it to store the options.
1107 if (exthdrs->ip6e_hbh == 0) {
1108 mopt = m_get(M_NOWAIT, MT_DATA);
1111 mopt->m_len = JUMBOOPTLEN;
1112 optbuf = mtod(mopt, u_char *);
1113 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1114 exthdrs->ip6e_hbh = mopt;
1116 struct ip6_hbh *hbh;
1118 mopt = exthdrs->ip6e_hbh;
1119 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1122 * - exthdrs->ip6e_hbh is not referenced from places
1123 * other than exthdrs.
1124 * - exthdrs->ip6e_hbh is not an mbuf chain.
1126 int oldoptlen = mopt->m_len;
1130 * XXX: give up if the whole (new) hbh header does
1131 * not fit even in an mbuf cluster.
1133 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1137 * As a consequence, we must always prepare a cluster
1140 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1143 n->m_len = oldoptlen + JUMBOOPTLEN;
1144 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1146 optbuf = mtod(n, caddr_t) + oldoptlen;
1148 mopt = exthdrs->ip6e_hbh = n;
1150 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1151 mopt->m_len += JUMBOOPTLEN;
1153 optbuf[0] = IP6OPT_PADN;
1157 * Adjust the header length according to the pad and
1158 * the jumbo payload option.
1160 hbh = mtod(mopt, struct ip6_hbh *);
1161 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1164 /* fill in the option. */
1165 optbuf[2] = IP6OPT_JUMBO;
1167 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1168 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1170 /* finally, adjust the packet header length */
1171 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1178 * Insert fragment header and copy unfragmentable header portions.
1181 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1182 struct ip6_frag **frghdrp)
1184 struct mbuf *n, *mlast;
1186 if (hlen > sizeof(struct ip6_hdr)) {
1187 n = m_copym(m0, sizeof(struct ip6_hdr),
1188 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1195 /* Search for the last mbuf of unfragmentable part. */
1196 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1199 if ((mlast->m_flags & M_EXT) == 0 &&
1200 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1201 /* use the trailing space of the last mbuf for the fragment hdr */
1202 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1204 mlast->m_len += sizeof(struct ip6_frag);
1205 m->m_pkthdr.len += sizeof(struct ip6_frag);
1207 /* allocate a new mbuf for the fragment header */
1210 mfrg = m_get(M_NOWAIT, MT_DATA);
1213 mfrg->m_len = sizeof(struct ip6_frag);
1214 *frghdrp = mtod(mfrg, struct ip6_frag *);
1215 mlast->m_next = mfrg;
1222 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1223 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1224 int *alwaysfragp, u_int fibnum)
1230 if (ro_pmtu != ro) {
1231 /* The first hop and the final destination may differ. */
1232 struct sockaddr_in6 *sa6_dst =
1233 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1234 if (ro_pmtu->ro_rt &&
1235 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1236 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1237 RTFREE(ro_pmtu->ro_rt);
1238 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1240 if (ro_pmtu->ro_rt == NULL) {
1241 bzero(sa6_dst, sizeof(*sa6_dst));
1242 sa6_dst->sin6_family = AF_INET6;
1243 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1244 sa6_dst->sin6_addr = *dst;
1246 in6_rtalloc(ro_pmtu, fibnum);
1249 if (ro_pmtu->ro_rt) {
1251 struct in_conninfo inc;
1253 bzero(&inc, sizeof(inc));
1254 inc.inc_flags |= INC_ISIPV6;
1255 inc.inc6_faddr = *dst;
1258 ifp = ro_pmtu->ro_rt->rt_ifp;
1259 ifmtu = IN6_LINKMTU(ifp);
1260 mtu = tcp_hc_getmtu(&inc);
1262 mtu = min(mtu, ro_pmtu->ro_rt->rt_mtu);
1264 mtu = ro_pmtu->ro_rt->rt_mtu;
1267 else if (mtu < IPV6_MMTU) {
1269 * RFC2460 section 5, last paragraph:
1270 * if we record ICMPv6 too big message with
1271 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1272 * or smaller, with framgent header attached.
1273 * (fragment header is needed regardless from the
1274 * packet size, for translators to identify packets)
1278 } else if (mtu > ifmtu) {
1280 * The MTU on the route is larger than the MTU on
1281 * the interface! This shouldn't happen, unless the
1282 * MTU of the interface has been changed after the
1283 * interface was brought up. Change the MTU in the
1284 * route to match the interface MTU (as long as the
1285 * field isn't locked).
1288 ro_pmtu->ro_rt->rt_mtu = mtu;
1291 mtu = IN6_LINKMTU(ifp);
1293 error = EHOSTUNREACH; /* XXX */
1297 *alwaysfragp = alwaysfrag;
1302 * IP6 socket option processing.
1305 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1307 int optdatalen, uproto;
1309 struct inpcb *in6p = sotoinpcb(so);
1311 int level, op, optname;
1315 uint32_t rss_bucket;
1319 level = sopt->sopt_level;
1320 op = sopt->sopt_dir;
1321 optname = sopt->sopt_name;
1322 optlen = sopt->sopt_valsize;
1326 uproto = (int)so->so_proto->pr_protocol;
1328 if (level != IPPROTO_IPV6) {
1331 if (sopt->sopt_level == SOL_SOCKET &&
1332 sopt->sopt_dir == SOPT_SET) {
1333 switch (sopt->sopt_name) {
1336 if ((so->so_options & SO_REUSEADDR) != 0)
1337 in6p->inp_flags2 |= INP_REUSEADDR;
1339 in6p->inp_flags2 &= ~INP_REUSEADDR;
1345 if ((so->so_options & SO_REUSEPORT) != 0)
1346 in6p->inp_flags2 |= INP_REUSEPORT;
1348 in6p->inp_flags2 &= ~INP_REUSEPORT;
1354 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1362 } else { /* level == IPPROTO_IPV6 */
1367 case IPV6_2292PKTOPTIONS:
1368 #ifdef IPV6_PKTOPTIONS
1369 case IPV6_PKTOPTIONS:
1374 error = soopt_getm(sopt, &m); /* XXX */
1377 error = soopt_mcopyin(sopt, m); /* XXX */
1380 error = ip6_pcbopts(&in6p->in6p_outputopts,
1382 m_freem(m); /* XXX */
1387 * Use of some Hop-by-Hop options or some
1388 * Destination options, might require special
1389 * privilege. That is, normal applications
1390 * (without special privilege) might be forbidden
1391 * from setting certain options in outgoing packets,
1392 * and might never see certain options in received
1393 * packets. [RFC 2292 Section 6]
1394 * KAME specific note:
1395 * KAME prevents non-privileged users from sending or
1396 * receiving ANY hbh/dst options in order to avoid
1397 * overhead of parsing options in the kernel.
1399 case IPV6_RECVHOPOPTS:
1400 case IPV6_RECVDSTOPTS:
1401 case IPV6_RECVRTHDRDSTOPTS:
1403 error = priv_check(td,
1404 PRIV_NETINET_SETHDROPTS);
1409 case IPV6_UNICAST_HOPS:
1413 case IPV6_RECVPKTINFO:
1414 case IPV6_RECVHOPLIMIT:
1415 case IPV6_RECVRTHDR:
1416 case IPV6_RECVPATHMTU:
1417 case IPV6_RECVTCLASS:
1419 case IPV6_AUTOFLOWLABEL:
1421 case IPV6_BINDMULTI:
1423 case IPV6_RSS_LISTEN_BUCKET:
1425 if (optname == IPV6_BINDANY && td != NULL) {
1426 error = priv_check(td,
1427 PRIV_NETINET_BINDANY);
1432 if (optlen != sizeof(int)) {
1436 error = sooptcopyin(sopt, &optval,
1437 sizeof optval, sizeof optval);
1442 case IPV6_UNICAST_HOPS:
1443 if (optval < -1 || optval >= 256)
1446 /* -1 = kernel default */
1447 in6p->in6p_hops = optval;
1448 if ((in6p->inp_vflag &
1450 in6p->inp_ip_ttl = optval;
1453 #define OPTSET(bit) \
1457 in6p->inp_flags |= (bit); \
1459 in6p->inp_flags &= ~(bit); \
1460 INP_WUNLOCK(in6p); \
1461 } while (/*CONSTCOND*/ 0)
1462 #define OPTSET2292(bit) \
1465 in6p->inp_flags |= IN6P_RFC2292; \
1467 in6p->inp_flags |= (bit); \
1469 in6p->inp_flags &= ~(bit); \
1470 INP_WUNLOCK(in6p); \
1471 } while (/*CONSTCOND*/ 0)
1472 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1474 #define OPTSET2(bit, val) do { \
1477 in6p->inp_flags2 |= bit; \
1479 in6p->inp_flags2 &= ~bit; \
1480 INP_WUNLOCK(in6p); \
1482 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1484 case IPV6_RECVPKTINFO:
1485 /* cannot mix with RFC2292 */
1486 if (OPTBIT(IN6P_RFC2292)) {
1490 OPTSET(IN6P_PKTINFO);
1495 struct ip6_pktopts **optp;
1497 /* cannot mix with RFC2292 */
1498 if (OPTBIT(IN6P_RFC2292)) {
1502 optp = &in6p->in6p_outputopts;
1503 error = ip6_pcbopt(IPV6_HOPLIMIT,
1504 (u_char *)&optval, sizeof(optval),
1505 optp, (td != NULL) ? td->td_ucred :
1510 case IPV6_RECVHOPLIMIT:
1511 /* cannot mix with RFC2292 */
1512 if (OPTBIT(IN6P_RFC2292)) {
1516 OPTSET(IN6P_HOPLIMIT);
1519 case IPV6_RECVHOPOPTS:
1520 /* cannot mix with RFC2292 */
1521 if (OPTBIT(IN6P_RFC2292)) {
1525 OPTSET(IN6P_HOPOPTS);
1528 case IPV6_RECVDSTOPTS:
1529 /* cannot mix with RFC2292 */
1530 if (OPTBIT(IN6P_RFC2292)) {
1534 OPTSET(IN6P_DSTOPTS);
1537 case IPV6_RECVRTHDRDSTOPTS:
1538 /* cannot mix with RFC2292 */
1539 if (OPTBIT(IN6P_RFC2292)) {
1543 OPTSET(IN6P_RTHDRDSTOPTS);
1546 case IPV6_RECVRTHDR:
1547 /* cannot mix with RFC2292 */
1548 if (OPTBIT(IN6P_RFC2292)) {
1559 case IPV6_RECVPATHMTU:
1561 * We ignore this option for TCP
1563 * (RFC3542 leaves this case
1566 if (uproto != IPPROTO_TCP)
1572 * make setsockopt(IPV6_V6ONLY)
1573 * available only prior to bind(2).
1574 * see ipng mailing list, Jun 22 2001.
1576 if (in6p->inp_lport ||
1577 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1581 OPTSET(IN6P_IPV6_V6ONLY);
1583 in6p->inp_vflag &= ~INP_IPV4;
1585 in6p->inp_vflag |= INP_IPV4;
1587 case IPV6_RECVTCLASS:
1588 /* cannot mix with RFC2292 XXX */
1589 if (OPTBIT(IN6P_RFC2292)) {
1593 OPTSET(IN6P_TCLASS);
1595 case IPV6_AUTOFLOWLABEL:
1596 OPTSET(IN6P_AUTOFLOWLABEL);
1600 OPTSET(INP_BINDANY);
1603 case IPV6_BINDMULTI:
1604 OPTSET2(INP_BINDMULTI, optval);
1607 case IPV6_RSS_LISTEN_BUCKET:
1608 if ((optval >= 0) &&
1609 (optval < rss_getnumbuckets())) {
1610 in6p->inp_rss_listen_bucket = optval;
1611 OPTSET2(INP_RSS_BUCKET_SET, 1);
1622 case IPV6_USE_MIN_MTU:
1623 case IPV6_PREFER_TEMPADDR:
1624 if (optlen != sizeof(optval)) {
1628 error = sooptcopyin(sopt, &optval,
1629 sizeof optval, sizeof optval);
1633 struct ip6_pktopts **optp;
1634 optp = &in6p->in6p_outputopts;
1635 error = ip6_pcbopt(optname,
1636 (u_char *)&optval, sizeof(optval),
1637 optp, (td != NULL) ? td->td_ucred :
1642 case IPV6_2292PKTINFO:
1643 case IPV6_2292HOPLIMIT:
1644 case IPV6_2292HOPOPTS:
1645 case IPV6_2292DSTOPTS:
1646 case IPV6_2292RTHDR:
1648 if (optlen != sizeof(int)) {
1652 error = sooptcopyin(sopt, &optval,
1653 sizeof optval, sizeof optval);
1657 case IPV6_2292PKTINFO:
1658 OPTSET2292(IN6P_PKTINFO);
1660 case IPV6_2292HOPLIMIT:
1661 OPTSET2292(IN6P_HOPLIMIT);
1663 case IPV6_2292HOPOPTS:
1665 * Check super-user privilege.
1666 * See comments for IPV6_RECVHOPOPTS.
1669 error = priv_check(td,
1670 PRIV_NETINET_SETHDROPTS);
1674 OPTSET2292(IN6P_HOPOPTS);
1676 case IPV6_2292DSTOPTS:
1678 error = priv_check(td,
1679 PRIV_NETINET_SETHDROPTS);
1683 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1685 case IPV6_2292RTHDR:
1686 OPTSET2292(IN6P_RTHDR);
1694 case IPV6_RTHDRDSTOPTS:
1697 /* new advanced API (RFC3542) */
1699 u_char optbuf_storage[MCLBYTES];
1701 struct ip6_pktopts **optp;
1703 /* cannot mix with RFC2292 */
1704 if (OPTBIT(IN6P_RFC2292)) {
1710 * We only ensure valsize is not too large
1711 * here. Further validation will be done
1714 error = sooptcopyin(sopt, optbuf_storage,
1715 sizeof(optbuf_storage), 0);
1718 optlen = sopt->sopt_valsize;
1719 optbuf = optbuf_storage;
1720 optp = &in6p->in6p_outputopts;
1721 error = ip6_pcbopt(optname, optbuf, optlen,
1722 optp, (td != NULL) ? td->td_ucred : NULL,
1728 case IPV6_MULTICAST_IF:
1729 case IPV6_MULTICAST_HOPS:
1730 case IPV6_MULTICAST_LOOP:
1731 case IPV6_JOIN_GROUP:
1732 case IPV6_LEAVE_GROUP:
1734 case MCAST_BLOCK_SOURCE:
1735 case MCAST_UNBLOCK_SOURCE:
1736 case MCAST_JOIN_GROUP:
1737 case MCAST_LEAVE_GROUP:
1738 case MCAST_JOIN_SOURCE_GROUP:
1739 case MCAST_LEAVE_SOURCE_GROUP:
1740 error = ip6_setmoptions(in6p, sopt);
1743 case IPV6_PORTRANGE:
1744 error = sooptcopyin(sopt, &optval,
1745 sizeof optval, sizeof optval);
1751 case IPV6_PORTRANGE_DEFAULT:
1752 in6p->inp_flags &= ~(INP_LOWPORT);
1753 in6p->inp_flags &= ~(INP_HIGHPORT);
1756 case IPV6_PORTRANGE_HIGH:
1757 in6p->inp_flags &= ~(INP_LOWPORT);
1758 in6p->inp_flags |= INP_HIGHPORT;
1761 case IPV6_PORTRANGE_LOW:
1762 in6p->inp_flags &= ~(INP_HIGHPORT);
1763 in6p->inp_flags |= INP_LOWPORT;
1774 case IPV6_IPSEC_POLICY:
1779 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1781 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1783 req = mtod(m, caddr_t);
1784 error = ipsec_set_policy(in6p, optname, req,
1785 m->m_len, (sopt->sopt_td != NULL) ?
1786 sopt->sopt_td->td_ucred : NULL);
1793 error = ENOPROTOOPT;
1801 case IPV6_2292PKTOPTIONS:
1802 #ifdef IPV6_PKTOPTIONS
1803 case IPV6_PKTOPTIONS:
1806 * RFC3542 (effectively) deprecated the
1807 * semantics of the 2292-style pktoptions.
1808 * Since it was not reliable in nature (i.e.,
1809 * applications had to expect the lack of some
1810 * information after all), it would make sense
1811 * to simplify this part by always returning
1814 sopt->sopt_valsize = 0;
1817 case IPV6_RECVHOPOPTS:
1818 case IPV6_RECVDSTOPTS:
1819 case IPV6_RECVRTHDRDSTOPTS:
1820 case IPV6_UNICAST_HOPS:
1821 case IPV6_RECVPKTINFO:
1822 case IPV6_RECVHOPLIMIT:
1823 case IPV6_RECVRTHDR:
1824 case IPV6_RECVPATHMTU:
1828 case IPV6_PORTRANGE:
1829 case IPV6_RECVTCLASS:
1830 case IPV6_AUTOFLOWLABEL:
1835 case IPV6_RSSBUCKETID:
1839 case IPV6_RECVHOPOPTS:
1840 optval = OPTBIT(IN6P_HOPOPTS);
1843 case IPV6_RECVDSTOPTS:
1844 optval = OPTBIT(IN6P_DSTOPTS);
1847 case IPV6_RECVRTHDRDSTOPTS:
1848 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1851 case IPV6_UNICAST_HOPS:
1852 optval = in6p->in6p_hops;
1855 case IPV6_RECVPKTINFO:
1856 optval = OPTBIT(IN6P_PKTINFO);
1859 case IPV6_RECVHOPLIMIT:
1860 optval = OPTBIT(IN6P_HOPLIMIT);
1863 case IPV6_RECVRTHDR:
1864 optval = OPTBIT(IN6P_RTHDR);
1867 case IPV6_RECVPATHMTU:
1868 optval = OPTBIT(IN6P_MTU);
1872 optval = OPTBIT(INP_FAITH);
1876 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1879 case IPV6_PORTRANGE:
1882 flags = in6p->inp_flags;
1883 if (flags & INP_HIGHPORT)
1884 optval = IPV6_PORTRANGE_HIGH;
1885 else if (flags & INP_LOWPORT)
1886 optval = IPV6_PORTRANGE_LOW;
1891 case IPV6_RECVTCLASS:
1892 optval = OPTBIT(IN6P_TCLASS);
1895 case IPV6_AUTOFLOWLABEL:
1896 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1900 optval = OPTBIT(INP_BINDANY);
1904 optval = in6p->inp_flowid;
1908 optval = in6p->inp_flowtype;
1911 case IPV6_RSSBUCKETID:
1913 rss_hash2bucket(in6p->inp_flowid,
1917 optval = rss_bucket;
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 if (pktopt && pktopt->ip6po_pktinfo)
2217 optdata = (void *)pktopt->ip6po_pktinfo;
2219 /* XXX: we don't have to do this every time... */
2220 bzero(&null_pktinfo, sizeof(null_pktinfo));
2221 optdata = (void *)&null_pktinfo;
2223 optdatalen = sizeof(struct in6_pktinfo);
2226 if (pktopt && pktopt->ip6po_tclass >= 0)
2227 optdata = (void *)&pktopt->ip6po_tclass;
2229 optdata = (void *)&deftclass;
2230 optdatalen = sizeof(int);
2233 if (pktopt && pktopt->ip6po_hbh) {
2234 optdata = (void *)pktopt->ip6po_hbh;
2235 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2236 optdatalen = (ip6e->ip6e_len + 1) << 3;
2240 if (pktopt && pktopt->ip6po_rthdr) {
2241 optdata = (void *)pktopt->ip6po_rthdr;
2242 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2243 optdatalen = (ip6e->ip6e_len + 1) << 3;
2246 case IPV6_RTHDRDSTOPTS:
2247 if (pktopt && pktopt->ip6po_dest1) {
2248 optdata = (void *)pktopt->ip6po_dest1;
2249 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2250 optdatalen = (ip6e->ip6e_len + 1) << 3;
2254 if (pktopt && pktopt->ip6po_dest2) {
2255 optdata = (void *)pktopt->ip6po_dest2;
2256 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2257 optdatalen = (ip6e->ip6e_len + 1) << 3;
2261 if (pktopt && pktopt->ip6po_nexthop) {
2262 optdata = (void *)pktopt->ip6po_nexthop;
2263 optdatalen = pktopt->ip6po_nexthop->sa_len;
2266 case IPV6_USE_MIN_MTU:
2268 optdata = (void *)&pktopt->ip6po_minmtu;
2270 optdata = (void *)&defminmtu;
2271 optdatalen = sizeof(int);
2274 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2278 optdata = (void *)&on;
2279 optdatalen = sizeof(on);
2281 case IPV6_PREFER_TEMPADDR:
2283 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2285 optdata = (void *)&defpreftemp;
2286 optdatalen = sizeof(int);
2288 default: /* should not happen */
2290 panic("ip6_getpcbopt: unexpected option\n");
2292 return (ENOPROTOOPT);
2295 error = sooptcopyout(sopt, optdata, optdatalen);
2301 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2306 if (optname == -1 || optname == IPV6_PKTINFO) {
2307 if (pktopt->ip6po_pktinfo)
2308 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2309 pktopt->ip6po_pktinfo = NULL;
2311 if (optname == -1 || optname == IPV6_HOPLIMIT)
2312 pktopt->ip6po_hlim = -1;
2313 if (optname == -1 || optname == IPV6_TCLASS)
2314 pktopt->ip6po_tclass = -1;
2315 if (optname == -1 || optname == IPV6_NEXTHOP) {
2316 if (pktopt->ip6po_nextroute.ro_rt) {
2317 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2318 pktopt->ip6po_nextroute.ro_rt = NULL;
2320 if (pktopt->ip6po_nexthop)
2321 free(pktopt->ip6po_nexthop, M_IP6OPT);
2322 pktopt->ip6po_nexthop = NULL;
2324 if (optname == -1 || optname == IPV6_HOPOPTS) {
2325 if (pktopt->ip6po_hbh)
2326 free(pktopt->ip6po_hbh, M_IP6OPT);
2327 pktopt->ip6po_hbh = NULL;
2329 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2330 if (pktopt->ip6po_dest1)
2331 free(pktopt->ip6po_dest1, M_IP6OPT);
2332 pktopt->ip6po_dest1 = NULL;
2334 if (optname == -1 || optname == IPV6_RTHDR) {
2335 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2336 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2337 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2338 if (pktopt->ip6po_route.ro_rt) {
2339 RTFREE(pktopt->ip6po_route.ro_rt);
2340 pktopt->ip6po_route.ro_rt = NULL;
2343 if (optname == -1 || optname == IPV6_DSTOPTS) {
2344 if (pktopt->ip6po_dest2)
2345 free(pktopt->ip6po_dest2, M_IP6OPT);
2346 pktopt->ip6po_dest2 = NULL;
2350 #define PKTOPT_EXTHDRCPY(type) \
2353 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2354 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2355 if (dst->type == NULL && canwait == M_NOWAIT)\
2357 bcopy(src->type, dst->type, hlen);\
2359 } while (/*CONSTCOND*/ 0)
2362 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2364 if (dst == NULL || src == NULL) {
2365 printf("ip6_clearpktopts: invalid argument\n");
2369 dst->ip6po_hlim = src->ip6po_hlim;
2370 dst->ip6po_tclass = src->ip6po_tclass;
2371 dst->ip6po_flags = src->ip6po_flags;
2372 dst->ip6po_minmtu = src->ip6po_minmtu;
2373 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2374 if (src->ip6po_pktinfo) {
2375 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2377 if (dst->ip6po_pktinfo == NULL)
2379 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2381 if (src->ip6po_nexthop) {
2382 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2384 if (dst->ip6po_nexthop == NULL)
2386 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2387 src->ip6po_nexthop->sa_len);
2389 PKTOPT_EXTHDRCPY(ip6po_hbh);
2390 PKTOPT_EXTHDRCPY(ip6po_dest1);
2391 PKTOPT_EXTHDRCPY(ip6po_dest2);
2392 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2396 ip6_clearpktopts(dst, -1);
2399 #undef PKTOPT_EXTHDRCPY
2401 struct ip6_pktopts *
2402 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2405 struct ip6_pktopts *dst;
2407 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2410 ip6_initpktopts(dst);
2412 if ((error = copypktopts(dst, src, canwait)) != 0) {
2413 free(dst, M_IP6OPT);
2421 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2426 ip6_clearpktopts(pktopt, -1);
2428 free(pktopt, M_IP6OPT);
2432 * Set IPv6 outgoing packet options based on advanced API.
2435 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2436 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2438 struct cmsghdr *cm = 0;
2440 if (control == NULL || opt == NULL)
2443 ip6_initpktopts(opt);
2448 * If stickyopt is provided, make a local copy of the options
2449 * for this particular packet, then override them by ancillary
2451 * XXX: copypktopts() does not copy the cached route to a next
2452 * hop (if any). This is not very good in terms of efficiency,
2453 * but we can allow this since this option should be rarely
2456 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2461 * XXX: Currently, we assume all the optional information is stored
2464 if (control->m_next)
2467 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2468 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2471 if (control->m_len < CMSG_LEN(0))
2474 cm = mtod(control, struct cmsghdr *);
2475 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2477 if (cm->cmsg_level != IPPROTO_IPV6)
2480 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2481 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2490 * Set a particular packet option, as a sticky option or an ancillary data
2491 * item. "len" can be 0 only when it's a sticky option.
2492 * We have 4 cases of combination of "sticky" and "cmsg":
2493 * "sticky=0, cmsg=0": impossible
2494 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2495 * "sticky=1, cmsg=0": RFC3542 socket option
2496 * "sticky=1, cmsg=1": RFC2292 socket option
2499 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2500 struct ucred *cred, int sticky, int cmsg, int uproto)
2502 int minmtupolicy, preftemp;
2505 if (!sticky && !cmsg) {
2507 printf("ip6_setpktopt: impossible case\n");
2513 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2514 * not be specified in the context of RFC3542. Conversely,
2515 * RFC3542 types should not be specified in the context of RFC2292.
2519 case IPV6_2292PKTINFO:
2520 case IPV6_2292HOPLIMIT:
2521 case IPV6_2292NEXTHOP:
2522 case IPV6_2292HOPOPTS:
2523 case IPV6_2292DSTOPTS:
2524 case IPV6_2292RTHDR:
2525 case IPV6_2292PKTOPTIONS:
2526 return (ENOPROTOOPT);
2529 if (sticky && cmsg) {
2536 case IPV6_RTHDRDSTOPTS:
2538 case IPV6_USE_MIN_MTU:
2541 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2542 return (ENOPROTOOPT);
2547 case IPV6_2292PKTINFO:
2550 struct ifnet *ifp = NULL;
2551 struct in6_pktinfo *pktinfo;
2553 if (len != sizeof(struct in6_pktinfo))
2556 pktinfo = (struct in6_pktinfo *)buf;
2559 * An application can clear any sticky IPV6_PKTINFO option by
2560 * doing a "regular" setsockopt with ipi6_addr being
2561 * in6addr_any and ipi6_ifindex being zero.
2562 * [RFC 3542, Section 6]
2564 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2565 pktinfo->ipi6_ifindex == 0 &&
2566 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2567 ip6_clearpktopts(opt, optname);
2571 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2572 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2575 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2577 /* validate the interface index if specified. */
2578 if (pktinfo->ipi6_ifindex > V_if_index)
2580 if (pktinfo->ipi6_ifindex) {
2581 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2585 if (ifp != NULL && (
2586 ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED))
2590 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2591 struct in6_ifaddr *ia;
2593 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2595 return (EADDRNOTAVAIL);
2596 ifa_free(&ia->ia_ifa);
2599 * We store the address anyway, and let in6_selectsrc()
2600 * validate the specified address. This is because ipi6_addr
2601 * may not have enough information about its scope zone, and
2602 * we may need additional information (such as outgoing
2603 * interface or the scope zone of a destination address) to
2604 * disambiguate the scope.
2605 * XXX: the delay of the validation may confuse the
2606 * application when it is used as a sticky option.
2608 if (opt->ip6po_pktinfo == NULL) {
2609 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2610 M_IP6OPT, M_NOWAIT);
2611 if (opt->ip6po_pktinfo == NULL)
2614 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2618 case IPV6_2292HOPLIMIT:
2624 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2625 * to simplify the ordering among hoplimit options.
2627 if (optname == IPV6_HOPLIMIT && sticky)
2628 return (ENOPROTOOPT);
2630 if (len != sizeof(int))
2633 if (*hlimp < -1 || *hlimp > 255)
2636 opt->ip6po_hlim = *hlimp;
2644 if (len != sizeof(int))
2646 tclass = *(int *)buf;
2647 if (tclass < -1 || tclass > 255)
2650 opt->ip6po_tclass = tclass;
2654 case IPV6_2292NEXTHOP:
2657 error = priv_check_cred(cred,
2658 PRIV_NETINET_SETHDROPTS, 0);
2663 if (len == 0) { /* just remove the option */
2664 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2668 /* check if cmsg_len is large enough for sa_len */
2669 if (len < sizeof(struct sockaddr) || len < *buf)
2672 switch (((struct sockaddr *)buf)->sa_family) {
2675 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2678 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2681 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2682 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2685 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2691 case AF_LINK: /* should eventually be supported */
2693 return (EAFNOSUPPORT);
2696 /* turn off the previous option, then set the new option. */
2697 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2698 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2699 if (opt->ip6po_nexthop == NULL)
2701 bcopy(buf, opt->ip6po_nexthop, *buf);
2704 case IPV6_2292HOPOPTS:
2707 struct ip6_hbh *hbh;
2711 * XXX: We don't allow a non-privileged user to set ANY HbH
2712 * options, since per-option restriction has too much
2716 error = priv_check_cred(cred,
2717 PRIV_NETINET_SETHDROPTS, 0);
2723 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2724 break; /* just remove the option */
2727 /* message length validation */
2728 if (len < sizeof(struct ip6_hbh))
2730 hbh = (struct ip6_hbh *)buf;
2731 hbhlen = (hbh->ip6h_len + 1) << 3;
2735 /* turn off the previous option, then set the new option. */
2736 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2737 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2738 if (opt->ip6po_hbh == NULL)
2740 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2745 case IPV6_2292DSTOPTS:
2747 case IPV6_RTHDRDSTOPTS:
2749 struct ip6_dest *dest, **newdest = NULL;
2752 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2753 error = priv_check_cred(cred,
2754 PRIV_NETINET_SETHDROPTS, 0);
2760 ip6_clearpktopts(opt, optname);
2761 break; /* just remove the option */
2764 /* message length validation */
2765 if (len < sizeof(struct ip6_dest))
2767 dest = (struct ip6_dest *)buf;
2768 destlen = (dest->ip6d_len + 1) << 3;
2773 * Determine the position that the destination options header
2774 * should be inserted; before or after the routing header.
2777 case IPV6_2292DSTOPTS:
2779 * The old advacned API is ambiguous on this point.
2780 * Our approach is to determine the position based
2781 * according to the existence of a routing header.
2782 * Note, however, that this depends on the order of the
2783 * extension headers in the ancillary data; the 1st
2784 * part of the destination options header must appear
2785 * before the routing header in the ancillary data,
2787 * RFC3542 solved the ambiguity by introducing
2788 * separate ancillary data or option types.
2790 if (opt->ip6po_rthdr == NULL)
2791 newdest = &opt->ip6po_dest1;
2793 newdest = &opt->ip6po_dest2;
2795 case IPV6_RTHDRDSTOPTS:
2796 newdest = &opt->ip6po_dest1;
2799 newdest = &opt->ip6po_dest2;
2803 /* turn off the previous option, then set the new option. */
2804 ip6_clearpktopts(opt, optname);
2805 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2806 if (*newdest == NULL)
2808 bcopy(dest, *newdest, destlen);
2813 case IPV6_2292RTHDR:
2816 struct ip6_rthdr *rth;
2820 ip6_clearpktopts(opt, IPV6_RTHDR);
2821 break; /* just remove the option */
2824 /* message length validation */
2825 if (len < sizeof(struct ip6_rthdr))
2827 rth = (struct ip6_rthdr *)buf;
2828 rthlen = (rth->ip6r_len + 1) << 3;
2832 switch (rth->ip6r_type) {
2833 case IPV6_RTHDR_TYPE_0:
2834 if (rth->ip6r_len == 0) /* must contain one addr */
2836 if (rth->ip6r_len % 2) /* length must be even */
2838 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2842 return (EINVAL); /* not supported */
2845 /* turn off the previous option */
2846 ip6_clearpktopts(opt, IPV6_RTHDR);
2847 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2848 if (opt->ip6po_rthdr == NULL)
2850 bcopy(rth, opt->ip6po_rthdr, rthlen);
2855 case IPV6_USE_MIN_MTU:
2856 if (len != sizeof(int))
2858 minmtupolicy = *(int *)buf;
2859 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2860 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2861 minmtupolicy != IP6PO_MINMTU_ALL) {
2864 opt->ip6po_minmtu = minmtupolicy;
2868 if (len != sizeof(int))
2871 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2873 * we ignore this option for TCP sockets.
2874 * (RFC3542 leaves this case unspecified.)
2876 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2878 opt->ip6po_flags |= IP6PO_DONTFRAG;
2881 case IPV6_PREFER_TEMPADDR:
2882 if (len != sizeof(int))
2884 preftemp = *(int *)buf;
2885 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2886 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2887 preftemp != IP6PO_TEMPADDR_PREFER) {
2890 opt->ip6po_prefer_tempaddr = preftemp;
2894 return (ENOPROTOOPT);
2895 } /* end of switch */
2901 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2902 * packet to the input queue of a specified interface. Note that this
2903 * calls the output routine of the loopback "driver", but with an interface
2904 * pointer that might NOT be &loif -- easier than replicating that code here.
2907 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
2910 struct ip6_hdr *ip6;
2912 copym = m_copy(m, 0, M_COPYALL);
2917 * Make sure to deep-copy IPv6 header portion in case the data
2918 * is in an mbuf cluster, so that we can safely override the IPv6
2919 * header portion later.
2921 if ((copym->m_flags & M_EXT) != 0 ||
2922 copym->m_len < sizeof(struct ip6_hdr)) {
2923 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2929 if (copym->m_len < sizeof(*ip6)) {
2935 ip6 = mtod(copym, struct ip6_hdr *);
2937 * clear embedded scope identifiers if necessary.
2938 * in6_clearscope will touch the addresses only when necessary.
2940 in6_clearscope(&ip6->ip6_src);
2941 in6_clearscope(&ip6->ip6_dst);
2943 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
2947 * Chop IPv6 header off from the payload.
2950 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
2953 struct ip6_hdr *ip6;
2955 ip6 = mtod(m, struct ip6_hdr *);
2956 if (m->m_len > sizeof(*ip6)) {
2957 mh = m_gethdr(M_NOWAIT, MT_DATA);
2962 m_move_pkthdr(mh, m);
2963 MH_ALIGN(mh, sizeof(*ip6));
2964 m->m_len -= sizeof(*ip6);
2965 m->m_data += sizeof(*ip6);
2968 m->m_len = sizeof(*ip6);
2969 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2971 exthdrs->ip6e_ip6 = m;
2976 * Compute IPv6 extension header length.
2979 ip6_optlen(struct inpcb *in6p)
2983 if (!in6p->in6p_outputopts)
2988 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2990 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2991 if (in6p->in6p_outputopts->ip6po_rthdr)
2992 /* dest1 is valid with rthdr only */
2993 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2994 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2995 len += elen(in6p->in6p_outputopts->ip6po_dest2);