2 /* $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1982, 1986, 1988, 1990, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
64 #include "opt_ip6fw.h"
66 #include "opt_inet6.h"
67 #include "opt_ipsec.h"
69 #include <sys/param.h>
70 #include <sys/malloc.h>
73 #include <sys/errno.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
81 #include <net/netisr.h>
82 #include <net/route.h>
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet6/in6_var.h>
88 #include <netinet/ip6.h>
89 #include <netinet/icmp6.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet/in_pcb.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet6/nd6.h>
96 #include <netinet6/ipsec.h>
98 #include <netinet6/ipsec6.h>
100 #include <netkey/key.h>
104 #include <netipsec/ipsec.h>
105 #include <netipsec/ipsec6.h>
106 #include <netipsec/key.h>
107 #endif /* FAST_IPSEC */
109 #include <netinet6/ip6_fw.h>
111 #include <net/net_osdep.h>
113 #include <netinet6/ip6protosw.h>
114 #include <netinet6/scope6_var.h>
116 static MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options");
119 struct mbuf *ip6e_ip6;
120 struct mbuf *ip6e_hbh;
121 struct mbuf *ip6e_dest1;
122 struct mbuf *ip6e_rthdr;
123 struct mbuf *ip6e_dest2;
126 static int ip6_pcbopt __P((int, u_char *, int, struct ip6_pktopts **,
128 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
129 struct socket *, struct sockopt *));
130 static int ip6_getpcbopt __P((struct ip6_pktopts *, int, struct sockopt *));
131 static int ip6_setpktopt __P((int, u_char *, int, struct ip6_pktopts *, int,
134 static int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *));
135 static int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **));
136 static int ip6_copyexthdr __P((struct mbuf **, caddr_t, int));
137 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
138 struct ip6_frag **));
139 static int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t));
140 static int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *));
141 static int ip6_getpmtu __P((struct route_in6 *, struct route_in6 *,
142 struct ifnet *, struct in6_addr *, u_long *, int *));
143 static int copypktopts __P((struct ip6_pktopts *, struct ip6_pktopts *, int));
147 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
148 * header (with pri, len, nxt, hlim, src, dst).
149 * This function may modify ver and hlim only.
150 * The mbuf chain containing the packet will be freed.
151 * The mbuf opt, if present, will not be freed.
153 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
154 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
155 * which is rt_rmx.rmx_mtu.
158 ip6_output(m0, opt, ro, flags, im6o, ifpp, inp)
160 struct ip6_pktopts *opt;
161 struct route_in6 *ro;
163 struct ip6_moptions *im6o;
164 struct ifnet **ifpp; /* XXX: just for statistics */
167 struct ip6_hdr *ip6, *mhip6;
168 struct ifnet *ifp, *origifp;
170 int hlen, tlen, len, off;
171 struct route_in6 ip6route;
172 struct rtentry *rt = NULL;
173 struct sockaddr_in6 *dst, src_sa, dst_sa;
174 struct in6_addr odst;
176 struct in6_ifaddr *ia = NULL;
178 int alwaysfrag, dontfrag;
179 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
180 struct ip6_exthdrs exthdrs;
181 struct in6_addr finaldst, src0, dst0;
183 struct route_in6 *ro_pmtu = NULL;
186 #if defined(IPSEC) || defined(FAST_IPSEC)
187 int needipsectun = 0;
188 struct secpolicy *sp = NULL;
189 #endif /*IPSEC || FAST_IPSEC*/
191 ip6 = mtod(m, struct ip6_hdr *);
192 finaldst = ip6->ip6_dst;
194 #define MAKE_EXTHDR(hp, mp) \
197 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
198 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
199 ((eh)->ip6e_len + 1) << 3); \
203 } while (/*CONSTCOND*/ 0)
205 bzero(&exthdrs, sizeof(exthdrs));
208 /* Hop-by-Hop options header */
209 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
210 /* Destination options header(1st part) */
211 if (opt->ip6po_rthdr) {
213 * Destination options header(1st part)
214 * This only makes sence with a routing header.
215 * See Section 9.2 of RFC 3542.
216 * Disabling this part just for MIP6 convenience is
217 * a bad idea. We need to think carefully about a
218 * way to make the advanced API coexist with MIP6
219 * options, which might automatically be inserted in
222 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
225 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
226 /* Destination options header(2nd part) */
227 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
231 /* get a security policy for this packet */
233 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
235 sp = ipsec6_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error);
238 ipsec6stat.out_inval++;
245 switch (sp->policy) {
246 case IPSEC_POLICY_DISCARD:
248 * This packet is just discarded.
250 ipsec6stat.out_polvio++;
253 case IPSEC_POLICY_BYPASS:
254 case IPSEC_POLICY_NONE:
255 /* no need to do IPsec. */
259 case IPSEC_POLICY_IPSEC:
260 if (sp->req == NULL) {
261 /* acquire a policy */
262 error = key_spdacquire(sp);
268 case IPSEC_POLICY_ENTRUST:
270 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
274 /* get a security policy for this packet */
276 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
278 sp = ipsec_getpolicybysock(m, IPSEC_DIR_OUTBOUND, inp, &error);
281 newipsecstat.ips_out_inval++;
288 switch (sp->policy) {
289 case IPSEC_POLICY_DISCARD:
291 * This packet is just discarded.
293 newipsecstat.ips_out_polvio++;
296 case IPSEC_POLICY_BYPASS:
297 case IPSEC_POLICY_NONE:
298 /* no need to do IPsec. */
302 case IPSEC_POLICY_IPSEC:
303 if (sp->req == NULL) {
304 /* acquire a policy */
305 error = key_spdacquire(sp);
311 case IPSEC_POLICY_ENTRUST:
313 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
315 #endif /* FAST_IPSEC */
318 * Calculate the total length of the extension header chain.
319 * Keep the length of the unfragmentable part for fragmentation.
322 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
323 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
324 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
325 unfragpartlen = optlen + sizeof(struct ip6_hdr);
326 /* NOTE: we don't add AH/ESP length here. do that later. */
327 if (exthdrs.ip6e_dest2) 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.
378 u_char *nexthdrp = &ip6->ip6_nxt;
379 struct mbuf *mprev = m;
382 * we treat dest2 specially. this makes IPsec processing
383 * much easier. the goal here is to make mprev point the
384 * mbuf prior to dest2.
386 * result: IPv6 dest2 payload
387 * m and mprev will point to IPv6 header.
389 if (exthdrs.ip6e_dest2) {
391 panic("assumption failed: hdr not split");
392 exthdrs.ip6e_dest2->m_next = m->m_next;
393 m->m_next = exthdrs.ip6e_dest2;
394 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
395 ip6->ip6_nxt = IPPROTO_DSTOPTS;
398 #define MAKE_CHAIN(m, mp, p, i)\
402 panic("assumption failed: hdr not split"); \
403 *mtod((m), u_char *) = *(p);\
405 p = mtod((m), u_char *);\
406 (m)->m_next = (mp)->m_next;\
410 } while (/*CONSTCOND*/ 0)
412 * result: IPv6 hbh dest1 rthdr dest2 payload
413 * m will point to IPv6 header. mprev will point to the
414 * extension header prior to dest2 (rthdr in the above case).
416 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
417 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
419 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
422 #if defined(IPSEC) || defined(FAST_IPSEC)
427 * pointers after IPsec headers are not valid any more.
428 * other pointers need a great care too.
429 * (IPsec routines should not mangle mbufs prior to AH/ESP)
431 exthdrs.ip6e_dest2 = NULL;
434 struct ip6_rthdr *rh = NULL;
436 struct ipsec_output_state state;
438 if (exthdrs.ip6e_rthdr) {
439 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
440 segleft_org = rh->ip6r_segleft;
441 rh->ip6r_segleft = 0;
444 bzero(&state, sizeof(state));
446 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
450 /* mbuf is already reclaimed in ipsec6_output_trans. */
460 printf("ip6_output (ipsec): error code %d\n", error);
463 /* don't show these error codes to the user */
469 if (exthdrs.ip6e_rthdr) {
470 /* ah6_output doesn't modify mbuf chain */
471 rh->ip6r_segleft = segleft_org;
479 * If there is a routing header, replace the destination address field
480 * with the first hop of the routing header.
482 if (exthdrs.ip6e_rthdr) {
483 struct ip6_rthdr *rh =
484 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
485 struct ip6_rthdr *));
486 struct ip6_rthdr0 *rh0;
487 struct in6_addr *addr;
488 struct sockaddr_in6 sa;
490 switch (rh->ip6r_type) {
491 case IPV6_RTHDR_TYPE_0:
492 rh0 = (struct ip6_rthdr0 *)rh;
493 addr = (struct in6_addr *)(rh0 + 1);
496 * construct a sockaddr_in6 form of
499 * XXX: we may not have enough
500 * information about its scope zone;
501 * there is no standard API to pass
502 * the information from the
505 bzero(&sa, sizeof(sa));
506 sa.sin6_family = AF_INET6;
507 sa.sin6_len = sizeof(sa);
508 sa.sin6_addr = addr[0];
509 if ((error = sa6_embedscope(&sa,
510 ip6_use_defzone)) != 0) {
513 ip6->ip6_dst = sa.sin6_addr;
514 bcopy(&addr[1], &addr[0], sizeof(struct in6_addr)
515 * (rh0->ip6r0_segleft - 1));
516 addr[rh0->ip6r0_segleft - 1] = finaldst;
518 in6_clearscope(addr + rh0->ip6r0_segleft - 1);
520 default: /* is it possible? */
526 /* Source address validation */
527 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
528 (flags & IPV6_DADOUTPUT) == 0) {
530 ip6stat.ip6s_badscope++;
533 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
535 ip6stat.ip6s_badscope++;
539 ip6stat.ip6s_localout++;
546 bzero((caddr_t)ro, sizeof(*ro));
549 if (opt && opt->ip6po_rthdr)
550 ro = &opt->ip6po_route;
551 dst = (struct sockaddr_in6 *)&ro->ro_dst;
555 * if specified, try to fill in the traffic class field.
556 * do not override if a non-zero value is already set.
557 * we check the diffserv field and the ecn field separately.
559 if (opt && opt->ip6po_tclass >= 0) {
562 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
564 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
567 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
570 /* fill in or override the hop limit field, if necessary. */
571 if (opt && opt->ip6po_hlim != -1)
572 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
573 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
575 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
577 ip6->ip6_hlim = ip6_defmcasthlim;
580 #if defined(IPSEC) || defined(FAST_IPSEC)
581 if (needipsec && needipsectun) {
582 struct ipsec_output_state state;
585 * All the extension headers will become inaccessible
586 * (since they can be encrypted).
587 * Don't panic, we need no more updates to extension headers
588 * on inner IPv6 packet (since they are now encapsulated).
590 * IPv6 [ESP|AH] IPv6 [extension headers] payload
592 bzero(&exthdrs, sizeof(exthdrs));
593 exthdrs.ip6e_ip6 = m;
595 bzero(&state, sizeof(state));
597 state.ro = (struct route *)ro;
598 state.dst = (struct sockaddr *)dst;
600 error = ipsec6_output_tunnel(&state, sp, flags);
603 ro = (struct route_in6 *)state.ro;
604 dst = (struct sockaddr_in6 *)state.dst;
606 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
617 printf("ip6_output (ipsec): error code %d\n", error);
620 /* don't show these error codes to the user */
627 exthdrs.ip6e_ip6 = m;
632 ip6 = mtod(m, struct ip6_hdr *);
634 bzero(&dst_sa, sizeof(dst_sa));
635 dst_sa.sin6_family = AF_INET6;
636 dst_sa.sin6_len = sizeof(dst_sa);
637 dst_sa.sin6_addr = ip6->ip6_dst;
638 if ((error = in6_selectroute(&dst_sa, opt, im6o, ro,
639 &ifp, &rt, 0)) != 0) {
642 ip6stat.ip6s_noroute++;
646 break; /* XXX statistics? */
649 in6_ifstat_inc(ifp, ifs6_out_discard);
654 * If in6_selectroute() does not return a route entry,
655 * dst may not have been updated.
657 *dst = dst_sa; /* XXX */
661 * then rt (for unicast) and ifp must be non-NULL valid values.
663 if ((flags & IPV6_FORWARDING) == 0) {
664 /* XXX: the FORWARDING flag can be set for mrouting. */
665 in6_ifstat_inc(ifp, ifs6_out_request);
668 ia = (struct in6_ifaddr *)(rt->rt_ifa);
673 * The outgoing interface must be in the zone of source and
674 * destination addresses. We should use ia_ifp to support the
675 * case of sending packets to an address of our own.
677 if (ia != NULL && ia->ia_ifp)
678 origifp = ia->ia_ifp;
683 if (in6_setscope(&src0, origifp, &zone))
685 bzero(&src_sa, sizeof(src_sa));
686 src_sa.sin6_family = AF_INET6;
687 src_sa.sin6_len = sizeof(src_sa);
688 src_sa.sin6_addr = ip6->ip6_src;
689 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
693 if (in6_setscope(&dst0, origifp, &zone))
695 /* re-initialize to be sure */
696 bzero(&dst_sa, sizeof(dst_sa));
697 dst_sa.sin6_family = AF_INET6;
698 dst_sa.sin6_len = sizeof(dst_sa);
699 dst_sa.sin6_addr = ip6->ip6_dst;
700 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
704 /* scope check is done. */
708 ip6stat.ip6s_badscope++;
709 in6_ifstat_inc(origifp, ifs6_out_discard);
711 error = EHOSTUNREACH; /* XXX */
715 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
716 if (opt && opt->ip6po_nextroute.ro_rt) {
718 * The nexthop is explicitly specified by the
719 * application. We assume the next hop is an IPv6
722 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
724 else if ((rt->rt_flags & RTF_GATEWAY))
725 dst = (struct sockaddr_in6 *)rt->rt_gateway;
728 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
729 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
731 struct in6_multi *in6m;
733 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
735 in6_ifstat_inc(ifp, ifs6_out_mcast);
738 * Confirm that the outgoing interface supports multicast.
740 if (!(ifp->if_flags & IFF_MULTICAST)) {
741 ip6stat.ip6s_noroute++;
742 in6_ifstat_inc(ifp, ifs6_out_discard);
746 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
748 (im6o == NULL || im6o->im6o_multicast_loop)) {
750 * If we belong to the destination multicast group
751 * on the outgoing interface, and the caller did not
752 * forbid loopback, loop back a copy.
754 ip6_mloopback(ifp, m, dst);
757 * If we are acting as a multicast router, perform
758 * multicast forwarding as if the packet had just
759 * arrived on the interface to which we are about
760 * to send. The multicast forwarding function
761 * recursively calls this function, using the
762 * IPV6_FORWARDING flag to prevent infinite recursion.
764 * Multicasts that are looped back by ip6_mloopback(),
765 * above, will be forwarded by the ip6_input() routine,
768 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
770 * XXX: ip6_mforward expects that rcvif is NULL
771 * when it is called from the originating path.
772 * However, it is not always the case, since
773 * some versions of MGETHDR() does not
774 * initialize the field.
776 m->m_pkthdr.rcvif = NULL;
777 if (ip6_mforward(ip6, ifp, m) != 0) {
784 * Multicasts with a hoplimit of zero may be looped back,
785 * above, but must not be transmitted on a network.
786 * Also, multicasts addressed to the loopback interface
787 * are not sent -- the above call to ip6_mloopback() will
788 * loop back a copy if this host actually belongs to the
789 * destination group on the loopback interface.
791 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
792 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
799 * Fill the outgoing inteface to tell the upper layer
800 * to increment per-interface statistics.
805 /* Determine path MTU. */
806 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
811 * The caller of this function may specify to use the minimum MTU
813 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
814 * setting. The logic is a bit complicated; by default, unicast
815 * packets will follow path MTU while multicast packets will be sent at
816 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
817 * including unicast ones will be sent at the minimum MTU. Multicast
818 * packets will always be sent at the minimum MTU unless
819 * IP6PO_MINMTU_DISABLE is explicitly specified.
820 * See RFC 3542 for more details.
822 if (mtu > IPV6_MMTU) {
823 if ((flags & IPV6_MINMTU))
825 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
827 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
829 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
835 * clear embedded scope identifiers if necessary.
836 * in6_clearscope will touch the addresses only when necessary.
838 in6_clearscope(&ip6->ip6_src);
839 in6_clearscope(&ip6->ip6_dst);
842 * Check with the firewall...
844 if (ip6_fw_enable && ip6_fw_chk_ptr) {
846 m->m_pkthdr.rcvif = NULL; /* XXX */
847 /* If ipfw says divert, we have to just drop packet */
848 if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) {
859 * If the outgoing packet contains a hop-by-hop options header,
860 * it must be examined and processed even by the source node.
861 * (RFC 2460, section 4.)
863 if (exthdrs.ip6e_hbh) {
864 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
865 u_int32_t dummy; /* XXX unused */
866 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
869 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
870 panic("ip6e_hbh is not continuous");
873 * XXX: if we have to send an ICMPv6 error to the sender,
874 * we need the M_LOOP flag since icmp6_error() expects
875 * the IPv6 and the hop-by-hop options header are
876 * continuous unless the flag is set.
878 m->m_flags |= M_LOOP;
879 m->m_pkthdr.rcvif = ifp;
880 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
881 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
882 &dummy, &plen) < 0) {
883 /* m was already freed at this point */
884 error = EINVAL;/* better error? */
887 m->m_flags &= ~M_LOOP; /* XXX */
888 m->m_pkthdr.rcvif = NULL;
891 /* Jump over all PFIL processing if hooks are not active. */
892 if (inet6_pfil_hook.ph_busy_count == -1)
896 /* Run through list of hooks for output packets. */
897 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
898 if (error != 0 || m == NULL)
900 ip6 = mtod(m, struct ip6_hdr *);
902 /* See if destination IP address was changed by packet filter. */
903 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
904 m->m_flags |= M_SKIP_FIREWALL;
905 /* If destination is now ourself drop to ip6_input(). */
906 if (in6_localaddr(&ip6->ip6_dst)) {
907 if (m->m_pkthdr.rcvif == NULL)
908 m->m_pkthdr.rcvif = loif;
909 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
910 m->m_pkthdr.csum_flags |=
911 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
912 m->m_pkthdr.csum_data = 0xffff;
914 m->m_pkthdr.csum_flags |=
915 CSUM_IP_CHECKED | CSUM_IP_VALID;
916 error = netisr_queue(NETISR_IPV6, m);
919 goto again; /* Redo the routing table lookup. */
922 /* XXX: IPFIREWALL_FORWARD */
926 * Send the packet to the outgoing interface.
927 * If necessary, do IPv6 fragmentation before sending.
929 * the logic here is rather complex:
930 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
931 * 1-a: send as is if tlen <= path mtu
932 * 1-b: fragment if tlen > path mtu
934 * 2: if user asks us not to fragment (dontfrag == 1)
935 * 2-a: send as is if tlen <= interface mtu
936 * 2-b: error if tlen > interface mtu
938 * 3: if we always need to attach fragment header (alwaysfrag == 1)
941 * 4: if dontfrag == 1 && alwaysfrag == 1
942 * error, as we cannot handle this conflicting request
944 tlen = m->m_pkthdr.len;
946 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
950 if (dontfrag && alwaysfrag) { /* case 4 */
951 /* conflicting request - can't transmit */
955 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
957 * Even if the DONTFRAG option is specified, we cannot send the
958 * packet when the data length is larger than the MTU of the
959 * outgoing interface.
960 * Notify the error by sending IPV6_PATHMTU ancillary data as
961 * well as returning an error code (the latter is not described
965 struct ip6ctlparam ip6cp;
967 mtu32 = (u_int32_t)mtu;
968 bzero(&ip6cp, sizeof(ip6cp));
969 ip6cp.ip6c_cmdarg = (void *)&mtu32;
970 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
978 * transmit packet without fragmentation
980 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
981 struct in6_ifaddr *ia6;
983 ip6 = mtod(m, struct ip6_hdr *);
984 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
986 /* Record statistics for this interface address. */
987 ia6->ia_ifa.if_opackets++;
988 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
991 /* clean ipsec history once it goes out of the node */
994 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
999 * try to fragment the packet. case 1-b and 3
1001 if (mtu < IPV6_MMTU) {
1002 /* path MTU cannot be less than IPV6_MMTU */
1004 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1006 } else if (ip6->ip6_plen == 0) {
1007 /* jumbo payload cannot be fragmented */
1009 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1012 struct mbuf **mnext, *m_frgpart;
1013 struct ip6_frag *ip6f;
1014 u_int32_t id = htonl(ip6_randomid());
1017 struct ip6ctlparam ip6cp;
1020 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
1023 * Too large for the destination or interface;
1024 * fragment if possible.
1025 * Must be able to put at least 8 bytes per fragment.
1027 hlen = unfragpartlen;
1028 if (mtu > IPV6_MAXPACKET)
1029 mtu = IPV6_MAXPACKET;
1033 * It is believed this code is a leftover from the
1034 * development of the IPV6_RECVPATHMTU sockopt and
1035 * associated work to implement RFC3542.
1036 * It's not entirely clear what the intent of the API
1037 * is at this point, so disable this code for now.
1038 * The IPV6_RECVPATHMTU sockopt and/or IPV6_DONTFRAG
1039 * will send notifications if the application requests.
1042 /* Notify a proper path MTU to applications. */
1043 mtu32 = (u_int32_t)mtu;
1044 bzero(&ip6cp, sizeof(ip6cp));
1045 ip6cp.ip6c_cmdarg = (void *)&mtu32;
1046 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
1050 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1053 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1058 * Verify that we have any chance at all of being able to queue
1059 * the packet or packet fragments
1061 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
1062 < tlen /* - hlen */)) {
1064 ip6stat.ip6s_odropped++;
1068 mnext = &m->m_nextpkt;
1071 * Change the next header field of the last header in the
1072 * unfragmentable part.
1074 if (exthdrs.ip6e_rthdr) {
1075 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1076 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1077 } else if (exthdrs.ip6e_dest1) {
1078 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1079 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1080 } else if (exthdrs.ip6e_hbh) {
1081 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1082 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1084 nextproto = ip6->ip6_nxt;
1085 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1089 * Loop through length of segment after first fragment,
1090 * make new header and copy data of each part and link onto
1094 for (off = hlen; off < tlen; off += len) {
1095 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1098 ip6stat.ip6s_odropped++;
1101 m->m_pkthdr.rcvif = NULL;
1102 m->m_flags = m0->m_flags & M_COPYFLAGS;
1104 mnext = &m->m_nextpkt;
1105 m->m_data += max_linkhdr;
1106 mhip6 = mtod(m, struct ip6_hdr *);
1108 m->m_len = sizeof(*mhip6);
1109 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1111 ip6stat.ip6s_odropped++;
1114 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1115 if (off + len >= tlen)
1118 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1119 mhip6->ip6_plen = htons((u_short)(len + hlen +
1120 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1121 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1123 ip6stat.ip6s_odropped++;
1126 m_cat(m, m_frgpart);
1127 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1128 m->m_pkthdr.rcvif = NULL;
1129 ip6f->ip6f_reserved = 0;
1130 ip6f->ip6f_ident = id;
1131 ip6f->ip6f_nxt = nextproto;
1132 ip6stat.ip6s_ofragments++;
1133 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1136 in6_ifstat_inc(ifp, ifs6_out_fragok);
1140 * Remove leading garbages.
1146 for (m0 = m; m; m = m0) {
1150 /* Record statistics for this interface address. */
1152 ia->ia_ifa.if_opackets++;
1153 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1156 /* clean ipsec history once it goes out of the node */
1159 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1165 ip6stat.ip6s_fragmented++;
1168 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1170 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1171 RTFREE(ro_pmtu->ro_rt);
1181 #endif /* FAST_IPSEC */
1186 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1187 m_freem(exthdrs.ip6e_dest1);
1188 m_freem(exthdrs.ip6e_rthdr);
1189 m_freem(exthdrs.ip6e_dest2);
1197 ip6_copyexthdr(mp, hdr, hlen)
1204 if (hlen > MCLBYTES)
1205 return (ENOBUFS); /* XXX */
1207 MGET(m, M_DONTWAIT, MT_DATA);
1212 MCLGET(m, M_DONTWAIT);
1213 if ((m->m_flags & M_EXT) == 0) {
1220 bcopy(hdr, mtod(m, caddr_t), hlen);
1227 * Insert jumbo payload option.
1230 ip6_insert_jumboopt(exthdrs, plen)
1231 struct ip6_exthdrs *exthdrs;
1238 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1241 * If there is no hop-by-hop options header, allocate new one.
1242 * If there is one but it doesn't have enough space to store the
1243 * jumbo payload option, allocate a cluster to store the whole options.
1244 * Otherwise, use it to store the options.
1246 if (exthdrs->ip6e_hbh == 0) {
1247 MGET(mopt, M_DONTWAIT, MT_DATA);
1250 mopt->m_len = JUMBOOPTLEN;
1251 optbuf = mtod(mopt, u_char *);
1252 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1253 exthdrs->ip6e_hbh = mopt;
1255 struct ip6_hbh *hbh;
1257 mopt = exthdrs->ip6e_hbh;
1258 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1261 * - exthdrs->ip6e_hbh is not referenced from places
1262 * other than exthdrs.
1263 * - exthdrs->ip6e_hbh is not an mbuf chain.
1265 int oldoptlen = mopt->m_len;
1269 * XXX: give up if the whole (new) hbh header does
1270 * not fit even in an mbuf cluster.
1272 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1276 * As a consequence, we must always prepare a cluster
1279 MGET(n, M_DONTWAIT, MT_DATA);
1281 MCLGET(n, M_DONTWAIT);
1282 if ((n->m_flags & M_EXT) == 0) {
1289 n->m_len = oldoptlen + JUMBOOPTLEN;
1290 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1292 optbuf = mtod(n, caddr_t) + oldoptlen;
1294 mopt = exthdrs->ip6e_hbh = n;
1296 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1297 mopt->m_len += JUMBOOPTLEN;
1299 optbuf[0] = IP6OPT_PADN;
1303 * Adjust the header length according to the pad and
1304 * the jumbo payload option.
1306 hbh = mtod(mopt, struct ip6_hbh *);
1307 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1310 /* fill in the option. */
1311 optbuf[2] = IP6OPT_JUMBO;
1313 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1314 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1316 /* finally, adjust the packet header length */
1317 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1324 * Insert fragment header and copy unfragmentable header portions.
1327 ip6_insertfraghdr(m0, m, hlen, frghdrp)
1328 struct mbuf *m0, *m;
1330 struct ip6_frag **frghdrp;
1332 struct mbuf *n, *mlast;
1334 if (hlen > sizeof(struct ip6_hdr)) {
1335 n = m_copym(m0, sizeof(struct ip6_hdr),
1336 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1343 /* Search for the last mbuf of unfragmentable part. */
1344 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1347 if ((mlast->m_flags & M_EXT) == 0 &&
1348 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1349 /* use the trailing space of the last mbuf for the fragment hdr */
1350 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1352 mlast->m_len += sizeof(struct ip6_frag);
1353 m->m_pkthdr.len += sizeof(struct ip6_frag);
1355 /* allocate a new mbuf for the fragment header */
1358 MGET(mfrg, M_DONTWAIT, MT_DATA);
1361 mfrg->m_len = sizeof(struct ip6_frag);
1362 *frghdrp = mtod(mfrg, struct ip6_frag *);
1363 mlast->m_next = mfrg;
1370 ip6_getpmtu(ro_pmtu, ro, ifp, dst, mtup, alwaysfragp)
1371 struct route_in6 *ro_pmtu, *ro;
1373 struct in6_addr *dst;
1381 if (ro_pmtu != ro) {
1382 /* The first hop and the final destination may differ. */
1383 struct sockaddr_in6 *sa6_dst =
1384 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1385 if (ro_pmtu->ro_rt &&
1386 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1387 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1388 RTFREE(ro_pmtu->ro_rt);
1389 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1391 if (ro_pmtu->ro_rt == NULL) {
1392 bzero(sa6_dst, sizeof(*sa6_dst));
1393 sa6_dst->sin6_family = AF_INET6;
1394 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1395 sa6_dst->sin6_addr = *dst;
1397 rtalloc((struct route *)ro_pmtu);
1400 if (ro_pmtu->ro_rt) {
1402 struct in_conninfo inc;
1404 bzero(&inc, sizeof(inc));
1405 inc.inc_flags = 1; /* IPv6 */
1406 inc.inc6_faddr = *dst;
1409 ifp = ro_pmtu->ro_rt->rt_ifp;
1410 ifmtu = IN6_LINKMTU(ifp);
1411 mtu = tcp_hc_getmtu(&inc);
1413 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu);
1415 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1418 else if (mtu < IPV6_MMTU) {
1420 * RFC2460 section 5, last paragraph:
1421 * if we record ICMPv6 too big message with
1422 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1423 * or smaller, with framgent header attached.
1424 * (fragment header is needed regardless from the
1425 * packet size, for translators to identify packets)
1429 } else if (mtu > ifmtu) {
1431 * The MTU on the route is larger than the MTU on
1432 * the interface! This shouldn't happen, unless the
1433 * MTU of the interface has been changed after the
1434 * interface was brought up. Change the MTU in the
1435 * route to match the interface MTU (as long as the
1436 * field isn't locked).
1439 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1442 mtu = IN6_LINKMTU(ifp);
1444 error = EHOSTUNREACH; /* XXX */
1448 *alwaysfragp = alwaysfrag;
1453 * IP6 socket option processing.
1456 ip6_ctloutput(so, sopt)
1458 struct sockopt *sopt;
1460 int privileged, optdatalen, uproto;
1462 struct inpcb *in6p = sotoinpcb(so);
1464 int level, op, optname;
1469 level = sopt->sopt_level;
1470 op = sopt->sopt_dir;
1471 optname = sopt->sopt_name;
1472 optlen = sopt->sopt_valsize;
1475 panic("ip6_ctloutput: arg soopt is NULL");
1479 privileged = (td == 0 || suser(td)) ? 0 : 1;
1480 uproto = (int)so->so_proto->pr_protocol;
1482 if (level == IPPROTO_IPV6) {
1487 case IPV6_2292PKTOPTIONS:
1488 #ifdef IPV6_PKTOPTIONS
1489 case IPV6_PKTOPTIONS:
1494 error = soopt_getm(sopt, &m); /* XXX */
1497 error = soopt_mcopyin(sopt, m); /* XXX */
1500 error = ip6_pcbopts(&in6p->in6p_outputopts,
1502 m_freem(m); /* XXX */
1507 * Use of some Hop-by-Hop options or some
1508 * Destination options, might require special
1509 * privilege. That is, normal applications
1510 * (without special privilege) might be forbidden
1511 * from setting certain options in outgoing packets,
1512 * and might never see certain options in received
1513 * packets. [RFC 2292 Section 6]
1514 * KAME specific note:
1515 * KAME prevents non-privileged users from sending or
1516 * receiving ANY hbh/dst options in order to avoid
1517 * overhead of parsing options in the kernel.
1519 case IPV6_RECVHOPOPTS:
1520 case IPV6_RECVDSTOPTS:
1521 case IPV6_RECVRTHDRDSTOPTS:
1527 case IPV6_UNICAST_HOPS:
1531 case IPV6_RECVPKTINFO:
1532 case IPV6_RECVHOPLIMIT:
1533 case IPV6_RECVRTHDR:
1534 case IPV6_RECVPATHMTU:
1535 case IPV6_RECVTCLASS:
1537 case IPV6_AUTOFLOWLABEL:
1538 if (optlen != sizeof(int)) {
1542 error = sooptcopyin(sopt, &optval,
1543 sizeof optval, sizeof optval);
1548 case IPV6_UNICAST_HOPS:
1549 if (optval < -1 || optval >= 256)
1552 /* -1 = kernel default */
1553 in6p->in6p_hops = optval;
1554 if ((in6p->in6p_vflag &
1556 in6p->inp_ip_ttl = optval;
1559 #define OPTSET(bit) \
1562 in6p->in6p_flags |= (bit); \
1564 in6p->in6p_flags &= ~(bit); \
1565 } while (/*CONSTCOND*/ 0)
1566 #define OPTSET2292(bit) \
1568 in6p->in6p_flags |= IN6P_RFC2292; \
1570 in6p->in6p_flags |= (bit); \
1572 in6p->in6p_flags &= ~(bit); \
1573 } while (/*CONSTCOND*/ 0)
1574 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1576 case IPV6_RECVPKTINFO:
1577 /* cannot mix with RFC2292 */
1578 if (OPTBIT(IN6P_RFC2292)) {
1582 OPTSET(IN6P_PKTINFO);
1587 struct ip6_pktopts **optp;
1589 /* cannot mix with RFC2292 */
1590 if (OPTBIT(IN6P_RFC2292)) {
1594 optp = &in6p->in6p_outputopts;
1595 error = ip6_pcbopt(IPV6_HOPLIMIT,
1599 privileged, uproto);
1603 case IPV6_RECVHOPLIMIT:
1604 /* cannot mix with RFC2292 */
1605 if (OPTBIT(IN6P_RFC2292)) {
1609 OPTSET(IN6P_HOPLIMIT);
1612 case IPV6_RECVHOPOPTS:
1613 /* cannot mix with RFC2292 */
1614 if (OPTBIT(IN6P_RFC2292)) {
1618 OPTSET(IN6P_HOPOPTS);
1621 case IPV6_RECVDSTOPTS:
1622 /* cannot mix with RFC2292 */
1623 if (OPTBIT(IN6P_RFC2292)) {
1627 OPTSET(IN6P_DSTOPTS);
1630 case IPV6_RECVRTHDRDSTOPTS:
1631 /* cannot mix with RFC2292 */
1632 if (OPTBIT(IN6P_RFC2292)) {
1636 OPTSET(IN6P_RTHDRDSTOPTS);
1639 case IPV6_RECVRTHDR:
1640 /* cannot mix with RFC2292 */
1641 if (OPTBIT(IN6P_RFC2292)) {
1652 case IPV6_RECVPATHMTU:
1654 * We ignore this option for TCP
1656 * (RFC3542 leaves this case
1659 if (uproto != IPPROTO_TCP)
1665 * make setsockopt(IPV6_V6ONLY)
1666 * available only prior to bind(2).
1667 * see ipng mailing list, Jun 22 2001.
1669 if (in6p->in6p_lport ||
1670 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1674 OPTSET(IN6P_IPV6_V6ONLY);
1676 in6p->in6p_vflag &= ~INP_IPV4;
1678 in6p->in6p_vflag |= INP_IPV4;
1680 case IPV6_RECVTCLASS:
1681 /* cannot mix with RFC2292 XXX */
1682 if (OPTBIT(IN6P_RFC2292)) {
1686 OPTSET(IN6P_TCLASS);
1688 case IPV6_AUTOFLOWLABEL:
1689 OPTSET(IN6P_AUTOFLOWLABEL);
1697 case IPV6_USE_MIN_MTU:
1698 case IPV6_PREFER_TEMPADDR:
1699 if (optlen != sizeof(optval)) {
1703 error = sooptcopyin(sopt, &optval,
1704 sizeof optval, sizeof optval);
1708 struct ip6_pktopts **optp;
1709 optp = &in6p->in6p_outputopts;
1710 error = ip6_pcbopt(optname,
1714 privileged, uproto);
1718 case IPV6_2292PKTINFO:
1719 case IPV6_2292HOPLIMIT:
1720 case IPV6_2292HOPOPTS:
1721 case IPV6_2292DSTOPTS:
1722 case IPV6_2292RTHDR:
1724 if (optlen != sizeof(int)) {
1728 error = sooptcopyin(sopt, &optval,
1729 sizeof optval, sizeof optval);
1733 case IPV6_2292PKTINFO:
1734 OPTSET2292(IN6P_PKTINFO);
1736 case IPV6_2292HOPLIMIT:
1737 OPTSET2292(IN6P_HOPLIMIT);
1739 case IPV6_2292HOPOPTS:
1741 * Check super-user privilege.
1742 * See comments for IPV6_RECVHOPOPTS.
1746 OPTSET2292(IN6P_HOPOPTS);
1748 case IPV6_2292DSTOPTS:
1751 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1753 case IPV6_2292RTHDR:
1754 OPTSET2292(IN6P_RTHDR);
1762 case IPV6_RTHDRDSTOPTS:
1765 /* new advanced API (RFC3542) */
1768 struct ip6_pktopts **optp;
1770 /* cannot mix with RFC2292 */
1771 if (OPTBIT(IN6P_RFC2292)) {
1779 case IPV6_RTHDRDSTOPTS:
1790 optlen = sizeof(struct in6_pktinfo);
1793 optlen = SOCK_MAXADDRLEN;
1796 optlen = IPV6_MAXOPTHDR;
1799 if (sopt->sopt_valsize > optlen) {
1804 optlen = sopt->sopt_valsize;
1805 optbuf = malloc(optlen, M_TEMP, M_WAITOK);
1806 error = sooptcopyin(sopt, optbuf, optlen,
1809 free(optbuf, M_TEMP);
1813 optp = &in6p->in6p_outputopts;
1814 error = ip6_pcbopt(optname,
1816 optp, privileged, uproto);
1817 free(optbuf, M_TEMP);
1822 case IPV6_MULTICAST_IF:
1823 case IPV6_MULTICAST_HOPS:
1824 case IPV6_MULTICAST_LOOP:
1825 case IPV6_JOIN_GROUP:
1826 case IPV6_LEAVE_GROUP:
1828 if (sopt->sopt_valsize > MLEN) {
1838 if (sopt->sopt_valsize > MCLBYTES) {
1843 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_HEADER);
1848 if (sopt->sopt_valsize > MLEN) {
1849 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
1850 if ((m->m_flags & M_EXT) == 0) {
1856 m->m_len = sopt->sopt_valsize;
1857 error = sooptcopyin(sopt, mtod(m, char *),
1858 m->m_len, m->m_len);
1863 error = ip6_setmoptions(sopt->sopt_name,
1864 &in6p->in6p_moptions,
1870 case IPV6_PORTRANGE:
1871 error = sooptcopyin(sopt, &optval,
1872 sizeof optval, sizeof optval);
1877 case IPV6_PORTRANGE_DEFAULT:
1878 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1879 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1882 case IPV6_PORTRANGE_HIGH:
1883 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1884 in6p->in6p_flags |= IN6P_HIGHPORT;
1887 case IPV6_PORTRANGE_LOW:
1888 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1889 in6p->in6p_flags |= IN6P_LOWPORT;
1898 #if defined(IPSEC) || defined(FAST_IPSEC)
1899 case IPV6_IPSEC_POLICY:
1905 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1907 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1910 req = mtod(m, caddr_t);
1913 error = ipsec6_set_policy(in6p, optname, req,
1918 #endif /* KAME IPSEC */
1926 struct mbuf **mp = &m;
1928 if (ip6_fw_ctl_ptr == NULL)
1931 if ((error = soopt_getm(sopt, &m)) != 0)
1934 if ((error = soopt_mcopyin(sopt, m)) != 0)
1936 error = (*ip6_fw_ctl_ptr)(optname, mp);
1942 error = ENOPROTOOPT;
1950 case IPV6_2292PKTOPTIONS:
1951 #ifdef IPV6_PKTOPTIONS
1952 case IPV6_PKTOPTIONS:
1955 * RFC3542 (effectively) deprecated the
1956 * semantics of the 2292-style pktoptions.
1957 * Since it was not reliable in nature (i.e.,
1958 * applications had to expect the lack of some
1959 * information after all), it would make sense
1960 * to simplify this part by always returning
1963 sopt->sopt_valsize = 0;
1966 case IPV6_RECVHOPOPTS:
1967 case IPV6_RECVDSTOPTS:
1968 case IPV6_RECVRTHDRDSTOPTS:
1969 case IPV6_UNICAST_HOPS:
1970 case IPV6_RECVPKTINFO:
1971 case IPV6_RECVHOPLIMIT:
1972 case IPV6_RECVRTHDR:
1973 case IPV6_RECVPATHMTU:
1977 case IPV6_PORTRANGE:
1978 case IPV6_RECVTCLASS:
1979 case IPV6_AUTOFLOWLABEL:
1982 case IPV6_RECVHOPOPTS:
1983 optval = OPTBIT(IN6P_HOPOPTS);
1986 case IPV6_RECVDSTOPTS:
1987 optval = OPTBIT(IN6P_DSTOPTS);
1990 case IPV6_RECVRTHDRDSTOPTS:
1991 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1994 case IPV6_UNICAST_HOPS:
1995 optval = in6p->in6p_hops;
1998 case IPV6_RECVPKTINFO:
1999 optval = OPTBIT(IN6P_PKTINFO);
2002 case IPV6_RECVHOPLIMIT:
2003 optval = OPTBIT(IN6P_HOPLIMIT);
2006 case IPV6_RECVRTHDR:
2007 optval = OPTBIT(IN6P_RTHDR);
2010 case IPV6_RECVPATHMTU:
2011 optval = OPTBIT(IN6P_MTU);
2015 optval = OPTBIT(IN6P_FAITH);
2019 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2022 case IPV6_PORTRANGE:
2025 flags = in6p->in6p_flags;
2026 if (flags & IN6P_HIGHPORT)
2027 optval = IPV6_PORTRANGE_HIGH;
2028 else if (flags & IN6P_LOWPORT)
2029 optval = IPV6_PORTRANGE_LOW;
2034 case IPV6_RECVTCLASS:
2035 optval = OPTBIT(IN6P_TCLASS);
2038 case IPV6_AUTOFLOWLABEL:
2039 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2044 error = sooptcopyout(sopt, &optval,
2051 struct ip6_mtuinfo mtuinfo;
2052 struct route_in6 sro;
2054 bzero(&sro, sizeof(sro));
2056 if (!(so->so_state & SS_ISCONNECTED))
2059 * XXX: we dot not consider the case of source
2060 * routing, or optional information to specify
2061 * the outgoing interface.
2063 error = ip6_getpmtu(&sro, NULL, NULL,
2064 &in6p->in6p_faddr, &pmtu, NULL);
2069 if (pmtu > IPV6_MAXPACKET)
2070 pmtu = IPV6_MAXPACKET;
2072 bzero(&mtuinfo, sizeof(mtuinfo));
2073 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2074 optdata = (void *)&mtuinfo;
2075 optdatalen = sizeof(mtuinfo);
2076 error = sooptcopyout(sopt, optdata,
2081 case IPV6_2292PKTINFO:
2082 case IPV6_2292HOPLIMIT:
2083 case IPV6_2292HOPOPTS:
2084 case IPV6_2292RTHDR:
2085 case IPV6_2292DSTOPTS:
2087 case IPV6_2292PKTINFO:
2088 optval = OPTBIT(IN6P_PKTINFO);
2090 case IPV6_2292HOPLIMIT:
2091 optval = OPTBIT(IN6P_HOPLIMIT);
2093 case IPV6_2292HOPOPTS:
2094 optval = OPTBIT(IN6P_HOPOPTS);
2096 case IPV6_2292RTHDR:
2097 optval = OPTBIT(IN6P_RTHDR);
2099 case IPV6_2292DSTOPTS:
2100 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2103 error = sooptcopyout(sopt, &optval,
2110 case IPV6_RTHDRDSTOPTS:
2114 case IPV6_USE_MIN_MTU:
2115 case IPV6_PREFER_TEMPADDR:
2116 error = ip6_getpcbopt(in6p->in6p_outputopts,
2120 case IPV6_MULTICAST_IF:
2121 case IPV6_MULTICAST_HOPS:
2122 case IPV6_MULTICAST_LOOP:
2123 case IPV6_JOIN_GROUP:
2124 case IPV6_LEAVE_GROUP:
2127 error = ip6_getmoptions(sopt->sopt_name,
2128 in6p->in6p_moptions, &m);
2130 error = sooptcopyout(sopt,
2131 mtod(m, char *), m->m_len);
2136 #if defined(IPSEC) || defined(FAST_IPSEC)
2137 case IPV6_IPSEC_POLICY:
2141 struct mbuf *m = NULL;
2142 struct mbuf **mp = &m;
2143 size_t ovalsize = sopt->sopt_valsize;
2144 caddr_t oval = (caddr_t)sopt->sopt_val;
2146 error = soopt_getm(sopt, &m); /* XXX */
2149 error = soopt_mcopyin(sopt, m); /* XXX */
2152 sopt->sopt_valsize = ovalsize;
2153 sopt->sopt_val = oval;
2155 req = mtod(m, caddr_t);
2158 error = ipsec6_get_policy(in6p, req, len, mp);
2160 error = soopt_mcopyout(sopt, m); /* XXX */
2161 if (error == 0 && m)
2165 #endif /* KAME IPSEC */
2170 struct mbuf **mp = &m;
2172 if (ip6_fw_ctl_ptr == NULL)
2176 error = (*ip6_fw_ctl_ptr)(optname, mp);
2178 error = soopt_mcopyout(sopt, m); /* XXX */
2179 if (error == 0 && m)
2185 error = ENOPROTOOPT;
2190 } else { /* level != IPPROTO_IPV6 */
2197 ip6_raw_ctloutput(so, sopt)
2199 struct sockopt *sopt;
2201 int error = 0, optval, optlen;
2202 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2203 struct in6pcb *in6p = sotoin6pcb(so);
2204 int level, op, optname;
2207 level = sopt->sopt_level;
2208 op = sopt->sopt_dir;
2209 optname = sopt->sopt_name;
2210 optlen = sopt->sopt_valsize;
2212 panic("ip6_raw_ctloutput: arg soopt is NULL");
2214 if (level != IPPROTO_IPV6) {
2221 * For ICMPv6 sockets, no modification allowed for checksum
2222 * offset, permit "no change" values to help existing apps.
2224 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2225 * for an ICMPv6 socket will fail."
2226 * The current behavior does not meet RFC3542.
2230 if (optlen != sizeof(int)) {
2234 error = sooptcopyin(sopt, &optval, sizeof(optval),
2238 if ((optval % 2) != 0) {
2239 /* the API assumes even offset values */
2241 } else if (so->so_proto->pr_protocol ==
2243 if (optval != icmp6off)
2246 in6p->in6p_cksum = optval;
2250 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2253 optval = in6p->in6p_cksum;
2255 error = sooptcopyout(sopt, &optval, sizeof(optval));
2265 error = ENOPROTOOPT;
2273 * Set up IP6 options in pcb for insertion in output packets or
2274 * specifying behavior of outgoing packets.
2277 ip6_pcbopts(pktopt, m, so, sopt)
2278 struct ip6_pktopts **pktopt;
2281 struct sockopt *sopt;
2283 struct ip6_pktopts *opt = *pktopt;
2285 struct thread *td = sopt->sopt_td;
2288 /* turn off any old options. */
2291 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2292 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2293 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2294 printf("ip6_pcbopts: all specified options are cleared.\n");
2296 ip6_clearpktopts(opt, -1);
2298 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2301 if (!m || m->m_len == 0) {
2303 * Only turning off any previous options, regardless of
2304 * whether the opt is just created or given.
2306 free(opt, M_IP6OPT);
2310 /* set options specified by user. */
2311 if (td && !suser(td))
2313 if ((error = ip6_setpktopts(m, opt, NULL, priv,
2314 so->so_proto->pr_protocol)) != 0) {
2315 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2316 free(opt, M_IP6OPT);
2324 * initialize ip6_pktopts. beware that there are non-zero default values in
2328 ip6_initpktopts(opt)
2329 struct ip6_pktopts *opt;
2332 bzero(opt, sizeof(*opt));
2333 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2334 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2335 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2336 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2340 ip6_pcbopt(optname, buf, len, pktopt, priv, uproto)
2341 int optname, len, priv;
2343 struct ip6_pktopts **pktopt;
2346 struct ip6_pktopts *opt;
2348 if (*pktopt == NULL) {
2349 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2351 ip6_initpktopts(*pktopt);
2355 return (ip6_setpktopt(optname, buf, len, opt, priv, 1, 0, uproto));
2359 ip6_getpcbopt(pktopt, optname, sopt)
2360 struct ip6_pktopts *pktopt;
2361 struct sockopt *sopt;
2364 void *optdata = NULL;
2366 struct ip6_ext *ip6e;
2368 struct in6_pktinfo null_pktinfo;
2369 int deftclass = 0, on;
2370 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2371 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2375 if (pktopt && pktopt->ip6po_pktinfo)
2376 optdata = (void *)pktopt->ip6po_pktinfo;
2378 /* XXX: we don't have to do this every time... */
2379 bzero(&null_pktinfo, sizeof(null_pktinfo));
2380 optdata = (void *)&null_pktinfo;
2382 optdatalen = sizeof(struct in6_pktinfo);
2385 if (pktopt && pktopt->ip6po_tclass >= 0)
2386 optdata = (void *)&pktopt->ip6po_tclass;
2388 optdata = (void *)&deftclass;
2389 optdatalen = sizeof(int);
2392 if (pktopt && pktopt->ip6po_hbh) {
2393 optdata = (void *)pktopt->ip6po_hbh;
2394 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2395 optdatalen = (ip6e->ip6e_len + 1) << 3;
2399 if (pktopt && pktopt->ip6po_rthdr) {
2400 optdata = (void *)pktopt->ip6po_rthdr;
2401 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2402 optdatalen = (ip6e->ip6e_len + 1) << 3;
2405 case IPV6_RTHDRDSTOPTS:
2406 if (pktopt && pktopt->ip6po_dest1) {
2407 optdata = (void *)pktopt->ip6po_dest1;
2408 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2409 optdatalen = (ip6e->ip6e_len + 1) << 3;
2413 if (pktopt && pktopt->ip6po_dest2) {
2414 optdata = (void *)pktopt->ip6po_dest2;
2415 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2416 optdatalen = (ip6e->ip6e_len + 1) << 3;
2420 if (pktopt && pktopt->ip6po_nexthop) {
2421 optdata = (void *)pktopt->ip6po_nexthop;
2422 optdatalen = pktopt->ip6po_nexthop->sa_len;
2425 case IPV6_USE_MIN_MTU:
2427 optdata = (void *)&pktopt->ip6po_minmtu;
2429 optdata = (void *)&defminmtu;
2430 optdatalen = sizeof(int);
2433 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2437 optdata = (void *)&on;
2438 optdatalen = sizeof(on);
2440 case IPV6_PREFER_TEMPADDR:
2442 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2444 optdata = (void *)&defpreftemp;
2445 optdatalen = sizeof(int);
2447 default: /* should not happen */
2449 panic("ip6_getpcbopt: unexpected option\n");
2451 return (ENOPROTOOPT);
2454 error = sooptcopyout(sopt, optdata, optdatalen);
2460 ip6_clearpktopts(pktopt, optname)
2461 struct ip6_pktopts *pktopt;
2467 if (optname == -1 || optname == IPV6_PKTINFO) {
2468 if (pktopt->ip6po_pktinfo)
2469 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2470 pktopt->ip6po_pktinfo = NULL;
2472 if (optname == -1 || optname == IPV6_HOPLIMIT)
2473 pktopt->ip6po_hlim = -1;
2474 if (optname == -1 || optname == IPV6_TCLASS)
2475 pktopt->ip6po_tclass = -1;
2476 if (optname == -1 || optname == IPV6_NEXTHOP) {
2477 if (pktopt->ip6po_nextroute.ro_rt) {
2478 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2479 pktopt->ip6po_nextroute.ro_rt = NULL;
2481 if (pktopt->ip6po_nexthop)
2482 free(pktopt->ip6po_nexthop, M_IP6OPT);
2483 pktopt->ip6po_nexthop = NULL;
2485 if (optname == -1 || optname == IPV6_HOPOPTS) {
2486 if (pktopt->ip6po_hbh)
2487 free(pktopt->ip6po_hbh, M_IP6OPT);
2488 pktopt->ip6po_hbh = NULL;
2490 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2491 if (pktopt->ip6po_dest1)
2492 free(pktopt->ip6po_dest1, M_IP6OPT);
2493 pktopt->ip6po_dest1 = NULL;
2495 if (optname == -1 || optname == IPV6_RTHDR) {
2496 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2497 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2498 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2499 if (pktopt->ip6po_route.ro_rt) {
2500 RTFREE(pktopt->ip6po_route.ro_rt);
2501 pktopt->ip6po_route.ro_rt = NULL;
2504 if (optname == -1 || optname == IPV6_DSTOPTS) {
2505 if (pktopt->ip6po_dest2)
2506 free(pktopt->ip6po_dest2, M_IP6OPT);
2507 pktopt->ip6po_dest2 = NULL;
2511 #define PKTOPT_EXTHDRCPY(type) \
2514 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2515 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2516 if (dst->type == NULL && canwait == M_NOWAIT)\
2518 bcopy(src->type, dst->type, hlen);\
2520 } while (/*CONSTCOND*/ 0)
2523 copypktopts(dst, src, canwait)
2524 struct ip6_pktopts *dst, *src;
2527 if (dst == NULL || src == NULL) {
2528 printf("ip6_clearpktopts: invalid argument\n");
2532 dst->ip6po_hlim = src->ip6po_hlim;
2533 dst->ip6po_tclass = src->ip6po_tclass;
2534 dst->ip6po_flags = src->ip6po_flags;
2535 if (src->ip6po_pktinfo) {
2536 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2538 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
2540 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2542 if (src->ip6po_nexthop) {
2543 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2545 if (dst->ip6po_nexthop == NULL)
2547 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2548 src->ip6po_nexthop->sa_len);
2550 PKTOPT_EXTHDRCPY(ip6po_hbh);
2551 PKTOPT_EXTHDRCPY(ip6po_dest1);
2552 PKTOPT_EXTHDRCPY(ip6po_dest2);
2553 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2557 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
2558 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
2559 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
2560 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
2561 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
2562 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
2565 #undef PKTOPT_EXTHDRCPY
2567 struct ip6_pktopts *
2568 ip6_copypktopts(src, canwait)
2569 struct ip6_pktopts *src;
2573 struct ip6_pktopts *dst;
2575 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2576 if (dst == NULL && canwait == M_NOWAIT)
2578 ip6_initpktopts(dst);
2580 if ((error = copypktopts(dst, src, canwait)) != 0) {
2581 free(dst, M_IP6OPT);
2589 ip6_freepcbopts(pktopt)
2590 struct ip6_pktopts *pktopt;
2595 ip6_clearpktopts(pktopt, -1);
2597 free(pktopt, M_IP6OPT);
2601 * Set the IP6 multicast options in response to user setsockopt().
2604 ip6_setmoptions(optname, im6op, m)
2606 struct ip6_moptions **im6op;
2610 u_int loop, ifindex;
2611 struct ipv6_mreq *mreq;
2613 struct ip6_moptions *im6o = *im6op;
2614 struct route_in6 ro;
2615 struct in6_multi_mship *imm;
2616 struct thread *td = curthread;
2620 * No multicast option buffer attached to the pcb;
2621 * allocate one and initialize to default values.
2623 im6o = (struct ip6_moptions *)
2624 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
2629 im6o->im6o_multicast_ifp = NULL;
2630 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2631 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2632 LIST_INIT(&im6o->im6o_memberships);
2637 case IPV6_MULTICAST_IF:
2639 * Select the interface for outgoing multicast packets.
2641 if (m == NULL || m->m_len != sizeof(u_int)) {
2645 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2646 if (ifindex < 0 || if_index < ifindex) {
2647 error = ENXIO; /* XXX EINVAL? */
2650 ifp = ifnet_byindex(ifindex);
2651 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2652 error = EADDRNOTAVAIL;
2655 im6o->im6o_multicast_ifp = ifp;
2658 case IPV6_MULTICAST_HOPS:
2661 * Set the IP6 hoplimit for outgoing multicast packets.
2664 if (m == NULL || m->m_len != sizeof(int)) {
2668 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2669 if (optval < -1 || optval >= 256)
2671 else if (optval == -1)
2672 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2674 im6o->im6o_multicast_hlim = optval;
2678 case IPV6_MULTICAST_LOOP:
2680 * Set the loopback flag for outgoing multicast packets.
2681 * Must be zero or one.
2683 if (m == NULL || m->m_len != sizeof(u_int)) {
2687 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2692 im6o->im6o_multicast_loop = loop;
2695 case IPV6_JOIN_GROUP:
2697 * Add a multicast group membership.
2698 * Group must be a valid IP6 multicast address.
2700 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2704 mreq = mtod(m, struct ipv6_mreq *);
2706 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2708 * We use the unspecified address to specify to accept
2709 * all multicast addresses. Only super user is allowed
2716 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2722 * If no interface was explicitly specified, choose an
2723 * appropriate one according to the given multicast address.
2725 if (mreq->ipv6mr_interface == 0) {
2726 struct sockaddr_in6 *dst;
2729 * Look up the routing table for the
2730 * address, and choose the outgoing interface.
2731 * XXX: is it a good approach?
2734 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2735 bzero(dst, sizeof(*dst));
2736 dst->sin6_family = AF_INET6;
2737 dst->sin6_len = sizeof(*dst);
2738 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2739 rtalloc((struct route *)&ro);
2740 if (ro.ro_rt == NULL) {
2741 error = EADDRNOTAVAIL;
2744 ifp = ro.ro_rt->rt_ifp;
2748 * If the interface is specified, validate it.
2750 if (mreq->ipv6mr_interface < 0 ||
2751 if_index < mreq->ipv6mr_interface) {
2752 error = ENXIO; /* XXX EINVAL? */
2755 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2757 error = ENXIO; /* XXX EINVAL? */
2763 * See if we found an interface, and confirm that it
2764 * supports multicast
2766 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2767 error = EADDRNOTAVAIL;
2771 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2772 error = EADDRNOTAVAIL; /* XXX: should not happen */
2777 * See if the membership already exists.
2779 for (imm = im6o->im6o_memberships.lh_first;
2780 imm != NULL; imm = imm->i6mm_chain.le_next)
2781 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2782 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2783 &mreq->ipv6mr_multiaddr))
2790 * Everything looks good; add a new record to the multicast
2791 * address list for the given interface.
2793 imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
2798 if ((imm->i6mm_maddr =
2799 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
2800 free(imm, M_IPMADDR);
2803 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2806 case IPV6_LEAVE_GROUP:
2808 * Drop a multicast group membership.
2809 * Group must be a valid IP6 multicast address.
2811 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2815 mreq = mtod(m, struct ipv6_mreq *);
2818 * If an interface address was specified, get a pointer
2819 * to its ifnet structure.
2821 if (mreq->ipv6mr_interface < 0 ||
2822 if_index < mreq->ipv6mr_interface) {
2823 error = ENXIO; /* XXX EINVAL? */
2826 if (mreq->ipv6mr_interface == 0)
2829 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2831 /* Fill in the scope zone ID */
2833 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2834 /* XXX: should not happen */
2835 error = EADDRNOTAVAIL;
2838 } else if (mreq->ipv6mr_interface != 0) {
2840 * This case happens when the (positive) index is in
2841 * the valid range, but the corresponding interface has
2842 * been detached dynamically (XXX).
2844 error = EADDRNOTAVAIL;
2846 } else { /* ipv6mr_interface == 0 */
2847 struct sockaddr_in6 sa6_mc;
2850 * The API spec says as follows:
2851 * If the interface index is specified as 0, the
2852 * system may choose a multicast group membership to
2853 * drop by matching the multicast address only.
2854 * On the other hand, we cannot disambiguate the scope
2855 * zone unless an interface is provided. Thus, we
2856 * check if there's ambiguity with the default scope
2857 * zone as the last resort.
2859 bzero(&sa6_mc, sizeof(sa6_mc));
2860 sa6_mc.sin6_family = AF_INET6;
2861 sa6_mc.sin6_len = sizeof(sa6_mc);
2862 sa6_mc.sin6_addr = mreq->ipv6mr_multiaddr;
2863 error = sa6_embedscope(&sa6_mc, ip6_use_defzone);
2866 mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr;
2870 * Find the membership in the membership list.
2872 for (imm = im6o->im6o_memberships.lh_first;
2873 imm != NULL; imm = imm->i6mm_chain.le_next) {
2874 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2875 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2876 &mreq->ipv6mr_multiaddr))
2880 /* Unable to resolve interface */
2881 error = EADDRNOTAVAIL;
2885 * Give up the multicast address record to which the
2886 * membership points.
2888 LIST_REMOVE(imm, i6mm_chain);
2889 in6_delmulti(imm->i6mm_maddr);
2890 free(imm, M_IPMADDR);
2899 * If all options have default values, no need to keep the mbuf.
2901 if (im6o->im6o_multicast_ifp == NULL &&
2902 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2903 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2904 im6o->im6o_memberships.lh_first == NULL) {
2905 free(*im6op, M_IPMOPTS);
2913 * Return the IP6 multicast options in response to user getsockopt().
2916 ip6_getmoptions(optname, im6o, mp)
2918 struct ip6_moptions *im6o;
2921 u_int *hlim, *loop, *ifindex;
2923 *mp = m_get(M_TRYWAIT, MT_HEADER); /* XXX */
2927 case IPV6_MULTICAST_IF:
2928 ifindex = mtod(*mp, u_int *);
2929 (*mp)->m_len = sizeof(u_int);
2930 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2933 *ifindex = im6o->im6o_multicast_ifp->if_index;
2936 case IPV6_MULTICAST_HOPS:
2937 hlim = mtod(*mp, u_int *);
2938 (*mp)->m_len = sizeof(u_int);
2940 *hlim = ip6_defmcasthlim;
2942 *hlim = im6o->im6o_multicast_hlim;
2945 case IPV6_MULTICAST_LOOP:
2946 loop = mtod(*mp, u_int *);
2947 (*mp)->m_len = sizeof(u_int);
2949 *loop = ip6_defmcasthlim;
2951 *loop = im6o->im6o_multicast_loop;
2955 return (EOPNOTSUPP);
2960 * Discard the IP6 multicast options.
2963 ip6_freemoptions(im6o)
2964 struct ip6_moptions *im6o;
2966 struct in6_multi_mship *imm;
2971 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2972 LIST_REMOVE(imm, i6mm_chain);
2973 if (imm->i6mm_maddr)
2974 in6_delmulti(imm->i6mm_maddr);
2975 free(imm, M_IPMADDR);
2977 free(im6o, M_IPMOPTS);
2981 * Set IPv6 outgoing packet options based on advanced API.
2984 ip6_setpktopts(control, opt, stickyopt, priv, uproto)
2985 struct mbuf *control;
2986 struct ip6_pktopts *opt, *stickyopt;
2989 struct cmsghdr *cm = 0;
2991 if (control == NULL || opt == NULL)
2994 ip6_initpktopts(opt);
2999 * If stickyopt is provided, make a local copy of the options
3000 * for this particular packet, then override them by ancillary
3002 * XXX: copypktopts() does not copy the cached route to a next
3003 * hop (if any). This is not very good in terms of efficiency,
3004 * but we can allow this since this option should be rarely
3007 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
3012 * XXX: Currently, we assume all the optional information is stored
3015 if (control->m_next)
3018 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
3019 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
3022 if (control->m_len < CMSG_LEN(0))
3025 cm = mtod(control, struct cmsghdr *);
3026 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
3028 if (cm->cmsg_level != IPPROTO_IPV6)
3031 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
3032 cm->cmsg_len - CMSG_LEN(0), opt, priv, 0, 1, uproto);
3041 * Set a particular packet option, as a sticky option or an ancillary data
3042 * item. "len" can be 0 only when it's a sticky option.
3043 * We have 4 cases of combination of "sticky" and "cmsg":
3044 * "sticky=0, cmsg=0": impossible
3045 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
3046 * "sticky=1, cmsg=0": RFC3542 socket option
3047 * "sticky=1, cmsg=1": RFC2292 socket option
3050 ip6_setpktopt(optname, buf, len, opt, priv, sticky, cmsg, uproto)
3051 int optname, len, priv, sticky, cmsg, uproto;
3053 struct ip6_pktopts *opt;
3055 int minmtupolicy, preftemp;
3057 if (!sticky && !cmsg) {
3059 printf("ip6_setpktopt: impossible case\n");
3065 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
3066 * not be specified in the context of RFC3542. Conversely,
3067 * RFC3542 types should not be specified in the context of RFC2292.
3071 case IPV6_2292PKTINFO:
3072 case IPV6_2292HOPLIMIT:
3073 case IPV6_2292NEXTHOP:
3074 case IPV6_2292HOPOPTS:
3075 case IPV6_2292DSTOPTS:
3076 case IPV6_2292RTHDR:
3077 case IPV6_2292PKTOPTIONS:
3078 return (ENOPROTOOPT);
3081 if (sticky && cmsg) {
3088 case IPV6_RTHDRDSTOPTS:
3090 case IPV6_USE_MIN_MTU:
3093 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
3094 return (ENOPROTOOPT);
3099 case IPV6_2292PKTINFO:
3102 struct ifnet *ifp = NULL;
3103 struct in6_pktinfo *pktinfo;
3105 if (len != sizeof(struct in6_pktinfo))
3108 pktinfo = (struct in6_pktinfo *)buf;
3111 * An application can clear any sticky IPV6_PKTINFO option by
3112 * doing a "regular" setsockopt with ipi6_addr being
3113 * in6addr_any and ipi6_ifindex being zero.
3114 * [RFC 3542, Section 6]
3116 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
3117 pktinfo->ipi6_ifindex == 0 &&
3118 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3119 ip6_clearpktopts(opt, optname);
3123 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
3124 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3128 /* validate the interface index if specified. */
3129 if (pktinfo->ipi6_ifindex > if_index ||
3130 pktinfo->ipi6_ifindex < 0) {
3133 if (pktinfo->ipi6_ifindex) {
3134 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
3140 * We store the address anyway, and let in6_selectsrc()
3141 * validate the specified address. This is because ipi6_addr
3142 * may not have enough information about its scope zone, and
3143 * we may need additional information (such as outgoing
3144 * interface or the scope zone of a destination address) to
3145 * disambiguate the scope.
3146 * XXX: the delay of the validation may confuse the
3147 * application when it is used as a sticky option.
3149 if (opt->ip6po_pktinfo == NULL) {
3150 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
3151 M_IP6OPT, M_NOWAIT);
3152 if (opt->ip6po_pktinfo == NULL)
3155 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
3159 case IPV6_2292HOPLIMIT:
3165 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3166 * to simplify the ordering among hoplimit options.
3168 if (optname == IPV6_HOPLIMIT && sticky)
3169 return (ENOPROTOOPT);
3171 if (len != sizeof(int))
3174 if (*hlimp < -1 || *hlimp > 255)
3177 opt->ip6po_hlim = *hlimp;
3185 if (len != sizeof(int))
3187 tclass = *(int *)buf;
3188 if (tclass < -1 || tclass > 255)
3191 opt->ip6po_tclass = tclass;
3195 case IPV6_2292NEXTHOP:
3200 if (len == 0) { /* just remove the option */
3201 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3205 /* check if cmsg_len is large enough for sa_len */
3206 if (len < sizeof(struct sockaddr) || len < *buf)
3209 switch (((struct sockaddr *)buf)->sa_family) {
3212 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3215 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3218 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3219 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3222 if ((error = sa6_embedscope(sa6, ip6_use_defzone))
3228 case AF_LINK: /* should eventually be supported */
3230 return (EAFNOSUPPORT);
3233 /* turn off the previous option, then set the new option. */
3234 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3235 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_WAITOK);
3236 bcopy(buf, opt->ip6po_nexthop, *buf);
3239 case IPV6_2292HOPOPTS:
3242 struct ip6_hbh *hbh;
3246 * XXX: We don't allow a non-privileged user to set ANY HbH
3247 * options, since per-option restriction has too much
3254 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3255 break; /* just remove the option */
3258 /* message length validation */
3259 if (len < sizeof(struct ip6_hbh))
3261 hbh = (struct ip6_hbh *)buf;
3262 hbhlen = (hbh->ip6h_len + 1) << 3;
3266 /* turn off the previous option, then set the new option. */
3267 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3268 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_WAITOK);
3269 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3274 case IPV6_2292DSTOPTS:
3276 case IPV6_RTHDRDSTOPTS:
3278 struct ip6_dest *dest, **newdest = NULL;
3281 if (!priv) /* XXX: see the comment for IPV6_HOPOPTS */
3285 ip6_clearpktopts(opt, optname);
3286 break; /* just remove the option */
3289 /* message length validation */
3290 if (len < sizeof(struct ip6_dest))
3292 dest = (struct ip6_dest *)buf;
3293 destlen = (dest->ip6d_len + 1) << 3;
3298 * Determine the position that the destination options header
3299 * should be inserted; before or after the routing header.
3302 case IPV6_2292DSTOPTS:
3304 * The old advacned API is ambiguous on this point.
3305 * Our approach is to determine the position based
3306 * according to the existence of a routing header.
3307 * Note, however, that this depends on the order of the
3308 * extension headers in the ancillary data; the 1st
3309 * part of the destination options header must appear
3310 * before the routing header in the ancillary data,
3312 * RFC3542 solved the ambiguity by introducing
3313 * separate ancillary data or option types.
3315 if (opt->ip6po_rthdr == NULL)
3316 newdest = &opt->ip6po_dest1;
3318 newdest = &opt->ip6po_dest2;
3320 case IPV6_RTHDRDSTOPTS:
3321 newdest = &opt->ip6po_dest1;
3324 newdest = &opt->ip6po_dest2;
3328 /* turn off the previous option, then set the new option. */
3329 ip6_clearpktopts(opt, optname);
3330 *newdest = malloc(destlen, M_IP6OPT, M_WAITOK);
3331 bcopy(dest, *newdest, destlen);
3336 case IPV6_2292RTHDR:
3339 struct ip6_rthdr *rth;
3343 ip6_clearpktopts(opt, IPV6_RTHDR);
3344 break; /* just remove the option */
3347 /* message length validation */
3348 if (len < sizeof(struct ip6_rthdr))
3350 rth = (struct ip6_rthdr *)buf;
3351 rthlen = (rth->ip6r_len + 1) << 3;
3355 switch (rth->ip6r_type) {
3356 case IPV6_RTHDR_TYPE_0:
3357 if (rth->ip6r_len == 0) /* must contain one addr */
3359 if (rth->ip6r_len % 2) /* length must be even */
3361 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3365 return (EINVAL); /* not supported */
3368 /* turn off the previous option */
3369 ip6_clearpktopts(opt, IPV6_RTHDR);
3370 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_WAITOK);
3371 bcopy(rth, opt->ip6po_rthdr, rthlen);
3376 case IPV6_USE_MIN_MTU:
3377 if (len != sizeof(int))
3379 minmtupolicy = *(int *)buf;
3380 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3381 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3382 minmtupolicy != IP6PO_MINMTU_ALL) {
3385 opt->ip6po_minmtu = minmtupolicy;
3389 if (len != sizeof(int))
3392 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3394 * we ignore this option for TCP sockets.
3395 * (RFC3542 leaves this case unspecified.)
3397 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3399 opt->ip6po_flags |= IP6PO_DONTFRAG;
3402 case IPV6_PREFER_TEMPADDR:
3403 if (len != sizeof(int))
3405 preftemp = *(int *)buf;
3406 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3407 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3408 preftemp != IP6PO_TEMPADDR_PREFER) {
3411 opt->ip6po_prefer_tempaddr = preftemp;
3415 return (ENOPROTOOPT);
3416 } /* end of switch */
3422 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3423 * packet to the input queue of a specified interface. Note that this
3424 * calls the output routine of the loopback "driver", but with an interface
3425 * pointer that might NOT be &loif -- easier than replicating that code here.
3428 ip6_mloopback(ifp, m, dst)
3431 struct sockaddr_in6 *dst;
3434 struct ip6_hdr *ip6;
3436 copym = m_copy(m, 0, M_COPYALL);
3441 * Make sure to deep-copy IPv6 header portion in case the data
3442 * is in an mbuf cluster, so that we can safely override the IPv6
3443 * header portion later.
3445 if ((copym->m_flags & M_EXT) != 0 ||
3446 copym->m_len < sizeof(struct ip6_hdr)) {
3447 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3453 if (copym->m_len < sizeof(*ip6)) {
3459 ip6 = mtod(copym, struct ip6_hdr *);
3461 * clear embedded scope identifiers if necessary.
3462 * in6_clearscope will touch the addresses only when necessary.
3464 in6_clearscope(&ip6->ip6_src);
3465 in6_clearscope(&ip6->ip6_dst);
3467 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
3471 * Chop IPv6 header off from the payload.
3474 ip6_splithdr(m, exthdrs)
3476 struct ip6_exthdrs *exthdrs;
3479 struct ip6_hdr *ip6;
3481 ip6 = mtod(m, struct ip6_hdr *);
3482 if (m->m_len > sizeof(*ip6)) {
3483 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3488 M_MOVE_PKTHDR(mh, m);
3489 MH_ALIGN(mh, sizeof(*ip6));
3490 m->m_len -= sizeof(*ip6);
3491 m->m_data += sizeof(*ip6);
3494 m->m_len = sizeof(*ip6);
3495 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3497 exthdrs->ip6e_ip6 = m;
3502 * Compute IPv6 extension header length.
3506 struct in6pcb *in6p;
3510 if (!in6p->in6p_outputopts)
3515 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3517 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3518 if (in6p->in6p_outputopts->ip6po_rthdr)
3519 /* dest1 is valid with rthdr only */
3520 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3521 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3522 len += elen(in6p->in6p_outputopts->ip6po_dest2);