1 /*
2 * Copyright (c) 2000-2025 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
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 $ */
31
32 /*
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 */
60
61 /*
62 * Copyright (c) 1982, 1986, 1988, 1993
63 * The Regents of the University of California. All rights reserved.
64 *
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
67 * are met:
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.
80 *
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
91 * SUCH DAMAGE.
92 *
93 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
94 */
95
96
97 #include <sys/param.h>
98 #include <sys/systm.h>
99 #include <sys/lock.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>
111
112 #include <net/if.h>
113 #include <net/route.h>
114 #include <net/if_dl.h>
115 #include <net/if_types.h>
116
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>
130
131 #if IPSEC
132 #include <netinet6/ipsec.h>
133 #include <netkey/key.h>
134 #endif
135
136 #include <net/net_osdep.h>
137
138 #if NECP
139 #include <net/necp.h>
140 #endif
141
142 #include <net/sockaddr_utils.h>
143
144 extern struct ip6protosw *ip6_protox[IPPROTO_MAX];
145
146 extern uint32_t rip_sendspace;
147 extern uint32_t rip_recvspace;
148
149 struct icmp6stat icmp6stat;
150
151 extern struct inpcbhead ripcb;
152 extern int icmp6errppslim;
153 extern int icmp6errppslim_random_incr;
154 extern int icmp6rappslim;
155 static int icmp6errpps_count = 0;
156 static int icmp6rapps_count = 0;
157 static struct timeval icmp6errppslim_last;
158 static struct timeval icmp6rappslim_last;
159 extern int icmp6_nodeinfo;
160 extern struct inpcbinfo ripcbinfo;
161
162 static void icmp6_errcount(struct icmp6errstat *, int, int);
163 static int icmp6_rip6_input(struct mbuf **, int);
164 static int icmp6_ratelimit(const struct in6_addr *, const int, const int);
165 static const char *icmp6_redirect_diag(struct in6_addr *,
166 struct in6_addr *, struct in6_addr *);
167 static struct mbuf *ni6_input(struct mbuf *, int);
168 static struct mbuf *ni6_nametodns(const char *__counted_by(len), uint32_t len, int);
169 static int ni6_dnsmatch(const char *__counted_by(alen), int alen, const char *__counted_by(blen), int blen);
170 static int ni6_addrs(struct icmp6_nodeinfo *,
171 struct ifnet **, char *__indexable);
172 static int ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *__indexable nni6,
173 struct ifnet *ifp0, int resid);
174 static int icmp6_notify_error(struct mbuf *, int, int, int);
175
176
177
178 void
icmp6_init(struct ip6protosw * pp,struct domain * dp)179 icmp6_init(struct ip6protosw *pp, struct domain *dp)
180 {
181 #pragma unused(dp)
182 static int icmp6_initialized = 0;
183
184 /* Also called from ip6_init() without pp */
185 VERIFY(pp == NULL ||
186 (pp->pr_flags & (PR_INITIALIZED | PR_ATTACHED)) == PR_ATTACHED);
187
188 /* This gets called by more than one protocols, so initialize once */
189 if (!icmp6_initialized) {
190 icmp6_initialized = 1;
191 mld_init();
192 if (icmp6errppslim >= 0 &&
193 icmp6errppslim_random_incr > 0 &&
194 icmp6errppslim <= INT32_MAX - (icmp6errppslim_random_incr + 1)) {
195 icmp6errppslim += (random() % icmp6errppslim_random_incr) + 1;
196 }
197 }
198 }
199
200 static void
icmp6_errcount(struct icmp6errstat * stat,int type,int code)201 icmp6_errcount(struct icmp6errstat *stat, int type, int code)
202 {
203 switch (type) {
204 case ICMP6_DST_UNREACH:
205 switch (code) {
206 case ICMP6_DST_UNREACH_NOROUTE:
207 stat->icp6errs_dst_unreach_noroute++;
208 return;
209 case ICMP6_DST_UNREACH_ADMIN:
210 stat->icp6errs_dst_unreach_admin++;
211 return;
212 case ICMP6_DST_UNREACH_BEYONDSCOPE:
213 stat->icp6errs_dst_unreach_beyondscope++;
214 return;
215 case ICMP6_DST_UNREACH_ADDR:
216 stat->icp6errs_dst_unreach_addr++;
217 return;
218 case ICMP6_DST_UNREACH_NOPORT:
219 stat->icp6errs_dst_unreach_noport++;
220 return;
221 }
222 break;
223 case ICMP6_PACKET_TOO_BIG:
224 stat->icp6errs_packet_too_big++;
225 return;
226 case ICMP6_TIME_EXCEEDED:
227 switch (code) {
228 case ICMP6_TIME_EXCEED_TRANSIT:
229 stat->icp6errs_time_exceed_transit++;
230 return;
231 case ICMP6_TIME_EXCEED_REASSEMBLY:
232 stat->icp6errs_time_exceed_reassembly++;
233 return;
234 }
235 break;
236 case ICMP6_PARAM_PROB:
237 switch (code) {
238 case ICMP6_PARAMPROB_HEADER:
239 stat->icp6errs_paramprob_header++;
240 return;
241 case ICMP6_PARAMPROB_NEXTHEADER:
242 stat->icp6errs_paramprob_nextheader++;
243 return;
244 case ICMP6_PARAMPROB_OPTION:
245 stat->icp6errs_paramprob_option++;
246 return;
247 }
248 break;
249 case ND_REDIRECT:
250 stat->icp6errs_redirect++;
251 return;
252 }
253 stat->icp6errs_unknown++;
254 }
255
256 /*
257 * Generate packet gencount for ICMPv6 for a given error type
258 * and code.
259 */
260 static uint32_t
icmp6_error_packet_gencount(int type,int code)261 icmp6_error_packet_gencount(int type, int code)
262 {
263 return (PF_INET6 << 24) | (type << 16) | (code << 8);
264 }
265
266 static int suppress_icmp6_port_unreach = 0;
267
268 SYSCTL_DECL(_net_inet6_icmp6);
269 SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, suppress_icmp6_port_unreach, CTLFLAG_RW | CTLFLAG_LOCKED,
270 &suppress_icmp6_port_unreach, 0,
271 "Suppress ICMPv6 destination unreachable type with code port unreachable");
272
273 /*
274 * Generate an error packet of type error in response to bad IP6 packet.
275 */
276 void
icmp6_error(struct mbuf * m,int type,int code,int param)277 icmp6_error(struct mbuf *m, int type, int code, int param)
278 {
279 icmp6_error_flag(m, type, code, param, ICMP6_ERROR_RST_MRCVIF);
280 }
281
282 void
icmp6_error_flag(struct mbuf * m,int type,int code,int param,int flags)283 icmp6_error_flag(struct mbuf *m, int type, int code, int param, int flags)
284 {
285 struct ip6_hdr *__single oip6;
286 struct ip6_hdr *nip6;
287 struct icmp6_hdr *__single icmp6;
288 u_int preplen;
289 int off;
290
291 icmp6stat.icp6s_error++;
292
293 /* count per-type-code statistics */
294 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
295
296 if (suppress_icmp6_port_unreach && type == ICMP6_DST_UNREACH &&
297 code == ICMP6_DST_UNREACH_NOPORT) {
298 goto freeit;
299 }
300
301 #ifdef M_DECRYPTED /*not openbsd*/
302 if (m->m_flags & M_DECRYPTED) {
303 icmp6stat.icp6s_canterror++;
304 goto freeit;
305 }
306 #endif
307
308 #ifndef PULLDOWN_TEST
309 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), return );
310 #else
311 if (m->m_len < sizeof(struct ip6_hdr)) {
312 m = m_pullup(m, sizeof(struct ip6_hdr));
313 if (m == NULL) {
314 return;
315 }
316 }
317 #endif
318 oip6 = mtod(m, struct ip6_hdr *);
319
320 /*
321 * If the destination address of the erroneous packet is a multicast
322 * address, or the packet was sent using link-layer multicast,
323 * we should basically suppress sending an error (RFC 2463, Section
324 * 2.4).
325 * We have two exceptions (the item e.2 in that section):
326 * - the Pakcet Too Big message can be sent for path MTU discovery.
327 * - the Parameter Problem Message that can be allowed an icmp6 error
328 * in the option type field. This check has been done in
329 * ip6_unknown_opt(), so we can just check the type and code.
330 */
331 if ((m->m_flags & (M_BCAST | M_MCAST) ||
332 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
333 (type != ICMP6_PACKET_TOO_BIG &&
334 (type != ICMP6_PARAM_PROB ||
335 code != ICMP6_PARAMPROB_OPTION))) {
336 goto freeit;
337 }
338
339 /*
340 * RFC 2463, 2.4 (e.5): source address check.
341 * XXX: the case of anycast source?
342 */
343 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
344 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) {
345 goto freeit;
346 }
347
348 /*
349 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
350 * don't do it.
351 *
352 * We want to check for that for all ICMP error types, other than
353 * ICMP6_PARAM_PROB when it is being sent in response of first frag
354 * with incomplete header.
355 * That also includes the case when the first frag has incomplete ICMPv6
356 * header. The check below in that case would fail the IP6_EXTHDR_CHECK
357 * and would otherwise prevent us from sending the error back.
358 */
359 if (type != ICMP6_PARAM_PROB ||
360 code != ICMP6_PARAMPROB_FIRSTFRAG_INCOMP_HDR) {
361 int nxt = -1;
362 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
363 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
364 struct icmp6_hdr *__single icp;
365
366 #ifndef PULLDOWN_TEST
367 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), return );
368 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
369 #else
370 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
371 sizeof(*icp));
372 if (icp == NULL) {
373 icmp6stat.icp6s_tooshort++;
374 return;
375 }
376 #endif
377 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
378 icp->icmp6_type == ND_REDIRECT) {
379 /*
380 * ICMPv6 error
381 * Special case: for redirect (which is
382 * informational) we must not send icmp6 error.
383 */
384 icmp6stat.icp6s_canterror++;
385 goto freeit;
386 } else {
387 /* ICMPv6 informational - send the error */
388 }
389 } else {
390 /* non-ICMPv6 - send the error */
391 }
392 }
393
394 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
395
396 /*
397 * OK, ICMP6 can be generated.
398 */
399
400 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) {
401 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
402 }
403
404 /*
405 * To avoid some flavors of port scanning and other attacks,
406 * use packet suppression without using any other sort of
407 * rate limiting with static bounds.
408 * XXX Not setting PKTF_FLOW_ID here because we were concerned
409 * about it triggering regression elsewhere outside of network stack
410 * where there might be an assumption around flow ID being non-zero.
411 * It should be noted though that previously if PKTF_FLOW_ID was not
412 * set, PF would have generated flow hash irrespective of ICMPv4/v6
413 * type. That doesn't happen now and PF only computes hash for ICMP
414 * types that need state creation (which is not true of error types).
415 * It would have been a problem because we really want all the ICMP
416 * error type packets to share the same flow ID for global suppression.
417 */
418 m->m_pkthdr.comp_gencnt = icmp6_error_packet_gencount(type, code);
419
420 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
421 M_PREPEND(m, preplen, M_DONTWAIT, 1);
422 if (m == NULL) {
423 nd6log(debug, "ENOBUFS in icmp6_error %d\n", __LINE__);
424 return;
425 }
426
427 nip6 = mtod(m, struct ip6_hdr *);
428 nip6->ip6_src = oip6->ip6_src;
429 nip6->ip6_dst = oip6->ip6_dst;
430
431 in6_clearscope(&oip6->ip6_src);
432 in6_clearscope(&oip6->ip6_dst);
433
434 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
435 icmp6->icmp6_type = (uint8_t)type;
436 icmp6->icmp6_code = (uint8_t)code;
437 icmp6->icmp6_pptr = htonl((u_int32_t)param);
438
439 /*
440 * icmp6_reflect() is designed to be in the input path.
441 * icmp6_error() can be called from both input and output path,
442 * and if we are in output path rcvif could contain bogus value.
443 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
444 * information in ip header (nip6).
445 */
446 if (flags & ICMP6_ERROR_RST_MRCVIF) {
447 m->m_pkthdr.rcvif = NULL;
448 }
449
450 icmp6stat.icp6s_outhist[type]++;
451 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
452
453 return;
454
455 freeit:
456 /*
457 * If we can't tell whether or not we can generate ICMP6, free it.
458 */
459 m_freem(m);
460 }
461
462 /*
463 * Process a received ICMP6 message.
464 */
465 int
icmp6_input(struct mbuf ** mp,int * offp,int proto)466 icmp6_input(struct mbuf **mp, int *offp, int proto)
467 {
468 #pragma unused(proto)
469 mbuf_ref_t m = *mp, n;
470 ifnet_ref_t ifp;
471 struct ip6_hdr *ip6, *nip6;
472 struct icmp6_hdr *__single icmp6, *__single nicmp6;
473 int off = *offp;
474 int icmp6len = m->m_pkthdr.len - *offp;
475 int code, sum, noff, proxy = 0;
476
477 ifp = m->m_pkthdr.rcvif;
478
479 #ifndef PULLDOWN_TEST
480 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), return IPPROTO_DONE);
481 /* m might change if M_LOOP. So, call mtod after this */
482 #endif
483
484 /* Expect 32-bit aligned data pointer on strict-align platforms */
485 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
486
487 /*
488 * Locate icmp6 structure in mbuf, and check
489 * that not corrupted and of at least minimum length
490 */
491 ip6 = mtod(m, struct ip6_hdr *);
492 if (icmp6len < sizeof(struct icmp6_hdr)) {
493 icmp6stat.icp6s_tooshort++;
494 goto freeit;
495 }
496
497 #ifndef PULLDOWN_TEST
498 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
499 #else
500 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
501 if (icmp6 == NULL) {
502 icmp6stat.icp6s_tooshort++;
503 return IPPROTO_DONE;
504 }
505 #endif
506 code = icmp6->icmp6_code;
507
508 /*
509 * Early check for RFC 6980
510 * Drop certain NDP packets if they came in fragmented
511 */
512 switch (icmp6->icmp6_type) {
513 case ND_ROUTER_SOLICIT:
514 case ND_ROUTER_ADVERT:
515 case ND_NEIGHBOR_SOLICIT:
516 case ND_NEIGHBOR_ADVERT:
517 case ND_REDIRECT:
518 if (m->m_pkthdr.pkt_flags & PKTF_REASSEMBLED) {
519 icmp6stat.icp6s_rfc6980_drop++;
520 goto freeit;
521 }
522 break;
523 default:
524 break;
525 }
526
527 /* Apply rate limit before checksum validation. */
528 if (icmp6_ratelimit(&ip6->ip6_dst, icmp6->icmp6_type, code)) {
529 icmp6stat.icp6s_toofreq++;
530 goto freeit;
531 }
532
533 /*
534 * Check multicast group membership.
535 * Note: SSM filters are not applied for ICMPv6 traffic.
536 */
537 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
538 struct in6_multi *__single inm;
539
540 in6_multihead_lock_shared();
541 IN6_LOOKUP_MULTI(&ip6->ip6_dst, ifp, inm);
542 in6_multihead_lock_done();
543
544 if (inm == NULL) {
545 /*
546 * Don't discard if this is a Neighbor Solicitation
547 * that needs to be proxied (see check down below.)
548 */
549 if (!(m->m_pkthdr.pkt_flags & PKTF_PROXY_DST)) {
550 ip6stat.ip6s_notmember++;
551 in6_ifstat_inc(m->m_pkthdr.rcvif,
552 ifs6_in_discard);
553 goto freeit;
554 }
555 } else {
556 IN6M_REMREF(inm);
557 }
558 }
559
560 /*
561 * calculate the checksum
562 */
563 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
564 nd6log(error,
565 "ICMP6 checksum error(%d|%x) %s\n",
566 icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src));
567 icmp6stat.icp6s_checksum++;
568 goto freeit;
569 }
570
571 if (m->m_pkthdr.pkt_flags & PKTF_PROXY_DST) {
572 /*
573 * This is the special case of proxying NS (dst is either
574 * solicited-node multicast or unicast); process it locally
575 * but don't deliver it to sockets. It practically lets us
576 * steer the packet to nd6_prproxy_ns_input, where more
577 * specific tests and actions will be taken.
578 */
579 switch (icmp6->icmp6_type) {
580 case ND_NEIGHBOR_SOLICIT:
581 proxy = 1;
582 break;
583 default:
584 goto freeit;
585 }
586 }
587
588 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
589 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
590 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK) {
591 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
592 }
593
594 switch (icmp6->icmp6_type) {
595 case ICMP6_DST_UNREACH:
596 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
597 switch (code) {
598 case ICMP6_DST_UNREACH_NOROUTE:
599 case ICMP6_DST_UNREACH_ADDR: /* PRC_HOSTDEAD is a DOS */
600 code = PRC_UNREACH_NET;
601 break;
602 case ICMP6_DST_UNREACH_ADMIN:
603 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
604 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
605 break;
606 case ICMP6_DST_UNREACH_BEYONDSCOPE:
607 /* I mean "source address was incorrect." */
608 code = PRC_PARAMPROB;
609 break;
610 case ICMP6_DST_UNREACH_NOPORT:
611 code = PRC_UNREACH_PORT;
612 break;
613 default:
614 goto badcode;
615 }
616 goto deliver;
617
618 case ICMP6_PACKET_TOO_BIG:
619 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
620 if (ntohl(icmp6->icmp6_mtu) < IPV6_MMTU) {
621 icmp6stat.icp6s_badpkttoobig++;
622 goto freeit;
623 }
624
625 code = PRC_MSGSIZE;
626
627 /*
628 * Updating the path MTU will be done after examining
629 * intermediate extension headers.
630 */
631 goto deliver;
632
633 case ICMP6_TIME_EXCEEDED:
634 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
635 switch (code) {
636 case ICMP6_TIME_EXCEED_TRANSIT:
637 code = PRC_TIMXCEED_INTRANS;
638 break;
639 case ICMP6_TIME_EXCEED_REASSEMBLY:
640 code = PRC_TIMXCEED_REASS;
641 break;
642 default:
643 goto badcode;
644 }
645 goto deliver;
646
647 case ICMP6_PARAM_PROB:
648 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
649 switch (code) {
650 case ICMP6_PARAMPROB_NEXTHEADER:
651 code = PRC_UNREACH_PROTOCOL;
652 break;
653 case ICMP6_PARAMPROB_HEADER:
654 case ICMP6_PARAMPROB_OPTION:
655 code = PRC_PARAMPROB;
656 break;
657 default:
658 goto badcode;
659 }
660 goto deliver;
661
662 case ICMP6_ECHO_REQUEST:
663 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
664 if (code != 0) {
665 goto badcode;
666 }
667
668 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
669 /* Give up remote */
670 goto rate_limit_checked;
671 }
672 if ((n->m_flags & M_EXT) != 0
673 || n->m_len < off + sizeof(struct icmp6_hdr)) {
674 mbuf_ref_t n0 = n;
675 const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
676
677 /*
678 * Prepare an internal mbuf. m_pullup() doesn't
679 * always copy the length we specified.
680 */
681 if (maxlen >= MCLBYTES) {
682 /* Give up remote */
683 m_freem(n0);
684 goto rate_limit_checked;
685 }
686 MGETHDR(n, M_DONTWAIT, n0->m_type); /* MAC-OK */
687 if (n && maxlen >= MHLEN) {
688 MCLGET(n, M_DONTWAIT);
689 if ((n->m_flags & M_EXT) == 0) {
690 m_free(n);
691 n = NULL;
692 }
693 }
694 if (n == NULL) {
695 /* Give up remote */
696 m_freem(n0);
697 goto rate_limit_checked;
698 }
699 M_COPY_PKTHDR(n, n0);
700 /*
701 * Copy IPv6 and ICMPv6 only.
702 */
703 nip6 = mtod(n, struct ip6_hdr *);
704 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
705 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
706 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
707 noff = sizeof(struct ip6_hdr);
708 n->m_pkthdr.len = n->m_len =
709 noff + sizeof(struct icmp6_hdr);
710 /*
711 * Adjust mbuf. ip6_plen will be adjusted in
712 * ip6_output().
713 */
714 m_adj(n0, off + sizeof(struct icmp6_hdr));
715 n->m_pkthdr.len += n0->m_pkthdr.len;
716 n->m_next = n0;
717 n0->m_flags &= ~M_PKTHDR;
718 } else {
719 nip6 = mtod(n, struct ip6_hdr *);
720 IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off,
721 sizeof(*nicmp6));
722 noff = off;
723 }
724 if (nicmp6 == NULL) {
725 panic("nicmp6 is NULL in %s, which isn't good!", __FUNCTION__);
726 } else {
727 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
728 nicmp6->icmp6_code = 0;
729 }
730 if (n) {
731 icmp6stat.icp6s_reflect++;
732 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
733 icmp6_reflect(n, noff);
734 }
735 goto rate_limit_checked;
736
737 case ICMP6_ECHO_REPLY:
738 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
739 if (code != 0) {
740 goto badcode;
741 }
742 break;
743
744 case MLD_LISTENER_QUERY:
745 case MLD_LISTENER_REPORT:
746
747 if (icmp6len < sizeof(struct mld_hdr)) {
748 goto badlen;
749 }
750 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) { /* XXX: ugly... */
751 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
752 } else {
753 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
754 }
755
756 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
757 /* give up local */
758 if (mld_input(m, off, icmp6len) == IPPROTO_DONE) {
759 m = NULL;
760 }
761 goto freeit;
762 }
763 if (mld_input(n, off, icmp6len) != IPPROTO_DONE) {
764 m_freem(n);
765 }
766 /* m stays. */
767 goto rate_limit_checked;
768
769 case MLD_LISTENER_DONE:
770 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
771 if (icmp6len < sizeof(struct mld_hdr)) { /* necessary? */
772 goto badlen;
773 }
774 break; /* nothing to be done in kernel */
775
776 case MLD_MTRACE_RESP:
777 case MLD_MTRACE:
778 /* XXX: these two are experimental. not officially defined. */
779 /* XXX: per-interface statistics? */
780 break; /* just pass it to applications */
781
782 case ICMP6_NI_QUERY:
783 if (!icmp6_nodeinfo) {
784 break;
785 }
786 //### LD 10/20 Check fbsd differences here. Not sure we're more advanced or not.
787 /* By RFC 4620 refuse to answer queries from global scope addresses */
788 if ((icmp6_nodeinfo & 8) != 8 && in6_addrscope(&ip6->ip6_src) == IPV6_ADDR_SCOPE_GLOBAL) {
789 break;
790 }
791
792 if (icmp6len < sizeof(struct icmp6_nodeinfo)) {
793 goto badlen;
794 }
795
796 #ifndef PULLDOWN_TEST
797 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
798 return IPPROTO_DONE);
799 #endif
800
801 n = m_copy(m, 0, M_COPYALL);
802 if (n) {
803 n = ni6_input(n, off);
804 }
805 if (n) {
806 noff = sizeof(struct ip6_hdr);
807 icmp6stat.icp6s_reflect++;
808 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
809 icmp6_reflect(n, noff);
810 }
811 goto rate_limit_checked;
812
813 case ICMP6_WRUREPLY:
814 if (code != 0) {
815 goto badcode;
816 }
817 break;
818
819 case ND_ROUTER_SOLICIT:
820 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
821 if (code != 0) {
822 goto badcode;
823 }
824 if (icmp6len < sizeof(struct nd_router_solicit)) {
825 goto badlen;
826 }
827
828 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
829 /* give up local */
830 nd6_rs_input(m, off, icmp6len);
831 m = NULL;
832 goto freeit;
833 }
834 nd6_rs_input(n, off, icmp6len);
835 /* m stays. */
836 goto rate_limit_checked;
837
838 case ND_ROUTER_ADVERT:
839 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
840 if (code != 0) {
841 goto badcode;
842 }
843 if (icmp6len < sizeof(struct nd_router_advert)) {
844 goto badlen;
845 }
846
847 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
848 /* give up local */
849 nd6_ra_input(m, off, icmp6len);
850 m = NULL;
851 goto freeit;
852 }
853 nd6_ra_input(n, off, icmp6len);
854 /* m stays. */
855 goto rate_limit_checked;
856
857 case ND_NEIGHBOR_SOLICIT:
858 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
859 if (code != 0) {
860 goto badcode;
861 }
862 if (icmp6len < sizeof(struct nd_neighbor_solicit)) {
863 goto badlen;
864 }
865
866 if (proxy ||
867 ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL)) {
868 /* give up local */
869 nd6_ns_input(m, off, icmp6len);
870 m = NULL;
871 goto freeit;
872 }
873 nd6_ns_input(n, off, icmp6len);
874 /* m stays. */
875 goto rate_limit_checked;
876
877 case ND_NEIGHBOR_ADVERT:
878 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
879 if (code != 0) {
880 goto badcode;
881 }
882 if (icmp6len < sizeof(struct nd_neighbor_advert)) {
883 goto badlen;
884 }
885
886 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
887 /* give up local */
888 nd6_na_input(m, off, icmp6len);
889 m = NULL;
890 goto freeit;
891 }
892 nd6_na_input(n, off, icmp6len);
893 /* m stays. */
894 goto rate_limit_checked;
895
896 case ND_REDIRECT:
897 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
898 if (code != 0) {
899 goto badcode;
900 }
901 if (icmp6len < sizeof(struct nd_redirect)) {
902 goto badlen;
903 }
904
905 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
906 /* give up local */
907 icmp6_redirect_input(m, off, icmp6len);
908 m = NULL;
909 goto freeit;
910 }
911 icmp6_redirect_input(n, off, icmp6len);
912 /* m stays. */
913 goto rate_limit_checked;
914
915 case ICMP6_ROUTER_RENUMBERING:
916 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
917 code != ICMP6_ROUTER_RENUMBERING_RESULT) {
918 goto badcode;
919 }
920 if (icmp6len < sizeof(struct icmp6_router_renum)) {
921 goto badlen;
922 }
923 break;
924
925 default:
926 nd6log(debug,
927 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
928 icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
929 ip6_sprintf(&ip6->ip6_dst),
930 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0);
931 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
932 /* ICMPv6 error: MUST deliver it by spec... */
933 code = PRC_NCMDS;
934 /* deliver */
935 } else {
936 /* ICMPv6 informational: MUST not deliver */
937 goto rate_limit_checked;
938 }
939 deliver:
940 if (icmp6_notify_error(m, off, icmp6len, code)) {
941 /* In this case, m should've been freed. */
942 return IPPROTO_DONE;
943 }
944 break;
945
946 badcode:
947 icmp6stat.icp6s_badcode++;
948 break;
949
950 badlen:
951 icmp6stat.icp6s_badlen++;
952 break;
953 }
954
955 rate_limit_checked:
956 icmp6_rip6_input(&m, *offp);
957 return IPPROTO_DONE;
958
959 freeit:
960 m_freem(m);
961 return IPPROTO_DONE;
962 }
963
964 static int
icmp6_notify_error(struct mbuf * m,int off,int icmp6len,int code)965 icmp6_notify_error(struct mbuf *m, int off, int icmp6len, int code)
966 {
967 struct icmp6_hdr *icmp6;
968 struct ip6_hdr *__single eip6;
969 u_int32_t notifymtu;
970 struct sockaddr_in6 icmp6src, icmp6dst;
971
972 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
973 icmp6stat.icp6s_tooshort++;
974 goto freeit;
975 }
976 #ifndef PULLDOWN_TEST
977 IP6_EXTHDR_CHECK(m, off,
978 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr),
979 return -1);
980 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
981 #else
982 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
983 sizeof(*icmp6) + sizeof(struct ip6_hdr));
984 if (icmp6 == NULL) {
985 icmp6stat.icp6s_tooshort++;
986 return -1;
987 }
988 #endif
989 eip6 = (struct ip6_hdr *)(icmp6 + 1);
990 SOCKADDR_ZERO(&icmp6dst, sizeof(icmp6dst));
991
992 /* Detect the upper level protocol */
993 {
994 void (*ctlfunc)(int, struct sockaddr *, void *, struct ifnet *);
995 u_int8_t nxt = eip6->ip6_nxt;
996 int eoff = off + sizeof(struct icmp6_hdr) +
997 sizeof(struct ip6_hdr);
998 struct ip6ctlparam ip6cp;
999 int icmp6type = icmp6->icmp6_type;
1000 struct ip6_frag *__single fh;
1001 struct ip6_rthdr *__single rth;
1002 struct ip6_rthdr0 *rth0;
1003 int rthlen;
1004
1005 while (1) { /* XXX: should avoid infinite loop explicitly? */
1006 struct ip6_ext *eh;
1007
1008 switch (nxt) {
1009 case IPPROTO_HOPOPTS:
1010 case IPPROTO_DSTOPTS:
1011 case IPPROTO_AH:
1012 #ifndef PULLDOWN_TEST
1013 IP6_EXTHDR_CHECK(m, 0,
1014 eoff + sizeof(struct ip6_ext), return -1);
1015 eh = (struct ip6_ext *)(mtod(m, caddr_t)
1016 + eoff);
1017 #else
1018 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
1019 eoff, sizeof(*eh));
1020 if (eh == NULL) {
1021 icmp6stat.icp6s_tooshort++;
1022 return -1;
1023 }
1024 #endif
1025
1026 if (nxt == IPPROTO_AH) {
1027 eoff += (eh->ip6e_len + 2) << 2;
1028 } else {
1029 eoff += (eh->ip6e_len + 1) << 3;
1030 }
1031 nxt = eh->ip6e_nxt;
1032 break;
1033 case IPPROTO_ROUTING:
1034 /*
1035 * When the erroneous packet contains a
1036 * routing header, we should examine the
1037 * header to determine the final destination.
1038 * Otherwise, we can't properly update
1039 * information that depends on the final
1040 * destination (e.g. path MTU).
1041 */
1042 #ifndef PULLDOWN_TEST
1043 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth),
1044 return -1);
1045 rth = (struct ip6_rthdr *)
1046 (mtod(m, caddr_t) + eoff);
1047 #else
1048 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
1049 eoff, sizeof(*rth));
1050 if (rth == NULL) {
1051 icmp6stat.icp6s_tooshort++;
1052 return -1;
1053 }
1054 #endif
1055 rthlen = (rth->ip6r_len + 1) << 3;
1056 /*
1057 * XXX: currently there is no
1058 * officially defined type other
1059 * than type-0.
1060 * Note that if the segment left field
1061 * is 0, all intermediate hops must
1062 * have been passed.
1063 */
1064 if (rth->ip6r_segleft &&
1065 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
1066 int hops;
1067
1068 #ifndef PULLDOWN_TEST
1069 IP6_EXTHDR_CHECK(m, 0, eoff + rthlen,
1070 return -1);
1071 rth0 = (struct ip6_rthdr0 *)(mtod(m, caddr_t) + eoff);
1072 #else
1073 IP6_EXTHDR_GET(rth0,
1074 struct ip6_rthdr0 *, m,
1075 eoff, rthlen);
1076 if (rth0 == NULL) {
1077 icmp6stat.icp6s_tooshort++;
1078 return -1;
1079 }
1080 #endif
1081 /* just ignore a bogus header */
1082 if ((rth0->ip6r0_len % 2) == 0 &&
1083 (hops = rth0->ip6r0_len / 2)) {
1084 icmp6dst.sin6_addr = *((struct in6_addr *)(void *)(rth0 + 1) + (hops - 1));
1085 }
1086 }
1087 eoff += rthlen;
1088 nxt = rth->ip6r_nxt;
1089 break;
1090 case IPPROTO_FRAGMENT:
1091 #ifndef PULLDOWN_TEST
1092 IP6_EXTHDR_CHECK(m, 0, eoff +
1093 sizeof(struct ip6_frag),
1094 return -1);
1095 fh = (struct ip6_frag *)(mtod(m, caddr_t)
1096 + eoff);
1097 #else
1098 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
1099 eoff, sizeof(*fh));
1100 if (fh == NULL) {
1101 icmp6stat.icp6s_tooshort++;
1102 return -1;
1103 }
1104 #endif
1105 /*
1106 * Data after a fragment header is meaningless
1107 * unless it is the first fragment, but
1108 * we'll go to the notify label for path MTU
1109 * discovery.
1110 */
1111 if (fh->ip6f_offlg & IP6F_OFF_MASK) {
1112 goto notify;
1113 }
1114
1115 eoff += sizeof(struct ip6_frag);
1116 nxt = fh->ip6f_nxt;
1117 break;
1118 default:
1119 /*
1120 * This case includes ESP and the No Next
1121 * Header. In such cases going to the notify
1122 * label does not have any meaning
1123 * (i.e. ctlfunc will be NULL), but we go
1124 * anyway since we might have to update
1125 * path MTU information.
1126 */
1127 goto notify;
1128 }
1129 }
1130 notify:
1131 #ifndef PULLDOWN_TEST
1132 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1133 #else
1134 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1135 sizeof(*icmp6) + sizeof(struct ip6_hdr));
1136 if (icmp6 == NULL) {
1137 icmp6stat.icp6s_tooshort++;
1138 return -1;
1139 }
1140 #endif
1141
1142 /*
1143 * retrieve parameters from the inner IPv6 header, and convert
1144 * them into sockaddr structures.
1145 * XXX: there is no guarantee that the source or destination
1146 * addresses of the inner packet are in the same scope as
1147 * the addresses of the icmp packet. But there is no other
1148 * way to determine the zone.
1149 */
1150 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1151
1152 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1153 icmp6dst.sin6_family = AF_INET6;
1154 if (IN6_IS_ADDR_UNSPECIFIED(&icmp6dst.sin6_addr)) {
1155 icmp6dst.sin6_addr = eip6->ip6_dst;
1156 }
1157 if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, IN6_NULL_IF_EMBEDDED_SCOPE(&icmp6dst.sin6_scope_id))) {
1158 goto freeit;
1159 }
1160 SOCKADDR_ZERO(&icmp6src, sizeof(icmp6src));
1161 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1162 icmp6src.sin6_family = AF_INET6;
1163 icmp6src.sin6_addr = eip6->ip6_src;
1164 if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, IN6_NULL_IF_EMBEDDED_SCOPE(&icmp6dst.sin6_scope_id))) {
1165 goto freeit;
1166 }
1167 icmp6src.sin6_flowinfo =
1168 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1169
1170 ip6cp.ip6c_m = m;
1171 ip6cp.ip6c_icmp6 = icmp6;
1172 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1173 ip6cp.ip6c_off = eoff;
1174 ip6cp.ip6c_finaldst = &icmp6dst.sin6_addr;
1175 ip6cp.ip6c_src = &icmp6src;
1176 ip6cp.ip6c_nxt = nxt;
1177
1178 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1179 notifymtu = ntohl(icmp6->icmp6_mtu);
1180 ip6cp.ip6c_cmdarg = (void *)¬ifymtu;
1181 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1182 }
1183
1184 ctlfunc = ip6_protox[nxt]->pr_ctlinput;
1185 if (ctlfunc) {
1186 LCK_MTX_ASSERT(inet6_domain_mutex, LCK_MTX_ASSERT_OWNED);
1187
1188 lck_mtx_unlock(inet6_domain_mutex);
1189
1190 (void) (*ctlfunc)(code, SA(&icmp6dst),
1191 &ip6cp, m->m_pkthdr.rcvif);
1192
1193 lck_mtx_lock(inet6_domain_mutex);
1194 }
1195 }
1196 return 0;
1197
1198 freeit:
1199 m_freem(m);
1200 return -1;
1201 }
1202
1203 void
icmp6_mtudisc_update(struct ip6ctlparam * ip6cp,int validated)1204 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
1205 {
1206 struct in6_addr *__single dst = ip6cp->ip6c_finaldst;
1207 struct icmp6_hdr *__single icmp6 = ip6cp->ip6c_icmp6;
1208 mbuf_ref_t m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1209 u_int mtu = ntohl(icmp6->icmp6_mtu);
1210 rtentry_ref_t rt = NULL;
1211 struct sockaddr_in6 sin6;
1212 /*
1213 * we reject ICMPv6 too big with abnormally small value.
1214 * XXX what is the good definition of "abnormally small"?
1215 */
1216 if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8) {
1217 return;
1218 }
1219
1220 if (!validated) {
1221 return;
1222 }
1223
1224 /* Limit the MTU to the minimum IPv6 MTU */
1225 if (mtu < IPV6_MMTU) {
1226 mtu = IPV6_MMTU;
1227 }
1228
1229 SOCKADDR_ZERO(&sin6, sizeof(sin6));
1230 sin6.sin6_family = PF_INET6;
1231 sin6.sin6_len = sizeof(struct sockaddr_in6);
1232 sin6.sin6_addr = *dst;
1233 /* XXX normally, this won't happen */
1234 if (in6_embedded_scope && IN6_IS_ADDR_LINKLOCAL(dst)) {
1235 sin6.sin6_addr.s6_addr16[1] =
1236 htons(m->m_pkthdr.rcvif->if_index);
1237 }
1238 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1239 /*
1240 * XXX On a side note, for asymmetric data-path
1241 * the lookup on receive interace is probably not
1242 * what we want to do.
1243 * That requires looking at the cached route for the
1244 * protocol control block.
1245 */
1246 rt = rtalloc1_scoped(SA(&sin6), 0,
1247 RTF_CLONING | RTF_PRCLONING, m->m_pkthdr.rcvif->if_index);
1248 if (rt != NULL) {
1249 RT_LOCK(rt);
1250 if ((rt->rt_flags & RTF_HOST) &&
1251 !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
1252 mtu < IN6_LINKMTU(rt->rt_ifp) &&
1253 rt->rt_rmx.rmx_mtu > mtu) {
1254 icmp6stat.icp6s_pmtuchg++;
1255 rt->rt_rmx.rmx_mtu = mtu;
1256 }
1257 RT_UNLOCK(rt);
1258 rtfree(rt);
1259 }
1260 }
1261
1262 /*
1263 * Process a Node Information Query packet, based on
1264 * draft-ietf-ipngwg-icmp-name-lookups-07.
1265 *
1266 * Spec incompatibilities:
1267 * - IPv6 Subject address handling
1268 * - IPv4 Subject address handling support missing
1269 * - Proxy reply (answer even if it's not for me)
1270 * - joins NI group address at in6_ifattach() time only, does not cope
1271 * with hostname changes by sethostname(3)
1272 */
1273 #define hostnamelen (uint32_t)strbuflen(hostname, sizeof(hostname))
1274 static struct mbuf *
ni6_input(struct mbuf * m,int off)1275 ni6_input(struct mbuf *m, int off)
1276 {
1277 struct icmp6_nodeinfo *ni6, *nni6;
1278 mbuf_ref_t n = NULL;
1279 u_int16_t qtype;
1280 int subjlen;
1281 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1282 struct ni_reply_fqdn *__single fqdn;
1283 int addrs; /* for NI_QTYPE_NODEADDR */
1284 ifnet_ref_t ifp = NULL; /* for NI_QTYPE_NODEADDR */
1285 struct sockaddr_in6 sin6; /* double meaning; ip6_dst and subjectaddr */
1286 struct sockaddr_in6 sin6_d; /* XXX: we should retrieve this from m_aux */
1287 struct ip6_hdr *__single ip6;
1288 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1289 char *subj = NULL;
1290
1291 ip6 = mtod(m, struct ip6_hdr *);
1292 #ifndef PULLDOWN_TEST
1293 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1294 #else
1295 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1296 if (ni6 == NULL) {
1297 /* m is already reclaimed */
1298 return NULL;
1299 }
1300 #endif
1301
1302 /*
1303 * Validate IPv6 source address.
1304 * The default configuration MUST be to refuse answering queries from
1305 * global-scope addresses according to RFC4602.
1306 * Notes:
1307 * - it's not very clear what "refuse" means; this implementation
1308 * simply drops it.
1309 * - it's not very easy to identify global-scope (unicast) addresses
1310 * since there are many prefixes for them. It should be safer
1311 * and in practice sufficient to check "all" but loopback and
1312 * link-local (note that site-local unicast was deprecated and
1313 * ULA is defined as global scope-wise)
1314 */
1315 if ((icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 &&
1316 !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) &&
1317 !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
1318 goto bad;
1319 }
1320
1321 /*
1322 * Validate IPv6 destination address.
1323 *
1324 * The Responder must discard the Query without further processing
1325 * unless it is one of the Responder's unicast or anycast addresses, or
1326 * a link-local scope multicast address which the Responder has joined.
1327 * [RFC4602, Section 5.]
1328 */
1329 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1330 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst) &&
1331 !IN6_IS_ADDR_MC_UNICAST_BASED_LINKLOCAL(&ip6->ip6_dst)) {
1332 goto bad;
1333 }
1334 /* else it's a link-local multicast, fine */
1335 } else { /* unicast or anycast */
1336 uint32_t ia6_flags;
1337
1338 if (ip6_getdstifaddr_info(m, NULL, &ia6_flags) != 0) {
1339 goto bad; /* XXX impossible */
1340 }
1341 if ((ia6_flags & IN6_IFF_TEMPORARY) &&
1342 !(icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) {
1343 nd6log(debug, "ni6_input: ignore node info to a temporary address in %s:%d",
1344 __func__, __LINE__);
1345 goto bad;
1346 }
1347 }
1348
1349 /* validate query Subject field. */
1350 qtype = ntohs(ni6->ni_qtype);
1351 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1352 switch (qtype) {
1353 case NI_QTYPE_NOOP:
1354 case NI_QTYPE_SUPTYPES:
1355 /* 07 draft */
1356 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) {
1357 break;
1358 }
1359 OS_FALLTHROUGH;
1360 case NI_QTYPE_FQDN:
1361 case NI_QTYPE_NODEADDR:
1362 case NI_QTYPE_IPV4ADDR:
1363 switch (ni6->ni_code) {
1364 case ICMP6_NI_SUBJ_IPV6:
1365 #if ICMP6_NI_SUBJ_IPV6 != 0
1366 case 0:
1367 #endif
1368 /*
1369 * backward compatibility - try to accept 03 draft
1370 * format, where no Subject is present.
1371 */
1372 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1373 subjlen == 0) {
1374 oldfqdn++;
1375 break;
1376 }
1377 #if ICMP6_NI_SUBJ_IPV6 != 0
1378 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) {
1379 goto bad;
1380 }
1381 #endif
1382
1383 if (subjlen != sizeof(struct in6_addr)) {
1384 goto bad;
1385 }
1386
1387 /*
1388 * Validate Subject address.
1389 *
1390 * Not sure what exactly "address belongs to the node"
1391 * means in the spec, is it just unicast, or what?
1392 *
1393 * At this moment we consider Subject address as
1394 * "belong to the node" if the Subject address equals
1395 * to the IPv6 destination address; validation for
1396 * IPv6 destination address should have done enough
1397 * check for us.
1398 *
1399 * We do not do proxy at this moment.
1400 */
1401 /* m_pulldown instead of copy? */
1402 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1403 subjlen, (caddr_t)&sin6.sin6_addr);
1404 sin6.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1405 &sin6.sin6_addr);
1406 in6_embedscope(&sin6.sin6_addr, &sin6, NULL, NULL,
1407 NULL, IN6_NULL_IF_EMBEDDED_SCOPE(&sin6.sin6_scope_id));
1408 SOCKADDR_ZERO(&sin6_d, sizeof(sin6_d));
1409 sin6_d.sin6_family = AF_INET6; /* not used, actually */
1410 sin6_d.sin6_len = sizeof(sin6_d); /* ditto */
1411 sin6_d.sin6_addr = ip6->ip6_dst;
1412 sin6_d.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1413 &ip6->ip6_dst);
1414 in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL,
1415 NULL, IN6_NULL_IF_EMBEDDED_SCOPE(&sin6_d.sin6_scope_id));
1416 subj = (char*)__SA_UTILS_CONV_TO_BYTES(&sin6);
1417 if (SA6_ARE_ADDR_EQUAL(&sin6, &sin6_d)) {
1418 break;
1419 }
1420
1421 /*
1422 * XXX if we are to allow other cases, we should really
1423 * be careful about scope here.
1424 * basically, we should disallow queries toward IPv6
1425 * destination X with subject Y,
1426 * if scope(X) > scope(Y).
1427 * if we allow scope(X) > scope(Y), it will result in
1428 * information leakage across scope boundary.
1429 */
1430 goto bad;
1431
1432 case ICMP6_NI_SUBJ_FQDN:
1433 /*
1434 * Validate Subject name with gethostname(3).
1435 *
1436 * The behavior may need some debate, since:
1437 * - we are not sure if the node has FQDN as
1438 * hostname (returned by gethostname(3)).
1439 * - the code does wildcard match for truncated names.
1440 * however, we are not sure if we want to perform
1441 * wildcard match, if gethostname(3) side has
1442 * truncated hostname.
1443 */
1444 lck_mtx_lock(&hostname_lock);
1445 n = ni6_nametodns(hostname, hostnamelen, 0);
1446 lck_mtx_unlock(&hostname_lock);
1447 if (!n || n->m_next || n->m_len == 0) {
1448 goto bad;
1449 }
1450 IP6_EXTHDR_GET(subj, char *, m,
1451 off + sizeof(struct icmp6_nodeinfo), subjlen);
1452 if (subj == NULL) {
1453 goto bad;
1454 }
1455 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1456 n->m_len)) {
1457 goto bad;
1458 }
1459 m_freem(n);
1460 n = NULL;
1461 break;
1462
1463 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1464 default:
1465 goto bad;
1466 }
1467 break;
1468 }
1469
1470 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1471 switch (qtype) {
1472 case NI_QTYPE_FQDN:
1473 if ((icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0) {
1474 goto bad;
1475 }
1476 break;
1477 case NI_QTYPE_NODEADDR:
1478 case NI_QTYPE_IPV4ADDR:
1479 if ((icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0) {
1480 goto bad;
1481 }
1482 break;
1483 }
1484
1485 /* guess reply length */
1486 switch (qtype) {
1487 case NI_QTYPE_NOOP:
1488 break; /* no reply data */
1489 case NI_QTYPE_SUPTYPES:
1490 replylen += sizeof(u_int32_t);
1491 break;
1492 case NI_QTYPE_FQDN:
1493 /* XXX will append an mbuf */
1494 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1495 break;
1496 case NI_QTYPE_NODEADDR:
1497 addrs = ni6_addrs(ni6, &ifp, subj);
1498 if ((replylen += addrs * (sizeof(struct in6_addr) +
1499 sizeof(u_int32_t))) > MCLBYTES) {
1500 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1501 }
1502 break;
1503 case NI_QTYPE_IPV4ADDR:
1504 /* unsupported - should respond with unknown Qtype? */
1505 break;
1506 default:
1507 /*
1508 * XXX: We must return a reply with the ICMP6 code
1509 * `unknown Qtype' in this case. However we regard the case
1510 * as an FQDN query for backward compatibility.
1511 * Older versions set a random value to this field,
1512 * so it rarely varies in the defined qtypes.
1513 * But the mechanism is not reliable...
1514 * maybe we should obsolete older versions.
1515 */
1516 qtype = NI_QTYPE_FQDN;
1517 /* XXX will append an mbuf */
1518 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1519 oldfqdn++;
1520 break;
1521 }
1522
1523 /* allocate an mbuf to reply. */
1524 MGETHDR(n, M_DONTWAIT, m->m_type); /* MAC-OK */
1525 if (n == NULL) {
1526 m_freem(m);
1527 if (ifp != NULL) {
1528 ifnet_release(ifp);
1529 }
1530 return NULL;
1531 }
1532 M_COPY_PKTHDR(n, m); /* just for recvif */
1533 if (replylen > MHLEN) {
1534 if (replylen > MCLBYTES) {
1535 /*
1536 * XXX: should we try to allocate more? But MCLBYTES
1537 * is probably much larger than IPV6_MMTU...
1538 */
1539 goto bad;
1540 }
1541 MCLGET(n, M_DONTWAIT);
1542 if ((n->m_flags & M_EXT) == 0) {
1543 goto bad;
1544 }
1545 }
1546 n->m_pkthdr.len = n->m_len = replylen;
1547
1548 /* copy mbuf header and IPv6 + Node Information base headers */
1549 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1550 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1551 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1552
1553 /* qtype dependent procedure */
1554 switch (qtype) {
1555 case NI_QTYPE_NOOP:
1556 nni6->ni_code = ICMP6_NI_SUCCESS;
1557 nni6->ni_flags = 0;
1558 break;
1559 case NI_QTYPE_SUPTYPES:
1560 {
1561 u_int32_t v;
1562 nni6->ni_code = ICMP6_NI_SUCCESS;
1563 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1564 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1565 v = (u_int32_t)htonl(0x0000000f);
1566 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1567 break;
1568 }
1569 case NI_QTYPE_FQDN:
1570 nni6->ni_code = ICMP6_NI_SUCCESS;
1571 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1572 sizeof(struct ip6_hdr) +
1573 sizeof(struct icmp6_nodeinfo));
1574 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1575 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1576 /*
1577 * XXX do we really have FQDN in variable "hostname"?
1578 */
1579 lck_mtx_lock(&hostname_lock);
1580 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1581 lck_mtx_unlock(&hostname_lock);
1582 if (n->m_next == NULL) {
1583 goto bad;
1584 }
1585 /* XXX we assume that n->m_next is not a chain */
1586 if (n->m_next->m_next != NULL) {
1587 goto bad;
1588 }
1589 n->m_pkthdr.len += n->m_next->m_len;
1590 break;
1591 case NI_QTYPE_NODEADDR:
1592 {
1593 int lenlim, copied;
1594
1595 nni6->ni_code = ICMP6_NI_SUCCESS;
1596 n->m_pkthdr.len = n->m_len =
1597 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1598 lenlim = (int)M_TRAILINGSPACE(n);
1599 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1600 /* XXX: reset mbuf length */
1601 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1602 sizeof(struct icmp6_nodeinfo) + copied;
1603 break;
1604 }
1605 default:
1606 break; /* XXX impossible! */
1607 }
1608
1609 nni6->ni_type = ICMP6_NI_REPLY;
1610 m_freem(m);
1611 if (ifp != NULL) {
1612 ifnet_release(ifp);
1613 }
1614 return n;
1615
1616 bad:
1617 m_freem(m);
1618 if (n) {
1619 m_freem(n);
1620 }
1621 if (ifp != NULL) {
1622 ifnet_release(ifp);
1623 }
1624 return NULL;
1625 }
1626 #undef hostnamelen
1627
1628 /*
1629 * make a mbuf with DNS-encoded string. no compression support.
1630 *
1631 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1632 * treated as truncated name (two \0 at the end). this is a wild guess.
1633 */
1634 static struct mbuf *
ni6_nametodns(const char * __counted_by (namelen)name,uint32_t namelen,int old)1635 ni6_nametodns(
1636 const char *__counted_by(namelen)name,
1637 uint32_t namelen,
1638 int old) /* return pascal string if non-zero */
1639 {
1640 mbuf_ref_t m;
1641 char *cp, *ep;
1642 const char *p, *q;
1643 int i, nterm;
1644 uint32_t len;
1645
1646 if (old) {
1647 len = namelen + 1;
1648 } else {
1649 len = MCLBYTES;
1650 }
1651
1652 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1653 MGET(m, M_DONTWAIT, MT_DATA);
1654 if (m && len > MLEN) {
1655 MCLGET(m, M_DONTWAIT);
1656 if ((m->m_flags & M_EXT) == 0) {
1657 goto fail;
1658 }
1659 }
1660 if (!m) {
1661 goto fail;
1662 }
1663 m->m_next = NULL;
1664
1665 if (old) {
1666 m->m_len = len;
1667 *mtod(m, char *) = (char)namelen;
1668 bcopy(name, mtod(m, char *) + 1, namelen);
1669 return m;
1670 } else {
1671 m->m_len = 0;
1672 cp = mtod(m, char *);
1673 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1674
1675 /* if not certain about my name, return empty buffer */
1676 if (namelen == 0) {
1677 return m;
1678 }
1679
1680 /*
1681 * guess if it looks like shortened hostname, or FQDN.
1682 * shortened hostname needs two trailing "\0".
1683 */
1684 i = 0;
1685 for (p = name; p < name + namelen; p++) {
1686 if (*p && *p == '.') {
1687 i++;
1688 }
1689 }
1690 if (i < 2) {
1691 nterm = 2;
1692 } else {
1693 nterm = 1;
1694 }
1695
1696 p = name;
1697 while (cp < ep && p < name + namelen) {
1698 i = 0;
1699 for (q = p; q < name + namelen && *q && *q != '.'; q++) {
1700 i++;
1701 }
1702 /* result does not fit into mbuf */
1703 if (cp + i + 1 >= ep) {
1704 goto fail;
1705 }
1706 /*
1707 * DNS label length restriction, RFC1035 page 8.
1708 * "i == 0" case is included here to avoid returning
1709 * 0-length label on "foo..bar".
1710 */
1711 if (i <= 0 || i >= 64) {
1712 goto fail;
1713 }
1714 *cp++ = (char)i;
1715 bcopy(p, cp, i);
1716 cp += i;
1717 p = q;
1718 if (p < name + namelen && *p == '.') {
1719 p++;
1720 }
1721 }
1722 /* termination */
1723 if (cp + nterm >= ep) {
1724 goto fail;
1725 }
1726 while (nterm-- > 0) {
1727 *cp++ = '\0';
1728 }
1729 m->m_len = (int32_t)(cp - mtod(m, char *));
1730 return m;
1731 }
1732
1733 panic("should not reach here");
1734 /* NOTREACHED */
1735
1736 fail:
1737 if (m) {
1738 m_freem(m);
1739 }
1740 return NULL;
1741 }
1742
1743 /*
1744 * check if two DNS-encoded string matches. takes care of truncated
1745 * form (with \0\0 at the end). no compression support.
1746 * XXX upper/lowercase match (see RFC2065)
1747 */
1748 static int
ni6_dnsmatch(const char * __counted_by (alen0)a0,int alen0,const char * __counted_by (blen0)b0,int blen0)1749 ni6_dnsmatch(const char *__counted_by(alen0)a0, int alen0, const char *__counted_by(blen0)b0, int blen0)
1750 {
1751 const char *a = a0, *b = b0;
1752 int l;
1753
1754 /* simplest case - need validation? */
1755 if (alen0 == blen0 && bcmp(a, b, alen0) == 0) {
1756 return 1;
1757 }
1758
1759 /* termination is mandatory */
1760 if (alen0 < 2 || blen0 < 2) {
1761 return 0;
1762 }
1763 if (a0[alen0 - 1] != '\0' || b0[blen0 - 1] != '\0') {
1764 return 0;
1765 }
1766
1767 const int alen = alen0 - 1;
1768 const int blen = blen0 - 1;
1769
1770 while (a - a0 < alen && b - b0 < blen) {
1771 if (a - a0 + 1 > alen || b - b0 + 1 > blen) {
1772 return 0;
1773 }
1774
1775 if ((signed char)a[0] < 0 || (signed char)b[0] < 0) {
1776 return 0;
1777 }
1778 /* we don't support compression yet */
1779 if (a[0] >= 64 || b[0] >= 64) {
1780 return 0;
1781 }
1782
1783 /* truncated case */
1784 if (a[0] == 0 && a - a0 == alen - 1) {
1785 return 1;
1786 }
1787 if (b[0] == 0 && b - b0 == blen - 1) {
1788 return 1;
1789 }
1790 if (a[0] == 0 || b[0] == 0) {
1791 return 0;
1792 }
1793
1794 if (a[0] != b[0]) {
1795 return 0;
1796 }
1797 l = a[0];
1798 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) {
1799 return 0;
1800 }
1801 if (bcmp(a + 1, b + 1, l) != 0) {
1802 return 0;
1803 }
1804
1805 a += 1 + l;
1806 b += 1 + l;
1807 }
1808
1809 if (a - a0 == alen && b - b0 == blen) {
1810 return 1;
1811 } else {
1812 return 0;
1813 }
1814 }
1815
1816 /*
1817 * calculate the number of addresses to be returned in the node info reply.
1818 */
1819 static int
ni6_addrs(struct icmp6_nodeinfo * ni6,struct ifnet ** ifpp,char * __indexable subj)1820 ni6_addrs(struct icmp6_nodeinfo *ni6, struct ifnet **ifpp, char *__indexable subj)
1821 {
1822 ifnet_ref_t ifp;
1823 struct in6_ifaddr *__single ifa6;
1824 struct ifaddr *__single ifa;
1825 struct sockaddr_in6 *__single subj_ip6 = NULL; /* XXX pedant */
1826 int addrs = 0, addrsofif, iffound = 0;
1827 int niflags = ni6->ni_flags;
1828
1829 if (ifpp != NULL) {
1830 *ifpp = NULL;
1831 }
1832
1833 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1834 switch (ni6->ni_code) {
1835 case ICMP6_NI_SUBJ_IPV6:
1836 if (subj == NULL) { /* must be impossible... */
1837 return 0;
1838 }
1839 subj_ip6 = SIN6(subj);
1840 break;
1841 default:
1842 /*
1843 * XXX: we only support IPv6 subject address for
1844 * this Qtype.
1845 */
1846 return 0;
1847 }
1848 }
1849
1850 ifnet_head_lock_shared();
1851 TAILQ_FOREACH(ifp, &ifnet_head, if_list) {
1852 addrsofif = 0;
1853 ifnet_lock_shared(ifp);
1854 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1855 {
1856 IFA_LOCK(ifa);
1857 if (ifa->ifa_addr->sa_family != AF_INET6) {
1858 IFA_UNLOCK(ifa);
1859 continue;
1860 }
1861 ifa6 = ifatoia6(ifa);
1862
1863 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1864 IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
1865 &ifa6->ia_addr.sin6_addr)) {
1866 iffound = 1;
1867 }
1868
1869 /*
1870 * IPv4-mapped addresses can only be returned by a
1871 * Node Information proxy, since they represent
1872 * addresses of IPv4-only nodes, which perforce do
1873 * not implement this protocol.
1874 * [icmp-name-lookups-07, Section 5.4]
1875 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1876 * this function at this moment.
1877 */
1878
1879 /* What do we have to do about ::1? */
1880 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1881 case IPV6_ADDR_SCOPE_LINKLOCAL:
1882 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) {
1883 IFA_UNLOCK(ifa);
1884 continue;
1885 }
1886 break;
1887 case IPV6_ADDR_SCOPE_SITELOCAL:
1888 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) {
1889 IFA_UNLOCK(ifa);
1890 continue;
1891 }
1892 break;
1893 case IPV6_ADDR_SCOPE_GLOBAL:
1894 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) {
1895 IFA_UNLOCK(ifa);
1896 continue;
1897 }
1898 break;
1899 default:
1900 IFA_UNLOCK(ifa);
1901 continue;
1902 }
1903
1904 /*
1905 * check if anycast is okay.
1906 * XXX: just experimental. not in the spec.
1907 */
1908 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1909 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) {
1910 IFA_UNLOCK(ifa);
1911 continue; /* we need only unicast addresses */
1912 }
1913 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1914 (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1915 IFA_UNLOCK(ifa);
1916 continue;
1917 }
1918 addrsofif++; /* count the address */
1919 IFA_UNLOCK(ifa);
1920 }
1921 ifnet_lock_done(ifp);
1922 if (iffound) {
1923 if (ifpp != NULL) {
1924 *ifpp = ifp;
1925 ifnet_reference(ifp);
1926 }
1927 ifnet_head_done();
1928 return addrsofif;
1929 }
1930
1931 addrs += addrsofif;
1932 }
1933 ifnet_head_done();
1934
1935 return addrs;
1936 }
1937
1938 static int
ni6_store_addrs(struct icmp6_nodeinfo * ni6,struct icmp6_nodeinfo * __indexable nni6,struct ifnet * ifp0,int resid)1939 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *__indexable nni6,
1940 struct ifnet *ifp0, int resid)
1941 {
1942 ifnet_ref_t ifp = ifp0;
1943 struct in6_ifaddr *__single ifa6;
1944 struct ifaddr *__single ifa;
1945 ifnet_ref_t ifp_dep = NULL;
1946 int copied = 0, allow_deprecated = 0;
1947 u_char *cp = (u_char *)(nni6 + 1);
1948 int niflags = ni6->ni_flags;
1949 u_int32_t ltime;
1950 uint64_t now = net_uptime();
1951
1952 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) {
1953 return 0; /* needless to copy */
1954 }
1955 again:
1956
1957 ifnet_head_lock_shared();
1958 if (ifp == NULL) {
1959 ifp = TAILQ_FIRST(&ifnet_head);
1960 }
1961
1962 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1963 ifnet_lock_shared(ifp);
1964 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1965 ifa = ifa->ifa_list.tqe_next) {
1966 struct in6_addrlifetime_i *__single lt;
1967
1968 IFA_LOCK(ifa);
1969 if (ifa->ifa_addr->sa_family != AF_INET6) {
1970 IFA_UNLOCK(ifa);
1971 continue;
1972 }
1973 ifa6 = ifatoia6(ifa);
1974
1975 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1976 allow_deprecated == 0) {
1977 /*
1978 * prefererred address should be put before
1979 * deprecated addresses.
1980 */
1981
1982 /* record the interface for later search */
1983 if (ifp_dep == NULL) {
1984 ifp_dep = ifp;
1985 }
1986
1987 IFA_UNLOCK(ifa);
1988 continue;
1989 } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1990 allow_deprecated != 0) {
1991 IFA_UNLOCK(ifa);
1992 continue; /* we now collect deprecated addrs */
1993 }
1994 /* What do we have to do about ::1? */
1995 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1996 case IPV6_ADDR_SCOPE_LINKLOCAL:
1997 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) {
1998 IFA_UNLOCK(ifa);
1999 continue;
2000 }
2001 break;
2002 case IPV6_ADDR_SCOPE_SITELOCAL:
2003 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) {
2004 IFA_UNLOCK(ifa);
2005 continue;
2006 }
2007 break;
2008 case IPV6_ADDR_SCOPE_GLOBAL:
2009 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) {
2010 IFA_UNLOCK(ifa);
2011 continue;
2012 }
2013 break;
2014 default:
2015 IFA_UNLOCK(ifa);
2016 continue;
2017 }
2018
2019 /*
2020 * check if anycast is okay.
2021 * XXX: just experimental. not in the spec.
2022 */
2023 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
2024 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) {
2025 IFA_UNLOCK(ifa);
2026 continue;
2027 }
2028 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
2029 (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
2030 IFA_UNLOCK(ifa);
2031 continue;
2032 }
2033
2034 /* now we can copy the address */
2035 if (resid < sizeof(struct in6_addr) +
2036 sizeof(u_int32_t)) {
2037 IFA_UNLOCK(ifa);
2038 /*
2039 * We give up much more copy.
2040 * Set the truncate flag and return.
2041 */
2042 nni6->ni_flags |=
2043 NI_NODEADDR_FLAG_TRUNCATE;
2044 ifnet_lock_done(ifp);
2045 ifnet_head_done();
2046 return copied;
2047 }
2048
2049 /*
2050 * Set the TTL of the address.
2051 * The TTL value should be one of the following
2052 * according to the specification:
2053 *
2054 * 1. The remaining lifetime of a DHCP lease on the
2055 * address, or
2056 * 2. The remaining Valid Lifetime of a prefix from
2057 * which the address was derived through Stateless
2058 * Autoconfiguration.
2059 *
2060 * Note that we currently do not support stateful
2061 * address configuration by DHCPv6, so the former
2062 * case can't happen.
2063 */
2064 lt = &ifa6->ia6_lifetime;
2065 if (lt->ia6ti_expire == 0) {
2066 ltime = ND6_INFINITE_LIFETIME;
2067 } else {
2068 if (lt->ia6ti_expire > now) {
2069 ltime = htonl((uint32_t)(lt->ia6ti_expire - now));
2070 } else {
2071 ltime = 0;
2072 }
2073 }
2074
2075 bcopy(<ime, cp, sizeof(u_int32_t));
2076 cp += sizeof(u_int32_t);
2077
2078 /* copy the address itself */
2079 bcopy(&ifa6->ia_addr.sin6_addr, cp,
2080 sizeof(struct in6_addr));
2081 /* XXX: KAME link-local hack; remove ifindex */
2082 if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr)) {
2083 ((struct in6_addr *)(void *)cp)->s6_addr16[1] = 0;
2084 }
2085 cp += sizeof(struct in6_addr);
2086
2087 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
2088 copied += (sizeof(struct in6_addr) +
2089 sizeof(u_int32_t));
2090 IFA_UNLOCK(ifa);
2091 }
2092 ifnet_lock_done(ifp);
2093 if (ifp0) { /* we need search only on the specified IF */
2094 break;
2095 }
2096 }
2097 ifnet_head_done();
2098
2099 if (allow_deprecated == 0 && ifp_dep != NULL) {
2100 ifp = ifp_dep;
2101 allow_deprecated = 1;
2102
2103 goto again;
2104 }
2105
2106 return copied;
2107 }
2108
2109 /*
2110 * XXX almost dup'ed code with rip6_input.
2111 */
2112 static int
icmp6_rip6_input(struct mbuf ** mp,int off)2113 icmp6_rip6_input(struct mbuf **mp, int off)
2114 {
2115 mbuf_ref_t m = *mp;
2116 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2117 struct in6pcb *__single in6p;
2118 struct in6pcb *__single last = NULL;
2119 struct sockaddr_in6 rip6src;
2120 struct icmp6_hdr *icmp6;
2121 mbuf_ref_t opts = NULL;
2122 int ret = 0;
2123 ifnet_ref_t ifp = m->m_pkthdr.rcvif;
2124
2125 #ifndef PULLDOWN_TEST
2126 /* this is assumed to be safe. */
2127 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
2128 #else
2129 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
2130 if (icmp6 == NULL) {
2131 /* m is already reclaimed */
2132 return IPPROTO_DONE;
2133 }
2134 #endif
2135
2136 /*
2137 * XXX: the address may have embedded scope zone ID, which should be
2138 * hidden from applications.
2139 */
2140 SOCKADDR_ZERO(&rip6src, sizeof(rip6src));
2141 rip6src.sin6_family = AF_INET6;
2142 rip6src.sin6_len = sizeof(struct sockaddr_in6);
2143 rip6src.sin6_addr = ip6->ip6_src;
2144 if (!in6_embedded_scope) {
2145 rip6src.sin6_scope_id = IN6_IS_SCOPE_EMBED(&rip6src.sin6_addr) ? ip6_input_getsrcifscope(m) : IFSCOPE_NONE;
2146 }
2147 if (sa6_recoverscope(&rip6src, TRUE)) {
2148 return IPPROTO_DONE;
2149 }
2150
2151 lck_rw_lock_shared(&ripcbinfo.ipi_lock);
2152 LIST_FOREACH(in6p, &ripcb, inp_list)
2153 {
2154 if ((in6p->inp_vflag & INP_IPV6) == 0) {
2155 continue;
2156 }
2157 if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6) {
2158 continue;
2159 }
2160 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
2161 !in6_are_addr_equal_scoped(&in6p->in6p_laddr, &ip6->ip6_dst, in6p->inp_lifscope, ifp->if_index)) {
2162 continue;
2163 }
2164 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
2165 !in6_are_addr_equal_scoped(&in6p->in6p_faddr, &ip6->ip6_src, in6p->inp_fifscope, ifp->if_index)) {
2166 continue;
2167 }
2168 if (in6p->in6p_icmp6filt
2169 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
2170 in6p->in6p_icmp6filt)) {
2171 continue;
2172 }
2173
2174 if (inp_restricted_recv(in6p, ifp)) {
2175 continue;
2176 }
2177
2178 if (last) {
2179 mbuf_ref_t n;
2180 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
2181 if ((last->in6p_flags & INP_CONTROLOPTS) != 0 ||
2182 SOFLOW_ENABLED(last->in6p_socket) ||
2183 SO_RECV_CONTROL_OPTS(last->inp_socket)) {
2184 ret = ip6_savecontrol(last, n, &opts);
2185 if (ret != 0) {
2186 m_freem(n);
2187 m_freem(opts);
2188 last = in6p;
2189 continue;
2190 }
2191 }
2192 /* strip intermediate headers */
2193 m_adj(n, off);
2194 so_recv_data_stat(last->in6p_socket, m, 0);
2195 if (sbappendaddr(&last->in6p_socket->so_rcv,
2196 SA(&rip6src),
2197 n, opts, NULL) != 0) {
2198 sorwakeup(last->in6p_socket);
2199 }
2200 opts = NULL;
2201 }
2202 }
2203 last = in6p;
2204 }
2205 if (last) {
2206 if ((last->in6p_flags & INP_CONTROLOPTS) != 0 ||
2207 SOFLOW_ENABLED(last->in6p_socket) ||
2208 SO_RECV_CONTROL_OPTS(last->inp_socket)) {
2209 ret = ip6_savecontrol(last, m, &opts);
2210 if (ret != 0) {
2211 goto error;
2212 }
2213 }
2214 /* strip intermediate headers */
2215 m_adj(m, off);
2216 so_recv_data_stat(last->in6p_socket, m, 0);
2217 if (sbappendaddr(&last->in6p_socket->so_rcv,
2218 SA(&rip6src), m, opts, NULL) != 0) {
2219 sorwakeup(last->in6p_socket);
2220 }
2221 } else {
2222 goto error;
2223 }
2224 lck_rw_done(&ripcbinfo.ipi_lock);
2225 return IPPROTO_DONE;
2226
2227 error:
2228 lck_rw_done(&ripcbinfo.ipi_lock);
2229 m_freem(m);
2230 m_freem(opts);
2231 ip6stat.ip6s_delivered--;
2232 return IPPROTO_DONE;
2233 }
2234
2235 /*
2236 * Reflect the ip6 packet back to the source.
2237 * OFF points to the icmp6 header, counted from the top of the mbuf.
2238 */
2239 void
icmp6_reflect(struct mbuf * m,size_t off)2240 icmp6_reflect(struct mbuf *m, size_t off)
2241 {
2242 mbuf_ref_t m_ip6hdr = m;
2243 struct ip6_hdr *ip6;
2244 struct icmp6_hdr *icmp6;
2245 struct in6_ifaddr *__single ia;
2246 struct in6_addr t, src_storage;
2247 struct in6_addr *__single src = 0;
2248 int plen;
2249 int type, code;
2250 ifnet_ref_t outif = NULL;
2251 struct sockaddr_in6 sa6_src, sa6_dst;
2252 struct nd_ifinfo *__single ndi = NULL;
2253 u_int32_t oflow;
2254 uint32_t sifscope = IFSCOPE_NONE;
2255 uint32_t fifscope = IFSCOPE_NONE;
2256 uint32_t tifscope;
2257 struct ip6_out_args ip6oa;
2258
2259 bzero(&ip6oa, sizeof(ip6oa));
2260 ip6oa.ip6oa_boundif = IFSCOPE_NONE;
2261 ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR |
2262 IP6OAF_INTCOPROC_ALLOWED | IP6OAF_AWDL_UNRESTRICTED |
2263 IP6OAF_MANAGEMENT_ALLOWED | IPOAF_ULTRA_CONSTRAINED_ALLOWED;
2264 ip6oa.ip6oa_sotc = SO_TC_UNSPEC;
2265 ip6oa.ip6oa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
2266
2267 if (!(m->m_pkthdr.pkt_flags & PKTF_LOOP) && m->m_pkthdr.rcvif != NULL) {
2268 ip6oa.ip6oa_boundif = m->m_pkthdr.rcvif->if_index;
2269 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
2270 }
2271
2272 /* too short to reflect */
2273 if (off < sizeof(struct ip6_hdr)) {
2274 nd6log(debug,
2275 "sanity fail: off=%x, sizeof(ip6)=%x in %s:%d\n",
2276 (u_int32_t)off, (u_int32_t)sizeof(struct ip6_hdr),
2277 __func__, __LINE__);
2278 goto bad;
2279 }
2280
2281 /*
2282 * If there are extra headers between IPv6 and ICMPv6, strip
2283 * off that header first.
2284 */
2285 if (off > sizeof(struct ip6_hdr)) {
2286 size_t l;
2287 struct ip6_hdr nip6;
2288
2289 l = off - sizeof(struct ip6_hdr);
2290 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2291 m_adj(m, (int)l);
2292 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2293 if (m->m_len < l) {
2294 if ((m_ip6hdr = m_pulldown(m, 0, (int)l, NULL)) == NULL) {
2295 return;
2296 }
2297 }
2298 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2299 } else { /* off == sizeof(struct ip6_hdr) */
2300 size_t l;
2301 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2302 if (m->m_len < l) {
2303 if ((m_ip6hdr = m_pulldown(m, 0, (int)l, NULL)) == NULL) {
2304 return;
2305 }
2306 }
2307 }
2308 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2309 ip6 = mtod(m_ip6hdr, struct ip6_hdr *);
2310 ip6->ip6_nxt = IPPROTO_ICMPV6;
2311 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2312 type = icmp6->icmp6_type; /* keep type for statistics */
2313 code = icmp6->icmp6_code; /* ditto. */
2314 if (m->m_pkthdr.rcvif != NULL) {
2315 fifscope = m->m_pkthdr.rcvif->if_index;
2316 sifscope = m->m_pkthdr.rcvif->if_index;
2317 } else if (m->m_pkthdr.pkt_flags & PKTF_IFAINFO) {
2318 ip6_getsrcifaddr_info(m, &fifscope, NULL);
2319 ip6_getdstifaddr_info(m, &sifscope, NULL);
2320 } else if (m->m_pkthdr.pkt_ext_flags & PKTF_EXT_OUTPUT_SCOPE) {
2321 fifscope = ip6_output_getsrcifscope(m);
2322 sifscope = ip6_output_getdstifscope(m);
2323 }
2324 fifscope = IN6_IS_SCOPE_EMBED(&ip6->ip6_src) ? fifscope : IFSCOPE_NONE;
2325 sifscope = IN6_IS_SCOPE_EMBED(&ip6->ip6_dst) ? sifscope : IFSCOPE_NONE;
2326 if (!in6_embedded_scope) {
2327 m->m_pkthdr.pkt_flags &= ~PKTF_IFAINFO;
2328 }
2329
2330 t = ip6->ip6_dst;
2331 /*
2332 * ip6_input() drops a packet if its src is multicast.
2333 * So, the src is never multicast.
2334 */
2335 ip6->ip6_dst = ip6->ip6_src;
2336 /*
2337 * XXX: make sure to embed scope zone information, using
2338 * already embedded IDs or the received interface (if any).
2339 * Note that rcvif may be NULL.
2340 */
2341 SOCKADDR_ZERO(&sa6_src, sizeof(sa6_src));
2342 sa6_src.sin6_family = AF_INET6;
2343 sa6_src.sin6_len = sizeof(sa6_src);
2344 sa6_src.sin6_addr = ip6->ip6_dst;
2345 if (!in6_embedded_scope) {
2346 sa6_src.sin6_scope_id = fifscope;
2347 }
2348 in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif);
2349 in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL, NULL, IN6_NULL_IF_EMBEDDED_SCOPE(&sa6_src.sin6_scope_id));
2350 in6_verify_ifscope(&ip6->ip6_dst, sa6_src.sin6_scope_id);
2351 ip6_output_setdstifscope(m, sa6_src.sin6_scope_id, NULL);
2352
2353 if (!in6_embedded_scope &&
2354 (ip6oa.ip6oa_flags & IP6OAF_BOUND_IF) == 0 &&
2355 IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
2356 ip6oa.ip6oa_boundif = sa6_src.sin6_scope_id;
2357 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
2358 }
2359
2360 SOCKADDR_ZERO(&sa6_dst, sizeof(sa6_dst));
2361 sa6_dst.sin6_family = AF_INET6;
2362 sa6_dst.sin6_len = sizeof(sa6_dst);
2363 sa6_dst.sin6_addr = t;
2364 if (!in6_embedded_scope) {
2365 sa6_dst.sin6_scope_id = sifscope;
2366 }
2367 in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif);
2368 in6_embedscope(&t, &sa6_dst, NULL, NULL, NULL, IN6_NULL_IF_EMBEDDED_SCOPE(&sa6_dst.sin6_scope_id));
2369 tifscope = sa6_dst.sin6_scope_id;
2370 /*
2371 * If the incoming packet was addressed directly to us(i.e. unicast),
2372 * use dst as the src for the reply.
2373 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2374 * (for example) when we encounter an error while forwarding procedure
2375 * destined to a duplicated address of ours.
2376 * Note that ip6_getdstifaddr() may fail if we are in an error handling
2377 * procedure of an outgoing packet of our own, in which case we need
2378 * to search in the ifaddr list.
2379 */
2380 lck_rw_lock_shared(&in6_ifaddr_rwlock);
2381 TAILQ_FOREACH(ia, IN6ADDR_HASH(&t), ia6_hash) {
2382 IFA_LOCK(&ia->ia_ifa);
2383 if (in6_are_addr_equal_scoped(&t, &ia->ia_addr.sin6_addr, tifscope, ia->ia_addr.sin6_scope_id) &&
2384 (ia->ia6_flags & (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY)) == 0) {
2385 IFA_UNLOCK(&ia->ia_ifa);
2386 src = &t;
2387 sifscope = tifscope;
2388 break;
2389 }
2390 IFA_UNLOCK(&ia->ia_ifa);
2391 }
2392 lck_rw_done(&in6_ifaddr_rwlock);
2393 if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) &&
2394 ((m->m_flags & M_LOOP) || (m->m_pkthdr.pkt_flags & PKTF_LOOP))) {
2395 /*
2396 * This is the case if the dst is our link-local address
2397 * and the sender is also ourselves. Here we test for both
2398 * M_LOOP and PKTF_LOOP, since the former may have been set
2399 * in ip6_output() and that we get here as part of callling
2400 * ip6_process_hopopts(). See comments in <sys/mbuf.h>
2401 */
2402 src = &t;
2403 sifscope = tifscope;
2404 }
2405
2406 if (src == NULL) {
2407 int e;
2408 struct sockaddr_in6 sin6;
2409 struct route_in6 ro;
2410
2411 /*
2412 * This case matches to multicasts, our anycast, or unicasts
2413 * that we do not own. Select a source address based on the
2414 * source address of the erroneous packet.
2415 */
2416 SOCKADDR_ZERO(&sin6, sizeof(sin6));
2417 sin6.sin6_family = AF_INET6;
2418 sin6.sin6_len = sizeof(sin6);
2419 sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */
2420 if (!in6_embedded_scope) {
2421 sin6.sin6_scope_id = fifscope;
2422 }
2423
2424 bzero(&ro, sizeof(ro));
2425 /*
2426 * in6_selectsrc() might return outif with its reference held
2427 * even in the error case, so we always need to release it
2428 * if non-NULL.
2429 */
2430 src = in6_selectsrc(&sin6, NULL, NULL, &ro, &outif,
2431 &src_storage, ip6oa.ip6oa_boundif, &e);
2432 ROUTE_RELEASE(&ro);
2433 if (src == NULL) {
2434 nd6log(debug,
2435 "icmp6_reflect: source can't be determined: "
2436 "dst=%s, error=%d\n",
2437 ip6_sprintf(&sa6_src.sin6_addr), e);
2438 goto bad;
2439 }
2440 if (outif != NULL) {
2441 sifscope = outif->if_index;
2442 }
2443 }
2444 oflow = ip6->ip6_flow; /* Save for later */
2445 ip6->ip6_src = *src;
2446 ip6->ip6_flow = 0;
2447 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2448 ip6->ip6_vfc |= IPV6_VERSION;
2449 ip6_output_setsrcifscope(m, sifscope, NULL);
2450 in6_verify_ifscope(&ip6->ip6_src, sifscope);
2451
2452 if (icmp6->icmp6_type == ICMP6_ECHO_REPLY && icmp6->icmp6_code == 0) {
2453 ip6->ip6_flow |= (oflow & htonl(0x0ff00000));
2454 }
2455 ip6->ip6_nxt = IPPROTO_ICMPV6;
2456 if (outif != NULL && (ndi = ND_IFINFO(outif)) != NULL &&
2457 ndi->initialized) {
2458 lck_mtx_lock(&ndi->lock);
2459 ip6->ip6_hlim = ndi->chlim;
2460 lck_mtx_unlock(&ndi->lock);
2461 }
2462 if (m->m_pkthdr.rcvif != NULL &&
2463 (ndi = ND_IFINFO(m->m_pkthdr.rcvif)) != NULL &&
2464 ndi->initialized) {
2465 /* XXX: This may not be the outgoing interface */
2466 lck_mtx_lock(&ndi->lock);
2467 ip6->ip6_hlim = ndi->chlim;
2468 lck_mtx_unlock(&ndi->lock);
2469 } else {
2470 ip6->ip6_hlim = (uint8_t)ip6_defhlim;
2471 }
2472 /* Use the same traffic class as in the request to match IPv4 */
2473 icmp6->icmp6_cksum = 0;
2474 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2475 sizeof(struct ip6_hdr), plen);
2476
2477 /*
2478 * XXX option handling
2479 */
2480 m->m_flags &= ~(M_BCAST | M_MCAST);
2481
2482 if (outif != NULL) {
2483 ifnet_release(outif);
2484 outif = NULL;
2485 }
2486
2487 m->m_pkthdr.csum_data = 0;
2488 m->m_pkthdr.csum_flags = 0;
2489 ip6_output(m, NULL, NULL, IPV6_OUTARGS, NULL, &outif, &ip6oa);
2490 if (outif != NULL) {
2491 icmp6_ifoutstat_inc(outif, type, code);
2492 ifnet_release(outif);
2493 }
2494 return;
2495
2496 bad:
2497 m_freem(m);
2498 if (outif != NULL) {
2499 ifnet_release(outif);
2500 }
2501 return;
2502 }
2503
2504 static const char *
icmp6_redirect_diag(struct in6_addr * src6,struct in6_addr * dst6,struct in6_addr * tgt6)2505 icmp6_redirect_diag(struct in6_addr *src6,
2506 struct in6_addr *dst6,
2507 struct in6_addr *tgt6)
2508 {
2509 static char buf[1024];
2510 return tsnprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2511 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2512 }
2513
2514 void
icmp6_redirect_input(struct mbuf * m,int off,int icmp6len)2515 icmp6_redirect_input(struct mbuf *m, int off, int icmp6len)
2516 {
2517 ifnet_ref_t ifp = NULL;
2518 struct ip6_hdr *__single ip6 = NULL;
2519 struct nd_redirect *nd_rd = NULL;
2520 char *lladdr = NULL;
2521 int lladdrlen = 0;
2522 char *redirhdr = NULL;
2523 int redirhdrlen = 0;
2524 rtentry_ref_t rt = NULL;
2525 int is_router = 0;
2526 int is_onlink = 0;
2527 struct in6_addr src6 = {};
2528 struct in6_addr redtgt6 = {};
2529 struct in6_addr reddst6 = {};
2530 uint32_t src_ifscope = IFSCOPE_NONE, redtgt_ifscope = IFSCOPE_NONE, reddst_ifscope = IFSCOPE_NONE;
2531 union nd_opts ndopts = {};
2532
2533 if (m == NULL) {
2534 return;
2535 }
2536
2537 ifp = m->m_pkthdr.rcvif;
2538 if (ifp == NULL) {
2539 goto freeit;
2540 }
2541
2542 /*
2543 * If we are an advertising router on this interface,
2544 * don't update route by icmp6 redirect.
2545 */
2546 if (ifp->if_ipv6_router_mode == IPV6_ROUTER_MODE_EXCLUSIVE) {
2547 goto freeit;
2548 }
2549 if (!icmp6_rediraccept) {
2550 goto freeit;
2551 }
2552
2553 ip6 = mtod(m, struct ip6_hdr *);
2554 src6 = ip6->ip6_src;
2555 src_ifscope = in6_addr2scopeid(ifp, &src6);
2556 #ifndef PULLDOWN_TEST
2557 IP6_EXTHDR_CHECK(m, off, icmp6len, return );
2558 nd_rd = (struct nd_redirect *)(mtod(m, caddr_t) + off);
2559 #else
2560 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2561 if (nd_rd == NULL) {
2562 icmp6stat.icp6s_tooshort++;
2563 goto freeit;
2564 }
2565 #endif
2566 ip6 = mtod(m, struct ip6_hdr *);
2567
2568 redtgt6 = nd_rd->nd_rd_target;
2569 reddst6 = nd_rd->nd_rd_dst;
2570
2571 if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, &redtgt_ifscope) ||
2572 in6_setscope(&reddst6, m->m_pkthdr.rcvif, &reddst_ifscope)) {
2573 goto freeit;
2574 }
2575
2576 /* validation */
2577 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2578 nd6log(error,
2579 "ICMP6 redirect sent from %s rejected; "
2580 "must be from linklocal\n", ip6_sprintf(&src6));
2581 goto bad;
2582 }
2583 if (ip6->ip6_hlim != IPV6_MAXHLIM) {
2584 nd6log(error,
2585 "ICMP6 redirect sent from %s rejected; "
2586 "hlim=%d (must be 255)\n",
2587 ip6_sprintf(&src6), ip6->ip6_hlim);
2588 goto bad;
2589 }
2590 {
2591 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2592 struct sockaddr_in6 sin6;
2593 struct in6_addr *__single gw6;
2594
2595 SOCKADDR_ZERO(&sin6, sizeof(sin6));
2596 sin6.sin6_family = AF_INET6;
2597 sin6.sin6_len = sizeof(struct sockaddr_in6);
2598 if (!in6_embedded_scope) {
2599 sin6.sin6_scope_id = reddst_ifscope;
2600 }
2601 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2602 rt = rtalloc1_scoped(SA(&sin6), 0, 0, ifp->if_index);
2603 if (rt) {
2604 RT_LOCK(rt);
2605 if (rt->rt_gateway == NULL ||
2606 rt->rt_gateway->sa_family != AF_INET6) {
2607 nd6log(error,
2608 "ICMP6 redirect rejected; no route "
2609 "with inet6 gateway found for redirect dst: %s\n",
2610 icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
2611 RT_UNLOCK(rt);
2612 rtfree(rt);
2613 goto bad;
2614 }
2615
2616 gw6 = &((SIN6(rt->rt_gateway))->sin6_addr);
2617 if (!in6_are_addr_equal_scoped(&src6, gw6, src_ifscope, (SIN6(rt->rt_gateway))->sin6_scope_id)) {
2618 nd6log(error,
2619 "ICMP6 redirect rejected; "
2620 "not equal to gw-for-src=%s (must be same): "
2621 "%s\n",
2622 ip6_sprintf(gw6),
2623 icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
2624 RT_UNLOCK(rt);
2625 rtfree(rt);
2626 goto bad;
2627 }
2628 } else {
2629 nd6log(error,
2630 "ICMP6 redirect rejected; "
2631 "no route found for redirect dst: %s\n",
2632 icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
2633 goto bad;
2634 }
2635 RT_UNLOCK(rt);
2636 rtfree(rt);
2637 rt = NULL;
2638 }
2639 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2640 nd6log(error,
2641 "ICMP6 redirect rejected; "
2642 "redirect dst must be unicast: %s\n",
2643 icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
2644 goto bad;
2645 }
2646
2647 is_router = is_onlink = 0;
2648 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) {
2649 is_router = 1; /* router case */
2650 }
2651 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) {
2652 is_onlink = 1; /* on-link destination case */
2653 }
2654 if (!is_router && !is_onlink) {
2655 nd6log(error,
2656 "ICMP6 redirect rejected; "
2657 "neither router case nor onlink case: %s\n",
2658 icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
2659 goto bad;
2660 }
2661 /* validation passed */
2662
2663 icmp6len -= sizeof(*nd_rd);
2664
2665 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2666 if (nd6_options(&ndopts) < 0) {
2667 nd6log(info, "icmp6_redirect_input: "
2668 "invalid ND option, rejected: %s\n",
2669 icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
2670 /* nd6_options have incremented stats */
2671 goto freeit;
2672 }
2673
2674 if (ndopts.nd_opts_tgt_lladdr) {
2675 ND_OPT_LLADDR(ndopts.nd_opts_tgt_lladdr, nd_opt_len, lladdr, lladdrlen);
2676 }
2677
2678 if (ndopts.nd_opts_rh) {
2679 ND_OPT_LLADDR(ndopts.nd_opts_rh, nd_opt_rh_len, redirhdr, redirhdrlen);
2680 }
2681
2682 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2683 nd6log(info,
2684 "icmp6_redirect_input: lladdrlen mismatch for %s "
2685 "(if %d, icmp6 packet %d): %s\n",
2686 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2687 icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
2688 goto bad;
2689 }
2690
2691 /* RFC 2461 8.3 */
2692 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2693 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER, NULL);
2694
2695 if (!is_onlink) { /* better router case. perform rtredirect. */
2696 /* perform rtredirect */
2697 struct sockaddr_in6 sdst;
2698 struct sockaddr_in6 sgw;
2699 struct sockaddr_in6 ssrc;
2700
2701 SOCKADDR_ZERO(&sdst, sizeof(sdst));
2702 SOCKADDR_ZERO(&sgw, sizeof(sgw));
2703 SOCKADDR_ZERO(&ssrc, sizeof(ssrc));
2704 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2705 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2706 sizeof(struct sockaddr_in6);
2707 if (!in6_embedded_scope) {
2708 sdst.sin6_scope_id = reddst_ifscope;
2709 sgw.sin6_scope_id = redtgt_ifscope;
2710 ssrc.sin6_scope_id = src_ifscope;
2711 }
2712 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2713 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2714 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2715
2716 rtredirect(ifp, SA(&sdst),
2717 SA(&sgw), NULL, RTF_GATEWAY | RTF_HOST,
2718 SA(&ssrc), NULL);
2719 }
2720 /* finally update cached route in each socket via pfctlinput */
2721 {
2722 struct sockaddr_in6 sdst;
2723
2724 SOCKADDR_ZERO(&sdst, sizeof(sdst));
2725 sdst.sin6_family = AF_INET6;
2726 sdst.sin6_len = sizeof(struct sockaddr_in6);
2727 if (!in6_embedded_scope) {
2728 sdst.sin6_scope_id = reddst_ifscope;
2729 }
2730 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2731
2732 pfctlinput(PRC_REDIRECT_HOST, SA(&sdst));
2733 #if IPSEC
2734 key_sa_routechange(SA(&sdst));
2735 #endif
2736 }
2737
2738 freeit:
2739 m_freem(m);
2740 return;
2741
2742 bad:
2743 icmp6stat.icp6s_badredirect++;
2744 m_freem(m);
2745 }
2746
2747 void
icmp6_redirect_output(struct mbuf * m0,struct rtentry * rt)2748 icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
2749 {
2750 ifnet_ref_t ifp; /* my outgoing interface */
2751 struct in6_addr ifp_ll6;
2752 struct in6_addr *__single router_ll6;
2753 struct ip6_hdr *__single sip6; /* m0 as struct ip6_hdr */
2754 mbuf_ref_t m = NULL; /* newly allocated one */
2755 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2756 struct nd_redirect *nd_rd;
2757 size_t maxlen;
2758 u_char *p;
2759 ifnet_ref_t outif = NULL;
2760 struct sockaddr_in6 src_sa;
2761 struct ip6_out_args ip6oa;
2762
2763 bzero(&ip6oa, sizeof(ip6oa));
2764 ip6oa.ip6oa_boundif = IFSCOPE_NONE;
2765 ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR;
2766 ip6oa.ip6oa_sotc = SO_TC_UNSPEC;
2767 ip6oa.ip6oa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
2768
2769 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2770
2771 if (rt != NULL) {
2772 RT_LOCK(rt);
2773 }
2774
2775 /* sanity check */
2776 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) {
2777 goto fail;
2778 }
2779
2780 /*
2781 * If we are not a router to begin with, or not an advertising
2782 * router on this interface, don't send icmp6 redirect.
2783 */
2784 if (!ip6_forwarding || ifp->if_ipv6_router_mode != IPV6_ROUTER_MODE_EXCLUSIVE) {
2785 goto fail;
2786 }
2787
2788 /*
2789 * Address check:
2790 * the source address must identify a neighbor, and
2791 * the destination address must not be a multicast address
2792 * [RFC 2461, sec 8.2]
2793 */
2794 sip6 = mtod(m0, struct ip6_hdr *);
2795 SOCKADDR_ZERO(&src_sa, sizeof(src_sa));
2796 src_sa.sin6_family = AF_INET6;
2797 src_sa.sin6_len = sizeof(src_sa);
2798 src_sa.sin6_addr = sip6->ip6_src;
2799 /* we don't currently use sin6_scope_id, but eventually use it */
2800 src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src);
2801
2802 RT_UNLOCK(rt);
2803 if (nd6_is_addr_neighbor(&src_sa, ifp, 0) == 0) {
2804 /* already unlocked */
2805 rt = NULL;
2806 goto fail;
2807 }
2808 RT_LOCK(rt);
2809 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) {
2810 goto fail; /* what should we do here? */
2811 }
2812 /* rate limit */
2813 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) {
2814 goto fail;
2815 }
2816
2817 /*
2818 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2819 * we almost always ask for an mbuf cluster for simplicity.
2820 * (MHLEN < IPV6_MMTU is almost always true)
2821 */
2822 #if IPV6_MMTU >= MCLBYTES
2823 # error assumption failed about IPV6_MMTU and MCLBYTES
2824 #endif
2825 MGETHDR(m, M_DONTWAIT, MT_HEADER); /* MAC-OK */
2826 if (m && IPV6_MMTU >= MHLEN) {
2827 MCLGET(m, M_DONTWAIT);
2828 }
2829 if (!m) {
2830 goto fail;
2831 }
2832 m->m_pkthdr.rcvif = NULL;
2833 m->m_len = 0;
2834 maxlen = M_TRAILINGSPACE(m);
2835 maxlen = MIN(IPV6_MMTU, maxlen);
2836 /* just for safety */
2837 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2838 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2839 goto fail;
2840 }
2841
2842 {
2843 /* get ip6 linklocal address for ifp(my outgoing interface). */
2844 struct in6_ifaddr *ia;
2845 if ((ia = in6ifa_ifpforlinklocal(ifp,
2846 IN6_IFF_NOTREADY |
2847 IN6_IFF_ANYCAST)) == NULL) {
2848 goto fail;
2849 }
2850 IFA_LOCK(&ia->ia_ifa);
2851 ifp_ll6 = ia->ia_addr.sin6_addr;
2852 IFA_UNLOCK(&ia->ia_ifa);
2853 ifa_remref(&ia->ia_ifa);
2854 }
2855
2856 /* get ip6 linklocal address for the router. */
2857 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2858 struct sockaddr_in6 *__single sin6;
2859 sin6 = SIN6(rt->rt_gateway);
2860 router_ll6 = &sin6->sin6_addr;
2861 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) {
2862 router_ll6 = (struct in6_addr *)NULL;
2863 }
2864 } else {
2865 router_ll6 = (struct in6_addr *)NULL;
2866 }
2867
2868 /* ip6 */
2869 ip6 = mtod(m, struct ip6_hdr *);
2870 ip6->ip6_flow = 0;
2871 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2872 ip6->ip6_vfc |= IPV6_VERSION;
2873 /* ip6->ip6_plen will be set later */
2874 ip6->ip6_nxt = IPPROTO_ICMPV6;
2875 ip6->ip6_hlim = IPV6_MAXHLIM;
2876 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2877 bcopy(&ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2878 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2879
2880 /* ND Redirect */
2881 nd_rd = (struct nd_redirect *)(ip6 + 1);
2882 nd_rd->nd_rd_type = ND_REDIRECT;
2883 nd_rd->nd_rd_code = 0;
2884 nd_rd->nd_rd_reserved = 0;
2885 if (rt->rt_flags & RTF_GATEWAY) {
2886 /*
2887 * nd_rd->nd_rd_target must be a link-local address in
2888 * better router cases.
2889 */
2890 if (!router_ll6) {
2891 goto fail;
2892 }
2893 bcopy(router_ll6, &nd_rd->nd_rd_target,
2894 sizeof(nd_rd->nd_rd_target));
2895 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2896 sizeof(nd_rd->nd_rd_dst));
2897 } else {
2898 /* make sure redtgt == reddst */
2899 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2900 sizeof(nd_rd->nd_rd_target));
2901 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2902 sizeof(nd_rd->nd_rd_dst));
2903 }
2904 RT_UNLOCK(rt);
2905 rt = NULL;
2906
2907 p = (u_char *)(nd_rd + 1);
2908
2909 if (!router_ll6) {
2910 goto nolladdropt;
2911 }
2912
2913 {
2914 /* target lladdr option */
2915 rtentry_ref_t rt_router = NULL;
2916 int len;
2917 struct sockaddr_dl *__single sdl;
2918 struct nd_opt_hdr *nd_opt;
2919 char *lladdr;
2920
2921 /* Callee returns a locked route upon success */
2922 rt_router = nd6_lookup(router_ll6, 0, ifp, 0);
2923 if (!rt_router) {
2924 goto nolladdropt;
2925 }
2926 RT_LOCK_ASSERT_HELD(rt_router);
2927 len = sizeof(*nd_opt) + ifp->if_addrlen;
2928 len = (len + 7) & ~7; /* round by 8 */
2929 /* safety check */
2930 if (len + (p - (u_char *)ip6) > maxlen) {
2931 RT_REMREF_LOCKED(rt_router);
2932 RT_UNLOCK(rt_router);
2933 goto nolladdropt;
2934 }
2935
2936 if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2937 (rt_router->rt_flags & RTF_LLINFO) &&
2938 (rt_router->rt_gateway->sa_family == AF_LINK) &&
2939 (sdl = SDL(rt_router->rt_gateway)) && sdl->sdl_alen) {
2940 nd_opt = (struct nd_opt_hdr *)p;
2941 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2942 nd_opt->nd_opt_len = (uint8_t)(len >> 3);
2943 lladdr = (char *)(nd_opt + 1);
2944 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2945 p += len;
2946 }
2947 RT_REMREF_LOCKED(rt_router);
2948 RT_UNLOCK(rt_router);
2949 }
2950
2951 nolladdropt:;
2952
2953 m->m_pkthdr.len = m->m_len = (int32_t)(p - (u_char *)ip6);
2954
2955 /* just to be safe */
2956 #ifdef M_DECRYPTED /*not openbsd*/
2957 if (m0->m_flags & M_DECRYPTED) {
2958 goto noredhdropt;
2959 }
2960 #endif
2961 if (p - (u_char *)ip6 > maxlen) {
2962 goto noredhdropt;
2963 }
2964
2965 {
2966 /* redirected header option */
2967 int len;
2968 struct nd_opt_rd_hdr *nd_opt_rh;
2969
2970 /*
2971 * compute the maximum size for icmp6 redirect header option.
2972 * XXX room for auth header?
2973 */
2974 len = (int)(maxlen - (p - (u_char *)ip6));
2975 len &= ~7;
2976
2977 /* This is just for simplicity. */
2978 if (m0->m_pkthdr.len != m0->m_len) {
2979 if (m0->m_next) {
2980 m_freem(m0->m_next);
2981 m0->m_next = NULL;
2982 }
2983 m0->m_pkthdr.len = m0->m_len;
2984 }
2985
2986 /*
2987 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2988 * about padding/truncate rule for the original IP packet.
2989 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2990 * - "attach as much as possible" is the goal
2991 * - pad if not aligned (original size can be guessed by original
2992 * ip6 header)
2993 * Following code adds the padding if it is simple enough,
2994 * and truncates if not.
2995 */
2996 if (m0->m_next || m0->m_pkthdr.len != m0->m_len) {
2997 panic("assumption failed in %s:%d", __func__, __LINE__);
2998 }
2999
3000 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
3001 /* not enough room, truncate */
3002 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
3003 } else {
3004 /* enough room, pad or truncate */
3005 size_t extra;
3006
3007 extra = m0->m_pkthdr.len % 8;
3008 if (extra) {
3009 /* pad if easy enough, truncate if not */
3010 if (8 - extra <= M_TRAILINGSPACE(m0)) {
3011 /* pad */
3012 m0->m_len += (8 - extra);
3013 m0->m_pkthdr.len += (8 - extra);
3014 } else {
3015 /* truncate */
3016 m0->m_pkthdr.len -= extra;
3017 m0->m_len -= extra;
3018 }
3019 }
3020 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
3021 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
3022 }
3023
3024 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
3025 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
3026 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
3027 nd_opt_rh->nd_opt_rh_len = (uint8_t)(len >> 3);
3028 p += sizeof(*nd_opt_rh);
3029 m->m_pkthdr.len = m->m_len = (int32_t)(p - (u_char *)ip6);
3030
3031 /* connect m0 to m */
3032 m->m_next = m0;
3033 m->m_pkthdr.len = m->m_len + m0->m_len;
3034 }
3035 noredhdropt:;
3036
3037 /* XXX: clear embedded link IDs in the inner header */
3038 in6_clearscope(&sip6->ip6_src);
3039 in6_clearscope(&sip6->ip6_dst);
3040 in6_clearscope(&nd_rd->nd_rd_target);
3041 in6_clearscope(&nd_rd->nd_rd_dst);
3042
3043 ip6->ip6_plen = htons((uint16_t)(m->m_pkthdr.len - sizeof(struct ip6_hdr)));
3044
3045 nd_rd->nd_rd_cksum = 0;
3046 nd_rd->nd_rd_cksum
3047 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
3048
3049 /* send the packet to outside... */
3050 ip6oa.ip6oa_boundif = ifp->if_index;
3051 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
3052 ip6oa.ip6oa_flags |= IP6OAF_DONT_FRAG;
3053
3054 ip6_output_setsrcifscope(m, ifp->if_index, NULL);
3055 ip6_output_setdstifscope(m, ifp->if_index, NULL);
3056
3057 ip6_output(m, NULL, NULL, IPV6_OUTARGS, NULL, &outif, &ip6oa);
3058 if (outif) {
3059 icmp6_ifstat_inc(outif, ifs6_out_msg);
3060 icmp6_ifstat_inc(outif, ifs6_out_redirect);
3061 ifnet_release(outif);
3062 }
3063 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
3064
3065 return;
3066
3067 fail:
3068 if (rt != NULL) {
3069 RT_UNLOCK(rt);
3070 }
3071 if (m) {
3072 m_freem(m);
3073 }
3074 if (m0) {
3075 m_freem(m0);
3076 }
3077 }
3078
3079 /*
3080 * ICMPv6 socket option processing.
3081 */
3082 int
icmp6_ctloutput(struct socket * so,struct sockopt * sopt)3083 icmp6_ctloutput(struct socket *so, struct sockopt *sopt)
3084 {
3085 int error = 0;
3086 size_t optlen;
3087 struct inpcb *__single inp = sotoinpcb(so);
3088 int level, op, optname;
3089
3090 if (sopt) {
3091 level = sopt->sopt_level;
3092 op = sopt->sopt_dir;
3093 optname = sopt->sopt_name;
3094 optlen = sopt->sopt_valsize;
3095 } else {
3096 level = op = optname = optlen = 0;
3097 }
3098
3099 if (level != IPPROTO_ICMPV6) {
3100 return EINVAL;
3101 }
3102
3103 switch (op) {
3104 case PRCO_SETOPT:
3105 switch (optname) {
3106 case ICMP6_FILTER:
3107 {
3108 struct icmp6_filter *__single p;
3109
3110 if (optlen != 0 && optlen != sizeof(*p)) {
3111 error = EMSGSIZE;
3112 break;
3113 }
3114 if (inp->in6p_icmp6filt == NULL) {
3115 error = EINVAL;
3116 break;
3117 }
3118
3119 if (optlen == 0) {
3120 /* According to RFC 3542, an installed filter can be
3121 * cleared by issuing a setsockopt for ICMP6_FILTER
3122 * with a zero length.
3123 */
3124 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
3125 } else {
3126 error = sooptcopyin(sopt, __unsafe_forge_bidi_indexable(void*, inp->in6p_icmp6filt, optlen), optlen,
3127 optlen);
3128 }
3129 break;
3130 }
3131
3132 default:
3133 error = ENOPROTOOPT;
3134 break;
3135 }
3136 break;
3137
3138 case PRCO_GETOPT:
3139 switch (optname) {
3140 case ICMP6_FILTER:
3141 {
3142 if (inp->in6p_icmp6filt == NULL) {
3143 error = EINVAL;
3144 break;
3145 }
3146 size_t copylen = MIN(sizeof(struct icmp6_filter), optlen);
3147 error = sooptcopyout(sopt, __unsafe_forge_bidi_indexable(void*, inp->in6p_icmp6filt, optlen), copylen);
3148 break;
3149 }
3150
3151 default:
3152 error = ENOPROTOOPT;
3153 break;
3154 }
3155 break;
3156 }
3157
3158 return error;
3159 }
3160
3161 /*
3162 * ICMPv6 socket datagram option processing.
3163 */
3164 int
icmp6_dgram_ctloutput(struct socket * so,struct sockopt * sopt)3165 icmp6_dgram_ctloutput(struct socket *so, struct sockopt *sopt)
3166 {
3167 /*
3168 * For { SOCK_RAW, IPPROTO_ICMPV6 } the pr_ctloutput is
3169 * rip6_ctloutput() and not icmp6_ctloutput()
3170 */
3171 if (kauth_cred_issuser(so->so_cred)) {
3172 return rip6_ctloutput(so, sopt);
3173 }
3174
3175 /* Allow <SOL_SOCKET,SO_BINDTODEVICE> at this level */
3176 if (sopt->sopt_level == SOL_SOCKET) {
3177 if (sopt->sopt_name == SO_BINDTODEVICE) {
3178 return ip6_ctloutput(so, sopt);
3179 }
3180 return EINVAL;
3181 } else if (sopt->sopt_level == IPPROTO_ICMPV6) {
3182 switch (sopt->sopt_name) {
3183 case ICMP6_FILTER:
3184 return icmp6_ctloutput(so, sopt);
3185 default:
3186 return EPERM;
3187 }
3188 } else if (sopt->sopt_level != IPPROTO_IPV6) {
3189 return EINVAL;
3190 }
3191
3192 switch (sopt->sopt_name) {
3193 case IPV6_UNICAST_HOPS:
3194 case IPV6_CHECKSUM:
3195 case IPV6_V6ONLY:
3196 case IPV6_USE_MIN_MTU:
3197 case IPV6_RECVRTHDR:
3198 case IPV6_RECVPKTINFO:
3199 case IPV6_RECVHOPLIMIT:
3200 case IPV6_PATHMTU:
3201 case IPV6_PKTINFO:
3202 case IPV6_HOPLIMIT:
3203 case IPV6_HOPOPTS:
3204 case IPV6_DSTOPTS:
3205 case IPV6_MULTICAST_IF:
3206 case IPV6_MULTICAST_HOPS:
3207 case IPV6_MULTICAST_LOOP:
3208 case IPV6_JOIN_GROUP:
3209 case IPV6_LEAVE_GROUP:
3210 case IPV6_PORTRANGE:
3211 case IPV6_IPSEC_POLICY:
3212 case IPV6_RECVTCLASS:
3213 case IPV6_TCLASS:
3214 case IPV6_2292PKTOPTIONS:
3215 case IPV6_2292PKTINFO:
3216 case IPV6_2292HOPLIMIT:
3217 case IPV6_2292HOPOPTS:
3218 case IPV6_2292DSTOPTS:
3219 case IPV6_2292RTHDR:
3220 case IPV6_BOUND_IF:
3221 case IPV6_NO_IFT_CELLULAR:
3222 return ip6_ctloutput(so, sopt);
3223
3224 default:
3225 return EPERM;
3226 }
3227 }
3228
3229 __private_extern__ int
icmp6_dgram_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct proc * p)3230 icmp6_dgram_send(struct socket *so, int flags, struct mbuf *m,
3231 struct sockaddr *nam, struct mbuf *control, struct proc *p)
3232 {
3233 #pragma unused(flags, p)
3234 int error = 0;
3235 struct inpcb *__single inp = sotoinpcb(so);
3236 struct icmp6_hdr *__single icmp6;
3237
3238 if (inp == NULL
3239 #if NECP
3240 || (necp_socket_should_use_flow_divert(inp))
3241 #endif /* NECP */
3242 ) {
3243 error = (inp == NULL ? EINVAL : EPROTOTYPE);
3244 goto bad;
3245 }
3246
3247 if (kauth_cred_issuser(so->so_cred)) {
3248 return rip6_output(m, so, SIN6(nam), control, 0);
3249 }
3250
3251 /*
3252 * For an ICMPv6 packet, we should know its type and code
3253 */
3254 if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
3255 if (m->m_len < sizeof(struct icmp6_hdr) &&
3256 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
3257 error = ENOBUFS;
3258 goto bad;
3259 }
3260 icmp6 = mtod(m, struct icmp6_hdr *);
3261
3262 /*
3263 * Allow only to send echo request and node information request
3264 * See RFC 2463 for Echo Request Message format
3265 */
3266 if ((icmp6->icmp6_type == ICMP6_ECHO_REQUEST &&
3267 icmp6->icmp6_code == 0) ||
3268 (icmp6->icmp6_type == ICMP6_NI_QUERY &&
3269 (icmp6->icmp6_code == ICMP6_NI_SUBJ_IPV6 ||
3270 icmp6->icmp6_code == ICMP6_NI_SUBJ_FQDN))) {
3271 /* Good */
3272 ;
3273 } else {
3274 error = EPERM;
3275 goto bad;
3276 }
3277 }
3278
3279 so_update_tx_data_stats(so, 1, m->m_pkthdr.len);
3280
3281 return rip6_output(m, so, SIN6(nam), control, 0);
3282 bad:
3283 VERIFY(error != 0);
3284
3285 if (m != NULL) {
3286 m_freem(m);
3287 }
3288 if (control != NULL) {
3289 m_freem(control);
3290 }
3291
3292 return error;
3293 }
3294
3295 /* Like rip6_attach but without root privilege enforcement */
3296 __private_extern__ int
icmp6_dgram_attach(struct socket * so,int proto,struct proc * p)3297 icmp6_dgram_attach(struct socket *so, int proto, struct proc *p)
3298 {
3299 struct inpcb *__single inp;
3300 int error;
3301
3302 inp = sotoinpcb(so);
3303 if (inp) {
3304 panic("icmp6_dgram_attach");
3305 }
3306
3307 if (proto != IPPROTO_ICMPV6) {
3308 return EINVAL;
3309 }
3310
3311 error = soreserve(so, rip_sendspace, rip_recvspace);
3312 if (error) {
3313 return error;
3314 }
3315 error = in_pcballoc(so, &ripcbinfo, p);
3316 if (error) {
3317 return error;
3318 }
3319 inp = (struct inpcb *)so->so_pcb;
3320 inp->inp_vflag |= INP_IPV6;
3321 inp->in6p_ip6_nxt = IPPROTO_ICMPV6;
3322 inp->in6p_hops = -1; /* use kernel default */
3323 inp->in6p_cksum = -1;
3324 inp->in6p_icmp6filt = kalloc_type(struct icmp6_filter,
3325 Z_WAITOK | Z_NOFAIL);
3326 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
3327 return 0;
3328 }
3329
3330
3331 /*
3332 * Perform rate limit check.
3333 * Returns 0 if it is okay to send the icmp6 packet.
3334 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
3335 * limitation.
3336 *
3337 * XXX per-destination check necessary?
3338 */
3339 static int
icmp6_ratelimit(__unused const struct in6_addr * dst,const int type,__unused const int code)3340 icmp6_ratelimit(
3341 __unused const struct in6_addr *dst, /* not used at this moment */
3342 const int type,
3343 __unused const int code)
3344 {
3345 int ret;
3346
3347 ret = 0; /* okay to send */
3348
3349 /* PPS limit */
3350 if (type == ND_ROUTER_ADVERT) {
3351 if (!ppsratecheck(&icmp6rappslim_last, &icmp6rapps_count,
3352 icmp6rappslim)) {
3353 ret++;
3354 }
3355 } else if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
3356 icmp6errppslim)) {
3357 /*
3358 * We add some randomness here to still generate ICMPv6 error
3359 * post icmp6errppslim limit with a probability that goes down
3360 * with increased value of icmp6errpps_count.
3361 */
3362 if (icmp6errpps_count > 0 && icmp6errppslim > 0 &&
3363 icmp6errpps_count > icmp6errppslim &&
3364 (random() % (icmp6errpps_count - icmp6errppslim)) != 0) {
3365 /* The packet is subject to rate limit */
3366 ret++;
3367 }
3368 }
3369
3370 return ret;
3371 }
3372