2 * Copyright (c) 2000-2020 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/icmp6.c,v 1.6.2.6 2001/07/10 09:44:16 ume Exp $ */
30 /* $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun 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, 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_icmp.c 8.2 (Berkeley) 1/4/94
97 #include <sys/param.h>
98 #include <sys/systm.h>
100 #include <sys/malloc.h>
101 #include <sys/mcache.h>
102 #include <sys/mbuf.h>
103 #include <sys/protosw.h>
104 #include <sys/socket.h>
105 #include <sys/socketvar.h>
106 #include <sys/time.h>
107 #include <sys/kernel.h>
108 #include <sys/syslog.h>
109 #include <sys/domain.h>
110 #include <sys/kauth.h>
113 #include <net/route.h>
114 #include <net/if_dl.h>
115 #include <net/if_types.h>
117 #include <netinet/in.h>
118 #include <netinet/in_var.h>
119 #include <netinet/ip6.h>
120 #include <netinet6/ip6_var.h>
121 #include <netinet/icmp6.h>
122 #include <netinet6/mld6_var.h>
123 #include <netinet/in_pcb.h>
124 #include <netinet6/in6_pcb.h>
125 #include <netinet6/in6_var.h>
126 #include <netinet6/nd6.h>
127 #include <netinet6/in6_ifattach.h>
128 #include <netinet6/ip6protosw.h>
129 #include <netinet6/scope6_var.h>
132 #include <netinet6/ipsec.h>
133 #include <netkey/key.h>
136 #include <net/net_osdep.h>
139 #include <net/necp.h>
142 extern struct ip6protosw
*ip6_protox
[];
144 extern uint32_t rip_sendspace
;
145 extern uint32_t rip_recvspace
;
147 struct icmp6stat icmp6stat
;
149 extern struct inpcbhead ripcb
;
150 extern int icmp6errppslim
;
151 extern int icmp6errppslim_random_incr
;
152 extern int icmp6rappslim
;
153 static int icmp6errpps_count
= 0;
154 static int icmp6rapps_count
= 0;
155 static struct timeval icmp6errppslim_last
;
156 static struct timeval icmp6rappslim_last
;
157 extern int icmp6_nodeinfo
;
158 extern struct inpcbinfo ripcbinfo
;
160 static void icmp6_errcount(struct icmp6errstat
*, int, int);
161 static int icmp6_rip6_input(struct mbuf
**, int);
162 static int icmp6_ratelimit(const struct in6_addr
*, const int, const int);
163 static const char *icmp6_redirect_diag(struct in6_addr
*,
164 struct in6_addr
*, struct in6_addr
*);
165 static struct mbuf
*ni6_input(struct mbuf
*, int);
166 static struct mbuf
*ni6_nametodns(const char *, uint32_t, int);
167 static int ni6_dnsmatch(const char *, int, const char *, int);
168 static int ni6_addrs(struct icmp6_nodeinfo
*,
169 struct ifnet
**, char *);
170 static int ni6_store_addrs(struct icmp6_nodeinfo
*, struct icmp6_nodeinfo
*,
171 struct ifnet
*, int);
172 static int icmp6_notify_error(struct mbuf
*, int, int, int);
177 icmp6_init(struct ip6protosw
*pp
, struct domain
*dp
)
180 static int icmp6_initialized
= 0;
182 /* Also called from ip6_init() without pp */
184 (pp
->pr_flags
& (PR_INITIALIZED
| PR_ATTACHED
)) == PR_ATTACHED
);
186 /* This gets called by more than one protocols, so initialize once */
187 if (!icmp6_initialized
) {
188 icmp6_initialized
= 1;
190 if (icmp6errppslim
>= 0 &&
191 icmp6errppslim_random_incr
> 0 &&
192 icmp6errppslim
<= INT32_MAX
- (icmp6errppslim_random_incr
+ 1)) {
193 icmp6errppslim
+= (random() % icmp6errppslim_random_incr
) + 1;
199 icmp6_errcount(struct icmp6errstat
*stat
, int type
, int code
)
202 case ICMP6_DST_UNREACH
:
204 case ICMP6_DST_UNREACH_NOROUTE
:
205 stat
->icp6errs_dst_unreach_noroute
++;
207 case ICMP6_DST_UNREACH_ADMIN
:
208 stat
->icp6errs_dst_unreach_admin
++;
210 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
211 stat
->icp6errs_dst_unreach_beyondscope
++;
213 case ICMP6_DST_UNREACH_ADDR
:
214 stat
->icp6errs_dst_unreach_addr
++;
216 case ICMP6_DST_UNREACH_NOPORT
:
217 stat
->icp6errs_dst_unreach_noport
++;
221 case ICMP6_PACKET_TOO_BIG
:
222 stat
->icp6errs_packet_too_big
++;
224 case ICMP6_TIME_EXCEEDED
:
226 case ICMP6_TIME_EXCEED_TRANSIT
:
227 stat
->icp6errs_time_exceed_transit
++;
229 case ICMP6_TIME_EXCEED_REASSEMBLY
:
230 stat
->icp6errs_time_exceed_reassembly
++;
234 case ICMP6_PARAM_PROB
:
236 case ICMP6_PARAMPROB_HEADER
:
237 stat
->icp6errs_paramprob_header
++;
239 case ICMP6_PARAMPROB_NEXTHEADER
:
240 stat
->icp6errs_paramprob_nextheader
++;
242 case ICMP6_PARAMPROB_OPTION
:
243 stat
->icp6errs_paramprob_option
++;
248 stat
->icp6errs_redirect
++;
251 stat
->icp6errs_unknown
++;
255 * A wrapper function for icmp6_error() necessary when the erroneous packet
256 * may not contain enough scope zone information.
259 icmp6_error2(struct mbuf
*m
, int type
, int code
, int param
,
268 #ifndef PULLDOWN_TEST
269 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
), return );
271 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
272 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
279 ip6
= mtod(m
, struct ip6_hdr
*);
281 if (in6_setscope(&ip6
->ip6_src
, ifp
, NULL
) != 0) {
284 if (in6_setscope(&ip6
->ip6_dst
, ifp
, NULL
) != 0) {
288 icmp6_error(m
, type
, code
, param
);
292 * Generate an error packet of type error in response to bad IP6 packet.
295 icmp6_error(struct mbuf
*m
, int type
, int code
, int param
)
297 icmp6_error_flag(m
, type
, code
, param
, ICMP6_ERROR_RST_MRCVIF
);
301 icmp6_error_flag(struct mbuf
*m
, int type
, int code
, int param
, int flags
)
303 struct ip6_hdr
*oip6
, *nip6
;
304 struct icmp6_hdr
*icmp6
;
308 icmp6stat
.icp6s_error
++;
310 /* count per-type-code statistics */
311 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, type
, code
);
313 #ifdef M_DECRYPTED /*not openbsd*/
314 if (m
->m_flags
& M_DECRYPTED
) {
315 icmp6stat
.icp6s_canterror
++;
320 #ifndef PULLDOWN_TEST
321 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
), return );
323 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
324 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
330 oip6
= mtod(m
, struct ip6_hdr
*);
333 * If the destination address of the erroneous packet is a multicast
334 * address, or the packet was sent using link-layer multicast,
335 * we should basically suppress sending an error (RFC 2463, Section
337 * We have two exceptions (the item e.2 in that section):
338 * - the Pakcet Too Big message can be sent for path MTU discovery.
339 * - the Parameter Problem Message that can be allowed an icmp6 error
340 * in the option type field. This check has been done in
341 * ip6_unknown_opt(), so we can just check the type and code.
343 if ((m
->m_flags
& (M_BCAST
| M_MCAST
) ||
344 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_dst
)) &&
345 (type
!= ICMP6_PACKET_TOO_BIG
&&
346 (type
!= ICMP6_PARAM_PROB
||
347 code
!= ICMP6_PARAMPROB_OPTION
))) {
352 * RFC 2463, 2.4 (e.5): source address check.
353 * XXX: the case of anycast source?
355 if (IN6_IS_ADDR_UNSPECIFIED(&oip6
->ip6_src
) ||
356 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_src
)) {
361 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
364 * We want to check for that for all ICMP error types, other than
365 * ICMP6_PARAM_PROB when it is being sent in response of first frag
366 * with incomplete header.
367 * That also includes the case when the first frag has incomplete ICMPv6
368 * header. The check below in that case would fail the IP6_EXTHDR_CHECK
369 * and would otherwise prevent us from sending the error back.
371 if (type
!= ICMP6_PARAM_PROB
||
372 code
!= ICMP6_PARAMPROB_FIRSTFRAG_INCOMP_HDR
) {
374 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
375 if (off
>= 0 && nxt
== IPPROTO_ICMPV6
) {
376 struct icmp6_hdr
*icp
;
378 #ifndef PULLDOWN_TEST
379 IP6_EXTHDR_CHECK(m
, 0, off
+ sizeof(struct icmp6_hdr
), return );
380 icp
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
382 IP6_EXTHDR_GET(icp
, struct icmp6_hdr
*, m
, off
,
385 icmp6stat
.icp6s_tooshort
++;
389 if (icp
->icmp6_type
< ICMP6_ECHO_REQUEST
||
390 icp
->icmp6_type
== ND_REDIRECT
) {
393 * Special case: for redirect (which is
394 * informational) we must not send icmp6 error.
396 icmp6stat
.icp6s_canterror
++;
399 /* ICMPv6 informational - send the error */
402 /* non-ICMPv6 - send the error */
406 oip6
= mtod(m
, struct ip6_hdr
*); /* adjust pointer */
408 /* Finally, do rate limitation check. */
409 if (icmp6_ratelimit(&oip6
->ip6_src
, type
, code
)) {
410 icmp6stat
.icp6s_toofreq
++;
415 * OK, ICMP6 can be generated.
418 if (m
->m_pkthdr
.len
>= ICMPV6_PLD_MAXLEN
) {
419 m_adj(m
, ICMPV6_PLD_MAXLEN
- m
->m_pkthdr
.len
);
422 preplen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
423 M_PREPEND(m
, preplen
, M_DONTWAIT
, 1);
424 if (m
&& m
->m_len
< preplen
) {
425 m
= m_pullup(m
, preplen
);
428 nd6log(debug
, "ENOBUFS in icmp6_error %d\n", __LINE__
);
432 nip6
= mtod(m
, struct ip6_hdr
*);
433 nip6
->ip6_src
= oip6
->ip6_src
;
434 nip6
->ip6_dst
= oip6
->ip6_dst
;
436 in6_clearscope(&oip6
->ip6_src
);
437 in6_clearscope(&oip6
->ip6_dst
);
439 icmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
440 icmp6
->icmp6_type
= (uint8_t)type
;
441 icmp6
->icmp6_code
= (uint8_t)code
;
442 icmp6
->icmp6_pptr
= htonl((u_int32_t
)param
);
445 * icmp6_reflect() is designed to be in the input path.
446 * icmp6_error() can be called from both input and output path,
447 * and if we are in output path rcvif could contain bogus value.
448 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
449 * information in ip header (nip6).
451 if (flags
& ICMP6_ERROR_RST_MRCVIF
) {
452 m
->m_pkthdr
.rcvif
= NULL
;
455 icmp6stat
.icp6s_outhist
[type
]++;
456 icmp6_reflect(m
, sizeof(struct ip6_hdr
)); /* header order: IPv6 - ICMPv6 */
462 * If we can't tell whether or not we can generate ICMP6, free it.
468 * Process a received ICMP6 message.
471 icmp6_input(struct mbuf
**mp
, int *offp
, int proto
)
473 #pragma unused(proto)
474 struct mbuf
*m
= *mp
, *n
;
476 struct ip6_hdr
*ip6
, *nip6
;
477 struct icmp6_hdr
*icmp6
, *nicmp6
;
479 int icmp6len
= m
->m_pkthdr
.len
- *offp
;
480 int code
, sum
, noff
, proxy
= 0;
482 ifp
= m
->m_pkthdr
.rcvif
;
484 #ifndef PULLDOWN_TEST
485 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_hdr
), return IPPROTO_DONE
);
486 /* m might change if M_LOOP. So, call mtod after this */
489 /* Expect 32-bit aligned data pointer on strict-align platforms */
490 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
493 * Locate icmp6 structure in mbuf, and check
494 * that not corrupted and of at least minimum length
496 ip6
= mtod(m
, struct ip6_hdr
*);
497 if (icmp6len
< sizeof(struct icmp6_hdr
)) {
498 icmp6stat
.icp6s_tooshort
++;
502 #ifndef PULLDOWN_TEST
503 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
505 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
507 icmp6stat
.icp6s_tooshort
++;
511 code
= icmp6
->icmp6_code
;
514 * Early check for RFC 6980
515 * Drop certain NDP packets if they came in fragmented
517 switch (icmp6
->icmp6_type
) {
518 case ND_ROUTER_SOLICIT
:
519 case ND_ROUTER_ADVERT
:
520 case ND_NEIGHBOR_SOLICIT
:
521 case ND_NEIGHBOR_ADVERT
:
523 if (m
->m_pkthdr
.pkt_flags
& PKTF_REASSEMBLED
) {
524 icmp6stat
.icp6s_rfc6980_drop
++;
532 /* Apply rate limit before checksum validation. */
533 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
534 icmp6stat
.icp6s_toofreq
++;
539 * Check multicast group membership.
540 * Note: SSM filters are not applied for ICMPv6 traffic.
542 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
543 struct in6_multi
*inm
;
545 in6_multihead_lock_shared();
546 IN6_LOOKUP_MULTI(&ip6
->ip6_dst
, ifp
, inm
);
547 in6_multihead_lock_done();
551 * Don't discard if this is a Neighbor Solicitation
552 * that needs to be proxied (see check down below.)
554 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_PROXY_DST
)) {
555 ip6stat
.ip6s_notmember
++;
556 in6_ifstat_inc(m
->m_pkthdr
.rcvif
,
566 * calculate the checksum
568 if ((sum
= in6_cksum(m
, IPPROTO_ICMPV6
, off
, icmp6len
)) != 0) {
570 "ICMP6 checksum error(%d|%x) %s\n",
571 icmp6
->icmp6_type
, sum
, ip6_sprintf(&ip6
->ip6_src
));
572 icmp6stat
.icp6s_checksum
++;
576 if (m
->m_pkthdr
.pkt_flags
& PKTF_PROXY_DST
) {
578 * This is the special case of proxying NS (dst is either
579 * solicited-node multicast or unicast); process it locally
580 * but don't deliver it to sockets. It practically lets us
581 * steer the packet to nd6_prproxy_ns_input, where more
582 * specific tests and actions will be taken.
584 switch (icmp6
->icmp6_type
) {
585 case ND_NEIGHBOR_SOLICIT
:
593 icmp6stat
.icp6s_inhist
[icmp6
->icmp6_type
]++;
594 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_msg
);
595 if (icmp6
->icmp6_type
< ICMP6_INFOMSG_MASK
) {
596 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_error
);
599 switch (icmp6
->icmp6_type
) {
600 case ICMP6_DST_UNREACH
:
601 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_dstunreach
);
603 case ICMP6_DST_UNREACH_NOROUTE
:
604 case ICMP6_DST_UNREACH_ADDR
: /* PRC_HOSTDEAD is a DOS */
605 code
= PRC_UNREACH_NET
;
607 case ICMP6_DST_UNREACH_ADMIN
:
608 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_adminprohib
);
609 code
= PRC_UNREACH_PROTOCOL
; /* is this a good code? */
611 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
612 /* I mean "source address was incorrect." */
613 code
= PRC_PARAMPROB
;
615 case ICMP6_DST_UNREACH_NOPORT
:
616 code
= PRC_UNREACH_PORT
;
623 case ICMP6_PACKET_TOO_BIG
:
624 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_pkttoobig
);
625 if (ntohl(icmp6
->icmp6_mtu
) < IPV6_MMTU
) {
626 icmp6stat
.icp6s_badpkttoobig
++;
633 * Updating the path MTU will be done after examining
634 * intermediate extension headers.
638 case ICMP6_TIME_EXCEEDED
:
639 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_timeexceed
);
641 case ICMP6_TIME_EXCEED_TRANSIT
:
642 code
= PRC_TIMXCEED_INTRANS
;
644 case ICMP6_TIME_EXCEED_REASSEMBLY
:
645 code
= PRC_TIMXCEED_REASS
;
652 case ICMP6_PARAM_PROB
:
653 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_paramprob
);
655 case ICMP6_PARAMPROB_NEXTHEADER
:
656 code
= PRC_UNREACH_PROTOCOL
;
658 case ICMP6_PARAMPROB_HEADER
:
659 case ICMP6_PARAMPROB_OPTION
:
660 code
= PRC_PARAMPROB
;
667 case ICMP6_ECHO_REQUEST
:
668 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echo
);
673 if ((n
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
675 goto rate_limit_checked
;
677 if ((n
->m_flags
& M_EXT
) != 0
678 || n
->m_len
< off
+ sizeof(struct icmp6_hdr
)) {
680 const int maxlen
= sizeof(*nip6
) + sizeof(*nicmp6
);
683 * Prepare an internal mbuf. m_pullup() doesn't
684 * always copy the length we specified.
686 if (maxlen
>= MCLBYTES
) {
689 goto rate_limit_checked
;
691 MGETHDR(n
, M_DONTWAIT
, n0
->m_type
); /* MAC-OK */
692 if (n
&& maxlen
>= MHLEN
) {
693 MCLGET(n
, M_DONTWAIT
);
694 if ((n
->m_flags
& M_EXT
) == 0) {
702 goto rate_limit_checked
;
704 M_COPY_PKTHDR(n
, n0
);
706 * Copy IPv6 and ICMPv6 only.
708 nip6
= mtod(n
, struct ip6_hdr
*);
709 bcopy(ip6
, nip6
, sizeof(struct ip6_hdr
));
710 nicmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
711 bcopy(icmp6
, nicmp6
, sizeof(struct icmp6_hdr
));
712 noff
= sizeof(struct ip6_hdr
);
713 n
->m_pkthdr
.len
= n
->m_len
=
714 noff
+ sizeof(struct icmp6_hdr
);
716 * Adjust mbuf. ip6_plen will be adjusted in
719 m_adj(n0
, off
+ sizeof(struct icmp6_hdr
));
720 n
->m_pkthdr
.len
+= n0
->m_pkthdr
.len
;
722 n0
->m_flags
&= ~M_PKTHDR
;
724 nip6
= mtod(n
, struct ip6_hdr
*);
725 IP6_EXTHDR_GET(nicmp6
, struct icmp6_hdr
*, n
, off
,
729 if (nicmp6
== NULL
) {
730 panic("nicmp6 is NULL in %s, which isn't good!\n", __FUNCTION__
);
732 nicmp6
->icmp6_type
= ICMP6_ECHO_REPLY
;
733 nicmp6
->icmp6_code
= 0;
736 icmp6stat
.icp6s_reflect
++;
737 icmp6stat
.icp6s_outhist
[ICMP6_ECHO_REPLY
]++;
738 icmp6_reflect(n
, noff
);
740 goto rate_limit_checked
;
742 case ICMP6_ECHO_REPLY
:
743 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echoreply
);
749 case MLD_LISTENER_QUERY
:
750 case MLD_LISTENER_REPORT
:
752 if (icmp6len
< sizeof(struct mld_hdr
)) {
755 if (icmp6
->icmp6_type
== MLD_LISTENER_QUERY
) { /* XXX: ugly... */
756 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldquery
);
758 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldreport
);
761 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
763 if (mld_input(m
, off
, icmp6len
) == IPPROTO_DONE
) {
768 if (mld_input(n
, off
, icmp6len
) != IPPROTO_DONE
) {
772 goto rate_limit_checked
;
774 case MLD_LISTENER_DONE
:
775 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mlddone
);
776 if (icmp6len
< sizeof(struct mld_hdr
)) { /* necessary? */
779 break; /* nothing to be done in kernel */
781 case MLD_MTRACE_RESP
:
783 /* XXX: these two are experimental. not officially defined. */
784 /* XXX: per-interface statistics? */
785 break; /* just pass it to applications */
788 if (!icmp6_nodeinfo
) {
791 //### LD 10/20 Check fbsd differences here. Not sure we're more advanced or not.
792 /* By RFC 4620 refuse to answer queries from global scope addresses */
793 if ((icmp6_nodeinfo
& 8) != 8 && in6_addrscope(&ip6
->ip6_src
) == IPV6_ADDR_SCOPE_GLOBAL
) {
797 if (icmp6len
< sizeof(struct icmp6_nodeinfo
)) {
801 #ifndef PULLDOWN_TEST
802 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_nodeinfo
),
803 return IPPROTO_DONE
);
806 n
= m_copy(m
, 0, M_COPYALL
);
808 n
= ni6_input(n
, off
);
811 noff
= sizeof(struct ip6_hdr
);
812 icmp6stat
.icp6s_reflect
++;
813 icmp6stat
.icp6s_outhist
[ICMP6_WRUREPLY
]++;
814 icmp6_reflect(n
, noff
);
816 goto rate_limit_checked
;
824 case ND_ROUTER_SOLICIT
:
825 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routersolicit
);
829 if (icmp6len
< sizeof(struct nd_router_solicit
)) {
833 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
835 nd6_rs_input(m
, off
, icmp6len
);
839 nd6_rs_input(n
, off
, icmp6len
);
841 goto rate_limit_checked
;
843 case ND_ROUTER_ADVERT
:
844 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routeradvert
);
848 if (icmp6len
< sizeof(struct nd_router_advert
)) {
852 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
854 nd6_ra_input(m
, off
, icmp6len
);
858 nd6_ra_input(n
, off
, icmp6len
);
860 goto rate_limit_checked
;
862 case ND_NEIGHBOR_SOLICIT
:
863 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighborsolicit
);
867 if (icmp6len
< sizeof(struct nd_neighbor_solicit
)) {
872 ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
)) {
874 nd6_ns_input(m
, off
, icmp6len
);
878 nd6_ns_input(n
, off
, icmp6len
);
880 goto rate_limit_checked
;
882 case ND_NEIGHBOR_ADVERT
:
883 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighboradvert
);
887 if (icmp6len
< sizeof(struct nd_neighbor_advert
)) {
891 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
893 nd6_na_input(m
, off
, icmp6len
);
897 nd6_na_input(n
, off
, icmp6len
);
899 goto rate_limit_checked
;
902 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_redirect
);
906 if (icmp6len
< sizeof(struct nd_redirect
)) {
910 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
912 icmp6_redirect_input(m
, off
, icmp6len
);
916 icmp6_redirect_input(n
, off
, icmp6len
);
918 goto rate_limit_checked
;
920 case ICMP6_ROUTER_RENUMBERING
:
921 if (code
!= ICMP6_ROUTER_RENUMBERING_COMMAND
&&
922 code
!= ICMP6_ROUTER_RENUMBERING_RESULT
) {
925 if (icmp6len
< sizeof(struct icmp6_router_renum
)) {
932 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
933 icmp6
->icmp6_type
, ip6_sprintf(&ip6
->ip6_src
),
934 ip6_sprintf(&ip6
->ip6_dst
),
935 m
->m_pkthdr
.rcvif
? m
->m_pkthdr
.rcvif
->if_index
: 0);
936 if (icmp6
->icmp6_type
< ICMP6_ECHO_REQUEST
) {
937 /* ICMPv6 error: MUST deliver it by spec... */
941 /* ICMPv6 informational: MUST not deliver */
942 goto rate_limit_checked
;
945 if (icmp6_notify_error(m
, off
, icmp6len
, code
)) {
946 /* In this case, m should've been freed. */
952 icmp6stat
.icp6s_badcode
++;
956 icmp6stat
.icp6s_badlen
++;
961 icmp6_rip6_input(&m
, *offp
);
970 icmp6_notify_error(struct mbuf
*m
, int off
, int icmp6len
, int code
)
972 struct icmp6_hdr
*icmp6
;
973 struct ip6_hdr
*eip6
;
975 struct sockaddr_in6 icmp6src
, icmp6dst
;
977 if (icmp6len
< sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
)) {
978 icmp6stat
.icp6s_tooshort
++;
981 #ifndef PULLDOWN_TEST
982 IP6_EXTHDR_CHECK(m
, off
,
983 sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
),
985 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
987 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
988 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
990 icmp6stat
.icp6s_tooshort
++;
994 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
995 bzero(&icmp6dst
, sizeof(icmp6dst
));
997 /* Detect the upper level protocol */
999 void (*ctlfunc
)(int, struct sockaddr
*, void *, struct ifnet
*);
1000 u_int8_t nxt
= eip6
->ip6_nxt
;
1001 int eoff
= off
+ sizeof(struct icmp6_hdr
) +
1002 sizeof(struct ip6_hdr
);
1003 struct ip6ctlparam ip6cp
;
1004 int icmp6type
= icmp6
->icmp6_type
;
1005 struct ip6_frag
*fh
;
1006 struct ip6_rthdr
*rth
;
1007 struct ip6_rthdr0
*rth0
;
1010 while (1) { /* XXX: should avoid infinite loop explicitly? */
1014 case IPPROTO_HOPOPTS
:
1015 case IPPROTO_DSTOPTS
:
1017 #ifndef PULLDOWN_TEST
1018 IP6_EXTHDR_CHECK(m
, 0,
1019 eoff
+ sizeof(struct ip6_ext
), return -1);
1020 eh
= (struct ip6_ext
*)(mtod(m
, caddr_t
)
1023 IP6_EXTHDR_GET(eh
, struct ip6_ext
*, m
,
1026 icmp6stat
.icp6s_tooshort
++;
1031 if (nxt
== IPPROTO_AH
) {
1032 eoff
+= (eh
->ip6e_len
+ 2) << 2;
1034 eoff
+= (eh
->ip6e_len
+ 1) << 3;
1038 case IPPROTO_ROUTING
:
1040 * When the erroneous packet contains a
1041 * routing header, we should examine the
1042 * header to determine the final destination.
1043 * Otherwise, we can't properly update
1044 * information that depends on the final
1045 * destination (e.g. path MTU).
1047 #ifndef PULLDOWN_TEST
1048 IP6_EXTHDR_CHECK(m
, 0, eoff
+ sizeof(*rth
),
1050 rth
= (struct ip6_rthdr
*)
1051 (mtod(m
, caddr_t
) + eoff
);
1053 IP6_EXTHDR_GET(rth
, struct ip6_rthdr
*, m
,
1054 eoff
, sizeof(*rth
));
1056 icmp6stat
.icp6s_tooshort
++;
1060 rthlen
= (rth
->ip6r_len
+ 1) << 3;
1062 * XXX: currently there is no
1063 * officially defined type other
1065 * Note that if the segment left field
1066 * is 0, all intermediate hops must
1069 if (rth
->ip6r_segleft
&&
1070 rth
->ip6r_type
== IPV6_RTHDR_TYPE_0
) {
1073 #ifndef PULLDOWN_TEST
1074 IP6_EXTHDR_CHECK(m
, 0, eoff
+ rthlen
,
1076 rth0
= (struct ip6_rthdr0
*)(mtod(m
, caddr_t
) + eoff
);
1078 IP6_EXTHDR_GET(rth0
,
1079 struct ip6_rthdr0
*, m
,
1082 icmp6stat
.icp6s_tooshort
++;
1086 /* just ignore a bogus header */
1087 if ((rth0
->ip6r0_len
% 2) == 0 &&
1088 (hops
= rth0
->ip6r0_len
/ 2)) {
1089 icmp6dst
.sin6_addr
= *((struct in6_addr
*)(void *)(rth0
+ 1) + (hops
- 1));
1093 nxt
= rth
->ip6r_nxt
;
1095 case IPPROTO_FRAGMENT
:
1096 #ifndef PULLDOWN_TEST
1097 IP6_EXTHDR_CHECK(m
, 0, eoff
+
1098 sizeof(struct ip6_frag
),
1100 fh
= (struct ip6_frag
*)(mtod(m
, caddr_t
)
1103 IP6_EXTHDR_GET(fh
, struct ip6_frag
*, m
,
1106 icmp6stat
.icp6s_tooshort
++;
1111 * Data after a fragment header is meaningless
1112 * unless it is the first fragment, but
1113 * we'll go to the notify label for path MTU
1116 if (fh
->ip6f_offlg
& IP6F_OFF_MASK
) {
1120 eoff
+= sizeof(struct ip6_frag
);
1125 * This case includes ESP and the No Next
1126 * Header. In such cases going to the notify
1127 * label does not have any meaning
1128 * (i.e. ctlfunc will be NULL), but we go
1129 * anyway since we might have to update
1130 * path MTU information.
1136 #ifndef PULLDOWN_TEST
1137 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
1139 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
1140 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
1141 if (icmp6
== NULL
) {
1142 icmp6stat
.icp6s_tooshort
++;
1148 * retrieve parameters from the inner IPv6 header, and convert
1149 * them into sockaddr structures.
1150 * XXX: there is no guarantee that the source or destination
1151 * addresses of the inner packet are in the same scope as
1152 * the addresses of the icmp packet. But there is no other
1153 * way to determine the zone.
1155 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1157 icmp6dst
.sin6_len
= sizeof(struct sockaddr_in6
);
1158 icmp6dst
.sin6_family
= AF_INET6
;
1159 if (IN6_IS_ADDR_UNSPECIFIED(&icmp6dst
.sin6_addr
)) {
1160 icmp6dst
.sin6_addr
= eip6
->ip6_dst
;
1162 if (in6_setscope(&icmp6dst
.sin6_addr
, m
->m_pkthdr
.rcvif
, NULL
)) {
1165 bzero(&icmp6src
, sizeof(icmp6src
));
1166 icmp6src
.sin6_len
= sizeof(struct sockaddr_in6
);
1167 icmp6src
.sin6_family
= AF_INET6
;
1168 icmp6src
.sin6_addr
= eip6
->ip6_src
;
1169 if (in6_setscope(&icmp6src
.sin6_addr
, m
->m_pkthdr
.rcvif
, NULL
)) {
1172 icmp6src
.sin6_flowinfo
=
1173 (eip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
);
1176 ip6cp
.ip6c_icmp6
= icmp6
;
1177 ip6cp
.ip6c_ip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1178 ip6cp
.ip6c_off
= eoff
;
1179 ip6cp
.ip6c_finaldst
= &icmp6dst
.sin6_addr
;
1180 ip6cp
.ip6c_src
= &icmp6src
;
1181 ip6cp
.ip6c_nxt
= nxt
;
1183 if (icmp6type
== ICMP6_PACKET_TOO_BIG
) {
1184 notifymtu
= ntohl(icmp6
->icmp6_mtu
);
1185 ip6cp
.ip6c_cmdarg
= (void *)¬ifymtu
;
1186 icmp6_mtudisc_update(&ip6cp
, 1); /*XXX*/
1189 ctlfunc
= ip6_protox
[nxt
]->pr_ctlinput
;
1191 LCK_MTX_ASSERT(inet6_domain_mutex
, LCK_MTX_ASSERT_OWNED
);
1193 lck_mtx_unlock(inet6_domain_mutex
);
1195 (void) (*ctlfunc
)(code
, (struct sockaddr
*)&icmp6dst
,
1196 &ip6cp
, m
->m_pkthdr
.rcvif
);
1198 lck_mtx_lock(inet6_domain_mutex
);
1209 icmp6_mtudisc_update(struct ip6ctlparam
*ip6cp
, int validated
)
1211 struct in6_addr
*dst
= ip6cp
->ip6c_finaldst
;
1212 struct icmp6_hdr
*icmp6
= ip6cp
->ip6c_icmp6
;
1213 struct mbuf
*m
= ip6cp
->ip6c_m
; /* will be necessary for scope issue */
1214 u_int mtu
= ntohl(icmp6
->icmp6_mtu
);
1215 struct rtentry
*rt
= NULL
;
1216 struct sockaddr_in6 sin6
;
1218 * we reject ICMPv6 too big with abnormally small value.
1219 * XXX what is the good definition of "abnormally small"?
1221 if (mtu
< sizeof(struct ip6_hdr
) + sizeof(struct ip6_frag
) + 8) {
1229 /* Limit the MTU to the minimum IPv6 MTU */
1230 if (mtu
< IPV6_MMTU
) {
1234 bzero(&sin6
, sizeof(sin6
));
1235 sin6
.sin6_family
= PF_INET6
;
1236 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1237 sin6
.sin6_addr
= *dst
;
1238 /* XXX normally, this won't happen */
1239 if (IN6_IS_ADDR_LINKLOCAL(dst
)) {
1240 sin6
.sin6_addr
.s6_addr16
[1] =
1241 htons(m
->m_pkthdr
.rcvif
->if_index
);
1243 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1245 * XXX On a side note, for asymmetric data-path
1246 * the lookup on receive interace is probably not
1247 * what we want to do.
1248 * That requires looking at the cached route for the
1249 * protocol control block.
1251 rt
= rtalloc1_scoped((struct sockaddr
*)&sin6
, 0,
1252 RTF_CLONING
| RTF_PRCLONING
, m
->m_pkthdr
.rcvif
->if_index
);
1255 if ((rt
->rt_flags
& RTF_HOST
) &&
1256 !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) &&
1257 mtu
< IN6_LINKMTU(rt
->rt_ifp
) &&
1258 rt
->rt_rmx
.rmx_mtu
> mtu
) {
1259 icmp6stat
.icp6s_pmtuchg
++;
1260 rt
->rt_rmx
.rmx_mtu
= mtu
;
1268 * Process a Node Information Query packet, based on
1269 * draft-ietf-ipngwg-icmp-name-lookups-07.
1271 * Spec incompatibilities:
1272 * - IPv6 Subject address handling
1273 * - IPv4 Subject address handling support missing
1274 * - Proxy reply (answer even if it's not for me)
1275 * - joins NI group address at in6_ifattach() time only, does not cope
1276 * with hostname changes by sethostname(3)
1278 #define hostnamelen (uint32_t)strlen(hostname)
1279 static struct mbuf
*
1280 ni6_input(struct mbuf
*m
, int off
)
1282 struct icmp6_nodeinfo
*ni6
, *nni6
;
1283 struct mbuf
*n
= NULL
;
1286 int replylen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1287 struct ni_reply_fqdn
*fqdn
;
1288 int addrs
; /* for NI_QTYPE_NODEADDR */
1289 struct ifnet
*ifp
= NULL
; /* for NI_QTYPE_NODEADDR */
1290 struct sockaddr_in6 sin6
; /* double meaning; ip6_dst and subjectaddr */
1291 struct sockaddr_in6 sin6_d
; /* XXX: we should retrieve this from m_aux */
1292 struct ip6_hdr
*ip6
;
1293 int oldfqdn
= 0; /* if 1, return pascal string (03 draft) */
1296 ip6
= mtod(m
, struct ip6_hdr
*);
1297 #ifndef PULLDOWN_TEST
1298 ni6
= (struct icmp6_nodeinfo
*)(mtod(m
, caddr_t
) + off
);
1300 IP6_EXTHDR_GET(ni6
, struct icmp6_nodeinfo
*, m
, off
, sizeof(*ni6
));
1302 /* m is already reclaimed */
1308 * Validate IPv6 source address.
1309 * The default configuration MUST be to refuse answering queries from
1310 * global-scope addresses according to RFC4602.
1312 * - it's not very clear what "refuse" means; this implementation
1314 * - it's not very easy to identify global-scope (unicast) addresses
1315 * since there are many prefixes for them. It should be safer
1316 * and in practice sufficient to check "all" but loopback and
1317 * link-local (note that site-local unicast was deprecated and
1318 * ULA is defined as global scope-wise)
1320 if ((icmp6_nodeinfo
& ICMP6_NODEINFO_GLOBALOK
) == 0 &&
1321 !IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
) &&
1322 !IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_src
)) {
1327 * Validate IPv6 destination address.
1329 * The Responder must discard the Query without further processing
1330 * unless it is one of the Responder's unicast or anycast addresses, or
1331 * a link-local scope multicast address which the Responder has joined.
1332 * [RFC4602, Section 5.]
1334 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1335 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6
->ip6_dst
) &&
1336 !IN6_IS_ADDR_MC_UNICAST_BASED_LINKLOCAL(&ip6
->ip6_dst
)) {
1339 /* else it's a link-local multicast, fine */
1340 } else { /* unicast or anycast */
1343 if (ip6_getdstifaddr_info(m
, NULL
, &ia6_flags
) != 0) {
1344 goto bad
; /* XXX impossible */
1346 if ((ia6_flags
& IN6_IFF_TEMPORARY
) &&
1347 !(icmp6_nodeinfo
& ICMP6_NODEINFO_TMPADDROK
)) {
1348 nd6log(debug
, "ni6_input: ignore node info to a temporary address in %s:%d",
1349 __func__
, __LINE__
);
1354 /* validate query Subject field. */
1355 qtype
= ntohs(ni6
->ni_qtype
);
1356 subjlen
= m
->m_pkthdr
.len
- off
- sizeof(struct icmp6_nodeinfo
);
1359 case NI_QTYPE_SUPTYPES
:
1361 if (ni6
->ni_code
== ICMP6_NI_SUBJ_FQDN
&& subjlen
== 0) {
1366 case NI_QTYPE_NODEADDR
:
1367 case NI_QTYPE_IPV4ADDR
:
1368 switch (ni6
->ni_code
) {
1369 case ICMP6_NI_SUBJ_IPV6
:
1370 #if ICMP6_NI_SUBJ_IPV6 != 0
1374 * backward compatibility - try to accept 03 draft
1375 * format, where no Subject is present.
1377 if (qtype
== NI_QTYPE_FQDN
&& ni6
->ni_code
== 0 &&
1382 #if ICMP6_NI_SUBJ_IPV6 != 0
1383 if (ni6
->ni_code
!= ICMP6_NI_SUBJ_IPV6
) {
1388 if (subjlen
!= sizeof(struct in6_addr
)) {
1393 * Validate Subject address.
1395 * Not sure what exactly "address belongs to the node"
1396 * means in the spec, is it just unicast, or what?
1398 * At this moment we consider Subject address as
1399 * "belong to the node" if the Subject address equals
1400 * to the IPv6 destination address; validation for
1401 * IPv6 destination address should have done enough
1404 * We do not do proxy at this moment.
1406 /* m_pulldown instead of copy? */
1407 m_copydata(m
, off
+ sizeof(struct icmp6_nodeinfo
),
1408 subjlen
, (caddr_t
)&sin6
.sin6_addr
);
1409 sin6
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1411 in6_embedscope(&sin6
.sin6_addr
, &sin6
, NULL
, NULL
,
1413 bzero(&sin6_d
, sizeof(sin6_d
));
1414 sin6_d
.sin6_family
= AF_INET6
; /* not used, actually */
1415 sin6_d
.sin6_len
= sizeof(sin6_d
); /* ditto */
1416 sin6_d
.sin6_addr
= ip6
->ip6_dst
;
1417 sin6_d
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1419 in6_embedscope(&sin6_d
.sin6_addr
, &sin6_d
, NULL
, NULL
,
1421 subj
= (char *)&sin6
;
1422 if (SA6_ARE_ADDR_EQUAL(&sin6
, &sin6_d
)) {
1427 * XXX if we are to allow other cases, we should really
1428 * be careful about scope here.
1429 * basically, we should disallow queries toward IPv6
1430 * destination X with subject Y,
1431 * if scope(X) > scope(Y).
1432 * if we allow scope(X) > scope(Y), it will result in
1433 * information leakage across scope boundary.
1437 case ICMP6_NI_SUBJ_FQDN
:
1439 * Validate Subject name with gethostname(3).
1441 * The behavior may need some debate, since:
1442 * - we are not sure if the node has FQDN as
1443 * hostname (returned by gethostname(3)).
1444 * - the code does wildcard match for truncated names.
1445 * however, we are not sure if we want to perform
1446 * wildcard match, if gethostname(3) side has
1447 * truncated hostname.
1449 lck_mtx_lock(&hostname_lock
);
1450 n
= ni6_nametodns(hostname
, hostnamelen
, 0);
1451 lck_mtx_unlock(&hostname_lock
);
1452 if (!n
|| n
->m_next
|| n
->m_len
== 0) {
1455 IP6_EXTHDR_GET(subj
, char *, m
,
1456 off
+ sizeof(struct icmp6_nodeinfo
), subjlen
);
1460 if (!ni6_dnsmatch(subj
, subjlen
, mtod(n
, const char *),
1468 case ICMP6_NI_SUBJ_IPV4
: /* XXX: to be implemented? */
1475 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1478 if ((icmp6_nodeinfo
& ICMP6_NODEINFO_FQDNOK
) == 0) {
1482 case NI_QTYPE_NODEADDR
:
1483 case NI_QTYPE_IPV4ADDR
:
1484 if ((icmp6_nodeinfo
& ICMP6_NODEINFO_NODEADDROK
) == 0) {
1490 /* guess reply length */
1493 break; /* no reply data */
1494 case NI_QTYPE_SUPTYPES
:
1495 replylen
+= sizeof(u_int32_t
);
1498 /* XXX will append an mbuf */
1499 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1501 case NI_QTYPE_NODEADDR
:
1502 addrs
= ni6_addrs(ni6
, &ifp
, subj
);
1503 if ((replylen
+= addrs
* (sizeof(struct in6_addr
) +
1504 sizeof(u_int32_t
))) > MCLBYTES
) {
1505 replylen
= MCLBYTES
; /* XXX: will truncate pkt later */
1508 case NI_QTYPE_IPV4ADDR
:
1509 /* unsupported - should respond with unknown Qtype? */
1513 * XXX: We must return a reply with the ICMP6 code
1514 * `unknown Qtype' in this case. However we regard the case
1515 * as an FQDN query for backward compatibility.
1516 * Older versions set a random value to this field,
1517 * so it rarely varies in the defined qtypes.
1518 * But the mechanism is not reliable...
1519 * maybe we should obsolete older versions.
1521 qtype
= NI_QTYPE_FQDN
;
1522 /* XXX will append an mbuf */
1523 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1528 /* allocate an mbuf to reply. */
1529 MGETHDR(n
, M_DONTWAIT
, m
->m_type
); /* MAC-OK */
1537 M_COPY_PKTHDR(n
, m
); /* just for recvif */
1538 if (replylen
> MHLEN
) {
1539 if (replylen
> MCLBYTES
) {
1541 * XXX: should we try to allocate more? But MCLBYTES
1542 * is probably much larger than IPV6_MMTU...
1546 MCLGET(n
, M_DONTWAIT
);
1547 if ((n
->m_flags
& M_EXT
) == 0) {
1551 n
->m_pkthdr
.len
= n
->m_len
= replylen
;
1553 /* copy mbuf header and IPv6 + Node Information base headers */
1554 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
), sizeof(struct ip6_hdr
));
1555 nni6
= (struct icmp6_nodeinfo
*)(mtod(n
, struct ip6_hdr
*) + 1);
1556 bcopy((caddr_t
)ni6
, (caddr_t
)nni6
, sizeof(struct icmp6_nodeinfo
));
1558 /* qtype dependent procedure */
1561 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1564 case NI_QTYPE_SUPTYPES
:
1567 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1568 nni6
->ni_flags
= htons(0x0000); /* raw bitmap */
1569 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1570 v
= (u_int32_t
)htonl(0x0000000f);
1571 bcopy(&v
, nni6
+ 1, sizeof(u_int32_t
));
1575 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1576 fqdn
= (struct ni_reply_fqdn
*)(mtod(n
, caddr_t
) +
1577 sizeof(struct ip6_hdr
) +
1578 sizeof(struct icmp6_nodeinfo
));
1579 nni6
->ni_flags
= 0; /* XXX: meaningless TTL */
1580 fqdn
->ni_fqdn_ttl
= 0; /* ditto. */
1582 * XXX do we really have FQDN in variable "hostname"?
1584 lck_mtx_lock(&hostname_lock
);
1585 n
->m_next
= ni6_nametodns(hostname
, hostnamelen
, oldfqdn
);
1586 lck_mtx_unlock(&hostname_lock
);
1587 if (n
->m_next
== NULL
) {
1590 /* XXX we assume that n->m_next is not a chain */
1591 if (n
->m_next
->m_next
!= NULL
) {
1594 n
->m_pkthdr
.len
+= n
->m_next
->m_len
;
1596 case NI_QTYPE_NODEADDR
:
1600 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1601 n
->m_pkthdr
.len
= n
->m_len
=
1602 sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1603 lenlim
= (int)M_TRAILINGSPACE(n
);
1604 copied
= ni6_store_addrs(ni6
, nni6
, ifp
, lenlim
);
1605 /* XXX: reset mbuf length */
1606 n
->m_pkthdr
.len
= n
->m_len
= sizeof(struct ip6_hdr
) +
1607 sizeof(struct icmp6_nodeinfo
) + copied
;
1611 break; /* XXX impossible! */
1614 nni6
->ni_type
= ICMP6_NI_REPLY
;
1634 * make a mbuf with DNS-encoded string. no compression support.
1636 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1637 * treated as truncated name (two \0 at the end). this is a wild guess.
1639 static struct mbuf
*
1643 int old
) /* return pascal string if non-zero */
1657 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1658 MGET(m
, M_DONTWAIT
, MT_DATA
);
1659 if (m
&& len
> MLEN
) {
1660 MCLGET(m
, M_DONTWAIT
);
1661 if ((m
->m_flags
& M_EXT
) == 0) {
1672 *mtod(m
, char *) = (char)namelen
;
1673 bcopy(name
, mtod(m
, char *) + 1, namelen
);
1677 cp
= mtod(m
, char *);
1678 ep
= mtod(m
, char *) + M_TRAILINGSPACE(m
);
1680 /* if not certain about my name, return empty buffer */
1686 * guess if it looks like shortened hostname, or FQDN.
1687 * shortened hostname needs two trailing "\0".
1690 for (p
= name
; p
< name
+ namelen
; p
++) {
1691 if (*p
&& *p
== '.') {
1702 while (cp
< ep
&& p
< name
+ namelen
) {
1704 for (q
= p
; q
< name
+ namelen
&& *q
&& *q
!= '.'; q
++) {
1707 /* result does not fit into mbuf */
1708 if (cp
+ i
+ 1 >= ep
) {
1712 * DNS label length restriction, RFC1035 page 8.
1713 * "i == 0" case is included here to avoid returning
1714 * 0-length label on "foo..bar".
1716 if (i
<= 0 || i
>= 64) {
1723 if (p
< name
+ namelen
&& *p
== '.') {
1728 if (cp
+ nterm
>= ep
) {
1731 while (nterm
-- > 0) {
1734 m
->m_len
= (int32_t)(cp
- mtod(m
, char *));
1738 panic("should not reach here");
1749 * check if two DNS-encoded string matches. takes care of truncated
1750 * form (with \0\0 at the end). no compression support.
1751 * XXX upper/lowercase match (see RFC2065)
1754 ni6_dnsmatch(const char *a
, int alen
, const char *b
, int blen
)
1756 const char *a0
, *b0
;
1759 /* simplest case - need validation? */
1760 if (alen
== blen
&& bcmp(a
, b
, alen
) == 0) {
1767 /* termination is mandatory */
1768 if (alen
< 2 || blen
< 2) {
1771 if (a0
[alen
- 1] != '\0' || b0
[blen
- 1] != '\0') {
1777 while (a
- a0
< alen
&& b
- b0
< blen
) {
1778 if (a
- a0
+ 1 > alen
|| b
- b0
+ 1 > blen
) {
1782 if ((signed char)a
[0] < 0 || (signed char)b
[0] < 0) {
1785 /* we don't support compression yet */
1786 if (a
[0] >= 64 || b
[0] >= 64) {
1790 /* truncated case */
1791 if (a
[0] == 0 && a
- a0
== alen
- 1) {
1794 if (b
[0] == 0 && b
- b0
== blen
- 1) {
1797 if (a
[0] == 0 || b
[0] == 0) {
1805 if (a
- a0
+ 1 + l
> alen
|| b
- b0
+ 1 + l
> blen
) {
1808 if (bcmp(a
+ 1, b
+ 1, l
) != 0) {
1816 if (a
- a0
== alen
&& b
- b0
== blen
) {
1824 * calculate the number of addresses to be returned in the node info reply.
1827 ni6_addrs(struct icmp6_nodeinfo
*ni6
, struct ifnet
**ifpp
, char *subj
)
1830 struct in6_ifaddr
*ifa6
;
1832 struct sockaddr_in6
*subj_ip6
= NULL
; /* XXX pedant */
1833 int addrs
= 0, addrsofif
, iffound
= 0;
1834 int niflags
= ni6
->ni_flags
;
1840 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0) {
1841 switch (ni6
->ni_code
) {
1842 case ICMP6_NI_SUBJ_IPV6
:
1843 if (subj
== NULL
) { /* must be impossible... */
1846 subj_ip6
= (struct sockaddr_in6
*)(void *)subj
;
1850 * XXX: we only support IPv6 subject address for
1857 ifnet_head_lock_shared();
1858 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
1860 ifnet_lock_shared(ifp
);
1861 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1864 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
1868 ifa6
= (struct in6_ifaddr
*)ifa
;
1870 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0 &&
1871 IN6_ARE_ADDR_EQUAL(&subj_ip6
->sin6_addr
,
1872 &ifa6
->ia_addr
.sin6_addr
)) {
1877 * IPv4-mapped addresses can only be returned by a
1878 * Node Information proxy, since they represent
1879 * addresses of IPv4-only nodes, which perforce do
1880 * not implement this protocol.
1881 * [icmp-name-lookups-07, Section 5.4]
1882 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1883 * this function at this moment.
1886 /* What do we have to do about ::1? */
1887 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1888 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1889 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0) {
1894 case IPV6_ADDR_SCOPE_SITELOCAL
:
1895 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0) {
1900 case IPV6_ADDR_SCOPE_GLOBAL
:
1901 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0) {
1912 * check if anycast is okay.
1913 * XXX: just experimental. not in the spec.
1915 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
1916 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0) {
1918 continue; /* we need only unicast addresses */
1920 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1921 (icmp6_nodeinfo
& ICMP6_NODEINFO_TMPADDROK
) == 0) {
1925 addrsofif
++; /* count the address */
1928 ifnet_lock_done(ifp
);
1932 ifnet_reference(ifp
);
1946 ni6_store_addrs(struct icmp6_nodeinfo
*ni6
, struct icmp6_nodeinfo
*nni6
,
1947 struct ifnet
*ifp0
, int resid
)
1949 struct ifnet
*ifp
= ifp0
;
1950 struct in6_ifaddr
*ifa6
;
1952 struct ifnet
*ifp_dep
= NULL
;
1953 int copied
= 0, allow_deprecated
= 0;
1954 u_char
*cp
= (u_char
*)(nni6
+ 1);
1955 int niflags
= ni6
->ni_flags
;
1957 uint64_t now
= net_uptime();
1959 if (ifp0
== NULL
&& !(niflags
& NI_NODEADDR_FLAG_ALL
)) {
1960 return 0; /* needless to copy */
1964 ifnet_head_lock_shared();
1966 ifp
= TAILQ_FIRST(&ifnet_head
);
1969 for (; ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
)) {
1970 ifnet_lock_shared(ifp
);
1971 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
1972 ifa
= ifa
->ifa_list
.tqe_next
) {
1973 struct in6_addrlifetime_i
*lt
;
1976 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
1980 ifa6
= (struct in6_ifaddr
*)ifa
;
1982 if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0 &&
1983 allow_deprecated
== 0) {
1985 * prefererred address should be put before
1986 * deprecated addresses.
1989 /* record the interface for later search */
1990 if (ifp_dep
== NULL
) {
1996 } else if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) == 0 &&
1997 allow_deprecated
!= 0) {
1999 continue; /* we now collect deprecated addrs */
2001 /* What do we have to do about ::1? */
2002 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
2003 case IPV6_ADDR_SCOPE_LINKLOCAL
:
2004 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0) {
2009 case IPV6_ADDR_SCOPE_SITELOCAL
:
2010 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0) {
2015 case IPV6_ADDR_SCOPE_GLOBAL
:
2016 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0) {
2027 * check if anycast is okay.
2028 * XXX: just experimental. not in the spec.
2030 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
2031 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0) {
2035 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
2036 (icmp6_nodeinfo
& ICMP6_NODEINFO_TMPADDROK
) == 0) {
2041 /* now we can copy the address */
2042 if (resid
< sizeof(struct in6_addr
) +
2043 sizeof(u_int32_t
)) {
2046 * We give up much more copy.
2047 * Set the truncate flag and return.
2050 NI_NODEADDR_FLAG_TRUNCATE
;
2051 ifnet_lock_done(ifp
);
2057 * Set the TTL of the address.
2058 * The TTL value should be one of the following
2059 * according to the specification:
2061 * 1. The remaining lifetime of a DHCP lease on the
2063 * 2. The remaining Valid Lifetime of a prefix from
2064 * which the address was derived through Stateless
2065 * Autoconfiguration.
2067 * Note that we currently do not support stateful
2068 * address configuration by DHCPv6, so the former
2069 * case can't happen.
2071 lt
= &ifa6
->ia6_lifetime
;
2072 if (lt
->ia6ti_expire
== 0) {
2073 ltime
= ND6_INFINITE_LIFETIME
;
2075 if (lt
->ia6ti_expire
> now
) {
2076 ltime
= htonl((uint32_t)(lt
->ia6ti_expire
- now
));
2082 bcopy(<ime
, cp
, sizeof(u_int32_t
));
2083 cp
+= sizeof(u_int32_t
);
2085 /* copy the address itself */
2086 bcopy(&ifa6
->ia_addr
.sin6_addr
, cp
,
2087 sizeof(struct in6_addr
));
2088 /* XXX: KAME link-local hack; remove ifindex */
2089 if (IN6_IS_ADDR_LINKLOCAL(&ifa6
->ia_addr
.sin6_addr
)) {
2090 ((struct in6_addr
*)(void *)cp
)->s6_addr16
[1] = 0;
2092 cp
+= sizeof(struct in6_addr
);
2094 resid
-= (sizeof(struct in6_addr
) + sizeof(u_int32_t
));
2095 copied
+= (sizeof(struct in6_addr
) +
2099 ifnet_lock_done(ifp
);
2100 if (ifp0
) { /* we need search only on the specified IF */
2106 if (allow_deprecated
== 0 && ifp_dep
!= NULL
) {
2108 allow_deprecated
= 1;
2117 * XXX almost dup'ed code with rip6_input.
2120 icmp6_rip6_input(struct mbuf
**mp
, int off
)
2122 struct mbuf
*m
= *mp
;
2123 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2124 struct in6pcb
*in6p
;
2125 struct in6pcb
*last
= NULL
;
2126 struct sockaddr_in6 rip6src
;
2127 struct icmp6_hdr
*icmp6
;
2128 struct mbuf
*opts
= NULL
;
2130 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
2132 #ifndef PULLDOWN_TEST
2133 /* this is assumed to be safe. */
2134 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
2136 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
2137 if (icmp6
== NULL
) {
2138 /* m is already reclaimed */
2139 return IPPROTO_DONE
;
2144 * XXX: the address may have embedded scope zone ID, which should be
2145 * hidden from applications.
2147 bzero(&rip6src
, sizeof(rip6src
));
2148 rip6src
.sin6_family
= AF_INET6
;
2149 rip6src
.sin6_len
= sizeof(struct sockaddr_in6
);
2150 rip6src
.sin6_addr
= ip6
->ip6_src
;
2151 if (sa6_recoverscope(&rip6src
, TRUE
)) {
2152 return IPPROTO_DONE
;
2155 lck_rw_lock_shared(ripcbinfo
.ipi_lock
);
2156 LIST_FOREACH(in6p
, &ripcb
, inp_list
)
2158 if ((in6p
->inp_vflag
& INP_IPV6
) == 0) {
2161 if (in6p
->in6p_ip6_nxt
!= IPPROTO_ICMPV6
) {
2164 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
) &&
2165 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_laddr
, &ip6
->ip6_dst
)) {
2168 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
) &&
2169 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_faddr
, &ip6
->ip6_src
)) {
2172 if (in6p
->in6p_icmp6filt
2173 && ICMP6_FILTER_WILLBLOCK(icmp6
->icmp6_type
,
2174 in6p
->in6p_icmp6filt
)) {
2178 if (inp_restricted_recv(in6p
, ifp
)) {
2184 if ((n
= m_copy(m
, 0, (int)M_COPYALL
)) != NULL
) {
2185 if ((last
->in6p_flags
& INP_CONTROLOPTS
) != 0 ||
2187 /* Content Filter needs to see local address */
2188 (last
->in6p_socket
->so_cfil_db
!= NULL
) ||
2190 (last
->in6p_socket
->so_options
& SO_TIMESTAMP
) != 0 ||
2191 (last
->in6p_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0 ||
2192 (last
->in6p_socket
->so_options
& SO_TIMESTAMP_CONTINUOUS
) != 0) {
2193 ret
= ip6_savecontrol(last
, n
, &opts
);
2201 /* strip intermediate headers */
2203 so_recv_data_stat(last
->in6p_socket
, m
, 0);
2204 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
2205 (struct sockaddr
*)&rip6src
,
2206 n
, opts
, NULL
) != 0) {
2207 sorwakeup(last
->in6p_socket
);
2215 if ((last
->in6p_flags
& INP_CONTROLOPTS
) != 0 ||
2217 /* Content Filter needs to see local address */
2218 (last
->in6p_socket
->so_cfil_db
!= NULL
) ||
2220 (last
->in6p_socket
->so_options
& SO_TIMESTAMP
) != 0 ||
2221 (last
->in6p_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0 ||
2222 (last
->in6p_socket
->so_options
& SO_TIMESTAMP_CONTINUOUS
) != 0) {
2223 ret
= ip6_savecontrol(last
, m
, &opts
);
2228 /* strip intermediate headers */
2230 so_recv_data_stat(last
->in6p_socket
, m
, 0);
2231 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
2232 (struct sockaddr
*)&rip6src
, m
, opts
, NULL
) != 0) {
2233 sorwakeup(last
->in6p_socket
);
2238 lck_rw_done(ripcbinfo
.ipi_lock
);
2239 return IPPROTO_DONE
;
2242 lck_rw_done(ripcbinfo
.ipi_lock
);
2245 ip6stat
.ip6s_delivered
--;
2246 return IPPROTO_DONE
;
2250 * Reflect the ip6 packet back to the source.
2251 * OFF points to the icmp6 header, counted from the top of the mbuf.
2254 icmp6_reflect(struct mbuf
*m
, size_t off
)
2256 struct mbuf
*m_ip6hdr
= m
;
2257 struct ip6_hdr
*ip6
;
2258 struct icmp6_hdr
*icmp6
;
2259 struct in6_ifaddr
*ia
;
2260 struct in6_addr t
, src_storage
, *src
= 0;
2263 struct ifnet
*outif
= NULL
;
2264 struct sockaddr_in6 sa6_src
, sa6_dst
;
2265 struct nd_ifinfo
*ndi
= NULL
;
2267 struct ip6_out_args ip6oa
;
2269 bzero(&ip6oa
, sizeof(ip6oa
));
2270 ip6oa
.ip6oa_boundif
= IFSCOPE_NONE
;
2271 ip6oa
.ip6oa_flags
= IP6OAF_SELECT_SRCIF
| IP6OAF_BOUND_SRCADDR
|
2272 IP6OAF_INTCOPROC_ALLOWED
| IP6OAF_AWDL_UNRESTRICTED
;
2273 ip6oa
.ip6oa_sotc
= SO_TC_UNSPEC
;
2274 ip6oa
.ip6oa_netsvctype
= _NET_SERVICE_TYPE_UNSPEC
;
2276 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
) && m
->m_pkthdr
.rcvif
!= NULL
) {
2277 ip6oa
.ip6oa_boundif
= m
->m_pkthdr
.rcvif
->if_index
;
2278 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
2281 /* too short to reflect */
2282 if (off
< sizeof(struct ip6_hdr
)) {
2284 "sanity fail: off=%x, sizeof(ip6)=%x in %s:%d\n",
2285 (u_int32_t
)off
, (u_int32_t
)sizeof(struct ip6_hdr
),
2286 __func__
, __LINE__
);
2291 * If there are extra headers between IPv6 and ICMPv6, strip
2292 * off that header first.
2294 if (off
> sizeof(struct ip6_hdr
)) {
2296 struct ip6_hdr nip6
;
2298 l
= off
- sizeof(struct ip6_hdr
);
2299 m_copydata(m
, 0, sizeof(nip6
), (caddr_t
)&nip6
);
2301 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
2303 if ((m_ip6hdr
= m_pulldown(m
, 0, (int)l
, NULL
)) == NULL
) {
2307 bcopy((caddr_t
)&nip6
, mtod(m
, caddr_t
), sizeof(nip6
));
2308 } else { /* off == sizeof(struct ip6_hdr) */
2310 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
2312 if ((m_ip6hdr
= m_pulldown(m
, 0, (int)l
, NULL
)) == NULL
) {
2317 plen
= m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
2318 ip6
= mtod(m_ip6hdr
, struct ip6_hdr
*);
2319 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2320 icmp6
= (struct icmp6_hdr
*)(ip6
+ 1);
2321 type
= icmp6
->icmp6_type
; /* keep type for statistics */
2322 code
= icmp6
->icmp6_code
; /* ditto. */
2326 * ip6_input() drops a packet if its src is multicast.
2327 * So, the src is never multicast.
2329 ip6
->ip6_dst
= ip6
->ip6_src
;
2332 * XXX: make sure to embed scope zone information, using
2333 * already embedded IDs or the received interface (if any).
2334 * Note that rcvif may be NULL.
2336 bzero(&sa6_src
, sizeof(sa6_src
));
2337 sa6_src
.sin6_family
= AF_INET6
;
2338 sa6_src
.sin6_len
= sizeof(sa6_src
);
2339 sa6_src
.sin6_addr
= ip6
->ip6_dst
;
2340 in6_recoverscope(&sa6_src
, &ip6
->ip6_dst
, m
->m_pkthdr
.rcvif
);
2341 in6_embedscope(&ip6
->ip6_dst
, &sa6_src
, NULL
, NULL
, NULL
);
2342 bzero(&sa6_dst
, sizeof(sa6_dst
));
2343 sa6_dst
.sin6_family
= AF_INET6
;
2344 sa6_dst
.sin6_len
= sizeof(sa6_dst
);
2345 sa6_dst
.sin6_addr
= t
;
2346 in6_recoverscope(&sa6_dst
, &t
, m
->m_pkthdr
.rcvif
);
2347 in6_embedscope(&t
, &sa6_dst
, NULL
, NULL
, NULL
);
2350 * If the incoming packet was addressed directly to us(i.e. unicast),
2351 * use dst as the src for the reply.
2352 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2353 * (for example) when we encounter an error while forwarding procedure
2354 * destined to a duplicated address of ours.
2355 * Note that ip6_getdstifaddr() may fail if we are in an error handling
2356 * procedure of an outgoing packet of our own, in which case we need
2357 * to search in the ifaddr list.
2359 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2360 TAILQ_FOREACH(ia
, IN6ADDR_HASH(&t
), ia6_hash
) {
2361 IFA_LOCK(&ia
->ia_ifa
);
2362 if (IN6_ARE_ADDR_EQUAL(&t
, &ia
->ia_addr
.sin6_addr
) &&
2363 (ia
->ia6_flags
& (IN6_IFF_ANYCAST
| IN6_IFF_NOTREADY
| IN6_IFF_CLAT46
)) == 0) {
2364 IFA_UNLOCK(&ia
->ia_ifa
);
2368 IFA_UNLOCK(&ia
->ia_ifa
);
2370 lck_rw_done(&in6_ifaddr_rwlock
);
2371 if (ia
== NULL
&& IN6_IS_ADDR_LINKLOCAL(&t
) &&
2372 ((m
->m_flags
& M_LOOP
) || (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
))) {
2374 * This is the case if the dst is our link-local address
2375 * and the sender is also ourselves. Here we test for both
2376 * M_LOOP and PKTF_LOOP, since the former may have been set
2377 * in ip6_output() and that we get here as part of callling
2378 * ip6_process_hopopts(). See comments in <sys/mbuf.h>
2385 struct sockaddr_in6 sin6
;
2386 struct route_in6 ro
;
2389 * This case matches to multicasts, our anycast, or unicasts
2390 * that we do not own. Select a source address based on the
2391 * source address of the erroneous packet.
2393 bzero(&sin6
, sizeof(sin6
));
2394 sin6
.sin6_family
= AF_INET6
;
2395 sin6
.sin6_len
= sizeof(sin6
);
2396 sin6
.sin6_addr
= ip6
->ip6_dst
; /* zone ID should be embedded */
2398 bzero(&ro
, sizeof(ro
));
2400 * in6_selectsrc() might return outif with its reference held
2401 * even in the error case, so we always need to release it
2404 src
= in6_selectsrc(&sin6
, NULL
, NULL
, &ro
, &outif
,
2405 &src_storage
, ip6oa
.ip6oa_boundif
, &e
);
2409 "icmp6_reflect: source can't be determined: "
2410 "dst=%s, error=%d\n",
2411 ip6_sprintf(&sa6_src
.sin6_addr
), e
);
2415 oflow
= ip6
->ip6_flow
; /* Save for later */
2416 ip6
->ip6_src
= *src
;
2418 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2419 ip6
->ip6_vfc
|= IPV6_VERSION
;
2420 if (icmp6
->icmp6_type
== ICMP6_ECHO_REPLY
&& icmp6
->icmp6_code
== 0) {
2421 ip6
->ip6_flow
|= (oflow
& htonl(0x0ff00000));
2423 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2424 if (outif
!= NULL
&& (ndi
= ND_IFINFO(outif
)) != NULL
&&
2426 lck_mtx_lock(&ndi
->lock
);
2427 ip6
->ip6_hlim
= ndi
->chlim
;
2428 lck_mtx_unlock(&ndi
->lock
);
2430 if (m
->m_pkthdr
.rcvif
!= NULL
&&
2431 (ndi
= ND_IFINFO(m
->m_pkthdr
.rcvif
)) != NULL
&&
2433 /* XXX: This may not be the outgoing interface */
2434 lck_mtx_lock(&ndi
->lock
);
2435 ip6
->ip6_hlim
= ndi
->chlim
;
2436 lck_mtx_unlock(&ndi
->lock
);
2438 ip6
->ip6_hlim
= (uint8_t)ip6_defhlim
;
2440 /* Use the same traffic class as in the request to match IPv4 */
2441 icmp6
->icmp6_cksum
= 0;
2442 icmp6
->icmp6_cksum
= in6_cksum(m
, IPPROTO_ICMPV6
,
2443 sizeof(struct ip6_hdr
), plen
);
2446 * XXX option handling
2448 m
->m_flags
&= ~(M_BCAST
| M_MCAST
);
2450 if (outif
!= NULL
) {
2451 ifnet_release(outif
);
2455 m
->m_pkthdr
.csum_data
= 0;
2456 m
->m_pkthdr
.csum_flags
= 0;
2457 ip6_output(m
, NULL
, NULL
, IPV6_OUTARGS
, NULL
, &outif
, &ip6oa
);
2458 if (outif
!= NULL
) {
2459 icmp6_ifoutstat_inc(outif
, type
, code
);
2460 ifnet_release(outif
);
2466 if (outif
!= NULL
) {
2467 ifnet_release(outif
);
2473 icmp6_redirect_diag(struct in6_addr
*src6
,
2474 struct in6_addr
*dst6
,
2475 struct in6_addr
*tgt6
)
2477 static char buf
[1024];
2478 snprintf(buf
, sizeof(buf
), "(src=%s dst=%s tgt=%s)",
2479 ip6_sprintf(src6
), ip6_sprintf(dst6
), ip6_sprintf(tgt6
));
2484 icmp6_redirect_input(struct mbuf
*m
, int off
, int icmp6len
)
2486 struct ifnet
*ifp
= NULL
;
2487 struct ip6_hdr
*ip6
= NULL
;
2488 struct nd_redirect
*nd_rd
= NULL
;
2489 char *lladdr
= NULL
;
2491 u_char
*redirhdr
= NULL
;
2492 int redirhdrlen
= 0;
2493 struct rtentry
*rt
= NULL
;
2496 struct in6_addr src6
= {};
2497 struct in6_addr redtgt6
= {};
2498 struct in6_addr reddst6
= {};
2499 union nd_opts ndopts
= {};
2505 ifp
= m
->m_pkthdr
.rcvif
;
2511 * If we are an advertising router on this interface,
2512 * don't update route by icmp6 redirect.
2514 if (ifp
->if_ipv6_router_mode
== IPV6_ROUTER_MODE_EXCLUSIVE
) {
2517 if (!icmp6_rediraccept
) {
2521 ip6
= mtod(m
, struct ip6_hdr
*);
2522 src6
= ip6
->ip6_src
;
2523 #ifndef PULLDOWN_TEST
2524 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return );
2525 nd_rd
= (struct nd_redirect
*)(mtod(m
, caddr_t
) + off
);
2527 IP6_EXTHDR_GET(nd_rd
, struct nd_redirect
*, m
, off
, icmp6len
);
2528 if (nd_rd
== NULL
) {
2529 icmp6stat
.icp6s_tooshort
++;
2533 ip6
= mtod(m
, struct ip6_hdr
*);
2535 redtgt6
= nd_rd
->nd_rd_target
;
2536 reddst6
= nd_rd
->nd_rd_dst
;
2538 if (in6_setscope(&redtgt6
, m
->m_pkthdr
.rcvif
, NULL
) ||
2539 in6_setscope(&reddst6
, m
->m_pkthdr
.rcvif
, NULL
)) {
2544 if (!IN6_IS_ADDR_LINKLOCAL(&src6
)) {
2546 "ICMP6 redirect sent from %s rejected; "
2547 "must be from linklocal\n", ip6_sprintf(&src6
));
2550 if (ip6
->ip6_hlim
!= IPV6_MAXHLIM
) {
2552 "ICMP6 redirect sent from %s rejected; "
2553 "hlim=%d (must be 255)\n",
2554 ip6_sprintf(&src6
), ip6
->ip6_hlim
);
2558 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2559 struct sockaddr_in6 sin6
;
2560 struct in6_addr
*gw6
;
2562 bzero(&sin6
, sizeof(sin6
));
2563 sin6
.sin6_family
= AF_INET6
;
2564 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
2565 bcopy(&reddst6
, &sin6
.sin6_addr
, sizeof(reddst6
));
2566 rt
= rtalloc1_scoped((struct sockaddr
*)&sin6
, 0, 0, ifp
->if_index
);
2569 if (rt
->rt_gateway
== NULL
||
2570 rt
->rt_gateway
->sa_family
!= AF_INET6
) {
2572 "ICMP6 redirect rejected; no route "
2573 "with inet6 gateway found for redirect dst: %s\n",
2574 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
));
2580 gw6
= &(((struct sockaddr_in6
*)(void *)
2581 rt
->rt_gateway
)->sin6_addr
);
2582 if (bcmp(&src6
, gw6
, sizeof(struct in6_addr
)) != 0) {
2584 "ICMP6 redirect rejected; "
2585 "not equal to gw-for-src=%s (must be same): "
2588 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
));
2595 "ICMP6 redirect rejected; "
2596 "no route found for redirect dst: %s\n",
2597 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
));
2604 if (IN6_IS_ADDR_MULTICAST(&reddst6
)) {
2606 "ICMP6 redirect rejected; "
2607 "redirect dst must be unicast: %s\n",
2608 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
));
2612 is_router
= is_onlink
= 0;
2613 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6
)) {
2614 is_router
= 1; /* router case */
2616 if (bcmp(&redtgt6
, &reddst6
, sizeof(redtgt6
)) == 0) {
2617 is_onlink
= 1; /* on-link destination case */
2619 if (!is_router
&& !is_onlink
) {
2621 "ICMP6 redirect rejected; "
2622 "neither router case nor onlink case: %s\n",
2623 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
));
2626 /* validation passed */
2628 icmp6len
-= sizeof(*nd_rd
);
2629 nd6_option_init(nd_rd
+ 1, icmp6len
, &ndopts
);
2630 if (nd6_options(&ndopts
) < 0) {
2631 nd6log(info
, "icmp6_redirect_input: "
2632 "invalid ND option, rejected: %s\n",
2633 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
));
2634 /* nd6_options have incremented stats */
2638 if (ndopts
.nd_opts_tgt_lladdr
) {
2639 lladdr
= (char *)(ndopts
.nd_opts_tgt_lladdr
+ 1);
2640 lladdrlen
= ndopts
.nd_opts_tgt_lladdr
->nd_opt_len
<< 3;
2643 if (ndopts
.nd_opts_rh
) {
2644 redirhdrlen
= ndopts
.nd_opts_rh
->nd_opt_rh_len
;
2645 redirhdr
= (u_char
*)(ndopts
.nd_opts_rh
+ 1); /* xxx */
2648 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
2650 "icmp6_redirect_input: lladdrlen mismatch for %s "
2651 "(if %d, icmp6 packet %d): %s\n",
2652 ip6_sprintf(&redtgt6
), ifp
->if_addrlen
, lladdrlen
- 2,
2653 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
));
2658 nd6_cache_lladdr(ifp
, &redtgt6
, lladdr
, lladdrlen
, ND_REDIRECT
,
2659 is_onlink
? ND_REDIRECT_ONLINK
: ND_REDIRECT_ROUTER
);
2661 if (!is_onlink
) { /* better router case. perform rtredirect. */
2662 /* perform rtredirect */
2663 struct sockaddr_in6 sdst
;
2664 struct sockaddr_in6 sgw
;
2665 struct sockaddr_in6 ssrc
;
2667 bzero(&sdst
, sizeof(sdst
));
2668 bzero(&sgw
, sizeof(sgw
));
2669 bzero(&ssrc
, sizeof(ssrc
));
2670 sdst
.sin6_family
= sgw
.sin6_family
= ssrc
.sin6_family
= AF_INET6
;
2671 sdst
.sin6_len
= sgw
.sin6_len
= ssrc
.sin6_len
=
2672 sizeof(struct sockaddr_in6
);
2673 bcopy(&redtgt6
, &sgw
.sin6_addr
, sizeof(struct in6_addr
));
2674 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2675 bcopy(&src6
, &ssrc
.sin6_addr
, sizeof(struct in6_addr
));
2676 rtredirect(ifp
, (struct sockaddr
*)&sdst
,
2677 (struct sockaddr
*)&sgw
, NULL
, RTF_GATEWAY
| RTF_HOST
,
2678 (struct sockaddr
*)&ssrc
, NULL
);
2680 /* finally update cached route in each socket via pfctlinput */
2682 struct sockaddr_in6 sdst
;
2684 bzero(&sdst
, sizeof(sdst
));
2685 sdst
.sin6_family
= AF_INET6
;
2686 sdst
.sin6_len
= sizeof(struct sockaddr_in6
);
2687 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2689 pfctlinput(PRC_REDIRECT_HOST
, (struct sockaddr
*)&sdst
);
2691 key_sa_routechange((struct sockaddr
*)&sdst
);
2700 icmp6stat
.icp6s_badredirect
++;
2705 icmp6_redirect_output(struct mbuf
*m0
, struct rtentry
*rt
)
2707 struct ifnet
*ifp
; /* my outgoing interface */
2708 struct in6_addr ifp_ll6
;
2709 struct in6_addr
*router_ll6
;
2710 struct ip6_hdr
*sip6
; /* m0 as struct ip6_hdr */
2711 struct mbuf
*m
= NULL
; /* newly allocated one */
2712 struct ip6_hdr
*ip6
; /* m as struct ip6_hdr */
2713 struct nd_redirect
*nd_rd
;
2716 struct ifnet
*outif
= NULL
;
2717 struct sockaddr_in6 src_sa
;
2718 struct ip6_out_args ip6oa
;
2720 bzero(&ip6oa
, sizeof(ip6oa
));
2721 ip6oa
.ip6oa_boundif
= IFSCOPE_NONE
;
2722 ip6oa
.ip6oa_flags
= IP6OAF_SELECT_SRCIF
| IP6OAF_BOUND_SRCADDR
;
2723 ip6oa
.ip6oa_sotc
= SO_TC_UNSPEC
;
2724 ip6oa
.ip6oa_netsvctype
= _NET_SERVICE_TYPE_UNSPEC
;
2726 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, ND_REDIRECT
, 0);
2733 if (!m0
|| !rt
|| !(rt
->rt_flags
& RTF_UP
) || !(ifp
= rt
->rt_ifp
)) {
2738 * If we are not a router to begin with, or not an advertising
2739 * router on this interface, don't send icmp6 redirect.
2741 if (!ip6_forwarding
|| ifp
->if_ipv6_router_mode
!= IPV6_ROUTER_MODE_EXCLUSIVE
) {
2747 * the source address must identify a neighbor, and
2748 * the destination address must not be a multicast address
2749 * [RFC 2461, sec 8.2]
2751 sip6
= mtod(m0
, struct ip6_hdr
*);
2752 bzero(&src_sa
, sizeof(src_sa
));
2753 src_sa
.sin6_family
= AF_INET6
;
2754 src_sa
.sin6_len
= sizeof(src_sa
);
2755 src_sa
.sin6_addr
= sip6
->ip6_src
;
2756 /* we don't currently use sin6_scope_id, but eventually use it */
2757 src_sa
.sin6_scope_id
= in6_addr2scopeid(ifp
, &sip6
->ip6_src
);
2759 if (nd6_is_addr_neighbor(&src_sa
, ifp
, 0) == 0) {
2760 /* already unlocked */
2765 if (IN6_IS_ADDR_MULTICAST(&sip6
->ip6_dst
)) {
2766 goto fail
; /* what should we do here? */
2769 if (icmp6_ratelimit(&sip6
->ip6_src
, ND_REDIRECT
, 0)) {
2774 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2775 * we almost always ask for an mbuf cluster for simplicity.
2776 * (MHLEN < IPV6_MMTU is almost always true)
2778 #if IPV6_MMTU >= MCLBYTES
2779 # error assumption failed about IPV6_MMTU and MCLBYTES
2781 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
2782 if (m
&& IPV6_MMTU
>= MHLEN
) {
2783 MCLGET(m
, M_DONTWAIT
);
2788 m
->m_pkthdr
.rcvif
= NULL
;
2790 maxlen
= M_TRAILINGSPACE(m
);
2791 maxlen
= MIN(IPV6_MMTU
, maxlen
);
2792 /* just for safety */
2793 if (maxlen
< sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
) +
2794 ((sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7)) {
2799 /* get ip6 linklocal address for ifp(my outgoing interface). */
2800 struct in6_ifaddr
*ia
;
2801 if ((ia
= in6ifa_ifpforlinklocal(ifp
,
2803 IN6_IFF_ANYCAST
)) == NULL
) {
2806 IFA_LOCK(&ia
->ia_ifa
);
2807 ifp_ll6
= ia
->ia_addr
.sin6_addr
;
2808 IFA_UNLOCK(&ia
->ia_ifa
);
2809 IFA_REMREF(&ia
->ia_ifa
);
2812 /* get ip6 linklocal address for the router. */
2813 if (rt
->rt_gateway
&& (rt
->rt_flags
& RTF_GATEWAY
)) {
2814 struct sockaddr_in6
*sin6
;
2815 sin6
= (struct sockaddr_in6
*)(void *)rt
->rt_gateway
;
2816 router_ll6
= &sin6
->sin6_addr
;
2817 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6
)) {
2818 router_ll6
= (struct in6_addr
*)NULL
;
2821 router_ll6
= (struct in6_addr
*)NULL
;
2825 ip6
= mtod(m
, struct ip6_hdr
*);
2827 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2828 ip6
->ip6_vfc
|= IPV6_VERSION
;
2829 /* ip6->ip6_plen will be set later */
2830 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2831 ip6
->ip6_hlim
= IPV6_MAXHLIM
;
2832 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2833 bcopy(&ifp_ll6
, &ip6
->ip6_src
, sizeof(struct in6_addr
));
2834 bcopy(&sip6
->ip6_src
, &ip6
->ip6_dst
, sizeof(struct in6_addr
));
2837 nd_rd
= (struct nd_redirect
*)(ip6
+ 1);
2838 nd_rd
->nd_rd_type
= ND_REDIRECT
;
2839 nd_rd
->nd_rd_code
= 0;
2840 nd_rd
->nd_rd_reserved
= 0;
2841 if (rt
->rt_flags
& RTF_GATEWAY
) {
2843 * nd_rd->nd_rd_target must be a link-local address in
2844 * better router cases.
2849 bcopy(router_ll6
, &nd_rd
->nd_rd_target
,
2850 sizeof(nd_rd
->nd_rd_target
));
2851 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2852 sizeof(nd_rd
->nd_rd_dst
));
2854 /* make sure redtgt == reddst */
2855 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_target
,
2856 sizeof(nd_rd
->nd_rd_target
));
2857 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2858 sizeof(nd_rd
->nd_rd_dst
));
2863 p
= (u_char
*)(nd_rd
+ 1);
2870 /* target lladdr option */
2871 struct rtentry
*rt_router
= NULL
;
2873 struct sockaddr_dl
*sdl
;
2874 struct nd_opt_hdr
*nd_opt
;
2877 /* Callee returns a locked route upon success */
2878 rt_router
= nd6_lookup(router_ll6
, 0, ifp
, 0);
2882 RT_LOCK_ASSERT_HELD(rt_router
);
2883 len
= sizeof(*nd_opt
) + ifp
->if_addrlen
;
2884 len
= (len
+ 7) & ~7; /* round by 8 */
2886 if (len
+ (p
- (u_char
*)ip6
) > maxlen
) {
2887 RT_REMREF_LOCKED(rt_router
);
2888 RT_UNLOCK(rt_router
);
2892 if (!(rt_router
->rt_flags
& RTF_GATEWAY
) &&
2893 (rt_router
->rt_flags
& RTF_LLINFO
) &&
2894 (rt_router
->rt_gateway
->sa_family
== AF_LINK
) &&
2895 (sdl
= (struct sockaddr_dl
*)(void *)
2896 rt_router
->rt_gateway
) && sdl
->sdl_alen
) {
2897 nd_opt
= (struct nd_opt_hdr
*)p
;
2898 nd_opt
->nd_opt_type
= ND_OPT_TARGET_LINKADDR
;
2899 nd_opt
->nd_opt_len
= (uint8_t)(len
>> 3);
2900 lladdr
= (char *)(nd_opt
+ 1);
2901 bcopy(LLADDR(sdl
), lladdr
, ifp
->if_addrlen
);
2904 RT_REMREF_LOCKED(rt_router
);
2905 RT_UNLOCK(rt_router
);
2910 m
->m_pkthdr
.len
= m
->m_len
= (int32_t)(p
- (u_char
*)ip6
);
2912 /* just to be safe */
2913 #ifdef M_DECRYPTED /*not openbsd*/
2914 if (m0
->m_flags
& M_DECRYPTED
) {
2918 if (p
- (u_char
*)ip6
> maxlen
) {
2923 /* redirected header option */
2925 struct nd_opt_rd_hdr
*nd_opt_rh
;
2928 * compute the maximum size for icmp6 redirect header option.
2929 * XXX room for auth header?
2931 len
= (int)(maxlen
- (p
- (u_char
*)ip6
));
2934 /* This is just for simplicity. */
2935 if (m0
->m_pkthdr
.len
!= m0
->m_len
) {
2937 m_freem(m0
->m_next
);
2940 m0
->m_pkthdr
.len
= m0
->m_len
;
2944 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2945 * about padding/truncate rule for the original IP packet.
2946 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2947 * - "attach as much as possible" is the goal
2948 * - pad if not aligned (original size can be guessed by original
2950 * Following code adds the padding if it is simple enough,
2951 * and truncates if not.
2953 if (m0
->m_next
|| m0
->m_pkthdr
.len
!= m0
->m_len
) {
2954 panic("assumption failed in %s:%d\n", __func__
, __LINE__
);
2957 if (len
- sizeof(*nd_opt_rh
) < m0
->m_pkthdr
.len
) {
2958 /* not enough room, truncate */
2959 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2961 /* enough room, pad or truncate */
2964 extra
= m0
->m_pkthdr
.len
% 8;
2966 /* pad if easy enough, truncate if not */
2967 if (8 - extra
<= M_TRAILINGSPACE(m0
)) {
2969 m0
->m_len
+= (8 - extra
);
2970 m0
->m_pkthdr
.len
+= (8 - extra
);
2973 m0
->m_pkthdr
.len
-= extra
;
2977 len
= m0
->m_pkthdr
.len
+ sizeof(*nd_opt_rh
);
2978 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2981 nd_opt_rh
= (struct nd_opt_rd_hdr
*)p
;
2982 bzero(nd_opt_rh
, sizeof(*nd_opt_rh
));
2983 nd_opt_rh
->nd_opt_rh_type
= ND_OPT_REDIRECTED_HEADER
;
2984 nd_opt_rh
->nd_opt_rh_len
= (uint8_t)(len
>> 3);
2985 p
+= sizeof(*nd_opt_rh
);
2986 m
->m_pkthdr
.len
= m
->m_len
= (int32_t)(p
- (u_char
*)ip6
);
2988 /* connect m0 to m */
2990 m
->m_pkthdr
.len
= m
->m_len
+ m0
->m_len
;
2994 /* XXX: clear embedded link IDs in the inner header */
2995 in6_clearscope(&sip6
->ip6_src
);
2996 in6_clearscope(&sip6
->ip6_dst
);
2997 in6_clearscope(&nd_rd
->nd_rd_target
);
2998 in6_clearscope(&nd_rd
->nd_rd_dst
);
3000 ip6
->ip6_plen
= htons((uint16_t)(m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
)));
3002 nd_rd
->nd_rd_cksum
= 0;
3004 = in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(*ip6
), ntohs(ip6
->ip6_plen
));
3006 /* send the packet to outside... */
3007 ip6oa
.ip6oa_boundif
= ifp
->if_index
;
3008 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
3009 ip6oa
.ip6oa_flags
|= IP6OAF_DONT_FRAG
;
3011 ip6_output(m
, NULL
, NULL
, IPV6_OUTARGS
, NULL
, &outif
, &ip6oa
);
3013 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
3014 icmp6_ifstat_inc(outif
, ifs6_out_redirect
);
3015 ifnet_release(outif
);
3017 icmp6stat
.icp6s_outhist
[ND_REDIRECT
]++;
3034 * ICMPv6 socket option processing.
3037 icmp6_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
3041 struct inpcb
*inp
= sotoinpcb(so
);
3042 int level
, op
, optname
;
3045 level
= sopt
->sopt_level
;
3046 op
= sopt
->sopt_dir
;
3047 optname
= sopt
->sopt_name
;
3048 optlen
= sopt
->sopt_valsize
;
3050 level
= op
= optname
= optlen
= 0;
3053 if (level
!= IPPROTO_ICMPV6
) {
3062 struct icmp6_filter
*p
;
3064 if (optlen
!= 0 && optlen
!= sizeof(*p
)) {
3068 if (inp
->in6p_icmp6filt
== NULL
) {
3074 /* According to RFC 3542, an installed filter can be
3075 * cleared by issuing a setsockopt for ICMP6_FILTER
3076 * with a zero length.
3078 ICMP6_FILTER_SETPASSALL(inp
->in6p_icmp6filt
);
3080 error
= sooptcopyin(sopt
, inp
->in6p_icmp6filt
, optlen
,
3087 error
= ENOPROTOOPT
;
3096 if (inp
->in6p_icmp6filt
== NULL
) {
3100 error
= sooptcopyout(sopt
, inp
->in6p_icmp6filt
,
3101 MIN(sizeof(struct icmp6_filter
), optlen
));
3106 error
= ENOPROTOOPT
;
3116 * ICMPv6 socket datagram option processing.
3119 icmp6_dgram_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
3121 if (kauth_cred_issuser(so
->so_cred
)) {
3122 return icmp6_ctloutput(so
, sopt
);
3125 if (sopt
->sopt_level
== IPPROTO_ICMPV6
) {
3126 switch (sopt
->sopt_name
) {
3128 return icmp6_ctloutput(so
, sopt
);
3134 if (sopt
->sopt_level
!= IPPROTO_IPV6
) {
3138 switch (sopt
->sopt_name
) {
3139 case IPV6_UNICAST_HOPS
:
3142 case IPV6_USE_MIN_MTU
:
3143 case IPV6_RECVRTHDR
:
3144 case IPV6_RECVPKTINFO
:
3145 case IPV6_RECVHOPLIMIT
:
3151 case IPV6_MULTICAST_IF
:
3152 case IPV6_MULTICAST_HOPS
:
3153 case IPV6_MULTICAST_LOOP
:
3154 case IPV6_JOIN_GROUP
:
3155 case IPV6_LEAVE_GROUP
:
3156 case IPV6_PORTRANGE
:
3157 case IPV6_IPSEC_POLICY
:
3158 case IPV6_RECVTCLASS
:
3160 case IPV6_2292PKTOPTIONS
:
3161 case IPV6_2292PKTINFO
:
3162 case IPV6_2292HOPLIMIT
:
3163 case IPV6_2292HOPOPTS
:
3164 case IPV6_2292DSTOPTS
:
3165 case IPV6_2292RTHDR
:
3167 case IPV6_NO_IFT_CELLULAR
:
3169 return ip6_ctloutput(so
, sopt
);
3176 __private_extern__
int
3177 icmp6_dgram_send(struct socket
*so
, int flags
, struct mbuf
*m
,
3178 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
3180 #pragma unused(flags, p)
3182 struct inpcb
*inp
= sotoinpcb(so
);
3183 struct icmp6_hdr
*icmp6
;
3187 || (necp_socket_should_use_flow_divert(inp
))
3190 error
= (inp
== NULL
? EINVAL
: EPROTOTYPE
);
3194 if (kauth_cred_issuser(so
->so_cred
)) {
3195 return rip6_output(m
, so
, SIN6(nam
), control
, 0);
3199 * For an ICMPv6 packet, we should know its type and code
3201 if (SOCK_PROTO(so
) == IPPROTO_ICMPV6
) {
3202 if (m
->m_len
< sizeof(struct icmp6_hdr
) &&
3203 (m
= m_pullup(m
, sizeof(struct icmp6_hdr
))) == NULL
) {
3207 icmp6
= mtod(m
, struct icmp6_hdr
*);
3210 * Allow only to send echo request and node information request
3211 * See RFC 2463 for Echo Request Message format
3213 if ((icmp6
->icmp6_type
== ICMP6_ECHO_REQUEST
&&
3214 icmp6
->icmp6_code
== 0) ||
3215 (icmp6
->icmp6_type
== ICMP6_NI_QUERY
&&
3216 (icmp6
->icmp6_code
== ICMP6_NI_SUBJ_IPV6
||
3217 icmp6
->icmp6_code
== ICMP6_NI_SUBJ_FQDN
))) {
3226 return rip6_output(m
, so
, SIN6(nam
), control
, 0);
3233 if (control
!= NULL
) {
3240 /* Like rip6_attach but without root privilege enforcement */
3241 __private_extern__
int
3242 icmp6_dgram_attach(struct socket
*so
, int proto
, struct proc
*p
)
3247 inp
= sotoinpcb(so
);
3249 panic("icmp6_dgram_attach");
3252 if (proto
!= IPPROTO_ICMPV6
) {
3256 error
= soreserve(so
, rip_sendspace
, rip_recvspace
);
3260 error
= in_pcballoc(so
, &ripcbinfo
, p
);
3264 inp
= (struct inpcb
*)so
->so_pcb
;
3265 inp
->inp_vflag
|= INP_IPV6
;
3266 inp
->in6p_ip6_nxt
= IPPROTO_ICMPV6
;
3267 inp
->in6p_hops
= -1; /* use kernel default */
3268 inp
->in6p_cksum
= -1;
3269 MALLOC(inp
->in6p_icmp6filt
, struct icmp6_filter
*,
3270 sizeof(struct icmp6_filter
), M_PCB
, M_WAITOK
);
3271 if (inp
->in6p_icmp6filt
== NULL
) {
3274 ICMP6_FILTER_SETPASSALL(inp
->in6p_icmp6filt
);
3280 * Perform rate limit check.
3281 * Returns 0 if it is okay to send the icmp6 packet.
3282 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
3285 * XXX per-destination check necessary?
3289 __unused
const struct in6_addr
*dst
, /* not used at this moment */
3291 __unused
const int code
)
3295 ret
= 0; /* okay to send */
3298 if (type
== ND_ROUTER_ADVERT
) {
3299 if (!ppsratecheck(&icmp6rappslim_last
, &icmp6rapps_count
,
3303 } else if (!ppsratecheck(&icmp6errppslim_last
, &icmp6errpps_count
,
3306 * We add some randomness here to still generate ICMPv6 error
3307 * post icmp6errppslim limit with a probability that goes down
3308 * with increased value of icmp6errpps_count.
3310 if (icmp6errpps_count
> 0 && icmp6errppslim
> 0 &&
3311 icmp6errpps_count
> icmp6errppslim
&&
3312 (random() % (icmp6errpps_count
- icmp6errppslim
)) != 0) {
3313 /* The packet is subject to rate limit */