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_IP6MOPTS, "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_UNSPECSRC) == 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 (!PFIL_HOOKED(&inet6_pfil_hook))
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) */
1767 u_char optbuf_storage[MCLBYTES];
1769 struct ip6_pktopts **optp;
1771 /* cannot mix with RFC2292 */
1772 if (OPTBIT(IN6P_RFC2292)) {
1778 * We only ensure valsize is not too large
1779 * here. Further validation will be done
1782 error = sooptcopyin(sopt, optbuf_storage,
1783 sizeof(optbuf_storage), 0);
1786 optlen = sopt->sopt_valsize;
1787 optbuf = optbuf_storage;
1788 optp = &in6p->in6p_outputopts;
1789 error = ip6_pcbopt(optname,
1791 optp, privileged, uproto);
1796 case IPV6_MULTICAST_IF:
1797 case IPV6_MULTICAST_HOPS:
1798 case IPV6_MULTICAST_LOOP:
1799 case IPV6_JOIN_GROUP:
1800 case IPV6_LEAVE_GROUP:
1802 if (sopt->sopt_valsize > MLEN) {
1812 if (sopt->sopt_valsize > MCLBYTES) {
1817 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
1822 if (sopt->sopt_valsize > MLEN) {
1823 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
1824 if ((m->m_flags & M_EXT) == 0) {
1830 m->m_len = sopt->sopt_valsize;
1831 error = sooptcopyin(sopt, mtod(m, char *),
1832 m->m_len, m->m_len);
1837 error = ip6_setmoptions(sopt->sopt_name,
1838 &in6p->in6p_moptions,
1844 case IPV6_PORTRANGE:
1845 error = sooptcopyin(sopt, &optval,
1846 sizeof optval, sizeof optval);
1851 case IPV6_PORTRANGE_DEFAULT:
1852 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1853 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1856 case IPV6_PORTRANGE_HIGH:
1857 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1858 in6p->in6p_flags |= IN6P_HIGHPORT;
1861 case IPV6_PORTRANGE_LOW:
1862 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1863 in6p->in6p_flags |= IN6P_LOWPORT;
1872 #if defined(IPSEC) || defined(FAST_IPSEC)
1873 case IPV6_IPSEC_POLICY:
1879 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1881 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1884 req = mtod(m, caddr_t);
1887 error = ipsec6_set_policy(in6p, optname, req,
1892 #endif /* KAME IPSEC */
1900 struct mbuf **mp = &m;
1902 if (ip6_fw_ctl_ptr == NULL)
1905 if ((error = soopt_getm(sopt, &m)) != 0)
1908 if ((error = soopt_mcopyin(sopt, m)) != 0)
1910 error = (*ip6_fw_ctl_ptr)(optname, mp);
1916 error = ENOPROTOOPT;
1924 case IPV6_2292PKTOPTIONS:
1925 #ifdef IPV6_PKTOPTIONS
1926 case IPV6_PKTOPTIONS:
1929 * RFC3542 (effectively) deprecated the
1930 * semantics of the 2292-style pktoptions.
1931 * Since it was not reliable in nature (i.e.,
1932 * applications had to expect the lack of some
1933 * information after all), it would make sense
1934 * to simplify this part by always returning
1937 sopt->sopt_valsize = 0;
1940 case IPV6_RECVHOPOPTS:
1941 case IPV6_RECVDSTOPTS:
1942 case IPV6_RECVRTHDRDSTOPTS:
1943 case IPV6_UNICAST_HOPS:
1944 case IPV6_RECVPKTINFO:
1945 case IPV6_RECVHOPLIMIT:
1946 case IPV6_RECVRTHDR:
1947 case IPV6_RECVPATHMTU:
1951 case IPV6_PORTRANGE:
1952 case IPV6_RECVTCLASS:
1953 case IPV6_AUTOFLOWLABEL:
1956 case IPV6_RECVHOPOPTS:
1957 optval = OPTBIT(IN6P_HOPOPTS);
1960 case IPV6_RECVDSTOPTS:
1961 optval = OPTBIT(IN6P_DSTOPTS);
1964 case IPV6_RECVRTHDRDSTOPTS:
1965 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1968 case IPV6_UNICAST_HOPS:
1969 optval = in6p->in6p_hops;
1972 case IPV6_RECVPKTINFO:
1973 optval = OPTBIT(IN6P_PKTINFO);
1976 case IPV6_RECVHOPLIMIT:
1977 optval = OPTBIT(IN6P_HOPLIMIT);
1980 case IPV6_RECVRTHDR:
1981 optval = OPTBIT(IN6P_RTHDR);
1984 case IPV6_RECVPATHMTU:
1985 optval = OPTBIT(IN6P_MTU);
1989 optval = OPTBIT(IN6P_FAITH);
1993 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1996 case IPV6_PORTRANGE:
1999 flags = in6p->in6p_flags;
2000 if (flags & IN6P_HIGHPORT)
2001 optval = IPV6_PORTRANGE_HIGH;
2002 else if (flags & IN6P_LOWPORT)
2003 optval = IPV6_PORTRANGE_LOW;
2008 case IPV6_RECVTCLASS:
2009 optval = OPTBIT(IN6P_TCLASS);
2012 case IPV6_AUTOFLOWLABEL:
2013 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2018 error = sooptcopyout(sopt, &optval,
2025 struct ip6_mtuinfo mtuinfo;
2026 struct route_in6 sro;
2028 bzero(&sro, sizeof(sro));
2030 if (!(so->so_state & SS_ISCONNECTED))
2033 * XXX: we dot not consider the case of source
2034 * routing, or optional information to specify
2035 * the outgoing interface.
2037 error = ip6_getpmtu(&sro, NULL, NULL,
2038 &in6p->in6p_faddr, &pmtu, NULL);
2043 if (pmtu > IPV6_MAXPACKET)
2044 pmtu = IPV6_MAXPACKET;
2046 bzero(&mtuinfo, sizeof(mtuinfo));
2047 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2048 optdata = (void *)&mtuinfo;
2049 optdatalen = sizeof(mtuinfo);
2050 error = sooptcopyout(sopt, optdata,
2055 case IPV6_2292PKTINFO:
2056 case IPV6_2292HOPLIMIT:
2057 case IPV6_2292HOPOPTS:
2058 case IPV6_2292RTHDR:
2059 case IPV6_2292DSTOPTS:
2061 case IPV6_2292PKTINFO:
2062 optval = OPTBIT(IN6P_PKTINFO);
2064 case IPV6_2292HOPLIMIT:
2065 optval = OPTBIT(IN6P_HOPLIMIT);
2067 case IPV6_2292HOPOPTS:
2068 optval = OPTBIT(IN6P_HOPOPTS);
2070 case IPV6_2292RTHDR:
2071 optval = OPTBIT(IN6P_RTHDR);
2073 case IPV6_2292DSTOPTS:
2074 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2077 error = sooptcopyout(sopt, &optval,
2084 case IPV6_RTHDRDSTOPTS:
2088 case IPV6_USE_MIN_MTU:
2089 case IPV6_PREFER_TEMPADDR:
2090 error = ip6_getpcbopt(in6p->in6p_outputopts,
2094 case IPV6_MULTICAST_IF:
2095 case IPV6_MULTICAST_HOPS:
2096 case IPV6_MULTICAST_LOOP:
2097 case IPV6_JOIN_GROUP:
2098 case IPV6_LEAVE_GROUP:
2101 error = ip6_getmoptions(sopt->sopt_name,
2102 in6p->in6p_moptions, &m);
2104 error = sooptcopyout(sopt,
2105 mtod(m, char *), m->m_len);
2110 #if defined(IPSEC) || defined(FAST_IPSEC)
2111 case IPV6_IPSEC_POLICY:
2115 struct mbuf *m = NULL;
2116 struct mbuf **mp = &m;
2117 size_t ovalsize = sopt->sopt_valsize;
2118 caddr_t oval = (caddr_t)sopt->sopt_val;
2120 error = soopt_getm(sopt, &m); /* XXX */
2123 error = soopt_mcopyin(sopt, m); /* XXX */
2126 sopt->sopt_valsize = ovalsize;
2127 sopt->sopt_val = oval;
2129 req = mtod(m, caddr_t);
2132 error = ipsec6_get_policy(in6p, req, len, mp);
2134 error = soopt_mcopyout(sopt, m); /* XXX */
2135 if (error == 0 && m)
2139 #endif /* KAME IPSEC */
2144 struct mbuf **mp = &m;
2146 if (ip6_fw_ctl_ptr == NULL)
2150 error = (*ip6_fw_ctl_ptr)(optname, mp);
2152 error = soopt_mcopyout(sopt, m); /* XXX */
2153 if (error == 0 && m)
2159 error = ENOPROTOOPT;
2164 } else { /* level != IPPROTO_IPV6 */
2171 ip6_raw_ctloutput(so, sopt)
2173 struct sockopt *sopt;
2175 int error = 0, optval, optlen;
2176 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2177 struct in6pcb *in6p = sotoin6pcb(so);
2178 int level, op, optname;
2181 level = sopt->sopt_level;
2182 op = sopt->sopt_dir;
2183 optname = sopt->sopt_name;
2184 optlen = sopt->sopt_valsize;
2186 panic("ip6_raw_ctloutput: arg soopt is NULL");
2188 if (level != IPPROTO_IPV6) {
2195 * For ICMPv6 sockets, no modification allowed for checksum
2196 * offset, permit "no change" values to help existing apps.
2198 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2199 * for an ICMPv6 socket will fail."
2200 * The current behavior does not meet RFC3542.
2204 if (optlen != sizeof(int)) {
2208 error = sooptcopyin(sopt, &optval, sizeof(optval),
2212 if ((optval % 2) != 0) {
2213 /* the API assumes even offset values */
2215 } else if (so->so_proto->pr_protocol ==
2217 if (optval != icmp6off)
2220 in6p->in6p_cksum = optval;
2224 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2227 optval = in6p->in6p_cksum;
2229 error = sooptcopyout(sopt, &optval, sizeof(optval));
2239 error = ENOPROTOOPT;
2247 * Set up IP6 options in pcb for insertion in output packets or
2248 * specifying behavior of outgoing packets.
2251 ip6_pcbopts(pktopt, m, so, sopt)
2252 struct ip6_pktopts **pktopt;
2255 struct sockopt *sopt;
2257 struct ip6_pktopts *opt = *pktopt;
2259 struct thread *td = sopt->sopt_td;
2262 /* turn off any old options. */
2265 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2266 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2267 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2268 printf("ip6_pcbopts: all specified options are cleared.\n");
2270 ip6_clearpktopts(opt, -1);
2272 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2275 if (!m || m->m_len == 0) {
2277 * Only turning off any previous options, regardless of
2278 * whether the opt is just created or given.
2280 free(opt, M_IP6OPT);
2284 /* set options specified by user. */
2285 if (td && !suser(td))
2287 if ((error = ip6_setpktopts(m, opt, NULL, priv,
2288 so->so_proto->pr_protocol)) != 0) {
2289 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2290 free(opt, M_IP6OPT);
2298 * initialize ip6_pktopts. beware that there are non-zero default values in
2302 ip6_initpktopts(opt)
2303 struct ip6_pktopts *opt;
2306 bzero(opt, sizeof(*opt));
2307 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2308 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2309 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2310 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2314 ip6_pcbopt(optname, buf, len, pktopt, priv, uproto)
2315 int optname, len, priv;
2317 struct ip6_pktopts **pktopt;
2320 struct ip6_pktopts *opt;
2322 if (*pktopt == NULL) {
2323 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2325 ip6_initpktopts(*pktopt);
2329 return (ip6_setpktopt(optname, buf, len, opt, priv, 1, 0, uproto));
2333 ip6_getpcbopt(pktopt, optname, sopt)
2334 struct ip6_pktopts *pktopt;
2335 struct sockopt *sopt;
2338 void *optdata = NULL;
2340 struct ip6_ext *ip6e;
2342 struct in6_pktinfo null_pktinfo;
2343 int deftclass = 0, on;
2344 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2345 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2349 if (pktopt && pktopt->ip6po_pktinfo)
2350 optdata = (void *)pktopt->ip6po_pktinfo;
2352 /* XXX: we don't have to do this every time... */
2353 bzero(&null_pktinfo, sizeof(null_pktinfo));
2354 optdata = (void *)&null_pktinfo;
2356 optdatalen = sizeof(struct in6_pktinfo);
2359 if (pktopt && pktopt->ip6po_tclass >= 0)
2360 optdata = (void *)&pktopt->ip6po_tclass;
2362 optdata = (void *)&deftclass;
2363 optdatalen = sizeof(int);
2366 if (pktopt && pktopt->ip6po_hbh) {
2367 optdata = (void *)pktopt->ip6po_hbh;
2368 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2369 optdatalen = (ip6e->ip6e_len + 1) << 3;
2373 if (pktopt && pktopt->ip6po_rthdr) {
2374 optdata = (void *)pktopt->ip6po_rthdr;
2375 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2376 optdatalen = (ip6e->ip6e_len + 1) << 3;
2379 case IPV6_RTHDRDSTOPTS:
2380 if (pktopt && pktopt->ip6po_dest1) {
2381 optdata = (void *)pktopt->ip6po_dest1;
2382 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2383 optdatalen = (ip6e->ip6e_len + 1) << 3;
2387 if (pktopt && pktopt->ip6po_dest2) {
2388 optdata = (void *)pktopt->ip6po_dest2;
2389 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2390 optdatalen = (ip6e->ip6e_len + 1) << 3;
2394 if (pktopt && pktopt->ip6po_nexthop) {
2395 optdata = (void *)pktopt->ip6po_nexthop;
2396 optdatalen = pktopt->ip6po_nexthop->sa_len;
2399 case IPV6_USE_MIN_MTU:
2401 optdata = (void *)&pktopt->ip6po_minmtu;
2403 optdata = (void *)&defminmtu;
2404 optdatalen = sizeof(int);
2407 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2411 optdata = (void *)&on;
2412 optdatalen = sizeof(on);
2414 case IPV6_PREFER_TEMPADDR:
2416 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2418 optdata = (void *)&defpreftemp;
2419 optdatalen = sizeof(int);
2421 default: /* should not happen */
2423 panic("ip6_getpcbopt: unexpected option\n");
2425 return (ENOPROTOOPT);
2428 error = sooptcopyout(sopt, optdata, optdatalen);
2434 ip6_clearpktopts(pktopt, optname)
2435 struct ip6_pktopts *pktopt;
2441 if (optname == -1 || optname == IPV6_PKTINFO) {
2442 if (pktopt->ip6po_pktinfo)
2443 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2444 pktopt->ip6po_pktinfo = NULL;
2446 if (optname == -1 || optname == IPV6_HOPLIMIT)
2447 pktopt->ip6po_hlim = -1;
2448 if (optname == -1 || optname == IPV6_TCLASS)
2449 pktopt->ip6po_tclass = -1;
2450 if (optname == -1 || optname == IPV6_NEXTHOP) {
2451 if (pktopt->ip6po_nextroute.ro_rt) {
2452 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2453 pktopt->ip6po_nextroute.ro_rt = NULL;
2455 if (pktopt->ip6po_nexthop)
2456 free(pktopt->ip6po_nexthop, M_IP6OPT);
2457 pktopt->ip6po_nexthop = NULL;
2459 if (optname == -1 || optname == IPV6_HOPOPTS) {
2460 if (pktopt->ip6po_hbh)
2461 free(pktopt->ip6po_hbh, M_IP6OPT);
2462 pktopt->ip6po_hbh = NULL;
2464 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2465 if (pktopt->ip6po_dest1)
2466 free(pktopt->ip6po_dest1, M_IP6OPT);
2467 pktopt->ip6po_dest1 = NULL;
2469 if (optname == -1 || optname == IPV6_RTHDR) {
2470 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2471 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2472 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2473 if (pktopt->ip6po_route.ro_rt) {
2474 RTFREE(pktopt->ip6po_route.ro_rt);
2475 pktopt->ip6po_route.ro_rt = NULL;
2478 if (optname == -1 || optname == IPV6_DSTOPTS) {
2479 if (pktopt->ip6po_dest2)
2480 free(pktopt->ip6po_dest2, M_IP6OPT);
2481 pktopt->ip6po_dest2 = NULL;
2485 #define PKTOPT_EXTHDRCPY(type) \
2488 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2489 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2490 if (dst->type == NULL && canwait == M_NOWAIT)\
2492 bcopy(src->type, dst->type, hlen);\
2494 } while (/*CONSTCOND*/ 0)
2497 copypktopts(dst, src, canwait)
2498 struct ip6_pktopts *dst, *src;
2501 if (dst == NULL || src == NULL) {
2502 printf("ip6_clearpktopts: invalid argument\n");
2506 dst->ip6po_hlim = src->ip6po_hlim;
2507 dst->ip6po_tclass = src->ip6po_tclass;
2508 dst->ip6po_flags = src->ip6po_flags;
2509 if (src->ip6po_pktinfo) {
2510 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2512 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
2514 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2516 if (src->ip6po_nexthop) {
2517 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2519 if (dst->ip6po_nexthop == NULL)
2521 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2522 src->ip6po_nexthop->sa_len);
2524 PKTOPT_EXTHDRCPY(ip6po_hbh);
2525 PKTOPT_EXTHDRCPY(ip6po_dest1);
2526 PKTOPT_EXTHDRCPY(ip6po_dest2);
2527 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2531 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
2532 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
2533 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
2534 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
2535 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
2536 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
2539 #undef PKTOPT_EXTHDRCPY
2541 struct ip6_pktopts *
2542 ip6_copypktopts(src, canwait)
2543 struct ip6_pktopts *src;
2547 struct ip6_pktopts *dst;
2549 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2550 if (dst == NULL && canwait == M_NOWAIT)
2552 ip6_initpktopts(dst);
2554 if ((error = copypktopts(dst, src, canwait)) != 0) {
2555 free(dst, M_IP6OPT);
2563 ip6_freepcbopts(pktopt)
2564 struct ip6_pktopts *pktopt;
2569 ip6_clearpktopts(pktopt, -1);
2571 free(pktopt, M_IP6OPT);
2575 * Set the IP6 multicast options in response to user setsockopt().
2578 ip6_setmoptions(optname, im6op, m)
2580 struct ip6_moptions **im6op;
2584 u_int loop, ifindex;
2585 struct ipv6_mreq *mreq;
2587 struct ip6_moptions *im6o = *im6op;
2588 struct route_in6 ro;
2589 struct in6_multi_mship *imm;
2590 struct thread *td = curthread;
2594 * No multicast option buffer attached to the pcb;
2595 * allocate one and initialize to default values.
2597 im6o = (struct ip6_moptions *)
2598 malloc(sizeof(*im6o), M_IP6MOPTS, M_WAITOK);
2603 im6o->im6o_multicast_ifp = NULL;
2604 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2605 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2606 LIST_INIT(&im6o->im6o_memberships);
2611 case IPV6_MULTICAST_IF:
2613 * Select the interface for outgoing multicast packets.
2615 if (m == NULL || m->m_len != sizeof(u_int)) {
2619 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2620 if (ifindex < 0 || if_index < ifindex) {
2621 error = ENXIO; /* XXX EINVAL? */
2624 ifp = ifnet_byindex(ifindex);
2625 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2626 error = EADDRNOTAVAIL;
2629 im6o->im6o_multicast_ifp = ifp;
2632 case IPV6_MULTICAST_HOPS:
2635 * Set the IP6 hoplimit for outgoing multicast packets.
2638 if (m == NULL || m->m_len != sizeof(int)) {
2642 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2643 if (optval < -1 || optval >= 256)
2645 else if (optval == -1)
2646 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2648 im6o->im6o_multicast_hlim = optval;
2652 case IPV6_MULTICAST_LOOP:
2654 * Set the loopback flag for outgoing multicast packets.
2655 * Must be zero or one.
2657 if (m == NULL || m->m_len != sizeof(u_int)) {
2661 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2666 im6o->im6o_multicast_loop = loop;
2669 case IPV6_JOIN_GROUP:
2671 * Add a multicast group membership.
2672 * Group must be a valid IP6 multicast address.
2674 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2678 mreq = mtod(m, struct ipv6_mreq *);
2680 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2682 * We use the unspecified address to specify to accept
2683 * all multicast addresses. Only super user is allowed
2690 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2696 * If no interface was explicitly specified, choose an
2697 * appropriate one according to the given multicast address.
2699 if (mreq->ipv6mr_interface == 0) {
2700 struct sockaddr_in6 *dst;
2703 * Look up the routing table for the
2704 * address, and choose the outgoing interface.
2705 * XXX: is it a good approach?
2708 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2709 bzero(dst, sizeof(*dst));
2710 dst->sin6_family = AF_INET6;
2711 dst->sin6_len = sizeof(*dst);
2712 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2713 rtalloc((struct route *)&ro);
2714 if (ro.ro_rt == NULL) {
2715 error = EADDRNOTAVAIL;
2718 ifp = ro.ro_rt->rt_ifp;
2722 * If the interface is specified, validate it.
2724 if (mreq->ipv6mr_interface < 0 ||
2725 if_index < mreq->ipv6mr_interface) {
2726 error = ENXIO; /* XXX EINVAL? */
2729 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2731 error = ENXIO; /* XXX EINVAL? */
2737 * See if we found an interface, and confirm that it
2738 * supports multicast
2740 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2741 error = EADDRNOTAVAIL;
2745 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2746 error = EADDRNOTAVAIL; /* XXX: should not happen */
2751 * See if the membership already exists.
2753 for (imm = im6o->im6o_memberships.lh_first;
2754 imm != NULL; imm = imm->i6mm_chain.le_next)
2755 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2756 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2757 &mreq->ipv6mr_multiaddr))
2764 * Everything looks good; add a new record to the multicast
2765 * address list for the given interface.
2767 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error, 0);
2770 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2773 case IPV6_LEAVE_GROUP:
2775 * Drop a multicast group membership.
2776 * Group must be a valid IP6 multicast address.
2778 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2782 mreq = mtod(m, struct ipv6_mreq *);
2785 * If an interface address was specified, get a pointer
2786 * to its ifnet structure.
2788 if (mreq->ipv6mr_interface < 0 ||
2789 if_index < mreq->ipv6mr_interface) {
2790 error = ENXIO; /* XXX EINVAL? */
2793 if (mreq->ipv6mr_interface == 0)
2796 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2798 /* Fill in the scope zone ID */
2800 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2801 /* XXX: should not happen */
2802 error = EADDRNOTAVAIL;
2805 } else if (mreq->ipv6mr_interface != 0) {
2807 * This case happens when the (positive) index is in
2808 * the valid range, but the corresponding interface has
2809 * been detached dynamically (XXX).
2811 error = EADDRNOTAVAIL;
2813 } else { /* ipv6mr_interface == 0 */
2814 struct sockaddr_in6 sa6_mc;
2817 * The API spec says as follows:
2818 * If the interface index is specified as 0, the
2819 * system may choose a multicast group membership to
2820 * drop by matching the multicast address only.
2821 * On the other hand, we cannot disambiguate the scope
2822 * zone unless an interface is provided. Thus, we
2823 * check if there's ambiguity with the default scope
2824 * zone as the last resort.
2826 bzero(&sa6_mc, sizeof(sa6_mc));
2827 sa6_mc.sin6_family = AF_INET6;
2828 sa6_mc.sin6_len = sizeof(sa6_mc);
2829 sa6_mc.sin6_addr = mreq->ipv6mr_multiaddr;
2830 error = sa6_embedscope(&sa6_mc, ip6_use_defzone);
2833 mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr;
2837 * Find the membership in the membership list.
2839 for (imm = im6o->im6o_memberships.lh_first;
2840 imm != NULL; imm = imm->i6mm_chain.le_next) {
2841 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2842 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2843 &mreq->ipv6mr_multiaddr))
2847 /* Unable to resolve interface */
2848 error = EADDRNOTAVAIL;
2852 * Give up the multicast address record to which the
2853 * membership points.
2855 LIST_REMOVE(imm, i6mm_chain);
2856 in6_delmulti(imm->i6mm_maddr);
2857 free(imm, M_IP6MADDR);
2866 * If all options have default values, no need to keep the mbuf.
2868 if (im6o->im6o_multicast_ifp == NULL &&
2869 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2870 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2871 im6o->im6o_memberships.lh_first == NULL) {
2872 free(*im6op, M_IP6MOPTS);
2880 * Return the IP6 multicast options in response to user getsockopt().
2883 ip6_getmoptions(optname, im6o, mp)
2885 struct ip6_moptions *im6o;
2888 u_int *hlim, *loop, *ifindex;
2890 *mp = m_get(M_TRYWAIT, MT_HEADER); /* XXX */
2894 case IPV6_MULTICAST_IF:
2895 ifindex = mtod(*mp, u_int *);
2896 (*mp)->m_len = sizeof(u_int);
2897 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2900 *ifindex = im6o->im6o_multicast_ifp->if_index;
2903 case IPV6_MULTICAST_HOPS:
2904 hlim = mtod(*mp, u_int *);
2905 (*mp)->m_len = sizeof(u_int);
2907 *hlim = ip6_defmcasthlim;
2909 *hlim = im6o->im6o_multicast_hlim;
2912 case IPV6_MULTICAST_LOOP:
2913 loop = mtod(*mp, u_int *);
2914 (*mp)->m_len = sizeof(u_int);
2916 *loop = ip6_defmcasthlim;
2918 *loop = im6o->im6o_multicast_loop;
2922 return (EOPNOTSUPP);
2927 * Discard the IP6 multicast options.
2930 ip6_freemoptions(im6o)
2931 struct ip6_moptions *im6o;
2933 struct in6_multi_mship *imm;
2938 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2939 LIST_REMOVE(imm, i6mm_chain);
2940 if (imm->i6mm_maddr)
2941 in6_delmulti(imm->i6mm_maddr);
2942 free(imm, M_IP6MADDR);
2944 free(im6o, M_IP6MOPTS);
2948 * Set IPv6 outgoing packet options based on advanced API.
2951 ip6_setpktopts(control, opt, stickyopt, priv, uproto)
2952 struct mbuf *control;
2953 struct ip6_pktopts *opt, *stickyopt;
2956 struct cmsghdr *cm = 0;
2958 if (control == NULL || opt == NULL)
2961 ip6_initpktopts(opt);
2966 * If stickyopt is provided, make a local copy of the options
2967 * for this particular packet, then override them by ancillary
2969 * XXX: copypktopts() does not copy the cached route to a next
2970 * hop (if any). This is not very good in terms of efficiency,
2971 * but we can allow this since this option should be rarely
2974 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2979 * XXX: Currently, we assume all the optional information is stored
2982 if (control->m_next)
2985 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2986 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2989 if (control->m_len < CMSG_LEN(0))
2992 cm = mtod(control, struct cmsghdr *);
2993 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2995 if (cm->cmsg_level != IPPROTO_IPV6)
2998 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2999 cm->cmsg_len - CMSG_LEN(0), opt, priv, 0, 1, uproto);
3008 * Set a particular packet option, as a sticky option or an ancillary data
3009 * item. "len" can be 0 only when it's a sticky option.
3010 * We have 4 cases of combination of "sticky" and "cmsg":
3011 * "sticky=0, cmsg=0": impossible
3012 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
3013 * "sticky=1, cmsg=0": RFC3542 socket option
3014 * "sticky=1, cmsg=1": RFC2292 socket option
3017 ip6_setpktopt(optname, buf, len, opt, priv, sticky, cmsg, uproto)
3018 int optname, len, priv, sticky, cmsg, uproto;
3020 struct ip6_pktopts *opt;
3022 int minmtupolicy, preftemp;
3024 if (!sticky && !cmsg) {
3026 printf("ip6_setpktopt: impossible case\n");
3032 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
3033 * not be specified in the context of RFC3542. Conversely,
3034 * RFC3542 types should not be specified in the context of RFC2292.
3038 case IPV6_2292PKTINFO:
3039 case IPV6_2292HOPLIMIT:
3040 case IPV6_2292NEXTHOP:
3041 case IPV6_2292HOPOPTS:
3042 case IPV6_2292DSTOPTS:
3043 case IPV6_2292RTHDR:
3044 case IPV6_2292PKTOPTIONS:
3045 return (ENOPROTOOPT);
3048 if (sticky && cmsg) {
3055 case IPV6_RTHDRDSTOPTS:
3057 case IPV6_USE_MIN_MTU:
3060 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
3061 return (ENOPROTOOPT);
3066 case IPV6_2292PKTINFO:
3069 struct ifnet *ifp = NULL;
3070 struct in6_pktinfo *pktinfo;
3072 if (len != sizeof(struct in6_pktinfo))
3075 pktinfo = (struct in6_pktinfo *)buf;
3078 * An application can clear any sticky IPV6_PKTINFO option by
3079 * doing a "regular" setsockopt with ipi6_addr being
3080 * in6addr_any and ipi6_ifindex being zero.
3081 * [RFC 3542, Section 6]
3083 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
3084 pktinfo->ipi6_ifindex == 0 &&
3085 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3086 ip6_clearpktopts(opt, optname);
3090 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
3091 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3095 /* validate the interface index if specified. */
3096 if (pktinfo->ipi6_ifindex > if_index ||
3097 pktinfo->ipi6_ifindex < 0) {
3100 if (pktinfo->ipi6_ifindex) {
3101 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
3107 * We store the address anyway, and let in6_selectsrc()
3108 * validate the specified address. This is because ipi6_addr
3109 * may not have enough information about its scope zone, and
3110 * we may need additional information (such as outgoing
3111 * interface or the scope zone of a destination address) to
3112 * disambiguate the scope.
3113 * XXX: the delay of the validation may confuse the
3114 * application when it is used as a sticky option.
3116 if (opt->ip6po_pktinfo == NULL) {
3117 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
3118 M_IP6OPT, M_NOWAIT);
3119 if (opt->ip6po_pktinfo == NULL)
3122 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
3126 case IPV6_2292HOPLIMIT:
3132 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3133 * to simplify the ordering among hoplimit options.
3135 if (optname == IPV6_HOPLIMIT && sticky)
3136 return (ENOPROTOOPT);
3138 if (len != sizeof(int))
3141 if (*hlimp < -1 || *hlimp > 255)
3144 opt->ip6po_hlim = *hlimp;
3152 if (len != sizeof(int))
3154 tclass = *(int *)buf;
3155 if (tclass < -1 || tclass > 255)
3158 opt->ip6po_tclass = tclass;
3162 case IPV6_2292NEXTHOP:
3167 if (len == 0) { /* just remove the option */
3168 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3172 /* check if cmsg_len is large enough for sa_len */
3173 if (len < sizeof(struct sockaddr) || len < *buf)
3176 switch (((struct sockaddr *)buf)->sa_family) {
3179 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3182 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3185 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3186 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3189 if ((error = sa6_embedscope(sa6, ip6_use_defzone))
3195 case AF_LINK: /* should eventually be supported */
3197 return (EAFNOSUPPORT);
3200 /* turn off the previous option, then set the new option. */
3201 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3202 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3203 if (opt->ip6po_nexthop == NULL)
3205 bcopy(buf, opt->ip6po_nexthop, *buf);
3208 case IPV6_2292HOPOPTS:
3211 struct ip6_hbh *hbh;
3215 * XXX: We don't allow a non-privileged user to set ANY HbH
3216 * options, since per-option restriction has too much
3223 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3224 break; /* just remove the option */
3227 /* message length validation */
3228 if (len < sizeof(struct ip6_hbh))
3230 hbh = (struct ip6_hbh *)buf;
3231 hbhlen = (hbh->ip6h_len + 1) << 3;
3235 /* turn off the previous option, then set the new option. */
3236 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3237 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3238 if (opt->ip6po_hbh == NULL)
3240 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3245 case IPV6_2292DSTOPTS:
3247 case IPV6_RTHDRDSTOPTS:
3249 struct ip6_dest *dest, **newdest = NULL;
3252 if (!priv) /* XXX: see the comment for IPV6_HOPOPTS */
3256 ip6_clearpktopts(opt, optname);
3257 break; /* just remove the option */
3260 /* message length validation */
3261 if (len < sizeof(struct ip6_dest))
3263 dest = (struct ip6_dest *)buf;
3264 destlen = (dest->ip6d_len + 1) << 3;
3269 * Determine the position that the destination options header
3270 * should be inserted; before or after the routing header.
3273 case IPV6_2292DSTOPTS:
3275 * The old advacned API is ambiguous on this point.
3276 * Our approach is to determine the position based
3277 * according to the existence of a routing header.
3278 * Note, however, that this depends on the order of the
3279 * extension headers in the ancillary data; the 1st
3280 * part of the destination options header must appear
3281 * before the routing header in the ancillary data,
3283 * RFC3542 solved the ambiguity by introducing
3284 * separate ancillary data or option types.
3286 if (opt->ip6po_rthdr == NULL)
3287 newdest = &opt->ip6po_dest1;
3289 newdest = &opt->ip6po_dest2;
3291 case IPV6_RTHDRDSTOPTS:
3292 newdest = &opt->ip6po_dest1;
3295 newdest = &opt->ip6po_dest2;
3299 /* turn off the previous option, then set the new option. */
3300 ip6_clearpktopts(opt, optname);
3301 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3302 if (*newdest == NULL)
3304 bcopy(dest, *newdest, destlen);
3309 case IPV6_2292RTHDR:
3312 struct ip6_rthdr *rth;
3316 ip6_clearpktopts(opt, IPV6_RTHDR);
3317 break; /* just remove the option */
3320 /* message length validation */
3321 if (len < sizeof(struct ip6_rthdr))
3323 rth = (struct ip6_rthdr *)buf;
3324 rthlen = (rth->ip6r_len + 1) << 3;
3328 switch (rth->ip6r_type) {
3329 case IPV6_RTHDR_TYPE_0:
3330 if (rth->ip6r_len == 0) /* must contain one addr */
3332 if (rth->ip6r_len % 2) /* length must be even */
3334 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3338 return (EINVAL); /* not supported */
3341 /* turn off the previous option */
3342 ip6_clearpktopts(opt, IPV6_RTHDR);
3343 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3344 if (opt->ip6po_rthdr == NULL)
3346 bcopy(rth, opt->ip6po_rthdr, rthlen);
3351 case IPV6_USE_MIN_MTU:
3352 if (len != sizeof(int))
3354 minmtupolicy = *(int *)buf;
3355 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3356 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3357 minmtupolicy != IP6PO_MINMTU_ALL) {
3360 opt->ip6po_minmtu = minmtupolicy;
3364 if (len != sizeof(int))
3367 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3369 * we ignore this option for TCP sockets.
3370 * (RFC3542 leaves this case unspecified.)
3372 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3374 opt->ip6po_flags |= IP6PO_DONTFRAG;
3377 case IPV6_PREFER_TEMPADDR:
3378 if (len != sizeof(int))
3380 preftemp = *(int *)buf;
3381 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3382 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3383 preftemp != IP6PO_TEMPADDR_PREFER) {
3386 opt->ip6po_prefer_tempaddr = preftemp;
3390 return (ENOPROTOOPT);
3391 } /* end of switch */
3397 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3398 * packet to the input queue of a specified interface. Note that this
3399 * calls the output routine of the loopback "driver", but with an interface
3400 * pointer that might NOT be &loif -- easier than replicating that code here.
3403 ip6_mloopback(ifp, m, dst)
3406 struct sockaddr_in6 *dst;
3409 struct ip6_hdr *ip6;
3411 copym = m_copy(m, 0, M_COPYALL);
3416 * Make sure to deep-copy IPv6 header portion in case the data
3417 * is in an mbuf cluster, so that we can safely override the IPv6
3418 * header portion later.
3420 if ((copym->m_flags & M_EXT) != 0 ||
3421 copym->m_len < sizeof(struct ip6_hdr)) {
3422 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3428 if (copym->m_len < sizeof(*ip6)) {
3434 ip6 = mtod(copym, struct ip6_hdr *);
3436 * clear embedded scope identifiers if necessary.
3437 * in6_clearscope will touch the addresses only when necessary.
3439 in6_clearscope(&ip6->ip6_src);
3440 in6_clearscope(&ip6->ip6_dst);
3442 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
3446 * Chop IPv6 header off from the payload.
3449 ip6_splithdr(m, exthdrs)
3451 struct ip6_exthdrs *exthdrs;
3454 struct ip6_hdr *ip6;
3456 ip6 = mtod(m, struct ip6_hdr *);
3457 if (m->m_len > sizeof(*ip6)) {
3458 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3463 M_MOVE_PKTHDR(mh, m);
3464 MH_ALIGN(mh, sizeof(*ip6));
3465 m->m_len -= sizeof(*ip6);
3466 m->m_data += sizeof(*ip6);
3469 m->m_len = sizeof(*ip6);
3470 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3472 exthdrs->ip6e_ip6 = m;
3477 * Compute IPv6 extension header length.
3481 struct in6pcb *in6p;
3485 if (!in6p->in6p_outputopts)
3490 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3492 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3493 if (in6p->in6p_outputopts->ip6po_rthdr)
3494 /* dest1 is valid with rthdr only */
3495 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3496 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3497 len += elen(in6p->in6p_outputopts->ip6po_dest2);