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