Filter.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. default:
  39. if ((!_protocol)&&(!_port))
  40. return true; // match other ethertypes if protocol and port are ANY, since we don't know what to do with them
  41. break;
  42. case ZT_ETHERTYPE_IPV4:
  43. if (len > 20) {
  44. if ((!_protocol)||(_protocol(((const uint8_t *)data)[9]))) { // IP protocol
  45. if (!_port)
  46. return true; // protocol matches or is ANY, port is ANY
  47. // Don't match on fragments beyond fragment 0. If we've blocked
  48. // fragment 0, further fragments will fall on deaf ears anyway.
  49. if ((Utils::ntoh(((const uint16_t *)data)[3]) & 0x1fff))
  50. return false;
  51. // Internet header length determines where data begins, in multiples of 32 bits
  52. unsigned int ihl = 4 * (((const uint8_t *)data)[0] & 0x0f);
  53. switch(((const uint8_t *)data)[9]) { // port's meaning depends on IP protocol
  54. case ZT_IPPROTO_ICMP:
  55. return _port(((const uint8_t *)data)[ihl]); // port = ICMP type
  56. case ZT_IPPROTO_TCP:
  57. case ZT_IPPROTO_UDP:
  58. case ZT_IPPROTO_SCTP:
  59. case ZT_IPPROTO_UDPLITE:
  60. return _port(((const uint16_t *)data)[(ihl / 2) + 1]); // destination port
  61. }
  62. return false; // no match on port
  63. }
  64. }
  65. break;
  66. case ZT_ETHERTYPE_IPV6:
  67. if (len > 40) {
  68. // see: http://stackoverflow.com/questions/17518951/is-the-ipv6-header-really-this-nutty
  69. int nextHeader = ((const uint8_t *)data)[6];
  70. unsigned int pos = 40;
  71. while ((pos < len)&&(nextHeader >= 0)&&(nextHeader != 59)) { // 59 == no next header
  72. fprintf(stderr,"[rule] V6: start header parse, header %.2x pos %d\n",nextHeader,pos);
  73. switch(nextHeader) {
  74. case 0: // hop-by-hop options
  75. case 60: // destination options
  76. case 43: // routing
  77. case 135: // mobility (mobile IPv6 options)
  78. if (_protocol((unsigned int)nextHeader))
  79. return true; // match if our goal was to match any of these
  80. nextHeader = ((const uint8_t *)data)[pos];
  81. pos += 8 + (8 * ((const uint8_t *)data)[pos + 1]);
  82. break;
  83. case 44: // fragment
  84. if (_protocol(44))
  85. return true; // match if our goal was to match fragments
  86. nextHeader = ((const uint8_t *)data)[pos];
  87. pos += 8;
  88. break;
  89. case ZT_IPPROTO_AH: // AH
  90. return _protocol(ZT_IPPROTO_AH); // true if AH is matched protocol, otherwise false since packet will be IPsec
  91. case ZT_IPPROTO_ESP: // ESP
  92. return _protocol(ZT_IPPROTO_ESP); // true if ESP is matched protocol, otherwise false since packet will be IPsec
  93. case ZT_IPPROTO_ICMPV6:
  94. if (_protocol(ZT_IPPROTO_ICMPV6)) { // only match ICMPv6 if specified
  95. if ((!_port)||(_port(((const uint8_t *)data)[pos])))
  96. return true; // protocol matches, port is ANY or matches ICMP type
  97. }
  98. break;
  99. case ZT_IPPROTO_TCP:
  100. case ZT_IPPROTO_UDP:
  101. case ZT_IPPROTO_SCTP:
  102. case ZT_IPPROTO_UDPLITE:
  103. // If we encounter any of these, match if protocol matches or is wildcard as
  104. // we'll consider these the "real payload" if present.
  105. if ((!_protocol)||(_protocol(nextHeader))) {
  106. if ((!_port)||(_port(((const uint16_t *)data)[(pos / 2) + 1])))
  107. return true; // protocol matches or is ANY, port is ANY or matches
  108. }
  109. break;
  110. }
  111. fprintf(stderr,"[rule] V6: end header parse, next header %.2x, new pos %d\n",nextHeader,pos);
  112. }
  113. }
  114. break;
  115. }
  116. }
  117. return false;
  118. }
  119. Filter::Filter(const RuntimeEnvironment *renv) :
  120. _r(renv)
  121. {
  122. }
  123. Filter::~Filter()
  124. {
  125. }
  126. void Filter::add(const Rule &r,const Action &a)
  127. {
  128. Mutex::Lock _l(_chain_m);
  129. for(std::vector<Entry>::iterator i(_chain.begin());i!=_chain.end();++i) {
  130. if (i->rule == r) {
  131. _chain.erase(i);
  132. break;
  133. }
  134. }
  135. _chain.push_back(Entry(r,a));
  136. }
  137. std::string Filter::toString(const char *sep) const
  138. {
  139. char buf[256];
  140. if (!sep)
  141. sep = ",";
  142. std::string s;
  143. Mutex::Lock _l(_chain_m);
  144. for(std::vector<Entry>::const_iterator i(_chain.begin());i!=_chain.end();++i) {
  145. bool first = (i == _chain.begin());
  146. s.push_back('[');
  147. if (i->rule.etherType()) {
  148. sprintf(buf,"%u-%u",i->rule.etherType().start,i->rule.etherType().end);
  149. s.append(buf);
  150. } else s.push_back('*');
  151. s.push_back(';');
  152. if (i->rule.protocol()) {
  153. sprintf(buf,"%u-%u",i->rule.protocol().start,i->rule.protocol().end);
  154. s.append(buf);
  155. } else s.push_back('*');
  156. s.push_back(';');
  157. if (i->rule.port()) {
  158. sprintf(buf,"%u-%u",i->rule.port().start,i->rule.port().end);
  159. s.append(buf);
  160. } else s.push_back('*');
  161. s.append("]:");
  162. switch(i->action) {
  163. case ACTION_DENY:
  164. s.append("DENY");
  165. break;
  166. case ACTION_ALLOW:
  167. s.append("ALLOW");
  168. break;
  169. case ACTION_LOG:
  170. s.append("LOG");
  171. break;
  172. }
  173. if (!first)
  174. s.append(sep);
  175. }
  176. return s;
  177. }
  178. const char *Filter::etherTypeName(const unsigned int etherType)
  179. throw()
  180. {
  181. static char tmp[6];
  182. switch(etherType) {
  183. case ZT_ETHERTYPE_IPV4:
  184. return "IPV4";
  185. case ZT_ETHERTYPE_ARP:
  186. return "ARP";
  187. case ZT_ETHERTYPE_RARP:
  188. return "RARP";
  189. case ZT_ETHERTYPE_ATALK:
  190. return "ATALK";
  191. case ZT_ETHERTYPE_AARP:
  192. return "AARP";
  193. case ZT_ETHERTYPE_IPX_A:
  194. return "IPX_A";
  195. case ZT_ETHERTYPE_IPX_B:
  196. return "IPX_B";
  197. case ZT_ETHERTYPE_IPV6:
  198. return "IPV6";
  199. }
  200. sprintf(tmp,"%.4x",etherType);
  201. return tmp; // technically not thread safe, but we're only going to see this in debugging if ever
  202. }
  203. } // namespace ZeroTier