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 __P((int, u_char *, int, struct ip6_pktopts **,
134 struct ucred *, int));
135 static int ip6_pcbopts __P((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 __P((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 __P((struct mbuf *, struct mbuf *, int,
143 struct ip6_frag **));
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 __P((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 off: %d\n",
199 __func__, m->m_len, offset);
201 * XXX this should not happen, but if it does, the correct
202 * behavior may be to insert the checksum in the appropriate
203 * next mbuf in the chain.
207 *(u_short *)(m->m_data + offset) = csum;
211 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
212 * header (with pri, len, nxt, hlim, src, dst).
213 * This function may modify ver and hlim only.
214 * The mbuf chain containing the packet will be freed.
215 * The mbuf opt, if present, will not be freed.
217 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
218 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
219 * which is rt_rmx.rmx_mtu.
221 * ifpp - XXX: just for statistics
224 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
225 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
226 struct ifnet **ifpp, struct inpcb *inp)
228 struct ip6_hdr *ip6, *mhip6;
229 struct ifnet *ifp, *origifp;
231 struct mbuf *mprev = NULL;
232 int hlen, tlen, len, off;
233 struct route_in6 ip6route;
234 struct rtentry *rt = NULL;
235 struct sockaddr_in6 *dst, src_sa, dst_sa;
236 struct in6_addr odst;
238 struct in6_ifaddr *ia = NULL;
240 int alwaysfrag, dontfrag;
241 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
242 struct ip6_exthdrs exthdrs;
243 struct in6_addr finaldst, src0, dst0;
245 struct route_in6 *ro_pmtu = NULL;
251 struct ipsec_output_state state;
252 struct ip6_rthdr *rh = NULL;
253 int needipsectun = 0;
255 struct secpolicy *sp = NULL;
257 #ifdef IPFIREWALL_FORWARD
258 struct m_tag *fwd_tag;
261 ip6 = mtod(m, struct ip6_hdr *);
263 printf ("ip6 is NULL");
268 M_SETFIB(m, inp->inp_inc.inc_fibnum);
270 finaldst = ip6->ip6_dst;
271 bzero(&exthdrs, sizeof(exthdrs));
273 /* Hop-by-Hop options header */
274 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
275 /* Destination options header(1st part) */
276 if (opt->ip6po_rthdr) {
278 * Destination options header(1st part)
279 * This only makes sense with a routing header.
280 * See Section 9.2 of RFC 3542.
281 * Disabling this part just for MIP6 convenience is
282 * a bad idea. We need to think carefully about a
283 * way to make the advanced API coexist with MIP6
284 * options, which might automatically be inserted in
287 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
290 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
291 /* Destination options header(2nd part) */
292 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
296 * IPSec checking which handles several cases.
297 * FAST IPSEC: We re-injected the packet.
300 switch(ip6_ipsec_output(&m, inp, &flags, &error, &ifp, &sp))
302 case 1: /* Bad packet */
304 case -1: /* Do IPSec */
306 case 0: /* No IPSec */
313 * Calculate the total length of the extension header chain.
314 * Keep the length of the unfragmentable part for fragmentation.
317 if (exthdrs.ip6e_hbh)
318 optlen += exthdrs.ip6e_hbh->m_len;
319 if (exthdrs.ip6e_dest1)
320 optlen += exthdrs.ip6e_dest1->m_len;
321 if (exthdrs.ip6e_rthdr)
322 optlen += exthdrs.ip6e_rthdr->m_len;
323 unfragpartlen = optlen + sizeof(struct ip6_hdr);
325 /* NOTE: we don't add AH/ESP length here. do that later. */
326 if (exthdrs.ip6e_dest2)
327 optlen += exthdrs.ip6e_dest2->m_len;
330 * If we need IPsec, or there is at least one extension header,
331 * separate IP6 header from the payload.
333 if ((needipsec || optlen) && !hdrsplit) {
334 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
338 m = exthdrs.ip6e_ip6;
343 ip6 = mtod(m, struct ip6_hdr *);
345 /* adjust mbuf packet header length */
346 m->m_pkthdr.len += optlen;
347 plen = m->m_pkthdr.len - sizeof(*ip6);
349 /* If this is a jumbo payload, insert a jumbo payload option. */
350 if (plen > IPV6_MAXPACKET) {
352 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
356 m = exthdrs.ip6e_ip6;
360 ip6 = mtod(m, struct ip6_hdr *);
361 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
365 ip6->ip6_plen = htons(plen);
368 * Concatenate headers and fill in next header fields.
369 * Here we have, on "m"
371 * and we insert headers accordingly. Finally, we should be getting:
372 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
374 * during the header composing process, "m" points to IPv6 header.
375 * "mprev" points to an extension header prior to esp.
377 u_char *nexthdrp = &ip6->ip6_nxt;
381 * we treat dest2 specially. this makes IPsec processing
382 * much easier. the goal here is to make mprev point the
383 * mbuf prior to dest2.
385 * result: IPv6 dest2 payload
386 * m and mprev will point to IPv6 header.
388 if (exthdrs.ip6e_dest2) {
390 panic("assumption failed: hdr not split");
391 exthdrs.ip6e_dest2->m_next = m->m_next;
392 m->m_next = exthdrs.ip6e_dest2;
393 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
394 ip6->ip6_nxt = IPPROTO_DSTOPTS;
398 * result: IPv6 hbh dest1 rthdr dest2 payload
399 * m will point to IPv6 header. mprev will point to the
400 * extension header prior to dest2 (rthdr in the above case).
402 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
403 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
405 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
413 * pointers after IPsec headers are not valid any more.
414 * other pointers need a great care too.
415 * (IPsec routines should not mangle mbufs prior to AH/ESP)
417 exthdrs.ip6e_dest2 = NULL;
419 if (exthdrs.ip6e_rthdr) {
420 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
421 segleft_org = rh->ip6r_segleft;
422 rh->ip6r_segleft = 0;
425 bzero(&state, sizeof(state));
427 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
430 if (error == EJUSTRETURN) {
432 * We had a SP with a level of 'use' and no SA. We
433 * will just continue to process the packet without
438 /* mbuf is already reclaimed in ipsec6_output_trans. */
448 printf("[%s:%d] (ipsec): error code %d\n",
449 __func__, __LINE__, error);
452 /* don't show these error codes to the user */
457 } else if (!needipsectun) {
459 * In the FAST IPSec case we have already
460 * re-injected the packet and it has been freed
461 * by the ipsec_done() function. So, just clean
462 * up after ourselves.
467 if (exthdrs.ip6e_rthdr) {
468 /* ah6_output doesn't modify mbuf chain */
469 rh->ip6r_segleft = segleft_org;
475 * If there is a routing header, discard the packet.
477 if (exthdrs.ip6e_rthdr) {
482 /* Source address validation */
483 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
484 (flags & IPV6_UNSPECSRC) == 0) {
486 V_ip6stat.ip6s_badscope++;
489 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
491 V_ip6stat.ip6s_badscope++;
495 V_ip6stat.ip6s_localout++;
502 bzero((caddr_t)ro, sizeof(*ro));
505 if (opt && opt->ip6po_rthdr)
506 ro = &opt->ip6po_route;
507 dst = (struct sockaddr_in6 *)&ro->ro_dst;
509 if (ro == &ip6route) {
513 * The flow table returns route entries valid for up to 30
514 * seconds; we rely on the remainder of ip_output() taking no
515 * longer than that long for the stability of ro_rt. The
516 * flow ID assignment must have happened before this point.
518 if ((fle = flowtable_lookup_mbuf(V_ip6_ft, m, AF_INET6)) != NULL) {
519 flow_to_route_in6(fle, ro);
520 if (ro->ro_rt != NULL && ro->ro_lle != NULL)
527 * if specified, try to fill in the traffic class field.
528 * do not override if a non-zero value is already set.
529 * we check the diffserv field and the ecn field separately.
531 if (opt && opt->ip6po_tclass >= 0) {
534 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
536 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
539 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
542 /* fill in or override the hop limit field, if necessary. */
543 if (opt && opt->ip6po_hlim != -1)
544 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
545 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
547 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
549 ip6->ip6_hlim = V_ip6_defmcasthlim;
554 * We may re-inject packets into the stack here.
556 if (needipsec && needipsectun) {
557 struct ipsec_output_state state;
560 * All the extension headers will become inaccessible
561 * (since they can be encrypted).
562 * Don't panic, we need no more updates to extension headers
563 * on inner IPv6 packet (since they are now encapsulated).
565 * IPv6 [ESP|AH] IPv6 [extension headers] payload
567 bzero(&exthdrs, sizeof(exthdrs));
568 exthdrs.ip6e_ip6 = m;
570 bzero(&state, sizeof(state));
572 state.ro = (struct route *)ro;
573 state.dst = (struct sockaddr *)dst;
575 error = ipsec6_output_tunnel(&state, sp, flags);
578 ro = (struct route_in6 *)state.ro;
579 dst = (struct sockaddr_in6 *)state.dst;
580 if (error == EJUSTRETURN) {
582 * We had a SP with a level of 'use' and no SA. We
583 * will just continue to process the packet without
588 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
599 printf("[%s:%d] (ipsec): error code %d\n",
600 __func__, __LINE__, error);
603 /* don't show these error codes to the user */
610 * In the FAST IPSec case we have already
611 * re-injected the packet and it has been freed
612 * by the ipsec_done() function. So, just clean
613 * up after ourselves.
619 exthdrs.ip6e_ip6 = m;
624 ip6 = mtod(m, struct ip6_hdr *);
626 bzero(&dst_sa, sizeof(dst_sa));
627 dst_sa.sin6_family = AF_INET6;
628 dst_sa.sin6_len = sizeof(dst_sa);
629 dst_sa.sin6_addr = ip6->ip6_dst;
632 ifp = ro->ro_rt->rt_ifp;
633 } else if ((error = in6_selectroute_fib(&dst_sa, opt, im6o, ro,
634 &ifp, &rt, inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m))) != 0) {
637 V_ip6stat.ip6s_noroute++;
641 break; /* XXX statistics? */
644 in6_ifstat_inc(ifp, ifs6_out_discard);
649 * If in6_selectroute() does not return a route entry,
650 * dst may not have been updated.
652 *dst = dst_sa; /* XXX */
656 * then rt (for unicast) and ifp must be non-NULL valid values.
658 if ((flags & IPV6_FORWARDING) == 0) {
659 /* XXX: the FORWARDING flag can be set for mrouting. */
660 in6_ifstat_inc(ifp, ifs6_out_request);
663 ia = (struct in6_ifaddr *)(rt->rt_ifa);
669 * The outgoing interface must be in the zone of source and
670 * destination addresses.
675 if (in6_setscope(&src0, origifp, &zone))
677 bzero(&src_sa, sizeof(src_sa));
678 src_sa.sin6_family = AF_INET6;
679 src_sa.sin6_len = sizeof(src_sa);
680 src_sa.sin6_addr = ip6->ip6_src;
681 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
685 if (in6_setscope(&dst0, origifp, &zone))
687 /* re-initialize to be sure */
688 bzero(&dst_sa, sizeof(dst_sa));
689 dst_sa.sin6_family = AF_INET6;
690 dst_sa.sin6_len = sizeof(dst_sa);
691 dst_sa.sin6_addr = ip6->ip6_dst;
692 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
696 /* We should use ia_ifp to support the case of
697 * sending packets to an address of our own.
699 if (ia != NULL && ia->ia_ifp)
702 /* scope check is done. */
706 V_ip6stat.ip6s_badscope++;
707 in6_ifstat_inc(origifp, ifs6_out_discard);
709 error = EHOSTUNREACH; /* XXX */
713 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
714 if (opt && opt->ip6po_nextroute.ro_rt) {
716 * The nexthop is explicitly specified by the
717 * application. We assume the next hop is an IPv6
720 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
722 else if ((rt->rt_flags & RTF_GATEWAY))
723 dst = (struct sockaddr_in6 *)rt->rt_gateway;
726 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
727 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
729 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
730 in6_ifstat_inc(ifp, ifs6_out_mcast);
732 * Confirm that the outgoing interface supports multicast.
734 if (!(ifp->if_flags & IFF_MULTICAST)) {
735 V_ip6stat.ip6s_noroute++;
736 in6_ifstat_inc(ifp, ifs6_out_discard);
740 if ((im6o == NULL && in6_mcast_loop) ||
741 (im6o && im6o->im6o_multicast_loop)) {
743 * Loop back multicast datagram if not expressly
744 * forbidden to do so, even if we have not joined
745 * the address; protocols will filter it later,
746 * thus deferring a hash lookup and lock acquisition
747 * at the expense of an m_copym().
749 ip6_mloopback(ifp, m, dst);
752 * If we are acting as a multicast router, perform
753 * multicast forwarding as if the packet had just
754 * arrived on the interface to which we are about
755 * to send. The multicast forwarding function
756 * recursively calls this function, using the
757 * IPV6_FORWARDING flag to prevent infinite recursion.
759 * Multicasts that are looped back by ip6_mloopback(),
760 * above, will be forwarded by the ip6_input() routine,
763 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
765 * XXX: ip6_mforward expects that rcvif is NULL
766 * when it is called from the originating path.
767 * However, it is not always the case, since
768 * some versions of MGETHDR() does not
769 * initialize the field.
771 m->m_pkthdr.rcvif = NULL;
772 if (ip6_mforward(ip6, ifp, m) != 0) {
779 * Multicasts with a hoplimit of zero may be looped back,
780 * above, but must not be transmitted on a network.
781 * Also, multicasts addressed to the loopback interface
782 * are not sent -- the above call to ip6_mloopback() will
783 * loop back a copy if this host actually belongs to the
784 * destination group on the loopback interface.
786 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
787 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
794 * Fill the outgoing inteface to tell the upper layer
795 * to increment per-interface statistics.
800 /* Determine path MTU. */
801 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
802 &alwaysfrag, inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m))) != 0)
806 * The caller of this function may specify to use the minimum MTU
808 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
809 * setting. The logic is a bit complicated; by default, unicast
810 * packets will follow path MTU while multicast packets will be sent at
811 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
812 * including unicast ones will be sent at the minimum MTU. Multicast
813 * packets will always be sent at the minimum MTU unless
814 * IP6PO_MINMTU_DISABLE is explicitly specified.
815 * See RFC 3542 for more details.
817 if (mtu > IPV6_MMTU) {
818 if ((flags & IPV6_MINMTU))
820 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
822 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
824 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
830 * clear embedded scope identifiers if necessary.
831 * in6_clearscope will touch the addresses only when necessary.
833 in6_clearscope(&ip6->ip6_src);
834 in6_clearscope(&ip6->ip6_dst);
837 * If the outgoing packet contains a hop-by-hop options header,
838 * it must be examined and processed even by the source node.
839 * (RFC 2460, section 4.)
841 if (exthdrs.ip6e_hbh) {
842 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
843 u_int32_t dummy; /* XXX unused */
844 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
847 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
848 panic("ip6e_hbh is not contiguous");
851 * XXX: if we have to send an ICMPv6 error to the sender,
852 * we need the M_LOOP flag since icmp6_error() expects
853 * the IPv6 and the hop-by-hop options header are
854 * contiguous unless the flag is set.
856 m->m_flags |= M_LOOP;
857 m->m_pkthdr.rcvif = ifp;
858 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
859 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
860 &dummy, &plen) < 0) {
861 /* m was already freed at this point */
862 error = EINVAL;/* better error? */
865 m->m_flags &= ~M_LOOP; /* XXX */
866 m->m_pkthdr.rcvif = NULL;
869 /* Jump over all PFIL processing if hooks are not active. */
870 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
874 /* Run through list of hooks for output packets. */
875 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
876 if (error != 0 || m == NULL)
878 ip6 = mtod(m, struct ip6_hdr *);
880 /* See if destination IP address was changed by packet filter. */
881 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
882 m->m_flags |= M_SKIP_FIREWALL;
883 /* If destination is now ourself drop to ip6_input(). */
884 if (in6_localip(&ip6->ip6_dst)) {
885 m->m_flags |= M_FASTFWD_OURS;
886 if (m->m_pkthdr.rcvif == NULL)
887 m->m_pkthdr.rcvif = V_loif;
888 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
889 m->m_pkthdr.csum_flags |=
890 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
891 m->m_pkthdr.csum_data = 0xffff;
894 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
895 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
897 error = netisr_queue(NETISR_IPV6, m);
900 goto again; /* Redo the routing table lookup. */
903 #ifdef IPFIREWALL_FORWARD
904 /* See if local, if yes, send it to netisr. */
905 if (m->m_flags & M_FASTFWD_OURS) {
906 if (m->m_pkthdr.rcvif == NULL)
907 m->m_pkthdr.rcvif = V_loif;
908 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
909 m->m_pkthdr.csum_flags |=
910 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
911 m->m_pkthdr.csum_data = 0xffff;
914 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
915 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
917 error = netisr_queue(NETISR_IPV6, m);
920 /* Or forward to some other address? */
921 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
923 dst = (struct sockaddr_in6 *)&ro->ro_dst;
924 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in6));
925 m->m_flags |= M_SKIP_FIREWALL;
926 m_tag_delete(m, fwd_tag);
929 #endif /* IPFIREWALL_FORWARD */
933 * Send the packet to the outgoing interface.
934 * If necessary, do IPv6 fragmentation before sending.
936 * the logic here is rather complex:
937 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
938 * 1-a: send as is if tlen <= path mtu
939 * 1-b: fragment if tlen > path mtu
941 * 2: if user asks us not to fragment (dontfrag == 1)
942 * 2-a: send as is if tlen <= interface mtu
943 * 2-b: error if tlen > interface mtu
945 * 3: if we always need to attach fragment header (alwaysfrag == 1)
948 * 4: if dontfrag == 1 && alwaysfrag == 1
949 * error, as we cannot handle this conflicting request
951 sw_csum = m->m_pkthdr.csum_flags;
953 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
954 sw_csum &= ~ifp->if_hwassist;
958 * If we added extension headers, we will not do TSO and calculate the
959 * checksums ourselves for now.
960 * XXX-BZ Need a framework to know when the NIC can handle it, even
963 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
964 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
965 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
968 if (sw_csum & CSUM_SCTP_IPV6) {
969 sw_csum &= ~CSUM_SCTP_IPV6;
970 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
973 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
974 tlen = m->m_pkthdr.len;
976 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
980 if (dontfrag && alwaysfrag) { /* case 4 */
981 /* conflicting request - can't transmit */
985 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
987 * Even if the DONTFRAG option is specified, we cannot send the
988 * packet when the data length is larger than the MTU of the
989 * outgoing interface.
990 * Notify the error by sending IPV6_PATHMTU ancillary data as
991 * well as returning an error code (the latter is not described
995 struct ip6ctlparam ip6cp;
997 mtu32 = (u_int32_t)mtu;
998 bzero(&ip6cp, sizeof(ip6cp));
999 ip6cp.ip6c_cmdarg = (void *)&mtu32;
1000 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
1008 * transmit packet without fragmentation
1010 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
1011 struct in6_ifaddr *ia6;
1013 ip6 = mtod(m, struct ip6_hdr *);
1014 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1016 /* Record statistics for this interface address. */
1017 ia6->ia_ifa.if_opackets++;
1018 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
1019 ifa_free(&ia6->ia_ifa);
1021 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1026 * try to fragment the packet. case 1-b and 3
1028 if (mtu < IPV6_MMTU) {
1029 /* path MTU cannot be less than IPV6_MMTU */
1031 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1033 } else if (ip6->ip6_plen == 0) {
1034 /* jumbo payload cannot be fragmented */
1036 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1039 struct mbuf **mnext, *m_frgpart;
1040 struct ip6_frag *ip6f;
1041 u_int32_t id = htonl(ip6_randomid());
1044 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
1047 * Too large for the destination or interface;
1048 * fragment if possible.
1049 * Must be able to put at least 8 bytes per fragment.
1051 hlen = unfragpartlen;
1052 if (mtu > IPV6_MAXPACKET)
1053 mtu = IPV6_MAXPACKET;
1055 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1058 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1063 * Verify that we have any chance at all of being able to queue
1064 * the packet or packet fragments
1066 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
1067 < tlen /* - hlen */)) {
1069 V_ip6stat.ip6s_odropped++;
1075 * If the interface will not calculate checksums on
1076 * fragmented packets, then do it here.
1077 * XXX-BZ handle the hw offloading case. Need flags.
1079 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1080 in6_delayed_cksum(m, plen, hlen);
1081 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1084 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1085 sctp_delayed_cksum(m, hlen);
1086 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1089 mnext = &m->m_nextpkt;
1092 * Change the next header field of the last header in the
1093 * unfragmentable part.
1095 if (exthdrs.ip6e_rthdr) {
1096 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1097 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1098 } else if (exthdrs.ip6e_dest1) {
1099 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1100 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1101 } else if (exthdrs.ip6e_hbh) {
1102 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1103 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1105 nextproto = ip6->ip6_nxt;
1106 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1110 * Loop through length of segment after first fragment,
1111 * make new header and copy data of each part and link onto
1115 for (off = hlen; off < tlen; off += len) {
1116 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1119 V_ip6stat.ip6s_odropped++;
1122 m->m_pkthdr.rcvif = NULL;
1123 m->m_flags = m0->m_flags & M_COPYFLAGS; /* incl. FIB */
1125 mnext = &m->m_nextpkt;
1126 m->m_data += max_linkhdr;
1127 mhip6 = mtod(m, struct ip6_hdr *);
1129 m->m_len = sizeof(*mhip6);
1130 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1132 V_ip6stat.ip6s_odropped++;
1135 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1136 if (off + len >= tlen)
1139 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1140 mhip6->ip6_plen = htons((u_short)(len + hlen +
1141 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1142 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1144 V_ip6stat.ip6s_odropped++;
1147 m_cat(m, m_frgpart);
1148 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1149 m->m_pkthdr.rcvif = NULL;
1150 ip6f->ip6f_reserved = 0;
1151 ip6f->ip6f_ident = id;
1152 ip6f->ip6f_nxt = nextproto;
1153 V_ip6stat.ip6s_ofragments++;
1154 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1157 in6_ifstat_inc(ifp, ifs6_out_fragok);
1161 * Remove leading garbages.
1167 for (m0 = m; m; m = m0) {
1171 /* Record statistics for this interface address. */
1173 ia->ia_ifa.if_opackets++;
1174 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1176 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1182 V_ip6stat.ip6s_fragmented++;
1185 if (ro == &ip6route && ro->ro_rt && flevalid == 0) {
1186 /* brace necessary for RTFREE */
1188 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt &&
1189 ((flevalid == 0) || (ro_pmtu != ro))) {
1190 RTFREE(ro_pmtu->ro_rt);
1200 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1201 m_freem(exthdrs.ip6e_dest1);
1202 m_freem(exthdrs.ip6e_rthdr);
1203 m_freem(exthdrs.ip6e_dest2);
1212 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1216 if (hlen > MCLBYTES)
1217 return (ENOBUFS); /* XXX */
1219 MGET(m, M_DONTWAIT, MT_DATA);
1224 MCLGET(m, M_DONTWAIT);
1225 if ((m->m_flags & M_EXT) == 0) {
1232 bcopy(hdr, mtod(m, caddr_t), hlen);
1239 * Insert jumbo payload option.
1242 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1248 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1251 * If there is no hop-by-hop options header, allocate new one.
1252 * If there is one but it doesn't have enough space to store the
1253 * jumbo payload option, allocate a cluster to store the whole options.
1254 * Otherwise, use it to store the options.
1256 if (exthdrs->ip6e_hbh == 0) {
1257 MGET(mopt, M_DONTWAIT, MT_DATA);
1260 mopt->m_len = JUMBOOPTLEN;
1261 optbuf = mtod(mopt, u_char *);
1262 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1263 exthdrs->ip6e_hbh = mopt;
1265 struct ip6_hbh *hbh;
1267 mopt = exthdrs->ip6e_hbh;
1268 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1271 * - exthdrs->ip6e_hbh is not referenced from places
1272 * other than exthdrs.
1273 * - exthdrs->ip6e_hbh is not an mbuf chain.
1275 int oldoptlen = mopt->m_len;
1279 * XXX: give up if the whole (new) hbh header does
1280 * not fit even in an mbuf cluster.
1282 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1286 * As a consequence, we must always prepare a cluster
1289 MGET(n, M_DONTWAIT, MT_DATA);
1291 MCLGET(n, M_DONTWAIT);
1292 if ((n->m_flags & M_EXT) == 0) {
1299 n->m_len = oldoptlen + JUMBOOPTLEN;
1300 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1302 optbuf = mtod(n, caddr_t) + oldoptlen;
1304 mopt = exthdrs->ip6e_hbh = n;
1306 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1307 mopt->m_len += JUMBOOPTLEN;
1309 optbuf[0] = IP6OPT_PADN;
1313 * Adjust the header length according to the pad and
1314 * the jumbo payload option.
1316 hbh = mtod(mopt, struct ip6_hbh *);
1317 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1320 /* fill in the option. */
1321 optbuf[2] = IP6OPT_JUMBO;
1323 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1324 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1326 /* finally, adjust the packet header length */
1327 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1334 * Insert fragment header and copy unfragmentable header portions.
1337 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1338 struct ip6_frag **frghdrp)
1340 struct mbuf *n, *mlast;
1342 if (hlen > sizeof(struct ip6_hdr)) {
1343 n = m_copym(m0, sizeof(struct ip6_hdr),
1344 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1351 /* Search for the last mbuf of unfragmentable part. */
1352 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1355 if ((mlast->m_flags & M_EXT) == 0 &&
1356 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1357 /* use the trailing space of the last mbuf for the fragment hdr */
1358 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1360 mlast->m_len += sizeof(struct ip6_frag);
1361 m->m_pkthdr.len += sizeof(struct ip6_frag);
1363 /* allocate a new mbuf for the fragment header */
1366 MGET(mfrg, M_DONTWAIT, MT_DATA);
1369 mfrg->m_len = sizeof(struct ip6_frag);
1370 *frghdrp = mtod(mfrg, struct ip6_frag *);
1371 mlast->m_next = mfrg;
1378 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1379 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1380 int *alwaysfragp, u_int fibnum)
1386 if (ro_pmtu != ro) {
1387 /* The first hop and the final destination may differ. */
1388 struct sockaddr_in6 *sa6_dst =
1389 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1390 if (ro_pmtu->ro_rt &&
1391 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1392 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1393 RTFREE(ro_pmtu->ro_rt);
1394 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1396 if (ro_pmtu->ro_rt == NULL) {
1397 bzero(sa6_dst, sizeof(*sa6_dst));
1398 sa6_dst->sin6_family = AF_INET6;
1399 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1400 sa6_dst->sin6_addr = *dst;
1402 in6_rtalloc(ro_pmtu, fibnum);
1405 if (ro_pmtu->ro_rt) {
1407 struct in_conninfo inc;
1409 bzero(&inc, sizeof(inc));
1410 inc.inc_flags |= INC_ISIPV6;
1411 inc.inc6_faddr = *dst;
1414 ifp = ro_pmtu->ro_rt->rt_ifp;
1415 ifmtu = IN6_LINKMTU(ifp);
1416 mtu = tcp_hc_getmtu(&inc);
1418 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu);
1420 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1423 else if (mtu < IPV6_MMTU) {
1425 * RFC2460 section 5, last paragraph:
1426 * if we record ICMPv6 too big message with
1427 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1428 * or smaller, with framgent header attached.
1429 * (fragment header is needed regardless from the
1430 * packet size, for translators to identify packets)
1434 } else if (mtu > ifmtu) {
1436 * The MTU on the route is larger than the MTU on
1437 * the interface! This shouldn't happen, unless the
1438 * MTU of the interface has been changed after the
1439 * interface was brought up. Change the MTU in the
1440 * route to match the interface MTU (as long as the
1441 * field isn't locked).
1444 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1447 mtu = IN6_LINKMTU(ifp);
1449 error = EHOSTUNREACH; /* XXX */
1453 *alwaysfragp = alwaysfrag;
1458 * IP6 socket option processing.
1461 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1463 int optdatalen, uproto;
1465 struct inpcb *in6p = sotoinpcb(so);
1467 int level, op, optname;
1471 level = sopt->sopt_level;
1472 op = sopt->sopt_dir;
1473 optname = sopt->sopt_name;
1474 optlen = sopt->sopt_valsize;
1478 uproto = (int)so->so_proto->pr_protocol;
1480 if (level != IPPROTO_IPV6) {
1483 if (sopt->sopt_level == SOL_SOCKET &&
1484 sopt->sopt_dir == SOPT_SET) {
1485 switch (sopt->sopt_name) {
1488 if (IN_MULTICAST(ntohl(in6p->inp_laddr.s_addr))) {
1489 if ((so->so_options &
1490 (SO_REUSEADDR | SO_REUSEPORT)) != 0)
1491 in6p->inp_flags2 |= INP_REUSEPORT;
1493 in6p->inp_flags2 &= ~INP_REUSEPORT;
1500 if ((so->so_options & SO_REUSEPORT) != 0)
1501 in6p->inp_flags2 |= INP_REUSEPORT;
1503 in6p->inp_flags2 &= ~INP_REUSEPORT;
1509 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1517 } else { /* level == IPPROTO_IPV6 */
1522 case IPV6_2292PKTOPTIONS:
1523 #ifdef IPV6_PKTOPTIONS
1524 case IPV6_PKTOPTIONS:
1529 error = soopt_getm(sopt, &m); /* XXX */
1532 error = soopt_mcopyin(sopt, m); /* XXX */
1535 error = ip6_pcbopts(&in6p->in6p_outputopts,
1537 m_freem(m); /* XXX */
1542 * Use of some Hop-by-Hop options or some
1543 * Destination options, might require special
1544 * privilege. That is, normal applications
1545 * (without special privilege) might be forbidden
1546 * from setting certain options in outgoing packets,
1547 * and might never see certain options in received
1548 * packets. [RFC 2292 Section 6]
1549 * KAME specific note:
1550 * KAME prevents non-privileged users from sending or
1551 * receiving ANY hbh/dst options in order to avoid
1552 * overhead of parsing options in the kernel.
1554 case IPV6_RECVHOPOPTS:
1555 case IPV6_RECVDSTOPTS:
1556 case IPV6_RECVRTHDRDSTOPTS:
1558 error = priv_check(td,
1559 PRIV_NETINET_SETHDROPTS);
1564 case IPV6_UNICAST_HOPS:
1568 case IPV6_RECVPKTINFO:
1569 case IPV6_RECVHOPLIMIT:
1570 case IPV6_RECVRTHDR:
1571 case IPV6_RECVPATHMTU:
1572 case IPV6_RECVTCLASS:
1574 case IPV6_AUTOFLOWLABEL:
1576 if (optname == IPV6_BINDANY && td != NULL) {
1577 error = priv_check(td,
1578 PRIV_NETINET_BINDANY);
1583 if (optlen != sizeof(int)) {
1587 error = sooptcopyin(sopt, &optval,
1588 sizeof optval, sizeof optval);
1593 case IPV6_UNICAST_HOPS:
1594 if (optval < -1 || optval >= 256)
1597 /* -1 = kernel default */
1598 in6p->in6p_hops = optval;
1599 if ((in6p->inp_vflag &
1601 in6p->inp_ip_ttl = optval;
1604 #define OPTSET(bit) \
1607 in6p->inp_flags |= (bit); \
1609 in6p->inp_flags &= ~(bit); \
1610 } while (/*CONSTCOND*/ 0)
1611 #define OPTSET2292(bit) \
1613 in6p->inp_flags |= IN6P_RFC2292; \
1615 in6p->inp_flags |= (bit); \
1617 in6p->inp_flags &= ~(bit); \
1618 } while (/*CONSTCOND*/ 0)
1619 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1621 case IPV6_RECVPKTINFO:
1622 /* cannot mix with RFC2292 */
1623 if (OPTBIT(IN6P_RFC2292)) {
1627 OPTSET(IN6P_PKTINFO);
1632 struct ip6_pktopts **optp;
1634 /* cannot mix with RFC2292 */
1635 if (OPTBIT(IN6P_RFC2292)) {
1639 optp = &in6p->in6p_outputopts;
1640 error = ip6_pcbopt(IPV6_HOPLIMIT,
1641 (u_char *)&optval, sizeof(optval),
1642 optp, (td != NULL) ? td->td_ucred :
1647 case IPV6_RECVHOPLIMIT:
1648 /* cannot mix with RFC2292 */
1649 if (OPTBIT(IN6P_RFC2292)) {
1653 OPTSET(IN6P_HOPLIMIT);
1656 case IPV6_RECVHOPOPTS:
1657 /* cannot mix with RFC2292 */
1658 if (OPTBIT(IN6P_RFC2292)) {
1662 OPTSET(IN6P_HOPOPTS);
1665 case IPV6_RECVDSTOPTS:
1666 /* cannot mix with RFC2292 */
1667 if (OPTBIT(IN6P_RFC2292)) {
1671 OPTSET(IN6P_DSTOPTS);
1674 case IPV6_RECVRTHDRDSTOPTS:
1675 /* cannot mix with RFC2292 */
1676 if (OPTBIT(IN6P_RFC2292)) {
1680 OPTSET(IN6P_RTHDRDSTOPTS);
1683 case IPV6_RECVRTHDR:
1684 /* cannot mix with RFC2292 */
1685 if (OPTBIT(IN6P_RFC2292)) {
1696 case IPV6_RECVPATHMTU:
1698 * We ignore this option for TCP
1700 * (RFC3542 leaves this case
1703 if (uproto != IPPROTO_TCP)
1709 * make setsockopt(IPV6_V6ONLY)
1710 * available only prior to bind(2).
1711 * see ipng mailing list, Jun 22 2001.
1713 if (in6p->inp_lport ||
1714 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1718 OPTSET(IN6P_IPV6_V6ONLY);
1720 in6p->inp_vflag &= ~INP_IPV4;
1722 in6p->inp_vflag |= INP_IPV4;
1724 case IPV6_RECVTCLASS:
1725 /* cannot mix with RFC2292 XXX */
1726 if (OPTBIT(IN6P_RFC2292)) {
1730 OPTSET(IN6P_TCLASS);
1732 case IPV6_AUTOFLOWLABEL:
1733 OPTSET(IN6P_AUTOFLOWLABEL);
1737 OPTSET(INP_BINDANY);
1744 case IPV6_USE_MIN_MTU:
1745 case IPV6_PREFER_TEMPADDR:
1746 if (optlen != sizeof(optval)) {
1750 error = sooptcopyin(sopt, &optval,
1751 sizeof optval, sizeof optval);
1755 struct ip6_pktopts **optp;
1756 optp = &in6p->in6p_outputopts;
1757 error = ip6_pcbopt(optname,
1758 (u_char *)&optval, sizeof(optval),
1759 optp, (td != NULL) ? td->td_ucred :
1764 case IPV6_2292PKTINFO:
1765 case IPV6_2292HOPLIMIT:
1766 case IPV6_2292HOPOPTS:
1767 case IPV6_2292DSTOPTS:
1768 case IPV6_2292RTHDR:
1770 if (optlen != sizeof(int)) {
1774 error = sooptcopyin(sopt, &optval,
1775 sizeof optval, sizeof optval);
1779 case IPV6_2292PKTINFO:
1780 OPTSET2292(IN6P_PKTINFO);
1782 case IPV6_2292HOPLIMIT:
1783 OPTSET2292(IN6P_HOPLIMIT);
1785 case IPV6_2292HOPOPTS:
1787 * Check super-user privilege.
1788 * See comments for IPV6_RECVHOPOPTS.
1791 error = priv_check(td,
1792 PRIV_NETINET_SETHDROPTS);
1796 OPTSET2292(IN6P_HOPOPTS);
1798 case IPV6_2292DSTOPTS:
1800 error = priv_check(td,
1801 PRIV_NETINET_SETHDROPTS);
1805 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1807 case IPV6_2292RTHDR:
1808 OPTSET2292(IN6P_RTHDR);
1816 case IPV6_RTHDRDSTOPTS:
1819 /* new advanced API (RFC3542) */
1821 u_char optbuf_storage[MCLBYTES];
1823 struct ip6_pktopts **optp;
1825 /* cannot mix with RFC2292 */
1826 if (OPTBIT(IN6P_RFC2292)) {
1832 * We only ensure valsize is not too large
1833 * here. Further validation will be done
1836 error = sooptcopyin(sopt, optbuf_storage,
1837 sizeof(optbuf_storage), 0);
1840 optlen = sopt->sopt_valsize;
1841 optbuf = optbuf_storage;
1842 optp = &in6p->in6p_outputopts;
1843 error = ip6_pcbopt(optname, optbuf, optlen,
1844 optp, (td != NULL) ? td->td_ucred : NULL,
1850 case IPV6_MULTICAST_IF:
1851 case IPV6_MULTICAST_HOPS:
1852 case IPV6_MULTICAST_LOOP:
1853 case IPV6_JOIN_GROUP:
1854 case IPV6_LEAVE_GROUP:
1856 case MCAST_BLOCK_SOURCE:
1857 case MCAST_UNBLOCK_SOURCE:
1858 case MCAST_JOIN_GROUP:
1859 case MCAST_LEAVE_GROUP:
1860 case MCAST_JOIN_SOURCE_GROUP:
1861 case MCAST_LEAVE_SOURCE_GROUP:
1862 error = ip6_setmoptions(in6p, sopt);
1865 case IPV6_PORTRANGE:
1866 error = sooptcopyin(sopt, &optval,
1867 sizeof optval, sizeof optval);
1872 case IPV6_PORTRANGE_DEFAULT:
1873 in6p->inp_flags &= ~(INP_LOWPORT);
1874 in6p->inp_flags &= ~(INP_HIGHPORT);
1877 case IPV6_PORTRANGE_HIGH:
1878 in6p->inp_flags &= ~(INP_LOWPORT);
1879 in6p->inp_flags |= INP_HIGHPORT;
1882 case IPV6_PORTRANGE_LOW:
1883 in6p->inp_flags &= ~(INP_HIGHPORT);
1884 in6p->inp_flags |= INP_LOWPORT;
1894 case IPV6_IPSEC_POLICY:
1899 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1901 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1903 req = mtod(m, caddr_t);
1904 error = ipsec_set_policy(in6p, optname, req,
1905 m->m_len, (sopt->sopt_td != NULL) ?
1906 sopt->sopt_td->td_ucred : NULL);
1913 error = ENOPROTOOPT;
1921 case IPV6_2292PKTOPTIONS:
1922 #ifdef IPV6_PKTOPTIONS
1923 case IPV6_PKTOPTIONS:
1926 * RFC3542 (effectively) deprecated the
1927 * semantics of the 2292-style pktoptions.
1928 * Since it was not reliable in nature (i.e.,
1929 * applications had to expect the lack of some
1930 * information after all), it would make sense
1931 * to simplify this part by always returning
1934 sopt->sopt_valsize = 0;
1937 case IPV6_RECVHOPOPTS:
1938 case IPV6_RECVDSTOPTS:
1939 case IPV6_RECVRTHDRDSTOPTS:
1940 case IPV6_UNICAST_HOPS:
1941 case IPV6_RECVPKTINFO:
1942 case IPV6_RECVHOPLIMIT:
1943 case IPV6_RECVRTHDR:
1944 case IPV6_RECVPATHMTU:
1948 case IPV6_PORTRANGE:
1949 case IPV6_RECVTCLASS:
1950 case IPV6_AUTOFLOWLABEL:
1954 case IPV6_RECVHOPOPTS:
1955 optval = OPTBIT(IN6P_HOPOPTS);
1958 case IPV6_RECVDSTOPTS:
1959 optval = OPTBIT(IN6P_DSTOPTS);
1962 case IPV6_RECVRTHDRDSTOPTS:
1963 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1966 case IPV6_UNICAST_HOPS:
1967 optval = in6p->in6p_hops;
1970 case IPV6_RECVPKTINFO:
1971 optval = OPTBIT(IN6P_PKTINFO);
1974 case IPV6_RECVHOPLIMIT:
1975 optval = OPTBIT(IN6P_HOPLIMIT);
1978 case IPV6_RECVRTHDR:
1979 optval = OPTBIT(IN6P_RTHDR);
1982 case IPV6_RECVPATHMTU:
1983 optval = OPTBIT(IN6P_MTU);
1987 optval = OPTBIT(INP_FAITH);
1991 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1994 case IPV6_PORTRANGE:
1997 flags = in6p->inp_flags;
1998 if (flags & INP_HIGHPORT)
1999 optval = IPV6_PORTRANGE_HIGH;
2000 else if (flags & INP_LOWPORT)
2001 optval = IPV6_PORTRANGE_LOW;
2006 case IPV6_RECVTCLASS:
2007 optval = OPTBIT(IN6P_TCLASS);
2010 case IPV6_AUTOFLOWLABEL:
2011 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2015 optval = OPTBIT(INP_BINDANY);
2020 error = sooptcopyout(sopt, &optval,
2027 struct ip6_mtuinfo mtuinfo;
2028 struct route_in6 sro;
2030 bzero(&sro, sizeof(sro));
2032 if (!(so->so_state & SS_ISCONNECTED))
2035 * XXX: we dot not consider the case of source
2036 * routing, or optional information to specify
2037 * the outgoing interface.
2039 error = ip6_getpmtu(&sro, NULL, NULL,
2040 &in6p->in6p_faddr, &pmtu, NULL,
2046 if (pmtu > IPV6_MAXPACKET)
2047 pmtu = IPV6_MAXPACKET;
2049 bzero(&mtuinfo, sizeof(mtuinfo));
2050 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2051 optdata = (void *)&mtuinfo;
2052 optdatalen = sizeof(mtuinfo);
2053 error = sooptcopyout(sopt, optdata,
2058 case IPV6_2292PKTINFO:
2059 case IPV6_2292HOPLIMIT:
2060 case IPV6_2292HOPOPTS:
2061 case IPV6_2292RTHDR:
2062 case IPV6_2292DSTOPTS:
2064 case IPV6_2292PKTINFO:
2065 optval = OPTBIT(IN6P_PKTINFO);
2067 case IPV6_2292HOPLIMIT:
2068 optval = OPTBIT(IN6P_HOPLIMIT);
2070 case IPV6_2292HOPOPTS:
2071 optval = OPTBIT(IN6P_HOPOPTS);
2073 case IPV6_2292RTHDR:
2074 optval = OPTBIT(IN6P_RTHDR);
2076 case IPV6_2292DSTOPTS:
2077 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2080 error = sooptcopyout(sopt, &optval,
2087 case IPV6_RTHDRDSTOPTS:
2091 case IPV6_USE_MIN_MTU:
2092 case IPV6_PREFER_TEMPADDR:
2093 error = ip6_getpcbopt(in6p->in6p_outputopts,
2097 case IPV6_MULTICAST_IF:
2098 case IPV6_MULTICAST_HOPS:
2099 case IPV6_MULTICAST_LOOP:
2101 error = ip6_getmoptions(in6p, sopt);
2105 case IPV6_IPSEC_POLICY:
2109 struct mbuf *m = NULL;
2110 struct mbuf **mp = &m;
2111 size_t ovalsize = sopt->sopt_valsize;
2112 caddr_t oval = (caddr_t)sopt->sopt_val;
2114 error = soopt_getm(sopt, &m); /* XXX */
2117 error = soopt_mcopyin(sopt, m); /* XXX */
2120 sopt->sopt_valsize = ovalsize;
2121 sopt->sopt_val = oval;
2123 req = mtod(m, caddr_t);
2126 error = ipsec_get_policy(in6p, req, len, mp);
2128 error = soopt_mcopyout(sopt, m); /* XXX */
2129 if (error == 0 && m)
2136 error = ENOPROTOOPT;
2146 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2148 int error = 0, optval, optlen;
2149 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2150 struct inpcb *in6p = sotoinpcb(so);
2151 int level, op, optname;
2153 level = sopt->sopt_level;
2154 op = sopt->sopt_dir;
2155 optname = sopt->sopt_name;
2156 optlen = sopt->sopt_valsize;
2158 if (level != IPPROTO_IPV6) {
2165 * For ICMPv6 sockets, no modification allowed for checksum
2166 * offset, permit "no change" values to help existing apps.
2168 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2169 * for an ICMPv6 socket will fail."
2170 * The current behavior does not meet RFC3542.
2174 if (optlen != sizeof(int)) {
2178 error = sooptcopyin(sopt, &optval, sizeof(optval),
2182 if ((optval % 2) != 0) {
2183 /* the API assumes even offset values */
2185 } else if (so->so_proto->pr_protocol ==
2187 if (optval != icmp6off)
2190 in6p->in6p_cksum = optval;
2194 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2197 optval = in6p->in6p_cksum;
2199 error = sooptcopyout(sopt, &optval, sizeof(optval));
2209 error = ENOPROTOOPT;
2217 * Set up IP6 options in pcb for insertion in output packets or
2218 * specifying behavior of outgoing packets.
2221 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2222 struct socket *so, struct sockopt *sopt)
2224 struct ip6_pktopts *opt = *pktopt;
2226 struct thread *td = sopt->sopt_td;
2228 /* turn off any old options. */
2231 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2232 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2233 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2234 printf("ip6_pcbopts: all specified options are cleared.\n");
2236 ip6_clearpktopts(opt, -1);
2238 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2241 if (!m || m->m_len == 0) {
2243 * Only turning off any previous options, regardless of
2244 * whether the opt is just created or given.
2246 free(opt, M_IP6OPT);
2250 /* set options specified by user. */
2251 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2252 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2253 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2254 free(opt, M_IP6OPT);
2262 * initialize ip6_pktopts. beware that there are non-zero default values in
2266 ip6_initpktopts(struct ip6_pktopts *opt)
2269 bzero(opt, sizeof(*opt));
2270 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2271 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2272 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2273 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2277 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2278 struct ucred *cred, int uproto)
2280 struct ip6_pktopts *opt;
2282 if (*pktopt == NULL) {
2283 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2285 ip6_initpktopts(*pktopt);
2289 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2293 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2295 void *optdata = NULL;
2297 struct ip6_ext *ip6e;
2299 struct in6_pktinfo null_pktinfo;
2300 int deftclass = 0, on;
2301 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2302 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2306 if (pktopt && pktopt->ip6po_pktinfo)
2307 optdata = (void *)pktopt->ip6po_pktinfo;
2309 /* XXX: we don't have to do this every time... */
2310 bzero(&null_pktinfo, sizeof(null_pktinfo));
2311 optdata = (void *)&null_pktinfo;
2313 optdatalen = sizeof(struct in6_pktinfo);
2316 if (pktopt && pktopt->ip6po_tclass >= 0)
2317 optdata = (void *)&pktopt->ip6po_tclass;
2319 optdata = (void *)&deftclass;
2320 optdatalen = sizeof(int);
2323 if (pktopt && pktopt->ip6po_hbh) {
2324 optdata = (void *)pktopt->ip6po_hbh;
2325 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2326 optdatalen = (ip6e->ip6e_len + 1) << 3;
2330 if (pktopt && pktopt->ip6po_rthdr) {
2331 optdata = (void *)pktopt->ip6po_rthdr;
2332 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2333 optdatalen = (ip6e->ip6e_len + 1) << 3;
2336 case IPV6_RTHDRDSTOPTS:
2337 if (pktopt && pktopt->ip6po_dest1) {
2338 optdata = (void *)pktopt->ip6po_dest1;
2339 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2340 optdatalen = (ip6e->ip6e_len + 1) << 3;
2344 if (pktopt && pktopt->ip6po_dest2) {
2345 optdata = (void *)pktopt->ip6po_dest2;
2346 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2347 optdatalen = (ip6e->ip6e_len + 1) << 3;
2351 if (pktopt && pktopt->ip6po_nexthop) {
2352 optdata = (void *)pktopt->ip6po_nexthop;
2353 optdatalen = pktopt->ip6po_nexthop->sa_len;
2356 case IPV6_USE_MIN_MTU:
2358 optdata = (void *)&pktopt->ip6po_minmtu;
2360 optdata = (void *)&defminmtu;
2361 optdatalen = sizeof(int);
2364 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2368 optdata = (void *)&on;
2369 optdatalen = sizeof(on);
2371 case IPV6_PREFER_TEMPADDR:
2373 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2375 optdata = (void *)&defpreftemp;
2376 optdatalen = sizeof(int);
2378 default: /* should not happen */
2380 panic("ip6_getpcbopt: unexpected option\n");
2382 return (ENOPROTOOPT);
2385 error = sooptcopyout(sopt, optdata, optdatalen);
2391 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2396 if (optname == -1 || optname == IPV6_PKTINFO) {
2397 if (pktopt->ip6po_pktinfo)
2398 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2399 pktopt->ip6po_pktinfo = NULL;
2401 if (optname == -1 || optname == IPV6_HOPLIMIT)
2402 pktopt->ip6po_hlim = -1;
2403 if (optname == -1 || optname == IPV6_TCLASS)
2404 pktopt->ip6po_tclass = -1;
2405 if (optname == -1 || optname == IPV6_NEXTHOP) {
2406 if (pktopt->ip6po_nextroute.ro_rt) {
2407 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2408 pktopt->ip6po_nextroute.ro_rt = NULL;
2410 if (pktopt->ip6po_nexthop)
2411 free(pktopt->ip6po_nexthop, M_IP6OPT);
2412 pktopt->ip6po_nexthop = NULL;
2414 if (optname == -1 || optname == IPV6_HOPOPTS) {
2415 if (pktopt->ip6po_hbh)
2416 free(pktopt->ip6po_hbh, M_IP6OPT);
2417 pktopt->ip6po_hbh = NULL;
2419 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2420 if (pktopt->ip6po_dest1)
2421 free(pktopt->ip6po_dest1, M_IP6OPT);
2422 pktopt->ip6po_dest1 = NULL;
2424 if (optname == -1 || optname == IPV6_RTHDR) {
2425 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2426 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2427 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2428 if (pktopt->ip6po_route.ro_rt) {
2429 RTFREE(pktopt->ip6po_route.ro_rt);
2430 pktopt->ip6po_route.ro_rt = NULL;
2433 if (optname == -1 || optname == IPV6_DSTOPTS) {
2434 if (pktopt->ip6po_dest2)
2435 free(pktopt->ip6po_dest2, M_IP6OPT);
2436 pktopt->ip6po_dest2 = NULL;
2440 #define PKTOPT_EXTHDRCPY(type) \
2443 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2444 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2445 if (dst->type == NULL && canwait == M_NOWAIT)\
2447 bcopy(src->type, dst->type, hlen);\
2449 } while (/*CONSTCOND*/ 0)
2452 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2454 if (dst == NULL || src == NULL) {
2455 printf("ip6_clearpktopts: invalid argument\n");
2459 dst->ip6po_hlim = src->ip6po_hlim;
2460 dst->ip6po_tclass = src->ip6po_tclass;
2461 dst->ip6po_flags = src->ip6po_flags;
2462 dst->ip6po_minmtu = src->ip6po_minmtu;
2463 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2464 if (src->ip6po_pktinfo) {
2465 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2467 if (dst->ip6po_pktinfo == NULL)
2469 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2471 if (src->ip6po_nexthop) {
2472 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2474 if (dst->ip6po_nexthop == NULL)
2476 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2477 src->ip6po_nexthop->sa_len);
2479 PKTOPT_EXTHDRCPY(ip6po_hbh);
2480 PKTOPT_EXTHDRCPY(ip6po_dest1);
2481 PKTOPT_EXTHDRCPY(ip6po_dest2);
2482 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2486 ip6_clearpktopts(dst, -1);
2489 #undef PKTOPT_EXTHDRCPY
2491 struct ip6_pktopts *
2492 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2495 struct ip6_pktopts *dst;
2497 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2500 ip6_initpktopts(dst);
2502 if ((error = copypktopts(dst, src, canwait)) != 0) {
2503 free(dst, M_IP6OPT);
2511 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2516 ip6_clearpktopts(pktopt, -1);
2518 free(pktopt, M_IP6OPT);
2522 * Set IPv6 outgoing packet options based on advanced API.
2525 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2526 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2528 struct cmsghdr *cm = 0;
2530 if (control == NULL || opt == NULL)
2533 ip6_initpktopts(opt);
2538 * If stickyopt is provided, make a local copy of the options
2539 * for this particular packet, then override them by ancillary
2541 * XXX: copypktopts() does not copy the cached route to a next
2542 * hop (if any). This is not very good in terms of efficiency,
2543 * but we can allow this since this option should be rarely
2546 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2551 * XXX: Currently, we assume all the optional information is stored
2554 if (control->m_next)
2557 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2558 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2561 if (control->m_len < CMSG_LEN(0))
2564 cm = mtod(control, struct cmsghdr *);
2565 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2567 if (cm->cmsg_level != IPPROTO_IPV6)
2570 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2571 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2580 * Set a particular packet option, as a sticky option or an ancillary data
2581 * item. "len" can be 0 only when it's a sticky option.
2582 * We have 4 cases of combination of "sticky" and "cmsg":
2583 * "sticky=0, cmsg=0": impossible
2584 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2585 * "sticky=1, cmsg=0": RFC3542 socket option
2586 * "sticky=1, cmsg=1": RFC2292 socket option
2589 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2590 struct ucred *cred, int sticky, int cmsg, int uproto)
2592 int minmtupolicy, preftemp;
2595 if (!sticky && !cmsg) {
2597 printf("ip6_setpktopt: impossible case\n");
2603 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2604 * not be specified in the context of RFC3542. Conversely,
2605 * RFC3542 types should not be specified in the context of RFC2292.
2609 case IPV6_2292PKTINFO:
2610 case IPV6_2292HOPLIMIT:
2611 case IPV6_2292NEXTHOP:
2612 case IPV6_2292HOPOPTS:
2613 case IPV6_2292DSTOPTS:
2614 case IPV6_2292RTHDR:
2615 case IPV6_2292PKTOPTIONS:
2616 return (ENOPROTOOPT);
2619 if (sticky && cmsg) {
2626 case IPV6_RTHDRDSTOPTS:
2628 case IPV6_USE_MIN_MTU:
2631 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2632 return (ENOPROTOOPT);
2637 case IPV6_2292PKTINFO:
2640 struct ifnet *ifp = NULL;
2641 struct in6_pktinfo *pktinfo;
2643 if (len != sizeof(struct in6_pktinfo))
2646 pktinfo = (struct in6_pktinfo *)buf;
2649 * An application can clear any sticky IPV6_PKTINFO option by
2650 * doing a "regular" setsockopt with ipi6_addr being
2651 * in6addr_any and ipi6_ifindex being zero.
2652 * [RFC 3542, Section 6]
2654 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2655 pktinfo->ipi6_ifindex == 0 &&
2656 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2657 ip6_clearpktopts(opt, optname);
2661 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2662 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2666 /* validate the interface index if specified. */
2667 if (pktinfo->ipi6_ifindex > V_if_index ||
2668 pktinfo->ipi6_ifindex < 0) {
2671 if (pktinfo->ipi6_ifindex) {
2672 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2678 * We store the address anyway, and let in6_selectsrc()
2679 * validate the specified address. This is because ipi6_addr
2680 * may not have enough information about its scope zone, and
2681 * we may need additional information (such as outgoing
2682 * interface or the scope zone of a destination address) to
2683 * disambiguate the scope.
2684 * XXX: the delay of the validation may confuse the
2685 * application when it is used as a sticky option.
2687 if (opt->ip6po_pktinfo == NULL) {
2688 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2689 M_IP6OPT, M_NOWAIT);
2690 if (opt->ip6po_pktinfo == NULL)
2693 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2697 case IPV6_2292HOPLIMIT:
2703 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2704 * to simplify the ordering among hoplimit options.
2706 if (optname == IPV6_HOPLIMIT && sticky)
2707 return (ENOPROTOOPT);
2709 if (len != sizeof(int))
2712 if (*hlimp < -1 || *hlimp > 255)
2715 opt->ip6po_hlim = *hlimp;
2723 if (len != sizeof(int))
2725 tclass = *(int *)buf;
2726 if (tclass < -1 || tclass > 255)
2729 opt->ip6po_tclass = tclass;
2733 case IPV6_2292NEXTHOP:
2736 error = priv_check_cred(cred,
2737 PRIV_NETINET_SETHDROPTS, 0);
2742 if (len == 0) { /* just remove the option */
2743 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2747 /* check if cmsg_len is large enough for sa_len */
2748 if (len < sizeof(struct sockaddr) || len < *buf)
2751 switch (((struct sockaddr *)buf)->sa_family) {
2754 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2757 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2760 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2761 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2764 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2770 case AF_LINK: /* should eventually be supported */
2772 return (EAFNOSUPPORT);
2775 /* turn off the previous option, then set the new option. */
2776 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2777 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2778 if (opt->ip6po_nexthop == NULL)
2780 bcopy(buf, opt->ip6po_nexthop, *buf);
2783 case IPV6_2292HOPOPTS:
2786 struct ip6_hbh *hbh;
2790 * XXX: We don't allow a non-privileged user to set ANY HbH
2791 * options, since per-option restriction has too much
2795 error = priv_check_cred(cred,
2796 PRIV_NETINET_SETHDROPTS, 0);
2802 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2803 break; /* just remove the option */
2806 /* message length validation */
2807 if (len < sizeof(struct ip6_hbh))
2809 hbh = (struct ip6_hbh *)buf;
2810 hbhlen = (hbh->ip6h_len + 1) << 3;
2814 /* turn off the previous option, then set the new option. */
2815 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2816 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2817 if (opt->ip6po_hbh == NULL)
2819 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2824 case IPV6_2292DSTOPTS:
2826 case IPV6_RTHDRDSTOPTS:
2828 struct ip6_dest *dest, **newdest = NULL;
2831 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2832 error = priv_check_cred(cred,
2833 PRIV_NETINET_SETHDROPTS, 0);
2839 ip6_clearpktopts(opt, optname);
2840 break; /* just remove the option */
2843 /* message length validation */
2844 if (len < sizeof(struct ip6_dest))
2846 dest = (struct ip6_dest *)buf;
2847 destlen = (dest->ip6d_len + 1) << 3;
2852 * Determine the position that the destination options header
2853 * should be inserted; before or after the routing header.
2856 case IPV6_2292DSTOPTS:
2858 * The old advacned API is ambiguous on this point.
2859 * Our approach is to determine the position based
2860 * according to the existence of a routing header.
2861 * Note, however, that this depends on the order of the
2862 * extension headers in the ancillary data; the 1st
2863 * part of the destination options header must appear
2864 * before the routing header in the ancillary data,
2866 * RFC3542 solved the ambiguity by introducing
2867 * separate ancillary data or option types.
2869 if (opt->ip6po_rthdr == NULL)
2870 newdest = &opt->ip6po_dest1;
2872 newdest = &opt->ip6po_dest2;
2874 case IPV6_RTHDRDSTOPTS:
2875 newdest = &opt->ip6po_dest1;
2878 newdest = &opt->ip6po_dest2;
2882 /* turn off the previous option, then set the new option. */
2883 ip6_clearpktopts(opt, optname);
2884 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2885 if (*newdest == NULL)
2887 bcopy(dest, *newdest, destlen);
2892 case IPV6_2292RTHDR:
2895 struct ip6_rthdr *rth;
2899 ip6_clearpktopts(opt, IPV6_RTHDR);
2900 break; /* just remove the option */
2903 /* message length validation */
2904 if (len < sizeof(struct ip6_rthdr))
2906 rth = (struct ip6_rthdr *)buf;
2907 rthlen = (rth->ip6r_len + 1) << 3;
2911 switch (rth->ip6r_type) {
2912 case IPV6_RTHDR_TYPE_0:
2913 if (rth->ip6r_len == 0) /* must contain one addr */
2915 if (rth->ip6r_len % 2) /* length must be even */
2917 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2921 return (EINVAL); /* not supported */
2924 /* turn off the previous option */
2925 ip6_clearpktopts(opt, IPV6_RTHDR);
2926 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2927 if (opt->ip6po_rthdr == NULL)
2929 bcopy(rth, opt->ip6po_rthdr, rthlen);
2934 case IPV6_USE_MIN_MTU:
2935 if (len != sizeof(int))
2937 minmtupolicy = *(int *)buf;
2938 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2939 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2940 minmtupolicy != IP6PO_MINMTU_ALL) {
2943 opt->ip6po_minmtu = minmtupolicy;
2947 if (len != sizeof(int))
2950 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2952 * we ignore this option for TCP sockets.
2953 * (RFC3542 leaves this case unspecified.)
2955 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2957 opt->ip6po_flags |= IP6PO_DONTFRAG;
2960 case IPV6_PREFER_TEMPADDR:
2961 if (len != sizeof(int))
2963 preftemp = *(int *)buf;
2964 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2965 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2966 preftemp != IP6PO_TEMPADDR_PREFER) {
2969 opt->ip6po_prefer_tempaddr = preftemp;
2973 return (ENOPROTOOPT);
2974 } /* end of switch */
2980 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2981 * packet to the input queue of a specified interface. Note that this
2982 * calls the output routine of the loopback "driver", but with an interface
2983 * pointer that might NOT be &loif -- easier than replicating that code here.
2986 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
2989 struct ip6_hdr *ip6;
2991 copym = m_copy(m, 0, M_COPYALL);
2996 * Make sure to deep-copy IPv6 header portion in case the data
2997 * is in an mbuf cluster, so that we can safely override the IPv6
2998 * header portion later.
3000 if ((copym->m_flags & M_EXT) != 0 ||
3001 copym->m_len < sizeof(struct ip6_hdr)) {
3002 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3008 if (copym->m_len < sizeof(*ip6)) {
3014 ip6 = mtod(copym, struct ip6_hdr *);
3016 * clear embedded scope identifiers if necessary.
3017 * in6_clearscope will touch the addresses only when necessary.
3019 in6_clearscope(&ip6->ip6_src);
3020 in6_clearscope(&ip6->ip6_dst);
3022 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
3026 * Chop IPv6 header off from the payload.
3029 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3032 struct ip6_hdr *ip6;
3034 ip6 = mtod(m, struct ip6_hdr *);
3035 if (m->m_len > sizeof(*ip6)) {
3036 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3041 M_MOVE_PKTHDR(mh, m);
3042 MH_ALIGN(mh, sizeof(*ip6));
3043 m->m_len -= sizeof(*ip6);
3044 m->m_data += sizeof(*ip6);
3047 m->m_len = sizeof(*ip6);
3048 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3050 exthdrs->ip6e_ip6 = m;
3055 * Compute IPv6 extension header length.
3058 ip6_optlen(struct inpcb *in6p)
3062 if (!in6p->in6p_outputopts)
3067 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3069 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3070 if (in6p->in6p_outputopts->ip6po_rthdr)
3071 /* dest1 is valid with rthdr only */
3072 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3073 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3074 len += elen(in6p->in6p_outputopts->ip6po_dest2);