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