icmp6.c revision 1.219
1/* $NetBSD: icmp6.c,v 1.219 2018/01/23 10:55:38 maxv Exp $ */ 2/* $KAME: icmp6.c,v 1.217 2001/06/20 15:03:29 jinmei Exp $ */ 3 4/* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33/* 34 * Copyright (c) 1982, 1986, 1988, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 62 */ 63 64#include <sys/cdefs.h> 65__KERNEL_RCSID(0, "$NetBSD: icmp6.c,v 1.219 2018/01/23 10:55:38 maxv Exp $"); 66 67#ifdef _KERNEL_OPT 68#include "opt_inet.h" 69#include "opt_ipsec.h" 70#endif 71 72#include <sys/param.h> 73#include <sys/systm.h> 74#include <sys/kmem.h> 75#include <sys/mbuf.h> 76#include <sys/protosw.h> 77#include <sys/socket.h> 78#include <sys/socketvar.h> 79#include <sys/time.h> 80#include <sys/kernel.h> 81#include <sys/syslog.h> 82#include <sys/domain.h> 83#include <sys/sysctl.h> 84 85#include <net/if.h> 86#include <net/route.h> 87#include <net/if_dl.h> 88#include <net/if_types.h> 89 90#include <netinet/in.h> 91#include <netinet/in_var.h> 92#include <netinet/ip6.h> 93#include <netinet/wqinput.h> 94#include <netinet6/ip6_var.h> 95#include <netinet6/ip6_private.h> 96#include <netinet/icmp6.h> 97#include <netinet6/icmp6_private.h> 98#include <netinet6/mld6_var.h> 99#include <netinet6/in6_pcb.h> 100#include <netinet6/nd6.h> 101#include <netinet6/in6_ifattach.h> 102#include <netinet6/ip6protosw.h> 103#include <netinet6/scope6_var.h> 104 105#ifdef IPSEC 106#include <netipsec/ipsec.h> 107#include <netipsec/ipsec_var.h> 108#include <netipsec/ipsec_private.h> 109#include <netipsec/ipsec6.h> 110#include <netipsec/key.h> 111#endif 112 113 114#include "faith.h" 115#if defined(NFAITH) && 0 < NFAITH 116#include <net/if_faith.h> 117#endif 118 119#include <net/net_osdep.h> 120 121extern struct domain inet6domain; 122 123percpu_t *icmp6stat_percpu; 124 125extern struct inpcbtable raw6cbtable; 126extern int icmp6errppslim; 127static int icmp6errpps_count = 0; 128static struct timeval icmp6errppslim_last; 129extern int icmp6_nodeinfo; 130 131/* 132 * List of callbacks to notify when Path MTU changes are made. 133 */ 134struct icmp6_mtudisc_callback { 135 LIST_ENTRY(icmp6_mtudisc_callback) mc_list; 136 void (*mc_func)(struct in6_addr *); 137}; 138 139LIST_HEAD(, icmp6_mtudisc_callback) icmp6_mtudisc_callbacks = 140 LIST_HEAD_INITIALIZER(&icmp6_mtudisc_callbacks); 141 142static struct rttimer_queue *icmp6_mtudisc_timeout_q = NULL; 143extern int pmtu_expire; 144 145/* XXX do these values make any sense? */ 146static int icmp6_mtudisc_hiwat = 1280; 147static int icmp6_mtudisc_lowat = 256; 148 149/* 150 * keep track of # of redirect routes. 151 */ 152static struct rttimer_queue *icmp6_redirect_timeout_q = NULL; 153 154/* XXX experimental, turned off */ 155static int icmp6_redirect_hiwat = -1; 156static int icmp6_redirect_lowat = -1; 157 158/* Protect mtudisc and redirect stuffs */ 159static kmutex_t icmp6_mtx __cacheline_aligned; 160 161static void icmp6_errcount(u_int, int, int); 162static int icmp6_rip6_input(struct mbuf **, int); 163static void icmp6_reflect(struct mbuf *, size_t); 164static int icmp6_ratelimit(const struct in6_addr *, const int, const int); 165static const char *icmp6_redirect_diag(char *, size_t, struct in6_addr *, 166 struct in6_addr *, struct in6_addr *); 167static void icmp6_redirect_input(struct mbuf *, int); 168static struct mbuf *ni6_input(struct mbuf *, int); 169static struct mbuf *ni6_nametodns(const char *, int, int); 170static int ni6_dnsmatch(const char *, int, const char *, int); 171static int ni6_addrs(struct icmp6_nodeinfo *, struct ifnet **, char *, 172 struct psref *); 173static int ni6_store_addrs(struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, 174 struct ifnet *, int); 175static int icmp6_notify_error(struct mbuf *, int, int, int); 176static struct rtentry *icmp6_mtudisc_clone(struct sockaddr *); 177static void icmp6_mtudisc_timeout(struct rtentry *, struct rttimer *); 178static void icmp6_redirect_timeout(struct rtentry *, struct rttimer *); 179static void sysctl_net_inet6_icmp6_setup(struct sysctllog **); 180 181/* workqueue-based pr_input */ 182static struct wqinput *icmp6_wqinput; 183static void _icmp6_input(struct mbuf *m, int off, int proto); 184 185void 186icmp6_init(void) 187{ 188 189 sysctl_net_inet6_icmp6_setup(NULL); 190 mld_init(); 191 192 mutex_init(&icmp6_mtx, MUTEX_DEFAULT, IPL_NONE); 193 mutex_enter(&icmp6_mtx); 194 icmp6_mtudisc_timeout_q = rt_timer_queue_create(pmtu_expire); 195 icmp6_redirect_timeout_q = rt_timer_queue_create(icmp6_redirtimeout); 196 mutex_exit(&icmp6_mtx); 197 198 icmp6stat_percpu = percpu_alloc(sizeof(uint64_t) * ICMP6_NSTATS); 199 200 icmp6_wqinput = wqinput_create("icmp6", _icmp6_input); 201} 202 203static void 204icmp6_errcount(u_int base, int type, int code) 205{ 206 switch (type) { 207 case ICMP6_DST_UNREACH: 208 switch (code) { 209 case ICMP6_DST_UNREACH_NOROUTE: 210 ICMP6_STATINC(base + ICMP6_ERRSTAT_DST_UNREACH_NOROUTE); 211 return; 212 case ICMP6_DST_UNREACH_ADMIN: 213 ICMP6_STATINC(base + ICMP6_ERRSTAT_DST_UNREACH_ADMIN); 214 return; 215 case ICMP6_DST_UNREACH_BEYONDSCOPE: 216 ICMP6_STATINC(base + 217 ICMP6_ERRSTAT_DST_UNREACH_BEYONDSCOPE); 218 return; 219 case ICMP6_DST_UNREACH_ADDR: 220 ICMP6_STATINC(base + ICMP6_ERRSTAT_DST_UNREACH_ADDR); 221 return; 222 case ICMP6_DST_UNREACH_NOPORT: 223 ICMP6_STATINC(base + ICMP6_ERRSTAT_DST_UNREACH_NOPORT); 224 return; 225 } 226 break; 227 case ICMP6_PACKET_TOO_BIG: 228 ICMP6_STATINC(base + ICMP6_ERRSTAT_PACKET_TOO_BIG); 229 return; 230 case ICMP6_TIME_EXCEEDED: 231 switch (code) { 232 case ICMP6_TIME_EXCEED_TRANSIT: 233 ICMP6_STATINC(base + ICMP6_ERRSTAT_TIME_EXCEED_TRANSIT); 234 return; 235 case ICMP6_TIME_EXCEED_REASSEMBLY: 236 ICMP6_STATINC(base + 237 ICMP6_ERRSTAT_TIME_EXCEED_REASSEMBLY); 238 return; 239 } 240 break; 241 case ICMP6_PARAM_PROB: 242 switch (code) { 243 case ICMP6_PARAMPROB_HEADER: 244 ICMP6_STATINC(base + ICMP6_ERRSTAT_PARAMPROB_HEADER); 245 return; 246 case ICMP6_PARAMPROB_NEXTHEADER: 247 ICMP6_STATINC(base + 248 ICMP6_ERRSTAT_PARAMPROB_NEXTHEADER); 249 return; 250 case ICMP6_PARAMPROB_OPTION: 251 ICMP6_STATINC(base + ICMP6_ERRSTAT_PARAMPROB_OPTION); 252 return; 253 } 254 break; 255 case ND_REDIRECT: 256 ICMP6_STATINC(base + ICMP6_ERRSTAT_REDIRECT); 257 return; 258 } 259 ICMP6_STATINC(base + ICMP6_ERRSTAT_UNKNOWN); 260} 261 262/* 263 * Register a Path MTU Discovery callback. 264 */ 265void 266icmp6_mtudisc_callback_register(void (*func)(struct in6_addr *)) 267{ 268 struct icmp6_mtudisc_callback *mc, *new; 269 270 new = kmem_alloc(sizeof(*mc), KM_SLEEP); 271 272 mutex_enter(&icmp6_mtx); 273 for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL; 274 mc = LIST_NEXT(mc, mc_list)) { 275 if (mc->mc_func == func) { 276 mutex_exit(&icmp6_mtx); 277 kmem_free(new, sizeof(*mc)); 278 return; 279 } 280 } 281 282 new->mc_func = func; 283 LIST_INSERT_HEAD(&icmp6_mtudisc_callbacks, new, mc_list); 284 mutex_exit(&icmp6_mtx); 285} 286 287/* 288 * A wrapper function for icmp6_error() necessary when the erroneous packet 289 * may not contain enough scope zone information. 290 */ 291void 292icmp6_error2(struct mbuf *m, int type, int code, int param, 293 struct ifnet *ifp) 294{ 295 struct ip6_hdr *ip6; 296 297 KASSERT(ifp != NULL); 298 299 if (m->m_len < sizeof(struct ip6_hdr)) { 300 m = m_pullup(m, sizeof(struct ip6_hdr)); 301 if (m == NULL) 302 return; 303 } 304 305 ip6 = mtod(m, struct ip6_hdr *); 306 307 if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0) 308 goto out; 309 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) 310 goto out; 311 312 icmp6_error(m, type, code, param); 313 return; 314 315out: 316 m_freem(m); 317} 318 319/* 320 * Generate an error packet of type error in response to bad IP6 packet. 321 */ 322void 323icmp6_error(struct mbuf *m, int type, int code, int param) 324{ 325 struct ip6_hdr *oip6, *nip6; 326 struct icmp6_hdr *icmp6; 327 u_int preplen; 328 int off; 329 int nxt; 330 331 ICMP6_STATINC(ICMP6_STAT_ERROR); 332 333 /* count per-type-code statistics */ 334 icmp6_errcount(ICMP6_STAT_OUTERRHIST, type, code); 335 336 if (m->m_flags & M_DECRYPTED) { 337 ICMP6_STATINC(ICMP6_STAT_CANTERROR); 338 goto freeit; 339 } 340 341 if (M_UNWRITABLE(m, sizeof(struct ip6_hdr)) && 342 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) 343 return; 344 oip6 = mtod(m, struct ip6_hdr *); 345 346 /* 347 * If the destination address of the erroneous packet is a multicast 348 * address, or the packet was sent using link-layer multicast, 349 * we should basically suppress sending an error (RFC 2463, Section 350 * 2.4). 351 * We have two exceptions (the item e.2 in that section): 352 * - the Packet Too Big message can be sent for path MTU discovery. 353 * - the Parameter Problem Message that can be allowed an icmp6 error 354 * in the option type field. This check has been done in 355 * ip6_unknown_opt(), so we can just check the type and code. 356 */ 357 if ((m->m_flags & (M_BCAST|M_MCAST) || 358 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && 359 (type != ICMP6_PACKET_TOO_BIG && 360 (type != ICMP6_PARAM_PROB || 361 code != ICMP6_PARAMPROB_OPTION))) 362 goto freeit; 363 364 /* 365 * RFC 2463, 2.4 (e.5): source address check. 366 * XXX: the case of anycast source? 367 */ 368 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || 369 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) 370 goto freeit; 371 372 /* 373 * If we are about to send ICMPv6 against ICMPv6 error/redirect, 374 * don't do it. 375 */ 376 nxt = -1; 377 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 378 if (off >= 0 && nxt == IPPROTO_ICMPV6) { 379 struct icmp6_hdr *icp; 380 381 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off, 382 sizeof(*icp)); 383 if (icp == NULL) { 384 ICMP6_STATINC(ICMP6_STAT_TOOSHORT); 385 return; 386 } 387 if (icp->icmp6_type < ICMP6_ECHO_REQUEST || 388 icp->icmp6_type == ND_REDIRECT) { 389 /* 390 * ICMPv6 error 391 * Special case: for redirect (which is 392 * informational) we must not send icmp6 error. 393 */ 394 ICMP6_STATINC(ICMP6_STAT_CANTERROR); 395 goto freeit; 396 } else { 397 /* ICMPv6 informational - send the error */ 398 } 399 } else { 400 /* non-ICMPv6 - send the error */ 401 } 402 403 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */ 404 405 /* Finally, do rate limitation check. */ 406 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) { 407 ICMP6_STATINC(ICMP6_STAT_TOOFREQ); 408 goto freeit; 409 } 410 411 /* 412 * OK, ICMP6 can be generated. 413 */ 414 415 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) 416 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len); 417 418 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 419 M_PREPEND(m, preplen, M_DONTWAIT); 420 if (m && M_UNWRITABLE(m, preplen)) 421 m = m_pullup(m, preplen); 422 if (m == NULL) { 423 nd6log(LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__); 424 return; 425 } 426 427 nip6 = mtod(m, struct ip6_hdr *); 428 nip6->ip6_src = oip6->ip6_src; 429 nip6->ip6_dst = oip6->ip6_dst; 430 431 in6_clearscope(&oip6->ip6_src); 432 in6_clearscope(&oip6->ip6_dst); 433 434 icmp6 = (struct icmp6_hdr *)(nip6 + 1); 435 icmp6->icmp6_type = type; 436 icmp6->icmp6_code = code; 437 icmp6->icmp6_pptr = htonl((u_int32_t)param); 438 439 /* 440 * icmp6_reflect() is designed to be in the input path. 441 * icmp6_error() can be called from both input and output path, 442 * and if we are in output path rcvif could contain bogus value. 443 * clear m->m_pkthdr.rcvif for safety, we should have enough scope 444 * information in ip header (nip6). 445 */ 446 m_reset_rcvif(m); 447 448 ICMP6_STATINC(ICMP6_STAT_OUTHIST + type); 449 450 /* header order: IPv6 - ICMPv6 */ 451 icmp6_reflect(m, sizeof(struct ip6_hdr)); 452 453 return; 454 455freeit: 456 /* 457 * If we can't tell whether or not we can generate ICMP6, free it. 458 */ 459 m_freem(m); 460} 461 462/* 463 * Process a received ICMP6 message. 464 */ 465static void 466_icmp6_input(struct mbuf *m, int off, int proto) 467{ 468 struct mbuf *n; 469 struct ip6_hdr *ip6, *nip6; 470 struct icmp6_hdr *icmp6, *nicmp6; 471 int icmp6len = m->m_pkthdr.len - off; 472 int code, sum; 473 struct ifnet *rcvif; 474 struct psref psref; 475 char ip6buf[INET6_ADDRSTRLEN], ip6buf2[INET6_ADDRSTRLEN]; 476 477 rcvif = m_get_rcvif_psref(m, &psref); 478 if (__predict_false(rcvif == NULL)) 479 goto freeit; 480 481#define ICMP6_MAXLEN (sizeof(*nip6) + sizeof(*nicmp6) + 4) 482 KASSERT(ICMP6_MAXLEN < MCLBYTES); 483 icmp6_ifstat_inc(rcvif, ifs6_in_msg); 484 485 /* 486 * Locate icmp6 structure in mbuf, and check 487 * that not corrupted and of at least minimum length 488 */ 489 490 if (icmp6len < sizeof(struct icmp6_hdr)) { 491 ICMP6_STATINC(ICMP6_STAT_TOOSHORT); 492 icmp6_ifstat_inc(rcvif, ifs6_in_error); 493 goto freeit; 494 } 495 496 if (m->m_len < sizeof(struct ip6_hdr)) { 497 m = m_pullup(m, sizeof(struct ip6_hdr)); 498 if (m == NULL) { 499 ICMP6_STATINC(ICMP6_STAT_TOOSHORT); 500 icmp6_ifstat_inc(rcvif, ifs6_in_error); 501 goto freeit; 502 } 503 } 504 505 ip6 = mtod(m, struct ip6_hdr *); 506 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 507 if (icmp6 == NULL) { 508 ICMP6_STATINC(ICMP6_STAT_TOOSHORT); 509 icmp6_ifstat_inc(rcvif, ifs6_in_error); 510 goto freeit; 511 } 512 513 /* 514 * Enforce alignment requirements that are violated in 515 * some cases, see kern/50766 for details. 516 */ 517 if (IP6_HDR_ALIGNED_P(icmp6) == 0) { 518 m = m_copyup(m, off + sizeof(struct icmp6_hdr), 0); 519 if (m == NULL) { 520 ICMP6_STATINC(ICMP6_STAT_TOOSHORT); 521 icmp6_ifstat_inc(rcvif, ifs6_in_error); 522 goto freeit; 523 } 524 ip6 = mtod(m, struct ip6_hdr *); 525 icmp6 = (struct icmp6_hdr *)(mtod(m, char *) + off); 526 } 527 KASSERT(IP6_HDR_ALIGNED_P(icmp6)); 528 529 /* 530 * calculate the checksum 531 */ 532 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) { 533 nd6log(LOG_ERR, "ICMP6 checksum error(%d|%x) %s\n", 534 icmp6->icmp6_type, sum, IN6_PRINT(ip6buf, &ip6->ip6_src)); 535 ICMP6_STATINC(ICMP6_STAT_CHECKSUM); 536 icmp6_ifstat_inc(rcvif, ifs6_in_error); 537 goto freeit; 538 } 539 540#if defined(NFAITH) && 0 < NFAITH 541 if (faithprefix(&ip6->ip6_dst)) { 542 /* 543 * Deliver very specific ICMP6 type only. 544 * This is important to deliver TOOBIG. Otherwise PMTUD 545 * will not work. 546 */ 547 switch (icmp6->icmp6_type) { 548 case ICMP6_DST_UNREACH: 549 case ICMP6_PACKET_TOO_BIG: 550 case ICMP6_TIME_EXCEEDED: 551 break; 552 default: 553 goto freeit; 554 } 555 } 556#endif 557 558 code = icmp6->icmp6_code; 559 ICMP6_STATINC(ICMP6_STAT_INHIST + icmp6->icmp6_type); 560 561 switch (icmp6->icmp6_type) { 562 case ICMP6_DST_UNREACH: 563 icmp6_ifstat_inc(rcvif, ifs6_in_dstunreach); 564 switch (code) { 565 case ICMP6_DST_UNREACH_NOROUTE: 566 code = PRC_UNREACH_NET; 567 break; 568 case ICMP6_DST_UNREACH_ADMIN: 569 icmp6_ifstat_inc(rcvif, ifs6_in_adminprohib); 570 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */ 571 break; 572 case ICMP6_DST_UNREACH_ADDR: 573 code = PRC_HOSTDEAD; 574 break; 575#ifdef COMPAT_RFC1885 576 case ICMP6_DST_UNREACH_NOTNEIGHBOR: 577 code = PRC_UNREACH_SRCFAIL; 578 break; 579#else 580 case ICMP6_DST_UNREACH_BEYONDSCOPE: 581 /* I mean "source address was incorrect." */ 582 code = PRC_UNREACH_NET; 583 break; 584#endif 585 case ICMP6_DST_UNREACH_NOPORT: 586 code = PRC_UNREACH_PORT; 587 break; 588 default: 589 goto badcode; 590 } 591 goto deliver; 592 593 case ICMP6_PACKET_TOO_BIG: 594 icmp6_ifstat_inc(rcvif, ifs6_in_pkttoobig); 595 596 /* 597 * MTU is checked in icmp6_mtudisc. 598 */ 599 code = PRC_MSGSIZE; 600 601 /* 602 * Updating the path MTU will be done after examining 603 * intermediate extension headers. 604 */ 605 goto deliver; 606 607 case ICMP6_TIME_EXCEEDED: 608 icmp6_ifstat_inc(rcvif, ifs6_in_timeexceed); 609 switch (code) { 610 case ICMP6_TIME_EXCEED_TRANSIT: 611 code = PRC_TIMXCEED_INTRANS; 612 break; 613 case ICMP6_TIME_EXCEED_REASSEMBLY: 614 code = PRC_TIMXCEED_REASS; 615 break; 616 default: 617 goto badcode; 618 } 619 goto deliver; 620 621 case ICMP6_PARAM_PROB: 622 icmp6_ifstat_inc(rcvif, ifs6_in_paramprob); 623 switch (code) { 624 case ICMP6_PARAMPROB_NEXTHEADER: 625 code = PRC_UNREACH_PROTOCOL; 626 break; 627 case ICMP6_PARAMPROB_HEADER: 628 case ICMP6_PARAMPROB_OPTION: 629 code = PRC_PARAMPROB; 630 break; 631 default: 632 goto badcode; 633 } 634 goto deliver; 635 636 case ICMP6_ECHO_REQUEST: 637 icmp6_ifstat_inc(rcvif, ifs6_in_echo); 638 if (code != 0) 639 goto badcode; 640 /* 641 * Copy mbuf to send to two data paths: userland socket(s), 642 * and to the querier (echo reply). 643 * m: a copy for socket, n: a copy for querier 644 * 645 * If the first mbuf is shared, or the first mbuf is too short, 646 * copy the first part of the data into a fresh mbuf. 647 * Otherwise, we will wrongly overwrite both copies. 648 */ 649 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 650 /* Give up local */ 651 n = m; 652 m = NULL; 653 } else if (M_READONLY(n) || 654 n->m_len < off + sizeof(struct icmp6_hdr)) { 655 struct mbuf *n0 = n; 656 657 /* 658 * Prepare an internal mbuf. m_pullup() doesn't 659 * always copy the length we specified. 660 */ 661 if ((n = m_dup(n0, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 662 /* Give up local */ 663 n = m; 664 m = NULL; 665 } 666 m_freem(n0); 667 } 668 IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off, 669 sizeof(*nicmp6)); 670 if (nicmp6 == NULL) 671 goto freeit; 672 nicmp6->icmp6_type = ICMP6_ECHO_REPLY; 673 nicmp6->icmp6_code = 0; 674 if (n) { 675 uint64_t *icmp6s = ICMP6_STAT_GETREF(); 676 icmp6s[ICMP6_STAT_REFLECT]++; 677 icmp6s[ICMP6_STAT_OUTHIST + ICMP6_ECHO_REPLY]++; 678 ICMP6_STAT_PUTREF(); 679 icmp6_reflect(n, off); 680 } 681 if (!m) 682 goto freeit; 683 break; 684 685 case ICMP6_ECHO_REPLY: 686 icmp6_ifstat_inc(rcvif, ifs6_in_echoreply); 687 if (code != 0) 688 goto badcode; 689 break; 690 691 case MLD_LISTENER_QUERY: 692 case MLD_LISTENER_REPORT: 693 if (icmp6len < sizeof(struct mld_hdr)) 694 goto badlen; 695 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */ 696 icmp6_ifstat_inc(rcvif, ifs6_in_mldquery); 697 else 698 icmp6_ifstat_inc(rcvif, ifs6_in_mldreport); 699 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 700 /* give up local */ 701 mld_input(m, off); 702 m = NULL; 703 goto freeit; 704 } 705 mld_input(n, off); 706 /* m stays. */ 707 break; 708 709 case MLD_LISTENER_DONE: 710 icmp6_ifstat_inc(rcvif, ifs6_in_mlddone); 711 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */ 712 goto badlen; 713 break; /* nothing to be done in kernel */ 714 715 case MLD_MTRACE_RESP: 716 case MLD_MTRACE: 717 /* XXX: these two are experimental. not officially defined. */ 718 /* XXX: per-interface statistics? */ 719 break; /* just pass it to applications */ 720 721 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */ 722 { 723 enum { WRU, FQDN } mode; 724 725 if (!icmp6_nodeinfo) 726 break; 727 728 if (icmp6len == sizeof(struct icmp6_hdr) + 4) 729 mode = WRU; 730 else if (icmp6len >= sizeof(struct icmp6_nodeinfo)) 731 mode = FQDN; 732 else 733 goto badlen; 734 735 if (mode == FQDN) { 736 n = m_copym(m, 0, M_COPYALL, M_DONTWAIT); 737 if (n) 738 n = ni6_input(n, off); 739 } else { 740 u_char *p; 741 int maxhlen; 742 743 if ((icmp6_nodeinfo & 5) != 5) 744 break; 745 746 if (code != 0) 747 goto badcode; 748 MGETHDR(n, M_DONTWAIT, m->m_type); 749 if (n && ICMP6_MAXLEN > MHLEN) { 750 MCLGET(n, M_DONTWAIT); 751 if ((n->m_flags & M_EXT) == 0) { 752 m_free(n); 753 n = NULL; 754 } 755 } 756 if (n == NULL) { 757 /* Give up remote */ 758 break; 759 } 760 m_reset_rcvif(n); 761 n->m_len = 0; 762 maxhlen = M_TRAILINGSPACE(n) - ICMP6_MAXLEN; 763 if (maxhlen < 0) { 764 m_free(n); 765 break; 766 } 767 if (maxhlen > hostnamelen) 768 maxhlen = hostnamelen; 769 /* 770 * Copy IPv6 and ICMPv6 only. 771 */ 772 nip6 = mtod(n, struct ip6_hdr *); 773 memcpy(nip6, ip6, sizeof(struct ip6_hdr)); 774 nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 775 memcpy(nicmp6, icmp6, sizeof(struct icmp6_hdr)); 776 777 p = (u_char *)(nicmp6 + 1); 778 memset(p, 0, 4); 779 memcpy(p + 4, hostname, maxhlen); /* meaningless TTL */ 780 781 M_COPY_PKTHDR(n, m); /* just for rcvif */ 782 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 783 sizeof(struct icmp6_hdr) + 4 + maxhlen; 784 nicmp6->icmp6_type = ICMP6_WRUREPLY; 785 nicmp6->icmp6_code = 0; 786 } 787 if (n) { 788 uint64_t *icmp6s = ICMP6_STAT_GETREF(); 789 icmp6s[ICMP6_STAT_REFLECT]++; 790 icmp6s[ICMP6_STAT_OUTHIST + ICMP6_WRUREPLY]++; 791 ICMP6_STAT_PUTREF(); 792 icmp6_reflect(n, sizeof(struct ip6_hdr)); 793 } 794 break; 795 } 796 797 case ICMP6_WRUREPLY: 798 if (code != 0) 799 goto badcode; 800 break; 801 802 case ND_ROUTER_SOLICIT: 803 icmp6_ifstat_inc(rcvif, ifs6_in_routersolicit); 804 if (code != 0) 805 goto badcode; 806 if (icmp6len < sizeof(struct nd_router_solicit)) 807 goto badlen; 808 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 809 /* give up local */ 810 nd6_rs_input(m, off, icmp6len); 811 m = NULL; 812 goto freeit; 813 } 814 nd6_rs_input(n, off, icmp6len); 815 /* m stays. */ 816 break; 817 818 case ND_ROUTER_ADVERT: 819 icmp6_ifstat_inc(rcvif, ifs6_in_routeradvert); 820 if (code != 0) 821 goto badcode; 822 if (icmp6len < sizeof(struct nd_router_advert)) 823 goto badlen; 824 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 825 /* give up local */ 826 nd6_ra_input(m, off, icmp6len); 827 m = NULL; 828 goto freeit; 829 } 830 nd6_ra_input(n, off, icmp6len); 831 /* m stays. */ 832 break; 833 834 case ND_NEIGHBOR_SOLICIT: 835 icmp6_ifstat_inc(rcvif, ifs6_in_neighborsolicit); 836 if (code != 0) 837 goto badcode; 838 if (icmp6len < sizeof(struct nd_neighbor_solicit)) 839 goto badlen; 840 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 841 /* give up local */ 842 nd6_ns_input(m, off, icmp6len); 843 m = NULL; 844 goto freeit; 845 } 846 nd6_ns_input(n, off, icmp6len); 847 /* m stays. */ 848 break; 849 850 case ND_NEIGHBOR_ADVERT: 851 icmp6_ifstat_inc(rcvif, ifs6_in_neighboradvert); 852 if (code != 0) 853 goto badcode; 854 if (icmp6len < sizeof(struct nd_neighbor_advert)) 855 goto badlen; 856 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 857 /* give up local */ 858 nd6_na_input(m, off, icmp6len); 859 m = NULL; 860 goto freeit; 861 } 862 nd6_na_input(n, off, icmp6len); 863 /* m stays. */ 864 break; 865 866 case ND_REDIRECT: 867 icmp6_ifstat_inc(rcvif, ifs6_in_redirect); 868 if (code != 0) 869 goto badcode; 870 if (icmp6len < sizeof(struct nd_redirect)) 871 goto badlen; 872 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 873 /* give up local */ 874 icmp6_redirect_input(m, off); 875 m = NULL; 876 goto freeit; 877 } 878 icmp6_redirect_input(n, off); 879 /* m stays. */ 880 break; 881 882 case ICMP6_ROUTER_RENUMBERING: 883 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND && 884 code != ICMP6_ROUTER_RENUMBERING_RESULT) 885 goto badcode; 886 if (icmp6len < sizeof(struct icmp6_router_renum)) 887 goto badlen; 888 break; 889 890 default: 891 nd6log(LOG_DEBUG, 892 "unknown type %d(src=%s, dst=%s, ifid=%d)\n", 893 icmp6->icmp6_type, 894 IN6_PRINT(ip6buf, &ip6->ip6_src), 895 IN6_PRINT(ip6buf2, &ip6->ip6_dst), 896 rcvif ? rcvif->if_index : 0); 897 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) { 898 /* ICMPv6 error: MUST deliver it by spec... */ 899 code = PRC_NCMDS; 900 /* deliver */ 901 } else { 902 /* ICMPv6 informational: MUST not deliver */ 903 break; 904 } 905 deliver: 906 if (icmp6_notify_error(m, off, icmp6len, code)) { 907 /* In this case, m should've been freed. */ 908 m_put_rcvif_psref(rcvif, &psref); 909 return; 910 } 911 break; 912 913 badcode: 914 ICMP6_STATINC(ICMP6_STAT_BADCODE); 915 break; 916 917 badlen: 918 ICMP6_STATINC(ICMP6_STAT_BADLEN); 919 break; 920 } 921 m_put_rcvif_psref(rcvif, &psref); 922 923 /* deliver the packet to appropriate sockets */ 924 icmp6_rip6_input(&m, off); 925 926 return; 927 928freeit: 929 m_put_rcvif_psref(rcvif, &psref); 930 m_freem(m); 931 return; 932} 933 934int 935icmp6_input(struct mbuf **mp, int *offp, int proto) 936{ 937 938 wqinput_input(icmp6_wqinput, *mp, *offp, proto); 939 940 return IPPROTO_DONE; 941} 942 943static int 944icmp6_notify_error(struct mbuf *m, int off, int icmp6len, int code) 945{ 946 struct icmp6_hdr *icmp6; 947 struct ip6_hdr *eip6; 948 u_int32_t notifymtu; 949 struct sockaddr_in6 icmp6src, icmp6dst; 950 951 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) { 952 ICMP6_STATINC(ICMP6_STAT_TOOSHORT); 953 goto freeit; 954 } 955 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 956 sizeof(*icmp6) + sizeof(struct ip6_hdr)); 957 if (icmp6 == NULL) { 958 ICMP6_STATINC(ICMP6_STAT_TOOSHORT); 959 return (-1); 960 } 961 eip6 = (struct ip6_hdr *)(icmp6 + 1); 962 963 /* Detect the upper level protocol */ 964 { 965 void (*ctlfunc)(int, struct sockaddr *, void *); 966 u_int8_t nxt = eip6->ip6_nxt; 967 int eoff = off + sizeof(struct icmp6_hdr) + 968 sizeof(struct ip6_hdr); 969 struct ip6ctlparam ip6cp; 970 struct in6_addr *finaldst = NULL; 971 int icmp6type = icmp6->icmp6_type; 972 struct ip6_frag *fh; 973 struct ip6_rthdr *rth; 974 struct ip6_rthdr0 *rth0; 975 int rthlen; 976 struct ifnet *rcvif; 977 int s; 978 979 while (1) { /* XXX: should avoid infinite loop explicitly? */ 980 struct ip6_ext *eh; 981 982 switch (nxt) { 983 case IPPROTO_HOPOPTS: 984 case IPPROTO_DSTOPTS: 985 case IPPROTO_AH: 986 IP6_EXTHDR_GET(eh, struct ip6_ext *, m, 987 eoff, sizeof(*eh)); 988 if (eh == NULL) { 989 ICMP6_STATINC(ICMP6_STAT_TOOSHORT); 990 return (-1); 991 } 992 993 if (nxt == IPPROTO_AH) 994 eoff += (eh->ip6e_len + 2) << 2; 995 else 996 eoff += (eh->ip6e_len + 1) << 3; 997 nxt = eh->ip6e_nxt; 998 break; 999 case IPPROTO_ROUTING: 1000 /* 1001 * When the erroneous packet contains a 1002 * routing header, we should examine the 1003 * header to determine the final destination. 1004 * Otherwise, we can't properly update 1005 * information that depends on the final 1006 * destination (e.g. path MTU). 1007 */ 1008 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m, 1009 eoff, sizeof(*rth)); 1010 if (rth == NULL) { 1011 ICMP6_STATINC(ICMP6_STAT_TOOSHORT); 1012 return (-1); 1013 } 1014 rthlen = (rth->ip6r_len + 1) << 3; 1015 /* 1016 * XXX: currently there is no 1017 * officially defined type other 1018 * than type-0. 1019 * Note that if the segment left field 1020 * is 0, all intermediate hops must 1021 * have been passed. 1022 */ 1023 if (rth->ip6r_segleft && 1024 rth->ip6r_type == IPV6_RTHDR_TYPE_0) { 1025 int hops; 1026 1027 IP6_EXTHDR_GET(rth0, 1028 struct ip6_rthdr0 *, m, 1029 eoff, rthlen); 1030 if (rth0 == NULL) { 1031 ICMP6_STATINC(ICMP6_STAT_TOOSHORT); 1032 return (-1); 1033 } 1034 /* just ignore a bogus header */ 1035 if ((rth0->ip6r0_len % 2) == 0 && 1036 (hops = rth0->ip6r0_len/2)) 1037 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1); 1038 } 1039 eoff += rthlen; 1040 nxt = rth->ip6r_nxt; 1041 break; 1042 case IPPROTO_FRAGMENT: 1043 IP6_EXTHDR_GET(fh, struct ip6_frag *, m, 1044 eoff, sizeof(*fh)); 1045 if (fh == NULL) { 1046 ICMP6_STATINC(ICMP6_STAT_TOOSHORT); 1047 return (-1); 1048 } 1049 /* 1050 * Data after a fragment header is meaningless 1051 * unless it is the first fragment, but 1052 * we'll go to the notify label for path MTU 1053 * discovery. 1054 */ 1055 if (fh->ip6f_offlg & IP6F_OFF_MASK) 1056 goto notify; 1057 1058 eoff += sizeof(struct ip6_frag); 1059 nxt = fh->ip6f_nxt; 1060 break; 1061 default: 1062 /* 1063 * This case includes ESP and the No Next 1064 * Header. In such cases going to the notify 1065 * label does not have any meaning 1066 * (i.e. ctlfunc will be NULL), but we go 1067 * anyway since we might have to update 1068 * path MTU information. 1069 */ 1070 goto notify; 1071 } 1072 } 1073 notify: 1074 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, 1075 sizeof(*icmp6) + sizeof(struct ip6_hdr)); 1076 if (icmp6 == NULL) { 1077 ICMP6_STATINC(ICMP6_STAT_TOOSHORT); 1078 return (-1); 1079 } 1080 1081 /* 1082 * retrieve parameters from the inner IPv6 header, and convert 1083 * them into sockaddr structures. 1084 * XXX: there is no guarantee that the source or destination 1085 * addresses of the inner packet are in the same scope zone as 1086 * the addresses of the icmp packet. But there is no other 1087 * way to determine the zone. 1088 */ 1089 eip6 = (struct ip6_hdr *)(icmp6 + 1); 1090 1091 rcvif = m_get_rcvif(m, &s); 1092 if (__predict_false(rcvif == NULL)) 1093 goto freeit; 1094 sockaddr_in6_init(&icmp6dst, 1095 (finaldst == NULL) ? &eip6->ip6_dst : finaldst, 0, 0, 0); 1096 if (in6_setscope(&icmp6dst.sin6_addr, rcvif, NULL)) { 1097 m_put_rcvif(rcvif, &s); 1098 goto freeit; 1099 } 1100 sockaddr_in6_init(&icmp6src, &eip6->ip6_src, 0, 0, 0); 1101 if (in6_setscope(&icmp6src.sin6_addr, rcvif, NULL)) { 1102 m_put_rcvif(rcvif, &s); 1103 goto freeit; 1104 } 1105 m_put_rcvif(rcvif, &s); 1106 1107 icmp6src.sin6_flowinfo = 1108 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); 1109 1110 if (finaldst == NULL) 1111 finaldst = &eip6->ip6_dst; 1112 ip6cp.ip6c_m = m; 1113 ip6cp.ip6c_icmp6 = icmp6; 1114 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1); 1115 ip6cp.ip6c_off = eoff; 1116 ip6cp.ip6c_finaldst = finaldst; 1117 ip6cp.ip6c_src = &icmp6src; 1118 ip6cp.ip6c_nxt = nxt; 1119 1120 if (icmp6type == ICMP6_PACKET_TOO_BIG) { 1121 notifymtu = ntohl(icmp6->icmp6_mtu); 1122 ip6cp.ip6c_cmdarg = (void *)¬ifymtu; 1123 } 1124 1125 ctlfunc = (void (*)(int, struct sockaddr *, void *)) 1126 (inet6sw[ip6_protox[nxt]].pr_ctlinput); 1127 if (ctlfunc) { 1128 (void)(*ctlfunc)(code, sin6tosa(&icmp6dst), &ip6cp); 1129 } 1130 } 1131 return (0); 1132 1133freeit: 1134 m_freem(m); 1135 return (-1); 1136} 1137 1138void 1139icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated) 1140{ 1141 unsigned long rtcount; 1142 struct icmp6_mtudisc_callback *mc; 1143 struct in6_addr *dst = ip6cp->ip6c_finaldst; 1144 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6; 1145 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */ 1146 u_int mtu = ntohl(icmp6->icmp6_mtu); 1147 struct rtentry *rt = NULL; 1148 struct sockaddr_in6 sin6; 1149 struct ifnet *rcvif; 1150 int s; 1151 1152 /* 1153 * The MTU should not be less than the minimal IPv6 MTU except for the 1154 * hack in ip6_output/ip6_setpmtu where we always include a frag header. 1155 * In that one case, the MTU might be less than 1280. 1156 */ 1157 if (__predict_false(mtu < IPV6_MMTU - sizeof(struct ip6_frag))) { 1158 /* is the mtu even sane? */ 1159 if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8) 1160 return; 1161 if (!validated) 1162 return; 1163 mtu = IPV6_MMTU - sizeof(struct ip6_frag); 1164 } 1165 1166 /* 1167 * allow non-validated cases if memory is plenty, to make traffic 1168 * from non-connected pcb happy. 1169 */ 1170 mutex_enter(&icmp6_mtx); 1171 rtcount = rt_timer_count(icmp6_mtudisc_timeout_q); 1172 if (validated) { 1173 if (0 <= icmp6_mtudisc_hiwat && rtcount > icmp6_mtudisc_hiwat) { 1174 mutex_exit(&icmp6_mtx); 1175 return; 1176 } else if (0 <= icmp6_mtudisc_lowat && 1177 rtcount > icmp6_mtudisc_lowat) { 1178 /* 1179 * XXX nuke a victim, install the new one. 1180 */ 1181 } 1182 } else { 1183 if (0 <= icmp6_mtudisc_lowat && rtcount > icmp6_mtudisc_lowat) { 1184 mutex_exit(&icmp6_mtx); 1185 return; 1186 } 1187 } 1188 mutex_exit(&icmp6_mtx); 1189 1190 memset(&sin6, 0, sizeof(sin6)); 1191 sin6.sin6_family = PF_INET6; 1192 sin6.sin6_len = sizeof(struct sockaddr_in6); 1193 sin6.sin6_addr = *dst; 1194 rcvif = m_get_rcvif(m, &s); 1195 if (__predict_false(rcvif == NULL)) 1196 return; 1197 if (in6_setscope(&sin6.sin6_addr, rcvif, NULL)) { 1198 m_put_rcvif(rcvif, &s); 1199 return; 1200 } 1201 m_put_rcvif(rcvif, &s); 1202 1203 rt = icmp6_mtudisc_clone(sin6tosa(&sin6)); 1204 1205 if (rt && (rt->rt_flags & RTF_HOST) && 1206 !(rt->rt_rmx.rmx_locks & RTV_MTU) && 1207 (rt->rt_rmx.rmx_mtu > mtu || rt->rt_rmx.rmx_mtu == 0)) { 1208 if (mtu < IN6_LINKMTU(rt->rt_ifp)) { 1209 ICMP6_STATINC(ICMP6_STAT_PMTUCHG); 1210 rt->rt_rmx.rmx_mtu = mtu; 1211 } 1212 } 1213 if (rt) { 1214 rt_unref(rt); 1215 } 1216 1217 /* 1218 * Notify protocols that the MTU for this destination 1219 * has changed. 1220 */ 1221 mutex_enter(&icmp6_mtx); 1222 for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL; 1223 mc = LIST_NEXT(mc, mc_list)) 1224 (*mc->mc_func)(&sin6.sin6_addr); 1225 mutex_exit(&icmp6_mtx); 1226} 1227 1228/* 1229 * Process a Node Information Query packet, based on 1230 * draft-ietf-ipngwg-icmp-name-lookups-07. 1231 * 1232 * Spec incompatibilities: 1233 * - IPv6 Subject address handling 1234 * - IPv4 Subject address handling support missing 1235 * - Proxy reply (answer even if it's not for me) 1236 * - joins NI group address at in6_ifattach() time only, does not cope 1237 * with hostname changes by sethostname(3) 1238 */ 1239static struct mbuf * 1240ni6_input(struct mbuf *m, int off) 1241{ 1242 struct icmp6_nodeinfo *ni6, *nni6; 1243 struct mbuf *n = NULL; 1244 u_int16_t qtype; 1245 int subjlen; 1246 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 1247 struct ni_reply_fqdn *fqdn; 1248 int addrs; /* for NI_QTYPE_NODEADDR */ 1249 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ 1250 struct sockaddr_in6 sin6; /* ip6_dst */ 1251 struct in6_addr in6_subj; /* subject address */ 1252 struct ip6_hdr *ip6; 1253 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ 1254 char *subj = NULL; 1255 struct ifnet *rcvif; 1256 int s, ss; 1257 struct ifaddr *ifa; 1258 struct psref psref; 1259 1260 ip6 = mtod(m, struct ip6_hdr *); 1261 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); 1262 if (ni6 == NULL) { 1263 /* m is already reclaimed */ 1264 return NULL; 1265 } 1266 KASSERT((m->m_flags & M_PKTHDR) != 0); 1267 1268 /* 1269 * Validate IPv6 destination address. 1270 * 1271 * The Responder must discard the Query without further processing 1272 * unless it is one of the Responder's unicast or anycast addresses, or 1273 * a link-local scope multicast address which the Responder has joined. 1274 * [icmp-name-lookups-07, Section 4.] 1275 */ 1276 sockaddr_in6_init(&sin6, &ip6->ip6_dst, 0, 0, 0); 1277 /* XXX scopeid */ 1278 ss = pserialize_read_enter(); 1279 ifa = ifa_ifwithaddr(sin6tosa(&sin6)); 1280 if (ifa != NULL) 1281 ; /* unicast/anycast, fine */ 1282 else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) 1283 ; /* link-local multicast, fine */ 1284 else { 1285 pserialize_read_exit(ss); 1286 goto bad; 1287 } 1288 pserialize_read_exit(ss); 1289 1290 /* validate query Subject field. */ 1291 qtype = ntohs(ni6->ni_qtype); 1292 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); 1293 switch (qtype) { 1294 case NI_QTYPE_NOOP: 1295 case NI_QTYPE_SUPTYPES: 1296 /* 07 draft */ 1297 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) 1298 break; 1299 /* FALLTHROUGH */ 1300 case NI_QTYPE_FQDN: 1301 case NI_QTYPE_NODEADDR: 1302 case NI_QTYPE_IPV4ADDR: 1303 switch (ni6->ni_code) { 1304 case ICMP6_NI_SUBJ_IPV6: 1305#if ICMP6_NI_SUBJ_IPV6 != 0 1306 case 0: 1307#endif 1308 /* 1309 * backward compatibility - try to accept 03 draft 1310 * format, where no Subject is present. 1311 */ 1312 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && 1313 subjlen == 0) { 1314 oldfqdn++; 1315 break; 1316 } 1317#if ICMP6_NI_SUBJ_IPV6 != 0 1318 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) 1319 goto bad; 1320#endif 1321 1322 if (subjlen != sizeof(sin6.sin6_addr)) 1323 goto bad; 1324 1325 /* 1326 * Validate Subject address. 1327 * 1328 * Not sure what exactly "address belongs to the node" 1329 * means in the spec, is it just unicast, or what? 1330 * 1331 * At this moment we consider Subject address as 1332 * "belong to the node" if the Subject address equals 1333 * to the IPv6 destination address; validation for 1334 * IPv6 destination address should have done enough 1335 * check for us. 1336 * 1337 * We do not do proxy at this moment. 1338 */ 1339 /* m_pulldown instead of copy? */ 1340 m_copydata(m, off + sizeof(struct icmp6_nodeinfo), 1341 subjlen, (void *)&in6_subj); 1342 rcvif = m_get_rcvif(m, &s); 1343 if (__predict_false(rcvif == NULL)) 1344 goto bad; 1345 if (in6_setscope(&in6_subj, rcvif, NULL)) { 1346 m_put_rcvif(rcvif, &s); 1347 goto bad; 1348 } 1349 m_put_rcvif(rcvif, &s); 1350 1351 subj = (char *)&in6_subj; 1352 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj)) 1353 break; 1354 1355 /* 1356 * XXX if we are to allow other cases, we should really 1357 * be careful about scope here. 1358 * basically, we should disallow queries toward IPv6 1359 * destination X with subject Y, if scope(X) > scope(Y). 1360 * if we allow scope(X) > scope(Y), it will result in 1361 * information leakage across scope boundary. 1362 */ 1363 goto bad; 1364 1365 case ICMP6_NI_SUBJ_FQDN: 1366 /* 1367 * Validate Subject name with gethostname(3). 1368 * 1369 * The behavior may need some debate, since: 1370 * - we are not sure if the node has FQDN as 1371 * hostname (returned by gethostname(3)). 1372 * - the code does wildcard match for truncated names. 1373 * however, we are not sure if we want to perform 1374 * wildcard match, if gethostname(3) side has 1375 * truncated hostname. 1376 */ 1377 n = ni6_nametodns(hostname, hostnamelen, 0); 1378 if (!n || n->m_next || n->m_len == 0) 1379 goto bad; 1380 IP6_EXTHDR_GET(subj, char *, m, 1381 off + sizeof(struct icmp6_nodeinfo), subjlen); 1382 if (subj == NULL) 1383 goto bad; 1384 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), 1385 n->m_len)) { 1386 goto bad; 1387 } 1388 m_freem(n); 1389 n = NULL; 1390 break; 1391 1392 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ 1393 default: 1394 goto bad; 1395 } 1396 break; 1397 } 1398 1399 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ 1400 switch (qtype) { 1401 case NI_QTYPE_FQDN: 1402 if ((icmp6_nodeinfo & 1) == 0) 1403 goto bad; 1404 break; 1405 case NI_QTYPE_NODEADDR: 1406 case NI_QTYPE_IPV4ADDR: 1407 if ((icmp6_nodeinfo & 2) == 0) 1408 goto bad; 1409 break; 1410 } 1411 1412 /* guess reply length */ 1413 switch (qtype) { 1414 case NI_QTYPE_NOOP: 1415 break; /* no reply data */ 1416 case NI_QTYPE_SUPTYPES: 1417 replylen += sizeof(u_int32_t); 1418 break; 1419 case NI_QTYPE_FQDN: 1420 /* will append an mbuf */ 1421 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 1422 break; 1423 case NI_QTYPE_NODEADDR: 1424 addrs = ni6_addrs(ni6, &ifp, subj, &psref); 1425 replylen += addrs * 1426 (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1427 if (replylen > MCLBYTES) 1428 replylen = MCLBYTES; /* XXX: will truncate pkt later */ 1429 break; 1430 case NI_QTYPE_IPV4ADDR: 1431 /* unsupported - should respond with unknown Qtype? */ 1432 goto bad; 1433 default: 1434 /* 1435 * XXX: We must return a reply with the ICMP6 code 1436 * `unknown Qtype' in this case. However we regard the case 1437 * as an FQDN query for backward compatibility. 1438 * Older versions set a random value to this field, 1439 * so it rarely varies in the defined qtypes. 1440 * But the mechanism is not reliable... 1441 * maybe we should obsolete older versions. 1442 */ 1443 qtype = NI_QTYPE_FQDN; 1444 /* will append an mbuf */ 1445 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 1446 oldfqdn++; 1447 break; 1448 } 1449 1450 /* allocate an mbuf to reply. */ 1451 MGETHDR(n, M_DONTWAIT, m->m_type); 1452 if (n == NULL) { 1453 goto bad; 1454 } 1455 M_MOVE_PKTHDR(n, m); /* just for rcvif */ 1456 if (replylen > MHLEN) { 1457 if (replylen > MCLBYTES) { 1458 /* 1459 * XXX: should we try to allocate more? But MCLBYTES 1460 * is probably much larger than IPV6_MMTU... 1461 */ 1462 goto bad; 1463 } 1464 MCLGET(n, M_DONTWAIT); 1465 if ((n->m_flags & M_EXT) == 0) { 1466 goto bad; 1467 } 1468 } 1469 n->m_pkthdr.len = n->m_len = replylen; 1470 1471 /* copy mbuf header and IPv6 + Node Information base headers */ 1472 bcopy(mtod(m, void *), mtod(n, void *), sizeof(struct ip6_hdr)); 1473 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); 1474 bcopy((void *)ni6, (void *)nni6, sizeof(struct icmp6_nodeinfo)); 1475 1476 /* qtype dependent procedure */ 1477 switch (qtype) { 1478 case NI_QTYPE_NOOP: 1479 nni6->ni_code = ICMP6_NI_SUCCESS; 1480 nni6->ni_flags = 0; 1481 break; 1482 case NI_QTYPE_SUPTYPES: 1483 { 1484 u_int32_t v; 1485 nni6->ni_code = ICMP6_NI_SUCCESS; 1486 nni6->ni_flags = htons(0x0000); /* raw bitmap */ 1487 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ 1488 v = (u_int32_t)htonl(0x0000000f); 1489 bcopy(&v, nni6 + 1, sizeof(u_int32_t)); 1490 break; 1491 } 1492 case NI_QTYPE_FQDN: 1493 nni6->ni_code = ICMP6_NI_SUCCESS; 1494 fqdn = (struct ni_reply_fqdn *)(mtod(n, char *) + 1495 sizeof(struct ip6_hdr) + 1496 sizeof(struct icmp6_nodeinfo)); 1497 nni6->ni_flags = 0; /* XXX: meaningless TTL */ 1498 fqdn->ni_fqdn_ttl = 0; /* ditto. */ 1499 /* 1500 * XXX do we really have FQDN in variable "hostname"? 1501 */ 1502 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn); 1503 if (n->m_next == NULL) 1504 goto bad; 1505 /* XXX we assume that n->m_next is not a chain */ 1506 if (n->m_next->m_next != NULL) 1507 goto bad; 1508 n->m_pkthdr.len += n->m_next->m_len; 1509 break; 1510 case NI_QTYPE_NODEADDR: 1511 { 1512 int lenlim, copied; 1513 1514 nni6->ni_code = ICMP6_NI_SUCCESS; 1515 n->m_pkthdr.len = n->m_len = 1516 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 1517 lenlim = M_TRAILINGSPACE(n); 1518 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); 1519 if_put(ifp, &psref); 1520 ifp = NULL; 1521 /* XXX: reset mbuf length */ 1522 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 1523 sizeof(struct icmp6_nodeinfo) + copied; 1524 break; 1525 } 1526 default: 1527 panic("%s: impossible", __func__); 1528 break; 1529 } 1530 1531 nni6->ni_type = ICMP6_NI_REPLY; 1532 m_freem(m); 1533 return n; 1534 1535bad: 1536 if_put(ifp, &psref); 1537 m_freem(m); 1538 if (n) 1539 m_freem(n); 1540 return NULL; 1541} 1542 1543#define isupper(x) ('A' <= (x) && (x) <= 'Z') 1544#define isalpha(x) (('A' <= (x) && (x) <= 'Z') || ('a' <= (x) && (x) <= 'z')) 1545#define isalnum(x) (isalpha(x) || ('0' <= (x) && (x) <= '9')) 1546#define tolower(x) (isupper(x) ? (x) + 'a' - 'A' : (x)) 1547 1548/* 1549 * make a mbuf with DNS-encoded string. no compression support. 1550 * 1551 * XXX names with less than 2 dots (like "foo" or "foo.section") will be 1552 * treated as truncated name (two \0 at the end). this is a wild guess. 1553 * 1554 * old - return pascal string if non-zero 1555 */ 1556static struct mbuf * 1557ni6_nametodns(const char *name, int namelen, int old) 1558{ 1559 struct mbuf *m; 1560 char *cp, *ep; 1561 const char *p, *q; 1562 int i, len, nterm; 1563 1564 if (old) 1565 len = namelen + 1; 1566 else 1567 len = MCLBYTES; 1568 1569 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */ 1570 MGET(m, M_DONTWAIT, MT_DATA); 1571 if (m && len > MLEN) { 1572 MCLGET(m, M_DONTWAIT); 1573 if ((m->m_flags & M_EXT) == 0) 1574 goto fail; 1575 } 1576 if (!m) 1577 goto fail; 1578 m->m_next = NULL; 1579 1580 if (old) { 1581 m->m_len = len; 1582 *mtod(m, char *) = namelen; 1583 bcopy(name, mtod(m, char *) + 1, namelen); 1584 return m; 1585 } else { 1586 m->m_len = 0; 1587 cp = mtod(m, char *); 1588 ep = mtod(m, char *) + M_TRAILINGSPACE(m); 1589 1590 /* if not certain about my name, return empty buffer */ 1591 if (namelen == 0) 1592 return m; 1593 1594 /* 1595 * guess if it looks like shortened hostname, or FQDN. 1596 * shortened hostname needs two trailing "\0". 1597 */ 1598 i = 0; 1599 for (p = name; p < name + namelen; p++) { 1600 if (*p == '.') 1601 i++; 1602 } 1603 if (i < 2) 1604 nterm = 2; 1605 else 1606 nterm = 1; 1607 1608 p = name; 1609 while (cp < ep && p < name + namelen) { 1610 i = 0; 1611 for (q = p; q < name + namelen && *q && *q != '.'; q++) 1612 i++; 1613 /* result does not fit into mbuf */ 1614 if (cp + i + 1 >= ep) 1615 goto fail; 1616 /* 1617 * DNS label length restriction, RFC1035 page 8. 1618 * "i == 0" case is included here to avoid returning 1619 * 0-length label on "foo..bar". 1620 */ 1621 if (i <= 0 || i >= 64) 1622 goto fail; 1623 *cp++ = i; 1624 if (!isalpha(p[0]) || !isalnum(p[i - 1])) 1625 goto fail; 1626 while (i > 0) { 1627 if (!isalnum(*p) && *p != '-') 1628 goto fail; 1629 if (isupper(*p)) { 1630 *cp++ = tolower(*p); 1631 p++; 1632 } else 1633 *cp++ = *p++; 1634 i--; 1635 } 1636 p = q; 1637 if (p < name + namelen && *p == '.') 1638 p++; 1639 } 1640 /* termination */ 1641 if (cp + nterm >= ep) 1642 goto fail; 1643 while (nterm-- > 0) 1644 *cp++ = '\0'; 1645 m->m_len = cp - mtod(m, char *); 1646 return m; 1647 } 1648 1649 panic("should not reach here"); 1650 /* NOTREACHED */ 1651 1652fail: 1653 if (m) 1654 m_freem(m); 1655 return NULL; 1656} 1657 1658/* 1659 * check if two DNS-encoded string matches. takes care of truncated 1660 * form (with \0\0 at the end). no compression support. 1661 * XXX upper/lowercase match (see RFC2065) 1662 */ 1663static int 1664ni6_dnsmatch(const char *a, int alen, const char *b, int blen) 1665{ 1666 const char *a0, *b0; 1667 int l; 1668 1669 /* simplest case - need validation? */ 1670 if (alen == blen && memcmp(a, b, alen) == 0) 1671 return 1; 1672 1673 a0 = a; 1674 b0 = b; 1675 1676 /* termination is mandatory */ 1677 if (alen < 2 || blen < 2) 1678 return 0; 1679 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') 1680 return 0; 1681 alen--; 1682 blen--; 1683 1684 while (a - a0 < alen && b - b0 < blen) { 1685 if (a - a0 + 1 > alen || b - b0 + 1 > blen) 1686 return 0; 1687 1688 if ((signed char)a[0] < 0 || (signed char)b[0] < 0) 1689 return 0; 1690 /* we don't support compression yet */ 1691 if (a[0] >= 64 || b[0] >= 64) 1692 return 0; 1693 1694 /* truncated case */ 1695 if (a[0] == 0 && a - a0 == alen - 1) 1696 return 1; 1697 if (b[0] == 0 && b - b0 == blen - 1) 1698 return 1; 1699 if (a[0] == 0 || b[0] == 0) 1700 return 0; 1701 1702 if (a[0] != b[0]) 1703 return 0; 1704 l = a[0]; 1705 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) 1706 return 0; 1707 if (memcmp(a + 1, b + 1, l) != 0) 1708 return 0; 1709 1710 a += 1 + l; 1711 b += 1 + l; 1712 } 1713 1714 if (a - a0 == alen && b - b0 == blen) 1715 return 1; 1716 else 1717 return 0; 1718} 1719 1720/* 1721 * calculate the number of addresses to be returned in the node info reply. 1722 */ 1723static int 1724ni6_addrs(struct icmp6_nodeinfo *ni6, struct ifnet **ifpp, char *subj, 1725 struct psref *psref) 1726{ 1727 struct ifnet *ifp; 1728 struct in6_ifaddr *ia6; 1729 struct ifaddr *ifa; 1730 struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */ 1731 int addrs = 0, addrsofif, iffound = 0; 1732 int niflags = ni6->ni_flags; 1733 int s; 1734 1735 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { 1736 switch (ni6->ni_code) { 1737 case ICMP6_NI_SUBJ_IPV6: 1738 if (subj == NULL) /* must be impossible... */ 1739 return (0); 1740 subj_ip6 = (struct sockaddr_in6 *)subj; 1741 break; 1742 default: 1743 /* 1744 * XXX: we only support IPv6 subject address for 1745 * this Qtype. 1746 */ 1747 return (0); 1748 } 1749 } 1750 1751 s = pserialize_read_enter(); 1752 IFNET_READER_FOREACH(ifp) { 1753 addrsofif = 0; 1754 IFADDR_READER_FOREACH(ifa, ifp) { 1755 if (ifa->ifa_addr->sa_family != AF_INET6) 1756 continue; 1757 ia6 = (struct in6_ifaddr *)ifa; 1758 1759 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && 1760 IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr, 1761 &ia6->ia_addr.sin6_addr)) 1762 iffound = 1; 1763 1764 /* 1765 * IPv4-mapped addresses can only be returned by a 1766 * Node Information proxy, since they represent 1767 * addresses of IPv4-only nodes, which perforce do 1768 * not implement this protocol. 1769 * [icmp-name-lookups-07, Section 5.4] 1770 * So we don't support NI_NODEADDR_FLAG_COMPAT in 1771 * this function at this moment. 1772 */ 1773 1774 /* What do we have to do about ::1? */ 1775 switch (in6_addrscope(&ia6->ia_addr.sin6_addr)) { 1776 case IPV6_ADDR_SCOPE_LINKLOCAL: 1777 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 1778 continue; 1779 break; 1780 case IPV6_ADDR_SCOPE_SITELOCAL: 1781 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 1782 continue; 1783 break; 1784 case IPV6_ADDR_SCOPE_GLOBAL: 1785 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 1786 continue; 1787 break; 1788 default: 1789 continue; 1790 } 1791 1792 /* 1793 * check if anycast is okay. 1794 * XXX: just experimental. not in the spec. 1795 */ 1796 if ((ia6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 1797 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 1798 continue; /* we need only unicast addresses */ 1799 1800 addrsofif++; /* count the address */ 1801 } 1802 if (iffound) { 1803 if_acquire(ifp, psref); 1804 pserialize_read_exit(s); 1805 *ifpp = ifp; 1806 return (addrsofif); 1807 } 1808 1809 addrs += addrsofif; 1810 } 1811 pserialize_read_exit(s); 1812 1813 return (addrs); 1814} 1815 1816static int 1817ni6_store_addrs(struct icmp6_nodeinfo *ni6, 1818 struct icmp6_nodeinfo *nni6, struct ifnet *ifp0, 1819 int resid) 1820{ 1821 struct ifnet *ifp; 1822 struct in6_ifaddr *ia6; 1823 struct ifaddr *ifa; 1824 struct ifnet *ifp_dep = NULL; 1825 int copied = 0, allow_deprecated = 0; 1826 u_char *cp = (u_char *)(nni6 + 1); 1827 int niflags = ni6->ni_flags; 1828 u_int32_t ltime; 1829 int s; 1830 1831 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) 1832 return (0); /* needless to copy */ 1833 1834 s = pserialize_read_enter(); 1835 ifp = ifp0 ? ifp0 : IFNET_READER_FIRST(); 1836again: 1837 1838 for (; ifp; ifp = IFNET_READER_NEXT(ifp)) 1839 { 1840 IFADDR_READER_FOREACH(ifa, ifp) { 1841 if (ifa->ifa_addr->sa_family != AF_INET6) 1842 continue; 1843 ia6 = (struct in6_ifaddr *)ifa; 1844 1845 if ((ia6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && 1846 allow_deprecated == 0) { 1847 /* 1848 * prefererred address should be put before 1849 * deprecated addresses. 1850 */ 1851 1852 /* record the interface for later search */ 1853 if (ifp_dep == NULL) 1854 ifp_dep = ifp; 1855 1856 continue; 1857 } 1858 else if ((ia6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && 1859 allow_deprecated != 0) 1860 continue; /* we now collect deprecated addrs */ 1861 1862 /* What do we have to do about ::1? */ 1863 switch (in6_addrscope(&ia6->ia_addr.sin6_addr)) { 1864 case IPV6_ADDR_SCOPE_LINKLOCAL: 1865 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 1866 continue; 1867 break; 1868 case IPV6_ADDR_SCOPE_SITELOCAL: 1869 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 1870 continue; 1871 break; 1872 case IPV6_ADDR_SCOPE_GLOBAL: 1873 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 1874 continue; 1875 break; 1876 default: 1877 continue; 1878 } 1879 1880 /* 1881 * check if anycast is okay. 1882 * XXX: just experimental. not in the spec. 1883 */ 1884 if ((ia6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 1885 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 1886 continue; 1887 1888 /* now we can copy the address */ 1889 if (resid < sizeof(struct in6_addr) + 1890 sizeof(u_int32_t)) { 1891 /* 1892 * We give up much more copy. 1893 * Set the truncate flag and return. 1894 */ 1895 nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE; 1896 goto out; 1897 } 1898 1899 /* 1900 * Set the TTL of the address. 1901 * The TTL value should be one of the following 1902 * according to the specification: 1903 * 1904 * 1. The remaining lifetime of a DHCP lease on the 1905 * address, or 1906 * 2. The remaining Valid Lifetime of a prefix from 1907 * which the address was derived through Stateless 1908 * Autoconfiguration. 1909 * 1910 * Note that we currently do not support stateful 1911 * address configuration by DHCPv6, so the former 1912 * case can't happen. 1913 * 1914 * TTL must be 2^31 > TTL >= 0. 1915 */ 1916 if (ia6->ia6_lifetime.ia6t_expire == 0) 1917 ltime = ND6_INFINITE_LIFETIME; 1918 else { 1919 if (ia6->ia6_lifetime.ia6t_expire > 1920 time_uptime) 1921 ltime = ia6->ia6_lifetime.ia6t_expire - 1922 time_uptime; 1923 else 1924 ltime = 0; 1925 } 1926 if (ltime > 0x7fffffff) 1927 ltime = 0x7fffffff; 1928 ltime = htonl(ltime); 1929 1930 bcopy(<ime, cp, sizeof(u_int32_t)); 1931 cp += sizeof(u_int32_t); 1932 1933 /* copy the address itself */ 1934 bcopy(&ia6->ia_addr.sin6_addr, cp, 1935 sizeof(struct in6_addr)); 1936 in6_clearscope((struct in6_addr *)cp); /* XXX */ 1937 cp += sizeof(struct in6_addr); 1938 1939 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1940 copied += (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1941 } 1942 if (ifp0) /* we need search only on the specified IF */ 1943 break; 1944 } 1945 1946 if (allow_deprecated == 0 && ifp_dep != NULL) { 1947 ifp = ifp_dep; 1948 allow_deprecated = 1; 1949 1950 goto again; 1951 } 1952out: 1953 pserialize_read_exit(s); 1954 return (copied); 1955} 1956 1957/* 1958 * XXX almost dup'ed code with rip6_input. 1959 */ 1960static int 1961icmp6_rip6_input(struct mbuf **mp, int off) 1962{ 1963 struct mbuf *m = *mp; 1964 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1965 struct inpcb_hdr *inph; 1966 struct in6pcb *in6p; 1967 struct in6pcb *last = NULL; 1968 struct sockaddr_in6 rip6src; 1969 struct icmp6_hdr *icmp6; 1970 struct mbuf *opts = NULL; 1971 1972 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); 1973 if (icmp6 == NULL) { 1974 /* m is already reclaimed */ 1975 return IPPROTO_DONE; 1976 } 1977 1978 /* 1979 * XXX: the address may have embedded scope zone ID, which should be 1980 * hidden from applications. 1981 */ 1982 sockaddr_in6_init(&rip6src, &ip6->ip6_src, 0, 0, 0); 1983 if (sa6_recoverscope(&rip6src)) { 1984 m_freem(m); 1985 return (IPPROTO_DONE); 1986 } 1987 1988 TAILQ_FOREACH(inph, &raw6cbtable.inpt_queue, inph_queue) { 1989 in6p = (struct in6pcb *)inph; 1990 if (in6p->in6p_af != AF_INET6) 1991 continue; 1992 if (in6p->in6p_ip6.ip6_nxt != IPPROTO_ICMPV6) 1993 continue; 1994 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 1995 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 1996 continue; 1997 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 1998 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 1999 continue; 2000 if (in6p->in6p_icmp6filt 2001 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, 2002 in6p->in6p_icmp6filt)) 2003 continue; 2004 if (last) { 2005 struct mbuf *n; 2006#ifdef IPSEC 2007 /* 2008 * Check AH/ESP integrity 2009 */ 2010 if (!ipsec_used || 2011 (ipsec_used && !ipsec6_in_reject(m, last))) 2012#endif 2013 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 2014 if (last->in6p_flags & IN6P_CONTROLOPTS) 2015 ip6_savecontrol(last, &opts, ip6, n); 2016 /* strip intermediate headers */ 2017 m_adj(n, off); 2018 if (sbappendaddr(&last->in6p_socket->so_rcv, 2019 sin6tosa(&rip6src), n, opts) == 0) { 2020 /* should notify about lost packet */ 2021 m_freem(n); 2022 if (opts) 2023 m_freem(opts); 2024 } else 2025 sorwakeup(last->in6p_socket); 2026 opts = NULL; 2027 } 2028 } 2029 last = in6p; 2030 } 2031 2032#ifdef IPSEC 2033 if (ipsec_used && last && ipsec6_in_reject(m, last)) { 2034 m_freem(m); 2035 IP6_STATDEC(IP6_STAT_DELIVERED); 2036 /* do not inject data into pcb */ 2037 } else 2038#endif 2039 if (last) { 2040 if (last->in6p_flags & IN6P_CONTROLOPTS) 2041 ip6_savecontrol(last, &opts, ip6, m); 2042 /* strip intermediate headers */ 2043 m_adj(m, off); 2044 if (sbappendaddr(&last->in6p_socket->so_rcv, 2045 sin6tosa(&rip6src), m, opts) == 0) { 2046 m_freem(m); 2047 if (opts) 2048 m_freem(opts); 2049 } else 2050 sorwakeup(last->in6p_socket); 2051 } else { 2052 m_freem(m); 2053 IP6_STATDEC(IP6_STAT_DELIVERED); 2054 } 2055 return IPPROTO_DONE; 2056} 2057 2058/* 2059 * Reflect the ip6 packet back to the source. 2060 * OFF points to the icmp6 header, counted from the top of the mbuf. 2061 * 2062 * Note: RFC 1885 required that an echo reply should be truncated if it 2063 * did not fit in with (return) path MTU, and KAME code supported the 2064 * behavior. However, as a clarification after the RFC, this limitation 2065 * was removed in a revised version of the spec, RFC 2463. We had kept the 2066 * old behavior, with a (non-default) ifdef block, while the new version of 2067 * the spec was an internet-draft status, and even after the new RFC was 2068 * published. But it would rather make sense to clean the obsoleted part 2069 * up, and to make the code simpler at this stage. 2070 */ 2071static void 2072icmp6_reflect(struct mbuf *m, size_t off) 2073{ 2074 struct ip6_hdr *ip6; 2075 struct icmp6_hdr *icmp6; 2076 const struct in6_ifaddr *ia; 2077 const struct ip6aux *ip6a; 2078 int plen; 2079 int type, code; 2080 struct ifnet *outif = NULL; 2081 struct in6_addr origdst; 2082 struct ifnet *rcvif; 2083 int s; 2084 bool ip6_src_filled = false; 2085 2086 /* too short to reflect */ 2087 if (off < sizeof(struct ip6_hdr)) { 2088 nd6log(LOG_DEBUG, 2089 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", 2090 (u_long)off, (u_long)sizeof(struct ip6_hdr), 2091 __FILE__, __LINE__); 2092 goto bad; 2093 } 2094 2095 /* 2096 * If there are extra headers between IPv6 and ICMPv6, strip 2097 * off that header first. 2098 */ 2099 CTASSERT(sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) <= MHLEN); 2100 if (off > sizeof(struct ip6_hdr)) { 2101 size_t l; 2102 struct ip6_hdr nip6; 2103 2104 l = off - sizeof(struct ip6_hdr); 2105 m_copydata(m, 0, sizeof(nip6), (void *)&nip6); 2106 m_adj(m, l); 2107 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 2108 if (m->m_len < l) { 2109 if ((m = m_pullup(m, l)) == NULL) 2110 return; 2111 } 2112 bcopy((void *)&nip6, mtod(m, void *), sizeof(nip6)); 2113 } else { 2114 size_t l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 2115 if (m->m_len < l) { 2116 if ((m = m_pullup(m, l)) == NULL) 2117 return; 2118 } 2119 } 2120 2121 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); 2122 ip6 = mtod(m, struct ip6_hdr *); 2123 ip6->ip6_nxt = IPPROTO_ICMPV6; 2124 icmp6 = (struct icmp6_hdr *)(ip6 + 1); 2125 type = icmp6->icmp6_type; /* keep type for statistics */ 2126 code = icmp6->icmp6_code; /* ditto. */ 2127 2128 origdst = ip6->ip6_dst; 2129 /* 2130 * ip6_input() drops a packet if its src is multicast. 2131 * So, the src is never multicast. 2132 */ 2133 ip6->ip6_dst = ip6->ip6_src; 2134 2135 /* 2136 * If the incoming packet was addressed directly to us (i.e. unicast), 2137 * use dst as the src for the reply. 2138 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible 2139 * (for example) when we encounter an error while forwarding procedure 2140 * destined to a duplicated address of ours. 2141 * Note that ip6_getdstifaddr() may fail if we are in an error handling 2142 * procedure of an outgoing packet of our own, in which case we need 2143 * to search in the ifaddr list. 2144 */ 2145 if (IN6_IS_ADDR_MULTICAST(&origdst)) 2146 ; 2147 else if ((ip6a = ip6_getdstifaddr(m)) != NULL) { 2148 if ((ip6a->ip6a_flags & 2149 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) { 2150 ip6->ip6_src = ip6a->ip6a_src; 2151 ip6_src_filled = true; 2152 } 2153 } else { 2154 union { 2155 struct sockaddr_in6 sin6; 2156 struct sockaddr sa; 2157 } u; 2158 int _s; 2159 struct ifaddr *ifa; 2160 2161 sockaddr_in6_init(&u.sin6, &origdst, 0, 0, 0); 2162 2163 _s = pserialize_read_enter(); 2164 ifa = ifa_ifwithaddr(&u.sa); 2165 2166 if (ifa != NULL) { 2167 ia = ifatoia6(ifa); 2168 if ((ia->ia6_flags & 2169 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) { 2170 ip6->ip6_src = ia->ia_addr.sin6_addr; 2171 ip6_src_filled = true; 2172 } 2173 } 2174 pserialize_read_exit(_s); 2175 } 2176 2177 if (!ip6_src_filled) { 2178 int e; 2179 struct sockaddr_in6 sin6; 2180 struct route ro; 2181 2182 /* 2183 * This case matches to multicasts, our anycast, or unicasts 2184 * that we do not own. Select a source address based on the 2185 * source address of the erroneous packet. 2186 */ 2187 /* zone ID should be embedded */ 2188 sockaddr_in6_init(&sin6, &ip6->ip6_dst, 0, 0, 0); 2189 2190 memset(&ro, 0, sizeof(ro)); 2191 e = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, NULL, NULL, 2192 &ip6->ip6_src); 2193 rtcache_free(&ro); 2194 if (e != 0) { 2195 char ip6buf[INET6_ADDRSTRLEN]; 2196 nd6log(LOG_DEBUG, 2197 "source can't be determined: " 2198 "dst=%s, error=%d\n", 2199 IN6_PRINT(ip6buf, &sin6.sin6_addr), e); 2200 goto bad; 2201 } 2202 } 2203 2204 ip6->ip6_flow = 0; 2205 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 2206 ip6->ip6_vfc |= IPV6_VERSION; 2207 ip6->ip6_nxt = IPPROTO_ICMPV6; 2208 rcvif = m_get_rcvif(m, &s); 2209 if (rcvif) { 2210 /* XXX: This may not be the outgoing interface */ 2211 ip6->ip6_hlim = ND_IFINFO(rcvif)->chlim; 2212 } else 2213 ip6->ip6_hlim = ip6_defhlim; 2214 m_put_rcvif(rcvif, &s); 2215 2216 m->m_pkthdr.csum_flags = 0; 2217 icmp6->icmp6_cksum = 0; 2218 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2219 sizeof(struct ip6_hdr), plen); 2220 2221 /* 2222 * XXX option handling 2223 */ 2224 2225 m->m_flags &= ~(M_BCAST|M_MCAST); 2226 2227 /* 2228 * To avoid a "too big" situation at an intermediate router 2229 * and the path MTU discovery process, specify the IPV6_MINMTU flag. 2230 * Note that only echo and node information replies are affected, 2231 * since the length of ICMP6 errors is limited to the minimum MTU. 2232 */ 2233 if (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL, &outif) 2234 != 0 && outif) 2235 icmp6_ifstat_inc(outif, ifs6_out_error); 2236 if (outif) 2237 icmp6_ifoutstat_inc(outif, type, code); 2238 2239 return; 2240 2241 bad: 2242 m_freem(m); 2243 return; 2244} 2245 2246static const char * 2247icmp6_redirect_diag(char *buf, size_t buflen, struct in6_addr *src6, 2248 struct in6_addr *dst6, struct in6_addr *tgt6) 2249{ 2250 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 2251 char ip6buft[INET6_ADDRSTRLEN]; 2252 2253 snprintf(buf, buflen, "(src=%s dst=%s tgt=%s)", 2254 IN6_PRINT(ip6bufs, src6), IN6_PRINT(ip6bufd, dst6), 2255 IN6_PRINT(ip6buft, tgt6)); 2256 return buf; 2257} 2258 2259static void 2260icmp6_redirect_input(struct mbuf *m, int off) 2261{ 2262 struct ifnet *ifp; 2263 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 2264 struct nd_redirect *nd_rd; 2265 int icmp6len = ntohs(ip6->ip6_plen); 2266 char *lladdr = NULL; 2267 int lladdrlen = 0; 2268 struct rtentry *rt = NULL; 2269 int is_router; 2270 int is_onlink; 2271 struct in6_addr src6 = ip6->ip6_src; 2272 struct in6_addr redtgt6; 2273 struct in6_addr reddst6; 2274 union nd_opts ndopts; 2275 struct psref psref; 2276 char ip6buf[INET6_ADDRSTRLEN]; 2277 char diagbuf[256]; 2278 2279 ifp = m_get_rcvif_psref(m, &psref); 2280 if (ifp == NULL) 2281 goto freeit; 2282 2283 /* XXX if we are router, we don't update route by icmp6 redirect */ 2284 if (ip6_forwarding) 2285 goto freeit; 2286 if (!icmp6_rediraccept) 2287 goto freeit; 2288 2289 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); 2290 if (nd_rd == NULL) { 2291 ICMP6_STATINC(ICMP6_STAT_TOOSHORT); 2292 m_put_rcvif_psref(ifp, &psref); 2293 return; 2294 } 2295 redtgt6 = nd_rd->nd_rd_target; 2296 reddst6 = nd_rd->nd_rd_dst; 2297 2298 if (in6_setscope(&redtgt6, ifp, NULL) || 2299 in6_setscope(&reddst6, ifp, NULL)) { 2300 goto freeit; 2301 } 2302 2303 /* validation */ 2304 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { 2305 nd6log(LOG_ERR, 2306 "ICMP6 redirect sent from %s rejected; " 2307 "must be from linklocal\n", IN6_PRINT(ip6buf, &src6)); 2308 goto bad; 2309 } 2310 if (ip6->ip6_hlim != 255) { 2311 nd6log(LOG_ERR, 2312 "ICMP6 redirect sent from %s rejected; " 2313 "hlim=%d (must be 255)\n", 2314 IN6_PRINT(ip6buf, &src6), ip6->ip6_hlim); 2315 goto bad; 2316 } 2317 2318 { 2319 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ 2320 struct sockaddr_in6 sin6; 2321 struct in6_addr *gw6; 2322 2323 sockaddr_in6_init(&sin6, &reddst6, 0, 0, 0); 2324 rt = rtalloc1(sin6tosa(&sin6), 0); 2325 if (rt) { 2326 if (rt->rt_gateway == NULL || 2327 rt->rt_gateway->sa_family != AF_INET6) { 2328 nd6log(LOG_ERR, 2329 "ICMP6 redirect rejected; no route " 2330 "with inet6 gateway found for redirect dst: %s\n", 2331 icmp6_redirect_diag(diagbuf, sizeof(diagbuf), 2332 &src6, &reddst6, &redtgt6)); 2333 rt_unref(rt); 2334 goto bad; 2335 } 2336 2337 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); 2338 if (memcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { 2339 nd6log(LOG_ERR, 2340 "ICMP6 redirect rejected; " 2341 "not equal to gw-for-src=%s (must be same): %s\n", 2342 IN6_PRINT(ip6buf, gw6), 2343 icmp6_redirect_diag(diagbuf, sizeof(diagbuf), 2344 &src6, &reddst6, &redtgt6)); 2345 rt_unref(rt); 2346 goto bad; 2347 } 2348 } else { 2349 nd6log(LOG_ERR, "ICMP6 redirect rejected; " 2350 "no route found for redirect dst: %s\n", 2351 icmp6_redirect_diag(diagbuf, sizeof(diagbuf), 2352 &src6, &reddst6, &redtgt6)); 2353 goto bad; 2354 } 2355 rt_unref(rt); 2356 rt = NULL; 2357 } 2358 2359 if (IN6_IS_ADDR_MULTICAST(&reddst6)) { 2360 nd6log(LOG_ERR, "ICMP6 redirect rejected; " 2361 "redirect dst must be unicast: %s\n", 2362 icmp6_redirect_diag(diagbuf, sizeof(diagbuf), 2363 &src6, &reddst6, &redtgt6)); 2364 goto bad; 2365 } 2366 2367 is_router = is_onlink = 0; 2368 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 2369 is_router = 1; /* router case */ 2370 if (memcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) 2371 is_onlink = 1; /* on-link destination case */ 2372 if (!is_router && !is_onlink) { 2373 nd6log(LOG_ERR, "ICMP6 redirect rejected; " 2374 "neither router case nor onlink case: %s\n", 2375 icmp6_redirect_diag(diagbuf, sizeof(diagbuf), 2376 &src6, &reddst6, &redtgt6)); 2377 goto bad; 2378 } 2379 /* validation passed */ 2380 2381 icmp6len -= sizeof(*nd_rd); 2382 nd6_option_init(nd_rd + 1, icmp6len, &ndopts); 2383 if (nd6_options(&ndopts) < 0) { 2384 nd6log(LOG_INFO, "invalid ND option, rejected: %s\n", 2385 icmp6_redirect_diag(diagbuf, sizeof(diagbuf), 2386 &src6, &reddst6, &redtgt6)); 2387 /* nd6_options have incremented stats */ 2388 goto freeit; 2389 } 2390 2391 if (ndopts.nd_opts_tgt_lladdr) { 2392 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 2393 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 2394 } 2395 2396 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 2397 nd6log(LOG_INFO, "lladdrlen mismatch for %s " 2398 "(if %d, icmp6 packet %d): %s\n", 2399 IN6_PRINT(ip6buf, &redtgt6), 2400 ifp->if_addrlen, lladdrlen - 2, 2401 icmp6_redirect_diag(diagbuf, sizeof(diagbuf), 2402 &src6, &reddst6, &redtgt6)); 2403 goto bad; 2404 } 2405 2406 /* RFC 2461 8.3 */ 2407 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, 2408 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); 2409 2410 m_put_rcvif_psref(ifp, &psref); 2411 ifp = NULL; 2412 2413 if (!is_onlink) { /* better router case. perform rtredirect. */ 2414 /* perform rtredirect */ 2415 struct sockaddr_in6 sdst; 2416 struct sockaddr_in6 sgw; 2417 struct sockaddr_in6 ssrc; 2418 unsigned long rtcount; 2419 struct rtentry *newrt = NULL; 2420 2421 /* 2422 * do not install redirect route, if the number of entries 2423 * is too much (> hiwat). note that, the node (= host) will 2424 * work just fine even if we do not install redirect route 2425 * (there will be additional hops, though). 2426 */ 2427 mutex_enter(&icmp6_mtx); 2428 rtcount = rt_timer_count(icmp6_redirect_timeout_q); 2429 if (0 <= ip6_maxdynroutes && rtcount >= ip6_maxdynroutes) { 2430 mutex_exit(&icmp6_mtx); 2431 goto freeit; 2432 } 2433 if (0 <= icmp6_redirect_hiwat && rtcount > icmp6_redirect_hiwat) { 2434 mutex_exit(&icmp6_mtx); 2435 goto freeit; 2436 } else if (0 <= icmp6_redirect_lowat && 2437 rtcount > icmp6_redirect_lowat) { 2438 /* 2439 * XXX nuke a victim, install the new one. 2440 */ 2441 } 2442 2443 memset(&sdst, 0, sizeof(sdst)); 2444 memset(&sgw, 0, sizeof(sgw)); 2445 memset(&ssrc, 0, sizeof(ssrc)); 2446 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; 2447 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = 2448 sizeof(struct sockaddr_in6); 2449 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); 2450 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 2451 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); 2452 rtredirect(sin6tosa(&sdst), sin6tosa(&sgw), NULL, 2453 RTF_GATEWAY | RTF_HOST, sin6tosa(&ssrc), &newrt); 2454 2455 if (newrt) { 2456 (void)rt_timer_add(newrt, icmp6_redirect_timeout, 2457 icmp6_redirect_timeout_q); 2458 rt_unref(newrt); 2459 } 2460 mutex_exit(&icmp6_mtx); 2461 } 2462 /* finally update cached route in each socket via pfctlinput */ 2463 { 2464 struct sockaddr_in6 sdst; 2465 2466 sockaddr_in6_init(&sdst, &reddst6, 0, 0, 0); 2467 pfctlinput(PRC_REDIRECT_HOST, sin6tosa(&sdst)); 2468#if defined(IPSEC) 2469 if (ipsec_used) 2470 key_sa_routechange(sin6tosa(&sdst)); 2471#endif 2472 } 2473 2474freeit: 2475 if (ifp != NULL) 2476 m_put_rcvif_psref(ifp, &psref); 2477 m_freem(m); 2478 return; 2479 2480bad: 2481 m_put_rcvif_psref(ifp, &psref); 2482 ICMP6_STATINC(ICMP6_STAT_BADREDIRECT); 2483 m_freem(m); 2484} 2485 2486void 2487icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt) 2488{ 2489 struct ifnet *ifp; /* my outgoing interface */ 2490 struct in6_addr *ifp_ll6; 2491 struct in6_addr *nexthop; 2492 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ 2493 struct mbuf *m = NULL; /* newly allocated one */ 2494 struct ip6_hdr *ip6; /* m as struct ip6_hdr */ 2495 struct nd_redirect *nd_rd; 2496 size_t maxlen; 2497 u_char *p; 2498 struct sockaddr_in6 src_sa; 2499 2500 icmp6_errcount(ICMP6_STAT_OUTERRHIST, ND_REDIRECT, 0); 2501 2502 /* if we are not router, we don't send icmp6 redirect */ 2503 if (!ip6_forwarding) 2504 goto fail; 2505 2506 /* sanity check */ 2507 KASSERT(m0 != NULL); 2508 KASSERT(rt != NULL); 2509 2510 ifp = rt->rt_ifp; 2511 2512 /* 2513 * Address check: 2514 * the source address must identify a neighbor, and 2515 * the destination address must not be a multicast address 2516 * [RFC 2461, sec 8.2] 2517 */ 2518 sip6 = mtod(m0, struct ip6_hdr *); 2519 sockaddr_in6_init(&src_sa, &sip6->ip6_src, 0, 0, 0); 2520 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0) 2521 goto fail; 2522 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) 2523 goto fail; /* what should we do here? */ 2524 2525 /* rate limit */ 2526 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) 2527 goto fail; 2528 2529 /* 2530 * Since we are going to append up to 1280 bytes (= IPV6_MMTU), 2531 * we almost always ask for an mbuf cluster for simplicity. 2532 * (MHLEN < IPV6_MMTU is almost always true) 2533 */ 2534 MGETHDR(m, M_DONTWAIT, MT_HEADER); 2535 if (m && IPV6_MMTU >= MHLEN) { 2536#if IPV6_MMTU >= MCLBYTES 2537 _MCLGET(m, mcl_cache, IPV6_MMTU, M_DONTWAIT); 2538#else 2539 MCLGET(m, M_DONTWAIT); 2540#endif 2541 } 2542 2543 if (!m) 2544 goto fail; 2545 m_reset_rcvif(m); 2546 m->m_len = 0; 2547 maxlen = M_TRAILINGSPACE(m); 2548 maxlen = min(IPV6_MMTU, maxlen); 2549 2550 /* just for safety */ 2551 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct nd_redirect) + 2552 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { 2553 goto fail; 2554 } 2555 2556 { 2557 /* get ip6 linklocal address for ifp(my outgoing interface). */ 2558 struct in6_ifaddr *ia; 2559 int s = pserialize_read_enter(); 2560 if ((ia = in6ifa_ifpforlinklocal(ifp, 2561 IN6_IFF_NOTREADY| 2562 IN6_IFF_ANYCAST)) == NULL) { 2563 pserialize_read_exit(s); 2564 goto fail; 2565 } 2566 ifp_ll6 = &ia->ia_addr.sin6_addr; 2567 pserialize_read_exit(s); 2568 } 2569 2570 /* get ip6 linklocal address for the router. */ 2571 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { 2572 struct sockaddr_in6 *sin6; 2573 sin6 = (struct sockaddr_in6 *)rt->rt_gateway; 2574 nexthop = &sin6->sin6_addr; 2575 if (!IN6_IS_ADDR_LINKLOCAL(nexthop)) 2576 nexthop = NULL; 2577 } else 2578 nexthop = NULL; 2579 2580 /* ip6 */ 2581 ip6 = mtod(m, struct ip6_hdr *); 2582 ip6->ip6_flow = 0; 2583 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 2584 ip6->ip6_vfc |= IPV6_VERSION; 2585 /* ip6->ip6_plen will be set later */ 2586 ip6->ip6_nxt = IPPROTO_ICMPV6; 2587 ip6->ip6_hlim = 255; 2588 /* ip6->ip6_src must be linklocal addr for my outgoing if. */ 2589 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); 2590 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); 2591 2592 /* ND Redirect */ 2593 nd_rd = (struct nd_redirect *)(ip6 + 1); 2594 nd_rd->nd_rd_type = ND_REDIRECT; 2595 nd_rd->nd_rd_code = 0; 2596 nd_rd->nd_rd_reserved = 0; 2597 if (rt->rt_flags & RTF_GATEWAY) { 2598 /* 2599 * nd_rd->nd_rd_target must be a link-local address in 2600 * better router cases. 2601 */ 2602 if (!nexthop) 2603 goto fail; 2604 bcopy(nexthop, &nd_rd->nd_rd_target, 2605 sizeof(nd_rd->nd_rd_target)); 2606 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2607 sizeof(nd_rd->nd_rd_dst)); 2608 } else { 2609 /* make sure redtgt == reddst */ 2610 nexthop = &sip6->ip6_dst; 2611 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target, 2612 sizeof(nd_rd->nd_rd_target)); 2613 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2614 sizeof(nd_rd->nd_rd_dst)); 2615 } 2616 2617 p = (u_char *)(nd_rd + 1); 2618 2619 { 2620 /* target lladdr option */ 2621 struct llentry *ln = NULL; 2622 int len, pad; 2623 struct nd_opt_hdr *nd_opt; 2624 char *lladdr; 2625 2626 ln = nd6_lookup(nexthop, ifp, false); 2627 if (ln == NULL) 2628 goto nolladdropt; 2629 len = sizeof(*nd_opt) + ifp->if_addrlen; 2630 len = (len + 7) & ~7; /* round by 8 */ 2631 pad = len - (sizeof(*nd_opt) + ifp->if_addrlen); 2632 2633 /* safety check */ 2634 if (len + (p - (u_char *)ip6) > maxlen) { 2635 LLE_RUNLOCK(ln); 2636 goto nolladdropt; 2637 } 2638 2639 if (ln->la_flags & LLE_VALID) { 2640 nd_opt = (struct nd_opt_hdr *)p; 2641 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 2642 nd_opt->nd_opt_len = len >> 3; 2643 lladdr = (char *)(nd_opt + 1); 2644 memcpy(lladdr, &ln->ll_addr, ifp->if_addrlen); 2645 memset(lladdr + ifp->if_addrlen, 0, pad); 2646 p += len; 2647 } 2648 LLE_RUNLOCK(ln); 2649 } 2650nolladdropt: 2651 2652 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 2653 2654 /* just to be safe */ 2655 if (m0->m_flags & M_DECRYPTED) 2656 goto noredhdropt; 2657 if (p - (u_char *)ip6 > maxlen) 2658 goto noredhdropt; 2659 2660 { 2661 /* redirected header option */ 2662 int len; 2663 struct nd_opt_rd_hdr *nd_opt_rh; 2664 2665 /* 2666 * compute the maximum size for icmp6 redirect header option. 2667 * XXX room for auth header? 2668 */ 2669 len = maxlen - (p - (u_char *)ip6); 2670 len &= ~7; 2671 2672 if (len < sizeof(*nd_opt_rh)) { 2673 goto noredhdropt; 2674 } 2675 2676 /* 2677 * Redirected header option spec (RFC2461 4.6.3) talks nothing 2678 * about padding/truncate rule for the original IP packet. 2679 * From the discussion on IPv6imp in Feb 1999, 2680 * the consensus was: 2681 * - "attach as much as possible" is the goal 2682 * - pad if not aligned (original size can be guessed by 2683 * original ip6 header) 2684 * Following code adds the padding if it is simple enough, 2685 * and truncates if not. 2686 */ 2687 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { 2688 /* not enough room, truncate */ 2689 m_adj(m0, (len - sizeof(*nd_opt_rh)) - 2690 m0->m_pkthdr.len); 2691 } else { 2692 /* 2693 * enough room, truncate if not aligned. 2694 * we don't pad here for simplicity. 2695 */ 2696 int extra; 2697 2698 extra = m0->m_pkthdr.len % 8; 2699 if (extra) { 2700 /* truncate */ 2701 m_adj(m0, -extra); 2702 } 2703 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); 2704 } 2705 2706 nd_opt_rh = (struct nd_opt_rd_hdr *)p; 2707 memset(nd_opt_rh, 0, sizeof(*nd_opt_rh)); 2708 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; 2709 nd_opt_rh->nd_opt_rh_len = len >> 3; 2710 p += sizeof(*nd_opt_rh); 2711 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 2712 2713 /* connect m0 to m */ 2714 m->m_pkthdr.len += m0->m_pkthdr.len; 2715 m_cat(m, m0); 2716 m0 = NULL; 2717 } 2718noredhdropt: 2719 if (m0) { 2720 m_freem(m0); 2721 m0 = NULL; 2722 } 2723 2724 /* XXX: clear embedded link IDs in the inner header */ 2725 in6_clearscope(&sip6->ip6_src); 2726 in6_clearscope(&sip6->ip6_dst); 2727 in6_clearscope(&nd_rd->nd_rd_target); 2728 in6_clearscope(&nd_rd->nd_rd_dst); 2729 2730 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 2731 2732 nd_rd->nd_rd_cksum = 0; 2733 nd_rd->nd_rd_cksum = 2734 in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen)); 2735 2736 /* send the packet to outside... */ 2737 if (ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL) != 0) 2738 icmp6_ifstat_inc(ifp, ifs6_out_error); 2739 2740 icmp6_ifstat_inc(ifp, ifs6_out_msg); 2741 icmp6_ifstat_inc(ifp, ifs6_out_redirect); 2742 ICMP6_STATINC(ICMP6_STAT_OUTHIST + ND_REDIRECT); 2743 2744 return; 2745 2746fail: 2747 if (m) 2748 m_freem(m); 2749 if (m0) 2750 m_freem(m0); 2751} 2752 2753/* 2754 * ICMPv6 socket option processing. 2755 */ 2756int 2757icmp6_ctloutput(int op, struct socket *so, struct sockopt *sopt) 2758{ 2759 int error = 0; 2760 struct in6pcb *in6p = sotoin6pcb(so); 2761 2762 if (sopt->sopt_level != IPPROTO_ICMPV6) 2763 return rip6_ctloutput(op, so, sopt); 2764 2765 switch (op) { 2766 case PRCO_SETOPT: 2767 switch (sopt->sopt_name) { 2768 case ICMP6_FILTER: 2769 { 2770 struct icmp6_filter fil; 2771 2772 error = sockopt_get(sopt, &fil, sizeof(fil)); 2773 if (error) 2774 break; 2775 memcpy(in6p->in6p_icmp6filt, &fil, 2776 sizeof(struct icmp6_filter)); 2777 error = 0; 2778 break; 2779 } 2780 2781 default: 2782 error = ENOPROTOOPT; 2783 break; 2784 } 2785 break; 2786 2787 case PRCO_GETOPT: 2788 switch (sopt->sopt_name) { 2789 case ICMP6_FILTER: 2790 { 2791 if (in6p->in6p_icmp6filt == NULL) { 2792 error = EINVAL; 2793 break; 2794 } 2795 error = sockopt_set(sopt, in6p->in6p_icmp6filt, 2796 sizeof(struct icmp6_filter)); 2797 break; 2798 } 2799 2800 default: 2801 error = ENOPROTOOPT; 2802 break; 2803 } 2804 break; 2805 } 2806 2807 return (error); 2808} 2809 2810/* 2811 * Perform rate limit check. 2812 * Returns 0 if it is okay to send the icmp6 packet. 2813 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate 2814 * limitation. 2815 * 2816 * XXX per-destination/type check necessary? 2817 */ 2818static int 2819icmp6_ratelimit( 2820 const struct in6_addr *dst, /* not used at this moment */ 2821 const int type, /* not used at this moment */ 2822 const int code) /* not used at this moment */ 2823{ 2824 int ret; 2825 2826 ret = 0; /* okay to send */ 2827 2828 /* PPS limit */ 2829 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count, 2830 icmp6errppslim)) { 2831 /* The packet is subject to rate limit */ 2832 ret++; 2833 } 2834 2835 return ret; 2836} 2837 2838static struct rtentry * 2839icmp6_mtudisc_clone(struct sockaddr *dst) 2840{ 2841 struct rtentry *rt; 2842 int error; 2843 2844 rt = rtalloc1(dst, 1); 2845 if (rt == 0) 2846 return NULL; 2847 2848 /* If we didn't get a host route, allocate one */ 2849 if ((rt->rt_flags & RTF_HOST) == 0) { 2850 struct rtentry *nrt; 2851 2852 error = rtrequest(RTM_ADD, dst, rt->rt_gateway, NULL, 2853 RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt); 2854 if (error) { 2855 rt_unref(rt); 2856 return NULL; 2857 } 2858 nrt->rt_rmx = rt->rt_rmx; 2859 rt_unref(rt); 2860 rt = nrt; 2861 } 2862 2863 mutex_enter(&icmp6_mtx); 2864 error = rt_timer_add(rt, icmp6_mtudisc_timeout, 2865 icmp6_mtudisc_timeout_q); 2866 mutex_exit(&icmp6_mtx); 2867 2868 if (error) { 2869 rt_unref(rt); 2870 return NULL; 2871 } 2872 2873 return rt; /* caller need to call rtfree() */ 2874} 2875 2876static void 2877icmp6_mtudisc_timeout(struct rtentry *rt, struct rttimer *r) 2878{ 2879 2880 KASSERT(rt != NULL); 2881 rt_assert_referenced(rt); 2882 2883 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) == 2884 (RTF_DYNAMIC | RTF_HOST)) { 2885 rtrequest(RTM_DELETE, rt_getkey(rt), 2886 rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL); 2887 } else { 2888 if (!(rt->rt_rmx.rmx_locks & RTV_MTU)) 2889 rt->rt_rmx.rmx_mtu = 0; 2890 } 2891} 2892 2893static void 2894icmp6_redirect_timeout(struct rtentry *rt, struct rttimer *r) 2895{ 2896 2897 KASSERT(rt != NULL); 2898 rt_assert_referenced(rt); 2899 2900 if ((rt->rt_flags & (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) == 2901 (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) { 2902 rtrequest(RTM_DELETE, rt_getkey(rt), 2903 rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL); 2904 } 2905} 2906 2907/* 2908 * sysctl helper routine for the net.inet6.icmp6.nd6 nodes. silly? 2909 */ 2910static int 2911sysctl_net_inet6_icmp6_nd6(SYSCTLFN_ARGS) 2912{ 2913 (void)&name; 2914 (void)&l; 2915 (void)&oname; 2916 2917 if (namelen != 0) 2918 return (EINVAL); 2919 2920 return (nd6_sysctl(rnode->sysctl_num, oldp, oldlenp, 2921 /*XXXUNCONST*/ 2922 __UNCONST(newp), newlen)); 2923} 2924 2925static int 2926sysctl_net_inet6_icmp6_stats(SYSCTLFN_ARGS) 2927{ 2928 2929 return (NETSTAT_SYSCTL(icmp6stat_percpu, ICMP6_NSTATS)); 2930} 2931 2932static int 2933sysctl_net_inet6_icmp6_redirtimeout(SYSCTLFN_ARGS) 2934{ 2935 int error, tmp; 2936 struct sysctlnode node; 2937 2938 mutex_enter(&icmp6_mtx); 2939 2940 node = *rnode; 2941 node.sysctl_data = &tmp; 2942 tmp = icmp6_redirtimeout; 2943 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 2944 if (error || newp == NULL) 2945 goto out; 2946 if (tmp < 0) { 2947 error = EINVAL; 2948 goto out; 2949 } 2950 icmp6_redirtimeout = tmp; 2951 2952 if (icmp6_redirect_timeout_q != NULL) { 2953 if (icmp6_redirtimeout == 0) { 2954 rt_timer_queue_destroy(icmp6_redirect_timeout_q); 2955 } else { 2956 rt_timer_queue_change(icmp6_redirect_timeout_q, 2957 icmp6_redirtimeout); 2958 } 2959 } else if (icmp6_redirtimeout > 0) { 2960 icmp6_redirect_timeout_q = 2961 rt_timer_queue_create(icmp6_redirtimeout); 2962 } 2963 error = 0; 2964out: 2965 mutex_exit(&icmp6_mtx); 2966 return error; 2967} 2968 2969static void 2970sysctl_net_inet6_icmp6_setup(struct sysctllog **clog) 2971{ 2972 extern int nd6_maxqueuelen; /* defined in nd6.c */ 2973 2974 sysctl_createv(clog, 0, NULL, NULL, 2975 CTLFLAG_PERMANENT, 2976 CTLTYPE_NODE, "inet6", NULL, 2977 NULL, 0, NULL, 0, 2978 CTL_NET, PF_INET6, CTL_EOL); 2979 sysctl_createv(clog, 0, NULL, NULL, 2980 CTLFLAG_PERMANENT, 2981 CTLTYPE_NODE, "icmp6", 2982 SYSCTL_DESCR("ICMPv6 related settings"), 2983 NULL, 0, NULL, 0, 2984 CTL_NET, PF_INET6, IPPROTO_ICMPV6, CTL_EOL); 2985 2986 sysctl_createv(clog, 0, NULL, NULL, 2987 CTLFLAG_PERMANENT, 2988 CTLTYPE_STRUCT, "stats", 2989 SYSCTL_DESCR("ICMPv6 transmission statistics"), 2990 sysctl_net_inet6_icmp6_stats, 0, NULL, 0, 2991 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 2992 ICMPV6CTL_STATS, CTL_EOL); 2993 sysctl_createv(clog, 0, NULL, NULL, 2994 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2995 CTLTYPE_INT, "rediraccept", 2996 SYSCTL_DESCR("Accept and process redirect messages"), 2997 NULL, 0, &icmp6_rediraccept, 0, 2998 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 2999 ICMPV6CTL_REDIRACCEPT, CTL_EOL); 3000 sysctl_createv(clog, 0, NULL, NULL, 3001 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3002 CTLTYPE_INT, "redirtimeout", 3003 SYSCTL_DESCR("Redirect generated route lifetime"), 3004 sysctl_net_inet6_icmp6_redirtimeout, 0, 3005 &icmp6_redirtimeout, 0, 3006 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3007 ICMPV6CTL_REDIRTIMEOUT, CTL_EOL); 3008#if 0 /* obsoleted */ 3009 sysctl_createv(clog, 0, NULL, NULL, 3010 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3011 CTLTYPE_INT, "errratelimit", NULL, 3012 NULL, 0, &icmp6_errratelimit, 0, 3013 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3014 ICMPV6CTL_ERRRATELIMIT, CTL_EOL); 3015#endif 3016 sysctl_createv(clog, 0, NULL, NULL, 3017 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3018 CTLTYPE_INT, "nd6_prune", 3019 SYSCTL_DESCR("Neighbor discovery prune interval"), 3020 NULL, 0, &nd6_prune, 0, 3021 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3022 ICMPV6CTL_ND6_PRUNE, CTL_EOL); 3023 sysctl_createv(clog, 0, NULL, NULL, 3024 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3025 CTLTYPE_INT, "nd6_delay", 3026 SYSCTL_DESCR("First probe delay time"), 3027 NULL, 0, &nd6_delay, 0, 3028 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3029 ICMPV6CTL_ND6_DELAY, CTL_EOL); 3030 sysctl_createv(clog, 0, NULL, NULL, 3031 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3032 CTLTYPE_INT, "nd6_umaxtries", 3033 SYSCTL_DESCR("Number of unicast discovery attempts"), 3034 NULL, 0, &nd6_umaxtries, 0, 3035 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3036 ICMPV6CTL_ND6_UMAXTRIES, CTL_EOL); 3037 sysctl_createv(clog, 0, NULL, NULL, 3038 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3039 CTLTYPE_INT, "nd6_mmaxtries", 3040 SYSCTL_DESCR("Number of multicast discovery attempts"), 3041 NULL, 0, &nd6_mmaxtries, 0, 3042 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3043 ICMPV6CTL_ND6_MMAXTRIES, CTL_EOL); 3044 sysctl_createv(clog, 0, NULL, NULL, 3045 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3046 CTLTYPE_INT, "nd6_useloopback", 3047 SYSCTL_DESCR("Use loopback interface for local traffic"), 3048 NULL, 0, &nd6_useloopback, 0, 3049 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3050 ICMPV6CTL_ND6_USELOOPBACK, CTL_EOL); 3051#if 0 /* obsoleted */ 3052 sysctl_createv(clog, 0, NULL, NULL, 3053 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3054 CTLTYPE_INT, "nd6_proxyall", NULL, 3055 NULL, 0, &nd6_proxyall, 0, 3056 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3057 ICMPV6CTL_ND6_PROXYALL, CTL_EOL); 3058#endif 3059 sysctl_createv(clog, 0, NULL, NULL, 3060 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3061 CTLTYPE_INT, "nodeinfo", 3062 SYSCTL_DESCR("Respond to node information requests"), 3063 NULL, 0, &icmp6_nodeinfo, 0, 3064 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3065 ICMPV6CTL_NODEINFO, CTL_EOL); 3066 sysctl_createv(clog, 0, NULL, NULL, 3067 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3068 CTLTYPE_INT, "errppslimit", 3069 SYSCTL_DESCR("Maximum ICMP errors sent per second"), 3070 NULL, 0, &icmp6errppslim, 0, 3071 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3072 ICMPV6CTL_ERRPPSLIMIT, CTL_EOL); 3073 sysctl_createv(clog, 0, NULL, NULL, 3074 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3075 CTLTYPE_INT, "nd6_maxnudhint", 3076 SYSCTL_DESCR("Maximum neighbor unreachable hint count"), 3077 NULL, 0, &nd6_maxnudhint, 0, 3078 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3079 ICMPV6CTL_ND6_MAXNUDHINT, CTL_EOL); 3080 sysctl_createv(clog, 0, NULL, NULL, 3081 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3082 CTLTYPE_INT, "mtudisc_hiwat", 3083 SYSCTL_DESCR("Low mark on MTU Discovery route timers"), 3084 NULL, 0, &icmp6_mtudisc_hiwat, 0, 3085 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3086 ICMPV6CTL_MTUDISC_HIWAT, CTL_EOL); 3087 sysctl_createv(clog, 0, NULL, NULL, 3088 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3089 CTLTYPE_INT, "mtudisc_lowat", 3090 SYSCTL_DESCR("Low mark on MTU Discovery route timers"), 3091 NULL, 0, &icmp6_mtudisc_lowat, 0, 3092 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3093 ICMPV6CTL_MTUDISC_LOWAT, CTL_EOL); 3094 sysctl_createv(clog, 0, NULL, NULL, 3095 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3096 CTLTYPE_INT, "nd6_debug", 3097 SYSCTL_DESCR("Enable neighbor discovery debug output"), 3098 NULL, 0, &nd6_debug, 0, 3099 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3100 ICMPV6CTL_ND6_DEBUG, CTL_EOL); 3101 sysctl_createv(clog, 0, NULL, NULL, 3102 CTLFLAG_PERMANENT, 3103 CTLTYPE_STRUCT, "nd6_drlist", 3104 SYSCTL_DESCR("Default router list"), 3105 sysctl_net_inet6_icmp6_nd6, 0, NULL, 0, 3106 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3107 ICMPV6CTL_ND6_DRLIST, CTL_EOL); 3108 sysctl_createv(clog, 0, NULL, NULL, 3109 CTLFLAG_PERMANENT, 3110 CTLTYPE_STRUCT, "nd6_prlist", 3111 SYSCTL_DESCR("Prefix list"), 3112 sysctl_net_inet6_icmp6_nd6, 0, NULL, 0, 3113 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3114 ICMPV6CTL_ND6_PRLIST, CTL_EOL); 3115 sysctl_createv(clog, 0, NULL, NULL, 3116 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3117 CTLTYPE_INT, "maxqueuelen", 3118 SYSCTL_DESCR("max packet queue len for a unresolved ND"), 3119 NULL, 1, &nd6_maxqueuelen, 0, 3120 CTL_NET, PF_INET6, IPPROTO_ICMPV6, 3121 ICMPV6CTL_ND6_MAXQLEN, CTL_EOL); 3122} 3123 3124void 3125icmp6_statinc(u_int stat) 3126{ 3127 3128 KASSERT(stat < ICMP6_NSTATS); 3129 ICMP6_STATINC(stat); 3130} 3131