Filter.cpp 13 KB

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