Filter.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. #ifndef _ZT_FILTER_HPP
  28. #define _ZT_FILTER_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <string>
  33. #include <vector>
  34. #include <utility>
  35. #include <stdexcept>
  36. #include "Range.hpp"
  37. /* Ethernet frame types that might be relevant to us */
  38. #define ZT_ETHERTYPE_IPV4 0x0800
  39. #define ZT_ETHERTYPE_ARP 0x0806
  40. #define ZT_ETHERTYPE_RARP 0x8035
  41. #define ZT_ETHERTYPE_ATALK 0x809b
  42. #define ZT_ETHERTYPE_AARP 0x80f3
  43. #define ZT_ETHERTYPE_IPX_A 0x8137
  44. #define ZT_ETHERTYPE_IPX_B 0x8138
  45. #define ZT_ETHERTYPE_IPV6 0x86dd
  46. /* IP protocols we might care about */
  47. #define ZT_IPPROTO_ICMP 0x01
  48. #define ZT_IPPROTO_IGMP 0x02
  49. #define ZT_IPPROTO_TCP 0x06
  50. #define ZT_IPPROTO_UDP 0x11
  51. #define ZT_IPPROTO_GRE 0x2f
  52. #define ZT_IPPROTO_ESP 0x32
  53. #define ZT_IPPROTO_AH 0x33
  54. #define ZT_IPPROTO_ICMPV6 0x3a
  55. #define ZT_IPPROTO_OSPF 0x59
  56. #define ZT_IPPROTO_IPIP 0x5e
  57. #define ZT_IPPROTO_IPCOMP 0x6c
  58. #define ZT_IPPROTO_L2TP 0x73
  59. #define ZT_IPPROTO_SCTP 0x84
  60. #define ZT_IPPROTO_FC 0x85
  61. #define ZT_IPPROTO_UDPLITE 0x88
  62. #define ZT_IPPROTO_HIP 0x8b
  63. /* IPv4 ICMP types */
  64. #define ZT_ICMP_ECHO_REPLY 0
  65. #define ZT_ICMP_DESTINATION_UNREACHABLE 3
  66. #define ZT_ICMP_SOURCE_QUENCH 4
  67. #define ZT_ICMP_REDIRECT 5
  68. #define ZT_ICMP_ALTERNATE_HOST_ADDRESS 6
  69. #define ZT_ICMP_ECHO_REQUEST 8
  70. #define ZT_ICMP_ROUTER_ADVERTISEMENT 9
  71. #define ZT_ICMP_ROUTER_SOLICITATION 10
  72. #define ZT_ICMP_TIME_EXCEEDED 11
  73. #define ZT_ICMP_BAD_IP_HEADER 12
  74. #define ZT_ICMP_TIMESTAMP 13
  75. #define ZT_ICMP_TIMESTAMP_REPLY 14
  76. #define ZT_ICMP_INFORMATION_REQUEST 15
  77. #define ZT_ICMP_INFORMATION_REPLY 16
  78. #define ZT_ICMP_ADDRESS_MASK_REQUEST 17
  79. #define ZT_ICMP_ADDRESS_MASK_REPLY 18
  80. #define ZT_ICMP_TRACEROUTE 30
  81. #define ZT_ICMP_MOBILE_HOST_REDIRECT 32
  82. #define ZT_ICMP_MOBILE_REGISTRATION_REQUEST 35
  83. #define ZT_ICMP_MOBILE_REGISTRATION_REPLY 36
  84. /* IPv6 ICMP types */
  85. #define ZT_ICMP6_DESTINATION_UNREACHABLE 1
  86. #define ZT_ICMP6_PACKET_TOO_BIG 2
  87. #define ZT_ICMP6_TIME_EXCEEDED 3
  88. #define ZT_ICMP6_PARAMETER_PROBLEM 4
  89. #define ZT_ICMP6_ECHO_REQUEST 128
  90. #define ZT_ICMP6_ECHO_REPLY 129
  91. #define ZT_ICMP6_MULTICAST_LISTENER_QUERY 130
  92. #define ZT_ICMP6_MULTICAST_LISTENER_REPORT 131
  93. #define ZT_ICMP6_MULTICAST_LISTENER_DONE 132
  94. #define ZT_ICMP6_ROUTER_SOLICITATION 133
  95. #define ZT_ICMP6_ROUTER_ADVERTISEMENT 134
  96. #define ZT_ICMP6_NEIGHBOR_SOLICITATION 135
  97. #define ZT_ICMP6_NEIGHBOR_ADVERTISEMENT 136
  98. #define ZT_ICMP6_REDIRECT_MESSAGE 137
  99. #define ZT_ICMP6_ROUTER_RENUMBERING 138
  100. #define ZT_ICMP6_NODE_INFORMATION_QUERY 139
  101. #define ZT_ICMP6_NODE_INFORMATION_RESPONSE 140
  102. #define ZT_ICMP6_INV_NEIGHBOR_SOLICITATION 141
  103. #define ZT_ICMP6_INV_NEIGHBOR_ADVERTISEMENT 142
  104. #define ZT_ICMP6_MLDV2 143
  105. #define ZT_ICMP6_HOME_AGENT_ADDRESS_DISCOVERY_REQUEST 144
  106. #define ZT_ICMP6_HOME_AGENT_ADDRESS_DISCOVERY_REPLY 145
  107. #define ZT_ICMP6_MOBILE_PREFIX_SOLICITATION 146
  108. #define ZT_ICMP6_MOBILE_PREFIX_ADVERTISEMENT 147
  109. #define ZT_ICMP6_CERTIFICATION_PATH_SOLICITATION 148
  110. #define ZT_ICMP6_CERTIFICATION_PATH_ADVERTISEMENT 149
  111. #define ZT_ICMP6_MULTICAST_ROUTER_ADVERTISEMENT 151
  112. #define ZT_ICMP6_MULTICAST_ROUTER_SOLICITATION 152
  113. #define ZT_ICMP6_MULTICAST_ROUTER_TERMINATION 153
  114. #define ZT_ICMP6_RPL_CONTROL_MESSAGE 155
  115. namespace ZeroTier {
  116. class RuntimeEnvironment;
  117. /**
  118. * A simple Ethernet frame level filter
  119. *
  120. * This doesn't specify actions, since it's used as a deny filter. The rule
  121. * in ZT1 is "that which is not explicitly prohibited is allowed." (Except for
  122. * ethertypes, which are handled by a whitelist.)
  123. */
  124. class Filter
  125. {
  126. public:
  127. /**
  128. * Value returned by etherTypeName, etc. on unknown
  129. *
  130. * These static methods return precisely this, so a pointer equality
  131. * check will work.
  132. */
  133. static const char *const UNKNOWN_NAME;
  134. /**
  135. * An empty range as a more idiomatic way of specifying a wildcard match
  136. */
  137. static const Range<unsigned int> ANY;
  138. /**
  139. * A filter rule
  140. */
  141. class Rule
  142. {
  143. public:
  144. Rule()
  145. throw() :
  146. _etherType(),
  147. _protocol(),
  148. _port()
  149. {
  150. }
  151. /**
  152. * Construct a rule from a string-serialized value
  153. *
  154. * @param s String formatted rule, such as returned by toString()
  155. * @throws std::invalid_argument String formatted rule is not valid
  156. */
  157. Rule(const char *s)
  158. throw(std::invalid_argument);
  159. /**
  160. * Construct a new rule
  161. *
  162. * @param etype Ethernet type or empty range for ANY
  163. * @param prot Protocol or empty range for ANY (meaning depends on ethertype, e.g. IP protocol numbers)
  164. * @param prt Port or empty range for ANY (only applies to some protocols)
  165. */
  166. Rule(const Range<unsigned int> &etype,const Range<unsigned int> &prot,const Range<unsigned int> &prt)
  167. throw() :
  168. _etherType(etype),
  169. _protocol(prot),
  170. _port(prt)
  171. {
  172. }
  173. inline const Range<unsigned int> &etherType() const throw() { return _etherType; }
  174. inline const Range<unsigned int> &protocol() const throw() { return _protocol; }
  175. inline const Range<unsigned int> &port() const throw() { return _port; }
  176. /**
  177. * Test this rule against a frame
  178. *
  179. * @param etype Type of ethernet frame
  180. * @param data Ethernet frame data
  181. * @param len Length of ethernet frame
  182. * @return True if rule matches
  183. * @throws std::invalid_argument Frame invalid or not parseable
  184. */
  185. bool operator()(unsigned int etype,const void *data,unsigned int len) const
  186. throw(std::invalid_argument);
  187. /**
  188. * Serialize rule as string
  189. *
  190. * @return Human readable representation of rule
  191. */
  192. std::string toString() const;
  193. inline bool operator==(const Rule &r) const throw() { return ((_etherType == r._etherType)&&(_protocol == r._protocol)&&(_port == r._port)); }
  194. inline bool operator!=(const Rule &r) const throw() { return !(*this == r); }
  195. inline bool operator<(const Rule &r) const
  196. throw()
  197. {
  198. if (_etherType < r._etherType)
  199. return true;
  200. else if (_etherType == r._etherType) {
  201. if (_protocol < r._protocol)
  202. return true;
  203. else if (_protocol == r._protocol) {
  204. if (_port < r._port)
  205. return true;
  206. }
  207. }
  208. return false;
  209. }
  210. inline bool operator>(const Rule &r) const throw() { return (r < *this); }
  211. inline bool operator<=(const Rule &r) const throw() { return !(r < *this); }
  212. inline bool operator>=(const Rule &r) const throw() { return !(*this < r); }
  213. private:
  214. Range<unsigned int> _etherType;
  215. Range<unsigned int> _protocol;
  216. Range<unsigned int> _port;
  217. };
  218. Filter() {}
  219. /**
  220. * @param s String-serialized filter representation
  221. */
  222. Filter(const char *s)
  223. throw(std::invalid_argument);
  224. /**
  225. * @return Comma-delimited list of string-format rules
  226. */
  227. std::string toString() const;
  228. /**
  229. * Add a rule to this filter
  230. *
  231. * @param r Rule to add to filter
  232. */
  233. void add(const Rule &r);
  234. inline bool operator()(unsigned int etype,const void *data,unsigned int len) const
  235. throw(std::invalid_argument)
  236. {
  237. for(std::vector<Rule>::const_iterator r(_rules.begin());r!=_rules.end();++r) {
  238. if ((*r)(etype,data,len))
  239. return true;
  240. }
  241. return false;
  242. }
  243. static const char *etherTypeName(const unsigned int etherType)
  244. throw();
  245. static const char *ipProtocolName(const unsigned int ipp)
  246. throw();
  247. static const char *icmpTypeName(const unsigned int icmpType)
  248. throw();
  249. static const char *icmp6TypeName(const unsigned int icmp6Type)
  250. throw();
  251. private:
  252. std::vector<Rule> _rules;
  253. };
  254. } // namespace ZeroTier
  255. #endif