Filter.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #ifndef _ZT_FILTER_HPP
  28. #define _ZT_FILTER_HPP
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <string>
  32. #include <vector>
  33. #include <utility>
  34. #include "Mutex.hpp"
  35. #include "Range.hpp"
  36. /* Ethernet frame types that might be relevant to us */
  37. #define ZT_ETHERTYPE_IPV4 0x0800
  38. #define ZT_ETHERTYPE_ARP 0x0806
  39. #define ZT_ETHERTYPE_RARP 0x8035
  40. #define ZT_ETHERTYPE_ATALK 0x809b
  41. #define ZT_ETHERTYPE_AARP 0x80f3
  42. #define ZT_ETHERTYPE_IPX_A 0x8137
  43. #define ZT_ETHERTYPE_IPX_B 0x8138
  44. #define ZT_ETHERTYPE_IPV6 0x86dd
  45. /* IP protocols we might care about */
  46. #define ZT_IPPROTO_ICMP 0x01
  47. #define ZT_IPPROTO_IGMP 0x02
  48. #define ZT_IPPROTO_TCP 0x06
  49. #define ZT_IPPROTO_UDP 0x11
  50. #define ZT_IPPROTO_GRE 0x2f
  51. #define ZT_IPPROTO_ESP 0x32
  52. #define ZT_IPPROTO_AH 0x33
  53. #define ZT_IPPROTO_ICMPV6 0x3a
  54. #define ZT_IPPROTO_OSPF 0x59
  55. #define ZT_IPPROTO_IPIP 0x5e
  56. #define ZT_IPPROTO_IPCOMP 0x6c
  57. #define ZT_IPPROTO_L2TP 0x73
  58. #define ZT_IPPROTO_SCTP 0x84
  59. #define ZT_IPPROTO_FC 0x85
  60. #define ZT_IPPROTO_UDPLITE 0x88
  61. #define ZT_IPPROTO_HIP 0x8b
  62. /* IPv4 ICMP types */
  63. #define ZT_ICMP_ECHO_REPLY 0
  64. #define ZT_ICMP_DESTINATION_UNREACHABLE 3
  65. #define ZT_ICMP_SOURCE_QUENCH 4
  66. #define ZT_ICMP_REDIRECT 5
  67. #define ZT_ICMP_ALTERNATE_HOST_ADDRESS 6
  68. #define ZT_ICMP_ECHO_REQUEST 8
  69. #define ZT_ICMP_ROUTER_ADVERTISEMENT 9
  70. #define ZT_ICMP_ROUTER_SOLICITATION 10
  71. #define ZT_ICMP_TIME_EXCEEDED 11
  72. #define ZT_ICMP_BAD_IP_HEADER 12
  73. #define ZT_ICMP_TIMESTAMP 13
  74. #define ZT_ICMP_TIMESTAMP_REPLY 14
  75. #define ZT_ICMP_INFORMATION_REQUEST 15
  76. #define ZT_ICMP_INFORMATION_REPLY 16
  77. #define ZT_ICMP_ADDRESS_MASK_REQUEST 17
  78. #define ZT_ICMP_ADDRESS_MASK_REPLY 18
  79. #define ZT_ICMP_TRACEROUTE 30
  80. #define ZT_ICMP_MOBILE_HOST_REDIRECT 32
  81. #define ZT_ICMP_MOBILE_REGISTRATION_REQUEST 35
  82. #define ZT_ICMP_MOBILE_REGISTRATION_REPLY 36
  83. /* IPv6 ICMP types */
  84. #define ZT_ICMP6_DESTINATION_UNREACHABLE 1
  85. #define ZT_ICMP6_PACKET_TOO_BIG 2
  86. #define ZT_ICMP6_TIME_EXCEEDED 3
  87. #define ZT_ICMP6_PARAMETER_PROBLEM 4
  88. #define ZT_ICMP6_ECHO_REQUEST 128
  89. #define ZT_ICMP6_ECHO_REPLY 129
  90. #define ZT_ICMP6_MULTICAST_LISTENER_QUERY 130
  91. #define ZT_ICMP6_MULTICAST_LISTENER_REPORT 131
  92. #define ZT_ICMP6_MULTICAST_LISTENER_DONE 132
  93. #define ZT_ICMP6_ROUTER_SOLICITATION 133
  94. #define ZT_ICMP6_ROUTER_ADVERTISEMENT 134
  95. #define ZT_ICMP6_NEIGHBOR_SOLICITATION 135
  96. #define ZT_ICMP6_NEIGHBOR_ADVERTISEMENT 136
  97. #define ZT_ICMP6_REDIRECT_MESSAGE 137
  98. #define ZT_ICMP6_ROUTER_RENUMBERING 138
  99. #define ZT_ICMP6_NODE_INFORMATION_QUERY 139
  100. #define ZT_ICMP6_NODE_INFORMATION_RESPONSE 140
  101. #define ZT_ICMP6_INV_NEIGHBOR_SOLICITATION 141
  102. #define ZT_ICMP6_INV_NEIGHBOR_ADVERTISEMENT 142
  103. #define ZT_ICMP6_MLDV2 143
  104. #define ZT_ICMP6_HOME_AGENT_ADDRESS_DISCOVERY_REQUEST 144
  105. #define ZT_ICMP6_HOME_AGENT_ADDRESS_DISCOVERY_REPLY 145
  106. #define ZT_ICMP6_MOBILE_PREFIX_SOLICITATION 146
  107. #define ZT_ICMP6_MOBILE_PREFIX_ADVERTISEMENT 147
  108. #define ZT_ICMP6_CERTIFICATION_PATH_SOLICITATION 148
  109. #define ZT_ICMP6_CERTIFICATION_PATH_ADVERTISEMENT 149
  110. #define ZT_ICMP6_MULTICAST_ROUTER_ADVERTISEMENT 151
  111. #define ZT_ICMP6_MULTICAST_ROUTER_SOLICITATION 152
  112. #define ZT_ICMP6_MULTICAST_ROUTER_TERMINATION 153
  113. #define ZT_ICMP6_RPL_CONTROL_MESSAGE 155
  114. namespace ZeroTier {
  115. class RuntimeEnvironment;
  116. /**
  117. * A simple Ethernet frame level filter supporting basic IP port DENY
  118. */
  119. class Filter
  120. {
  121. public:
  122. /**
  123. * A filter rule
  124. *
  125. * This behaves as an immutable value object.
  126. */
  127. class Rule
  128. {
  129. public:
  130. Rule()
  131. throw() :
  132. _etherType(),
  133. _protocol(),
  134. _port()
  135. {
  136. }
  137. /**
  138. * Construct a new rule
  139. *
  140. * @param etype Ethernet type or empty range for ANY
  141. * @param prot Protocol or empty range for ANY (meaning depends on ethertype, e.g. IP protocol numbers)
  142. * @param prt Port or empty range for ANY (only applies to some protocols)
  143. */
  144. Rule(const Range<unsigned int> &etype,const Range<unsigned int> &prot,const Range<unsigned int> &prt)
  145. throw() :
  146. _etherType(etype),
  147. _protocol(prot),
  148. _port(prt)
  149. {
  150. }
  151. inline const Range<unsigned int> &etherType() const throw() { return _etherType; }
  152. inline const Range<unsigned int> &protocol() const throw() { return _protocol; }
  153. inline const Range<unsigned int> &port() const throw() { return _port; }
  154. /**
  155. * Test this rule against a frame
  156. *
  157. * @param etype Type of ethernet frame
  158. * @param data Ethernet frame data
  159. * @param len Length of ethernet frame
  160. * @return True if rule matches
  161. */
  162. bool operator()(unsigned int etype,const void *data,unsigned int len) const;
  163. inline bool operator==(const Rule &r) const throw() { return ((_etherType == r._etherType)&&(_protocol == r._protocol)&&(_port == r._port)); }
  164. inline bool operator!=(const Rule &r) const throw() { return !(*this == r); }
  165. inline bool operator<(const Rule &r) const
  166. throw()
  167. {
  168. if (_etherType < r._etherType)
  169. return true;
  170. else if (_etherType == r._etherType) {
  171. if (_protocol < r._protocol)
  172. return true;
  173. else if (_protocol == r._protocol) {
  174. if (_port < r._port)
  175. return true;
  176. }
  177. }
  178. return false;
  179. }
  180. inline bool operator>(const Rule &r) const throw() { return (r < *this); }
  181. inline bool operator<=(const Rule &r) const throw() { return !(r < *this); }
  182. inline bool operator>=(const Rule &r) const throw() { return !(*this < r); }
  183. private:
  184. Range<unsigned int> _etherType;
  185. Range<unsigned int> _protocol;
  186. Range<unsigned int> _port;
  187. };
  188. /**
  189. * Action if a rule matches
  190. */
  191. enum Action
  192. {
  193. ACTION_DENY = 0,
  194. ACTION_ALLOW = 1,
  195. ACTION_LOG = 2
  196. };
  197. /**
  198. * Entry in filter chain
  199. */
  200. struct Entry
  201. {
  202. Entry() {}
  203. Entry(const Rule &r,const Action &a) :
  204. rule(r),
  205. action(a)
  206. {
  207. }
  208. Rule rule;
  209. Action action;
  210. };
  211. Filter(const RuntimeEnvironment *renv);
  212. ~Filter();
  213. /**
  214. * Remove all filter entries
  215. */
  216. inline void clear()
  217. {
  218. Mutex::Lock _l(_chain_m);
  219. _chain.clear();
  220. }
  221. /**
  222. * Append a rule/action pair to this chain
  223. *
  224. * If an identical rule already exists it is removed and a new entry is
  225. * added to the end with the new action. (Two identical rules with the
  226. * same action wouldn't make sense.)
  227. *
  228. * @param r Rule to add
  229. * @param a Action if rule matches
  230. */
  231. void add(const Rule &r,const Action &a);
  232. /**
  233. * @return Number of rules in filter chain
  234. */
  235. inline unsigned int length() const
  236. throw()
  237. {
  238. Mutex::Lock _l(_chain_m);
  239. return _chain.size();
  240. }
  241. /**
  242. * @return Entry in filter chain or null entry if out of bounds
  243. */
  244. inline Entry operator[](const unsigned int i) const
  245. throw()
  246. {
  247. Mutex::Lock _l(_chain_m);
  248. if (i < _chain.size())
  249. return _chain[i];
  250. return Entry();
  251. }
  252. /**
  253. * Get a string representation of this filter
  254. *
  255. * @param sep Separator between filter rules, or NULL for comma (default)
  256. * @return Human-readable string
  257. */
  258. std::string toString(const char *sep = (const char *)0) const;
  259. /**
  260. * @param etherType Ethernet type ID
  261. * @return Name of Ethernet protocol (e.g. ARP, IPV4)
  262. */
  263. static const char *etherTypeName(const unsigned int etherType)
  264. throw();
  265. private:
  266. const RuntimeEnvironment *_r;
  267. std::vector<Entry> _chain;
  268. Mutex _chain_m;
  269. };
  270. } // namespace ZeroTier
  271. #endif