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"
73 #include <sys/param.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.h>
77 #include <sys/errno.h>
80 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/syslog.h>
84 #include <sys/ucred.h>
86 #include <machine/in_cksum.h>
89 #include <net/if_var.h>
90 #include <net/netisr.h>
91 #include <net/route.h>
95 #include <netinet/in.h>
96 #include <netinet/in_var.h>
97 #include <netinet/ip_var.h>
98 #include <netinet6/in6_var.h>
99 #include <netinet/ip6.h>
100 #include <netinet/icmp6.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet/in_pcb.h>
103 #include <netinet/tcp_var.h>
104 #include <netinet6/nd6.h>
107 #include <netipsec/ipsec.h>
108 #include <netipsec/ipsec6.h>
109 #include <netipsec/key.h>
110 #include <netinet6/ip6_ipsec.h>
113 #include <netinet/sctp.h>
114 #include <netinet/sctp_crc32.h>
117 #include <netinet6/ip6protosw.h>
118 #include <netinet6/scope6_var.h>
121 #include <net/flowtable.h>
124 extern int in6_mcast_loop;
127 struct mbuf *ip6e_ip6;
128 struct mbuf *ip6e_hbh;
129 struct mbuf *ip6e_dest1;
130 struct mbuf *ip6e_rthdr;
131 struct mbuf *ip6e_dest2;
134 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
135 struct ucred *, int);
136 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
137 struct socket *, struct sockopt *);
138 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
139 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
140 struct ucred *, int, int, int);
142 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
143 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
145 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
146 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
147 static int ip6_getpmtu(struct route_in6 *, struct route_in6 *,
148 struct ifnet *, struct in6_addr *, u_long *, int *, u_int);
149 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
153 * Make an extension header from option data. hp is the source, and
154 * mp is the destination.
156 #define MAKE_EXTHDR(hp, mp) \
159 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
160 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
161 ((eh)->ip6e_len + 1) << 3); \
165 } while (/*CONSTCOND*/ 0)
168 * Form a chain of extension headers.
169 * m is the extension header mbuf
170 * mp is the previous mbuf in the chain
171 * p is the next header
172 * i is the type of option.
174 #define MAKE_CHAIN(m, mp, p, i)\
178 panic("assumption failed: hdr not split"); \
179 *mtod((m), u_char *) = *(p);\
181 p = mtod((m), u_char *);\
182 (m)->m_next = (mp)->m_next;\
186 } while (/*CONSTCOND*/ 0)
189 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
193 csum = in_cksum_skip(m, offset + plen, offset);
194 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
196 offset += m->m_pkthdr.csum_data; /* checksum offset */
198 if (offset + sizeof(u_short) > m->m_len) {
199 printf("%s: delayed m_pullup, m->len: %d plen %u off %u "
200 "csum_flags=%b\n", __func__, m->m_len, plen, offset,
201 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
203 * XXX this should not happen, but if it does, the correct
204 * behavior may be to insert the checksum in the appropriate
205 * next mbuf in the chain.
209 *(u_short *)(m->m_data + offset) = csum;
213 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
214 * header (with pri, len, nxt, hlim, src, dst).
215 * This function may modify ver and hlim only.
216 * The mbuf chain containing the packet will be freed.
217 * The mbuf opt, if present, will not be freed.
218 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
219 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
220 * then result of route lookup is stored in ro->ro_rt.
222 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
223 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
226 * ifpp - XXX: just for statistics
229 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
230 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
231 struct ifnet **ifpp, struct inpcb *inp)
233 struct ip6_hdr *ip6, *mhip6;
234 struct ifnet *ifp, *origifp;
236 struct mbuf *mprev = NULL;
237 int hlen, tlen, len, off;
238 struct route_in6 ip6route;
239 struct rtentry *rt = NULL;
240 struct sockaddr_in6 *dst, src_sa, dst_sa;
241 struct in6_addr odst;
243 struct in6_ifaddr *ia = NULL;
245 int alwaysfrag, dontfrag;
246 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
247 struct ip6_exthdrs exthdrs;
248 struct in6_addr finaldst, src0, dst0;
250 struct route_in6 *ro_pmtu = NULL;
253 struct m_tag *fwd_tag = NULL;
255 ip6 = mtod(m, struct ip6_hdr *);
257 printf ("ip6 is NULL");
262 M_SETFIB(m, inp->inp_inc.inc_fibnum);
264 finaldst = ip6->ip6_dst;
265 bzero(&exthdrs, sizeof(exthdrs));
267 /* Hop-by-Hop options header */
268 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
269 /* Destination options header(1st part) */
270 if (opt->ip6po_rthdr) {
272 * Destination options header(1st part)
273 * This only makes sense with a routing header.
274 * See Section 9.2 of RFC 3542.
275 * Disabling this part just for MIP6 convenience is
276 * a bad idea. We need to think carefully about a
277 * way to make the advanced API coexist with MIP6
278 * options, which might automatically be inserted in
281 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
284 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
285 /* Destination options header(2nd part) */
286 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
291 * IPSec checking which handles several cases.
292 * FAST IPSEC: We re-injected the packet.
294 switch(ip6_ipsec_output(&m, inp, &flags, &error, &ifp))
296 case 1: /* Bad packet */
298 case -1: /* IPSec done */
300 case 0: /* No IPSec */
307 * Calculate the total length of the extension header chain.
308 * Keep the length of the unfragmentable part for fragmentation.
311 if (exthdrs.ip6e_hbh)
312 optlen += exthdrs.ip6e_hbh->m_len;
313 if (exthdrs.ip6e_dest1)
314 optlen += exthdrs.ip6e_dest1->m_len;
315 if (exthdrs.ip6e_rthdr)
316 optlen += exthdrs.ip6e_rthdr->m_len;
317 unfragpartlen = optlen + sizeof(struct ip6_hdr);
319 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
320 if (exthdrs.ip6e_dest2)
321 optlen += exthdrs.ip6e_dest2->m_len;
324 * If there is at least one extension header,
325 * separate IP6 header from the payload.
327 if (optlen && !hdrsplit) {
328 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
332 m = exthdrs.ip6e_ip6;
337 ip6 = mtod(m, struct ip6_hdr *);
339 /* adjust mbuf packet header length */
340 m->m_pkthdr.len += optlen;
341 plen = m->m_pkthdr.len - sizeof(*ip6);
343 /* If this is a jumbo payload, insert a jumbo payload option. */
344 if (plen > IPV6_MAXPACKET) {
346 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
350 m = exthdrs.ip6e_ip6;
354 ip6 = mtod(m, struct ip6_hdr *);
355 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
359 ip6->ip6_plen = htons(plen);
362 * Concatenate headers and fill in next header fields.
363 * Here we have, on "m"
365 * and we insert headers accordingly. Finally, we should be getting:
366 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
368 * during the header composing process, "m" points to IPv6 header.
369 * "mprev" points to an extension header prior to esp.
371 u_char *nexthdrp = &ip6->ip6_nxt;
375 * we treat dest2 specially. this makes IPsec processing
376 * much easier. the goal here is to make mprev point the
377 * mbuf prior to dest2.
379 * result: IPv6 dest2 payload
380 * m and mprev will point to IPv6 header.
382 if (exthdrs.ip6e_dest2) {
384 panic("assumption failed: hdr not split");
385 exthdrs.ip6e_dest2->m_next = m->m_next;
386 m->m_next = exthdrs.ip6e_dest2;
387 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
388 ip6->ip6_nxt = IPPROTO_DSTOPTS;
392 * result: IPv6 hbh dest1 rthdr dest2 payload
393 * m will point to IPv6 header. mprev will point to the
394 * extension header prior to dest2 (rthdr in the above case).
396 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
397 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
399 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
403 * If there is a routing header, discard the packet.
405 if (exthdrs.ip6e_rthdr) {
410 /* Source address validation */
411 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
412 (flags & IPV6_UNSPECSRC) == 0) {
414 IP6STAT_INC(ip6s_badscope);
417 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
419 IP6STAT_INC(ip6s_badscope);
423 IP6STAT_INC(ip6s_localout);
430 bzero((caddr_t)ro, sizeof(*ro));
433 if (opt && opt->ip6po_rthdr)
434 ro = &opt->ip6po_route;
435 dst = (struct sockaddr_in6 *)&ro->ro_dst;
437 if (ro->ro_rt == NULL)
438 (void )flowtable_lookup(AF_INET6, m, (struct route *)ro);
442 * if specified, try to fill in the traffic class field.
443 * do not override if a non-zero value is already set.
444 * we check the diffserv field and the ecn field separately.
446 if (opt && opt->ip6po_tclass >= 0) {
449 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
451 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
454 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
457 /* fill in or override the hop limit field, if necessary. */
458 if (opt && opt->ip6po_hlim != -1)
459 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
460 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
462 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
464 ip6->ip6_hlim = V_ip6_defmcasthlim;
468 ip6 = mtod(m, struct ip6_hdr *);
470 if (ro->ro_rt && fwd_tag == NULL) {
472 ifp = ro->ro_rt->rt_ifp;
474 if (fwd_tag == NULL) {
475 bzero(&dst_sa, sizeof(dst_sa));
476 dst_sa.sin6_family = AF_INET6;
477 dst_sa.sin6_len = sizeof(dst_sa);
478 dst_sa.sin6_addr = ip6->ip6_dst;
480 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
481 &rt, inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m));
484 in6_ifstat_inc(ifp, ifs6_out_discard);
490 * If in6_selectroute() does not return a route entry,
491 * dst may not have been updated.
493 *dst = dst_sa; /* XXX */
497 * then rt (for unicast) and ifp must be non-NULL valid values.
499 if ((flags & IPV6_FORWARDING) == 0) {
500 /* XXX: the FORWARDING flag can be set for mrouting. */
501 in6_ifstat_inc(ifp, ifs6_out_request);
504 ia = (struct in6_ifaddr *)(rt->rt_ifa);
505 counter_u64_add(rt->rt_pksent, 1);
510 * The outgoing interface must be in the zone of source and
511 * destination addresses.
516 if (in6_setscope(&src0, origifp, &zone))
518 bzero(&src_sa, sizeof(src_sa));
519 src_sa.sin6_family = AF_INET6;
520 src_sa.sin6_len = sizeof(src_sa);
521 src_sa.sin6_addr = ip6->ip6_src;
522 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
526 if (in6_setscope(&dst0, origifp, &zone))
528 /* re-initialize to be sure */
529 bzero(&dst_sa, sizeof(dst_sa));
530 dst_sa.sin6_family = AF_INET6;
531 dst_sa.sin6_len = sizeof(dst_sa);
532 dst_sa.sin6_addr = ip6->ip6_dst;
533 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
537 /* We should use ia_ifp to support the case of
538 * sending packets to an address of our own.
540 if (ia != NULL && ia->ia_ifp)
543 /* scope check is done. */
547 IP6STAT_INC(ip6s_badscope);
548 in6_ifstat_inc(origifp, ifs6_out_discard);
550 error = EHOSTUNREACH; /* XXX */
554 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
555 if (opt && opt->ip6po_nextroute.ro_rt) {
557 * The nexthop is explicitly specified by the
558 * application. We assume the next hop is an IPv6
561 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
563 else if ((rt->rt_flags & RTF_GATEWAY))
564 dst = (struct sockaddr_in6 *)rt->rt_gateway;
567 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
568 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
570 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
571 in6_ifstat_inc(ifp, ifs6_out_mcast);
573 * Confirm that the outgoing interface supports multicast.
575 if (!(ifp->if_flags & IFF_MULTICAST)) {
576 IP6STAT_INC(ip6s_noroute);
577 in6_ifstat_inc(ifp, ifs6_out_discard);
581 if ((im6o == NULL && in6_mcast_loop) ||
582 (im6o && im6o->im6o_multicast_loop)) {
584 * Loop back multicast datagram if not expressly
585 * forbidden to do so, even if we have not joined
586 * the address; protocols will filter it later,
587 * thus deferring a hash lookup and lock acquisition
588 * at the expense of an m_copym().
590 ip6_mloopback(ifp, m, dst);
593 * If we are acting as a multicast router, perform
594 * multicast forwarding as if the packet had just
595 * arrived on the interface to which we are about
596 * to send. The multicast forwarding function
597 * recursively calls this function, using the
598 * IPV6_FORWARDING flag to prevent infinite recursion.
600 * Multicasts that are looped back by ip6_mloopback(),
601 * above, will be forwarded by the ip6_input() routine,
604 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
606 * XXX: ip6_mforward expects that rcvif is NULL
607 * when it is called from the originating path.
608 * However, it may not always be the case.
610 m->m_pkthdr.rcvif = NULL;
611 if (ip6_mforward(ip6, ifp, m) != 0) {
618 * Multicasts with a hoplimit of zero may be looped back,
619 * above, but must not be transmitted on a network.
620 * Also, multicasts addressed to the loopback interface
621 * are not sent -- the above call to ip6_mloopback() will
622 * loop back a copy if this host actually belongs to the
623 * destination group on the loopback interface.
625 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
626 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
633 * Fill the outgoing inteface to tell the upper layer
634 * to increment per-interface statistics.
639 /* Determine path MTU. */
640 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
641 &alwaysfrag, inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m))) != 0)
645 * The caller of this function may specify to use the minimum MTU
647 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
648 * setting. The logic is a bit complicated; by default, unicast
649 * packets will follow path MTU while multicast packets will be sent at
650 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
651 * including unicast ones will be sent at the minimum MTU. Multicast
652 * packets will always be sent at the minimum MTU unless
653 * IP6PO_MINMTU_DISABLE is explicitly specified.
654 * See RFC 3542 for more details.
656 if (mtu > IPV6_MMTU) {
657 if ((flags & IPV6_MINMTU))
659 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
661 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
663 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
669 * clear embedded scope identifiers if necessary.
670 * in6_clearscope will touch the addresses only when necessary.
672 in6_clearscope(&ip6->ip6_src);
673 in6_clearscope(&ip6->ip6_dst);
676 * If the outgoing packet contains a hop-by-hop options header,
677 * it must be examined and processed even by the source node.
678 * (RFC 2460, section 4.)
680 if (exthdrs.ip6e_hbh) {
681 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
682 u_int32_t dummy; /* XXX unused */
683 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
686 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
687 panic("ip6e_hbh is not contiguous");
690 * XXX: if we have to send an ICMPv6 error to the sender,
691 * we need the M_LOOP flag since icmp6_error() expects
692 * the IPv6 and the hop-by-hop options header are
693 * contiguous unless the flag is set.
695 m->m_flags |= M_LOOP;
696 m->m_pkthdr.rcvif = ifp;
697 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
698 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
699 &dummy, &plen) < 0) {
700 /* m was already freed at this point */
701 error = EINVAL;/* better error? */
704 m->m_flags &= ~M_LOOP; /* XXX */
705 m->m_pkthdr.rcvif = NULL;
708 /* Jump over all PFIL processing if hooks are not active. */
709 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
713 /* Run through list of hooks for output packets. */
714 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
715 if (error != 0 || m == NULL)
717 ip6 = mtod(m, struct ip6_hdr *);
719 /* See if destination IP address was changed by packet filter. */
720 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
721 m->m_flags |= M_SKIP_FIREWALL;
722 /* If destination is now ourself drop to ip6_input(). */
723 if (in6_localip(&ip6->ip6_dst)) {
724 m->m_flags |= M_FASTFWD_OURS;
725 if (m->m_pkthdr.rcvif == NULL)
726 m->m_pkthdr.rcvif = V_loif;
727 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
728 m->m_pkthdr.csum_flags |=
729 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
730 m->m_pkthdr.csum_data = 0xffff;
733 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
734 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
736 error = netisr_queue(NETISR_IPV6, m);
739 goto again; /* Redo the routing table lookup. */
742 /* See if local, if yes, send it to netisr. */
743 if (m->m_flags & M_FASTFWD_OURS) {
744 if (m->m_pkthdr.rcvif == NULL)
745 m->m_pkthdr.rcvif = V_loif;
746 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
747 m->m_pkthdr.csum_flags |=
748 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
749 m->m_pkthdr.csum_data = 0xffff;
752 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
753 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
755 error = netisr_queue(NETISR_IPV6, m);
758 /* Or forward to some other address? */
759 if ((m->m_flags & M_IP6_NEXTHOP) &&
760 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
761 dst = (struct sockaddr_in6 *)&ro->ro_dst;
762 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
763 m->m_flags |= M_SKIP_FIREWALL;
764 m->m_flags &= ~M_IP6_NEXTHOP;
765 m_tag_delete(m, fwd_tag);
771 * Send the packet to the outgoing interface.
772 * If necessary, do IPv6 fragmentation before sending.
774 * the logic here is rather complex:
775 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
776 * 1-a: send as is if tlen <= path mtu
777 * 1-b: fragment if tlen > path mtu
779 * 2: if user asks us not to fragment (dontfrag == 1)
780 * 2-a: send as is if tlen <= interface mtu
781 * 2-b: error if tlen > interface mtu
783 * 3: if we always need to attach fragment header (alwaysfrag == 1)
786 * 4: if dontfrag == 1 && alwaysfrag == 1
787 * error, as we cannot handle this conflicting request
789 sw_csum = m->m_pkthdr.csum_flags;
791 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
792 sw_csum &= ~ifp->if_hwassist;
796 * If we added extension headers, we will not do TSO and calculate the
797 * checksums ourselves for now.
798 * XXX-BZ Need a framework to know when the NIC can handle it, even
801 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
802 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
803 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
806 if (sw_csum & CSUM_SCTP_IPV6) {
807 sw_csum &= ~CSUM_SCTP_IPV6;
808 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
811 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
812 tlen = m->m_pkthdr.len;
814 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
818 if (dontfrag && alwaysfrag) { /* case 4 */
819 /* conflicting request - can't transmit */
823 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
825 * Even if the DONTFRAG option is specified, we cannot send the
826 * packet when the data length is larger than the MTU of the
827 * outgoing interface.
828 * Notify the error by sending IPV6_PATHMTU ancillary data as
829 * well as returning an error code (the latter is not described
833 struct ip6ctlparam ip6cp;
835 mtu32 = (u_int32_t)mtu;
836 bzero(&ip6cp, sizeof(ip6cp));
837 ip6cp.ip6c_cmdarg = (void *)&mtu32;
838 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
846 * transmit packet without fragmentation
848 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
849 struct in6_ifaddr *ia6;
851 ip6 = mtod(m, struct ip6_hdr *);
852 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
854 /* Record statistics for this interface address. */
855 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
856 counter_u64_add(ia6->ia_ifa.ifa_obytes,
858 ifa_free(&ia6->ia_ifa);
860 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
865 * try to fragment the packet. case 1-b and 3
867 if (mtu < IPV6_MMTU) {
868 /* path MTU cannot be less than IPV6_MMTU */
870 in6_ifstat_inc(ifp, ifs6_out_fragfail);
872 } else if (ip6->ip6_plen == 0) {
873 /* jumbo payload cannot be fragmented */
875 in6_ifstat_inc(ifp, ifs6_out_fragfail);
878 struct mbuf **mnext, *m_frgpart;
879 struct ip6_frag *ip6f;
880 u_int32_t id = htonl(ip6_randomid());
883 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
886 * Too large for the destination or interface;
887 * fragment if possible.
888 * Must be able to put at least 8 bytes per fragment.
890 hlen = unfragpartlen;
891 if (mtu > IPV6_MAXPACKET)
892 mtu = IPV6_MAXPACKET;
894 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
897 in6_ifstat_inc(ifp, ifs6_out_fragfail);
902 * Verify that we have any chance at all of being able to queue
903 * the packet or packet fragments
905 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
906 < tlen /* - hlen */)) {
908 IP6STAT_INC(ip6s_odropped);
914 * If the interface will not calculate checksums on
915 * fragmented packets, then do it here.
916 * XXX-BZ handle the hw offloading case. Need flags.
918 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
919 in6_delayed_cksum(m, plen, hlen);
920 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
923 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
924 sctp_delayed_cksum(m, hlen);
925 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
928 mnext = &m->m_nextpkt;
931 * Change the next header field of the last header in the
932 * unfragmentable part.
934 if (exthdrs.ip6e_rthdr) {
935 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
936 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
937 } else if (exthdrs.ip6e_dest1) {
938 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
939 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
940 } else if (exthdrs.ip6e_hbh) {
941 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
942 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
944 nextproto = ip6->ip6_nxt;
945 ip6->ip6_nxt = IPPROTO_FRAGMENT;
949 * Loop through length of segment after first fragment,
950 * make new header and copy data of each part and link onto
954 for (off = hlen; off < tlen; off += len) {
955 m = m_gethdr(M_NOWAIT, MT_DATA);
958 IP6STAT_INC(ip6s_odropped);
961 m->m_flags = m0->m_flags & M_COPYFLAGS;
963 mnext = &m->m_nextpkt;
964 m->m_data += max_linkhdr;
965 mhip6 = mtod(m, struct ip6_hdr *);
967 m->m_len = sizeof(*mhip6);
968 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
970 IP6STAT_INC(ip6s_odropped);
973 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
974 if (off + len >= tlen)
977 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
978 mhip6->ip6_plen = htons((u_short)(len + hlen +
979 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
980 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
982 IP6STAT_INC(ip6s_odropped);
986 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
987 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
988 m->m_pkthdr.rcvif = NULL;
989 ip6f->ip6f_reserved = 0;
990 ip6f->ip6f_ident = id;
991 ip6f->ip6f_nxt = nextproto;
992 IP6STAT_INC(ip6s_ofragments);
993 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
996 in6_ifstat_inc(ifp, ifs6_out_fragok);
1000 * Remove leading garbages.
1006 for (m0 = m; m; m = m0) {
1010 /* Record statistics for this interface address. */
1012 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1013 counter_u64_add(ia->ia_ifa.ifa_obytes,
1016 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1022 IP6STAT_INC(ip6s_fragmented);
1025 if (ro == &ip6route)
1027 if (ro_pmtu == &ip6route)
1032 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1033 m_freem(exthdrs.ip6e_dest1);
1034 m_freem(exthdrs.ip6e_rthdr);
1035 m_freem(exthdrs.ip6e_dest2);
1044 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1048 if (hlen > MCLBYTES)
1049 return (ENOBUFS); /* XXX */
1052 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1054 m = m_get(M_NOWAIT, MT_DATA);
1059 bcopy(hdr, mtod(m, caddr_t), hlen);
1066 * Insert jumbo payload option.
1069 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1075 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1078 * If there is no hop-by-hop options header, allocate new one.
1079 * If there is one but it doesn't have enough space to store the
1080 * jumbo payload option, allocate a cluster to store the whole options.
1081 * Otherwise, use it to store the options.
1083 if (exthdrs->ip6e_hbh == 0) {
1084 mopt = m_get(M_NOWAIT, MT_DATA);
1087 mopt->m_len = JUMBOOPTLEN;
1088 optbuf = mtod(mopt, u_char *);
1089 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1090 exthdrs->ip6e_hbh = mopt;
1092 struct ip6_hbh *hbh;
1094 mopt = exthdrs->ip6e_hbh;
1095 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1098 * - exthdrs->ip6e_hbh is not referenced from places
1099 * other than exthdrs.
1100 * - exthdrs->ip6e_hbh is not an mbuf chain.
1102 int oldoptlen = mopt->m_len;
1106 * XXX: give up if the whole (new) hbh header does
1107 * not fit even in an mbuf cluster.
1109 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1113 * As a consequence, we must always prepare a cluster
1116 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1119 n->m_len = oldoptlen + JUMBOOPTLEN;
1120 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1122 optbuf = mtod(n, caddr_t) + oldoptlen;
1124 mopt = exthdrs->ip6e_hbh = n;
1126 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1127 mopt->m_len += JUMBOOPTLEN;
1129 optbuf[0] = IP6OPT_PADN;
1133 * Adjust the header length according to the pad and
1134 * the jumbo payload option.
1136 hbh = mtod(mopt, struct ip6_hbh *);
1137 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1140 /* fill in the option. */
1141 optbuf[2] = IP6OPT_JUMBO;
1143 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1144 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1146 /* finally, adjust the packet header length */
1147 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1154 * Insert fragment header and copy unfragmentable header portions.
1157 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1158 struct ip6_frag **frghdrp)
1160 struct mbuf *n, *mlast;
1162 if (hlen > sizeof(struct ip6_hdr)) {
1163 n = m_copym(m0, sizeof(struct ip6_hdr),
1164 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1171 /* Search for the last mbuf of unfragmentable part. */
1172 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1175 if ((mlast->m_flags & M_EXT) == 0 &&
1176 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1177 /* use the trailing space of the last mbuf for the fragment hdr */
1178 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1180 mlast->m_len += sizeof(struct ip6_frag);
1181 m->m_pkthdr.len += sizeof(struct ip6_frag);
1183 /* allocate a new mbuf for the fragment header */
1186 mfrg = m_get(M_NOWAIT, MT_DATA);
1189 mfrg->m_len = sizeof(struct ip6_frag);
1190 *frghdrp = mtod(mfrg, struct ip6_frag *);
1191 mlast->m_next = mfrg;
1198 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1199 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1200 int *alwaysfragp, u_int fibnum)
1206 if (ro_pmtu != ro) {
1207 /* The first hop and the final destination may differ. */
1208 struct sockaddr_in6 *sa6_dst =
1209 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1210 if (ro_pmtu->ro_rt &&
1211 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1212 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1213 RTFREE(ro_pmtu->ro_rt);
1214 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1216 if (ro_pmtu->ro_rt == NULL) {
1217 bzero(sa6_dst, sizeof(*sa6_dst));
1218 sa6_dst->sin6_family = AF_INET6;
1219 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1220 sa6_dst->sin6_addr = *dst;
1222 in6_rtalloc(ro_pmtu, fibnum);
1225 if (ro_pmtu->ro_rt) {
1227 struct in_conninfo inc;
1229 bzero(&inc, sizeof(inc));
1230 inc.inc_flags |= INC_ISIPV6;
1231 inc.inc6_faddr = *dst;
1234 ifp = ro_pmtu->ro_rt->rt_ifp;
1235 ifmtu = IN6_LINKMTU(ifp);
1236 mtu = tcp_hc_getmtu(&inc);
1238 mtu = min(mtu, ro_pmtu->ro_rt->rt_mtu);
1240 mtu = ro_pmtu->ro_rt->rt_mtu;
1243 else if (mtu < IPV6_MMTU) {
1245 * RFC2460 section 5, last paragraph:
1246 * if we record ICMPv6 too big message with
1247 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1248 * or smaller, with framgent header attached.
1249 * (fragment header is needed regardless from the
1250 * packet size, for translators to identify packets)
1254 } else if (mtu > ifmtu) {
1256 * The MTU on the route is larger than the MTU on
1257 * the interface! This shouldn't happen, unless the
1258 * MTU of the interface has been changed after the
1259 * interface was brought up. Change the MTU in the
1260 * route to match the interface MTU (as long as the
1261 * field isn't locked).
1264 ro_pmtu->ro_rt->rt_mtu = mtu;
1267 mtu = IN6_LINKMTU(ifp);
1269 error = EHOSTUNREACH; /* XXX */
1273 *alwaysfragp = alwaysfrag;
1278 * IP6 socket option processing.
1281 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1283 int optdatalen, uproto;
1285 struct inpcb *in6p = sotoinpcb(so);
1287 int level, op, optname;
1291 level = sopt->sopt_level;
1292 op = sopt->sopt_dir;
1293 optname = sopt->sopt_name;
1294 optlen = sopt->sopt_valsize;
1298 uproto = (int)so->so_proto->pr_protocol;
1300 if (level != IPPROTO_IPV6) {
1303 if (sopt->sopt_level == SOL_SOCKET &&
1304 sopt->sopt_dir == SOPT_SET) {
1305 switch (sopt->sopt_name) {
1308 if ((so->so_options & SO_REUSEADDR) != 0)
1309 in6p->inp_flags2 |= INP_REUSEADDR;
1311 in6p->inp_flags2 &= ~INP_REUSEADDR;
1317 if ((so->so_options & SO_REUSEPORT) != 0)
1318 in6p->inp_flags2 |= INP_REUSEPORT;
1320 in6p->inp_flags2 &= ~INP_REUSEPORT;
1326 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1334 } else { /* level == IPPROTO_IPV6 */
1339 case IPV6_2292PKTOPTIONS:
1340 #ifdef IPV6_PKTOPTIONS
1341 case IPV6_PKTOPTIONS:
1346 error = soopt_getm(sopt, &m); /* XXX */
1349 error = soopt_mcopyin(sopt, m); /* XXX */
1352 error = ip6_pcbopts(&in6p->in6p_outputopts,
1354 m_freem(m); /* XXX */
1359 * Use of some Hop-by-Hop options or some
1360 * Destination options, might require special
1361 * privilege. That is, normal applications
1362 * (without special privilege) might be forbidden
1363 * from setting certain options in outgoing packets,
1364 * and might never see certain options in received
1365 * packets. [RFC 2292 Section 6]
1366 * KAME specific note:
1367 * KAME prevents non-privileged users from sending or
1368 * receiving ANY hbh/dst options in order to avoid
1369 * overhead of parsing options in the kernel.
1371 case IPV6_RECVHOPOPTS:
1372 case IPV6_RECVDSTOPTS:
1373 case IPV6_RECVRTHDRDSTOPTS:
1375 error = priv_check(td,
1376 PRIV_NETINET_SETHDROPTS);
1381 case IPV6_UNICAST_HOPS:
1385 case IPV6_RECVPKTINFO:
1386 case IPV6_RECVHOPLIMIT:
1387 case IPV6_RECVRTHDR:
1388 case IPV6_RECVPATHMTU:
1389 case IPV6_RECVTCLASS:
1391 case IPV6_AUTOFLOWLABEL:
1393 if (optname == IPV6_BINDANY && td != NULL) {
1394 error = priv_check(td,
1395 PRIV_NETINET_BINDANY);
1400 if (optlen != sizeof(int)) {
1404 error = sooptcopyin(sopt, &optval,
1405 sizeof optval, sizeof optval);
1410 case IPV6_UNICAST_HOPS:
1411 if (optval < -1 || optval >= 256)
1414 /* -1 = kernel default */
1415 in6p->in6p_hops = optval;
1416 if ((in6p->inp_vflag &
1418 in6p->inp_ip_ttl = optval;
1421 #define OPTSET(bit) \
1425 in6p->inp_flags |= (bit); \
1427 in6p->inp_flags &= ~(bit); \
1428 INP_WUNLOCK(in6p); \
1429 } while (/*CONSTCOND*/ 0)
1430 #define OPTSET2292(bit) \
1433 in6p->inp_flags |= IN6P_RFC2292; \
1435 in6p->inp_flags |= (bit); \
1437 in6p->inp_flags &= ~(bit); \
1438 INP_WUNLOCK(in6p); \
1439 } while (/*CONSTCOND*/ 0)
1440 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1442 case IPV6_RECVPKTINFO:
1443 /* cannot mix with RFC2292 */
1444 if (OPTBIT(IN6P_RFC2292)) {
1448 OPTSET(IN6P_PKTINFO);
1453 struct ip6_pktopts **optp;
1455 /* cannot mix with RFC2292 */
1456 if (OPTBIT(IN6P_RFC2292)) {
1460 optp = &in6p->in6p_outputopts;
1461 error = ip6_pcbopt(IPV6_HOPLIMIT,
1462 (u_char *)&optval, sizeof(optval),
1463 optp, (td != NULL) ? td->td_ucred :
1468 case IPV6_RECVHOPLIMIT:
1469 /* cannot mix with RFC2292 */
1470 if (OPTBIT(IN6P_RFC2292)) {
1474 OPTSET(IN6P_HOPLIMIT);
1477 case IPV6_RECVHOPOPTS:
1478 /* cannot mix with RFC2292 */
1479 if (OPTBIT(IN6P_RFC2292)) {
1483 OPTSET(IN6P_HOPOPTS);
1486 case IPV6_RECVDSTOPTS:
1487 /* cannot mix with RFC2292 */
1488 if (OPTBIT(IN6P_RFC2292)) {
1492 OPTSET(IN6P_DSTOPTS);
1495 case IPV6_RECVRTHDRDSTOPTS:
1496 /* cannot mix with RFC2292 */
1497 if (OPTBIT(IN6P_RFC2292)) {
1501 OPTSET(IN6P_RTHDRDSTOPTS);
1504 case IPV6_RECVRTHDR:
1505 /* cannot mix with RFC2292 */
1506 if (OPTBIT(IN6P_RFC2292)) {
1517 case IPV6_RECVPATHMTU:
1519 * We ignore this option for TCP
1521 * (RFC3542 leaves this case
1524 if (uproto != IPPROTO_TCP)
1530 * make setsockopt(IPV6_V6ONLY)
1531 * available only prior to bind(2).
1532 * see ipng mailing list, Jun 22 2001.
1534 if (in6p->inp_lport ||
1535 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1539 OPTSET(IN6P_IPV6_V6ONLY);
1541 in6p->inp_vflag &= ~INP_IPV4;
1543 in6p->inp_vflag |= INP_IPV4;
1545 case IPV6_RECVTCLASS:
1546 /* cannot mix with RFC2292 XXX */
1547 if (OPTBIT(IN6P_RFC2292)) {
1551 OPTSET(IN6P_TCLASS);
1553 case IPV6_AUTOFLOWLABEL:
1554 OPTSET(IN6P_AUTOFLOWLABEL);
1558 OPTSET(INP_BINDANY);
1565 case IPV6_USE_MIN_MTU:
1566 case IPV6_PREFER_TEMPADDR:
1567 if (optlen != sizeof(optval)) {
1571 error = sooptcopyin(sopt, &optval,
1572 sizeof optval, sizeof optval);
1576 struct ip6_pktopts **optp;
1577 optp = &in6p->in6p_outputopts;
1578 error = ip6_pcbopt(optname,
1579 (u_char *)&optval, sizeof(optval),
1580 optp, (td != NULL) ? td->td_ucred :
1585 case IPV6_2292PKTINFO:
1586 case IPV6_2292HOPLIMIT:
1587 case IPV6_2292HOPOPTS:
1588 case IPV6_2292DSTOPTS:
1589 case IPV6_2292RTHDR:
1591 if (optlen != sizeof(int)) {
1595 error = sooptcopyin(sopt, &optval,
1596 sizeof optval, sizeof optval);
1600 case IPV6_2292PKTINFO:
1601 OPTSET2292(IN6P_PKTINFO);
1603 case IPV6_2292HOPLIMIT:
1604 OPTSET2292(IN6P_HOPLIMIT);
1606 case IPV6_2292HOPOPTS:
1608 * Check super-user privilege.
1609 * See comments for IPV6_RECVHOPOPTS.
1612 error = priv_check(td,
1613 PRIV_NETINET_SETHDROPTS);
1617 OPTSET2292(IN6P_HOPOPTS);
1619 case IPV6_2292DSTOPTS:
1621 error = priv_check(td,
1622 PRIV_NETINET_SETHDROPTS);
1626 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1628 case IPV6_2292RTHDR:
1629 OPTSET2292(IN6P_RTHDR);
1637 case IPV6_RTHDRDSTOPTS:
1640 /* new advanced API (RFC3542) */
1642 u_char optbuf_storage[MCLBYTES];
1644 struct ip6_pktopts **optp;
1646 /* cannot mix with RFC2292 */
1647 if (OPTBIT(IN6P_RFC2292)) {
1653 * We only ensure valsize is not too large
1654 * here. Further validation will be done
1657 error = sooptcopyin(sopt, optbuf_storage,
1658 sizeof(optbuf_storage), 0);
1661 optlen = sopt->sopt_valsize;
1662 optbuf = optbuf_storage;
1663 optp = &in6p->in6p_outputopts;
1664 error = ip6_pcbopt(optname, optbuf, optlen,
1665 optp, (td != NULL) ? td->td_ucred : NULL,
1671 case IPV6_MULTICAST_IF:
1672 case IPV6_MULTICAST_HOPS:
1673 case IPV6_MULTICAST_LOOP:
1674 case IPV6_JOIN_GROUP:
1675 case IPV6_LEAVE_GROUP:
1677 case MCAST_BLOCK_SOURCE:
1678 case MCAST_UNBLOCK_SOURCE:
1679 case MCAST_JOIN_GROUP:
1680 case MCAST_LEAVE_GROUP:
1681 case MCAST_JOIN_SOURCE_GROUP:
1682 case MCAST_LEAVE_SOURCE_GROUP:
1683 error = ip6_setmoptions(in6p, sopt);
1686 case IPV6_PORTRANGE:
1687 error = sooptcopyin(sopt, &optval,
1688 sizeof optval, sizeof optval);
1694 case IPV6_PORTRANGE_DEFAULT:
1695 in6p->inp_flags &= ~(INP_LOWPORT);
1696 in6p->inp_flags &= ~(INP_HIGHPORT);
1699 case IPV6_PORTRANGE_HIGH:
1700 in6p->inp_flags &= ~(INP_LOWPORT);
1701 in6p->inp_flags |= INP_HIGHPORT;
1704 case IPV6_PORTRANGE_LOW:
1705 in6p->inp_flags &= ~(INP_HIGHPORT);
1706 in6p->inp_flags |= INP_LOWPORT;
1717 case IPV6_IPSEC_POLICY:
1722 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1724 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1726 req = mtod(m, caddr_t);
1727 error = ipsec_set_policy(in6p, optname, req,
1728 m->m_len, (sopt->sopt_td != NULL) ?
1729 sopt->sopt_td->td_ucred : NULL);
1736 error = ENOPROTOOPT;
1744 case IPV6_2292PKTOPTIONS:
1745 #ifdef IPV6_PKTOPTIONS
1746 case IPV6_PKTOPTIONS:
1749 * RFC3542 (effectively) deprecated the
1750 * semantics of the 2292-style pktoptions.
1751 * Since it was not reliable in nature (i.e.,
1752 * applications had to expect the lack of some
1753 * information after all), it would make sense
1754 * to simplify this part by always returning
1757 sopt->sopt_valsize = 0;
1760 case IPV6_RECVHOPOPTS:
1761 case IPV6_RECVDSTOPTS:
1762 case IPV6_RECVRTHDRDSTOPTS:
1763 case IPV6_UNICAST_HOPS:
1764 case IPV6_RECVPKTINFO:
1765 case IPV6_RECVHOPLIMIT:
1766 case IPV6_RECVRTHDR:
1767 case IPV6_RECVPATHMTU:
1771 case IPV6_PORTRANGE:
1772 case IPV6_RECVTCLASS:
1773 case IPV6_AUTOFLOWLABEL:
1777 case IPV6_RECVHOPOPTS:
1778 optval = OPTBIT(IN6P_HOPOPTS);
1781 case IPV6_RECVDSTOPTS:
1782 optval = OPTBIT(IN6P_DSTOPTS);
1785 case IPV6_RECVRTHDRDSTOPTS:
1786 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1789 case IPV6_UNICAST_HOPS:
1790 optval = in6p->in6p_hops;
1793 case IPV6_RECVPKTINFO:
1794 optval = OPTBIT(IN6P_PKTINFO);
1797 case IPV6_RECVHOPLIMIT:
1798 optval = OPTBIT(IN6P_HOPLIMIT);
1801 case IPV6_RECVRTHDR:
1802 optval = OPTBIT(IN6P_RTHDR);
1805 case IPV6_RECVPATHMTU:
1806 optval = OPTBIT(IN6P_MTU);
1810 optval = OPTBIT(INP_FAITH);
1814 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1817 case IPV6_PORTRANGE:
1820 flags = in6p->inp_flags;
1821 if (flags & INP_HIGHPORT)
1822 optval = IPV6_PORTRANGE_HIGH;
1823 else if (flags & INP_LOWPORT)
1824 optval = IPV6_PORTRANGE_LOW;
1829 case IPV6_RECVTCLASS:
1830 optval = OPTBIT(IN6P_TCLASS);
1833 case IPV6_AUTOFLOWLABEL:
1834 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1838 optval = OPTBIT(INP_BINDANY);
1843 error = sooptcopyout(sopt, &optval,
1850 struct ip6_mtuinfo mtuinfo;
1851 struct route_in6 sro;
1853 bzero(&sro, sizeof(sro));
1855 if (!(so->so_state & SS_ISCONNECTED))
1858 * XXX: we dot not consider the case of source
1859 * routing, or optional information to specify
1860 * the outgoing interface.
1862 error = ip6_getpmtu(&sro, NULL, NULL,
1863 &in6p->in6p_faddr, &pmtu, NULL,
1869 if (pmtu > IPV6_MAXPACKET)
1870 pmtu = IPV6_MAXPACKET;
1872 bzero(&mtuinfo, sizeof(mtuinfo));
1873 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1874 optdata = (void *)&mtuinfo;
1875 optdatalen = sizeof(mtuinfo);
1876 error = sooptcopyout(sopt, optdata,
1881 case IPV6_2292PKTINFO:
1882 case IPV6_2292HOPLIMIT:
1883 case IPV6_2292HOPOPTS:
1884 case IPV6_2292RTHDR:
1885 case IPV6_2292DSTOPTS:
1887 case IPV6_2292PKTINFO:
1888 optval = OPTBIT(IN6P_PKTINFO);
1890 case IPV6_2292HOPLIMIT:
1891 optval = OPTBIT(IN6P_HOPLIMIT);
1893 case IPV6_2292HOPOPTS:
1894 optval = OPTBIT(IN6P_HOPOPTS);
1896 case IPV6_2292RTHDR:
1897 optval = OPTBIT(IN6P_RTHDR);
1899 case IPV6_2292DSTOPTS:
1900 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1903 error = sooptcopyout(sopt, &optval,
1910 case IPV6_RTHDRDSTOPTS:
1914 case IPV6_USE_MIN_MTU:
1915 case IPV6_PREFER_TEMPADDR:
1916 error = ip6_getpcbopt(in6p->in6p_outputopts,
1920 case IPV6_MULTICAST_IF:
1921 case IPV6_MULTICAST_HOPS:
1922 case IPV6_MULTICAST_LOOP:
1924 error = ip6_getmoptions(in6p, sopt);
1928 case IPV6_IPSEC_POLICY:
1932 struct mbuf *m = NULL;
1933 struct mbuf **mp = &m;
1934 size_t ovalsize = sopt->sopt_valsize;
1935 caddr_t oval = (caddr_t)sopt->sopt_val;
1937 error = soopt_getm(sopt, &m); /* XXX */
1940 error = soopt_mcopyin(sopt, m); /* XXX */
1943 sopt->sopt_valsize = ovalsize;
1944 sopt->sopt_val = oval;
1946 req = mtod(m, caddr_t);
1949 error = ipsec_get_policy(in6p, req, len, mp);
1951 error = soopt_mcopyout(sopt, m); /* XXX */
1952 if (error == 0 && m)
1959 error = ENOPROTOOPT;
1969 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1971 int error = 0, optval, optlen;
1972 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
1973 struct inpcb *in6p = sotoinpcb(so);
1974 int level, op, optname;
1976 level = sopt->sopt_level;
1977 op = sopt->sopt_dir;
1978 optname = sopt->sopt_name;
1979 optlen = sopt->sopt_valsize;
1981 if (level != IPPROTO_IPV6) {
1988 * For ICMPv6 sockets, no modification allowed for checksum
1989 * offset, permit "no change" values to help existing apps.
1991 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
1992 * for an ICMPv6 socket will fail."
1993 * The current behavior does not meet RFC3542.
1997 if (optlen != sizeof(int)) {
2001 error = sooptcopyin(sopt, &optval, sizeof(optval),
2005 if ((optval % 2) != 0) {
2006 /* the API assumes even offset values */
2008 } else if (so->so_proto->pr_protocol ==
2010 if (optval != icmp6off)
2013 in6p->in6p_cksum = optval;
2017 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2020 optval = in6p->in6p_cksum;
2022 error = sooptcopyout(sopt, &optval, sizeof(optval));
2032 error = ENOPROTOOPT;
2040 * Set up IP6 options in pcb for insertion in output packets or
2041 * specifying behavior of outgoing packets.
2044 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2045 struct socket *so, struct sockopt *sopt)
2047 struct ip6_pktopts *opt = *pktopt;
2049 struct thread *td = sopt->sopt_td;
2051 /* turn off any old options. */
2054 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2055 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2056 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2057 printf("ip6_pcbopts: all specified options are cleared.\n");
2059 ip6_clearpktopts(opt, -1);
2061 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2064 if (!m || m->m_len == 0) {
2066 * Only turning off any previous options, regardless of
2067 * whether the opt is just created or given.
2069 free(opt, M_IP6OPT);
2073 /* set options specified by user. */
2074 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2075 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2076 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2077 free(opt, M_IP6OPT);
2085 * initialize ip6_pktopts. beware that there are non-zero default values in
2089 ip6_initpktopts(struct ip6_pktopts *opt)
2092 bzero(opt, sizeof(*opt));
2093 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2094 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2095 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2096 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2100 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2101 struct ucred *cred, int uproto)
2103 struct ip6_pktopts *opt;
2105 if (*pktopt == NULL) {
2106 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2108 ip6_initpktopts(*pktopt);
2112 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2116 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2118 void *optdata = NULL;
2120 struct ip6_ext *ip6e;
2122 struct in6_pktinfo null_pktinfo;
2123 int deftclass = 0, on;
2124 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2125 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2129 if (pktopt && pktopt->ip6po_pktinfo)
2130 optdata = (void *)pktopt->ip6po_pktinfo;
2132 /* XXX: we don't have to do this every time... */
2133 bzero(&null_pktinfo, sizeof(null_pktinfo));
2134 optdata = (void *)&null_pktinfo;
2136 optdatalen = sizeof(struct in6_pktinfo);
2139 if (pktopt && pktopt->ip6po_tclass >= 0)
2140 optdata = (void *)&pktopt->ip6po_tclass;
2142 optdata = (void *)&deftclass;
2143 optdatalen = sizeof(int);
2146 if (pktopt && pktopt->ip6po_hbh) {
2147 optdata = (void *)pktopt->ip6po_hbh;
2148 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2149 optdatalen = (ip6e->ip6e_len + 1) << 3;
2153 if (pktopt && pktopt->ip6po_rthdr) {
2154 optdata = (void *)pktopt->ip6po_rthdr;
2155 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2156 optdatalen = (ip6e->ip6e_len + 1) << 3;
2159 case IPV6_RTHDRDSTOPTS:
2160 if (pktopt && pktopt->ip6po_dest1) {
2161 optdata = (void *)pktopt->ip6po_dest1;
2162 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2163 optdatalen = (ip6e->ip6e_len + 1) << 3;
2167 if (pktopt && pktopt->ip6po_dest2) {
2168 optdata = (void *)pktopt->ip6po_dest2;
2169 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2170 optdatalen = (ip6e->ip6e_len + 1) << 3;
2174 if (pktopt && pktopt->ip6po_nexthop) {
2175 optdata = (void *)pktopt->ip6po_nexthop;
2176 optdatalen = pktopt->ip6po_nexthop->sa_len;
2179 case IPV6_USE_MIN_MTU:
2181 optdata = (void *)&pktopt->ip6po_minmtu;
2183 optdata = (void *)&defminmtu;
2184 optdatalen = sizeof(int);
2187 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2191 optdata = (void *)&on;
2192 optdatalen = sizeof(on);
2194 case IPV6_PREFER_TEMPADDR:
2196 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2198 optdata = (void *)&defpreftemp;
2199 optdatalen = sizeof(int);
2201 default: /* should not happen */
2203 panic("ip6_getpcbopt: unexpected option\n");
2205 return (ENOPROTOOPT);
2208 error = sooptcopyout(sopt, optdata, optdatalen);
2214 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2219 if (optname == -1 || optname == IPV6_PKTINFO) {
2220 if (pktopt->ip6po_pktinfo)
2221 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2222 pktopt->ip6po_pktinfo = NULL;
2224 if (optname == -1 || optname == IPV6_HOPLIMIT)
2225 pktopt->ip6po_hlim = -1;
2226 if (optname == -1 || optname == IPV6_TCLASS)
2227 pktopt->ip6po_tclass = -1;
2228 if (optname == -1 || optname == IPV6_NEXTHOP) {
2229 if (pktopt->ip6po_nextroute.ro_rt) {
2230 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2231 pktopt->ip6po_nextroute.ro_rt = NULL;
2233 if (pktopt->ip6po_nexthop)
2234 free(pktopt->ip6po_nexthop, M_IP6OPT);
2235 pktopt->ip6po_nexthop = NULL;
2237 if (optname == -1 || optname == IPV6_HOPOPTS) {
2238 if (pktopt->ip6po_hbh)
2239 free(pktopt->ip6po_hbh, M_IP6OPT);
2240 pktopt->ip6po_hbh = NULL;
2242 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2243 if (pktopt->ip6po_dest1)
2244 free(pktopt->ip6po_dest1, M_IP6OPT);
2245 pktopt->ip6po_dest1 = NULL;
2247 if (optname == -1 || optname == IPV6_RTHDR) {
2248 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2249 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2250 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2251 if (pktopt->ip6po_route.ro_rt) {
2252 RTFREE(pktopt->ip6po_route.ro_rt);
2253 pktopt->ip6po_route.ro_rt = NULL;
2256 if (optname == -1 || optname == IPV6_DSTOPTS) {
2257 if (pktopt->ip6po_dest2)
2258 free(pktopt->ip6po_dest2, M_IP6OPT);
2259 pktopt->ip6po_dest2 = NULL;
2263 #define PKTOPT_EXTHDRCPY(type) \
2266 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2267 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2268 if (dst->type == NULL && canwait == M_NOWAIT)\
2270 bcopy(src->type, dst->type, hlen);\
2272 } while (/*CONSTCOND*/ 0)
2275 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2277 if (dst == NULL || src == NULL) {
2278 printf("ip6_clearpktopts: invalid argument\n");
2282 dst->ip6po_hlim = src->ip6po_hlim;
2283 dst->ip6po_tclass = src->ip6po_tclass;
2284 dst->ip6po_flags = src->ip6po_flags;
2285 dst->ip6po_minmtu = src->ip6po_minmtu;
2286 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2287 if (src->ip6po_pktinfo) {
2288 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2290 if (dst->ip6po_pktinfo == NULL)
2292 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2294 if (src->ip6po_nexthop) {
2295 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2297 if (dst->ip6po_nexthop == NULL)
2299 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2300 src->ip6po_nexthop->sa_len);
2302 PKTOPT_EXTHDRCPY(ip6po_hbh);
2303 PKTOPT_EXTHDRCPY(ip6po_dest1);
2304 PKTOPT_EXTHDRCPY(ip6po_dest2);
2305 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2309 ip6_clearpktopts(dst, -1);
2312 #undef PKTOPT_EXTHDRCPY
2314 struct ip6_pktopts *
2315 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2318 struct ip6_pktopts *dst;
2320 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2323 ip6_initpktopts(dst);
2325 if ((error = copypktopts(dst, src, canwait)) != 0) {
2326 free(dst, M_IP6OPT);
2334 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2339 ip6_clearpktopts(pktopt, -1);
2341 free(pktopt, M_IP6OPT);
2345 * Set IPv6 outgoing packet options based on advanced API.
2348 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2349 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2351 struct cmsghdr *cm = 0;
2353 if (control == NULL || opt == NULL)
2356 ip6_initpktopts(opt);
2361 * If stickyopt is provided, make a local copy of the options
2362 * for this particular packet, then override them by ancillary
2364 * XXX: copypktopts() does not copy the cached route to a next
2365 * hop (if any). This is not very good in terms of efficiency,
2366 * but we can allow this since this option should be rarely
2369 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2374 * XXX: Currently, we assume all the optional information is stored
2377 if (control->m_next)
2380 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2381 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2384 if (control->m_len < CMSG_LEN(0))
2387 cm = mtod(control, struct cmsghdr *);
2388 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2390 if (cm->cmsg_level != IPPROTO_IPV6)
2393 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2394 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2403 * Set a particular packet option, as a sticky option or an ancillary data
2404 * item. "len" can be 0 only when it's a sticky option.
2405 * We have 4 cases of combination of "sticky" and "cmsg":
2406 * "sticky=0, cmsg=0": impossible
2407 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2408 * "sticky=1, cmsg=0": RFC3542 socket option
2409 * "sticky=1, cmsg=1": RFC2292 socket option
2412 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2413 struct ucred *cred, int sticky, int cmsg, int uproto)
2415 int minmtupolicy, preftemp;
2418 if (!sticky && !cmsg) {
2420 printf("ip6_setpktopt: impossible case\n");
2426 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2427 * not be specified in the context of RFC3542. Conversely,
2428 * RFC3542 types should not be specified in the context of RFC2292.
2432 case IPV6_2292PKTINFO:
2433 case IPV6_2292HOPLIMIT:
2434 case IPV6_2292NEXTHOP:
2435 case IPV6_2292HOPOPTS:
2436 case IPV6_2292DSTOPTS:
2437 case IPV6_2292RTHDR:
2438 case IPV6_2292PKTOPTIONS:
2439 return (ENOPROTOOPT);
2442 if (sticky && cmsg) {
2449 case IPV6_RTHDRDSTOPTS:
2451 case IPV6_USE_MIN_MTU:
2454 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2455 return (ENOPROTOOPT);
2460 case IPV6_2292PKTINFO:
2463 struct ifnet *ifp = NULL;
2464 struct in6_pktinfo *pktinfo;
2466 if (len != sizeof(struct in6_pktinfo))
2469 pktinfo = (struct in6_pktinfo *)buf;
2472 * An application can clear any sticky IPV6_PKTINFO option by
2473 * doing a "regular" setsockopt with ipi6_addr being
2474 * in6addr_any and ipi6_ifindex being zero.
2475 * [RFC 3542, Section 6]
2477 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2478 pktinfo->ipi6_ifindex == 0 &&
2479 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2480 ip6_clearpktopts(opt, optname);
2484 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2485 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2489 /* validate the interface index if specified. */
2490 if (pktinfo->ipi6_ifindex > V_if_index)
2492 if (pktinfo->ipi6_ifindex) {
2493 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2499 * We store the address anyway, and let in6_selectsrc()
2500 * validate the specified address. This is because ipi6_addr
2501 * may not have enough information about its scope zone, and
2502 * we may need additional information (such as outgoing
2503 * interface or the scope zone of a destination address) to
2504 * disambiguate the scope.
2505 * XXX: the delay of the validation may confuse the
2506 * application when it is used as a sticky option.
2508 if (opt->ip6po_pktinfo == NULL) {
2509 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2510 M_IP6OPT, M_NOWAIT);
2511 if (opt->ip6po_pktinfo == NULL)
2514 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2518 case IPV6_2292HOPLIMIT:
2524 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2525 * to simplify the ordering among hoplimit options.
2527 if (optname == IPV6_HOPLIMIT && sticky)
2528 return (ENOPROTOOPT);
2530 if (len != sizeof(int))
2533 if (*hlimp < -1 || *hlimp > 255)
2536 opt->ip6po_hlim = *hlimp;
2544 if (len != sizeof(int))
2546 tclass = *(int *)buf;
2547 if (tclass < -1 || tclass > 255)
2550 opt->ip6po_tclass = tclass;
2554 case IPV6_2292NEXTHOP:
2557 error = priv_check_cred(cred,
2558 PRIV_NETINET_SETHDROPTS, 0);
2563 if (len == 0) { /* just remove the option */
2564 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2568 /* check if cmsg_len is large enough for sa_len */
2569 if (len < sizeof(struct sockaddr) || len < *buf)
2572 switch (((struct sockaddr *)buf)->sa_family) {
2575 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2578 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2581 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2582 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2585 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2591 case AF_LINK: /* should eventually be supported */
2593 return (EAFNOSUPPORT);
2596 /* turn off the previous option, then set the new option. */
2597 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2598 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2599 if (opt->ip6po_nexthop == NULL)
2601 bcopy(buf, opt->ip6po_nexthop, *buf);
2604 case IPV6_2292HOPOPTS:
2607 struct ip6_hbh *hbh;
2611 * XXX: We don't allow a non-privileged user to set ANY HbH
2612 * options, since per-option restriction has too much
2616 error = priv_check_cred(cred,
2617 PRIV_NETINET_SETHDROPTS, 0);
2623 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2624 break; /* just remove the option */
2627 /* message length validation */
2628 if (len < sizeof(struct ip6_hbh))
2630 hbh = (struct ip6_hbh *)buf;
2631 hbhlen = (hbh->ip6h_len + 1) << 3;
2635 /* turn off the previous option, then set the new option. */
2636 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2637 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2638 if (opt->ip6po_hbh == NULL)
2640 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2645 case IPV6_2292DSTOPTS:
2647 case IPV6_RTHDRDSTOPTS:
2649 struct ip6_dest *dest, **newdest = NULL;
2652 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2653 error = priv_check_cred(cred,
2654 PRIV_NETINET_SETHDROPTS, 0);
2660 ip6_clearpktopts(opt, optname);
2661 break; /* just remove the option */
2664 /* message length validation */
2665 if (len < sizeof(struct ip6_dest))
2667 dest = (struct ip6_dest *)buf;
2668 destlen = (dest->ip6d_len + 1) << 3;
2673 * Determine the position that the destination options header
2674 * should be inserted; before or after the routing header.
2677 case IPV6_2292DSTOPTS:
2679 * The old advacned API is ambiguous on this point.
2680 * Our approach is to determine the position based
2681 * according to the existence of a routing header.
2682 * Note, however, that this depends on the order of the
2683 * extension headers in the ancillary data; the 1st
2684 * part of the destination options header must appear
2685 * before the routing header in the ancillary data,
2687 * RFC3542 solved the ambiguity by introducing
2688 * separate ancillary data or option types.
2690 if (opt->ip6po_rthdr == NULL)
2691 newdest = &opt->ip6po_dest1;
2693 newdest = &opt->ip6po_dest2;
2695 case IPV6_RTHDRDSTOPTS:
2696 newdest = &opt->ip6po_dest1;
2699 newdest = &opt->ip6po_dest2;
2703 /* turn off the previous option, then set the new option. */
2704 ip6_clearpktopts(opt, optname);
2705 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2706 if (*newdest == NULL)
2708 bcopy(dest, *newdest, destlen);
2713 case IPV6_2292RTHDR:
2716 struct ip6_rthdr *rth;
2720 ip6_clearpktopts(opt, IPV6_RTHDR);
2721 break; /* just remove the option */
2724 /* message length validation */
2725 if (len < sizeof(struct ip6_rthdr))
2727 rth = (struct ip6_rthdr *)buf;
2728 rthlen = (rth->ip6r_len + 1) << 3;
2732 switch (rth->ip6r_type) {
2733 case IPV6_RTHDR_TYPE_0:
2734 if (rth->ip6r_len == 0) /* must contain one addr */
2736 if (rth->ip6r_len % 2) /* length must be even */
2738 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2742 return (EINVAL); /* not supported */
2745 /* turn off the previous option */
2746 ip6_clearpktopts(opt, IPV6_RTHDR);
2747 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2748 if (opt->ip6po_rthdr == NULL)
2750 bcopy(rth, opt->ip6po_rthdr, rthlen);
2755 case IPV6_USE_MIN_MTU:
2756 if (len != sizeof(int))
2758 minmtupolicy = *(int *)buf;
2759 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2760 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2761 minmtupolicy != IP6PO_MINMTU_ALL) {
2764 opt->ip6po_minmtu = minmtupolicy;
2768 if (len != sizeof(int))
2771 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2773 * we ignore this option for TCP sockets.
2774 * (RFC3542 leaves this case unspecified.)
2776 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2778 opt->ip6po_flags |= IP6PO_DONTFRAG;
2781 case IPV6_PREFER_TEMPADDR:
2782 if (len != sizeof(int))
2784 preftemp = *(int *)buf;
2785 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2786 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2787 preftemp != IP6PO_TEMPADDR_PREFER) {
2790 opt->ip6po_prefer_tempaddr = preftemp;
2794 return (ENOPROTOOPT);
2795 } /* end of switch */
2801 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2802 * packet to the input queue of a specified interface. Note that this
2803 * calls the output routine of the loopback "driver", but with an interface
2804 * pointer that might NOT be &loif -- easier than replicating that code here.
2807 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
2810 struct ip6_hdr *ip6;
2812 copym = m_copy(m, 0, M_COPYALL);
2817 * Make sure to deep-copy IPv6 header portion in case the data
2818 * is in an mbuf cluster, so that we can safely override the IPv6
2819 * header portion later.
2821 if ((copym->m_flags & M_EXT) != 0 ||
2822 copym->m_len < sizeof(struct ip6_hdr)) {
2823 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2829 if (copym->m_len < sizeof(*ip6)) {
2835 ip6 = mtod(copym, struct ip6_hdr *);
2837 * clear embedded scope identifiers if necessary.
2838 * in6_clearscope will touch the addresses only when necessary.
2840 in6_clearscope(&ip6->ip6_src);
2841 in6_clearscope(&ip6->ip6_dst);
2843 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
2847 * Chop IPv6 header off from the payload.
2850 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
2853 struct ip6_hdr *ip6;
2855 ip6 = mtod(m, struct ip6_hdr *);
2856 if (m->m_len > sizeof(*ip6)) {
2857 mh = m_gethdr(M_NOWAIT, MT_DATA);
2862 m_move_pkthdr(mh, m);
2863 MH_ALIGN(mh, sizeof(*ip6));
2864 m->m_len -= sizeof(*ip6);
2865 m->m_data += sizeof(*ip6);
2868 m->m_len = sizeof(*ip6);
2869 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2871 exthdrs->ip6e_ip6 = m;
2876 * Compute IPv6 extension header length.
2879 ip6_optlen(struct inpcb *in6p)
2883 if (!in6p->in6p_outputopts)
2888 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2890 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2891 if (in6p->in6p_outputopts->ip6po_rthdr)
2892 /* dest1 is valid with rthdr only */
2893 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2894 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2895 len += elen(in6p->in6p_outputopts->ip6po_dest2);