Filter.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdint.h>
  28. #include "RuntimeEnvironment.hpp"
  29. #include "Logger.hpp"
  30. #include "Filter.hpp"
  31. #include "Utils.hpp"
  32. namespace ZeroTier {
  33. bool Filter::Rule::operator()(unsigned int etype,const void *data,unsigned int len) const
  34. {
  35. if ((!_etherType)||(_etherType(etype))) { // ethertype is ANY, or matches
  36. // Ethertype determines meaning of protocol and port
  37. switch(etype) {
  38. case ZT_ETHERTYPE_IPV4:
  39. if (len > 20) {
  40. if ((!_protocol)||(_protocol(((const uint8_t *)data)[9]))) { // IP protocol
  41. if (!_port)
  42. return true; // protocol matches, port is ANY
  43. // Don't match on fragments beyond fragment 0. If we've blocked
  44. // fragment 0, further fragments will fall on deaf ears anyway.
  45. if ((Utils::ntoh(((const uint16_t *)data)[3]) & 0x1fff))
  46. return false;
  47. // Internet header length determines where data begins, in multiples of 32 bits
  48. unsigned int ihl = 4 * (((const uint8_t *)data)[0] & 0x0f);
  49. switch(((const uint8_t *)data)[9]) { // port's meaning depends on IP protocol
  50. case ZT_IPPROTO_ICMP:
  51. return _port(((const uint8_t *)data)[ihl]); // port = ICMP type
  52. case ZT_IPPROTO_TCP:
  53. case ZT_IPPROTO_UDP:
  54. case ZT_IPPROTO_SCTP:
  55. case ZT_IPPROTO_UDPLITE:
  56. return _port(((const uint16_t *)data)[(ihl / 2) + 1]); // destination port
  57. }
  58. return false; // no match on port
  59. }
  60. }
  61. break;
  62. case ZT_ETHERTYPE_IPV6:
  63. if (len > 40) {
  64. // see: http://stackoverflow.com/questions/17518951/is-the-ipv6-header-really-this-nutty
  65. }
  66. break;
  67. }
  68. }
  69. return false;
  70. }
  71. Filter::Filter(const RuntimeEnvironment *renv) :
  72. _r(renv)
  73. {
  74. }
  75. Filter::~Filter()
  76. {
  77. }
  78. void Filter::add(const Rule &r,const Action &a)
  79. {
  80. Mutex::Lock _l(_chain_m);
  81. for(std::vector<Entry>::iterator i(_chain.begin());i!=_chain.end();++i) {
  82. if (i->rule == r) {
  83. _chain.erase(i);
  84. break;
  85. }
  86. }
  87. _chain.push_back(Entry(r,a));
  88. }
  89. const char *Filter::etherTypeName(const unsigned int etherType)
  90. throw()
  91. {
  92. static char tmp[6];
  93. switch(etherType) {
  94. case ZT_ETHERTYPE_IPV4:
  95. return "IPV4";
  96. case ZT_ETHERTYPE_ARP:
  97. return "ARP";
  98. case ZT_ETHERTYPE_RARP:
  99. return "RARP";
  100. case ZT_ETHERTYPE_ATALK:
  101. return "ATALK";
  102. case ZT_ETHERTYPE_AARP:
  103. return "AARP";
  104. case ZT_ETHERTYPE_IPX_A:
  105. return "IPX_A";
  106. case ZT_ETHERTYPE_IPX_B:
  107. return "IPX_B";
  108. case ZT_ETHERTYPE_IPV6:
  109. return "IPV6";
  110. }
  111. sprintf(tmp,"%.4x",etherType);
  112. return tmp; // technically not thread safe, but we're only going to see this in debugging if ever
  113. }
  114. } // namespace ZeroTier