362#endif /* PFIL_HOOKS */ 363 364 ip6stat.ip6s_nxthist[ip6->ip6_nxt]++; 365 366 /* 367 * Check with the firewall... 368 */ 369 if (ip6_fw_enable && ip6_fw_chk_ptr) { 370 u_short port = 0; 371 /* If ipfw says divert, we have to just drop packet */ 372 /* use port as a dummy argument */ 373 if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) { 374 m_freem(m); 375 m = NULL; 376 } 377 if (!m) 378 return; 379 } 380 381 /* 382 * Check against address spoofing/corruption. 383 */ 384 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || 385 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { 386 /* 387 * XXX: "badscope" is not very suitable for a multicast source. 388 */ 389 ip6stat.ip6s_badscope++; 390 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 391 goto bad; 392 } 393 if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) || 394 IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) && 395 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 396 ip6stat.ip6s_badscope++; 397 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 398 goto bad; 399 } 400 401 /* 402 * The following check is not documented in specs. A malicious 403 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack 404 * and bypass security checks (act as if it was from 127.0.0.1 by using 405 * IPv6 src ::ffff:127.0.0.1). Be cautious. 406 * 407 * This check chokes if we are in an SIIT cloud. As none of BSDs 408 * support IPv4-less kernel compilation, we cannot support SIIT 409 * environment at all. So, it makes more sense for us to reject any 410 * malicious packets for non-SIIT environment, than try to do a 411 * partical support for SIIT environment. 412 */ 413 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 414 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 415 ip6stat.ip6s_badscope++; 416 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 417 goto bad; 418 } 419#if 0 420 /* 421 * Reject packets with IPv4 compatible addresses (auto tunnel). 422 * 423 * The code forbids auto tunnel relay case in RFC1933 (the check is 424 * stronger than RFC1933). We may want to re-enable it if mech-xx 425 * is revised to forbid relaying case. 426 */ 427 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) || 428 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) { 429 ip6stat.ip6s_badscope++; 430 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 431 goto bad; 432 } 433#endif 434 435 /* drop packets if interface ID portion is already filled */ 436 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 437 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) && 438 ip6->ip6_src.s6_addr16[1]) { 439 ip6stat.ip6s_badscope++; 440 goto bad; 441 } 442 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) && 443 ip6->ip6_dst.s6_addr16[1]) { 444 ip6stat.ip6s_badscope++; 445 goto bad; 446 } 447 } 448 449 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 450 ip6->ip6_src.s6_addr16[1] 451 = htons(m->m_pkthdr.rcvif->if_index); 452 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 453 ip6->ip6_dst.s6_addr16[1] 454 = htons(m->m_pkthdr.rcvif->if_index); 455 456#if 0 /* this case seems to be unnecessary. (jinmei, 20010401) */ 457 /* 458 * We use rt->rt_ifp to determine if the address is ours or not. 459 * If rt_ifp is lo0, the address is ours. 460 * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0, 461 * so any address under fe80::%lo0/64 will be mistakenly considered 462 * local. The special case is supplied to handle the case properly 463 * by actually looking at interface addresses 464 * (using in6ifa_ifpwithaddr). 465 */ 466 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 && 467 IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) { 468 if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) { 469 icmp6_error(m, ICMP6_DST_UNREACH, 470 ICMP6_DST_UNREACH_ADDR, 0); 471 /* m is already freed */ 472 return; 473 } 474 475 ours = 1; 476 deliverifp = m->m_pkthdr.rcvif; 477 goto hbhcheck; 478 } 479#endif 480 481 /* 482 * Multicast check 483 */ 484 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 485 struct in6_multi *in6m = 0; 486 487 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast); 488 /* 489 * See if we belong to the destination multicast group on the 490 * arrival interface. 491 */ 492 IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m); 493 if (in6m) 494 ours = 1; 495 else if (!ip6_mrouter) { 496 ip6stat.ip6s_notmember++; 497 ip6stat.ip6s_cantforward++; 498 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 499 goto bad; 500 } 501 deliverifp = m->m_pkthdr.rcvif; 502 goto hbhcheck; 503 } 504 505 /* 506 * Unicast check 507 */ 508 switch (ip6_ours_check_algorithm) { 509 default: 510 /* 511 * XXX: I intentionally broke our indentation rule here, 512 * since this switch-case is just for measurement and 513 * therefore should soon be removed. 514 */ 515 if (ip6_forward_rt.ro_rt != NULL && 516 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 && 517 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 518 &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr)) 519 ip6stat.ip6s_forward_cachehit++; 520 else { 521 struct sockaddr_in6 *dst6; 522 523 if (ip6_forward_rt.ro_rt) { 524 /* route is down or destination is different */ 525 ip6stat.ip6s_forward_cachemiss++; 526 RTFREE(ip6_forward_rt.ro_rt); 527 ip6_forward_rt.ro_rt = 0; 528 } 529 530 bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6)); 531 dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst; 532 dst6->sin6_len = sizeof(struct sockaddr_in6); 533 dst6->sin6_family = AF_INET6; 534 dst6->sin6_addr = ip6->ip6_dst; 535#ifdef SCOPEDROUTING 536 ip6_forward_rt.ro_dst.sin6_scope_id = 537 in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_dst); 538#endif 539 540 rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING); 541 } 542 543#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key)) 544 545 /* 546 * Accept the packet if the forwarding interface to the destination 547 * according to the routing table is the loopback interface, 548 * unless the associated route has a gateway. 549 * Note that this approach causes to accept a packet if there is a 550 * route to the loopback interface for the destination of the packet. 551 * But we think it's even useful in some situations, e.g. when using 552 * a special daemon which wants to intercept the packet. 553 * 554 * XXX: some OSes automatically make a cloned route for the destination 555 * of an outgoing packet. If the outgoing interface of the packet 556 * is a loopback one, the kernel would consider the packet to be 557 * accepted, even if we have no such address assinged on the interface. 558 * We check the cloned flag of the route entry to reject such cases, 559 * assuming that route entries for our own addresses are not made by 560 * cloning (it should be true because in6_addloop explicitly installs 561 * the host route). However, we might have to do an explicit check 562 * while it would be less efficient. Or, should we rather install a 563 * reject route for such a case? 564 */ 565 if (ip6_forward_rt.ro_rt && 566 (ip6_forward_rt.ro_rt->rt_flags & 567 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && 568#ifdef RTF_WASCLONED 569 !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) && 570#endif 571#ifdef RTF_CLONED 572 !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) && 573#endif 574#if 0 575 /* 576 * The check below is redundant since the comparison of 577 * the destination and the key of the rtentry has 578 * already done through looking up the routing table. 579 */ 580 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 581 &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) 582#endif 583 ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) { 584 struct in6_ifaddr *ia6 = 585 (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa; 586 587 /* 588 * record address information into m_aux. 589 */ 590 (void)ip6_setdstifaddr(m, ia6); 591 592 /* 593 * packets to a tentative, duplicated, or somehow invalid 594 * address must not be accepted. 595 */ 596 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 597 /* this address is ready */ 598 ours = 1; 599 deliverifp = ia6->ia_ifp; /* correct? */ 600 /* Count the packet in the ip address stats */ 601 ia6->ia_ifa.if_ipackets++; 602 ia6->ia_ifa.if_ibytes += m->m_pkthdr.len; 603 goto hbhcheck; 604 } else { 605 /* address is not ready, so discard the packet. */ 606 nd6log((LOG_INFO, 607 "ip6_input: packet to an unready address %s->%s\n", 608 ip6_sprintf(&ip6->ip6_src), 609 ip6_sprintf(&ip6->ip6_dst))); 610 611 goto bad; 612 } 613 } 614 } /* XXX indentation (see above) */ 615 616 /* 617 * FAITH(Firewall Aided Internet Translator) 618 */ 619 if (ip6_keepfaith) { 620 if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp 621 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) { 622 /* XXX do we need more sanity checks? */ 623 ours = 1; 624 deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */ 625 goto hbhcheck; 626 } 627 } 628 629 /* 630 * Now there is no reason to process the packet if it's not our own 631 * and we're not a router. 632 */ 633 if (!ip6_forwarding) { 634 ip6stat.ip6s_cantforward++; 635 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 636 goto bad; 637 } 638 639 hbhcheck: 640 /* 641 * record address information into m_aux, if we don't have one yet. 642 * note that we are unable to record it, if the address is not listed 643 * as our interface address (e.g. multicast addresses, addresses 644 * within FAITH prefixes and such). 645 */ 646 if (deliverifp && !ip6_getdstifaddr(m)) { 647 struct in6_ifaddr *ia6; 648 649 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); 650 if (ia6) { 651 if (!ip6_setdstifaddr(m, ia6)) { 652 /* 653 * XXX maybe we should drop the packet here, 654 * as we could not provide enough information 655 * to the upper layers. 656 */ 657 } 658 } 659 } 660 661 /* 662 * Process Hop-by-Hop options header if it's contained. 663 * m may be modified in ip6_hopopts_input(). 664 * If a JumboPayload option is included, plen will also be modified. 665 */ 666 plen = (u_int32_t)ntohs(ip6->ip6_plen); 667 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 668 struct ip6_hbh *hbh; 669 670 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) { 671#if 0 /*touches NULL pointer*/ 672 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 673#endif 674 return; /* m have already been freed */ 675 } 676 677 /* adjust pointer */ 678 ip6 = mtod(m, struct ip6_hdr *); 679 680 /* 681 * if the payload length field is 0 and the next header field 682 * indicates Hop-by-Hop Options header, then a Jumbo Payload 683 * option MUST be included. 684 */ 685 if (ip6->ip6_plen == 0 && plen == 0) { 686 /* 687 * Note that if a valid jumbo payload option is 688 * contained, ip6_hoptops_input() must set a valid 689 * (non-zero) payload length to the variable plen. 690 */ 691 ip6stat.ip6s_badoptions++; 692 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 693 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 694 icmp6_error(m, ICMP6_PARAM_PROB, 695 ICMP6_PARAMPROB_HEADER, 696 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6); 697 return; 698 } 699#ifndef PULLDOWN_TEST 700 /* ip6_hopopts_input() ensures that mbuf is contiguous */ 701 hbh = (struct ip6_hbh *)(ip6 + 1); 702#else 703 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 704 sizeof(struct ip6_hbh)); 705 if (hbh == NULL) { 706 ip6stat.ip6s_tooshort++; 707 return; 708 } 709#endif 710 nxt = hbh->ip6h_nxt; 711 712 /* 713 * accept the packet if a router alert option is included 714 * and we act as an IPv6 router. 715 */ 716 if (rtalert != ~0 && ip6_forwarding) 717 ours = 1; 718 } else 719 nxt = ip6->ip6_nxt; 720 721 /* 722 * Check that the amount of data in the buffers 723 * is as at least much as the IPv6 header would have us expect. 724 * Trim mbufs if longer than we expect. 725 * Drop packet if shorter than we expect. 726 */ 727 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 728 ip6stat.ip6s_tooshort++; 729 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 730 goto bad; 731 } 732 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 733 if (m->m_len == m->m_pkthdr.len) { 734 m->m_len = sizeof(struct ip6_hdr) + plen; 735 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 736 } else 737 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); 738 } 739 740 /* 741 * Forward if desirable. 742 */ 743 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 744 /* 745 * If we are acting as a multicast router, all 746 * incoming multicast packets are passed to the 747 * kernel-level multicast forwarding function. 748 * The packet is returned (relatively) intact; if 749 * ip6_mforward() returns a non-zero value, the packet 750 * must be discarded, else it may be accepted below. 751 */ 752 if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) { 753 ip6stat.ip6s_cantforward++; 754 m_freem(m); 755 return; 756 } 757 if (!ours) { 758 m_freem(m); 759 return; 760 } 761 } else if (!ours) { 762 ip6_forward(m, 0); 763 return; 764 } 765 766 ip6 = mtod(m, struct ip6_hdr *); 767 768 /* 769 * Malicious party may be able to use IPv4 mapped addr to confuse 770 * tcp/udp stack and bypass security checks (act as if it was from 771 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. 772 * 773 * For SIIT end node behavior, you may want to disable the check. 774 * However, you will become vulnerable to attacks using IPv4 mapped 775 * source. 776 */ 777 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 778 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 779 ip6stat.ip6s_badscope++; 780 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 781 goto bad; 782 } 783 784 /* 785 * Tell launch routine the next header 786 */ 787 ip6stat.ip6s_delivered++; 788 in6_ifstat_inc(deliverifp, ifs6_in_deliver); 789 nest = 0; 790 791 while (nxt != IPPROTO_DONE) { 792 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { 793 ip6stat.ip6s_toomanyhdr++; 794 goto bad; 795 } 796 797 /* 798 * protection against faulty packet - there should be 799 * more sanity checks in header chain processing. 800 */ 801 if (m->m_pkthdr.len < off) { 802 ip6stat.ip6s_tooshort++; 803 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 804 goto bad; 805 } 806 807#if 0 808 /* 809 * do we need to do it for every header? yeah, other 810 * functions can play with it (like re-allocate and copy). 811 */ 812 mhist = ip6_addaux(m); 813 if (mhist && M_TRAILINGSPACE(mhist) >= sizeof(nxt)) { 814 hist = mtod(mhist, caddr_t) + mhist->m_len; 815 bcopy(&nxt, hist, sizeof(nxt)); 816 mhist->m_len += sizeof(nxt); 817 } else { 818 ip6stat.ip6s_toomanyhdr++; 819 goto bad; 820 } 821#endif 822 823#ifdef IPSEC 824 /* 825 * enforce IPsec policy checking if we are seeing last header. 826 * note that we do not visit this with protocols with pcb layer 827 * code - like udp/tcp/raw ip. 828 */ 829 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 && 830 ipsec6_in_reject(m, NULL)) { 831 ipsec6stat.in_polvio++; 832 goto bad; 833 } 834#endif 835 836 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt); 837 } 838 return; 839 bad: 840 m_freem(m); 841} 842 843/* 844 * set/grab in6_ifaddr correspond to IPv6 destination address. 845 * XXX backward compatibility wrapper 846 */ 847static struct ip6aux * 848ip6_setdstifaddr(m, ia6) 849 struct mbuf *m; 850 struct in6_ifaddr *ia6; 851{ 852 struct ip6aux *n; 853 854 n = ip6_addaux(m); 855 if (n) 856 n->ip6a_dstia6 = ia6; 857 return n; /* NULL if failed to set */ 858} 859 860struct in6_ifaddr * 861ip6_getdstifaddr(m) 862 struct mbuf *m; 863{ 864 struct ip6aux *n; 865 866 n = ip6_findaux(m); 867 if (n) 868 return n->ip6a_dstia6; 869 else 870 return NULL; 871} 872 873/* 874 * Hop-by-Hop options header processing. If a valid jumbo payload option is 875 * included, the real payload length will be stored in plenp. 876 */ 877static int 878ip6_hopopts_input(plenp, rtalertp, mp, offp) 879 u_int32_t *plenp; 880 u_int32_t *rtalertp; /* XXX: should be stored more smart way */ 881 struct mbuf **mp; 882 int *offp; 883{ 884 struct mbuf *m = *mp; 885 int off = *offp, hbhlen; 886 struct ip6_hbh *hbh; 887 u_int8_t *opt; 888 889 /* validation of the length of the header */ 890#ifndef PULLDOWN_TEST 891 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1); 892 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 893 hbhlen = (hbh->ip6h_len + 1) << 3; 894 895 IP6_EXTHDR_CHECK(m, off, hbhlen, -1); 896 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 897#else 898 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, 899 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); 900 if (hbh == NULL) { 901 ip6stat.ip6s_tooshort++; 902 return -1; 903 } 904 hbhlen = (hbh->ip6h_len + 1) << 3; 905 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 906 hbhlen); 907 if (hbh == NULL) { 908 ip6stat.ip6s_tooshort++; 909 return -1; 910 } 911#endif 912 off += hbhlen; 913 hbhlen -= sizeof(struct ip6_hbh); 914 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh); 915 916 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 917 hbhlen, rtalertp, plenp) < 0) 918 return(-1); 919 920 *offp = off; 921 *mp = m; 922 return(0); 923} 924 925/* 926 * Search header for all Hop-by-hop options and process each option. 927 * This function is separate from ip6_hopopts_input() in order to 928 * handle a case where the sending node itself process its hop-by-hop 929 * options header. In such a case, the function is called from ip6_output(). 930 * 931 * The function assumes that hbh header is located right after the IPv6 header 932 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 933 * opthead + hbhlen is located in continuous memory region. 934 */ 935int 936ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp) 937 struct mbuf *m; 938 u_int8_t *opthead; 939 int hbhlen; 940 u_int32_t *rtalertp; 941 u_int32_t *plenp; 942{ 943 struct ip6_hdr *ip6; 944 int optlen = 0; 945 u_int8_t *opt = opthead; 946 u_int16_t rtalert_val; 947 u_int32_t jumboplen; 948 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); 949 950 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 951 switch (*opt) { 952 case IP6OPT_PAD1: 953 optlen = 1; 954 break; 955 case IP6OPT_PADN: 956 if (hbhlen < IP6OPT_MINLEN) { 957 ip6stat.ip6s_toosmall++; 958 goto bad; 959 } 960 optlen = *(opt + 1) + 2; 961 break; 962 case IP6OPT_RTALERT: 963 /* XXX may need check for alignment */ 964 if (hbhlen < IP6OPT_RTALERT_LEN) { 965 ip6stat.ip6s_toosmall++; 966 goto bad; 967 } 968 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 969 /* XXX stat */ 970 icmp6_error(m, ICMP6_PARAM_PROB, 971 ICMP6_PARAMPROB_HEADER, 972 erroff + opt + 1 - opthead); 973 return(-1); 974 } 975 optlen = IP6OPT_RTALERT_LEN; 976 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 977 *rtalertp = ntohs(rtalert_val); 978 break; 979 case IP6OPT_JUMBO: 980 /* XXX may need check for alignment */ 981 if (hbhlen < IP6OPT_JUMBO_LEN) { 982 ip6stat.ip6s_toosmall++; 983 goto bad; 984 } 985 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { 986 /* XXX stat */ 987 icmp6_error(m, ICMP6_PARAM_PROB, 988 ICMP6_PARAMPROB_HEADER, 989 erroff + opt + 1 - opthead); 990 return(-1); 991 } 992 optlen = IP6OPT_JUMBO_LEN; 993 994 /* 995 * IPv6 packets that have non 0 payload length 996 * must not contain a jumbo payload option. 997 */ 998 ip6 = mtod(m, struct ip6_hdr *); 999 if (ip6->ip6_plen) { 1000 ip6stat.ip6s_badoptions++; 1001 icmp6_error(m, ICMP6_PARAM_PROB, 1002 ICMP6_PARAMPROB_HEADER, 1003 erroff + opt - opthead); 1004 return(-1); 1005 } 1006 1007 /* 1008 * We may see jumbolen in unaligned location, so 1009 * we'd need to perform bcopy(). 1010 */ 1011 bcopy(opt + 2, &jumboplen, sizeof(jumboplen)); 1012 jumboplen = (u_int32_t)htonl(jumboplen); 1013 1014#if 1 1015 /* 1016 * if there are multiple jumbo payload options, 1017 * *plenp will be non-zero and the packet will be 1018 * rejected. 1019 * the behavior may need some debate in ipngwg - 1020 * multiple options does not make sense, however, 1021 * there's no explicit mention in specification. 1022 */ 1023 if (*plenp != 0) { 1024 ip6stat.ip6s_badoptions++; 1025 icmp6_error(m, ICMP6_PARAM_PROB, 1026 ICMP6_PARAMPROB_HEADER, 1027 erroff + opt + 2 - opthead); 1028 return(-1); 1029 } 1030#endif 1031 1032 /* 1033 * jumbo payload length must be larger than 65535. 1034 */ 1035 if (jumboplen <= IPV6_MAXPACKET) { 1036 ip6stat.ip6s_badoptions++; 1037 icmp6_error(m, ICMP6_PARAM_PROB, 1038 ICMP6_PARAMPROB_HEADER, 1039 erroff + opt + 2 - opthead); 1040 return(-1); 1041 } 1042 *plenp = jumboplen; 1043 1044 break; 1045 default: /* unknown option */ 1046 if (hbhlen < IP6OPT_MINLEN) { 1047 ip6stat.ip6s_toosmall++; 1048 goto bad; 1049 } 1050 optlen = ip6_unknown_opt(opt, m, 1051 erroff + opt - opthead); 1052 if (optlen == -1) 1053 return(-1); 1054 optlen += 2; 1055 break; 1056 } 1057 } 1058 1059 return(0); 1060 1061 bad: 1062 m_freem(m); 1063 return(-1); 1064} 1065 1066/* 1067 * Unknown option processing. 1068 * The third argument `off' is the offset from the IPv6 header to the option, 1069 * which is necessary if the IPv6 header the and option header and IPv6 header 1070 * is not continuous in order to return an ICMPv6 error. 1071 */ 1072int 1073ip6_unknown_opt(optp, m, off) 1074 u_int8_t *optp; 1075 struct mbuf *m; 1076 int off; 1077{ 1078 struct ip6_hdr *ip6; 1079 1080 switch (IP6OPT_TYPE(*optp)) { 1081 case IP6OPT_TYPE_SKIP: /* ignore the option */ 1082 return((int)*(optp + 1)); 1083 case IP6OPT_TYPE_DISCARD: /* silently discard */ 1084 m_freem(m); 1085 return(-1); 1086 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 1087 ip6stat.ip6s_badoptions++; 1088 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 1089 return(-1); 1090 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 1091 ip6stat.ip6s_badoptions++; 1092 ip6 = mtod(m, struct ip6_hdr *); 1093 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1094 (m->m_flags & (M_BCAST|M_MCAST))) 1095 m_freem(m); 1096 else 1097 icmp6_error(m, ICMP6_PARAM_PROB, 1098 ICMP6_PARAMPROB_OPTION, off); 1099 return(-1); 1100 } 1101 1102 m_freem(m); /* XXX: NOTREACHED */ 1103 return(-1); 1104} 1105 1106/* 1107 * Create the "control" list for this pcb. 1108 * The function will not modify mbuf chain at all. 1109 * 1110 * with KAME mbuf chain restriction: 1111 * The routine will be called from upper layer handlers like tcp6_input(). 1112 * Thus the routine assumes that the caller (tcp6_input) have already 1113 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the 1114 * very first mbuf on the mbuf chain. 1115 */ 1116void 1117ip6_savecontrol(in6p, mp, ip6, m) 1118 struct inpcb *in6p; 1119 struct mbuf **mp; 1120 struct ip6_hdr *ip6; 1121 struct mbuf *m; 1122{ 1123#if __FreeBSD_version >= 500000 1124 struct thread *td = curthread; /* XXX */ 1125#else 1126 struct proc *td = curproc; /* XXX */ 1127#endif 1128 int privileged = 0; 1129 int rthdr_exist = 0; 1130 1131 1132 if (td && !suser(td)) 1133 privileged++; 1134 1135#ifdef SO_TIMESTAMP 1136 if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) { 1137 struct timeval tv; 1138 1139 microtime(&tv); 1140 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1141 SCM_TIMESTAMP, SOL_SOCKET); 1142 if (*mp) { 1143 mp = &(*mp)->m_next; 1144 } 1145 } 1146#endif 1147 1148 /* RFC 2292 sec. 5 */ 1149 if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) { 1150 struct in6_pktinfo pi6; 1151 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 1152 if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr)) 1153 pi6.ipi6_addr.s6_addr16[1] = 0; 1154 pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif) 1155 ? m->m_pkthdr.rcvif->if_index 1156 : 0; 1157 *mp = sbcreatecontrol((caddr_t) &pi6, 1158 sizeof(struct in6_pktinfo), IPV6_PKTINFO, 1159 IPPROTO_IPV6); 1160 if (*mp) 1161 mp = &(*mp)->m_next; 1162 } 1163 1164 if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) { 1165 int hlim = ip6->ip6_hlim & 0xff; 1166 *mp = sbcreatecontrol((caddr_t) &hlim, 1167 sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6); 1168 if (*mp) 1169 mp = &(*mp)->m_next; 1170 } 1171 1172 /* 1173 * IPV6_HOPOPTS socket option. We require super-user privilege 1174 * for the option, but it might be too strict, since there might 1175 * be some hop-by-hop options which can be returned to normal user. 1176 * See RFC 2292 section 6. 1177 */ 1178 if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) { 1179 /* 1180 * Check if a hop-by-hop options header is contatined in the 1181 * received packet, and if so, store the options as ancillary 1182 * data. Note that a hop-by-hop options header must be 1183 * just after the IPv6 header, which fact is assured through 1184 * the IPv6 input processing. 1185 */ 1186 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1187 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 1188 struct ip6_hbh *hbh; 1189 int hbhlen = 0; 1190#ifdef PULLDOWN_TEST 1191 struct mbuf *ext; 1192#endif 1193 1194#ifndef PULLDOWN_TEST 1195 hbh = (struct ip6_hbh *)(ip6 + 1); 1196 hbhlen = (hbh->ip6h_len + 1) << 3; 1197#else 1198 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), 1199 ip6->ip6_nxt); 1200 if (ext == NULL) { 1201 ip6stat.ip6s_tooshort++; 1202 return; 1203 } 1204 hbh = mtod(ext, struct ip6_hbh *); 1205 hbhlen = (hbh->ip6h_len + 1) << 3; 1206 if (hbhlen != ext->m_len) { 1207 m_freem(ext); 1208 ip6stat.ip6s_tooshort++; 1209 return; 1210 } 1211#endif 1212 1213 /* 1214 * XXX: We copy whole the header even if a jumbo 1215 * payload option is included, which option is to 1216 * be removed before returning in the RFC 2292. 1217 * Note: this constraint is removed in 2292bis. 1218 */ 1219 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, 1220 IPV6_HOPOPTS, IPPROTO_IPV6); 1221 if (*mp) 1222 mp = &(*mp)->m_next; 1223#ifdef PULLDOWN_TEST 1224 m_freem(ext); 1225#endif 1226 } 1227 } 1228 1229 /* IPV6_DSTOPTS and IPV6_RTHDR socket options */ 1230 if ((in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) { 1231 int proto, off, nxt; 1232 1233 /* 1234 * go through the header chain to see if a routing header is 1235 * contained in the packet. We need this information to store 1236 * destination options headers (if any) properly. 1237 * XXX: performance issue. We should record this info when 1238 * processing extension headers in incoming routine. 1239 * (todo) use m_aux? 1240 */ 1241 proto = IPPROTO_IPV6; 1242 off = 0; 1243 nxt = -1; 1244 while (1) { 1245 int newoff; 1246 1247 newoff = ip6_nexthdr(m, off, proto, &nxt); 1248 if (newoff < 0) 1249 break; 1250 if (newoff < off) /* invalid, check for safety */ 1251 break; 1252 if ((proto = nxt) == IPPROTO_ROUTING) { 1253 rthdr_exist = 1; 1254 break; 1255 } 1256 off = newoff; 1257 } 1258 } 1259 1260 if ((in6p->in6p_flags & 1261 (IN6P_RTHDR | IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) { 1262 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1263 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 1264 1265 /* 1266 * Search for destination options headers or routing 1267 * header(s) through the header chain, and stores each 1268 * header as ancillary data. 1269 * Note that the order of the headers remains in 1270 * the chain of ancillary data. 1271 */ 1272 while (1) { /* is explicit loop prevention necessary? */ 1273 struct ip6_ext *ip6e = NULL; 1274 int elen; 1275#ifdef PULLDOWN_TEST 1276 struct mbuf *ext = NULL; 1277#endif 1278 1279 /* 1280 * if it is not an extension header, don't try to 1281 * pull it from the chain. 1282 */ 1283 switch (nxt) { 1284 case IPPROTO_DSTOPTS: 1285 case IPPROTO_ROUTING: 1286 case IPPROTO_HOPOPTS: 1287 case IPPROTO_AH: /* is it possible? */ 1288 break; 1289 default: 1290 goto loopend; 1291 } 1292 1293#ifndef PULLDOWN_TEST 1294 if (off + sizeof(*ip6e) > m->m_len) 1295 goto loopend; 1296 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 1297 if (nxt == IPPROTO_AH) 1298 elen = (ip6e->ip6e_len + 2) << 2; 1299 else 1300 elen = (ip6e->ip6e_len + 1) << 3; 1301 if (off + elen > m->m_len) 1302 goto loopend; 1303#else 1304 ext = ip6_pullexthdr(m, off, nxt); 1305 if (ext == NULL) { 1306 ip6stat.ip6s_tooshort++; 1307 return; 1308 } 1309 ip6e = mtod(ext, struct ip6_ext *); 1310 if (nxt == IPPROTO_AH) 1311 elen = (ip6e->ip6e_len + 2) << 2; 1312 else 1313 elen = (ip6e->ip6e_len + 1) << 3; 1314 if (elen != ext->m_len) { 1315 m_freem(ext); 1316 ip6stat.ip6s_tooshort++; 1317 return; 1318 } 1319#endif 1320 1321 switch (nxt) { 1322 case IPPROTO_DSTOPTS: 1323 if ((in6p->in6p_flags & IN6P_DSTOPTS) == 0) 1324 break; 1325 1326 /* 1327 * We also require super-user privilege for 1328 * the option. 1329 * See the comments on IN6_HOPOPTS. 1330 */ 1331 if (!privileged) 1332 break; 1333 1334 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1335 IPV6_DSTOPTS, 1336 IPPROTO_IPV6); 1337 if (*mp) 1338 mp = &(*mp)->m_next; 1339 break; 1340 case IPPROTO_ROUTING: 1341 if (!in6p->in6p_flags & IN6P_RTHDR) 1342 break; 1343 1344 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1345 IPV6_RTHDR, 1346 IPPROTO_IPV6); 1347 if (*mp) 1348 mp = &(*mp)->m_next; 1349 break; 1350 case IPPROTO_HOPOPTS: 1351 case IPPROTO_AH: /* is it possible? */ 1352 break; 1353 1354 default: 1355 /* 1356 * other cases have been filtered in the above. 1357 * none will visit this case. here we supply 1358 * the code just in case (nxt overwritten or 1359 * other cases). 1360 */ 1361#ifdef PULLDOWN_TEST 1362 m_freem(ext); 1363#endif 1364 goto loopend; 1365 1366 } 1367 1368 /* proceed with the next header. */ 1369 off += elen; 1370 nxt = ip6e->ip6e_nxt; 1371 ip6e = NULL; 1372#ifdef PULLDOWN_TEST 1373 m_freem(ext); 1374 ext = NULL; 1375#endif 1376 } 1377 loopend: 1378 ; 1379 } 1380 1381} 1382 1383#ifdef PULLDOWN_TEST 1384/* 1385 * pull single extension header from mbuf chain. returns single mbuf that 1386 * contains the result, or NULL on error. 1387 */ 1388static struct mbuf * 1389ip6_pullexthdr(m, off, nxt) 1390 struct mbuf *m; 1391 size_t off; 1392 int nxt; 1393{ 1394 struct ip6_ext ip6e; 1395 size_t elen; 1396 struct mbuf *n; 1397 1398#ifdef DIAGNOSTIC 1399 switch (nxt) { 1400 case IPPROTO_DSTOPTS: 1401 case IPPROTO_ROUTING: 1402 case IPPROTO_HOPOPTS: 1403 case IPPROTO_AH: /* is it possible? */ 1404 break; 1405 default: 1406 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 1407 } 1408#endif 1409 1410 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1411 if (nxt == IPPROTO_AH) 1412 elen = (ip6e.ip6e_len + 2) << 2; 1413 else 1414 elen = (ip6e.ip6e_len + 1) << 3; 1415 1416 MGET(n, M_DONTWAIT, MT_DATA); 1417 if (n && elen >= MLEN) { 1418 MCLGET(n, M_DONTWAIT); 1419 if ((n->m_flags & M_EXT) == 0) { 1420 m_free(n); 1421 n = NULL; 1422 } 1423 } 1424 if (!n) 1425 return NULL; 1426 1427 n->m_len = 0; 1428 if (elen >= M_TRAILINGSPACE(n)) { 1429 m_free(n); 1430 return NULL; 1431 } 1432 1433 m_copydata(m, off, elen, mtod(n, caddr_t)); 1434 n->m_len = elen; 1435 return n; 1436} 1437#endif 1438 1439/* 1440 * Get pointer to the previous header followed by the header 1441 * currently processed. 1442 * XXX: This function supposes that 1443 * M includes all headers, 1444 * the next header field and the header length field of each header 1445 * are valid, and 1446 * the sum of each header length equals to OFF. 1447 * Because of these assumptions, this function must be called very 1448 * carefully. Moreover, it will not be used in the near future when 1449 * we develop `neater' mechanism to process extension headers. 1450 */ 1451char * 1452ip6_get_prevhdr(m, off) 1453 struct mbuf *m; 1454 int off; 1455{ 1456 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1457 1458 if (off == sizeof(struct ip6_hdr)) 1459 return(&ip6->ip6_nxt); 1460 else { 1461 int len, nxt; 1462 struct ip6_ext *ip6e = NULL; 1463 1464 nxt = ip6->ip6_nxt; 1465 len = sizeof(struct ip6_hdr); 1466 while (len < off) { 1467 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); 1468 1469 switch (nxt) { 1470 case IPPROTO_FRAGMENT: 1471 len += sizeof(struct ip6_frag); 1472 break; 1473 case IPPROTO_AH: 1474 len += (ip6e->ip6e_len + 2) << 2; 1475 break; 1476 default: 1477 len += (ip6e->ip6e_len + 1) << 3; 1478 break; 1479 } 1480 nxt = ip6e->ip6e_nxt; 1481 } 1482 if (ip6e) 1483 return(&ip6e->ip6e_nxt); 1484 else 1485 return NULL; 1486 } 1487} 1488 1489/* 1490 * get next header offset. m will be retained. 1491 */ 1492int 1493ip6_nexthdr(m, off, proto, nxtp) 1494 struct mbuf *m; 1495 int off; 1496 int proto; 1497 int *nxtp; 1498{ 1499 struct ip6_hdr ip6; 1500 struct ip6_ext ip6e; 1501 struct ip6_frag fh; 1502 1503 /* just in case */ 1504 if (m == NULL) 1505 panic("ip6_nexthdr: m == NULL"); 1506 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 1507 return -1; 1508 1509 switch (proto) { 1510 case IPPROTO_IPV6: 1511 if (m->m_pkthdr.len < off + sizeof(ip6)) 1512 return -1; 1513 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); 1514 if (nxtp) 1515 *nxtp = ip6.ip6_nxt; 1516 off += sizeof(ip6); 1517 return off; 1518 1519 case IPPROTO_FRAGMENT: 1520 /* 1521 * terminate parsing if it is not the first fragment, 1522 * it does not make sense to parse through it. 1523 */ 1524 if (m->m_pkthdr.len < off + sizeof(fh)) 1525 return -1; 1526 m_copydata(m, off, sizeof(fh), (caddr_t)&fh); 1527 if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0) 1528 return -1; 1529 if (nxtp) 1530 *nxtp = fh.ip6f_nxt; 1531 off += sizeof(struct ip6_frag); 1532 return off; 1533 1534 case IPPROTO_AH: 1535 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1536 return -1; 1537 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1538 if (nxtp) 1539 *nxtp = ip6e.ip6e_nxt; 1540 off += (ip6e.ip6e_len + 2) << 2; 1541 return off; 1542 1543 case IPPROTO_HOPOPTS: 1544 case IPPROTO_ROUTING: 1545 case IPPROTO_DSTOPTS: 1546 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1547 return -1; 1548 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1549 if (nxtp) 1550 *nxtp = ip6e.ip6e_nxt; 1551 off += (ip6e.ip6e_len + 1) << 3; 1552 return off; 1553 1554 case IPPROTO_NONE: 1555 case IPPROTO_ESP: 1556 case IPPROTO_IPCOMP: 1557 /* give up */ 1558 return -1; 1559 1560 default: 1561 return -1; 1562 } 1563 1564 return -1; 1565} 1566 1567/* 1568 * get offset for the last header in the chain. m will be kept untainted. 1569 */ 1570int 1571ip6_lasthdr(m, off, proto, nxtp) 1572 struct mbuf *m; 1573 int off; 1574 int proto; 1575 int *nxtp; 1576{ 1577 int newoff; 1578 int nxt; 1579 1580 if (!nxtp) { 1581 nxt = -1; 1582 nxtp = &nxt; 1583 } 1584 while (1) { 1585 newoff = ip6_nexthdr(m, off, proto, nxtp); 1586 if (newoff < 0) 1587 return off; 1588 else if (newoff < off) 1589 return -1; /* invalid */ 1590 else if (newoff == off) 1591 return newoff; 1592 1593 off = newoff; 1594 proto = *nxtp; 1595 } 1596} 1597 1598struct ip6aux * 1599ip6_addaux(m) 1600 struct mbuf *m; 1601{ 1602 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1603 if (!tag) { 1604 tag = m_tag_get(PACKET_TAG_IPV6_INPUT, 1605 sizeof (struct ip6aux), 1606 M_NOWAIT); 1607 if (tag) 1608 m_tag_prepend(m, tag); 1609 } 1610 if (tag) 1611 bzero(tag+1, sizeof (struct ip6aux)); 1612 return tag ? (struct ip6aux*)(tag+1) : NULL; 1613} 1614 1615struct ip6aux * 1616ip6_findaux(m) 1617 struct mbuf *m; 1618{ 1619 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1620 return tag ? (struct ip6aux*)(tag+1) : NULL; 1621} 1622 1623void 1624ip6_delaux(m) 1625 struct mbuf *m; 1626{ 1627 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1628 if (tag) 1629 m_tag_delete(m, tag); 1630} 1631 1632/* 1633 * System control for IP6 1634 */ 1635 1636u_char inet6ctlerrmap[PRC_NCMDS] = { 1637 0, 0, 0, 0, 1638 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1639 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1640 EMSGSIZE, EHOSTUNREACH, 0, 0, 1641 0, 0, 0, 0, 1642 ENOPROTOOPT 1643};
| 355#endif /* PFIL_HOOKS */ 356 357 ip6stat.ip6s_nxthist[ip6->ip6_nxt]++; 358 359 /* 360 * Check with the firewall... 361 */ 362 if (ip6_fw_enable && ip6_fw_chk_ptr) { 363 u_short port = 0; 364 /* If ipfw says divert, we have to just drop packet */ 365 /* use port as a dummy argument */ 366 if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) { 367 m_freem(m); 368 m = NULL; 369 } 370 if (!m) 371 return; 372 } 373 374 /* 375 * Check against address spoofing/corruption. 376 */ 377 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || 378 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { 379 /* 380 * XXX: "badscope" is not very suitable for a multicast source. 381 */ 382 ip6stat.ip6s_badscope++; 383 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 384 goto bad; 385 } 386 if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) || 387 IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) && 388 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 389 ip6stat.ip6s_badscope++; 390 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 391 goto bad; 392 } 393 394 /* 395 * The following check is not documented in specs. A malicious 396 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack 397 * and bypass security checks (act as if it was from 127.0.0.1 by using 398 * IPv6 src ::ffff:127.0.0.1). Be cautious. 399 * 400 * This check chokes if we are in an SIIT cloud. As none of BSDs 401 * support IPv4-less kernel compilation, we cannot support SIIT 402 * environment at all. So, it makes more sense for us to reject any 403 * malicious packets for non-SIIT environment, than try to do a 404 * partical support for SIIT environment. 405 */ 406 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 407 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 408 ip6stat.ip6s_badscope++; 409 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 410 goto bad; 411 } 412#if 0 413 /* 414 * Reject packets with IPv4 compatible addresses (auto tunnel). 415 * 416 * The code forbids auto tunnel relay case in RFC1933 (the check is 417 * stronger than RFC1933). We may want to re-enable it if mech-xx 418 * is revised to forbid relaying case. 419 */ 420 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) || 421 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) { 422 ip6stat.ip6s_badscope++; 423 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 424 goto bad; 425 } 426#endif 427 428 /* drop packets if interface ID portion is already filled */ 429 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 430 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) && 431 ip6->ip6_src.s6_addr16[1]) { 432 ip6stat.ip6s_badscope++; 433 goto bad; 434 } 435 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) && 436 ip6->ip6_dst.s6_addr16[1]) { 437 ip6stat.ip6s_badscope++; 438 goto bad; 439 } 440 } 441 442 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 443 ip6->ip6_src.s6_addr16[1] 444 = htons(m->m_pkthdr.rcvif->if_index); 445 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 446 ip6->ip6_dst.s6_addr16[1] 447 = htons(m->m_pkthdr.rcvif->if_index); 448 449#if 0 /* this case seems to be unnecessary. (jinmei, 20010401) */ 450 /* 451 * We use rt->rt_ifp to determine if the address is ours or not. 452 * If rt_ifp is lo0, the address is ours. 453 * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0, 454 * so any address under fe80::%lo0/64 will be mistakenly considered 455 * local. The special case is supplied to handle the case properly 456 * by actually looking at interface addresses 457 * (using in6ifa_ifpwithaddr). 458 */ 459 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 && 460 IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) { 461 if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) { 462 icmp6_error(m, ICMP6_DST_UNREACH, 463 ICMP6_DST_UNREACH_ADDR, 0); 464 /* m is already freed */ 465 return; 466 } 467 468 ours = 1; 469 deliverifp = m->m_pkthdr.rcvif; 470 goto hbhcheck; 471 } 472#endif 473 474 /* 475 * Multicast check 476 */ 477 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 478 struct in6_multi *in6m = 0; 479 480 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast); 481 /* 482 * See if we belong to the destination multicast group on the 483 * arrival interface. 484 */ 485 IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m); 486 if (in6m) 487 ours = 1; 488 else if (!ip6_mrouter) { 489 ip6stat.ip6s_notmember++; 490 ip6stat.ip6s_cantforward++; 491 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 492 goto bad; 493 } 494 deliverifp = m->m_pkthdr.rcvif; 495 goto hbhcheck; 496 } 497 498 /* 499 * Unicast check 500 */ 501 switch (ip6_ours_check_algorithm) { 502 default: 503 /* 504 * XXX: I intentionally broke our indentation rule here, 505 * since this switch-case is just for measurement and 506 * therefore should soon be removed. 507 */ 508 if (ip6_forward_rt.ro_rt != NULL && 509 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 && 510 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 511 &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr)) 512 ip6stat.ip6s_forward_cachehit++; 513 else { 514 struct sockaddr_in6 *dst6; 515 516 if (ip6_forward_rt.ro_rt) { 517 /* route is down or destination is different */ 518 ip6stat.ip6s_forward_cachemiss++; 519 RTFREE(ip6_forward_rt.ro_rt); 520 ip6_forward_rt.ro_rt = 0; 521 } 522 523 bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6)); 524 dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst; 525 dst6->sin6_len = sizeof(struct sockaddr_in6); 526 dst6->sin6_family = AF_INET6; 527 dst6->sin6_addr = ip6->ip6_dst; 528#ifdef SCOPEDROUTING 529 ip6_forward_rt.ro_dst.sin6_scope_id = 530 in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_dst); 531#endif 532 533 rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING); 534 } 535 536#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key)) 537 538 /* 539 * Accept the packet if the forwarding interface to the destination 540 * according to the routing table is the loopback interface, 541 * unless the associated route has a gateway. 542 * Note that this approach causes to accept a packet if there is a 543 * route to the loopback interface for the destination of the packet. 544 * But we think it's even useful in some situations, e.g. when using 545 * a special daemon which wants to intercept the packet. 546 * 547 * XXX: some OSes automatically make a cloned route for the destination 548 * of an outgoing packet. If the outgoing interface of the packet 549 * is a loopback one, the kernel would consider the packet to be 550 * accepted, even if we have no such address assinged on the interface. 551 * We check the cloned flag of the route entry to reject such cases, 552 * assuming that route entries for our own addresses are not made by 553 * cloning (it should be true because in6_addloop explicitly installs 554 * the host route). However, we might have to do an explicit check 555 * while it would be less efficient. Or, should we rather install a 556 * reject route for such a case? 557 */ 558 if (ip6_forward_rt.ro_rt && 559 (ip6_forward_rt.ro_rt->rt_flags & 560 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && 561#ifdef RTF_WASCLONED 562 !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) && 563#endif 564#ifdef RTF_CLONED 565 !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) && 566#endif 567#if 0 568 /* 569 * The check below is redundant since the comparison of 570 * the destination and the key of the rtentry has 571 * already done through looking up the routing table. 572 */ 573 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 574 &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) 575#endif 576 ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) { 577 struct in6_ifaddr *ia6 = 578 (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa; 579 580 /* 581 * record address information into m_aux. 582 */ 583 (void)ip6_setdstifaddr(m, ia6); 584 585 /* 586 * packets to a tentative, duplicated, or somehow invalid 587 * address must not be accepted. 588 */ 589 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 590 /* this address is ready */ 591 ours = 1; 592 deliverifp = ia6->ia_ifp; /* correct? */ 593 /* Count the packet in the ip address stats */ 594 ia6->ia_ifa.if_ipackets++; 595 ia6->ia_ifa.if_ibytes += m->m_pkthdr.len; 596 goto hbhcheck; 597 } else { 598 /* address is not ready, so discard the packet. */ 599 nd6log((LOG_INFO, 600 "ip6_input: packet to an unready address %s->%s\n", 601 ip6_sprintf(&ip6->ip6_src), 602 ip6_sprintf(&ip6->ip6_dst))); 603 604 goto bad; 605 } 606 } 607 } /* XXX indentation (see above) */ 608 609 /* 610 * FAITH(Firewall Aided Internet Translator) 611 */ 612 if (ip6_keepfaith) { 613 if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp 614 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) { 615 /* XXX do we need more sanity checks? */ 616 ours = 1; 617 deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */ 618 goto hbhcheck; 619 } 620 } 621 622 /* 623 * Now there is no reason to process the packet if it's not our own 624 * and we're not a router. 625 */ 626 if (!ip6_forwarding) { 627 ip6stat.ip6s_cantforward++; 628 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 629 goto bad; 630 } 631 632 hbhcheck: 633 /* 634 * record address information into m_aux, if we don't have one yet. 635 * note that we are unable to record it, if the address is not listed 636 * as our interface address (e.g. multicast addresses, addresses 637 * within FAITH prefixes and such). 638 */ 639 if (deliverifp && !ip6_getdstifaddr(m)) { 640 struct in6_ifaddr *ia6; 641 642 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); 643 if (ia6) { 644 if (!ip6_setdstifaddr(m, ia6)) { 645 /* 646 * XXX maybe we should drop the packet here, 647 * as we could not provide enough information 648 * to the upper layers. 649 */ 650 } 651 } 652 } 653 654 /* 655 * Process Hop-by-Hop options header if it's contained. 656 * m may be modified in ip6_hopopts_input(). 657 * If a JumboPayload option is included, plen will also be modified. 658 */ 659 plen = (u_int32_t)ntohs(ip6->ip6_plen); 660 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 661 struct ip6_hbh *hbh; 662 663 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) { 664#if 0 /*touches NULL pointer*/ 665 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 666#endif 667 return; /* m have already been freed */ 668 } 669 670 /* adjust pointer */ 671 ip6 = mtod(m, struct ip6_hdr *); 672 673 /* 674 * if the payload length field is 0 and the next header field 675 * indicates Hop-by-Hop Options header, then a Jumbo Payload 676 * option MUST be included. 677 */ 678 if (ip6->ip6_plen == 0 && plen == 0) { 679 /* 680 * Note that if a valid jumbo payload option is 681 * contained, ip6_hoptops_input() must set a valid 682 * (non-zero) payload length to the variable plen. 683 */ 684 ip6stat.ip6s_badoptions++; 685 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 686 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 687 icmp6_error(m, ICMP6_PARAM_PROB, 688 ICMP6_PARAMPROB_HEADER, 689 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6); 690 return; 691 } 692#ifndef PULLDOWN_TEST 693 /* ip6_hopopts_input() ensures that mbuf is contiguous */ 694 hbh = (struct ip6_hbh *)(ip6 + 1); 695#else 696 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 697 sizeof(struct ip6_hbh)); 698 if (hbh == NULL) { 699 ip6stat.ip6s_tooshort++; 700 return; 701 } 702#endif 703 nxt = hbh->ip6h_nxt; 704 705 /* 706 * accept the packet if a router alert option is included 707 * and we act as an IPv6 router. 708 */ 709 if (rtalert != ~0 && ip6_forwarding) 710 ours = 1; 711 } else 712 nxt = ip6->ip6_nxt; 713 714 /* 715 * Check that the amount of data in the buffers 716 * is as at least much as the IPv6 header would have us expect. 717 * Trim mbufs if longer than we expect. 718 * Drop packet if shorter than we expect. 719 */ 720 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 721 ip6stat.ip6s_tooshort++; 722 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 723 goto bad; 724 } 725 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 726 if (m->m_len == m->m_pkthdr.len) { 727 m->m_len = sizeof(struct ip6_hdr) + plen; 728 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 729 } else 730 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); 731 } 732 733 /* 734 * Forward if desirable. 735 */ 736 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 737 /* 738 * If we are acting as a multicast router, all 739 * incoming multicast packets are passed to the 740 * kernel-level multicast forwarding function. 741 * The packet is returned (relatively) intact; if 742 * ip6_mforward() returns a non-zero value, the packet 743 * must be discarded, else it may be accepted below. 744 */ 745 if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) { 746 ip6stat.ip6s_cantforward++; 747 m_freem(m); 748 return; 749 } 750 if (!ours) { 751 m_freem(m); 752 return; 753 } 754 } else if (!ours) { 755 ip6_forward(m, 0); 756 return; 757 } 758 759 ip6 = mtod(m, struct ip6_hdr *); 760 761 /* 762 * Malicious party may be able to use IPv4 mapped addr to confuse 763 * tcp/udp stack and bypass security checks (act as if it was from 764 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. 765 * 766 * For SIIT end node behavior, you may want to disable the check. 767 * However, you will become vulnerable to attacks using IPv4 mapped 768 * source. 769 */ 770 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 771 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 772 ip6stat.ip6s_badscope++; 773 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 774 goto bad; 775 } 776 777 /* 778 * Tell launch routine the next header 779 */ 780 ip6stat.ip6s_delivered++; 781 in6_ifstat_inc(deliverifp, ifs6_in_deliver); 782 nest = 0; 783 784 while (nxt != IPPROTO_DONE) { 785 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { 786 ip6stat.ip6s_toomanyhdr++; 787 goto bad; 788 } 789 790 /* 791 * protection against faulty packet - there should be 792 * more sanity checks in header chain processing. 793 */ 794 if (m->m_pkthdr.len < off) { 795 ip6stat.ip6s_tooshort++; 796 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 797 goto bad; 798 } 799 800#if 0 801 /* 802 * do we need to do it for every header? yeah, other 803 * functions can play with it (like re-allocate and copy). 804 */ 805 mhist = ip6_addaux(m); 806 if (mhist && M_TRAILINGSPACE(mhist) >= sizeof(nxt)) { 807 hist = mtod(mhist, caddr_t) + mhist->m_len; 808 bcopy(&nxt, hist, sizeof(nxt)); 809 mhist->m_len += sizeof(nxt); 810 } else { 811 ip6stat.ip6s_toomanyhdr++; 812 goto bad; 813 } 814#endif 815 816#ifdef IPSEC 817 /* 818 * enforce IPsec policy checking if we are seeing last header. 819 * note that we do not visit this with protocols with pcb layer 820 * code - like udp/tcp/raw ip. 821 */ 822 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 && 823 ipsec6_in_reject(m, NULL)) { 824 ipsec6stat.in_polvio++; 825 goto bad; 826 } 827#endif 828 829 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt); 830 } 831 return; 832 bad: 833 m_freem(m); 834} 835 836/* 837 * set/grab in6_ifaddr correspond to IPv6 destination address. 838 * XXX backward compatibility wrapper 839 */ 840static struct ip6aux * 841ip6_setdstifaddr(m, ia6) 842 struct mbuf *m; 843 struct in6_ifaddr *ia6; 844{ 845 struct ip6aux *n; 846 847 n = ip6_addaux(m); 848 if (n) 849 n->ip6a_dstia6 = ia6; 850 return n; /* NULL if failed to set */ 851} 852 853struct in6_ifaddr * 854ip6_getdstifaddr(m) 855 struct mbuf *m; 856{ 857 struct ip6aux *n; 858 859 n = ip6_findaux(m); 860 if (n) 861 return n->ip6a_dstia6; 862 else 863 return NULL; 864} 865 866/* 867 * Hop-by-Hop options header processing. If a valid jumbo payload option is 868 * included, the real payload length will be stored in plenp. 869 */ 870static int 871ip6_hopopts_input(plenp, rtalertp, mp, offp) 872 u_int32_t *plenp; 873 u_int32_t *rtalertp; /* XXX: should be stored more smart way */ 874 struct mbuf **mp; 875 int *offp; 876{ 877 struct mbuf *m = *mp; 878 int off = *offp, hbhlen; 879 struct ip6_hbh *hbh; 880 u_int8_t *opt; 881 882 /* validation of the length of the header */ 883#ifndef PULLDOWN_TEST 884 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1); 885 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 886 hbhlen = (hbh->ip6h_len + 1) << 3; 887 888 IP6_EXTHDR_CHECK(m, off, hbhlen, -1); 889 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 890#else 891 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, 892 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); 893 if (hbh == NULL) { 894 ip6stat.ip6s_tooshort++; 895 return -1; 896 } 897 hbhlen = (hbh->ip6h_len + 1) << 3; 898 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 899 hbhlen); 900 if (hbh == NULL) { 901 ip6stat.ip6s_tooshort++; 902 return -1; 903 } 904#endif 905 off += hbhlen; 906 hbhlen -= sizeof(struct ip6_hbh); 907 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh); 908 909 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 910 hbhlen, rtalertp, plenp) < 0) 911 return(-1); 912 913 *offp = off; 914 *mp = m; 915 return(0); 916} 917 918/* 919 * Search header for all Hop-by-hop options and process each option. 920 * This function is separate from ip6_hopopts_input() in order to 921 * handle a case where the sending node itself process its hop-by-hop 922 * options header. In such a case, the function is called from ip6_output(). 923 * 924 * The function assumes that hbh header is located right after the IPv6 header 925 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 926 * opthead + hbhlen is located in continuous memory region. 927 */ 928int 929ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp) 930 struct mbuf *m; 931 u_int8_t *opthead; 932 int hbhlen; 933 u_int32_t *rtalertp; 934 u_int32_t *plenp; 935{ 936 struct ip6_hdr *ip6; 937 int optlen = 0; 938 u_int8_t *opt = opthead; 939 u_int16_t rtalert_val; 940 u_int32_t jumboplen; 941 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); 942 943 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 944 switch (*opt) { 945 case IP6OPT_PAD1: 946 optlen = 1; 947 break; 948 case IP6OPT_PADN: 949 if (hbhlen < IP6OPT_MINLEN) { 950 ip6stat.ip6s_toosmall++; 951 goto bad; 952 } 953 optlen = *(opt + 1) + 2; 954 break; 955 case IP6OPT_RTALERT: 956 /* XXX may need check for alignment */ 957 if (hbhlen < IP6OPT_RTALERT_LEN) { 958 ip6stat.ip6s_toosmall++; 959 goto bad; 960 } 961 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 962 /* XXX stat */ 963 icmp6_error(m, ICMP6_PARAM_PROB, 964 ICMP6_PARAMPROB_HEADER, 965 erroff + opt + 1 - opthead); 966 return(-1); 967 } 968 optlen = IP6OPT_RTALERT_LEN; 969 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 970 *rtalertp = ntohs(rtalert_val); 971 break; 972 case IP6OPT_JUMBO: 973 /* XXX may need check for alignment */ 974 if (hbhlen < IP6OPT_JUMBO_LEN) { 975 ip6stat.ip6s_toosmall++; 976 goto bad; 977 } 978 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { 979 /* XXX stat */ 980 icmp6_error(m, ICMP6_PARAM_PROB, 981 ICMP6_PARAMPROB_HEADER, 982 erroff + opt + 1 - opthead); 983 return(-1); 984 } 985 optlen = IP6OPT_JUMBO_LEN; 986 987 /* 988 * IPv6 packets that have non 0 payload length 989 * must not contain a jumbo payload option. 990 */ 991 ip6 = mtod(m, struct ip6_hdr *); 992 if (ip6->ip6_plen) { 993 ip6stat.ip6s_badoptions++; 994 icmp6_error(m, ICMP6_PARAM_PROB, 995 ICMP6_PARAMPROB_HEADER, 996 erroff + opt - opthead); 997 return(-1); 998 } 999 1000 /* 1001 * We may see jumbolen in unaligned location, so 1002 * we'd need to perform bcopy(). 1003 */ 1004 bcopy(opt + 2, &jumboplen, sizeof(jumboplen)); 1005 jumboplen = (u_int32_t)htonl(jumboplen); 1006 1007#if 1 1008 /* 1009 * if there are multiple jumbo payload options, 1010 * *plenp will be non-zero and the packet will be 1011 * rejected. 1012 * the behavior may need some debate in ipngwg - 1013 * multiple options does not make sense, however, 1014 * there's no explicit mention in specification. 1015 */ 1016 if (*plenp != 0) { 1017 ip6stat.ip6s_badoptions++; 1018 icmp6_error(m, ICMP6_PARAM_PROB, 1019 ICMP6_PARAMPROB_HEADER, 1020 erroff + opt + 2 - opthead); 1021 return(-1); 1022 } 1023#endif 1024 1025 /* 1026 * jumbo payload length must be larger than 65535. 1027 */ 1028 if (jumboplen <= IPV6_MAXPACKET) { 1029 ip6stat.ip6s_badoptions++; 1030 icmp6_error(m, ICMP6_PARAM_PROB, 1031 ICMP6_PARAMPROB_HEADER, 1032 erroff + opt + 2 - opthead); 1033 return(-1); 1034 } 1035 *plenp = jumboplen; 1036 1037 break; 1038 default: /* unknown option */ 1039 if (hbhlen < IP6OPT_MINLEN) { 1040 ip6stat.ip6s_toosmall++; 1041 goto bad; 1042 } 1043 optlen = ip6_unknown_opt(opt, m, 1044 erroff + opt - opthead); 1045 if (optlen == -1) 1046 return(-1); 1047 optlen += 2; 1048 break; 1049 } 1050 } 1051 1052 return(0); 1053 1054 bad: 1055 m_freem(m); 1056 return(-1); 1057} 1058 1059/* 1060 * Unknown option processing. 1061 * The third argument `off' is the offset from the IPv6 header to the option, 1062 * which is necessary if the IPv6 header the and option header and IPv6 header 1063 * is not continuous in order to return an ICMPv6 error. 1064 */ 1065int 1066ip6_unknown_opt(optp, m, off) 1067 u_int8_t *optp; 1068 struct mbuf *m; 1069 int off; 1070{ 1071 struct ip6_hdr *ip6; 1072 1073 switch (IP6OPT_TYPE(*optp)) { 1074 case IP6OPT_TYPE_SKIP: /* ignore the option */ 1075 return((int)*(optp + 1)); 1076 case IP6OPT_TYPE_DISCARD: /* silently discard */ 1077 m_freem(m); 1078 return(-1); 1079 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 1080 ip6stat.ip6s_badoptions++; 1081 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 1082 return(-1); 1083 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 1084 ip6stat.ip6s_badoptions++; 1085 ip6 = mtod(m, struct ip6_hdr *); 1086 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1087 (m->m_flags & (M_BCAST|M_MCAST))) 1088 m_freem(m); 1089 else 1090 icmp6_error(m, ICMP6_PARAM_PROB, 1091 ICMP6_PARAMPROB_OPTION, off); 1092 return(-1); 1093 } 1094 1095 m_freem(m); /* XXX: NOTREACHED */ 1096 return(-1); 1097} 1098 1099/* 1100 * Create the "control" list for this pcb. 1101 * The function will not modify mbuf chain at all. 1102 * 1103 * with KAME mbuf chain restriction: 1104 * The routine will be called from upper layer handlers like tcp6_input(). 1105 * Thus the routine assumes that the caller (tcp6_input) have already 1106 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the 1107 * very first mbuf on the mbuf chain. 1108 */ 1109void 1110ip6_savecontrol(in6p, mp, ip6, m) 1111 struct inpcb *in6p; 1112 struct mbuf **mp; 1113 struct ip6_hdr *ip6; 1114 struct mbuf *m; 1115{ 1116#if __FreeBSD_version >= 500000 1117 struct thread *td = curthread; /* XXX */ 1118#else 1119 struct proc *td = curproc; /* XXX */ 1120#endif 1121 int privileged = 0; 1122 int rthdr_exist = 0; 1123 1124 1125 if (td && !suser(td)) 1126 privileged++; 1127 1128#ifdef SO_TIMESTAMP 1129 if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) { 1130 struct timeval tv; 1131 1132 microtime(&tv); 1133 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1134 SCM_TIMESTAMP, SOL_SOCKET); 1135 if (*mp) { 1136 mp = &(*mp)->m_next; 1137 } 1138 } 1139#endif 1140 1141 /* RFC 2292 sec. 5 */ 1142 if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) { 1143 struct in6_pktinfo pi6; 1144 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 1145 if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr)) 1146 pi6.ipi6_addr.s6_addr16[1] = 0; 1147 pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif) 1148 ? m->m_pkthdr.rcvif->if_index 1149 : 0; 1150 *mp = sbcreatecontrol((caddr_t) &pi6, 1151 sizeof(struct in6_pktinfo), IPV6_PKTINFO, 1152 IPPROTO_IPV6); 1153 if (*mp) 1154 mp = &(*mp)->m_next; 1155 } 1156 1157 if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) { 1158 int hlim = ip6->ip6_hlim & 0xff; 1159 *mp = sbcreatecontrol((caddr_t) &hlim, 1160 sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6); 1161 if (*mp) 1162 mp = &(*mp)->m_next; 1163 } 1164 1165 /* 1166 * IPV6_HOPOPTS socket option. We require super-user privilege 1167 * for the option, but it might be too strict, since there might 1168 * be some hop-by-hop options which can be returned to normal user. 1169 * See RFC 2292 section 6. 1170 */ 1171 if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) { 1172 /* 1173 * Check if a hop-by-hop options header is contatined in the 1174 * received packet, and if so, store the options as ancillary 1175 * data. Note that a hop-by-hop options header must be 1176 * just after the IPv6 header, which fact is assured through 1177 * the IPv6 input processing. 1178 */ 1179 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1180 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 1181 struct ip6_hbh *hbh; 1182 int hbhlen = 0; 1183#ifdef PULLDOWN_TEST 1184 struct mbuf *ext; 1185#endif 1186 1187#ifndef PULLDOWN_TEST 1188 hbh = (struct ip6_hbh *)(ip6 + 1); 1189 hbhlen = (hbh->ip6h_len + 1) << 3; 1190#else 1191 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), 1192 ip6->ip6_nxt); 1193 if (ext == NULL) { 1194 ip6stat.ip6s_tooshort++; 1195 return; 1196 } 1197 hbh = mtod(ext, struct ip6_hbh *); 1198 hbhlen = (hbh->ip6h_len + 1) << 3; 1199 if (hbhlen != ext->m_len) { 1200 m_freem(ext); 1201 ip6stat.ip6s_tooshort++; 1202 return; 1203 } 1204#endif 1205 1206 /* 1207 * XXX: We copy whole the header even if a jumbo 1208 * payload option is included, which option is to 1209 * be removed before returning in the RFC 2292. 1210 * Note: this constraint is removed in 2292bis. 1211 */ 1212 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, 1213 IPV6_HOPOPTS, IPPROTO_IPV6); 1214 if (*mp) 1215 mp = &(*mp)->m_next; 1216#ifdef PULLDOWN_TEST 1217 m_freem(ext); 1218#endif 1219 } 1220 } 1221 1222 /* IPV6_DSTOPTS and IPV6_RTHDR socket options */ 1223 if ((in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) { 1224 int proto, off, nxt; 1225 1226 /* 1227 * go through the header chain to see if a routing header is 1228 * contained in the packet. We need this information to store 1229 * destination options headers (if any) properly. 1230 * XXX: performance issue. We should record this info when 1231 * processing extension headers in incoming routine. 1232 * (todo) use m_aux? 1233 */ 1234 proto = IPPROTO_IPV6; 1235 off = 0; 1236 nxt = -1; 1237 while (1) { 1238 int newoff; 1239 1240 newoff = ip6_nexthdr(m, off, proto, &nxt); 1241 if (newoff < 0) 1242 break; 1243 if (newoff < off) /* invalid, check for safety */ 1244 break; 1245 if ((proto = nxt) == IPPROTO_ROUTING) { 1246 rthdr_exist = 1; 1247 break; 1248 } 1249 off = newoff; 1250 } 1251 } 1252 1253 if ((in6p->in6p_flags & 1254 (IN6P_RTHDR | IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) { 1255 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1256 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 1257 1258 /* 1259 * Search for destination options headers or routing 1260 * header(s) through the header chain, and stores each 1261 * header as ancillary data. 1262 * Note that the order of the headers remains in 1263 * the chain of ancillary data. 1264 */ 1265 while (1) { /* is explicit loop prevention necessary? */ 1266 struct ip6_ext *ip6e = NULL; 1267 int elen; 1268#ifdef PULLDOWN_TEST 1269 struct mbuf *ext = NULL; 1270#endif 1271 1272 /* 1273 * if it is not an extension header, don't try to 1274 * pull it from the chain. 1275 */ 1276 switch (nxt) { 1277 case IPPROTO_DSTOPTS: 1278 case IPPROTO_ROUTING: 1279 case IPPROTO_HOPOPTS: 1280 case IPPROTO_AH: /* is it possible? */ 1281 break; 1282 default: 1283 goto loopend; 1284 } 1285 1286#ifndef PULLDOWN_TEST 1287 if (off + sizeof(*ip6e) > m->m_len) 1288 goto loopend; 1289 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 1290 if (nxt == IPPROTO_AH) 1291 elen = (ip6e->ip6e_len + 2) << 2; 1292 else 1293 elen = (ip6e->ip6e_len + 1) << 3; 1294 if (off + elen > m->m_len) 1295 goto loopend; 1296#else 1297 ext = ip6_pullexthdr(m, off, nxt); 1298 if (ext == NULL) { 1299 ip6stat.ip6s_tooshort++; 1300 return; 1301 } 1302 ip6e = mtod(ext, struct ip6_ext *); 1303 if (nxt == IPPROTO_AH) 1304 elen = (ip6e->ip6e_len + 2) << 2; 1305 else 1306 elen = (ip6e->ip6e_len + 1) << 3; 1307 if (elen != ext->m_len) { 1308 m_freem(ext); 1309 ip6stat.ip6s_tooshort++; 1310 return; 1311 } 1312#endif 1313 1314 switch (nxt) { 1315 case IPPROTO_DSTOPTS: 1316 if ((in6p->in6p_flags & IN6P_DSTOPTS) == 0) 1317 break; 1318 1319 /* 1320 * We also require super-user privilege for 1321 * the option. 1322 * See the comments on IN6_HOPOPTS. 1323 */ 1324 if (!privileged) 1325 break; 1326 1327 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1328 IPV6_DSTOPTS, 1329 IPPROTO_IPV6); 1330 if (*mp) 1331 mp = &(*mp)->m_next; 1332 break; 1333 case IPPROTO_ROUTING: 1334 if (!in6p->in6p_flags & IN6P_RTHDR) 1335 break; 1336 1337 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1338 IPV6_RTHDR, 1339 IPPROTO_IPV6); 1340 if (*mp) 1341 mp = &(*mp)->m_next; 1342 break; 1343 case IPPROTO_HOPOPTS: 1344 case IPPROTO_AH: /* is it possible? */ 1345 break; 1346 1347 default: 1348 /* 1349 * other cases have been filtered in the above. 1350 * none will visit this case. here we supply 1351 * the code just in case (nxt overwritten or 1352 * other cases). 1353 */ 1354#ifdef PULLDOWN_TEST 1355 m_freem(ext); 1356#endif 1357 goto loopend; 1358 1359 } 1360 1361 /* proceed with the next header. */ 1362 off += elen; 1363 nxt = ip6e->ip6e_nxt; 1364 ip6e = NULL; 1365#ifdef PULLDOWN_TEST 1366 m_freem(ext); 1367 ext = NULL; 1368#endif 1369 } 1370 loopend: 1371 ; 1372 } 1373 1374} 1375 1376#ifdef PULLDOWN_TEST 1377/* 1378 * pull single extension header from mbuf chain. returns single mbuf that 1379 * contains the result, or NULL on error. 1380 */ 1381static struct mbuf * 1382ip6_pullexthdr(m, off, nxt) 1383 struct mbuf *m; 1384 size_t off; 1385 int nxt; 1386{ 1387 struct ip6_ext ip6e; 1388 size_t elen; 1389 struct mbuf *n; 1390 1391#ifdef DIAGNOSTIC 1392 switch (nxt) { 1393 case IPPROTO_DSTOPTS: 1394 case IPPROTO_ROUTING: 1395 case IPPROTO_HOPOPTS: 1396 case IPPROTO_AH: /* is it possible? */ 1397 break; 1398 default: 1399 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 1400 } 1401#endif 1402 1403 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1404 if (nxt == IPPROTO_AH) 1405 elen = (ip6e.ip6e_len + 2) << 2; 1406 else 1407 elen = (ip6e.ip6e_len + 1) << 3; 1408 1409 MGET(n, M_DONTWAIT, MT_DATA); 1410 if (n && elen >= MLEN) { 1411 MCLGET(n, M_DONTWAIT); 1412 if ((n->m_flags & M_EXT) == 0) { 1413 m_free(n); 1414 n = NULL; 1415 } 1416 } 1417 if (!n) 1418 return NULL; 1419 1420 n->m_len = 0; 1421 if (elen >= M_TRAILINGSPACE(n)) { 1422 m_free(n); 1423 return NULL; 1424 } 1425 1426 m_copydata(m, off, elen, mtod(n, caddr_t)); 1427 n->m_len = elen; 1428 return n; 1429} 1430#endif 1431 1432/* 1433 * Get pointer to the previous header followed by the header 1434 * currently processed. 1435 * XXX: This function supposes that 1436 * M includes all headers, 1437 * the next header field and the header length field of each header 1438 * are valid, and 1439 * the sum of each header length equals to OFF. 1440 * Because of these assumptions, this function must be called very 1441 * carefully. Moreover, it will not be used in the near future when 1442 * we develop `neater' mechanism to process extension headers. 1443 */ 1444char * 1445ip6_get_prevhdr(m, off) 1446 struct mbuf *m; 1447 int off; 1448{ 1449 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1450 1451 if (off == sizeof(struct ip6_hdr)) 1452 return(&ip6->ip6_nxt); 1453 else { 1454 int len, nxt; 1455 struct ip6_ext *ip6e = NULL; 1456 1457 nxt = ip6->ip6_nxt; 1458 len = sizeof(struct ip6_hdr); 1459 while (len < off) { 1460 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); 1461 1462 switch (nxt) { 1463 case IPPROTO_FRAGMENT: 1464 len += sizeof(struct ip6_frag); 1465 break; 1466 case IPPROTO_AH: 1467 len += (ip6e->ip6e_len + 2) << 2; 1468 break; 1469 default: 1470 len += (ip6e->ip6e_len + 1) << 3; 1471 break; 1472 } 1473 nxt = ip6e->ip6e_nxt; 1474 } 1475 if (ip6e) 1476 return(&ip6e->ip6e_nxt); 1477 else 1478 return NULL; 1479 } 1480} 1481 1482/* 1483 * get next header offset. m will be retained. 1484 */ 1485int 1486ip6_nexthdr(m, off, proto, nxtp) 1487 struct mbuf *m; 1488 int off; 1489 int proto; 1490 int *nxtp; 1491{ 1492 struct ip6_hdr ip6; 1493 struct ip6_ext ip6e; 1494 struct ip6_frag fh; 1495 1496 /* just in case */ 1497 if (m == NULL) 1498 panic("ip6_nexthdr: m == NULL"); 1499 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 1500 return -1; 1501 1502 switch (proto) { 1503 case IPPROTO_IPV6: 1504 if (m->m_pkthdr.len < off + sizeof(ip6)) 1505 return -1; 1506 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); 1507 if (nxtp) 1508 *nxtp = ip6.ip6_nxt; 1509 off += sizeof(ip6); 1510 return off; 1511 1512 case IPPROTO_FRAGMENT: 1513 /* 1514 * terminate parsing if it is not the first fragment, 1515 * it does not make sense to parse through it. 1516 */ 1517 if (m->m_pkthdr.len < off + sizeof(fh)) 1518 return -1; 1519 m_copydata(m, off, sizeof(fh), (caddr_t)&fh); 1520 if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0) 1521 return -1; 1522 if (nxtp) 1523 *nxtp = fh.ip6f_nxt; 1524 off += sizeof(struct ip6_frag); 1525 return off; 1526 1527 case IPPROTO_AH: 1528 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1529 return -1; 1530 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1531 if (nxtp) 1532 *nxtp = ip6e.ip6e_nxt; 1533 off += (ip6e.ip6e_len + 2) << 2; 1534 return off; 1535 1536 case IPPROTO_HOPOPTS: 1537 case IPPROTO_ROUTING: 1538 case IPPROTO_DSTOPTS: 1539 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1540 return -1; 1541 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1542 if (nxtp) 1543 *nxtp = ip6e.ip6e_nxt; 1544 off += (ip6e.ip6e_len + 1) << 3; 1545 return off; 1546 1547 case IPPROTO_NONE: 1548 case IPPROTO_ESP: 1549 case IPPROTO_IPCOMP: 1550 /* give up */ 1551 return -1; 1552 1553 default: 1554 return -1; 1555 } 1556 1557 return -1; 1558} 1559 1560/* 1561 * get offset for the last header in the chain. m will be kept untainted. 1562 */ 1563int 1564ip6_lasthdr(m, off, proto, nxtp) 1565 struct mbuf *m; 1566 int off; 1567 int proto; 1568 int *nxtp; 1569{ 1570 int newoff; 1571 int nxt; 1572 1573 if (!nxtp) { 1574 nxt = -1; 1575 nxtp = &nxt; 1576 } 1577 while (1) { 1578 newoff = ip6_nexthdr(m, off, proto, nxtp); 1579 if (newoff < 0) 1580 return off; 1581 else if (newoff < off) 1582 return -1; /* invalid */ 1583 else if (newoff == off) 1584 return newoff; 1585 1586 off = newoff; 1587 proto = *nxtp; 1588 } 1589} 1590 1591struct ip6aux * 1592ip6_addaux(m) 1593 struct mbuf *m; 1594{ 1595 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1596 if (!tag) { 1597 tag = m_tag_get(PACKET_TAG_IPV6_INPUT, 1598 sizeof (struct ip6aux), 1599 M_NOWAIT); 1600 if (tag) 1601 m_tag_prepend(m, tag); 1602 } 1603 if (tag) 1604 bzero(tag+1, sizeof (struct ip6aux)); 1605 return tag ? (struct ip6aux*)(tag+1) : NULL; 1606} 1607 1608struct ip6aux * 1609ip6_findaux(m) 1610 struct mbuf *m; 1611{ 1612 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1613 return tag ? (struct ip6aux*)(tag+1) : NULL; 1614} 1615 1616void 1617ip6_delaux(m) 1618 struct mbuf *m; 1619{ 1620 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1621 if (tag) 1622 m_tag_delete(m, tag); 1623} 1624 1625/* 1626 * System control for IP6 1627 */ 1628 1629u_char inet6ctlerrmap[PRC_NCMDS] = { 1630 0, 0, 0, 0, 1631 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1632 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1633 EMSGSIZE, EHOSTUNREACH, 0, 0, 1634 0, 0, 0, 0, 1635 ENOPROTOOPT 1636};
|