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