2 * Copyright (c) 2000-2019 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 icmp6rappslim
;
152 static int icmp6errpps_count
= 0;
153 static int icmp6rapps_count
= 0;
154 static struct timeval icmp6errppslim_last
;
155 static struct timeval icmp6rappslim_last
;
156 extern int icmp6_nodeinfo
;
157 extern struct inpcbinfo ripcbinfo
;
159 static void icmp6_errcount(struct icmp6errstat
*, int, int);
160 static int icmp6_rip6_input(struct mbuf
**, int);
161 static int icmp6_ratelimit(const struct in6_addr
*, const int, const int);
162 static const char *icmp6_redirect_diag(struct in6_addr
*,
163 struct in6_addr
*, struct in6_addr
*);
164 static struct mbuf
*ni6_input(struct mbuf
*, int);
165 static struct mbuf
*ni6_nametodns(const char *, int, int);
166 static int ni6_dnsmatch(const char *, int, const char *, int);
167 static int ni6_addrs(struct icmp6_nodeinfo
*,
168 struct ifnet
**, char *);
169 static int ni6_store_addrs(struct icmp6_nodeinfo
*, struct icmp6_nodeinfo
*,
170 struct ifnet
*, int);
171 static int icmp6_notify_error(struct mbuf
*, int, int, int);
176 icmp6_init(struct ip6protosw
*pp
, struct domain
*dp
)
179 static int icmp6_initialized
= 0;
181 /* Also called from ip6_init() without pp */
183 (pp
->pr_flags
& (PR_INITIALIZED
| PR_ATTACHED
)) == PR_ATTACHED
);
185 /* This gets called by more than one protocols, so initialize once */
186 if (!icmp6_initialized
) {
187 icmp6_initialized
= 1;
193 icmp6_errcount(struct icmp6errstat
*stat
, int type
, int code
)
196 case ICMP6_DST_UNREACH
:
198 case ICMP6_DST_UNREACH_NOROUTE
:
199 stat
->icp6errs_dst_unreach_noroute
++;
201 case ICMP6_DST_UNREACH_ADMIN
:
202 stat
->icp6errs_dst_unreach_admin
++;
204 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
205 stat
->icp6errs_dst_unreach_beyondscope
++;
207 case ICMP6_DST_UNREACH_ADDR
:
208 stat
->icp6errs_dst_unreach_addr
++;
210 case ICMP6_DST_UNREACH_NOPORT
:
211 stat
->icp6errs_dst_unreach_noport
++;
215 case ICMP6_PACKET_TOO_BIG
:
216 stat
->icp6errs_packet_too_big
++;
218 case ICMP6_TIME_EXCEEDED
:
220 case ICMP6_TIME_EXCEED_TRANSIT
:
221 stat
->icp6errs_time_exceed_transit
++;
223 case ICMP6_TIME_EXCEED_REASSEMBLY
:
224 stat
->icp6errs_time_exceed_reassembly
++;
228 case ICMP6_PARAM_PROB
:
230 case ICMP6_PARAMPROB_HEADER
:
231 stat
->icp6errs_paramprob_header
++;
233 case ICMP6_PARAMPROB_NEXTHEADER
:
234 stat
->icp6errs_paramprob_nextheader
++;
236 case ICMP6_PARAMPROB_OPTION
:
237 stat
->icp6errs_paramprob_option
++;
242 stat
->icp6errs_redirect
++;
245 stat
->icp6errs_unknown
++;
249 * A wrapper function for icmp6_error() necessary when the erroneous packet
250 * may not contain enough scope zone information.
253 icmp6_error2(struct mbuf
*m
, int type
, int code
, int param
,
262 #ifndef PULLDOWN_TEST
263 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
), return );
265 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
266 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
273 ip6
= mtod(m
, struct ip6_hdr
*);
275 if (in6_setscope(&ip6
->ip6_src
, ifp
, NULL
) != 0) {
278 if (in6_setscope(&ip6
->ip6_dst
, ifp
, NULL
) != 0) {
282 icmp6_error(m
, type
, code
, param
);
286 * Generate an error packet of type error in response to bad IP6 packet.
289 icmp6_error(struct mbuf
*m
, int type
, int code
, int param
)
291 icmp6_error_flag(m
, type
, code
, param
, ICMP6_ERROR_RST_MRCVIF
);
295 icmp6_error_flag(struct mbuf
*m
, int type
, int code
, int param
, int flags
)
297 struct ip6_hdr
*oip6
, *nip6
;
298 struct icmp6_hdr
*icmp6
;
303 icmp6stat
.icp6s_error
++;
305 /* count per-type-code statistics */
306 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, type
, code
);
308 #ifdef M_DECRYPTED /*not openbsd*/
309 if (m
->m_flags
& M_DECRYPTED
) {
310 icmp6stat
.icp6s_canterror
++;
315 #ifndef PULLDOWN_TEST
316 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
), return );
318 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
319 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
325 oip6
= mtod(m
, struct ip6_hdr
*);
328 * If the destination address of the erroneous packet is a multicast
329 * address, or the packet was sent using link-layer multicast,
330 * we should basically suppress sending an error (RFC 2463, Section
332 * We have two exceptions (the item e.2 in that section):
333 * - the Pakcet Too Big message can be sent for path MTU discovery.
334 * - the Parameter Problem Message that can be allowed an icmp6 error
335 * in the option type field. This check has been done in
336 * ip6_unknown_opt(), so we can just check the type and code.
338 if ((m
->m_flags
& (M_BCAST
| M_MCAST
) ||
339 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_dst
)) &&
340 (type
!= ICMP6_PACKET_TOO_BIG
&&
341 (type
!= ICMP6_PARAM_PROB
||
342 code
!= ICMP6_PARAMPROB_OPTION
))) {
347 * RFC 2463, 2.4 (e.5): source address check.
348 * XXX: the case of anycast source?
350 if (IN6_IS_ADDR_UNSPECIFIED(&oip6
->ip6_src
) ||
351 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_src
)) {
356 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
360 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
361 if (off
>= 0 && nxt
== IPPROTO_ICMPV6
) {
362 struct icmp6_hdr
*icp
;
364 #ifndef PULLDOWN_TEST
365 IP6_EXTHDR_CHECK(m
, 0, off
+ sizeof(struct icmp6_hdr
), return );
366 icp
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
368 IP6_EXTHDR_GET(icp
, struct icmp6_hdr
*, m
, off
,
371 icmp6stat
.icp6s_tooshort
++;
375 if (icp
->icmp6_type
< ICMP6_ECHO_REQUEST
||
376 icp
->icmp6_type
== ND_REDIRECT
) {
379 * Special case: for redirect (which is
380 * informational) we must not send icmp6 error.
382 icmp6stat
.icp6s_canterror
++;
385 /* ICMPv6 informational - send the error */
388 /* non-ICMPv6 - send the error */
391 oip6
= mtod(m
, struct ip6_hdr
*); /* adjust pointer */
393 /* Finally, do rate limitation check. */
394 if (icmp6_ratelimit(&oip6
->ip6_src
, type
, code
)) {
395 icmp6stat
.icp6s_toofreq
++;
400 * OK, ICMP6 can be generated.
403 if (m
->m_pkthdr
.len
>= ICMPV6_PLD_MAXLEN
) {
404 m_adj(m
, ICMPV6_PLD_MAXLEN
- m
->m_pkthdr
.len
);
407 preplen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
408 M_PREPEND(m
, preplen
, M_DONTWAIT
, 1);
409 if (m
&& m
->m_len
< preplen
) {
410 m
= m_pullup(m
, preplen
);
413 nd6log((LOG_DEBUG
, "ENOBUFS in icmp6_error %d\n", __LINE__
));
417 nip6
= mtod(m
, struct ip6_hdr
*);
418 nip6
->ip6_src
= oip6
->ip6_src
;
419 nip6
->ip6_dst
= oip6
->ip6_dst
;
421 in6_clearscope(&oip6
->ip6_src
);
422 in6_clearscope(&oip6
->ip6_dst
);
424 icmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
425 icmp6
->icmp6_type
= type
;
426 icmp6
->icmp6_code
= code
;
427 icmp6
->icmp6_pptr
= htonl((u_int32_t
)param
);
430 * icmp6_reflect() is designed to be in the input path.
431 * icmp6_error() can be called from both input and output path,
432 * and if we are in output path rcvif could contain bogus value.
433 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
434 * information in ip header (nip6).
436 if (flags
& ICMP6_ERROR_RST_MRCVIF
) {
437 m
->m_pkthdr
.rcvif
= NULL
;
440 icmp6stat
.icp6s_outhist
[type
]++;
441 icmp6_reflect(m
, sizeof(struct ip6_hdr
)); /* header order: IPv6 - ICMPv6 */
447 * If we can't tell whether or not we can generate ICMP6, free it.
453 * Process a received ICMP6 message.
456 icmp6_input(struct mbuf
**mp
, int *offp
, int proto
)
458 #pragma unused(proto)
459 struct mbuf
*m
= *mp
, *n
;
461 struct ip6_hdr
*ip6
, *nip6
;
462 struct icmp6_hdr
*icmp6
, *nicmp6
;
464 int icmp6len
= m
->m_pkthdr
.len
- *offp
;
465 int code
, sum
, noff
, proxy
= 0;
467 ifp
= m
->m_pkthdr
.rcvif
;
469 #ifndef PULLDOWN_TEST
470 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_hdr
), return IPPROTO_DONE
);
471 /* m might change if M_LOOP. So, call mtod after this */
474 /* Expect 32-bit aligned data pointer on strict-align platforms */
475 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
478 * Locate icmp6 structure in mbuf, and check
479 * that not corrupted and of at least minimum length
481 ip6
= mtod(m
, struct ip6_hdr
*);
482 if (icmp6len
< sizeof(struct icmp6_hdr
)) {
483 icmp6stat
.icp6s_tooshort
++;
487 #ifndef PULLDOWN_TEST
488 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
490 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
492 icmp6stat
.icp6s_tooshort
++;
496 code
= icmp6
->icmp6_code
;
499 * Early check for RFC 6980
500 * Drop certain NDP packets if they came in fragmented
502 switch (icmp6
->icmp6_type
) {
503 case ND_ROUTER_SOLICIT
:
504 case ND_ROUTER_ADVERT
:
505 case ND_NEIGHBOR_SOLICIT
:
506 case ND_NEIGHBOR_ADVERT
:
508 if (m
->m_pkthdr
.pkt_flags
& PKTF_REASSEMBLED
) {
509 icmp6stat
.icp6s_rfc6980_drop
++;
517 /* Apply rate limit before checksum validation. */
518 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
519 icmp6stat
.icp6s_toofreq
++;
524 * Check multicast group membership.
525 * Note: SSM filters are not applied for ICMPv6 traffic.
527 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
528 struct in6_multi
*inm
;
530 in6_multihead_lock_shared();
531 IN6_LOOKUP_MULTI(&ip6
->ip6_dst
, ifp
, inm
);
532 in6_multihead_lock_done();
536 * Don't discard if this is a Neighbor Solicitation
537 * that needs to be proxied (see check down below.)
539 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_PROXY_DST
)) {
540 ip6stat
.ip6s_notmember
++;
541 in6_ifstat_inc(m
->m_pkthdr
.rcvif
,
551 * calculate the checksum
553 if ((sum
= in6_cksum(m
, IPPROTO_ICMPV6
, off
, icmp6len
)) != 0) {
555 "ICMP6 checksum error(%d|%x) %s\n",
556 icmp6
->icmp6_type
, sum
, ip6_sprintf(&ip6
->ip6_src
)));
557 icmp6stat
.icp6s_checksum
++;
561 if (m
->m_pkthdr
.pkt_flags
& PKTF_PROXY_DST
) {
563 * This is the special case of proxying NS (dst is either
564 * solicited-node multicast or unicast); process it locally
565 * but don't deliver it to sockets. It practically lets us
566 * steer the packet to nd6_prproxy_ns_input, where more
567 * specific tests and actions will be taken.
569 switch (icmp6
->icmp6_type
) {
570 case ND_NEIGHBOR_SOLICIT
:
578 icmp6stat
.icp6s_inhist
[icmp6
->icmp6_type
]++;
579 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_msg
);
580 if (icmp6
->icmp6_type
< ICMP6_INFOMSG_MASK
) {
581 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_error
);
584 switch (icmp6
->icmp6_type
) {
585 case ICMP6_DST_UNREACH
:
586 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_dstunreach
);
588 case ICMP6_DST_UNREACH_NOROUTE
:
589 case ICMP6_DST_UNREACH_ADDR
: /* PRC_HOSTDEAD is a DOS */
590 code
= PRC_UNREACH_NET
;
592 case ICMP6_DST_UNREACH_ADMIN
:
593 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_adminprohib
);
594 code
= PRC_UNREACH_PROTOCOL
; /* is this a good code? */
596 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
597 /* I mean "source address was incorrect." */
598 code
= PRC_PARAMPROB
;
600 case ICMP6_DST_UNREACH_NOPORT
:
601 code
= PRC_UNREACH_PORT
;
608 case ICMP6_PACKET_TOO_BIG
:
609 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_pkttoobig
);
614 * Updating the path MTU will be done after examining
615 * intermediate extension headers.
619 case ICMP6_TIME_EXCEEDED
:
620 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_timeexceed
);
622 case ICMP6_TIME_EXCEED_TRANSIT
:
623 code
= PRC_TIMXCEED_INTRANS
;
625 case ICMP6_TIME_EXCEED_REASSEMBLY
:
626 code
= PRC_TIMXCEED_REASS
;
633 case ICMP6_PARAM_PROB
:
634 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_paramprob
);
636 case ICMP6_PARAMPROB_NEXTHEADER
:
637 code
= PRC_UNREACH_PROTOCOL
;
639 case ICMP6_PARAMPROB_HEADER
:
640 case ICMP6_PARAMPROB_OPTION
:
641 code
= PRC_PARAMPROB
;
648 case ICMP6_ECHO_REQUEST
:
649 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echo
);
654 if ((n
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
656 goto rate_limit_checked
;
658 if ((n
->m_flags
& M_EXT
) != 0
659 || n
->m_len
< off
+ sizeof(struct icmp6_hdr
)) {
661 const int maxlen
= sizeof(*nip6
) + sizeof(*nicmp6
);
664 * Prepare an internal mbuf. m_pullup() doesn't
665 * always copy the length we specified.
667 if (maxlen
>= MCLBYTES
) {
670 goto rate_limit_checked
;
672 MGETHDR(n
, M_DONTWAIT
, n0
->m_type
); /* MAC-OK */
673 if (n
&& maxlen
>= MHLEN
) {
674 MCLGET(n
, M_DONTWAIT
);
675 if ((n
->m_flags
& M_EXT
) == 0) {
683 goto rate_limit_checked
;
685 M_COPY_PKTHDR(n
, n0
);
687 * Copy IPv6 and ICMPv6 only.
689 nip6
= mtod(n
, struct ip6_hdr
*);
690 bcopy(ip6
, nip6
, sizeof(struct ip6_hdr
));
691 nicmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
692 bcopy(icmp6
, nicmp6
, sizeof(struct icmp6_hdr
));
693 noff
= sizeof(struct ip6_hdr
);
694 n
->m_pkthdr
.len
= n
->m_len
=
695 noff
+ sizeof(struct icmp6_hdr
);
697 * Adjust mbuf. ip6_plen will be adjusted in
700 m_adj(n0
, off
+ sizeof(struct icmp6_hdr
));
701 n
->m_pkthdr
.len
+= n0
->m_pkthdr
.len
;
703 n0
->m_flags
&= ~M_PKTHDR
;
705 nip6
= mtod(n
, struct ip6_hdr
*);
706 IP6_EXTHDR_GET(nicmp6
, struct icmp6_hdr
*, n
, off
,
710 if (nicmp6
== NULL
) {
711 panic("nicmp6 is NULL in %s, which isn't good!\n", __FUNCTION__
);
713 nicmp6
->icmp6_type
= ICMP6_ECHO_REPLY
;
714 nicmp6
->icmp6_code
= 0;
717 icmp6stat
.icp6s_reflect
++;
718 icmp6stat
.icp6s_outhist
[ICMP6_ECHO_REPLY
]++;
719 icmp6_reflect(n
, noff
);
721 goto rate_limit_checked
;
723 case ICMP6_ECHO_REPLY
:
724 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echoreply
);
730 case MLD_LISTENER_QUERY
:
731 case MLD_LISTENER_REPORT
:
733 if (icmp6len
< sizeof(struct mld_hdr
)) {
736 if (icmp6
->icmp6_type
== MLD_LISTENER_QUERY
) { /* XXX: ugly... */
737 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldquery
);
739 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldreport
);
742 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
744 if (mld_input(m
, off
, icmp6len
) == IPPROTO_DONE
) {
749 if (mld_input(n
, off
, icmp6len
) != IPPROTO_DONE
) {
753 goto rate_limit_checked
;
755 case MLD_LISTENER_DONE
:
756 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mlddone
);
757 if (icmp6len
< sizeof(struct mld_hdr
)) { /* necessary? */
760 break; /* nothing to be done in kernel */
762 case MLD_MTRACE_RESP
:
764 /* XXX: these two are experimental. not officially defined. */
765 /* XXX: per-interface statistics? */
766 break; /* just pass it to applications */
769 if (!icmp6_nodeinfo
) {
772 //### LD 10/20 Check fbsd differences here. Not sure we're more advanced or not.
773 /* By RFC 4620 refuse to answer queries from global scope addresses */
774 if ((icmp6_nodeinfo
& 8) != 8 && in6_addrscope(&ip6
->ip6_src
) == IPV6_ADDR_SCOPE_GLOBAL
) {
778 if (icmp6len
< sizeof(struct icmp6_nodeinfo
)) {
782 #ifndef PULLDOWN_TEST
783 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_nodeinfo
),
784 return IPPROTO_DONE
);
787 n
= m_copy(m
, 0, M_COPYALL
);
789 n
= ni6_input(n
, off
);
792 noff
= sizeof(struct ip6_hdr
);
793 icmp6stat
.icp6s_reflect
++;
794 icmp6stat
.icp6s_outhist
[ICMP6_WRUREPLY
]++;
795 icmp6_reflect(n
, noff
);
797 goto rate_limit_checked
;
805 case ND_ROUTER_SOLICIT
:
806 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routersolicit
);
810 if (icmp6len
< sizeof(struct nd_router_solicit
)) {
814 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
816 nd6_rs_input(m
, off
, icmp6len
);
820 nd6_rs_input(n
, off
, icmp6len
);
822 goto rate_limit_checked
;
824 case ND_ROUTER_ADVERT
:
825 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routeradvert
);
829 if (icmp6len
< sizeof(struct nd_router_advert
)) {
833 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
835 nd6_ra_input(m
, off
, icmp6len
);
839 nd6_ra_input(n
, off
, icmp6len
);
841 goto rate_limit_checked
;
843 case ND_NEIGHBOR_SOLICIT
:
844 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighborsolicit
);
848 if (icmp6len
< sizeof(struct nd_neighbor_solicit
)) {
853 ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
)) {
855 nd6_ns_input(m
, off
, icmp6len
);
859 nd6_ns_input(n
, off
, icmp6len
);
861 goto rate_limit_checked
;
863 case ND_NEIGHBOR_ADVERT
:
864 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighboradvert
);
868 if (icmp6len
< sizeof(struct nd_neighbor_advert
)) {
872 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
874 nd6_na_input(m
, off
, icmp6len
);
878 nd6_na_input(n
, off
, icmp6len
);
880 goto rate_limit_checked
;
883 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_redirect
);
887 if (icmp6len
< sizeof(struct nd_redirect
)) {
891 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
893 icmp6_redirect_input(m
, off
);
897 icmp6_redirect_input(n
, off
);
899 goto rate_limit_checked
;
901 case ICMP6_ROUTER_RENUMBERING
:
902 if (code
!= ICMP6_ROUTER_RENUMBERING_COMMAND
&&
903 code
!= ICMP6_ROUTER_RENUMBERING_RESULT
) {
906 if (icmp6len
< sizeof(struct icmp6_router_renum
)) {
913 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
914 icmp6
->icmp6_type
, ip6_sprintf(&ip6
->ip6_src
),
915 ip6_sprintf(&ip6
->ip6_dst
),
916 m
->m_pkthdr
.rcvif
? m
->m_pkthdr
.rcvif
->if_index
: 0));
917 if (icmp6
->icmp6_type
< ICMP6_ECHO_REQUEST
) {
918 /* ICMPv6 error: MUST deliver it by spec... */
922 /* ICMPv6 informational: MUST not deliver */
923 goto rate_limit_checked
;
926 if (icmp6_notify_error(m
, off
, icmp6len
, code
)) {
927 /* In this case, m should've been freed. */
933 icmp6stat
.icp6s_badcode
++;
937 icmp6stat
.icp6s_badlen
++;
942 icmp6_rip6_input(&m
, *offp
);
951 icmp6_notify_error(struct mbuf
*m
, int off
, int icmp6len
, int code
)
953 struct icmp6_hdr
*icmp6
;
954 struct ip6_hdr
*eip6
;
956 struct sockaddr_in6 icmp6src
, icmp6dst
;
958 if (icmp6len
< sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
)) {
959 icmp6stat
.icp6s_tooshort
++;
962 #ifndef PULLDOWN_TEST
963 IP6_EXTHDR_CHECK(m
, off
,
964 sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
),
966 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
968 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
969 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
971 icmp6stat
.icp6s_tooshort
++;
975 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
977 /* Detect the upper level protocol */
979 void (*ctlfunc
)(int, struct sockaddr
*, void *, struct ifnet
*);
980 u_int8_t nxt
= eip6
->ip6_nxt
;
981 int eoff
= off
+ sizeof(struct icmp6_hdr
) +
982 sizeof(struct ip6_hdr
);
983 struct ip6ctlparam ip6cp
;
984 struct in6_addr
*finaldst
= NULL
;
985 int icmp6type
= icmp6
->icmp6_type
;
987 struct ip6_rthdr
*rth
;
988 struct ip6_rthdr0
*rth0
;
991 while (1) { /* XXX: should avoid infinite loop explicitly? */
995 case IPPROTO_HOPOPTS
:
996 case IPPROTO_DSTOPTS
:
998 #ifndef PULLDOWN_TEST
999 IP6_EXTHDR_CHECK(m
, 0,
1000 eoff
+ sizeof(struct ip6_ext
), return -1);
1001 eh
= (struct ip6_ext
*)(mtod(m
, caddr_t
)
1004 IP6_EXTHDR_GET(eh
, struct ip6_ext
*, m
,
1007 icmp6stat
.icp6s_tooshort
++;
1012 if (nxt
== IPPROTO_AH
) {
1013 eoff
+= (eh
->ip6e_len
+ 2) << 2;
1015 eoff
+= (eh
->ip6e_len
+ 1) << 3;
1019 case IPPROTO_ROUTING
:
1021 * When the erroneous packet contains a
1022 * routing header, we should examine the
1023 * header to determine the final destination.
1024 * Otherwise, we can't properly update
1025 * information that depends on the final
1026 * destination (e.g. path MTU).
1028 #ifndef PULLDOWN_TEST
1029 IP6_EXTHDR_CHECK(m
, 0, eoff
+ sizeof(*rth
),
1031 rth
= (struct ip6_rthdr
*)
1032 (mtod(m
, caddr_t
) + eoff
);
1034 IP6_EXTHDR_GET(rth
, struct ip6_rthdr
*, m
,
1035 eoff
, sizeof(*rth
));
1037 icmp6stat
.icp6s_tooshort
++;
1041 rthlen
= (rth
->ip6r_len
+ 1) << 3;
1043 * XXX: currently there is no
1044 * officially defined type other
1046 * Note that if the segment left field
1047 * is 0, all intermediate hops must
1050 if (rth
->ip6r_segleft
&&
1051 rth
->ip6r_type
== IPV6_RTHDR_TYPE_0
) {
1054 #ifndef PULLDOWN_TEST
1055 IP6_EXTHDR_CHECK(m
, 0, eoff
+ rthlen
,
1057 rth0
= (struct ip6_rthdr0
*)(mtod(m
, caddr_t
) + eoff
);
1059 IP6_EXTHDR_GET(rth0
,
1060 struct ip6_rthdr0
*, m
,
1063 icmp6stat
.icp6s_tooshort
++;
1067 /* just ignore a bogus header */
1068 if ((rth0
->ip6r0_len
% 2) == 0 &&
1069 (hops
= rth0
->ip6r0_len
/ 2)) {
1070 finaldst
= (struct in6_addr
*)(void *)(rth0
+ 1) + (hops
- 1);
1074 nxt
= rth
->ip6r_nxt
;
1076 case IPPROTO_FRAGMENT
:
1077 #ifndef PULLDOWN_TEST
1078 IP6_EXTHDR_CHECK(m
, 0, eoff
+
1079 sizeof(struct ip6_frag
),
1081 fh
= (struct ip6_frag
*)(mtod(m
, caddr_t
)
1084 IP6_EXTHDR_GET(fh
, struct ip6_frag
*, m
,
1087 icmp6stat
.icp6s_tooshort
++;
1092 * Data after a fragment header is meaningless
1093 * unless it is the first fragment, but
1094 * we'll go to the notify label for path MTU
1097 if (fh
->ip6f_offlg
& IP6F_OFF_MASK
) {
1101 eoff
+= sizeof(struct ip6_frag
);
1106 * This case includes ESP and the No Next
1107 * Header. In such cases going to the notify
1108 * label does not have any meaning
1109 * (i.e. ctlfunc will be NULL), but we go
1110 * anyway since we might have to update
1111 * path MTU information.
1117 #ifndef PULLDOWN_TEST
1118 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
1120 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
1121 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
1122 if (icmp6
== NULL
) {
1123 icmp6stat
.icp6s_tooshort
++;
1129 * retrieve parameters from the inner IPv6 header, and convert
1130 * them into sockaddr structures.
1131 * XXX: there is no guarantee that the source or destination
1132 * addresses of the inner packet are in the same scope as
1133 * the addresses of the icmp packet. But there is no other
1134 * way to determine the zone.
1136 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1138 bzero(&icmp6dst
, sizeof(icmp6dst
));
1139 icmp6dst
.sin6_len
= sizeof(struct sockaddr_in6
);
1140 icmp6dst
.sin6_family
= AF_INET6
;
1141 if (finaldst
== NULL
) {
1142 icmp6dst
.sin6_addr
= eip6
->ip6_dst
;
1144 icmp6dst
.sin6_addr
= *finaldst
;
1146 if (in6_setscope(&icmp6dst
.sin6_addr
, m
->m_pkthdr
.rcvif
, NULL
)) {
1149 bzero(&icmp6src
, sizeof(icmp6src
));
1150 icmp6src
.sin6_len
= sizeof(struct sockaddr_in6
);
1151 icmp6src
.sin6_family
= AF_INET6
;
1152 icmp6src
.sin6_addr
= eip6
->ip6_src
;
1153 if (in6_setscope(&icmp6src
.sin6_addr
, m
->m_pkthdr
.rcvif
, NULL
)) {
1156 icmp6src
.sin6_flowinfo
=
1157 (eip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
);
1159 if (finaldst
== NULL
) {
1160 finaldst
= &eip6
->ip6_dst
;
1163 ip6cp
.ip6c_icmp6
= icmp6
;
1164 ip6cp
.ip6c_ip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1165 ip6cp
.ip6c_off
= eoff
;
1166 ip6cp
.ip6c_finaldst
= finaldst
;
1167 ip6cp
.ip6c_src
= &icmp6src
;
1168 ip6cp
.ip6c_nxt
= nxt
;
1170 if (icmp6type
== ICMP6_PACKET_TOO_BIG
) {
1171 notifymtu
= ntohl(icmp6
->icmp6_mtu
);
1172 ip6cp
.ip6c_cmdarg
= (void *)¬ifymtu
;
1173 icmp6_mtudisc_update(&ip6cp
, 1); /*XXX*/
1176 ctlfunc
= ip6_protox
[nxt
]->pr_ctlinput
;
1178 LCK_MTX_ASSERT(inet6_domain_mutex
, LCK_MTX_ASSERT_OWNED
);
1180 lck_mtx_unlock(inet6_domain_mutex
);
1182 (void) (*ctlfunc
)(code
, (struct sockaddr
*)&icmp6dst
,
1183 &ip6cp
, m
->m_pkthdr
.rcvif
);
1185 lck_mtx_lock(inet6_domain_mutex
);
1196 icmp6_mtudisc_update(struct ip6ctlparam
*ip6cp
, int validated
)
1198 struct in6_addr
*dst
= ip6cp
->ip6c_finaldst
;
1199 struct icmp6_hdr
*icmp6
= ip6cp
->ip6c_icmp6
;
1200 struct mbuf
*m
= ip6cp
->ip6c_m
; /* will be necessary for scope issue */
1201 u_int mtu
= ntohl(icmp6
->icmp6_mtu
);
1202 struct rtentry
*rt
= NULL
;
1203 struct sockaddr_in6 sin6
;
1205 * we reject ICMPv6 too big with abnormally small value.
1206 * XXX what is the good definition of "abnormally small"?
1208 if (mtu
< sizeof(struct ip6_hdr
) + sizeof(struct ip6_frag
) + 8) {
1217 * In case the suggested mtu is less than IPV6_MMTU, we
1218 * only need to remember that it was for above mentioned
1219 * "alwaysfrag" case.
1220 * Try to be as close to the spec as possible.
1222 if (mtu
< IPV6_MMTU
) {
1223 mtu
= IPV6_MMTU
- 8;
1226 bzero(&sin6
, sizeof(sin6
));
1227 sin6
.sin6_family
= PF_INET6
;
1228 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1229 sin6
.sin6_addr
= *dst
;
1230 /* XXX normally, this won't happen */
1231 if (IN6_IS_ADDR_LINKLOCAL(dst
)) {
1232 sin6
.sin6_addr
.s6_addr16
[1] =
1233 htons(m
->m_pkthdr
.rcvif
->if_index
);
1235 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1237 * XXX On a side note, for asymmetric data-path
1238 * the lookup on receive interace is probably not
1239 * what we want to do.
1240 * That requires looking at the cached route for the
1241 * protocol control block.
1243 rt
= rtalloc1_scoped((struct sockaddr
*)&sin6
, 0,
1244 RTF_CLONING
| RTF_PRCLONING
, m
->m_pkthdr
.rcvif
->if_index
);
1247 if ((rt
->rt_flags
& RTF_HOST
) &&
1248 !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) &&
1249 mtu
< IN6_LINKMTU(rt
->rt_ifp
) &&
1250 rt
->rt_rmx
.rmx_mtu
> mtu
) {
1251 icmp6stat
.icp6s_pmtuchg
++;
1252 rt
->rt_rmx
.rmx_mtu
= mtu
;
1260 * Process a Node Information Query packet, based on
1261 * draft-ietf-ipngwg-icmp-name-lookups-07.
1263 * Spec incompatibilities:
1264 * - IPv6 Subject address handling
1265 * - IPv4 Subject address handling support missing
1266 * - Proxy reply (answer even if it's not for me)
1267 * - joins NI group address at in6_ifattach() time only, does not cope
1268 * with hostname changes by sethostname(3)
1270 #define hostnamelen strlen(hostname)
1271 static struct mbuf
*
1272 ni6_input(struct mbuf
*m
, int off
)
1274 struct icmp6_nodeinfo
*ni6
, *nni6
;
1275 struct mbuf
*n
= NULL
;
1278 int replylen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1279 struct ni_reply_fqdn
*fqdn
;
1280 int addrs
; /* for NI_QTYPE_NODEADDR */
1281 struct ifnet
*ifp
= NULL
; /* for NI_QTYPE_NODEADDR */
1282 struct sockaddr_in6 sin6
; /* double meaning; ip6_dst and subjectaddr */
1283 struct sockaddr_in6 sin6_d
; /* XXX: we should retrieve this from m_aux */
1284 struct ip6_hdr
*ip6
;
1285 int oldfqdn
= 0; /* if 1, return pascal string (03 draft) */
1288 ip6
= mtod(m
, struct ip6_hdr
*);
1289 #ifndef PULLDOWN_TEST
1290 ni6
= (struct icmp6_nodeinfo
*)(mtod(m
, caddr_t
) + off
);
1292 IP6_EXTHDR_GET(ni6
, struct icmp6_nodeinfo
*, m
, off
, sizeof(*ni6
));
1294 /* m is already reclaimed */
1300 * Validate IPv6 source address.
1301 * The default configuration MUST be to refuse answering queries from
1302 * global-scope addresses according to RFC4602.
1304 * - it's not very clear what "refuse" means; this implementation
1306 * - it's not very easy to identify global-scope (unicast) addresses
1307 * since there are many prefixes for them. It should be safer
1308 * and in practice sufficient to check "all" but loopback and
1309 * link-local (note that site-local unicast was deprecated and
1310 * ULA is defined as global scope-wise)
1312 if ((icmp6_nodeinfo
& ICMP6_NODEINFO_GLOBALOK
) == 0 &&
1313 !IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
) &&
1314 !IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_src
)) {
1319 * Validate IPv6 destination address.
1321 * The Responder must discard the Query without further processing
1322 * unless it is one of the Responder's unicast or anycast addresses, or
1323 * a link-local scope multicast address which the Responder has joined.
1324 * [RFC4602, Section 5.]
1326 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1327 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6
->ip6_dst
)) {
1330 /* else it's a link-local multicast, fine */
1331 } else { /* unicast or anycast */
1334 if (ip6_getdstifaddr_info(m
, NULL
, &ia6_flags
) != 0) {
1335 goto bad
; /* XXX impossible */
1337 if ((ia6_flags
& IN6_IFF_TEMPORARY
) &&
1338 !(icmp6_nodeinfo
& ICMP6_NODEINFO_TMPADDROK
)) {
1339 nd6log((LOG_DEBUG
, "ni6_input: ignore node info to "
1340 "a temporary address in %s:%d",
1341 __func__
, __LINE__
));
1346 /* validate query Subject field. */
1347 qtype
= ntohs(ni6
->ni_qtype
);
1348 subjlen
= m
->m_pkthdr
.len
- off
- sizeof(struct icmp6_nodeinfo
);
1351 case NI_QTYPE_SUPTYPES
:
1353 if (ni6
->ni_code
== ICMP6_NI_SUBJ_FQDN
&& subjlen
== 0) {
1358 case NI_QTYPE_NODEADDR
:
1359 case NI_QTYPE_IPV4ADDR
:
1360 switch (ni6
->ni_code
) {
1361 case ICMP6_NI_SUBJ_IPV6
:
1362 #if ICMP6_NI_SUBJ_IPV6 != 0
1366 * backward compatibility - try to accept 03 draft
1367 * format, where no Subject is present.
1369 if (qtype
== NI_QTYPE_FQDN
&& ni6
->ni_code
== 0 &&
1374 #if ICMP6_NI_SUBJ_IPV6 != 0
1375 if (ni6
->ni_code
!= ICMP6_NI_SUBJ_IPV6
) {
1380 if (subjlen
!= sizeof(struct in6_addr
)) {
1385 * Validate Subject address.
1387 * Not sure what exactly "address belongs to the node"
1388 * means in the spec, is it just unicast, or what?
1390 * At this moment we consider Subject address as
1391 * "belong to the node" if the Subject address equals
1392 * to the IPv6 destination address; validation for
1393 * IPv6 destination address should have done enough
1396 * We do not do proxy at this moment.
1398 /* m_pulldown instead of copy? */
1399 m_copydata(m
, off
+ sizeof(struct icmp6_nodeinfo
),
1400 subjlen
, (caddr_t
)&sin6
.sin6_addr
);
1401 sin6
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1403 in6_embedscope(&sin6
.sin6_addr
, &sin6
, NULL
, NULL
,
1405 bzero(&sin6_d
, sizeof(sin6_d
));
1406 sin6_d
.sin6_family
= AF_INET6
; /* not used, actually */
1407 sin6_d
.sin6_len
= sizeof(sin6_d
); /* ditto */
1408 sin6_d
.sin6_addr
= ip6
->ip6_dst
;
1409 sin6_d
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1411 in6_embedscope(&sin6_d
.sin6_addr
, &sin6_d
, NULL
, NULL
,
1413 subj
= (char *)&sin6
;
1414 if (SA6_ARE_ADDR_EQUAL(&sin6
, &sin6_d
)) {
1419 * XXX if we are to allow other cases, we should really
1420 * be careful about scope here.
1421 * basically, we should disallow queries toward IPv6
1422 * destination X with subject Y,
1423 * if scope(X) > scope(Y).
1424 * if we allow scope(X) > scope(Y), it will result in
1425 * information leakage across scope boundary.
1429 case ICMP6_NI_SUBJ_FQDN
:
1431 * Validate Subject name with gethostname(3).
1433 * The behavior may need some debate, since:
1434 * - we are not sure if the node has FQDN as
1435 * hostname (returned by gethostname(3)).
1436 * - the code does wildcard match for truncated names.
1437 * however, we are not sure if we want to perform
1438 * wildcard match, if gethostname(3) side has
1439 * truncated hostname.
1441 n
= ni6_nametodns(hostname
, hostnamelen
, 0);
1442 if (!n
|| n
->m_next
|| n
->m_len
== 0) {
1445 IP6_EXTHDR_GET(subj
, char *, m
,
1446 off
+ sizeof(struct icmp6_nodeinfo
), subjlen
);
1450 if (!ni6_dnsmatch(subj
, subjlen
, mtod(n
, const char *),
1458 case ICMP6_NI_SUBJ_IPV4
: /* XXX: to be implemented? */
1465 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1468 if ((icmp6_nodeinfo
& ICMP6_NODEINFO_FQDNOK
) == 0) {
1472 case NI_QTYPE_NODEADDR
:
1473 case NI_QTYPE_IPV4ADDR
:
1474 if ((icmp6_nodeinfo
& ICMP6_NODEINFO_NODEADDROK
) == 0) {
1480 /* guess reply length */
1483 break; /* no reply data */
1484 case NI_QTYPE_SUPTYPES
:
1485 replylen
+= sizeof(u_int32_t
);
1488 /* XXX will append an mbuf */
1489 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1491 case NI_QTYPE_NODEADDR
:
1492 addrs
= ni6_addrs(ni6
, &ifp
, subj
);
1493 if ((replylen
+= addrs
* (sizeof(struct in6_addr
) +
1494 sizeof(u_int32_t
))) > MCLBYTES
) {
1495 replylen
= MCLBYTES
; /* XXX: will truncate pkt later */
1498 case NI_QTYPE_IPV4ADDR
:
1499 /* unsupported - should respond with unknown Qtype? */
1503 * XXX: We must return a reply with the ICMP6 code
1504 * `unknown Qtype' in this case. However we regard the case
1505 * as an FQDN query for backward compatibility.
1506 * Older versions set a random value to this field,
1507 * so it rarely varies in the defined qtypes.
1508 * But the mechanism is not reliable...
1509 * maybe we should obsolete older versions.
1511 qtype
= NI_QTYPE_FQDN
;
1512 /* XXX will append an mbuf */
1513 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1518 /* allocate an mbuf to reply. */
1519 MGETHDR(n
, M_DONTWAIT
, m
->m_type
); /* MAC-OK */
1527 M_COPY_PKTHDR(n
, m
); /* just for recvif */
1528 if (replylen
> MHLEN
) {
1529 if (replylen
> MCLBYTES
) {
1531 * XXX: should we try to allocate more? But MCLBYTES
1532 * is probably much larger than IPV6_MMTU...
1536 MCLGET(n
, M_DONTWAIT
);
1537 if ((n
->m_flags
& M_EXT
) == 0) {
1541 n
->m_pkthdr
.len
= n
->m_len
= replylen
;
1543 /* copy mbuf header and IPv6 + Node Information base headers */
1544 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
), sizeof(struct ip6_hdr
));
1545 nni6
= (struct icmp6_nodeinfo
*)(mtod(n
, struct ip6_hdr
*) + 1);
1546 bcopy((caddr_t
)ni6
, (caddr_t
)nni6
, sizeof(struct icmp6_nodeinfo
));
1548 /* qtype dependent procedure */
1551 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1554 case NI_QTYPE_SUPTYPES
:
1557 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1558 nni6
->ni_flags
= htons(0x0000); /* raw bitmap */
1559 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1560 v
= (u_int32_t
)htonl(0x0000000f);
1561 bcopy(&v
, nni6
+ 1, sizeof(u_int32_t
));
1565 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1566 fqdn
= (struct ni_reply_fqdn
*)(mtod(n
, caddr_t
) +
1567 sizeof(struct ip6_hdr
) +
1568 sizeof(struct icmp6_nodeinfo
));
1569 nni6
->ni_flags
= 0; /* XXX: meaningless TTL */
1570 fqdn
->ni_fqdn_ttl
= 0; /* ditto. */
1572 * XXX do we really have FQDN in variable "hostname"?
1574 n
->m_next
= ni6_nametodns(hostname
, hostnamelen
, oldfqdn
);
1575 if (n
->m_next
== NULL
) {
1578 /* XXX we assume that n->m_next is not a chain */
1579 if (n
->m_next
->m_next
!= NULL
) {
1582 n
->m_pkthdr
.len
+= n
->m_next
->m_len
;
1584 case NI_QTYPE_NODEADDR
:
1588 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1589 n
->m_pkthdr
.len
= n
->m_len
=
1590 sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1591 lenlim
= M_TRAILINGSPACE(n
);
1592 copied
= ni6_store_addrs(ni6
, nni6
, ifp
, lenlim
);
1593 /* XXX: reset mbuf length */
1594 n
->m_pkthdr
.len
= n
->m_len
= sizeof(struct ip6_hdr
) +
1595 sizeof(struct icmp6_nodeinfo
) + copied
;
1599 break; /* XXX impossible! */
1602 nni6
->ni_type
= ICMP6_NI_REPLY
;
1622 * make a mbuf with DNS-encoded string. no compression support.
1624 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1625 * treated as truncated name (two \0 at the end). this is a wild guess.
1627 static struct mbuf
*
1631 int old
) /* return pascal string if non-zero */
1644 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1645 MGET(m
, M_DONTWAIT
, MT_DATA
);
1646 if (m
&& len
> MLEN
) {
1647 MCLGET(m
, M_DONTWAIT
);
1648 if ((m
->m_flags
& M_EXT
) == 0) {
1659 *mtod(m
, char *) = namelen
;
1660 bcopy(name
, mtod(m
, char *) + 1, namelen
);
1664 cp
= mtod(m
, char *);
1665 ep
= mtod(m
, char *) + M_TRAILINGSPACE(m
);
1667 /* if not certain about my name, return empty buffer */
1673 * guess if it looks like shortened hostname, or FQDN.
1674 * shortened hostname needs two trailing "\0".
1677 for (p
= name
; p
< name
+ namelen
; p
++) {
1678 if (*p
&& *p
== '.') {
1689 while (cp
< ep
&& p
< name
+ namelen
) {
1691 for (q
= p
; q
< name
+ namelen
&& *q
&& *q
!= '.'; q
++) {
1694 /* result does not fit into mbuf */
1695 if (cp
+ i
+ 1 >= ep
) {
1699 * DNS label length restriction, RFC1035 page 8.
1700 * "i == 0" case is included here to avoid returning
1701 * 0-length label on "foo..bar".
1703 if (i
<= 0 || i
>= 64) {
1710 if (p
< name
+ namelen
&& *p
== '.') {
1715 if (cp
+ nterm
>= ep
) {
1718 while (nterm
-- > 0) {
1721 m
->m_len
= cp
- mtod(m
, char *);
1725 panic("should not reach here");
1736 * check if two DNS-encoded string matches. takes care of truncated
1737 * form (with \0\0 at the end). no compression support.
1738 * XXX upper/lowercase match (see RFC2065)
1741 ni6_dnsmatch(const char *a
, int alen
, const char *b
, int blen
)
1743 const char *a0
, *b0
;
1746 /* simplest case - need validation? */
1747 if (alen
== blen
&& bcmp(a
, b
, alen
) == 0) {
1754 /* termination is mandatory */
1755 if (alen
< 2 || blen
< 2) {
1758 if (a0
[alen
- 1] != '\0' || b0
[blen
- 1] != '\0') {
1764 while (a
- a0
< alen
&& b
- b0
< blen
) {
1765 if (a
- a0
+ 1 > alen
|| b
- b0
+ 1 > blen
) {
1769 if ((signed char)a
[0] < 0 || (signed char)b
[0] < 0) {
1772 /* we don't support compression yet */
1773 if (a
[0] >= 64 || b
[0] >= 64) {
1777 /* truncated case */
1778 if (a
[0] == 0 && a
- a0
== alen
- 1) {
1781 if (b
[0] == 0 && b
- b0
== blen
- 1) {
1784 if (a
[0] == 0 || b
[0] == 0) {
1792 if (a
- a0
+ 1 + l
> alen
|| b
- b0
+ 1 + l
> blen
) {
1795 if (bcmp(a
+ 1, b
+ 1, l
) != 0) {
1803 if (a
- a0
== alen
&& b
- b0
== blen
) {
1811 * calculate the number of addresses to be returned in the node info reply.
1814 ni6_addrs(struct icmp6_nodeinfo
*ni6
, struct ifnet
**ifpp
, char *subj
)
1817 struct in6_ifaddr
*ifa6
;
1819 struct sockaddr_in6
*subj_ip6
= NULL
; /* XXX pedant */
1820 int addrs
= 0, addrsofif
, iffound
= 0;
1821 int niflags
= ni6
->ni_flags
;
1827 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0) {
1828 switch (ni6
->ni_code
) {
1829 case ICMP6_NI_SUBJ_IPV6
:
1830 if (subj
== NULL
) { /* must be impossible... */
1833 subj_ip6
= (struct sockaddr_in6
*)(void *)subj
;
1837 * XXX: we only support IPv6 subject address for
1844 ifnet_head_lock_shared();
1845 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
1847 ifnet_lock_shared(ifp
);
1848 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1851 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
1855 ifa6
= (struct in6_ifaddr
*)ifa
;
1857 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0 &&
1858 IN6_ARE_ADDR_EQUAL(&subj_ip6
->sin6_addr
,
1859 &ifa6
->ia_addr
.sin6_addr
)) {
1864 * IPv4-mapped addresses can only be returned by a
1865 * Node Information proxy, since they represent
1866 * addresses of IPv4-only nodes, which perforce do
1867 * not implement this protocol.
1868 * [icmp-name-lookups-07, Section 5.4]
1869 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1870 * this function at this moment.
1873 /* What do we have to do about ::1? */
1874 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1875 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1876 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0) {
1881 case IPV6_ADDR_SCOPE_SITELOCAL
:
1882 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0) {
1887 case IPV6_ADDR_SCOPE_GLOBAL
:
1888 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0) {
1899 * check if anycast is okay.
1900 * XXX: just experimental. not in the spec.
1902 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
1903 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0) {
1905 continue; /* we need only unicast addresses */
1907 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1908 (icmp6_nodeinfo
& ICMP6_NODEINFO_TMPADDROK
) == 0) {
1912 addrsofif
++; /* count the address */
1915 ifnet_lock_done(ifp
);
1919 ifnet_reference(ifp
);
1933 ni6_store_addrs(struct icmp6_nodeinfo
*ni6
, struct icmp6_nodeinfo
*nni6
,
1934 struct ifnet
*ifp0
, int resid
)
1936 struct ifnet
*ifp
= ifp0
;
1937 struct in6_ifaddr
*ifa6
;
1939 struct ifnet
*ifp_dep
= NULL
;
1940 int copied
= 0, allow_deprecated
= 0;
1941 u_char
*cp
= (u_char
*)(nni6
+ 1);
1942 int niflags
= ni6
->ni_flags
;
1944 uint64_t now
= net_uptime();
1946 if (ifp0
== NULL
&& !(niflags
& NI_NODEADDR_FLAG_ALL
)) {
1947 return 0; /* needless to copy */
1951 ifnet_head_lock_shared();
1953 ifp
= TAILQ_FIRST(&ifnet_head
);
1956 for (; ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
)) {
1957 ifnet_lock_shared(ifp
);
1958 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
1959 ifa
= ifa
->ifa_list
.tqe_next
) {
1960 struct in6_addrlifetime_i
*lt
;
1963 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
1967 ifa6
= (struct in6_ifaddr
*)ifa
;
1969 if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0 &&
1970 allow_deprecated
== 0) {
1972 * prefererred address should be put before
1973 * deprecated addresses.
1976 /* record the interface for later search */
1977 if (ifp_dep
== NULL
) {
1983 } else if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) == 0 &&
1984 allow_deprecated
!= 0) {
1986 continue; /* we now collect deprecated addrs */
1988 /* What do we have to do about ::1? */
1989 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1990 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1991 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0) {
1996 case IPV6_ADDR_SCOPE_SITELOCAL
:
1997 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0) {
2002 case IPV6_ADDR_SCOPE_GLOBAL
:
2003 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0) {
2014 * check if anycast is okay.
2015 * XXX: just experimental. not in the spec.
2017 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
2018 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0) {
2022 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
2023 (icmp6_nodeinfo
& ICMP6_NODEINFO_TMPADDROK
) == 0) {
2028 /* now we can copy the address */
2029 if (resid
< sizeof(struct in6_addr
) +
2030 sizeof(u_int32_t
)) {
2033 * We give up much more copy.
2034 * Set the truncate flag and return.
2037 NI_NODEADDR_FLAG_TRUNCATE
;
2038 ifnet_lock_done(ifp
);
2044 * Set the TTL of the address.
2045 * The TTL value should be one of the following
2046 * according to the specification:
2048 * 1. The remaining lifetime of a DHCP lease on the
2050 * 2. The remaining Valid Lifetime of a prefix from
2051 * which the address was derived through Stateless
2052 * Autoconfiguration.
2054 * Note that we currently do not support stateful
2055 * address configuration by DHCPv6, so the former
2056 * case can't happen.
2058 lt
= &ifa6
->ia6_lifetime
;
2059 if (lt
->ia6ti_expire
== 0) {
2060 ltime
= ND6_INFINITE_LIFETIME
;
2062 if (lt
->ia6ti_expire
> now
) {
2063 ltime
= htonl(lt
->ia6ti_expire
- now
);
2069 bcopy(<ime
, cp
, sizeof(u_int32_t
));
2070 cp
+= sizeof(u_int32_t
);
2072 /* copy the address itself */
2073 bcopy(&ifa6
->ia_addr
.sin6_addr
, cp
,
2074 sizeof(struct in6_addr
));
2075 /* XXX: KAME link-local hack; remove ifindex */
2076 if (IN6_IS_ADDR_LINKLOCAL(&ifa6
->ia_addr
.sin6_addr
)) {
2077 ((struct in6_addr
*)(void *)cp
)->s6_addr16
[1] = 0;
2079 cp
+= sizeof(struct in6_addr
);
2081 resid
-= (sizeof(struct in6_addr
) + sizeof(u_int32_t
));
2082 copied
+= (sizeof(struct in6_addr
) +
2086 ifnet_lock_done(ifp
);
2087 if (ifp0
) { /* we need search only on the specified IF */
2093 if (allow_deprecated
== 0 && ifp_dep
!= NULL
) {
2095 allow_deprecated
= 1;
2104 * XXX almost dup'ed code with rip6_input.
2107 icmp6_rip6_input(struct mbuf
**mp
, int off
)
2109 struct mbuf
*m
= *mp
;
2110 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2111 struct in6pcb
*in6p
;
2112 struct in6pcb
*last
= NULL
;
2113 struct sockaddr_in6 rip6src
;
2114 struct icmp6_hdr
*icmp6
;
2115 struct mbuf
*opts
= NULL
;
2117 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
2119 #ifndef PULLDOWN_TEST
2120 /* this is assumed to be safe. */
2121 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
2123 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
2124 if (icmp6
== NULL
) {
2125 /* m is already reclaimed */
2126 return IPPROTO_DONE
;
2131 * XXX: the address may have embedded scope zone ID, which should be
2132 * hidden from applications.
2134 bzero(&rip6src
, sizeof(rip6src
));
2135 rip6src
.sin6_family
= AF_INET6
;
2136 rip6src
.sin6_len
= sizeof(struct sockaddr_in6
);
2137 rip6src
.sin6_addr
= ip6
->ip6_src
;
2138 if (sa6_recoverscope(&rip6src
, TRUE
)) {
2139 return IPPROTO_DONE
;
2142 lck_rw_lock_shared(ripcbinfo
.ipi_lock
);
2143 LIST_FOREACH(in6p
, &ripcb
, inp_list
)
2145 if ((in6p
->inp_vflag
& INP_IPV6
) == 0) {
2148 if (in6p
->in6p_ip6_nxt
!= IPPROTO_ICMPV6
) {
2151 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
) &&
2152 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_laddr
, &ip6
->ip6_dst
)) {
2155 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
) &&
2156 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_faddr
, &ip6
->ip6_src
)) {
2159 if (in6p
->in6p_icmp6filt
2160 && ICMP6_FILTER_WILLBLOCK(icmp6
->icmp6_type
,
2161 in6p
->in6p_icmp6filt
)) {
2165 if (inp_restricted_recv(in6p
, ifp
)) {
2171 if ((n
= m_copy(m
, 0, (int)M_COPYALL
)) != NULL
) {
2172 if ((last
->in6p_flags
& INP_CONTROLOPTS
) != 0 ||
2173 (last
->in6p_socket
->so_options
& SO_TIMESTAMP
) != 0 ||
2174 (last
->in6p_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0 ||
2175 (last
->in6p_socket
->so_options
& SO_TIMESTAMP_CONTINUOUS
) != 0) {
2176 ret
= ip6_savecontrol(last
, n
, &opts
);
2184 /* strip intermediate headers */
2186 so_recv_data_stat(last
->in6p_socket
, m
, 0);
2187 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
2188 (struct sockaddr
*)&rip6src
,
2189 n
, opts
, NULL
) != 0) {
2190 sorwakeup(last
->in6p_socket
);
2198 if ((last
->in6p_flags
& INP_CONTROLOPTS
) != 0 ||
2199 (last
->in6p_socket
->so_options
& SO_TIMESTAMP
) != 0 ||
2200 (last
->in6p_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0 ||
2201 (last
->in6p_socket
->so_options
& SO_TIMESTAMP_CONTINUOUS
) != 0) {
2202 ret
= ip6_savecontrol(last
, m
, &opts
);
2207 /* strip intermediate headers */
2209 so_recv_data_stat(last
->in6p_socket
, m
, 0);
2210 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
2211 (struct sockaddr
*)&rip6src
, m
, opts
, NULL
) != 0) {
2212 sorwakeup(last
->in6p_socket
);
2217 lck_rw_done(ripcbinfo
.ipi_lock
);
2218 return IPPROTO_DONE
;
2221 lck_rw_done(ripcbinfo
.ipi_lock
);
2224 ip6stat
.ip6s_delivered
--;
2225 return IPPROTO_DONE
;
2229 * Reflect the ip6 packet back to the source.
2230 * OFF points to the icmp6 header, counted from the top of the mbuf.
2233 icmp6_reflect(struct mbuf
*m
, size_t off
)
2235 struct mbuf
*m_ip6hdr
= m
;
2236 struct ip6_hdr
*ip6
;
2237 struct icmp6_hdr
*icmp6
;
2238 struct in6_ifaddr
*ia
;
2239 struct in6_addr t
, src_storage
, *src
= 0;
2242 struct ifnet
*outif
= NULL
;
2243 struct sockaddr_in6 sa6_src
, sa6_dst
;
2244 struct nd_ifinfo
*ndi
= NULL
;
2246 struct ip6_out_args ip6oa
;
2248 bzero(&ip6oa
, sizeof(ip6oa
));
2249 ip6oa
.ip6oa_boundif
= IFSCOPE_NONE
;
2250 ip6oa
.ip6oa_flags
= IP6OAF_SELECT_SRCIF
| IP6OAF_BOUND_SRCADDR
|
2251 IP6OAF_INTCOPROC_ALLOWED
| IP6OAF_AWDL_UNRESTRICTED
;
2252 ip6oa
.ip6oa_sotc
= SO_TC_UNSPEC
;
2253 ip6oa
.ip6oa_netsvctype
= _NET_SERVICE_TYPE_UNSPEC
;
2255 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
) && m
->m_pkthdr
.rcvif
!= NULL
) {
2256 ip6oa
.ip6oa_boundif
= m
->m_pkthdr
.rcvif
->if_index
;
2257 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
2260 /* too short to reflect */
2261 if (off
< sizeof(struct ip6_hdr
)) {
2263 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2264 (u_int32_t
)off
, (u_int32_t
)sizeof(struct ip6_hdr
),
2265 __func__
, __LINE__
));
2270 * If there are extra headers between IPv6 and ICMPv6, strip
2271 * off that header first.
2273 if (off
> sizeof(struct ip6_hdr
)) {
2275 struct ip6_hdr nip6
;
2277 l
= off
- sizeof(struct ip6_hdr
);
2278 m_copydata(m
, 0, sizeof(nip6
), (caddr_t
)&nip6
);
2280 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
2282 if ((m_ip6hdr
= m_pulldown(m
, 0, l
, NULL
)) == NULL
) {
2286 bcopy((caddr_t
)&nip6
, mtod(m
, caddr_t
), sizeof(nip6
));
2287 } else { /* off == sizeof(struct ip6_hdr) */
2289 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
2291 if ((m_ip6hdr
= m_pulldown(m
, 0, l
, NULL
)) == NULL
) {
2296 plen
= m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
2297 ip6
= mtod(m_ip6hdr
, struct ip6_hdr
*);
2298 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2299 icmp6
= (struct icmp6_hdr
*)(ip6
+ 1);
2300 type
= icmp6
->icmp6_type
; /* keep type for statistics */
2301 code
= icmp6
->icmp6_code
; /* ditto. */
2305 * ip6_input() drops a packet if its src is multicast.
2306 * So, the src is never multicast.
2308 ip6
->ip6_dst
= ip6
->ip6_src
;
2311 * XXX: make sure to embed scope zone information, using
2312 * already embedded IDs or the received interface (if any).
2313 * Note that rcvif may be NULL.
2315 bzero(&sa6_src
, sizeof(sa6_src
));
2316 sa6_src
.sin6_family
= AF_INET6
;
2317 sa6_src
.sin6_len
= sizeof(sa6_src
);
2318 sa6_src
.sin6_addr
= ip6
->ip6_dst
;
2319 in6_recoverscope(&sa6_src
, &ip6
->ip6_dst
, m
->m_pkthdr
.rcvif
);
2320 in6_embedscope(&ip6
->ip6_dst
, &sa6_src
, NULL
, NULL
, NULL
);
2321 bzero(&sa6_dst
, sizeof(sa6_dst
));
2322 sa6_dst
.sin6_family
= AF_INET6
;
2323 sa6_dst
.sin6_len
= sizeof(sa6_dst
);
2324 sa6_dst
.sin6_addr
= t
;
2325 in6_recoverscope(&sa6_dst
, &t
, m
->m_pkthdr
.rcvif
);
2326 in6_embedscope(&t
, &sa6_dst
, NULL
, NULL
, NULL
);
2329 * If the incoming packet was addressed directly to us(i.e. unicast),
2330 * use dst as the src for the reply.
2331 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2332 * (for example) when we encounter an error while forwarding procedure
2333 * destined to a duplicated address of ours.
2334 * Note that ip6_getdstifaddr() may fail if we are in an error handling
2335 * procedure of an outgoing packet of our own, in which case we need
2336 * to search in the ifaddr list.
2338 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2339 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2340 IFA_LOCK(&ia
->ia_ifa
);
2341 if (IN6_ARE_ADDR_EQUAL(&t
, &ia
->ia_addr
.sin6_addr
) &&
2342 (ia
->ia6_flags
& (IN6_IFF_ANYCAST
| IN6_IFF_NOTREADY
| IN6_IFF_CLAT46
)) == 0) {
2343 IFA_UNLOCK(&ia
->ia_ifa
);
2347 IFA_UNLOCK(&ia
->ia_ifa
);
2349 lck_rw_done(&in6_ifaddr_rwlock
);
2350 if (ia
== NULL
&& IN6_IS_ADDR_LINKLOCAL(&t
) &&
2351 ((m
->m_flags
& M_LOOP
) || (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
))) {
2353 * This is the case if the dst is our link-local address
2354 * and the sender is also ourselves. Here we test for both
2355 * M_LOOP and PKTF_LOOP, since the former may have been set
2356 * in ip6_output() and that we get here as part of callling
2357 * ip6_process_hopopts(). See comments in <sys/mbuf.h>
2364 struct sockaddr_in6 sin6
;
2365 struct route_in6 ro
;
2368 * This case matches to multicasts, our anycast, or unicasts
2369 * that we do not own. Select a source address based on the
2370 * source address of the erroneous packet.
2372 bzero(&sin6
, sizeof(sin6
));
2373 sin6
.sin6_family
= AF_INET6
;
2374 sin6
.sin6_len
= sizeof(sin6
);
2375 sin6
.sin6_addr
= ip6
->ip6_dst
; /* zone ID should be embedded */
2377 bzero(&ro
, sizeof(ro
));
2379 * in6_selectsrc() might return outif with its reference held
2380 * even in the error case, so we always need to release it
2383 src
= in6_selectsrc(&sin6
, NULL
, NULL
, &ro
, &outif
,
2384 &src_storage
, ip6oa
.ip6oa_boundif
, &e
);
2388 "icmp6_reflect: source can't be determined: "
2389 "dst=%s, error=%d\n",
2390 ip6_sprintf(&sa6_src
.sin6_addr
), e
));
2394 oflow
= ip6
->ip6_flow
; /* Save for later */
2395 ip6
->ip6_src
= *src
;
2397 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2398 ip6
->ip6_vfc
|= IPV6_VERSION
;
2399 if (icmp6
->icmp6_type
== ICMP6_ECHO_REPLY
&& icmp6
->icmp6_code
== 0) {
2400 ip6
->ip6_flow
|= (oflow
& htonl(0x0ff00000));
2402 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2403 if (outif
!= NULL
&& (ndi
= ND_IFINFO(outif
)) != NULL
&&
2405 lck_mtx_lock(&ndi
->lock
);
2406 ip6
->ip6_hlim
= ndi
->chlim
;
2407 lck_mtx_unlock(&ndi
->lock
);
2409 if (m
->m_pkthdr
.rcvif
!= NULL
&&
2410 (ndi
= ND_IFINFO(m
->m_pkthdr
.rcvif
)) != NULL
&&
2412 /* XXX: This may not be the outgoing interface */
2413 lck_mtx_lock(&ndi
->lock
);
2414 ip6
->ip6_hlim
= ndi
->chlim
;
2415 lck_mtx_unlock(&ndi
->lock
);
2417 ip6
->ip6_hlim
= ip6_defhlim
;
2419 /* Use the same traffic class as in the request to match IPv4 */
2420 icmp6
->icmp6_cksum
= 0;
2421 icmp6
->icmp6_cksum
= in6_cksum(m
, IPPROTO_ICMPV6
,
2422 sizeof(struct ip6_hdr
), plen
);
2425 * XXX option handling
2427 m
->m_flags
&= ~(M_BCAST
| M_MCAST
);
2429 if (outif
!= NULL
) {
2430 ifnet_release(outif
);
2434 m
->m_pkthdr
.csum_data
= 0;
2435 m
->m_pkthdr
.csum_flags
= 0;
2436 ip6_output(m
, NULL
, NULL
, IPV6_OUTARGS
, NULL
, &outif
, &ip6oa
);
2437 if (outif
!= NULL
) {
2438 icmp6_ifoutstat_inc(outif
, type
, code
);
2439 ifnet_release(outif
);
2445 if (outif
!= NULL
) {
2446 ifnet_release(outif
);
2452 icmp6_redirect_diag(struct in6_addr
*src6
,
2453 struct in6_addr
*dst6
,
2454 struct in6_addr
*tgt6
)
2456 static char buf
[1024];
2457 snprintf(buf
, sizeof(buf
), "(src=%s dst=%s tgt=%s)",
2458 ip6_sprintf(src6
), ip6_sprintf(dst6
), ip6_sprintf(tgt6
));
2463 icmp6_redirect_input(struct mbuf
*m
, int off
)
2465 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
2466 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2467 struct nd_redirect
*nd_rd
;
2468 int icmp6len
= ntohs(ip6
->ip6_plen
);
2469 char *lladdr
= NULL
;
2471 u_char
*redirhdr
= NULL
;
2472 int redirhdrlen
= 0;
2473 struct rtentry
*rt
= NULL
;
2476 struct in6_addr src6
= ip6
->ip6_src
;
2477 struct in6_addr redtgt6
;
2478 struct in6_addr reddst6
;
2479 union nd_opts ndopts
;
2486 * If we are an advertising router on this interface,
2487 * don't update route by icmp6 redirect.
2489 if (ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
2492 if (!icmp6_rediraccept
) {
2496 #ifndef PULLDOWN_TEST
2497 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return );
2498 nd_rd
= (struct nd_redirect
*)((caddr_t
)ip6
+ off
);
2500 IP6_EXTHDR_GET(nd_rd
, struct nd_redirect
*, m
, off
, icmp6len
);
2501 if (nd_rd
== NULL
) {
2502 icmp6stat
.icp6s_tooshort
++;
2506 redtgt6
= nd_rd
->nd_rd_target
;
2507 reddst6
= nd_rd
->nd_rd_dst
;
2509 if (in6_setscope(&redtgt6
, m
->m_pkthdr
.rcvif
, NULL
) ||
2510 in6_setscope(&reddst6
, m
->m_pkthdr
.rcvif
, NULL
)) {
2515 if (!IN6_IS_ADDR_LINKLOCAL(&src6
)) {
2517 "ICMP6 redirect sent from %s rejected; "
2518 "must be from linklocal\n", ip6_sprintf(&src6
)));
2521 if (ip6
->ip6_hlim
!= 255) {
2523 "ICMP6 redirect sent from %s rejected; "
2524 "hlim=%d (must be 255)\n",
2525 ip6_sprintf(&src6
), ip6
->ip6_hlim
));
2529 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2530 struct sockaddr_in6 sin6
;
2531 struct in6_addr
*gw6
;
2533 bzero(&sin6
, sizeof(sin6
));
2534 sin6
.sin6_family
= AF_INET6
;
2535 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
2536 bcopy(&reddst6
, &sin6
.sin6_addr
, sizeof(reddst6
));
2537 rt
= rtalloc1_scoped((struct sockaddr
*)&sin6
, 0, 0, ifp
->if_index
);
2540 if (rt
->rt_gateway
== NULL
||
2541 rt
->rt_gateway
->sa_family
!= AF_INET6
) {
2543 "ICMP6 redirect rejected; no route "
2544 "with inet6 gateway found for redirect dst: %s\n",
2545 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2551 gw6
= &(((struct sockaddr_in6
*)(void *)
2552 rt
->rt_gateway
)->sin6_addr
);
2553 if (bcmp(&src6
, gw6
, sizeof(struct in6_addr
)) != 0) {
2555 "ICMP6 redirect rejected; "
2556 "not equal to gw-for-src=%s (must be same): "
2559 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2566 "ICMP6 redirect rejected; "
2567 "no route found for redirect dst: %s\n",
2568 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2575 if (IN6_IS_ADDR_MULTICAST(&reddst6
)) {
2577 "ICMP6 redirect rejected; "
2578 "redirect dst must be unicast: %s\n",
2579 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2583 is_router
= is_onlink
= 0;
2584 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6
)) {
2585 is_router
= 1; /* router case */
2587 if (bcmp(&redtgt6
, &reddst6
, sizeof(redtgt6
)) == 0) {
2588 is_onlink
= 1; /* on-link destination case */
2590 if (!is_router
&& !is_onlink
) {
2592 "ICMP6 redirect rejected; "
2593 "neither router case nor onlink case: %s\n",
2594 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2597 /* validation passed */
2599 icmp6len
-= sizeof(*nd_rd
);
2600 nd6_option_init(nd_rd
+ 1, icmp6len
, &ndopts
);
2601 if (nd6_options(&ndopts
) < 0) {
2602 nd6log((LOG_INFO
, "icmp6_redirect_input: "
2603 "invalid ND option, rejected: %s\n",
2604 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2605 /* nd6_options have incremented stats */
2609 if (ndopts
.nd_opts_tgt_lladdr
) {
2610 lladdr
= (char *)(ndopts
.nd_opts_tgt_lladdr
+ 1);
2611 lladdrlen
= ndopts
.nd_opts_tgt_lladdr
->nd_opt_len
<< 3;
2614 if (ndopts
.nd_opts_rh
) {
2615 redirhdrlen
= ndopts
.nd_opts_rh
->nd_opt_rh_len
;
2616 redirhdr
= (u_char
*)(ndopts
.nd_opts_rh
+ 1); /* xxx */
2619 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
2621 "icmp6_redirect_input: lladdrlen mismatch for %s "
2622 "(if %d, icmp6 packet %d): %s\n",
2623 ip6_sprintf(&redtgt6
), ifp
->if_addrlen
, lladdrlen
- 2,
2624 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2629 nd6_cache_lladdr(ifp
, &redtgt6
, lladdr
, lladdrlen
, ND_REDIRECT
,
2630 is_onlink
? ND_REDIRECT_ONLINK
: ND_REDIRECT_ROUTER
);
2632 if (!is_onlink
) { /* better router case. perform rtredirect. */
2633 /* perform rtredirect */
2634 struct sockaddr_in6 sdst
;
2635 struct sockaddr_in6 sgw
;
2636 struct sockaddr_in6 ssrc
;
2638 bzero(&sdst
, sizeof(sdst
));
2639 bzero(&sgw
, sizeof(sgw
));
2640 bzero(&ssrc
, sizeof(ssrc
));
2641 sdst
.sin6_family
= sgw
.sin6_family
= ssrc
.sin6_family
= AF_INET6
;
2642 sdst
.sin6_len
= sgw
.sin6_len
= ssrc
.sin6_len
=
2643 sizeof(struct sockaddr_in6
);
2644 bcopy(&redtgt6
, &sgw
.sin6_addr
, sizeof(struct in6_addr
));
2645 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2646 bcopy(&src6
, &ssrc
.sin6_addr
, sizeof(struct in6_addr
));
2647 rtredirect(ifp
, (struct sockaddr
*)&sdst
,
2648 (struct sockaddr
*)&sgw
, NULL
, RTF_GATEWAY
| RTF_HOST
,
2649 (struct sockaddr
*)&ssrc
, NULL
);
2651 /* finally update cached route in each socket via pfctlinput */
2653 struct sockaddr_in6 sdst
;
2655 bzero(&sdst
, sizeof(sdst
));
2656 sdst
.sin6_family
= AF_INET6
;
2657 sdst
.sin6_len
= sizeof(struct sockaddr_in6
);
2658 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2660 pfctlinput(PRC_REDIRECT_HOST
, (struct sockaddr
*)&sdst
);
2662 key_sa_routechange((struct sockaddr
*)&sdst
);
2671 icmp6stat
.icp6s_badredirect
++;
2676 icmp6_redirect_output(struct mbuf
*m0
, struct rtentry
*rt
)
2678 struct ifnet
*ifp
; /* my outgoing interface */
2679 struct in6_addr ifp_ll6
;
2680 struct in6_addr
*router_ll6
;
2681 struct ip6_hdr
*sip6
; /* m0 as struct ip6_hdr */
2682 struct mbuf
*m
= NULL
; /* newly allocated one */
2683 struct ip6_hdr
*ip6
; /* m as struct ip6_hdr */
2684 struct nd_redirect
*nd_rd
;
2687 struct ifnet
*outif
= NULL
;
2688 struct sockaddr_in6 src_sa
;
2689 struct ip6_out_args ip6oa
;
2691 bzero(&ip6oa
, sizeof(ip6oa
));
2692 ip6oa
.ip6oa_boundif
= IFSCOPE_NONE
;
2693 ip6oa
.ip6oa_flags
= IP6OAF_SELECT_SRCIF
| IP6OAF_BOUND_SRCADDR
;
2694 ip6oa
.ip6oa_sotc
= SO_TC_UNSPEC
;
2695 ip6oa
.ip6oa_netsvctype
= _NET_SERVICE_TYPE_UNSPEC
;
2697 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, ND_REDIRECT
, 0);
2704 if (!m0
|| !rt
|| !(rt
->rt_flags
& RTF_UP
) || !(ifp
= rt
->rt_ifp
)) {
2709 * If we are not a router to begin with, or not an advertising
2710 * router on this interface, don't send icmp6 redirect.
2712 if (!ip6_forwarding
|| !(ifp
->if_eflags
& IFEF_IPV6_ROUTER
)) {
2718 * the source address must identify a neighbor, and
2719 * the destination address must not be a multicast address
2720 * [RFC 2461, sec 8.2]
2722 sip6
= mtod(m0
, struct ip6_hdr
*);
2723 bzero(&src_sa
, sizeof(src_sa
));
2724 src_sa
.sin6_family
= AF_INET6
;
2725 src_sa
.sin6_len
= sizeof(src_sa
);
2726 src_sa
.sin6_addr
= sip6
->ip6_src
;
2727 /* we don't currently use sin6_scope_id, but eventually use it */
2728 src_sa
.sin6_scope_id
= in6_addr2scopeid(ifp
, &sip6
->ip6_src
);
2730 if (nd6_is_addr_neighbor(&src_sa
, ifp
, 0) == 0) {
2731 /* already unlocked */
2736 if (IN6_IS_ADDR_MULTICAST(&sip6
->ip6_dst
)) {
2737 goto fail
; /* what should we do here? */
2740 if (icmp6_ratelimit(&sip6
->ip6_src
, ND_REDIRECT
, 0)) {
2745 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2746 * we almost always ask for an mbuf cluster for simplicity.
2747 * (MHLEN < IPV6_MMTU is almost always true)
2749 #if IPV6_MMTU >= MCLBYTES
2750 # error assumption failed about IPV6_MMTU and MCLBYTES
2752 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
2753 if (m
&& IPV6_MMTU
>= MHLEN
) {
2754 MCLGET(m
, M_DONTWAIT
);
2759 m
->m_pkthdr
.rcvif
= NULL
;
2761 maxlen
= M_TRAILINGSPACE(m
);
2762 maxlen
= min(IPV6_MMTU
, maxlen
);
2763 /* just for safety */
2764 if (maxlen
< sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
) +
2765 ((sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7)) {
2770 /* get ip6 linklocal address for ifp(my outgoing interface). */
2771 struct in6_ifaddr
*ia
;
2772 if ((ia
= in6ifa_ifpforlinklocal(ifp
,
2774 IN6_IFF_ANYCAST
)) == NULL
) {
2777 IFA_LOCK(&ia
->ia_ifa
);
2778 ifp_ll6
= ia
->ia_addr
.sin6_addr
;
2779 IFA_UNLOCK(&ia
->ia_ifa
);
2780 IFA_REMREF(&ia
->ia_ifa
);
2783 /* get ip6 linklocal address for the router. */
2784 if (rt
->rt_gateway
&& (rt
->rt_flags
& RTF_GATEWAY
)) {
2785 struct sockaddr_in6
*sin6
;
2786 sin6
= (struct sockaddr_in6
*)(void *)rt
->rt_gateway
;
2787 router_ll6
= &sin6
->sin6_addr
;
2788 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6
)) {
2789 router_ll6
= (struct in6_addr
*)NULL
;
2792 router_ll6
= (struct in6_addr
*)NULL
;
2796 ip6
= mtod(m
, struct ip6_hdr
*);
2798 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2799 ip6
->ip6_vfc
|= IPV6_VERSION
;
2800 /* ip6->ip6_plen will be set later */
2801 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2802 ip6
->ip6_hlim
= 255;
2803 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2804 bcopy(&ifp_ll6
, &ip6
->ip6_src
, sizeof(struct in6_addr
));
2805 bcopy(&sip6
->ip6_src
, &ip6
->ip6_dst
, sizeof(struct in6_addr
));
2808 nd_rd
= (struct nd_redirect
*)(ip6
+ 1);
2809 nd_rd
->nd_rd_type
= ND_REDIRECT
;
2810 nd_rd
->nd_rd_code
= 0;
2811 nd_rd
->nd_rd_reserved
= 0;
2812 if (rt
->rt_flags
& RTF_GATEWAY
) {
2814 * nd_rd->nd_rd_target must be a link-local address in
2815 * better router cases.
2820 bcopy(router_ll6
, &nd_rd
->nd_rd_target
,
2821 sizeof(nd_rd
->nd_rd_target
));
2822 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2823 sizeof(nd_rd
->nd_rd_dst
));
2825 /* make sure redtgt == reddst */
2826 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_target
,
2827 sizeof(nd_rd
->nd_rd_target
));
2828 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2829 sizeof(nd_rd
->nd_rd_dst
));
2834 p
= (u_char
*)(nd_rd
+ 1);
2841 /* target lladdr option */
2842 struct rtentry
*rt_router
= NULL
;
2844 struct sockaddr_dl
*sdl
;
2845 struct nd_opt_hdr
*nd_opt
;
2848 /* Callee returns a locked route upon success */
2849 rt_router
= nd6_lookup(router_ll6
, 0, ifp
, 0);
2853 RT_LOCK_ASSERT_HELD(rt_router
);
2854 len
= sizeof(*nd_opt
) + ifp
->if_addrlen
;
2855 len
= (len
+ 7) & ~7; /* round by 8 */
2857 if (len
+ (p
- (u_char
*)ip6
) > maxlen
) {
2858 RT_REMREF_LOCKED(rt_router
);
2859 RT_UNLOCK(rt_router
);
2863 if (!(rt_router
->rt_flags
& RTF_GATEWAY
) &&
2864 (rt_router
->rt_flags
& RTF_LLINFO
) &&
2865 (rt_router
->rt_gateway
->sa_family
== AF_LINK
) &&
2866 (sdl
= (struct sockaddr_dl
*)(void *)
2867 rt_router
->rt_gateway
) && sdl
->sdl_alen
) {
2868 nd_opt
= (struct nd_opt_hdr
*)p
;
2869 nd_opt
->nd_opt_type
= ND_OPT_TARGET_LINKADDR
;
2870 nd_opt
->nd_opt_len
= len
>> 3;
2871 lladdr
= (char *)(nd_opt
+ 1);
2872 bcopy(LLADDR(sdl
), lladdr
, ifp
->if_addrlen
);
2875 RT_REMREF_LOCKED(rt_router
);
2876 RT_UNLOCK(rt_router
);
2881 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2883 /* just to be safe */
2884 #ifdef M_DECRYPTED /*not openbsd*/
2885 if (m0
->m_flags
& M_DECRYPTED
) {
2889 if (p
- (u_char
*)ip6
> maxlen
) {
2894 /* redirected header option */
2896 struct nd_opt_rd_hdr
*nd_opt_rh
;
2899 * compute the maximum size for icmp6 redirect header option.
2900 * XXX room for auth header?
2902 len
= maxlen
- (p
- (u_char
*)ip6
);
2905 /* This is just for simplicity. */
2906 if (m0
->m_pkthdr
.len
!= m0
->m_len
) {
2908 m_freem(m0
->m_next
);
2911 m0
->m_pkthdr
.len
= m0
->m_len
;
2915 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2916 * about padding/truncate rule for the original IP packet.
2917 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2918 * - "attach as much as possible" is the goal
2919 * - pad if not aligned (original size can be guessed by original
2921 * Following code adds the padding if it is simple enough,
2922 * and truncates if not.
2924 if (m0
->m_next
|| m0
->m_pkthdr
.len
!= m0
->m_len
) {
2925 panic("assumption failed in %s:%d\n", __func__
, __LINE__
);
2928 if (len
- sizeof(*nd_opt_rh
) < m0
->m_pkthdr
.len
) {
2929 /* not enough room, truncate */
2930 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2932 /* enough room, pad or truncate */
2935 extra
= m0
->m_pkthdr
.len
% 8;
2937 /* pad if easy enough, truncate if not */
2938 if (8 - extra
<= M_TRAILINGSPACE(m0
)) {
2940 m0
->m_len
+= (8 - extra
);
2941 m0
->m_pkthdr
.len
+= (8 - extra
);
2944 m0
->m_pkthdr
.len
-= extra
;
2948 len
= m0
->m_pkthdr
.len
+ sizeof(*nd_opt_rh
);
2949 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2952 nd_opt_rh
= (struct nd_opt_rd_hdr
*)p
;
2953 bzero(nd_opt_rh
, sizeof(*nd_opt_rh
));
2954 nd_opt_rh
->nd_opt_rh_type
= ND_OPT_REDIRECTED_HEADER
;
2955 nd_opt_rh
->nd_opt_rh_len
= len
>> 3;
2956 p
+= sizeof(*nd_opt_rh
);
2957 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2959 /* connect m0 to m */
2961 m
->m_pkthdr
.len
= m
->m_len
+ m0
->m_len
;
2965 /* XXX: clear embedded link IDs in the inner header */
2966 in6_clearscope(&sip6
->ip6_src
);
2967 in6_clearscope(&sip6
->ip6_dst
);
2968 in6_clearscope(&nd_rd
->nd_rd_target
);
2969 in6_clearscope(&nd_rd
->nd_rd_dst
);
2971 ip6
->ip6_plen
= htons(m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
));
2973 nd_rd
->nd_rd_cksum
= 0;
2975 = in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(*ip6
), ntohs(ip6
->ip6_plen
));
2977 /* send the packet to outside... */
2978 ip6oa
.ip6oa_boundif
= ifp
->if_index
;
2979 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
2981 ip6_output(m
, NULL
, NULL
, IPV6_OUTARGS
, NULL
, &outif
, &ip6oa
);
2983 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
2984 icmp6_ifstat_inc(outif
, ifs6_out_redirect
);
2985 ifnet_release(outif
);
2987 icmp6stat
.icp6s_outhist
[ND_REDIRECT
]++;
3004 * ICMPv6 socket option processing.
3007 icmp6_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
3011 struct inpcb
*inp
= sotoinpcb(so
);
3012 int level
, op
, optname
;
3015 level
= sopt
->sopt_level
;
3016 op
= sopt
->sopt_dir
;
3017 optname
= sopt
->sopt_name
;
3018 optlen
= sopt
->sopt_valsize
;
3020 level
= op
= optname
= optlen
= 0;
3023 if (level
!= IPPROTO_ICMPV6
) {
3032 struct icmp6_filter
*p
;
3034 if (optlen
!= 0 && optlen
!= sizeof(*p
)) {
3038 if (inp
->in6p_icmp6filt
== NULL
) {
3044 /* According to RFC 3542, an installed filter can be
3045 * cleared by issuing a setsockopt for ICMP6_FILTER
3046 * with a zero length.
3048 ICMP6_FILTER_SETPASSALL(inp
->in6p_icmp6filt
);
3050 error
= sooptcopyin(sopt
, inp
->in6p_icmp6filt
, optlen
,
3057 error
= ENOPROTOOPT
;
3066 if (inp
->in6p_icmp6filt
== NULL
) {
3070 error
= sooptcopyout(sopt
, inp
->in6p_icmp6filt
,
3071 min(sizeof(struct icmp6_filter
), optlen
));
3076 error
= ENOPROTOOPT
;
3086 * ICMPv6 socket datagram option processing.
3089 icmp6_dgram_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
3091 if (kauth_cred_issuser(so
->so_cred
)) {
3092 return icmp6_ctloutput(so
, sopt
);
3095 if (sopt
->sopt_level
== IPPROTO_ICMPV6
) {
3096 switch (sopt
->sopt_name
) {
3098 return icmp6_ctloutput(so
, sopt
);
3104 if (sopt
->sopt_level
!= IPPROTO_IPV6
) {
3108 switch (sopt
->sopt_name
) {
3109 case IPV6_UNICAST_HOPS
:
3112 case IPV6_USE_MIN_MTU
:
3113 case IPV6_RECVRTHDR
:
3114 case IPV6_RECVPKTINFO
:
3115 case IPV6_RECVHOPLIMIT
:
3121 case IPV6_MULTICAST_IF
:
3122 case IPV6_MULTICAST_HOPS
:
3123 case IPV6_MULTICAST_LOOP
:
3124 case IPV6_JOIN_GROUP
:
3125 case IPV6_LEAVE_GROUP
:
3126 case IPV6_PORTRANGE
:
3127 case IPV6_IPSEC_POLICY
:
3128 case IPV6_RECVTCLASS
:
3130 case IPV6_2292PKTOPTIONS
:
3131 case IPV6_2292PKTINFO
:
3132 case IPV6_2292HOPLIMIT
:
3133 case IPV6_2292HOPOPTS
:
3134 case IPV6_2292DSTOPTS
:
3135 case IPV6_2292RTHDR
:
3137 case IPV6_NO_IFT_CELLULAR
:
3139 return ip6_ctloutput(so
, sopt
);
3146 __private_extern__
int
3147 icmp6_dgram_send(struct socket
*so
, int flags
, struct mbuf
*m
,
3148 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
3150 #pragma unused(flags, p)
3152 struct inpcb
*inp
= sotoinpcb(so
);
3153 struct sockaddr_in6 tmp
;
3154 struct sockaddr_in6
*dst
= (struct sockaddr_in6
*)(void *)nam
;
3155 struct icmp6_hdr
*icmp6
;
3159 || (necp_socket_should_use_flow_divert(inp
))
3162 error
= (inp
== NULL
? EINVAL
: EPROTOTYPE
);
3166 if (kauth_cred_issuser(so
->so_cred
)) {
3167 return rip6_output(m
, so
, SIN6(nam
), control
, 0);
3170 /* always copy sockaddr to avoid overwrites */
3171 if (so
->so_state
& SS_ISCONNECTED
) {
3177 bzero(&tmp
, sizeof(tmp
));
3178 tmp
.sin6_family
= AF_INET6
;
3179 tmp
.sin6_len
= sizeof(struct sockaddr_in6
);
3180 bcopy(&inp
->in6p_faddr
, &tmp
.sin6_addr
,
3181 sizeof(struct in6_addr
));
3188 tmp
= *(struct sockaddr_in6
*)(void *)nam
;
3193 * For an ICMPv6 packet, we should know its type and code
3195 if (SOCK_PROTO(so
) == IPPROTO_ICMPV6
) {
3196 if (m
->m_len
< sizeof(struct icmp6_hdr
) &&
3197 (m
= m_pullup(m
, sizeof(struct icmp6_hdr
))) == NULL
) {
3201 icmp6
= mtod(m
, struct icmp6_hdr
*);
3204 * Allow only to send echo request and node information request
3205 * See RFC 2463 for Echo Request Message format
3207 if ((icmp6
->icmp6_type
== ICMP6_ECHO_REQUEST
&&
3208 icmp6
->icmp6_code
== 0) ||
3209 (icmp6
->icmp6_type
== ICMP6_NI_QUERY
&&
3210 (icmp6
->icmp6_code
== ICMP6_NI_SUBJ_IPV6
||
3211 icmp6
->icmp6_code
== ICMP6_NI_SUBJ_FQDN
))) {
3220 #if ENABLE_DEFAULT_SCOPE
3221 if (dst
->sin6_scope_id
== 0) { /* not change if specified */
3222 dst
->sin6_scope_id
= scope6_addr2default(&dst
->sin6_addr
);
3226 return rip6_output(m
, so
, dst
, 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
,
3305 /* The packet is subject to rate limit */