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