2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * Copyright (c) 1982, 1986, 1988, 1990, 1993
60 * The Regents of the University of California. All rights reserved.
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in the
69 * documentation and/or other materials provided with the distribution.
70 * 3. All advertising materials mentioning features or use of this software
71 * must display the following acknowledgement:
72 * This product includes software developed by the University of
73 * California, Berkeley and its contributors.
74 * 4. Neither the name of the University nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
90 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
93 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
94 * support for mandatory and extensible security protections. This notice
95 * is included in support of clause 2.2 (b) of the Apple Public License,
99 #include <sys/param.h>
100 #include <sys/malloc.h>
101 #include <sys/mbuf.h>
102 #include <sys/errno.h>
103 #include <sys/protosw.h>
104 #include <sys/socket.h>
105 #include <sys/socketvar.h>
106 #include <sys/systm.h>
107 #include <sys/kernel.h>
108 #include <sys/proc.h>
109 #include <sys/kauth.h>
110 #include <sys/mcache.h>
111 #include <sys/sysctl.h>
112 #include <kern/zalloc.h>
113 #include <libkern/OSByteOrder.h>
115 #include <pexpert/pexpert.h>
116 #include <mach/sdt.h>
119 #include <net/route.h>
120 #include <net/dlil.h>
121 #include <net/net_osdep.h>
123 #include <netinet/in.h>
124 #include <netinet/in_var.h>
125 #include <netinet/ip_var.h>
126 #include <netinet6/in6_var.h>
127 #include <netinet/ip6.h>
128 #include <netinet/kpi_ipfilter_var.h>
130 #include <netinet6/ip6protosw.h>
131 #include <netinet/icmp6.h>
132 #include <netinet6/ip6_var.h>
133 #include <netinet/in_pcb.h>
134 #include <netinet6/nd6.h>
135 #include <netinet6/scope6_var.h>
137 #include <netinet6/ipsec.h>
138 #include <netinet6/ipsec6.h>
139 #include <netkey/key.h>
140 extern int ipsec_bypass
;
144 #include <net/necp.h>
148 #include <security/mac.h>
149 #endif /* CONFIG_MACF_NET */
152 #include <netinet6/ip6_fw.h>
153 #include <netinet/ip_fw.h>
154 #include <netinet/ip_dummynet.h>
155 #endif /* DUMMYNET */
158 #include <net/pfvar.h>
161 static int ip6_copyexthdr(struct mbuf
**, caddr_t
, int);
162 static void ip6_out_cksum_stats(int, u_int32_t
);
163 static int ip6_insert_jumboopt(struct ip6_exthdrs
*, u_int32_t
);
164 static int ip6_insertfraghdr(struct mbuf
*, struct mbuf
*, int,
166 static int ip6_getpmtu(struct route_in6
*, struct route_in6
*,
167 struct ifnet
*, struct in6_addr
*, u_int32_t
*, boolean_t
*);
168 static int ip6_pcbopts(struct ip6_pktopts
**, struct mbuf
*, struct socket
*,
169 struct sockopt
*sopt
);
170 static int ip6_pcbopt(int, u_char
*, int, struct ip6_pktopts
**, int);
171 static int ip6_getpcbopt(struct ip6_pktopts
*, int, struct sockopt
*);
172 static int copypktopts(struct ip6_pktopts
*, struct ip6_pktopts
*, int);
173 static void im6o_trace(struct ip6_moptions
*, int);
174 static int ip6_setpktopt(int, u_char
*, int, struct ip6_pktopts
*, int,
176 static int ip6_splithdr(struct mbuf
*, struct ip6_exthdrs
*);
177 static void ip6_output_checksum(struct ifnet
*, uint32_t, struct mbuf
*,
178 int, uint32_t, uint32_t);
180 #define IM6O_TRACE_HIST_SIZE 32 /* size of trace history */
183 __private_extern__
unsigned int im6o_trace_hist_size
= IM6O_TRACE_HIST_SIZE
;
185 struct ip6_moptions_dbg
{
186 struct ip6_moptions im6o
; /* ip6_moptions */
187 u_int16_t im6o_refhold_cnt
; /* # of IM6O_ADDREF */
188 u_int16_t im6o_refrele_cnt
; /* # of IM6O_REMREF */
190 * Alloc and free callers.
195 * Circular lists of IM6O_ADDREF and IM6O_REMREF callers.
197 ctrace_t im6o_refhold
[IM6O_TRACE_HIST_SIZE
];
198 ctrace_t im6o_refrele
[IM6O_TRACE_HIST_SIZE
];
202 static unsigned int im6o_debug
= 1; /* debugging (enabled) */
204 static unsigned int im6o_debug
; /* debugging (disabled) */
207 static unsigned int im6o_size
; /* size of zone element */
208 static struct zone
*im6o_zone
; /* zone for ip6_moptions */
210 #define IM6O_ZONE_MAX 64 /* maximum elements in zone */
211 #define IM6O_ZONE_NAME "ip6_moptions" /* zone name */
213 SYSCTL_DECL(_net_inet6_ip6
);
215 static int ip6_maxchainsent
= 0;
216 SYSCTL_INT(_net_inet6_ip6
, OID_AUTO
, maxchainsent
,
217 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_maxchainsent
, 0,
218 "use dlil_output_list");
221 * XXX we don't handle mbuf chains yet in nd6_output() so ip6_output_list() only
222 * walks through the packet chain and sends each mbuf separately.
225 ip6_output_list(struct mbuf
*m0
, int packetlist
, struct ip6_pktopts
*opt
,
226 struct route_in6
*ro
, int flags
, struct ip6_moptions
*im6o
,
227 struct ifnet
**ifpp
, struct ip6_out_args
*ip6oa
)
229 #pragma unused(packetlist)
230 struct mbuf
*m
= m0
, *nextpkt
;
235 * Break the chain before calling ip6_output() and free the
236 * mbufs if there was an error.
238 nextpkt
= m
->m_nextpkt
;
240 error
= ip6_output(m
, opt
, ro
, flags
, im6o
, ifpp
, ip6oa
);
243 m_freem_list(nextpkt
);
253 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
254 * header (with pri, len, nxt, hlim, src, dst).
255 * This function may modify ver and hlim only.
256 * The mbuf chain containing the packet will be freed.
257 * The mbuf opt, if present, will not be freed.
259 * If ro is non-NULL and has valid ro->ro_rt, route lookup would be
260 * skipped and ro->ro_rt would be used. Otherwise the result of route
261 * lookup is stored in ro->ro_rt.
263 * type of "mtu": rt_rmx.rmx_mtu is u_int32_t, ifnet.ifr_mtu is int, and
264 * nd_ifinfo.linkmtu is u_int32_t. so we use u_int32_t to hold largest one,
265 * which is rt_rmx.rmx_mtu.
268 ip6_output(struct mbuf
*m0
, struct ip6_pktopts
*opt
, struct route_in6
*ro
,
269 int flags
, struct ip6_moptions
*im6o
, struct ifnet
**ifpp
,
270 struct ip6_out_args
*ip6oa
)
274 struct ifnet
*ifp
= NULL
, *origifp
= NULL
; /* refcnt'd */
275 struct mbuf
*m
, *mprev
;
276 int hlen
, tlen
, len
, off
, nxt0
;
277 struct route_in6
*ro_pmtu
= NULL
;
278 struct rtentry
*rt
= NULL
;
279 struct sockaddr_in6
*dst
, src_sa
, dst_sa
;
281 struct in6_ifaddr
*ia
= NULL
, *src_ia
= NULL
;
283 boolean_t alwaysfrag
= FALSE
;
284 u_int32_t optlen
= 0, plen
= 0, unfragpartlen
= 0;
285 struct ip6_rthdr
*rh
;
286 struct in6_addr finaldst
;
287 ipfilter_t inject_filter_ref
;
288 struct ipf_pktopts
*ippo
= NULL
;
289 struct flowadv
*adv
= NULL
;
292 struct ip6_out_args saved_ip6oa
;
293 struct sockaddr_in6 dst_buf
;
294 #endif /* DUMMYNET */
296 struct socket
*so
= NULL
;
297 struct secpolicy
*sp
= NULL
;
298 struct route_in6
*ipsec_saved_route
= NULL
;
299 boolean_t needipsectun
= FALSE
;
302 necp_kernel_policy_result necp_result
= 0;
303 necp_kernel_policy_result_parameter necp_result_parameter
;
304 necp_kernel_policy_id necp_matched_policy_id
= 0;
307 struct ipf_pktopts ipf_pktopts
;
308 struct ip6_exthdrs exthdrs
;
309 struct route_in6 ip6route
;
311 struct ipsec_output_state ipsec_state
;
314 struct route_in6 necp_route
;
317 struct route_in6 saved_route
;
318 struct route_in6 saved_ro_pmtu
;
319 struct ip_fw_args args
;
320 #endif /* DUMMYNET */
322 #define ipf_pktopts ip6obz.ipf_pktopts
323 #define exthdrs ip6obz.exthdrs
324 #define ip6route ip6obz.ip6route
325 #define ipsec_state ip6obz.ipsec_state
326 #define necp_route ip6obz.necp_route
327 #define saved_route ip6obz.saved_route
328 #define saved_ro_pmtu ip6obz.saved_ro_pmtu
329 #define args ip6obz.args
332 boolean_t select_srcif
: 1;
333 boolean_t hdrsplit
: 1;
334 boolean_t dontfrag
: 1;
336 boolean_t needipsec
: 1;
337 boolean_t noipsec
: 1;
341 } ip6obf
= { .raw
= 0 };
343 VERIFY(m0
->m_flags
& M_PKTHDR
);
345 /* zero out {saved_route, saved_ro_pmtu, ip6route, exthdrs, args} */
346 bzero(&ip6obz
, sizeof (ip6obz
));
349 if (SLIST_EMPTY(&m0
->m_pkthdr
.tags
))
352 /* Grab info from mtags prepended to the chain */
353 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
,
354 KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
355 struct dn_pkt_tag
*dn_tag
;
357 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
358 args
.fwa_pf_rule
= dn_tag
->dn_pf_rule
;
360 bcopy(&dn_tag
->dn_dst6
, &dst_buf
, sizeof (dst_buf
));
362 ifp
= dn_tag
->dn_ifp
;
364 ifnet_reference(ifp
);
365 flags
= dn_tag
->dn_flags
;
366 if (dn_tag
->dn_flags
& IPV6_OUTARGS
) {
367 saved_ip6oa
= dn_tag
->dn_ip6oa
;
368 ip6oa
= &saved_ip6oa
;
371 saved_route
= dn_tag
->dn_ro6
;
373 saved_ro_pmtu
= dn_tag
->dn_ro6_pmtu
;
374 ro_pmtu
= &saved_ro_pmtu
;
375 origifp
= dn_tag
->dn_origifp
;
377 ifnet_reference(origifp
);
378 mtu
= dn_tag
->dn_mtu
;
379 alwaysfrag
= (dn_tag
->dn_alwaysfrag
!= 0);
380 unfragpartlen
= dn_tag
->dn_unfragpartlen
;
382 bcopy(&dn_tag
->dn_exthdrs
, &exthdrs
, sizeof (exthdrs
));
384 m_tag_delete(m0
, tag
);
388 #endif /* DUMMYNET */
391 m
->m_pkthdr
.pkt_flags
&= ~(PKTF_LOOP
|PKTF_IFAINFO
);
394 if (ipsec_bypass
== 0) {
395 so
= ipsec_getsocket(m
);
397 (void) ipsec_setsocket(m
, NULL
);
399 /* If packet is bound to an interface, check bound policies */
400 if ((flags
& IPV6_OUTARGS
) &&
401 (ip6oa
->ip6oa_flags
& IPOAF_BOUND_IF
) &&
402 ip6oa
->ip6oa_boundif
!= IFSCOPE_NONE
) {
403 /* ip6obf.noipsec is a bitfield, use temp integer */
406 if (ipsec6_getpolicybyinterface(m
, IPSEC_DIR_OUTBOUND
,
407 flags
, ip6oa
, &noipsec
, &sp
) != 0)
410 ip6obf
.noipsec
= (noipsec
!= 0);
415 ip6
= mtod(m
, struct ip6_hdr
*);
417 finaldst
= ip6
->ip6_dst
;
418 inject_filter_ref
= ipf_get_inject_filter(m
);
421 if (ip6_doscopedroute
&& (flags
& IPV6_OUTARGS
)) {
423 * In the forwarding case, only the ifscope value is used,
424 * as source interface selection doesn't take place.
426 if ((ip6obf
.select_srcif
= (!(flags
& (IPV6_FORWARDING
|
427 IPV6_UNSPECSRC
| IPV6_FLAG_NOSRCIFSEL
)) &&
428 (ip6oa
->ip6oa_flags
& IP6OAF_SELECT_SRCIF
))))
429 ipf_pktopts
.ippo_flags
|= IPPOF_SELECT_SRCIF
;
431 if ((ip6oa
->ip6oa_flags
& IP6OAF_BOUND_IF
) &&
432 ip6oa
->ip6oa_boundif
!= IFSCOPE_NONE
) {
433 ipf_pktopts
.ippo_flags
|= (IPPOF_BOUND_IF
|
434 (ip6oa
->ip6oa_boundif
<< IPPOF_SHIFT_IFSCOPE
));
437 if (ip6oa
->ip6oa_flags
& IP6OAF_BOUND_SRCADDR
)
438 ipf_pktopts
.ippo_flags
|= IPPOF_BOUND_SRCADDR
;
440 ip6obf
.select_srcif
= FALSE
;
441 if (flags
& IPV6_OUTARGS
) {
442 ip6oa
->ip6oa_boundif
= IFSCOPE_NONE
;
443 ip6oa
->ip6oa_flags
&= ~(IP6OAF_SELECT_SRCIF
|
444 IP6OAF_BOUND_IF
| IP6OAF_BOUND_SRCADDR
);
448 if (flags
& IPV6_OUTARGS
) {
449 if (ip6oa
->ip6oa_flags
& IP6OAF_NO_CELLULAR
)
450 ipf_pktopts
.ippo_flags
|= IPPOF_NO_IFT_CELLULAR
;
451 if (ip6oa
->ip6oa_flags
& IP6OAF_NO_EXPENSIVE
)
452 ipf_pktopts
.ippo_flags
|= IPPOF_NO_IFF_EXPENSIVE
;
453 adv
= &ip6oa
->ip6oa_flowadv
;
454 adv
->code
= FADV_SUCCESS
;
455 ip6oa
->ip6oa_retflags
= 0;
459 if (args
.fwa_pf_rule
) {
460 ip6
= mtod(m
, struct ip6_hdr
*);
461 VERIFY(ro
!= NULL
); /* ro == saved_route */
464 #endif /* DUMMYNET */
466 #define MAKE_EXTHDR(hp, mp) do { \
468 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
469 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
470 ((eh)->ip6e_len + 1) << 3); \
477 /* Hop-by-Hop options header */
478 MAKE_EXTHDR(opt
->ip6po_hbh
, &exthdrs
.ip6e_hbh
);
479 /* Destination options header(1st part) */
480 if (opt
->ip6po_rthdr
) {
482 * Destination options header(1st part)
483 * This only makes sense with a routing header.
484 * See Section 9.2 of RFC 3542.
485 * Disabling this part just for MIP6 convenience is
486 * a bad idea. We need to think carefully about a
487 * way to make the advanced API coexist with MIP6
488 * options, which might automatically be inserted in
491 MAKE_EXTHDR(opt
->ip6po_dest1
, &exthdrs
.ip6e_dest1
);
494 MAKE_EXTHDR(opt
->ip6po_rthdr
, &exthdrs
.ip6e_rthdr
);
495 /* Destination options header(2nd part) */
496 MAKE_EXTHDR(opt
->ip6po_dest2
, &exthdrs
.ip6e_dest2
);
502 necp_matched_policy_id
= necp_ip6_output_find_policy_match (m
, flags
, (flags
& IPV6_OUTARGS
) ? ip6oa
: NULL
,
503 &necp_result
, &necp_result_parameter
);
504 if (necp_matched_policy_id
) {
505 necp_mark_packet_from_ip(m
, necp_matched_policy_id
);
506 switch (necp_result
) {
507 case NECP_KERNEL_POLICY_RESULT_PASS
:
509 case NECP_KERNEL_POLICY_RESULT_DROP
:
510 case NECP_KERNEL_POLICY_RESULT_SOCKET_DIVERT
:
511 /* Flow divert packets should be blocked at the IP layer */
512 error
= EHOSTUNREACH
;
514 case NECP_KERNEL_POLICY_RESULT_IP_TUNNEL
: {
515 /* Verify that the packet is being routed to the tunnel */
516 struct ifnet
*policy_ifp
= necp_get_ifnet_from_result_parameter(&necp_result_parameter
);
517 if (policy_ifp
== ifp
) {
520 if (necp_packet_can_rebind_to_ifnet(m
, policy_ifp
, (struct route
*)&necp_route
, AF_INET6
)) {
521 /* Set scoped index to the tunnel interface, since it is compatible with the packet */
522 /* This will only work for callers who pass IPV6_OUTARGS, but that covers all of the
523 clients we care about today */
524 if (flags
& IPV6_OUTARGS
) {
525 ip6oa
->ip6oa_boundif
= policy_ifp
->if_index
;
526 ip6oa
->ip6oa_flags
|= IP6OAF_BOUND_IF
;
528 if (opt
!= NULL
&& opt
->ip6po_pktinfo
!= NULL
) {
529 opt
->ip6po_pktinfo
->ipi6_ifindex
= policy_ifp
->if_index
;
547 if (ipsec_bypass
!= 0 || ip6obf
.noipsec
)
551 /* get a security policy for this packet */
553 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
,
556 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
,
560 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
568 switch (sp
->policy
) {
569 case IPSEC_POLICY_DISCARD
:
570 case IPSEC_POLICY_GENERATE
:
572 * This packet is just discarded.
574 IPSEC_STAT_INCREMENT(ipsec6stat
.out_polvio
);
577 case IPSEC_POLICY_BYPASS
:
578 case IPSEC_POLICY_NONE
:
579 /* no need to do IPsec. */
580 ip6obf
.needipsec
= FALSE
;
583 case IPSEC_POLICY_IPSEC
:
584 if (sp
->req
== NULL
) {
585 /* acquire a policy */
586 error
= key_spdacquire(sp
);
592 ip6obf
.needipsec
= TRUE
;
596 case IPSEC_POLICY_ENTRUST
:
598 printf("%s: Invalid policy found: %d\n", __func__
, sp
->policy
);
605 * Calculate the total length of the extension header chain.
606 * Keep the length of the unfragmentable part for fragmentation.
609 if (exthdrs
.ip6e_hbh
!= NULL
)
610 optlen
+= exthdrs
.ip6e_hbh
->m_len
;
611 if (exthdrs
.ip6e_dest1
!= NULL
)
612 optlen
+= exthdrs
.ip6e_dest1
->m_len
;
613 if (exthdrs
.ip6e_rthdr
!= NULL
)
614 optlen
+= exthdrs
.ip6e_rthdr
->m_len
;
615 unfragpartlen
= optlen
+ sizeof (struct ip6_hdr
);
617 /* NOTE: we don't add AH/ESP length here. do that later. */
618 if (exthdrs
.ip6e_dest2
!= NULL
)
619 optlen
+= exthdrs
.ip6e_dest2
->m_len
;
622 * If we need IPsec, or there is at least one extension header,
623 * separate IP6 header from the payload.
629 optlen
) && !ip6obf
.hdrsplit
) {
630 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
634 m
= exthdrs
.ip6e_ip6
;
635 ip6obf
.hdrsplit
= TRUE
;
639 ip6
= mtod(m
, struct ip6_hdr
*);
641 /* adjust mbuf packet header length */
642 m
->m_pkthdr
.len
+= optlen
;
643 plen
= m
->m_pkthdr
.len
- sizeof (*ip6
);
645 /* If this is a jumbo payload, insert a jumbo payload option. */
646 if (plen
> IPV6_MAXPACKET
) {
647 if (!ip6obf
.hdrsplit
) {
648 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
652 m
= exthdrs
.ip6e_ip6
;
653 ip6obf
.hdrsplit
= TRUE
;
656 ip6
= mtod(m
, struct ip6_hdr
*);
657 if ((error
= ip6_insert_jumboopt(&exthdrs
, plen
)) != 0)
661 ip6
->ip6_plen
= htons(plen
);
664 * Concatenate headers and fill in next header fields.
665 * Here we have, on "m"
667 * and we insert headers accordingly. Finally, we should be getting:
668 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
670 * during the header composing process, "m" points to IPv6 header.
671 * "mprev" points to an extension header prior to esp.
673 nexthdrp
= &ip6
->ip6_nxt
;
677 * we treat dest2 specially. this makes IPsec processing
678 * much easier. the goal here is to make mprev point the
679 * mbuf prior to dest2.
681 * result: IPv6 dest2 payload
682 * m and mprev will point to IPv6 header.
684 if (exthdrs
.ip6e_dest2
!= NULL
) {
685 if (!ip6obf
.hdrsplit
) {
686 panic("assumption failed: hdr not split");
689 exthdrs
.ip6e_dest2
->m_next
= m
->m_next
;
690 m
->m_next
= exthdrs
.ip6e_dest2
;
691 *mtod(exthdrs
.ip6e_dest2
, u_char
*) = ip6
->ip6_nxt
;
692 ip6
->ip6_nxt
= IPPROTO_DSTOPTS
;
695 #define MAKE_CHAIN(m, mp, p, i) do { \
697 if (!ip6obf.hdrsplit) { \
698 panic("assumption failed: hdr not split"); \
701 *mtod((m), u_char *) = *(p); \
703 p = mtod((m), u_char *); \
704 (m)->m_next = (mp)->m_next; \
705 (mp)->m_next = (m); \
710 * result: IPv6 hbh dest1 rthdr dest2 payload
711 * m will point to IPv6 header. mprev will point to the
712 * extension header prior to dest2 (rthdr in the above case).
714 MAKE_CHAIN(exthdrs
.ip6e_hbh
, mprev
, nexthdrp
, IPPROTO_HOPOPTS
);
715 MAKE_CHAIN(exthdrs
.ip6e_dest1
, mprev
, nexthdrp
, IPPROTO_DSTOPTS
);
716 MAKE_CHAIN(exthdrs
.ip6e_rthdr
, mprev
, nexthdrp
, IPPROTO_ROUTING
);
721 if (ip6obf
.needipsec
&& (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
))
722 in6_delayed_cksum_offset(m
, 0, optlen
, nxt0
);
725 if (!TAILQ_EMPTY(&ipv6_filters
)) {
726 struct ipfilter
*filter
;
727 int seen
= (inject_filter_ref
== NULL
);
730 if (im6o
!= NULL
&& IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
731 ippo
->ippo_flags
|= IPPOF_MCAST_OPTS
;
733 ippo
->ippo_mcast_ifnet
= im6o
->im6o_multicast_ifp
;
734 ippo
->ippo_mcast_ttl
= im6o
->im6o_multicast_hlim
;
735 ippo
->ippo_mcast_loop
= im6o
->im6o_multicast_loop
;
739 /* Hack: embed the scope_id in the destination */
740 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
) &&
741 (ip6
->ip6_dst
.s6_addr16
[1] == 0) && (ro
!= NULL
)) {
743 ip6
->ip6_dst
.s6_addr16
[1] =
744 htons(ro
->ro_dst
.sin6_scope_id
);
748 TAILQ_FOREACH(filter
, &ipv6_filters
, ipf_link
) {
750 * Don't process packet twice if we've already seen it.
753 if ((struct ipfilter
*)inject_filter_ref
==
756 } else if (filter
->ipf_filter
.ipf_output
!= NULL
) {
759 result
= filter
->ipf_filter
.ipf_output(
760 filter
->ipf_filter
.cookie
,
762 if (result
== EJUSTRETURN
) {
774 ip6
= mtod(m
, struct ip6_hdr
*);
775 /* Hack: cleanup embedded scope_id if we put it there */
777 ip6
->ip6_dst
.s6_addr16
[1] = 0;
781 if (ip6obf
.needipsec
) {
785 * pointers after IPsec headers are not valid any more.
786 * other pointers need a great care too.
787 * (IPsec routines should not mangle mbufs prior to AH/ESP)
789 exthdrs
.ip6e_dest2
= NULL
;
791 if (exthdrs
.ip6e_rthdr
!= NULL
) {
792 rh
= mtod(exthdrs
.ip6e_rthdr
, struct ip6_rthdr
*);
793 segleft_org
= rh
->ip6r_segleft
;
794 rh
->ip6r_segleft
= 0;
801 error
= ipsec6_output_trans(&ipsec_state
, nexthdrp
, mprev
,
802 sp
, flags
, &needipsectun
);
805 /* mbuf is already reclaimed in ipsec6_output_trans. */
815 printf("ip6_output (ipsec): error code %d\n",
819 /* don't show these error codes to the user */
825 if (exthdrs
.ip6e_rthdr
!= NULL
) {
826 /* ah6_output doesn't modify mbuf chain */
827 rh
->ip6r_segleft
= segleft_org
;
833 * If there is a routing header, replace the destination address field
834 * with the first hop of the routing header.
836 if (exthdrs
.ip6e_rthdr
!= NULL
) {
837 struct ip6_rthdr0
*rh0
;
838 struct in6_addr
*addr
;
839 struct sockaddr_in6 sa
;
841 rh
= (struct ip6_rthdr
*)
842 (mtod(exthdrs
.ip6e_rthdr
, struct ip6_rthdr
*));
843 switch (rh
->ip6r_type
) {
844 case IPV6_RTHDR_TYPE_0
:
845 rh0
= (struct ip6_rthdr0
*)rh
;
846 addr
= (struct in6_addr
*)(void *)(rh0
+ 1);
849 * construct a sockaddr_in6 form of
852 * XXX: we may not have enough
853 * information about its scope zone;
854 * there is no standard API to pass
855 * the information from the
858 bzero(&sa
, sizeof (sa
));
859 sa
.sin6_family
= AF_INET6
;
860 sa
.sin6_len
= sizeof (sa
);
861 sa
.sin6_addr
= addr
[0];
862 if ((error
= sa6_embedscope(&sa
,
863 ip6_use_defzone
)) != 0) {
866 ip6
->ip6_dst
= sa
.sin6_addr
;
867 bcopy(&addr
[1], &addr
[0], sizeof (struct in6_addr
) *
868 (rh0
->ip6r0_segleft
- 1));
869 addr
[rh0
->ip6r0_segleft
- 1] = finaldst
;
871 in6_clearscope(addr
+ rh0
->ip6r0_segleft
- 1);
873 default: /* is it possible? */
879 /* Source address validation */
880 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) &&
881 !(flags
& IPV6_UNSPECSRC
)) {
883 ip6stat
.ip6s_badscope
++;
886 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
)) {
888 ip6stat
.ip6s_badscope
++;
892 ip6stat
.ip6s_localout
++;
899 bzero((caddr_t
)ro
, sizeof (*ro
));
901 VERIFY(ro_pmtu
== NULL
); /* must not get here if dummynet */
903 if (opt
!= NULL
&& opt
->ip6po_rthdr
)
904 ro
= &opt
->ip6po_route
;
905 dst
= SIN6(&ro
->ro_dst
);
907 if (ro
->ro_rt
!= NULL
)
908 RT_LOCK_ASSERT_NOTHELD(ro
->ro_rt
);
910 * if specified, try to fill in the traffic class field.
911 * do not override if a non-zero value is already set.
912 * we check the diffserv field and the ecn field separately.
914 if (opt
!= NULL
&& opt
->ip6po_tclass
>= 0) {
917 if ((ip6
->ip6_flow
& htonl(0xfc << 20)) == 0)
919 if ((ip6
->ip6_flow
& htonl(0x03 << 20)) == 0)
923 htonl((opt
->ip6po_tclass
& mask
) << 20);
927 /* fill in or override the hop limit field, if necessary. */
928 if (opt
&& opt
->ip6po_hlim
!= -1) {
929 ip6
->ip6_hlim
= opt
->ip6po_hlim
& 0xff;
930 } else if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
933 ip6
->ip6_hlim
= im6o
->im6o_multicast_hlim
;
936 ip6
->ip6_hlim
= ip6_defmcasthlim
;
941 * If there is a cached route, check that it is to the same
942 * destination and is still up. If not, free it and try again.
943 * Test rt_flags without holding rt_lock for performance reasons;
944 * if the route is down it will hopefully be caught by the layer
945 * below (since it uses this route as a hint) or during the
948 if (ROUTE_UNUSABLE(ro
) || dst
->sin6_family
!= AF_INET6
||
949 !IN6_ARE_ADDR_EQUAL(&dst
->sin6_addr
, &ip6
->ip6_dst
))
952 if (ro
->ro_rt
== NULL
) {
953 bzero(dst
, sizeof (*dst
));
954 dst
->sin6_family
= AF_INET6
;
955 dst
->sin6_len
= sizeof (struct sockaddr_in6
);
956 dst
->sin6_addr
= ip6
->ip6_dst
;
959 if (ip6obf
.needipsec
&& needipsectun
) {
961 struct ifnet
*trace_ifp
= (ifpp
!= NULL
) ? (*ifpp
) : NULL
;
962 #endif /* CONFIG_DTRACE */
964 * All the extension headers will become inaccessible
965 * (since they can be encrypted).
966 * Don't panic, we need no more updates to extension headers
967 * on inner IPv6 packet (since they are now encapsulated).
969 * IPv6 [ESP|AH] IPv6 [extension headers] payload
971 bzero(&exthdrs
, sizeof (exthdrs
));
972 exthdrs
.ip6e_ip6
= m
;
975 route_copyout(&ipsec_state
.ro
, (struct route
*)ro
,
976 sizeof (ipsec_state
.ro
));
977 ipsec_state
.dst
= SA(dst
);
979 /* So that we can see packets inside the tunnel */
980 DTRACE_IP6(send
, struct mbuf
*, m
, struct inpcb
*, NULL
,
981 struct ip6_hdr
*, ip6
, struct ifnet
*, trace_ifp
,
982 struct ip
*, NULL
, struct ip6_hdr
*, ip6
);
984 error
= ipsec6_output_tunnel(&ipsec_state
, sp
, flags
);
985 /* tunneled in IPv4? packet is gone */
986 if (ipsec_state
.tunneled
== 4)
989 ipsec_saved_route
= ro
;
990 ro
= (struct route_in6
*)&ipsec_state
.ro
;
991 dst
= SIN6(ipsec_state
.dst
);
993 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
1004 printf("ip6_output (ipsec): error code %d\n",
1008 /* don't show these error codes to the user */
1015 * The packet has been encapsulated so the ifscope
1016 * is no longer valid since it does not apply to the
1017 * outer address: ignore the ifscope.
1019 if (flags
& IPV6_OUTARGS
) {
1020 ip6oa
->ip6oa_boundif
= IFSCOPE_NONE
;
1021 ip6oa
->ip6oa_flags
&= ~IP6OAF_BOUND_IF
;
1023 if (opt
!= NULL
&& opt
->ip6po_pktinfo
!= NULL
) {
1024 if (opt
->ip6po_pktinfo
->ipi6_ifindex
!= IFSCOPE_NONE
)
1025 opt
->ip6po_pktinfo
->ipi6_ifindex
= IFSCOPE_NONE
;
1027 exthdrs
.ip6e_ip6
= m
;
1037 /* adjust pointer */
1038 ip6
= mtod(m
, struct ip6_hdr
*);
1040 if (ip6obf
.select_srcif
) {
1041 bzero(&src_sa
, sizeof (src_sa
));
1042 src_sa
.sin6_family
= AF_INET6
;
1043 src_sa
.sin6_len
= sizeof (src_sa
);
1044 src_sa
.sin6_addr
= ip6
->ip6_src
;
1046 bzero(&dst_sa
, sizeof (dst_sa
));
1047 dst_sa
.sin6_family
= AF_INET6
;
1048 dst_sa
.sin6_len
= sizeof (dst_sa
);
1049 dst_sa
.sin6_addr
= ip6
->ip6_dst
;
1052 * in6_selectroute() might return an ifp with its reference held
1053 * even in the error case, so make sure to release its reference.
1054 * ip6oa may be NULL if IPV6_OUTARGS isn't set.
1056 if ((error
= in6_selectroute(ip6obf
.select_srcif
? &src_sa
: NULL
,
1057 &dst_sa
, opt
, im6o
, &src_ia
, ro
, &ifp
, &rt
, 0, ip6oa
)) != 0) {
1060 ip6stat
.ip6s_noroute
++;
1064 break; /* XXX statistics? */
1067 in6_ifstat_inc(ifp
, ifs6_out_discard
);
1068 /* ifp (if non-NULL) will be released at the end */
1073 * If in6_selectroute() does not return a route entry,
1074 * dst may not have been updated.
1076 *dst
= dst_sa
; /* XXX */
1080 * then rt (for unicast) and ifp must be non-NULL valid values.
1082 if (!(flags
& IPV6_FORWARDING
)) {
1083 in6_ifstat_inc_na(ifp
, ifs6_out_request
);
1087 ia
= (struct in6_ifaddr
*)(rt
->rt_ifa
);
1089 IFA_ADDREF(&ia
->ia_ifa
);
1095 * The outgoing interface must be in the zone of source and
1096 * destination addresses (except local/loopback). We should
1097 * use ia_ifp to support the case of sending packets to an
1098 * address of our own.
1100 if (ia
!= NULL
&& ia
->ia_ifp
) {
1101 ifnet_reference(ia
->ia_ifp
); /* for origifp */
1102 if (origifp
!= NULL
)
1103 ifnet_release(origifp
);
1104 origifp
= ia
->ia_ifp
;
1107 ifnet_reference(ifp
); /* for origifp */
1108 if (origifp
!= NULL
)
1109 ifnet_release(origifp
);
1113 /* skip scope enforcements for local/loopback route */
1114 if (rt
== NULL
|| !(rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
)) {
1115 struct in6_addr src0
, dst0
;
1118 src0
= ip6
->ip6_src
;
1119 if (in6_setscope(&src0
, origifp
, &zone
))
1121 bzero(&src_sa
, sizeof (src_sa
));
1122 src_sa
.sin6_family
= AF_INET6
;
1123 src_sa
.sin6_len
= sizeof (src_sa
);
1124 src_sa
.sin6_addr
= ip6
->ip6_src
;
1125 if ((sa6_recoverscope(&src_sa
, TRUE
) ||
1126 zone
!= src_sa
.sin6_scope_id
))
1129 dst0
= ip6
->ip6_dst
;
1130 if ((in6_setscope(&dst0
, origifp
, &zone
)))
1132 /* re-initialize to be sure */
1133 bzero(&dst_sa
, sizeof (dst_sa
));
1134 dst_sa
.sin6_family
= AF_INET6
;
1135 dst_sa
.sin6_len
= sizeof (dst_sa
);
1136 dst_sa
.sin6_addr
= ip6
->ip6_dst
;
1137 if ((sa6_recoverscope(&dst_sa
, TRUE
) ||
1138 zone
!= dst_sa
.sin6_scope_id
))
1141 /* scope check is done. */
1145 ip6stat
.ip6s_badscope
++;
1146 in6_ifstat_inc(origifp
, ifs6_out_discard
);
1148 error
= EHOSTUNREACH
; /* XXX */
1153 if (rt
!= NULL
&& !IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1154 if (opt
!= NULL
&& opt
->ip6po_nextroute
.ro_rt
) {
1156 * The nexthop is explicitly specified by the
1157 * application. We assume the next hop is an IPv6
1160 dst
= SIN6(opt
->ip6po_nexthop
);
1161 } else if ((rt
->rt_flags
& RTF_GATEWAY
)) {
1162 dst
= SIN6(rt
->rt_gateway
);
1165 * For packets destined to local/loopback, record the
1166 * source the source interface (which owns the source
1167 * address), as well as the output interface. This is
1168 * needed to reconstruct the embedded zone for the
1169 * link-local address case in ip6_input().
1171 if (ia
!= NULL
&& (ifp
->if_flags
& IFF_LOOPBACK
)) {
1175 srcidx
= src_ia
->ia_ifp
->if_index
;
1176 else if (ro
->ro_srcia
!= NULL
)
1177 srcidx
= ro
->ro_srcia
->ifa_ifp
->if_index
;
1181 ip6_setsrcifaddr_info(m
, srcidx
, NULL
);
1182 ip6_setdstifaddr_info(m
, 0, ia
);
1186 if (!IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1187 m
->m_flags
&= ~(M_BCAST
| M_MCAST
); /* just in case */
1189 struct in6_multi
*in6m
;
1191 m
->m_flags
= (m
->m_flags
& ~M_BCAST
) | M_MCAST
;
1192 in6_ifstat_inc_na(ifp
, ifs6_out_mcast
);
1195 * Confirm that the outgoing interface supports multicast.
1197 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
1198 ip6stat
.ip6s_noroute
++;
1199 in6_ifstat_inc(ifp
, ifs6_out_discard
);
1200 error
= ENETUNREACH
;
1203 in6_multihead_lock_shared();
1204 IN6_LOOKUP_MULTI(&ip6
->ip6_dst
, ifp
, in6m
);
1205 in6_multihead_lock_done();
1209 (im6o
== NULL
|| im6o
->im6o_multicast_loop
)) {
1213 * If we belong to the destination multicast group
1214 * on the outgoing interface, and the caller did not
1215 * forbid loopback, loop back a copy.
1217 ip6_mloopback(NULL
, ifp
, m
, dst
, optlen
, nxt0
);
1218 } else if (im6o
!= NULL
)
1223 * Multicasts with a hoplimit of zero may be looped back,
1224 * above, but must not be transmitted on a network.
1225 * Also, multicasts addressed to the loopback interface
1226 * are not sent -- the above call to ip6_mloopback() will
1227 * loop back a copy if this host actually belongs to the
1228 * destination group on the loopback interface.
1230 if (ip6
->ip6_hlim
== 0 || (ifp
->if_flags
& IFF_LOOPBACK
) ||
1231 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6
->ip6_dst
)) {
1238 * Fill the outgoing inteface to tell the upper layer
1239 * to increment per-interface statistics.
1242 ifnet_reference(ifp
); /* for caller */
1244 ifnet_release(*ifpp
);
1248 /* Determine path MTU. */
1249 if ((error
= ip6_getpmtu(ro_pmtu
, ro
, ifp
, &finaldst
, &mtu
,
1254 * The caller of this function may specify to use the minimum MTU
1256 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
1257 * setting. The logic is a bit complicated; by default, unicast
1258 * packets will follow path MTU while multicast packets will be sent at
1259 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
1260 * including unicast ones will be sent at the minimum MTU. Multicast
1261 * packets will always be sent at the minimum MTU unless
1262 * IP6PO_MINMTU_DISABLE is explicitly specified.
1263 * See RFC 3542 for more details.
1265 if (mtu
> IPV6_MMTU
) {
1266 if ((flags
& IPV6_MINMTU
)) {
1268 } else if (opt
&& opt
->ip6po_minmtu
== IP6PO_MINMTU_ALL
) {
1270 } else if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) &&
1272 opt
->ip6po_minmtu
!= IP6PO_MINMTU_DISABLE
)) {
1278 * clear embedded scope identifiers if necessary.
1279 * in6_clearscope will touch the addresses only when necessary.
1281 in6_clearscope(&ip6
->ip6_src
);
1282 in6_clearscope(&ip6
->ip6_dst
);
1286 * Check with the firewall...
1288 if (ip6_fw_enable
&& ip6_fw_chk_ptr
) {
1290 m
->m_pkthdr
.rcvif
= NULL
; /* XXX */
1291 /* If ipfw says divert, we have to just drop packet */
1292 if (ip6_fw_chk_ptr(&ip6
, ifp
, &port
, &m
)) {
1304 * If the outgoing packet contains a hop-by-hop options header,
1305 * it must be examined and processed even by the source node.
1306 * (RFC 2460, section 4.)
1308 if (exthdrs
.ip6e_hbh
!= NULL
) {
1309 struct ip6_hbh
*hbh
= mtod(exthdrs
.ip6e_hbh
, struct ip6_hbh
*);
1310 u_int32_t dummy
; /* XXX unused */
1311 uint32_t oplen
= 0; /* for ip6_process_hopopts() */
1313 if ((hbh
->ip6h_len
+ 1) << 3 > exthdrs
.ip6e_hbh
->m_len
)
1314 panic("ip6e_hbh is not continuous");
1317 * XXX: If we have to send an ICMPv6 error to the sender,
1318 * we need the M_LOOP flag since icmp6_error() expects
1319 * the IPv6 and the hop-by-hop options header are
1320 * continuous unless the flag is set.
1322 m
->m_flags
|= M_LOOP
;
1323 m
->m_pkthdr
.rcvif
= ifp
;
1324 if (ip6_process_hopopts(m
, (u_int8_t
*)(hbh
+ 1),
1325 ((hbh
->ip6h_len
+ 1) << 3) - sizeof (struct ip6_hbh
),
1326 &dummy
, &oplen
) < 0) {
1327 /* m was already freed at this point */
1328 error
= EINVAL
; /* better error? */
1331 m
->m_flags
&= ~M_LOOP
; /* XXX */
1332 m
->m_pkthdr
.rcvif
= NULL
;
1337 #endif /* DUMMYNET */
1339 if (PF_IS_ENABLED
) {
1342 * TODO: Need to save opt->ip6po_flags for reinjection
1347 args
.fwa_oflags
= flags
;
1348 if (flags
& IPV6_OUTARGS
)
1349 args
.fwa_ip6oa
= ip6oa
;
1351 args
.fwa_dst6
= dst
;
1352 args
.fwa_ro6_pmtu
= ro_pmtu
;
1353 args
.fwa_origifp
= origifp
;
1355 args
.fwa_alwaysfrag
= alwaysfrag
;
1356 args
.fwa_unfragpartlen
= unfragpartlen
;
1357 args
.fwa_exthdrs
= &exthdrs
;
1358 /* Invoke outbound packet filter */
1359 error
= pf_af_hook(ifp
, NULL
, &m
, AF_INET6
, FALSE
, &args
);
1360 #else /* !DUMMYNET */
1361 error
= pf_af_hook(ifp
, NULL
, &m
, AF_INET6
, FALSE
, NULL
);
1362 #endif /* !DUMMYNET */
1364 if (error
!= 0 || m
== NULL
) {
1366 * Note that if we ever handle packet chain, we will
1367 * have to restore the linkage from the previous
1368 * packet to the next like in ip_outout_list()
1371 panic("%s: unexpected packet %p\n",
1375 /* Already freed by callee */
1378 ip6
= mtod(m
, struct ip6_hdr
*);
1383 * Send the packet to the outgoing interface.
1384 * If necessary, do IPv6 fragmentation before sending.
1386 * the logic here is rather complex:
1387 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
1388 * 1-a: send as is if tlen <= path mtu
1389 * 1-b: fragment if tlen > path mtu
1391 * 2: if user asks us not to fragment (dontfrag == 1)
1392 * 2-a: send as is if tlen <= interface mtu
1393 * 2-b: error if tlen > interface mtu
1395 * 3: if we always need to attach fragment header (alwaysfrag == 1)
1398 * 4: if dontfrag == 1 && alwaysfrag == 1
1399 * error, as we cannot handle this conflicting request
1401 tlen
= m
->m_pkthdr
.len
;
1403 if (opt
!= NULL
&& (opt
->ip6po_flags
& IP6PO_DONTFRAG
))
1404 ip6obf
.dontfrag
= TRUE
;
1406 ip6obf
.dontfrag
= FALSE
;
1407 if (ip6obf
.dontfrag
&& alwaysfrag
) { /* case 4 */
1408 /* conflicting request - can't transmit */
1413 lck_rw_lock_shared(nd_if_rwlock
);
1414 /* Access without acquiring nd_ifinfo lock for performance */
1415 if (ip6obf
.dontfrag
&& tlen
> IN6_LINKMTU(ifp
)) { /* case 2-b */
1416 lck_rw_done(nd_if_rwlock
);
1418 * Even if the DONTFRAG option is specified, we cannot send the
1419 * packet when the data length is larger than the MTU of the
1420 * outgoing interface.
1421 * Notify the error by sending IPV6_PATHMTU ancillary data as
1422 * well as returning an error code (the latter is not described
1426 struct ip6ctlparam ip6cp
;
1428 mtu32
= (u_int32_t
)mtu
;
1429 bzero(&ip6cp
, sizeof (ip6cp
));
1430 ip6cp
.ip6c_cmdarg
= (void *)&mtu32
;
1431 pfctlinput2(PRC_MSGSIZE
, SA(&ro_pmtu
->ro_dst
), (void *)&ip6cp
);
1435 lck_rw_done(nd_if_rwlock
);
1439 * transmit packet without fragmentation
1441 if (ip6obf
.dontfrag
|| (!alwaysfrag
&& /* case 1-a and 2-a */
1442 (tlen
<= mtu
|| TSO_IPV6_OK(ifp
, m
) ||
1443 (ifp
->if_hwassist
& CSUM_FRAGMENT_IPV6
)))) {
1445 /* clean ipsec history once it goes out of the node */
1449 ip6_output_checksum(ifp
, mtu
, m
, nxt0
, tlen
, optlen
);
1452 RT_LOCK_ASSERT_NOTHELD(ro
->ro_rt
);
1453 error
= nd6_output(ifp
, origifp
, m
, dst
, ro
->ro_rt
, adv
);
1458 * try to fragment the packet. case 1-b and 3
1460 if ((m
->m_pkthdr
.csum_flags
& CSUM_TSO_IPV6
)) {
1461 /* TSO and fragment aren't compatible */
1463 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1465 } else if (mtu
< IPV6_MMTU
) {
1466 /* path MTU cannot be less than IPV6_MMTU */
1468 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1470 } else if (ip6
->ip6_plen
== 0) {
1471 /* jumbo payload cannot be fragmented */
1473 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1476 struct mbuf
**mnext
, *m_frgpart
;
1477 struct ip6_frag
*ip6f
;
1478 u_int32_t id
= htonl(ip6_randomid());
1482 * Too large for the destination or interface;
1483 * fragment if possible.
1484 * Must be able to put at least 8 bytes per fragment.
1486 hlen
= unfragpartlen
;
1487 if (mtu
> IPV6_MAXPACKET
)
1488 mtu
= IPV6_MAXPACKET
;
1490 len
= (mtu
- hlen
- sizeof (struct ip6_frag
)) & ~7;
1493 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1497 mnext
= &m
->m_nextpkt
;
1500 * Change the next header field of the last header in the
1501 * unfragmentable part.
1503 if (exthdrs
.ip6e_rthdr
!= NULL
) {
1504 nextproto
= *mtod(exthdrs
.ip6e_rthdr
, u_char
*);
1505 *mtod(exthdrs
.ip6e_rthdr
, u_char
*) = IPPROTO_FRAGMENT
;
1506 } else if (exthdrs
.ip6e_dest1
!= NULL
) {
1507 nextproto
= *mtod(exthdrs
.ip6e_dest1
, u_char
*);
1508 *mtod(exthdrs
.ip6e_dest1
, u_char
*) = IPPROTO_FRAGMENT
;
1509 } else if (exthdrs
.ip6e_hbh
!= NULL
) {
1510 nextproto
= *mtod(exthdrs
.ip6e_hbh
, u_char
*);
1511 *mtod(exthdrs
.ip6e_hbh
, u_char
*) = IPPROTO_FRAGMENT
;
1513 nextproto
= ip6
->ip6_nxt
;
1514 ip6
->ip6_nxt
= IPPROTO_FRAGMENT
;
1517 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
)
1518 in6_delayed_cksum_offset(m
, 0, optlen
, nxt0
);
1521 * Loop through length of segment after first fragment,
1522 * make new header and copy data of each part and link onto
1526 for (off
= hlen
; off
< tlen
; off
+= len
) {
1527 struct ip6_hdr
*mhip6
;
1529 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1532 ip6stat
.ip6s_odropped
++;
1535 m
->m_pkthdr
.rcvif
= NULL
;
1536 m
->m_flags
= m0
->m_flags
& M_COPYFLAGS
;
1538 mnext
= &m
->m_nextpkt
;
1539 m
->m_data
+= max_linkhdr
;
1540 mhip6
= mtod(m
, struct ip6_hdr
*);
1542 m
->m_len
= sizeof (*mhip6
);
1543 error
= ip6_insertfraghdr(m0
, m
, hlen
, &ip6f
);
1545 ip6stat
.ip6s_odropped
++;
1548 ip6f
->ip6f_offlg
= htons((u_short
)((off
- hlen
) & ~7));
1549 if (off
+ len
>= tlen
)
1552 ip6f
->ip6f_offlg
|= IP6F_MORE_FRAG
;
1553 mhip6
->ip6_plen
= htons((u_short
)(len
+ hlen
+
1554 sizeof (*ip6f
) - sizeof (struct ip6_hdr
)));
1555 if ((m_frgpart
= m_copy(m0
, off
, len
)) == NULL
) {
1557 ip6stat
.ip6s_odropped
++;
1560 m_cat(m
, m_frgpart
);
1561 m
->m_pkthdr
.len
= len
+ hlen
+ sizeof (*ip6f
);
1562 m
->m_pkthdr
.rcvif
= NULL
;
1564 M_COPY_CLASSIFIER(m
, m0
);
1565 M_COPY_PFTAG(m
, m0
);
1569 mac_create_fragment(m0
, m
);
1570 #endif /* CONFIG_MACF_NET */
1573 ip6f
->ip6f_reserved
= 0;
1574 ip6f
->ip6f_ident
= id
;
1575 ip6f
->ip6f_nxt
= nextproto
;
1576 ip6stat
.ip6s_ofragments
++;
1577 in6_ifstat_inc(ifp
, ifs6_out_fragcreat
);
1580 in6_ifstat_inc(ifp
, ifs6_out_fragok
);
1584 * Remove leading garbages.
1588 m0
->m_nextpkt
= NULL
;
1590 for (m0
= m
; m
!= NULL
; m
= m0
) {
1592 m
->m_nextpkt
= NULL
;
1595 /* clean ipsec history once it goes out of the node */
1598 error
= nd6_output(ifp
, origifp
, m
, dst
, ro
->ro_rt
,
1606 ip6stat
.ip6s_fragmented
++;
1609 ROUTE_RELEASE(&ip6route
);
1611 ROUTE_RELEASE(&ipsec_state
.ro
);
1613 key_freesp(sp
, KEY_SADB_UNLOCKED
);
1616 ROUTE_RELEASE(&necp_route
);
1619 ROUTE_RELEASE(&saved_route
);
1620 ROUTE_RELEASE(&saved_ro_pmtu
);
1621 #endif /* DUMMYNET */
1624 IFA_REMREF(&ia
->ia_ifa
);
1626 IFA_REMREF(&src_ia
->ia_ifa
);
1629 if (origifp
!= NULL
)
1630 ifnet_release(origifp
);
1634 if (exthdrs
.ip6e_hbh
!= NULL
)
1635 m_freem(exthdrs
.ip6e_hbh
);
1636 if (exthdrs
.ip6e_dest1
!= NULL
)
1637 m_freem(exthdrs
.ip6e_dest1
);
1638 if (exthdrs
.ip6e_rthdr
!= NULL
)
1639 m_freem(exthdrs
.ip6e_rthdr
);
1640 if (exthdrs
.ip6e_dest2
!= NULL
)
1641 m_freem(exthdrs
.ip6e_dest2
);
1653 #undef saved_ro_pmtu
1658 ip6_copyexthdr(struct mbuf
**mp
, caddr_t hdr
, int hlen
)
1662 if (hlen
> MCLBYTES
)
1663 return (ENOBUFS
); /* XXX */
1665 MGET(m
, M_DONTWAIT
, MT_DATA
);
1670 MCLGET(m
, M_DONTWAIT
);
1671 if (!(m
->m_flags
& M_EXT
)) {
1678 bcopy(hdr
, mtod(m
, caddr_t
), hlen
);
1685 ip6_out_cksum_stats(int proto
, u_int32_t len
)
1689 tcp_out6_cksum_stats(len
);
1692 udp_out6_cksum_stats(len
);
1695 /* keep only TCP or UDP stats for now */
1701 * Process a delayed payload checksum calculation (outbound path.)
1703 * hoff is the number of bytes beyond the mbuf data pointer which
1704 * points to the IPv6 header. optlen is the number of bytes, if any,
1705 * between the end of IPv6 header and the beginning of the ULP payload
1706 * header, which represents the extension headers. If optlen is less
1707 * than zero, this routine will bail when it detects extension headers.
1709 * Returns a bitmask representing all the work done in software.
1712 in6_finalize_cksum(struct mbuf
*m
, uint32_t hoff
, int32_t optlen
,
1713 int32_t nxt0
, uint32_t csum_flags
)
1715 unsigned char buf
[sizeof (struct ip6_hdr
)] __attribute__((aligned(8)));
1716 struct ip6_hdr
*ip6
;
1717 uint32_t offset
, mlen
, hlen
, olen
, sw_csum
;
1718 uint16_t csum
, ulpoff
, plen
;
1721 _CASSERT(sizeof (csum
) == sizeof (uint16_t));
1722 VERIFY(m
->m_flags
& M_PKTHDR
);
1724 sw_csum
= (csum_flags
& m
->m_pkthdr
.csum_flags
);
1726 if ((sw_csum
&= CSUM_DELAY_IPV6_DATA
) == 0)
1729 mlen
= m
->m_pkthdr
.len
; /* total mbuf len */
1730 hlen
= sizeof (*ip6
); /* IPv6 header len */
1732 /* sanity check (need at least IPv6 header) */
1733 if (mlen
< (hoff
+ hlen
)) {
1734 panic("%s: mbuf %p pkt len (%u) < hoff+ip6_hdr "
1735 "(%u+%u)\n", __func__
, m
, mlen
, hoff
, hlen
);
1740 * In case the IPv6 header is not contiguous, or not 32-bit
1741 * aligned, copy it to a local buffer.
1743 if ((hoff
+ hlen
) > m
->m_len
||
1744 !IP6_HDR_ALIGNED_P(mtod(m
, caddr_t
) + hoff
)) {
1745 m_copydata(m
, hoff
, hlen
, (caddr_t
)buf
);
1746 ip6
= (struct ip6_hdr
*)(void *)buf
;
1748 ip6
= (struct ip6_hdr
*)(void *)(m
->m_data
+ hoff
);
1752 plen
= ntohs(ip6
->ip6_plen
);
1753 if (plen
!= (mlen
- (hoff
+ hlen
))) {
1754 plen
= OSSwapInt16(plen
);
1755 if (plen
!= (mlen
- (hoff
+ hlen
))) {
1756 /* Don't complain for jumbograms */
1757 if (plen
!= 0 || nxt
!= IPPROTO_HOPOPTS
) {
1758 printf("%s: mbuf 0x%llx proto %d IPv6 "
1759 "plen %d (%x) [swapped %d (%x)] doesn't "
1760 "match actual packet length; %d is used "
1761 "instead\n", __func__
,
1762 (uint64_t)VM_KERNEL_ADDRPERM(m
), nxt
,
1763 ip6
->ip6_plen
, ip6
->ip6_plen
, plen
, plen
,
1764 (mlen
- (hoff
+ hlen
)));
1766 plen
= mlen
- (hoff
+ hlen
);
1771 /* next header isn't TCP/UDP and we don't know optlen, bail */
1772 if (nxt
!= IPPROTO_TCP
&& nxt
!= IPPROTO_UDP
) {
1778 /* caller supplied the original transport number; use it */
1784 offset
= hoff
+ hlen
+ olen
; /* ULP header */
1787 if (mlen
< offset
) {
1788 panic("%s: mbuf %p pkt len (%u) < hoff+ip6_hdr+ext_hdr "
1789 "(%u+%u+%u)\n", __func__
, m
, mlen
, hoff
, hlen
, olen
);
1794 * offset is added to the lower 16-bit value of csum_data,
1795 * which is expected to contain the ULP offset; therefore
1796 * CSUM_PARTIAL offset adjustment must be undone.
1798 if ((m
->m_pkthdr
.csum_flags
& (CSUM_PARTIAL
|CSUM_DATA_VALID
)) ==
1799 (CSUM_PARTIAL
|CSUM_DATA_VALID
)) {
1801 * Get back the original ULP offset (this will
1802 * undo the CSUM_PARTIAL logic in ip6_output.)
1804 m
->m_pkthdr
.csum_data
= (m
->m_pkthdr
.csum_tx_stuff
-
1805 m
->m_pkthdr
.csum_tx_start
);
1808 ulpoff
= (m
->m_pkthdr
.csum_data
& 0xffff); /* ULP csum offset */
1810 if (mlen
< (ulpoff
+ sizeof (csum
))) {
1811 panic("%s: mbuf %p pkt len (%u) proto %d invalid ULP "
1812 "cksum offset (%u) cksum flags 0x%x\n", __func__
,
1813 m
, mlen
, nxt
, ulpoff
, m
->m_pkthdr
.csum_flags
);
1817 csum
= inet6_cksum(m
, 0, offset
, plen
- olen
);
1820 ip6_out_cksum_stats(nxt
, plen
- olen
);
1822 /* RFC1122 4.1.3.4 */
1823 if (csum
== 0 && (m
->m_pkthdr
.csum_flags
& CSUM_UDPIPV6
))
1826 /* Insert the checksum in the ULP csum field */
1828 if ((offset
+ sizeof (csum
)) > m
->m_len
) {
1829 m_copyback(m
, offset
, sizeof (csum
), &csum
);
1830 } else if (IP6_HDR_ALIGNED_P(mtod(m
, char *) + hoff
)) {
1831 *(uint16_t *)(void *)(mtod(m
, char *) + offset
) = csum
;
1833 bcopy(&csum
, (mtod(m
, char *) + offset
), sizeof (csum
));
1835 m
->m_pkthdr
.csum_flags
&=
1836 ~(CSUM_DELAY_IPV6_DATA
| CSUM_DATA_VALID
| CSUM_PARTIAL
);
1843 * Insert jumbo payload option.
1846 ip6_insert_jumboopt(struct ip6_exthdrs
*exthdrs
, u_int32_t plen
)
1852 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1855 * If there is no hop-by-hop options header, allocate new one.
1856 * If there is one but it doesn't have enough space to store the
1857 * jumbo payload option, allocate a cluster to store the whole options.
1858 * Otherwise, use it to store the options.
1860 if (exthdrs
->ip6e_hbh
== NULL
) {
1861 MGET(mopt
, M_DONTWAIT
, MT_DATA
);
1864 mopt
->m_len
= JUMBOOPTLEN
;
1865 optbuf
= mtod(mopt
, u_char
*);
1866 optbuf
[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1867 exthdrs
->ip6e_hbh
= mopt
;
1869 struct ip6_hbh
*hbh
;
1871 mopt
= exthdrs
->ip6e_hbh
;
1872 if (M_TRAILINGSPACE(mopt
) < JUMBOOPTLEN
) {
1875 * - exthdrs->ip6e_hbh is not referenced from places
1876 * other than exthdrs.
1877 * - exthdrs->ip6e_hbh is not an mbuf chain.
1879 u_int32_t oldoptlen
= mopt
->m_len
;
1883 * XXX: give up if the whole (new) hbh header does
1884 * not fit even in an mbuf cluster.
1886 if (oldoptlen
+ JUMBOOPTLEN
> MCLBYTES
)
1890 * As a consequence, we must always prepare a cluster
1893 MGET(n
, M_DONTWAIT
, MT_DATA
);
1895 MCLGET(n
, M_DONTWAIT
);
1896 if (!(n
->m_flags
& M_EXT
)) {
1903 n
->m_len
= oldoptlen
+ JUMBOOPTLEN
;
1904 bcopy(mtod(mopt
, caddr_t
), mtod(n
, caddr_t
),
1906 optbuf
= mtod(n
, u_char
*) + oldoptlen
;
1908 mopt
= exthdrs
->ip6e_hbh
= n
;
1910 optbuf
= mtod(mopt
, u_char
*) + mopt
->m_len
;
1911 mopt
->m_len
+= JUMBOOPTLEN
;
1913 optbuf
[0] = IP6OPT_PADN
;
1917 * Adjust the header length according to the pad and
1918 * the jumbo payload option.
1920 hbh
= mtod(mopt
, struct ip6_hbh
*);
1921 hbh
->ip6h_len
+= (JUMBOOPTLEN
>> 3);
1924 /* fill in the option. */
1925 optbuf
[2] = IP6OPT_JUMBO
;
1927 v
= (u_int32_t
)htonl(plen
+ JUMBOOPTLEN
);
1928 bcopy(&v
, &optbuf
[4], sizeof (u_int32_t
));
1930 /* finally, adjust the packet header length */
1931 exthdrs
->ip6e_ip6
->m_pkthdr
.len
+= JUMBOOPTLEN
;
1938 * Insert fragment header and copy unfragmentable header portions.
1941 ip6_insertfraghdr(struct mbuf
*m0
, struct mbuf
*m
, int hlen
,
1942 struct ip6_frag
**frghdrp
)
1944 struct mbuf
*n
, *mlast
;
1946 if (hlen
> sizeof (struct ip6_hdr
)) {
1947 n
= m_copym(m0
, sizeof (struct ip6_hdr
),
1948 hlen
- sizeof (struct ip6_hdr
), M_DONTWAIT
);
1955 /* Search for the last mbuf of unfragmentable part. */
1956 for (mlast
= n
; mlast
->m_next
; mlast
= mlast
->m_next
)
1959 if (!(mlast
->m_flags
& M_EXT
) &&
1960 M_TRAILINGSPACE(mlast
) >= sizeof (struct ip6_frag
)) {
1961 /* use the trailing space of the last mbuf for the frag hdr */
1962 *frghdrp
= (struct ip6_frag
*)(mtod(mlast
, caddr_t
) +
1964 mlast
->m_len
+= sizeof (struct ip6_frag
);
1965 m
->m_pkthdr
.len
+= sizeof (struct ip6_frag
);
1967 /* allocate a new mbuf for the fragment header */
1970 MGET(mfrg
, M_DONTWAIT
, MT_DATA
);
1973 mfrg
->m_len
= sizeof (struct ip6_frag
);
1974 *frghdrp
= mtod(mfrg
, struct ip6_frag
*);
1975 mlast
->m_next
= mfrg
;
1982 ip6_getpmtu(struct route_in6
*ro_pmtu
, struct route_in6
*ro
,
1983 struct ifnet
*ifp
, struct in6_addr
*dst
, u_int32_t
*mtup
,
1984 boolean_t
*alwaysfragp
)
1987 boolean_t alwaysfrag
= FALSE
;
1990 if (ro_pmtu
!= ro
) {
1991 /* The first hop and the final destination may differ. */
1992 struct sockaddr_in6
*sa6_dst
= SIN6(&ro_pmtu
->ro_dst
);
1993 if (ROUTE_UNUSABLE(ro_pmtu
) ||
1994 !IN6_ARE_ADDR_EQUAL(&sa6_dst
->sin6_addr
, dst
))
1995 ROUTE_RELEASE(ro_pmtu
);
1997 if (ro_pmtu
->ro_rt
== NULL
) {
1998 bzero(sa6_dst
, sizeof (*sa6_dst
));
1999 sa6_dst
->sin6_family
= AF_INET6
;
2000 sa6_dst
->sin6_len
= sizeof (struct sockaddr_in6
);
2001 sa6_dst
->sin6_addr
= *dst
;
2003 rtalloc_scoped((struct route
*)ro_pmtu
,
2004 ifp
!= NULL
? ifp
->if_index
: IFSCOPE_NONE
);
2008 if (ro_pmtu
->ro_rt
!= NULL
) {
2012 ifp
= ro_pmtu
->ro_rt
->rt_ifp
;
2013 lck_rw_lock_shared(nd_if_rwlock
);
2014 /* Access without acquiring nd_ifinfo lock for performance */
2015 ifmtu
= IN6_LINKMTU(ifp
);
2016 lck_rw_done(nd_if_rwlock
);
2019 * Access rmx_mtu without holding the route entry lock,
2020 * for performance; this isn't something that changes
2021 * often, so optimize.
2023 mtu
= ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
;
2024 if (mtu
> ifmtu
|| mtu
== 0) {
2026 * The MTU on the route is larger than the MTU on
2027 * the interface! This shouldn't happen, unless the
2028 * MTU of the interface has been changed after the
2029 * interface was brought up. Change the MTU in the
2030 * route to match the interface MTU (as long as the
2031 * field isn't locked).
2033 * if MTU on the route is 0, we need to fix the MTU.
2034 * this case happens with path MTU discovery timeouts.
2037 if (!(ro_pmtu
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
))
2038 ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
= mtu
; /* XXX */
2039 } else if (mtu
< IPV6_MMTU
) {
2041 * RFC2460 section 5, last paragraph:
2042 * if we record ICMPv6 too big message with
2043 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
2044 * or smaller, with framgent header attached.
2045 * (fragment header is needed regardless from the
2046 * packet size, for translators to identify packets)
2053 lck_rw_lock_shared(nd_if_rwlock
);
2054 /* Don't hold nd_ifinfo lock for performance */
2055 mtu
= IN6_LINKMTU(ifp
);
2056 lck_rw_done(nd_if_rwlock
);
2058 error
= EHOSTUNREACH
; /* XXX */
2063 if (alwaysfragp
!= NULL
)
2064 *alwaysfragp
= alwaysfrag
;
2069 * IP6 socket option processing.
2072 ip6_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
2074 int optdatalen
, uproto
;
2077 struct inpcb
*in6p
= sotoinpcb(so
);
2078 int error
= 0, optval
= 0;
2079 int level
, op
= -1, optname
= 0;
2083 VERIFY(sopt
!= NULL
);
2085 level
= sopt
->sopt_level
;
2086 op
= sopt
->sopt_dir
;
2087 optname
= sopt
->sopt_name
;
2088 optlen
= sopt
->sopt_valsize
;
2090 uproto
= (int)SOCK_PROTO(so
);
2092 privileged
= (proc_suser(p
) == 0);
2094 if (level
== IPPROTO_IPV6
) {
2098 case IPV6_2292PKTOPTIONS
: {
2101 error
= soopt_getm(sopt
, &m
);
2104 error
= soopt_mcopyin(sopt
, m
);
2107 error
= ip6_pcbopts(&in6p
->in6p_outputopts
,
2114 * Use of some Hop-by-Hop options or some
2115 * Destination options, might require special
2116 * privilege. That is, normal applications
2117 * (without special privilege) might be forbidden
2118 * from setting certain options in outgoing packets,
2119 * and might never see certain options in received
2120 * packets. [RFC 2292 Section 6]
2121 * KAME specific note:
2122 * KAME prevents non-privileged users from sending or
2123 * receiving ANY hbh/dst options in order to avoid
2124 * overhead of parsing options in the kernel.
2126 case IPV6_RECVHOPOPTS
:
2127 case IPV6_RECVDSTOPTS
:
2128 case IPV6_RECVRTHDRDSTOPTS
:
2132 case IPV6_UNICAST_HOPS
:
2134 case IPV6_RECVPKTINFO
:
2135 case IPV6_RECVHOPLIMIT
:
2136 case IPV6_RECVRTHDR
:
2137 case IPV6_RECVPATHMTU
:
2138 case IPV6_RECVTCLASS
:
2140 case IPV6_AUTOFLOWLABEL
:
2141 if (optlen
!= sizeof (int)) {
2145 error
= sooptcopyin(sopt
, &optval
,
2146 sizeof (optval
), sizeof (optval
));
2151 case IPV6_UNICAST_HOPS
:
2152 if (optval
< -1 || optval
>= 256) {
2155 /* -1 = kernel default */
2156 in6p
->in6p_hops
= optval
;
2157 if (in6p
->inp_vflag
&
2164 #define OPTSET(bit) do { \
2166 in6p->inp_flags |= (bit); \
2168 in6p->inp_flags &= ~(bit); \
2171 #define OPTSET2292(bit) do { \
2172 in6p->inp_flags |= IN6P_RFC2292; \
2174 in6p->inp_flags |= (bit); \
2176 in6p->inp_flags &= ~(bit); \
2179 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
2181 case IPV6_RECVPKTINFO
:
2182 /* cannot mix with RFC2292 */
2183 if (OPTBIT(IN6P_RFC2292
)) {
2187 OPTSET(IN6P_PKTINFO
);
2190 case IPV6_HOPLIMIT
: {
2191 struct ip6_pktopts
**optp
;
2193 /* cannot mix with RFC2292 */
2194 if (OPTBIT(IN6P_RFC2292
)) {
2198 optp
= &in6p
->in6p_outputopts
;
2199 error
= ip6_pcbopt(IPV6_HOPLIMIT
,
2200 (u_char
*)&optval
, sizeof (optval
),
2205 case IPV6_RECVHOPLIMIT
:
2206 /* cannot mix with RFC2292 */
2207 if (OPTBIT(IN6P_RFC2292
)) {
2211 OPTSET(IN6P_HOPLIMIT
);
2214 case IPV6_RECVHOPOPTS
:
2215 /* cannot mix with RFC2292 */
2216 if (OPTBIT(IN6P_RFC2292
)) {
2220 OPTSET(IN6P_HOPOPTS
);
2223 case IPV6_RECVDSTOPTS
:
2224 /* cannot mix with RFC2292 */
2225 if (OPTBIT(IN6P_RFC2292
)) {
2229 OPTSET(IN6P_DSTOPTS
);
2232 case IPV6_RECVRTHDRDSTOPTS
:
2233 /* cannot mix with RFC2292 */
2234 if (OPTBIT(IN6P_RFC2292
)) {
2238 OPTSET(IN6P_RTHDRDSTOPTS
);
2241 case IPV6_RECVRTHDR
:
2242 /* cannot mix with RFC2292 */
2243 if (OPTBIT(IN6P_RFC2292
)) {
2250 case IPV6_RECVPATHMTU
:
2252 * We ignore this option for TCP
2254 * (RFC3542 leaves this case
2257 if (uproto
!= IPPROTO_TCP
)
2263 * make setsockopt(IPV6_V6ONLY)
2264 * available only prior to bind(2).
2265 * see ipng mailing list, Jun 22 2001.
2267 if (in6p
->inp_lport
||
2268 !IN6_IS_ADDR_UNSPECIFIED(
2269 &in6p
->in6p_laddr
)) {
2273 OPTSET(IN6P_IPV6_V6ONLY
);
2275 in6p
->inp_vflag
&= ~INP_IPV4
;
2277 in6p
->inp_vflag
|= INP_IPV4
;
2280 case IPV6_RECVTCLASS
:
2281 /* we can mix with RFC2292 */
2282 OPTSET(IN6P_TCLASS
);
2285 case IPV6_AUTOFLOWLABEL
:
2286 OPTSET(IN6P_AUTOFLOWLABEL
);
2294 case IPV6_USE_MIN_MTU
:
2295 case IPV6_PREFER_TEMPADDR
: {
2296 struct ip6_pktopts
**optp
;
2298 if (optlen
!= sizeof (optval
)) {
2302 error
= sooptcopyin(sopt
, &optval
,
2303 sizeof (optval
), sizeof (optval
));
2307 optp
= &in6p
->in6p_outputopts
;
2308 error
= ip6_pcbopt(optname
, (u_char
*)&optval
,
2309 sizeof (optval
), optp
, uproto
);
2313 case IPV6_2292PKTINFO
:
2314 case IPV6_2292HOPLIMIT
:
2315 case IPV6_2292HOPOPTS
:
2316 case IPV6_2292DSTOPTS
:
2317 case IPV6_2292RTHDR
:
2319 if (optlen
!= sizeof (int)) {
2323 error
= sooptcopyin(sopt
, &optval
,
2324 sizeof (optval
), sizeof (optval
));
2328 case IPV6_2292PKTINFO
:
2329 OPTSET2292(IN6P_PKTINFO
);
2331 case IPV6_2292HOPLIMIT
:
2332 OPTSET2292(IN6P_HOPLIMIT
);
2334 case IPV6_2292HOPOPTS
:
2336 * Check super-user privilege.
2337 * See comments for IPV6_RECVHOPOPTS.
2341 OPTSET2292(IN6P_HOPOPTS
);
2343 case IPV6_2292DSTOPTS
:
2346 OPTSET2292(IN6P_DSTOPTS
|
2347 IN6P_RTHDRDSTOPTS
); /* XXX */
2349 case IPV6_2292RTHDR
:
2350 OPTSET2292(IN6P_RTHDR
);
2355 case IPV6_3542PKTINFO
:
2356 case IPV6_3542HOPOPTS
:
2357 case IPV6_3542RTHDR
:
2358 case IPV6_3542DSTOPTS
:
2359 case IPV6_RTHDRDSTOPTS
:
2360 case IPV6_3542NEXTHOP
: {
2361 struct ip6_pktopts
**optp
;
2362 /* new advanced API (RFC3542) */
2365 /* cannot mix with RFC2292 */
2366 if (OPTBIT(IN6P_RFC2292
)) {
2370 error
= soopt_getm(sopt
, &m
);
2373 error
= soopt_mcopyin(sopt
, m
);
2377 optp
= &in6p
->in6p_outputopts
;
2378 error
= ip6_pcbopt(optname
, mtod(m
, u_char
*),
2379 m
->m_len
, optp
, uproto
);
2384 case IPV6_MULTICAST_IF
:
2385 case IPV6_MULTICAST_HOPS
:
2386 case IPV6_MULTICAST_LOOP
:
2387 case IPV6_JOIN_GROUP
:
2388 case IPV6_LEAVE_GROUP
:
2390 case MCAST_BLOCK_SOURCE
:
2391 case MCAST_UNBLOCK_SOURCE
:
2392 case MCAST_JOIN_GROUP
:
2393 case MCAST_LEAVE_GROUP
:
2394 case MCAST_JOIN_SOURCE_GROUP
:
2395 case MCAST_LEAVE_SOURCE_GROUP
:
2396 error
= ip6_setmoptions(in6p
, sopt
);
2399 case IPV6_PORTRANGE
:
2400 error
= sooptcopyin(sopt
, &optval
,
2401 sizeof (optval
), sizeof (optval
));
2406 case IPV6_PORTRANGE_DEFAULT
:
2407 in6p
->inp_flags
&= ~(INP_LOWPORT
);
2408 in6p
->inp_flags
&= ~(INP_HIGHPORT
);
2411 case IPV6_PORTRANGE_HIGH
:
2412 in6p
->inp_flags
&= ~(INP_LOWPORT
);
2413 in6p
->inp_flags
|= INP_HIGHPORT
;
2416 case IPV6_PORTRANGE_LOW
:
2417 in6p
->inp_flags
&= ~(INP_HIGHPORT
);
2418 in6p
->inp_flags
|= INP_LOWPORT
;
2427 case IPV6_IPSEC_POLICY
: {
2432 if ((error
= soopt_getm(sopt
, &m
)) != 0)
2434 if ((error
= soopt_mcopyin(sopt
, m
)) != 0)
2437 req
= mtod(m
, caddr_t
);
2439 error
= ipsec6_set_policy(in6p
, optname
, req
,
2449 case IPV6_FW_ZERO
: {
2450 if (ip6_fw_ctl_ptr
== NULL
)
2452 if (ip6_fw_ctl_ptr
!= NULL
)
2453 error
= (*ip6_fw_ctl_ptr
)(sopt
);
2455 error
= ENOPROTOOPT
;
2458 #endif /* IPFIREWALL */
2460 * IPv6 variant of IP_BOUND_IF; for details see
2461 * comments on IP_BOUND_IF in ip_ctloutput().
2464 /* This option is settable only on IPv6 */
2465 if (!(in6p
->inp_vflag
& INP_IPV6
)) {
2470 error
= sooptcopyin(sopt
, &optval
,
2471 sizeof (optval
), sizeof (optval
));
2476 error
= inp_bindif(in6p
, optval
, NULL
);
2479 case IPV6_NO_IFT_CELLULAR
:
2480 /* This option is settable only for IPv6 */
2481 if (!(in6p
->inp_vflag
& INP_IPV6
)) {
2486 error
= sooptcopyin(sopt
, &optval
,
2487 sizeof (optval
), sizeof (optval
));
2492 /* once set, it cannot be unset */
2493 if (!optval
&& INP_NO_CELLULAR(in6p
)) {
2498 error
= so_set_restrictions(so
,
2499 SO_RESTRICT_DENY_CELLULAR
);
2503 /* This option is not settable */
2508 error
= ENOPROTOOPT
;
2516 case IPV6_2292PKTOPTIONS
:
2518 * RFC3542 (effectively) deprecated the
2519 * semantics of the 2292-style pktoptions.
2520 * Since it was not reliable in nature (i.e.,
2521 * applications had to expect the lack of some
2522 * information after all), it would make sense
2523 * to simplify this part by always returning
2526 sopt
->sopt_valsize
= 0;
2529 case IPV6_RECVHOPOPTS
:
2530 case IPV6_RECVDSTOPTS
:
2531 case IPV6_RECVRTHDRDSTOPTS
:
2532 case IPV6_UNICAST_HOPS
:
2533 case IPV6_RECVPKTINFO
:
2534 case IPV6_RECVHOPLIMIT
:
2535 case IPV6_RECVRTHDR
:
2536 case IPV6_RECVPATHMTU
:
2538 case IPV6_PORTRANGE
:
2539 case IPV6_RECVTCLASS
:
2540 case IPV6_AUTOFLOWLABEL
:
2543 case IPV6_RECVHOPOPTS
:
2544 optval
= OPTBIT(IN6P_HOPOPTS
);
2547 case IPV6_RECVDSTOPTS
:
2548 optval
= OPTBIT(IN6P_DSTOPTS
);
2551 case IPV6_RECVRTHDRDSTOPTS
:
2552 optval
= OPTBIT(IN6P_RTHDRDSTOPTS
);
2555 case IPV6_UNICAST_HOPS
:
2556 optval
= in6p
->in6p_hops
;
2559 case IPV6_RECVPKTINFO
:
2560 optval
= OPTBIT(IN6P_PKTINFO
);
2563 case IPV6_RECVHOPLIMIT
:
2564 optval
= OPTBIT(IN6P_HOPLIMIT
);
2567 case IPV6_RECVRTHDR
:
2568 optval
= OPTBIT(IN6P_RTHDR
);
2571 case IPV6_RECVPATHMTU
:
2572 optval
= OPTBIT(IN6P_MTU
);
2576 optval
= OPTBIT(IN6P_IPV6_V6ONLY
);
2579 case IPV6_PORTRANGE
: {
2581 flags
= in6p
->inp_flags
;
2582 if (flags
& INP_HIGHPORT
)
2583 optval
= IPV6_PORTRANGE_HIGH
;
2584 else if (flags
& INP_LOWPORT
)
2585 optval
= IPV6_PORTRANGE_LOW
;
2590 case IPV6_RECVTCLASS
:
2591 optval
= OPTBIT(IN6P_TCLASS
);
2594 case IPV6_AUTOFLOWLABEL
:
2595 optval
= OPTBIT(IN6P_AUTOFLOWLABEL
);
2600 error
= sooptcopyout(sopt
, &optval
,
2604 case IPV6_PATHMTU
: {
2606 struct ip6_mtuinfo mtuinfo
;
2607 struct route_in6 sro
;
2609 bzero(&sro
, sizeof (sro
));
2611 if (!(so
->so_state
& SS_ISCONNECTED
))
2614 * XXX: we dot not consider the case of source
2615 * routing, or optional information to specify
2616 * the outgoing interface.
2618 error
= ip6_getpmtu(&sro
, NULL
, NULL
,
2619 &in6p
->in6p_faddr
, &pmtu
, NULL
);
2620 ROUTE_RELEASE(&sro
);
2623 if (pmtu
> IPV6_MAXPACKET
)
2624 pmtu
= IPV6_MAXPACKET
;
2626 bzero(&mtuinfo
, sizeof (mtuinfo
));
2627 mtuinfo
.ip6m_mtu
= (u_int32_t
)pmtu
;
2628 optdata
= (void *)&mtuinfo
;
2629 optdatalen
= sizeof (mtuinfo
);
2630 error
= sooptcopyout(sopt
, optdata
,
2635 case IPV6_2292PKTINFO
:
2636 case IPV6_2292HOPLIMIT
:
2637 case IPV6_2292HOPOPTS
:
2638 case IPV6_2292RTHDR
:
2639 case IPV6_2292DSTOPTS
:
2641 case IPV6_2292PKTINFO
:
2642 optval
= OPTBIT(IN6P_PKTINFO
);
2644 case IPV6_2292HOPLIMIT
:
2645 optval
= OPTBIT(IN6P_HOPLIMIT
);
2647 case IPV6_2292HOPOPTS
:
2648 optval
= OPTBIT(IN6P_HOPOPTS
);
2650 case IPV6_2292RTHDR
:
2651 optval
= OPTBIT(IN6P_RTHDR
);
2653 case IPV6_2292DSTOPTS
:
2654 optval
= OPTBIT(IN6P_DSTOPTS
|
2658 error
= sooptcopyout(sopt
, &optval
,
2666 case IPV6_RTHDRDSTOPTS
:
2670 case IPV6_USE_MIN_MTU
:
2671 case IPV6_PREFER_TEMPADDR
:
2672 error
= ip6_getpcbopt(in6p
->in6p_outputopts
,
2676 case IPV6_MULTICAST_IF
:
2677 case IPV6_MULTICAST_HOPS
:
2678 case IPV6_MULTICAST_LOOP
:
2680 error
= ip6_getmoptions(in6p
, sopt
);
2683 case IPV6_IPSEC_POLICY
: {
2684 error
= 0; /* This option is no longer supported */
2690 if (ip6_fw_ctl_ptr
== NULL
)
2692 if (ip6_fw_ctl_ptr
!= NULL
)
2693 error
= (*ip6_fw_ctl_ptr
)(sopt
);
2695 error
= ENOPROTOOPT
;
2698 #endif /* IPFIREWALL */
2700 if (in6p
->inp_flags
& INP_BOUND_IF
)
2701 optval
= in6p
->inp_boundifp
->if_index
;
2702 error
= sooptcopyout(sopt
, &optval
,
2706 case IPV6_NO_IFT_CELLULAR
:
2707 optval
= INP_NO_CELLULAR(in6p
) ? 1 : 0;
2708 error
= sooptcopyout(sopt
, &optval
,
2713 optval
= (in6p
->in6p_last_outifp
!= NULL
) ?
2714 in6p
->in6p_last_outifp
->if_index
: 0;
2715 error
= sooptcopyout(sopt
, &optval
,
2720 error
= ENOPROTOOPT
;
2732 ip6_raw_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
2734 int error
= 0, optval
, optlen
;
2735 const int icmp6off
= offsetof(struct icmp6_hdr
, icmp6_cksum
);
2736 struct inpcb
*in6p
= sotoinpcb(so
);
2737 int level
, op
, optname
;
2739 level
= sopt
->sopt_level
;
2740 op
= sopt
->sopt_dir
;
2741 optname
= sopt
->sopt_name
;
2742 optlen
= sopt
->sopt_valsize
;
2744 if (level
!= IPPROTO_IPV6
)
2750 * For ICMPv6 sockets, no modification allowed for checksum
2751 * offset, permit "no change" values to help existing apps.
2753 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2754 * for an ICMPv6 socket will fail."
2755 * The current behavior does not meet RFC3542.
2759 if (optlen
!= sizeof (int)) {
2763 error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2767 if ((optval
% 2) != 0) {
2768 /* the API assumes even offset values */
2770 } else if (SOCK_PROTO(so
) == IPPROTO_ICMPV6
) {
2771 if (optval
!= icmp6off
)
2774 in6p
->in6p_cksum
= optval
;
2779 if (SOCK_PROTO(so
) == IPPROTO_ICMPV6
)
2782 optval
= in6p
->in6p_cksum
;
2784 error
= sooptcopyout(sopt
, &optval
, sizeof (optval
));
2794 error
= ENOPROTOOPT
;
2802 * Set up IP6 options in pcb for insertion in output packets or
2803 * specifying behavior of outgoing packets.
2806 ip6_pcbopts(struct ip6_pktopts
**pktopt
, struct mbuf
*m
, struct socket
*so
,
2807 struct sockopt
*sopt
)
2809 #pragma unused(sopt)
2810 struct ip6_pktopts
*opt
= *pktopt
;
2813 /* turn off any old options. */
2816 if (opt
->ip6po_pktinfo
|| opt
->ip6po_nexthop
||
2817 opt
->ip6po_hbh
|| opt
->ip6po_dest1
|| opt
->ip6po_dest2
||
2818 opt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
2819 printf("%s: all specified options are cleared.\n",
2822 ip6_clearpktopts(opt
, -1);
2824 opt
= _MALLOC(sizeof (*opt
), M_IP6OPT
, M_WAITOK
);
2830 if (m
== NULL
|| m
->m_len
== 0) {
2832 * Only turning off any previous options, regardless of
2833 * whether the opt is just created or given.
2836 FREE(opt
, M_IP6OPT
);
2840 /* set options specified by user. */
2841 if ((error
= ip6_setpktopts(m
, opt
, NULL
, SOCK_PROTO(so
))) != 0) {
2842 ip6_clearpktopts(opt
, -1); /* XXX: discard all options */
2843 FREE(opt
, M_IP6OPT
);
2851 * initialize ip6_pktopts. beware that there are non-zero default values in
2855 ip6_initpktopts(struct ip6_pktopts
*opt
)
2858 bzero(opt
, sizeof (*opt
));
2859 opt
->ip6po_hlim
= -1; /* -1 means default hop limit */
2860 opt
->ip6po_tclass
= -1; /* -1 means default traffic class */
2861 opt
->ip6po_minmtu
= IP6PO_MINMTU_MCASTONLY
;
2862 opt
->ip6po_prefer_tempaddr
= IP6PO_TEMPADDR_SYSTEM
;
2866 ip6_pcbopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
**pktopt
,
2869 struct ip6_pktopts
*opt
;
2873 opt
= _MALLOC(sizeof (*opt
), M_IP6OPT
, M_WAITOK
);
2876 ip6_initpktopts(opt
);
2880 return (ip6_setpktopt(optname
, buf
, len
, opt
, 1, 0, uproto
));
2884 ip6_getpcbopt(struct ip6_pktopts
*pktopt
, int optname
, struct sockopt
*sopt
)
2886 void *optdata
= NULL
;
2888 struct ip6_ext
*ip6e
;
2889 struct in6_pktinfo null_pktinfo
;
2890 int deftclass
= 0, on
;
2891 int defminmtu
= IP6PO_MINMTU_MCASTONLY
;
2892 int defpreftemp
= IP6PO_TEMPADDR_SYSTEM
;
2897 if (pktopt
&& pktopt
->ip6po_pktinfo
)
2898 optdata
= (void *)pktopt
->ip6po_pktinfo
;
2900 /* XXX: we don't have to do this every time... */
2901 bzero(&null_pktinfo
, sizeof (null_pktinfo
));
2902 optdata
= (void *)&null_pktinfo
;
2904 optdatalen
= sizeof (struct in6_pktinfo
);
2908 if (pktopt
&& pktopt
->ip6po_tclass
>= 0)
2909 optdata
= (void *)&pktopt
->ip6po_tclass
;
2911 optdata
= (void *)&deftclass
;
2912 optdatalen
= sizeof (int);
2916 if (pktopt
&& pktopt
->ip6po_hbh
) {
2917 optdata
= (void *)pktopt
->ip6po_hbh
;
2918 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_hbh
;
2919 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2924 if (pktopt
&& pktopt
->ip6po_rthdr
) {
2925 optdata
= (void *)pktopt
->ip6po_rthdr
;
2926 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_rthdr
;
2927 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2931 case IPV6_RTHDRDSTOPTS
:
2932 if (pktopt
&& pktopt
->ip6po_dest1
) {
2933 optdata
= (void *)pktopt
->ip6po_dest1
;
2934 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest1
;
2935 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2940 if (pktopt
&& pktopt
->ip6po_dest2
) {
2941 optdata
= (void *)pktopt
->ip6po_dest2
;
2942 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest2
;
2943 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2948 if (pktopt
&& pktopt
->ip6po_nexthop
) {
2949 optdata
= (void *)pktopt
->ip6po_nexthop
;
2950 optdatalen
= pktopt
->ip6po_nexthop
->sa_len
;
2954 case IPV6_USE_MIN_MTU
:
2956 optdata
= (void *)&pktopt
->ip6po_minmtu
;
2958 optdata
= (void *)&defminmtu
;
2959 optdatalen
= sizeof (int);
2963 if (pktopt
&& ((pktopt
->ip6po_flags
) & IP6PO_DONTFRAG
))
2967 optdata
= (void *)&on
;
2968 optdatalen
= sizeof (on
);
2971 case IPV6_PREFER_TEMPADDR
:
2973 optdata
= (void *)&pktopt
->ip6po_prefer_tempaddr
;
2975 optdata
= (void *)&defpreftemp
;
2976 optdatalen
= sizeof (int);
2979 default: /* should not happen */
2981 panic("ip6_getpcbopt: unexpected option\n");
2983 return (ENOPROTOOPT
);
2986 return (sooptcopyout(sopt
, optdata
, optdatalen
));
2990 ip6_clearpktopts(struct ip6_pktopts
*pktopt
, int optname
)
2995 if (optname
== -1 || optname
== IPV6_PKTINFO
) {
2996 if (pktopt
->ip6po_pktinfo
)
2997 FREE(pktopt
->ip6po_pktinfo
, M_IP6OPT
);
2998 pktopt
->ip6po_pktinfo
= NULL
;
3000 if (optname
== -1 || optname
== IPV6_HOPLIMIT
)
3001 pktopt
->ip6po_hlim
= -1;
3002 if (optname
== -1 || optname
== IPV6_TCLASS
)
3003 pktopt
->ip6po_tclass
= -1;
3004 if (optname
== -1 || optname
== IPV6_NEXTHOP
) {
3005 ROUTE_RELEASE(&pktopt
->ip6po_nextroute
);
3006 if (pktopt
->ip6po_nexthop
)
3007 FREE(pktopt
->ip6po_nexthop
, M_IP6OPT
);
3008 pktopt
->ip6po_nexthop
= NULL
;
3010 if (optname
== -1 || optname
== IPV6_HOPOPTS
) {
3011 if (pktopt
->ip6po_hbh
)
3012 FREE(pktopt
->ip6po_hbh
, M_IP6OPT
);
3013 pktopt
->ip6po_hbh
= NULL
;
3015 if (optname
== -1 || optname
== IPV6_RTHDRDSTOPTS
) {
3016 if (pktopt
->ip6po_dest1
)
3017 FREE(pktopt
->ip6po_dest1
, M_IP6OPT
);
3018 pktopt
->ip6po_dest1
= NULL
;
3020 if (optname
== -1 || optname
== IPV6_RTHDR
) {
3021 if (pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
3022 FREE(pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
, M_IP6OPT
);
3023 pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
= NULL
;
3024 ROUTE_RELEASE(&pktopt
->ip6po_route
);
3026 if (optname
== -1 || optname
== IPV6_DSTOPTS
) {
3027 if (pktopt
->ip6po_dest2
)
3028 FREE(pktopt
->ip6po_dest2
, M_IP6OPT
);
3029 pktopt
->ip6po_dest2
= NULL
;
3033 #define PKTOPT_EXTHDRCPY(type) do { \
3036 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3; \
3037 dst->type = _MALLOC(hlen, M_IP6OPT, canwait); \
3038 if (dst->type == NULL && canwait == M_NOWAIT) \
3040 bcopy(src->type, dst->type, hlen); \
3045 copypktopts(struct ip6_pktopts
*dst
, struct ip6_pktopts
*src
, int canwait
)
3047 if (dst
== NULL
|| src
== NULL
) {
3048 printf("copypktopts: invalid argument\n");
3052 dst
->ip6po_hlim
= src
->ip6po_hlim
;
3053 dst
->ip6po_tclass
= src
->ip6po_tclass
;
3054 dst
->ip6po_flags
= src
->ip6po_flags
;
3055 if (src
->ip6po_pktinfo
) {
3056 dst
->ip6po_pktinfo
= _MALLOC(sizeof (*dst
->ip6po_pktinfo
),
3058 if (dst
->ip6po_pktinfo
== NULL
&& canwait
== M_NOWAIT
)
3060 *dst
->ip6po_pktinfo
= *src
->ip6po_pktinfo
;
3062 if (src
->ip6po_nexthop
) {
3063 dst
->ip6po_nexthop
= _MALLOC(src
->ip6po_nexthop
->sa_len
,
3065 if (dst
->ip6po_nexthop
== NULL
&& canwait
== M_NOWAIT
)
3067 bcopy(src
->ip6po_nexthop
, dst
->ip6po_nexthop
,
3068 src
->ip6po_nexthop
->sa_len
);
3070 PKTOPT_EXTHDRCPY(ip6po_hbh
);
3071 PKTOPT_EXTHDRCPY(ip6po_dest1
);
3072 PKTOPT_EXTHDRCPY(ip6po_dest2
);
3073 PKTOPT_EXTHDRCPY(ip6po_rthdr
); /* not copy the cached route */
3077 ip6_clearpktopts(dst
, -1);
3080 #undef PKTOPT_EXTHDRCPY
3082 struct ip6_pktopts
*
3083 ip6_copypktopts(struct ip6_pktopts
*src
, int canwait
)
3086 struct ip6_pktopts
*dst
;
3088 dst
= _MALLOC(sizeof (*dst
), M_IP6OPT
, canwait
);
3091 ip6_initpktopts(dst
);
3093 if ((error
= copypktopts(dst
, src
, canwait
)) != 0) {
3094 FREE(dst
, M_IP6OPT
);
3102 ip6_freepcbopts(struct ip6_pktopts
*pktopt
)
3107 ip6_clearpktopts(pktopt
, -1);
3109 FREE(pktopt
, M_IP6OPT
);
3113 ip6_moptions_init(void)
3115 PE_parse_boot_argn("ifa_debug", &im6o_debug
, sizeof (im6o_debug
));
3117 im6o_size
= (im6o_debug
== 0) ? sizeof (struct ip6_moptions
) :
3118 sizeof (struct ip6_moptions_dbg
);
3120 im6o_zone
= zinit(im6o_size
, IM6O_ZONE_MAX
* im6o_size
, 0,
3122 if (im6o_zone
== NULL
) {
3123 panic("%s: failed allocating %s", __func__
, IM6O_ZONE_NAME
);
3126 zone_change(im6o_zone
, Z_EXPAND
, TRUE
);
3130 im6o_addref(struct ip6_moptions
*im6o
, int locked
)
3135 IM6O_LOCK_ASSERT_HELD(im6o
);
3137 if (++im6o
->im6o_refcnt
== 0) {
3138 panic("%s: im6o %p wraparound refcnt\n", __func__
, im6o
);
3140 } else if (im6o
->im6o_trace
!= NULL
) {
3141 (*im6o
->im6o_trace
)(im6o
, TRUE
);
3149 im6o_remref(struct ip6_moptions
*im6o
)
3154 if (im6o
->im6o_refcnt
== 0) {
3155 panic("%s: im6o %p negative refcnt", __func__
, im6o
);
3157 } else if (im6o
->im6o_trace
!= NULL
) {
3158 (*im6o
->im6o_trace
)(im6o
, FALSE
);
3161 --im6o
->im6o_refcnt
;
3162 if (im6o
->im6o_refcnt
> 0) {
3167 for (i
= 0; i
< im6o
->im6o_num_memberships
; ++i
) {
3168 struct in6_mfilter
*imf
;
3170 imf
= im6o
->im6o_mfilters
? &im6o
->im6o_mfilters
[i
] : NULL
;
3174 (void) in6_mc_leave(im6o
->im6o_membership
[i
], imf
);
3179 IN6M_REMREF(im6o
->im6o_membership
[i
]);
3180 im6o
->im6o_membership
[i
] = NULL
;
3182 im6o
->im6o_num_memberships
= 0;
3183 if (im6o
->im6o_mfilters
!= NULL
) {
3184 FREE(im6o
->im6o_mfilters
, M_IN6MFILTER
);
3185 im6o
->im6o_mfilters
= NULL
;
3187 if (im6o
->im6o_membership
!= NULL
) {
3188 FREE(im6o
->im6o_membership
, M_IP6MOPTS
);
3189 im6o
->im6o_membership
= NULL
;
3193 lck_mtx_destroy(&im6o
->im6o_lock
, ifa_mtx_grp
);
3195 if (!(im6o
->im6o_debug
& IFD_ALLOC
)) {
3196 panic("%s: im6o %p cannot be freed", __func__
, im6o
);
3199 zfree(im6o_zone
, im6o
);
3203 im6o_trace(struct ip6_moptions
*im6o
, int refhold
)
3205 struct ip6_moptions_dbg
*im6o_dbg
= (struct ip6_moptions_dbg
*)im6o
;
3210 if (!(im6o
->im6o_debug
& IFD_DEBUG
)) {
3211 panic("%s: im6o %p has no debug structure", __func__
, im6o
);
3215 cnt
= &im6o_dbg
->im6o_refhold_cnt
;
3216 tr
= im6o_dbg
->im6o_refhold
;
3218 cnt
= &im6o_dbg
->im6o_refrele_cnt
;
3219 tr
= im6o_dbg
->im6o_refrele
;
3222 idx
= atomic_add_16_ov(cnt
, 1) % IM6O_TRACE_HIST_SIZE
;
3223 ctrace_record(&tr
[idx
]);
3226 struct ip6_moptions
*
3227 ip6_allocmoptions(int how
)
3229 struct ip6_moptions
*im6o
;
3231 im6o
= (how
== M_WAITOK
) ?
3232 zalloc(im6o_zone
) : zalloc_noblock(im6o_zone
);
3234 bzero(im6o
, im6o_size
);
3235 lck_mtx_init(&im6o
->im6o_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
3236 im6o
->im6o_debug
|= IFD_ALLOC
;
3237 if (im6o_debug
!= 0) {
3238 im6o
->im6o_debug
|= IFD_DEBUG
;
3239 im6o
->im6o_trace
= im6o_trace
;
3248 * Set IPv6 outgoing packet options based on advanced API.
3251 ip6_setpktopts(struct mbuf
*control
, struct ip6_pktopts
*opt
,
3252 struct ip6_pktopts
*stickyopt
, int uproto
)
3254 struct cmsghdr
*cm
= NULL
;
3256 if (control
== NULL
|| opt
== NULL
)
3259 ip6_initpktopts(opt
);
3264 * If stickyopt is provided, make a local copy of the options
3265 * for this particular packet, then override them by ancillary
3267 * XXX: copypktopts() does not copy the cached route to a next
3268 * hop (if any). This is not very good in terms of efficiency,
3269 * but we can allow this since this option should be rarely
3272 if ((error
= copypktopts(opt
, stickyopt
, M_NOWAIT
)) != 0)
3277 * XXX: Currently, we assume all the optional information is stored
3280 if (control
->m_next
)
3283 if (control
->m_len
< CMSG_LEN(0))
3286 for (cm
= M_FIRST_CMSGHDR(control
); cm
!= NULL
;
3287 cm
= M_NXT_CMSGHDR(control
, cm
)) {
3290 if (cm
->cmsg_len
< sizeof (struct cmsghdr
) ||
3291 cm
->cmsg_len
> control
->m_len
)
3293 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
3296 error
= ip6_setpktopt(cm
->cmsg_type
, CMSG_DATA(cm
),
3297 cm
->cmsg_len
- CMSG_LEN(0), opt
, 0, 1, uproto
);
3305 * Set a particular packet option, as a sticky option or an ancillary data
3306 * item. "len" can be 0 only when it's a sticky option.
3307 * We have 4 cases of combination of "sticky" and "cmsg":
3308 * "sticky=0, cmsg=0": impossible
3309 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
3310 * "sticky=1, cmsg=0": RFC3542 socket option
3311 * "sticky=1, cmsg=1": RFC2292 socket option
3314 ip6_setpktopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
*opt
,
3315 int sticky
, int cmsg
, int uproto
)
3317 int minmtupolicy
, preftemp
;
3320 if (!sticky
&& !cmsg
) {
3322 printf("ip6_setpktopt: impossible case\n");
3328 * Caller must have ensured that the buffer is at least
3329 * aligned on 32-bit boundary.
3331 VERIFY(IS_P2ALIGNED(buf
, sizeof (u_int32_t
)));
3334 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
3335 * not be specified in the context of RFC3542. Conversely,
3336 * RFC3542 types should not be specified in the context of RFC2292.
3340 case IPV6_2292PKTINFO
:
3341 case IPV6_2292HOPLIMIT
:
3342 case IPV6_2292NEXTHOP
:
3343 case IPV6_2292HOPOPTS
:
3344 case IPV6_2292DSTOPTS
:
3345 case IPV6_2292RTHDR
:
3346 case IPV6_2292PKTOPTIONS
:
3347 return (ENOPROTOOPT
);
3350 if (sticky
&& cmsg
) {
3357 case IPV6_RTHDRDSTOPTS
:
3359 case IPV6_USE_MIN_MTU
:
3362 case IPV6_PREFER_TEMPADDR
: /* XXX: not an RFC3542 option */
3363 return (ENOPROTOOPT
);
3368 case IPV6_2292PKTINFO
:
3369 case IPV6_PKTINFO
: {
3370 struct ifnet
*ifp
= NULL
;
3371 struct in6_pktinfo
*pktinfo
;
3373 if (len
!= sizeof (struct in6_pktinfo
))
3376 pktinfo
= (struct in6_pktinfo
*)(void *)buf
;
3379 * An application can clear any sticky IPV6_PKTINFO option by
3380 * doing a "regular" setsockopt with ipi6_addr being
3381 * in6addr_any and ipi6_ifindex being zero.
3382 * [RFC 3542, Section 6]
3384 if (optname
== IPV6_PKTINFO
&& opt
->ip6po_pktinfo
&&
3385 pktinfo
->ipi6_ifindex
== 0 &&
3386 IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
3387 ip6_clearpktopts(opt
, optname
);
3391 if (uproto
== IPPROTO_TCP
&& optname
== IPV6_PKTINFO
&&
3392 sticky
&& !IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
3396 /* validate the interface index if specified. */
3397 ifnet_head_lock_shared();
3399 if (pktinfo
->ipi6_ifindex
> if_index
) {
3404 if (pktinfo
->ipi6_ifindex
) {
3405 ifp
= ifindex2ifnet
[pktinfo
->ipi6_ifindex
];
3415 * We store the address anyway, and let in6_selectsrc()
3416 * validate the specified address. This is because ipi6_addr
3417 * may not have enough information about its scope zone, and
3418 * we may need additional information (such as outgoing
3419 * interface or the scope zone of a destination address) to
3420 * disambiguate the scope.
3421 * XXX: the delay of the validation may confuse the
3422 * application when it is used as a sticky option.
3424 if (opt
->ip6po_pktinfo
== NULL
) {
3425 opt
->ip6po_pktinfo
= _MALLOC(sizeof (*pktinfo
),
3426 M_IP6OPT
, M_NOWAIT
);
3427 if (opt
->ip6po_pktinfo
== NULL
)
3430 bcopy(pktinfo
, opt
->ip6po_pktinfo
, sizeof (*pktinfo
));
3434 case IPV6_2292HOPLIMIT
:
3435 case IPV6_HOPLIMIT
: {
3439 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3440 * to simplify the ordering among hoplimit options.
3442 if (optname
== IPV6_HOPLIMIT
&& sticky
)
3443 return (ENOPROTOOPT
);
3445 if (len
!= sizeof (int))
3447 hlimp
= (int *)(void *)buf
;
3448 if (*hlimp
< -1 || *hlimp
> 255)
3451 opt
->ip6po_hlim
= *hlimp
;
3458 if (len
!= sizeof (int))
3460 tclass
= *(int *)(void *)buf
;
3461 if (tclass
< -1 || tclass
> 255)
3464 opt
->ip6po_tclass
= tclass
;
3468 case IPV6_2292NEXTHOP
:
3470 error
= suser(kauth_cred_get(), 0);
3474 if (len
== 0) { /* just remove the option */
3475 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3479 /* check if cmsg_len is large enough for sa_len */
3480 if (len
< sizeof (struct sockaddr
) || len
< *buf
)
3483 switch (SA(buf
)->sa_family
) {
3485 struct sockaddr_in6
*sa6
= SIN6(buf
);
3487 if (sa6
->sin6_len
!= sizeof (struct sockaddr_in6
))
3490 if (IN6_IS_ADDR_UNSPECIFIED(&sa6
->sin6_addr
) ||
3491 IN6_IS_ADDR_MULTICAST(&sa6
->sin6_addr
)) {
3494 if ((error
= sa6_embedscope(sa6
, ip6_use_defzone
))
3500 case AF_LINK
: /* should eventually be supported */
3502 return (EAFNOSUPPORT
);
3505 /* turn off the previous option, then set the new option. */
3506 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3507 opt
->ip6po_nexthop
= _MALLOC(*buf
, M_IP6OPT
, M_NOWAIT
);
3508 if (opt
->ip6po_nexthop
== NULL
)
3510 bcopy(buf
, opt
->ip6po_nexthop
, *buf
);
3513 case IPV6_2292HOPOPTS
:
3514 case IPV6_HOPOPTS
: {
3515 struct ip6_hbh
*hbh
;
3519 * XXX: We don't allow a non-privileged user to set ANY HbH
3520 * options, since per-option restriction has too much
3523 error
= suser(kauth_cred_get(), 0);
3528 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3529 break; /* just remove the option */
3532 /* message length validation */
3533 if (len
< sizeof (struct ip6_hbh
))
3535 hbh
= (struct ip6_hbh
*)(void *)buf
;
3536 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
3540 /* turn off the previous option, then set the new option. */
3541 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3542 opt
->ip6po_hbh
= _MALLOC(hbhlen
, M_IP6OPT
, M_NOWAIT
);
3543 if (opt
->ip6po_hbh
== NULL
)
3545 bcopy(hbh
, opt
->ip6po_hbh
, hbhlen
);
3550 case IPV6_2292DSTOPTS
:
3552 case IPV6_RTHDRDSTOPTS
: {
3553 struct ip6_dest
*dest
, **newdest
= NULL
;
3556 error
= suser(kauth_cred_get(), 0);
3561 ip6_clearpktopts(opt
, optname
);
3562 break; /* just remove the option */
3565 /* message length validation */
3566 if (len
< sizeof (struct ip6_dest
))
3568 dest
= (struct ip6_dest
*)(void *)buf
;
3569 destlen
= (dest
->ip6d_len
+ 1) << 3;
3574 * Determine the position that the destination options header
3575 * should be inserted; before or after the routing header.
3578 case IPV6_2292DSTOPTS
:
3580 * The old advacned API is ambiguous on this point.
3581 * Our approach is to determine the position based
3582 * according to the existence of a routing header.
3583 * Note, however, that this depends on the order of the
3584 * extension headers in the ancillary data; the 1st
3585 * part of the destination options header must appear
3586 * before the routing header in the ancillary data,
3588 * RFC3542 solved the ambiguity by introducing
3589 * separate ancillary data or option types.
3591 if (opt
->ip6po_rthdr
== NULL
)
3592 newdest
= &opt
->ip6po_dest1
;
3594 newdest
= &opt
->ip6po_dest2
;
3596 case IPV6_RTHDRDSTOPTS
:
3597 newdest
= &opt
->ip6po_dest1
;
3600 newdest
= &opt
->ip6po_dest2
;
3604 /* turn off the previous option, then set the new option. */
3605 ip6_clearpktopts(opt
, optname
);
3606 *newdest
= _MALLOC(destlen
, M_IP6OPT
, M_NOWAIT
);
3607 if (*newdest
== NULL
)
3609 bcopy(dest
, *newdest
, destlen
);
3613 case IPV6_2292RTHDR
:
3615 struct ip6_rthdr
*rth
;
3619 ip6_clearpktopts(opt
, IPV6_RTHDR
);
3620 break; /* just remove the option */
3623 /* message length validation */
3624 if (len
< sizeof (struct ip6_rthdr
))
3626 rth
= (struct ip6_rthdr
*)(void *)buf
;
3627 rthlen
= (rth
->ip6r_len
+ 1) << 3;
3631 switch (rth
->ip6r_type
) {
3632 case IPV6_RTHDR_TYPE_0
:
3633 if (rth
->ip6r_len
== 0) /* must contain one addr */
3635 if (rth
->ip6r_len
% 2) /* length must be even */
3637 if (rth
->ip6r_len
/ 2 != rth
->ip6r_segleft
)
3641 return (EINVAL
); /* not supported */
3644 /* turn off the previous option */
3645 ip6_clearpktopts(opt
, IPV6_RTHDR
);
3646 opt
->ip6po_rthdr
= _MALLOC(rthlen
, M_IP6OPT
, M_NOWAIT
);
3647 if (opt
->ip6po_rthdr
== NULL
)
3649 bcopy(rth
, opt
->ip6po_rthdr
, rthlen
);
3653 case IPV6_USE_MIN_MTU
:
3654 if (len
!= sizeof (int))
3656 minmtupolicy
= *(int *)(void *)buf
;
3657 if (minmtupolicy
!= IP6PO_MINMTU_MCASTONLY
&&
3658 minmtupolicy
!= IP6PO_MINMTU_DISABLE
&&
3659 minmtupolicy
!= IP6PO_MINMTU_ALL
) {
3662 opt
->ip6po_minmtu
= minmtupolicy
;
3666 if (len
!= sizeof (int))
3669 if (uproto
== IPPROTO_TCP
|| *(int *)(void *)buf
== 0) {
3671 * we ignore this option for TCP sockets.
3672 * (RFC3542 leaves this case unspecified.)
3674 opt
->ip6po_flags
&= ~IP6PO_DONTFRAG
;
3676 opt
->ip6po_flags
|= IP6PO_DONTFRAG
;
3680 case IPV6_PREFER_TEMPADDR
:
3681 if (len
!= sizeof (int))
3683 preftemp
= *(int *)(void *)buf
;
3684 if (preftemp
!= IP6PO_TEMPADDR_SYSTEM
&&
3685 preftemp
!= IP6PO_TEMPADDR_NOTPREFER
&&
3686 preftemp
!= IP6PO_TEMPADDR_PREFER
) {
3689 opt
->ip6po_prefer_tempaddr
= preftemp
;
3693 return (ENOPROTOOPT
);
3694 } /* end of switch */
3700 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3701 * packet to the input queue of a specified interface. Note that this
3702 * calls the output routine of the loopback "driver", but with an interface
3703 * pointer that might NOT be &loif -- easier than replicating that code here.
3706 ip6_mloopback(struct ifnet
*srcifp
, struct ifnet
*origifp
, struct mbuf
*m
,
3707 struct sockaddr_in6
*dst
, uint32_t optlen
, int32_t nxt0
)
3710 struct ip6_hdr
*ip6
;
3711 struct in6_addr src
;
3717 * Copy the packet header as it's needed for the checksum.
3718 * Make sure to deep-copy IPv6 header portion in case the data
3719 * is in an mbuf cluster, so that we can safely override the IPv6
3720 * header portion later.
3722 copym
= m_copym_mode(m
, 0, M_COPYALL
, M_DONTWAIT
, M_COPYM_COPY_HDR
);
3723 if (copym
!= NULL
&& ((copym
->m_flags
& M_EXT
) ||
3724 copym
->m_len
< sizeof (struct ip6_hdr
)))
3725 copym
= m_pullup(copym
, sizeof (struct ip6_hdr
));
3730 ip6
= mtod(copym
, struct ip6_hdr
*);
3733 * clear embedded scope identifiers if necessary.
3734 * in6_clearscope will touch the addresses only when necessary.
3736 in6_clearscope(&ip6
->ip6_src
);
3737 in6_clearscope(&ip6
->ip6_dst
);
3739 if (copym
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
)
3740 in6_delayed_cksum_offset(copym
, 0, optlen
, nxt0
);
3743 * Stuff the 'real' ifp into the pkthdr, to be used in matching
3744 * in ip6_input(); we need the loopback ifp/dl_tag passed as args
3745 * to make the loopback driver compliant with the data link
3748 copym
->m_pkthdr
.rcvif
= origifp
;
3751 * Also record the source interface (which owns the source address).
3752 * This is basically a stripped down version of ifa_foraddr6().
3754 if (srcifp
== NULL
) {
3755 struct in6_ifaddr
*ia
;
3757 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
3758 for (ia
= in6_ifaddrs
; ia
!= NULL
; ia
= ia
->ia_next
) {
3759 IFA_LOCK_SPIN(&ia
->ia_ifa
);
3760 /* compare against src addr with embedded scope */
3761 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
, &src
)) {
3762 srcifp
= ia
->ia_ifp
;
3763 IFA_UNLOCK(&ia
->ia_ifa
);
3766 IFA_UNLOCK(&ia
->ia_ifa
);
3768 lck_rw_done(&in6_ifaddr_rwlock
);
3771 ip6_setsrcifaddr_info(copym
, srcifp
->if_index
, NULL
);
3772 ip6_setdstifaddr_info(copym
, origifp
->if_index
, NULL
);
3774 dlil_output(lo_ifp
, PF_INET6
, copym
, NULL
, SA(dst
), 0, NULL
);
3778 * Chop IPv6 header off from the payload.
3781 ip6_splithdr(struct mbuf
*m
, struct ip6_exthdrs
*exthdrs
)
3784 struct ip6_hdr
*ip6
;
3786 ip6
= mtod(m
, struct ip6_hdr
*);
3787 if (m
->m_len
> sizeof (*ip6
)) {
3788 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
3793 M_COPY_PKTHDR(mh
, m
);
3794 MH_ALIGN(mh
, sizeof (*ip6
));
3795 m
->m_flags
&= ~M_PKTHDR
;
3796 m
->m_len
-= sizeof (*ip6
);
3797 m
->m_data
+= sizeof (*ip6
);
3800 m
->m_len
= sizeof (*ip6
);
3801 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), sizeof (*ip6
));
3803 exthdrs
->ip6e_ip6
= m
;
3808 ip6_output_checksum(struct ifnet
*ifp
, uint32_t mtu
, struct mbuf
*m
,
3809 int nxt0
, uint32_t tlen
, uint32_t optlen
)
3811 uint32_t sw_csum
, hwcap
= ifp
->if_hwassist
;
3812 int tso
= TSO_IPV6_OK(ifp
, m
);
3815 /* do all in software; checksum offload is disabled */
3816 sw_csum
= CSUM_DELAY_IPV6_DATA
& m
->m_pkthdr
.csum_flags
;
3818 /* do in software what the hardware cannot */
3819 sw_csum
= m
->m_pkthdr
.csum_flags
&
3820 ~IF_HWASSIST_CSUM_FLAGS(hwcap
);
3824 sw_csum
|= (CSUM_DELAY_IPV6_DATA
&
3825 m
->m_pkthdr
.csum_flags
);
3826 } else if (!(sw_csum
& CSUM_DELAY_IPV6_DATA
) &&
3827 (hwcap
& CSUM_PARTIAL
)) {
3829 * Partial checksum offload, ere), if no extension
3830 * headers, and TCP only (no UDP support, as the
3831 * hardware may not be able to convert +0 to
3832 * -0 (0xffff) per RFC1122 4.1.3.4.)
3834 if (hwcksum_tx
&& !tso
&&
3835 (m
->m_pkthdr
.csum_flags
& CSUM_TCPIPV6
) &&
3837 uint16_t start
= sizeof (struct ip6_hdr
);
3839 m
->m_pkthdr
.csum_data
& 0xffff;
3840 m
->m_pkthdr
.csum_flags
|=
3841 (CSUM_DATA_VALID
| CSUM_PARTIAL
);
3842 m
->m_pkthdr
.csum_tx_stuff
= (ulpoff
+ start
);
3843 m
->m_pkthdr
.csum_tx_start
= start
;
3846 sw_csum
|= (CSUM_DELAY_IPV6_DATA
&
3847 m
->m_pkthdr
.csum_flags
);
3851 if (sw_csum
& CSUM_DELAY_IPV6_DATA
) {
3852 in6_delayed_cksum_offset(m
, 0, optlen
, nxt0
);
3853 sw_csum
&= ~CSUM_DELAY_IPV6_DATA
;
3858 * Drop off bits that aren't supported by hardware;
3859 * also make sure to preserve non-checksum related bits.
3861 m
->m_pkthdr
.csum_flags
=
3862 ((m
->m_pkthdr
.csum_flags
&
3863 (IF_HWASSIST_CSUM_FLAGS(hwcap
) | CSUM_DATA_VALID
)) |
3864 (m
->m_pkthdr
.csum_flags
& ~IF_HWASSIST_CSUM_MASK
));
3866 /* drop all bits; checksum offload is disabled */
3867 m
->m_pkthdr
.csum_flags
= 0;
3872 * Compute IPv6 extension header length.
3875 ip6_optlen(struct in6pcb
*in6p
)
3879 if (!in6p
->in6p_outputopts
)
3884 (((struct ip6_ext *)(x)) ? \
3885 (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3887 len
+= elen(in6p
->in6p_outputopts
->ip6po_hbh
);
3888 if (in6p
->in6p_outputopts
->ip6po_rthdr
) {
3889 /* dest1 is valid with rthdr only */
3890 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest1
);
3892 len
+= elen(in6p
->in6p_outputopts
->ip6po_rthdr
);
3893 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest2
);