2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000-2014 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(sscanf(str, "%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !str[consumed]) {
274 if(prefixlength == -1) {
278 if(prefixlength > 32) {
282 subnet->type = SUBNET_IPV4;
283 subnet->net.ipv4.prefixlength = prefixlength;
284 subnet->weight = weight;
286 for(int i = 0; i < 4; i++) {
291 subnet->net.ipv4.address.x[i] = x[i];
299 char *last_colon = strrchr(str, ':');
301 if(last_colon && sscanf(last_colon, ":%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !last_colon[consumed]) {
302 /* Dotted quad suffix notation, convert to standard IPv6 notation */
303 for(int i = 0; i < 4; i++)
308 snprintf(last_colon, sizeof(str) - (last_colon - str), ":%02x%02x:%02x%02x", x[0], x[1], x[2], x[3]);
311 char *double_colon = strstr(str, "::");
314 /* Figure out how many zero groups we need to expand */
315 int zero_group_count = 8;
317 for(const char *cur = str; *cur; cur++)
321 while(cur[1] && cur[1] != ':') {
326 if(zero_group_count < 1) {
330 /* Split the double colon in the middle to make room for zero groups */
332 memmove(double_colon + (zero_group_count * 2 - 1), double_colon, strlen(double_colon) + 1);
334 /* Write zero groups in the resulting gap, overwriting the second colon */
335 for(int i = 0; i < zero_group_count; i++) {
336 memcpy(&double_colon[i * 2], "0:", 2);
339 /* Remove any leading or trailing colons */
341 memmove(&str[0], &str[1], strlen(&str[1]) + 1);
344 if(str[strlen(str) - 1] == ':') {
345 str[strlen(str) - 1] = 0;
349 if(sscanf(str, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx%n",
350 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &consumed) >= 8 && !str[consumed]) {
351 if(prefixlength == -1) {
355 if(prefixlength > 128) {
359 subnet->type = SUBNET_IPV6;
360 subnet->net.ipv6.prefixlength = prefixlength;
361 subnet->weight = weight;
363 for(int i = 0; i < 8; i++) {
364 subnet->net.ipv6.address.x[i] = htons(x[i]);
373 bool net2str(char *netstr, int len, const subnet_t *subnet) {
374 if(!netstr || !subnet) {
375 logger(LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", (void *)netstr, (void *)subnet);
379 switch(subnet->type) {
381 snprintf(netstr, len, "%x:%x:%x:%x:%x:%x#%d",
382 subnet->net.mac.address.x[0],
383 subnet->net.mac.address.x[1],
384 subnet->net.mac.address.x[2],
385 subnet->net.mac.address.x[3],
386 subnet->net.mac.address.x[4],
387 subnet->net.mac.address.x[5],
392 snprintf(netstr, len, "%u.%u.%u.%u/%d#%d",
393 subnet->net.ipv4.address.x[0],
394 subnet->net.ipv4.address.x[1],
395 subnet->net.ipv4.address.x[2],
396 subnet->net.ipv4.address.x[3],
397 subnet->net.ipv4.prefixlength,
402 snprintf(netstr, len, "%x:%x:%x:%x:%x:%x:%x:%x/%d#%d",
403 ntohs(subnet->net.ipv6.address.x[0]),
404 ntohs(subnet->net.ipv6.address.x[1]),
405 ntohs(subnet->net.ipv6.address.x[2]),
406 ntohs(subnet->net.ipv6.address.x[3]),
407 ntohs(subnet->net.ipv6.address.x[4]),
408 ntohs(subnet->net.ipv6.address.x[5]),
409 ntohs(subnet->net.ipv6.address.x[6]),
410 ntohs(subnet->net.ipv6.address.x[7]),
411 subnet->net.ipv6.prefixlength,
417 "net2str() was called with unknown subnet type %d, exiting!",
425 /* Subnet lookup routines */
427 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet) {
428 return avl_search(owner->subnet_tree, subnet);
431 subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
432 subnet_t *p, *r = NULL;
436 // Check if this address is cached
438 for(i = 0; i < 2; i++) {
439 if(!cache_mac_valid[i]) {
443 if(owner && cache_mac_subnet[i] && cache_mac_subnet[i]->owner != owner) {
447 if(!memcmp(address, &cache_mac_address[i], sizeof(*address))) {
448 return cache_mac_subnet[i];
452 // Search all subnets for a matching one
454 for(n = owner ? owner->subnet_tree->head : subnet_tree->head; n; n = n->next) {
457 if(!p || p->type != SUBNET_MAC) {
461 if(!memcmp(address, &p->net.mac.address, sizeof(*address))) {
464 if(p->owner->status.reachable) {
472 cache_mac_slot = !cache_mac_slot;
473 memcpy(&cache_mac_address[cache_mac_slot], address, sizeof(*address));
474 cache_mac_subnet[cache_mac_slot] = r;
475 cache_mac_valid[cache_mac_slot] = true;
480 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
481 subnet_t *p, *r = NULL;
485 // Check if this address is cached
487 for(i = 0; i < 2; i++) {
488 if(!cache_ipv4_valid[i]) {
492 if(!memcmp(address, &cache_ipv4_address[i], sizeof(*address))) {
493 return cache_ipv4_subnet[i];
497 // Search all subnets for a matching one
499 for(n = subnet_tree->head; n; n = n->next) {
502 if(!p || p->type != SUBNET_IPV4) {
506 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
509 if(p->owner->status.reachable) {
517 cache_ipv4_slot = !cache_ipv4_slot;
518 memcpy(&cache_ipv4_address[cache_ipv4_slot], address, sizeof(*address));
519 cache_ipv4_subnet[cache_ipv4_slot] = r;
520 cache_ipv4_valid[cache_ipv4_slot] = true;
525 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
526 subnet_t *p, *r = NULL;
530 // Check if this address is cached
532 for(i = 0; i < 2; i++) {
533 if(!cache_ipv6_valid[i]) {
537 if(!memcmp(address, &cache_ipv6_address[i], sizeof(*address))) {
538 return cache_ipv6_subnet[i];
542 // Search all subnets for a matching one
544 for(n = subnet_tree->head; n; n = n->next) {
547 if(!p || p->type != SUBNET_IPV6) {
551 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
554 if(p->owner->status.reachable) {
562 cache_ipv6_slot = !cache_ipv6_slot;
563 memcpy(&cache_ipv6_address[cache_ipv6_slot], address, sizeof(*address));
564 cache_ipv6_subnet[cache_ipv6_slot] = r;
565 cache_ipv6_valid[cache_ipv6_slot] = true;
570 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
573 char *envp[10] = {NULL};
574 char netstr[MAXNETSTR];
575 char *name, *address, *port;
578 // Prepare environment variables to be passed to the script
580 xasprintf(&envp[0], "NETNAME=%s", netname ? netname : "");
581 xasprintf(&envp[1], "DEVICE=%s", device ? device : "");
582 xasprintf(&envp[2], "INTERFACE=%s", iface ? iface : "");
583 xasprintf(&envp[3], "NODE=%s", owner->name);
584 xasprintf(&envp[4], "NAME=%s", myself->name);
586 if(owner != myself) {
587 sockaddr2str(&owner->address, &address, &port);
588 // 5 and 6 are reserved for SUBNET and WEIGHT
589 xasprintf(&envp[7], "REMOTEADDRESS=%s", address);
590 xasprintf(&envp[8], "REMOTEPORT=%s", port);
595 name = up ? "subnet-up" : "subnet-down";
598 for(node = owner->subnet_tree->head; node; node = node->next) {
601 if(!net2str(netstr, sizeof(netstr), subnet)) {
605 // Strip the weight from the subnet, and put it in its own environment variable
606 char *weight = strchr(netstr, '#');
614 // Prepare the SUBNET and WEIGHT variables
618 xasprintf(&envp[5], "SUBNET=%s", netstr);
619 xasprintf(&envp[6], "WEIGHT=%s", weight);
621 execute_script(name, envp);
624 if(net2str(netstr, sizeof(netstr), subnet)) {
625 // Strip the weight from the subnet, and put it in its own environment variable
626 char *weight = strchr(netstr, '#');
634 // Prepare the SUBNET and WEIGHT variables
635 xasprintf(&envp[5], "SUBNET=%s", netstr);
636 xasprintf(&envp[6], "WEIGHT=%s", weight);
638 execute_script(name, envp);
642 for(i = 0; i < 9; i++) {
647 void dump_subnets(void) {
648 char netstr[MAXNETSTR];
652 logger(LOG_DEBUG, "Subnet list:");
654 for(node = subnet_tree->head; node; node = node->next) {
657 if(!net2str(netstr, sizeof(netstr), subnet)) {
661 logger(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
664 logger(LOG_DEBUG, "End of subnet list.");