2 * Copyright (c) 2000-2011 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@
29 /* $FreeBSD: src/sys/netinet6/ip6_output.c,v 1.43 2002/10/31 19:45:48 ume Exp $ */
30 /* $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $ */
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * Copyright (c) 1982, 1986, 1988, 1990, 1993
63 * The Regents of the University of California. All rights reserved.
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
73 * 3. All advertising materials mentioning features or use of this software
74 * must display the following acknowledgement:
75 * This product includes software developed by the University of
76 * California, Berkeley and its contributors.
77 * 4. Neither the name of the University nor the names of its contributors
78 * may be used to endorse or promote products derived from this software
79 * without specific prior written permission.
81 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
93 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
96 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
97 * support for mandatory and extensible security protections. This notice
98 * is included in support of clause 2.2 (b) of the Apple Public License,
102 #include <sys/param.h>
103 #include <sys/malloc.h>
104 #include <sys/mbuf.h>
105 #include <sys/errno.h>
106 #include <sys/protosw.h>
107 #include <sys/socket.h>
108 #include <sys/socketvar.h>
109 #include <sys/systm.h>
110 #include <sys/kernel.h>
111 #include <sys/proc.h>
112 #include <sys/kauth.h>
113 #include <sys/mcache.h>
114 #include <sys/sysctl.h>
115 #include <kern/zalloc.h>
117 #include <pexpert/pexpert.h>
120 #include <net/route.h>
121 #include <net/dlil.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 <netinet6/ip6protosw.h>
129 #include <netinet/icmp6.h>
130 #include <netinet6/ip6_var.h>
131 #include <netinet/in_pcb.h>
132 #include <netinet6/nd6.h>
133 #include <netinet6/scope6_var.h>
134 #include <mach/sdt.h>
137 #include <netinet6/ipsec.h>
139 #include <netinet6/ipsec6.h>
141 #include <netkey/key.h>
142 extern int ipsec_bypass
;
146 #include <security/mac.h>
149 #include <netinet6/ip6_fw.h>
151 #include <net/net_osdep.h>
153 #include <netinet/kpi_ipfilter_var.h>
156 #include <net/pfvar.h>
160 static MALLOC_DEFINE(M_IPMOPTS
, "ip6_moptions", "internet multicast options");
164 struct mbuf
*ip6e_ip6
;
165 struct mbuf
*ip6e_hbh
;
166 struct mbuf
*ip6e_dest1
;
167 struct mbuf
*ip6e_rthdr
;
168 struct mbuf
*ip6e_dest2
;
171 int ip6_raw_ctloutput(struct socket
*so
, struct sockopt
*sopt
);
172 static int ip6_pcbopts(struct ip6_pktopts
**, struct mbuf
*,
173 struct socket
*, struct sockopt
*sopt
);
174 static int ip6_pcbopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
**pktopt
, int uproto
);
175 static int ip6_getpcbopt(struct ip6_pktopts
*pktopt
, int optname
, struct sockopt
*sopt
);
176 static int ip6_setpktopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
*opt
, int sticky
, int cmsg
, int uproto
);
177 static void im6o_trace(struct ip6_moptions
*, int);
178 static int ip6_copyexthdr(struct mbuf
**, caddr_t
, int);
179 static int ip6_insertfraghdr(struct mbuf
*, struct mbuf
*, int,
181 static int ip6_insert_jumboopt(struct ip6_exthdrs
*, u_int32_t
);
182 static int ip6_splithdr(struct mbuf
*, struct ip6_exthdrs
*);
183 static int ip6_getpmtu (struct route_in6
*, struct route_in6
*,
184 struct ifnet
*, struct in6_addr
*, u_int32_t
*, int *);
186 #define IM6O_TRACE_HIST_SIZE 32 /* size of trace history */
189 __private_extern__
unsigned int im6o_trace_hist_size
= IM6O_TRACE_HIST_SIZE
;
191 struct ip6_moptions_dbg
{
192 struct ip6_moptions im6o
; /* ip6_moptions */
193 u_int16_t im6o_refhold_cnt
; /* # of IM6O_ADDREF */
194 u_int16_t im6o_refrele_cnt
; /* # of IM6O_REMREF */
196 * Alloc and free callers.
201 * Circular lists of IM6O_ADDREF and IM6O_REMREF callers.
203 ctrace_t im6o_refhold
[IM6O_TRACE_HIST_SIZE
];
204 ctrace_t im6o_refrele
[IM6O_TRACE_HIST_SIZE
];
208 static unsigned int im6o_debug
= 1; /* debugging (enabled) */
210 static unsigned int im6o_debug
; /* debugging (disabled) */
213 static unsigned int im6o_size
; /* size of zone element */
214 static struct zone
*im6o_zone
; /* zone for ip6_moptions */
216 #define IM6O_ZONE_MAX 64 /* maximum elements in zone */
217 #define IM6O_ZONE_NAME "ip6_moptions" /* zone name */
221 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
222 * header (with pri, len, nxt, hlim, src, dst).
223 * This function may modify ver and hlim only.
224 * The mbuf chain containing the packet will be freed.
225 * The mbuf opt, if present, will not be freed.
227 * type of "mtu": rt_rmx.rmx_mtu is u_int32_t, ifnet.ifr_mtu is int, and
228 * nd_ifinfo.linkmtu is u_int32_t. so we use u_int32_t to hold largest one,
229 * which is rt_rmx.rmx_mtu.
234 struct ip6_pktopts
*opt
,
235 struct route_in6
*ro
,
237 struct ip6_moptions
*im6o
,
238 struct ifnet
**ifpp
, /* XXX: just for statistics */
239 struct ip6_out_args
*ip6oa
)
241 struct ip6_hdr
*ip6
, *mhip6
;
242 struct ifnet
*ifp
= NULL
, *origifp
= NULL
;
244 int hlen
, tlen
, len
, off
;
245 struct route_in6 ip6route
;
246 struct rtentry
*rt
= NULL
;
247 struct sockaddr_in6
*dst
, src_sa
, dst_sa
;
249 struct in6_ifaddr
*ia
= NULL
;
251 int alwaysfrag
= 0, dontfrag
= 0;
252 u_int32_t optlen
= 0, plen
= 0, unfragpartlen
= 0;
253 struct ip6_exthdrs exthdrs
;
254 struct in6_addr finaldst
, src0
, dst0
;
256 struct route_in6
*ro_pmtu
= NULL
;
259 ipfilter_t inject_filter_ref
;
261 unsigned int ifscope
;
263 boolean_t select_srcif
;
264 struct ipf_pktopts
*ippo
= NULL
, ipf_pktopts
;
268 int needipsectun
= 0;
269 struct socket
*so
= NULL
;
270 struct secpolicy
*sp
= NULL
;
271 struct route_in6
*ipsec_saved_route
= NULL
;
272 struct ipsec_output_state ipsec_state
;
274 bzero(&ipsec_state
, sizeof(ipsec_state
));
276 /* for AH processing. stupid to have "socket" variable in IP layer... */
277 if (ipsec_bypass
== 0)
279 so
= ipsec_getsocket(m
);
280 (void)ipsec_setsocket(m
, NULL
);
284 bzero(&ipf_pktopts
, sizeof(struct ipf_pktopts
));
287 ip6
= mtod(m
, struct ip6_hdr
*);
288 inject_filter_ref
= ipf_get_inject_filter(m
);
290 finaldst
= ip6
->ip6_dst
;
292 if (ip6_doscopedroute
&& (flags
& IPV6_OUTARGS
)) {
293 select_srcif
= !(flags
& (IPV6_FORWARDING
| IPV6_UNSPECSRC
| IPV6_FLAG_NOSRCIFSEL
));
294 ifscope
= ip6oa
->ip6oa_boundif
;
295 ipf_pktopts
.ippo_flags
= IPPOF_BOUND_IF
;
296 ipf_pktopts
.ippo_flags
|= (ifscope
<< IPPOF_SHIFT_IFSCOPE
);
298 select_srcif
= FALSE
;
299 ifscope
= IFSCOPE_NONE
;
302 if (flags
& IPV6_OUTARGS
) {
303 nocell
= ip6oa
->ip6oa_nocell
;
305 ipf_pktopts
.ippo_flags
|= IPPOF_NO_IFT_CELLULAR
;
310 #define MAKE_EXTHDR(hp, mp) \
313 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
314 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
315 ((eh)->ip6e_len + 1) << 3); \
321 bzero(&exthdrs
, sizeof(exthdrs
));
324 /* Hop-by-Hop options header */
325 MAKE_EXTHDR(opt
->ip6po_hbh
, &exthdrs
.ip6e_hbh
);
326 /* Destination options header(1st part) */
327 if (opt
->ip6po_rthdr
) {
329 * Destination options header(1st part)
330 * This only makes sense with a routing header.
331 * See Section 9.2 of RFC 3542.
332 * Disabling this part just for MIP6 convenience is
333 * a bad idea. We need to think carefully about a
334 * way to make the advanced API coexist with MIP6
335 * options, which might automatically be inserted in
338 MAKE_EXTHDR(opt
->ip6po_dest1
, &exthdrs
.ip6e_dest1
);
341 MAKE_EXTHDR(opt
->ip6po_rthdr
, &exthdrs
.ip6e_rthdr
);
342 /* Destination options header(2nd part) */
343 MAKE_EXTHDR(opt
->ip6po_dest2
, &exthdrs
.ip6e_dest2
);
347 if (ipsec_bypass
!= 0)
350 /* get a security policy for this packet */
352 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, 0, &error
);
354 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, so
, &error
);
357 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
364 switch (sp
->policy
) {
365 case IPSEC_POLICY_DISCARD
:
366 case IPSEC_POLICY_GENERATE
:
368 * This packet is just discarded.
370 IPSEC_STAT_INCREMENT(ipsec6stat
.out_polvio
);
373 case IPSEC_POLICY_BYPASS
:
374 case IPSEC_POLICY_NONE
:
375 /* no need to do IPsec. */
379 case IPSEC_POLICY_IPSEC
:
380 if (sp
->req
== NULL
) {
381 /* acquire a policy */
382 error
= key_spdacquire(sp
);
388 case IPSEC_POLICY_ENTRUST
:
390 printf("ip6_output: Invalid policy found. %d\n", sp
->policy
);
396 * Calculate the total length of the extension header chain.
397 * Keep the length of the unfragmentable part for fragmentation.
400 if (exthdrs
.ip6e_hbh
)
401 optlen
+= exthdrs
.ip6e_hbh
->m_len
;
402 if (exthdrs
.ip6e_dest1
)
403 optlen
+= exthdrs
.ip6e_dest1
->m_len
;
404 if (exthdrs
.ip6e_rthdr
)
405 optlen
+= exthdrs
.ip6e_rthdr
->m_len
;
406 unfragpartlen
= optlen
+ sizeof(struct ip6_hdr
);
408 /* NOTE: we don't add AH/ESP length here. do that later. */
409 if (exthdrs
.ip6e_dest2
)
410 optlen
+= exthdrs
.ip6e_dest2
->m_len
;
414 (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
) != 0) {
415 in6_delayed_cksum(m
, sizeof(struct ip6_hdr
) + optlen
);
416 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_IPV6_DATA
;
420 * If we need IPsec, or there is at least one extension header,
421 * separate IP6 header from the payload.
423 if ((needipsec
|| optlen
) && !hdrsplit
) {
424 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
428 m
= exthdrs
.ip6e_ip6
;
433 ip6
= mtod(m
, struct ip6_hdr
*);
435 /* adjust mbuf packet header length */
436 m
->m_pkthdr
.len
+= optlen
;
437 plen
= m
->m_pkthdr
.len
- sizeof(*ip6
);
439 /* If this is a jumbo payload, insert a jumbo payload option. */
440 if (plen
> IPV6_MAXPACKET
) {
442 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
446 m
= exthdrs
.ip6e_ip6
;
450 ip6
= mtod(m
, struct ip6_hdr
*);
451 if ((error
= ip6_insert_jumboopt(&exthdrs
, plen
)) != 0)
455 ip6
->ip6_plen
= htons(plen
);
458 * Concatenate headers and fill in next header fields.
459 * Here we have, on "m"
461 * and we insert headers accordingly. Finally, we should be getting:
462 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
464 * during the header composing process, "m" points to IPv6 header.
465 * "mprev" points to an extension header prior to esp.
468 u_char
*nexthdrp
= &ip6
->ip6_nxt
;
469 struct mbuf
*mprev
= m
;
472 * we treat dest2 specially. this makes IPsec processing
473 * much easier. the goal here is to make mprev point the
474 * mbuf prior to dest2.
476 * result: IPv6 dest2 payload
477 * m and mprev will point to IPv6 header.
479 if (exthdrs
.ip6e_dest2
) {
481 panic("assumption failed: hdr not split");
482 exthdrs
.ip6e_dest2
->m_next
= m
->m_next
;
483 m
->m_next
= exthdrs
.ip6e_dest2
;
484 *mtod(exthdrs
.ip6e_dest2
, u_char
*) = ip6
->ip6_nxt
;
485 ip6
->ip6_nxt
= IPPROTO_DSTOPTS
;
488 #define MAKE_CHAIN(m, mp, p, i)\
492 panic("assumption failed: hdr not split"); \
493 *mtod((m), u_char *) = *(p);\
495 p = mtod((m), u_char *);\
496 (m)->m_next = (mp)->m_next;\
502 * result: IPv6 hbh dest1 rthdr dest2 payload
503 * m will point to IPv6 header. mprev will point to the
504 * extension header prior to dest2 (rthdr in the above case).
506 MAKE_CHAIN(exthdrs
.ip6e_hbh
, mprev
,
507 nexthdrp
, IPPROTO_HOPOPTS
);
508 MAKE_CHAIN(exthdrs
.ip6e_dest1
, mprev
,
509 nexthdrp
, IPPROTO_DSTOPTS
);
510 MAKE_CHAIN(exthdrs
.ip6e_rthdr
, mprev
,
511 nexthdrp
, IPPROTO_ROUTING
);
513 if (!TAILQ_EMPTY(&ipv6_filters
)) {
514 struct ipfilter
*filter
;
515 int seen
= (inject_filter_ref
== 0);
518 if (im6o
!= NULL
&& IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
519 ippo
->ippo_flags
|= IPPOF_MCAST_OPTS
;
521 ippo
->ippo_mcast_ifnet
= im6o
->im6o_multicast_ifp
;
522 ippo
->ippo_mcast_ttl
= im6o
->im6o_multicast_hlim
;
523 ippo
->ippo_mcast_loop
= im6o
->im6o_multicast_loop
;
527 /* Hack: embed the scope_id in the destination */
528 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
) &&
529 (ip6
->ip6_dst
.s6_addr16
[1] == 0) && (ro
!= NULL
)) {
531 ip6
->ip6_dst
.s6_addr16
[1] = htons(ro
->ro_dst
.sin6_scope_id
);
535 TAILQ_FOREACH(filter
, &ipv6_filters
, ipf_link
) {
537 * No need to proccess packet twice if we've
541 if ((struct ipfilter
*)inject_filter_ref
== filter
)
543 } else if (filter
->ipf_filter
.ipf_output
) {
546 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, ippo
);
547 if (result
== EJUSTRETURN
) {
559 ip6
= mtod(m
, struct ip6_hdr
*);
560 /* Hack: cleanup embedded scope_id if we put it there */
562 ip6
->ip6_dst
.s6_addr16
[1] = 0;
570 * pointers after IPsec headers are not valid any more.
571 * other pointers need a great care too.
572 * (IPsec routines should not mangle mbufs prior to AH/ESP)
574 exthdrs
.ip6e_dest2
= NULL
;
577 struct ip6_rthdr
*rh
= NULL
;
580 if (exthdrs
.ip6e_rthdr
) {
581 rh
= mtod(exthdrs
.ip6e_rthdr
, struct ip6_rthdr
*);
582 segleft_org
= rh
->ip6r_segleft
;
583 rh
->ip6r_segleft
= 0;
587 error
= ipsec6_output_trans(&ipsec_state
, nexthdrp
, mprev
, sp
, flags
,
591 /* mbuf is already reclaimed in ipsec6_output_trans. */
601 printf("ip6_output (ipsec): error code %d\n", error
);
604 /* don't show these error codes to the user */
610 if (exthdrs
.ip6e_rthdr
) {
611 /* ah6_output doesn't modify mbuf chain */
612 rh
->ip6r_segleft
= segleft_org
;
620 * If there is a routing header, replace the destination address field
621 * with the first hop of the routing header.
623 if (exthdrs
.ip6e_rthdr
) {
624 struct ip6_rthdr
*rh
=
625 (struct ip6_rthdr
*)(mtod(exthdrs
.ip6e_rthdr
,
626 struct ip6_rthdr
*));
627 struct ip6_rthdr0
*rh0
;
628 struct in6_addr
*addr
;
629 struct sockaddr_in6 sa
;
631 switch (rh
->ip6r_type
) {
632 case IPV6_RTHDR_TYPE_0
:
633 rh0
= (struct ip6_rthdr0
*)rh
;
634 addr
= (struct in6_addr
*)(rh0
+ 1);
637 * construct a sockaddr_in6 form of
640 * XXX: we may not have enough
641 * information about its scope zone;
642 * there is no standard API to pass
643 * the information from the
646 bzero(&sa
, sizeof(sa
));
647 sa
.sin6_family
= AF_INET6
;
648 sa
.sin6_len
= sizeof(sa
);
649 sa
.sin6_addr
= addr
[0];
650 if ((error
= sa6_embedscope(&sa
,
651 ip6_use_defzone
)) != 0) {
654 ip6
->ip6_dst
= sa
.sin6_addr
;
655 bcopy(&addr
[1], &addr
[0], sizeof(struct in6_addr
)
656 * (rh0
->ip6r0_segleft
- 1));
657 addr
[rh0
->ip6r0_segleft
- 1] = finaldst
;
659 in6_clearscope(addr
+ rh0
->ip6r0_segleft
- 1);
661 default: /* is it possible? */
667 /* Source address validation */
668 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) &&
669 (flags
& IPV6_UNSPECSRC
) == 0) {
671 ip6stat
.ip6s_badscope
++;
674 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
)) {
676 ip6stat
.ip6s_badscope
++;
680 ip6stat
.ip6s_localout
++;
687 bzero((caddr_t
)ro
, sizeof(*ro
));
690 if (opt
&& opt
->ip6po_rthdr
)
691 ro
= &opt
->ip6po_route
;
692 dst
= (struct sockaddr_in6
*)&ro
->ro_dst
;
695 RT_LOCK_ASSERT_NOTHELD(ro
->ro_rt
);
697 * if specified, try to fill in the traffic class field.
698 * do not override if a non-zero value is already set.
699 * we check the diffserv field and the ecn field separately.
701 if (opt
&& opt
->ip6po_tclass
>= 0) {
704 if ((ip6
->ip6_flow
& htonl(0xfc << 20)) == 0)
706 if ((ip6
->ip6_flow
& htonl(0x03 << 20)) == 0)
709 ip6
->ip6_flow
|= htonl((opt
->ip6po_tclass
& mask
) << 20);
712 /* fill in or override the hop limit field, if necessary. */
713 if (opt
&& opt
->ip6po_hlim
!= -1)
714 ip6
->ip6_hlim
= opt
->ip6po_hlim
& 0xff;
715 else if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
718 ip6
->ip6_hlim
= im6o
->im6o_multicast_hlim
;
721 ip6
->ip6_hlim
= ip6_defmcasthlim
;
726 * If there is a cached route, check that it is to the same
727 * destination and is still up. If not, free it and try again.
728 * Test rt_flags without holding rt_lock for performance reasons;
729 * if the route is down it will hopefully be caught by the layer
730 * below (since it uses this route as a hint) or during the
733 if (ro
->ro_rt
!= NULL
&& (!(ro
->ro_rt
->rt_flags
& RTF_UP
) ||
734 dst
->sin6_family
!= AF_INET6
||
735 !IN6_ARE_ADDR_EQUAL(&dst
->sin6_addr
, &ip6
->ip6_dst
) ||
736 ro
->ro_rt
->generation_id
!= route_generation
)) {
740 if (ro
->ro_rt
== NULL
) {
741 bzero(dst
, sizeof(*dst
));
742 dst
->sin6_family
= AF_INET6
;
743 dst
->sin6_len
= sizeof(struct sockaddr_in6
);
744 dst
->sin6_addr
= ip6
->ip6_dst
;
748 if (needipsec
&& needipsectun
) {
750 struct ifnet
*trace_ifp
= (ifpp
!= NULL
) ? (*ifpp
) : NULL
;
751 #endif /* CONFIG_DTRACE */
753 * All the extension headers will become inaccessible
754 * (since they can be encrypted).
755 * Don't panic, we need no more updates to extension headers
756 * on inner IPv6 packet (since they are now encapsulated).
758 * IPv6 [ESP|AH] IPv6 [extension headers] payload
760 bzero(&exthdrs
, sizeof(exthdrs
));
761 exthdrs
.ip6e_ip6
= m
;
764 route_copyout(&ipsec_state
.ro
, (struct route
*)ro
, sizeof(ipsec_state
.ro
));
765 ipsec_state
.dst
= (struct sockaddr
*)dst
;
767 /* Added a trace here so that we can see packets inside a tunnel */
768 DTRACE_IP6(send
, struct mbuf
*, m
, struct inpcb
*, NULL
,
769 struct ip6_hdr
*, ip6
, struct ifnet
*, trace_ifp
,
770 struct ip
*, NULL
, struct ip6_hdr
*, ip6
);
772 error
= ipsec6_output_tunnel(&ipsec_state
, sp
, flags
);
773 if (ipsec_state
.tunneled
== 4) /* tunneled in IPv4 - packet is gone */
776 ipsec_saved_route
= ro
;
777 ro
= (struct route_in6
*)&ipsec_state
.ro
;
778 dst
= (struct sockaddr_in6
*)ipsec_state
.dst
;
780 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
791 printf("ip6_output (ipsec): error code %d\n", error
);
794 /* don't show these error codes to the user */
801 * The packet has been encapsulated so the ifscope is no longer valid
802 * since it does not apply to the outer address: ignore the ifscope.
804 ifscope
= IFSCOPE_NONE
;
805 if (opt
!= NULL
&& opt
->ip6po_pktinfo
!= NULL
) {
806 if (opt
->ip6po_pktinfo
->ipi6_ifindex
!= IFSCOPE_NONE
)
807 opt
->ip6po_pktinfo
->ipi6_ifindex
= IFSCOPE_NONE
;
809 exthdrs
.ip6e_ip6
= m
;
820 ip6
= mtod(m
, struct ip6_hdr
*);
823 bzero(&src_sa
, sizeof(src_sa
));
824 src_sa
.sin6_family
= AF_INET6
;
825 src_sa
.sin6_len
= sizeof(src_sa
);
826 src_sa
.sin6_addr
= ip6
->ip6_src
;
828 bzero(&dst_sa
, sizeof(dst_sa
));
829 dst_sa
.sin6_family
= AF_INET6
;
830 dst_sa
.sin6_len
= sizeof(dst_sa
);
831 dst_sa
.sin6_addr
= ip6
->ip6_dst
;
833 if ((error
= in6_selectroute(select_srcif
? &src_sa
: NULL
,
834 &dst_sa
, opt
, im6o
, ro
, &ifp
, &rt
, 0, ifscope
, nocell
)) != 0) {
837 ip6stat
.ip6s_noroute
++;
841 break; /* XXX statistics? */
844 in6_ifstat_inc(ifp
, ifs6_out_discard
);
849 * If in6_selectroute() does not return a route entry,
850 * dst may not have been updated.
852 *dst
= dst_sa
; /* XXX */
856 * then rt (for unicast) and ifp must be non-NULL valid values.
858 if ((flags
& IPV6_FORWARDING
) == 0) {
859 /* XXX: the FORWARDING flag can be set for mrouting. */
860 in6_ifstat_inc(ifp
, ifs6_out_request
);
864 ia
= (struct in6_ifaddr
*)(rt
->rt_ifa
);
866 IFA_ADDREF(&ia
->ia_ifa
);
872 * The outgoing interface must be in the zone of source and
873 * destination addresses. We should use ia_ifp to support the
874 * case of sending packets to an address of our own.
876 if (ia
!= NULL
&& ia
->ia_ifp
) {
877 ifnet_reference(ia
->ia_ifp
);
879 ifnet_release(origifp
);
880 origifp
= ia
->ia_ifp
;
883 ifnet_reference(ifp
);
885 ifnet_release(origifp
);
889 if (in6_setscope(&src0
, origifp
, &zone
))
891 bzero(&src_sa
, sizeof(src_sa
));
892 src_sa
.sin6_family
= AF_INET6
;
893 src_sa
.sin6_len
= sizeof(src_sa
);
894 src_sa
.sin6_addr
= ip6
->ip6_src
;
895 if (sa6_recoverscope(&src_sa
) || zone
!= src_sa
.sin6_scope_id
)
899 if (in6_setscope(&dst0
, origifp
, &zone
))
901 /* re-initialize to be sure */
902 bzero(&dst_sa
, sizeof(dst_sa
));
903 dst_sa
.sin6_family
= AF_INET6
;
904 dst_sa
.sin6_len
= sizeof(dst_sa
);
905 dst_sa
.sin6_addr
= ip6
->ip6_dst
;
906 if (sa6_recoverscope(&dst_sa
) || zone
!= dst_sa
.sin6_scope_id
) {
910 /* scope check is done. */
914 ip6stat
.ip6s_badscope
++;
915 in6_ifstat_inc(origifp
, ifs6_out_discard
);
917 error
= EHOSTUNREACH
; /* XXX */
921 if (rt
&& !IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
922 if (opt
&& opt
->ip6po_nextroute
.ro_rt
) {
924 * The nexthop is explicitly specified by the
925 * application. We assume the next hop is an IPv6
928 dst
= (struct sockaddr_in6
*)opt
->ip6po_nexthop
;
930 else if ((rt
->rt_flags
& RTF_GATEWAY
))
931 dst
= (struct sockaddr_in6
*)rt
->rt_gateway
;
934 if (!IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
935 m
->m_flags
&= ~(M_BCAST
| M_MCAST
); /* just in case */
937 struct in6_multi
*in6m
;
939 m
->m_flags
= (m
->m_flags
& ~M_BCAST
) | M_MCAST
;
941 in6_ifstat_inc(ifp
, ifs6_out_mcast
);
944 * Confirm that the outgoing interface supports multicast.
946 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
947 ip6stat
.ip6s_noroute
++;
948 in6_ifstat_inc(ifp
, ifs6_out_discard
);
952 in6_multihead_lock_shared();
953 IN6_LOOKUP_MULTI(&ip6
->ip6_dst
, ifp
, in6m
);
954 in6_multihead_lock_done();
958 (im6o
== NULL
|| im6o
->im6o_multicast_loop
)) {
962 * If we belong to the destination multicast group
963 * on the outgoing interface, and the caller did not
964 * forbid loopback, loop back a copy.
966 ip6_mloopback(ifp
, m
, dst
);
971 * If we are acting as a multicast router, perform
972 * multicast forwarding as if the packet had just
973 * arrived on the interface to which we are about
974 * to send. The multicast forwarding function
975 * recursively calls this function, using the
976 * IPV6_FORWARDING flag to prevent infinite recursion.
978 * Multicasts that are looped back by ip6_mloopback(),
979 * above, will be forwarded by the ip6_input() routine,
983 if (ip6_mrouter
&& (flags
& IPV6_FORWARDING
) == 0) {
985 * XXX: ip6_mforward expects that rcvif is NULL
986 * when it is called from the originating path.
987 * However, it is not always the case, since
988 * some versions of MGETHDR() does not
989 * initialize the field.
991 m
->m_pkthdr
.rcvif
= NULL
;
992 if (ip6_mforward(ip6
, ifp
, m
) != 0) {
1004 * Multicasts with a hoplimit of zero may be looped back,
1005 * above, but must not be transmitted on a network.
1006 * Also, multicasts addressed to the loopback interface
1007 * are not sent -- the above call to ip6_mloopback() will
1008 * loop back a copy if this host actually belongs to the
1009 * destination group on the loopback interface.
1011 if (ip6
->ip6_hlim
== 0 || (ifp
->if_flags
& IFF_LOOPBACK
) ||
1012 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6
->ip6_dst
)) {
1019 * Fill the outgoing inteface to tell the upper layer
1020 * to increment per-interface statistics.
1023 ifnet_reference(ifp
); /* for caller */
1025 ifnet_release(*ifpp
);
1029 /* Determine path MTU. */
1030 if ((error
= ip6_getpmtu(ro_pmtu
, ro
, ifp
, &finaldst
, &mtu
,
1035 * The caller of this function may specify to use the minimum MTU
1037 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
1038 * setting. The logic is a bit complicated; by default, unicast
1039 * packets will follow path MTU while multicast packets will be sent at
1040 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
1041 * including unicast ones will be sent at the minimum MTU. Multicast
1042 * packets will always be sent at the minimum MTU unless
1043 * IP6PO_MINMTU_DISABLE is explicitly specified.
1044 * See RFC 3542 for more details.
1046 if (mtu
> IPV6_MMTU
) {
1047 if ((flags
& IPV6_MINMTU
))
1049 else if (opt
&& opt
->ip6po_minmtu
== IP6PO_MINMTU_ALL
)
1051 else if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) &&
1053 opt
->ip6po_minmtu
!= IP6PO_MINMTU_DISABLE
)) {
1059 * clear embedded scope identifiers if necessary.
1060 * in6_clearscope will touch the addresses only when necessary.
1062 in6_clearscope(&ip6
->ip6_src
);
1063 in6_clearscope(&ip6
->ip6_dst
);
1067 * Check with the firewall...
1069 if (ip6_fw_enable
&& ip6_fw_chk_ptr
) {
1071 m
->m_pkthdr
.rcvif
= NULL
; /* XXX */
1072 /* If ipfw says divert, we have to just drop packet */
1073 if (ip6_fw_chk_ptr(&ip6
, ifp
, &port
, &m
)) {
1085 * If the outgoing packet contains a hop-by-hop options header,
1086 * it must be examined and processed even by the source node.
1087 * (RFC 2460, section 4.)
1089 if (exthdrs
.ip6e_hbh
) {
1090 struct ip6_hbh
*hbh
= mtod(exthdrs
.ip6e_hbh
, struct ip6_hbh
*);
1091 u_int32_t dummy
; /* XXX unused */
1094 if ((hbh
->ip6h_len
+ 1) << 3 > exthdrs
.ip6e_hbh
->m_len
)
1095 panic("ip6e_hbh is not continuous");
1098 * XXX: if we have to send an ICMPv6 error to the sender,
1099 * we need the M_LOOP flag since icmp6_error() expects
1100 * the IPv6 and the hop-by-hop options header are
1101 * continuous unless the flag is set.
1103 m
->m_flags
|= M_LOOP
;
1104 m
->m_pkthdr
.rcvif
= ifp
;
1105 if (ip6_process_hopopts(m
, (u_int8_t
*)(hbh
+ 1),
1106 ((hbh
->ip6h_len
+ 1) << 3) - sizeof(struct ip6_hbh
),
1107 &dummy
, &plen
) < 0) {
1108 /* m was already freed at this point */
1109 error
= EINVAL
;/* better error? */
1112 m
->m_flags
&= ~M_LOOP
; /* XXX */
1113 m
->m_pkthdr
.rcvif
= NULL
;
1117 if (PF_IS_ENABLED
) {
1118 /* Invoke outbound packet filter */
1119 error
= pf_af_hook(ifp
, NULL
, &m
, AF_INET6
, FALSE
);
1123 panic("%s: unexpected packet %p\n", __func__
, m
);
1126 /* Already freed by callee */
1129 ip6
= mtod(m
, struct ip6_hdr
*);
1134 * Send the packet to the outgoing interface.
1135 * If necessary, do IPv6 fragmentation before sending.
1137 * the logic here is rather complex:
1138 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
1139 * 1-a: send as is if tlen <= path mtu
1140 * 1-b: fragment if tlen > path mtu
1142 * 2: if user asks us not to fragment (dontfrag == 1)
1143 * 2-a: send as is if tlen <= interface mtu
1144 * 2-b: error if tlen > interface mtu
1146 * 3: if we always need to attach fragment header (alwaysfrag == 1)
1149 * 4: if dontfrag == 1 && alwaysfrag == 1
1150 * error, as we cannot handle this conflicting request
1152 tlen
= m
->m_pkthdr
.len
;
1154 if (opt
&& (opt
->ip6po_flags
& IP6PO_DONTFRAG
))
1158 if (dontfrag
&& alwaysfrag
) { /* case 4 */
1159 /* conflicting request - can't transmit */
1164 lck_rw_lock_shared(nd_if_rwlock
);
1165 ifmtu
= IN6_LINKMTU(ifp
);
1166 lck_rw_done(nd_if_rwlock
);
1168 if (dontfrag
&& tlen
> ifmtu
) { /* case 2-b */
1170 * Even if the DONTFRAG option is specified, we cannot send the
1171 * packet when the data length is larger than the MTU of the
1172 * outgoing interface.
1173 * Notify the error by sending IPV6_PATHMTU ancillary data as
1174 * well as returning an error code (the latter is not described
1178 struct ip6ctlparam ip6cp
;
1180 mtu32
= (u_int32_t
)mtu
;
1181 bzero(&ip6cp
, sizeof(ip6cp
));
1182 ip6cp
.ip6c_cmdarg
= (void *)&mtu32
;
1183 pfctlinput2(PRC_MSGSIZE
, (struct sockaddr
*)&ro_pmtu
->ro_dst
,
1191 * transmit packet without fragmentation
1193 tso
= (ifp
->if_hwassist
& IFNET_TSO_IPV6
) &&
1194 (m
->m_pkthdr
.csum_flags
& CSUM_TSO_IPV6
);
1195 if (dontfrag
|| (!alwaysfrag
&& /* case 1-a and 2-a */
1196 (tlen
<= mtu
|| tso
|| (ifp
->if_hwassist
& CSUM_FRAGMENT_IPV6
)))) {
1199 ip6
= mtod(m
, struct ip6_hdr
*);
1201 /* clean ipsec history once it goes out of the node */
1205 if (apple_hwcksum_tx
== 0) /* Do not let HW handle cksum */
1206 sw_csum
= m
->m_pkthdr
.csum_flags
;
1208 sw_csum
= m
->m_pkthdr
.csum_flags
&
1209 ~IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
1211 if ((sw_csum
& CSUM_DELAY_IPV6_DATA
) != 0) {
1212 in6_delayed_cksum(m
, sizeof(struct ip6_hdr
) + optlen
);
1213 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_IPV6_DATA
;
1216 RT_LOCK_ASSERT_NOTHELD(ro
->ro_rt
);
1217 error
= nd6_output(ifp
, origifp
, m
, dst
, ro
->ro_rt
);
1222 * try to fragment the packet. case 1-b and 3
1224 if (mtu
< IPV6_MMTU
) {
1225 /* path MTU cannot be less than IPV6_MMTU */
1227 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1229 } else if (ip6
->ip6_plen
== 0) {
1230 /* jumbo payload cannot be fragmented */
1232 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1235 struct mbuf
**mnext
, *m_frgpart
;
1236 struct ip6_frag
*ip6f
;
1237 u_int32_t id
= htonl(ip6_randomid());
1241 * Too large for the destination or interface;
1242 * fragment if possible.
1243 * Must be able to put at least 8 bytes per fragment.
1245 hlen
= unfragpartlen
;
1246 if (mtu
> IPV6_MAXPACKET
)
1247 mtu
= IPV6_MAXPACKET
;
1249 len
= (mtu
- hlen
- sizeof(struct ip6_frag
)) & ~7;
1252 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1256 mnext
= &m
->m_nextpkt
;
1259 * Change the next header field of the last header in the
1260 * unfragmentable part.
1262 if (exthdrs
.ip6e_rthdr
) {
1263 nextproto
= *mtod(exthdrs
.ip6e_rthdr
, u_char
*);
1264 *mtod(exthdrs
.ip6e_rthdr
, u_char
*) = IPPROTO_FRAGMENT
;
1265 } else if (exthdrs
.ip6e_dest1
) {
1266 nextproto
= *mtod(exthdrs
.ip6e_dest1
, u_char
*);
1267 *mtod(exthdrs
.ip6e_dest1
, u_char
*) = IPPROTO_FRAGMENT
;
1268 } else if (exthdrs
.ip6e_hbh
) {
1269 nextproto
= *mtod(exthdrs
.ip6e_hbh
, u_char
*);
1270 *mtod(exthdrs
.ip6e_hbh
, u_char
*) = IPPROTO_FRAGMENT
;
1272 nextproto
= ip6
->ip6_nxt
;
1273 ip6
->ip6_nxt
= IPPROTO_FRAGMENT
;
1276 if ((m
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
) != 0) {
1277 in6_delayed_cksum(m
, sizeof(struct ip6_hdr
) + optlen
);
1278 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_IPV6_DATA
;
1282 * Loop through length of segment after first fragment,
1283 * make new header and copy data of each part and link onto
1287 for (off
= hlen
; off
< tlen
; off
+= len
) {
1288 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1291 ip6stat
.ip6s_odropped
++;
1294 m
->m_pkthdr
.rcvif
= NULL
;
1295 m
->m_flags
= m0
->m_flags
& M_COPYFLAGS
;
1297 mnext
= &m
->m_nextpkt
;
1298 m
->m_data
+= max_linkhdr
;
1299 mhip6
= mtod(m
, struct ip6_hdr
*);
1301 m
->m_len
= sizeof(*mhip6
);
1302 error
= ip6_insertfraghdr(m0
, m
, hlen
, &ip6f
);
1304 ip6stat
.ip6s_odropped
++;
1307 ip6f
->ip6f_offlg
= htons((u_short
)((off
- hlen
) & ~7));
1308 if (off
+ len
>= tlen
)
1311 ip6f
->ip6f_offlg
|= IP6F_MORE_FRAG
;
1312 mhip6
->ip6_plen
= htons((u_short
)(len
+ hlen
+
1314 sizeof(struct ip6_hdr
)));
1315 if ((m_frgpart
= m_copy(m0
, off
, len
)) == 0) {
1317 ip6stat
.ip6s_odropped
++;
1320 m_cat(m
, m_frgpart
);
1321 m
->m_pkthdr
.len
= len
+ hlen
+ sizeof(*ip6f
);
1322 m
->m_pkthdr
.rcvif
= 0;
1323 m
->m_pkthdr
.socket_id
= m0
->m_pkthdr
.socket_id
;
1324 #ifdef __darwin8_notyet
1326 mac_create_fragment(m0
, m
);
1329 ip6f
->ip6f_reserved
= 0;
1330 ip6f
->ip6f_ident
= id
;
1331 ip6f
->ip6f_nxt
= nextproto
;
1332 ip6stat
.ip6s_ofragments
++;
1333 in6_ifstat_inc(ifp
, ifs6_out_fragcreat
);
1336 in6_ifstat_inc(ifp
, ifs6_out_fragok
);
1340 * Remove leading garbages.
1346 for (m0
= m
; m
; m
= m0
) {
1350 /* Record statistics for this interface address. */
1353 ia
->ia_ifa
.if_opackets
++;
1354 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1358 /* clean ipsec history once it goes out of the node */
1361 error
= nd6_output(ifp
, origifp
, m
, dst
, ro
->ro_rt
);
1368 ip6stat
.ip6s_fragmented
++;
1372 if (ipsec_saved_route
) {
1373 ro
= ipsec_saved_route
;
1374 if (ipsec_state
.ro
.ro_rt
) {
1375 rtfree(ipsec_state
.ro
.ro_rt
);
1379 if (ro
== &ip6route
&& ro
->ro_rt
) { /* brace necessary for rtfree */
1381 } else if (ro_pmtu
== &ip6route
&& ro_pmtu
->ro_rt
) {
1382 rtfree(ro_pmtu
->ro_rt
);
1387 key_freesp(sp
, KEY_SADB_UNLOCKED
);
1391 IFA_REMREF(&ia
->ia_ifa
);
1394 if (origifp
!= NULL
)
1395 ifnet_release(origifp
);
1399 m_freem(exthdrs
.ip6e_hbh
); /* m_freem will check if mbuf is 0 */
1400 m_freem(exthdrs
.ip6e_dest1
);
1401 m_freem(exthdrs
.ip6e_rthdr
);
1402 m_freem(exthdrs
.ip6e_dest2
);
1410 ip6_copyexthdr(mp
, hdr
, hlen
)
1417 if (hlen
> MCLBYTES
)
1418 return(ENOBUFS
); /* XXX */
1420 MGET(m
, M_DONTWAIT
, MT_DATA
);
1425 MCLGET(m
, M_DONTWAIT
);
1426 if ((m
->m_flags
& M_EXT
) == 0) {
1433 bcopy(hdr
, mtod(m
, caddr_t
), hlen
);
1440 * Process a delayed payload checksum calculation.
1443 in6_delayed_cksum(struct mbuf
*m
, uint16_t offset
)
1447 csum
= in6_cksum(m
, 0, offset
, m
->m_pkthdr
.len
- offset
);
1448 if (csum
== 0 && (m
->m_pkthdr
.csum_flags
& CSUM_UDPIPV6
) != 0) {
1452 offset
+= (m
->m_pkthdr
.csum_data
& 0xffff);
1453 if ((offset
+ sizeof(csum
)) > m
->m_len
) {
1454 m_copyback(m
, offset
, sizeof(csum
), &csum
);
1456 *(uint16_t *)(mtod(m
, char *) + offset
) = csum
;
1460 * Insert jumbo payload option.
1463 ip6_insert_jumboopt(exthdrs
, plen
)
1464 struct ip6_exthdrs
*exthdrs
;
1471 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1474 * If there is no hop-by-hop options header, allocate new one.
1475 * If there is one but it doesn't have enough space to store the
1476 * jumbo payload option, allocate a cluster to store the whole options.
1477 * Otherwise, use it to store the options.
1479 if (exthdrs
->ip6e_hbh
== 0) {
1480 MGET(mopt
, M_DONTWAIT
, MT_DATA
);
1483 mopt
->m_len
= JUMBOOPTLEN
;
1484 optbuf
= mtod(mopt
, u_char
*);
1485 optbuf
[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1486 exthdrs
->ip6e_hbh
= mopt
;
1488 struct ip6_hbh
*hbh
;
1490 mopt
= exthdrs
->ip6e_hbh
;
1491 if (M_TRAILINGSPACE(mopt
) < JUMBOOPTLEN
) {
1494 * - exthdrs->ip6e_hbh is not referenced from places
1495 * other than exthdrs.
1496 * - exthdrs->ip6e_hbh is not an mbuf chain.
1498 u_int32_t oldoptlen
= mopt
->m_len
;
1502 * XXX: give up if the whole (new) hbh header does
1503 * not fit even in an mbuf cluster.
1505 if (oldoptlen
+ JUMBOOPTLEN
> MCLBYTES
)
1509 * As a consequence, we must always prepare a cluster
1512 MGET(n
, M_DONTWAIT
, MT_DATA
);
1514 MCLGET(n
, M_DONTWAIT
);
1515 if ((n
->m_flags
& M_EXT
) == 0) {
1522 n
->m_len
= oldoptlen
+ JUMBOOPTLEN
;
1523 bcopy(mtod(mopt
, caddr_t
), mtod(n
, caddr_t
),
1525 optbuf
= mtod(n
, u_char
*) + oldoptlen
;
1527 mopt
= exthdrs
->ip6e_hbh
= n
;
1529 optbuf
= mtod(mopt
, u_char
*) + mopt
->m_len
;
1530 mopt
->m_len
+= JUMBOOPTLEN
;
1532 optbuf
[0] = IP6OPT_PADN
;
1536 * Adjust the header length according to the pad and
1537 * the jumbo payload option.
1539 hbh
= mtod(mopt
, struct ip6_hbh
*);
1540 hbh
->ip6h_len
+= (JUMBOOPTLEN
>> 3);
1543 /* fill in the option. */
1544 optbuf
[2] = IP6OPT_JUMBO
;
1546 v
= (u_int32_t
)htonl(plen
+ JUMBOOPTLEN
);
1547 bcopy(&v
, &optbuf
[4], sizeof(u_int32_t
));
1549 /* finally, adjust the packet header length */
1550 exthdrs
->ip6e_ip6
->m_pkthdr
.len
+= JUMBOOPTLEN
;
1557 * Insert fragment header and copy unfragmentable header portions.
1560 ip6_insertfraghdr(m0
, m
, hlen
, frghdrp
)
1561 struct mbuf
*m0
, *m
;
1563 struct ip6_frag
**frghdrp
;
1565 struct mbuf
*n
, *mlast
;
1567 if (hlen
> sizeof(struct ip6_hdr
)) {
1568 n
= m_copym(m0
, sizeof(struct ip6_hdr
),
1569 hlen
- sizeof(struct ip6_hdr
), M_DONTWAIT
);
1576 /* Search for the last mbuf of unfragmentable part. */
1577 for (mlast
= n
; mlast
->m_next
; mlast
= mlast
->m_next
)
1580 if ((mlast
->m_flags
& M_EXT
) == 0 &&
1581 M_TRAILINGSPACE(mlast
) >= sizeof(struct ip6_frag
)) {
1582 /* use the trailing space of the last mbuf for the fragment hdr */
1583 *frghdrp
= (struct ip6_frag
*)(mtod(mlast
, caddr_t
) +
1585 mlast
->m_len
+= sizeof(struct ip6_frag
);
1586 m
->m_pkthdr
.len
+= sizeof(struct ip6_frag
);
1588 /* allocate a new mbuf for the fragment header */
1591 MGET(mfrg
, M_DONTWAIT
, MT_DATA
);
1594 mfrg
->m_len
= sizeof(struct ip6_frag
);
1595 *frghdrp
= mtod(mfrg
, struct ip6_frag
*);
1596 mlast
->m_next
= mfrg
;
1602 extern int load_ipfw(void);
1604 ip6_getpmtu(struct route_in6
*ro_pmtu
, struct route_in6
*ro
,
1605 struct ifnet
*ifp
, struct in6_addr
*dst
, u_int32_t
*mtup
,
1612 if (ro_pmtu
!= ro
) {
1613 /* The first hop and the final destination may differ. */
1614 struct sockaddr_in6
*sa6_dst
=
1615 (struct sockaddr_in6
*)&ro_pmtu
->ro_dst
;
1616 if (ro_pmtu
->ro_rt
&&
1617 ((ro_pmtu
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
1618 ro_pmtu
->ro_rt
->generation_id
!= route_generation
||
1619 !IN6_ARE_ADDR_EQUAL(&sa6_dst
->sin6_addr
, dst
))) {
1620 rtfree(ro_pmtu
->ro_rt
);
1621 ro_pmtu
->ro_rt
= (struct rtentry
*)NULL
;
1623 if (ro_pmtu
->ro_rt
== NULL
) {
1624 bzero(sa6_dst
, sizeof(*sa6_dst
));
1625 sa6_dst
->sin6_family
= AF_INET6
;
1626 sa6_dst
->sin6_len
= sizeof(struct sockaddr_in6
);
1627 sa6_dst
->sin6_addr
= *dst
;
1629 rtalloc_scoped((struct route
*)ro_pmtu
,
1630 ifp
!= NULL
? ifp
->if_index
: IFSCOPE_NONE
);
1635 if (ro_pmtu
->ro_rt
!= NULL
) {
1638 lck_rw_lock_shared(nd_if_rwlock
);
1639 ifmtu
= IN6_LINKMTU(ifp
);
1640 lck_rw_done(nd_if_rwlock
);
1642 RT_LOCK_SPIN(ro_pmtu
->ro_rt
);
1643 mtu
= ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
;
1644 if (mtu
> ifmtu
|| mtu
== 0) {
1646 * The MTU on the route is larger than the MTU on
1647 * the interface! This shouldn't happen, unless the
1648 * MTU of the interface has been changed after the
1649 * interface was brought up. Change the MTU in the
1650 * route to match the interface MTU (as long as the
1651 * field isn't locked).
1653 * if MTU on the route is 0, we need to fix the MTU.
1654 * this case happens with path MTU discovery timeouts.
1657 if ((ro_pmtu
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
) == 0)
1658 ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
= mtu
; /* XXX */
1660 else if (mtu
< IPV6_MMTU
) {
1662 * RFC2460 section 5, last paragraph:
1663 * if we record ICMPv6 too big message with
1664 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1665 * or smaller, with framgent header attached.
1666 * (fragment header is needed regardless from the
1667 * packet size, for translators to identify packets)
1672 RT_UNLOCK(ro_pmtu
->ro_rt
);
1675 lck_rw_lock_shared(nd_if_rwlock
);
1676 mtu
= IN6_LINKMTU(ifp
);
1677 lck_rw_done(nd_if_rwlock
);
1679 error
= EHOSTUNREACH
; /* XXX */
1684 *alwaysfragp
= alwaysfrag
;
1689 * IP6 socket option processing.
1692 ip6_ctloutput(so
, sopt
)
1694 struct sockopt
*sopt
;
1696 int optdatalen
, uproto
;
1699 struct inpcb
*in6p
= sotoinpcb(so
);
1700 int error
= 0, optval
= 0;
1701 int level
, op
= -1, optname
= 0;
1706 panic("ip6_ctloutput: arg soopt is NULL");
1709 level
= sopt
->sopt_level
;
1710 op
= sopt
->sopt_dir
;
1711 optname
= sopt
->sopt_name
;
1712 optlen
= sopt
->sopt_valsize
;
1714 uproto
= (int)so
->so_proto
->pr_protocol
;
1716 privileged
= (proc_suser(p
) == 0);
1718 if (level
== IPPROTO_IPV6
) {
1723 case IPV6_2292PKTOPTIONS
:
1727 error
= soopt_getm(sopt
, &m
); /* XXX */
1730 error
= soopt_mcopyin(sopt
, m
); /* XXX */
1733 error
= ip6_pcbopts(&in6p
->in6p_outputopts
,
1735 m_freem(m
); /* XXX */
1740 * Use of some Hop-by-Hop options or some
1741 * Destination options, might require special
1742 * privilege. That is, normal applications
1743 * (without special privilege) might be forbidden
1744 * from setting certain options in outgoing packets,
1745 * and might never see certain options in received
1746 * packets. [RFC 2292 Section 6]
1747 * KAME specific note:
1748 * KAME prevents non-privileged users from sending or
1749 * receiving ANY hbh/dst options in order to avoid
1750 * overhead of parsing options in the kernel.
1752 case IPV6_RECVHOPOPTS
:
1753 case IPV6_RECVDSTOPTS
:
1754 case IPV6_RECVRTHDRDSTOPTS
:
1758 case IPV6_UNICAST_HOPS
:
1762 case IPV6_RECVPKTINFO
:
1763 case IPV6_RECVHOPLIMIT
:
1764 case IPV6_RECVRTHDR
:
1765 case IPV6_RECVPATHMTU
:
1766 case IPV6_RECVTCLASS
:
1768 case IPV6_AUTOFLOWLABEL
:
1769 if (optlen
!= sizeof(int)) {
1773 error
= sooptcopyin(sopt
, &optval
,
1774 sizeof optval
, sizeof optval
);
1779 case IPV6_UNICAST_HOPS
:
1780 if (optval
< -1 || optval
>= 256)
1783 /* -1 = kernel default */
1784 in6p
->in6p_hops
= optval
;
1785 if ((in6p
->inp_vflag
&
1787 in6p
->inp_ip_ttl
= optval
;
1790 #define OPTSET(bit) \
1793 in6p->inp_flags |= (bit); \
1795 in6p->inp_flags &= ~(bit); \
1796 } while (/*CONSTCOND*/ 0)
1797 #define OPTSET2292(bit) \
1799 in6p->inp_flags |= IN6P_RFC2292; \
1801 in6p->inp_flags |= (bit); \
1803 in6p->inp_flags &= ~(bit); \
1804 } while (/*CONSTCOND*/ 0)
1805 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1807 case IPV6_RECVPKTINFO
:
1808 /* cannot mix with RFC2292 */
1809 if (OPTBIT(IN6P_RFC2292
)) {
1813 OPTSET(IN6P_PKTINFO
);
1818 struct ip6_pktopts
**optp
;
1820 /* cannot mix with RFC2292 */
1821 if (OPTBIT(IN6P_RFC2292
)) {
1825 optp
= &in6p
->in6p_outputopts
;
1826 error
= ip6_pcbopt(IPV6_HOPLIMIT
,
1827 (u_char
*)&optval
, sizeof(optval
),
1832 case IPV6_RECVHOPLIMIT
:
1833 /* cannot mix with RFC2292 */
1834 if (OPTBIT(IN6P_RFC2292
)) {
1838 OPTSET(IN6P_HOPLIMIT
);
1841 case IPV6_RECVHOPOPTS
:
1842 /* cannot mix with RFC2292 */
1843 if (OPTBIT(IN6P_RFC2292
)) {
1847 OPTSET(IN6P_HOPOPTS
);
1850 case IPV6_RECVDSTOPTS
:
1851 /* cannot mix with RFC2292 */
1852 if (OPTBIT(IN6P_RFC2292
)) {
1856 OPTSET(IN6P_DSTOPTS
);
1859 case IPV6_RECVRTHDRDSTOPTS
:
1860 /* cannot mix with RFC2292 */
1861 if (OPTBIT(IN6P_RFC2292
)) {
1865 OPTSET(IN6P_RTHDRDSTOPTS
);
1868 case IPV6_RECVRTHDR
:
1869 /* cannot mix with RFC2292 */
1870 if (OPTBIT(IN6P_RFC2292
)) {
1881 case IPV6_RECVPATHMTU
:
1883 * We ignore this option for TCP
1885 * (RFC3542 leaves this case
1888 if (uproto
!= IPPROTO_TCP
)
1894 * make setsockopt(IPV6_V6ONLY)
1895 * available only prior to bind(2).
1896 * see ipng mailing list, Jun 22 2001.
1898 if (in6p
->inp_lport
||
1899 !IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
)) {
1903 OPTSET(IN6P_IPV6_V6ONLY
);
1905 in6p
->inp_vflag
&= ~INP_IPV4
;
1907 in6p
->inp_vflag
|= INP_IPV4
;
1909 case IPV6_RECVTCLASS
:
1910 /* we can mix with RFC2292 */
1911 OPTSET(IN6P_TCLASS
);
1913 case IPV6_AUTOFLOWLABEL
:
1914 OPTSET(IN6P_AUTOFLOWLABEL
);
1922 case IPV6_USE_MIN_MTU
:
1923 case IPV6_PREFER_TEMPADDR
:
1924 if (optlen
!= sizeof(optval
)) {
1928 error
= sooptcopyin(sopt
, &optval
,
1929 sizeof optval
, sizeof optval
);
1933 struct ip6_pktopts
**optp
;
1934 optp
= &in6p
->in6p_outputopts
;
1935 error
= ip6_pcbopt(optname
,
1936 (u_char
*)&optval
, sizeof(optval
),
1941 case IPV6_2292PKTINFO
:
1942 case IPV6_2292HOPLIMIT
:
1943 case IPV6_2292HOPOPTS
:
1944 case IPV6_2292DSTOPTS
:
1945 case IPV6_2292RTHDR
:
1947 if (optlen
!= sizeof(int)) {
1951 error
= sooptcopyin(sopt
, &optval
,
1952 sizeof optval
, sizeof optval
);
1956 case IPV6_2292PKTINFO
:
1957 OPTSET2292(IN6P_PKTINFO
);
1959 case IPV6_2292HOPLIMIT
:
1960 OPTSET2292(IN6P_HOPLIMIT
);
1962 case IPV6_2292HOPOPTS
:
1964 * Check super-user privilege.
1965 * See comments for IPV6_RECVHOPOPTS.
1969 OPTSET2292(IN6P_HOPOPTS
);
1971 case IPV6_2292DSTOPTS
:
1974 OPTSET2292(IN6P_DSTOPTS
|IN6P_RTHDRDSTOPTS
); /* XXX */
1976 case IPV6_2292RTHDR
:
1977 OPTSET2292(IN6P_RTHDR
);
1981 case IPV6_3542PKTINFO
:
1982 case IPV6_3542HOPOPTS
:
1983 case IPV6_3542RTHDR
:
1984 case IPV6_3542DSTOPTS
:
1985 case IPV6_RTHDRDSTOPTS
:
1986 case IPV6_3542NEXTHOP
:
1988 /* new advanced API (RFC3542) */
1991 /* cannot mix with RFC2292 */
1992 if (OPTBIT(IN6P_RFC2292
)) {
1996 error
= soopt_getm(sopt
, &m
);
1999 error
= soopt_mcopyin(sopt
, m
);
2004 error
= ip6_pcbopt(optname
, mtod(m
, u_char
*),
2005 m
->m_len
, &in6p
->in6p_outputopts
, uproto
);
2011 case IPV6_MULTICAST_IF
:
2012 case IPV6_MULTICAST_HOPS
:
2013 case IPV6_MULTICAST_LOOP
:
2014 case IPV6_JOIN_GROUP
:
2015 case IPV6_LEAVE_GROUP
:
2017 case MCAST_BLOCK_SOURCE
:
2018 case MCAST_UNBLOCK_SOURCE
:
2019 case MCAST_JOIN_GROUP
:
2020 case MCAST_LEAVE_GROUP
:
2021 case MCAST_JOIN_SOURCE_GROUP
:
2022 case MCAST_LEAVE_SOURCE_GROUP
:
2023 error
= ip6_setmoptions(in6p
, sopt
);
2026 case IPV6_PORTRANGE
:
2027 error
= sooptcopyin(sopt
, &optval
,
2028 sizeof optval
, sizeof optval
);
2033 case IPV6_PORTRANGE_DEFAULT
:
2034 in6p
->inp_flags
&= ~(INP_LOWPORT
);
2035 in6p
->inp_flags
&= ~(INP_HIGHPORT
);
2038 case IPV6_PORTRANGE_HIGH
:
2039 in6p
->inp_flags
&= ~(INP_LOWPORT
);
2040 in6p
->inp_flags
|= INP_HIGHPORT
;
2043 case IPV6_PORTRANGE_LOW
:
2044 in6p
->inp_flags
&= ~(INP_HIGHPORT
);
2045 in6p
->inp_flags
|= INP_LOWPORT
;
2055 case IPV6_IPSEC_POLICY
:
2061 if ((error
= soopt_getm(sopt
, &m
)) != 0) /* XXX */
2063 if ((error
= soopt_mcopyin(sopt
, m
)) != 0) /* XXX */
2066 req
= mtod(m
, caddr_t
);
2069 error
= ipsec6_set_policy(in6p
, optname
, req
,
2074 #endif /* KAME IPSEC */
2082 if (ip6_fw_ctl_ptr
== NULL
)
2084 if (ip6_fw_ctl_ptr
!= NULL
)
2085 error
= (*ip6_fw_ctl_ptr
)(sopt
);
2090 #endif /* IPFIREWALL */
2093 * IPv6 variant of IP_BOUND_IF; for details see
2094 * comments on IP_BOUND_IF in ip_ctloutput().
2097 /* This option is settable only on IPv6 */
2098 if (!(in6p
->inp_vflag
& INP_IPV6
)) {
2103 error
= sooptcopyin(sopt
, &optval
,
2104 sizeof (optval
), sizeof (optval
));
2109 inp_bindif(in6p
, optval
);
2112 case IPV6_NO_IFT_CELLULAR
:
2113 /* This option is settable only for IPv6 */
2114 if (!(in6p
->inp_vflag
& INP_IPV6
)) {
2119 error
= sooptcopyin(sopt
, &optval
,
2120 sizeof (optval
), sizeof (optval
));
2125 error
= inp_nocellular(in6p
, optval
);
2129 /* This option is not settable */
2134 error
= ENOPROTOOPT
;
2142 case IPV6_2292PKTOPTIONS
:
2144 * RFC3542 (effectively) deprecated the
2145 * semantics of the 2292-style pktoptions.
2146 * Since it was not reliable in nature (i.e.,
2147 * applications had to expect the lack of some
2148 * information after all), it would make sense
2149 * to simplify this part by always returning
2152 sopt
->sopt_valsize
= 0;
2155 case IPV6_RECVHOPOPTS
:
2156 case IPV6_RECVDSTOPTS
:
2157 case IPV6_RECVRTHDRDSTOPTS
:
2158 case IPV6_UNICAST_HOPS
:
2159 case IPV6_RECVPKTINFO
:
2160 case IPV6_RECVHOPLIMIT
:
2161 case IPV6_RECVRTHDR
:
2162 case IPV6_RECVPATHMTU
:
2166 case IPV6_PORTRANGE
:
2167 case IPV6_RECVTCLASS
:
2168 case IPV6_AUTOFLOWLABEL
:
2171 case IPV6_RECVHOPOPTS
:
2172 optval
= OPTBIT(IN6P_HOPOPTS
);
2175 case IPV6_RECVDSTOPTS
:
2176 optval
= OPTBIT(IN6P_DSTOPTS
);
2179 case IPV6_RECVRTHDRDSTOPTS
:
2180 optval
= OPTBIT(IN6P_RTHDRDSTOPTS
);
2183 case IPV6_UNICAST_HOPS
:
2184 optval
= in6p
->in6p_hops
;
2187 case IPV6_RECVPKTINFO
:
2188 optval
= OPTBIT(IN6P_PKTINFO
);
2191 case IPV6_RECVHOPLIMIT
:
2192 optval
= OPTBIT(IN6P_HOPLIMIT
);
2195 case IPV6_RECVRTHDR
:
2196 optval
= OPTBIT(IN6P_RTHDR
);
2199 case IPV6_RECVPATHMTU
:
2200 optval
= OPTBIT(IN6P_MTU
);
2204 optval
= OPTBIT(INP_FAITH
);
2208 optval
= OPTBIT(IN6P_IPV6_V6ONLY
);
2211 case IPV6_PORTRANGE
:
2214 flags
= in6p
->inp_flags
;
2215 if (flags
& INP_HIGHPORT
)
2216 optval
= IPV6_PORTRANGE_HIGH
;
2217 else if (flags
& INP_LOWPORT
)
2218 optval
= IPV6_PORTRANGE_LOW
;
2223 case IPV6_RECVTCLASS
:
2224 optval
= OPTBIT(IN6P_TCLASS
);
2227 case IPV6_AUTOFLOWLABEL
:
2228 optval
= OPTBIT(IN6P_AUTOFLOWLABEL
);
2233 error
= sooptcopyout(sopt
, &optval
,
2240 struct ip6_mtuinfo mtuinfo
;
2241 struct route_in6 sro
;
2243 bzero(&sro
, sizeof(sro
));
2245 if (!(so
->so_state
& SS_ISCONNECTED
))
2248 * XXX: we dot not consider the case of source
2249 * routing, or optional information to specify
2250 * the outgoing interface.
2252 error
= ip6_getpmtu(&sro
, NULL
, NULL
,
2253 &in6p
->in6p_faddr
, &pmtu
, NULL
);
2258 if (pmtu
> IPV6_MAXPACKET
)
2259 pmtu
= IPV6_MAXPACKET
;
2261 bzero(&mtuinfo
, sizeof(mtuinfo
));
2262 mtuinfo
.ip6m_mtu
= (u_int32_t
)pmtu
;
2263 optdata
= (void *)&mtuinfo
;
2264 optdatalen
= sizeof(mtuinfo
);
2265 error
= sooptcopyout(sopt
, optdata
,
2270 case IPV6_2292PKTINFO
:
2271 case IPV6_2292HOPLIMIT
:
2272 case IPV6_2292HOPOPTS
:
2273 case IPV6_2292RTHDR
:
2274 case IPV6_2292DSTOPTS
:
2276 case IPV6_2292PKTINFO
:
2277 optval
= OPTBIT(IN6P_PKTINFO
);
2279 case IPV6_2292HOPLIMIT
:
2280 optval
= OPTBIT(IN6P_HOPLIMIT
);
2282 case IPV6_2292HOPOPTS
:
2283 optval
= OPTBIT(IN6P_HOPOPTS
);
2285 case IPV6_2292RTHDR
:
2286 optval
= OPTBIT(IN6P_RTHDR
);
2288 case IPV6_2292DSTOPTS
:
2289 optval
= OPTBIT(IN6P_DSTOPTS
|IN6P_RTHDRDSTOPTS
);
2292 error
= sooptcopyout(sopt
, &optval
,
2299 case IPV6_RTHDRDSTOPTS
:
2303 case IPV6_USE_MIN_MTU
:
2304 case IPV6_PREFER_TEMPADDR
:
2305 error
= ip6_getpcbopt(in6p
->in6p_outputopts
,
2309 case IPV6_MULTICAST_IF
:
2310 case IPV6_MULTICAST_HOPS
:
2311 case IPV6_MULTICAST_LOOP
:
2313 error
= ip6_getmoptions(in6p
, sopt
);
2317 case IPV6_IPSEC_POLICY
:
2321 struct mbuf
*m
= NULL
;
2322 struct mbuf
**mp
= &m
;
2324 error
= soopt_getm(sopt
, &m
); /* XXX */
2327 error
= soopt_mcopyin(sopt
, m
); /* XXX */
2331 req
= mtod(m
, caddr_t
);
2334 error
= ipsec6_get_policy(in6p
, req
, len
, mp
);
2336 error
= soopt_mcopyout(sopt
, m
); /*XXX*/
2337 if (error
== 0 && m
)
2341 #endif /* KAME IPSEC */
2346 if (ip6_fw_ctl_ptr
== NULL
)
2348 if (ip6_fw_ctl_ptr
!= NULL
)
2349 error
= (*ip6_fw_ctl_ptr
)(sopt
);
2354 #endif /* IPFIREWALL */
2357 if (in6p
->inp_flags
& INP_BOUND_IF
)
2358 optval
= in6p
->inp_boundif
;
2359 error
= sooptcopyout(sopt
, &optval
,
2363 case IPV6_NO_IFT_CELLULAR
:
2364 optval
= (in6p
->inp_flags
& INP_NO_IFT_CELLULAR
)
2366 error
= sooptcopyout(sopt
, &optval
,
2371 optval
= in6p
->in6p_last_outif
;
2372 error
= sooptcopyout(sopt
, &optval
,
2377 error
= ENOPROTOOPT
;
2389 ip6_raw_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
2391 int error
= 0, optval
, optlen
;
2392 const int icmp6off
= offsetof(struct icmp6_hdr
, icmp6_cksum
);
2393 struct inpcb
*in6p
= sotoinpcb(so
);
2394 int level
, op
, optname
;
2396 level
= sopt
->sopt_level
;
2397 op
= sopt
->sopt_dir
;
2398 optname
= sopt
->sopt_name
;
2399 optlen
= sopt
->sopt_valsize
;
2401 if (level
!= IPPROTO_IPV6
) {
2408 * For ICMPv6 sockets, no modification allowed for checksum
2409 * offset, permit "no change" values to help existing apps.
2411 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2412 * for an ICMPv6 socket will fail."
2413 * The current behavior does not meet RFC3542.
2417 if (optlen
!= sizeof(int)) {
2421 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2425 if ((optval
% 2) != 0) {
2426 /* the API assumes even offset values */
2428 } else if (so
->so_proto
->pr_protocol
==
2430 if (optval
!= icmp6off
)
2433 in6p
->in6p_cksum
= optval
;
2437 if (so
->so_proto
->pr_protocol
== IPPROTO_ICMPV6
)
2440 optval
= in6p
->in6p_cksum
;
2442 error
= sooptcopyout(sopt
, &optval
, sizeof(optval
));
2452 error
= ENOPROTOOPT
;
2460 * Set up IP6 options in pcb for insertion in output packets or
2461 * specifying behavior of outgoing packets.
2465 struct ip6_pktopts
**pktopt
,
2467 __unused
struct socket
*so
,
2468 __unused
struct sockopt
*sopt
)
2470 struct ip6_pktopts
*opt
= *pktopt
;
2473 /* turn off any old options. */
2476 if (opt
->ip6po_pktinfo
|| opt
->ip6po_nexthop
||
2477 opt
->ip6po_hbh
|| opt
->ip6po_dest1
|| opt
->ip6po_dest2
||
2478 opt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
2479 printf("ip6_pcbopts: all specified options are cleared.\n");
2481 ip6_clearpktopts(opt
, -1);
2483 opt
= _MALLOC(sizeof(*opt
), M_IP6OPT
, M_WAITOK
);
2489 if (!m
|| m
->m_len
== 0) {
2491 * Only turning off any previous options, regardless of
2492 * whether the opt is just created or given.
2495 FREE(opt
, M_IP6OPT
);
2499 /* set options specified by user. */
2500 if ((error
= ip6_setpktopts(m
, opt
, NULL
, so
->so_proto
->pr_protocol
)) != 0) {
2501 ip6_clearpktopts(opt
, -1); /* XXX: discard all options */
2502 FREE(opt
, M_IP6OPT
);
2510 * initialize ip6_pktopts. beware that there are non-zero default values in
2514 ip6_initpktopts(struct ip6_pktopts
*opt
)
2517 bzero(opt
, sizeof(*opt
));
2518 opt
->ip6po_hlim
= -1; /* -1 means default hop limit */
2519 opt
->ip6po_tclass
= -1; /* -1 means default traffic class */
2520 opt
->ip6po_minmtu
= IP6PO_MINMTU_MCASTONLY
;
2521 opt
->ip6po_prefer_tempaddr
= IP6PO_TEMPADDR_SYSTEM
;
2525 ip6_pcbopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
**pktopt
, int uproto
)
2527 struct ip6_pktopts
*opt
;
2531 opt
= _MALLOC(sizeof(*opt
), M_IP6OPT
, M_WAITOK
);
2534 ip6_initpktopts(opt
);
2538 return (ip6_setpktopt(optname
, buf
, len
, opt
, 1, 0, uproto
));
2542 ip6_getpcbopt(struct ip6_pktopts
*pktopt
, int optname
, struct sockopt
*sopt
)
2544 void *optdata
= NULL
;
2546 struct ip6_ext
*ip6e
;
2548 struct in6_pktinfo null_pktinfo
;
2549 int deftclass
= 0, on
;
2550 int defminmtu
= IP6PO_MINMTU_MCASTONLY
;
2551 int defpreftemp
= IP6PO_TEMPADDR_SYSTEM
;
2555 if (pktopt
&& pktopt
->ip6po_pktinfo
)
2556 optdata
= (void *)pktopt
->ip6po_pktinfo
;
2558 /* XXX: we don't have to do this every time... */
2559 bzero(&null_pktinfo
, sizeof(null_pktinfo
));
2560 optdata
= (void *)&null_pktinfo
;
2562 optdatalen
= sizeof(struct in6_pktinfo
);
2565 if (pktopt
&& pktopt
->ip6po_tclass
>= 0)
2566 optdata
= (void *)&pktopt
->ip6po_tclass
;
2568 optdata
= (void *)&deftclass
;
2569 optdatalen
= sizeof(int);
2572 if (pktopt
&& pktopt
->ip6po_hbh
) {
2573 optdata
= (void *)pktopt
->ip6po_hbh
;
2574 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_hbh
;
2575 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2579 if (pktopt
&& pktopt
->ip6po_rthdr
) {
2580 optdata
= (void *)pktopt
->ip6po_rthdr
;
2581 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_rthdr
;
2582 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2585 case IPV6_RTHDRDSTOPTS
:
2586 if (pktopt
&& pktopt
->ip6po_dest1
) {
2587 optdata
= (void *)pktopt
->ip6po_dest1
;
2588 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest1
;
2589 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2593 if (pktopt
&& pktopt
->ip6po_dest2
) {
2594 optdata
= (void *)pktopt
->ip6po_dest2
;
2595 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest2
;
2596 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2600 if (pktopt
&& pktopt
->ip6po_nexthop
) {
2601 optdata
= (void *)pktopt
->ip6po_nexthop
;
2602 optdatalen
= pktopt
->ip6po_nexthop
->sa_len
;
2605 case IPV6_USE_MIN_MTU
:
2607 optdata
= (void *)&pktopt
->ip6po_minmtu
;
2609 optdata
= (void *)&defminmtu
;
2610 optdatalen
= sizeof(int);
2613 if (pktopt
&& ((pktopt
->ip6po_flags
) & IP6PO_DONTFRAG
))
2617 optdata
= (void *)&on
;
2618 optdatalen
= sizeof(on
);
2620 case IPV6_PREFER_TEMPADDR
:
2622 optdata
= (void *)&pktopt
->ip6po_prefer_tempaddr
;
2624 optdata
= (void *)&defpreftemp
;
2625 optdatalen
= sizeof(int);
2627 default: /* should not happen */
2629 panic("ip6_getpcbopt: unexpected option\n");
2631 return (ENOPROTOOPT
);
2634 error
= sooptcopyout(sopt
, optdata
, optdatalen
);
2640 ip6_clearpktopts(pktopt
, optname
)
2641 struct ip6_pktopts
*pktopt
;
2647 if (optname
== -1 || optname
== IPV6_PKTINFO
) {
2648 if (pktopt
->ip6po_pktinfo
)
2649 FREE(pktopt
->ip6po_pktinfo
, M_IP6OPT
);
2650 pktopt
->ip6po_pktinfo
= NULL
;
2652 if (optname
== -1 || optname
== IPV6_HOPLIMIT
)
2653 pktopt
->ip6po_hlim
= -1;
2654 if (optname
== -1 || optname
== IPV6_TCLASS
)
2655 pktopt
->ip6po_tclass
= -1;
2656 if (optname
== -1 || optname
== IPV6_NEXTHOP
) {
2657 if (pktopt
->ip6po_nextroute
.ro_rt
) {
2658 rtfree(pktopt
->ip6po_nextroute
.ro_rt
);
2659 pktopt
->ip6po_nextroute
.ro_rt
= NULL
;
2661 if (pktopt
->ip6po_nexthop
)
2662 FREE(pktopt
->ip6po_nexthop
, M_IP6OPT
);
2663 pktopt
->ip6po_nexthop
= NULL
;
2665 if (optname
== -1 || optname
== IPV6_HOPOPTS
) {
2666 if (pktopt
->ip6po_hbh
)
2667 FREE(pktopt
->ip6po_hbh
, M_IP6OPT
);
2668 pktopt
->ip6po_hbh
= NULL
;
2670 if (optname
== -1 || optname
== IPV6_RTHDRDSTOPTS
) {
2671 if (pktopt
->ip6po_dest1
)
2672 FREE(pktopt
->ip6po_dest1
, M_IP6OPT
);
2673 pktopt
->ip6po_dest1
= NULL
;
2675 if (optname
== -1 || optname
== IPV6_RTHDR
) {
2676 if (pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
2677 FREE(pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
, M_IP6OPT
);
2678 pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
= NULL
;
2679 if (pktopt
->ip6po_route
.ro_rt
) {
2680 rtfree(pktopt
->ip6po_route
.ro_rt
);
2681 pktopt
->ip6po_route
.ro_rt
= NULL
;
2684 if (optname
== -1 || optname
== IPV6_DSTOPTS
) {
2685 if (pktopt
->ip6po_dest2
)
2686 FREE(pktopt
->ip6po_dest2
, M_IP6OPT
);
2687 pktopt
->ip6po_dest2
= NULL
;
2691 #define PKTOPT_EXTHDRCPY(type) \
2695 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2696 dst->type = _MALLOC(hlen, M_IP6OPT, canwait);\
2697 if (dst->type == NULL && canwait == M_NOWAIT)\
2699 bcopy(src->type, dst->type, hlen);\
2704 copypktopts(struct ip6_pktopts
*dst
, struct ip6_pktopts
*src
, int canwait
)
2706 if (dst
== NULL
|| src
== NULL
) {
2707 printf("ip6_clearpktopts: invalid argument\n");
2711 dst
->ip6po_hlim
= src
->ip6po_hlim
;
2712 dst
->ip6po_tclass
= src
->ip6po_tclass
;
2713 dst
->ip6po_flags
= src
->ip6po_flags
;
2714 if (src
->ip6po_pktinfo
) {
2715 dst
->ip6po_pktinfo
= _MALLOC(sizeof(*dst
->ip6po_pktinfo
),
2717 if (dst
->ip6po_pktinfo
== NULL
&& canwait
== M_NOWAIT
)
2719 *dst
->ip6po_pktinfo
= *src
->ip6po_pktinfo
;
2721 if (src
->ip6po_nexthop
) {
2722 dst
->ip6po_nexthop
= _MALLOC(src
->ip6po_nexthop
->sa_len
,
2724 if (dst
->ip6po_nexthop
== NULL
&& canwait
== M_NOWAIT
)
2726 bcopy(src
->ip6po_nexthop
, dst
->ip6po_nexthop
,
2727 src
->ip6po_nexthop
->sa_len
);
2729 PKTOPT_EXTHDRCPY(ip6po_hbh
);
2730 PKTOPT_EXTHDRCPY(ip6po_dest1
);
2731 PKTOPT_EXTHDRCPY(ip6po_dest2
);
2732 PKTOPT_EXTHDRCPY(ip6po_rthdr
); /* not copy the cached route */
2736 ip6_clearpktopts(dst
, -1);
2739 #undef PKTOPT_EXTHDRCPY
2741 struct ip6_pktopts
*
2742 ip6_copypktopts(struct ip6_pktopts
*src
, int canwait
)
2745 struct ip6_pktopts
*dst
;
2747 dst
= _MALLOC(sizeof(*dst
), M_IP6OPT
, canwait
);
2750 ip6_initpktopts(dst
);
2752 if ((error
= copypktopts(dst
, src
, canwait
)) != 0) {
2753 FREE(dst
, M_IP6OPT
);
2761 ip6_freepcbopts(pktopt
)
2762 struct ip6_pktopts
*pktopt
;
2767 ip6_clearpktopts(pktopt
, -1);
2769 FREE(pktopt
, M_IP6OPT
);
2773 ip6_moptions_init(void)
2775 PE_parse_boot_argn("ifa_debug", &im6o_debug
, sizeof (im6o_debug
));
2777 im6o_size
= (im6o_debug
== 0) ? sizeof (struct ip6_moptions
) :
2778 sizeof (struct ip6_moptions_dbg
);
2780 im6o_zone
= zinit(im6o_size
, IM6O_ZONE_MAX
* im6o_size
, 0,
2782 if (im6o_zone
== NULL
) {
2783 panic("%s: failed allocating %s", __func__
, IM6O_ZONE_NAME
);
2786 zone_change(im6o_zone
, Z_EXPAND
, TRUE
);
2790 im6o_addref(struct ip6_moptions
*im6o
, int locked
)
2795 IM6O_LOCK_ASSERT_HELD(im6o
);
2797 if (++im6o
->im6o_refcnt
== 0) {
2798 panic("%s: im6o %p wraparound refcnt\n", __func__
, im6o
);
2800 } else if (im6o
->im6o_trace
!= NULL
) {
2801 (*im6o
->im6o_trace
)(im6o
, TRUE
);
2809 im6o_remref(struct ip6_moptions
*im6o
)
2814 if (im6o
->im6o_refcnt
== 0) {
2815 panic("%s: im6o %p negative refcnt", __func__
, im6o
);
2817 } else if (im6o
->im6o_trace
!= NULL
) {
2818 (*im6o
->im6o_trace
)(im6o
, FALSE
);
2821 --im6o
->im6o_refcnt
;
2822 if (im6o
->im6o_refcnt
> 0) {
2827 for (i
= 0; i
< im6o
->im6o_num_memberships
; ++i
) {
2828 struct in6_mfilter
*imf
;
2830 imf
= im6o
->im6o_mfilters
? &im6o
->im6o_mfilters
[i
] : NULL
;
2834 (void) in6_mc_leave(im6o
->im6o_membership
[i
], imf
);
2839 IN6M_REMREF(im6o
->im6o_membership
[i
]);
2840 im6o
->im6o_membership
[i
] = NULL
;
2842 im6o
->im6o_num_memberships
= 0;
2843 if (im6o
->im6o_mfilters
!= NULL
) {
2844 FREE(im6o
->im6o_mfilters
, M_IN6MFILTER
);
2845 im6o
->im6o_mfilters
= NULL
;
2847 if (im6o
->im6o_membership
!= NULL
) {
2848 FREE(im6o
->im6o_membership
, M_IP6MOPTS
);
2849 im6o
->im6o_membership
= NULL
;
2853 lck_mtx_destroy(&im6o
->im6o_lock
, ifa_mtx_grp
);
2855 if (!(im6o
->im6o_debug
& IFD_ALLOC
)) {
2856 panic("%s: im6o %p cannot be freed", __func__
, im6o
);
2859 zfree(im6o_zone
, im6o
);
2863 im6o_trace(struct ip6_moptions
*im6o
, int refhold
)
2865 struct ip6_moptions_dbg
*im6o_dbg
= (struct ip6_moptions_dbg
*)im6o
;
2870 if (!(im6o
->im6o_debug
& IFD_DEBUG
)) {
2871 panic("%s: im6o %p has no debug structure", __func__
, im6o
);
2875 cnt
= &im6o_dbg
->im6o_refhold_cnt
;
2876 tr
= im6o_dbg
->im6o_refhold
;
2878 cnt
= &im6o_dbg
->im6o_refrele_cnt
;
2879 tr
= im6o_dbg
->im6o_refrele
;
2882 idx
= atomic_add_16_ov(cnt
, 1) % IM6O_TRACE_HIST_SIZE
;
2883 ctrace_record(&tr
[idx
]);
2886 struct ip6_moptions
*
2887 ip6_allocmoptions(int how
)
2889 struct ip6_moptions
*im6o
;
2891 im6o
= (how
== M_WAITOK
) ?
2892 zalloc(im6o_zone
) : zalloc_noblock(im6o_zone
);
2894 bzero(im6o
, im6o_size
);
2895 lck_mtx_init(&im6o
->im6o_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2896 im6o
->im6o_debug
|= IFD_ALLOC
;
2897 if (im6o_debug
!= 0) {
2898 im6o
->im6o_debug
|= IFD_DEBUG
;
2899 im6o
->im6o_trace
= im6o_trace
;
2908 * Set IPv6 outgoing packet options based on advanced API.
2911 ip6_setpktopts(struct mbuf
*control
, struct ip6_pktopts
*opt
,
2912 struct ip6_pktopts
*stickyopt
, int uproto
)
2914 struct cmsghdr
*cm
= 0;
2916 if (control
== NULL
|| opt
== NULL
)
2919 ip6_initpktopts(opt
);
2924 * If stickyopt is provided, make a local copy of the options
2925 * for this particular packet, then override them by ancillary
2927 * XXX: copypktopts() does not copy the cached route to a next
2928 * hop (if any). This is not very good in terms of efficiency,
2929 * but we can allow this since this option should be rarely
2932 if ((error
= copypktopts(opt
, stickyopt
, M_NOWAIT
)) != 0)
2937 * XXX: Currently, we assume all the optional information is stored
2940 if (control
->m_next
)
2943 if (control
->m_len
< CMSG_LEN(0))
2946 for (cm
= M_FIRST_CMSGHDR(control
); cm
; cm
= M_NXT_CMSGHDR(control
, cm
)) {
2949 if (cm
->cmsg_len
< sizeof(struct cmsghdr
) || cm
->cmsg_len
> control
->m_len
)
2951 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
2954 error
= ip6_setpktopt(cm
->cmsg_type
, CMSG_DATA(cm
),
2955 cm
->cmsg_len
- CMSG_LEN(0), opt
, 0, 1, uproto
);
2963 * Set a particular packet option, as a sticky option or an ancillary data
2964 * item. "len" can be 0 only when it's a sticky option.
2965 * We have 4 cases of combination of "sticky" and "cmsg":
2966 * "sticky=0, cmsg=0": impossible
2967 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2968 * "sticky=1, cmsg=0": RFC3542 socket option
2969 * "sticky=1, cmsg=1": RFC2292 socket option
2972 ip6_setpktopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
*opt
,
2973 int sticky
, int cmsg
, int uproto
)
2975 int minmtupolicy
, preftemp
;
2978 if (!sticky
&& !cmsg
) {
2980 printf("ip6_setpktopt: impossible case\n");
2986 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2987 * not be specified in the context of RFC3542. Conversely,
2988 * RFC3542 types should not be specified in the context of RFC2292.
2992 case IPV6_2292PKTINFO
:
2993 case IPV6_2292HOPLIMIT
:
2994 case IPV6_2292NEXTHOP
:
2995 case IPV6_2292HOPOPTS
:
2996 case IPV6_2292DSTOPTS
:
2997 case IPV6_2292RTHDR
:
2998 case IPV6_2292PKTOPTIONS
:
2999 return (ENOPROTOOPT
);
3002 if (sticky
&& cmsg
) {
3009 case IPV6_RTHDRDSTOPTS
:
3011 case IPV6_USE_MIN_MTU
:
3014 case IPV6_PREFER_TEMPADDR
: /* XXX: not an RFC3542 option */
3015 return (ENOPROTOOPT
);
3020 case IPV6_2292PKTINFO
:
3023 struct ifnet
*ifp
= NULL
;
3024 struct in6_pktinfo
*pktinfo
;
3026 if (len
!= sizeof(struct in6_pktinfo
))
3029 pktinfo
= (struct in6_pktinfo
*)buf
;
3032 * An application can clear any sticky IPV6_PKTINFO option by
3033 * doing a "regular" setsockopt with ipi6_addr being
3034 * in6addr_any and ipi6_ifindex being zero.
3035 * [RFC 3542, Section 6]
3037 if (optname
== IPV6_PKTINFO
&& opt
->ip6po_pktinfo
&&
3038 pktinfo
->ipi6_ifindex
== 0 &&
3039 IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
3040 ip6_clearpktopts(opt
, optname
);
3044 if (uproto
== IPPROTO_TCP
&& optname
== IPV6_PKTINFO
&&
3045 sticky
&& !IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
3049 /* validate the interface index if specified. */
3050 ifnet_head_lock_shared();
3052 if (pktinfo
->ipi6_ifindex
> if_index
) {
3057 if (pktinfo
->ipi6_ifindex
) {
3058 ifp
= ifindex2ifnet
[pktinfo
->ipi6_ifindex
];
3068 * We store the address anyway, and let in6_selectsrc()
3069 * validate the specified address. This is because ipi6_addr
3070 * may not have enough information about its scope zone, and
3071 * we may need additional information (such as outgoing
3072 * interface or the scope zone of a destination address) to
3073 * disambiguate the scope.
3074 * XXX: the delay of the validation may confuse the
3075 * application when it is used as a sticky option.
3077 if (opt
->ip6po_pktinfo
== NULL
) {
3078 opt
->ip6po_pktinfo
= _MALLOC(sizeof(*pktinfo
),
3079 M_IP6OPT
, M_NOWAIT
);
3080 if (opt
->ip6po_pktinfo
== NULL
)
3083 bcopy(pktinfo
, opt
->ip6po_pktinfo
, sizeof(*pktinfo
));
3087 case IPV6_2292HOPLIMIT
:
3093 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3094 * to simplify the ordering among hoplimit options.
3096 if (optname
== IPV6_HOPLIMIT
&& sticky
)
3097 return (ENOPROTOOPT
);
3099 if (len
!= sizeof(int))
3102 if (*hlimp
< -1 || *hlimp
> 255)
3105 opt
->ip6po_hlim
= *hlimp
;
3113 if (len
!= sizeof(int))
3115 tclass
= *(int *)buf
;
3116 if (tclass
< -1 || tclass
> 255)
3119 opt
->ip6po_tclass
= tclass
;
3123 case IPV6_2292NEXTHOP
:
3125 error
= suser(kauth_cred_get(), 0);
3129 if (len
== 0) { /* just remove the option */
3130 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3134 /* check if cmsg_len is large enough for sa_len */
3135 if (len
< sizeof(struct sockaddr
) || len
< *buf
)
3138 switch (((struct sockaddr
*)buf
)->sa_family
) {
3141 struct sockaddr_in6
*sa6
= (struct sockaddr_in6
*)buf
;
3143 if (sa6
->sin6_len
!= sizeof(struct sockaddr_in6
))
3146 if (IN6_IS_ADDR_UNSPECIFIED(&sa6
->sin6_addr
) ||
3147 IN6_IS_ADDR_MULTICAST(&sa6
->sin6_addr
)) {
3150 if ((error
= sa6_embedscope(sa6
, ip6_use_defzone
))
3156 case AF_LINK
: /* should eventually be supported */
3158 return (EAFNOSUPPORT
);
3161 /* turn off the previous option, then set the new option. */
3162 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3163 opt
->ip6po_nexthop
= _MALLOC(*buf
, M_IP6OPT
, M_NOWAIT
);
3164 if (opt
->ip6po_nexthop
== NULL
)
3166 bcopy(buf
, opt
->ip6po_nexthop
, *buf
);
3169 case IPV6_2292HOPOPTS
:
3172 struct ip6_hbh
*hbh
;
3176 * XXX: We don't allow a non-privileged user to set ANY HbH
3177 * options, since per-option restriction has too much
3180 error
= suser(kauth_cred_get(), 0);
3185 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3186 break; /* just remove the option */
3189 /* message length validation */
3190 if (len
< sizeof(struct ip6_hbh
))
3192 hbh
= (struct ip6_hbh
*)buf
;
3193 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
3197 /* turn off the previous option, then set the new option. */
3198 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3199 opt
->ip6po_hbh
= _MALLOC(hbhlen
, M_IP6OPT
, M_NOWAIT
);
3200 if (opt
->ip6po_hbh
== NULL
)
3202 bcopy(hbh
, opt
->ip6po_hbh
, hbhlen
);
3207 case IPV6_2292DSTOPTS
:
3209 case IPV6_RTHDRDSTOPTS
:
3211 struct ip6_dest
*dest
, **newdest
= NULL
;
3214 error
= suser(kauth_cred_get(), 0);
3219 ip6_clearpktopts(opt
, optname
);
3220 break; /* just remove the option */
3223 /* message length validation */
3224 if (len
< sizeof(struct ip6_dest
))
3226 dest
= (struct ip6_dest
*)buf
;
3227 destlen
= (dest
->ip6d_len
+ 1) << 3;
3232 * Determine the position that the destination options header
3233 * should be inserted; before or after the routing header.
3236 case IPV6_2292DSTOPTS
:
3238 * The old advacned API is ambiguous on this point.
3239 * Our approach is to determine the position based
3240 * according to the existence of a routing header.
3241 * Note, however, that this depends on the order of the
3242 * extension headers in the ancillary data; the 1st
3243 * part of the destination options header must appear
3244 * before the routing header in the ancillary data,
3246 * RFC3542 solved the ambiguity by introducing
3247 * separate ancillary data or option types.
3249 if (opt
->ip6po_rthdr
== NULL
)
3250 newdest
= &opt
->ip6po_dest1
;
3252 newdest
= &opt
->ip6po_dest2
;
3254 case IPV6_RTHDRDSTOPTS
:
3255 newdest
= &opt
->ip6po_dest1
;
3258 newdest
= &opt
->ip6po_dest2
;
3262 /* turn off the previous option, then set the new option. */
3263 ip6_clearpktopts(opt
, optname
);
3264 *newdest
= _MALLOC(destlen
, M_IP6OPT
, M_NOWAIT
);
3265 if (*newdest
== NULL
)
3267 bcopy(dest
, *newdest
, destlen
);
3272 case IPV6_2292RTHDR
:
3275 struct ip6_rthdr
*rth
;
3279 ip6_clearpktopts(opt
, IPV6_RTHDR
);
3280 break; /* just remove the option */
3283 /* message length validation */
3284 if (len
< sizeof(struct ip6_rthdr
))
3286 rth
= (struct ip6_rthdr
*)buf
;
3287 rthlen
= (rth
->ip6r_len
+ 1) << 3;
3291 switch (rth
->ip6r_type
) {
3292 case IPV6_RTHDR_TYPE_0
:
3293 if (rth
->ip6r_len
== 0) /* must contain one addr */
3295 if (rth
->ip6r_len
% 2) /* length must be even */
3297 if (rth
->ip6r_len
/ 2 != rth
->ip6r_segleft
)
3301 return (EINVAL
); /* not supported */
3304 /* turn off the previous option */
3305 ip6_clearpktopts(opt
, IPV6_RTHDR
);
3306 opt
->ip6po_rthdr
= _MALLOC(rthlen
, M_IP6OPT
, M_NOWAIT
);
3307 if (opt
->ip6po_rthdr
== NULL
)
3309 bcopy(rth
, opt
->ip6po_rthdr
, rthlen
);
3314 case IPV6_USE_MIN_MTU
:
3315 if (len
!= sizeof(int))
3317 minmtupolicy
= *(int *)buf
;
3318 if (minmtupolicy
!= IP6PO_MINMTU_MCASTONLY
&&
3319 minmtupolicy
!= IP6PO_MINMTU_DISABLE
&&
3320 minmtupolicy
!= IP6PO_MINMTU_ALL
) {
3323 opt
->ip6po_minmtu
= minmtupolicy
;
3327 if (len
!= sizeof(int))
3330 if (uproto
== IPPROTO_TCP
|| *(int *)buf
== 0) {
3332 * we ignore this option for TCP sockets.
3333 * (RFC3542 leaves this case unspecified.)
3335 opt
->ip6po_flags
&= ~IP6PO_DONTFRAG
;
3337 opt
->ip6po_flags
|= IP6PO_DONTFRAG
;
3340 case IPV6_PREFER_TEMPADDR
:
3341 if (len
!= sizeof(int))
3343 preftemp
= *(int *)buf
;
3344 if (preftemp
!= IP6PO_TEMPADDR_SYSTEM
&&
3345 preftemp
!= IP6PO_TEMPADDR_NOTPREFER
&&
3346 preftemp
!= IP6PO_TEMPADDR_PREFER
) {
3349 opt
->ip6po_prefer_tempaddr
= preftemp
;
3353 return (ENOPROTOOPT
);
3354 } /* end of switch */
3360 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3361 * packet to the input queue of a specified interface. Note that this
3362 * calls the output routine of the loopback "driver", but with an interface
3363 * pointer that might NOT be &loif -- easier than replicating that code here.
3369 struct sockaddr_in6
*dst
)
3372 struct ip6_hdr
*ip6
;
3374 copym
= m_copy(m
, 0, M_COPYALL
);
3379 * Make sure to deep-copy IPv6 header portion in case the data
3380 * is in an mbuf cluster, so that we can safely override the IPv6
3381 * header portion later.
3383 if ((copym
->m_flags
& M_EXT
) != 0 ||
3384 copym
->m_len
< sizeof(struct ip6_hdr
)) {
3385 copym
= m_pullup(copym
, sizeof(struct ip6_hdr
));
3391 if (copym
->m_len
< sizeof(*ip6
)) {
3397 ip6
= mtod(copym
, struct ip6_hdr
*);
3399 * clear embedded scope identifiers if necessary.
3400 * in6_clearscope will touch the addresses only when necessary.
3402 in6_clearscope(&ip6
->ip6_src
);
3403 in6_clearscope(&ip6
->ip6_dst
);
3407 /* Makes sure the HW checksum flags are cleaned before sending the packet */
3409 if ((copym
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
) != 0) {
3410 in6_delayed_cksum(copym
, sizeof(struct ip6_hdr
));
3411 copym
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_IPV6_DATA
;
3413 copym
->m_pkthdr
.rcvif
= 0;
3414 copym
->m_pkthdr
.csum_data
= 0;
3415 copym
->m_pkthdr
.csum_flags
= 0;
3418 copym
->m_pkthdr
.rcvif
= ifp
;
3419 dlil_output(lo_ifp
, PF_INET6
, copym
, 0, (struct sockaddr
*)dst
, 0);
3423 (void)if_simloop(ifp
, copym
, dst
->sin6_family
, NULL
);
3428 * Chop IPv6 header off from the payload.
3431 ip6_splithdr(m
, exthdrs
)
3433 struct ip6_exthdrs
*exthdrs
;
3436 struct ip6_hdr
*ip6
;
3438 ip6
= mtod(m
, struct ip6_hdr
*);
3439 if (m
->m_len
> sizeof(*ip6
)) {
3440 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
3445 M_COPY_PKTHDR(mh
, m
);
3446 MH_ALIGN(mh
, sizeof(*ip6
));
3447 m
->m_flags
&= ~M_PKTHDR
;
3448 m
->m_len
-= sizeof(*ip6
);
3449 m
->m_data
+= sizeof(*ip6
);
3452 m
->m_len
= sizeof(*ip6
);
3453 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), sizeof(*ip6
));
3455 exthdrs
->ip6e_ip6
= m
;
3460 * Compute IPv6 extension header length.
3464 struct in6pcb
*in6p
;
3468 if (!in6p
->in6p_outputopts
)
3473 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3475 len
+= elen(in6p
->in6p_outputopts
->ip6po_hbh
);
3476 if (in6p
->in6p_outputopts
->ip6po_rthdr
)
3477 /* dest1 is valid with rthdr only */
3478 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest1
);
3479 len
+= elen(in6p
->in6p_outputopts
->ip6po_rthdr
);
3480 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest2
);