subnet.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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[1024];
  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(sscanf(str, "%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !str[consumed]) {
  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. for(int i = 0; i < 4; i++) {
  207. if(x[i] > 255) {
  208. return false;
  209. }
  210. subnet->net.ipv4.address.x[i] = x[i];
  211. }
  212. return true;
  213. }
  214. /* IPv6 */
  215. char *last_colon = strrchr(str, ':');
  216. if(last_colon && sscanf(last_colon, ":%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !last_colon[consumed]) {
  217. /* Dotted quad suffix notation, convert to standard IPv6 notation */
  218. for(int i = 0; i < 4; i++)
  219. if(x[i] > 255) {
  220. return false;
  221. }
  222. snprintf(last_colon, sizeof(str) - (last_colon - str), ":%02x%02x:%02x%02x", x[0], x[1], x[2], x[3]);
  223. }
  224. char *double_colon = strstr(str, "::");
  225. if(double_colon) {
  226. /* Figure out how many zero groups we need to expand */
  227. int zero_group_count = 8;
  228. for(const char *cur = str; *cur; cur++)
  229. if(*cur != ':') {
  230. zero_group_count--;
  231. while(cur[1] && cur[1] != ':') {
  232. cur++;
  233. }
  234. }
  235. if(zero_group_count < 1) {
  236. return false;
  237. }
  238. /* Split the double colon in the middle to make room for zero groups */
  239. double_colon++;
  240. memmove(double_colon + (zero_group_count * 2 - 1), double_colon, strlen(double_colon) + 1);
  241. /* Write zero groups in the resulting gap, overwriting the second colon */
  242. for(int i = 0; i < zero_group_count; i++) {
  243. memcpy(&double_colon[i * 2], "0:", 2);
  244. }
  245. /* Remove any leading or trailing colons */
  246. if(str[0] == ':') {
  247. memmove(&str[0], &str[1], strlen(&str[1]) + 1);
  248. }
  249. if(str[strlen(str) - 1] == ':') {
  250. str[strlen(str) - 1] = 0;
  251. }
  252. }
  253. if(sscanf(str, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx%n",
  254. &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &consumed) >= 8 && !str[consumed]) {
  255. if(prefixlength == -1) {
  256. prefixlength = 128;
  257. }
  258. if(prefixlength > 128) {
  259. return false;
  260. }
  261. subnet->type = SUBNET_IPV6;
  262. subnet->net.ipv6.prefixlength = prefixlength;
  263. subnet->weight = weight;
  264. for(int i = 0; i < 8; i++) {
  265. subnet->net.ipv6.address.x[i] = htons(x[i]);
  266. }
  267. return true;
  268. }
  269. return false;
  270. }
  271. bool net2str(char *netstr, int len, const subnet_t *subnet) {
  272. if(!netstr || !subnet) {
  273. logger(LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", (void *)netstr, (void *)subnet);
  274. return false;
  275. }
  276. switch(subnet->type) {
  277. case SUBNET_MAC:
  278. snprintf(netstr, len, "%x:%x:%x:%x:%x:%x#%d",
  279. subnet->net.mac.address.x[0],
  280. subnet->net.mac.address.x[1],
  281. subnet->net.mac.address.x[2],
  282. subnet->net.mac.address.x[3],
  283. subnet->net.mac.address.x[4],
  284. subnet->net.mac.address.x[5],
  285. subnet->weight);
  286. break;
  287. case SUBNET_IPV4:
  288. snprintf(netstr, len, "%u.%u.%u.%u/%d#%d",
  289. subnet->net.ipv4.address.x[0],
  290. subnet->net.ipv4.address.x[1],
  291. subnet->net.ipv4.address.x[2],
  292. subnet->net.ipv4.address.x[3],
  293. subnet->net.ipv4.prefixlength,
  294. subnet->weight);
  295. break;
  296. case SUBNET_IPV6:
  297. snprintf(netstr, len, "%x:%x:%x:%x:%x:%x:%x:%x/%d#%d",
  298. ntohs(subnet->net.ipv6.address.x[0]),
  299. ntohs(subnet->net.ipv6.address.x[1]),
  300. ntohs(subnet->net.ipv6.address.x[2]),
  301. ntohs(subnet->net.ipv6.address.x[3]),
  302. ntohs(subnet->net.ipv6.address.x[4]),
  303. ntohs(subnet->net.ipv6.address.x[5]),
  304. ntohs(subnet->net.ipv6.address.x[6]),
  305. ntohs(subnet->net.ipv6.address.x[7]),
  306. subnet->net.ipv6.prefixlength,
  307. subnet->weight);
  308. break;
  309. default:
  310. logger(LOG_ERR,
  311. "net2str() was called with unknown subnet type %d, exiting!",
  312. subnet->type);
  313. exit(0);
  314. }
  315. return true;
  316. }
  317. /* Subnet lookup routines */
  318. subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet) {
  319. return avl_search(owner->subnet_tree, subnet);
  320. }
  321. subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
  322. subnet_t *p, *r = NULL;
  323. avl_node_t *n;
  324. int i;
  325. // Check if this address is cached
  326. for(i = 0; i < 2; i++) {
  327. if(!cache_mac_valid[i]) {
  328. continue;
  329. }
  330. if(owner && cache_mac_subnet[i] && cache_mac_subnet[i]->owner != owner) {
  331. continue;
  332. }
  333. if(!memcmp(address, &cache_mac_address[i], sizeof(*address))) {
  334. return cache_mac_subnet[i];
  335. }
  336. }
  337. // Search all subnets for a matching one
  338. for(n = owner ? owner->subnet_tree->head : subnet_tree->head; n; n = n->next) {
  339. p = n->data;
  340. if(!p || p->type != SUBNET_MAC) {
  341. continue;
  342. }
  343. if(!memcmp(address, &p->net.mac.address, sizeof(*address))) {
  344. r = p;
  345. if(p->owner->status.reachable) {
  346. break;
  347. }
  348. }
  349. }
  350. // Cache the result
  351. cache_mac_slot = !cache_mac_slot;
  352. memcpy(&cache_mac_address[cache_mac_slot], address, sizeof(*address));
  353. cache_mac_subnet[cache_mac_slot] = r;
  354. cache_mac_valid[cache_mac_slot] = true;
  355. return r;
  356. }
  357. subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
  358. subnet_t *p, *r = NULL;
  359. avl_node_t *n;
  360. int i;
  361. // Check if this address is cached
  362. for(i = 0; i < 2; i++) {
  363. if(!cache_ipv4_valid[i]) {
  364. continue;
  365. }
  366. if(!memcmp(address, &cache_ipv4_address[i], sizeof(*address))) {
  367. return cache_ipv4_subnet[i];
  368. }
  369. }
  370. // Search all subnets for a matching one
  371. for(n = subnet_tree->head; n; n = n->next) {
  372. p = n->data;
  373. if(!p || p->type != SUBNET_IPV4) {
  374. continue;
  375. }
  376. if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
  377. r = p;
  378. if(p->owner->status.reachable) {
  379. break;
  380. }
  381. }
  382. }
  383. // Cache the result
  384. cache_ipv4_slot = !cache_ipv4_slot;
  385. memcpy(&cache_ipv4_address[cache_ipv4_slot], address, sizeof(*address));
  386. cache_ipv4_subnet[cache_ipv4_slot] = r;
  387. cache_ipv4_valid[cache_ipv4_slot] = true;
  388. return r;
  389. }
  390. subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
  391. subnet_t *p, *r = NULL;
  392. avl_node_t *n;
  393. int i;
  394. // Check if this address is cached
  395. for(i = 0; i < 2; i++) {
  396. if(!cache_ipv6_valid[i]) {
  397. continue;
  398. }
  399. if(!memcmp(address, &cache_ipv6_address[i], sizeof(*address))) {
  400. return cache_ipv6_subnet[i];
  401. }
  402. }
  403. // Search all subnets for a matching one
  404. for(n = subnet_tree->head; n; n = n->next) {
  405. p = n->data;
  406. if(!p || p->type != SUBNET_IPV6) {
  407. continue;
  408. }
  409. if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
  410. r = p;
  411. if(p->owner->status.reachable) {
  412. break;
  413. }
  414. }
  415. }
  416. // Cache the result
  417. cache_ipv6_slot = !cache_ipv6_slot;
  418. memcpy(&cache_ipv6_address[cache_ipv6_slot], address, sizeof(*address));
  419. cache_ipv6_subnet[cache_ipv6_slot] = r;
  420. cache_ipv6_valid[cache_ipv6_slot] = true;
  421. return r;
  422. }
  423. void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
  424. avl_node_t *node;
  425. int i;
  426. char *envp[10] = {NULL};
  427. char netstr[MAXNETSTR];
  428. char *name, *address, *port;
  429. char empty[] = "";
  430. // Prepare environment variables to be passed to the script
  431. xasprintf(&envp[0], "NETNAME=%s", netname ? netname : "");
  432. xasprintf(&envp[1], "DEVICE=%s", device ? device : "");
  433. xasprintf(&envp[2], "INTERFACE=%s", iface ? iface : "");
  434. xasprintf(&envp[3], "NODE=%s", owner->name);
  435. xasprintf(&envp[4], "NAME=%s", myself->name);
  436. if(owner != myself) {
  437. sockaddr2str(&owner->address, &address, &port);
  438. // 5 and 6 are reserved for SUBNET and WEIGHT
  439. xasprintf(&envp[7], "REMOTEADDRESS=%s", address);
  440. xasprintf(&envp[8], "REMOTEPORT=%s", port);
  441. free(port);
  442. free(address);
  443. }
  444. name = up ? "subnet-up" : "subnet-down";
  445. if(!subnet) {
  446. for(node = owner->subnet_tree->head; node; node = node->next) {
  447. subnet = node->data;
  448. if(!net2str(netstr, sizeof(netstr), subnet)) {
  449. continue;
  450. }
  451. // Strip the weight from the subnet, and put it in its own environment variable
  452. char *weight = strchr(netstr, '#');
  453. if(weight) {
  454. *weight++ = 0;
  455. } else {
  456. weight = empty;
  457. }
  458. // Prepare the SUBNET and WEIGHT variables
  459. free(envp[5]);
  460. free(envp[6]);
  461. xasprintf(&envp[5], "SUBNET=%s", netstr);
  462. xasprintf(&envp[6], "WEIGHT=%s", weight);
  463. execute_script(name, envp);
  464. }
  465. } else {
  466. if(net2str(netstr, sizeof(netstr), subnet)) {
  467. // Strip the weight from the subnet, and put it in its own environment variable
  468. char *weight = strchr(netstr, '#');
  469. if(weight) {
  470. *weight++ = 0;
  471. } else {
  472. weight = empty;
  473. }
  474. // Prepare the SUBNET and WEIGHT variables
  475. xasprintf(&envp[5], "SUBNET=%s", netstr);
  476. xasprintf(&envp[6], "WEIGHT=%s", weight);
  477. execute_script(name, envp);
  478. }
  479. }
  480. for(i = 0; i < 9; i++) {
  481. free(envp[i]);
  482. }
  483. }
  484. void dump_subnets(void) {
  485. char netstr[MAXNETSTR];
  486. subnet_t *subnet;
  487. avl_node_t *node;
  488. logger(LOG_DEBUG, "Subnet list:");
  489. for(node = subnet_tree->head; node; node = node->next) {
  490. subnet = node->data;
  491. if(!net2str(netstr, sizeof(netstr), subnet)) {
  492. continue;
  493. }
  494. logger(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
  495. }
  496. logger(LOG_DEBUG, "End of subnet list.");
  497. }