Filter.cpp 11 KB

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