subnet.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. subnet.c -- handle subnet lookups and lists
  3. Copyright (C) 2000-2019 Guus Sliepen <guus@tinc-vpn.org>,
  4. 2000-2005 Ivo Timmermans
  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 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "system.h"
  18. #include "avl_tree.h"
  19. #include "device.h"
  20. #include "logger.h"
  21. #include "net.h"
  22. #include "netutl.h"
  23. #include "node.h"
  24. #include "process.h"
  25. #include "subnet.h"
  26. #include "utils.h"
  27. #include "xalloc.h"
  28. /* lists type of subnet */
  29. avl_tree_t *subnet_tree;
  30. /* Subnet lookup cache */
  31. static ipv4_t cache_ipv4_address[2];
  32. static subnet_t *cache_ipv4_subnet[2];
  33. static bool cache_ipv4_valid[2];
  34. static int cache_ipv4_slot;
  35. static ipv6_t cache_ipv6_address[2];
  36. static subnet_t *cache_ipv6_subnet[2];
  37. static bool cache_ipv6_valid[2];
  38. static int cache_ipv6_slot;
  39. static mac_t cache_mac_address[2];
  40. static subnet_t *cache_mac_subnet[2];
  41. static bool cache_mac_valid[2];
  42. static int cache_mac_slot;
  43. void subnet_cache_flush(void) {
  44. cache_ipv4_valid[0] = cache_ipv4_valid[1] = false;
  45. cache_ipv6_valid[0] = cache_ipv6_valid[1] = false;
  46. cache_mac_valid[0] = cache_mac_valid[1] = false;
  47. }
  48. /* Subnet comparison */
  49. static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
  50. int result;
  51. result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
  52. if(result) {
  53. return result;
  54. }
  55. result = a->weight - b->weight;
  56. if(result || !a->owner || !b->owner) {
  57. return result;
  58. }
  59. return strcmp(a->owner->name, b->owner->name);
  60. }
  61. static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
  62. int result;
  63. result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
  64. if(result) {
  65. return result;
  66. }
  67. result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
  68. if(result) {
  69. return result;
  70. }
  71. result = a->weight - b->weight;
  72. if(result || !a->owner || !b->owner) {
  73. return result;
  74. }
  75. return strcmp(a->owner->name, b->owner->name);
  76. }
  77. static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
  78. int result;
  79. result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
  80. if(result) {
  81. return result;
  82. }
  83. result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
  84. if(result) {
  85. return result;
  86. }
  87. result = a->weight - b->weight;
  88. if(result || !a->owner || !b->owner) {
  89. return result;
  90. }
  91. return strcmp(a->owner->name, b->owner->name);
  92. }
  93. int subnet_compare(const subnet_t *a, const subnet_t *b) {
  94. int result;
  95. result = a->type - b->type;
  96. if(result) {
  97. return result;
  98. }
  99. switch(a->type) {
  100. case SUBNET_MAC:
  101. return subnet_compare_mac(a, b);
  102. case SUBNET_IPV4:
  103. return subnet_compare_ipv4(a, b);
  104. case SUBNET_IPV6:
  105. return subnet_compare_ipv6(a, b);
  106. default:
  107. logger(LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!",
  108. a->type);
  109. exit(0);
  110. }
  111. return 0;
  112. }
  113. /* Initialising trees */
  114. void init_subnets(void) {
  115. subnet_tree = avl_alloc_tree((avl_compare_t) subnet_compare, (avl_action_t) free_subnet);
  116. subnet_cache_flush();
  117. }
  118. void exit_subnets(void) {
  119. avl_delete_tree(subnet_tree);
  120. }
  121. avl_tree_t *new_subnet_tree(void) {
  122. return avl_alloc_tree((avl_compare_t) subnet_compare, NULL);
  123. }
  124. void free_subnet_tree(avl_tree_t *subnet_tree) {
  125. avl_delete_tree(subnet_tree);
  126. }
  127. /* Allocating and freeing space for subnets */
  128. subnet_t *new_subnet(void) {
  129. return xmalloc_and_zero(sizeof(subnet_t));
  130. }
  131. void free_subnet(subnet_t *subnet) {
  132. free(subnet);
  133. }
  134. /* Adding and removing subnets */
  135. void subnet_add(node_t *n, subnet_t *subnet) {
  136. subnet->owner = n;
  137. avl_insert(subnet_tree, subnet);
  138. avl_insert(n->subnet_tree, subnet);
  139. subnet_cache_flush();
  140. }
  141. void subnet_del(node_t *n, subnet_t *subnet) {
  142. avl_delete(n->subnet_tree, subnet);
  143. avl_delete(subnet_tree, subnet);
  144. subnet_cache_flush();
  145. }
  146. /* Ascii representation of subnets */
  147. bool str2net(subnet_t *subnet, const char *subnetstr) {
  148. char str[64];
  149. strncpy(str, subnetstr, sizeof(str));
  150. str[sizeof(str) - 1] = 0;
  151. int consumed;
  152. int weight = 10;
  153. char *weight_separator = strchr(str, '#');
  154. if(weight_separator) {
  155. char *weight_str = weight_separator + 1;
  156. if(sscanf(weight_str, "%d%n", &weight, &consumed) < 1) {
  157. return false;
  158. }
  159. if(weight_str[consumed]) {
  160. return false;
  161. }
  162. *weight_separator = 0;
  163. }
  164. int prefixlength = -1;
  165. char *prefixlength_separator = strchr(str, '/');
  166. if(prefixlength_separator) {
  167. char *prefixlength_str = prefixlength_separator + 1;
  168. if(sscanf(prefixlength_str, "%d%n", &prefixlength, &consumed) < 1) {
  169. return false;
  170. }
  171. if(prefixlength_str[consumed]) {
  172. return false;
  173. }
  174. *prefixlength_separator = 0;
  175. if(prefixlength < 0) {
  176. return false;
  177. }
  178. }
  179. uint16_t x[8];
  180. if(sscanf(str, "%hx:%hx:%hx:%hx:%hx:%hx%n", &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &consumed) >= 6 && !str[consumed]) {
  181. /*
  182. Normally we should check that each part has two digits to prevent ambiguities.
  183. However, in old tinc versions net2str() will aggressively return MAC addresses with one-digit parts,
  184. so we have to accept them otherwise we would be unable to parse ADD_SUBNET messages.
  185. */
  186. if(prefixlength >= 0) {
  187. return false;
  188. }
  189. subnet->type = SUBNET_MAC;
  190. subnet->weight = weight;
  191. for(int i = 0; i < 6; i++) {
  192. subnet->net.mac.address.x[i] = x[i];
  193. }
  194. return true;
  195. }
  196. if(inet_pton(AF_INET, str, &subnet->net.ipv4.address)) {
  197. if(prefixlength == -1) {
  198. prefixlength = 32;
  199. }
  200. if(prefixlength > 32) {
  201. return false;
  202. }
  203. subnet->type = SUBNET_IPV4;
  204. subnet->net.ipv4.prefixlength = prefixlength;
  205. subnet->weight = weight;
  206. return true;
  207. }
  208. if(inet_pton(AF_INET6, str, &subnet->net.ipv6.address)) {
  209. if(prefixlength == -1) {
  210. prefixlength = 128;
  211. }
  212. if(prefixlength > 128) {
  213. return false;
  214. }
  215. subnet->type = SUBNET_IPV6;
  216. subnet->net.ipv6.prefixlength = prefixlength;
  217. subnet->weight = weight;
  218. return true;
  219. }
  220. return false;
  221. }
  222. bool net2str(char *netstr, int len, const subnet_t *subnet) {
  223. if(!netstr || !subnet) {
  224. logger(LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", (void *)netstr, (void *)subnet);
  225. return false;
  226. }
  227. int result;
  228. int prefixlength = -1;
  229. switch(subnet->type) {
  230. case SUBNET_MAC:
  231. snprintf(netstr, len, "%02x:%02x:%02x:%02x:%02x:%02x",
  232. subnet->net.mac.address.x[0],
  233. subnet->net.mac.address.x[1],
  234. subnet->net.mac.address.x[2],
  235. subnet->net.mac.address.x[3],
  236. subnet->net.mac.address.x[4],
  237. subnet->net.mac.address.x[5]);
  238. break;
  239. case SUBNET_IPV4:
  240. inet_ntop(AF_INET, &subnet->net.ipv4.address, netstr, len);
  241. prefixlength = subnet->net.ipv4.prefixlength;
  242. if(prefixlength == 32) {
  243. prefixlength = -1;
  244. }
  245. break;
  246. case SUBNET_IPV6: {
  247. inet_ntop(AF_INET6, &subnet->net.ipv6.address, netstr, len);
  248. prefixlength = subnet->net.ipv6.prefixlength;
  249. if(prefixlength == 128) {
  250. prefixlength = -1;
  251. }
  252. break;
  253. }
  254. default:
  255. logger(LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);
  256. exit(1);
  257. }
  258. size_t used = strlen(netstr);
  259. netstr += used;
  260. len -= used;
  261. if(prefixlength >= 0) {
  262. result = snprintf(netstr, len, "/%d", prefixlength);
  263. netstr += result;
  264. len -= result;
  265. }
  266. if(subnet->weight != 10) {
  267. snprintf(netstr, len, "#%d", subnet->weight);
  268. }
  269. return true;
  270. }
  271. /* Subnet lookup routines */
  272. subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet) {
  273. return avl_search(owner->subnet_tree, subnet);
  274. }
  275. subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
  276. subnet_t *p, *r = NULL;
  277. avl_node_t *n;
  278. int i;
  279. // Check if this address is cached
  280. for(i = 0; i < 2; i++) {
  281. if(!cache_mac_valid[i]) {
  282. continue;
  283. }
  284. if(owner && cache_mac_subnet[i] && cache_mac_subnet[i]->owner != owner) {
  285. continue;
  286. }
  287. if(!memcmp(address, &cache_mac_address[i], sizeof(*address))) {
  288. return cache_mac_subnet[i];
  289. }
  290. }
  291. // Search all subnets for a matching one
  292. for(n = owner ? owner->subnet_tree->head : subnet_tree->head; n; n = n->next) {
  293. p = n->data;
  294. if(!p || p->type != SUBNET_MAC) {
  295. continue;
  296. }
  297. if(!memcmp(address, &p->net.mac.address, sizeof(*address))) {
  298. r = p;
  299. if(p->owner->status.reachable) {
  300. break;
  301. }
  302. }
  303. }
  304. // Cache the result
  305. cache_mac_slot = !cache_mac_slot;
  306. memcpy(&cache_mac_address[cache_mac_slot], address, sizeof(*address));
  307. cache_mac_subnet[cache_mac_slot] = r;
  308. cache_mac_valid[cache_mac_slot] = true;
  309. return r;
  310. }
  311. subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
  312. subnet_t *p, *r = NULL;
  313. avl_node_t *n;
  314. int i;
  315. // Check if this address is cached
  316. for(i = 0; i < 2; i++) {
  317. if(!cache_ipv4_valid[i]) {
  318. continue;
  319. }
  320. if(!memcmp(address, &cache_ipv4_address[i], sizeof(*address))) {
  321. return cache_ipv4_subnet[i];
  322. }
  323. }
  324. // Search all subnets for a matching one
  325. for(n = subnet_tree->head; n; n = n->next) {
  326. p = n->data;
  327. if(!p || p->type != SUBNET_IPV4) {
  328. continue;
  329. }
  330. if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
  331. r = p;
  332. if(p->owner->status.reachable) {
  333. break;
  334. }
  335. }
  336. }
  337. // Cache the result
  338. cache_ipv4_slot = !cache_ipv4_slot;
  339. memcpy(&cache_ipv4_address[cache_ipv4_slot], address, sizeof(*address));
  340. cache_ipv4_subnet[cache_ipv4_slot] = r;
  341. cache_ipv4_valid[cache_ipv4_slot] = true;
  342. return r;
  343. }
  344. subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
  345. subnet_t *p, *r = NULL;
  346. avl_node_t *n;
  347. int i;
  348. // Check if this address is cached
  349. for(i = 0; i < 2; i++) {
  350. if(!cache_ipv6_valid[i]) {
  351. continue;
  352. }
  353. if(!memcmp(address, &cache_ipv6_address[i], sizeof(*address))) {
  354. return cache_ipv6_subnet[i];
  355. }
  356. }
  357. // Search all subnets for a matching one
  358. for(n = subnet_tree->head; n; n = n->next) {
  359. p = n->data;
  360. if(!p || p->type != SUBNET_IPV6) {
  361. continue;
  362. }
  363. if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
  364. r = p;
  365. if(p->owner->status.reachable) {
  366. break;
  367. }
  368. }
  369. }
  370. // Cache the result
  371. cache_ipv6_slot = !cache_ipv6_slot;
  372. memcpy(&cache_ipv6_address[cache_ipv6_slot], address, sizeof(*address));
  373. cache_ipv6_subnet[cache_ipv6_slot] = r;
  374. cache_ipv6_valid[cache_ipv6_slot] = true;
  375. return r;
  376. }
  377. void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
  378. avl_node_t *node;
  379. int i;
  380. char *envp[10] = {NULL};
  381. char netstr[MAXNETSTR];
  382. char *name, *address, *port;
  383. char empty[] = "";
  384. // Prepare environment variables to be passed to the script
  385. xasprintf(&envp[0], "NETNAME=%s", netname ? netname : "");
  386. xasprintf(&envp[1], "DEVICE=%s", device ? device : "");
  387. xasprintf(&envp[2], "INTERFACE=%s", iface ? iface : "");
  388. xasprintf(&envp[3], "NODE=%s", owner->name);
  389. xasprintf(&envp[4], "NAME=%s", myself->name);
  390. if(owner != myself) {
  391. sockaddr2str(&owner->address, &address, &port);
  392. // 5 and 6 are reserved for SUBNET and WEIGHT
  393. xasprintf(&envp[7], "REMOTEADDRESS=%s", address);
  394. xasprintf(&envp[8], "REMOTEPORT=%s", port);
  395. free(port);
  396. free(address);
  397. }
  398. name = up ? "subnet-up" : "subnet-down";
  399. if(!subnet) {
  400. for(node = owner->subnet_tree->head; node; node = node->next) {
  401. subnet = node->data;
  402. if(!net2str(netstr, sizeof(netstr), subnet)) {
  403. continue;
  404. }
  405. // Strip the weight from the subnet, and put it in its own environment variable
  406. char *weight = strchr(netstr, '#');
  407. if(weight) {
  408. *weight++ = 0;
  409. } else {
  410. weight = empty;
  411. }
  412. // Prepare the SUBNET and WEIGHT variables
  413. free(envp[5]);
  414. free(envp[6]);
  415. xasprintf(&envp[5], "SUBNET=%s", netstr);
  416. xasprintf(&envp[6], "WEIGHT=%s", weight);
  417. execute_script(name, envp);
  418. }
  419. } else {
  420. if(net2str(netstr, sizeof(netstr), subnet)) {
  421. // Strip the weight from the subnet, and put it in its own environment variable
  422. char *weight = strchr(netstr, '#');
  423. if(weight) {
  424. *weight++ = 0;
  425. } else {
  426. weight = empty;
  427. }
  428. // Prepare the SUBNET and WEIGHT variables
  429. xasprintf(&envp[5], "SUBNET=%s", netstr);
  430. xasprintf(&envp[6], "WEIGHT=%s", weight);
  431. execute_script(name, envp);
  432. }
  433. }
  434. for(i = 0; i < 9; i++) {
  435. free(envp[i]);
  436. }
  437. }
  438. void dump_subnets(void) {
  439. char netstr[MAXNETSTR];
  440. subnet_t *subnet;
  441. avl_node_t *node;
  442. logger(LOG_DEBUG, "Subnet list:");
  443. for(node = subnet_tree->head; node; node = node->next) {
  444. subnet = node->data;
  445. if(!net2str(netstr, sizeof(netstr), subnet)) {
  446. continue;
  447. }
  448. logger(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
  449. }
  450. logger(LOG_DEBUG, "End of subnet list.");
  451. }