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/netisr.h>
90 #include <net/route.h>
94 #include <netinet/in.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip_var.h>
97 #include <netinet6/in6_var.h>
98 #include <netinet/ip6.h>
99 #include <netinet/icmp6.h>
100 #include <netinet6/ip6_var.h>
101 #include <netinet/in_pcb.h>
102 #include <netinet/tcp_var.h>
103 #include <netinet6/nd6.h>
106 #include <netipsec/ipsec.h>
107 #include <netipsec/ipsec6.h>
108 #include <netipsec/key.h>
109 #include <netinet6/ip6_ipsec.h>
112 #include <netinet/sctp.h>
113 #include <netinet/sctp_crc32.h>
116 #include <netinet6/ip6protosw.h>
117 #include <netinet6/scope6_var.h>
120 #include <net/flowtable.h>
123 extern int in6_mcast_loop;
126 struct mbuf *ip6e_ip6;
127 struct mbuf *ip6e_hbh;
128 struct mbuf *ip6e_dest1;
129 struct mbuf *ip6e_rthdr;
130 struct mbuf *ip6e_dest2;
133 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
134 struct ucred *, int);
135 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
136 struct socket *, struct sockopt *);
137 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
138 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
139 struct ucred *, int, int, int);
141 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
142 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
144 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
145 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
146 static int ip6_getpmtu(struct route_in6 *, struct route_in6 *,
147 struct ifnet *, struct in6_addr *, u_long *, int *, u_int);
148 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
152 * Make an extension header from option data. hp is the source, and
153 * mp is the destination.
155 #define MAKE_EXTHDR(hp, mp) \
158 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
159 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
160 ((eh)->ip6e_len + 1) << 3); \
164 } while (/*CONSTCOND*/ 0)
167 * Form a chain of extension headers.
168 * m is the extension header mbuf
169 * mp is the previous mbuf in the chain
170 * p is the next header
171 * i is the type of option.
173 #define MAKE_CHAIN(m, mp, p, i)\
177 panic("assumption failed: hdr not split"); \
178 *mtod((m), u_char *) = *(p);\
180 p = mtod((m), u_char *);\
181 (m)->m_next = (mp)->m_next;\
185 } while (/*CONSTCOND*/ 0)
188 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
192 csum = in_cksum_skip(m, offset + plen, offset);
193 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
195 offset += m->m_pkthdr.csum_data; /* checksum offset */
197 if (offset + sizeof(u_short) > m->m_len) {
198 printf("%s: delayed m_pullup, m->len: %d plen %u off %u "
199 "csum_flags=0x%04x\n", __func__, m->m_len, plen, offset,
200 m->m_pkthdr.csum_flags);
202 * XXX this should not happen, but if it does, the correct
203 * behavior may be to insert the checksum in the appropriate
204 * next mbuf in the chain.
208 *(u_short *)(m->m_data + offset) = csum;
212 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
213 * header (with pri, len, nxt, hlim, src, dst).
214 * This function may modify ver and hlim only.
215 * The mbuf chain containing the packet will be freed.
216 * The mbuf opt, if present, will not be freed.
217 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
218 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
219 * then result of route lookup is stored in ro->ro_rt.
221 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
222 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
223 * which is rt_rmx.rmx_mtu.
225 * ifpp - XXX: just for statistics
228 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
229 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
230 struct ifnet **ifpp, struct inpcb *inp)
232 struct ip6_hdr *ip6, *mhip6;
233 struct ifnet *ifp, *origifp;
235 struct mbuf *mprev = NULL;
236 int hlen, tlen, len, off;
237 struct route_in6 ip6route;
238 struct rtentry *rt = NULL;
239 struct sockaddr_in6 *dst, src_sa, dst_sa;
240 struct in6_addr odst;
242 struct in6_ifaddr *ia = NULL;
244 int alwaysfrag, dontfrag;
245 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
246 struct ip6_exthdrs exthdrs;
247 struct in6_addr finaldst, src0, dst0;
249 struct route_in6 *ro_pmtu = NULL;
254 struct ipsec_output_state state;
255 struct ip6_rthdr *rh = NULL;
256 int needipsectun = 0;
258 struct secpolicy *sp = NULL;
260 struct m_tag *fwd_tag;
262 ip6 = mtod(m, struct ip6_hdr *);
264 printf ("ip6 is NULL");
269 M_SETFIB(m, inp->inp_inc.inc_fibnum);
271 finaldst = ip6->ip6_dst;
272 bzero(&exthdrs, sizeof(exthdrs));
274 /* Hop-by-Hop options header */
275 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
276 /* Destination options header(1st part) */
277 if (opt->ip6po_rthdr) {
279 * Destination options header(1st part)
280 * This only makes sense with a routing header.
281 * See Section 9.2 of RFC 3542.
282 * Disabling this part just for MIP6 convenience is
283 * a bad idea. We need to think carefully about a
284 * way to make the advanced API coexist with MIP6
285 * options, which might automatically be inserted in
288 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
291 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
292 /* Destination options header(2nd part) */
293 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
298 * IPSec checking which handles several cases.
299 * FAST IPSEC: We re-injected the packet.
301 switch(ip6_ipsec_output(&m, inp, &flags, &error, &ifp, &sp))
303 case 1: /* Bad packet */
305 case -1: /* Do IPSec */
308 * Do delayed checksums now, as we may send before returning.
310 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
311 plen = m->m_pkthdr.len - sizeof(*ip6);
312 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
313 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
316 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
317 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
318 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
321 case 0: /* No IPSec */
328 * Calculate the total length of the extension header chain.
329 * Keep the length of the unfragmentable part for fragmentation.
332 if (exthdrs.ip6e_hbh)
333 optlen += exthdrs.ip6e_hbh->m_len;
334 if (exthdrs.ip6e_dest1)
335 optlen += exthdrs.ip6e_dest1->m_len;
336 if (exthdrs.ip6e_rthdr)
337 optlen += exthdrs.ip6e_rthdr->m_len;
338 unfragpartlen = optlen + sizeof(struct ip6_hdr);
340 /* NOTE: we don't add AH/ESP length here. do that later. */
341 if (exthdrs.ip6e_dest2)
342 optlen += exthdrs.ip6e_dest2->m_len;
345 * If we need IPsec, or there is at least one extension header,
346 * separate IP6 header from the payload.
348 if ((needipsec || optlen) && !hdrsplit) {
349 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
353 m = exthdrs.ip6e_ip6;
358 ip6 = mtod(m, struct ip6_hdr *);
360 /* adjust mbuf packet header length */
361 m->m_pkthdr.len += optlen;
362 plen = m->m_pkthdr.len - sizeof(*ip6);
364 /* If this is a jumbo payload, insert a jumbo payload option. */
365 if (plen > IPV6_MAXPACKET) {
367 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
371 m = exthdrs.ip6e_ip6;
375 ip6 = mtod(m, struct ip6_hdr *);
376 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
380 ip6->ip6_plen = htons(plen);
383 * Concatenate headers and fill in next header fields.
384 * Here we have, on "m"
386 * and we insert headers accordingly. Finally, we should be getting:
387 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
389 * during the header composing process, "m" points to IPv6 header.
390 * "mprev" points to an extension header prior to esp.
392 u_char *nexthdrp = &ip6->ip6_nxt;
396 * we treat dest2 specially. this makes IPsec processing
397 * much easier. the goal here is to make mprev point the
398 * mbuf prior to dest2.
400 * result: IPv6 dest2 payload
401 * m and mprev will point to IPv6 header.
403 if (exthdrs.ip6e_dest2) {
405 panic("assumption failed: hdr not split");
406 exthdrs.ip6e_dest2->m_next = m->m_next;
407 m->m_next = exthdrs.ip6e_dest2;
408 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
409 ip6->ip6_nxt = IPPROTO_DSTOPTS;
413 * result: IPv6 hbh dest1 rthdr dest2 payload
414 * m will point to IPv6 header. mprev will point to the
415 * extension header prior to dest2 (rthdr in the above case).
417 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
418 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
420 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
428 * pointers after IPsec headers are not valid any more.
429 * other pointers need a great care too.
430 * (IPsec routines should not mangle mbufs prior to AH/ESP)
432 exthdrs.ip6e_dest2 = NULL;
434 if (exthdrs.ip6e_rthdr) {
435 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
436 segleft_org = rh->ip6r_segleft;
437 rh->ip6r_segleft = 0;
440 bzero(&state, sizeof(state));
442 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
445 if (error == EJUSTRETURN) {
447 * We had a SP with a level of 'use' and no SA. We
448 * will just continue to process the packet without
453 /* mbuf is already reclaimed in ipsec6_output_trans. */
463 printf("[%s:%d] (ipsec): error code %d\n",
464 __func__, __LINE__, error);
467 /* don't show these error codes to the user */
472 } else if (!needipsectun) {
474 * In the FAST IPSec case we have already
475 * re-injected the packet and it has been freed
476 * by the ipsec_done() function. So, just clean
477 * up after ourselves.
482 if (exthdrs.ip6e_rthdr) {
483 /* ah6_output doesn't modify mbuf chain */
484 rh->ip6r_segleft = segleft_org;
490 * If there is a routing header, discard the packet.
492 if (exthdrs.ip6e_rthdr) {
497 /* Source address validation */
498 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
499 (flags & IPV6_UNSPECSRC) == 0) {
501 V_ip6stat.ip6s_badscope++;
504 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
506 V_ip6stat.ip6s_badscope++;
510 V_ip6stat.ip6s_localout++;
517 bzero((caddr_t)ro, sizeof(*ro));
520 if (opt && opt->ip6po_rthdr)
521 ro = &opt->ip6po_route;
522 dst = (struct sockaddr_in6 *)&ro->ro_dst;
524 if (ro->ro_rt == NULL) {
528 * The flow table returns route entries valid for up to 30
529 * seconds; we rely on the remainder of ip_output() taking no
530 * longer than that long for the stability of ro_rt. The
531 * flow ID assignment must have happened before this point.
533 fle = flowtable_lookup_mbuf(V_ip6_ft, m, AF_INET6);
535 flow_to_route_in6(fle, ro);
540 * if specified, try to fill in the traffic class field.
541 * do not override if a non-zero value is already set.
542 * we check the diffserv field and the ecn field separately.
544 if (opt && opt->ip6po_tclass >= 0) {
547 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
549 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
552 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
555 /* fill in or override the hop limit field, if necessary. */
556 if (opt && opt->ip6po_hlim != -1)
557 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
558 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
560 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
562 ip6->ip6_hlim = V_ip6_defmcasthlim;
567 * We may re-inject packets into the stack here.
569 if (needipsec && needipsectun) {
570 struct ipsec_output_state state;
573 * All the extension headers will become inaccessible
574 * (since they can be encrypted).
575 * Don't panic, we need no more updates to extension headers
576 * on inner IPv6 packet (since they are now encapsulated).
578 * IPv6 [ESP|AH] IPv6 [extension headers] payload
580 bzero(&exthdrs, sizeof(exthdrs));
581 exthdrs.ip6e_ip6 = m;
583 bzero(&state, sizeof(state));
585 state.ro = (struct route *)ro;
586 state.dst = (struct sockaddr *)dst;
588 error = ipsec6_output_tunnel(&state, sp, flags);
591 ro = (struct route_in6 *)state.ro;
592 dst = (struct sockaddr_in6 *)state.dst;
593 if (error == EJUSTRETURN) {
595 * We had a SP with a level of 'use' and no SA. We
596 * will just continue to process the packet without
601 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
612 printf("[%s:%d] (ipsec): error code %d\n",
613 __func__, __LINE__, error);
616 /* don't show these error codes to the user */
623 * In the FAST IPSec case we have already
624 * re-injected the packet and it has been freed
625 * by the ipsec_done() function. So, just clean
626 * up after ourselves.
632 exthdrs.ip6e_ip6 = m;
637 ip6 = mtod(m, struct ip6_hdr *);
639 bzero(&dst_sa, sizeof(dst_sa));
640 dst_sa.sin6_family = AF_INET6;
641 dst_sa.sin6_len = sizeof(dst_sa);
642 dst_sa.sin6_addr = ip6->ip6_dst;
645 ifp = ro->ro_rt->rt_ifp;
646 } else if ((error = in6_selectroute_fib(&dst_sa, opt, im6o, ro,
647 &ifp, &rt, inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m))) != 0) {
650 V_ip6stat.ip6s_noroute++;
654 break; /* XXX statistics? */
657 in6_ifstat_inc(ifp, ifs6_out_discard);
662 * If in6_selectroute() does not return a route entry,
663 * dst may not have been updated.
665 *dst = dst_sa; /* XXX */
669 * then rt (for unicast) and ifp must be non-NULL valid values.
671 if ((flags & IPV6_FORWARDING) == 0) {
672 /* XXX: the FORWARDING flag can be set for mrouting. */
673 in6_ifstat_inc(ifp, ifs6_out_request);
676 ia = (struct in6_ifaddr *)(rt->rt_ifa);
682 * The outgoing interface must be in the zone of source and
683 * destination addresses.
688 if (in6_setscope(&src0, origifp, &zone))
690 bzero(&src_sa, sizeof(src_sa));
691 src_sa.sin6_family = AF_INET6;
692 src_sa.sin6_len = sizeof(src_sa);
693 src_sa.sin6_addr = ip6->ip6_src;
694 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
698 if (in6_setscope(&dst0, origifp, &zone))
700 /* re-initialize to be sure */
701 bzero(&dst_sa, sizeof(dst_sa));
702 dst_sa.sin6_family = AF_INET6;
703 dst_sa.sin6_len = sizeof(dst_sa);
704 dst_sa.sin6_addr = ip6->ip6_dst;
705 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
709 /* We should use ia_ifp to support the case of
710 * sending packets to an address of our own.
712 if (ia != NULL && ia->ia_ifp)
715 /* scope check is done. */
719 V_ip6stat.ip6s_badscope++;
720 in6_ifstat_inc(origifp, ifs6_out_discard);
722 error = EHOSTUNREACH; /* XXX */
726 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
727 if (opt && opt->ip6po_nextroute.ro_rt) {
729 * The nexthop is explicitly specified by the
730 * application. We assume the next hop is an IPv6
733 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
735 else if ((rt->rt_flags & RTF_GATEWAY))
736 dst = (struct sockaddr_in6 *)rt->rt_gateway;
739 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
740 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
742 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
743 in6_ifstat_inc(ifp, ifs6_out_mcast);
745 * Confirm that the outgoing interface supports multicast.
747 if (!(ifp->if_flags & IFF_MULTICAST)) {
748 V_ip6stat.ip6s_noroute++;
749 in6_ifstat_inc(ifp, ifs6_out_discard);
753 if ((im6o == NULL && in6_mcast_loop) ||
754 (im6o && im6o->im6o_multicast_loop)) {
756 * Loop back multicast datagram if not expressly
757 * forbidden to do so, even if we have not joined
758 * the address; protocols will filter it later,
759 * thus deferring a hash lookup and lock acquisition
760 * at the expense of an m_copym().
762 ip6_mloopback(ifp, m, dst);
765 * If we are acting as a multicast router, perform
766 * multicast forwarding as if the packet had just
767 * arrived on the interface to which we are about
768 * to send. The multicast forwarding function
769 * recursively calls this function, using the
770 * IPV6_FORWARDING flag to prevent infinite recursion.
772 * Multicasts that are looped back by ip6_mloopback(),
773 * above, will be forwarded by the ip6_input() routine,
776 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
778 * XXX: ip6_mforward expects that rcvif is NULL
779 * when it is called from the originating path.
780 * However, it is not always the case, since
781 * some versions of MGETHDR() does not
782 * initialize the field.
784 m->m_pkthdr.rcvif = NULL;
785 if (ip6_mforward(ip6, ifp, m) != 0) {
792 * Multicasts with a hoplimit of zero may be looped back,
793 * above, but must not be transmitted on a network.
794 * Also, multicasts addressed to the loopback interface
795 * are not sent -- the above call to ip6_mloopback() will
796 * loop back a copy if this host actually belongs to the
797 * destination group on the loopback interface.
799 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
800 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
807 * Fill the outgoing inteface to tell the upper layer
808 * to increment per-interface statistics.
813 /* Determine path MTU. */
814 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
815 &alwaysfrag, inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m))) != 0)
819 * The caller of this function may specify to use the minimum MTU
821 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
822 * setting. The logic is a bit complicated; by default, unicast
823 * packets will follow path MTU while multicast packets will be sent at
824 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
825 * including unicast ones will be sent at the minimum MTU. Multicast
826 * packets will always be sent at the minimum MTU unless
827 * IP6PO_MINMTU_DISABLE is explicitly specified.
828 * See RFC 3542 for more details.
830 if (mtu > IPV6_MMTU) {
831 if ((flags & IPV6_MINMTU))
833 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
835 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
837 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
843 * clear embedded scope identifiers if necessary.
844 * in6_clearscope will touch the addresses only when necessary.
846 in6_clearscope(&ip6->ip6_src);
847 in6_clearscope(&ip6->ip6_dst);
850 * If the outgoing packet contains a hop-by-hop options header,
851 * it must be examined and processed even by the source node.
852 * (RFC 2460, section 4.)
854 if (exthdrs.ip6e_hbh) {
855 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
856 u_int32_t dummy; /* XXX unused */
857 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
860 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
861 panic("ip6e_hbh is not contiguous");
864 * XXX: if we have to send an ICMPv6 error to the sender,
865 * we need the M_LOOP flag since icmp6_error() expects
866 * the IPv6 and the hop-by-hop options header are
867 * contiguous unless the flag is set.
869 m->m_flags |= M_LOOP;
870 m->m_pkthdr.rcvif = ifp;
871 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
872 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
873 &dummy, &plen) < 0) {
874 /* m was already freed at this point */
875 error = EINVAL;/* better error? */
878 m->m_flags &= ~M_LOOP; /* XXX */
879 m->m_pkthdr.rcvif = NULL;
882 /* Jump over all PFIL processing if hooks are not active. */
883 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
887 /* Run through list of hooks for output packets. */
888 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
889 if (error != 0 || m == NULL)
891 ip6 = mtod(m, struct ip6_hdr *);
893 /* See if destination IP address was changed by packet filter. */
894 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
895 m->m_flags |= M_SKIP_FIREWALL;
896 /* If destination is now ourself drop to ip6_input(). */
897 if (in6_localip(&ip6->ip6_dst)) {
898 m->m_flags |= M_FASTFWD_OURS;
899 if (m->m_pkthdr.rcvif == NULL)
900 m->m_pkthdr.rcvif = V_loif;
901 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
902 m->m_pkthdr.csum_flags |=
903 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
904 m->m_pkthdr.csum_data = 0xffff;
907 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
908 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
910 error = netisr_queue(NETISR_IPV6, m);
913 goto again; /* Redo the routing table lookup. */
916 /* See if local, if yes, send it to netisr. */
917 if (m->m_flags & M_FASTFWD_OURS) {
918 if (m->m_pkthdr.rcvif == NULL)
919 m->m_pkthdr.rcvif = V_loif;
920 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
921 m->m_pkthdr.csum_flags |=
922 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
923 m->m_pkthdr.csum_data = 0xffff;
926 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
927 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
929 error = netisr_queue(NETISR_IPV6, m);
932 /* Or forward to some other address? */
933 if ((m->m_flags & M_IP6_NEXTHOP) &&
934 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
935 dst = (struct sockaddr_in6 *)&ro->ro_dst;
936 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in6));
937 m->m_flags |= M_SKIP_FIREWALL;
938 m->m_flags &= ~M_IP6_NEXTHOP;
939 m_tag_delete(m, fwd_tag);
945 * Send the packet to the outgoing interface.
946 * If necessary, do IPv6 fragmentation before sending.
948 * the logic here is rather complex:
949 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
950 * 1-a: send as is if tlen <= path mtu
951 * 1-b: fragment if tlen > path mtu
953 * 2: if user asks us not to fragment (dontfrag == 1)
954 * 2-a: send as is if tlen <= interface mtu
955 * 2-b: error if tlen > interface mtu
957 * 3: if we always need to attach fragment header (alwaysfrag == 1)
960 * 4: if dontfrag == 1 && alwaysfrag == 1
961 * error, as we cannot handle this conflicting request
963 sw_csum = m->m_pkthdr.csum_flags;
965 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
966 sw_csum &= ~ifp->if_hwassist;
970 * If we added extension headers, we will not do TSO and calculate the
971 * checksums ourselves for now.
972 * XXX-BZ Need a framework to know when the NIC can handle it, even
975 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
976 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
977 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
980 if (sw_csum & CSUM_SCTP_IPV6) {
981 sw_csum &= ~CSUM_SCTP_IPV6;
982 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
985 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
986 tlen = m->m_pkthdr.len;
988 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
992 if (dontfrag && alwaysfrag) { /* case 4 */
993 /* conflicting request - can't transmit */
997 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
999 * Even if the DONTFRAG option is specified, we cannot send the
1000 * packet when the data length is larger than the MTU of the
1001 * outgoing interface.
1002 * Notify the error by sending IPV6_PATHMTU ancillary data as
1003 * well as returning an error code (the latter is not described
1007 struct ip6ctlparam ip6cp;
1009 mtu32 = (u_int32_t)mtu;
1010 bzero(&ip6cp, sizeof(ip6cp));
1011 ip6cp.ip6c_cmdarg = (void *)&mtu32;
1012 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
1020 * transmit packet without fragmentation
1022 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
1023 struct in6_ifaddr *ia6;
1025 ip6 = mtod(m, struct ip6_hdr *);
1026 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1028 /* Record statistics for this interface address. */
1029 ia6->ia_ifa.if_opackets++;
1030 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
1031 ifa_free(&ia6->ia_ifa);
1033 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1038 * try to fragment the packet. case 1-b and 3
1040 if (mtu < IPV6_MMTU) {
1041 /* path MTU cannot be less than IPV6_MMTU */
1043 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1045 } else if (ip6->ip6_plen == 0) {
1046 /* jumbo payload cannot be fragmented */
1048 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1051 struct mbuf **mnext, *m_frgpart;
1052 struct ip6_frag *ip6f;
1053 u_int32_t id = htonl(ip6_randomid());
1056 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
1059 * Too large for the destination or interface;
1060 * fragment if possible.
1061 * Must be able to put at least 8 bytes per fragment.
1063 hlen = unfragpartlen;
1064 if (mtu > IPV6_MAXPACKET)
1065 mtu = IPV6_MAXPACKET;
1067 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1070 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1075 * Verify that we have any chance at all of being able to queue
1076 * the packet or packet fragments
1078 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
1079 < tlen /* - hlen */)) {
1081 V_ip6stat.ip6s_odropped++;
1087 * If the interface will not calculate checksums on
1088 * fragmented packets, then do it here.
1089 * XXX-BZ handle the hw offloading case. Need flags.
1091 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1092 in6_delayed_cksum(m, plen, hlen);
1093 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1096 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1097 sctp_delayed_cksum(m, hlen);
1098 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1101 mnext = &m->m_nextpkt;
1104 * Change the next header field of the last header in the
1105 * unfragmentable part.
1107 if (exthdrs.ip6e_rthdr) {
1108 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1109 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1110 } else if (exthdrs.ip6e_dest1) {
1111 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1112 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1113 } else if (exthdrs.ip6e_hbh) {
1114 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1115 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1117 nextproto = ip6->ip6_nxt;
1118 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1122 * Loop through length of segment after first fragment,
1123 * make new header and copy data of each part and link onto
1127 for (off = hlen; off < tlen; off += len) {
1128 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1131 V_ip6stat.ip6s_odropped++;
1134 m->m_pkthdr.rcvif = NULL;
1135 m->m_flags = m0->m_flags & M_COPYFLAGS; /* incl. FIB */
1137 mnext = &m->m_nextpkt;
1138 m->m_data += max_linkhdr;
1139 mhip6 = mtod(m, struct ip6_hdr *);
1141 m->m_len = sizeof(*mhip6);
1142 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1144 V_ip6stat.ip6s_odropped++;
1147 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1148 if (off + len >= tlen)
1151 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1152 mhip6->ip6_plen = htons((u_short)(len + hlen +
1153 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1154 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1156 V_ip6stat.ip6s_odropped++;
1159 m_cat(m, m_frgpart);
1160 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1161 m->m_pkthdr.rcvif = NULL;
1162 ip6f->ip6f_reserved = 0;
1163 ip6f->ip6f_ident = id;
1164 ip6f->ip6f_nxt = nextproto;
1165 V_ip6stat.ip6s_ofragments++;
1166 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1169 in6_ifstat_inc(ifp, ifs6_out_fragok);
1173 * Remove leading garbages.
1179 for (m0 = m; m; m = m0) {
1183 /* Record statistics for this interface address. */
1185 ia->ia_ifa.if_opackets++;
1186 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1188 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1194 V_ip6stat.ip6s_fragmented++;
1197 if (ro == &ip6route)
1199 if (ro_pmtu == &ip6route)
1209 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1210 m_freem(exthdrs.ip6e_dest1);
1211 m_freem(exthdrs.ip6e_rthdr);
1212 m_freem(exthdrs.ip6e_dest2);
1221 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1225 if (hlen > MCLBYTES)
1226 return (ENOBUFS); /* XXX */
1228 MGET(m, M_DONTWAIT, MT_DATA);
1233 MCLGET(m, M_DONTWAIT);
1234 if ((m->m_flags & M_EXT) == 0) {
1241 bcopy(hdr, mtod(m, caddr_t), hlen);
1248 * Insert jumbo payload option.
1251 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1257 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1260 * If there is no hop-by-hop options header, allocate new one.
1261 * If there is one but it doesn't have enough space to store the
1262 * jumbo payload option, allocate a cluster to store the whole options.
1263 * Otherwise, use it to store the options.
1265 if (exthdrs->ip6e_hbh == 0) {
1266 MGET(mopt, M_DONTWAIT, MT_DATA);
1269 mopt->m_len = JUMBOOPTLEN;
1270 optbuf = mtod(mopt, u_char *);
1271 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1272 exthdrs->ip6e_hbh = mopt;
1274 struct ip6_hbh *hbh;
1276 mopt = exthdrs->ip6e_hbh;
1277 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1280 * - exthdrs->ip6e_hbh is not referenced from places
1281 * other than exthdrs.
1282 * - exthdrs->ip6e_hbh is not an mbuf chain.
1284 int oldoptlen = mopt->m_len;
1288 * XXX: give up if the whole (new) hbh header does
1289 * not fit even in an mbuf cluster.
1291 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1295 * As a consequence, we must always prepare a cluster
1298 MGET(n, M_DONTWAIT, MT_DATA);
1300 MCLGET(n, M_DONTWAIT);
1301 if ((n->m_flags & M_EXT) == 0) {
1308 n->m_len = oldoptlen + JUMBOOPTLEN;
1309 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1311 optbuf = mtod(n, caddr_t) + oldoptlen;
1313 mopt = exthdrs->ip6e_hbh = n;
1315 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1316 mopt->m_len += JUMBOOPTLEN;
1318 optbuf[0] = IP6OPT_PADN;
1322 * Adjust the header length according to the pad and
1323 * the jumbo payload option.
1325 hbh = mtod(mopt, struct ip6_hbh *);
1326 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1329 /* fill in the option. */
1330 optbuf[2] = IP6OPT_JUMBO;
1332 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1333 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1335 /* finally, adjust the packet header length */
1336 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1343 * Insert fragment header and copy unfragmentable header portions.
1346 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1347 struct ip6_frag **frghdrp)
1349 struct mbuf *n, *mlast;
1351 if (hlen > sizeof(struct ip6_hdr)) {
1352 n = m_copym(m0, sizeof(struct ip6_hdr),
1353 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1360 /* Search for the last mbuf of unfragmentable part. */
1361 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1364 if ((mlast->m_flags & M_EXT) == 0 &&
1365 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1366 /* use the trailing space of the last mbuf for the fragment hdr */
1367 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1369 mlast->m_len += sizeof(struct ip6_frag);
1370 m->m_pkthdr.len += sizeof(struct ip6_frag);
1372 /* allocate a new mbuf for the fragment header */
1375 MGET(mfrg, M_DONTWAIT, MT_DATA);
1378 mfrg->m_len = sizeof(struct ip6_frag);
1379 *frghdrp = mtod(mfrg, struct ip6_frag *);
1380 mlast->m_next = mfrg;
1387 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1388 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1389 int *alwaysfragp, u_int fibnum)
1395 if (ro_pmtu != ro) {
1396 /* The first hop and the final destination may differ. */
1397 struct sockaddr_in6 *sa6_dst =
1398 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1399 if (ro_pmtu->ro_rt &&
1400 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1401 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1402 RTFREE(ro_pmtu->ro_rt);
1403 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1405 if (ro_pmtu->ro_rt == NULL) {
1406 bzero(sa6_dst, sizeof(*sa6_dst));
1407 sa6_dst->sin6_family = AF_INET6;
1408 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1409 sa6_dst->sin6_addr = *dst;
1411 in6_rtalloc(ro_pmtu, fibnum);
1414 if (ro_pmtu->ro_rt) {
1416 struct in_conninfo inc;
1418 bzero(&inc, sizeof(inc));
1419 inc.inc_flags |= INC_ISIPV6;
1420 inc.inc6_faddr = *dst;
1423 ifp = ro_pmtu->ro_rt->rt_ifp;
1424 ifmtu = IN6_LINKMTU(ifp);
1425 mtu = tcp_hc_getmtu(&inc);
1427 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu);
1429 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1432 else if (mtu < IPV6_MMTU) {
1434 * RFC2460 section 5, last paragraph:
1435 * if we record ICMPv6 too big message with
1436 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1437 * or smaller, with framgent header attached.
1438 * (fragment header is needed regardless from the
1439 * packet size, for translators to identify packets)
1443 } else if (mtu > ifmtu) {
1445 * The MTU on the route is larger than the MTU on
1446 * the interface! This shouldn't happen, unless the
1447 * MTU of the interface has been changed after the
1448 * interface was brought up. Change the MTU in the
1449 * route to match the interface MTU (as long as the
1450 * field isn't locked).
1453 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1456 mtu = IN6_LINKMTU(ifp);
1458 error = EHOSTUNREACH; /* XXX */
1462 *alwaysfragp = alwaysfrag;
1467 * IP6 socket option processing.
1470 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1472 int optdatalen, uproto;
1474 struct inpcb *in6p = sotoinpcb(so);
1476 int level, op, optname;
1480 level = sopt->sopt_level;
1481 op = sopt->sopt_dir;
1482 optname = sopt->sopt_name;
1483 optlen = sopt->sopt_valsize;
1487 uproto = (int)so->so_proto->pr_protocol;
1489 if (level != IPPROTO_IPV6) {
1492 if (sopt->sopt_level == SOL_SOCKET &&
1493 sopt->sopt_dir == SOPT_SET) {
1494 switch (sopt->sopt_name) {
1497 if (IN_MULTICAST(ntohl(in6p->inp_laddr.s_addr))) {
1498 if ((so->so_options &
1499 (SO_REUSEADDR | SO_REUSEPORT)) != 0)
1500 in6p->inp_flags2 |= INP_REUSEPORT;
1502 in6p->inp_flags2 &= ~INP_REUSEPORT;
1509 if ((so->so_options & SO_REUSEPORT) != 0)
1510 in6p->inp_flags2 |= INP_REUSEPORT;
1512 in6p->inp_flags2 &= ~INP_REUSEPORT;
1518 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1526 } else { /* level == IPPROTO_IPV6 */
1531 case IPV6_2292PKTOPTIONS:
1532 #ifdef IPV6_PKTOPTIONS
1533 case IPV6_PKTOPTIONS:
1538 error = soopt_getm(sopt, &m); /* XXX */
1541 error = soopt_mcopyin(sopt, m); /* XXX */
1544 error = ip6_pcbopts(&in6p->in6p_outputopts,
1546 m_freem(m); /* XXX */
1551 * Use of some Hop-by-Hop options or some
1552 * Destination options, might require special
1553 * privilege. That is, normal applications
1554 * (without special privilege) might be forbidden
1555 * from setting certain options in outgoing packets,
1556 * and might never see certain options in received
1557 * packets. [RFC 2292 Section 6]
1558 * KAME specific note:
1559 * KAME prevents non-privileged users from sending or
1560 * receiving ANY hbh/dst options in order to avoid
1561 * overhead of parsing options in the kernel.
1563 case IPV6_RECVHOPOPTS:
1564 case IPV6_RECVDSTOPTS:
1565 case IPV6_RECVRTHDRDSTOPTS:
1567 error = priv_check(td,
1568 PRIV_NETINET_SETHDROPTS);
1573 case IPV6_UNICAST_HOPS:
1577 case IPV6_RECVPKTINFO:
1578 case IPV6_RECVHOPLIMIT:
1579 case IPV6_RECVRTHDR:
1580 case IPV6_RECVPATHMTU:
1581 case IPV6_RECVTCLASS:
1583 case IPV6_AUTOFLOWLABEL:
1585 if (optname == IPV6_BINDANY && td != NULL) {
1586 error = priv_check(td,
1587 PRIV_NETINET_BINDANY);
1592 if (optlen != sizeof(int)) {
1596 error = sooptcopyin(sopt, &optval,
1597 sizeof optval, sizeof optval);
1602 case IPV6_UNICAST_HOPS:
1603 if (optval < -1 || optval >= 256)
1606 /* -1 = kernel default */
1607 in6p->in6p_hops = optval;
1608 if ((in6p->inp_vflag &
1610 in6p->inp_ip_ttl = optval;
1613 #define OPTSET(bit) \
1617 in6p->inp_flags |= (bit); \
1619 in6p->inp_flags &= ~(bit); \
1620 INP_WUNLOCK(in6p); \
1621 } while (/*CONSTCOND*/ 0)
1622 #define OPTSET2292(bit) \
1625 in6p->inp_flags |= IN6P_RFC2292; \
1627 in6p->inp_flags |= (bit); \
1629 in6p->inp_flags &= ~(bit); \
1630 INP_WUNLOCK(in6p); \
1631 } while (/*CONSTCOND*/ 0)
1632 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1634 case IPV6_RECVPKTINFO:
1635 /* cannot mix with RFC2292 */
1636 if (OPTBIT(IN6P_RFC2292)) {
1640 OPTSET(IN6P_PKTINFO);
1645 struct ip6_pktopts **optp;
1647 /* cannot mix with RFC2292 */
1648 if (OPTBIT(IN6P_RFC2292)) {
1652 optp = &in6p->in6p_outputopts;
1653 error = ip6_pcbopt(IPV6_HOPLIMIT,
1654 (u_char *)&optval, sizeof(optval),
1655 optp, (td != NULL) ? td->td_ucred :
1660 case IPV6_RECVHOPLIMIT:
1661 /* cannot mix with RFC2292 */
1662 if (OPTBIT(IN6P_RFC2292)) {
1666 OPTSET(IN6P_HOPLIMIT);
1669 case IPV6_RECVHOPOPTS:
1670 /* cannot mix with RFC2292 */
1671 if (OPTBIT(IN6P_RFC2292)) {
1675 OPTSET(IN6P_HOPOPTS);
1678 case IPV6_RECVDSTOPTS:
1679 /* cannot mix with RFC2292 */
1680 if (OPTBIT(IN6P_RFC2292)) {
1684 OPTSET(IN6P_DSTOPTS);
1687 case IPV6_RECVRTHDRDSTOPTS:
1688 /* cannot mix with RFC2292 */
1689 if (OPTBIT(IN6P_RFC2292)) {
1693 OPTSET(IN6P_RTHDRDSTOPTS);
1696 case IPV6_RECVRTHDR:
1697 /* cannot mix with RFC2292 */
1698 if (OPTBIT(IN6P_RFC2292)) {
1709 case IPV6_RECVPATHMTU:
1711 * We ignore this option for TCP
1713 * (RFC3542 leaves this case
1716 if (uproto != IPPROTO_TCP)
1722 * make setsockopt(IPV6_V6ONLY)
1723 * available only prior to bind(2).
1724 * see ipng mailing list, Jun 22 2001.
1726 if (in6p->inp_lport ||
1727 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1731 OPTSET(IN6P_IPV6_V6ONLY);
1733 in6p->inp_vflag &= ~INP_IPV4;
1735 in6p->inp_vflag |= INP_IPV4;
1737 case IPV6_RECVTCLASS:
1738 /* cannot mix with RFC2292 XXX */
1739 if (OPTBIT(IN6P_RFC2292)) {
1743 OPTSET(IN6P_TCLASS);
1745 case IPV6_AUTOFLOWLABEL:
1746 OPTSET(IN6P_AUTOFLOWLABEL);
1750 OPTSET(INP_BINDANY);
1757 case IPV6_USE_MIN_MTU:
1758 case IPV6_PREFER_TEMPADDR:
1759 if (optlen != sizeof(optval)) {
1763 error = sooptcopyin(sopt, &optval,
1764 sizeof optval, sizeof optval);
1768 struct ip6_pktopts **optp;
1769 optp = &in6p->in6p_outputopts;
1770 error = ip6_pcbopt(optname,
1771 (u_char *)&optval, sizeof(optval),
1772 optp, (td != NULL) ? td->td_ucred :
1777 case IPV6_2292PKTINFO:
1778 case IPV6_2292HOPLIMIT:
1779 case IPV6_2292HOPOPTS:
1780 case IPV6_2292DSTOPTS:
1781 case IPV6_2292RTHDR:
1783 if (optlen != sizeof(int)) {
1787 error = sooptcopyin(sopt, &optval,
1788 sizeof optval, sizeof optval);
1792 case IPV6_2292PKTINFO:
1793 OPTSET2292(IN6P_PKTINFO);
1795 case IPV6_2292HOPLIMIT:
1796 OPTSET2292(IN6P_HOPLIMIT);
1798 case IPV6_2292HOPOPTS:
1800 * Check super-user privilege.
1801 * See comments for IPV6_RECVHOPOPTS.
1804 error = priv_check(td,
1805 PRIV_NETINET_SETHDROPTS);
1809 OPTSET2292(IN6P_HOPOPTS);
1811 case IPV6_2292DSTOPTS:
1813 error = priv_check(td,
1814 PRIV_NETINET_SETHDROPTS);
1818 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1820 case IPV6_2292RTHDR:
1821 OPTSET2292(IN6P_RTHDR);
1829 case IPV6_RTHDRDSTOPTS:
1832 /* new advanced API (RFC3542) */
1834 u_char optbuf_storage[MCLBYTES];
1836 struct ip6_pktopts **optp;
1838 /* cannot mix with RFC2292 */
1839 if (OPTBIT(IN6P_RFC2292)) {
1845 * We only ensure valsize is not too large
1846 * here. Further validation will be done
1849 error = sooptcopyin(sopt, optbuf_storage,
1850 sizeof(optbuf_storage), 0);
1853 optlen = sopt->sopt_valsize;
1854 optbuf = optbuf_storage;
1855 optp = &in6p->in6p_outputopts;
1856 error = ip6_pcbopt(optname, optbuf, optlen,
1857 optp, (td != NULL) ? td->td_ucred : NULL,
1863 case IPV6_MULTICAST_IF:
1864 case IPV6_MULTICAST_HOPS:
1865 case IPV6_MULTICAST_LOOP:
1866 case IPV6_JOIN_GROUP:
1867 case IPV6_LEAVE_GROUP:
1869 case MCAST_BLOCK_SOURCE:
1870 case MCAST_UNBLOCK_SOURCE:
1871 case MCAST_JOIN_GROUP:
1872 case MCAST_LEAVE_GROUP:
1873 case MCAST_JOIN_SOURCE_GROUP:
1874 case MCAST_LEAVE_SOURCE_GROUP:
1875 error = ip6_setmoptions(in6p, sopt);
1878 case IPV6_PORTRANGE:
1879 error = sooptcopyin(sopt, &optval,
1880 sizeof optval, sizeof optval);
1886 case IPV6_PORTRANGE_DEFAULT:
1887 in6p->inp_flags &= ~(INP_LOWPORT);
1888 in6p->inp_flags &= ~(INP_HIGHPORT);
1891 case IPV6_PORTRANGE_HIGH:
1892 in6p->inp_flags &= ~(INP_LOWPORT);
1893 in6p->inp_flags |= INP_HIGHPORT;
1896 case IPV6_PORTRANGE_LOW:
1897 in6p->inp_flags &= ~(INP_HIGHPORT);
1898 in6p->inp_flags |= INP_LOWPORT;
1909 case IPV6_IPSEC_POLICY:
1914 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1916 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1918 req = mtod(m, caddr_t);
1919 error = ipsec_set_policy(in6p, optname, req,
1920 m->m_len, (sopt->sopt_td != NULL) ?
1921 sopt->sopt_td->td_ucred : NULL);
1928 error = ENOPROTOOPT;
1936 case IPV6_2292PKTOPTIONS:
1937 #ifdef IPV6_PKTOPTIONS
1938 case IPV6_PKTOPTIONS:
1941 * RFC3542 (effectively) deprecated the
1942 * semantics of the 2292-style pktoptions.
1943 * Since it was not reliable in nature (i.e.,
1944 * applications had to expect the lack of some
1945 * information after all), it would make sense
1946 * to simplify this part by always returning
1949 sopt->sopt_valsize = 0;
1952 case IPV6_RECVHOPOPTS:
1953 case IPV6_RECVDSTOPTS:
1954 case IPV6_RECVRTHDRDSTOPTS:
1955 case IPV6_UNICAST_HOPS:
1956 case IPV6_RECVPKTINFO:
1957 case IPV6_RECVHOPLIMIT:
1958 case IPV6_RECVRTHDR:
1959 case IPV6_RECVPATHMTU:
1963 case IPV6_PORTRANGE:
1964 case IPV6_RECVTCLASS:
1965 case IPV6_AUTOFLOWLABEL:
1969 case IPV6_RECVHOPOPTS:
1970 optval = OPTBIT(IN6P_HOPOPTS);
1973 case IPV6_RECVDSTOPTS:
1974 optval = OPTBIT(IN6P_DSTOPTS);
1977 case IPV6_RECVRTHDRDSTOPTS:
1978 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1981 case IPV6_UNICAST_HOPS:
1982 optval = in6p->in6p_hops;
1985 case IPV6_RECVPKTINFO:
1986 optval = OPTBIT(IN6P_PKTINFO);
1989 case IPV6_RECVHOPLIMIT:
1990 optval = OPTBIT(IN6P_HOPLIMIT);
1993 case IPV6_RECVRTHDR:
1994 optval = OPTBIT(IN6P_RTHDR);
1997 case IPV6_RECVPATHMTU:
1998 optval = OPTBIT(IN6P_MTU);
2002 optval = OPTBIT(INP_FAITH);
2006 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2009 case IPV6_PORTRANGE:
2012 flags = in6p->inp_flags;
2013 if (flags & INP_HIGHPORT)
2014 optval = IPV6_PORTRANGE_HIGH;
2015 else if (flags & INP_LOWPORT)
2016 optval = IPV6_PORTRANGE_LOW;
2021 case IPV6_RECVTCLASS:
2022 optval = OPTBIT(IN6P_TCLASS);
2025 case IPV6_AUTOFLOWLABEL:
2026 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2030 optval = OPTBIT(INP_BINDANY);
2035 error = sooptcopyout(sopt, &optval,
2042 struct ip6_mtuinfo mtuinfo;
2043 struct route_in6 sro;
2045 bzero(&sro, sizeof(sro));
2047 if (!(so->so_state & SS_ISCONNECTED))
2050 * XXX: we dot not consider the case of source
2051 * routing, or optional information to specify
2052 * the outgoing interface.
2054 error = ip6_getpmtu(&sro, NULL, NULL,
2055 &in6p->in6p_faddr, &pmtu, NULL,
2061 if (pmtu > IPV6_MAXPACKET)
2062 pmtu = IPV6_MAXPACKET;
2064 bzero(&mtuinfo, sizeof(mtuinfo));
2065 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2066 optdata = (void *)&mtuinfo;
2067 optdatalen = sizeof(mtuinfo);
2068 error = sooptcopyout(sopt, optdata,
2073 case IPV6_2292PKTINFO:
2074 case IPV6_2292HOPLIMIT:
2075 case IPV6_2292HOPOPTS:
2076 case IPV6_2292RTHDR:
2077 case IPV6_2292DSTOPTS:
2079 case IPV6_2292PKTINFO:
2080 optval = OPTBIT(IN6P_PKTINFO);
2082 case IPV6_2292HOPLIMIT:
2083 optval = OPTBIT(IN6P_HOPLIMIT);
2085 case IPV6_2292HOPOPTS:
2086 optval = OPTBIT(IN6P_HOPOPTS);
2088 case IPV6_2292RTHDR:
2089 optval = OPTBIT(IN6P_RTHDR);
2091 case IPV6_2292DSTOPTS:
2092 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2095 error = sooptcopyout(sopt, &optval,
2102 case IPV6_RTHDRDSTOPTS:
2106 case IPV6_USE_MIN_MTU:
2107 case IPV6_PREFER_TEMPADDR:
2108 error = ip6_getpcbopt(in6p->in6p_outputopts,
2112 case IPV6_MULTICAST_IF:
2113 case IPV6_MULTICAST_HOPS:
2114 case IPV6_MULTICAST_LOOP:
2116 error = ip6_getmoptions(in6p, sopt);
2120 case IPV6_IPSEC_POLICY:
2124 struct mbuf *m = NULL;
2125 struct mbuf **mp = &m;
2126 size_t ovalsize = sopt->sopt_valsize;
2127 caddr_t oval = (caddr_t)sopt->sopt_val;
2129 error = soopt_getm(sopt, &m); /* XXX */
2132 error = soopt_mcopyin(sopt, m); /* XXX */
2135 sopt->sopt_valsize = ovalsize;
2136 sopt->sopt_val = oval;
2138 req = mtod(m, caddr_t);
2141 error = ipsec_get_policy(in6p, req, len, mp);
2143 error = soopt_mcopyout(sopt, m); /* XXX */
2144 if (error == 0 && m)
2151 error = ENOPROTOOPT;
2161 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2163 int error = 0, optval, optlen;
2164 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2165 struct inpcb *in6p = sotoinpcb(so);
2166 int level, op, optname;
2168 level = sopt->sopt_level;
2169 op = sopt->sopt_dir;
2170 optname = sopt->sopt_name;
2171 optlen = sopt->sopt_valsize;
2173 if (level != IPPROTO_IPV6) {
2180 * For ICMPv6 sockets, no modification allowed for checksum
2181 * offset, permit "no change" values to help existing apps.
2183 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2184 * for an ICMPv6 socket will fail."
2185 * The current behavior does not meet RFC3542.
2189 if (optlen != sizeof(int)) {
2193 error = sooptcopyin(sopt, &optval, sizeof(optval),
2197 if ((optval % 2) != 0) {
2198 /* the API assumes even offset values */
2200 } else if (so->so_proto->pr_protocol ==
2202 if (optval != icmp6off)
2205 in6p->in6p_cksum = optval;
2209 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2212 optval = in6p->in6p_cksum;
2214 error = sooptcopyout(sopt, &optval, sizeof(optval));
2224 error = ENOPROTOOPT;
2232 * Set up IP6 options in pcb for insertion in output packets or
2233 * specifying behavior of outgoing packets.
2236 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2237 struct socket *so, struct sockopt *sopt)
2239 struct ip6_pktopts *opt = *pktopt;
2241 struct thread *td = sopt->sopt_td;
2243 /* turn off any old options. */
2246 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2247 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2248 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2249 printf("ip6_pcbopts: all specified options are cleared.\n");
2251 ip6_clearpktopts(opt, -1);
2253 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2256 if (!m || m->m_len == 0) {
2258 * Only turning off any previous options, regardless of
2259 * whether the opt is just created or given.
2261 free(opt, M_IP6OPT);
2265 /* set options specified by user. */
2266 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2267 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2268 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2269 free(opt, M_IP6OPT);
2277 * initialize ip6_pktopts. beware that there are non-zero default values in
2281 ip6_initpktopts(struct ip6_pktopts *opt)
2284 bzero(opt, sizeof(*opt));
2285 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2286 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2287 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2288 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2292 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2293 struct ucred *cred, int uproto)
2295 struct ip6_pktopts *opt;
2297 if (*pktopt == NULL) {
2298 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2300 ip6_initpktopts(*pktopt);
2304 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2308 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2310 void *optdata = NULL;
2312 struct ip6_ext *ip6e;
2314 struct in6_pktinfo null_pktinfo;
2315 int deftclass = 0, on;
2316 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2317 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2321 if (pktopt && pktopt->ip6po_pktinfo)
2322 optdata = (void *)pktopt->ip6po_pktinfo;
2324 /* XXX: we don't have to do this every time... */
2325 bzero(&null_pktinfo, sizeof(null_pktinfo));
2326 optdata = (void *)&null_pktinfo;
2328 optdatalen = sizeof(struct in6_pktinfo);
2331 if (pktopt && pktopt->ip6po_tclass >= 0)
2332 optdata = (void *)&pktopt->ip6po_tclass;
2334 optdata = (void *)&deftclass;
2335 optdatalen = sizeof(int);
2338 if (pktopt && pktopt->ip6po_hbh) {
2339 optdata = (void *)pktopt->ip6po_hbh;
2340 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2341 optdatalen = (ip6e->ip6e_len + 1) << 3;
2345 if (pktopt && pktopt->ip6po_rthdr) {
2346 optdata = (void *)pktopt->ip6po_rthdr;
2347 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2348 optdatalen = (ip6e->ip6e_len + 1) << 3;
2351 case IPV6_RTHDRDSTOPTS:
2352 if (pktopt && pktopt->ip6po_dest1) {
2353 optdata = (void *)pktopt->ip6po_dest1;
2354 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2355 optdatalen = (ip6e->ip6e_len + 1) << 3;
2359 if (pktopt && pktopt->ip6po_dest2) {
2360 optdata = (void *)pktopt->ip6po_dest2;
2361 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2362 optdatalen = (ip6e->ip6e_len + 1) << 3;
2366 if (pktopt && pktopt->ip6po_nexthop) {
2367 optdata = (void *)pktopt->ip6po_nexthop;
2368 optdatalen = pktopt->ip6po_nexthop->sa_len;
2371 case IPV6_USE_MIN_MTU:
2373 optdata = (void *)&pktopt->ip6po_minmtu;
2375 optdata = (void *)&defminmtu;
2376 optdatalen = sizeof(int);
2379 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2383 optdata = (void *)&on;
2384 optdatalen = sizeof(on);
2386 case IPV6_PREFER_TEMPADDR:
2388 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2390 optdata = (void *)&defpreftemp;
2391 optdatalen = sizeof(int);
2393 default: /* should not happen */
2395 panic("ip6_getpcbopt: unexpected option\n");
2397 return (ENOPROTOOPT);
2400 error = sooptcopyout(sopt, optdata, optdatalen);
2406 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2411 if (optname == -1 || optname == IPV6_PKTINFO) {
2412 if (pktopt->ip6po_pktinfo)
2413 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2414 pktopt->ip6po_pktinfo = NULL;
2416 if (optname == -1 || optname == IPV6_HOPLIMIT)
2417 pktopt->ip6po_hlim = -1;
2418 if (optname == -1 || optname == IPV6_TCLASS)
2419 pktopt->ip6po_tclass = -1;
2420 if (optname == -1 || optname == IPV6_NEXTHOP) {
2421 if (pktopt->ip6po_nextroute.ro_rt) {
2422 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2423 pktopt->ip6po_nextroute.ro_rt = NULL;
2425 if (pktopt->ip6po_nexthop)
2426 free(pktopt->ip6po_nexthop, M_IP6OPT);
2427 pktopt->ip6po_nexthop = NULL;
2429 if (optname == -1 || optname == IPV6_HOPOPTS) {
2430 if (pktopt->ip6po_hbh)
2431 free(pktopt->ip6po_hbh, M_IP6OPT);
2432 pktopt->ip6po_hbh = NULL;
2434 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2435 if (pktopt->ip6po_dest1)
2436 free(pktopt->ip6po_dest1, M_IP6OPT);
2437 pktopt->ip6po_dest1 = NULL;
2439 if (optname == -1 || optname == IPV6_RTHDR) {
2440 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2441 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2442 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2443 if (pktopt->ip6po_route.ro_rt) {
2444 RTFREE(pktopt->ip6po_route.ro_rt);
2445 pktopt->ip6po_route.ro_rt = NULL;
2448 if (optname == -1 || optname == IPV6_DSTOPTS) {
2449 if (pktopt->ip6po_dest2)
2450 free(pktopt->ip6po_dest2, M_IP6OPT);
2451 pktopt->ip6po_dest2 = NULL;
2455 #define PKTOPT_EXTHDRCPY(type) \
2458 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2459 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2460 if (dst->type == NULL && canwait == M_NOWAIT)\
2462 bcopy(src->type, dst->type, hlen);\
2464 } while (/*CONSTCOND*/ 0)
2467 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2469 if (dst == NULL || src == NULL) {
2470 printf("ip6_clearpktopts: invalid argument\n");
2474 dst->ip6po_hlim = src->ip6po_hlim;
2475 dst->ip6po_tclass = src->ip6po_tclass;
2476 dst->ip6po_flags = src->ip6po_flags;
2477 dst->ip6po_minmtu = src->ip6po_minmtu;
2478 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2479 if (src->ip6po_pktinfo) {
2480 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2482 if (dst->ip6po_pktinfo == NULL)
2484 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2486 if (src->ip6po_nexthop) {
2487 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2489 if (dst->ip6po_nexthop == NULL)
2491 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2492 src->ip6po_nexthop->sa_len);
2494 PKTOPT_EXTHDRCPY(ip6po_hbh);
2495 PKTOPT_EXTHDRCPY(ip6po_dest1);
2496 PKTOPT_EXTHDRCPY(ip6po_dest2);
2497 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2501 ip6_clearpktopts(dst, -1);
2504 #undef PKTOPT_EXTHDRCPY
2506 struct ip6_pktopts *
2507 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2510 struct ip6_pktopts *dst;
2512 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2515 ip6_initpktopts(dst);
2517 if ((error = copypktopts(dst, src, canwait)) != 0) {
2518 free(dst, M_IP6OPT);
2526 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2531 ip6_clearpktopts(pktopt, -1);
2533 free(pktopt, M_IP6OPT);
2537 * Set IPv6 outgoing packet options based on advanced API.
2540 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2541 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2543 struct cmsghdr *cm = 0;
2545 if (control == NULL || opt == NULL)
2548 ip6_initpktopts(opt);
2553 * If stickyopt is provided, make a local copy of the options
2554 * for this particular packet, then override them by ancillary
2556 * XXX: copypktopts() does not copy the cached route to a next
2557 * hop (if any). This is not very good in terms of efficiency,
2558 * but we can allow this since this option should be rarely
2561 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2566 * XXX: Currently, we assume all the optional information is stored
2569 if (control->m_next)
2572 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2573 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2576 if (control->m_len < CMSG_LEN(0))
2579 cm = mtod(control, struct cmsghdr *);
2580 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2582 if (cm->cmsg_level != IPPROTO_IPV6)
2585 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2586 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2595 * Set a particular packet option, as a sticky option or an ancillary data
2596 * item. "len" can be 0 only when it's a sticky option.
2597 * We have 4 cases of combination of "sticky" and "cmsg":
2598 * "sticky=0, cmsg=0": impossible
2599 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2600 * "sticky=1, cmsg=0": RFC3542 socket option
2601 * "sticky=1, cmsg=1": RFC2292 socket option
2604 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2605 struct ucred *cred, int sticky, int cmsg, int uproto)
2607 int minmtupolicy, preftemp;
2610 if (!sticky && !cmsg) {
2612 printf("ip6_setpktopt: impossible case\n");
2618 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2619 * not be specified in the context of RFC3542. Conversely,
2620 * RFC3542 types should not be specified in the context of RFC2292.
2624 case IPV6_2292PKTINFO:
2625 case IPV6_2292HOPLIMIT:
2626 case IPV6_2292NEXTHOP:
2627 case IPV6_2292HOPOPTS:
2628 case IPV6_2292DSTOPTS:
2629 case IPV6_2292RTHDR:
2630 case IPV6_2292PKTOPTIONS:
2631 return (ENOPROTOOPT);
2634 if (sticky && cmsg) {
2641 case IPV6_RTHDRDSTOPTS:
2643 case IPV6_USE_MIN_MTU:
2646 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2647 return (ENOPROTOOPT);
2652 case IPV6_2292PKTINFO:
2655 struct ifnet *ifp = NULL;
2656 struct in6_pktinfo *pktinfo;
2658 if (len != sizeof(struct in6_pktinfo))
2661 pktinfo = (struct in6_pktinfo *)buf;
2664 * An application can clear any sticky IPV6_PKTINFO option by
2665 * doing a "regular" setsockopt with ipi6_addr being
2666 * in6addr_any and ipi6_ifindex being zero.
2667 * [RFC 3542, Section 6]
2669 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2670 pktinfo->ipi6_ifindex == 0 &&
2671 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2672 ip6_clearpktopts(opt, optname);
2676 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2677 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2681 /* validate the interface index if specified. */
2682 if (pktinfo->ipi6_ifindex > V_if_index ||
2683 pktinfo->ipi6_ifindex < 0) {
2686 if (pktinfo->ipi6_ifindex) {
2687 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2693 * We store the address anyway, and let in6_selectsrc()
2694 * validate the specified address. This is because ipi6_addr
2695 * may not have enough information about its scope zone, and
2696 * we may need additional information (such as outgoing
2697 * interface or the scope zone of a destination address) to
2698 * disambiguate the scope.
2699 * XXX: the delay of the validation may confuse the
2700 * application when it is used as a sticky option.
2702 if (opt->ip6po_pktinfo == NULL) {
2703 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2704 M_IP6OPT, M_NOWAIT);
2705 if (opt->ip6po_pktinfo == NULL)
2708 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2712 case IPV6_2292HOPLIMIT:
2718 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2719 * to simplify the ordering among hoplimit options.
2721 if (optname == IPV6_HOPLIMIT && sticky)
2722 return (ENOPROTOOPT);
2724 if (len != sizeof(int))
2727 if (*hlimp < -1 || *hlimp > 255)
2730 opt->ip6po_hlim = *hlimp;
2738 if (len != sizeof(int))
2740 tclass = *(int *)buf;
2741 if (tclass < -1 || tclass > 255)
2744 opt->ip6po_tclass = tclass;
2748 case IPV6_2292NEXTHOP:
2751 error = priv_check_cred(cred,
2752 PRIV_NETINET_SETHDROPTS, 0);
2757 if (len == 0) { /* just remove the option */
2758 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2762 /* check if cmsg_len is large enough for sa_len */
2763 if (len < sizeof(struct sockaddr) || len < *buf)
2766 switch (((struct sockaddr *)buf)->sa_family) {
2769 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2772 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2775 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2776 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2779 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2785 case AF_LINK: /* should eventually be supported */
2787 return (EAFNOSUPPORT);
2790 /* turn off the previous option, then set the new option. */
2791 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2792 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2793 if (opt->ip6po_nexthop == NULL)
2795 bcopy(buf, opt->ip6po_nexthop, *buf);
2798 case IPV6_2292HOPOPTS:
2801 struct ip6_hbh *hbh;
2805 * XXX: We don't allow a non-privileged user to set ANY HbH
2806 * options, since per-option restriction has too much
2810 error = priv_check_cred(cred,
2811 PRIV_NETINET_SETHDROPTS, 0);
2817 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2818 break; /* just remove the option */
2821 /* message length validation */
2822 if (len < sizeof(struct ip6_hbh))
2824 hbh = (struct ip6_hbh *)buf;
2825 hbhlen = (hbh->ip6h_len + 1) << 3;
2829 /* turn off the previous option, then set the new option. */
2830 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2831 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2832 if (opt->ip6po_hbh == NULL)
2834 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2839 case IPV6_2292DSTOPTS:
2841 case IPV6_RTHDRDSTOPTS:
2843 struct ip6_dest *dest, **newdest = NULL;
2846 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2847 error = priv_check_cred(cred,
2848 PRIV_NETINET_SETHDROPTS, 0);
2854 ip6_clearpktopts(opt, optname);
2855 break; /* just remove the option */
2858 /* message length validation */
2859 if (len < sizeof(struct ip6_dest))
2861 dest = (struct ip6_dest *)buf;
2862 destlen = (dest->ip6d_len + 1) << 3;
2867 * Determine the position that the destination options header
2868 * should be inserted; before or after the routing header.
2871 case IPV6_2292DSTOPTS:
2873 * The old advacned API is ambiguous on this point.
2874 * Our approach is to determine the position based
2875 * according to the existence of a routing header.
2876 * Note, however, that this depends on the order of the
2877 * extension headers in the ancillary data; the 1st
2878 * part of the destination options header must appear
2879 * before the routing header in the ancillary data,
2881 * RFC3542 solved the ambiguity by introducing
2882 * separate ancillary data or option types.
2884 if (opt->ip6po_rthdr == NULL)
2885 newdest = &opt->ip6po_dest1;
2887 newdest = &opt->ip6po_dest2;
2889 case IPV6_RTHDRDSTOPTS:
2890 newdest = &opt->ip6po_dest1;
2893 newdest = &opt->ip6po_dest2;
2897 /* turn off the previous option, then set the new option. */
2898 ip6_clearpktopts(opt, optname);
2899 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2900 if (*newdest == NULL)
2902 bcopy(dest, *newdest, destlen);
2907 case IPV6_2292RTHDR:
2910 struct ip6_rthdr *rth;
2914 ip6_clearpktopts(opt, IPV6_RTHDR);
2915 break; /* just remove the option */
2918 /* message length validation */
2919 if (len < sizeof(struct ip6_rthdr))
2921 rth = (struct ip6_rthdr *)buf;
2922 rthlen = (rth->ip6r_len + 1) << 3;
2926 switch (rth->ip6r_type) {
2927 case IPV6_RTHDR_TYPE_0:
2928 if (rth->ip6r_len == 0) /* must contain one addr */
2930 if (rth->ip6r_len % 2) /* length must be even */
2932 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2936 return (EINVAL); /* not supported */
2939 /* turn off the previous option */
2940 ip6_clearpktopts(opt, IPV6_RTHDR);
2941 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2942 if (opt->ip6po_rthdr == NULL)
2944 bcopy(rth, opt->ip6po_rthdr, rthlen);
2949 case IPV6_USE_MIN_MTU:
2950 if (len != sizeof(int))
2952 minmtupolicy = *(int *)buf;
2953 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2954 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2955 minmtupolicy != IP6PO_MINMTU_ALL) {
2958 opt->ip6po_minmtu = minmtupolicy;
2962 if (len != sizeof(int))
2965 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2967 * we ignore this option for TCP sockets.
2968 * (RFC3542 leaves this case unspecified.)
2970 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2972 opt->ip6po_flags |= IP6PO_DONTFRAG;
2975 case IPV6_PREFER_TEMPADDR:
2976 if (len != sizeof(int))
2978 preftemp = *(int *)buf;
2979 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2980 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2981 preftemp != IP6PO_TEMPADDR_PREFER) {
2984 opt->ip6po_prefer_tempaddr = preftemp;
2988 return (ENOPROTOOPT);
2989 } /* end of switch */
2995 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2996 * packet to the input queue of a specified interface. Note that this
2997 * calls the output routine of the loopback "driver", but with an interface
2998 * pointer that might NOT be &loif -- easier than replicating that code here.
3001 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
3004 struct ip6_hdr *ip6;
3006 copym = m_copy(m, 0, M_COPYALL);
3011 * Make sure to deep-copy IPv6 header portion in case the data
3012 * is in an mbuf cluster, so that we can safely override the IPv6
3013 * header portion later.
3015 if ((copym->m_flags & M_EXT) != 0 ||
3016 copym->m_len < sizeof(struct ip6_hdr)) {
3017 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3023 if (copym->m_len < sizeof(*ip6)) {
3029 ip6 = mtod(copym, struct ip6_hdr *);
3031 * clear embedded scope identifiers if necessary.
3032 * in6_clearscope will touch the addresses only when necessary.
3034 in6_clearscope(&ip6->ip6_src);
3035 in6_clearscope(&ip6->ip6_dst);
3037 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
3041 * Chop IPv6 header off from the payload.
3044 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3047 struct ip6_hdr *ip6;
3049 ip6 = mtod(m, struct ip6_hdr *);
3050 if (m->m_len > sizeof(*ip6)) {
3051 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3056 M_MOVE_PKTHDR(mh, m);
3057 MH_ALIGN(mh, sizeof(*ip6));
3058 m->m_len -= sizeof(*ip6);
3059 m->m_data += sizeof(*ip6);
3062 m->m_len = sizeof(*ip6);
3063 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3065 exthdrs->ip6e_ip6 = m;
3070 * Compute IPv6 extension header length.
3073 ip6_optlen(struct inpcb *in6p)
3077 if (!in6p->in6p_outputopts)
3082 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3084 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3085 if (in6p->in6p_outputopts->ip6po_rthdr)
3086 /* dest1 is valid with rthdr only */
3087 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3088 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3089 len += elen(in6p->in6p_outputopts->ip6po_dest2);