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