1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ 32 */ 33 34 /*- 35 * Copyright (c) 1982, 1986, 1988, 1993 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. Neither the name of the University nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 */ 62 63 #include <sys/cdefs.h> 64 #define MBUF_PRIVATE /* XXXRW: Optimisation tries to avoid M_EXT mbufs */ 65 66 #include "opt_inet.h" 67 #include "opt_inet6.h" 68 69 #include <sys/param.h> 70 #include <sys/domain.h> 71 #include <sys/jail.h> 72 #include <sys/kernel.h> 73 #include <sys/lock.h> 74 #include <sys/malloc.h> 75 #include <sys/mbuf.h> 76 #include <sys/proc.h> 77 #include <sys/protosw.h> 78 #include <sys/signalvar.h> 79 #include <sys/socket.h> 80 #include <sys/socketvar.h> 81 #include <sys/sx.h> 82 #include <sys/syslog.h> 83 #include <sys/systm.h> 84 #include <sys/time.h> 85 86 #include <net/if.h> 87 #include <net/if_var.h> 88 #include <net/if_dl.h> 89 #include <net/if_llatbl.h> 90 #include <net/if_private.h> 91 #include <net/if_types.h> 92 #include <net/route.h> 93 #include <net/route/route_ctl.h> 94 #include <net/route/nhop.h> 95 #include <net/vnet.h> 96 97 #include <netinet/in.h> 98 #include <netinet/in_pcb.h> 99 #include <netinet/in_var.h> 100 #include <netinet/ip6.h> 101 #include <netinet/icmp6.h> 102 #include <netinet/tcp_var.h> 103 104 #include <netinet6/in6_fib.h> 105 #include <netinet6/in6_ifattach.h> 106 #include <netinet6/in6_pcb.h> 107 #include <netinet6/ip6_var.h> 108 #include <netinet6/scope6_var.h> 109 #include <netinet6/mld6_var.h> 110 #include <netinet6/nd6.h> 111 #include <netinet6/send.h> 112 113 extern ip6proto_ctlinput_t *ip6_ctlprotox[]; 114 115 VNET_PCPUSTAT_DEFINE(struct icmp6stat, icmp6stat); 116 VNET_PCPUSTAT_SYSINIT(icmp6stat); 117 SYSCTL_VNET_PCPUSTAT(_net_inet6_icmp6, ICMPV6CTL_STATS, stats, 118 struct icmp6stat, icmp6stat, 119 "ICMPv6 statistics (struct icmp6stat, netinet/icmp6.h)"); 120 121 #ifdef VIMAGE 122 VNET_PCPUSTAT_SYSUNINIT(icmp6stat); 123 #endif /* VIMAGE */ 124 125 VNET_DEFINE_STATIC(int, icmp6_rediraccept) = 1; 126 #define V_icmp6_rediraccept VNET(icmp6_rediraccept) 127 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_REDIRACCEPT, rediraccept, 128 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(icmp6_rediraccept), 0, 129 "Accept ICMPv6 redirect messages"); 130 131 VNET_DEFINE_STATIC(int, icmp6_redirtimeout) = 10 * 60; /* 10 minutes */ 132 #define V_icmp6_redirtimeout VNET(icmp6_redirtimeout) 133 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_REDIRTIMEOUT, redirtimeout, 134 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(icmp6_redirtimeout), 0, 135 "Delay in seconds before expiring redirect route"); 136 137 VNET_DEFINE_STATIC(int, icmp6_nodeinfo) = 0; 138 #define V_icmp6_nodeinfo VNET(icmp6_nodeinfo) 139 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_NODEINFO, nodeinfo, 140 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(icmp6_nodeinfo), 0, 141 "Mask of enabled RFC4620 node information query types"); 142 143 VNET_DECLARE(struct inpcbinfo, ripcbinfo); 144 #define V_ripcbinfo VNET(ripcbinfo) 145 146 static void icmp6_errcount(int, int); 147 static int icmp6_rip6_input(struct mbuf **, int); 148 static void icmp6_reflect(struct mbuf *, size_t); 149 static const char *icmp6_redirect_diag(struct in6_addr *, 150 struct in6_addr *, struct in6_addr *); 151 static struct mbuf *ni6_input(struct mbuf *, int, struct prison *); 152 static struct mbuf *ni6_nametodns(const char *, int, int); 153 static int ni6_dnsmatch(const char *, int, const char *, int); 154 static int ni6_addrs(struct icmp6_nodeinfo *, struct mbuf *, 155 struct ifnet **, struct in6_addr *); 156 static int ni6_store_addrs(struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, 157 struct ifnet *, int); 158 static int icmp6_notify_error(struct mbuf **, int, int); 159 160 /* 161 * Kernel module interface for updating icmp6stat. The argument is an index 162 * into icmp6stat treated as an array of u_quad_t. While this encodes the 163 * general layout of icmp6stat into the caller, it doesn't encode its 164 * location, so that future changes to add, for example, per-CPU stats 165 * support won't cause binary compatibility problems for kernel modules. 166 */ 167 void 168 kmod_icmp6stat_inc(int statnum) 169 { 170 171 counter_u64_add(VNET(icmp6stat)[statnum], 1); 172 } 173 174 static void 175 icmp6_errcount(int type, int code) 176 { 177 switch (type) { 178 case ICMP6_DST_UNREACH: 179 switch (code) { 180 case ICMP6_DST_UNREACH_NOROUTE: 181 ICMP6STAT_INC(icp6s_odst_unreach_noroute); 182 return; 183 case ICMP6_DST_UNREACH_ADMIN: 184 ICMP6STAT_INC(icp6s_odst_unreach_admin); 185 return; 186 case ICMP6_DST_UNREACH_BEYONDSCOPE: 187 ICMP6STAT_INC(icp6s_odst_unreach_beyondscope); 188 return; 189 case ICMP6_DST_UNREACH_ADDR: 190 ICMP6STAT_INC(icp6s_odst_unreach_addr); 191 return; 192 case ICMP6_DST_UNREACH_NOPORT: 193 ICMP6STAT_INC(icp6s_odst_unreach_noport); 194 return; 195 } 196 break; 197 case ICMP6_PACKET_TOO_BIG: 198 ICMP6STAT_INC(icp6s_opacket_too_big); 199 return; 200 case ICMP6_TIME_EXCEEDED: 201 switch (code) { 202 case ICMP6_TIME_EXCEED_TRANSIT: 203 ICMP6STAT_INC(icp6s_otime_exceed_transit); 204 return; 205 case ICMP6_TIME_EXCEED_REASSEMBLY: 206 ICMP6STAT_INC(icp6s_otime_exceed_reassembly); 207 return; 208 } 209 break; 210 case ICMP6_PARAM_PROB: 211 switch (code) { 212 case ICMP6_PARAMPROB_HEADER: 213 ICMP6STAT_INC(icp6s_oparamprob_header); 214 return; 215 case ICMP6_PARAMPROB_NEXTHEADER: 216 ICMP6STAT_INC(icp6s_oparamprob_nextheader); 217 return; 218 case ICMP6_PARAMPROB_OPTION: 219 ICMP6STAT_INC(icp6s_oparamprob_option); 220 return; 221 } 222 break; 223 case ND_REDIRECT: 224 ICMP6STAT_INC(icp6s_oredirect); 225 return; 226 } 227 ICMP6STAT_INC(icp6s_ounknown); 228 } 229 230 /* 231 * A wrapper function for icmp6_error() necessary when the erroneous packet 232 * may not contain enough scope zone information. 233 */ 234 void 235 icmp6_error2(struct mbuf *m, int type, int code, int param, 236 struct ifnet *ifp) 237 { 238 struct ip6_hdr *ip6; 239 240 if (ifp == NULL) 241 return; 242 243 if (m->m_len < sizeof(struct ip6_hdr)) { 244 m = m_pullup(m, sizeof(struct ip6_hdr)); 245 if (m == NULL) { 246 IP6STAT_INC(ip6s_exthdrtoolong); 247 return; 248 } 249 } 250 ip6 = mtod(m, struct ip6_hdr *); 251 252 if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0) 253 return; 254 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) 255 return; 256 257 icmp6_error(m, type, code, param); 258 } 259 260 /* 261 * Generate an error packet of type error in response to bad IP6 packet. 262 */ 263 void 264 icmp6_error(struct mbuf *m, int type, int code, int param) 265 { 266 struct ip6_hdr *oip6, *nip6; 267 struct icmp6_hdr *icmp6; 268 struct epoch_tracker et; 269 u_int preplen; 270 int off; 271 int nxt; 272 273 ICMP6STAT_INC(icp6s_error); 274 275 /* count per-type-code statistics */ 276 icmp6_errcount(type, code); 277 278 #ifdef M_DECRYPTED /*not openbsd*/ 279 if (m->m_flags & M_DECRYPTED) { 280 ICMP6STAT_INC(icp6s_canterror); 281 goto freeit; 282 } 283 #endif 284 285 if (m->m_len < sizeof(struct ip6_hdr)) { 286 m = m_pullup(m, sizeof(struct ip6_hdr)); 287 if (m == NULL) { 288 IP6STAT_INC(ip6s_exthdrtoolong); 289 return; 290 } 291 } 292 oip6 = mtod(m, struct ip6_hdr *); 293 294 /* 295 * If the destination address of the erroneous packet is a multicast 296 * address, or the packet was sent using link-layer multicast, 297 * we should basically suppress sending an error (RFC 2463, Section 298 * 2.4). 299 * We have two exceptions (the item e.2 in that section): 300 * - the Packet Too Big message can be sent for path MTU discovery. 301 * - the Parameter Problem Message that can be allowed an icmp6 error 302 * in the option type field. This check has been done in 303 * ip6_unknown_opt(), so we can just check the type and code. 304 */ 305 if ((m->m_flags & (M_BCAST|M_MCAST) || 306 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && 307 (type != ICMP6_PACKET_TOO_BIG && 308 (type != ICMP6_PARAM_PROB || 309 code != ICMP6_PARAMPROB_OPTION))) 310 goto freeit; 311 312 /* 313 * RFC 2463, 2.4 (e.5): source address check. 314 * XXX: the case of anycast source? 315 */ 316 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || 317 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) 318 goto freeit; 319 320 /* 321 * If we are about to send ICMPv6 against ICMPv6 error/redirect, 322 * don't do it. 323 */ 324 nxt = -1; 325 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 326 if (off >= 0 && nxt == IPPROTO_ICMPV6) { 327 struct icmp6_hdr *icp; 328 329 if (m->m_len < off + sizeof(struct icmp6_hdr)) { 330 m = m_pullup(m, off + sizeof(struct icmp6_hdr)); 331 if (m == NULL) { 332 IP6STAT_INC(ip6s_exthdrtoolong); 333 return; 334 } 335 } 336 oip6 = mtod(m, struct ip6_hdr *); 337 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 338 339 if (icp->icmp6_type < ICMP6_ECHO_REQUEST || 340 icp->icmp6_type == ND_REDIRECT) { 341 /* 342 * ICMPv6 error 343 * Special case: for redirect (which is 344 * informational) we must not send icmp6 error. 345 */ 346 ICMP6STAT_INC(icp6s_canterror); 347 goto freeit; 348 } else { 349 /* ICMPv6 informational - send the error */ 350 } 351 } else { 352 /* non-ICMPv6 - send the error */ 353 } 354 355 /* Finally, do rate limitation check. */ 356 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) 357 goto freeit; 358 359 /* 360 * OK, ICMP6 can be generated. 361 */ 362 363 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) 364 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len); 365 366 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 367 M_PREPEND(m, preplen, M_NOWAIT); /* FIB is also copied over. */ 368 if (m == NULL) { 369 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__)); 370 return; 371 } 372 373 nip6 = mtod(m, struct ip6_hdr *); 374 nip6->ip6_src = oip6->ip6_src; 375 nip6->ip6_dst = oip6->ip6_dst; 376 377 in6_clearscope(&oip6->ip6_src); 378 in6_clearscope(&oip6->ip6_dst); 379 380 icmp6 = (struct icmp6_hdr *)(nip6 + 1); 381 icmp6->icmp6_type = type; 382 icmp6->icmp6_code = code; 383 icmp6->icmp6_pptr = htonl((u_int32_t)param); 384 385 ICMP6STAT_INC2(icp6s_outhist, type); 386 NET_EPOCH_ENTER(et); 387 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */ 388 NET_EPOCH_EXIT(et); 389 390 return; 391 392 freeit: 393 /* 394 * If we can't tell whether or not we can generate ICMP6, free it. 395 */ 396 m_freem(m); 397 } 398 399 int 400 icmp6_errmap(const struct icmp6_hdr *icmp6) 401 { 402 403 switch (icmp6->icmp6_type) { 404 case ICMP6_DST_UNREACH: 405 switch (icmp6->icmp6_code) { 406 case ICMP6_DST_UNREACH_NOROUTE: 407 case ICMP6_DST_UNREACH_ADDR: 408 return (EHOSTUNREACH); 409 case ICMP6_DST_UNREACH_NOPORT: 410 case ICMP6_DST_UNREACH_ADMIN: 411 return (ECONNREFUSED); 412 case ICMP6_DST_UNREACH_BEYONDSCOPE: 413 return (ENOPROTOOPT); 414 default: 415 return (0); /* Shouldn't happen. */ 416 } 417 case ICMP6_PACKET_TOO_BIG: 418 return (EMSGSIZE); 419 case ICMP6_TIME_EXCEEDED: 420 switch (icmp6->icmp6_code) { 421 case ICMP6_TIME_EXCEED_TRANSIT: 422 return (EHOSTUNREACH); 423 case ICMP6_TIME_EXCEED_REASSEMBLY: 424 return (0); 425 default: 426 return (0); /* Shouldn't happen. */ 427 } 428 case ICMP6_PARAM_PROB: 429 switch (icmp6->icmp6_code) { 430 case ICMP6_PARAMPROB_NEXTHEADER: 431 return (ECONNREFUSED); 432 case ICMP6_PARAMPROB_HEADER: 433 case ICMP6_PARAMPROB_OPTION: 434 return (ENOPROTOOPT); 435 default: 436 return (0); /* Shouldn't happen. */ 437 } 438 default: 439 return (0); 440 } 441 } 442 443 /* 444 * Process a received ICMP6 message. 445 */ 446 int 447 icmp6_input(struct mbuf **mp, int *offp, int proto) 448 { 449 struct mbuf *m, *n; 450 struct ifnet *ifp; 451 struct ip6_hdr *ip6, *nip6; 452 struct icmp6_hdr *icmp6, *nicmp6; 453 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 454 int code, error, icmp6len, ip6len, noff, off, sum; 455 456 NET_EPOCH_ASSERT(); 457 458 m = *mp; 459 off = *offp; 460 461 if (m->m_len < off + sizeof(struct icmp6_hdr)) { 462 m = m_pullup(m, off + sizeof(struct icmp6_hdr)); 463 if (m == NULL) { 464 IP6STAT_INC(ip6s_exthdrtoolong); 465 *mp = m; 466 return (IPPROTO_DONE); 467 } 468 } 469 470 /* 471 * Locate icmp6 structure in mbuf, and check 472 * that not corrupted and of at least minimum length 473 */ 474 475 icmp6len = m->m_pkthdr.len - off; 476 if (icmp6len < sizeof(struct icmp6_hdr)) { 477 ICMP6STAT_INC(icp6s_tooshort); 478 goto freeit; 479 } 480 481 ip6 = mtod(m, struct ip6_hdr *); 482 ifp = m->m_pkthdr.rcvif; 483 /* 484 * Check multicast group membership. 485 * Note: SSM filters are not applied for ICMPv6 traffic. 486 */ 487 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 488 struct in6_multi *inm; 489 490 inm = in6m_lookup(ifp, &ip6->ip6_dst); 491 if (inm == NULL) { 492 IP6STAT_INC(ip6s_notmember); 493 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 494 goto freeit; 495 } 496 } 497 498 /* Calculate the checksum. */ 499 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 500 code = icmp6->icmp6_code; 501 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) { 502 nd6log((LOG_ERR, 503 "ICMP6 checksum error(%d|%x) %s\n", 504 icmp6->icmp6_type, sum, 505 ip6_sprintf(ip6bufs, &ip6->ip6_src))); 506 ICMP6STAT_INC(icp6s_checksum); 507 goto freeit; 508 } 509 510 ICMP6STAT_INC2(icp6s_inhist, icmp6->icmp6_type); 511 icmp6_ifstat_inc(ifp, ifs6_in_msg); 512 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK) 513 icmp6_ifstat_inc(ifp, ifs6_in_error); 514 515 ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen); 516 switch (icmp6->icmp6_type) { 517 case ICMP6_DST_UNREACH: 518 icmp6_ifstat_inc(ifp, ifs6_in_dstunreach); 519 switch (code) { 520 case ICMP6_DST_UNREACH_ADMIN: 521 icmp6_ifstat_inc(ifp, ifs6_in_adminprohib); 522 case ICMP6_DST_UNREACH_NOROUTE: 523 case ICMP6_DST_UNREACH_ADDR: 524 case ICMP6_DST_UNREACH_BEYONDSCOPE: 525 case ICMP6_DST_UNREACH_NOPORT: 526 goto deliver; 527 default: 528 goto badcode; 529 } 530 case ICMP6_PACKET_TOO_BIG: 531 icmp6_ifstat_inc(ifp, ifs6_in_pkttoobig); 532 /* 533 * Validation is made in icmp6_mtudisc_update. 534 * Updating the path MTU will be done after examining 535 * intermediate extension headers. 536 */ 537 goto deliver; 538 case ICMP6_TIME_EXCEEDED: 539 icmp6_ifstat_inc(ifp, ifs6_in_timeexceed); 540 switch (code) { 541 case ICMP6_TIME_EXCEED_TRANSIT: 542 case ICMP6_TIME_EXCEED_REASSEMBLY: 543 goto deliver; 544 default: 545 goto badcode; 546 } 547 case ICMP6_PARAM_PROB: 548 icmp6_ifstat_inc(ifp, ifs6_in_paramprob); 549 switch (code) { 550 case ICMP6_PARAMPROB_NEXTHEADER: 551 case ICMP6_PARAMPROB_HEADER: 552 case ICMP6_PARAMPROB_OPTION: 553 goto deliver; 554 default: 555 goto badcode; 556 } 557 case ICMP6_ECHO_REQUEST: 558 icmp6_ifstat_inc(ifp, ifs6_in_echo); 559 if (code != 0) 560 goto badcode; 561 if (icmp6_ratelimit(&ip6->ip6_src, ICMP6_ECHO_REPLY, 0)) 562 break; 563 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) { 564 /* Give up remote */ 565 break; 566 } 567 if (!M_WRITABLE(n) 568 || n->m_len < off + sizeof(struct icmp6_hdr)) { 569 struct mbuf *n0 = n; 570 int n0len; 571 572 CTASSERT(sizeof(*nip6) + sizeof(*nicmp6) <= MHLEN); 573 n = m_gethdr(M_NOWAIT, n0->m_type); 574 if (n == NULL) { 575 /* Give up remote */ 576 m_freem(n0); 577 break; 578 } 579 580 m_move_pkthdr(n, n0); /* FIB copied. */ 581 n0len = n0->m_pkthdr.len; /* save for use below */ 582 /* 583 * Copy IPv6 and ICMPv6 only. 584 */ 585 nip6 = mtod(n, struct ip6_hdr *); 586 bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 587 nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 588 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 589 noff = sizeof(struct ip6_hdr); 590 /* new mbuf contains only ipv6+icmpv6 headers */ 591 n->m_len = noff + sizeof(struct icmp6_hdr); 592 /* 593 * Adjust mbuf. ip6_plen will be adjusted in 594 * ip6_output(). 595 */ 596 m_adj(n0, off + sizeof(struct icmp6_hdr)); 597 /* recalculate complete packet size */ 598 n->m_pkthdr.len = n0len + (noff - off); 599 n->m_next = n0; 600 } else { 601 if (n->m_len < off + sizeof(*nicmp6)) { 602 n = m_pullup(n, off + sizeof(*nicmp6)); 603 if (n == NULL) { 604 IP6STAT_INC(ip6s_exthdrtoolong); 605 break; 606 } 607 } 608 nicmp6 = (struct icmp6_hdr *)(mtod(n, caddr_t) + off); 609 noff = off; 610 } 611 if (n) { 612 nicmp6->icmp6_type = ICMP6_ECHO_REPLY; 613 nicmp6->icmp6_code = 0; 614 ICMP6STAT_INC(icp6s_reflect); 615 ICMP6STAT_INC2(icp6s_outhist, ICMP6_ECHO_REPLY); 616 icmp6_reflect(n, noff); 617 } 618 break; 619 620 case ICMP6_ECHO_REPLY: 621 icmp6_ifstat_inc(ifp, ifs6_in_echoreply); 622 if (code != 0) 623 goto badcode; 624 break; 625 626 case MLD_LISTENER_QUERY: 627 case MLD_LISTENER_REPORT: 628 case MLD_LISTENER_DONE: 629 case MLDV2_LISTENER_REPORT: 630 /* 631 * Drop MLD traffic which is not link-local, has a hop limit 632 * of greater than 1 hop, or which does not have the 633 * IPv6 HBH Router Alert option. 634 * As IPv6 HBH options are stripped in ip6_input() we must 635 * check an mbuf header flag. 636 * XXX Should we also sanity check that these messages 637 * were directed to a link-local multicast prefix? 638 */ 639 if ((ip6->ip6_hlim != 1) || (m->m_flags & M_RTALERT_MLD) == 0) 640 goto freeit; 641 if (mld_input(&m, off, icmp6len) != 0) { 642 *mp = NULL; 643 return (IPPROTO_DONE); 644 } 645 /* m stays. */ 646 break; 647 648 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */ 649 { 650 enum { WRU, FQDN } mode; 651 struct prison *pr; 652 653 if (!V_icmp6_nodeinfo) 654 break; 655 656 if (icmp6len == sizeof(struct icmp6_hdr) + 4) 657 mode = WRU; 658 else if (icmp6len >= sizeof(struct icmp6_nodeinfo)) 659 mode = FQDN; 660 else 661 goto badlen; 662 663 pr = NULL; 664 sx_slock(&allprison_lock); 665 TAILQ_FOREACH(pr, &allprison, pr_list) 666 if (pr->pr_vnet == ifp->if_vnet) 667 break; 668 sx_sunlock(&allprison_lock); 669 if (pr == NULL) 670 pr = curthread->td_ucred->cr_prison; 671 if (mode == FQDN) { 672 if (m->m_len < off + sizeof(struct icmp6_nodeinfo)) { 673 m = m_pullup(m, off + 674 sizeof(struct icmp6_nodeinfo)); 675 if (m == NULL) { 676 IP6STAT_INC(ip6s_exthdrtoolong); 677 *mp = m; 678 return (IPPROTO_DONE); 679 } 680 } 681 n = m_copym(m, 0, M_COPYALL, M_NOWAIT); 682 if (n) 683 n = ni6_input(n, off, pr); 684 /* XXX meaningless if n == NULL */ 685 noff = sizeof(struct ip6_hdr); 686 } else { 687 u_char *p; 688 int maxhlen, hlen; 689 690 /* 691 * XXX: this combination of flags is pointless, 692 * but should we keep this for compatibility? 693 */ 694 if ((V_icmp6_nodeinfo & (ICMP6_NODEINFO_FQDNOK | 695 ICMP6_NODEINFO_TMPADDROK)) != 696 (ICMP6_NODEINFO_FQDNOK | ICMP6_NODEINFO_TMPADDROK)) 697 break; 698 699 if (code != 0) 700 goto badcode; 701 702 CTASSERT(sizeof(*nip6) + sizeof(*nicmp6) + 4 <= MHLEN); 703 n = m_gethdr(M_NOWAIT, m->m_type); 704 if (n == NULL) { 705 /* Give up remote */ 706 break; 707 } 708 if (!m_dup_pkthdr(n, m, M_NOWAIT)) { 709 /* 710 * Previous code did a blind M_COPY_PKTHDR 711 * and said "just for rcvif". If true, then 712 * we could tolerate the dup failing (due to 713 * the deep copy of the tag chain). For now 714 * be conservative and just fail. 715 */ 716 m_free(n); 717 n = NULL; 718 break; 719 } 720 /* 721 * Copy IPv6 and ICMPv6 only. 722 */ 723 nip6 = mtod(n, struct ip6_hdr *); 724 bcopy(ip6, nip6, sizeof(struct ip6_hdr)); 725 nicmp6 = (struct icmp6_hdr *)(nip6 + 1); 726 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); 727 p = (u_char *)(nicmp6 + 1); 728 bzero(p, 4); 729 730 maxhlen = M_TRAILINGSPACE(n) - 731 (sizeof(*nip6) + sizeof(*nicmp6) + 4); 732 mtx_lock(&pr->pr_mtx); 733 hlen = strlen(pr->pr_hostname); 734 if (maxhlen > hlen) 735 maxhlen = hlen; 736 /* meaningless TTL */ 737 bcopy(pr->pr_hostname, p + 4, maxhlen); 738 mtx_unlock(&pr->pr_mtx); 739 noff = sizeof(struct ip6_hdr); 740 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 741 sizeof(struct icmp6_hdr) + 4 + maxhlen; 742 nicmp6->icmp6_type = ICMP6_WRUREPLY; 743 nicmp6->icmp6_code = 0; 744 } 745 if (n) { 746 ICMP6STAT_INC(icp6s_reflect); 747 ICMP6STAT_INC2(icp6s_outhist, ICMP6_WRUREPLY); 748 icmp6_reflect(n, noff); 749 } 750 break; 751 } 752 753 case ICMP6_WRUREPLY: 754 if (code != 0) 755 goto badcode; 756 break; 757 758 case ND_ROUTER_SOLICIT: 759 icmp6_ifstat_inc(ifp, ifs6_in_routersolicit); 760 if (code != 0) 761 goto badcode; 762 if (icmp6len < sizeof(struct nd_router_solicit)) 763 goto badlen; 764 if (send_sendso_input_hook != NULL) { 765 if (m->m_len < off + icmp6len) { 766 m = m_pullup(m, off + icmp6len); 767 if (m == NULL) { 768 IP6STAT_INC(ip6s_exthdrtoolong); 769 *mp = NULL; 770 return (IPPROTO_DONE); 771 } 772 } 773 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len); 774 if (error == 0) { 775 m = NULL; 776 goto freeit; 777 } 778 } 779 n = m_copym(m, 0, M_COPYALL, M_NOWAIT); 780 nd6_rs_input(m, off, icmp6len); 781 m = n; 782 if (m == NULL) 783 goto freeit; 784 break; 785 786 case ND_ROUTER_ADVERT: 787 icmp6_ifstat_inc(ifp, ifs6_in_routeradvert); 788 if (code != 0) 789 goto badcode; 790 if (icmp6len < sizeof(struct nd_router_advert)) 791 goto badlen; 792 if (send_sendso_input_hook != NULL) { 793 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len); 794 if (error == 0) { 795 m = NULL; 796 goto freeit; 797 } 798 } 799 n = m_copym(m, 0, M_COPYALL, M_NOWAIT); 800 nd6_ra_input(m, off, icmp6len); 801 m = n; 802 if (m == NULL) 803 goto freeit; 804 break; 805 806 case ND_NEIGHBOR_SOLICIT: 807 icmp6_ifstat_inc(ifp, ifs6_in_neighborsolicit); 808 if (code != 0) 809 goto badcode; 810 if (icmp6len < sizeof(struct nd_neighbor_solicit)) 811 goto badlen; 812 if (send_sendso_input_hook != NULL) { 813 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len); 814 if (error == 0) { 815 m = NULL; 816 goto freeit; 817 } 818 } 819 n = m_copym(m, 0, M_COPYALL, M_NOWAIT); 820 nd6_ns_input(m, off, icmp6len); 821 m = n; 822 if (m == NULL) 823 goto freeit; 824 break; 825 826 case ND_NEIGHBOR_ADVERT: 827 icmp6_ifstat_inc(ifp, ifs6_in_neighboradvert); 828 if (code != 0) 829 goto badcode; 830 if (icmp6len < sizeof(struct nd_neighbor_advert)) 831 goto badlen; 832 if (send_sendso_input_hook != NULL) { 833 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len); 834 if (error == 0) { 835 m = NULL; 836 goto freeit; 837 } 838 } 839 n = m_copym(m, 0, M_COPYALL, M_NOWAIT); 840 nd6_na_input(m, off, icmp6len); 841 m = n; 842 if (m == NULL) 843 goto freeit; 844 break; 845 846 case ND_REDIRECT: 847 icmp6_ifstat_inc(ifp, ifs6_in_redirect); 848 if (code != 0) 849 goto badcode; 850 if (icmp6len < sizeof(struct nd_redirect)) 851 goto badlen; 852 if (send_sendso_input_hook != NULL) { 853 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len); 854 if (error == 0) { 855 m = NULL; 856 goto freeit; 857 } 858 } 859 n = m_copym(m, 0, M_COPYALL, M_NOWAIT); 860 icmp6_redirect_input(m, off); 861 m = n; 862 if (m == NULL) 863 goto freeit; 864 break; 865 866 case ICMP6_ROUTER_RENUMBERING: 867 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND && 868 code != ICMP6_ROUTER_RENUMBERING_RESULT) 869 goto badcode; 870 if (icmp6len < sizeof(struct icmp6_router_renum)) 871 goto badlen; 872 break; 873 874 default: 875 nd6log((LOG_DEBUG, 876 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n", 877 icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src), 878 ip6_sprintf(ip6bufd, &ip6->ip6_dst), 879 ifp ? ifp->if_index : 0)); 880 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) { 881 /* ICMPv6 error: MUST deliver it by spec... */ 882 goto deliver; 883 } else { 884 /* ICMPv6 informational: MUST not deliver */ 885 break; 886 } 887 deliver: 888 if (icmp6_notify_error(&m, off, icmp6len) != 0) { 889 /* In this case, m should've been freed. */ 890 *mp = NULL; 891 return (IPPROTO_DONE); 892 } 893 break; 894 895 badcode: 896 ICMP6STAT_INC(icp6s_badcode); 897 break; 898 899 badlen: 900 ICMP6STAT_INC(icp6s_badlen); 901 break; 902 } 903 904 /* deliver the packet to appropriate sockets */ 905 icmp6_rip6_input(&m, *offp); 906 907 *mp = m; 908 return (IPPROTO_DONE); 909 910 freeit: 911 m_freem(m); 912 *mp = NULL; 913 return (IPPROTO_DONE); 914 } 915 916 static int 917 icmp6_notify_error(struct mbuf **mp, int off, int icmp6len) 918 { 919 struct mbuf *m; 920 struct icmp6_hdr *icmp6; 921 struct ip6_hdr *eip6; 922 u_int32_t notifymtu; 923 struct sockaddr_in6 icmp6src, icmp6dst; 924 925 m = *mp; 926 927 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) { 928 ICMP6STAT_INC(icp6s_tooshort); 929 goto freeit; 930 } 931 932 if (m->m_len < off + sizeof(*icmp6) + sizeof(struct ip6_hdr)) { 933 m = m_pullup(m, off + sizeof(*icmp6) + sizeof(struct ip6_hdr)); 934 if (m == NULL) { 935 IP6STAT_INC(ip6s_exthdrtoolong); 936 *mp = m; 937 return (-1); 938 } 939 } 940 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 941 eip6 = (struct ip6_hdr *)(icmp6 + 1); 942 bzero(&icmp6dst, sizeof(icmp6dst)); 943 944 /* Detect the upper level protocol */ 945 { 946 u_int8_t nxt = eip6->ip6_nxt; 947 int eoff = off + sizeof(struct icmp6_hdr) + 948 sizeof(struct ip6_hdr); 949 struct ip6ctlparam ip6cp; 950 int icmp6type = icmp6->icmp6_type; 951 struct ip6_frag *fh; 952 struct ip6_rthdr *rth; 953 struct ip6_rthdr0 *rth0; 954 int rthlen; 955 956 while (1) { /* XXX: should avoid infinite loop explicitly? */ 957 struct ip6_ext *eh; 958 959 switch (nxt) { 960 case IPPROTO_HOPOPTS: 961 case IPPROTO_DSTOPTS: 962 case IPPROTO_AH: 963 if (m->m_len < eoff + sizeof(struct ip6_ext)) { 964 m = m_pullup(m, eoff + 965 sizeof(struct ip6_ext)); 966 if (m == NULL) { 967 IP6STAT_INC(ip6s_exthdrtoolong); 968 *mp = m; 969 return (-1); 970 } 971 } 972 eh = (struct ip6_ext *) 973 (mtod(m, caddr_t) + eoff); 974 if (nxt == IPPROTO_AH) 975 eoff += (eh->ip6e_len + 2) << 2; 976 else 977 eoff += (eh->ip6e_len + 1) << 3; 978 nxt = eh->ip6e_nxt; 979 break; 980 case IPPROTO_ROUTING: 981 /* 982 * When the erroneous packet contains a 983 * routing header, we should examine the 984 * header to determine the final destination. 985 * Otherwise, we can't properly update 986 * information that depends on the final 987 * destination (e.g. path MTU). 988 */ 989 if (m->m_len < eoff + sizeof(*rth)) { 990 m = m_pullup(m, eoff + sizeof(*rth)); 991 if (m == NULL) { 992 IP6STAT_INC(ip6s_exthdrtoolong); 993 *mp = m; 994 return (-1); 995 } 996 } 997 rth = (struct ip6_rthdr *) 998 (mtod(m, caddr_t) + eoff); 999 rthlen = (rth->ip6r_len + 1) << 3; 1000 /* 1001 * XXX: currently there is no 1002 * officially defined type other 1003 * than type-0. 1004 * Note that if the segment left field 1005 * is 0, all intermediate hops must 1006 * have been passed. 1007 */ 1008 if (rth->ip6r_segleft && 1009 rth->ip6r_type == IPV6_RTHDR_TYPE_0) { 1010 int hops; 1011 1012 if (m->m_len < eoff + rthlen) { 1013 m = m_pullup(m, eoff + rthlen); 1014 if (m == NULL) { 1015 IP6STAT_INC( 1016 ip6s_exthdrtoolong); 1017 *mp = m; 1018 return (-1); 1019 } 1020 } 1021 rth0 = (struct ip6_rthdr0 *) 1022 (mtod(m, caddr_t) + eoff); 1023 1024 /* just ignore a bogus header */ 1025 if ((rth0->ip6r0_len % 2) == 0 && 1026 (hops = rth0->ip6r0_len/2)) 1027 icmp6dst.sin6_addr = *((struct in6_addr *)(rth0 + 1) + (hops - 1)); 1028 } 1029 eoff += rthlen; 1030 nxt = rth->ip6r_nxt; 1031 break; 1032 case IPPROTO_FRAGMENT: 1033 if (m->m_len < eoff + sizeof(struct ip6_frag)) { 1034 m = m_pullup(m, eoff + 1035 sizeof(struct ip6_frag)); 1036 if (m == NULL) { 1037 IP6STAT_INC(ip6s_exthdrtoolong); 1038 *mp = m; 1039 return (-1); 1040 } 1041 } 1042 fh = (struct ip6_frag *)(mtod(m, caddr_t) + 1043 eoff); 1044 /* 1045 * Data after a fragment header is meaningless 1046 * unless it is the first fragment, but 1047 * we'll go to the notify label for path MTU 1048 * discovery. 1049 */ 1050 if (fh->ip6f_offlg & IP6F_OFF_MASK) 1051 goto notify; 1052 1053 eoff += sizeof(struct ip6_frag); 1054 nxt = fh->ip6f_nxt; 1055 break; 1056 default: 1057 /* 1058 * This case includes ESP and the No Next 1059 * Header. In such cases going to the notify 1060 * label does not have any meaning 1061 * (i.e. ctlfunc will be NULL), but we go 1062 * anyway since we might have to update 1063 * path MTU information. 1064 */ 1065 goto notify; 1066 } 1067 } 1068 notify: 1069 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); 1070 1071 /* 1072 * retrieve parameters from the inner IPv6 header, and convert 1073 * them into sockaddr structures. 1074 * XXX: there is no guarantee that the source or destination 1075 * addresses of the inner packet are in the same scope as 1076 * the addresses of the icmp packet. But there is no other 1077 * way to determine the zone. 1078 */ 1079 eip6 = (struct ip6_hdr *)(icmp6 + 1); 1080 1081 /* 1082 * Protocol layers can't do anything useful with unspecified 1083 * addresses. 1084 */ 1085 if (IN6_IS_ADDR_UNSPECIFIED(&eip6->ip6_src) || 1086 IN6_IS_ADDR_UNSPECIFIED(&eip6->ip6_dst)) 1087 goto freeit; 1088 1089 icmp6dst.sin6_len = sizeof(struct sockaddr_in6); 1090 icmp6dst.sin6_family = AF_INET6; 1091 if (IN6_IS_ADDR_UNSPECIFIED(&icmp6dst.sin6_addr)) 1092 icmp6dst.sin6_addr = eip6->ip6_dst; 1093 if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL)) 1094 goto freeit; 1095 bzero(&icmp6src, sizeof(icmp6src)); 1096 icmp6src.sin6_len = sizeof(struct sockaddr_in6); 1097 icmp6src.sin6_family = AF_INET6; 1098 icmp6src.sin6_addr = eip6->ip6_src; 1099 if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL)) 1100 goto freeit; 1101 icmp6src.sin6_flowinfo = 1102 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); 1103 1104 ip6cp.ip6c_m = m; 1105 ip6cp.ip6c_icmp6 = icmp6; 1106 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1); 1107 ip6cp.ip6c_off = eoff; 1108 ip6cp.ip6c_finaldst = &icmp6dst; 1109 ip6cp.ip6c_src = &icmp6src; 1110 ip6cp.ip6c_nxt = nxt; 1111 1112 if (icmp6type == ICMP6_PACKET_TOO_BIG) { 1113 notifymtu = ntohl(icmp6->icmp6_mtu); 1114 ip6cp.ip6c_cmdarg = (void *)¬ifymtu; 1115 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/ 1116 } 1117 1118 if (ip6_ctlprotox[nxt] != NULL) 1119 ip6_ctlprotox[nxt](&ip6cp); 1120 } 1121 *mp = m; 1122 return (0); 1123 1124 freeit: 1125 m_freem(m); 1126 *mp = NULL; 1127 return (-1); 1128 } 1129 1130 void 1131 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated) 1132 { 1133 struct in6_addr *dst = &ip6cp->ip6c_finaldst->sin6_addr; 1134 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6; 1135 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */ 1136 u_int mtu = ntohl(icmp6->icmp6_mtu); 1137 struct in_conninfo inc; 1138 uint32_t max_mtu; 1139 1140 #if 0 1141 /* 1142 * RFC2460 section 5, last paragraph. 1143 * even though minimum link MTU for IPv6 is IPV6_MMTU, 1144 * we may see ICMPv6 too big with mtu < IPV6_MMTU 1145 * due to packet translator in the middle. 1146 * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for 1147 * special handling. 1148 */ 1149 if (mtu < IPV6_MMTU) 1150 return; 1151 #endif 1152 1153 /* 1154 * we reject ICMPv6 too big with abnormally small value. 1155 * XXX what is the good definition of "abnormally small"? 1156 */ 1157 if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8) 1158 return; 1159 1160 if (!validated) 1161 return; 1162 1163 /* 1164 * In case the suggested mtu is less than IPV6_MMTU, we 1165 * only need to remember that it was for above mentioned 1166 * "alwaysfrag" case. 1167 * Try to be as close to the spec as possible. 1168 */ 1169 if (mtu < IPV6_MMTU) 1170 mtu = IPV6_MMTU - 8; 1171 1172 bzero(&inc, sizeof(inc)); 1173 inc.inc_fibnum = M_GETFIB(m); 1174 inc.inc_flags |= INC_ISIPV6; 1175 inc.inc6_faddr = *dst; 1176 if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL)) 1177 return; 1178 1179 max_mtu = tcp_hc_getmtu(&inc); 1180 if (max_mtu == 0) 1181 max_mtu = tcp_maxmtu6(&inc, NULL); 1182 1183 if (mtu < max_mtu) { 1184 tcp_hc_updatemtu(&inc, mtu); 1185 ICMP6STAT_INC(icp6s_pmtuchg); 1186 } 1187 } 1188 1189 /* 1190 * Process a Node Information Query packet, based on 1191 * draft-ietf-ipngwg-icmp-name-lookups-07. 1192 * 1193 * Spec incompatibilities: 1194 * - IPv6 Subject address handling 1195 * - IPv4 Subject address handling support missing 1196 * - Proxy reply (answer even if it's not for me) 1197 * - joins NI group address at in6_ifattach() time only, does not cope 1198 * with hostname changes by sethostname(3) 1199 */ 1200 static struct mbuf * 1201 ni6_input(struct mbuf *m, int off, struct prison *pr) 1202 { 1203 struct icmp6_nodeinfo *ni6, *nni6; 1204 struct mbuf *n = NULL; 1205 u_int16_t qtype; 1206 int subjlen; 1207 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 1208 struct ni_reply_fqdn *fqdn; 1209 int addrs; /* for NI_QTYPE_NODEADDR */ 1210 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ 1211 struct in6_addr in6_subj; /* subject address */ 1212 struct ip6_hdr *ip6; 1213 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ 1214 char *subj = NULL; 1215 struct in6_ifaddr *ia6 = NULL; 1216 1217 ip6 = mtod(m, struct ip6_hdr *); 1218 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); 1219 1220 /* 1221 * Validate IPv6 source address. 1222 * The default configuration MUST be to refuse answering queries from 1223 * global-scope addresses according to RFC4602. 1224 * Notes: 1225 * - it's not very clear what "refuse" means; this implementation 1226 * simply drops it. 1227 * - it's not very easy to identify global-scope (unicast) addresses 1228 * since there are many prefixes for them. It should be safer 1229 * and in practice sufficient to check "all" but loopback and 1230 * link-local (note that site-local unicast was deprecated and 1231 * ULA is defined as global scope-wise) 1232 */ 1233 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 && 1234 !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) && 1235 !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) 1236 goto bad; 1237 1238 /* 1239 * Validate IPv6 destination address. 1240 * 1241 * The Responder must discard the Query without further processing 1242 * unless it is one of the Responder's unicast or anycast addresses, or 1243 * a link-local scope multicast address which the Responder has joined. 1244 * [RFC4602, Section 5.] 1245 */ 1246 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 1247 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) 1248 goto bad; 1249 /* else it's a link-local multicast, fine */ 1250 } else { /* unicast or anycast */ 1251 ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false); 1252 if (ia6 == NULL) 1253 goto bad; /* XXX impossible */ 1254 1255 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) && 1256 !(V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) { 1257 nd6log((LOG_DEBUG, "ni6_input: ignore node info to " 1258 "a temporary address in %s:%d", 1259 __FILE__, __LINE__)); 1260 goto bad; 1261 } 1262 } 1263 1264 /* validate query Subject field. */ 1265 qtype = ntohs(ni6->ni_qtype); 1266 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); 1267 switch (qtype) { 1268 case NI_QTYPE_NOOP: 1269 case NI_QTYPE_SUPTYPES: 1270 /* 07 draft */ 1271 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) 1272 break; 1273 /* FALLTHROUGH */ 1274 case NI_QTYPE_FQDN: 1275 case NI_QTYPE_NODEADDR: 1276 case NI_QTYPE_IPV4ADDR: 1277 switch (ni6->ni_code) { 1278 case ICMP6_NI_SUBJ_IPV6: 1279 #if ICMP6_NI_SUBJ_IPV6 != 0 1280 case 0: 1281 #endif 1282 /* 1283 * backward compatibility - try to accept 03 draft 1284 * format, where no Subject is present. 1285 */ 1286 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && 1287 subjlen == 0) { 1288 oldfqdn++; 1289 break; 1290 } 1291 #if ICMP6_NI_SUBJ_IPV6 != 0 1292 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) 1293 goto bad; 1294 #endif 1295 1296 if (subjlen != sizeof(struct in6_addr)) 1297 goto bad; 1298 1299 /* 1300 * Validate Subject address. 1301 * 1302 * Not sure what exactly "address belongs to the node" 1303 * means in the spec, is it just unicast, or what? 1304 * 1305 * At this moment we consider Subject address as 1306 * "belong to the node" if the Subject address equals 1307 * to the IPv6 destination address; validation for 1308 * IPv6 destination address should have done enough 1309 * check for us. 1310 * 1311 * We do not do proxy at this moment. 1312 */ 1313 m_copydata(m, off + sizeof(struct icmp6_nodeinfo), 1314 subjlen, (caddr_t)&in6_subj); 1315 if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL)) 1316 goto bad; 1317 1318 subj = (char *)&in6_subj; 1319 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj)) 1320 break; 1321 1322 /* 1323 * XXX if we are to allow other cases, we should really 1324 * be careful about scope here. 1325 * basically, we should disallow queries toward IPv6 1326 * destination X with subject Y, 1327 * if scope(X) > scope(Y). 1328 * if we allow scope(X) > scope(Y), it will result in 1329 * information leakage across scope boundary. 1330 */ 1331 goto bad; 1332 1333 case ICMP6_NI_SUBJ_FQDN: 1334 /* 1335 * Validate Subject name with gethostname(3). 1336 * 1337 * The behavior may need some debate, since: 1338 * - we are not sure if the node has FQDN as 1339 * hostname (returned by gethostname(3)). 1340 * - the code does wildcard match for truncated names. 1341 * however, we are not sure if we want to perform 1342 * wildcard match, if gethostname(3) side has 1343 * truncated hostname. 1344 */ 1345 mtx_lock(&pr->pr_mtx); 1346 n = ni6_nametodns(pr->pr_hostname, 1347 strlen(pr->pr_hostname), 0); 1348 mtx_unlock(&pr->pr_mtx); 1349 if (!n || n->m_next || n->m_len == 0) 1350 goto bad; 1351 if (m->m_len < off + sizeof(struct icmp6_nodeinfo) + 1352 subjlen) { 1353 m = m_pullup(m, off + 1354 sizeof(struct icmp6_nodeinfo) + subjlen); 1355 if (m == NULL) { 1356 IP6STAT_INC(ip6s_exthdrtoolong); 1357 goto bad; 1358 } 1359 } 1360 /* ip6 possibly invalid but not used after. */ 1361 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); 1362 subj = (char *)(mtod(m, caddr_t) + off + 1363 sizeof(struct icmp6_nodeinfo)); 1364 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), 1365 n->m_len)) { 1366 goto bad; 1367 } 1368 m_freem(n); 1369 n = NULL; 1370 break; 1371 1372 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ 1373 default: 1374 goto bad; 1375 } 1376 break; 1377 } 1378 1379 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ 1380 switch (qtype) { 1381 case NI_QTYPE_FQDN: 1382 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0) 1383 goto bad; 1384 break; 1385 case NI_QTYPE_NODEADDR: 1386 case NI_QTYPE_IPV4ADDR: 1387 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0) 1388 goto bad; 1389 break; 1390 } 1391 1392 /* guess reply length */ 1393 switch (qtype) { 1394 case NI_QTYPE_NOOP: 1395 break; /* no reply data */ 1396 case NI_QTYPE_SUPTYPES: 1397 replylen += sizeof(u_int32_t); 1398 break; 1399 case NI_QTYPE_FQDN: 1400 /* XXX will append an mbuf */ 1401 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 1402 break; 1403 case NI_QTYPE_NODEADDR: 1404 addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj); 1405 if ((replylen += addrs * (sizeof(struct in6_addr) + 1406 sizeof(u_int32_t))) > MCLBYTES) 1407 replylen = MCLBYTES; /* XXX: will truncate pkt later */ 1408 break; 1409 case NI_QTYPE_IPV4ADDR: 1410 /* unsupported - should respond with unknown Qtype? */ 1411 break; 1412 default: 1413 /* 1414 * XXX: We must return a reply with the ICMP6 code 1415 * `unknown Qtype' in this case. However we regard the case 1416 * as an FQDN query for backward compatibility. 1417 * Older versions set a random value to this field, 1418 * so it rarely varies in the defined qtypes. 1419 * But the mechanism is not reliable... 1420 * maybe we should obsolete older versions. 1421 */ 1422 qtype = NI_QTYPE_FQDN; 1423 /* XXX will append an mbuf */ 1424 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); 1425 oldfqdn++; 1426 break; 1427 } 1428 1429 /* Allocate an mbuf to reply. */ 1430 if (replylen > MCLBYTES) { 1431 /* 1432 * XXX: should we try to allocate more? But MCLBYTES 1433 * is probably much larger than IPV6_MMTU... 1434 */ 1435 goto bad; 1436 } 1437 if (replylen > MHLEN) 1438 n = m_getcl(M_NOWAIT, m->m_type, M_PKTHDR); 1439 else 1440 n = m_gethdr(M_NOWAIT, m->m_type); 1441 if (n == NULL) { 1442 m_freem(m); 1443 return (NULL); 1444 } 1445 m_move_pkthdr(n, m); /* just for recvif and FIB */ 1446 n->m_pkthdr.len = n->m_len = replylen; 1447 1448 /* copy mbuf header and IPv6 + Node Information base headers */ 1449 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); 1450 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); 1451 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); 1452 1453 /* qtype dependent procedure */ 1454 switch (qtype) { 1455 case NI_QTYPE_NOOP: 1456 nni6->ni_code = ICMP6_NI_SUCCESS; 1457 nni6->ni_flags = 0; 1458 break; 1459 case NI_QTYPE_SUPTYPES: 1460 { 1461 u_int32_t v; 1462 nni6->ni_code = ICMP6_NI_SUCCESS; 1463 nni6->ni_flags = htons(0x0000); /* raw bitmap */ 1464 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ 1465 v = (u_int32_t)htonl(0x0000000f); 1466 bcopy(&v, nni6 + 1, sizeof(u_int32_t)); 1467 break; 1468 } 1469 case NI_QTYPE_FQDN: 1470 nni6->ni_code = ICMP6_NI_SUCCESS; 1471 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + 1472 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo)); 1473 nni6->ni_flags = 0; /* XXX: meaningless TTL */ 1474 fqdn->ni_fqdn_ttl = 0; /* ditto. */ 1475 /* 1476 * XXX do we really have FQDN in hostname? 1477 */ 1478 mtx_lock(&pr->pr_mtx); 1479 n->m_next = ni6_nametodns(pr->pr_hostname, 1480 strlen(pr->pr_hostname), oldfqdn); 1481 mtx_unlock(&pr->pr_mtx); 1482 if (n->m_next == NULL) 1483 goto bad; 1484 /* XXX we assume that n->m_next is not a chain */ 1485 if (n->m_next->m_next != NULL) 1486 goto bad; 1487 n->m_pkthdr.len += n->m_next->m_len; 1488 break; 1489 case NI_QTYPE_NODEADDR: 1490 { 1491 int lenlim, copied; 1492 1493 nni6->ni_code = ICMP6_NI_SUCCESS; 1494 n->m_pkthdr.len = n->m_len = 1495 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); 1496 lenlim = M_TRAILINGSPACE(n); 1497 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); 1498 /* XXX: reset mbuf length */ 1499 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + 1500 sizeof(struct icmp6_nodeinfo) + copied; 1501 break; 1502 } 1503 default: 1504 break; /* XXX impossible! */ 1505 } 1506 1507 nni6->ni_type = ICMP6_NI_REPLY; 1508 m_freem(m); 1509 return (n); 1510 1511 bad: 1512 m_freem(m); 1513 if (n) 1514 m_freem(n); 1515 return (NULL); 1516 } 1517 1518 /* 1519 * make a mbuf with DNS-encoded string. no compression support. 1520 * 1521 * XXX names with less than 2 dots (like "foo" or "foo.section") will be 1522 * treated as truncated name (two \0 at the end). this is a wild guess. 1523 * 1524 * old - return pascal string if non-zero 1525 */ 1526 static struct mbuf * 1527 ni6_nametodns(const char *name, int namelen, int old) 1528 { 1529 struct mbuf *m; 1530 char *cp, *ep; 1531 const char *p, *q; 1532 int i, len, nterm; 1533 1534 if (old) 1535 len = namelen + 1; 1536 else 1537 len = MCLBYTES; 1538 1539 /* Because MAXHOSTNAMELEN is usually 256, we use cluster mbuf. */ 1540 if (len > MLEN) 1541 m = m_getcl(M_NOWAIT, MT_DATA, 0); 1542 else 1543 m = m_get(M_NOWAIT, MT_DATA); 1544 if (m == NULL) 1545 goto fail; 1546 1547 if (old) { 1548 m->m_len = len; 1549 *mtod(m, char *) = namelen; 1550 bcopy(name, mtod(m, char *) + 1, namelen); 1551 return m; 1552 } else { 1553 m->m_len = 0; 1554 cp = mtod(m, char *); 1555 ep = mtod(m, char *) + M_TRAILINGSPACE(m); 1556 1557 /* if not certain about my name, return empty buffer */ 1558 if (namelen == 0) 1559 return m; 1560 1561 /* 1562 * guess if it looks like shortened hostname, or FQDN. 1563 * shortened hostname needs two trailing "\0". 1564 */ 1565 i = 0; 1566 for (p = name; p < name + namelen; p++) { 1567 if (*p && *p == '.') 1568 i++; 1569 } 1570 if (i < 2) 1571 nterm = 2; 1572 else 1573 nterm = 1; 1574 1575 p = name; 1576 while (cp < ep && p < name + namelen) { 1577 i = 0; 1578 for (q = p; q < name + namelen && *q && *q != '.'; q++) 1579 i++; 1580 /* result does not fit into mbuf */ 1581 if (cp + i + 1 >= ep) 1582 goto fail; 1583 /* 1584 * DNS label length restriction, RFC1035 page 8. 1585 * "i == 0" case is included here to avoid returning 1586 * 0-length label on "foo..bar". 1587 */ 1588 if (i <= 0 || i >= 64) 1589 goto fail; 1590 *cp++ = i; 1591 bcopy(p, cp, i); 1592 cp += i; 1593 p = q; 1594 if (p < name + namelen && *p == '.') 1595 p++; 1596 } 1597 /* termination */ 1598 if (cp + nterm >= ep) 1599 goto fail; 1600 while (nterm-- > 0) 1601 *cp++ = '\0'; 1602 m->m_len = cp - mtod(m, char *); 1603 return m; 1604 } 1605 1606 panic("should not reach here"); 1607 /* NOTREACHED */ 1608 1609 fail: 1610 if (m) 1611 m_freem(m); 1612 return NULL; 1613 } 1614 1615 /* 1616 * check if two DNS-encoded string matches. takes care of truncated 1617 * form (with \0\0 at the end). no compression support. 1618 * XXX upper/lowercase match (see RFC2065) 1619 */ 1620 static int 1621 ni6_dnsmatch(const char *a, int alen, const char *b, int blen) 1622 { 1623 const char *a0, *b0; 1624 int l; 1625 1626 /* simplest case - need validation? */ 1627 if (alen == blen && bcmp(a, b, alen) == 0) 1628 return 1; 1629 1630 a0 = a; 1631 b0 = b; 1632 1633 /* termination is mandatory */ 1634 if (alen < 2 || blen < 2) 1635 return 0; 1636 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') 1637 return 0; 1638 alen--; 1639 blen--; 1640 1641 while (a - a0 < alen && b - b0 < blen) { 1642 if (a - a0 + 1 > alen || b - b0 + 1 > blen) 1643 return 0; 1644 1645 if ((signed char)a[0] < 0 || (signed char)b[0] < 0) 1646 return 0; 1647 /* we don't support compression yet */ 1648 if (a[0] >= 64 || b[0] >= 64) 1649 return 0; 1650 1651 /* truncated case */ 1652 if (a[0] == 0 && a - a0 == alen - 1) 1653 return 1; 1654 if (b[0] == 0 && b - b0 == blen - 1) 1655 return 1; 1656 if (a[0] == 0 || b[0] == 0) 1657 return 0; 1658 1659 if (a[0] != b[0]) 1660 return 0; 1661 l = a[0]; 1662 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) 1663 return 0; 1664 if (bcmp(a + 1, b + 1, l) != 0) 1665 return 0; 1666 1667 a += 1 + l; 1668 b += 1 + l; 1669 } 1670 1671 if (a - a0 == alen && b - b0 == blen) 1672 return 1; 1673 else 1674 return 0; 1675 } 1676 1677 /* 1678 * calculate the number of addresses to be returned in the node info reply. 1679 */ 1680 static int 1681 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp, 1682 struct in6_addr *subj) 1683 { 1684 struct ifnet *ifp; 1685 struct in6_ifaddr *ifa6; 1686 struct ifaddr *ifa; 1687 int addrs = 0, addrsofif, iffound = 0; 1688 int niflags = ni6->ni_flags; 1689 1690 NET_EPOCH_ASSERT(); 1691 1692 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { 1693 switch (ni6->ni_code) { 1694 case ICMP6_NI_SUBJ_IPV6: 1695 if (subj == NULL) /* must be impossible... */ 1696 return (0); 1697 break; 1698 default: 1699 /* 1700 * XXX: we only support IPv6 subject address for 1701 * this Qtype. 1702 */ 1703 return (0); 1704 } 1705 } 1706 1707 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1708 addrsofif = 0; 1709 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1710 if (ifa->ifa_addr->sa_family != AF_INET6) 1711 continue; 1712 ifa6 = (struct in6_ifaddr *)ifa; 1713 1714 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && 1715 IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr)) 1716 iffound = 1; 1717 1718 /* 1719 * IPv4-mapped addresses can only be returned by a 1720 * Node Information proxy, since they represent 1721 * addresses of IPv4-only nodes, which perforce do 1722 * not implement this protocol. 1723 * [icmp-name-lookups-07, Section 5.4] 1724 * So we don't support NI_NODEADDR_FLAG_COMPAT in 1725 * this function at this moment. 1726 */ 1727 1728 /* What do we have to do about ::1? */ 1729 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 1730 case IPV6_ADDR_SCOPE_LINKLOCAL: 1731 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 1732 continue; 1733 break; 1734 case IPV6_ADDR_SCOPE_SITELOCAL: 1735 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 1736 continue; 1737 break; 1738 case IPV6_ADDR_SCOPE_GLOBAL: 1739 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 1740 continue; 1741 break; 1742 default: 1743 continue; 1744 } 1745 1746 /* 1747 * check if anycast is okay. 1748 * XXX: just experimental. not in the spec. 1749 */ 1750 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 1751 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 1752 continue; /* we need only unicast addresses */ 1753 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1754 (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { 1755 continue; 1756 } 1757 addrsofif++; /* count the address */ 1758 } 1759 if (iffound) { 1760 *ifpp = ifp; 1761 return (addrsofif); 1762 } 1763 1764 addrs += addrsofif; 1765 } 1766 1767 return (addrs); 1768 } 1769 1770 static int 1771 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6, 1772 struct ifnet *ifp0, int resid) 1773 { 1774 struct ifnet *ifp; 1775 struct in6_ifaddr *ifa6; 1776 struct ifaddr *ifa; 1777 struct ifnet *ifp_dep = NULL; 1778 int copied = 0, allow_deprecated = 0; 1779 u_char *cp = (u_char *)(nni6 + 1); 1780 int niflags = ni6->ni_flags; 1781 u_int32_t ltime; 1782 1783 NET_EPOCH_ASSERT(); 1784 1785 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) 1786 return (0); /* needless to copy */ 1787 1788 ifp = ifp0 ? ifp0 : CK_STAILQ_FIRST(&V_ifnet); 1789 again: 1790 1791 for (; ifp; ifp = CK_STAILQ_NEXT(ifp, if_link)) { 1792 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1793 if (ifa->ifa_addr->sa_family != AF_INET6) 1794 continue; 1795 ifa6 = (struct in6_ifaddr *)ifa; 1796 1797 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && 1798 allow_deprecated == 0) { 1799 /* 1800 * prefererred address should be put before 1801 * deprecated addresses. 1802 */ 1803 1804 /* record the interface for later search */ 1805 if (ifp_dep == NULL) 1806 ifp_dep = ifp; 1807 1808 continue; 1809 } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && 1810 allow_deprecated != 0) 1811 continue; /* we now collect deprecated addrs */ 1812 1813 /* What do we have to do about ::1? */ 1814 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { 1815 case IPV6_ADDR_SCOPE_LINKLOCAL: 1816 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) 1817 continue; 1818 break; 1819 case IPV6_ADDR_SCOPE_SITELOCAL: 1820 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) 1821 continue; 1822 break; 1823 case IPV6_ADDR_SCOPE_GLOBAL: 1824 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) 1825 continue; 1826 break; 1827 default: 1828 continue; 1829 } 1830 1831 /* 1832 * check if anycast is okay. 1833 * XXX: just experimental. not in the spec. 1834 */ 1835 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && 1836 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) 1837 continue; 1838 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1839 (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { 1840 continue; 1841 } 1842 1843 /* now we can copy the address */ 1844 if (resid < sizeof(struct in6_addr) + 1845 sizeof(u_int32_t)) { 1846 /* 1847 * We give up much more copy. 1848 * Set the truncate flag and return. 1849 */ 1850 nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE; 1851 return (copied); 1852 } 1853 1854 /* 1855 * Set the TTL of the address. 1856 * The TTL value should be one of the following 1857 * according to the specification: 1858 * 1859 * 1. The remaining lifetime of a DHCP lease on the 1860 * address, or 1861 * 2. The remaining Valid Lifetime of a prefix from 1862 * which the address was derived through Stateless 1863 * Autoconfiguration. 1864 * 1865 * Note that we currently do not support stateful 1866 * address configuration by DHCPv6, so the former 1867 * case can't happen. 1868 */ 1869 if (ifa6->ia6_lifetime.ia6t_expire == 0) 1870 ltime = ND6_INFINITE_LIFETIME; 1871 else { 1872 if (ifa6->ia6_lifetime.ia6t_expire > 1873 time_uptime) 1874 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_uptime); 1875 else 1876 ltime = 0; 1877 } 1878 1879 bcopy(<ime, cp, sizeof(u_int32_t)); 1880 cp += sizeof(u_int32_t); 1881 1882 /* copy the address itself */ 1883 bcopy(&ifa6->ia_addr.sin6_addr, cp, 1884 sizeof(struct in6_addr)); 1885 in6_clearscope((struct in6_addr *)cp); /* XXX */ 1886 cp += sizeof(struct in6_addr); 1887 1888 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1889 copied += (sizeof(struct in6_addr) + sizeof(u_int32_t)); 1890 } 1891 if (ifp0) /* we need search only on the specified IF */ 1892 break; 1893 } 1894 1895 if (allow_deprecated == 0 && ifp_dep != NULL) { 1896 ifp = ifp_dep; 1897 allow_deprecated = 1; 1898 1899 goto again; 1900 } 1901 1902 return (copied); 1903 } 1904 1905 static bool 1906 icmp6_rip6_match(const struct inpcb *inp, void *v) 1907 { 1908 struct ip6_hdr *ip6 = v; 1909 1910 if ((inp->inp_vflag & INP_IPV6) == 0) 1911 return (false); 1912 if (inp->inp_ip_p != IPPROTO_ICMPV6) 1913 return (false); 1914 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) && 1915 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &ip6->ip6_dst)) 1916 return (false); 1917 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 1918 !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &ip6->ip6_src)) 1919 return (false); 1920 return (true); 1921 } 1922 1923 /* 1924 * XXX almost dup'ed code with rip6_input. 1925 */ 1926 static int 1927 icmp6_rip6_input(struct mbuf **mp, int off) 1928 { 1929 struct mbuf *n, *m = *mp; 1930 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1931 struct inpcb_iterator inpi = INP_ITERATOR(&V_ripcbinfo, 1932 INPLOOKUP_RLOCKPCB, icmp6_rip6_match, ip6); 1933 struct inpcb *inp; 1934 struct sockaddr_in6 fromsa; 1935 struct icmp6_hdr *icmp6; 1936 struct mbuf *opts = NULL; 1937 int delivered = 0; 1938 1939 /* This is assumed to be safe; icmp6_input() does a pullup. */ 1940 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); 1941 1942 /* 1943 * XXX: the address may have embedded scope zone ID, which should be 1944 * hidden from applications. 1945 */ 1946 bzero(&fromsa, sizeof(fromsa)); 1947 fromsa.sin6_family = AF_INET6; 1948 fromsa.sin6_len = sizeof(struct sockaddr_in6); 1949 fromsa.sin6_addr = ip6->ip6_src; 1950 if (sa6_recoverscope(&fromsa)) { 1951 m_freem(m); 1952 *mp = NULL; 1953 return (IPPROTO_DONE); 1954 } 1955 1956 while ((inp = inp_next(&inpi)) != NULL) { 1957 if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, 1958 inp->in6p_icmp6filt)) 1959 continue; 1960 /* 1961 * Recent network drivers tend to allocate a single 1962 * mbuf cluster, rather than to make a couple of 1963 * mbufs without clusters. Also, since the IPv6 code 1964 * path tries to avoid m_pullup(), it is highly 1965 * probable that we still have an mbuf cluster here 1966 * even though the necessary length can be stored in an 1967 * mbuf's internal buffer. 1968 * Meanwhile, the default size of the receive socket 1969 * buffer for raw sockets is not so large. This means 1970 * the possibility of packet loss is relatively higher 1971 * than before. To avoid this scenario, we copy the 1972 * received data to a separate mbuf that does not use 1973 * a cluster, if possible. 1974 * XXX: it is better to copy the data after stripping 1975 * intermediate headers. 1976 */ 1977 if ((m->m_flags & M_EXT) && m->m_next == NULL && 1978 m->m_len <= MHLEN) { 1979 n = m_get(M_NOWAIT, m->m_type); 1980 if (n != NULL) { 1981 if (m_dup_pkthdr(n, m, M_NOWAIT)) { 1982 bcopy(m->m_data, n->m_data, m->m_len); 1983 n->m_len = m->m_len; 1984 } else { 1985 m_free(n); 1986 n = NULL; 1987 } 1988 } 1989 } else 1990 n = m_copym(m, 0, M_COPYALL, M_NOWAIT); 1991 if (n == NULL) 1992 continue; 1993 if (inp->inp_flags & INP_CONTROLOPTS) 1994 ip6_savecontrol(inp, n, &opts); 1995 /* strip intermediate headers */ 1996 m_adj(n, off); 1997 SOCKBUF_LOCK(&inp->inp_socket->so_rcv); 1998 if (sbappendaddr_locked(&inp->inp_socket->so_rcv, 1999 (struct sockaddr *)&fromsa, n, opts) == 0) { 2000 soroverflow_locked(inp->inp_socket); 2001 m_freem(n); 2002 if (opts) 2003 m_freem(opts); 2004 } else { 2005 sorwakeup_locked(inp->inp_socket); 2006 delivered++; 2007 } 2008 opts = NULL; 2009 } 2010 m_freem(m); 2011 *mp = NULL; 2012 if (delivered == 0) 2013 IP6STAT_DEC(ip6s_delivered); 2014 return (IPPROTO_DONE); 2015 } 2016 2017 /* 2018 * Reflect the ip6 packet back to the source. 2019 * OFF points to the icmp6 header, counted from the top of the mbuf. 2020 */ 2021 static void 2022 icmp6_reflect(struct mbuf *m, size_t off) 2023 { 2024 struct in6_addr src6, *srcp; 2025 struct ip6_hdr *ip6; 2026 struct icmp6_hdr *icmp6; 2027 struct in6_ifaddr *ia = NULL; 2028 struct ifnet *outif = NULL; 2029 int plen; 2030 int type, code, hlim; 2031 2032 /* too short to reflect */ 2033 if (off < sizeof(struct ip6_hdr)) { 2034 nd6log((LOG_DEBUG, 2035 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", 2036 (u_long)off, (u_long)sizeof(struct ip6_hdr), 2037 __FILE__, __LINE__)); 2038 goto bad; 2039 } 2040 2041 /* 2042 * If there are extra headers between IPv6 and ICMPv6, strip 2043 * off that header first. 2044 */ 2045 #ifdef DIAGNOSTIC 2046 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN) 2047 panic("assumption failed in icmp6_reflect"); 2048 #endif 2049 if (off > sizeof(struct ip6_hdr)) { 2050 size_t l; 2051 struct ip6_hdr nip6; 2052 2053 l = off - sizeof(struct ip6_hdr); 2054 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); 2055 m_adj(m, l); 2056 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 2057 if (m->m_len < l) { 2058 if ((m = m_pullup(m, l)) == NULL) 2059 return; 2060 } 2061 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); 2062 } else /* off == sizeof(struct ip6_hdr) */ { 2063 size_t l; 2064 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); 2065 if (m->m_len < l) { 2066 if ((m = m_pullup(m, l)) == NULL) 2067 return; 2068 } 2069 } 2070 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); 2071 ip6 = mtod(m, struct ip6_hdr *); 2072 ip6->ip6_nxt = IPPROTO_ICMPV6; 2073 icmp6 = (struct icmp6_hdr *)(ip6 + 1); 2074 type = icmp6->icmp6_type; /* keep type for statistics */ 2075 code = icmp6->icmp6_code; /* ditto. */ 2076 hlim = 0; 2077 srcp = NULL; 2078 2079 /* 2080 * If the incoming packet was addressed directly to us (i.e. unicast), 2081 * use dst as the src for the reply. 2082 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible 2083 * (for example) when we encounter an error while forwarding procedure 2084 * destined to a duplicated address of ours. 2085 */ 2086 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 2087 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false); 2088 if (ia != NULL && !(ia->ia6_flags & 2089 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) { 2090 src6 = ia->ia_addr.sin6_addr; 2091 srcp = &src6; 2092 2093 if (m->m_pkthdr.rcvif != NULL) { 2094 /* XXX: This may not be the outgoing interface */ 2095 hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim; 2096 } else 2097 hlim = V_ip6_defhlim; 2098 } 2099 } 2100 2101 if (srcp == NULL) { 2102 int error; 2103 struct in6_addr dst6; 2104 uint32_t scopeid; 2105 2106 /* 2107 * This case matches to multicasts, our anycast, or unicasts 2108 * that we do not own. Select a source address based on the 2109 * source address of the erroneous packet. 2110 */ 2111 in6_splitscope(&ip6->ip6_src, &dst6, &scopeid); 2112 error = in6_selectsrc_addr(M_GETFIB(m), &dst6, 2113 scopeid, NULL, &src6, &hlim); 2114 2115 if (error) { 2116 char ip6buf[INET6_ADDRSTRLEN]; 2117 nd6log((LOG_DEBUG, 2118 "icmp6_reflect: source can't be determined: " 2119 "dst=%s, error=%d\n", 2120 ip6_sprintf(ip6buf, &ip6->ip6_dst), error)); 2121 goto bad; 2122 } 2123 srcp = &src6; 2124 } 2125 /* 2126 * ip6_input() drops a packet if its src is multicast. 2127 * So, the src is never multicast. 2128 */ 2129 ip6->ip6_dst = ip6->ip6_src; 2130 ip6->ip6_src = *srcp; 2131 ip6->ip6_flow = 0; 2132 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 2133 ip6->ip6_vfc |= IPV6_VERSION; 2134 ip6->ip6_nxt = IPPROTO_ICMPV6; 2135 ip6->ip6_hlim = hlim; 2136 2137 icmp6->icmp6_cksum = 0; 2138 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2139 sizeof(struct ip6_hdr), plen); 2140 2141 /* 2142 * XXX option handling 2143 */ 2144 2145 m->m_flags &= ~(M_BCAST|M_MCAST); 2146 m->m_pkthdr.rcvif = NULL; 2147 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 2148 if (outif) 2149 icmp6_ifoutstat_inc(outif, type, code); 2150 2151 return; 2152 2153 bad: 2154 m_freem(m); 2155 return; 2156 } 2157 2158 static const char * 2159 icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6, 2160 struct in6_addr *tgt6) 2161 { 2162 static char buf[1024]; 2163 char ip6bufs[INET6_ADDRSTRLEN]; 2164 char ip6bufd[INET6_ADDRSTRLEN]; 2165 char ip6buft[INET6_ADDRSTRLEN]; 2166 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", 2167 ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6), 2168 ip6_sprintf(ip6buft, tgt6)); 2169 return buf; 2170 } 2171 2172 void 2173 icmp6_redirect_input(struct mbuf *m, int off) 2174 { 2175 struct ifnet *ifp; 2176 struct ip6_hdr *ip6; 2177 struct nd_redirect *nd_rd; 2178 struct in6_addr src6, redtgt6, reddst6; 2179 union nd_opts ndopts; 2180 char ip6buf[INET6_ADDRSTRLEN]; 2181 char *lladdr; 2182 int icmp6len, is_onlink, is_router, lladdrlen; 2183 2184 M_ASSERTPKTHDR(m); 2185 KASSERT(m->m_pkthdr.rcvif != NULL, ("%s: no rcvif", __func__)); 2186 2187 /* XXX if we are router, we don't update route by icmp6 redirect */ 2188 if (V_ip6_forwarding) 2189 goto freeit; 2190 if (!V_icmp6_rediraccept) 2191 goto freeit; 2192 2193 /* RFC 6980: Nodes MUST silently ignore fragments */ 2194 if(m->m_flags & M_FRAGMENTED) 2195 goto freeit; 2196 2197 ip6 = mtod(m, struct ip6_hdr *); 2198 icmp6len = ntohs(ip6->ip6_plen); 2199 if (m->m_len < off + icmp6len) { 2200 m = m_pullup(m, off + icmp6len); 2201 if (m == NULL) { 2202 IP6STAT_INC(ip6s_exthdrtoolong); 2203 return; 2204 } 2205 } 2206 ip6 = mtod(m, struct ip6_hdr *); 2207 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); 2208 2209 ifp = m->m_pkthdr.rcvif; 2210 redtgt6 = nd_rd->nd_rd_target; 2211 reddst6 = nd_rd->nd_rd_dst; 2212 2213 if (in6_setscope(&redtgt6, ifp, NULL) || 2214 in6_setscope(&reddst6, ifp, NULL)) { 2215 goto freeit; 2216 } 2217 2218 /* validation */ 2219 src6 = ip6->ip6_src; 2220 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { 2221 nd6log((LOG_ERR, 2222 "ICMP6 redirect sent from %s rejected; " 2223 "must be from linklocal\n", 2224 ip6_sprintf(ip6buf, &src6))); 2225 goto bad; 2226 } 2227 if (__predict_false(ip6->ip6_hlim != 255)) { 2228 ICMP6STAT_INC(icp6s_invlhlim); 2229 nd6log((LOG_ERR, 2230 "ICMP6 redirect sent from %s rejected; " 2231 "hlim=%d (must be 255)\n", 2232 ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim)); 2233 goto bad; 2234 } 2235 { 2236 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ 2237 struct nhop_object *nh; 2238 struct in6_addr kdst; 2239 uint32_t scopeid; 2240 2241 in6_splitscope(&reddst6, &kdst, &scopeid); 2242 NET_EPOCH_ASSERT(); 2243 nh = fib6_lookup(ifp->if_fib, &kdst, scopeid, 0, 0); 2244 if (nh != NULL) { 2245 struct in6_addr nh_addr; 2246 nh_addr = ifatoia6(nh->nh_ifa)->ia_addr.sin6_addr; 2247 if ((nh->nh_flags & NHF_GATEWAY) == 0) { 2248 nd6log((LOG_ERR, 2249 "ICMP6 redirect rejected; no route " 2250 "with inet6 gateway found for redirect dst: %s\n", 2251 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2252 goto bad; 2253 } 2254 2255 /* 2256 * Embed scope zone id into next hop address. 2257 */ 2258 nh_addr = nh->gw6_sa.sin6_addr; 2259 2260 if (IN6_ARE_ADDR_EQUAL(&src6, &nh_addr) == 0) { 2261 nd6log((LOG_ERR, 2262 "ICMP6 redirect rejected; " 2263 "not equal to gw-for-src=%s (must be same): " 2264 "%s\n", 2265 ip6_sprintf(ip6buf, &nh_addr), 2266 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2267 goto bad; 2268 } 2269 } else { 2270 nd6log((LOG_ERR, 2271 "ICMP6 redirect rejected; " 2272 "no route found for redirect dst: %s\n", 2273 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2274 goto bad; 2275 } 2276 } 2277 if (IN6_IS_ADDR_MULTICAST(&reddst6)) { 2278 nd6log((LOG_ERR, 2279 "ICMP6 redirect rejected; " 2280 "redirect dst must be unicast: %s\n", 2281 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2282 goto bad; 2283 } 2284 2285 is_router = is_onlink = 0; 2286 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) 2287 is_router = 1; /* router case */ 2288 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) 2289 is_onlink = 1; /* on-link destination case */ 2290 if (!is_router && !is_onlink) { 2291 nd6log((LOG_ERR, 2292 "ICMP6 redirect rejected; " 2293 "neither router case nor onlink case: %s\n", 2294 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2295 goto bad; 2296 } 2297 2298 icmp6len -= sizeof(*nd_rd); 2299 nd6_option_init(nd_rd + 1, icmp6len, &ndopts); 2300 if (nd6_options(&ndopts) < 0) { 2301 nd6log((LOG_INFO, "%s: invalid ND option, rejected: %s\n", 2302 __func__, icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2303 /* nd6_options have incremented stats */ 2304 goto freeit; 2305 } 2306 2307 lladdr = NULL; 2308 lladdrlen = 0; 2309 if (ndopts.nd_opts_tgt_lladdr) { 2310 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 2311 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 2312 } 2313 2314 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 2315 nd6log((LOG_INFO, "%s: lladdrlen mismatch for %s " 2316 "(if %d, icmp6 packet %d): %s\n", 2317 __func__, ip6_sprintf(ip6buf, &redtgt6), 2318 ifp->if_addrlen, lladdrlen - 2, 2319 icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); 2320 goto bad; 2321 } 2322 2323 /* Validation passed. */ 2324 2325 /* RFC 2461 8.3 */ 2326 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, 2327 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); 2328 2329 /* 2330 * Install a gateway route in the better-router case or an interface 2331 * route in the on-link-destination case. 2332 */ 2333 { 2334 struct sockaddr_in6 sdst; 2335 struct sockaddr_in6 sgw; 2336 struct sockaddr_in6 ssrc; 2337 struct sockaddr *gw; 2338 int rt_flags; 2339 u_int fibnum; 2340 2341 bzero(&sdst, sizeof(sdst)); 2342 bzero(&ssrc, sizeof(ssrc)); 2343 sdst.sin6_family = ssrc.sin6_family = AF_INET6; 2344 sdst.sin6_len = ssrc.sin6_len = sizeof(struct sockaddr_in6); 2345 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); 2346 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); 2347 rt_flags = 0; 2348 if (is_router) { 2349 bzero(&sgw, sizeof(sgw)); 2350 sgw.sin6_family = AF_INET6; 2351 sgw.sin6_len = sizeof(struct sockaddr_in6); 2352 bcopy(&redtgt6, &sgw.sin6_addr, 2353 sizeof(struct in6_addr)); 2354 gw = (struct sockaddr *)&sgw; 2355 rt_flags |= RTF_GATEWAY; 2356 } else 2357 gw = ifp->if_addr->ifa_addr; 2358 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) 2359 rib_add_redirect(fibnum, (struct sockaddr *)&sdst, gw, 2360 (struct sockaddr *)&ssrc, ifp, rt_flags, 2361 V_icmp6_redirtimeout); 2362 } 2363 2364 freeit: 2365 m_freem(m); 2366 return; 2367 2368 bad: 2369 ICMP6STAT_INC(icp6s_badredirect); 2370 m_freem(m); 2371 } 2372 2373 void 2374 icmp6_redirect_output(struct mbuf *m0, struct nhop_object *nh) 2375 { 2376 struct ifnet *ifp; /* my outgoing interface */ 2377 struct in6_addr *ifp_ll6; 2378 struct in6_addr *router_ll6; 2379 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ 2380 struct mbuf *m = NULL; /* newly allocated one */ 2381 struct m_tag *mtag; 2382 struct ip6_hdr *ip6; /* m as struct ip6_hdr */ 2383 struct nd_redirect *nd_rd; 2384 struct llentry *ln = NULL; 2385 size_t maxlen; 2386 u_char *p; 2387 struct ifnet *outif = NULL; 2388 struct sockaddr_in6 src_sa; 2389 2390 icmp6_errcount(ND_REDIRECT, 0); 2391 2392 /* if we are not router, we don't send icmp6 redirect */ 2393 if (!V_ip6_forwarding) 2394 goto fail; 2395 2396 /* sanity check */ 2397 if (!m0 || !nh || !(NH_IS_VALID(nh)) || !(ifp = nh->nh_ifp)) 2398 goto fail; 2399 2400 /* 2401 * Address check: 2402 * the source address must identify a neighbor, and 2403 * the destination address must not be a multicast address 2404 * [RFC 2461, sec 8.2] 2405 */ 2406 sip6 = mtod(m0, struct ip6_hdr *); 2407 bzero(&src_sa, sizeof(src_sa)); 2408 src_sa.sin6_family = AF_INET6; 2409 src_sa.sin6_len = sizeof(src_sa); 2410 src_sa.sin6_addr = sip6->ip6_src; 2411 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0) 2412 goto fail; 2413 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) 2414 goto fail; /* what should we do here? */ 2415 2416 /* rate limit */ 2417 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) 2418 goto fail; 2419 2420 /* 2421 * Since we are going to append up to 1280 bytes (= IPV6_MMTU), 2422 * we almost always ask for an mbuf cluster for simplicity. 2423 * (MHLEN < IPV6_MMTU is almost always true) 2424 */ 2425 #if IPV6_MMTU >= MCLBYTES 2426 # error assumption failed about IPV6_MMTU and MCLBYTES 2427 #endif 2428 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 2429 if (m == NULL) 2430 goto fail; 2431 M_SETFIB(m, M_GETFIB(m0)); 2432 maxlen = M_TRAILINGSPACE(m); 2433 maxlen = min(IPV6_MMTU, maxlen); 2434 /* just for safety */ 2435 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 2436 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { 2437 goto fail; 2438 } 2439 2440 { 2441 /* get ip6 linklocal address for ifp(my outgoing interface). */ 2442 struct in6_ifaddr *ia; 2443 if ((ia = in6ifa_ifpforlinklocal(ifp, 2444 IN6_IFF_NOTREADY| 2445 IN6_IFF_ANYCAST)) == NULL) 2446 goto fail; 2447 ifp_ll6 = &ia->ia_addr.sin6_addr; 2448 /* XXXRW: reference released prematurely. */ 2449 ifa_free(&ia->ia_ifa); 2450 } 2451 2452 /* get ip6 linklocal address for the router. */ 2453 if (nh->nh_flags & NHF_GATEWAY) { 2454 struct sockaddr_in6 *sin6; 2455 sin6 = &nh->gw6_sa; 2456 router_ll6 = &sin6->sin6_addr; 2457 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) 2458 router_ll6 = (struct in6_addr *)NULL; 2459 } else 2460 router_ll6 = (struct in6_addr *)NULL; 2461 2462 /* ip6 */ 2463 ip6 = mtod(m, struct ip6_hdr *); 2464 ip6->ip6_flow = 0; 2465 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 2466 ip6->ip6_vfc |= IPV6_VERSION; 2467 /* ip6->ip6_plen will be set later */ 2468 ip6->ip6_nxt = IPPROTO_ICMPV6; 2469 ip6->ip6_hlim = 255; 2470 /* ip6->ip6_src must be linklocal addr for my outgoing if. */ 2471 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); 2472 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); 2473 2474 /* ND Redirect */ 2475 nd_rd = (struct nd_redirect *)(ip6 + 1); 2476 nd_rd->nd_rd_type = ND_REDIRECT; 2477 nd_rd->nd_rd_code = 0; 2478 nd_rd->nd_rd_reserved = 0; 2479 if (nh->nh_flags & NHF_GATEWAY) { 2480 /* 2481 * nd_rd->nd_rd_target must be a link-local address in 2482 * better router cases. 2483 */ 2484 if (!router_ll6) 2485 goto fail; 2486 bcopy(router_ll6, &nd_rd->nd_rd_target, 2487 sizeof(nd_rd->nd_rd_target)); 2488 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2489 sizeof(nd_rd->nd_rd_dst)); 2490 } else { 2491 /* make sure redtgt == reddst */ 2492 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target, 2493 sizeof(nd_rd->nd_rd_target)); 2494 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, 2495 sizeof(nd_rd->nd_rd_dst)); 2496 } 2497 2498 p = (u_char *)(nd_rd + 1); 2499 2500 if (!router_ll6) 2501 goto nolladdropt; 2502 2503 { 2504 /* target lladdr option */ 2505 int len; 2506 struct nd_opt_hdr *nd_opt; 2507 char *lladdr; 2508 2509 ln = nd6_lookup(router_ll6, LLE_SF(AF_INET6, 0), ifp); 2510 if (ln == NULL) 2511 goto nolladdropt; 2512 2513 len = sizeof(*nd_opt) + ifp->if_addrlen; 2514 len = (len + 7) & ~7; /* round by 8 */ 2515 /* safety check */ 2516 if (len + (p - (u_char *)ip6) > maxlen) 2517 goto nolladdropt; 2518 2519 if (ln->la_flags & LLE_VALID) { 2520 nd_opt = (struct nd_opt_hdr *)p; 2521 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 2522 nd_opt->nd_opt_len = len >> 3; 2523 lladdr = (char *)(nd_opt + 1); 2524 bcopy(ln->ll_addr, lladdr, ifp->if_addrlen); 2525 p += len; 2526 } 2527 } 2528 nolladdropt: 2529 if (ln != NULL) 2530 LLE_RUNLOCK(ln); 2531 2532 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 2533 2534 /* just to be safe */ 2535 #ifdef M_DECRYPTED /*not openbsd*/ 2536 if (m0->m_flags & M_DECRYPTED) 2537 goto noredhdropt; 2538 #endif 2539 if (p - (u_char *)ip6 > maxlen) 2540 goto noredhdropt; 2541 2542 { 2543 /* redirected header option */ 2544 int len; 2545 struct nd_opt_rd_hdr *nd_opt_rh; 2546 2547 /* 2548 * compute the maximum size for icmp6 redirect header option. 2549 * XXX room for auth header? 2550 */ 2551 len = maxlen - (p - (u_char *)ip6); 2552 len &= ~7; 2553 2554 /* This is just for simplicity. */ 2555 if (m0->m_pkthdr.len != m0->m_len) { 2556 if (m0->m_next) { 2557 m_freem(m0->m_next); 2558 m0->m_next = NULL; 2559 } 2560 m0->m_pkthdr.len = m0->m_len; 2561 } 2562 2563 /* 2564 * Redirected header option spec (RFC2461 4.6.3) talks nothing 2565 * about padding/truncate rule for the original IP packet. 2566 * From the discussion on IPv6imp in Feb 1999, 2567 * the consensus was: 2568 * - "attach as much as possible" is the goal 2569 * - pad if not aligned (original size can be guessed by 2570 * original ip6 header) 2571 * Following code adds the padding if it is simple enough, 2572 * and truncates if not. 2573 */ 2574 if (m0->m_next || m0->m_pkthdr.len != m0->m_len) 2575 panic("assumption failed in %s:%d", __FILE__, 2576 __LINE__); 2577 2578 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { 2579 /* not enough room, truncate */ 2580 m0->m_pkthdr.len = m0->m_len = len - 2581 sizeof(*nd_opt_rh); 2582 } else { 2583 /* enough room, pad or truncate */ 2584 size_t extra; 2585 2586 extra = m0->m_pkthdr.len % 8; 2587 if (extra) { 2588 /* pad if easy enough, truncate if not */ 2589 if (8 - extra <= M_TRAILINGSPACE(m0)) { 2590 /* pad */ 2591 m0->m_len += (8 - extra); 2592 m0->m_pkthdr.len += (8 - extra); 2593 } else { 2594 /* truncate */ 2595 m0->m_pkthdr.len -= extra; 2596 m0->m_len -= extra; 2597 } 2598 } 2599 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); 2600 m0->m_pkthdr.len = m0->m_len = len - 2601 sizeof(*nd_opt_rh); 2602 } 2603 2604 nd_opt_rh = (struct nd_opt_rd_hdr *)p; 2605 bzero(nd_opt_rh, sizeof(*nd_opt_rh)); 2606 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; 2607 nd_opt_rh->nd_opt_rh_len = len >> 3; 2608 p += sizeof(*nd_opt_rh); 2609 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; 2610 2611 /* connect m0 to m */ 2612 m_tag_delete_chain(m0, NULL); 2613 m0->m_flags &= ~M_PKTHDR; 2614 m->m_next = m0; 2615 m->m_pkthdr.len = m->m_len + m0->m_len; 2616 m0 = NULL; 2617 } 2618 noredhdropt:; 2619 if (m0) { 2620 m_freem(m0); 2621 m0 = NULL; 2622 } 2623 2624 /* XXX: clear embedded link IDs in the inner header */ 2625 in6_clearscope(&sip6->ip6_src); 2626 in6_clearscope(&sip6->ip6_dst); 2627 in6_clearscope(&nd_rd->nd_rd_target); 2628 in6_clearscope(&nd_rd->nd_rd_dst); 2629 2630 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 2631 2632 nd_rd->nd_rd_cksum = 0; 2633 nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6, 2634 sizeof(*ip6), ntohs(ip6->ip6_plen)); 2635 2636 if (send_sendso_input_hook != NULL) { 2637 mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, sizeof(unsigned short), 2638 M_NOWAIT); 2639 if (mtag == NULL) 2640 goto fail; 2641 *(unsigned short *)(mtag + 1) = nd_rd->nd_rd_type; 2642 m_tag_prepend(m, mtag); 2643 } 2644 2645 /* send the packet to outside... */ 2646 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL); 2647 if (outif) { 2648 icmp6_ifstat_inc(outif, ifs6_out_msg); 2649 icmp6_ifstat_inc(outif, ifs6_out_redirect); 2650 } 2651 ICMP6STAT_INC2(icp6s_outhist, ND_REDIRECT); 2652 2653 return; 2654 2655 fail: 2656 if (m) 2657 m_freem(m); 2658 if (m0) 2659 m_freem(m0); 2660 } 2661 2662 /* 2663 * ICMPv6 socket option processing. 2664 */ 2665 int 2666 icmp6_ctloutput(struct socket *so, struct sockopt *sopt) 2667 { 2668 int error = 0; 2669 int optlen; 2670 struct inpcb *inp = sotoinpcb(so); 2671 int level, op, optname; 2672 2673 if (sopt) { 2674 level = sopt->sopt_level; 2675 op = sopt->sopt_dir; 2676 optname = sopt->sopt_name; 2677 optlen = sopt->sopt_valsize; 2678 } else 2679 level = op = optname = optlen = 0; 2680 2681 if (level != IPPROTO_ICMPV6) { 2682 return EINVAL; 2683 } 2684 2685 switch (op) { 2686 case PRCO_SETOPT: 2687 switch (optname) { 2688 case ICMP6_FILTER: 2689 { 2690 struct icmp6_filter ic6f; 2691 2692 if (optlen != sizeof(ic6f)) { 2693 error = EMSGSIZE; 2694 break; 2695 } 2696 error = sooptcopyin(sopt, &ic6f, optlen, optlen); 2697 if (error == 0) { 2698 INP_WLOCK(inp); 2699 *inp->in6p_icmp6filt = ic6f; 2700 INP_WUNLOCK(inp); 2701 } 2702 break; 2703 } 2704 2705 default: 2706 error = ENOPROTOOPT; 2707 break; 2708 } 2709 break; 2710 2711 case PRCO_GETOPT: 2712 switch (optname) { 2713 case ICMP6_FILTER: 2714 { 2715 struct icmp6_filter ic6f; 2716 2717 INP_RLOCK(inp); 2718 ic6f = *inp->in6p_icmp6filt; 2719 INP_RUNLOCK(inp); 2720 error = sooptcopyout(sopt, &ic6f, sizeof(ic6f)); 2721 break; 2722 } 2723 2724 default: 2725 error = ENOPROTOOPT; 2726 break; 2727 } 2728 break; 2729 } 2730 2731 return (error); 2732 } 2733 2734 static int sysctl_icmp6lim_and_jitter(SYSCTL_HANDLER_ARGS); 2735 VNET_DEFINE_STATIC(u_int, icmp6errppslim) = 100; 2736 #define V_icmp6errppslim VNET(icmp6errppslim) 2737 SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ERRPPSLIMIT, errppslimit, 2738 CTLTYPE_UINT | CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(icmp6errppslim), 0, 2739 &sysctl_icmp6lim_and_jitter, "IU", 2740 "Maximum number of ICMPv6 error/reply messages per second"); 2741 2742 VNET_DEFINE_STATIC(int, icmp6lim_curr_jitter) = 0; 2743 #define V_icmp6lim_curr_jitter VNET(icmp6lim_curr_jitter) 2744 2745 VNET_DEFINE_STATIC(u_int, icmp6lim_jitter) = 8; 2746 #define V_icmp6lim_jitter VNET(icmp6lim_jitter) 2747 SYSCTL_PROC(_net_inet6_icmp6, OID_AUTO, icmp6lim_jitter, CTLTYPE_UINT | 2748 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(icmp6lim_jitter), 0, 2749 &sysctl_icmp6lim_and_jitter, "IU", 2750 "Random errppslimit jitter adjustment limit"); 2751 2752 VNET_DEFINE_STATIC(int, icmp6lim_output) = 1; 2753 #define V_icmp6lim_output VNET(icmp6lim_output) 2754 SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, icmp6lim_output, 2755 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(icmp6lim_output), 0, 2756 "Enable logging of ICMPv6 response rate limiting"); 2757 2758 typedef enum { 2759 RATELIM_PARAM_PROB = 0, 2760 RATELIM_TOO_BIG, 2761 RATELIM_UNREACH, 2762 RATELIM_TEXCEED, 2763 RATELIM_REDIR, 2764 RATELIM_REPLY, 2765 RATELIM_OTHER, 2766 RATELIM_MAX 2767 } ratelim_which; 2768 2769 static const char *icmp6_rate_descrs[RATELIM_MAX] = { 2770 [RATELIM_PARAM_PROB] = "bad IPv6 header", 2771 [RATELIM_TOO_BIG] = "packet too big", 2772 [RATELIM_UNREACH] = "destination unreachable", 2773 [RATELIM_TEXCEED] = "time exceeded", 2774 [RATELIM_REPLY] = "echo reply", 2775 [RATELIM_REDIR] = "neighbor discovery redirect", 2776 [RATELIM_OTHER] = "(other)", 2777 }; 2778 2779 static void 2780 icmp6lim_new_jitter(void) 2781 { 2782 /* 2783 * Adjust limit +/- to jitter the measurement to deny a side-channel 2784 * port scan as in https://dl.acm.org/doi/10.1145/3372297.3417280 2785 */ 2786 if (V_icmp6lim_jitter > 0) 2787 V_icmp6lim_curr_jitter = 2788 arc4random_uniform(V_icmp6lim_jitter * 2 + 1) - 2789 V_icmp6lim_jitter; 2790 } 2791 2792 static int 2793 sysctl_icmp6lim_and_jitter(SYSCTL_HANDLER_ARGS) 2794 { 2795 uint32_t new; 2796 int error; 2797 bool lim; 2798 2799 MPASS(oidp->oid_arg1 == &VNET_NAME(icmp6errppslim) || 2800 oidp->oid_arg1 == &VNET_NAME(icmp6lim_jitter)); 2801 2802 lim = (oidp->oid_arg1 == &VNET_NAME(icmp6errppslim)); 2803 new = lim ? V_icmp6errppslim : V_icmp6lim_jitter; 2804 error = sysctl_handle_int(oidp, &new, 0, req); 2805 if (error == 0 && req->newptr) { 2806 if (lim) { 2807 if (new != 0 && new <= V_icmp6lim_jitter) 2808 error = EINVAL; 2809 else 2810 V_icmp6errppslim = new; 2811 } else { 2812 if (new >= V_icmp6errppslim) 2813 error = EINVAL; 2814 else { 2815 V_icmp6lim_jitter = new; 2816 icmp6lim_new_jitter(); 2817 } 2818 } 2819 } 2820 MPASS(V_icmp6errppslim + V_icmp6lim_curr_jitter >= 0); 2821 2822 return (error); 2823 } 2824 2825 2826 VNET_DEFINE_STATIC(struct counter_rate, icmp6_rates[RATELIM_MAX]); 2827 #define V_icmp6_rates VNET(icmp6_rates) 2828 2829 static void 2830 icmp6_ratelimit_init(void) 2831 { 2832 2833 for (int i = 0; i < RATELIM_MAX; i++) { 2834 V_icmp6_rates[i].cr_rate = counter_u64_alloc(M_WAITOK); 2835 V_icmp6_rates[i].cr_ticks = ticks; 2836 } 2837 icmp6lim_new_jitter(); 2838 } 2839 VNET_SYSINIT(icmp6_ratelimit, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, 2840 icmp6_ratelimit_init, NULL); 2841 2842 #ifdef VIMAGE 2843 static void 2844 icmp6_ratelimit_uninit(void) 2845 { 2846 2847 for (int i = 0; i < RATELIM_MAX; i++) 2848 counter_u64_free(V_icmp6_rates[i].cr_rate); 2849 } 2850 VNET_SYSUNINIT(icmp6_ratelimit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, 2851 icmp6_ratelimit_uninit, NULL); 2852 #endif 2853 2854 /* 2855 * Perform rate limit check. 2856 * Returns 0 if it is okay to send the icmp6 packet. 2857 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate 2858 * limitation. 2859 * 2860 * XXX per-destination/type check necessary? 2861 * 2862 * dst - not used at this moment 2863 * code - not used at this moment 2864 */ 2865 int 2866 icmp6_ratelimit(const struct in6_addr *dst, const int type, const int code) 2867 { 2868 ratelim_which which; 2869 int64_t pps; 2870 2871 if (V_icmp6errppslim == 0) 2872 return (0); 2873 2874 switch (type) { 2875 case ICMP6_PARAM_PROB: 2876 which = RATELIM_PARAM_PROB; 2877 break; 2878 case ICMP6_PACKET_TOO_BIG: 2879 which = RATELIM_TOO_BIG; 2880 break; 2881 case ICMP6_DST_UNREACH: 2882 which = RATELIM_UNREACH; 2883 break; 2884 case ICMP6_TIME_EXCEEDED: 2885 which = RATELIM_TEXCEED; 2886 break; 2887 case ND_REDIRECT: 2888 which = RATELIM_REDIR; 2889 break; 2890 case ICMP6_ECHO_REPLY: 2891 which = RATELIM_REPLY; 2892 break; 2893 default: 2894 which = RATELIM_OTHER; 2895 break; 2896 }; 2897 2898 pps = counter_ratecheck(&V_icmp6_rates[which], V_icmp6errppslim + 2899 V_icmp6lim_curr_jitter); 2900 if (pps > 0) { 2901 if (V_icmp6lim_output) 2902 log(LOG_NOTICE, "Limiting ICMPv6 %s output from %jd " 2903 "to %d packets/sec\n", icmp6_rate_descrs[which], 2904 (intmax_t )pps, V_icmp6errppslim + 2905 V_icmp6lim_curr_jitter); 2906 icmp6lim_new_jitter(); 2907 } 2908 if (pps == -1) { 2909 ICMP6STAT_INC(icp6s_toofreq); 2910 return (-1); 2911 } 2912 2913 return (0); 2914 } 2915