2 * Copyright (c) 2000-2013 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 <security/mac.h>
145 #endif /* CONFIG_MACF_NET */
148 #include <netinet6/ip6_fw.h>
149 #include <netinet/ip_fw.h>
150 #include <netinet/ip_dummynet.h>
151 #endif /* DUMMYNET */
154 #include <net/pfvar.h>
157 static int ip6_copyexthdr(struct mbuf
**, caddr_t
, int);
158 static void ip6_out_cksum_stats(int, u_int32_t
);
159 static int ip6_insert_jumboopt(struct ip6_exthdrs
*, u_int32_t
);
160 static int ip6_insertfraghdr(struct mbuf
*, struct mbuf
*, int,
162 static int ip6_getpmtu(struct route_in6
*, struct route_in6
*,
163 struct ifnet
*, struct in6_addr
*, u_int32_t
*, boolean_t
*);
164 static int ip6_pcbopts(struct ip6_pktopts
**, struct mbuf
*, struct socket
*,
165 struct sockopt
*sopt
);
166 static int ip6_pcbopt(int, u_char
*, int, struct ip6_pktopts
**, int);
167 static int ip6_getpcbopt(struct ip6_pktopts
*, int, struct sockopt
*);
168 static int copypktopts(struct ip6_pktopts
*, struct ip6_pktopts
*, int);
169 static void im6o_trace(struct ip6_moptions
*, int);
170 static int ip6_setpktopt(int, u_char
*, int, struct ip6_pktopts
*, int,
172 static int ip6_splithdr(struct mbuf
*, struct ip6_exthdrs
*);
173 static void ip6_output_checksum(struct ifnet
*, uint32_t, struct mbuf
*,
174 int, uint32_t, uint32_t);
176 #define IM6O_TRACE_HIST_SIZE 32 /* size of trace history */
179 __private_extern__
unsigned int im6o_trace_hist_size
= IM6O_TRACE_HIST_SIZE
;
181 struct ip6_moptions_dbg
{
182 struct ip6_moptions im6o
; /* ip6_moptions */
183 u_int16_t im6o_refhold_cnt
; /* # of IM6O_ADDREF */
184 u_int16_t im6o_refrele_cnt
; /* # of IM6O_REMREF */
186 * Alloc and free callers.
191 * Circular lists of IM6O_ADDREF and IM6O_REMREF callers.
193 ctrace_t im6o_refhold
[IM6O_TRACE_HIST_SIZE
];
194 ctrace_t im6o_refrele
[IM6O_TRACE_HIST_SIZE
];
198 static unsigned int im6o_debug
= 1; /* debugging (enabled) */
200 static unsigned int im6o_debug
; /* debugging (disabled) */
203 static unsigned int im6o_size
; /* size of zone element */
204 static struct zone
*im6o_zone
; /* zone for ip6_moptions */
206 #define IM6O_ZONE_MAX 64 /* maximum elements in zone */
207 #define IM6O_ZONE_NAME "ip6_moptions" /* zone name */
209 SYSCTL_DECL(_net_inet6_ip6
);
211 static int ip6_maxchainsent
= 0;
212 SYSCTL_INT(_net_inet6_ip6
, OID_AUTO
, maxchainsent
,
213 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_maxchainsent
, 0,
214 "use dlil_output_list");
217 * XXX we don't handle mbuf chains yet in nd6_output() so ip6_output_list() only
218 * walks through the packet chain and sends each mbuf separately.
221 ip6_output_list(struct mbuf
*m0
, int packetlist
, struct ip6_pktopts
*opt
,
222 struct route_in6
*ro
, int flags
, struct ip6_moptions
*im6o
,
223 struct ifnet
**ifpp
, struct ip6_out_args
*ip6oa
)
225 #pragma unused(packetlist)
226 struct mbuf
*m
= m0
, *nextpkt
;
231 * Break the chain before calling ip6_output() and free the
232 * mbufs if there was an error.
234 nextpkt
= m
->m_nextpkt
;
236 error
= ip6_output(m
, opt
, ro
, flags
, im6o
, ifpp
, ip6oa
);
239 m_freem_list(nextpkt
);
249 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
250 * header (with pri, len, nxt, hlim, src, dst).
251 * This function may modify ver and hlim only.
252 * The mbuf chain containing the packet will be freed.
253 * The mbuf opt, if present, will not be freed.
255 * If ro is non-NULL and has valid ro->ro_rt, route lookup would be
256 * skipped and ro->ro_rt would be used. Otherwise the result of route
257 * lookup is stored in ro->ro_rt.
259 * type of "mtu": rt_rmx.rmx_mtu is u_int32_t, ifnet.ifr_mtu is int, and
260 * nd_ifinfo.linkmtu is u_int32_t. so we use u_int32_t to hold largest one,
261 * which is rt_rmx.rmx_mtu.
264 ip6_output(struct mbuf
*m0
, struct ip6_pktopts
*opt
, struct route_in6
*ro
,
265 int flags
, struct ip6_moptions
*im6o
, struct ifnet
**ifpp
,
266 struct ip6_out_args
*ip6oa
)
270 struct ifnet
*ifp
= NULL
, *origifp
= NULL
; /* refcnt'd */
271 struct mbuf
*m
, *mprev
;
272 int hlen
, tlen
, len
, off
, nxt0
;
273 struct route_in6
*ro_pmtu
= NULL
;
274 struct rtentry
*rt
= NULL
;
275 struct sockaddr_in6
*dst
, src_sa
, dst_sa
;
277 struct in6_ifaddr
*ia
= NULL
, *src_ia
= NULL
;
279 boolean_t alwaysfrag
= FALSE
;
280 u_int32_t optlen
= 0, plen
= 0, unfragpartlen
= 0;
281 struct ip6_rthdr
*rh
;
282 struct in6_addr finaldst
;
283 ipfilter_t inject_filter_ref
;
284 struct ipf_pktopts
*ippo
= NULL
;
285 struct flowadv
*adv
= NULL
;
288 struct ip6_out_args saved_ip6oa
;
289 struct sockaddr_in6 dst_buf
;
290 #endif /* DUMMYNET */
292 struct socket
*so
= NULL
;
293 struct secpolicy
*sp
= NULL
;
294 struct route_in6
*ipsec_saved_route
= NULL
;
295 boolean_t needipsectun
= FALSE
;
298 struct ipf_pktopts ipf_pktopts
;
299 struct ip6_exthdrs exthdrs
;
300 struct route_in6 ip6route
;
302 struct ipsec_output_state ipsec_state
;
305 struct route_in6 saved_route
;
306 struct route_in6 saved_ro_pmtu
;
307 struct ip_fw_args args
;
308 #endif /* DUMMYNET */
310 #define ipf_pktopts ip6obz.ipf_pktopts
311 #define exthdrs ip6obz.exthdrs
312 #define ip6route ip6obz.ip6route
313 #define ipsec_state ip6obz.ipsec_state
314 #define saved_route ip6obz.saved_route
315 #define saved_ro_pmtu ip6obz.saved_ro_pmtu
316 #define args ip6obz.args
319 boolean_t select_srcif
: 1;
320 boolean_t hdrsplit
: 1;
321 boolean_t dontfrag
: 1;
323 boolean_t needipsec
: 1;
324 boolean_t noipsec
: 1;
328 } ip6obf
= { .raw
= 0 };
330 VERIFY(m0
->m_flags
& M_PKTHDR
);
332 /* zero out {saved_route, saved_ro_pmtu, ip6route, exthdrs, args} */
333 bzero(&ip6obz
, sizeof (ip6obz
));
336 if (SLIST_EMPTY(&m0
->m_pkthdr
.tags
))
339 /* Grab info from mtags prepended to the chain */
340 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
,
341 KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
342 struct dn_pkt_tag
*dn_tag
;
344 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
345 args
.fwa_pf_rule
= dn_tag
->dn_pf_rule
;
347 bcopy(&dn_tag
->dn_dst6
, &dst_buf
, sizeof (dst_buf
));
349 ifp
= dn_tag
->dn_ifp
;
351 ifnet_reference(ifp
);
352 flags
= dn_tag
->dn_flags
;
353 if (dn_tag
->dn_flags
& IPV6_OUTARGS
) {
354 saved_ip6oa
= dn_tag
->dn_ip6oa
;
355 ip6oa
= &saved_ip6oa
;
358 saved_route
= dn_tag
->dn_ro6
;
360 saved_ro_pmtu
= dn_tag
->dn_ro6_pmtu
;
361 ro_pmtu
= &saved_ro_pmtu
;
362 origifp
= dn_tag
->dn_origifp
;
364 ifnet_reference(origifp
);
365 mtu
= dn_tag
->dn_mtu
;
366 alwaysfrag
= (dn_tag
->dn_alwaysfrag
!= 0);
367 unfragpartlen
= dn_tag
->dn_unfragpartlen
;
369 bcopy(&dn_tag
->dn_exthdrs
, &exthdrs
, sizeof (exthdrs
));
371 m_tag_delete(m0
, tag
);
375 #endif /* DUMMYNET */
378 m
->m_pkthdr
.pkt_flags
&= ~(PKTF_LOOP
|PKTF_IFAINFO
);
381 /* for AH processing. stupid to have "socket" variable in IP layer... */
382 if (ipsec_bypass
== 0) {
383 so
= ipsec_getsocket(m
);
384 (void) ipsec_setsocket(m
, NULL
);
386 /* If packet is bound to an interface, check bound policies */
387 if ((flags
& IPV6_OUTARGS
) &&
388 (ip6oa
->ip6oa_flags
& IPOAF_BOUND_IF
) &&
389 ip6oa
->ip6oa_boundif
!= IFSCOPE_NONE
) {
390 /* ip6obf.noipsec is a bitfield, use temp integer */
393 if (ipsec6_getpolicybyinterface(m
, IPSEC_DIR_OUTBOUND
,
394 flags
, ip6oa
, &noipsec
, &sp
) != 0)
397 ip6obf
.noipsec
= (noipsec
!= 0);
402 ip6
= mtod(m
, struct ip6_hdr
*);
404 finaldst
= ip6
->ip6_dst
;
405 inject_filter_ref
= ipf_get_inject_filter(m
);
408 if (ip6_doscopedroute
&& (flags
& IPV6_OUTARGS
)) {
410 * In the forwarding case, only the ifscope value is used,
411 * as source interface selection doesn't take place.
413 if ((ip6obf
.select_srcif
= (!(flags
& (IPV6_FORWARDING
|
414 IPV6_UNSPECSRC
| IPV6_FLAG_NOSRCIFSEL
)) &&
415 (ip6oa
->ip6oa_flags
& IP6OAF_SELECT_SRCIF
))))
416 ipf_pktopts
.ippo_flags
|= IPPOF_SELECT_SRCIF
;
418 if ((ip6oa
->ip6oa_flags
& IP6OAF_BOUND_IF
) &&
419 ip6oa
->ip6oa_boundif
!= IFSCOPE_NONE
) {
420 ipf_pktopts
.ippo_flags
|= (IPPOF_BOUND_IF
|
421 (ip6oa
->ip6oa_boundif
<< IPPOF_SHIFT_IFSCOPE
));
424 if (ip6oa
->ip6oa_flags
& IP6OAF_BOUND_SRCADDR
)
425 ipf_pktopts
.ippo_flags
|= IPPOF_BOUND_SRCADDR
;
427 ip6obf
.select_srcif
= FALSE
;
428 if (flags
& IPV6_OUTARGS
) {
429 ip6oa
->ip6oa_boundif
= IFSCOPE_NONE
;
430 ip6oa
->ip6oa_flags
&= ~(IP6OAF_SELECT_SRCIF
|
431 IP6OAF_BOUND_IF
| IP6OAF_BOUND_SRCADDR
);
435 if ((flags
& IPV6_OUTARGS
) && (ip6oa
->ip6oa_flags
& IP6OAF_NO_CELLULAR
))
436 ipf_pktopts
.ippo_flags
|= IPPOF_NO_IFT_CELLULAR
;
438 if (flags
& IPV6_OUTARGS
) {
439 adv
= &ip6oa
->ip6oa_flowadv
;
440 adv
->code
= FADV_SUCCESS
;
441 ip6oa
->ip6oa_retflags
= 0;
445 if (args
.fwa_pf_rule
) {
446 ip6
= mtod(m
, struct ip6_hdr
*);
447 VERIFY(ro
!= NULL
); /* ro == saved_route */
450 #endif /* DUMMYNET */
452 #define MAKE_EXTHDR(hp, mp) do { \
454 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
455 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
456 ((eh)->ip6e_len + 1) << 3); \
463 /* Hop-by-Hop options header */
464 MAKE_EXTHDR(opt
->ip6po_hbh
, &exthdrs
.ip6e_hbh
);
465 /* Destination options header(1st part) */
466 if (opt
->ip6po_rthdr
) {
468 * Destination options header(1st part)
469 * This only makes sense with a routing header.
470 * See Section 9.2 of RFC 3542.
471 * Disabling this part just for MIP6 convenience is
472 * a bad idea. We need to think carefully about a
473 * way to make the advanced API coexist with MIP6
474 * options, which might automatically be inserted in
477 MAKE_EXTHDR(opt
->ip6po_dest1
, &exthdrs
.ip6e_dest1
);
480 MAKE_EXTHDR(opt
->ip6po_rthdr
, &exthdrs
.ip6e_rthdr
);
481 /* Destination options header(2nd part) */
482 MAKE_EXTHDR(opt
->ip6po_dest2
, &exthdrs
.ip6e_dest2
);
488 if (ipsec_bypass
!= 0 || ip6obf
.noipsec
)
491 /* May have been set above if packet was bound */
493 /* get a security policy for this packet */
495 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
,
498 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
,
502 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
510 switch (sp
->policy
) {
511 case IPSEC_POLICY_DISCARD
:
512 case IPSEC_POLICY_GENERATE
:
514 * This packet is just discarded.
516 IPSEC_STAT_INCREMENT(ipsec6stat
.out_polvio
);
519 case IPSEC_POLICY_BYPASS
:
520 case IPSEC_POLICY_NONE
:
521 /* no need to do IPsec. */
522 ip6obf
.needipsec
= FALSE
;
525 case IPSEC_POLICY_IPSEC
:
526 if (sp
->req
== NULL
) {
527 /* acquire a policy */
528 error
= key_spdacquire(sp
);
532 /* Verify the redirect to ipsec interface */
533 if (sp
->ipsec_if
== ifp
) {
534 /* Set policy for mbuf */
535 m
->m_pkthdr
.ipsec_policy
= sp
->id
;
540 ip6obf
.needipsec
= TRUE
;
544 case IPSEC_POLICY_ENTRUST
:
546 printf("%s: Invalid policy found: %d\n", __func__
, sp
->policy
);
553 * Calculate the total length of the extension header chain.
554 * Keep the length of the unfragmentable part for fragmentation.
557 if (exthdrs
.ip6e_hbh
!= NULL
)
558 optlen
+= exthdrs
.ip6e_hbh
->m_len
;
559 if (exthdrs
.ip6e_dest1
!= NULL
)
560 optlen
+= exthdrs
.ip6e_dest1
->m_len
;
561 if (exthdrs
.ip6e_rthdr
!= NULL
)
562 optlen
+= exthdrs
.ip6e_rthdr
->m_len
;
563 unfragpartlen
= optlen
+ sizeof (struct ip6_hdr
);
565 /* NOTE: we don't add AH/ESP length here. do that later. */
566 if (exthdrs
.ip6e_dest2
!= NULL
)
567 optlen
+= exthdrs
.ip6e_dest2
->m_len
;
570 * If we need IPsec, or there is at least one extension header,
571 * separate IP6 header from the payload.
577 optlen
) && !ip6obf
.hdrsplit
) {
578 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
582 m
= exthdrs
.ip6e_ip6
;
583 ip6obf
.hdrsplit
= TRUE
;
587 ip6
= mtod(m
, struct ip6_hdr
*);
589 /* adjust mbuf packet header length */
590 m
->m_pkthdr
.len
+= optlen
;
591 plen
= m
->m_pkthdr
.len
- sizeof (*ip6
);
593 /* If this is a jumbo payload, insert a jumbo payload option. */
594 if (plen
> IPV6_MAXPACKET
) {
595 if (!ip6obf
.hdrsplit
) {
596 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
600 m
= exthdrs
.ip6e_ip6
;
601 ip6obf
.hdrsplit
= TRUE
;
604 ip6
= mtod(m
, struct ip6_hdr
*);
605 if ((error
= ip6_insert_jumboopt(&exthdrs
, plen
)) != 0)
609 ip6
->ip6_plen
= htons(plen
);
612 * Concatenate headers and fill in next header fields.
613 * Here we have, on "m"
615 * and we insert headers accordingly. Finally, we should be getting:
616 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
618 * during the header composing process, "m" points to IPv6 header.
619 * "mprev" points to an extension header prior to esp.
621 nexthdrp
= &ip6
->ip6_nxt
;
625 * we treat dest2 specially. this makes IPsec processing
626 * much easier. the goal here is to make mprev point the
627 * mbuf prior to dest2.
629 * result: IPv6 dest2 payload
630 * m and mprev will point to IPv6 header.
632 if (exthdrs
.ip6e_dest2
!= NULL
) {
633 if (!ip6obf
.hdrsplit
) {
634 panic("assumption failed: hdr not split");
637 exthdrs
.ip6e_dest2
->m_next
= m
->m_next
;
638 m
->m_next
= exthdrs
.ip6e_dest2
;
639 *mtod(exthdrs
.ip6e_dest2
, u_char
*) = ip6
->ip6_nxt
;
640 ip6
->ip6_nxt
= IPPROTO_DSTOPTS
;
643 #define MAKE_CHAIN(m, mp, p, i) do { \
645 if (!ip6obf.hdrsplit) { \
646 panic("assumption failed: hdr not split"); \
649 *mtod((m), u_char *) = *(p); \
651 p = mtod((m), u_char *); \
652 (m)->m_next = (mp)->m_next; \
653 (mp)->m_next = (m); \
658 * result: IPv6 hbh dest1 rthdr dest2 payload
659 * m will point to IPv6 header. mprev will point to the
660 * extension header prior to dest2 (rthdr in the above case).
662 MAKE_CHAIN(exthdrs
.ip6e_hbh
, mprev
, nexthdrp
, IPPROTO_HOPOPTS
);
663 MAKE_CHAIN(exthdrs
.ip6e_dest1
, mprev
, nexthdrp
, IPPROTO_DSTOPTS
);
664 MAKE_CHAIN(exthdrs
.ip6e_rthdr
, mprev
, nexthdrp
, IPPROTO_ROUTING
);
669 if (ip6obf
.needipsec
&& (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
))
670 in6_delayed_cksum_offset(m
, 0, optlen
, nxt0
);
673 if (!TAILQ_EMPTY(&ipv6_filters
)) {
674 struct ipfilter
*filter
;
675 int seen
= (inject_filter_ref
== NULL
);
678 if (im6o
!= NULL
&& IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
679 ippo
->ippo_flags
|= IPPOF_MCAST_OPTS
;
681 ippo
->ippo_mcast_ifnet
= im6o
->im6o_multicast_ifp
;
682 ippo
->ippo_mcast_ttl
= im6o
->im6o_multicast_hlim
;
683 ippo
->ippo_mcast_loop
= im6o
->im6o_multicast_loop
;
687 /* Hack: embed the scope_id in the destination */
688 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
) &&
689 (ip6
->ip6_dst
.s6_addr16
[1] == 0) && (ro
!= NULL
)) {
691 ip6
->ip6_dst
.s6_addr16
[1] =
692 htons(ro
->ro_dst
.sin6_scope_id
);
696 TAILQ_FOREACH(filter
, &ipv6_filters
, ipf_link
) {
698 * Don't process packet twice if we've already seen it.
701 if ((struct ipfilter
*)inject_filter_ref
==
704 } else if (filter
->ipf_filter
.ipf_output
!= NULL
) {
707 result
= filter
->ipf_filter
.ipf_output(
708 filter
->ipf_filter
.cookie
,
710 if (result
== EJUSTRETURN
) {
722 ip6
= mtod(m
, struct ip6_hdr
*);
723 /* Hack: cleanup embedded scope_id if we put it there */
725 ip6
->ip6_dst
.s6_addr16
[1] = 0;
729 if (ip6obf
.needipsec
) {
733 * pointers after IPsec headers are not valid any more.
734 * other pointers need a great care too.
735 * (IPsec routines should not mangle mbufs prior to AH/ESP)
737 exthdrs
.ip6e_dest2
= NULL
;
739 if (exthdrs
.ip6e_rthdr
!= NULL
) {
740 rh
= mtod(exthdrs
.ip6e_rthdr
, struct ip6_rthdr
*);
741 segleft_org
= rh
->ip6r_segleft
;
742 rh
->ip6r_segleft
= 0;
749 error
= ipsec6_output_trans(&ipsec_state
, nexthdrp
, mprev
,
750 sp
, flags
, &needipsectun
);
753 /* mbuf is already reclaimed in ipsec6_output_trans. */
763 printf("ip6_output (ipsec): error code %d\n",
767 /* don't show these error codes to the user */
773 if (exthdrs
.ip6e_rthdr
!= NULL
) {
774 /* ah6_output doesn't modify mbuf chain */
775 rh
->ip6r_segleft
= segleft_org
;
781 * If there is a routing header, replace the destination address field
782 * with the first hop of the routing header.
784 if (exthdrs
.ip6e_rthdr
!= NULL
) {
785 struct ip6_rthdr0
*rh0
;
786 struct in6_addr
*addr
;
787 struct sockaddr_in6 sa
;
789 rh
= (struct ip6_rthdr
*)
790 (mtod(exthdrs
.ip6e_rthdr
, struct ip6_rthdr
*));
791 switch (rh
->ip6r_type
) {
792 case IPV6_RTHDR_TYPE_0
:
793 rh0
= (struct ip6_rthdr0
*)rh
;
794 addr
= (struct in6_addr
*)(void *)(rh0
+ 1);
797 * construct a sockaddr_in6 form of
800 * XXX: we may not have enough
801 * information about its scope zone;
802 * there is no standard API to pass
803 * the information from the
806 bzero(&sa
, sizeof (sa
));
807 sa
.sin6_family
= AF_INET6
;
808 sa
.sin6_len
= sizeof (sa
);
809 sa
.sin6_addr
= addr
[0];
810 if ((error
= sa6_embedscope(&sa
,
811 ip6_use_defzone
)) != 0) {
814 ip6
->ip6_dst
= sa
.sin6_addr
;
815 bcopy(&addr
[1], &addr
[0], sizeof (struct in6_addr
) *
816 (rh0
->ip6r0_segleft
- 1));
817 addr
[rh0
->ip6r0_segleft
- 1] = finaldst
;
819 in6_clearscope(addr
+ rh0
->ip6r0_segleft
- 1);
821 default: /* is it possible? */
827 /* Source address validation */
828 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) &&
829 !(flags
& IPV6_UNSPECSRC
)) {
831 ip6stat
.ip6s_badscope
++;
834 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
)) {
836 ip6stat
.ip6s_badscope
++;
840 ip6stat
.ip6s_localout
++;
847 bzero((caddr_t
)ro
, sizeof (*ro
));
849 VERIFY(ro_pmtu
== NULL
); /* must not get here if dummynet */
851 if (opt
!= NULL
&& opt
->ip6po_rthdr
)
852 ro
= &opt
->ip6po_route
;
853 dst
= SIN6(&ro
->ro_dst
);
855 if (ro
->ro_rt
!= NULL
)
856 RT_LOCK_ASSERT_NOTHELD(ro
->ro_rt
);
858 * if specified, try to fill in the traffic class field.
859 * do not override if a non-zero value is already set.
860 * we check the diffserv field and the ecn field separately.
862 if (opt
!= NULL
&& opt
->ip6po_tclass
>= 0) {
865 if ((ip6
->ip6_flow
& htonl(0xfc << 20)) == 0)
867 if ((ip6
->ip6_flow
& htonl(0x03 << 20)) == 0)
871 htonl((opt
->ip6po_tclass
& mask
) << 20);
875 /* fill in or override the hop limit field, if necessary. */
876 if (opt
&& opt
->ip6po_hlim
!= -1) {
877 ip6
->ip6_hlim
= opt
->ip6po_hlim
& 0xff;
878 } else if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
881 ip6
->ip6_hlim
= im6o
->im6o_multicast_hlim
;
884 ip6
->ip6_hlim
= ip6_defmcasthlim
;
889 * If there is a cached route, check that it is to the same
890 * destination and is still up. If not, free it and try again.
891 * Test rt_flags without holding rt_lock for performance reasons;
892 * if the route is down it will hopefully be caught by the layer
893 * below (since it uses this route as a hint) or during the
896 if (ROUTE_UNUSABLE(ro
) || dst
->sin6_family
!= AF_INET6
||
897 !IN6_ARE_ADDR_EQUAL(&dst
->sin6_addr
, &ip6
->ip6_dst
))
900 if (ro
->ro_rt
== NULL
) {
901 bzero(dst
, sizeof (*dst
));
902 dst
->sin6_family
= AF_INET6
;
903 dst
->sin6_len
= sizeof (struct sockaddr_in6
);
904 dst
->sin6_addr
= ip6
->ip6_dst
;
907 if (ip6obf
.needipsec
&& needipsectun
) {
909 struct ifnet
*trace_ifp
= (ifpp
!= NULL
) ? (*ifpp
) : NULL
;
910 #endif /* CONFIG_DTRACE */
912 * All the extension headers will become inaccessible
913 * (since they can be encrypted).
914 * Don't panic, we need no more updates to extension headers
915 * on inner IPv6 packet (since they are now encapsulated).
917 * IPv6 [ESP|AH] IPv6 [extension headers] payload
919 bzero(&exthdrs
, sizeof (exthdrs
));
920 exthdrs
.ip6e_ip6
= m
;
923 route_copyout(&ipsec_state
.ro
, (struct route
*)ro
,
924 sizeof (ipsec_state
.ro
));
925 ipsec_state
.dst
= SA(dst
);
927 /* So that we can see packets inside the tunnel */
928 DTRACE_IP6(send
, struct mbuf
*, m
, struct inpcb
*, NULL
,
929 struct ip6_hdr
*, ip6
, struct ifnet
*, trace_ifp
,
930 struct ip
*, NULL
, struct ip6_hdr
*, ip6
);
932 error
= ipsec6_output_tunnel(&ipsec_state
, sp
, flags
);
933 /* tunneled in IPv4? packet is gone */
934 if (ipsec_state
.tunneled
== 4)
937 ipsec_saved_route
= ro
;
938 ro
= (struct route_in6
*)&ipsec_state
.ro
;
939 dst
= SIN6(ipsec_state
.dst
);
941 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
952 printf("ip6_output (ipsec): error code %d\n",
956 /* don't show these error codes to the user */
963 * The packet has been encapsulated so the ifscope
964 * is no longer valid since it does not apply to the
965 * outer address: ignore the ifscope.
967 if (flags
& IPV6_OUTARGS
) {
968 ip6oa
->ip6oa_boundif
= IFSCOPE_NONE
;
969 ip6oa
->ip6oa_flags
&= ~IP6OAF_BOUND_IF
;
971 if (opt
!= NULL
&& opt
->ip6po_pktinfo
!= NULL
) {
972 if (opt
->ip6po_pktinfo
->ipi6_ifindex
!= IFSCOPE_NONE
)
973 opt
->ip6po_pktinfo
->ipi6_ifindex
= IFSCOPE_NONE
;
975 exthdrs
.ip6e_ip6
= m
;
986 ip6
= mtod(m
, struct ip6_hdr
*);
988 if (ip6obf
.select_srcif
) {
989 bzero(&src_sa
, sizeof (src_sa
));
990 src_sa
.sin6_family
= AF_INET6
;
991 src_sa
.sin6_len
= sizeof (src_sa
);
992 src_sa
.sin6_addr
= ip6
->ip6_src
;
994 bzero(&dst_sa
, sizeof (dst_sa
));
995 dst_sa
.sin6_family
= AF_INET6
;
996 dst_sa
.sin6_len
= sizeof (dst_sa
);
997 dst_sa
.sin6_addr
= ip6
->ip6_dst
;
1000 * in6_selectroute() might return an ifp with its reference held
1001 * even in the error case, so make sure to release its reference.
1002 * ip6oa may be NULL if IPV6_OUTARGS isn't set.
1004 if ((error
= in6_selectroute(ip6obf
.select_srcif
? &src_sa
: NULL
,
1005 &dst_sa
, opt
, im6o
, &src_ia
, ro
, &ifp
, &rt
, 0, ip6oa
)) != 0) {
1008 ip6stat
.ip6s_noroute
++;
1012 break; /* XXX statistics? */
1015 in6_ifstat_inc(ifp
, ifs6_out_discard
);
1016 /* ifp (if non-NULL) will be released at the end */
1021 * If in6_selectroute() does not return a route entry,
1022 * dst may not have been updated.
1024 *dst
= dst_sa
; /* XXX */
1028 * then rt (for unicast) and ifp must be non-NULL valid values.
1030 if (!(flags
& IPV6_FORWARDING
)) {
1031 /* XXX: the FORWARDING flag can be set for mrouting. */
1032 in6_ifstat_inc_na(ifp
, ifs6_out_request
);
1036 ia
= (struct in6_ifaddr
*)(rt
->rt_ifa
);
1038 IFA_ADDREF(&ia
->ia_ifa
);
1044 * The outgoing interface must be in the zone of source and
1045 * destination addresses (except local/loopback). We should
1046 * use ia_ifp to support the case of sending packets to an
1047 * address of our own.
1049 if (ia
!= NULL
&& ia
->ia_ifp
) {
1050 ifnet_reference(ia
->ia_ifp
); /* for origifp */
1051 if (origifp
!= NULL
)
1052 ifnet_release(origifp
);
1053 origifp
= ia
->ia_ifp
;
1056 ifnet_reference(ifp
); /* for origifp */
1057 if (origifp
!= NULL
)
1058 ifnet_release(origifp
);
1062 /* skip scope enforcements for local/loopback route */
1063 if (rt
== NULL
|| !(rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
)) {
1064 struct in6_addr src0
, dst0
;
1067 src0
= ip6
->ip6_src
;
1068 if (in6_setscope(&src0
, origifp
, &zone
))
1070 bzero(&src_sa
, sizeof (src_sa
));
1071 src_sa
.sin6_family
= AF_INET6
;
1072 src_sa
.sin6_len
= sizeof (src_sa
);
1073 src_sa
.sin6_addr
= ip6
->ip6_src
;
1074 if ((sa6_recoverscope(&src_sa
, TRUE
) ||
1075 zone
!= src_sa
.sin6_scope_id
))
1078 dst0
= ip6
->ip6_dst
;
1079 if ((in6_setscope(&dst0
, origifp
, &zone
)))
1081 /* re-initialize to be sure */
1082 bzero(&dst_sa
, sizeof (dst_sa
));
1083 dst_sa
.sin6_family
= AF_INET6
;
1084 dst_sa
.sin6_len
= sizeof (dst_sa
);
1085 dst_sa
.sin6_addr
= ip6
->ip6_dst
;
1086 if ((sa6_recoverscope(&dst_sa
, TRUE
) ||
1087 zone
!= dst_sa
.sin6_scope_id
))
1090 /* scope check is done. */
1094 ip6stat
.ip6s_badscope
++;
1095 in6_ifstat_inc(origifp
, ifs6_out_discard
);
1097 error
= EHOSTUNREACH
; /* XXX */
1102 if (rt
!= NULL
&& !IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1103 if (opt
!= NULL
&& opt
->ip6po_nextroute
.ro_rt
) {
1105 * The nexthop is explicitly specified by the
1106 * application. We assume the next hop is an IPv6
1109 dst
= SIN6(opt
->ip6po_nexthop
);
1110 } else if ((rt
->rt_flags
& RTF_GATEWAY
)) {
1111 dst
= SIN6(rt
->rt_gateway
);
1114 * For packets destined to local/loopback, record the
1115 * source the source interface (which owns the source
1116 * address), as well as the output interface. This is
1117 * needed to reconstruct the embedded zone for the
1118 * link-local address case in ip6_input().
1120 if (ia
!= NULL
&& (ifp
->if_flags
& IFF_LOOPBACK
)) {
1124 srcidx
= src_ia
->ia_ifp
->if_index
;
1125 else if (ro
->ro_srcia
!= NULL
)
1126 srcidx
= ro
->ro_srcia
->ifa_ifp
->if_index
;
1130 ip6_setsrcifaddr_info(m
, srcidx
, NULL
);
1131 ip6_setdstifaddr_info(m
, 0, ia
);
1135 if (!IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1136 m
->m_flags
&= ~(M_BCAST
| M_MCAST
); /* just in case */
1138 struct in6_multi
*in6m
;
1140 m
->m_flags
= (m
->m_flags
& ~M_BCAST
) | M_MCAST
;
1141 in6_ifstat_inc_na(ifp
, ifs6_out_mcast
);
1144 * Confirm that the outgoing interface supports multicast.
1146 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
1147 ip6stat
.ip6s_noroute
++;
1148 in6_ifstat_inc(ifp
, ifs6_out_discard
);
1149 error
= ENETUNREACH
;
1152 in6_multihead_lock_shared();
1153 IN6_LOOKUP_MULTI(&ip6
->ip6_dst
, ifp
, in6m
);
1154 in6_multihead_lock_done();
1158 (im6o
== NULL
|| im6o
->im6o_multicast_loop
)) {
1162 * If we belong to the destination multicast group
1163 * on the outgoing interface, and the caller did not
1164 * forbid loopback, loop back a copy.
1166 ip6_mloopback(NULL
, ifp
, m
, dst
, optlen
, nxt0
);
1171 * If we are acting as a multicast router, perform
1172 * multicast forwarding as if the packet had just
1173 * arrived on the interface to which we are about
1174 * to send. The multicast forwarding function
1175 * recursively calls this function, using the
1176 * IPV6_FORWARDING flag to prevent infinite recursion.
1178 * Multicasts that are looped back by ip6_mloopback(),
1179 * above, will be forwarded by the ip6_input() routine,
1183 if (ip6_mrouter
&& !(flags
& IPV6_FORWARDING
)) {
1185 * XXX: ip6_mforward expects that rcvif is NULL
1186 * when it is called from the originating path.
1187 * However, it is not always the case, since
1188 * some versions of MGETHDR() does not
1189 * initialize the field.
1191 m
->m_pkthdr
.rcvif
= NULL
;
1192 if (ip6_mforward(ip6
, ifp
, m
) != 0) {
1199 #endif /* MROUTING */
1204 * Multicasts with a hoplimit of zero may be looped back,
1205 * above, but must not be transmitted on a network.
1206 * Also, multicasts addressed to the loopback interface
1207 * are not sent -- the above call to ip6_mloopback() will
1208 * loop back a copy if this host actually belongs to the
1209 * destination group on the loopback interface.
1211 if (ip6
->ip6_hlim
== 0 || (ifp
->if_flags
& IFF_LOOPBACK
) ||
1212 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6
->ip6_dst
)) {
1219 * Fill the outgoing inteface to tell the upper layer
1220 * to increment per-interface statistics.
1223 ifnet_reference(ifp
); /* for caller */
1225 ifnet_release(*ifpp
);
1229 /* Determine path MTU. */
1230 if ((error
= ip6_getpmtu(ro_pmtu
, ro
, ifp
, &finaldst
, &mtu
,
1235 * The caller of this function may specify to use the minimum MTU
1237 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
1238 * setting. The logic is a bit complicated; by default, unicast
1239 * packets will follow path MTU while multicast packets will be sent at
1240 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
1241 * including unicast ones will be sent at the minimum MTU. Multicast
1242 * packets will always be sent at the minimum MTU unless
1243 * IP6PO_MINMTU_DISABLE is explicitly specified.
1244 * See RFC 3542 for more details.
1246 if (mtu
> IPV6_MMTU
) {
1247 if ((flags
& IPV6_MINMTU
)) {
1249 } else if (opt
&& opt
->ip6po_minmtu
== IP6PO_MINMTU_ALL
) {
1251 } else if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) &&
1253 opt
->ip6po_minmtu
!= IP6PO_MINMTU_DISABLE
)) {
1259 * clear embedded scope identifiers if necessary.
1260 * in6_clearscope will touch the addresses only when necessary.
1262 in6_clearscope(&ip6
->ip6_src
);
1263 in6_clearscope(&ip6
->ip6_dst
);
1267 * Check with the firewall...
1269 if (ip6_fw_enable
&& ip6_fw_chk_ptr
) {
1271 m
->m_pkthdr
.rcvif
= NULL
; /* XXX */
1272 /* If ipfw says divert, we have to just drop packet */
1273 if (ip6_fw_chk_ptr(&ip6
, ifp
, &port
, &m
)) {
1285 * If the outgoing packet contains a hop-by-hop options header,
1286 * it must be examined and processed even by the source node.
1287 * (RFC 2460, section 4.)
1289 if (exthdrs
.ip6e_hbh
!= NULL
) {
1290 struct ip6_hbh
*hbh
= mtod(exthdrs
.ip6e_hbh
, struct ip6_hbh
*);
1291 u_int32_t dummy
; /* XXX unused */
1292 uint32_t oplen
= 0; /* for ip6_process_hopopts() */
1294 if ((hbh
->ip6h_len
+ 1) << 3 > exthdrs
.ip6e_hbh
->m_len
)
1295 panic("ip6e_hbh is not continuous");
1298 * XXX: If we have to send an ICMPv6 error to the sender,
1299 * we need the M_LOOP flag since icmp6_error() expects
1300 * the IPv6 and the hop-by-hop options header are
1301 * continuous unless the flag is set.
1303 m
->m_flags
|= M_LOOP
;
1304 m
->m_pkthdr
.rcvif
= ifp
;
1305 if (ip6_process_hopopts(m
, (u_int8_t
*)(hbh
+ 1),
1306 ((hbh
->ip6h_len
+ 1) << 3) - sizeof (struct ip6_hbh
),
1307 &dummy
, &oplen
) < 0) {
1308 /* m was already freed at this point */
1309 error
= EINVAL
; /* better error? */
1312 m
->m_flags
&= ~M_LOOP
; /* XXX */
1313 m
->m_pkthdr
.rcvif
= NULL
;
1318 #endif /* DUMMYNET */
1320 if (PF_IS_ENABLED
) {
1323 * TODO: Need to save opt->ip6po_flags for reinjection
1328 args
.fwa_oflags
= flags
;
1329 if (flags
& IPV6_OUTARGS
)
1330 args
.fwa_ip6oa
= ip6oa
;
1332 args
.fwa_dst6
= dst
;
1333 args
.fwa_ro6_pmtu
= ro_pmtu
;
1334 args
.fwa_origifp
= origifp
;
1336 args
.fwa_alwaysfrag
= alwaysfrag
;
1337 args
.fwa_unfragpartlen
= unfragpartlen
;
1338 args
.fwa_exthdrs
= &exthdrs
;
1339 /* Invoke outbound packet filter */
1340 error
= pf_af_hook(ifp
, NULL
, &m
, AF_INET6
, FALSE
, &args
);
1341 #else /* !DUMMYNET */
1342 error
= pf_af_hook(ifp
, NULL
, &m
, AF_INET6
, FALSE
, NULL
);
1343 #endif /* !DUMMYNET */
1345 if (error
!= 0 || m
== NULL
) {
1347 * Note that if we ever handle packet chain, we will
1348 * have to restore the linkage from the previous
1349 * packet to the next like in ip_outout_list()
1352 panic("%s: unexpected packet %p\n",
1356 /* Already freed by callee */
1359 ip6
= mtod(m
, struct ip6_hdr
*);
1364 * Send the packet to the outgoing interface.
1365 * If necessary, do IPv6 fragmentation before sending.
1367 * the logic here is rather complex:
1368 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
1369 * 1-a: send as is if tlen <= path mtu
1370 * 1-b: fragment if tlen > path mtu
1372 * 2: if user asks us not to fragment (dontfrag == 1)
1373 * 2-a: send as is if tlen <= interface mtu
1374 * 2-b: error if tlen > interface mtu
1376 * 3: if we always need to attach fragment header (alwaysfrag == 1)
1379 * 4: if dontfrag == 1 && alwaysfrag == 1
1380 * error, as we cannot handle this conflicting request
1382 tlen
= m
->m_pkthdr
.len
;
1384 if (opt
!= NULL
&& (opt
->ip6po_flags
& IP6PO_DONTFRAG
))
1385 ip6obf
.dontfrag
= TRUE
;
1387 ip6obf
.dontfrag
= FALSE
;
1388 if (ip6obf
.dontfrag
&& alwaysfrag
) { /* case 4 */
1389 /* conflicting request - can't transmit */
1394 lck_rw_lock_shared(nd_if_rwlock
);
1395 /* Access without acquiring nd_ifinfo lock for performance */
1396 if (ip6obf
.dontfrag
&& tlen
> IN6_LINKMTU(ifp
)) { /* case 2-b */
1397 lck_rw_done(nd_if_rwlock
);
1399 * Even if the DONTFRAG option is specified, we cannot send the
1400 * packet when the data length is larger than the MTU of the
1401 * outgoing interface.
1402 * Notify the error by sending IPV6_PATHMTU ancillary data as
1403 * well as returning an error code (the latter is not described
1407 struct ip6ctlparam ip6cp
;
1409 mtu32
= (u_int32_t
)mtu
;
1410 bzero(&ip6cp
, sizeof (ip6cp
));
1411 ip6cp
.ip6c_cmdarg
= (void *)&mtu32
;
1412 pfctlinput2(PRC_MSGSIZE
, SA(&ro_pmtu
->ro_dst
), (void *)&ip6cp
);
1416 lck_rw_done(nd_if_rwlock
);
1420 * transmit packet without fragmentation
1422 if (ip6obf
.dontfrag
|| (!alwaysfrag
&& /* case 1-a and 2-a */
1423 (tlen
<= mtu
|| TSO_IPV6_OK(ifp
, m
) ||
1424 (ifp
->if_hwassist
& CSUM_FRAGMENT_IPV6
)))) {
1426 /* clean ipsec history once it goes out of the node */
1430 ip6_output_checksum(ifp
, mtu
, m
, nxt0
, tlen
, optlen
);
1433 RT_LOCK_ASSERT_NOTHELD(ro
->ro_rt
);
1434 error
= nd6_output(ifp
, origifp
, m
, dst
, ro
->ro_rt
, adv
);
1439 * try to fragment the packet. case 1-b and 3
1441 if ((m
->m_pkthdr
.csum_flags
& CSUM_TSO_IPV6
)) {
1442 /* TSO and fragment aren't compatible */
1444 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1446 } else if (mtu
< IPV6_MMTU
) {
1447 /* path MTU cannot be less than IPV6_MMTU */
1449 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1451 } else if (ip6
->ip6_plen
== 0) {
1452 /* jumbo payload cannot be fragmented */
1454 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1457 struct mbuf
**mnext
, *m_frgpart
;
1458 struct ip6_frag
*ip6f
;
1459 u_int32_t id
= htonl(ip6_randomid());
1463 * Too large for the destination or interface;
1464 * fragment if possible.
1465 * Must be able to put at least 8 bytes per fragment.
1467 hlen
= unfragpartlen
;
1468 if (mtu
> IPV6_MAXPACKET
)
1469 mtu
= IPV6_MAXPACKET
;
1471 len
= (mtu
- hlen
- sizeof (struct ip6_frag
)) & ~7;
1474 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1478 mnext
= &m
->m_nextpkt
;
1481 * Change the next header field of the last header in the
1482 * unfragmentable part.
1484 if (exthdrs
.ip6e_rthdr
!= NULL
) {
1485 nextproto
= *mtod(exthdrs
.ip6e_rthdr
, u_char
*);
1486 *mtod(exthdrs
.ip6e_rthdr
, u_char
*) = IPPROTO_FRAGMENT
;
1487 } else if (exthdrs
.ip6e_dest1
!= NULL
) {
1488 nextproto
= *mtod(exthdrs
.ip6e_dest1
, u_char
*);
1489 *mtod(exthdrs
.ip6e_dest1
, u_char
*) = IPPROTO_FRAGMENT
;
1490 } else if (exthdrs
.ip6e_hbh
!= NULL
) {
1491 nextproto
= *mtod(exthdrs
.ip6e_hbh
, u_char
*);
1492 *mtod(exthdrs
.ip6e_hbh
, u_char
*) = IPPROTO_FRAGMENT
;
1494 nextproto
= ip6
->ip6_nxt
;
1495 ip6
->ip6_nxt
= IPPROTO_FRAGMENT
;
1498 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
)
1499 in6_delayed_cksum_offset(m
, 0, optlen
, nxt0
);
1502 * Loop through length of segment after first fragment,
1503 * make new header and copy data of each part and link onto
1507 for (off
= hlen
; off
< tlen
; off
+= len
) {
1508 struct ip6_hdr
*mhip6
;
1510 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1513 ip6stat
.ip6s_odropped
++;
1516 m
->m_pkthdr
.rcvif
= NULL
;
1517 m
->m_flags
= m0
->m_flags
& M_COPYFLAGS
;
1519 mnext
= &m
->m_nextpkt
;
1520 m
->m_data
+= max_linkhdr
;
1521 mhip6
= mtod(m
, struct ip6_hdr
*);
1523 m
->m_len
= sizeof (*mhip6
);
1524 error
= ip6_insertfraghdr(m0
, m
, hlen
, &ip6f
);
1526 ip6stat
.ip6s_odropped
++;
1529 ip6f
->ip6f_offlg
= htons((u_short
)((off
- hlen
) & ~7));
1530 if (off
+ len
>= tlen
)
1533 ip6f
->ip6f_offlg
|= IP6F_MORE_FRAG
;
1534 mhip6
->ip6_plen
= htons((u_short
)(len
+ hlen
+
1535 sizeof (*ip6f
) - sizeof (struct ip6_hdr
)));
1536 if ((m_frgpart
= m_copy(m0
, off
, len
)) == NULL
) {
1538 ip6stat
.ip6s_odropped
++;
1541 m_cat(m
, m_frgpart
);
1542 m
->m_pkthdr
.len
= len
+ hlen
+ sizeof (*ip6f
);
1543 m
->m_pkthdr
.rcvif
= NULL
;
1545 M_COPY_CLASSIFIER(m
, m0
);
1546 M_COPY_PFTAG(m
, m0
);
1550 mac_create_fragment(m0
, m
);
1551 #endif /* CONFIG_MACF_NET */
1554 ip6f
->ip6f_reserved
= 0;
1555 ip6f
->ip6f_ident
= id
;
1556 ip6f
->ip6f_nxt
= nextproto
;
1557 ip6stat
.ip6s_ofragments
++;
1558 in6_ifstat_inc(ifp
, ifs6_out_fragcreat
);
1561 in6_ifstat_inc(ifp
, ifs6_out_fragok
);
1565 * Remove leading garbages.
1569 m0
->m_nextpkt
= NULL
;
1571 for (m0
= m
; m
!= NULL
; m
= m0
) {
1573 m
->m_nextpkt
= NULL
;
1576 /* clean ipsec history once it goes out of the node */
1579 error
= nd6_output(ifp
, origifp
, m
, dst
, ro
->ro_rt
,
1587 ip6stat
.ip6s_fragmented
++;
1590 ROUTE_RELEASE(&ip6route
);
1592 ROUTE_RELEASE(&ipsec_state
.ro
);
1594 key_freesp(sp
, KEY_SADB_UNLOCKED
);
1597 ROUTE_RELEASE(&saved_route
);
1598 ROUTE_RELEASE(&saved_ro_pmtu
);
1599 #endif /* DUMMYNET */
1602 IFA_REMREF(&ia
->ia_ifa
);
1604 IFA_REMREF(&src_ia
->ia_ifa
);
1607 if (origifp
!= NULL
)
1608 ifnet_release(origifp
);
1612 if (exthdrs
.ip6e_hbh
!= NULL
)
1613 m_freem(exthdrs
.ip6e_hbh
);
1614 if (exthdrs
.ip6e_dest1
!= NULL
)
1615 m_freem(exthdrs
.ip6e_dest1
);
1616 if (exthdrs
.ip6e_rthdr
!= NULL
)
1617 m_freem(exthdrs
.ip6e_rthdr
);
1618 if (exthdrs
.ip6e_dest2
!= NULL
)
1619 m_freem(exthdrs
.ip6e_dest2
);
1631 #undef saved_ro_pmtu
1636 ip6_copyexthdr(struct mbuf
**mp
, caddr_t hdr
, int hlen
)
1640 if (hlen
> MCLBYTES
)
1641 return (ENOBUFS
); /* XXX */
1643 MGET(m
, M_DONTWAIT
, MT_DATA
);
1648 MCLGET(m
, M_DONTWAIT
);
1649 if (!(m
->m_flags
& M_EXT
)) {
1656 bcopy(hdr
, mtod(m
, caddr_t
), hlen
);
1663 ip6_out_cksum_stats(int proto
, u_int32_t len
)
1667 tcp_out6_cksum_stats(len
);
1670 udp_out6_cksum_stats(len
);
1673 /* keep only TCP or UDP stats for now */
1679 * Process a delayed payload checksum calculation (outbound path.)
1681 * hoff is the number of bytes beyond the mbuf data pointer which
1682 * points to the IPv6 header. optlen is the number of bytes, if any,
1683 * between the end of IPv6 header and the beginning of the ULP payload
1684 * header, which represents the extension headers. If optlen is less
1685 * than zero, this routine will bail when it detects extension headers.
1687 * Returns a bitmask representing all the work done in software.
1690 in6_finalize_cksum(struct mbuf
*m
, uint32_t hoff
, int32_t optlen
,
1691 int32_t nxt0
, uint32_t csum_flags
)
1693 unsigned char buf
[sizeof (struct ip6_hdr
)] __attribute__((aligned(8)));
1694 struct ip6_hdr
*ip6
;
1695 uint32_t offset
, mlen
, hlen
, olen
, sw_csum
;
1696 uint16_t csum
, ulpoff
, plen
;
1699 _CASSERT(sizeof (csum
) == sizeof (uint16_t));
1700 VERIFY(m
->m_flags
& M_PKTHDR
);
1702 sw_csum
= (csum_flags
& m
->m_pkthdr
.csum_flags
);
1704 if ((sw_csum
&= CSUM_DELAY_IPV6_DATA
) == 0)
1707 mlen
= m
->m_pkthdr
.len
; /* total mbuf len */
1708 hlen
= sizeof (*ip6
); /* IPv6 header len */
1710 /* sanity check (need at least IPv6 header) */
1711 if (mlen
< (hoff
+ hlen
)) {
1712 panic("%s: mbuf %p pkt len (%u) < hoff+ip6_hdr "
1713 "(%u+%u)\n", __func__
, m
, mlen
, hoff
, hlen
);
1718 * In case the IPv6 header is not contiguous, or not 32-bit
1719 * aligned, copy it to a local buffer.
1721 if ((hoff
+ hlen
) > m
->m_len
||
1722 !IP6_HDR_ALIGNED_P(mtod(m
, caddr_t
) + hoff
)) {
1723 m_copydata(m
, hoff
, hlen
, (caddr_t
)buf
);
1724 ip6
= (struct ip6_hdr
*)(void *)buf
;
1726 ip6
= (struct ip6_hdr
*)(void *)(m
->m_data
+ hoff
);
1730 plen
= ntohs(ip6
->ip6_plen
);
1731 if (plen
!= (mlen
- (hoff
+ hlen
))) {
1732 plen
= OSSwapInt16(plen
);
1733 if (plen
!= (mlen
- (hoff
+ hlen
))) {
1734 /* Don't complain for jumbograms */
1735 if (plen
!= 0 || nxt
!= IPPROTO_HOPOPTS
) {
1736 printf("%s: mbuf 0x%llx proto %d IPv6 "
1737 "plen %d (%x) [swapped %d (%x)] doesn't "
1738 "match actual packet length; %d is used "
1739 "instead\n", __func__
,
1740 (uint64_t)VM_KERNEL_ADDRPERM(m
), nxt
,
1741 ip6
->ip6_plen
, ip6
->ip6_plen
, plen
, plen
,
1742 (mlen
- (hoff
+ hlen
)));
1744 plen
= mlen
- (hoff
+ hlen
);
1749 /* next header isn't TCP/UDP and we don't know optlen, bail */
1750 if (nxt
!= IPPROTO_TCP
&& nxt
!= IPPROTO_UDP
) {
1756 /* caller supplied the original transport number; use it */
1762 offset
= hoff
+ hlen
+ olen
; /* ULP header */
1765 if (mlen
< offset
) {
1766 panic("%s: mbuf %p pkt len (%u) < hoff+ip6_hdr+ext_hdr "
1767 "(%u+%u+%u)\n", __func__
, m
, mlen
, hoff
, hlen
, olen
);
1772 * offset is added to the lower 16-bit value of csum_data,
1773 * which is expected to contain the ULP offset; therefore
1774 * CSUM_PARTIAL offset adjustment must be undone.
1776 if ((m
->m_pkthdr
.csum_flags
& (CSUM_PARTIAL
|CSUM_DATA_VALID
)) ==
1777 (CSUM_PARTIAL
|CSUM_DATA_VALID
)) {
1779 * Get back the original ULP offset (this will
1780 * undo the CSUM_PARTIAL logic in ip6_output.)
1782 m
->m_pkthdr
.csum_data
= (m
->m_pkthdr
.csum_tx_stuff
-
1783 m
->m_pkthdr
.csum_tx_start
);
1786 ulpoff
= (m
->m_pkthdr
.csum_data
& 0xffff); /* ULP csum offset */
1788 if (mlen
< (ulpoff
+ sizeof (csum
))) {
1789 panic("%s: mbuf %p pkt len (%u) proto %d invalid ULP "
1790 "cksum offset (%u) cksum flags 0x%x\n", __func__
,
1791 m
, mlen
, nxt
, ulpoff
, m
->m_pkthdr
.csum_flags
);
1795 csum
= inet6_cksum(m
, 0, offset
, plen
- olen
);
1798 ip6_out_cksum_stats(nxt
, plen
- olen
);
1800 /* RFC1122 4.1.3.4 */
1801 if (csum
== 0 && (m
->m_pkthdr
.csum_flags
& CSUM_UDPIPV6
))
1804 /* Insert the checksum in the ULP csum field */
1806 if ((offset
+ sizeof (csum
)) > m
->m_len
) {
1807 m_copyback(m
, offset
, sizeof (csum
), &csum
);
1808 } else if (IP6_HDR_ALIGNED_P(mtod(m
, char *) + hoff
)) {
1809 *(uint16_t *)(void *)(mtod(m
, char *) + offset
) = csum
;
1811 bcopy(&csum
, (mtod(m
, char *) + offset
), sizeof (csum
));
1813 m
->m_pkthdr
.csum_flags
&=
1814 ~(CSUM_DELAY_IPV6_DATA
| CSUM_DATA_VALID
| CSUM_PARTIAL
);
1821 * Insert jumbo payload option.
1824 ip6_insert_jumboopt(struct ip6_exthdrs
*exthdrs
, u_int32_t plen
)
1830 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1833 * If there is no hop-by-hop options header, allocate new one.
1834 * If there is one but it doesn't have enough space to store the
1835 * jumbo payload option, allocate a cluster to store the whole options.
1836 * Otherwise, use it to store the options.
1838 if (exthdrs
->ip6e_hbh
== NULL
) {
1839 MGET(mopt
, M_DONTWAIT
, MT_DATA
);
1842 mopt
->m_len
= JUMBOOPTLEN
;
1843 optbuf
= mtod(mopt
, u_char
*);
1844 optbuf
[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1845 exthdrs
->ip6e_hbh
= mopt
;
1847 struct ip6_hbh
*hbh
;
1849 mopt
= exthdrs
->ip6e_hbh
;
1850 if (M_TRAILINGSPACE(mopt
) < JUMBOOPTLEN
) {
1853 * - exthdrs->ip6e_hbh is not referenced from places
1854 * other than exthdrs.
1855 * - exthdrs->ip6e_hbh is not an mbuf chain.
1857 u_int32_t oldoptlen
= mopt
->m_len
;
1861 * XXX: give up if the whole (new) hbh header does
1862 * not fit even in an mbuf cluster.
1864 if (oldoptlen
+ JUMBOOPTLEN
> MCLBYTES
)
1868 * As a consequence, we must always prepare a cluster
1871 MGET(n
, M_DONTWAIT
, MT_DATA
);
1873 MCLGET(n
, M_DONTWAIT
);
1874 if (!(n
->m_flags
& M_EXT
)) {
1881 n
->m_len
= oldoptlen
+ JUMBOOPTLEN
;
1882 bcopy(mtod(mopt
, caddr_t
), mtod(n
, caddr_t
),
1884 optbuf
= mtod(n
, u_char
*) + oldoptlen
;
1886 mopt
= exthdrs
->ip6e_hbh
= n
;
1888 optbuf
= mtod(mopt
, u_char
*) + mopt
->m_len
;
1889 mopt
->m_len
+= JUMBOOPTLEN
;
1891 optbuf
[0] = IP6OPT_PADN
;
1895 * Adjust the header length according to the pad and
1896 * the jumbo payload option.
1898 hbh
= mtod(mopt
, struct ip6_hbh
*);
1899 hbh
->ip6h_len
+= (JUMBOOPTLEN
>> 3);
1902 /* fill in the option. */
1903 optbuf
[2] = IP6OPT_JUMBO
;
1905 v
= (u_int32_t
)htonl(plen
+ JUMBOOPTLEN
);
1906 bcopy(&v
, &optbuf
[4], sizeof (u_int32_t
));
1908 /* finally, adjust the packet header length */
1909 exthdrs
->ip6e_ip6
->m_pkthdr
.len
+= JUMBOOPTLEN
;
1916 * Insert fragment header and copy unfragmentable header portions.
1919 ip6_insertfraghdr(struct mbuf
*m0
, struct mbuf
*m
, int hlen
,
1920 struct ip6_frag
**frghdrp
)
1922 struct mbuf
*n
, *mlast
;
1924 if (hlen
> sizeof (struct ip6_hdr
)) {
1925 n
= m_copym(m0
, sizeof (struct ip6_hdr
),
1926 hlen
- sizeof (struct ip6_hdr
), M_DONTWAIT
);
1933 /* Search for the last mbuf of unfragmentable part. */
1934 for (mlast
= n
; mlast
->m_next
; mlast
= mlast
->m_next
)
1937 if (!(mlast
->m_flags
& M_EXT
) &&
1938 M_TRAILINGSPACE(mlast
) >= sizeof (struct ip6_frag
)) {
1939 /* use the trailing space of the last mbuf for the frag hdr */
1940 *frghdrp
= (struct ip6_frag
*)(mtod(mlast
, caddr_t
) +
1942 mlast
->m_len
+= sizeof (struct ip6_frag
);
1943 m
->m_pkthdr
.len
+= sizeof (struct ip6_frag
);
1945 /* allocate a new mbuf for the fragment header */
1948 MGET(mfrg
, M_DONTWAIT
, MT_DATA
);
1951 mfrg
->m_len
= sizeof (struct ip6_frag
);
1952 *frghdrp
= mtod(mfrg
, struct ip6_frag
*);
1953 mlast
->m_next
= mfrg
;
1960 ip6_getpmtu(struct route_in6
*ro_pmtu
, struct route_in6
*ro
,
1961 struct ifnet
*ifp
, struct in6_addr
*dst
, u_int32_t
*mtup
,
1962 boolean_t
*alwaysfragp
)
1965 boolean_t alwaysfrag
= FALSE
;
1968 if (ro_pmtu
!= ro
) {
1969 /* The first hop and the final destination may differ. */
1970 struct sockaddr_in6
*sa6_dst
= SIN6(&ro_pmtu
->ro_dst
);
1971 if (ROUTE_UNUSABLE(ro_pmtu
) ||
1972 !IN6_ARE_ADDR_EQUAL(&sa6_dst
->sin6_addr
, dst
))
1973 ROUTE_RELEASE(ro_pmtu
);
1975 if (ro_pmtu
->ro_rt
== NULL
) {
1976 bzero(sa6_dst
, sizeof (*sa6_dst
));
1977 sa6_dst
->sin6_family
= AF_INET6
;
1978 sa6_dst
->sin6_len
= sizeof (struct sockaddr_in6
);
1979 sa6_dst
->sin6_addr
= *dst
;
1981 rtalloc_scoped((struct route
*)ro_pmtu
,
1982 ifp
!= NULL
? ifp
->if_index
: IFSCOPE_NONE
);
1986 if (ro_pmtu
->ro_rt
!= NULL
) {
1989 lck_rw_lock_shared(nd_if_rwlock
);
1990 /* Access without acquiring nd_ifinfo lock for performance */
1991 ifmtu
= IN6_LINKMTU(ifp
);
1992 lck_rw_done(nd_if_rwlock
);
1995 * Access rmx_mtu without holding the route entry lock,
1996 * for performance; this isn't something that changes
1997 * often, so optimize.
1999 mtu
= ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
;
2000 if (mtu
> ifmtu
|| mtu
== 0) {
2002 * The MTU on the route is larger than the MTU on
2003 * the interface! This shouldn't happen, unless the
2004 * MTU of the interface has been changed after the
2005 * interface was brought up. Change the MTU in the
2006 * route to match the interface MTU (as long as the
2007 * field isn't locked).
2009 * if MTU on the route is 0, we need to fix the MTU.
2010 * this case happens with path MTU discovery timeouts.
2013 if (!(ro_pmtu
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
))
2014 ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
= mtu
; /* XXX */
2015 } else if (mtu
< IPV6_MMTU
) {
2017 * RFC2460 section 5, last paragraph:
2018 * if we record ICMPv6 too big message with
2019 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
2020 * or smaller, with framgent header attached.
2021 * (fragment header is needed regardless from the
2022 * packet size, for translators to identify packets)
2029 lck_rw_lock_shared(nd_if_rwlock
);
2030 /* Don't hold nd_ifinfo lock for performance */
2031 mtu
= IN6_LINKMTU(ifp
);
2032 lck_rw_done(nd_if_rwlock
);
2034 error
= EHOSTUNREACH
; /* XXX */
2039 if (alwaysfragp
!= NULL
)
2040 *alwaysfragp
= alwaysfrag
;
2045 * IP6 socket option processing.
2048 ip6_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
2050 int optdatalen
, uproto
;
2053 struct inpcb
*in6p
= sotoinpcb(so
);
2054 int error
= 0, optval
= 0;
2055 int level
, op
= -1, optname
= 0;
2059 VERIFY(sopt
!= NULL
);
2061 level
= sopt
->sopt_level
;
2062 op
= sopt
->sopt_dir
;
2063 optname
= sopt
->sopt_name
;
2064 optlen
= sopt
->sopt_valsize
;
2066 uproto
= (int)SOCK_PROTO(so
);
2068 privileged
= (proc_suser(p
) == 0);
2070 if (level
== IPPROTO_IPV6
) {
2074 case IPV6_2292PKTOPTIONS
: {
2077 error
= soopt_getm(sopt
, &m
);
2080 error
= soopt_mcopyin(sopt
, m
);
2083 error
= ip6_pcbopts(&in6p
->in6p_outputopts
,
2090 * Use of some Hop-by-Hop options or some
2091 * Destination options, might require special
2092 * privilege. That is, normal applications
2093 * (without special privilege) might be forbidden
2094 * from setting certain options in outgoing packets,
2095 * and might never see certain options in received
2096 * packets. [RFC 2292 Section 6]
2097 * KAME specific note:
2098 * KAME prevents non-privileged users from sending or
2099 * receiving ANY hbh/dst options in order to avoid
2100 * overhead of parsing options in the kernel.
2102 case IPV6_RECVHOPOPTS
:
2103 case IPV6_RECVDSTOPTS
:
2104 case IPV6_RECVRTHDRDSTOPTS
:
2108 case IPV6_UNICAST_HOPS
:
2110 case IPV6_RECVPKTINFO
:
2111 case IPV6_RECVHOPLIMIT
:
2112 case IPV6_RECVRTHDR
:
2113 case IPV6_RECVPATHMTU
:
2114 case IPV6_RECVTCLASS
:
2116 case IPV6_AUTOFLOWLABEL
:
2117 if (optlen
!= sizeof (int)) {
2121 error
= sooptcopyin(sopt
, &optval
,
2122 sizeof (optval
), sizeof (optval
));
2127 case IPV6_UNICAST_HOPS
:
2128 if (optval
< -1 || optval
>= 256) {
2131 /* -1 = kernel default */
2132 in6p
->in6p_hops
= optval
;
2133 if (in6p
->inp_vflag
&
2140 #define OPTSET(bit) do { \
2142 in6p->inp_flags |= (bit); \
2144 in6p->inp_flags &= ~(bit); \
2147 #define OPTSET2292(bit) do { \
2148 in6p->inp_flags |= IN6P_RFC2292; \
2150 in6p->inp_flags |= (bit); \
2152 in6p->inp_flags &= ~(bit); \
2155 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
2157 case IPV6_RECVPKTINFO
:
2158 /* cannot mix with RFC2292 */
2159 if (OPTBIT(IN6P_RFC2292
)) {
2163 OPTSET(IN6P_PKTINFO
);
2166 case IPV6_HOPLIMIT
: {
2167 struct ip6_pktopts
**optp
;
2169 /* cannot mix with RFC2292 */
2170 if (OPTBIT(IN6P_RFC2292
)) {
2174 optp
= &in6p
->in6p_outputopts
;
2175 error
= ip6_pcbopt(IPV6_HOPLIMIT
,
2176 (u_char
*)&optval
, sizeof (optval
),
2181 case IPV6_RECVHOPLIMIT
:
2182 /* cannot mix with RFC2292 */
2183 if (OPTBIT(IN6P_RFC2292
)) {
2187 OPTSET(IN6P_HOPLIMIT
);
2190 case IPV6_RECVHOPOPTS
:
2191 /* cannot mix with RFC2292 */
2192 if (OPTBIT(IN6P_RFC2292
)) {
2196 OPTSET(IN6P_HOPOPTS
);
2199 case IPV6_RECVDSTOPTS
:
2200 /* cannot mix with RFC2292 */
2201 if (OPTBIT(IN6P_RFC2292
)) {
2205 OPTSET(IN6P_DSTOPTS
);
2208 case IPV6_RECVRTHDRDSTOPTS
:
2209 /* cannot mix with RFC2292 */
2210 if (OPTBIT(IN6P_RFC2292
)) {
2214 OPTSET(IN6P_RTHDRDSTOPTS
);
2217 case IPV6_RECVRTHDR
:
2218 /* cannot mix with RFC2292 */
2219 if (OPTBIT(IN6P_RFC2292
)) {
2226 case IPV6_RECVPATHMTU
:
2228 * We ignore this option for TCP
2230 * (RFC3542 leaves this case
2233 if (uproto
!= IPPROTO_TCP
)
2239 * make setsockopt(IPV6_V6ONLY)
2240 * available only prior to bind(2).
2241 * see ipng mailing list, Jun 22 2001.
2243 if (in6p
->inp_lport
||
2244 !IN6_IS_ADDR_UNSPECIFIED(
2245 &in6p
->in6p_laddr
)) {
2249 OPTSET(IN6P_IPV6_V6ONLY
);
2251 in6p
->inp_vflag
&= ~INP_IPV4
;
2253 in6p
->inp_vflag
|= INP_IPV4
;
2256 case IPV6_RECVTCLASS
:
2257 /* we can mix with RFC2292 */
2258 OPTSET(IN6P_TCLASS
);
2261 case IPV6_AUTOFLOWLABEL
:
2262 OPTSET(IN6P_AUTOFLOWLABEL
);
2270 case IPV6_USE_MIN_MTU
:
2271 case IPV6_PREFER_TEMPADDR
: {
2272 struct ip6_pktopts
**optp
;
2274 if (optlen
!= sizeof (optval
)) {
2278 error
= sooptcopyin(sopt
, &optval
,
2279 sizeof (optval
), sizeof (optval
));
2283 optp
= &in6p
->in6p_outputopts
;
2284 error
= ip6_pcbopt(optname
, (u_char
*)&optval
,
2285 sizeof (optval
), optp
, uproto
);
2289 case IPV6_2292PKTINFO
:
2290 case IPV6_2292HOPLIMIT
:
2291 case IPV6_2292HOPOPTS
:
2292 case IPV6_2292DSTOPTS
:
2293 case IPV6_2292RTHDR
:
2295 if (optlen
!= sizeof (int)) {
2299 error
= sooptcopyin(sopt
, &optval
,
2300 sizeof (optval
), sizeof (optval
));
2304 case IPV6_2292PKTINFO
:
2305 OPTSET2292(IN6P_PKTINFO
);
2307 case IPV6_2292HOPLIMIT
:
2308 OPTSET2292(IN6P_HOPLIMIT
);
2310 case IPV6_2292HOPOPTS
:
2312 * Check super-user privilege.
2313 * See comments for IPV6_RECVHOPOPTS.
2317 OPTSET2292(IN6P_HOPOPTS
);
2319 case IPV6_2292DSTOPTS
:
2322 OPTSET2292(IN6P_DSTOPTS
|
2323 IN6P_RTHDRDSTOPTS
); /* XXX */
2325 case IPV6_2292RTHDR
:
2326 OPTSET2292(IN6P_RTHDR
);
2331 case IPV6_3542PKTINFO
:
2332 case IPV6_3542HOPOPTS
:
2333 case IPV6_3542RTHDR
:
2334 case IPV6_3542DSTOPTS
:
2335 case IPV6_RTHDRDSTOPTS
:
2336 case IPV6_3542NEXTHOP
: {
2337 struct ip6_pktopts
**optp
;
2338 /* new advanced API (RFC3542) */
2341 /* cannot mix with RFC2292 */
2342 if (OPTBIT(IN6P_RFC2292
)) {
2346 error
= soopt_getm(sopt
, &m
);
2349 error
= soopt_mcopyin(sopt
, m
);
2353 optp
= &in6p
->in6p_outputopts
;
2354 error
= ip6_pcbopt(optname
, mtod(m
, u_char
*),
2355 m
->m_len
, optp
, uproto
);
2360 case IPV6_MULTICAST_IF
:
2361 case IPV6_MULTICAST_HOPS
:
2362 case IPV6_MULTICAST_LOOP
:
2363 case IPV6_JOIN_GROUP
:
2364 case IPV6_LEAVE_GROUP
:
2366 case MCAST_BLOCK_SOURCE
:
2367 case MCAST_UNBLOCK_SOURCE
:
2368 case MCAST_JOIN_GROUP
:
2369 case MCAST_LEAVE_GROUP
:
2370 case MCAST_JOIN_SOURCE_GROUP
:
2371 case MCAST_LEAVE_SOURCE_GROUP
:
2372 error
= ip6_setmoptions(in6p
, sopt
);
2375 case IPV6_PORTRANGE
:
2376 error
= sooptcopyin(sopt
, &optval
,
2377 sizeof (optval
), sizeof (optval
));
2382 case IPV6_PORTRANGE_DEFAULT
:
2383 in6p
->inp_flags
&= ~(INP_LOWPORT
);
2384 in6p
->inp_flags
&= ~(INP_HIGHPORT
);
2387 case IPV6_PORTRANGE_HIGH
:
2388 in6p
->inp_flags
&= ~(INP_LOWPORT
);
2389 in6p
->inp_flags
|= INP_HIGHPORT
;
2392 case IPV6_PORTRANGE_LOW
:
2393 in6p
->inp_flags
&= ~(INP_HIGHPORT
);
2394 in6p
->inp_flags
|= INP_LOWPORT
;
2403 case IPV6_IPSEC_POLICY
: {
2408 if ((error
= soopt_getm(sopt
, &m
)) != 0)
2410 if ((error
= soopt_mcopyin(sopt
, m
)) != 0)
2413 req
= mtod(m
, caddr_t
);
2415 error
= ipsec6_set_policy(in6p
, optname
, req
,
2425 case IPV6_FW_ZERO
: {
2426 if (ip6_fw_ctl_ptr
== NULL
)
2428 if (ip6_fw_ctl_ptr
!= NULL
)
2429 error
= (*ip6_fw_ctl_ptr
)(sopt
);
2431 error
= ENOPROTOOPT
;
2434 #endif /* IPFIREWALL */
2436 * IPv6 variant of IP_BOUND_IF; for details see
2437 * comments on IP_BOUND_IF in ip_ctloutput().
2440 /* This option is settable only on IPv6 */
2441 if (!(in6p
->inp_vflag
& INP_IPV6
)) {
2446 error
= sooptcopyin(sopt
, &optval
,
2447 sizeof (optval
), sizeof (optval
));
2452 error
= inp_bindif(in6p
, optval
, NULL
);
2455 case IPV6_NO_IFT_CELLULAR
:
2456 /* This option is settable only for IPv6 */
2457 if (!(in6p
->inp_vflag
& INP_IPV6
)) {
2462 error
= sooptcopyin(sopt
, &optval
,
2463 sizeof (optval
), sizeof (optval
));
2468 /* once set, it cannot be unset */
2470 (in6p
->inp_flags
& INP_NO_IFT_CELLULAR
)) {
2475 error
= so_set_restrictions(so
,
2476 SO_RESTRICT_DENY_CELLULAR
);
2480 /* This option is not settable */
2485 error
= ENOPROTOOPT
;
2493 case IPV6_2292PKTOPTIONS
:
2495 * RFC3542 (effectively) deprecated the
2496 * semantics of the 2292-style pktoptions.
2497 * Since it was not reliable in nature (i.e.,
2498 * applications had to expect the lack of some
2499 * information after all), it would make sense
2500 * to simplify this part by always returning
2503 sopt
->sopt_valsize
= 0;
2506 case IPV6_RECVHOPOPTS
:
2507 case IPV6_RECVDSTOPTS
:
2508 case IPV6_RECVRTHDRDSTOPTS
:
2509 case IPV6_UNICAST_HOPS
:
2510 case IPV6_RECVPKTINFO
:
2511 case IPV6_RECVHOPLIMIT
:
2512 case IPV6_RECVRTHDR
:
2513 case IPV6_RECVPATHMTU
:
2515 case IPV6_PORTRANGE
:
2516 case IPV6_RECVTCLASS
:
2517 case IPV6_AUTOFLOWLABEL
:
2520 case IPV6_RECVHOPOPTS
:
2521 optval
= OPTBIT(IN6P_HOPOPTS
);
2524 case IPV6_RECVDSTOPTS
:
2525 optval
= OPTBIT(IN6P_DSTOPTS
);
2528 case IPV6_RECVRTHDRDSTOPTS
:
2529 optval
= OPTBIT(IN6P_RTHDRDSTOPTS
);
2532 case IPV6_UNICAST_HOPS
:
2533 optval
= in6p
->in6p_hops
;
2536 case IPV6_RECVPKTINFO
:
2537 optval
= OPTBIT(IN6P_PKTINFO
);
2540 case IPV6_RECVHOPLIMIT
:
2541 optval
= OPTBIT(IN6P_HOPLIMIT
);
2544 case IPV6_RECVRTHDR
:
2545 optval
= OPTBIT(IN6P_RTHDR
);
2548 case IPV6_RECVPATHMTU
:
2549 optval
= OPTBIT(IN6P_MTU
);
2553 optval
= OPTBIT(IN6P_IPV6_V6ONLY
);
2556 case IPV6_PORTRANGE
: {
2558 flags
= in6p
->inp_flags
;
2559 if (flags
& INP_HIGHPORT
)
2560 optval
= IPV6_PORTRANGE_HIGH
;
2561 else if (flags
& INP_LOWPORT
)
2562 optval
= IPV6_PORTRANGE_LOW
;
2567 case IPV6_RECVTCLASS
:
2568 optval
= OPTBIT(IN6P_TCLASS
);
2571 case IPV6_AUTOFLOWLABEL
:
2572 optval
= OPTBIT(IN6P_AUTOFLOWLABEL
);
2577 error
= sooptcopyout(sopt
, &optval
,
2581 case IPV6_PATHMTU
: {
2583 struct ip6_mtuinfo mtuinfo
;
2584 struct route_in6 sro
;
2586 bzero(&sro
, sizeof (sro
));
2588 if (!(so
->so_state
& SS_ISCONNECTED
))
2591 * XXX: we dot not consider the case of source
2592 * routing, or optional information to specify
2593 * the outgoing interface.
2595 error
= ip6_getpmtu(&sro
, NULL
, NULL
,
2596 &in6p
->in6p_faddr
, &pmtu
, NULL
);
2597 ROUTE_RELEASE(&sro
);
2600 if (pmtu
> IPV6_MAXPACKET
)
2601 pmtu
= IPV6_MAXPACKET
;
2603 bzero(&mtuinfo
, sizeof (mtuinfo
));
2604 mtuinfo
.ip6m_mtu
= (u_int32_t
)pmtu
;
2605 optdata
= (void *)&mtuinfo
;
2606 optdatalen
= sizeof (mtuinfo
);
2607 error
= sooptcopyout(sopt
, optdata
,
2612 case IPV6_2292PKTINFO
:
2613 case IPV6_2292HOPLIMIT
:
2614 case IPV6_2292HOPOPTS
:
2615 case IPV6_2292RTHDR
:
2616 case IPV6_2292DSTOPTS
:
2618 case IPV6_2292PKTINFO
:
2619 optval
= OPTBIT(IN6P_PKTINFO
);
2621 case IPV6_2292HOPLIMIT
:
2622 optval
= OPTBIT(IN6P_HOPLIMIT
);
2624 case IPV6_2292HOPOPTS
:
2625 optval
= OPTBIT(IN6P_HOPOPTS
);
2627 case IPV6_2292RTHDR
:
2628 optval
= OPTBIT(IN6P_RTHDR
);
2630 case IPV6_2292DSTOPTS
:
2631 optval
= OPTBIT(IN6P_DSTOPTS
|
2635 error
= sooptcopyout(sopt
, &optval
,
2643 case IPV6_RTHDRDSTOPTS
:
2647 case IPV6_USE_MIN_MTU
:
2648 case IPV6_PREFER_TEMPADDR
:
2649 error
= ip6_getpcbopt(in6p
->in6p_outputopts
,
2653 case IPV6_MULTICAST_IF
:
2654 case IPV6_MULTICAST_HOPS
:
2655 case IPV6_MULTICAST_LOOP
:
2657 error
= ip6_getmoptions(in6p
, sopt
);
2660 case IPV6_IPSEC_POLICY
: {
2663 struct mbuf
*m
= NULL
;
2664 struct mbuf
*mp
= NULL
;
2666 error
= soopt_getm(sopt
, &m
);
2669 error
= soopt_mcopyin(sopt
, m
);
2673 req
= mtod(m
, caddr_t
);
2675 error
= ipsec6_get_policy(in6p
, req
, len
, &mp
);
2677 error
= soopt_mcopyout(sopt
, mp
);
2686 if (ip6_fw_ctl_ptr
== NULL
)
2688 if (ip6_fw_ctl_ptr
!= NULL
)
2689 error
= (*ip6_fw_ctl_ptr
)(sopt
);
2691 error
= ENOPROTOOPT
;
2694 #endif /* IPFIREWALL */
2696 if (in6p
->inp_flags
& INP_BOUND_IF
)
2697 optval
= in6p
->inp_boundifp
->if_index
;
2698 error
= sooptcopyout(sopt
, &optval
,
2702 case IPV6_NO_IFT_CELLULAR
:
2703 optval
= (in6p
->inp_flags
& INP_NO_IFT_CELLULAR
)
2705 error
= sooptcopyout(sopt
, &optval
,
2710 optval
= (in6p
->in6p_last_outifp
!= NULL
) ?
2711 in6p
->in6p_last_outifp
->if_index
: 0;
2712 error
= sooptcopyout(sopt
, &optval
,
2717 error
= ENOPROTOOPT
;
2729 ip6_raw_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
2731 int error
= 0, optval
, optlen
;
2732 const int icmp6off
= offsetof(struct icmp6_hdr
, icmp6_cksum
);
2733 struct inpcb
*in6p
= sotoinpcb(so
);
2734 int level
, op
, optname
;
2736 level
= sopt
->sopt_level
;
2737 op
= sopt
->sopt_dir
;
2738 optname
= sopt
->sopt_name
;
2739 optlen
= sopt
->sopt_valsize
;
2741 if (level
!= IPPROTO_IPV6
)
2747 * For ICMPv6 sockets, no modification allowed for checksum
2748 * offset, permit "no change" values to help existing apps.
2750 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2751 * for an ICMPv6 socket will fail."
2752 * The current behavior does not meet RFC3542.
2756 if (optlen
!= sizeof (int)) {
2760 error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2764 if ((optval
% 2) != 0) {
2765 /* the API assumes even offset values */
2767 } else if (SOCK_PROTO(so
) == IPPROTO_ICMPV6
) {
2768 if (optval
!= icmp6off
)
2771 in6p
->in6p_cksum
= optval
;
2776 if (SOCK_PROTO(so
) == IPPROTO_ICMPV6
)
2779 optval
= in6p
->in6p_cksum
;
2781 error
= sooptcopyout(sopt
, &optval
, sizeof (optval
));
2791 error
= ENOPROTOOPT
;
2799 * Set up IP6 options in pcb for insertion in output packets or
2800 * specifying behavior of outgoing packets.
2803 ip6_pcbopts(struct ip6_pktopts
**pktopt
, struct mbuf
*m
, struct socket
*so
,
2804 struct sockopt
*sopt
)
2806 #pragma unused(sopt)
2807 struct ip6_pktopts
*opt
= *pktopt
;
2810 /* turn off any old options. */
2813 if (opt
->ip6po_pktinfo
|| opt
->ip6po_nexthop
||
2814 opt
->ip6po_hbh
|| opt
->ip6po_dest1
|| opt
->ip6po_dest2
||
2815 opt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
2816 printf("%s: all specified options are cleared.\n",
2819 ip6_clearpktopts(opt
, -1);
2821 opt
= _MALLOC(sizeof (*opt
), M_IP6OPT
, M_WAITOK
);
2827 if (m
== NULL
|| m
->m_len
== 0) {
2829 * Only turning off any previous options, regardless of
2830 * whether the opt is just created or given.
2833 FREE(opt
, M_IP6OPT
);
2837 /* set options specified by user. */
2838 if ((error
= ip6_setpktopts(m
, opt
, NULL
, SOCK_PROTO(so
))) != 0) {
2839 ip6_clearpktopts(opt
, -1); /* XXX: discard all options */
2840 FREE(opt
, M_IP6OPT
);
2848 * initialize ip6_pktopts. beware that there are non-zero default values in
2852 ip6_initpktopts(struct ip6_pktopts
*opt
)
2855 bzero(opt
, sizeof (*opt
));
2856 opt
->ip6po_hlim
= -1; /* -1 means default hop limit */
2857 opt
->ip6po_tclass
= -1; /* -1 means default traffic class */
2858 opt
->ip6po_minmtu
= IP6PO_MINMTU_MCASTONLY
;
2859 opt
->ip6po_prefer_tempaddr
= IP6PO_TEMPADDR_SYSTEM
;
2863 ip6_pcbopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
**pktopt
,
2866 struct ip6_pktopts
*opt
;
2870 opt
= _MALLOC(sizeof (*opt
), M_IP6OPT
, M_WAITOK
);
2873 ip6_initpktopts(opt
);
2877 return (ip6_setpktopt(optname
, buf
, len
, opt
, 1, 0, uproto
));
2881 ip6_getpcbopt(struct ip6_pktopts
*pktopt
, int optname
, struct sockopt
*sopt
)
2883 void *optdata
= NULL
;
2885 struct ip6_ext
*ip6e
;
2886 struct in6_pktinfo null_pktinfo
;
2887 int deftclass
= 0, on
;
2888 int defminmtu
= IP6PO_MINMTU_MCASTONLY
;
2889 int defpreftemp
= IP6PO_TEMPADDR_SYSTEM
;
2894 if (pktopt
&& pktopt
->ip6po_pktinfo
)
2895 optdata
= (void *)pktopt
->ip6po_pktinfo
;
2897 /* XXX: we don't have to do this every time... */
2898 bzero(&null_pktinfo
, sizeof (null_pktinfo
));
2899 optdata
= (void *)&null_pktinfo
;
2901 optdatalen
= sizeof (struct in6_pktinfo
);
2905 if (pktopt
&& pktopt
->ip6po_tclass
>= 0)
2906 optdata
= (void *)&pktopt
->ip6po_tclass
;
2908 optdata
= (void *)&deftclass
;
2909 optdatalen
= sizeof (int);
2913 if (pktopt
&& pktopt
->ip6po_hbh
) {
2914 optdata
= (void *)pktopt
->ip6po_hbh
;
2915 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_hbh
;
2916 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2921 if (pktopt
&& pktopt
->ip6po_rthdr
) {
2922 optdata
= (void *)pktopt
->ip6po_rthdr
;
2923 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_rthdr
;
2924 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2928 case IPV6_RTHDRDSTOPTS
:
2929 if (pktopt
&& pktopt
->ip6po_dest1
) {
2930 optdata
= (void *)pktopt
->ip6po_dest1
;
2931 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest1
;
2932 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2937 if (pktopt
&& pktopt
->ip6po_dest2
) {
2938 optdata
= (void *)pktopt
->ip6po_dest2
;
2939 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest2
;
2940 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2945 if (pktopt
&& pktopt
->ip6po_nexthop
) {
2946 optdata
= (void *)pktopt
->ip6po_nexthop
;
2947 optdatalen
= pktopt
->ip6po_nexthop
->sa_len
;
2951 case IPV6_USE_MIN_MTU
:
2953 optdata
= (void *)&pktopt
->ip6po_minmtu
;
2955 optdata
= (void *)&defminmtu
;
2956 optdatalen
= sizeof (int);
2960 if (pktopt
&& ((pktopt
->ip6po_flags
) & IP6PO_DONTFRAG
))
2964 optdata
= (void *)&on
;
2965 optdatalen
= sizeof (on
);
2968 case IPV6_PREFER_TEMPADDR
:
2970 optdata
= (void *)&pktopt
->ip6po_prefer_tempaddr
;
2972 optdata
= (void *)&defpreftemp
;
2973 optdatalen
= sizeof (int);
2976 default: /* should not happen */
2978 panic("ip6_getpcbopt: unexpected option\n");
2980 return (ENOPROTOOPT
);
2983 return (sooptcopyout(sopt
, optdata
, optdatalen
));
2987 ip6_clearpktopts(struct ip6_pktopts
*pktopt
, int optname
)
2992 if (optname
== -1 || optname
== IPV6_PKTINFO
) {
2993 if (pktopt
->ip6po_pktinfo
)
2994 FREE(pktopt
->ip6po_pktinfo
, M_IP6OPT
);
2995 pktopt
->ip6po_pktinfo
= NULL
;
2997 if (optname
== -1 || optname
== IPV6_HOPLIMIT
)
2998 pktopt
->ip6po_hlim
= -1;
2999 if (optname
== -1 || optname
== IPV6_TCLASS
)
3000 pktopt
->ip6po_tclass
= -1;
3001 if (optname
== -1 || optname
== IPV6_NEXTHOP
) {
3002 ROUTE_RELEASE(&pktopt
->ip6po_nextroute
);
3003 if (pktopt
->ip6po_nexthop
)
3004 FREE(pktopt
->ip6po_nexthop
, M_IP6OPT
);
3005 pktopt
->ip6po_nexthop
= NULL
;
3007 if (optname
== -1 || optname
== IPV6_HOPOPTS
) {
3008 if (pktopt
->ip6po_hbh
)
3009 FREE(pktopt
->ip6po_hbh
, M_IP6OPT
);
3010 pktopt
->ip6po_hbh
= NULL
;
3012 if (optname
== -1 || optname
== IPV6_RTHDRDSTOPTS
) {
3013 if (pktopt
->ip6po_dest1
)
3014 FREE(pktopt
->ip6po_dest1
, M_IP6OPT
);
3015 pktopt
->ip6po_dest1
= NULL
;
3017 if (optname
== -1 || optname
== IPV6_RTHDR
) {
3018 if (pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
3019 FREE(pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
, M_IP6OPT
);
3020 pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
= NULL
;
3021 ROUTE_RELEASE(&pktopt
->ip6po_route
);
3023 if (optname
== -1 || optname
== IPV6_DSTOPTS
) {
3024 if (pktopt
->ip6po_dest2
)
3025 FREE(pktopt
->ip6po_dest2
, M_IP6OPT
);
3026 pktopt
->ip6po_dest2
= NULL
;
3030 #define PKTOPT_EXTHDRCPY(type) do { \
3033 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3; \
3034 dst->type = _MALLOC(hlen, M_IP6OPT, canwait); \
3035 if (dst->type == NULL && canwait == M_NOWAIT) \
3037 bcopy(src->type, dst->type, hlen); \
3042 copypktopts(struct ip6_pktopts
*dst
, struct ip6_pktopts
*src
, int canwait
)
3044 if (dst
== NULL
|| src
== NULL
) {
3045 printf("copypktopts: invalid argument\n");
3049 dst
->ip6po_hlim
= src
->ip6po_hlim
;
3050 dst
->ip6po_tclass
= src
->ip6po_tclass
;
3051 dst
->ip6po_flags
= src
->ip6po_flags
;
3052 if (src
->ip6po_pktinfo
) {
3053 dst
->ip6po_pktinfo
= _MALLOC(sizeof (*dst
->ip6po_pktinfo
),
3055 if (dst
->ip6po_pktinfo
== NULL
&& canwait
== M_NOWAIT
)
3057 *dst
->ip6po_pktinfo
= *src
->ip6po_pktinfo
;
3059 if (src
->ip6po_nexthop
) {
3060 dst
->ip6po_nexthop
= _MALLOC(src
->ip6po_nexthop
->sa_len
,
3062 if (dst
->ip6po_nexthop
== NULL
&& canwait
== M_NOWAIT
)
3064 bcopy(src
->ip6po_nexthop
, dst
->ip6po_nexthop
,
3065 src
->ip6po_nexthop
->sa_len
);
3067 PKTOPT_EXTHDRCPY(ip6po_hbh
);
3068 PKTOPT_EXTHDRCPY(ip6po_dest1
);
3069 PKTOPT_EXTHDRCPY(ip6po_dest2
);
3070 PKTOPT_EXTHDRCPY(ip6po_rthdr
); /* not copy the cached route */
3074 ip6_clearpktopts(dst
, -1);
3077 #undef PKTOPT_EXTHDRCPY
3079 struct ip6_pktopts
*
3080 ip6_copypktopts(struct ip6_pktopts
*src
, int canwait
)
3083 struct ip6_pktopts
*dst
;
3085 dst
= _MALLOC(sizeof (*dst
), M_IP6OPT
, canwait
);
3088 ip6_initpktopts(dst
);
3090 if ((error
= copypktopts(dst
, src
, canwait
)) != 0) {
3091 FREE(dst
, M_IP6OPT
);
3099 ip6_freepcbopts(struct ip6_pktopts
*pktopt
)
3104 ip6_clearpktopts(pktopt
, -1);
3106 FREE(pktopt
, M_IP6OPT
);
3110 ip6_moptions_init(void)
3112 PE_parse_boot_argn("ifa_debug", &im6o_debug
, sizeof (im6o_debug
));
3114 im6o_size
= (im6o_debug
== 0) ? sizeof (struct ip6_moptions
) :
3115 sizeof (struct ip6_moptions_dbg
);
3117 im6o_zone
= zinit(im6o_size
, IM6O_ZONE_MAX
* im6o_size
, 0,
3119 if (im6o_zone
== NULL
) {
3120 panic("%s: failed allocating %s", __func__
, IM6O_ZONE_NAME
);
3123 zone_change(im6o_zone
, Z_EXPAND
, TRUE
);
3127 im6o_addref(struct ip6_moptions
*im6o
, int locked
)
3132 IM6O_LOCK_ASSERT_HELD(im6o
);
3134 if (++im6o
->im6o_refcnt
== 0) {
3135 panic("%s: im6o %p wraparound refcnt\n", __func__
, im6o
);
3137 } else if (im6o
->im6o_trace
!= NULL
) {
3138 (*im6o
->im6o_trace
)(im6o
, TRUE
);
3146 im6o_remref(struct ip6_moptions
*im6o
)
3151 if (im6o
->im6o_refcnt
== 0) {
3152 panic("%s: im6o %p negative refcnt", __func__
, im6o
);
3154 } else if (im6o
->im6o_trace
!= NULL
) {
3155 (*im6o
->im6o_trace
)(im6o
, FALSE
);
3158 --im6o
->im6o_refcnt
;
3159 if (im6o
->im6o_refcnt
> 0) {
3164 for (i
= 0; i
< im6o
->im6o_num_memberships
; ++i
) {
3165 struct in6_mfilter
*imf
;
3167 imf
= im6o
->im6o_mfilters
? &im6o
->im6o_mfilters
[i
] : NULL
;
3171 (void) in6_mc_leave(im6o
->im6o_membership
[i
], imf
);
3176 IN6M_REMREF(im6o
->im6o_membership
[i
]);
3177 im6o
->im6o_membership
[i
] = NULL
;
3179 im6o
->im6o_num_memberships
= 0;
3180 if (im6o
->im6o_mfilters
!= NULL
) {
3181 FREE(im6o
->im6o_mfilters
, M_IN6MFILTER
);
3182 im6o
->im6o_mfilters
= NULL
;
3184 if (im6o
->im6o_membership
!= NULL
) {
3185 FREE(im6o
->im6o_membership
, M_IP6MOPTS
);
3186 im6o
->im6o_membership
= NULL
;
3190 lck_mtx_destroy(&im6o
->im6o_lock
, ifa_mtx_grp
);
3192 if (!(im6o
->im6o_debug
& IFD_ALLOC
)) {
3193 panic("%s: im6o %p cannot be freed", __func__
, im6o
);
3196 zfree(im6o_zone
, im6o
);
3200 im6o_trace(struct ip6_moptions
*im6o
, int refhold
)
3202 struct ip6_moptions_dbg
*im6o_dbg
= (struct ip6_moptions_dbg
*)im6o
;
3207 if (!(im6o
->im6o_debug
& IFD_DEBUG
)) {
3208 panic("%s: im6o %p has no debug structure", __func__
, im6o
);
3212 cnt
= &im6o_dbg
->im6o_refhold_cnt
;
3213 tr
= im6o_dbg
->im6o_refhold
;
3215 cnt
= &im6o_dbg
->im6o_refrele_cnt
;
3216 tr
= im6o_dbg
->im6o_refrele
;
3219 idx
= atomic_add_16_ov(cnt
, 1) % IM6O_TRACE_HIST_SIZE
;
3220 ctrace_record(&tr
[idx
]);
3223 struct ip6_moptions
*
3224 ip6_allocmoptions(int how
)
3226 struct ip6_moptions
*im6o
;
3228 im6o
= (how
== M_WAITOK
) ?
3229 zalloc(im6o_zone
) : zalloc_noblock(im6o_zone
);
3231 bzero(im6o
, im6o_size
);
3232 lck_mtx_init(&im6o
->im6o_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
3233 im6o
->im6o_debug
|= IFD_ALLOC
;
3234 if (im6o_debug
!= 0) {
3235 im6o
->im6o_debug
|= IFD_DEBUG
;
3236 im6o
->im6o_trace
= im6o_trace
;
3245 * Set IPv6 outgoing packet options based on advanced API.
3248 ip6_setpktopts(struct mbuf
*control
, struct ip6_pktopts
*opt
,
3249 struct ip6_pktopts
*stickyopt
, int uproto
)
3251 struct cmsghdr
*cm
= NULL
;
3253 if (control
== NULL
|| opt
== NULL
)
3256 ip6_initpktopts(opt
);
3261 * If stickyopt is provided, make a local copy of the options
3262 * for this particular packet, then override them by ancillary
3264 * XXX: copypktopts() does not copy the cached route to a next
3265 * hop (if any). This is not very good in terms of efficiency,
3266 * but we can allow this since this option should be rarely
3269 if ((error
= copypktopts(opt
, stickyopt
, M_NOWAIT
)) != 0)
3274 * XXX: Currently, we assume all the optional information is stored
3277 if (control
->m_next
)
3280 if (control
->m_len
< CMSG_LEN(0))
3283 for (cm
= M_FIRST_CMSGHDR(control
); cm
!= NULL
;
3284 cm
= M_NXT_CMSGHDR(control
, cm
)) {
3287 if (cm
->cmsg_len
< sizeof (struct cmsghdr
) ||
3288 cm
->cmsg_len
> control
->m_len
)
3290 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
3293 error
= ip6_setpktopt(cm
->cmsg_type
, CMSG_DATA(cm
),
3294 cm
->cmsg_len
- CMSG_LEN(0), opt
, 0, 1, uproto
);
3302 * Set a particular packet option, as a sticky option or an ancillary data
3303 * item. "len" can be 0 only when it's a sticky option.
3304 * We have 4 cases of combination of "sticky" and "cmsg":
3305 * "sticky=0, cmsg=0": impossible
3306 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
3307 * "sticky=1, cmsg=0": RFC3542 socket option
3308 * "sticky=1, cmsg=1": RFC2292 socket option
3311 ip6_setpktopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
*opt
,
3312 int sticky
, int cmsg
, int uproto
)
3314 int minmtupolicy
, preftemp
;
3317 if (!sticky
&& !cmsg
) {
3319 printf("ip6_setpktopt: impossible case\n");
3325 * Caller must have ensured that the buffer is at least
3326 * aligned on 32-bit boundary.
3328 VERIFY(IS_P2ALIGNED(buf
, sizeof (u_int32_t
)));
3331 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
3332 * not be specified in the context of RFC3542. Conversely,
3333 * RFC3542 types should not be specified in the context of RFC2292.
3337 case IPV6_2292PKTINFO
:
3338 case IPV6_2292HOPLIMIT
:
3339 case IPV6_2292NEXTHOP
:
3340 case IPV6_2292HOPOPTS
:
3341 case IPV6_2292DSTOPTS
:
3342 case IPV6_2292RTHDR
:
3343 case IPV6_2292PKTOPTIONS
:
3344 return (ENOPROTOOPT
);
3347 if (sticky
&& cmsg
) {
3354 case IPV6_RTHDRDSTOPTS
:
3356 case IPV6_USE_MIN_MTU
:
3359 case IPV6_PREFER_TEMPADDR
: /* XXX: not an RFC3542 option */
3360 return (ENOPROTOOPT
);
3365 case IPV6_2292PKTINFO
:
3366 case IPV6_PKTINFO
: {
3367 struct ifnet
*ifp
= NULL
;
3368 struct in6_pktinfo
*pktinfo
;
3370 if (len
!= sizeof (struct in6_pktinfo
))
3373 pktinfo
= (struct in6_pktinfo
*)(void *)buf
;
3376 * An application can clear any sticky IPV6_PKTINFO option by
3377 * doing a "regular" setsockopt with ipi6_addr being
3378 * in6addr_any and ipi6_ifindex being zero.
3379 * [RFC 3542, Section 6]
3381 if (optname
== IPV6_PKTINFO
&& opt
->ip6po_pktinfo
&&
3382 pktinfo
->ipi6_ifindex
== 0 &&
3383 IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
3384 ip6_clearpktopts(opt
, optname
);
3388 if (uproto
== IPPROTO_TCP
&& optname
== IPV6_PKTINFO
&&
3389 sticky
&& !IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
3393 /* validate the interface index if specified. */
3394 ifnet_head_lock_shared();
3396 if (pktinfo
->ipi6_ifindex
> if_index
) {
3401 if (pktinfo
->ipi6_ifindex
) {
3402 ifp
= ifindex2ifnet
[pktinfo
->ipi6_ifindex
];
3412 * We store the address anyway, and let in6_selectsrc()
3413 * validate the specified address. This is because ipi6_addr
3414 * may not have enough information about its scope zone, and
3415 * we may need additional information (such as outgoing
3416 * interface or the scope zone of a destination address) to
3417 * disambiguate the scope.
3418 * XXX: the delay of the validation may confuse the
3419 * application when it is used as a sticky option.
3421 if (opt
->ip6po_pktinfo
== NULL
) {
3422 opt
->ip6po_pktinfo
= _MALLOC(sizeof (*pktinfo
),
3423 M_IP6OPT
, M_NOWAIT
);
3424 if (opt
->ip6po_pktinfo
== NULL
)
3427 bcopy(pktinfo
, opt
->ip6po_pktinfo
, sizeof (*pktinfo
));
3431 case IPV6_2292HOPLIMIT
:
3432 case IPV6_HOPLIMIT
: {
3436 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3437 * to simplify the ordering among hoplimit options.
3439 if (optname
== IPV6_HOPLIMIT
&& sticky
)
3440 return (ENOPROTOOPT
);
3442 if (len
!= sizeof (int))
3444 hlimp
= (int *)(void *)buf
;
3445 if (*hlimp
< -1 || *hlimp
> 255)
3448 opt
->ip6po_hlim
= *hlimp
;
3455 if (len
!= sizeof (int))
3457 tclass
= *(int *)(void *)buf
;
3458 if (tclass
< -1 || tclass
> 255)
3461 opt
->ip6po_tclass
= tclass
;
3465 case IPV6_2292NEXTHOP
:
3467 error
= suser(kauth_cred_get(), 0);
3471 if (len
== 0) { /* just remove the option */
3472 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3476 /* check if cmsg_len is large enough for sa_len */
3477 if (len
< sizeof (struct sockaddr
) || len
< *buf
)
3480 switch (SA(buf
)->sa_family
) {
3482 struct sockaddr_in6
*sa6
= SIN6(buf
);
3484 if (sa6
->sin6_len
!= sizeof (struct sockaddr_in6
))
3487 if (IN6_IS_ADDR_UNSPECIFIED(&sa6
->sin6_addr
) ||
3488 IN6_IS_ADDR_MULTICAST(&sa6
->sin6_addr
)) {
3491 if ((error
= sa6_embedscope(sa6
, ip6_use_defzone
))
3497 case AF_LINK
: /* should eventually be supported */
3499 return (EAFNOSUPPORT
);
3502 /* turn off the previous option, then set the new option. */
3503 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3504 opt
->ip6po_nexthop
= _MALLOC(*buf
, M_IP6OPT
, M_NOWAIT
);
3505 if (opt
->ip6po_nexthop
== NULL
)
3507 bcopy(buf
, opt
->ip6po_nexthop
, *buf
);
3510 case IPV6_2292HOPOPTS
:
3511 case IPV6_HOPOPTS
: {
3512 struct ip6_hbh
*hbh
;
3516 * XXX: We don't allow a non-privileged user to set ANY HbH
3517 * options, since per-option restriction has too much
3520 error
= suser(kauth_cred_get(), 0);
3525 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3526 break; /* just remove the option */
3529 /* message length validation */
3530 if (len
< sizeof (struct ip6_hbh
))
3532 hbh
= (struct ip6_hbh
*)(void *)buf
;
3533 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
3537 /* turn off the previous option, then set the new option. */
3538 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3539 opt
->ip6po_hbh
= _MALLOC(hbhlen
, M_IP6OPT
, M_NOWAIT
);
3540 if (opt
->ip6po_hbh
== NULL
)
3542 bcopy(hbh
, opt
->ip6po_hbh
, hbhlen
);
3547 case IPV6_2292DSTOPTS
:
3549 case IPV6_RTHDRDSTOPTS
: {
3550 struct ip6_dest
*dest
, **newdest
= NULL
;
3553 error
= suser(kauth_cred_get(), 0);
3558 ip6_clearpktopts(opt
, optname
);
3559 break; /* just remove the option */
3562 /* message length validation */
3563 if (len
< sizeof (struct ip6_dest
))
3565 dest
= (struct ip6_dest
*)(void *)buf
;
3566 destlen
= (dest
->ip6d_len
+ 1) << 3;
3571 * Determine the position that the destination options header
3572 * should be inserted; before or after the routing header.
3575 case IPV6_2292DSTOPTS
:
3577 * The old advacned API is ambiguous on this point.
3578 * Our approach is to determine the position based
3579 * according to the existence of a routing header.
3580 * Note, however, that this depends on the order of the
3581 * extension headers in the ancillary data; the 1st
3582 * part of the destination options header must appear
3583 * before the routing header in the ancillary data,
3585 * RFC3542 solved the ambiguity by introducing
3586 * separate ancillary data or option types.
3588 if (opt
->ip6po_rthdr
== NULL
)
3589 newdest
= &opt
->ip6po_dest1
;
3591 newdest
= &opt
->ip6po_dest2
;
3593 case IPV6_RTHDRDSTOPTS
:
3594 newdest
= &opt
->ip6po_dest1
;
3597 newdest
= &opt
->ip6po_dest2
;
3601 /* turn off the previous option, then set the new option. */
3602 ip6_clearpktopts(opt
, optname
);
3603 *newdest
= _MALLOC(destlen
, M_IP6OPT
, M_NOWAIT
);
3604 if (*newdest
== NULL
)
3606 bcopy(dest
, *newdest
, destlen
);
3610 case IPV6_2292RTHDR
:
3612 struct ip6_rthdr
*rth
;
3616 ip6_clearpktopts(opt
, IPV6_RTHDR
);
3617 break; /* just remove the option */
3620 /* message length validation */
3621 if (len
< sizeof (struct ip6_rthdr
))
3623 rth
= (struct ip6_rthdr
*)(void *)buf
;
3624 rthlen
= (rth
->ip6r_len
+ 1) << 3;
3628 switch (rth
->ip6r_type
) {
3629 case IPV6_RTHDR_TYPE_0
:
3630 if (rth
->ip6r_len
== 0) /* must contain one addr */
3632 if (rth
->ip6r_len
% 2) /* length must be even */
3634 if (rth
->ip6r_len
/ 2 != rth
->ip6r_segleft
)
3638 return (EINVAL
); /* not supported */
3641 /* turn off the previous option */
3642 ip6_clearpktopts(opt
, IPV6_RTHDR
);
3643 opt
->ip6po_rthdr
= _MALLOC(rthlen
, M_IP6OPT
, M_NOWAIT
);
3644 if (opt
->ip6po_rthdr
== NULL
)
3646 bcopy(rth
, opt
->ip6po_rthdr
, rthlen
);
3650 case IPV6_USE_MIN_MTU
:
3651 if (len
!= sizeof (int))
3653 minmtupolicy
= *(int *)(void *)buf
;
3654 if (minmtupolicy
!= IP6PO_MINMTU_MCASTONLY
&&
3655 minmtupolicy
!= IP6PO_MINMTU_DISABLE
&&
3656 minmtupolicy
!= IP6PO_MINMTU_ALL
) {
3659 opt
->ip6po_minmtu
= minmtupolicy
;
3663 if (len
!= sizeof (int))
3666 if (uproto
== IPPROTO_TCP
|| *(int *)(void *)buf
== 0) {
3668 * we ignore this option for TCP sockets.
3669 * (RFC3542 leaves this case unspecified.)
3671 opt
->ip6po_flags
&= ~IP6PO_DONTFRAG
;
3673 opt
->ip6po_flags
|= IP6PO_DONTFRAG
;
3677 case IPV6_PREFER_TEMPADDR
:
3678 if (len
!= sizeof (int))
3680 preftemp
= *(int *)(void *)buf
;
3681 if (preftemp
!= IP6PO_TEMPADDR_SYSTEM
&&
3682 preftemp
!= IP6PO_TEMPADDR_NOTPREFER
&&
3683 preftemp
!= IP6PO_TEMPADDR_PREFER
) {
3686 opt
->ip6po_prefer_tempaddr
= preftemp
;
3690 return (ENOPROTOOPT
);
3691 } /* end of switch */
3697 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3698 * packet to the input queue of a specified interface. Note that this
3699 * calls the output routine of the loopback "driver", but with an interface
3700 * pointer that might NOT be &loif -- easier than replicating that code here.
3703 ip6_mloopback(struct ifnet
*srcifp
, struct ifnet
*origifp
, struct mbuf
*m
,
3704 struct sockaddr_in6
*dst
, uint32_t optlen
, int32_t nxt0
)
3707 struct ip6_hdr
*ip6
;
3708 struct in6_addr src
;
3714 * Copy the packet header as it's needed for the checksum.
3715 * Make sure to deep-copy IPv6 header portion in case the data
3716 * is in an mbuf cluster, so that we can safely override the IPv6
3717 * header portion later.
3719 copym
= m_copym_mode(m
, 0, M_COPYALL
, M_DONTWAIT
, M_COPYM_COPY_HDR
);
3720 if (copym
!= NULL
&& ((copym
->m_flags
& M_EXT
) ||
3721 copym
->m_len
< sizeof (struct ip6_hdr
)))
3722 copym
= m_pullup(copym
, sizeof (struct ip6_hdr
));
3727 ip6
= mtod(copym
, struct ip6_hdr
*);
3730 * clear embedded scope identifiers if necessary.
3731 * in6_clearscope will touch the addresses only when necessary.
3733 in6_clearscope(&ip6
->ip6_src
);
3734 in6_clearscope(&ip6
->ip6_dst
);
3736 if (copym
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
)
3737 in6_delayed_cksum_offset(copym
, 0, optlen
, nxt0
);
3740 * Stuff the 'real' ifp into the pkthdr, to be used in matching
3741 * in ip6_input(); we need the loopback ifp/dl_tag passed as args
3742 * to make the loopback driver compliant with the data link
3745 copym
->m_pkthdr
.rcvif
= origifp
;
3748 * Also record the source interface (which owns the source address).
3749 * This is basically a stripped down version of ifa_foraddr6().
3751 if (srcifp
== NULL
) {
3752 struct in6_ifaddr
*ia
;
3754 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
3755 for (ia
= in6_ifaddrs
; ia
!= NULL
; ia
= ia
->ia_next
) {
3756 IFA_LOCK_SPIN(&ia
->ia_ifa
);
3757 /* compare against src addr with embedded scope */
3758 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
, &src
)) {
3759 srcifp
= ia
->ia_ifp
;
3760 IFA_UNLOCK(&ia
->ia_ifa
);
3763 IFA_UNLOCK(&ia
->ia_ifa
);
3765 lck_rw_done(&in6_ifaddr_rwlock
);
3768 ip6_setsrcifaddr_info(copym
, srcifp
->if_index
, NULL
);
3769 ip6_setdstifaddr_info(copym
, origifp
->if_index
, NULL
);
3771 dlil_output(lo_ifp
, PF_INET6
, copym
, NULL
, SA(dst
), 0, NULL
);
3775 * Chop IPv6 header off from the payload.
3778 ip6_splithdr(struct mbuf
*m
, struct ip6_exthdrs
*exthdrs
)
3781 struct ip6_hdr
*ip6
;
3783 ip6
= mtod(m
, struct ip6_hdr
*);
3784 if (m
->m_len
> sizeof (*ip6
)) {
3785 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
3790 M_COPY_PKTHDR(mh
, m
);
3791 MH_ALIGN(mh
, sizeof (*ip6
));
3792 m
->m_flags
&= ~M_PKTHDR
;
3793 m
->m_len
-= sizeof (*ip6
);
3794 m
->m_data
+= sizeof (*ip6
);
3797 m
->m_len
= sizeof (*ip6
);
3798 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), sizeof (*ip6
));
3800 exthdrs
->ip6e_ip6
= m
;
3805 ip6_output_checksum(struct ifnet
*ifp
, uint32_t mtu
, struct mbuf
*m
,
3806 int nxt0
, uint32_t tlen
, uint32_t optlen
)
3808 uint32_t sw_csum
, hwcap
= ifp
->if_hwassist
;
3809 int tso
= TSO_IPV6_OK(ifp
, m
);
3812 /* do all in software; checksum offload is disabled */
3813 sw_csum
= CSUM_DELAY_IPV6_DATA
& m
->m_pkthdr
.csum_flags
;
3815 /* do in software what the hardware cannot */
3816 sw_csum
= m
->m_pkthdr
.csum_flags
&
3817 ~IF_HWASSIST_CSUM_FLAGS(hwcap
);
3821 sw_csum
|= (CSUM_DELAY_IPV6_DATA
&
3822 m
->m_pkthdr
.csum_flags
);
3823 } else if (!(sw_csum
& CSUM_DELAY_IPV6_DATA
) &&
3824 (hwcap
& CSUM_PARTIAL
)) {
3826 * Partial checksum offload, ere), if no extension
3827 * headers, and TCP only (no UDP support, as the
3828 * hardware may not be able to convert +0 to
3829 * -0 (0xffff) per RFC1122 4.1.3.4.)
3831 if (hwcksum_tx
&& !tso
&&
3832 (m
->m_pkthdr
.csum_flags
& CSUM_TCPIPV6
) &&
3834 uint16_t start
= sizeof (struct ip6_hdr
);
3836 m
->m_pkthdr
.csum_data
& 0xffff;
3837 m
->m_pkthdr
.csum_flags
|=
3838 (CSUM_DATA_VALID
| CSUM_PARTIAL
);
3839 m
->m_pkthdr
.csum_tx_stuff
= (ulpoff
+ start
);
3840 m
->m_pkthdr
.csum_tx_start
= start
;
3843 sw_csum
|= (CSUM_DELAY_IPV6_DATA
&
3844 m
->m_pkthdr
.csum_flags
);
3848 if (sw_csum
& CSUM_DELAY_IPV6_DATA
) {
3849 in6_delayed_cksum_offset(m
, 0, optlen
, nxt0
);
3850 sw_csum
&= ~CSUM_DELAY_IPV6_DATA
;
3855 * Drop off bits that aren't supported by hardware;
3856 * also make sure to preserve non-checksum related bits.
3858 m
->m_pkthdr
.csum_flags
=
3859 ((m
->m_pkthdr
.csum_flags
&
3860 (IF_HWASSIST_CSUM_FLAGS(hwcap
) | CSUM_DATA_VALID
)) |
3861 (m
->m_pkthdr
.csum_flags
& ~IF_HWASSIST_CSUM_MASK
));
3863 /* drop all bits; checksum offload is disabled */
3864 m
->m_pkthdr
.csum_flags
= 0;
3869 * Compute IPv6 extension header length.
3872 ip6_optlen(struct in6pcb
*in6p
)
3876 if (!in6p
->in6p_outputopts
)
3881 (((struct ip6_ext *)(x)) ? \
3882 (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3884 len
+= elen(in6p
->in6p_outputopts
->ip6po_hbh
);
3885 if (in6p
->in6p_outputopts
->ip6po_rthdr
) {
3886 /* dest1 is valid with rthdr only */
3887 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest1
);
3889 len
+= elen(in6p
->in6p_outputopts
->ip6po_rthdr
);
3890 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest2
);