Network.cpp 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "Network.hpp"
  14. #include "../include/ZeroTierDebug.h"
  15. #include "../version.h"
  16. #include "Address.hpp"
  17. #include "Buffer.hpp"
  18. #include "Constants.hpp"
  19. #include "InetAddress.hpp"
  20. #include "MAC.hpp"
  21. #include "Metrics.hpp"
  22. #include "NetworkController.hpp"
  23. #include "Node.hpp"
  24. #include "Packet.hpp"
  25. #include "Peer.hpp"
  26. #include "RuntimeEnvironment.hpp"
  27. #include "Switch.hpp"
  28. #include "Trace.hpp"
  29. #include <math.h>
  30. #include <set>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. namespace ZeroTier {
  35. namespace {
  36. // Returns true if packet appears valid; pos and proto will be set
  37. static inline bool _ipv6GetPayload(const uint8_t* frameData, unsigned int frameLen, unsigned int& pos, unsigned int& proto)
  38. {
  39. if (frameLen < 40) {
  40. return false;
  41. }
  42. pos = 40;
  43. proto = frameData[6];
  44. while (pos <= frameLen) {
  45. switch (proto) {
  46. case 0: // hop-by-hop options
  47. case 43: // routing
  48. case 60: // destination options
  49. case 135: // mobility options
  50. if ((pos + 8) > frameLen) {
  51. return false; // invalid!
  52. }
  53. proto = frameData[pos];
  54. pos += ((unsigned int)frameData[pos + 1] * 8) + 8;
  55. break;
  56. // case 44: // fragment -- we currently can't parse these and they are deprecated in IPv6 anyway
  57. // case 50:
  58. // case 51: // IPSec ESP and AH -- we have to stop here since this is encrypted stuff
  59. default:
  60. return true;
  61. }
  62. }
  63. return false; // overflow == invalid
  64. }
  65. enum _doZtFilterResult { DOZTFILTER_NO_MATCH, DOZTFILTER_DROP, DOZTFILTER_REDIRECT, DOZTFILTER_ACCEPT, DOZTFILTER_SUPER_ACCEPT };
  66. static _doZtFilterResult _doZtFilter(
  67. const RuntimeEnvironment* RR,
  68. Trace::RuleResultLog& rrl,
  69. const NetworkConfig& nconf,
  70. const Membership* membership, // can be NULL
  71. const bool inbound,
  72. const Address& ztSource,
  73. Address& ztDest, // MUTABLE -- is changed on REDIRECT actions
  74. const MAC& macSource,
  75. const MAC& macDest,
  76. const uint8_t* const frameData,
  77. const unsigned int frameLen,
  78. const unsigned int etherType,
  79. const unsigned int vlanId,
  80. const ZT_VirtualNetworkRule* rules, // cannot be NULL
  81. const unsigned int ruleCount,
  82. Address& cc, // MUTABLE -- set to TEE destination if TEE action is taken or left alone otherwise
  83. unsigned int& ccLength, // MUTABLE -- set to length of packet payload to TEE
  84. bool& ccWatch, // MUTABLE -- set to true for WATCH target as opposed to normal TEE
  85. uint8_t& qosBucket) // MUTABLE -- set to the value of the argument provided to PRIORITY
  86. {
  87. // Set to true if we are a TEE/REDIRECT/WATCH target
  88. bool superAccept = false;
  89. // The default match state for each set of entries starts as 'true' since an
  90. // ACTION with no MATCH entries preceding it is always taken.
  91. uint8_t thisSetMatches = 1;
  92. uint8_t skipDrop = 0;
  93. rrl.clear();
  94. // uncomment for easier debugging fprintf
  95. // if (!ztDest) { return DOZTFILTER_ACCEPT; }
  96. #ifdef ZT_TRACE
  97. // char buf[40], buf2[40];
  98. // fprintf(stderr, "\nsrc %s dest %s inbound: %d ethertype %u", ztSource.toString(buf), ztDest.toString(buf2), inbound, etherType);
  99. #endif
  100. for (unsigned int rn = 0; rn < ruleCount; ++rn) {
  101. const ZT_VirtualNetworkRuleType rt = (ZT_VirtualNetworkRuleType)(rules[rn].t & 0x3f);
  102. #ifdef ZT_TRACE
  103. // fprintf(stderr, "\n%02u %02d", rn, rt);
  104. #endif
  105. // First check if this is an ACTION
  106. if ((unsigned int)rt <= (unsigned int)ZT_NETWORK_RULE_ACTION__MAX_ID) {
  107. if (thisSetMatches) {
  108. switch (rt) {
  109. case ZT_NETWORK_RULE_ACTION_PRIORITY:
  110. qosBucket = (rules[rn].v.qosBucket <= 8) ? rules[rn].v.qosBucket : 4; // 4 = default bucket (no priority)
  111. return DOZTFILTER_ACCEPT;
  112. case ZT_NETWORK_RULE_ACTION_DROP: {
  113. if (! ! skipDrop) {
  114. #ifdef ZT_TRACE
  115. // fprintf(stderr, "\tskip Drop");
  116. #endif
  117. skipDrop = 0;
  118. continue;
  119. }
  120. #ifdef ZT_TRACE
  121. // fprintf(stderr, "\tDrop\n");
  122. #endif
  123. return DOZTFILTER_DROP;
  124. }
  125. case ZT_NETWORK_RULE_ACTION_ACCEPT: {
  126. #ifdef ZT_TRACE
  127. // fprintf(stderr, "\tAccept\n");
  128. #endif
  129. return (superAccept ? DOZTFILTER_SUPER_ACCEPT : DOZTFILTER_ACCEPT); // match, accept packet
  130. }
  131. // These are initially handled together since preliminary logic is common
  132. case ZT_NETWORK_RULE_ACTION_TEE:
  133. case ZT_NETWORK_RULE_ACTION_WATCH:
  134. case ZT_NETWORK_RULE_ACTION_REDIRECT: {
  135. const Address fwdAddr(rules[rn].v.fwd.address);
  136. if (fwdAddr == ztSource) {
  137. // Skip as no-op since source is target
  138. }
  139. else if (fwdAddr == RR->identity.address()) {
  140. if (inbound) {
  141. return DOZTFILTER_SUPER_ACCEPT;
  142. }
  143. else {
  144. }
  145. }
  146. else if (fwdAddr == ztDest) {
  147. }
  148. else {
  149. if (rt == ZT_NETWORK_RULE_ACTION_REDIRECT) {
  150. ztDest = fwdAddr;
  151. return DOZTFILTER_REDIRECT;
  152. }
  153. else {
  154. cc = fwdAddr;
  155. ccLength = (rules[rn].v.fwd.length != 0) ? ((frameLen < (unsigned int)rules[rn].v.fwd.length) ? frameLen : (unsigned int)rules[rn].v.fwd.length) : frameLen;
  156. ccWatch = (rt == ZT_NETWORK_RULE_ACTION_WATCH);
  157. }
  158. }
  159. }
  160. continue;
  161. case ZT_NETWORK_RULE_ACTION_BREAK:
  162. return DOZTFILTER_NO_MATCH;
  163. // Unrecognized ACTIONs are ignored as no-ops
  164. default:
  165. continue;
  166. }
  167. }
  168. else {
  169. // If this is an incoming packet and we are a TEE or REDIRECT target, we should
  170. // super-accept if we accept at all. This will cause us to accept redirected or
  171. // tee'd packets in spite of MAC and ZT addressing checks.
  172. if (inbound) {
  173. switch (rt) {
  174. case ZT_NETWORK_RULE_ACTION_TEE:
  175. case ZT_NETWORK_RULE_ACTION_WATCH:
  176. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  177. if (RR->identity.address() == rules[rn].v.fwd.address) {
  178. superAccept = true;
  179. }
  180. break;
  181. default:
  182. break;
  183. }
  184. }
  185. thisSetMatches = 1; // reset to default true for next batch of entries
  186. continue;
  187. }
  188. }
  189. // Circuit breaker: no need to evaluate an AND if the set's match state
  190. // is currently false since anything AND false is false.
  191. if ((! thisSetMatches) && (! (rules[rn].t & 0x40))) {
  192. rrl.logSkipped(rn, thisSetMatches);
  193. continue;
  194. }
  195. // If this was not an ACTION evaluate next MATCH and update thisSetMatches with (AND [result])
  196. uint8_t thisRuleMatches = 0;
  197. uint64_t ownershipVerificationMask = 1; // this magic value means it hasn't been computed yet -- this is done lazily the first time it's needed
  198. uint8_t hardYes = (rules[rn].t >> 7) ^ 1; // XOR with the NOT bit of the rule
  199. uint8_t hardNo = (rules[rn].t >> 7) ^ 0;
  200. switch (rt) {
  201. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  202. thisRuleMatches = (uint8_t)(rules[rn].v.zt == ztSource.toInt());
  203. break;
  204. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  205. thisRuleMatches = (uint8_t)(rules[rn].v.zt == ztDest.toInt());
  206. break;
  207. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  208. thisRuleMatches = (uint8_t)(rules[rn].v.vlanId == (uint16_t)vlanId);
  209. break;
  210. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  211. // NOT SUPPORTED YET
  212. thisRuleMatches = (uint8_t)(rules[rn].v.vlanPcp == 0);
  213. break;
  214. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  215. // NOT SUPPORTED YET
  216. thisRuleMatches = (uint8_t)(rules[rn].v.vlanDei == 0);
  217. break;
  218. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  219. thisRuleMatches = (uint8_t)(MAC(rules[rn].v.mac, 6) == macSource);
  220. break;
  221. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  222. thisRuleMatches = (uint8_t)(MAC(rules[rn].v.mac, 6) == macDest);
  223. break;
  224. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  225. if ((etherType == ZT_ETHERTYPE_IPV4) && (frameLen >= 20)) {
  226. 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)));
  227. }
  228. else {
  229. thisRuleMatches = hardNo;
  230. }
  231. break;
  232. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  233. if ((etherType == ZT_ETHERTYPE_IPV4) && (frameLen >= 20)) {
  234. 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)));
  235. }
  236. else {
  237. thisRuleMatches = hardNo;
  238. }
  239. break;
  240. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  241. if ((etherType == ZT_ETHERTYPE_IPV6) && (frameLen >= 40)) {
  242. 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)));
  243. }
  244. else {
  245. thisRuleMatches = hardNo;
  246. }
  247. break;
  248. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  249. if ((etherType == ZT_ETHERTYPE_IPV6) && (frameLen >= 40)) {
  250. 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)));
  251. }
  252. else {
  253. thisRuleMatches = hardNo;
  254. }
  255. break;
  256. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  257. if ((etherType == ZT_ETHERTYPE_IPV4) && (frameLen >= 20)) {
  258. const uint8_t tosMasked = frameData[1] & rules[rn].v.ipTos.mask;
  259. thisRuleMatches = (uint8_t)((tosMasked >= rules[rn].v.ipTos.value[0]) && (tosMasked <= rules[rn].v.ipTos.value[1]));
  260. }
  261. else if ((etherType == ZT_ETHERTYPE_IPV6) && (frameLen >= 40)) {
  262. const uint8_t tosMasked = (((frameData[0] << 4) & 0xf0) | ((frameData[1] >> 4) & 0x0f)) & rules[rn].v.ipTos.mask;
  263. thisRuleMatches = (uint8_t)((tosMasked >= rules[rn].v.ipTos.value[0]) && (tosMasked <= rules[rn].v.ipTos.value[1]));
  264. }
  265. else {
  266. thisRuleMatches = hardNo;
  267. }
  268. break;
  269. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  270. if ((etherType == ZT_ETHERTYPE_IPV4) && (frameLen >= 20)) {
  271. thisRuleMatches = (uint8_t)(rules[rn].v.ipProtocol == frameData[9]);
  272. }
  273. else if (etherType == ZT_ETHERTYPE_IPV6) {
  274. unsigned int pos = 0, proto = 0;
  275. if (_ipv6GetPayload(frameData, frameLen, pos, proto)) {
  276. thisRuleMatches = (uint8_t)(rules[rn].v.ipProtocol == (uint8_t)proto);
  277. }
  278. else {
  279. thisRuleMatches = hardNo;
  280. }
  281. }
  282. else {
  283. thisRuleMatches = hardNo;
  284. }
  285. break;
  286. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  287. thisRuleMatches = (uint8_t)(rules[rn].v.etherType == (uint16_t)etherType);
  288. break;
  289. case ZT_NETWORK_RULE_MATCH_ICMP:
  290. if ((etherType == ZT_ETHERTYPE_IPV4) && (frameLen >= 20)) {
  291. if (frameData[9] == 0x01) { // IP protocol == ICMP
  292. const unsigned int ihl = (frameData[0] & 0xf) * 4;
  293. if (frameLen >= (ihl + 2)) {
  294. if (rules[rn].v.icmp.type == frameData[ihl]) {
  295. if ((rules[rn].v.icmp.flags & 0x01) != 0) {
  296. thisRuleMatches = (uint8_t)(frameData[ihl + 1] == rules[rn].v.icmp.code);
  297. }
  298. else {
  299. thisRuleMatches = hardYes;
  300. }
  301. }
  302. else {
  303. thisRuleMatches = hardNo;
  304. }
  305. }
  306. else {
  307. thisRuleMatches = hardNo;
  308. }
  309. }
  310. else {
  311. thisRuleMatches = hardNo;
  312. }
  313. }
  314. else if (etherType == ZT_ETHERTYPE_IPV6) {
  315. unsigned int pos = 0, proto = 0;
  316. if (_ipv6GetPayload(frameData, frameLen, pos, proto)) {
  317. if ((proto == 0x3a) && (frameLen >= (pos + 2))) {
  318. if (rules[rn].v.icmp.type == frameData[pos]) {
  319. if ((rules[rn].v.icmp.flags & 0x01) != 0) {
  320. thisRuleMatches = (uint8_t)(frameData[pos + 1] == rules[rn].v.icmp.code);
  321. }
  322. else {
  323. thisRuleMatches = hardYes;
  324. }
  325. }
  326. else {
  327. thisRuleMatches = hardNo;
  328. }
  329. }
  330. else {
  331. thisRuleMatches = hardNo;
  332. }
  333. }
  334. else {
  335. thisRuleMatches = hardNo;
  336. }
  337. }
  338. else {
  339. thisRuleMatches = hardNo;
  340. }
  341. break;
  342. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  343. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  344. if ((etherType == ZT_ETHERTYPE_IPV4) && (frameLen >= 20)) {
  345. const unsigned int headerLen = 4 * (frameData[0] & 0xf);
  346. int p = -1;
  347. switch (frameData[9]) { // IP protocol number
  348. // All these start with 16-bit source and destination port in that order
  349. case 0x06: // TCP
  350. case 0x11: // UDP
  351. case 0x84: // SCTP
  352. case 0x88: // UDPLite
  353. if (frameLen > (headerLen + 4)) {
  354. unsigned int pos = headerLen + ((rt == ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE) ? 2 : 0);
  355. p = (int)frameData[pos++] << 8;
  356. p |= (int)frameData[pos];
  357. }
  358. break;
  359. }
  360. thisRuleMatches = (p >= 0) ? (uint8_t)((p >= (int)rules[rn].v.port[0]) && (p <= (int)rules[rn].v.port[1])) : (uint8_t)0;
  361. }
  362. else if (etherType == ZT_ETHERTYPE_IPV6) {
  363. unsigned int pos = 0, proto = 0;
  364. if (_ipv6GetPayload(frameData, frameLen, pos, proto)) {
  365. int p = -1;
  366. switch (proto) { // IP protocol number
  367. // All these start with 16-bit source and destination port in that order
  368. case 0x06: // TCP
  369. case 0x11: // UDP
  370. case 0x84: // SCTP
  371. case 0x88: // UDPLite
  372. if (frameLen > (pos + 4)) {
  373. if (rt == ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE) {
  374. pos += 2;
  375. }
  376. p = (int)frameData[pos++] << 8;
  377. p |= (int)frameData[pos];
  378. }
  379. break;
  380. }
  381. thisRuleMatches = (p > 0) ? (uint8_t)((p >= (int)rules[rn].v.port[0]) && (p <= (int)rules[rn].v.port[1])) : (uint8_t)0;
  382. }
  383. else {
  384. thisRuleMatches = hardNo;
  385. }
  386. }
  387. else {
  388. thisRuleMatches = hardNo;
  389. }
  390. break;
  391. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS: {
  392. uint64_t cf = (inbound) ? ZT_RULE_PACKET_CHARACTERISTICS_INBOUND : 0ULL;
  393. if (macDest.isMulticast()) {
  394. cf |= ZT_RULE_PACKET_CHARACTERISTICS_MULTICAST;
  395. }
  396. if (macDest.isBroadcast()) {
  397. cf |= ZT_RULE_PACKET_CHARACTERISTICS_BROADCAST;
  398. }
  399. if (ownershipVerificationMask == 1) {
  400. ownershipVerificationMask = 0;
  401. InetAddress src;
  402. if ((etherType == ZT_ETHERTYPE_IPV4) && (frameLen >= 20)) {
  403. src.set((const void*)(frameData + 12), 4, 0);
  404. }
  405. else if ((etherType == ZT_ETHERTYPE_IPV6) && (frameLen >= 40)) {
  406. // IPv6 NDP requires special handling, since the src and dest IPs in the packet are empty or link-local.
  407. if ((frameLen >= (40 + 8 + 16)) && (frameData[6] == 0x3a) && ((frameData[40] == 0x87) || (frameData[40] == 0x88))) {
  408. if (frameData[40] == 0x87) {
  409. // Neighbor solicitations contain no reliable source address, so we implement a small
  410. // hack by considering them authenticated. Otherwise you would pretty much have to do
  411. // this manually in the rule set for IPv6 to work at all.
  412. ownershipVerificationMask |= ZT_RULE_PACKET_CHARACTERISTICS_SENDER_IP_AUTHENTICATED;
  413. }
  414. else {
  415. // Neighbor advertisements on the other hand can absolutely be authenticated.
  416. src.set((const void*)(frameData + 40 + 8), 16, 0);
  417. }
  418. }
  419. else {
  420. // Other IPv6 packets can be handled normally
  421. src.set((const void*)(frameData + 8), 16, 0);
  422. }
  423. }
  424. else if ((etherType == ZT_ETHERTYPE_ARP) && (frameLen >= 28)) {
  425. src.set((const void*)(frameData + 14), 4, 0);
  426. }
  427. if (inbound) {
  428. if (membership) {
  429. if ((src) && (membership->hasCertificateOfOwnershipFor<InetAddress>(nconf, src))) {
  430. ownershipVerificationMask |= ZT_RULE_PACKET_CHARACTERISTICS_SENDER_IP_AUTHENTICATED;
  431. }
  432. if (membership->hasCertificateOfOwnershipFor<MAC>(nconf, macSource)) {
  433. ownershipVerificationMask |= ZT_RULE_PACKET_CHARACTERISTICS_SENDER_MAC_AUTHENTICATED;
  434. }
  435. }
  436. }
  437. else {
  438. for (unsigned int i = 0; i < nconf.certificateOfOwnershipCount; ++i) {
  439. if ((src) && (nconf.certificatesOfOwnership[i].owns(src))) {
  440. ownershipVerificationMask |= ZT_RULE_PACKET_CHARACTERISTICS_SENDER_IP_AUTHENTICATED;
  441. }
  442. if (nconf.certificatesOfOwnership[i].owns(macSource)) {
  443. ownershipVerificationMask |= ZT_RULE_PACKET_CHARACTERISTICS_SENDER_MAC_AUTHENTICATED;
  444. }
  445. }
  446. }
  447. }
  448. cf |= ownershipVerificationMask;
  449. if ((etherType == ZT_ETHERTYPE_IPV4) && (frameLen >= 20) && (frameData[9] == 0x06)) {
  450. const unsigned int headerLen = 4 * (frameData[0] & 0xf);
  451. cf |= (uint64_t)frameData[headerLen + 13];
  452. cf |= (((uint64_t)(frameData[headerLen + 12] & 0x0f)) << 8);
  453. }
  454. else if (etherType == ZT_ETHERTYPE_IPV6) {
  455. unsigned int pos = 0, proto = 0;
  456. if (_ipv6GetPayload(frameData, frameLen, pos, proto)) {
  457. if ((proto == 0x06) && (frameLen > (pos + 14))) {
  458. cf |= (uint64_t)frameData[pos + 13];
  459. cf |= (((uint64_t)(frameData[pos + 12] & 0x0f)) << 8);
  460. }
  461. }
  462. }
  463. thisRuleMatches = (uint8_t)((cf & rules[rn].v.characteristics) != 0);
  464. } break;
  465. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  466. thisRuleMatches = (uint8_t)((frameLen >= (unsigned int)rules[rn].v.frameSize[0]) && (frameLen <= (unsigned int)rules[rn].v.frameSize[1]));
  467. break;
  468. case ZT_NETWORK_RULE_MATCH_RANDOM:
  469. thisRuleMatches = (uint8_t)((uint32_t)(RR->node->prng() & 0xffffffffULL) <= rules[rn].v.randomProbability);
  470. break;
  471. case ZT_NETWORK_RULE_MATCH_TAGS_DIFFERENCE:
  472. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_AND:
  473. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_OR:
  474. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_XOR:
  475. case ZT_NETWORK_RULE_MATCH_TAGS_EQUAL: {
  476. const Tag* const localTag = std::lower_bound(&(nconf.tags[0]), &(nconf.tags[nconf.tagCount]), rules[rn].v.tag.id, Tag::IdComparePredicate());
  477. if ((localTag != &(nconf.tags[nconf.tagCount])) && (localTag->id() == rules[rn].v.tag.id)) {
  478. const Tag* const remoteTag = ((membership) ? membership->getTag(nconf, rules[rn].v.tag.id) : (const Tag*)0);
  479. #ifdef ZT_TRACE
  480. /*fprintf(stderr, "\tlocal tag [%u: %u] remote tag [%u: %u] match [%u]",
  481. !!localTag ? localTag->id() : 0,
  482. !!localTag ? localTag->value() : 0,
  483. !!remoteTag ? remoteTag->id() : 0,
  484. !!remoteTag ? remoteTag->value() : 0,
  485. thisRuleMatches);*/
  486. #endif
  487. if (remoteTag) {
  488. const uint32_t ltv = localTag->value();
  489. const uint32_t rtv = remoteTag->value();
  490. if (rt == ZT_NETWORK_RULE_MATCH_TAGS_DIFFERENCE) {
  491. const uint32_t diff = (ltv > rtv) ? (ltv - rtv) : (rtv - ltv);
  492. thisRuleMatches = (uint8_t)(diff <= rules[rn].v.tag.value);
  493. }
  494. else if (rt == ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_AND) {
  495. thisRuleMatches = (uint8_t)((ltv & rtv) == rules[rn].v.tag.value);
  496. }
  497. else if (rt == ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_OR) {
  498. thisRuleMatches = (uint8_t)((ltv | rtv) == rules[rn].v.tag.value);
  499. }
  500. else if (rt == ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_XOR) {
  501. thisRuleMatches = (uint8_t)((ltv ^ rtv) == rules[rn].v.tag.value);
  502. }
  503. else if (rt == ZT_NETWORK_RULE_MATCH_TAGS_EQUAL) {
  504. thisRuleMatches = (uint8_t)((ltv == rules[rn].v.tag.value) && (rtv == rules[rn].v.tag.value));
  505. }
  506. else { // sanity check, can't really happen
  507. thisRuleMatches = hardNo;
  508. }
  509. }
  510. else {
  511. if ((inbound) && (! superAccept)) {
  512. thisRuleMatches = hardNo;
  513. #ifdef ZT_TRACE
  514. // fprintf(stderr, "\tinbound ");
  515. #endif
  516. }
  517. else {
  518. // Outbound side is not strict since if we have to match both tags and
  519. // we are sending a first packet to a recipient, we probably do not know
  520. // about their tags yet. They will filter on inbound and we will filter
  521. // once we get their tag. If we are a tee/redirect target we are also
  522. // not strict since we likely do not have these tags.
  523. skipDrop = 1;
  524. thisRuleMatches = hardYes;
  525. #ifdef ZT_TRACE
  526. // fprintf(stderr, "\toutbound ");
  527. #endif
  528. }
  529. }
  530. }
  531. else {
  532. thisRuleMatches = hardNo;
  533. }
  534. } break;
  535. case ZT_NETWORK_RULE_MATCH_TAG_SENDER:
  536. case ZT_NETWORK_RULE_MATCH_TAG_RECEIVER: {
  537. const Tag* const localTag = std::lower_bound(&(nconf.tags[0]), &(nconf.tags[nconf.tagCount]), rules[rn].v.tag.id, Tag::IdComparePredicate());
  538. #ifdef ZT_TRACE
  539. /*const Tag *const remoteTag = ((membership) ? membership->getTag(nconf,rules[rn].v.tag.id) : (const Tag *)0);
  540. fprintf(stderr, "\tlocal tag [%u: %u] remote tag [%u: %u] match [%u]",
  541. !!localTag ? localTag->id() : 0,
  542. !!localTag ? localTag->value() : 0,
  543. !!remoteTag ? remoteTag->id() : 0,
  544. !!remoteTag ? remoteTag->value() : 0,
  545. thisRuleMatches);*/
  546. #endif
  547. if (superAccept) {
  548. skipDrop = 1;
  549. thisRuleMatches = hardYes;
  550. }
  551. else if (((rt == ZT_NETWORK_RULE_MATCH_TAG_SENDER) && (inbound)) || ((rt == ZT_NETWORK_RULE_MATCH_TAG_RECEIVER) && (! inbound))) {
  552. const Tag* const remoteTag = ((membership) ? membership->getTag(nconf, rules[rn].v.tag.id) : (const Tag*)0);
  553. if (remoteTag) {
  554. thisRuleMatches = (uint8_t)(remoteTag->value() == rules[rn].v.tag.value);
  555. }
  556. else {
  557. if (rt == ZT_NETWORK_RULE_MATCH_TAG_RECEIVER) {
  558. // If we are checking the receiver and this is an outbound packet, we
  559. // can't be strict since we may not yet know the receiver's tag.
  560. skipDrop = 1;
  561. thisRuleMatches = hardYes;
  562. }
  563. else {
  564. thisRuleMatches = hardNo;
  565. }
  566. }
  567. }
  568. else { // sender and outbound or receiver and inbound
  569. if ((localTag != &(nconf.tags[nconf.tagCount])) && (localTag->id() == rules[rn].v.tag.id)) {
  570. thisRuleMatches = (uint8_t)(localTag->value() == rules[rn].v.tag.value);
  571. }
  572. else {
  573. thisRuleMatches = hardNo;
  574. }
  575. }
  576. } break;
  577. case ZT_NETWORK_RULE_MATCH_INTEGER_RANGE: {
  578. uint64_t integer = 0;
  579. const unsigned int bits = (rules[rn].v.intRange.format & 63) + 1;
  580. const unsigned int bytes = ((bits + 8 - 1) / 8); // integer ceiling of division by 8
  581. if ((rules[rn].v.intRange.format & 0x80) == 0) {
  582. // Big-endian
  583. unsigned int idx = rules[rn].v.intRange.idx + (8 - bytes);
  584. const unsigned int eof = idx + bytes;
  585. if (eof <= frameLen) {
  586. while (idx < eof) {
  587. integer <<= 8;
  588. integer |= frameData[idx++];
  589. }
  590. }
  591. integer &= 0xffffffffffffffffULL >> (64 - bits);
  592. }
  593. else {
  594. // Little-endian
  595. unsigned int idx = rules[rn].v.intRange.idx;
  596. const unsigned int eof = idx + bytes;
  597. if (eof <= frameLen) {
  598. while (idx < eof) {
  599. integer >>= 8;
  600. integer |= ((uint64_t)frameData[idx++]) << 56;
  601. }
  602. }
  603. integer >>= (64 - bits);
  604. }
  605. thisRuleMatches = (uint8_t)((integer >= rules[rn].v.intRange.start) && (integer <= (rules[rn].v.intRange.start + (uint64_t)rules[rn].v.intRange.end)));
  606. } break;
  607. // The result of an unsupported MATCH is configurable at the network
  608. // level via a flag.
  609. default:
  610. thisRuleMatches = (uint8_t)((nconf.flags & ZT_NETWORKCONFIG_FLAG_RULES_RESULT_OF_UNSUPPORTED_MATCH) != 0);
  611. break;
  612. }
  613. rrl.log(rn, thisRuleMatches, thisSetMatches);
  614. if ((rules[rn].t & 0x40)) {
  615. thisSetMatches |= (thisRuleMatches ^ ((rules[rn].t >> 7) & 1));
  616. }
  617. else {
  618. thisSetMatches &= (thisRuleMatches ^ ((rules[rn].t >> 7) & 1));
  619. }
  620. }
  621. return DOZTFILTER_NO_MATCH;
  622. }
  623. } // anonymous namespace
  624. const ZeroTier::MulticastGroup Network::BROADCAST(ZeroTier::MAC(0xffffffffffffULL), 0);
  625. Network::Network(const RuntimeEnvironment* renv, void* tPtr, uint64_t nwid, void* uptr, const NetworkConfig* nconf)
  626. : RR(renv)
  627. , _uPtr(uptr)
  628. , _id(nwid)
  629. , _nwidStr(OSUtils::networkIDStr(nwid))
  630. , _lastAnnouncedMulticastGroupsUpstream(0)
  631. , _mac(renv->identity.address(), nwid)
  632. , _portInitialized(false)
  633. , _lastConfigUpdate(0)
  634. , _destroyed(false)
  635. , _netconfFailure(NETCONF_FAILURE_NONE)
  636. , _portError(0)
  637. , _num_multicast_groups { Metrics::network_num_multicast_groups.Add({ { "network_id", _nwidStr } }) }
  638. , _incoming_packets_accepted { Metrics::network_packets.Add({ { "direction", "rx" }, { "network_id", _nwidStr }, { "accepted", "yes" } }) }
  639. , _incoming_packets_dropped { Metrics::network_packets.Add({ { "direction", "rx" }, { "network_id", _nwidStr }, { "accepted", "no" } }) }
  640. , _outgoing_packets_accepted { Metrics::network_packets.Add({ { "direction", "tx" }, { "network_id", _nwidStr }, { "accepted", "yes" } }) }
  641. , _outgoing_packets_dropped { Metrics::network_packets.Add({ { "direction", "tx" }, { "network_id", _nwidStr }, { "accepted", "no" } }) }
  642. {
  643. for (int i = 0; i < ZT_NETWORK_MAX_INCOMING_UPDATES; ++i) {
  644. _incomingConfigChunks[i].ts = 0;
  645. }
  646. if (nconf) {
  647. this->setConfiguration(tPtr, *nconf, false);
  648. _lastConfigUpdate = 0; // still want to re-request since it's likely outdated
  649. }
  650. else {
  651. uint64_t tmp[2];
  652. tmp[0] = nwid;
  653. tmp[1] = 0;
  654. bool got = false;
  655. Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>* dict = new Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>();
  656. try {
  657. int n = RR->node->stateObjectGet(tPtr, ZT_STATE_OBJECT_NETWORK_CONFIG, tmp, dict->unsafeData(), ZT_NETWORKCONFIG_DICT_CAPACITY - 1);
  658. if (n > 1) {
  659. NetworkConfig* nconf = new NetworkConfig();
  660. try {
  661. if (nconf->fromDictionary(*dict)) {
  662. this->setConfiguration(tPtr, *nconf, false);
  663. _lastConfigUpdate = 0; // still want to re-request an update since it's likely outdated
  664. got = true;
  665. }
  666. }
  667. catch (...) {
  668. }
  669. delete nconf;
  670. }
  671. }
  672. catch (...) {
  673. }
  674. delete dict;
  675. if (! got) {
  676. RR->node->stateObjectPut(tPtr, ZT_STATE_OBJECT_NETWORK_CONFIG, tmp, "\n", 1);
  677. }
  678. }
  679. if (! _portInitialized) {
  680. ZT_VirtualNetworkConfig ctmp;
  681. memset(&ctmp, 0, sizeof(ZT_VirtualNetworkConfig));
  682. _externalConfig(&ctmp);
  683. _portError = RR->node->configureVirtualNetworkPort(tPtr, _id, &_uPtr, ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP, &ctmp);
  684. _portInitialized = true;
  685. }
  686. Metrics::network_num_joined++;
  687. }
  688. Network::~Network()
  689. {
  690. ZT_VirtualNetworkConfig ctmp;
  691. _externalConfig(&ctmp);
  692. Metrics::network_num_joined--;
  693. if (_destroyed) {
  694. // This is done in Node::leave() so we can pass tPtr properly
  695. // RR->node->configureVirtualNetworkPort((void *)0,_id,&_uPtr,ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY,&ctmp);
  696. }
  697. else {
  698. RR->node->configureVirtualNetworkPort((void*)0, _id, &_uPtr, ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN, &ctmp);
  699. }
  700. }
  701. bool Network::filterOutgoingPacket(
  702. void* tPtr,
  703. const bool noTee,
  704. const Address& ztSource,
  705. const Address& ztDest,
  706. const MAC& macSource,
  707. const MAC& macDest,
  708. const uint8_t* frameData,
  709. const unsigned int frameLen,
  710. const unsigned int etherType,
  711. const unsigned int vlanId,
  712. uint8_t& qosBucket)
  713. {
  714. Address ztFinalDest(ztDest);
  715. int localCapabilityIndex = -1;
  716. int accept = 0;
  717. Trace::RuleResultLog rrl, crrl;
  718. Address cc;
  719. unsigned int ccLength = 0;
  720. bool ccWatch = false;
  721. Mutex::Lock _l(_lock);
  722. Membership* const membership = (ztDest) ? _memberships.get(ztDest) : (Membership*)0;
  723. switch (_doZtFilter(RR, rrl, _config, membership, false, ztSource, ztFinalDest, macSource, macDest, frameData, frameLen, etherType, vlanId, _config.rules, _config.ruleCount, cc, ccLength, ccWatch, qosBucket)) {
  724. case DOZTFILTER_NO_MATCH: {
  725. for (unsigned int c = 0; c < _config.capabilityCount; ++c) {
  726. ztFinalDest = ztDest; // sanity check, shouldn't be possible if there was no match
  727. Address cc2;
  728. unsigned int ccLength2 = 0;
  729. bool ccWatch2 = false;
  730. switch (_doZtFilter(
  731. RR,
  732. crrl,
  733. _config,
  734. membership,
  735. false,
  736. ztSource,
  737. ztFinalDest,
  738. macSource,
  739. macDest,
  740. frameData,
  741. frameLen,
  742. etherType,
  743. vlanId,
  744. _config.capabilities[c].rules(),
  745. _config.capabilities[c].ruleCount(),
  746. cc2,
  747. ccLength2,
  748. ccWatch2,
  749. qosBucket)) {
  750. case DOZTFILTER_NO_MATCH:
  751. case DOZTFILTER_DROP: // explicit DROP in a capability just terminates its evaluation and is an anti-pattern
  752. break;
  753. case DOZTFILTER_REDIRECT: // interpreted as ACCEPT but ztFinalDest will have been changed in _doZtFilter()
  754. case DOZTFILTER_ACCEPT:
  755. case DOZTFILTER_SUPER_ACCEPT: // no difference in behavior on outbound side in capabilities
  756. localCapabilityIndex = (int)c;
  757. accept = 1;
  758. if ((! noTee) && (cc2)) {
  759. Packet outp(cc2, RR->identity.address(), Packet::VERB_EXT_FRAME);
  760. outp.append(_id);
  761. outp.append((uint8_t)(ccWatch2 ? 0x16 : 0x02));
  762. macDest.appendTo(outp);
  763. macSource.appendTo(outp);
  764. outp.append((uint16_t)etherType);
  765. outp.append(frameData, ccLength2);
  766. outp.compress();
  767. RR->sw->send(tPtr, outp, true);
  768. }
  769. break;
  770. }
  771. if (accept) {
  772. break;
  773. }
  774. }
  775. } break;
  776. case DOZTFILTER_DROP:
  777. if (_config.remoteTraceTarget) {
  778. RR->t->networkFilter(tPtr, *this, rrl, (Trace::RuleResultLog*)0, (Capability*)0, ztSource, ztDest, macSource, macDest, frameData, frameLen, etherType, vlanId, noTee, false, 0);
  779. }
  780. return false;
  781. case DOZTFILTER_REDIRECT: // interpreted as ACCEPT but ztFinalDest will have been changed in _doZtFilter()
  782. case DOZTFILTER_ACCEPT:
  783. accept = 1;
  784. break;
  785. case DOZTFILTER_SUPER_ACCEPT:
  786. accept = 2;
  787. break;
  788. }
  789. if (accept) {
  790. _outgoing_packets_accepted++;
  791. if ((! noTee) && (cc)) {
  792. Packet outp(cc, RR->identity.address(), Packet::VERB_EXT_FRAME);
  793. outp.append(_id);
  794. outp.append((uint8_t)(ccWatch ? 0x16 : 0x02));
  795. macDest.appendTo(outp);
  796. macSource.appendTo(outp);
  797. outp.append((uint16_t)etherType);
  798. outp.append(frameData, ccLength);
  799. outp.compress();
  800. RR->sw->send(tPtr, outp, true);
  801. }
  802. if ((ztDest != ztFinalDest) && (ztFinalDest)) {
  803. Packet outp(ztFinalDest, RR->identity.address(), Packet::VERB_EXT_FRAME);
  804. outp.append(_id);
  805. outp.append((uint8_t)0x04);
  806. macDest.appendTo(outp);
  807. macSource.appendTo(outp);
  808. outp.append((uint16_t)etherType);
  809. outp.append(frameData, frameLen);
  810. outp.compress();
  811. RR->sw->send(tPtr, outp, true);
  812. if (_config.remoteTraceTarget) {
  813. RR->t->networkFilter(
  814. tPtr,
  815. *this,
  816. rrl,
  817. (localCapabilityIndex >= 0) ? &crrl : (Trace::RuleResultLog*)0,
  818. (localCapabilityIndex >= 0) ? &(_config.capabilities[localCapabilityIndex]) : (Capability*)0,
  819. ztSource,
  820. ztDest,
  821. macSource,
  822. macDest,
  823. frameData,
  824. frameLen,
  825. etherType,
  826. vlanId,
  827. noTee,
  828. false,
  829. 0);
  830. }
  831. return false; // DROP locally, since we redirected
  832. }
  833. else {
  834. if (_config.remoteTraceTarget) {
  835. RR->t->networkFilter(
  836. tPtr,
  837. *this,
  838. rrl,
  839. (localCapabilityIndex >= 0) ? &crrl : (Trace::RuleResultLog*)0,
  840. (localCapabilityIndex >= 0) ? &(_config.capabilities[localCapabilityIndex]) : (Capability*)0,
  841. ztSource,
  842. ztDest,
  843. macSource,
  844. macDest,
  845. frameData,
  846. frameLen,
  847. etherType,
  848. vlanId,
  849. noTee,
  850. false,
  851. 1);
  852. }
  853. return true;
  854. }
  855. }
  856. else {
  857. _outgoing_packets_dropped++;
  858. if (_config.remoteTraceTarget) {
  859. RR->t->networkFilter(
  860. tPtr,
  861. *this,
  862. rrl,
  863. (localCapabilityIndex >= 0) ? &crrl : (Trace::RuleResultLog*)0,
  864. (localCapabilityIndex >= 0) ? &(_config.capabilities[localCapabilityIndex]) : (Capability*)0,
  865. ztSource,
  866. ztDest,
  867. macSource,
  868. macDest,
  869. frameData,
  870. frameLen,
  871. etherType,
  872. vlanId,
  873. noTee,
  874. false,
  875. 0);
  876. }
  877. return false;
  878. }
  879. }
  880. int Network::filterIncomingPacket(
  881. void* tPtr,
  882. const SharedPtr<Peer>& sourcePeer,
  883. const Address& ztDest,
  884. const MAC& macSource,
  885. const MAC& macDest,
  886. const uint8_t* frameData,
  887. const unsigned int frameLen,
  888. const unsigned int etherType,
  889. const unsigned int vlanId)
  890. {
  891. Address ztFinalDest(ztDest);
  892. Trace::RuleResultLog rrl, crrl;
  893. int accept = 0;
  894. Address cc;
  895. unsigned int ccLength = 0;
  896. bool ccWatch = false;
  897. const Capability* c = (Capability*)0;
  898. uint8_t qosBucket = 255; // For incoming packets this is a dummy value
  899. Mutex::Lock _l(_lock);
  900. Membership& membership = _membership(sourcePeer->address());
  901. switch (_doZtFilter(RR, rrl, _config, &membership, true, sourcePeer->address(), ztFinalDest, macSource, macDest, frameData, frameLen, etherType, vlanId, _config.rules, _config.ruleCount, cc, ccLength, ccWatch, qosBucket)) {
  902. case DOZTFILTER_NO_MATCH: {
  903. Membership::CapabilityIterator mci(membership, _config);
  904. while ((c = mci.next())) {
  905. ztFinalDest = ztDest; // sanity check, should be unmodified if there was no match
  906. Address cc2;
  907. unsigned int ccLength2 = 0;
  908. bool ccWatch2 = false;
  909. switch (_doZtFilter(RR, crrl, _config, &membership, true, sourcePeer->address(), ztFinalDest, macSource, macDest, frameData, frameLen, etherType, vlanId, c->rules(), c->ruleCount(), cc2, ccLength2, ccWatch2, qosBucket)) {
  910. case DOZTFILTER_NO_MATCH:
  911. case DOZTFILTER_DROP: // explicit DROP in a capability just terminates its evaluation and is an anti-pattern
  912. break;
  913. case DOZTFILTER_REDIRECT: // interpreted as ACCEPT but ztDest will have been changed in _doZtFilter()
  914. case DOZTFILTER_ACCEPT:
  915. accept = 1; // ACCEPT
  916. break;
  917. case DOZTFILTER_SUPER_ACCEPT:
  918. accept = 2; // super-ACCEPT
  919. break;
  920. }
  921. if (accept) {
  922. if (cc2) {
  923. Packet outp(cc2, RR->identity.address(), Packet::VERB_EXT_FRAME);
  924. outp.append(_id);
  925. outp.append((uint8_t)(ccWatch2 ? 0x1c : 0x08));
  926. macDest.appendTo(outp);
  927. macSource.appendTo(outp);
  928. outp.append((uint16_t)etherType);
  929. outp.append(frameData, ccLength2);
  930. outp.compress();
  931. RR->sw->send(tPtr, outp, true);
  932. }
  933. break;
  934. }
  935. }
  936. } break;
  937. case DOZTFILTER_DROP:
  938. if (_config.remoteTraceTarget) {
  939. RR->t->networkFilter(tPtr, *this, rrl, (Trace::RuleResultLog*)0, (Capability*)0, sourcePeer->address(), ztDest, macSource, macDest, frameData, frameLen, etherType, vlanId, false, true, 0);
  940. }
  941. return 0; // DROP
  942. case DOZTFILTER_REDIRECT: // interpreted as ACCEPT but ztFinalDest will have been changed in _doZtFilter()
  943. case DOZTFILTER_ACCEPT:
  944. accept = 1; // ACCEPT
  945. break;
  946. case DOZTFILTER_SUPER_ACCEPT:
  947. accept = 2; // super-ACCEPT
  948. break;
  949. }
  950. if (accept) {
  951. _incoming_packets_accepted++;
  952. if (cc) {
  953. Packet outp(cc, RR->identity.address(), Packet::VERB_EXT_FRAME);
  954. outp.append(_id);
  955. outp.append((uint8_t)(ccWatch ? 0x1c : 0x08));
  956. macDest.appendTo(outp);
  957. macSource.appendTo(outp);
  958. outp.append((uint16_t)etherType);
  959. outp.append(frameData, ccLength);
  960. outp.compress();
  961. RR->sw->send(tPtr, outp, true);
  962. }
  963. if ((ztDest != ztFinalDest) && (ztFinalDest)) {
  964. Packet outp(ztFinalDest, RR->identity.address(), Packet::VERB_EXT_FRAME);
  965. outp.append(_id);
  966. outp.append((uint8_t)0x0a);
  967. macDest.appendTo(outp);
  968. macSource.appendTo(outp);
  969. outp.append((uint16_t)etherType);
  970. outp.append(frameData, frameLen);
  971. outp.compress();
  972. RR->sw->send(tPtr, outp, true);
  973. if (_config.remoteTraceTarget) {
  974. RR->t->networkFilter(tPtr, *this, rrl, (c) ? &crrl : (Trace::RuleResultLog*)0, c, sourcePeer->address(), ztDest, macSource, macDest, frameData, frameLen, etherType, vlanId, false, true, 0);
  975. }
  976. return 0; // DROP locally, since we redirected
  977. }
  978. }
  979. else {
  980. _incoming_packets_dropped++;
  981. }
  982. if (_config.remoteTraceTarget) {
  983. RR->t->networkFilter(tPtr, *this, rrl, (c) ? &crrl : (Trace::RuleResultLog*)0, c, sourcePeer->address(), ztDest, macSource, macDest, frameData, frameLen, etherType, vlanId, false, true, accept);
  984. }
  985. return accept;
  986. }
  987. bool Network::subscribedToMulticastGroup(const MulticastGroup& mg, bool includeBridgedGroups) const
  988. {
  989. Mutex::Lock _l(_lock);
  990. if (std::binary_search(_myMulticastGroups.begin(), _myMulticastGroups.end(), mg)) {
  991. return true;
  992. }
  993. else if (includeBridgedGroups) {
  994. return _multicastGroupsBehindMe.contains(mg);
  995. }
  996. return false;
  997. }
  998. void Network::multicastSubscribe(void* tPtr, const MulticastGroup& mg)
  999. {
  1000. Mutex::Lock _l(_lock);
  1001. if (! std::binary_search(_myMulticastGroups.begin(), _myMulticastGroups.end(), mg)) {
  1002. _myMulticastGroups.insert(std::upper_bound(_myMulticastGroups.begin(), _myMulticastGroups.end(), mg), mg);
  1003. _sendUpdatesToMembers(tPtr, &mg);
  1004. _num_multicast_groups++;
  1005. }
  1006. }
  1007. void Network::multicastUnsubscribe(const MulticastGroup& mg)
  1008. {
  1009. Mutex::Lock _l(_lock);
  1010. std::vector<MulticastGroup>::iterator i(std::lower_bound(_myMulticastGroups.begin(), _myMulticastGroups.end(), mg));
  1011. if ((i != _myMulticastGroups.end()) && (*i == mg)) {
  1012. _myMulticastGroups.erase(i);
  1013. _num_multicast_groups--;
  1014. }
  1015. }
  1016. uint64_t Network::handleConfigChunk(void* tPtr, const uint64_t packetId, const Address& source, const Buffer<ZT_PROTO_MAX_PACKET_LENGTH>& chunk, unsigned int ptr)
  1017. {
  1018. if (_destroyed) {
  1019. return 0;
  1020. }
  1021. const unsigned int start = ptr;
  1022. ptr += 8; // skip network ID, which is already obviously known
  1023. const unsigned int chunkLen = chunk.at<uint16_t>(ptr);
  1024. ptr += 2;
  1025. const void* chunkData = chunk.field(ptr, chunkLen);
  1026. ptr += chunkLen;
  1027. NetworkConfig* nc = (NetworkConfig*)0;
  1028. uint64_t configUpdateId;
  1029. {
  1030. Mutex::Lock _l(_lock);
  1031. _IncomingConfigChunk* c = (_IncomingConfigChunk*)0;
  1032. uint64_t chunkId = 0;
  1033. unsigned long totalLength, chunkIndex;
  1034. if (ptr < chunk.size()) {
  1035. const bool fastPropagate = ((chunk[ptr++] & 0x01) != 0);
  1036. configUpdateId = chunk.at<uint64_t>(ptr);
  1037. ptr += 8;
  1038. totalLength = chunk.at<uint32_t>(ptr);
  1039. ptr += 4;
  1040. chunkIndex = chunk.at<uint32_t>(ptr);
  1041. ptr += 4;
  1042. if (((chunkIndex + chunkLen) > totalLength) || (totalLength >= ZT_NETWORKCONFIG_DICT_CAPACITY)) { // >= since we need room for a null at the end
  1043. return 0;
  1044. }
  1045. if ((chunk[ptr] != 1) || (chunk.at<uint16_t>(ptr + 1) != ZT_C25519_SIGNATURE_LEN)) {
  1046. return 0;
  1047. }
  1048. const uint8_t* sig = reinterpret_cast<const uint8_t*>(chunk.field(ptr + 3, ZT_C25519_SIGNATURE_LEN));
  1049. // We can use the signature, which is unique per chunk, to get a per-chunk ID for local deduplication use
  1050. for (unsigned int i = 0; i < 16; ++i) {
  1051. reinterpret_cast<uint8_t*>(&chunkId)[i & 7] ^= sig[i];
  1052. }
  1053. // Find existing or new slot for this update and check if this is a duplicate chunk
  1054. for (int i = 0; i < ZT_NETWORK_MAX_INCOMING_UPDATES; ++i) {
  1055. if (_incomingConfigChunks[i].updateId == configUpdateId) {
  1056. c = &(_incomingConfigChunks[i]);
  1057. for (unsigned long j = 0; j < c->haveChunks; ++j) {
  1058. if (c->haveChunkIds[j] == chunkId) {
  1059. return 0;
  1060. }
  1061. }
  1062. break;
  1063. }
  1064. else if ((! c) || (_incomingConfigChunks[i].ts < c->ts)) {
  1065. c = &(_incomingConfigChunks[i]);
  1066. }
  1067. }
  1068. // If it's not a duplicate, check chunk signature
  1069. const Identity controllerId(RR->topology->getIdentity(tPtr, controller()));
  1070. if (! controllerId) { // we should always have the controller identity by now, otherwise how would we have queried it the first time?
  1071. return 0;
  1072. }
  1073. if (! controllerId.verify(chunk.field(start, ptr - start), ptr - start, sig, ZT_C25519_SIGNATURE_LEN)) {
  1074. return 0;
  1075. }
  1076. // New properly verified chunks can be flooded "virally" through the network
  1077. if (fastPropagate) {
  1078. Address* a = (Address*)0;
  1079. Membership* m = (Membership*)0;
  1080. Hashtable<Address, Membership>::Iterator i(_memberships);
  1081. while (i.next(a, m)) {
  1082. if ((*a != source) && (*a != controller())) {
  1083. Packet outp(*a, RR->identity.address(), Packet::VERB_NETWORK_CONFIG);
  1084. outp.append(reinterpret_cast<const uint8_t*>(chunk.data()) + start, chunk.size() - start);
  1085. RR->sw->send(tPtr, outp, true);
  1086. }
  1087. }
  1088. }
  1089. }
  1090. else if ((source == controller()) || (! source)) { // since old chunks aren't signed, only accept from controller itself (or via cluster backplane)
  1091. // Legacy support for OK(NETWORK_CONFIG_REQUEST) from older controllers
  1092. chunkId = packetId;
  1093. configUpdateId = chunkId;
  1094. totalLength = chunkLen;
  1095. chunkIndex = 0;
  1096. if (totalLength >= ZT_NETWORKCONFIG_DICT_CAPACITY) {
  1097. return 0;
  1098. }
  1099. for (int i = 0; i < ZT_NETWORK_MAX_INCOMING_UPDATES; ++i) {
  1100. if ((! c) || (_incomingConfigChunks[i].ts < c->ts)) {
  1101. c = &(_incomingConfigChunks[i]);
  1102. }
  1103. }
  1104. }
  1105. else {
  1106. // Single-chunk unsigned legacy configs are only allowed from the controller itself
  1107. return 0;
  1108. }
  1109. ++c->ts; // newer is higher, that's all we need
  1110. if (c->updateId != configUpdateId) {
  1111. c->updateId = configUpdateId;
  1112. c->haveChunks = 0;
  1113. c->haveBytes = 0;
  1114. }
  1115. if (c->haveChunks >= ZT_NETWORK_MAX_UPDATE_CHUNKS) {
  1116. return false;
  1117. }
  1118. c->haveChunkIds[c->haveChunks++] = chunkId;
  1119. memcpy(c->data.unsafeData() + chunkIndex, chunkData, chunkLen);
  1120. c->haveBytes += chunkLen;
  1121. if (c->haveBytes == totalLength) {
  1122. c->data.unsafeData()[c->haveBytes] = (char)0; // ensure null terminated
  1123. nc = new NetworkConfig();
  1124. try {
  1125. if (! nc->fromDictionary(c->data)) {
  1126. delete nc;
  1127. nc = (NetworkConfig*)0;
  1128. }
  1129. }
  1130. catch (...) {
  1131. delete nc;
  1132. nc = (NetworkConfig*)0;
  1133. }
  1134. }
  1135. }
  1136. if (nc) {
  1137. this->setConfiguration(tPtr, *nc, true);
  1138. delete nc;
  1139. return configUpdateId;
  1140. }
  1141. else {
  1142. return 0;
  1143. }
  1144. return 0;
  1145. }
  1146. int Network::setConfiguration(void* tPtr, const NetworkConfig& nconf, bool saveToDisk)
  1147. {
  1148. if (_destroyed) {
  1149. return 0;
  1150. }
  1151. // _lock is NOT locked when this is called
  1152. try {
  1153. if ((nconf.issuedTo != RR->identity.address()) || (nconf.networkId != _id)) {
  1154. return 0; // invalid config that is not for us or not for this network
  1155. }
  1156. if (_config == nconf) {
  1157. return 1; // OK config, but duplicate of what we already have
  1158. }
  1159. ZT_VirtualNetworkConfig ctmp;
  1160. bool oldPortInitialized;
  1161. { // do things that require lock here, but unlock before calling callbacks
  1162. Mutex::Lock _l(_lock);
  1163. _config = nconf;
  1164. _lastConfigUpdate = RR->node->now();
  1165. _netconfFailure = NETCONF_FAILURE_NONE;
  1166. oldPortInitialized = _portInitialized;
  1167. _portInitialized = true;
  1168. _externalConfig(&ctmp);
  1169. }
  1170. _portError = RR->node->configureVirtualNetworkPort(tPtr, _id, &_uPtr, (oldPortInitialized) ? ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE : ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP, &ctmp);
  1171. _authenticationURL = nconf.authenticationURL;
  1172. if (saveToDisk) {
  1173. Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>* const d = new Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>();
  1174. try {
  1175. if (nconf.toDictionary(*d, false)) {
  1176. uint64_t tmp[2];
  1177. tmp[0] = _id;
  1178. tmp[1] = 0;
  1179. RR->node->stateObjectPut(tPtr, ZT_STATE_OBJECT_NETWORK_CONFIG, tmp, d->data(), d->sizeBytes());
  1180. }
  1181. }
  1182. catch (...) {
  1183. }
  1184. delete d;
  1185. }
  1186. return 2; // OK and configuration has changed
  1187. }
  1188. catch (...) {
  1189. } // ignore invalid configs
  1190. return 0;
  1191. }
  1192. void Network::requestConfiguration(void* tPtr)
  1193. {
  1194. if (_destroyed) {
  1195. return;
  1196. }
  1197. if ((_id >> 56) == 0xff) {
  1198. if ((_id & 0xffffff) == 0) {
  1199. const uint16_t startPortRange = (uint16_t)((_id >> 40) & 0xffff);
  1200. const uint16_t endPortRange = (uint16_t)((_id >> 24) & 0xffff);
  1201. if (endPortRange >= startPortRange) {
  1202. NetworkConfig* const nconf = new NetworkConfig();
  1203. nconf->networkId = _id;
  1204. nconf->timestamp = RR->node->now();
  1205. nconf->credentialTimeMaxDelta = ZT_NETWORKCONFIG_DEFAULT_CREDENTIAL_TIME_MAX_MAX_DELTA;
  1206. nconf->revision = 1;
  1207. nconf->issuedTo = RR->identity.address();
  1208. nconf->flags = ZT_NETWORKCONFIG_FLAG_ENABLE_IPV6_NDP_EMULATION;
  1209. nconf->mtu = ZT_DEFAULT_MTU;
  1210. nconf->multicastLimit = 0;
  1211. nconf->staticIpCount = 1;
  1212. nconf->ruleCount = 14;
  1213. nconf->staticIps[0] = InetAddress::makeIpv66plane(_id, RR->identity.address().toInt());
  1214. // Drop everything but IPv6
  1215. nconf->rules[0].t = (uint8_t)ZT_NETWORK_RULE_MATCH_ETHERTYPE | 0x80; // NOT
  1216. nconf->rules[0].v.etherType = 0x86dd; // IPv6
  1217. nconf->rules[1].t = (uint8_t)ZT_NETWORK_RULE_ACTION_DROP;
  1218. // Allow ICMPv6
  1219. nconf->rules[2].t = (uint8_t)ZT_NETWORK_RULE_MATCH_IP_PROTOCOL;
  1220. nconf->rules[2].v.ipProtocol = 0x3a; // ICMPv6
  1221. nconf->rules[3].t = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  1222. // Allow destination ports within range
  1223. nconf->rules[4].t = (uint8_t)ZT_NETWORK_RULE_MATCH_IP_PROTOCOL;
  1224. nconf->rules[4].v.ipProtocol = 0x11; // UDP
  1225. nconf->rules[5].t = (uint8_t)ZT_NETWORK_RULE_MATCH_IP_PROTOCOL | 0x40; // OR
  1226. nconf->rules[5].v.ipProtocol = 0x06; // TCP
  1227. nconf->rules[6].t = (uint8_t)ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE;
  1228. nconf->rules[6].v.port[0] = startPortRange;
  1229. nconf->rules[6].v.port[1] = endPortRange;
  1230. nconf->rules[7].t = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  1231. // Allow non-SYN TCP packets to permit non-connection-initiating traffic
  1232. nconf->rules[8].t = (uint8_t)ZT_NETWORK_RULE_MATCH_CHARACTERISTICS | 0x80; // NOT
  1233. nconf->rules[8].v.characteristics = ZT_RULE_PACKET_CHARACTERISTICS_TCP_SYN;
  1234. nconf->rules[9].t = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  1235. // Also allow SYN+ACK which are replies to SYN
  1236. nconf->rules[10].t = (uint8_t)ZT_NETWORK_RULE_MATCH_CHARACTERISTICS;
  1237. nconf->rules[10].v.characteristics = ZT_RULE_PACKET_CHARACTERISTICS_TCP_SYN;
  1238. nconf->rules[11].t = (uint8_t)ZT_NETWORK_RULE_MATCH_CHARACTERISTICS;
  1239. nconf->rules[11].v.characteristics = ZT_RULE_PACKET_CHARACTERISTICS_TCP_ACK;
  1240. nconf->rules[12].t = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  1241. nconf->rules[13].t = (uint8_t)ZT_NETWORK_RULE_ACTION_DROP;
  1242. nconf->type = ZT_NETWORK_TYPE_PUBLIC;
  1243. nconf->name[0] = 'a';
  1244. nconf->name[1] = 'd';
  1245. nconf->name[2] = 'h';
  1246. nconf->name[3] = 'o';
  1247. nconf->name[4] = 'c';
  1248. nconf->name[5] = '-';
  1249. Utils::hex((uint16_t)startPortRange, nconf->name + 6);
  1250. nconf->name[10] = '-';
  1251. Utils::hex((uint16_t)endPortRange, nconf->name + 11);
  1252. nconf->name[15] = (char)0;
  1253. this->setConfiguration(tPtr, *nconf, false);
  1254. delete nconf;
  1255. }
  1256. else {
  1257. this->setNotFound(tPtr);
  1258. }
  1259. }
  1260. else if ((_id & 0xff) == 0x01) {
  1261. // ffAAaaaaaaaaaa01 -- where AA is the IPv4 /8 to use and aaaaaaaaaa is the anchor node for multicast gather and replication
  1262. const uint64_t myAddress = RR->identity.address().toInt();
  1263. const uint64_t networkHub = (_id >> 8) & 0xffffffffffULL;
  1264. uint8_t ipv4[4];
  1265. ipv4[0] = (uint8_t)((_id >> 48) & 0xff);
  1266. ipv4[1] = (uint8_t)((myAddress >> 16) & 0xff);
  1267. ipv4[2] = (uint8_t)((myAddress >> 8) & 0xff);
  1268. ipv4[3] = (uint8_t)(myAddress & 0xff);
  1269. char v4ascii[24];
  1270. Utils::decimal(ipv4[0], v4ascii);
  1271. NetworkConfig* const nconf = new NetworkConfig();
  1272. nconf->networkId = _id;
  1273. nconf->timestamp = RR->node->now();
  1274. nconf->credentialTimeMaxDelta = ZT_NETWORKCONFIG_DEFAULT_CREDENTIAL_TIME_MAX_MAX_DELTA;
  1275. nconf->revision = 1;
  1276. nconf->issuedTo = RR->identity.address();
  1277. nconf->flags = ZT_NETWORKCONFIG_FLAG_ENABLE_IPV6_NDP_EMULATION;
  1278. nconf->mtu = ZT_DEFAULT_MTU;
  1279. nconf->multicastLimit = 1024;
  1280. nconf->specialistCount = (networkHub == 0) ? 0 : 1;
  1281. nconf->staticIpCount = 2;
  1282. nconf->ruleCount = 1;
  1283. if (networkHub != 0) {
  1284. nconf->specialists[0] = networkHub;
  1285. }
  1286. nconf->staticIps[0] = InetAddress::makeIpv66plane(_id, myAddress);
  1287. nconf->staticIps[1].set(ipv4, 4, 8);
  1288. nconf->rules[0].t = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  1289. nconf->type = ZT_NETWORK_TYPE_PUBLIC;
  1290. nconf->name[0] = 'a';
  1291. nconf->name[1] = 'd';
  1292. nconf->name[2] = 'h';
  1293. nconf->name[3] = 'o';
  1294. nconf->name[4] = 'c';
  1295. nconf->name[5] = '-';
  1296. unsigned long nn = 6;
  1297. while ((nconf->name[nn] = v4ascii[nn - 6])) {
  1298. ++nn;
  1299. }
  1300. nconf->name[nn++] = '.';
  1301. nconf->name[nn++] = '0';
  1302. nconf->name[nn++] = '.';
  1303. nconf->name[nn++] = '0';
  1304. nconf->name[nn++] = '.';
  1305. nconf->name[nn++] = '0';
  1306. nconf->name[nn++] = (char)0;
  1307. this->setConfiguration(tPtr, *nconf, false);
  1308. delete nconf;
  1309. }
  1310. return;
  1311. }
  1312. const Address ctrl(controller());
  1313. Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> rmd;
  1314. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_VERSION, (uint64_t)ZT_NETWORKCONFIG_VERSION);
  1315. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_VENDOR, (uint64_t)ZT_VENDOR_ZEROTIER);
  1316. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_PROTOCOL_VERSION, (uint64_t)ZT_PROTO_VERSION);
  1317. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION, (uint64_t)ZEROTIER_ONE_VERSION_MAJOR);
  1318. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION, (uint64_t)ZEROTIER_ONE_VERSION_MINOR);
  1319. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION, (uint64_t)ZEROTIER_ONE_VERSION_REVISION);
  1320. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_NETWORK_RULES, (uint64_t)ZT_MAX_NETWORK_RULES);
  1321. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_NETWORK_CAPABILITIES, (uint64_t)ZT_MAX_NETWORK_CAPABILITIES);
  1322. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_CAPABILITY_RULES, (uint64_t)ZT_MAX_CAPABILITY_RULES);
  1323. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_NETWORK_TAGS, (uint64_t)ZT_MAX_NETWORK_TAGS);
  1324. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_FLAGS, (uint64_t)0);
  1325. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_RULES_ENGINE_REV, (uint64_t)ZT_RULES_ENGINE_REVISION);
  1326. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_OS_ARCH, ZT_TARGET_NAME);
  1327. RR->t->networkConfigRequestSent(tPtr, *this, ctrl);
  1328. if (ctrl == RR->identity.address()) {
  1329. if (RR->localNetworkController) {
  1330. RR->localNetworkController->request(_id, InetAddress(), 0xffffffffffffffffULL, RR->identity, rmd);
  1331. }
  1332. else {
  1333. this->setNotFound(tPtr);
  1334. }
  1335. return;
  1336. }
  1337. Packet outp(ctrl, RR->identity.address(), Packet::VERB_NETWORK_CONFIG_REQUEST);
  1338. outp.append((uint64_t)_id);
  1339. const unsigned int rmdSize = rmd.sizeBytes();
  1340. outp.append((uint16_t)rmdSize);
  1341. outp.append((const void*)rmd.data(), rmdSize);
  1342. if (_config) {
  1343. outp.append((uint64_t)_config.revision);
  1344. outp.append((uint64_t)_config.timestamp);
  1345. }
  1346. else {
  1347. outp.append((unsigned char)0, 16);
  1348. }
  1349. outp.compress();
  1350. RR->node->expectReplyTo(outp.packetId());
  1351. RR->sw->send(tPtr, outp, true);
  1352. }
  1353. bool Network::gate(void* tPtr, const SharedPtr<Peer>& peer)
  1354. {
  1355. const int64_t now = RR->node->now();
  1356. // int64_t comTimestamp = 0;
  1357. // int64_t comRevocationThreshold = 0;
  1358. Mutex::Lock _l(_lock);
  1359. try {
  1360. if (_config) {
  1361. Membership* m = _memberships.get(peer->address());
  1362. // if (m) {
  1363. // comTimestamp = m->comTimestamp();
  1364. // comRevocationThreshold = m->comRevocationThreshold();
  1365. // }
  1366. if ((_config.isPublic()) || ((m) && (m->isAllowedOnNetwork(_config, peer->identity())))) {
  1367. if (! m) {
  1368. m = &(_membership(peer->address()));
  1369. }
  1370. if (m->multicastLikeGate(now)) {
  1371. _announceMulticastGroupsTo(tPtr, peer->address(), _allMulticastGroups());
  1372. }
  1373. return true;
  1374. }
  1375. }
  1376. }
  1377. catch (...) {
  1378. }
  1379. // printf("%.16llx %.10llx not allowed, COM ts %lld revocation %lld\n", _id, peer->address().toInt(), comTimestamp, comRevocationThreshold); fflush(stdout);
  1380. return false;
  1381. }
  1382. bool Network::recentlyAssociatedWith(const Address& addr)
  1383. {
  1384. Mutex::Lock _l(_lock);
  1385. const Membership* m = _memberships.get(addr);
  1386. return ((m) && (m->recentlyAssociated(RR->node->now())));
  1387. }
  1388. void Network::clean()
  1389. {
  1390. const int64_t now = RR->node->now();
  1391. Mutex::Lock _l(_lock);
  1392. if (_destroyed) {
  1393. return;
  1394. }
  1395. {
  1396. Hashtable<MulticastGroup, uint64_t>::Iterator i(_multicastGroupsBehindMe);
  1397. MulticastGroup* mg = (MulticastGroup*)0;
  1398. uint64_t* ts = (uint64_t*)0;
  1399. while (i.next(mg, ts)) {
  1400. if ((now - *ts) > (ZT_MULTICAST_LIKE_EXPIRE * 2)) {
  1401. _multicastGroupsBehindMe.erase(*mg);
  1402. }
  1403. }
  1404. }
  1405. {
  1406. Address* a = (Address*)0;
  1407. Membership* m = (Membership*)0;
  1408. Hashtable<Address, Membership>::Iterator i(_memberships);
  1409. while (i.next(a, m)) {
  1410. if (! RR->topology->getPeerNoCache(*a)) {
  1411. _memberships.erase(*a);
  1412. }
  1413. else {
  1414. m->clean(now, _config);
  1415. }
  1416. }
  1417. }
  1418. }
  1419. void Network::learnBridgeRoute(const MAC& mac, const Address& addr)
  1420. {
  1421. Mutex::Lock _l(_lock);
  1422. _remoteBridgeRoutes[mac] = addr;
  1423. // Anti-DOS circuit breaker to prevent nodes from spamming us with absurd numbers of bridge routes
  1424. while (_remoteBridgeRoutes.size() > ZT_MAX_BRIDGE_ROUTES) {
  1425. Hashtable<Address, unsigned long> counts;
  1426. Address maxAddr;
  1427. unsigned long maxCount = 0;
  1428. MAC* m = (MAC*)0;
  1429. Address* a = (Address*)0;
  1430. // Find the address responsible for the most entries
  1431. {
  1432. Hashtable<MAC, Address>::Iterator i(_remoteBridgeRoutes);
  1433. while (i.next(m, a)) {
  1434. const unsigned long c = ++counts[*a];
  1435. if (c > maxCount) {
  1436. maxCount = c;
  1437. maxAddr = *a;
  1438. }
  1439. }
  1440. }
  1441. // Kill this address from our table, since it's most likely spamming us
  1442. {
  1443. Hashtable<MAC, Address>::Iterator i(_remoteBridgeRoutes);
  1444. while (i.next(m, a)) {
  1445. if (*a == maxAddr) {
  1446. _remoteBridgeRoutes.erase(*m);
  1447. }
  1448. }
  1449. }
  1450. }
  1451. }
  1452. void Network::learnBridgedMulticastGroup(void* tPtr, const MulticastGroup& mg, int64_t now)
  1453. {
  1454. Mutex::Lock _l(_lock);
  1455. const unsigned long tmp = (unsigned long)_multicastGroupsBehindMe.size();
  1456. _multicastGroupsBehindMe.set(mg, now);
  1457. if (tmp != _multicastGroupsBehindMe.size()) {
  1458. _sendUpdatesToMembers(tPtr, &mg);
  1459. }
  1460. }
  1461. Membership::AddCredentialResult Network::addCredential(void* tPtr, const CertificateOfMembership& com)
  1462. {
  1463. if (com.networkId() != _id) {
  1464. return Membership::ADD_REJECTED;
  1465. }
  1466. Mutex::Lock _l(_lock);
  1467. return _membership(com.issuedTo()).addCredential(RR, tPtr, _config, com);
  1468. }
  1469. Membership::AddCredentialResult Network::addCredential(void* tPtr, const Address& sentFrom, const Revocation& rev)
  1470. {
  1471. if (rev.networkId() != _id) {
  1472. return Membership::ADD_REJECTED;
  1473. }
  1474. Mutex::Lock _l(_lock);
  1475. Membership& m = _membership(rev.target());
  1476. const Membership::AddCredentialResult result = m.addCredential(RR, tPtr, _config, rev);
  1477. if ((result == Membership::ADD_ACCEPTED_NEW) && (rev.fastPropagate())) {
  1478. Address* a = (Address*)0;
  1479. Membership* m = (Membership*)0;
  1480. Hashtable<Address, Membership>::Iterator i(_memberships);
  1481. while (i.next(a, m)) {
  1482. if ((*a != sentFrom) && (*a != rev.signer())) {
  1483. Packet outp(*a, RR->identity.address(), Packet::VERB_NETWORK_CREDENTIALS);
  1484. outp.append((uint8_t)0x00); // no COM
  1485. outp.append((uint16_t)0); // no capabilities
  1486. outp.append((uint16_t)0); // no tags
  1487. outp.append((uint16_t)1); // one revocation!
  1488. rev.serialize(outp);
  1489. outp.append((uint16_t)0); // no certificates of ownership
  1490. RR->sw->send(tPtr, outp, true);
  1491. }
  1492. }
  1493. }
  1494. return result;
  1495. }
  1496. void Network::destroy()
  1497. {
  1498. Mutex::Lock _l(_lock);
  1499. _destroyed = true;
  1500. }
  1501. ZT_VirtualNetworkStatus Network::_status() const
  1502. {
  1503. // assumes _lock is locked
  1504. if (_portError) {
  1505. return ZT_NETWORK_STATUS_PORT_ERROR;
  1506. }
  1507. switch (_netconfFailure) {
  1508. case NETCONF_FAILURE_ACCESS_DENIED:
  1509. return ZT_NETWORK_STATUS_ACCESS_DENIED;
  1510. case NETCONF_FAILURE_NOT_FOUND:
  1511. return ZT_NETWORK_STATUS_NOT_FOUND;
  1512. case NETCONF_FAILURE_NONE:
  1513. return ((_config) ? ZT_NETWORK_STATUS_OK : ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION);
  1514. case NETCONF_FAILURE_AUTHENTICATION_REQUIRED:
  1515. return ZT_NETWORK_STATUS_AUTHENTICATION_REQUIRED;
  1516. default:
  1517. return ZT_NETWORK_STATUS_PORT_ERROR;
  1518. }
  1519. }
  1520. void Network::_externalConfig(ZT_VirtualNetworkConfig* ec) const
  1521. {
  1522. // assumes _lock is locked
  1523. ec->nwid = _id;
  1524. ec->mac = _mac.toInt();
  1525. if (_config) {
  1526. Utils::scopy(ec->name, sizeof(ec->name), _config.name);
  1527. }
  1528. else {
  1529. ec->name[0] = (char)0;
  1530. }
  1531. ec->status = _status();
  1532. ec->type = (_config) ? (_config.isPrivate() ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC) : ZT_NETWORK_TYPE_PRIVATE;
  1533. ec->mtu = (_config) ? _config.mtu : ZT_DEFAULT_MTU;
  1534. ec->dhcp = 0;
  1535. std::vector<Address> ab(_config.activeBridges());
  1536. ec->bridge = (std::find(ab.begin(), ab.end(), RR->identity.address()) != ab.end()) ? 1 : 0;
  1537. ec->broadcastEnabled = (_config) ? (_config.enableBroadcast() ? 1 : 0) : 0;
  1538. ec->portError = _portError;
  1539. ec->netconfRevision = (_config) ? (unsigned long)_config.revision : 0;
  1540. ec->assignedAddressCount = 0;
  1541. for (unsigned int i = 0; i < ZT_MAX_ZT_ASSIGNED_ADDRESSES; ++i) {
  1542. if (i < _config.staticIpCount) {
  1543. memcpy(&(ec->assignedAddresses[i]), &(_config.staticIps[i]), sizeof(struct sockaddr_storage));
  1544. ++ec->assignedAddressCount;
  1545. }
  1546. else {
  1547. memset(&(ec->assignedAddresses[i]), 0, sizeof(struct sockaddr_storage));
  1548. }
  1549. }
  1550. ec->routeCount = 0;
  1551. for (unsigned int i = 0; i < ZT_MAX_NETWORK_ROUTES; ++i) {
  1552. if (i < _config.routeCount) {
  1553. memcpy(&(ec->routes[i]), &(_config.routes[i]), sizeof(ZT_VirtualNetworkRoute));
  1554. ++ec->routeCount;
  1555. }
  1556. else {
  1557. memset(&(ec->routes[i]), 0, sizeof(ZT_VirtualNetworkRoute));
  1558. }
  1559. }
  1560. ec->multicastSubscriptionCount = (unsigned int)_myMulticastGroups.size();
  1561. for (unsigned long i = 0; i < (unsigned long)_myMulticastGroups.size(); ++i) {
  1562. ec->multicastSubscriptions[i].mac = _myMulticastGroups[i].mac().toInt();
  1563. ec->multicastSubscriptions[i].adi = _myMulticastGroups[i].adi();
  1564. }
  1565. memcpy(&ec->dns, &_config.dns, sizeof(ZT_VirtualNetworkDNS));
  1566. Utils::scopy(ec->authenticationURL, sizeof(ec->authenticationURL), _authenticationURL.c_str());
  1567. ec->ssoVersion = _config.ssoVersion;
  1568. ec->authenticationExpiryTime = _config.authenticationExpiryTime;
  1569. ec->ssoEnabled = _config.ssoEnabled;
  1570. Utils::scopy(ec->centralAuthURL, sizeof(ec->centralAuthURL), _config.centralAuthURL);
  1571. Utils::scopy(ec->issuerURL, sizeof(ec->issuerURL), _config.issuerURL);
  1572. Utils::scopy(ec->ssoNonce, sizeof(ec->ssoNonce), _config.ssoNonce);
  1573. Utils::scopy(ec->ssoState, sizeof(ec->ssoState), _config.ssoState);
  1574. Utils::scopy(ec->ssoClientID, sizeof(ec->ssoClientID), _config.ssoClientID);
  1575. Utils::scopy(ec->ssoProvider, sizeof(ec->ssoProvider), _config.ssoProvider);
  1576. }
  1577. void Network::_sendUpdatesToMembers(void* tPtr, const MulticastGroup* const newMulticastGroup)
  1578. {
  1579. // Assumes _lock is locked
  1580. const int64_t now = RR->node->now();
  1581. std::vector<MulticastGroup> groups;
  1582. if (newMulticastGroup) {
  1583. groups.push_back(*newMulticastGroup);
  1584. }
  1585. else {
  1586. groups = _allMulticastGroups();
  1587. }
  1588. std::vector<Address> alwaysAnnounceTo;
  1589. if ((newMulticastGroup) || ((now - _lastAnnouncedMulticastGroupsUpstream) >= ZT_MULTICAST_ANNOUNCE_PERIOD)) {
  1590. if (! newMulticastGroup) {
  1591. _lastAnnouncedMulticastGroupsUpstream = now;
  1592. }
  1593. alwaysAnnounceTo = _config.alwaysContactAddresses();
  1594. if (std::find(alwaysAnnounceTo.begin(), alwaysAnnounceTo.end(), controller()) == alwaysAnnounceTo.end()) {
  1595. alwaysAnnounceTo.push_back(controller());
  1596. }
  1597. const std::vector<Address> upstreams(RR->topology->upstreamAddresses());
  1598. for (std::vector<Address>::const_iterator a(upstreams.begin()); a != upstreams.end(); ++a) {
  1599. if (std::find(alwaysAnnounceTo.begin(), alwaysAnnounceTo.end(), *a) == alwaysAnnounceTo.end()) {
  1600. alwaysAnnounceTo.push_back(*a);
  1601. }
  1602. }
  1603. std::sort(alwaysAnnounceTo.begin(), alwaysAnnounceTo.end());
  1604. for (std::vector<Address>::const_iterator a(alwaysAnnounceTo.begin()); a != alwaysAnnounceTo.end(); ++a) {
  1605. /*
  1606. // push COM to non-members so they can do multicast request auth
  1607. if ( (_config.com) && (!_memberships.contains(*a)) && (*a != RR->identity.address()) ) {
  1608. Packet outp(*a,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  1609. _config.com.serialize(outp);
  1610. outp.append((uint8_t)0x00);
  1611. outp.append((uint16_t)0); // no capabilities
  1612. outp.append((uint16_t)0); // no tags
  1613. outp.append((uint16_t)0); // no revocations
  1614. outp.append((uint16_t)0); // no certificates of ownership
  1615. RR->sw->send(tPtr,outp,true);
  1616. }
  1617. */
  1618. _announceMulticastGroupsTo(tPtr, *a, groups);
  1619. }
  1620. }
  1621. {
  1622. Address* a = (Address*)0;
  1623. Membership* m = (Membership*)0;
  1624. Hashtable<Address, Membership>::Iterator i(_memberships);
  1625. while (i.next(a, m)) {
  1626. const Identity remoteIdentity(RR->topology->getIdentity(tPtr, *a));
  1627. if (remoteIdentity) {
  1628. if ((m->multicastLikeGate(now) || (newMulticastGroup)) && (m->isAllowedOnNetwork(_config, remoteIdentity)) && (! std::binary_search(alwaysAnnounceTo.begin(), alwaysAnnounceTo.end(), *a))) {
  1629. _announceMulticastGroupsTo(tPtr, *a, groups);
  1630. }
  1631. }
  1632. }
  1633. }
  1634. }
  1635. void Network::_announceMulticastGroupsTo(void* tPtr, const Address& peer, const std::vector<MulticastGroup>& allMulticastGroups)
  1636. {
  1637. // Assumes _lock is locked
  1638. Packet* const outp = new Packet(peer, RR->identity.address(), Packet::VERB_MULTICAST_LIKE);
  1639. for (std::vector<MulticastGroup>::const_iterator mg(allMulticastGroups.begin()); mg != allMulticastGroups.end(); ++mg) {
  1640. if ((outp->size() + 24) >= ZT_PROTO_MAX_PACKET_LENGTH) {
  1641. outp->compress();
  1642. RR->sw->send(tPtr, *outp, true);
  1643. outp->reset(peer, RR->identity.address(), Packet::VERB_MULTICAST_LIKE);
  1644. }
  1645. // network ID, MAC, ADI
  1646. outp->append((uint64_t)_id);
  1647. mg->mac().appendTo(*outp);
  1648. outp->append((uint32_t)mg->adi());
  1649. }
  1650. if (outp->size() > ZT_PROTO_MIN_PACKET_LENGTH) {
  1651. outp->compress();
  1652. RR->sw->send(tPtr, *outp, true);
  1653. }
  1654. delete outp;
  1655. }
  1656. std::vector<MulticastGroup> Network::_allMulticastGroups() const
  1657. {
  1658. // Assumes _lock is locked
  1659. std::vector<MulticastGroup> mgs;
  1660. mgs.reserve(_myMulticastGroups.size() + _multicastGroupsBehindMe.size() + 1);
  1661. mgs.insert(mgs.end(), _myMulticastGroups.begin(), _myMulticastGroups.end());
  1662. _multicastGroupsBehindMe.appendKeys(mgs);
  1663. if ((_config) && (_config.enableBroadcast())) {
  1664. mgs.push_back(Network::BROADCAST);
  1665. }
  1666. std::sort(mgs.begin(), mgs.end());
  1667. mgs.erase(std::unique(mgs.begin(), mgs.end()), mgs.end());
  1668. return mgs;
  1669. }
  1670. Membership& Network::_membership(const Address& a)
  1671. {
  1672. // assumes _lock is locked
  1673. return _memberships[a];
  1674. }
  1675. void Network::setAuthenticationRequired(void* tPtr, const char* issuerURL, const char* centralEndpoint, const char* clientID, const char* ssoProvider, const char* nonce, const char* state)
  1676. {
  1677. Mutex::Lock _l(_lock);
  1678. _netconfFailure = NETCONF_FAILURE_AUTHENTICATION_REQUIRED;
  1679. _config.ssoEnabled = true;
  1680. _config.ssoVersion = 1;
  1681. Utils::scopy(_config.issuerURL, sizeof(_config.issuerURL), issuerURL);
  1682. Utils::scopy(_config.centralAuthURL, sizeof(_config.centralAuthURL), centralEndpoint);
  1683. Utils::scopy(_config.ssoClientID, sizeof(_config.ssoClientID), clientID);
  1684. Utils::scopy(_config.ssoNonce, sizeof(_config.ssoNonce), nonce);
  1685. Utils::scopy(_config.ssoState, sizeof(_config.ssoState), state);
  1686. Utils::scopy(_config.ssoProvider, sizeof(_config.ssoProvider), ssoProvider);
  1687. _sendUpdateEvent(tPtr);
  1688. }
  1689. void Network::_sendUpdateEvent(void* tPtr)
  1690. {
  1691. ZT_VirtualNetworkConfig ctmp;
  1692. _externalConfig(&ctmp);
  1693. RR->node->configureVirtualNetworkPort(tPtr, _id, &_uPtr, (_portInitialized) ? ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE : ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP, &ctmp);
  1694. }
  1695. } // namespace ZeroTier