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