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) {
212 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d#%d",
213 &x[0], &x[1], &x[2], &x[3], &l, &weight) >= 5) {
214 if(l < 0 || l > 32) {
218 subnet->type = SUBNET_IPV4;
219 subnet->net.ipv4.prefixlength = l;
220 subnet->weight = weight;
222 for(i = 0; i < 4; i++) {
227 subnet->net.ipv4.address.x[i] = x[i];
233 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
234 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
236 if(l < 0 || l > 128) {
240 subnet->type = SUBNET_IPV6;
241 subnet->net.ipv6.prefixlength = l;
242 subnet->weight = weight;
244 for(i = 0; i < 8; i++) {
245 subnet->net.ipv6.address.x[i] = htons(x[i]);
251 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu#%d", &x[0], &x[1], &x[2], &x[3], &weight) >= 4) {
252 subnet->type = SUBNET_IPV4;
253 subnet->net.ipv4.prefixlength = 32;
254 subnet->weight = weight;
256 for(i = 0; i < 4; i++) {
261 subnet->net.ipv4.address.x[i] = x[i];
267 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx#%d",
268 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &weight) >= 8) {
269 subnet->type = SUBNET_IPV6;
270 subnet->net.ipv6.prefixlength = 128;
271 subnet->weight = weight;
273 for(i = 0; i < 8; i++) {
274 subnet->net.ipv6.address.x[i] = htons(x[i]);
280 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
281 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &weight) >= 6) {
282 subnet->type = SUBNET_MAC;
283 subnet->weight = weight;
285 for(i = 0; i < 6; i++) {
286 subnet->net.mac.address.x[i] = x[i];
293 if(strstr(subnetstr, "::")) {
298 // Count number of colons
299 for(p = subnetstr; *p; p++)
308 // Scan numbers before the double colon
311 for(i = 0; i < colons; i++) {
316 x[i] = strtoul(p, &q, 0x10);
318 if(!q || p == q || *q != ':') {
333 if(!*p || *p == '/' || *p == '#') {
337 // Fill in the blanks
338 for(; i < 8 - colons; i++) {
342 // Scan the remaining numbers
344 x[i] = strtoul(p, &q, 0x10);
365 sscanf(p, "/%d#%d", &l, &weight);
366 } else if(*p == '#') {
367 sscanf(p, "#%d", &weight);
370 if(l < 0 || l > 128) {
374 subnet->type = SUBNET_IPV6;
375 subnet->net.ipv6.prefixlength = l;
376 subnet->weight = weight;
378 for(i = 0; i < 8; i++) {
379 subnet->net.ipv6.address.x[i] = htons(x[i]);
388 bool net2str(char *netstr, int len, const subnet_t *subnet) {
389 if(!netstr || !subnet) {
390 logger(LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", netstr, subnet);
394 switch(subnet->type) {
396 snprintf(netstr, len, "%x:%x:%x:%x:%x:%x#%d",
397 subnet->net.mac.address.x[0],
398 subnet->net.mac.address.x[1],
399 subnet->net.mac.address.x[2],
400 subnet->net.mac.address.x[3],
401 subnet->net.mac.address.x[4],
402 subnet->net.mac.address.x[5],
407 snprintf(netstr, len, "%u.%u.%u.%u/%d#%d",
408 subnet->net.ipv4.address.x[0],
409 subnet->net.ipv4.address.x[1],
410 subnet->net.ipv4.address.x[2],
411 subnet->net.ipv4.address.x[3],
412 subnet->net.ipv4.prefixlength,
417 snprintf(netstr, len, "%x:%x:%x:%x:%x:%x:%x:%x/%d#%d",
418 ntohs(subnet->net.ipv6.address.x[0]),
419 ntohs(subnet->net.ipv6.address.x[1]),
420 ntohs(subnet->net.ipv6.address.x[2]),
421 ntohs(subnet->net.ipv6.address.x[3]),
422 ntohs(subnet->net.ipv6.address.x[4]),
423 ntohs(subnet->net.ipv6.address.x[5]),
424 ntohs(subnet->net.ipv6.address.x[6]),
425 ntohs(subnet->net.ipv6.address.x[7]),
426 subnet->net.ipv6.prefixlength,
432 "net2str() was called with unknown subnet type %d, exiting!",
440 /* Subnet lookup routines */
442 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet) {
443 return avl_search(owner->subnet_tree, subnet);
446 subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
447 subnet_t *p, *r = NULL;
451 // Check if this address is cached
453 for(i = 0; i < 2; i++) {
454 if(!cache_mac_valid[i]) {
458 if(owner && cache_mac_subnet[i] && cache_mac_subnet[i]->owner != owner) {
462 if(!memcmp(address, &cache_mac_address[i], sizeof(*address))) {
463 return cache_mac_subnet[i];
467 // Search all subnets for a matching one
469 for(n = owner ? owner->subnet_tree->head : subnet_tree->head; n; n = n->next) {
472 if(!p || p->type != SUBNET_MAC) {
476 if(!memcmp(address, &p->net.mac.address, sizeof(*address))) {
479 if(p->owner->status.reachable) {
487 cache_mac_slot = !cache_mac_slot;
488 memcpy(&cache_mac_address[cache_mac_slot], address, sizeof(*address));
489 cache_mac_subnet[cache_mac_slot] = r;
490 cache_mac_valid[cache_mac_slot] = true;
495 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
496 subnet_t *p, *r = NULL;
500 // Check if this address is cached
502 for(i = 0; i < 2; i++) {
503 if(!cache_ipv4_valid[i]) {
507 if(!memcmp(address, &cache_ipv4_address[i], sizeof(*address))) {
508 return cache_ipv4_subnet[i];
512 // Search all subnets for a matching one
514 for(n = subnet_tree->head; n; n = n->next) {
517 if(!p || p->type != SUBNET_IPV4) {
521 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
524 if(p->owner->status.reachable) {
532 cache_ipv4_slot = !cache_ipv4_slot;
533 memcpy(&cache_ipv4_address[cache_ipv4_slot], address, sizeof(*address));
534 cache_ipv4_subnet[cache_ipv4_slot] = r;
535 cache_ipv4_valid[cache_ipv4_slot] = true;
540 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
541 subnet_t *p, *r = NULL;
545 // Check if this address is cached
547 for(i = 0; i < 2; i++) {
548 if(!cache_ipv6_valid[i]) {
552 if(!memcmp(address, &cache_ipv6_address[i], sizeof(*address))) {
553 return cache_ipv6_subnet[i];
557 // Search all subnets for a matching one
559 for(n = subnet_tree->head; n; n = n->next) {
562 if(!p || p->type != SUBNET_IPV6) {
566 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
569 if(p->owner->status.reachable) {
577 cache_ipv6_slot = !cache_ipv6_slot;
578 memcpy(&cache_ipv6_address[cache_ipv6_slot], address, sizeof(*address));
579 cache_ipv6_subnet[cache_ipv6_slot] = r;
580 cache_ipv6_valid[cache_ipv6_slot] = true;
585 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
588 char *envp[10] = {NULL};
589 char netstr[MAXNETSTR];
590 char *name, *address, *port;
593 // Prepare environment variables to be passed to the script
595 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
596 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
597 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
598 xasprintf(&envp[3], "NODE=%s", owner->name);
599 xasprintf(&envp[4], "NAME=%s", myself->name);
601 if(owner != myself) {
602 sockaddr2str(&owner->address, &address, &port);
603 // 5 and 6 are reserved for SUBNET and WEIGHT
604 xasprintf(&envp[7], "REMOTEADDRESS=%s", address);
605 xasprintf(&envp[8], "REMOTEPORT=%s", port);
610 name = up ? "subnet-up" : "subnet-down";
613 for(node = owner->subnet_tree->head; node; node = node->next) {
616 if(!net2str(netstr, sizeof(netstr), subnet)) {
620 // Strip the weight from the subnet, and put it in its own environment variable
621 char *weight = strchr(netstr, '#');
629 // Prepare the SUBNET and WEIGHT variables
638 xasprintf(&envp[5], "SUBNET=%s", netstr);
639 xasprintf(&envp[6], "WEIGHT=%s", weight);
641 execute_script(name, envp);
644 if(net2str(netstr, sizeof(netstr), subnet)) {
645 // Strip the weight from the subnet, and put it in its own environment variable
646 char *weight = strchr(netstr, '#');
654 // Prepare the SUBNET and WEIGHT variables
655 xasprintf(&envp[5], "SUBNET=%s", netstr);
656 xasprintf(&envp[6], "WEIGHT=%s", weight);
658 execute_script(name, envp);
662 for(i = 0; envp[i] && i < 9; i++) {
667 void dump_subnets(void) {
668 char netstr[MAXNETSTR];
672 logger(LOG_DEBUG, "Subnet list:");
674 for(node = subnet_tree->head; node; node = node->next) {
677 if(!net2str(netstr, sizeof(netstr), subnet)) {
681 logger(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
684 logger(LOG_DEBUG, "End of subnet list.");