Filter.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  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. #include <stdint.h>
  19. #include "Constants.hpp"
  20. #include "RuntimeEnvironment.hpp"
  21. #include "Address.hpp"
  22. #include "MAC.hpp"
  23. #include "InetAddress.hpp"
  24. #include "Filter.hpp"
  25. #include "Packet.hpp"
  26. #include "Switch.hpp"
  27. #include "Topology.hpp"
  28. #include "Node.hpp"
  29. // Returns true if packet appears valid; pos and proto will be set
  30. static bool _ipv6GetPayload(const uint8_t *frameData,unsigned int frameLen,unsigned int &pos,unsigned int &proto)
  31. {
  32. if (frameLen < 40)
  33. return false;
  34. pos = 40;
  35. proto = frameData[6];
  36. while (pos <= frameLen) {
  37. switch(proto) {
  38. case 0: // hop-by-hop options
  39. case 43: // routing
  40. case 60: // destination options
  41. case 135: // mobility options
  42. if ((pos + 8) > frameLen)
  43. return false; // invalid!
  44. proto = frameData[pos];
  45. pos += ((unsigned int)frameData[pos + 1] * 8) + 8;
  46. break;
  47. //case 44: // fragment -- we currently can't parse these and they are deprecated in IPv6 anyway
  48. //case 50:
  49. //case 51: // IPSec ESP and AH -- we have to stop here since this is encrypted stuff
  50. default:
  51. return true;
  52. }
  53. }
  54. return false; // overflow == invalid
  55. }
  56. namespace ZeroTier {
  57. bool Filter::run(
  58. const RuntimeEnvironment *RR,
  59. const uint64_t nwid,
  60. const Address &ztSource,
  61. const Address &ztDest,
  62. const MAC &macSource,
  63. const MAC &macDest,
  64. const uint8_t *frameData,
  65. const unsigned int frameLen,
  66. const unsigned int etherType,
  67. const unsigned int vlanId,
  68. const ZT_VirtualNetworkRule *rules,
  69. const unsigned int ruleCount)
  70. {
  71. // For each set of rules we start by assuming that they match (since no constraints
  72. // yields a 'match all' rule).
  73. uint8_t thisSetMatches = 1;
  74. for(unsigned int rn=0;rn<ruleCount;++rn) {
  75. const ZT_VirtualNetworkRuleType rt = (ZT_VirtualNetworkRuleType)(rules[rn].t & 0x7f);
  76. uint8_t thisRuleMatches = 0;
  77. switch(rt) {
  78. // Actions end a set of ANDed rules
  79. case ZT_NETWORK_RULE_ACTION_DROP:
  80. case ZT_NETWORK_RULE_ACTION_ACCEPT:
  81. case ZT_NETWORK_RULE_ACTION_TEE:
  82. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  83. if (thisSetMatches) {
  84. // This set did match, so perform action!
  85. if (rt != ZT_NETWORK_RULE_ACTION_DROP) {
  86. if ((rt == ZT_NETWORK_RULE_ACTION_TEE)||(rt == ZT_NETWORK_RULE_ACTION_REDIRECT)) {
  87. // Tee and redirect both want this frame copied to somewhere else.
  88. Packet outp(Address(rules[rn].v.zt),RR->identity.address(),Packet::VERB_EXT_FRAME);
  89. outp.append(nwid);
  90. outp.append((unsigned char)0x00); // TODO: should maybe include COM if needed
  91. macDest.appendTo(outp);
  92. macSource.appendTo(outp);
  93. outp.append((uint16_t)etherType);
  94. outp.append(frameData,frameLen);
  95. outp.compress();
  96. RR->sw->send(outp,true,nwid);
  97. }
  98. // For REDIRECT we will want to DROP at this node. For TEE we ACCEPT at this node but
  99. // also forward it along as we just did.
  100. return (rt != ZT_NETWORK_RULE_ACTION_REDIRECT);
  101. }
  102. return false;
  103. } else {
  104. // Otherwise start a new set, assuming that it will match
  105. //TRACE("[%u] %u previous set did not match, starting next",rn,(unsigned int)rt);
  106. thisSetMatches = 1;
  107. }
  108. continue;
  109. // A rule can consist of one or more MATCH criterion
  110. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  111. thisRuleMatches = (uint8_t)(rules[rn].v.zt == ztSource.toInt());
  112. break;
  113. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  114. thisRuleMatches = (uint8_t)(rules[rn].v.zt == ztDest.toInt());
  115. break;
  116. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  117. thisRuleMatches = (uint8_t)(rules[rn].v.vlanId == (uint16_t)vlanId);
  118. break;
  119. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  120. // NOT SUPPORTED YET
  121. thisRuleMatches = (uint8_t)(rules[rn].v.vlanPcp == 0);
  122. break;
  123. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  124. // NOT SUPPORTED YET
  125. thisRuleMatches = (uint8_t)(rules[rn].v.vlanDei == 0);
  126. break;
  127. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  128. thisRuleMatches = (uint8_t)(rules[rn].v.etherType == (uint16_t)etherType);
  129. break;
  130. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  131. thisRuleMatches = (uint8_t)(MAC(rules[rn].v.mac,6) == macSource);
  132. break;
  133. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  134. thisRuleMatches = (uint8_t)(MAC(rules[rn].v.mac,6) == macDest);
  135. break;
  136. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  137. if ((etherType == ZT_ETHERTYPE_IPV4)&&(frameLen >= 20)) {
  138. thisRuleMatches = (uint8_t)(InetAddress((const void *)&(rules[rn].v.ipv4.ip),4,rules[rn].v.ipv4.mask).containsAddress(InetAddress((const void *)(frameData + 12),4,0)));
  139. } else {
  140. thisRuleMatches = 0;
  141. }
  142. break;
  143. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  144. if ((etherType == ZT_ETHERTYPE_IPV4)&&(frameLen >= 20)) {
  145. thisRuleMatches = (uint8_t)(InetAddress((const void *)&(rules[rn].v.ipv4.ip),4,rules[rn].v.ipv4.mask).containsAddress(InetAddress((const void *)(frameData + 16),4,0)));
  146. } else {
  147. thisRuleMatches = 0;
  148. }
  149. break;
  150. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  151. if ((etherType == ZT_ETHERTYPE_IPV6)&&(frameLen >= 40)) {
  152. thisRuleMatches = (uint8_t)(InetAddress((const void *)rules[rn].v.ipv6.ip,16,rules[rn].v.ipv6.mask).containsAddress(InetAddress((const void *)(frameData + 8),16,0)));
  153. } else {
  154. thisRuleMatches = 0;
  155. }
  156. break;
  157. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  158. if ((etherType == ZT_ETHERTYPE_IPV6)&&(frameLen >= 40)) {
  159. thisRuleMatches = (uint8_t)(InetAddress((const void *)rules[rn].v.ipv6.ip,16,rules[rn].v.ipv6.mask).containsAddress(InetAddress((const void *)(frameData + 24),16,0)));
  160. } else {
  161. thisRuleMatches = 0;
  162. }
  163. break;
  164. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  165. if ((etherType == ZT_ETHERTYPE_IPV4)&&(frameLen >= 20)) {
  166. thisRuleMatches = (uint8_t)(rules[rn].v.ipTos == ((frameData[1] & 0xfc) >> 2));
  167. } else if ((etherType == ZT_ETHERTYPE_IPV6)&&(frameLen >= 40)) {
  168. const uint8_t trafficClass = ((frameData[0] << 4) & 0xf0) | ((frameData[1] >> 4) & 0x0f);
  169. thisRuleMatches = (uint8_t)(rules[rn].v.ipTos == ((trafficClass & 0xfc) >> 2));
  170. } else {
  171. thisRuleMatches = 0;
  172. }
  173. break;
  174. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  175. if ((etherType == ZT_ETHERTYPE_IPV4)&&(frameLen >= 20)) {
  176. thisRuleMatches = (uint8_t)(rules[rn].v.ipProtocol == frameData[9]);
  177. } else if (etherType == ZT_ETHERTYPE_IPV6) {
  178. unsigned int pos = 0,proto = 0;
  179. if (_ipv6GetPayload(frameData,frameLen,pos,proto)) {
  180. thisRuleMatches = (uint8_t)(rules[rn].v.ipProtocol == (uint8_t)proto);
  181. } else {
  182. thisRuleMatches = 0;
  183. }
  184. } else {
  185. thisRuleMatches = 0;
  186. }
  187. break;
  188. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  189. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  190. if ((etherType == ZT_ETHERTYPE_IPV4)&&(frameLen >= 20)) {
  191. const unsigned int headerLen = 4 * (frameData[0] & 0xf);
  192. int p = -1;
  193. switch(frameData[9]) { // IP protocol number
  194. // All these start with 16-bit source and destination port in that order
  195. case 0x06: // TCP
  196. case 0x11: // UDP
  197. case 0x84: // SCTP
  198. case 0x88: // UDPLite
  199. if (frameLen > (headerLen + 4)) {
  200. unsigned int pos = headerLen + (((unsigned int)(rt == ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE)) << 1); // headerLen or +2 for destination port
  201. p = (int)frameData[pos++] << 8;
  202. p |= (int)frameData[pos];
  203. }
  204. break;
  205. }
  206. thisRuleMatches = (p > 0) ? (uint8_t)((p >= (int)rules[rn].v.port[0])&&(p <= (int)rules[rn].v.port[1])) : (uint8_t)0;
  207. } else if (etherType == ZT_ETHERTYPE_IPV6) {
  208. unsigned int pos = 0,proto = 0;
  209. if (_ipv6GetPayload(frameData,frameLen,pos,proto)) {
  210. int p = -1;
  211. switch(proto) { // IP protocol number
  212. // All these start with 16-bit source and destination port in that order
  213. case 0x06: // TCP
  214. case 0x11: // UDP
  215. case 0x84: // SCTP
  216. case 0x88: // UDPLite
  217. if (frameLen > (pos + 4)) {
  218. p = (int)frameData[pos++] << 8;
  219. p |= (int)frameData[pos];
  220. }
  221. break;
  222. }
  223. thisRuleMatches = (p > 0) ? (uint8_t)((p >= (int)rules[rn].v.port[0])&&(p <= (int)rules[rn].v.port[1])) : (uint8_t)0;
  224. } else {
  225. thisRuleMatches = 0;
  226. }
  227. } else {
  228. thisRuleMatches = 0;
  229. }
  230. break;
  231. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  232. // TODO: not supported yet
  233. break;
  234. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  235. thisRuleMatches = (uint8_t)((frameLen >= (unsigned int)rules[rn].v.frameSize[0])&&(frameLen <= (unsigned int)rules[rn].v.frameSize[1]));
  236. break;
  237. case ZT_NETWORK_RULE_MATCH_TAG_VALUE_RANGE:
  238. break;
  239. case ZT_NETWORK_RULE_MATCH_TAG_VALUE_BITS_ALL:
  240. case ZT_NETWORK_RULE_MATCH_TAG_VALUE_BITS_ANY:
  241. break;
  242. }
  243. // thisSetMatches remains true if the current rule matched... or does NOT match if not bit (0x80) is 1
  244. thisSetMatches &= (thisRuleMatches ^ ((rules[rn].t & 0x80) >> 7));
  245. //TRACE("[%u] %u result==%u set==%u",rn,(unsigned int)rt,(unsigned int)thisRuleMatches,(unsigned int)thisSetMatches);
  246. }
  247. return false; // no matches, no rules, default action is therefore DROP
  248. }
  249. } // namespace ZeroTier