1 /* $NetBSD: ip6_output.c,v 1.138 2009/05/06 21:41:59 elad Exp $ */
2 /* $KAME: ip6_output.c,v 1.172 2001/03/25 09:55:56 itojun 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 * 3. 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 <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: ip6_output.c,v 1.138 2009/05/06 21:41:59 elad Exp $");
68 #include "opt_inet6.h"
69 #include "opt_ipsec.h"
70 #include "opt_pfil_hooks.h"
72 #include <sys/param.h>
73 #include <sys/malloc.h>
75 #include <sys/errno.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/systm.h>
81 #include <sys/kauth.h>
84 #include <net/route.h>
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <netinet/ip6.h>
92 #include <netinet/icmp6.h>
93 #include <netinet/in_offload.h>
94 #include <netinet6/in6_offload.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet6/ip6_private.h>
97 #include <netinet6/in6_pcb.h>
98 #include <netinet6/nd6.h>
99 #include <netinet6/ip6protosw.h>
100 #include <netinet6/scope6_var.h>
103 #include <netinet6/ipsec.h>
104 #include <netinet6/ipsec_private.h>
105 #include <netkey/key.h>
109 #include <netipsec/ipsec.h>
110 #include <netipsec/ipsec6.h>
111 #include <netipsec/key.h>
112 #include <netipsec/xform.h>
116 #include <net/net_osdep.h>
119 extern struct pfil_head inet6_pfil_hook
; /* XXX */
123 struct mbuf
*ip6e_ip6
;
124 struct mbuf
*ip6e_hbh
;
125 struct mbuf
*ip6e_dest1
;
126 struct mbuf
*ip6e_rthdr
;
127 struct mbuf
*ip6e_dest2
;
130 static int ip6_pcbopt(int, u_char
*, int, struct ip6_pktopts
**,
132 static int ip6_getpcbopt(struct ip6_pktopts
*, int, struct sockopt
*);
133 static int ip6_setpktopt(int, u_char
*, int, struct ip6_pktopts
*, kauth_cred_t
,
135 static int ip6_setmoptions(const struct sockopt
*, struct ip6_moptions
**);
136 static int ip6_getmoptions(struct sockopt
*, struct ip6_moptions
*);
137 static int ip6_copyexthdr(struct mbuf
**, void *, int);
138 static int ip6_insertfraghdr(struct mbuf
*, struct mbuf
*, int,
140 static int ip6_insert_jumboopt(struct ip6_exthdrs
*, u_int32_t
);
141 static int ip6_splithdr(struct mbuf
*, struct ip6_exthdrs
*);
142 static int ip6_getpmtu(struct route
*, struct route
*, struct ifnet
*,
143 const struct in6_addr
*, u_long
*, int *);
144 static int copypktopts(struct ip6_pktopts
*, struct ip6_pktopts
*, int);
147 static int ip6_pcbopts(struct ip6_pktopts
**, struct socket
*, struct sockopt
*);
150 #define IN6_NEED_CHECKSUM(ifp, csum_flags) \
151 (__predict_true(((ifp)->if_flags & IFF_LOOPBACK) == 0 || \
152 (((csum_flags) & M_CSUM_UDPv6) != 0 && udp_do_loopback_cksum) || \
153 (((csum_flags) & M_CSUM_TCPv6) != 0 && tcp_do_loopback_cksum)))
156 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
157 * header (with pri, len, nxt, hlim, src, dst).
158 * This function may modify ver and hlim only.
159 * The mbuf chain containing the packet will be freed.
160 * The mbuf opt, if present, will not be freed.
162 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
163 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
164 * which is rt_rmx.rmx_mtu.
169 struct ip6_pktopts
*opt
,
172 struct ip6_moptions
*im6o
,
174 struct ifnet
**ifpp
/* XXX: just for statistics */
177 struct ip6_hdr
*ip6
, *mhip6
;
178 struct ifnet
*ifp
, *origifp
;
180 int hlen
, tlen
, len
, off
;
182 struct route ip6route
;
183 struct rtentry
*rt
= NULL
;
184 const struct sockaddr_in6
*dst
= NULL
;
185 struct sockaddr_in6 src_sa
, dst_sa
;
187 struct in6_ifaddr
*ia
= NULL
;
189 int alwaysfrag
, dontfrag
;
190 u_int32_t optlen
= 0, plen
= 0, unfragpartlen
= 0;
191 struct ip6_exthdrs exthdrs
;
192 struct in6_addr finaldst
, src0
, dst0
;
194 struct route
*ro_pmtu
= NULL
;
198 int needipsectun
= 0;
199 struct secpolicy
*sp
= NULL
;
201 ip6
= mtod(m
, struct ip6_hdr
*);
204 struct secpolicy
*sp
= NULL
;
208 memset(&ip6route
, 0, sizeof(ip6route
));
211 if ((m
->m_flags
& M_PKTHDR
) == 0)
212 panic("ip6_output: no HDR");
214 if ((m
->m_pkthdr
.csum_flags
&
215 (M_CSUM_TCPv4
|M_CSUM_UDPv4
|M_CSUM_TSOv4
)) != 0) {
216 panic("ip6_output: IPv4 checksum offload flags: %d",
217 m
->m_pkthdr
.csum_flags
);
220 if ((m
->m_pkthdr
.csum_flags
& (M_CSUM_TCPv6
|M_CSUM_UDPv6
)) ==
221 (M_CSUM_TCPv6
|M_CSUM_UDPv6
)) {
222 panic("ip6_output: conflicting checksum offload flags: %d",
223 m
->m_pkthdr
.csum_flags
);
227 M_CSUM_DATA_IPv6_HL_SET(m
->m_pkthdr
.csum_data
, sizeof(struct ip6_hdr
));
229 #define MAKE_EXTHDR(hp, mp) \
232 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
233 error = ip6_copyexthdr((mp), (void *)(hp), \
234 ((eh)->ip6e_len + 1) << 3); \
238 } while (/*CONSTCOND*/ 0)
240 memset(&exthdrs
, 0, sizeof(exthdrs
));
242 /* Hop-by-Hop options header */
243 MAKE_EXTHDR(opt
->ip6po_hbh
, &exthdrs
.ip6e_hbh
);
244 /* Destination options header(1st part) */
245 MAKE_EXTHDR(opt
->ip6po_dest1
, &exthdrs
.ip6e_dest1
);
247 MAKE_EXTHDR(opt
->ip6po_rthdr
, &exthdrs
.ip6e_rthdr
);
248 /* Destination options header(2nd part) */
249 MAKE_EXTHDR(opt
->ip6po_dest2
, &exthdrs
.ip6e_dest2
);
253 if ((flags
& IPV6_FORWARDING
) != 0) {
255 goto skippolicycheck
;
258 /* get a security policy for this packet */
260 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, 0, &error
);
262 if (IPSEC_PCB_SKIP_IPSEC(sotoinpcb_hdr(so
)->inph_sp
,
263 IPSEC_DIR_OUTBOUND
)) {
265 goto skippolicycheck
;
267 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, so
, &error
);
271 IPSEC6_STATINC(IPSEC_STAT_OUT_INVAL
);
278 switch (sp
->policy
) {
279 case IPSEC_POLICY_DISCARD
:
281 * This packet is just discarded.
283 IPSEC6_STATINC(IPSEC_STAT_OUT_POLVIO
);
286 case IPSEC_POLICY_BYPASS
:
287 case IPSEC_POLICY_NONE
:
288 /* no need to do IPsec. */
292 case IPSEC_POLICY_IPSEC
:
293 if (sp
->req
== NULL
) {
294 /* XXX should be panic ? */
295 printf("ip6_output: No IPsec request specified.\n");
302 case IPSEC_POLICY_ENTRUST
:
304 printf("ip6_output: Invalid policy found. %d\n", sp
->policy
);
311 * Calculate the total length of the extension header chain.
312 * Keep the length of the unfragmentable part for fragmentation.
315 if (exthdrs
.ip6e_hbh
) optlen
+= exthdrs
.ip6e_hbh
->m_len
;
316 if (exthdrs
.ip6e_dest1
) optlen
+= exthdrs
.ip6e_dest1
->m_len
;
317 if (exthdrs
.ip6e_rthdr
) optlen
+= exthdrs
.ip6e_rthdr
->m_len
;
318 unfragpartlen
= optlen
+ sizeof(struct ip6_hdr
);
319 /* NOTE: we don't add AH/ESP length here. do that later. */
320 if (exthdrs
.ip6e_dest2
) optlen
+= exthdrs
.ip6e_dest2
->m_len
;
323 /* Check the security policy (SP) for the packet */
325 /* XXX For moment, we doesn't support packet with extented action */
329 sp
= ipsec6_check_policy(m
,so
,flags
,&needipsec
,&error
);
332 * Hack: -EINVAL is used to signal that a packet
333 * should be silently discarded. This is typically
334 * because we asked key management for an SA and
335 * it was delayed (e.g. kicked up to IKE).
337 if (error
== -EINVAL
)
341 #endif /* FAST_IPSEC */
345 (m
->m_pkthdr
.csum_flags
& (M_CSUM_UDPv6
|M_CSUM_TCPv6
)) != 0) {
346 in6_delayed_cksum(m
);
347 m
->m_pkthdr
.csum_flags
&= ~(M_CSUM_UDPv6
|M_CSUM_TCPv6
);
352 * If we need IPsec, or there is at least one extension header,
353 * separate IP6 header from the payload.
355 if ((needipsec
|| optlen
) && !hdrsplit
) {
356 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
360 m
= exthdrs
.ip6e_ip6
;
365 ip6
= mtod(m
, struct ip6_hdr
*);
367 /* adjust mbuf packet header length */
368 m
->m_pkthdr
.len
+= optlen
;
369 plen
= m
->m_pkthdr
.len
- sizeof(*ip6
);
371 /* If this is a jumbo payload, insert a jumbo payload option. */
372 if (plen
> IPV6_MAXPACKET
) {
374 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
378 m
= exthdrs
.ip6e_ip6
;
382 ip6
= mtod(m
, struct ip6_hdr
*);
383 if ((error
= ip6_insert_jumboopt(&exthdrs
, plen
)) != 0)
385 optlen
+= 8; /* XXX JUMBOOPTLEN */
388 ip6
->ip6_plen
= htons(plen
);
391 * Concatenate headers and fill in next header fields.
392 * Here we have, on "m"
394 * and we insert headers accordingly. Finally, we should be getting:
395 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
397 * during the header composing process, "m" points to IPv6 header.
398 * "mprev" points to an extension header prior to esp.
401 u_char
*nexthdrp
= &ip6
->ip6_nxt
;
402 struct mbuf
*mprev
= m
;
405 * we treat dest2 specially. this makes IPsec processing
406 * much easier. the goal here is to make mprev point the
407 * mbuf prior to dest2.
409 * result: IPv6 dest2 payload
410 * m and mprev will point to IPv6 header.
412 if (exthdrs
.ip6e_dest2
) {
414 panic("assumption failed: hdr not split");
415 exthdrs
.ip6e_dest2
->m_next
= m
->m_next
;
416 m
->m_next
= exthdrs
.ip6e_dest2
;
417 *mtod(exthdrs
.ip6e_dest2
, u_char
*) = ip6
->ip6_nxt
;
418 ip6
->ip6_nxt
= IPPROTO_DSTOPTS
;
421 #define MAKE_CHAIN(m, mp, p, i)\
425 panic("assumption failed: hdr not split"); \
426 *mtod((m), u_char *) = *(p);\
428 p = mtod((m), u_char *);\
429 (m)->m_next = (mp)->m_next;\
433 } while (/*CONSTCOND*/ 0)
435 * result: IPv6 hbh dest1 rthdr dest2 payload
436 * m will point to IPv6 header. mprev will point to the
437 * extension header prior to dest2 (rthdr in the above case).
439 MAKE_CHAIN(exthdrs
.ip6e_hbh
, mprev
, nexthdrp
, IPPROTO_HOPOPTS
);
440 MAKE_CHAIN(exthdrs
.ip6e_dest1
, mprev
, nexthdrp
,
442 MAKE_CHAIN(exthdrs
.ip6e_rthdr
, mprev
, nexthdrp
,
445 M_CSUM_DATA_IPv6_HL_SET(m
->m_pkthdr
.csum_data
,
446 sizeof(struct ip6_hdr
) + optlen
);
453 * pointers after IPsec headers are not valid any more.
454 * other pointers need a great care too.
455 * (IPsec routines should not mangle mbufs prior to AH/ESP)
457 exthdrs
.ip6e_dest2
= NULL
;
460 struct ip6_rthdr
*rh
= NULL
;
462 struct ipsec_output_state state
;
464 if (exthdrs
.ip6e_rthdr
) {
465 rh
= mtod(exthdrs
.ip6e_rthdr
, struct ip6_rthdr
*);
466 segleft_org
= rh
->ip6r_segleft
;
467 rh
->ip6r_segleft
= 0;
470 memset(&state
, 0, sizeof(state
));
472 error
= ipsec6_output_trans(&state
, nexthdrp
, mprev
, sp
, flags
,
476 rh
= mtod(exthdrs
.ip6e_rthdr
, struct ip6_rthdr
*);
477 /* mbuf is already reclaimed in ipsec6_output_trans. */
487 printf("ip6_output (ipsec): error code %d\n", error
);
490 /* don't show these error codes to the user */
496 if (exthdrs
.ip6e_rthdr
) {
497 /* ah6_output doesn't modify mbuf chain */
498 rh
->ip6r_segleft
= segleft_org
;
506 * If there is a routing header, replace destination address field
507 * with the first hop of the routing header.
509 if (exthdrs
.ip6e_rthdr
) {
510 struct ip6_rthdr
*rh
;
511 struct ip6_rthdr0
*rh0
;
512 struct in6_addr
*addr
;
513 struct sockaddr_in6 sa
;
515 rh
= (struct ip6_rthdr
*)(mtod(exthdrs
.ip6e_rthdr
,
516 struct ip6_rthdr
*));
517 finaldst
= ip6
->ip6_dst
;
518 switch (rh
->ip6r_type
) {
519 case IPV6_RTHDR_TYPE_0
:
520 rh0
= (struct ip6_rthdr0
*)rh
;
521 addr
= (struct in6_addr
*)(rh0
+ 1);
524 * construct a sockaddr_in6 form of
527 * XXX: we may not have enough
528 * information about its scope zone;
529 * there is no standard API to pass
530 * the information from the
533 sockaddr_in6_init(&sa
, addr
, 0, 0, 0);
534 if ((error
= sa6_embedscope(&sa
,
535 ip6_use_defzone
)) != 0) {
538 ip6
->ip6_dst
= sa
.sin6_addr
;
539 (void)memmove(&addr
[0], &addr
[1],
540 sizeof(struct in6_addr
) *
541 (rh0
->ip6r0_segleft
- 1));
542 addr
[rh0
->ip6r0_segleft
- 1] = finaldst
;
544 in6_clearscope(addr
+ rh0
->ip6r0_segleft
- 1);
546 default: /* is it possible? */
552 /* Source address validation */
553 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) &&
554 (flags
& IPV6_UNSPECSRC
) == 0) {
556 IP6_STATINC(IP6_STAT_BADSCOPE
);
559 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
)) {
561 IP6_STATINC(IP6_STAT_BADSCOPE
);
565 IP6_STATINC(IP6_STAT_LOCALOUT
);
570 /* initialize cached route */
575 if (opt
&& opt
->ip6po_rthdr
)
576 ro
= &opt
->ip6po_route
;
579 * if specified, try to fill in the traffic class field.
580 * do not override if a non-zero value is already set.
581 * we check the diffserv field and the ecn field separately.
583 if (opt
&& opt
->ip6po_tclass
>= 0) {
586 if ((ip6
->ip6_flow
& htonl(0xfc << 20)) == 0)
588 if ((ip6
->ip6_flow
& htonl(0x03 << 20)) == 0)
591 ip6
->ip6_flow
|= htonl((opt
->ip6po_tclass
& mask
) << 20);
594 /* fill in or override the hop limit field, if necessary. */
595 if (opt
&& opt
->ip6po_hlim
!= -1)
596 ip6
->ip6_hlim
= opt
->ip6po_hlim
& 0xff;
597 else if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
599 ip6
->ip6_hlim
= im6o
->im6o_multicast_hlim
;
601 ip6
->ip6_hlim
= ip6_defmcasthlim
;
605 if (needipsec
&& needipsectun
) {
606 struct ipsec_output_state state
;
609 * All the extension headers will become inaccessible
610 * (since they can be encrypted).
611 * Don't panic, we need no more updates to extension headers
612 * on inner IPv6 packet (since they are now encapsulated).
614 * IPv6 [ESP|AH] IPv6 [extension headers] payload
616 memset(&exthdrs
, 0, sizeof(exthdrs
));
617 exthdrs
.ip6e_ip6
= m
;
619 memset(&state
, 0, sizeof(state
));
622 state
.dst
= rtcache_getdst(ro
);
624 error
= ipsec6_output_tunnel(&state
, sp
, flags
);
627 ro_pmtu
= ro
= state
.ro
;
628 dst
= satocsin6(state
.dst
);
630 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
641 printf("ip6_output (ipsec): error code %d\n", error
);
644 /* don't show these error codes to the user */
651 exthdrs
.ip6e_ip6
= m
;
657 error
= ipsec6_process_packet(m
,sp
->req
);
660 * Preserve KAME behaviour: ENOENT can be returned
661 * when an SA acquire is in progress. Don't propagate
662 * this to user-level; it confuses applications.
663 * XXX this will go away when the SADB is redone.
670 #endif /* FAST_IPSEC */
675 ip6
= mtod(m
, struct ip6_hdr
*);
677 sockaddr_in6_init(&dst_sa
, &ip6
->ip6_dst
, 0, 0, 0);
678 if ((error
= in6_selectroute(&dst_sa
, opt
, im6o
, ro
,
679 &ifp
, &rt
, 0)) != 0) {
681 in6_ifstat_inc(ifp
, ifs6_out_discard
);
686 * If in6_selectroute() does not return a route entry,
687 * dst may not have been updated.
689 rtcache_setdst(ro
, sin6tosa(&dst_sa
));
693 * then rt (for unicast) and ifp must be non-NULL valid values.
695 if ((flags
& IPV6_FORWARDING
) == 0) {
696 /* XXX: the FORWARDING flag can be set for mrouting. */
697 in6_ifstat_inc(ifp
, ifs6_out_request
);
700 ia
= (struct in6_ifaddr
*)(rt
->rt_ifa
);
705 * The outgoing interface must be in the zone of source and
706 * destination addresses. We should use ia_ifp to support the
707 * case of sending packets to an address of our own.
709 if (ia
!= NULL
&& ia
->ia_ifp
)
710 origifp
= ia
->ia_ifp
;
715 if (in6_setscope(&src0
, origifp
, &zone
))
717 sockaddr_in6_init(&src_sa
, &ip6
->ip6_src
, 0, 0, 0);
718 if (sa6_recoverscope(&src_sa
) || zone
!= src_sa
.sin6_scope_id
)
722 if (in6_setscope(&dst0
, origifp
, &zone
))
724 /* re-initialize to be sure */
725 sockaddr_in6_init(&dst_sa
, &ip6
->ip6_dst
, 0, 0, 0);
726 if (sa6_recoverscope(&dst_sa
) || zone
!= dst_sa
.sin6_scope_id
)
729 /* scope check is done. */
731 if (rt
== NULL
|| IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
733 dst
= satocsin6(rtcache_getdst(ro
));
734 KASSERT(dst
!= NULL
);
735 } else if (opt
&& rtcache_validate(&opt
->ip6po_nextroute
) != NULL
) {
737 * The nexthop is explicitly specified by the
738 * application. We assume the next hop is an IPv6
741 dst
= (struct sockaddr_in6
*)opt
->ip6po_nexthop
;
742 } else if ((rt
->rt_flags
& RTF_GATEWAY
))
743 dst
= (struct sockaddr_in6
*)rt
->rt_gateway
;
744 else if (dst
== NULL
)
745 dst
= satocsin6(rtcache_getdst(ro
));
748 * XXXXXX: original code follows:
750 if (!IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
))
751 m
->m_flags
&= ~(M_BCAST
| M_MCAST
); /* just in case */
753 struct in6_multi
*in6m
;
755 m
->m_flags
= (m
->m_flags
& ~M_BCAST
) | M_MCAST
;
757 in6_ifstat_inc(ifp
, ifs6_out_mcast
);
760 * Confirm that the outgoing interface supports multicast.
762 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
763 IP6_STATINC(IP6_STAT_NOROUTE
);
764 in6_ifstat_inc(ifp
, ifs6_out_discard
);
769 IN6_LOOKUP_MULTI(ip6
->ip6_dst
, ifp
, in6m
);
771 (im6o
== NULL
|| im6o
->im6o_multicast_loop
)) {
773 * If we belong to the destination multicast group
774 * on the outgoing interface, and the caller did not
775 * forbid loopback, loop back a copy.
777 KASSERT(dst
!= NULL
);
778 ip6_mloopback(ifp
, m
, dst
);
781 * If we are acting as a multicast router, perform
782 * multicast forwarding as if the packet had just
783 * arrived on the interface to which we are about
784 * to send. The multicast forwarding function
785 * recursively calls this function, using the
786 * IPV6_FORWARDING flag to prevent infinite recursion.
788 * Multicasts that are looped back by ip6_mloopback(),
789 * above, will be forwarded by the ip6_input() routine,
792 if (ip6_mrouter
&& (flags
& IPV6_FORWARDING
) == 0) {
793 if (ip6_mforward(ip6
, ifp
, m
) != 0) {
800 * Multicasts with a hoplimit of zero may be looped back,
801 * above, but must not be transmitted on a network.
802 * Also, multicasts addressed to the loopback interface
803 * are not sent -- the above call to ip6_mloopback() will
804 * loop back a copy if this host actually belongs to the
805 * destination group on the loopback interface.
807 if (ip6
->ip6_hlim
== 0 || (ifp
->if_flags
& IFF_LOOPBACK
) ||
808 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6
->ip6_dst
)) {
815 * Fill the outgoing inteface to tell the upper layer
816 * to increment per-interface statistics.
821 /* Determine path MTU. */
822 if ((error
= ip6_getpmtu(ro_pmtu
, ro
, ifp
, &finaldst
, &mtu
,
831 * The caller of this function may specify to use the minimum MTU
833 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
834 * setting. The logic is a bit complicated; by default, unicast
835 * packets will follow path MTU while multicast packets will be sent at
836 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
837 * including unicast ones will be sent at the minimum MTU. Multicast
838 * packets will always be sent at the minimum MTU unless
839 * IP6PO_MINMTU_DISABLE is explicitly specified.
840 * See RFC 3542 for more details.
842 if (mtu
> IPV6_MMTU
) {
843 if ((flags
& IPV6_MINMTU
))
845 else if (opt
&& opt
->ip6po_minmtu
== IP6PO_MINMTU_ALL
)
847 else if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) &&
849 opt
->ip6po_minmtu
!= IP6PO_MINMTU_DISABLE
)) {
855 * clear embedded scope identifiers if necessary.
856 * in6_clearscope will touch the addresses only when necessary.
858 in6_clearscope(&ip6
->ip6_src
);
859 in6_clearscope(&ip6
->ip6_dst
);
862 * If the outgoing packet contains a hop-by-hop options header,
863 * it must be examined and processed even by the source node.
864 * (RFC 2460, section 4.)
866 if (exthdrs
.ip6e_hbh
) {
867 struct ip6_hbh
*hbh
= mtod(exthdrs
.ip6e_hbh
, struct ip6_hbh
*);
868 u_int32_t dummy1
; /* XXX unused */
869 u_int32_t dummy2
; /* XXX unused */
872 * XXX: if we have to send an ICMPv6 error to the sender,
873 * we need the M_LOOP flag since icmp6_error() expects
874 * the IPv6 and the hop-by-hop options header are
875 * continuous unless the flag is set.
877 m
->m_flags
|= M_LOOP
;
878 m
->m_pkthdr
.rcvif
= ifp
;
879 if (ip6_process_hopopts(m
, (u_int8_t
*)(hbh
+ 1),
880 ((hbh
->ip6h_len
+ 1) << 3) - sizeof(struct ip6_hbh
),
881 &dummy1
, &dummy2
) < 0) {
882 /* m was already freed at this point */
883 error
= EINVAL
;/* better error? */
886 m
->m_flags
&= ~M_LOOP
; /* XXX */
887 m
->m_pkthdr
.rcvif
= NULL
;
892 * Run through list of hooks for output packets.
894 if ((error
= pfil_run_hooks(&inet6_pfil_hook
, &m
, ifp
, PFIL_OUT
)) != 0)
898 ip6
= mtod(m
, struct ip6_hdr
*);
899 #endif /* PFIL_HOOKS */
901 * Send the packet to the outgoing interface.
902 * If necessary, do IPv6 fragmentation before sending.
904 * the logic here is rather complex:
905 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
906 * 1-a: send as is if tlen <= path mtu
907 * 1-b: fragment if tlen > path mtu
909 * 2: if user asks us not to fragment (dontfrag == 1)
910 * 2-a: send as is if tlen <= interface mtu
911 * 2-b: error if tlen > interface mtu
913 * 3: if we always need to attach fragment header (alwaysfrag == 1)
916 * 4: if dontfrag == 1 && alwaysfrag == 1
917 * error, as we cannot handle this conflicting request
919 tlen
= m
->m_pkthdr
.len
;
920 tso
= (m
->m_pkthdr
.csum_flags
& M_CSUM_TSOv6
) != 0;
921 if (opt
&& (opt
->ip6po_flags
& IP6PO_DONTFRAG
))
926 if (dontfrag
&& alwaysfrag
) { /* case 4 */
927 /* conflicting request - can't transmit */
931 if (dontfrag
&& (!tso
&& tlen
> IN6_LINKMTU(ifp
))) { /* case 2-b */
933 * Even if the DONTFRAG option is specified, we cannot send the
934 * packet when the data length is larger than the MTU of the
935 * outgoing interface.
936 * Notify the error by sending IPV6_PATHMTU ancillary data as
937 * well as returning an error code (the latter is not described
941 struct ip6ctlparam ip6cp
;
943 mtu32
= (u_int32_t
)mtu
;
944 memset(&ip6cp
, 0, sizeof(ip6cp
));
945 ip6cp
.ip6c_cmdarg
= (void *)&mtu32
;
946 pfctlinput2(PRC_MSGSIZE
,
947 rtcache_getdst(ro_pmtu
), &ip6cp
);
954 * transmit packet without fragmentation
956 if (dontfrag
|| (!alwaysfrag
&& (tlen
<= mtu
|| tso
))) {
957 /* case 1-a and 2-a */
958 struct in6_ifaddr
*ia6
;
961 ip6
= mtod(m
, struct ip6_hdr
*);
962 ia6
= in6_ifawithifp(ifp
, &ip6
->ip6_src
);
964 /* Record statistics for this interface address. */
965 ia6
->ia_ifa
.ifa_data
.ifad_outbytes
+= m
->m_pkthdr
.len
;
968 /* clean ipsec history once it goes out of the node */
972 sw_csum
= m
->m_pkthdr
.csum_flags
& ~ifp
->if_csum_flags_tx
;
973 if ((sw_csum
& (M_CSUM_UDPv6
|M_CSUM_TCPv6
)) != 0) {
974 if (IN6_NEED_CHECKSUM(ifp
,
975 sw_csum
& (M_CSUM_UDPv6
|M_CSUM_TCPv6
))) {
976 in6_delayed_cksum(m
);
978 m
->m_pkthdr
.csum_flags
&= ~(M_CSUM_UDPv6
|M_CSUM_TCPv6
);
981 KASSERT(dst
!= NULL
);
982 if (__predict_true(!tso
||
983 (ifp
->if_capenable
& IFCAP_TSOv6
) != 0)) {
984 error
= nd6_output(ifp
, origifp
, m
, dst
, rt
);
986 error
= ip6_tso_output(ifp
, origifp
, m
, dst
, rt
);
992 error
= EINVAL
; /* XXX */
997 * try to fragment the packet. case 1-b and 3
999 if (mtu
< IPV6_MMTU
) {
1000 /* path MTU cannot be less than IPV6_MMTU */
1002 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1004 } else if (ip6
->ip6_plen
== 0) {
1005 /* jumbo payload cannot be fragmented */
1007 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1010 struct mbuf
**mnext
, *m_frgpart
;
1011 struct ip6_frag
*ip6f
;
1012 u_int32_t id
= htonl(ip6_randomid());
1014 #if 0 /* see below */
1015 struct ip6ctlparam ip6cp
;
1020 * Too large for the destination or interface;
1021 * fragment if possible.
1022 * Must be able to put at least 8 bytes per fragment.
1024 hlen
= unfragpartlen
;
1025 if (mtu
> IPV6_MAXPACKET
)
1026 mtu
= IPV6_MAXPACKET
;
1030 * It is believed this code is a leftover from the
1031 * development of the IPV6_RECVPATHMTU sockopt and
1032 * associated work to implement RFC3542.
1033 * It's not entirely clear what the intent of the API
1034 * is at this point, so disable this code for now.
1035 * The IPV6_RECVPATHMTU sockopt and/or IPV6_DONTFRAG
1036 * will send notifications if the application requests.
1039 /* Notify a proper path MTU to applications. */
1040 mtu32
= (u_int32_t
)mtu
;
1041 memset(&ip6cp
, 0, sizeof(ip6cp
));
1042 ip6cp
.ip6c_cmdarg
= (void *)&mtu32
;
1043 pfctlinput2(PRC_MSGSIZE
,
1044 rtcache_getdst(ro_pmtu
), &ip6cp
);
1047 len
= (mtu
- hlen
- sizeof(struct ip6_frag
)) & ~7;
1050 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1054 mnext
= &m
->m_nextpkt
;
1057 * Change the next header field of the last header in the
1058 * unfragmentable part.
1060 if (exthdrs
.ip6e_rthdr
) {
1061 nextproto
= *mtod(exthdrs
.ip6e_rthdr
, u_char
*);
1062 *mtod(exthdrs
.ip6e_rthdr
, u_char
*) = IPPROTO_FRAGMENT
;
1063 } else if (exthdrs
.ip6e_dest1
) {
1064 nextproto
= *mtod(exthdrs
.ip6e_dest1
, u_char
*);
1065 *mtod(exthdrs
.ip6e_dest1
, u_char
*) = IPPROTO_FRAGMENT
;
1066 } else if (exthdrs
.ip6e_hbh
) {
1067 nextproto
= *mtod(exthdrs
.ip6e_hbh
, u_char
*);
1068 *mtod(exthdrs
.ip6e_hbh
, u_char
*) = IPPROTO_FRAGMENT
;
1070 nextproto
= ip6
->ip6_nxt
;
1071 ip6
->ip6_nxt
= IPPROTO_FRAGMENT
;
1074 if ((m
->m_pkthdr
.csum_flags
& (M_CSUM_UDPv6
|M_CSUM_TCPv6
))
1076 if (IN6_NEED_CHECKSUM(ifp
,
1077 m
->m_pkthdr
.csum_flags
&
1078 (M_CSUM_UDPv6
|M_CSUM_TCPv6
))) {
1079 in6_delayed_cksum(m
);
1081 m
->m_pkthdr
.csum_flags
&= ~(M_CSUM_UDPv6
|M_CSUM_TCPv6
);
1085 * Loop through length of segment after first fragment,
1086 * make new header and copy data of each part and link onto
1090 for (off
= hlen
; off
< tlen
; off
+= len
) {
1093 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
1096 IP6_STATINC(IP6_STAT_ODROPPED
);
1099 m
->m_pkthdr
.rcvif
= NULL
;
1100 m
->m_flags
= m0
->m_flags
& M_COPYFLAGS
;
1102 mnext
= &m
->m_nextpkt
;
1103 m
->m_data
+= max_linkhdr
;
1104 mhip6
= mtod(m
, struct ip6_hdr
*);
1106 m
->m_len
= sizeof(*mhip6
);
1107 error
= ip6_insertfraghdr(m0
, m
, hlen
, &ip6f
);
1109 IP6_STATINC(IP6_STAT_ODROPPED
);
1112 ip6f
->ip6f_offlg
= htons((u_int16_t
)((off
- hlen
) & ~7));
1113 if (off
+ len
>= tlen
)
1116 ip6f
->ip6f_offlg
|= IP6F_MORE_FRAG
;
1117 mhip6
->ip6_plen
= htons((u_int16_t
)(len
+ hlen
+
1118 sizeof(*ip6f
) - sizeof(struct ip6_hdr
)));
1119 if ((m_frgpart
= m_copy(m0
, off
, len
)) == 0) {
1121 IP6_STATINC(IP6_STAT_ODROPPED
);
1124 for (mlast
= m
; mlast
->m_next
; mlast
= mlast
->m_next
)
1126 mlast
->m_next
= m_frgpart
;
1127 m
->m_pkthdr
.len
= len
+ hlen
+ sizeof(*ip6f
);
1128 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
1129 ip6f
->ip6f_reserved
= 0;
1130 ip6f
->ip6f_ident
= id
;
1131 ip6f
->ip6f_nxt
= nextproto
;
1132 IP6_STATINC(IP6_STAT_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 struct in6_ifaddr
*ia6
;
1151 ip6
= mtod(m
, struct ip6_hdr
*);
1152 ia6
= in6_ifawithifp(ifp
, &ip6
->ip6_src
);
1155 * Record statistics for this interface
1158 ia6
->ia_ifa
.ifa_data
.ifad_outbytes
+=
1162 /* clean ipsec history once it goes out of the node */
1165 KASSERT(dst
!= NULL
);
1166 error
= nd6_output(ifp
, origifp
, m
, dst
, rt
);
1172 IP6_STATINC(IP6_STAT_FRAGMENTED
);
1175 rtcache_free(&ip6route
);
1184 #endif /* FAST_IPSEC */
1190 m_freem(exthdrs
.ip6e_hbh
); /* m_freem will check if mbuf is 0 */
1191 m_freem(exthdrs
.ip6e_dest1
);
1192 m_freem(exthdrs
.ip6e_rthdr
);
1193 m_freem(exthdrs
.ip6e_dest2
);
1199 IP6_STATINC(IP6_STAT_BADSCOPE
);
1200 in6_ifstat_inc(origifp
, ifs6_out_discard
);
1202 error
= EHOSTUNREACH
; /* XXX */
1207 ip6_copyexthdr(struct mbuf
**mp
, void *hdr
, int hlen
)
1211 if (hlen
> MCLBYTES
)
1212 return (ENOBUFS
); /* XXX */
1214 MGET(m
, M_DONTWAIT
, MT_DATA
);
1219 MCLGET(m
, M_DONTWAIT
);
1220 if ((m
->m_flags
& M_EXT
) == 0) {
1227 bcopy(hdr
, mtod(m
, void *), hlen
);
1234 * Process a delayed payload checksum calculation.
1237 in6_delayed_cksum(struct mbuf
*m
)
1239 uint16_t csum
, offset
;
1241 KASSERT((m
->m_pkthdr
.csum_flags
& (M_CSUM_UDPv6
|M_CSUM_TCPv6
)) != 0);
1242 KASSERT((~m
->m_pkthdr
.csum_flags
& (M_CSUM_UDPv6
|M_CSUM_TCPv6
)) != 0);
1243 KASSERT((m
->m_pkthdr
.csum_flags
1244 & (M_CSUM_UDPv4
|M_CSUM_TCPv4
|M_CSUM_TSOv4
)) == 0);
1246 offset
= M_CSUM_DATA_IPv6_HL(m
->m_pkthdr
.csum_data
);
1247 csum
= in6_cksum(m
, 0, offset
, m
->m_pkthdr
.len
- offset
);
1248 if (csum
== 0 && (m
->m_pkthdr
.csum_flags
& M_CSUM_UDPv6
) != 0) {
1252 offset
+= M_CSUM_DATA_IPv6_OFFSET(m
->m_pkthdr
.csum_data
);
1253 if ((offset
+ sizeof(csum
)) > m
->m_len
) {
1254 m_copyback(m
, offset
, sizeof(csum
), &csum
);
1256 *(uint16_t *)(mtod(m
, char *) + offset
) = csum
;
1261 * Insert jumbo payload option.
1264 ip6_insert_jumboopt(struct ip6_exthdrs
*exthdrs
, u_int32_t plen
)
1270 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1273 * If there is no hop-by-hop options header, allocate new one.
1274 * If there is one but it doesn't have enough space to store the
1275 * jumbo payload option, allocate a cluster to store the whole options.
1276 * Otherwise, use it to store the options.
1278 if (exthdrs
->ip6e_hbh
== 0) {
1279 MGET(mopt
, M_DONTWAIT
, MT_DATA
);
1282 mopt
->m_len
= JUMBOOPTLEN
;
1283 optbuf
= mtod(mopt
, u_int8_t
*);
1284 optbuf
[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1285 exthdrs
->ip6e_hbh
= mopt
;
1287 struct ip6_hbh
*hbh
;
1289 mopt
= exthdrs
->ip6e_hbh
;
1290 if (M_TRAILINGSPACE(mopt
) < JUMBOOPTLEN
) {
1293 * - exthdrs->ip6e_hbh is not referenced from places
1294 * other than exthdrs.
1295 * - exthdrs->ip6e_hbh is not an mbuf chain.
1297 int oldoptlen
= mopt
->m_len
;
1301 * XXX: give up if the whole (new) hbh header does
1302 * not fit even in an mbuf cluster.
1304 if (oldoptlen
+ JUMBOOPTLEN
> MCLBYTES
)
1308 * As a consequence, we must always prepare a cluster
1311 MGET(n
, M_DONTWAIT
, MT_DATA
);
1313 MCLGET(n
, M_DONTWAIT
);
1314 if ((n
->m_flags
& M_EXT
) == 0) {
1321 n
->m_len
= oldoptlen
+ JUMBOOPTLEN
;
1322 bcopy(mtod(mopt
, void *), mtod(n
, void *),
1324 optbuf
= mtod(n
, u_int8_t
*) + oldoptlen
;
1326 mopt
= exthdrs
->ip6e_hbh
= n
;
1328 optbuf
= mtod(mopt
, u_int8_t
*) + mopt
->m_len
;
1329 mopt
->m_len
+= JUMBOOPTLEN
;
1331 optbuf
[0] = IP6OPT_PADN
;
1335 * Adjust the header length according to the pad and
1336 * the jumbo payload option.
1338 hbh
= mtod(mopt
, struct ip6_hbh
*);
1339 hbh
->ip6h_len
+= (JUMBOOPTLEN
>> 3);
1342 /* fill in the option. */
1343 optbuf
[2] = IP6OPT_JUMBO
;
1345 v
= (u_int32_t
)htonl(plen
+ JUMBOOPTLEN
);
1346 bcopy(&v
, &optbuf
[4], sizeof(u_int32_t
));
1348 /* finally, adjust the packet header length */
1349 exthdrs
->ip6e_ip6
->m_pkthdr
.len
+= JUMBOOPTLEN
;
1356 * Insert fragment header and copy unfragmentable header portions.
1359 ip6_insertfraghdr(struct mbuf
*m0
, struct mbuf
*m
, int hlen
,
1360 struct ip6_frag
**frghdrp
)
1362 struct mbuf
*n
, *mlast
;
1364 if (hlen
> sizeof(struct ip6_hdr
)) {
1365 n
= m_copym(m0
, sizeof(struct ip6_hdr
),
1366 hlen
- sizeof(struct ip6_hdr
), M_DONTWAIT
);
1373 /* Search for the last mbuf of unfragmentable part. */
1374 for (mlast
= n
; mlast
->m_next
; mlast
= mlast
->m_next
)
1377 if ((mlast
->m_flags
& M_EXT
) == 0 &&
1378 M_TRAILINGSPACE(mlast
) >= sizeof(struct ip6_frag
)) {
1379 /* use the trailing space of the last mbuf for the fragment hdr */
1380 *frghdrp
= (struct ip6_frag
*)(mtod(mlast
, char *) +
1382 mlast
->m_len
+= sizeof(struct ip6_frag
);
1383 m
->m_pkthdr
.len
+= sizeof(struct ip6_frag
);
1385 /* allocate a new mbuf for the fragment header */
1388 MGET(mfrg
, M_DONTWAIT
, MT_DATA
);
1391 mfrg
->m_len
= sizeof(struct ip6_frag
);
1392 *frghdrp
= mtod(mfrg
, struct ip6_frag
*);
1393 mlast
->m_next
= mfrg
;
1400 ip6_getpmtu(struct route
*ro_pmtu
, struct route
*ro
, struct ifnet
*ifp
,
1401 const struct in6_addr
*dst
, u_long
*mtup
, int *alwaysfragp
)
1408 if (ro_pmtu
!= ro
) {
1410 struct sockaddr dst
;
1411 struct sockaddr_in6 dst6
;
1414 /* The first hop and the final destination may differ. */
1415 sockaddr_in6_init(&u
.dst6
, dst
, 0, 0, 0);
1416 rt
= rtcache_lookup(ro_pmtu
, &u
.dst
);
1418 rt
= rtcache_validate(ro_pmtu
);
1424 ifmtu
= IN6_LINKMTU(ifp
);
1425 mtu
= rt
->rt_rmx
.rmx_mtu
;
1428 else if (mtu
< IPV6_MMTU
) {
1430 * RFC2460 section 5, last paragraph:
1431 * if we record ICMPv6 too big message with
1432 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1433 * or smaller, with fragment header attached.
1434 * (fragment header is needed regardless from the
1435 * packet size, for translators to identify packets)
1439 } else if (mtu
> ifmtu
) {
1441 * The MTU on the route is larger than the MTU on
1442 * the interface! This shouldn't happen, unless the
1443 * MTU of the interface has been changed after the
1444 * interface was brought up. Change the MTU in the
1445 * route to match the interface MTU (as long as the
1446 * field isn't locked).
1449 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
))
1450 rt
->rt_rmx
.rmx_mtu
= mtu
;
1453 mtu
= IN6_LINKMTU(ifp
);
1455 error
= EHOSTUNREACH
; /* XXX */
1459 *alwaysfragp
= alwaysfrag
;
1464 * IP6 socket option processing.
1467 ip6_ctloutput(int op
, struct socket
*so
, struct sockopt
*sopt
)
1469 int optdatalen
, uproto
;
1471 struct in6pcb
*in6p
= sotoin6pcb(so
);
1475 KASSERT(sopt
!= NULL
);
1477 level
= sopt
->sopt_level
;
1478 optname
= sopt
->sopt_name
;
1481 uproto
= (int)so
->so_proto
->pr_protocol
;
1483 if (level
!= IPPROTO_IPV6
) {
1490 case IPV6_2292PKTOPTIONS
:
1491 error
= ip6_pcbopts(&in6p
->in6p_outputopts
, so
, sopt
);
1496 * Use of some Hop-by-Hop options or some
1497 * Destination options, might require special
1498 * privilege. That is, normal applications
1499 * (without special privilege) might be forbidden
1500 * from setting certain options in outgoing packets,
1501 * and might never see certain options in received
1502 * packets. [RFC 2292 Section 6]
1503 * KAME specific note:
1504 * KAME prevents non-privileged users from sending or
1505 * receiving ANY hbh/dst options in order to avoid
1506 * overhead of parsing options in the kernel.
1508 case IPV6_RECVHOPOPTS
:
1509 case IPV6_RECVDSTOPTS
:
1510 case IPV6_RECVRTHDRDSTOPTS
:
1511 error
= kauth_authorize_generic(kauth_cred_get(),
1512 KAUTH_GENERIC_ISSUSER
, NULL
);
1516 case IPV6_UNICAST_HOPS
:
1520 case IPV6_RECVPKTINFO
:
1521 case IPV6_RECVHOPLIMIT
:
1522 case IPV6_RECVRTHDR
:
1523 case IPV6_RECVPATHMTU
:
1524 case IPV6_RECVTCLASS
:
1526 error
= sockopt_getint(sopt
, &optval
);
1530 case IPV6_UNICAST_HOPS
:
1531 if (optval
< -1 || optval
>= 256)
1534 /* -1 = kernel default */
1535 in6p
->in6p_hops
= optval
;
1538 #define OPTSET(bit) \
1541 in6p->in6p_flags |= (bit); \
1543 in6p->in6p_flags &= ~(bit); \
1544 } while (/*CONSTCOND*/ 0)
1547 #define OPTSET2292(bit) \
1549 in6p->in6p_flags |= IN6P_RFC2292; \
1551 in6p->in6p_flags |= (bit); \
1553 in6p->in6p_flags &= ~(bit); \
1554 } while (/*CONSTCOND*/ 0)
1557 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1559 case IPV6_RECVPKTINFO
:
1561 /* cannot mix with RFC2292 */
1562 if (OPTBIT(IN6P_RFC2292
)) {
1567 OPTSET(IN6P_PKTINFO
);
1572 struct ip6_pktopts
**optp
;
1575 /* cannot mix with RFC2292 */
1576 if (OPTBIT(IN6P_RFC2292
)) {
1581 optp
= &in6p
->in6p_outputopts
;
1582 error
= ip6_pcbopt(IPV6_HOPLIMIT
,
1586 kauth_cred_get(), uproto
);
1590 case IPV6_RECVHOPLIMIT
:
1592 /* cannot mix with RFC2292 */
1593 if (OPTBIT(IN6P_RFC2292
)) {
1598 OPTSET(IN6P_HOPLIMIT
);
1601 case IPV6_RECVHOPOPTS
:
1603 /* cannot mix with RFC2292 */
1604 if (OPTBIT(IN6P_RFC2292
)) {
1609 OPTSET(IN6P_HOPOPTS
);
1612 case IPV6_RECVDSTOPTS
:
1614 /* cannot mix with RFC2292 */
1615 if (OPTBIT(IN6P_RFC2292
)) {
1620 OPTSET(IN6P_DSTOPTS
);
1623 case IPV6_RECVRTHDRDSTOPTS
:
1625 /* cannot mix with RFC2292 */
1626 if (OPTBIT(IN6P_RFC2292
)) {
1631 OPTSET(IN6P_RTHDRDSTOPTS
);
1634 case IPV6_RECVRTHDR
:
1636 /* cannot mix with RFC2292 */
1637 if (OPTBIT(IN6P_RFC2292
)) {
1649 case IPV6_RECVPATHMTU
:
1651 * We ignore this option for TCP
1653 * (RFC3542 leaves this case
1656 if (uproto
!= IPPROTO_TCP
)
1662 * make setsockopt(IPV6_V6ONLY)
1663 * available only prior to bind(2).
1664 * see ipng mailing list, Jun 22 2001.
1666 if (in6p
->in6p_lport
||
1667 !IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
)) {
1671 #ifdef INET6_BINDV6ONLY
1675 OPTSET(IN6P_IPV6_V6ONLY
);
1678 case IPV6_RECVTCLASS
:
1680 /* cannot mix with RFC2292 XXX */
1681 if (OPTBIT(IN6P_RFC2292
)) {
1686 OPTSET(IN6P_TCLASS
);
1694 struct ip6_pktopts
**optp
;
1697 error
= sockopt_get(sopt
, &tclass
, sizeof(tclass
));
1700 optp
= &in6p
->in6p_outputopts
;
1701 error
= ip6_pcbopt(optname
,
1705 kauth_cred_get(), uproto
);
1711 case IPV6_USE_MIN_MTU
:
1712 error
= sockopt_getint(sopt
, &optval
);
1716 struct ip6_pktopts
**optp
;
1717 optp
= &in6p
->in6p_outputopts
;
1718 error
= ip6_pcbopt(optname
,
1722 kauth_cred_get(), uproto
);
1727 case IPV6_2292PKTINFO
:
1728 case IPV6_2292HOPLIMIT
:
1729 case IPV6_2292HOPOPTS
:
1730 case IPV6_2292DSTOPTS
:
1731 case IPV6_2292RTHDR
:
1733 error
= sockopt_getint(sopt
, &optval
);
1738 case IPV6_2292PKTINFO
:
1739 OPTSET2292(IN6P_PKTINFO
);
1741 case IPV6_2292HOPLIMIT
:
1742 OPTSET2292(IN6P_HOPLIMIT
);
1744 case IPV6_2292HOPOPTS
:
1746 * Check super-user privilege.
1747 * See comments for IPV6_RECVHOPOPTS.
1750 kauth_authorize_generic(kauth_cred_get(),
1751 KAUTH_GENERIC_ISSUSER
, NULL
);
1754 OPTSET2292(IN6P_HOPOPTS
);
1756 case IPV6_2292DSTOPTS
:
1758 kauth_authorize_generic(kauth_cred_get(),
1759 KAUTH_GENERIC_ISSUSER
, NULL
);
1762 OPTSET2292(IN6P_DSTOPTS
|IN6P_RTHDRDSTOPTS
); /* XXX */
1764 case IPV6_2292RTHDR
:
1765 OPTSET2292(IN6P_RTHDR
);
1774 case IPV6_RTHDRDSTOPTS
:
1775 case IPV6_NEXTHOP
: {
1776 /* new advanced API (RFC3542) */
1779 struct ip6_pktopts
**optp
;
1782 /* cannot mix with RFC2292 */
1783 if (OPTBIT(IN6P_RFC2292
)) {
1789 optbuflen
= sopt
->sopt_size
;
1790 optbuf
= malloc(optbuflen
, M_IP6OPT
, M_NOWAIT
);
1791 if (optbuf
== NULL
) {
1796 sockopt_get(sopt
, optbuf
, optbuflen
);
1797 optp
= &in6p
->in6p_outputopts
;
1798 error
= ip6_pcbopt(optname
, optbuf
, optbuflen
,
1799 optp
, kauth_cred_get(), uproto
);
1804 case IPV6_MULTICAST_IF
:
1805 case IPV6_MULTICAST_HOPS
:
1806 case IPV6_MULTICAST_LOOP
:
1807 case IPV6_JOIN_GROUP
:
1808 case IPV6_LEAVE_GROUP
:
1809 error
= ip6_setmoptions(sopt
, &in6p
->in6p_moptions
);
1812 case IPV6_PORTRANGE
:
1813 error
= sockopt_getint(sopt
, &optval
);
1818 case IPV6_PORTRANGE_DEFAULT
:
1819 in6p
->in6p_flags
&= ~(IN6P_LOWPORT
);
1820 in6p
->in6p_flags
&= ~(IN6P_HIGHPORT
);
1823 case IPV6_PORTRANGE_HIGH
:
1824 in6p
->in6p_flags
&= ~(IN6P_LOWPORT
);
1825 in6p
->in6p_flags
|= IN6P_HIGHPORT
;
1828 case IPV6_PORTRANGE_LOW
:
1829 in6p
->in6p_flags
&= ~(IN6P_HIGHPORT
);
1830 in6p
->in6p_flags
|= IN6P_LOWPORT
;
1840 #if defined(IPSEC) || defined(FAST_IPSEC)
1841 case IPV6_IPSEC_POLICY
:
1842 error
= ipsec6_set_policy(in6p
, optname
,
1843 sopt
->sopt_data
, sopt
->sopt_size
, kauth_cred_get());
1848 error
= ENOPROTOOPT
;
1856 case IPV6_2292PKTOPTIONS
:
1858 * RFC3542 (effectively) deprecated the
1859 * semantics of the 2292-style pktoptions.
1860 * Since it was not reliable in nature (i.e.,
1861 * applications had to expect the lack of some
1862 * information after all), it would make sense
1863 * to simplify this part by always returning
1869 case IPV6_RECVHOPOPTS
:
1870 case IPV6_RECVDSTOPTS
:
1871 case IPV6_RECVRTHDRDSTOPTS
:
1872 case IPV6_UNICAST_HOPS
:
1873 case IPV6_RECVPKTINFO
:
1874 case IPV6_RECVHOPLIMIT
:
1875 case IPV6_RECVRTHDR
:
1876 case IPV6_RECVPATHMTU
:
1880 case IPV6_PORTRANGE
:
1881 case IPV6_RECVTCLASS
:
1884 case IPV6_RECVHOPOPTS
:
1885 optval
= OPTBIT(IN6P_HOPOPTS
);
1888 case IPV6_RECVDSTOPTS
:
1889 optval
= OPTBIT(IN6P_DSTOPTS
);
1892 case IPV6_RECVRTHDRDSTOPTS
:
1893 optval
= OPTBIT(IN6P_RTHDRDSTOPTS
);
1896 case IPV6_UNICAST_HOPS
:
1897 optval
= in6p
->in6p_hops
;
1900 case IPV6_RECVPKTINFO
:
1901 optval
= OPTBIT(IN6P_PKTINFO
);
1904 case IPV6_RECVHOPLIMIT
:
1905 optval
= OPTBIT(IN6P_HOPLIMIT
);
1908 case IPV6_RECVRTHDR
:
1909 optval
= OPTBIT(IN6P_RTHDR
);
1912 case IPV6_RECVPATHMTU
:
1913 optval
= OPTBIT(IN6P_MTU
);
1917 optval
= OPTBIT(IN6P_FAITH
);
1921 optval
= OPTBIT(IN6P_IPV6_V6ONLY
);
1924 case IPV6_PORTRANGE
:
1927 flags
= in6p
->in6p_flags
;
1928 if (flags
& IN6P_HIGHPORT
)
1929 optval
= IPV6_PORTRANGE_HIGH
;
1930 else if (flags
& IN6P_LOWPORT
)
1931 optval
= IPV6_PORTRANGE_LOW
;
1936 case IPV6_RECVTCLASS
:
1937 optval
= OPTBIT(IN6P_TCLASS
);
1943 error
= sockopt_setint(sopt
, optval
);
1949 struct ip6_mtuinfo mtuinfo
;
1950 struct route
*ro
= &in6p
->in6p_route
;
1952 if (!(so
->so_state
& SS_ISCONNECTED
))
1955 * XXX: we dot not consider the case of source
1956 * routing, or optional information to specify
1957 * the outgoing interface.
1959 error
= ip6_getpmtu(ro
, NULL
, NULL
,
1960 &in6p
->in6p_faddr
, &pmtu
, NULL
);
1963 if (pmtu
> IPV6_MAXPACKET
)
1964 pmtu
= IPV6_MAXPACKET
;
1966 memset(&mtuinfo
, 0, sizeof(mtuinfo
));
1967 mtuinfo
.ip6m_mtu
= (u_int32_t
)pmtu
;
1968 optdata
= (void *)&mtuinfo
;
1969 optdatalen
= sizeof(mtuinfo
);
1970 if (optdatalen
> MCLBYTES
)
1971 return (EMSGSIZE
); /* XXX */
1972 error
= sockopt_set(sopt
, optdata
, optdatalen
);
1977 case IPV6_2292PKTINFO
:
1978 case IPV6_2292HOPLIMIT
:
1979 case IPV6_2292HOPOPTS
:
1980 case IPV6_2292RTHDR
:
1981 case IPV6_2292DSTOPTS
:
1983 case IPV6_2292PKTINFO
:
1984 optval
= OPTBIT(IN6P_PKTINFO
);
1986 case IPV6_2292HOPLIMIT
:
1987 optval
= OPTBIT(IN6P_HOPLIMIT
);
1989 case IPV6_2292HOPOPTS
:
1990 optval
= OPTBIT(IN6P_HOPOPTS
);
1992 case IPV6_2292RTHDR
:
1993 optval
= OPTBIT(IN6P_RTHDR
);
1995 case IPV6_2292DSTOPTS
:
1996 optval
= OPTBIT(IN6P_DSTOPTS
|IN6P_RTHDRDSTOPTS
);
1999 error
= sockopt_setint(sopt
, optval
);
2006 case IPV6_RTHDRDSTOPTS
:
2011 case IPV6_USE_MIN_MTU
:
2012 error
= ip6_getpcbopt(in6p
->in6p_outputopts
,
2016 case IPV6_MULTICAST_IF
:
2017 case IPV6_MULTICAST_HOPS
:
2018 case IPV6_MULTICAST_LOOP
:
2019 case IPV6_JOIN_GROUP
:
2020 case IPV6_LEAVE_GROUP
:
2021 error
= ip6_getmoptions(sopt
, in6p
->in6p_moptions
);
2024 #if defined(IPSEC) || defined(FAST_IPSEC)
2025 case IPV6_IPSEC_POLICY
:
2027 struct mbuf
*m
= NULL
;
2029 /* XXX this will return EINVAL as sopt is empty */
2030 error
= ipsec6_get_policy(in6p
, sopt
->sopt_data
,
2031 sopt
->sopt_size
, &m
);
2033 error
= sockopt_setmbuf(sopt
, m
);
2040 error
= ENOPROTOOPT
;
2049 ip6_raw_ctloutput(int op
, struct socket
*so
, struct sockopt
*sopt
)
2051 int error
= 0, optval
;
2052 const int icmp6off
= offsetof(struct icmp6_hdr
, icmp6_cksum
);
2053 struct in6pcb
*in6p
= sotoin6pcb(so
);
2056 KASSERT(sopt
!= NULL
);
2058 level
= sopt
->sopt_level
;
2059 optname
= sopt
->sopt_name
;
2061 if (level
!= IPPROTO_IPV6
) {
2068 * For ICMPv6 sockets, no modification allowed for checksum
2069 * offset, permit "no change" values to help existing apps.
2071 * XXX RFC3542 says: "An attempt to set IPV6_CHECKSUM
2072 * for an ICMPv6 socket will fail." The current
2073 * behavior does not meet RFC3542.
2077 error
= sockopt_getint(sopt
, &optval
);
2080 if ((optval
% 2) != 0) {
2081 /* the API assumes even offset values */
2083 } else if (so
->so_proto
->pr_protocol
==
2085 if (optval
!= icmp6off
)
2088 in6p
->in6p_cksum
= optval
;
2092 if (so
->so_proto
->pr_protocol
== IPPROTO_ICMPV6
)
2095 optval
= in6p
->in6p_cksum
;
2097 error
= sockopt_setint(sopt
, optval
);
2107 error
= ENOPROTOOPT
;
2116 * Set up IP6 options in pcb for insertion in output packets or
2117 * specifying behavior of outgoing packets.
2120 ip6_pcbopts(struct ip6_pktopts
**pktopt
, struct socket
*so
,
2121 struct sockopt
*sopt
)
2123 struct ip6_pktopts
*opt
= *pktopt
;
2127 /* turn off any old options. */
2130 if (opt
->ip6po_pktinfo
|| opt
->ip6po_nexthop
||
2131 opt
->ip6po_hbh
|| opt
->ip6po_dest1
|| opt
->ip6po_dest2
||
2132 opt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
2133 printf("ip6_pcbopts: all specified options are cleared.\n");
2135 ip6_clearpktopts(opt
, -1);
2137 opt
= malloc(sizeof(*opt
), M_IP6OPT
, M_NOWAIT
);
2143 if (sopt
== NULL
|| sopt
->sopt_size
== 0) {
2145 * Only turning off any previous options, regardless of
2146 * whether the opt is just created or given.
2148 free(opt
, M_IP6OPT
);
2152 /* set options specified by user. */
2153 m
= sockopt_getmbuf(sopt
);
2155 free(opt
, M_IP6OPT
);
2159 error
= ip6_setpktopts(m
, opt
, NULL
, kauth_cred_get(),
2160 so
->so_proto
->pr_protocol
);
2163 ip6_clearpktopts(opt
, -1); /* XXX: discard all options */
2164 free(opt
, M_IP6OPT
);
2173 * initialize ip6_pktopts. beware that there are non-zero default values in
2177 ip6_initpktopts(struct ip6_pktopts
*opt
)
2180 memset(opt
, 0, sizeof(*opt
));
2181 opt
->ip6po_hlim
= -1; /* -1 means default hop limit */
2182 opt
->ip6po_tclass
= -1; /* -1 means default traffic class */
2183 opt
->ip6po_minmtu
= IP6PO_MINMTU_MCASTONLY
;
2186 #define sin6tosa(sin6) ((struct sockaddr *)(sin6)) /* XXX */
2188 ip6_pcbopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
**pktopt
,
2189 kauth_cred_t cred
, int uproto
)
2191 struct ip6_pktopts
*opt
;
2193 if (*pktopt
== NULL
) {
2194 *pktopt
= malloc(sizeof(struct ip6_pktopts
), M_IP6OPT
,
2196 if (*pktopt
== NULL
)
2199 ip6_initpktopts(*pktopt
);
2203 return (ip6_setpktopt(optname
, buf
, len
, opt
, cred
, 1, 0, uproto
));
2207 ip6_getpcbopt(struct ip6_pktopts
*pktopt
, int optname
, struct sockopt
*sopt
)
2209 void *optdata
= NULL
;
2211 struct ip6_ext
*ip6e
;
2213 struct in6_pktinfo null_pktinfo
;
2214 int deftclass
= 0, on
;
2215 int defminmtu
= IP6PO_MINMTU_MCASTONLY
;
2219 if (pktopt
&& pktopt
->ip6po_pktinfo
)
2220 optdata
= (void *)pktopt
->ip6po_pktinfo
;
2222 /* XXX: we don't have to do this every time... */
2223 memset(&null_pktinfo
, 0, sizeof(null_pktinfo
));
2224 optdata
= (void *)&null_pktinfo
;
2226 optdatalen
= sizeof(struct in6_pktinfo
);
2232 if (pktopt
&& pktopt
->ip6po_tclass
>= 0)
2233 optdata
= (void *)&pktopt
->ip6po_tclass
;
2235 optdata
= (void *)&deftclass
;
2236 optdatalen
= sizeof(int);
2239 if (pktopt
&& pktopt
->ip6po_hbh
) {
2240 optdata
= (void *)pktopt
->ip6po_hbh
;
2241 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_hbh
;
2242 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2246 if (pktopt
&& pktopt
->ip6po_rthdr
) {
2247 optdata
= (void *)pktopt
->ip6po_rthdr
;
2248 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_rthdr
;
2249 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2252 case IPV6_RTHDRDSTOPTS
:
2253 if (pktopt
&& pktopt
->ip6po_dest1
) {
2254 optdata
= (void *)pktopt
->ip6po_dest1
;
2255 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest1
;
2256 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2260 if (pktopt
&& pktopt
->ip6po_dest2
) {
2261 optdata
= (void *)pktopt
->ip6po_dest2
;
2262 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest2
;
2263 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2267 if (pktopt
&& pktopt
->ip6po_nexthop
) {
2268 optdata
= (void *)pktopt
->ip6po_nexthop
;
2269 optdatalen
= pktopt
->ip6po_nexthop
->sa_len
;
2272 case IPV6_USE_MIN_MTU
:
2274 optdata
= (void *)&pktopt
->ip6po_minmtu
;
2276 optdata
= (void *)&defminmtu
;
2277 optdatalen
= sizeof(int);
2280 if (pktopt
&& ((pktopt
->ip6po_flags
) & IP6PO_DONTFRAG
))
2284 optdata
= (void *)&on
;
2285 optdatalen
= sizeof(on
);
2287 default: /* should not happen */
2289 panic("ip6_getpcbopt: unexpected option\n");
2291 return (ENOPROTOOPT
);
2294 error
= sockopt_set(sopt
, optdata
, optdatalen
);
2300 ip6_clearpktopts(struct ip6_pktopts
*pktopt
, int optname
)
2302 if (optname
== -1 || optname
== IPV6_PKTINFO
) {
2303 if (pktopt
->ip6po_pktinfo
)
2304 free(pktopt
->ip6po_pktinfo
, M_IP6OPT
);
2305 pktopt
->ip6po_pktinfo
= NULL
;
2307 if (optname
== -1 || optname
== IPV6_HOPLIMIT
)
2308 pktopt
->ip6po_hlim
= -1;
2309 if (optname
== -1 || optname
== IPV6_TCLASS
)
2310 pktopt
->ip6po_tclass
= -1;
2311 if (optname
== -1 || optname
== IPV6_NEXTHOP
) {
2312 rtcache_free(&pktopt
->ip6po_nextroute
);
2313 if (pktopt
->ip6po_nexthop
)
2314 free(pktopt
->ip6po_nexthop
, M_IP6OPT
);
2315 pktopt
->ip6po_nexthop
= NULL
;
2317 if (optname
== -1 || optname
== IPV6_HOPOPTS
) {
2318 if (pktopt
->ip6po_hbh
)
2319 free(pktopt
->ip6po_hbh
, M_IP6OPT
);
2320 pktopt
->ip6po_hbh
= NULL
;
2322 if (optname
== -1 || optname
== IPV6_RTHDRDSTOPTS
) {
2323 if (pktopt
->ip6po_dest1
)
2324 free(pktopt
->ip6po_dest1
, M_IP6OPT
);
2325 pktopt
->ip6po_dest1
= NULL
;
2327 if (optname
== -1 || optname
== IPV6_RTHDR
) {
2328 if (pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
2329 free(pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
, M_IP6OPT
);
2330 pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
= NULL
;
2331 rtcache_free(&pktopt
->ip6po_route
);
2333 if (optname
== -1 || optname
== IPV6_DSTOPTS
) {
2334 if (pktopt
->ip6po_dest2
)
2335 free(pktopt
->ip6po_dest2
, M_IP6OPT
);
2336 pktopt
->ip6po_dest2
= NULL
;
2340 #define PKTOPT_EXTHDRCPY(type) \
2343 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2344 dst->type = malloc(hlen, M_IP6OPT, canwait); \
2345 if (dst->type == NULL && canwait == M_NOWAIT) \
2347 memcpy(dst->type, src->type, hlen); \
2349 } while (/*CONSTCOND*/ 0)
2352 copypktopts(struct ip6_pktopts
*dst
, struct ip6_pktopts
*src
, int canwait
)
2354 dst
->ip6po_hlim
= src
->ip6po_hlim
;
2355 dst
->ip6po_tclass
= src
->ip6po_tclass
;
2356 dst
->ip6po_flags
= src
->ip6po_flags
;
2357 if (src
->ip6po_pktinfo
) {
2358 dst
->ip6po_pktinfo
= malloc(sizeof(*dst
->ip6po_pktinfo
),
2360 if (dst
->ip6po_pktinfo
== NULL
&& canwait
== M_NOWAIT
)
2362 *dst
->ip6po_pktinfo
= *src
->ip6po_pktinfo
;
2364 if (src
->ip6po_nexthop
) {
2365 dst
->ip6po_nexthop
= malloc(src
->ip6po_nexthop
->sa_len
,
2367 if (dst
->ip6po_nexthop
== NULL
&& canwait
== M_NOWAIT
)
2369 memcpy(dst
->ip6po_nexthop
, src
->ip6po_nexthop
,
2370 src
->ip6po_nexthop
->sa_len
);
2372 PKTOPT_EXTHDRCPY(ip6po_hbh
);
2373 PKTOPT_EXTHDRCPY(ip6po_dest1
);
2374 PKTOPT_EXTHDRCPY(ip6po_dest2
);
2375 PKTOPT_EXTHDRCPY(ip6po_rthdr
); /* not copy the cached route */
2379 if (dst
->ip6po_pktinfo
) free(dst
->ip6po_pktinfo
, M_IP6OPT
);
2380 if (dst
->ip6po_nexthop
) free(dst
->ip6po_nexthop
, M_IP6OPT
);
2381 if (dst
->ip6po_hbh
) free(dst
->ip6po_hbh
, M_IP6OPT
);
2382 if (dst
->ip6po_dest1
) free(dst
->ip6po_dest1
, M_IP6OPT
);
2383 if (dst
->ip6po_dest2
) free(dst
->ip6po_dest2
, M_IP6OPT
);
2384 if (dst
->ip6po_rthdr
) free(dst
->ip6po_rthdr
, M_IP6OPT
);
2388 #undef PKTOPT_EXTHDRCPY
2390 struct ip6_pktopts
*
2391 ip6_copypktopts(struct ip6_pktopts
*src
, int canwait
)
2394 struct ip6_pktopts
*dst
;
2396 dst
= malloc(sizeof(*dst
), M_IP6OPT
, canwait
);
2397 if (dst
== NULL
&& canwait
== M_NOWAIT
)
2399 ip6_initpktopts(dst
);
2401 if ((error
= copypktopts(dst
, src
, canwait
)) != 0) {
2402 free(dst
, M_IP6OPT
);
2410 ip6_freepcbopts(struct ip6_pktopts
*pktopt
)
2415 ip6_clearpktopts(pktopt
, -1);
2417 free(pktopt
, M_IP6OPT
);
2421 * Set the IP6 multicast options in response to user setsockopt().
2424 ip6_setmoptions(const struct sockopt
*sopt
, struct ip6_moptions
**im6op
)
2427 u_int loop
, ifindex
;
2428 struct ipv6_mreq mreq
;
2430 struct ip6_moptions
*im6o
= *im6op
;
2432 struct in6_multi_mship
*imm
;
2433 struct lwp
*l
= curlwp
; /* XXX */
2437 * No multicast option buffer attached to the pcb;
2438 * allocate one and initialize to default values.
2440 im6o
= malloc(sizeof(*im6o
), M_IPMOPTS
, M_NOWAIT
);
2445 im6o
->im6o_multicast_ifp
= NULL
;
2446 im6o
->im6o_multicast_hlim
= ip6_defmcasthlim
;
2447 im6o
->im6o_multicast_loop
= IPV6_DEFAULT_MULTICAST_LOOP
;
2448 LIST_INIT(&im6o
->im6o_memberships
);
2451 switch (sopt
->sopt_name
) {
2453 case IPV6_MULTICAST_IF
:
2455 * Select the interface for outgoing multicast packets.
2457 error
= sockopt_get(sopt
, &ifindex
, sizeof(ifindex
));
2462 if (if_indexlim
<= ifindex
|| !ifindex2ifnet
[ifindex
]) {
2463 error
= ENXIO
; /* XXX EINVAL? */
2466 ifp
= ifindex2ifnet
[ifindex
];
2467 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2468 error
= EADDRNOTAVAIL
;
2473 im6o
->im6o_multicast_ifp
= ifp
;
2476 case IPV6_MULTICAST_HOPS
:
2479 * Set the IP6 hoplimit for outgoing multicast packets.
2483 error
= sockopt_getint(sopt
, &optval
);
2487 if (optval
< -1 || optval
>= 256)
2489 else if (optval
== -1)
2490 im6o
->im6o_multicast_hlim
= ip6_defmcasthlim
;
2492 im6o
->im6o_multicast_hlim
= optval
;
2496 case IPV6_MULTICAST_LOOP
:
2498 * Set the loopback flag for outgoing multicast packets.
2499 * Must be zero or one.
2501 error
= sockopt_get(sopt
, &loop
, sizeof(loop
));
2508 im6o
->im6o_multicast_loop
= loop
;
2511 case IPV6_JOIN_GROUP
:
2513 * Add a multicast group membership.
2514 * Group must be a valid IP6 multicast address.
2516 error
= sockopt_get(sopt
, &mreq
, sizeof(mreq
));
2520 if (IN6_IS_ADDR_UNSPECIFIED(&mreq
.ipv6mr_multiaddr
)) {
2522 * We use the unspecified address to specify to accept
2523 * all multicast addresses. Only super user is allowed
2526 if (kauth_authorize_generic(l
->l_cred
,
2527 KAUTH_GENERIC_ISSUSER
, NULL
))
2532 } else if (!IN6_IS_ADDR_MULTICAST(&mreq
.ipv6mr_multiaddr
)) {
2538 * If no interface was explicitly specified, choose an
2539 * appropriate one according to the given multicast address.
2541 if (mreq
.ipv6mr_interface
== 0) {
2544 struct sockaddr dst
;
2545 struct sockaddr_in6 dst6
;
2549 * Look up the routing table for the
2550 * address, and choose the outgoing interface.
2551 * XXX: is it a good approach?
2553 memset(&ro
, 0, sizeof(ro
));
2554 sockaddr_in6_init(&u
.dst6
, &mreq
.ipv6mr_multiaddr
, 0,
2556 rtcache_setdst(&ro
, &u
.dst
);
2557 ifp
= (rt
= rtcache_init(&ro
)) != NULL
? rt
->rt_ifp
2562 * If the interface is specified, validate it.
2564 if (if_indexlim
<= mreq
.ipv6mr_interface
||
2565 !ifindex2ifnet
[mreq
.ipv6mr_interface
]) {
2566 error
= ENXIO
; /* XXX EINVAL? */
2569 ifp
= ifindex2ifnet
[mreq
.ipv6mr_interface
];
2573 * See if we found an interface, and confirm that it
2574 * supports multicast
2576 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2577 error
= EADDRNOTAVAIL
;
2581 if (in6_setscope(&mreq
.ipv6mr_multiaddr
, ifp
, NULL
)) {
2582 error
= EADDRNOTAVAIL
; /* XXX: should not happen */
2587 * See if the membership already exists.
2589 for (imm
= im6o
->im6o_memberships
.lh_first
;
2590 imm
!= NULL
; imm
= imm
->i6mm_chain
.le_next
)
2591 if (imm
->i6mm_maddr
->in6m_ifp
== ifp
&&
2592 IN6_ARE_ADDR_EQUAL(&imm
->i6mm_maddr
->in6m_addr
,
2593 &mreq
.ipv6mr_multiaddr
))
2600 * Everything looks good; add a new record to the multicast
2601 * address list for the given interface.
2603 imm
= in6_joingroup(ifp
, &mreq
.ipv6mr_multiaddr
, &error
, 0);
2606 LIST_INSERT_HEAD(&im6o
->im6o_memberships
, imm
, i6mm_chain
);
2609 case IPV6_LEAVE_GROUP
:
2611 * Drop a multicast group membership.
2612 * Group must be a valid IP6 multicast address.
2614 error
= sockopt_get(sopt
, &mreq
, sizeof(mreq
));
2619 * If an interface address was specified, get a pointer
2620 * to its ifnet structure.
2622 if (mreq
.ipv6mr_interface
!= 0) {
2623 if (if_indexlim
<= mreq
.ipv6mr_interface
||
2624 !ifindex2ifnet
[mreq
.ipv6mr_interface
]) {
2625 error
= ENXIO
; /* XXX EINVAL? */
2628 ifp
= ifindex2ifnet
[mreq
.ipv6mr_interface
];
2632 /* Fill in the scope zone ID */
2634 if (in6_setscope(&mreq
.ipv6mr_multiaddr
, ifp
, NULL
)) {
2635 /* XXX: should not happen */
2636 error
= EADDRNOTAVAIL
;
2639 } else if (mreq
.ipv6mr_interface
!= 0) {
2641 * XXX: This case would happens when the (positive)
2642 * index is in the valid range, but the corresponding
2643 * interface has been detached dynamically. The above
2644 * check probably avoids such case to happen here, but
2645 * we check it explicitly for safety.
2647 error
= EADDRNOTAVAIL
;
2649 } else { /* ipv6mr_interface == 0 */
2650 struct sockaddr_in6 sa6_mc
;
2653 * The API spec says as follows:
2654 * If the interface index is specified as 0, the
2655 * system may choose a multicast group membership to
2656 * drop by matching the multicast address only.
2657 * On the other hand, we cannot disambiguate the scope
2658 * zone unless an interface is provided. Thus, we
2659 * check if there's ambiguity with the default scope
2660 * zone as the last resort.
2662 sockaddr_in6_init(&sa6_mc
, &mreq
.ipv6mr_multiaddr
,
2664 error
= sa6_embedscope(&sa6_mc
, ip6_use_defzone
);
2667 mreq
.ipv6mr_multiaddr
= sa6_mc
.sin6_addr
;
2671 * Find the membership in the membership list.
2673 for (imm
= im6o
->im6o_memberships
.lh_first
;
2674 imm
!= NULL
; imm
= imm
->i6mm_chain
.le_next
) {
2675 if ((ifp
== NULL
|| imm
->i6mm_maddr
->in6m_ifp
== ifp
) &&
2676 IN6_ARE_ADDR_EQUAL(&imm
->i6mm_maddr
->in6m_addr
,
2677 &mreq
.ipv6mr_multiaddr
))
2681 /* Unable to resolve interface */
2682 error
= EADDRNOTAVAIL
;
2686 * Give up the multicast address record to which the
2687 * membership points.
2689 LIST_REMOVE(imm
, i6mm_chain
);
2690 in6_leavegroup(imm
);
2699 * If all options have default values, no need to keep the mbuf.
2701 if (im6o
->im6o_multicast_ifp
== NULL
&&
2702 im6o
->im6o_multicast_hlim
== ip6_defmcasthlim
&&
2703 im6o
->im6o_multicast_loop
== IPV6_DEFAULT_MULTICAST_LOOP
&&
2704 im6o
->im6o_memberships
.lh_first
== NULL
) {
2705 free(*im6op
, M_IPMOPTS
);
2713 * Return the IP6 multicast options in response to user getsockopt().
2716 ip6_getmoptions(struct sockopt
*sopt
, struct ip6_moptions
*im6o
)
2721 switch (sopt
->sopt_name
) {
2722 case IPV6_MULTICAST_IF
:
2723 if (im6o
== NULL
|| im6o
->im6o_multicast_ifp
== NULL
)
2726 optval
= im6o
->im6o_multicast_ifp
->if_index
;
2728 error
= sockopt_set(sopt
, &optval
, sizeof(optval
));
2731 case IPV6_MULTICAST_HOPS
:
2733 optval
= ip6_defmcasthlim
;
2735 optval
= im6o
->im6o_multicast_hlim
;
2737 error
= sockopt_set(sopt
, &optval
, sizeof(optval
));
2740 case IPV6_MULTICAST_LOOP
:
2742 optval
= ip6_defmcasthlim
;
2744 optval
= im6o
->im6o_multicast_loop
;
2746 error
= sockopt_set(sopt
, &optval
, sizeof(optval
));
2757 * Discard the IP6 multicast options.
2760 ip6_freemoptions(struct ip6_moptions
*im6o
)
2762 struct in6_multi_mship
*imm
;
2767 while ((imm
= im6o
->im6o_memberships
.lh_first
) != NULL
) {
2768 LIST_REMOVE(imm
, i6mm_chain
);
2769 in6_leavegroup(imm
);
2771 free(im6o
, M_IPMOPTS
);
2775 * Set IPv6 outgoing packet options based on advanced API.
2778 ip6_setpktopts(struct mbuf
*control
, struct ip6_pktopts
*opt
,
2779 struct ip6_pktopts
*stickyopt
, kauth_cred_t cred
, int uproto
)
2781 struct cmsghdr
*cm
= 0;
2783 if (control
== NULL
|| opt
== NULL
)
2786 ip6_initpktopts(opt
);
2791 * If stickyopt is provided, make a local copy of the options
2792 * for this particular packet, then override them by ancillary
2794 * XXX: copypktopts() does not copy the cached route to a next
2795 * hop (if any). This is not very good in terms of efficiency,
2796 * but we can allow this since this option should be rarely
2799 if ((error
= copypktopts(opt
, stickyopt
, M_NOWAIT
)) != 0)
2804 * XXX: Currently, we assume all the optional information is stored
2807 if (control
->m_next
)
2810 /* XXX if cm->cmsg_len is not aligned, control->m_len can become <0 */
2811 for (; control
->m_len
> 0; control
->m_data
+= CMSG_ALIGN(cm
->cmsg_len
),
2812 control
->m_len
-= CMSG_ALIGN(cm
->cmsg_len
)) {
2815 if (control
->m_len
< CMSG_LEN(0))
2818 cm
= mtod(control
, struct cmsghdr
*);
2819 if (cm
->cmsg_len
== 0 || cm
->cmsg_len
> control
->m_len
)
2821 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
2824 error
= ip6_setpktopt(cm
->cmsg_type
, CMSG_DATA(cm
),
2825 cm
->cmsg_len
- CMSG_LEN(0), opt
, cred
, 0, 1, uproto
);
2834 * Set a particular packet option, as a sticky option or an ancillary data
2835 * item. "len" can be 0 only when it's a sticky option.
2836 * We have 4 cases of combination of "sticky" and "cmsg":
2837 * "sticky=0, cmsg=0": impossible
2838 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2839 * "sticky=1, cmsg=0": RFC3542 socket option
2840 * "sticky=1, cmsg=1": RFC2292 socket option
2843 ip6_setpktopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
*opt
,
2844 kauth_cred_t cred
, int sticky
, int cmsg
, int uproto
)
2849 if (!sticky
&& !cmsg
) {
2851 printf("ip6_setpktopt: impossible case\n");
2857 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2858 * not be specified in the context of RFC3542. Conversely,
2859 * RFC3542 types should not be specified in the context of RFC2292.
2863 case IPV6_2292PKTINFO
:
2864 case IPV6_2292HOPLIMIT
:
2865 case IPV6_2292NEXTHOP
:
2866 case IPV6_2292HOPOPTS
:
2867 case IPV6_2292DSTOPTS
:
2868 case IPV6_2292RTHDR
:
2869 case IPV6_2292PKTOPTIONS
:
2870 return (ENOPROTOOPT
);
2873 if (sticky
&& cmsg
) {
2880 case IPV6_RTHDRDSTOPTS
:
2882 case IPV6_USE_MIN_MTU
:
2886 return (ENOPROTOOPT
);
2892 case IPV6_2292PKTINFO
:
2896 struct ifnet
*ifp
= NULL
;
2897 struct in6_pktinfo
*pktinfo
;
2899 if (len
!= sizeof(struct in6_pktinfo
))
2902 pktinfo
= (struct in6_pktinfo
*)buf
;
2905 * An application can clear any sticky IPV6_PKTINFO option by
2906 * doing a "regular" setsockopt with ipi6_addr being
2907 * in6addr_any and ipi6_ifindex being zero.
2908 * [RFC 3542, Section 6]
2910 if (optname
== IPV6_PKTINFO
&& opt
->ip6po_pktinfo
&&
2911 pktinfo
->ipi6_ifindex
== 0 &&
2912 IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
2913 ip6_clearpktopts(opt
, optname
);
2917 if (uproto
== IPPROTO_TCP
&& optname
== IPV6_PKTINFO
&&
2918 sticky
&& !IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
2922 /* validate the interface index if specified. */
2923 if (pktinfo
->ipi6_ifindex
>= if_indexlim
) {
2926 if (pktinfo
->ipi6_ifindex
) {
2927 ifp
= ifindex2ifnet
[pktinfo
->ipi6_ifindex
];
2933 * We store the address anyway, and let in6_selectsrc()
2934 * validate the specified address. This is because ipi6_addr
2935 * may not have enough information about its scope zone, and
2936 * we may need additional information (such as outgoing
2937 * interface or the scope zone of a destination address) to
2938 * disambiguate the scope.
2939 * XXX: the delay of the validation may confuse the
2940 * application when it is used as a sticky option.
2942 if (opt
->ip6po_pktinfo
== NULL
) {
2943 opt
->ip6po_pktinfo
= malloc(sizeof(*pktinfo
),
2944 M_IP6OPT
, M_NOWAIT
);
2945 if (opt
->ip6po_pktinfo
== NULL
)
2948 memcpy(opt
->ip6po_pktinfo
, pktinfo
, sizeof(*pktinfo
));
2953 case IPV6_2292HOPLIMIT
:
2960 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2961 * to simplify the ordering among hoplimit options.
2963 if (optname
== IPV6_HOPLIMIT
&& sticky
)
2964 return (ENOPROTOOPT
);
2966 if (len
!= sizeof(int))
2969 if (*hlimp
< -1 || *hlimp
> 255)
2972 opt
->ip6po_hlim
= *hlimp
;
2977 if (len
!= sizeof(u_int8_t
))
2980 opt
->ip6po_tclass
= *(u_int8_t
*)buf
;
2987 if (len
!= sizeof(int))
2989 tclass
= *(int *)buf
;
2990 if (tclass
< -1 || tclass
> 255)
2993 opt
->ip6po_tclass
= tclass
;
2998 case IPV6_2292NEXTHOP
:
3001 error
= kauth_authorize_generic(cred
, KAUTH_GENERIC_ISSUSER
,
3006 if (len
== 0) { /* just remove the option */
3007 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3011 /* check if cmsg_len is large enough for sa_len */
3012 if (len
< sizeof(struct sockaddr
) || len
< *buf
)
3015 switch (((struct sockaddr
*)buf
)->sa_family
) {
3018 struct sockaddr_in6
*sa6
= (struct sockaddr_in6
*)buf
;
3020 if (sa6
->sin6_len
!= sizeof(struct sockaddr_in6
))
3023 if (IN6_IS_ADDR_UNSPECIFIED(&sa6
->sin6_addr
) ||
3024 IN6_IS_ADDR_MULTICAST(&sa6
->sin6_addr
)) {
3027 if ((error
= sa6_embedscope(sa6
, ip6_use_defzone
))
3033 case AF_LINK
: /* eventually be supported? */
3035 return (EAFNOSUPPORT
);
3038 /* turn off the previous option, then set the new option. */
3039 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3040 opt
->ip6po_nexthop
= malloc(*buf
, M_IP6OPT
, M_NOWAIT
);
3041 if (opt
->ip6po_nexthop
== NULL
)
3043 memcpy(opt
->ip6po_nexthop
, buf
, *buf
);
3047 case IPV6_2292HOPOPTS
:
3051 struct ip6_hbh
*hbh
;
3055 * XXX: We don't allow a non-privileged user to set ANY HbH
3056 * options, since per-option restriction has too much
3059 error
= kauth_authorize_generic(cred
, KAUTH_GENERIC_ISSUSER
,
3065 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3066 break; /* just remove the option */
3069 /* message length validation */
3070 if (len
< sizeof(struct ip6_hbh
))
3072 hbh
= (struct ip6_hbh
*)buf
;
3073 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
3077 /* turn off the previous option, then set the new option. */
3078 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3079 opt
->ip6po_hbh
= malloc(hbhlen
, M_IP6OPT
, M_NOWAIT
);
3080 if (opt
->ip6po_hbh
== NULL
)
3082 memcpy(opt
->ip6po_hbh
, hbh
, hbhlen
);
3088 case IPV6_2292DSTOPTS
:
3091 case IPV6_RTHDRDSTOPTS
:
3093 struct ip6_dest
*dest
, **newdest
= NULL
;
3096 /* XXX: see the comment for IPV6_HOPOPTS */
3097 error
= kauth_authorize_generic(cred
, KAUTH_GENERIC_ISSUSER
,
3103 ip6_clearpktopts(opt
, optname
);
3104 break; /* just remove the option */
3107 /* message length validation */
3108 if (len
< sizeof(struct ip6_dest
))
3110 dest
= (struct ip6_dest
*)buf
;
3111 destlen
= (dest
->ip6d_len
+ 1) << 3;
3115 * Determine the position that the destination options header
3116 * should be inserted; before or after the routing header.
3119 case IPV6_2292DSTOPTS
:
3121 * The old advanced API is ambiguous on this point.
3122 * Our approach is to determine the position based
3123 * according to the existence of a routing header.
3124 * Note, however, that this depends on the order of the
3125 * extension headers in the ancillary data; the 1st
3126 * part of the destination options header must appear
3127 * before the routing header in the ancillary data,
3129 * RFC3542 solved the ambiguity by introducing
3130 * separate ancillary data or option types.
3132 if (opt
->ip6po_rthdr
== NULL
)
3133 newdest
= &opt
->ip6po_dest1
;
3135 newdest
= &opt
->ip6po_dest2
;
3137 case IPV6_RTHDRDSTOPTS
:
3138 newdest
= &opt
->ip6po_dest1
;
3141 newdest
= &opt
->ip6po_dest2
;
3145 /* turn off the previous option, then set the new option. */
3146 ip6_clearpktopts(opt
, optname
);
3147 *newdest
= malloc(destlen
, M_IP6OPT
, M_NOWAIT
);
3148 if (*newdest
== NULL
)
3150 memcpy(*newdest
, dest
, destlen
);
3156 case IPV6_2292RTHDR
:
3160 struct ip6_rthdr
*rth
;
3164 ip6_clearpktopts(opt
, IPV6_RTHDR
);
3165 break; /* just remove the option */
3168 /* message length validation */
3169 if (len
< sizeof(struct ip6_rthdr
))
3171 rth
= (struct ip6_rthdr
*)buf
;
3172 rthlen
= (rth
->ip6r_len
+ 1) << 3;
3175 switch (rth
->ip6r_type
) {
3176 case IPV6_RTHDR_TYPE_0
:
3177 if (rth
->ip6r_len
== 0) /* must contain one addr */
3179 if (rth
->ip6r_len
% 2) /* length must be even */
3181 if (rth
->ip6r_len
/ 2 != rth
->ip6r_segleft
)
3185 return (EINVAL
); /* not supported */
3187 /* turn off the previous option */
3188 ip6_clearpktopts(opt
, IPV6_RTHDR
);
3189 opt
->ip6po_rthdr
= malloc(rthlen
, M_IP6OPT
, M_NOWAIT
);
3190 if (opt
->ip6po_rthdr
== NULL
)
3192 memcpy(opt
->ip6po_rthdr
, rth
, rthlen
);
3196 case IPV6_USE_MIN_MTU
:
3197 if (len
!= sizeof(int))
3199 minmtupolicy
= *(int *)buf
;
3200 if (minmtupolicy
!= IP6PO_MINMTU_MCASTONLY
&&
3201 minmtupolicy
!= IP6PO_MINMTU_DISABLE
&&
3202 minmtupolicy
!= IP6PO_MINMTU_ALL
) {
3205 opt
->ip6po_minmtu
= minmtupolicy
;
3209 if (len
!= sizeof(int))
3212 if (uproto
== IPPROTO_TCP
|| *(int *)buf
== 0) {
3214 * we ignore this option for TCP sockets.
3215 * (RFC3542 leaves this case unspecified.)
3217 opt
->ip6po_flags
&= ~IP6PO_DONTFRAG
;
3219 opt
->ip6po_flags
|= IP6PO_DONTFRAG
;
3223 return (ENOPROTOOPT
);
3224 } /* end of switch */
3230 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3231 * packet to the input queue of a specified interface. Note that this
3232 * calls the output routine of the loopback "driver", but with an interface
3233 * pointer that might NOT be lo0ifp -- easier than replicating that code here.
3236 ip6_mloopback(struct ifnet
*ifp
, struct mbuf
*m
,
3237 const struct sockaddr_in6
*dst
)
3240 struct ip6_hdr
*ip6
;
3242 copym
= m_copy(m
, 0, M_COPYALL
);
3247 * Make sure to deep-copy IPv6 header portion in case the data
3248 * is in an mbuf cluster, so that we can safely override the IPv6
3249 * header portion later.
3251 if ((copym
->m_flags
& M_EXT
) != 0 ||
3252 copym
->m_len
< sizeof(struct ip6_hdr
)) {
3253 copym
= m_pullup(copym
, sizeof(struct ip6_hdr
));
3259 if (copym
->m_len
< sizeof(*ip6
)) {
3265 ip6
= mtod(copym
, struct ip6_hdr
*);
3267 * clear embedded scope identifiers if necessary.
3268 * in6_clearscope will touch the addresses only when necessary.
3270 in6_clearscope(&ip6
->ip6_src
);
3271 in6_clearscope(&ip6
->ip6_dst
);
3273 (void)looutput(ifp
, copym
, (const struct sockaddr
*)dst
, NULL
);
3277 * Chop IPv6 header off from the payload.
3280 ip6_splithdr(struct mbuf
*m
, struct ip6_exthdrs
*exthdrs
)
3283 struct ip6_hdr
*ip6
;
3285 ip6
= mtod(m
, struct ip6_hdr
*);
3286 if (m
->m_len
> sizeof(*ip6
)) {
3287 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
);
3292 M_MOVE_PKTHDR(mh
, m
);
3293 MH_ALIGN(mh
, sizeof(*ip6
));
3294 m
->m_len
-= sizeof(*ip6
);
3295 m
->m_data
+= sizeof(*ip6
);
3298 m
->m_len
= sizeof(*ip6
);
3299 bcopy((void *)ip6
, mtod(m
, void *), sizeof(*ip6
));
3301 exthdrs
->ip6e_ip6
= m
;
3306 * Compute IPv6 extension header length.
3309 ip6_optlen(struct in6pcb
*in6p
)
3313 if (!in6p
->in6p_outputopts
)
3318 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3320 len
+= elen(in6p
->in6p_outputopts
->ip6po_hbh
);
3321 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest1
);
3322 len
+= elen(in6p
->in6p_outputopts
->ip6po_rthdr
);
3323 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest2
);