Filter.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdint.h>
  31. #include "RuntimeEnvironment.hpp"
  32. #include "Logger.hpp"
  33. #include "Filter.hpp"
  34. #include "Utils.hpp"
  35. namespace ZeroTier {
  36. const char *const Filter::UNKNOWN_NAME = "(unknown)";
  37. const Range<unsigned int> Filter::ANY;
  38. bool Filter::Rule::operator()(unsigned int etype,const void *data,unsigned int len) const
  39. throw(std::invalid_argument)
  40. {
  41. if ((!_etherType)||(_etherType(etype))) { // ethertype is ANY, or matches
  42. // Ethertype determines meaning of protocol and port
  43. switch(etype) {
  44. case ZT_ETHERTYPE_IPV4:
  45. if (len > 20) {
  46. if ((!_protocol)||(_protocol(((const uint8_t *)data)[9]))) { // protocol is ANY or match
  47. if (!_port) // port is ANY
  48. return true;
  49. // Don't match on fragments beyond fragment 0. If we've blocked
  50. // fragment 0, further fragments will fall on deaf ears anyway.
  51. if ((Utils::ntoh(((const uint16_t *)data)[3]) & 0x1fff))
  52. return false;
  53. // Internet header length determines where data begins, in multiples of 32 bits
  54. unsigned int ihl = 4 * (((const uint8_t *)data)[0] & 0x0f);
  55. switch(((const uint8_t *)data)[9]) { // port's meaning depends on IP protocol
  56. case ZT_IPPROTO_ICMP:
  57. // For ICMP, port is ICMP type
  58. return _port(((const uint8_t *)data)[ihl]);
  59. case ZT_IPPROTO_TCP:
  60. case ZT_IPPROTO_UDP:
  61. case ZT_IPPROTO_SCTP:
  62. case ZT_IPPROTO_UDPLITE:
  63. // For these, port is destination port. Protocol designers were
  64. // nice enough to put the field in the same place.
  65. return _port(((const uint16_t *)data)[(ihl / 2) + 1]);
  66. default:
  67. // port has no meaning for other IP types, so ignore it
  68. return true;
  69. }
  70. return false; // no match on port
  71. }
  72. } else throw std::invalid_argument("undersized IPv4 packet");
  73. break;
  74. case ZT_ETHERTYPE_IPV6:
  75. if (len > 40) {
  76. int nextHeader = ((const uint8_t *)data)[6];
  77. unsigned int pos = 40;
  78. while ((pos < len)&&(nextHeader >= 0)&&(nextHeader != 59)) { // 59 == no next header
  79. fprintf(stderr,"[rule] V6: start header parse, header %.2x pos %d\n",nextHeader,pos);
  80. switch(nextHeader) {
  81. case 0: // hop-by-hop options
  82. case 60: // destination options
  83. case 43: // routing
  84. case 135: // mobility (mobile IPv6 options)
  85. if (_protocol((unsigned int)nextHeader))
  86. return true; // match if our goal was to match any of these
  87. nextHeader = ((const uint8_t *)data)[pos];
  88. pos += 8 + (8 * ((const uint8_t *)data)[pos + 1]);
  89. break;
  90. case 44: // fragment
  91. if (_protocol(44))
  92. return true; // match if our goal was to match fragments
  93. nextHeader = ((const uint8_t *)data)[pos];
  94. pos += 8;
  95. break;
  96. case ZT_IPPROTO_AH: // AH
  97. return _protocol(ZT_IPPROTO_AH); // true if AH is matched protocol, otherwise false since packet will be IPsec
  98. case ZT_IPPROTO_ESP: // ESP
  99. return _protocol(ZT_IPPROTO_ESP); // true if ESP is matched protocol, otherwise false since packet will be IPsec
  100. case ZT_IPPROTO_ICMPV6:
  101. // Only match ICMPv6 if we've selected it specifically
  102. if (_protocol(ZT_IPPROTO_ICMPV6)) {
  103. // Port is interpreted as ICMPv6 type
  104. if ((!_port)||(_port(((const uint8_t *)data)[pos])))
  105. return true;
  106. }
  107. break;
  108. case ZT_IPPROTO_TCP:
  109. case ZT_IPPROTO_UDP:
  110. case ZT_IPPROTO_SCTP:
  111. case ZT_IPPROTO_UDPLITE:
  112. // If we encounter any of these, match if protocol matches or is wildcard as
  113. // we'll consider these the "real payload" if present.
  114. if ((!_protocol)||(_protocol(nextHeader))) {
  115. if ((!_port)||(_port(((const uint16_t *)data)[(pos / 2) + 1])))
  116. return true; // protocol matches or is ANY, port is ANY or matches
  117. }
  118. break;
  119. default: {
  120. char foo[128];
  121. sprintf(foo,"unrecognized IPv6 header type %d",(int)nextHeader);
  122. throw std::invalid_argument(foo);
  123. }
  124. }
  125. fprintf(stderr,"[rule] V6: end header parse, next header %.2x, new pos %d\n",nextHeader,pos);
  126. }
  127. } else throw std::invalid_argument("undersized IPv6 packet");
  128. break;
  129. default:
  130. // For other ethertypes, protocol and port are ignored. What would they mean?
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. std::string Filter::Rule::toString() const
  137. {
  138. char buf[128];
  139. std::string s;
  140. switch(_etherType.magnitude()) {
  141. case 0:
  142. s.push_back('*');
  143. break;
  144. case 1:
  145. sprintf(buf,"%u",_etherType.start);
  146. s.append(buf);
  147. break;
  148. default:
  149. sprintf(buf,"%u-%u",_etherType.start,_etherType.end);
  150. s.append(buf);
  151. break;
  152. }
  153. s.push_back('/');
  154. switch(_protocol.magnitude()) {
  155. case 0:
  156. s.push_back('*');
  157. break;
  158. case 1:
  159. sprintf(buf,"%u",_protocol.start);
  160. s.append(buf);
  161. break;
  162. default:
  163. sprintf(buf,"%u-%u",_protocol.start,_protocol.end);
  164. s.append(buf);
  165. break;
  166. }
  167. s.push_back('/');
  168. switch(_port.magnitude()) {
  169. case 0:
  170. s.push_back('*');
  171. break;
  172. case 1:
  173. sprintf(buf,"%u",_port.start);
  174. s.append(buf);
  175. break;
  176. default:
  177. sprintf(buf,"%u-%u",_port.start,_port.end);
  178. s.append(buf);
  179. break;
  180. }
  181. return s;
  182. }
  183. void Filter::add(const Rule &r,const Action &a)
  184. {
  185. Mutex::Lock _l(_chain_m);
  186. for(std::vector<Entry>::iterator i(_chain.begin());i!=_chain.end();++i) {
  187. if (i->rule == r) {
  188. _chain.erase(i);
  189. break;
  190. }
  191. }
  192. _chain.push_back(Entry(r,a));
  193. }
  194. std::string Filter::toString(const char *sep) const
  195. {
  196. if (!sep)
  197. sep = ",";
  198. std::string s;
  199. bool first = true;
  200. Mutex::Lock _l(_chain_m);
  201. for(std::vector<Entry>::const_iterator i(_chain.begin());i!=_chain.end();++i) {
  202. s.append(i->rule.toString());
  203. if (first)
  204. first = false;
  205. else s.append(sep);
  206. }
  207. return s;
  208. }
  209. const char *Filter::etherTypeName(const unsigned int etherType)
  210. throw()
  211. {
  212. switch(etherType) {
  213. case ZT_ETHERTYPE_IPV4: return "ETHERTYPE_IPV4";
  214. case ZT_ETHERTYPE_ARP: return "ETHERTYPE_ARP";
  215. case ZT_ETHERTYPE_RARP: return "ETHERTYPE_RARP";
  216. case ZT_ETHERTYPE_ATALK: return "ETHERTYPE_ATALK";
  217. case ZT_ETHERTYPE_AARP: return "ETHERTYPE_AARP";
  218. case ZT_ETHERTYPE_IPX_A: return "ETHERTYPE_IPX_A";
  219. case ZT_ETHERTYPE_IPX_B: return "ETHERTYPE_IPX_B";
  220. case ZT_ETHERTYPE_IPV6: return "ETHERTYPE_IPV6";
  221. }
  222. return UNKNOWN_NAME;
  223. }
  224. const char *Filter::ipProtocolName(const unsigned int ipp)
  225. throw()
  226. {
  227. switch(ipp) {
  228. case ZT_IPPROTO_ICMP: return "IPPROTO_ICMP";
  229. case ZT_IPPROTO_IGMP: return "IPPROTO_IGMP";
  230. case ZT_IPPROTO_TCP: return "IPPROTO_TCP";
  231. case ZT_IPPROTO_UDP: return "IPPROTO_UDP";
  232. case ZT_IPPROTO_GRE: return "IPPROTO_GRE";
  233. case ZT_IPPROTO_ESP: return "IPPROTO_ESP";
  234. case ZT_IPPROTO_AH: return "IPPROTO_AH";
  235. case ZT_IPPROTO_ICMPV6: return "IPPROTO_ICMPV6";
  236. case ZT_IPPROTO_OSPF: return "IPPROTO_OSPF";
  237. case ZT_IPPROTO_IPIP: return "IPPROTO_IPIP";
  238. case ZT_IPPROTO_IPCOMP: return "IPPROTO_IPCOMP";
  239. case ZT_IPPROTO_L2TP: return "IPPROTO_L2TP";
  240. case ZT_IPPROTO_SCTP: return "IPPROTO_SCTP";
  241. case ZT_IPPROTO_FC: return "IPPROTO_FC";
  242. case ZT_IPPROTO_UDPLITE: return "IPPROTO_UDPLITE";
  243. case ZT_IPPROTO_HIP: return "IPPROTO_HIP";
  244. }
  245. return UNKNOWN_NAME;
  246. }
  247. const char *Filter::icmpTypeName(const unsigned int icmpType)
  248. throw()
  249. {
  250. switch(icmpType) {
  251. case ZT_ICMP_ECHO_REPLY: return "ICMP_ECHO_REPLY";
  252. case ZT_ICMP_DESTINATION_UNREACHABLE: return "ICMP_DESTINATION_UNREACHABLE";
  253. case ZT_ICMP_SOURCE_QUENCH: return "ICMP_SOURCE_QUENCH";
  254. case ZT_ICMP_REDIRECT: return "ICMP_REDIRECT";
  255. case ZT_ICMP_ALTERNATE_HOST_ADDRESS: return "ICMP_ALTERNATE_HOST_ADDRESS";
  256. case ZT_ICMP_ECHO_REQUEST: return "ICMP_ECHO_REQUEST";
  257. case ZT_ICMP_ROUTER_ADVERTISEMENT: return "ICMP_ROUTER_ADVERTISEMENT";
  258. case ZT_ICMP_ROUTER_SOLICITATION: return "ICMP_ROUTER_SOLICITATION";
  259. case ZT_ICMP_TIME_EXCEEDED: return "ICMP_TIME_EXCEEDED";
  260. case ZT_ICMP_BAD_IP_HEADER: return "ICMP_BAD_IP_HEADER";
  261. case ZT_ICMP_TIMESTAMP: return "ICMP_TIMESTAMP";
  262. case ZT_ICMP_TIMESTAMP_REPLY: return "ICMP_TIMESTAMP_REPLY";
  263. case ZT_ICMP_INFORMATION_REQUEST: return "ICMP_INFORMATION_REQUEST";
  264. case ZT_ICMP_INFORMATION_REPLY: return "ICMP_INFORMATION_REPLY";
  265. case ZT_ICMP_ADDRESS_MASK_REQUEST: return "ICMP_ADDRESS_MASK_REQUEST";
  266. case ZT_ICMP_ADDRESS_MASK_REPLY: return "ICMP_ADDRESS_MASK_REPLY";
  267. case ZT_ICMP_TRACEROUTE: return "ICMP_TRACEROUTE";
  268. case ZT_ICMP_MOBILE_HOST_REDIRECT: return "ICMP_MOBILE_HOST_REDIRECT";
  269. case ZT_ICMP_MOBILE_REGISTRATION_REQUEST: return "ICMP_MOBILE_REGISTRATION_REQUEST";
  270. case ZT_ICMP_MOBILE_REGISTRATION_REPLY: return "ICMP_MOBILE_REGISTRATION_REPLY";
  271. }
  272. return UNKNOWN_NAME;
  273. }
  274. const char *Filter::icmp6TypeName(const unsigned int icmp6Type)
  275. throw()
  276. {
  277. switch(icmp6Type) {
  278. case ZT_ICMP6_DESTINATION_UNREACHABLE: return "ICMP6_DESTINATION_UNREACHABLE";
  279. case ZT_ICMP6_PACKET_TOO_BIG: return "ICMP6_PACKET_TOO_BIG";
  280. case ZT_ICMP6_TIME_EXCEEDED: return "ICMP6_TIME_EXCEEDED";
  281. case ZT_ICMP6_PARAMETER_PROBLEM: return "ICMP6_PARAMETER_PROBLEM";
  282. case ZT_ICMP6_ECHO_REQUEST: return "ICMP6_ECHO_REQUEST";
  283. case ZT_ICMP6_ECHO_REPLY: return "ICMP6_ECHO_REPLY";
  284. case ZT_ICMP6_MULTICAST_LISTENER_QUERY: return "ICMP6_MULTICAST_LISTENER_QUERY";
  285. case ZT_ICMP6_MULTICAST_LISTENER_REPORT: return "ICMP6_MULTICAST_LISTENER_REPORT";
  286. case ZT_ICMP6_MULTICAST_LISTENER_DONE: return "ICMP6_MULTICAST_LISTENER_DONE";
  287. case ZT_ICMP6_ROUTER_SOLICITATION: return "ICMP6_ROUTER_SOLICITATION";
  288. case ZT_ICMP6_ROUTER_ADVERTISEMENT: return "ICMP6_ROUTER_ADVERTISEMENT";
  289. case ZT_ICMP6_NEIGHBOR_SOLICITATION: return "ICMP6_NEIGHBOR_SOLICITATION";
  290. case ZT_ICMP6_NEIGHBOR_ADVERTISEMENT: return "ICMP6_NEIGHBOR_ADVERTISEMENT";
  291. case ZT_ICMP6_REDIRECT_MESSAGE: return "ICMP6_REDIRECT_MESSAGE";
  292. case ZT_ICMP6_ROUTER_RENUMBERING: return "ICMP6_ROUTER_RENUMBERING";
  293. case ZT_ICMP6_NODE_INFORMATION_QUERY: return "ICMP6_NODE_INFORMATION_QUERY";
  294. case ZT_ICMP6_NODE_INFORMATION_RESPONSE: return "ICMP6_NODE_INFORMATION_RESPONSE";
  295. case ZT_ICMP6_INV_NEIGHBOR_SOLICITATION: return "ICMP6_INV_NEIGHBOR_SOLICITATION";
  296. case ZT_ICMP6_INV_NEIGHBOR_ADVERTISEMENT: return "ICMP6_INV_NEIGHBOR_ADVERTISEMENT";
  297. case ZT_ICMP6_MLDV2: return "ICMP6_MLDV2";
  298. case ZT_ICMP6_HOME_AGENT_ADDRESS_DISCOVERY_REQUEST: return "ICMP6_HOME_AGENT_ADDRESS_DISCOVERY_REQUEST";
  299. case ZT_ICMP6_HOME_AGENT_ADDRESS_DISCOVERY_REPLY: return "ICMP6_HOME_AGENT_ADDRESS_DISCOVERY_REPLY";
  300. case ZT_ICMP6_MOBILE_PREFIX_SOLICITATION: return "ICMP6_MOBILE_PREFIX_SOLICITATION";
  301. case ZT_ICMP6_MOBILE_PREFIX_ADVERTISEMENT: return "ICMP6_MOBILE_PREFIX_ADVERTISEMENT";
  302. case ZT_ICMP6_CERTIFICATION_PATH_SOLICITATION: return "ICMP6_CERTIFICATION_PATH_SOLICITATION";
  303. case ZT_ICMP6_CERTIFICATION_PATH_ADVERTISEMENT: return "ICMP6_CERTIFICATION_PATH_ADVERTISEMENT";
  304. case ZT_ICMP6_MULTICAST_ROUTER_ADVERTISEMENT: return "ICMP6_MULTICAST_ROUTER_ADVERTISEMENT";
  305. case ZT_ICMP6_MULTICAST_ROUTER_SOLICITATION: return "ICMP6_MULTICAST_ROUTER_SOLICITATION";
  306. case ZT_ICMP6_MULTICAST_ROUTER_TERMINATION: return "ICMP6_MULTICAST_ROUTER_TERMINATION";
  307. case ZT_ICMP6_RPL_CONTROL_MESSAGE: return "ICMP6_RPL_CONTROL_MESSAGE";
  308. }
  309. return UNKNOWN_NAME;
  310. }
  311. Filter::Action Filter::operator()(const RuntimeEnvironment *_r,unsigned int etherType,const void *frame,unsigned int len) const
  312. {
  313. Mutex::Lock _l(_chain_m);
  314. TRACE("starting match against %d rules",(int)_chain.size());
  315. int ruleNo = 0;
  316. for(std::vector<Entry>::const_iterator r(_chain.begin());r!=_chain.end();++r,++ruleNo) {
  317. try {
  318. if (r->rule(etherType,frame,len)) {
  319. TRACE("match: %s",r->rule.toString().c_str());
  320. switch(r->action) {
  321. case ACTION_ALLOW:
  322. case ACTION_DENY:
  323. return r->action;
  324. default:
  325. break;
  326. }
  327. } else {
  328. TRACE("no match: %s",r->rule.toString().c_str());
  329. }
  330. } catch (std::invalid_argument &exc) {
  331. LOG("filter: unable to parse packet on rule %s (%d): %s",r->rule.toString().c_str(),ruleNo,exc.what());
  332. return ACTION_UNPARSEABLE;
  333. } catch ( ... ) {
  334. LOG("filter: unable to parse packet on rule %s (%d): unknown exception",r->rule.toString().c_str(),ruleNo);
  335. return ACTION_UNPARSEABLE;
  336. }
  337. }
  338. return ACTION_ALLOW;
  339. }
  340. } // namespace ZeroTier