2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000-2019 Guus Sliepen <guus@tinc-vpn.org>,
4 2000-2005 Ivo Timmermans
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 /* lists type of subnet */
36 avl_tree_t *subnet_tree;
38 /* Subnet lookup cache */
40 static ipv4_t cache_ipv4_address[2];
41 static subnet_t *cache_ipv4_subnet[2];
42 static bool cache_ipv4_valid[2];
43 static int cache_ipv4_slot;
45 static ipv6_t cache_ipv6_address[2];
46 static subnet_t *cache_ipv6_subnet[2];
47 static bool cache_ipv6_valid[2];
48 static int cache_ipv6_slot;
50 static mac_t cache_mac_address[2];
51 static subnet_t *cache_mac_subnet[2];
52 static bool cache_mac_valid[2];
53 static int cache_mac_slot;
55 void subnet_cache_flush(void) {
56 cache_ipv4_valid[0] = cache_ipv4_valid[1] = false;
57 cache_ipv6_valid[0] = cache_ipv6_valid[1] = false;
58 cache_mac_valid[0] = cache_mac_valid[1] = false;
61 /* Subnet comparison */
63 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
66 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
72 result = a->weight - b->weight;
74 if(result || !a->owner || !b->owner) {
78 return strcmp(a->owner->name, b->owner->name);
81 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
84 result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
90 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
96 result = a->weight - b->weight;
98 if(result || !a->owner || !b->owner) {
102 return strcmp(a->owner->name, b->owner->name);
105 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
108 result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
114 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
120 result = a->weight - b->weight;
122 if(result || !a->owner || !b->owner) {
126 return strcmp(a->owner->name, b->owner->name);
129 int subnet_compare(const subnet_t *a, const subnet_t *b) {
132 result = a->type - b->type;
140 return subnet_compare_mac(a, b);
143 return subnet_compare_ipv4(a, b);
146 return subnet_compare_ipv6(a, b);
149 logger(LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!",
157 /* Initialising trees */
159 void init_subnets(void) {
160 subnet_tree = avl_alloc_tree((avl_compare_t) subnet_compare, (avl_action_t) free_subnet);
162 subnet_cache_flush();
165 void exit_subnets(void) {
166 avl_delete_tree(subnet_tree);
169 avl_tree_t *new_subnet_tree(void) {
170 return avl_alloc_tree((avl_compare_t) subnet_compare, NULL);
173 void free_subnet_tree(avl_tree_t *subnet_tree) {
174 avl_delete_tree(subnet_tree);
177 /* Allocating and freeing space for subnets */
179 subnet_t *new_subnet(void) {
180 return xmalloc_and_zero(sizeof(subnet_t));
183 void free_subnet(subnet_t *subnet) {
187 /* Adding and removing subnets */
189 void subnet_add(node_t *n, subnet_t *subnet) {
192 avl_insert(subnet_tree, subnet);
193 avl_insert(n->subnet_tree, subnet);
195 subnet_cache_flush();
198 void subnet_del(node_t *n, subnet_t *subnet) {
199 avl_delete(n->subnet_tree, subnet);
200 avl_delete(subnet_tree, subnet);
202 subnet_cache_flush();
205 /* Ascii representation of subnets */
207 bool str2net(subnet_t *subnet, const char *subnetstr) {
209 strncpy(str, subnetstr, sizeof(str));
210 str[sizeof(str) - 1] = 0;
214 char *weight_separator = strchr(str, '#');
216 if(weight_separator) {
217 char *weight_str = weight_separator + 1;
219 if(sscanf(weight_str, "%d%n", &weight, &consumed) < 1) {
223 if(weight_str[consumed]) {
227 *weight_separator = 0;
230 int prefixlength = -1;
231 char *prefixlength_separator = strchr(str, '/');
233 if(prefixlength_separator) {
234 char *prefixlength_str = prefixlength_separator + 1;
236 if(sscanf(prefixlength_str, "%d%n", &prefixlength, &consumed) < 1) {
240 if(prefixlength_str[consumed]) {
244 *prefixlength_separator = 0;
246 if(prefixlength < 0) {
253 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]) {
255 Normally we should check that each part has two digits to prevent ambiguities.
256 However, in old tinc versions net2str() will aggressively return MAC addresses with one-digit parts,
257 so we have to accept them otherwise we would be unable to parse ADD_SUBNET messages.
259 if(prefixlength >= 0) {
263 subnet->type = SUBNET_MAC;
264 subnet->weight = weight;
266 for(int i = 0; i < 6; i++) {
267 subnet->net.mac.address.x[i] = x[i];
273 if(inet_pton(AF_INET, str, &subnet->net.ipv4.address)) {
274 if(prefixlength == -1) {
278 if(prefixlength > 32) {
282 subnet->type = SUBNET_IPV4;
283 subnet->net.ipv4.prefixlength = prefixlength;
284 subnet->weight = weight;
289 if(inet_pton(AF_INET6, str, &subnet->net.ipv6.address)) {
290 if(prefixlength == -1) {
294 if(prefixlength > 128) {
298 subnet->type = SUBNET_IPV6;
299 subnet->net.ipv6.prefixlength = prefixlength;
300 subnet->weight = weight;
308 bool net2str(char *netstr, int len, const subnet_t *subnet) {
309 if(!netstr || !subnet) {
310 logger(LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", (void *)netstr, (void *)subnet);
315 int prefixlength = -1;
317 switch(subnet->type) {
319 snprintf(netstr, len, "%02x:%02x:%02x:%02x:%02x:%02x",
320 subnet->net.mac.address.x[0],
321 subnet->net.mac.address.x[1],
322 subnet->net.mac.address.x[2],
323 subnet->net.mac.address.x[3],
324 subnet->net.mac.address.x[4],
325 subnet->net.mac.address.x[5]);
329 inet_ntop(AF_INET, &subnet->net.ipv4.address, netstr, len);
330 prefixlength = subnet->net.ipv4.prefixlength;
332 if(prefixlength == 32) {
339 inet_ntop(AF_INET6, &subnet->net.ipv6.address, netstr, len);
340 prefixlength = subnet->net.ipv6.prefixlength;
342 if(prefixlength == 128) {
350 logger(LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);
354 size_t used = strlen(netstr);
358 if(prefixlength >= 0) {
359 result = snprintf(netstr, len, "/%d", prefixlength);
364 if(subnet->weight != 10) {
365 snprintf(netstr, len, "#%d", subnet->weight);
371 /* Subnet lookup routines */
373 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet) {
374 return avl_search(owner->subnet_tree, subnet);
377 subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
378 subnet_t *p, *r = NULL;
382 // Check if this address is cached
384 for(i = 0; i < 2; i++) {
385 if(!cache_mac_valid[i]) {
389 if(owner && cache_mac_subnet[i] && cache_mac_subnet[i]->owner != owner) {
393 if(!memcmp(address, &cache_mac_address[i], sizeof(*address))) {
394 return cache_mac_subnet[i];
398 // Search all subnets for a matching one
400 for(n = owner ? owner->subnet_tree->head : subnet_tree->head; n; n = n->next) {
403 if(!p || p->type != SUBNET_MAC) {
407 if(!memcmp(address, &p->net.mac.address, sizeof(*address))) {
410 if(p->owner->status.reachable) {
418 cache_mac_slot = !cache_mac_slot;
419 memcpy(&cache_mac_address[cache_mac_slot], address, sizeof(*address));
420 cache_mac_subnet[cache_mac_slot] = r;
421 cache_mac_valid[cache_mac_slot] = true;
426 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
427 subnet_t *p, *r = NULL;
431 // Check if this address is cached
433 for(i = 0; i < 2; i++) {
434 if(!cache_ipv4_valid[i]) {
438 if(!memcmp(address, &cache_ipv4_address[i], sizeof(*address))) {
439 return cache_ipv4_subnet[i];
443 // Search all subnets for a matching one
445 for(n = subnet_tree->head; n; n = n->next) {
448 if(!p || p->type != SUBNET_IPV4) {
452 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
455 if(p->owner->status.reachable) {
463 cache_ipv4_slot = !cache_ipv4_slot;
464 memcpy(&cache_ipv4_address[cache_ipv4_slot], address, sizeof(*address));
465 cache_ipv4_subnet[cache_ipv4_slot] = r;
466 cache_ipv4_valid[cache_ipv4_slot] = true;
471 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
472 subnet_t *p, *r = NULL;
476 // Check if this address is cached
478 for(i = 0; i < 2; i++) {
479 if(!cache_ipv6_valid[i]) {
483 if(!memcmp(address, &cache_ipv6_address[i], sizeof(*address))) {
484 return cache_ipv6_subnet[i];
488 // Search all subnets for a matching one
490 for(n = subnet_tree->head; n; n = n->next) {
493 if(!p || p->type != SUBNET_IPV6) {
497 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
500 if(p->owner->status.reachable) {
508 cache_ipv6_slot = !cache_ipv6_slot;
509 memcpy(&cache_ipv6_address[cache_ipv6_slot], address, sizeof(*address));
510 cache_ipv6_subnet[cache_ipv6_slot] = r;
511 cache_ipv6_valid[cache_ipv6_slot] = true;
516 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
519 char *envp[10] = {NULL};
520 char netstr[MAXNETSTR];
521 char *name, *address, *port;
524 // Prepare environment variables to be passed to the script
526 xasprintf(&envp[0], "NETNAME=%s", netname ? netname : "");
527 xasprintf(&envp[1], "DEVICE=%s", device ? device : "");
528 xasprintf(&envp[2], "INTERFACE=%s", iface ? iface : "");
529 xasprintf(&envp[3], "NODE=%s", owner->name);
530 xasprintf(&envp[4], "NAME=%s", myself->name);
532 if(owner != myself) {
533 sockaddr2str(&owner->address, &address, &port);
534 // 5 and 6 are reserved for SUBNET and WEIGHT
535 xasprintf(&envp[7], "REMOTEADDRESS=%s", address);
536 xasprintf(&envp[8], "REMOTEPORT=%s", port);
541 name = up ? "subnet-up" : "subnet-down";
544 for(node = owner->subnet_tree->head; node; node = node->next) {
547 if(!net2str(netstr, sizeof(netstr), subnet)) {
551 // Strip the weight from the subnet, and put it in its own environment variable
552 char *weight = strchr(netstr, '#');
560 // Prepare the SUBNET and WEIGHT variables
564 xasprintf(&envp[5], "SUBNET=%s", netstr);
565 xasprintf(&envp[6], "WEIGHT=%s", weight);
567 execute_script(name, envp);
570 if(net2str(netstr, sizeof(netstr), subnet)) {
571 // Strip the weight from the subnet, and put it in its own environment variable
572 char *weight = strchr(netstr, '#');
580 // Prepare the SUBNET and WEIGHT variables
581 xasprintf(&envp[5], "SUBNET=%s", netstr);
582 xasprintf(&envp[6], "WEIGHT=%s", weight);
584 execute_script(name, envp);
588 for(i = 0; i < 9; i++) {
593 void dump_subnets(void) {
594 char netstr[MAXNETSTR];
598 logger(LOG_DEBUG, "Subnet list:");
600 for(node = subnet_tree->head; node; node = node->next) {
603 if(!net2str(netstr, sizeof(netstr), subnet)) {
607 logger(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
610 logger(LOG_DEBUG, "End of subnet list.");