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