2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000-2013 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));
71 result = a->weight - b->weight;
73 if(result || !a->owner || !b->owner)
76 return strcmp(a->owner->name, b->owner->name);
79 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
82 result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
87 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
92 result = a->weight - b->weight;
94 if(result || !a->owner || !b->owner)
97 return strcmp(a->owner->name, b->owner->name);
100 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
103 result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
108 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
113 result = a->weight - b->weight;
115 if(result || !a->owner || !b->owner)
118 return strcmp(a->owner->name, b->owner->name);
121 int subnet_compare(const subnet_t *a, const subnet_t *b) {
124 result = a->type - b->type;
131 return subnet_compare_mac(a, b);
133 return subnet_compare_ipv4(a, b);
135 return subnet_compare_ipv6(a, b);
137 logger(LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!",
145 /* Initialising trees */
147 void init_subnets(void) {
148 subnet_tree = avl_alloc_tree((avl_compare_t) subnet_compare, (avl_action_t) free_subnet);
150 subnet_cache_flush();
153 void exit_subnets(void) {
154 avl_delete_tree(subnet_tree);
157 avl_tree_t *new_subnet_tree(void) {
158 return avl_alloc_tree((avl_compare_t) subnet_compare, NULL);
161 void free_subnet_tree(avl_tree_t *subnet_tree) {
162 avl_delete_tree(subnet_tree);
165 /* Allocating and freeing space for subnets */
167 subnet_t *new_subnet(void) {
168 return xmalloc_and_zero(sizeof(subnet_t));
171 void free_subnet(subnet_t *subnet) {
175 /* Adding and removing subnets */
177 void subnet_add(node_t *n, subnet_t *subnet) {
180 avl_insert(subnet_tree, subnet);
181 avl_insert(n->subnet_tree, subnet);
183 subnet_cache_flush();
186 void subnet_del(node_t *n, subnet_t *subnet) {
187 avl_delete(n->subnet_tree, subnet);
188 avl_delete(subnet_tree, subnet);
190 subnet_cache_flush();
193 /* Ascii representation of subnets */
195 bool str2net(subnet_t *subnet, const char *subnetstr) {
200 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d#%d",
201 &x[0], &x[1], &x[2], &x[3], &l, &weight) >= 5) {
205 subnet->type = SUBNET_IPV4;
206 subnet->net.ipv4.prefixlength = l;
207 subnet->weight = weight;
209 for(i = 0; i < 4; i++) {
212 subnet->net.ipv4.address.x[i] = x[i];
218 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
219 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
224 subnet->type = SUBNET_IPV6;
225 subnet->net.ipv6.prefixlength = l;
226 subnet->weight = weight;
228 for(i = 0; i < 8; i++)
229 subnet->net.ipv6.address.x[i] = htons(x[i]);
234 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu#%d", &x[0], &x[1], &x[2], &x[3], &weight) >= 4) {
235 subnet->type = SUBNET_IPV4;
236 subnet->net.ipv4.prefixlength = 32;
237 subnet->weight = weight;
239 for(i = 0; i < 4; i++) {
242 subnet->net.ipv4.address.x[i] = x[i];
248 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx#%d",
249 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &weight) >= 8) {
250 subnet->type = SUBNET_IPV6;
251 subnet->net.ipv6.prefixlength = 128;
252 subnet->weight = weight;
254 for(i = 0; i < 8; i++)
255 subnet->net.ipv6.address.x[i] = htons(x[i]);
260 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
261 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &weight) >= 6) {
262 subnet->type = SUBNET_MAC;
263 subnet->weight = weight;
265 for(i = 0; i < 6; i++)
266 subnet->net.mac.address.x[i] = x[i];
272 if(strstr(subnetstr, "::")) {
277 // Count number of colons
278 for(p = subnetstr; *p; p++)
285 // Scan numbers before the double colon
287 for(i = 0; i < colons; i++) {
290 x[i] = strtoul(p, &q, 0x10);
291 if(!q || p == q || *q != ':')
303 if(!*p || *p == '/' || *p == '#')
306 // Fill in the blanks
307 for(; i < 8 - colons; i++)
310 // Scan the remaining numbers
312 x[i] = strtoul(p, &q, 0x10);
326 sscanf(p, "/%d#%d", &l, &weight);
328 sscanf(p, "#%d", &weight);
333 subnet->type = SUBNET_IPV6;
334 subnet->net.ipv6.prefixlength = l;
335 subnet->weight = weight;
337 for(i = 0; i < 8; i++)
338 subnet->net.ipv6.address.x[i] = htons(x[i]);
346 bool net2str(char *netstr, int len, const subnet_t *subnet) {
347 if(!netstr || !subnet) {
348 logger(LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", netstr, subnet);
352 switch (subnet->type) {
354 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
355 subnet->net.mac.address.x[0],
356 subnet->net.mac.address.x[1],
357 subnet->net.mac.address.x[2],
358 subnet->net.mac.address.x[3],
359 subnet->net.mac.address.x[4],
360 subnet->net.mac.address.x[5],
365 snprintf(netstr, len, "%hu.%hu.%hu.%hu/%d#%d",
366 subnet->net.ipv4.address.x[0],
367 subnet->net.ipv4.address.x[1],
368 subnet->net.ipv4.address.x[2],
369 subnet->net.ipv4.address.x[3],
370 subnet->net.ipv4.prefixlength,
375 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
376 ntohs(subnet->net.ipv6.address.x[0]),
377 ntohs(subnet->net.ipv6.address.x[1]),
378 ntohs(subnet->net.ipv6.address.x[2]),
379 ntohs(subnet->net.ipv6.address.x[3]),
380 ntohs(subnet->net.ipv6.address.x[4]),
381 ntohs(subnet->net.ipv6.address.x[5]),
382 ntohs(subnet->net.ipv6.address.x[6]),
383 ntohs(subnet->net.ipv6.address.x[7]),
384 subnet->net.ipv6.prefixlength,
390 "net2str() was called with unknown subnet type %d, exiting!",
398 /* Subnet lookup routines */
400 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet) {
401 return avl_search(owner->subnet_tree, subnet);
404 subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
405 subnet_t *p, *r = NULL;
409 // Check if this address is cached
411 for(i = 0; i < 2; i++) {
412 if(!cache_mac_valid[i])
414 if(owner && cache_mac_subnet[i] && cache_mac_subnet[i]->owner != owner)
416 if(!memcmp(address, &cache_mac_address[i], sizeof *address))
417 return cache_mac_subnet[i];
420 // Search all subnets for a matching one
422 for(n = owner ? owner->subnet_tree->head : subnet_tree->head; n; n = n->next) {
425 if(!p || p->type != SUBNET_MAC)
428 if(!memcmp(address, &p->net.mac.address, sizeof *address)) {
430 if(p->owner->status.reachable)
437 cache_mac_slot = !cache_mac_slot;
438 memcpy(&cache_mac_address[cache_mac_slot], address, sizeof *address);
439 cache_mac_subnet[cache_mac_slot] = r;
440 cache_mac_valid[cache_mac_slot] = true;
445 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
446 subnet_t *p, *r = NULL;
450 // Check if this address is cached
452 for(i = 0; i < 2; i++) {
453 if(!cache_ipv4_valid[i])
455 if(!memcmp(address, &cache_ipv4_address[i], sizeof *address))
456 return cache_ipv4_subnet[i];
459 // Search all subnets for a matching one
461 for(n = subnet_tree->head; n; n = n->next) {
464 if(!p || p->type != SUBNET_IPV4)
467 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
469 if(p->owner->status.reachable)
476 cache_ipv4_slot = !cache_ipv4_slot;
477 memcpy(&cache_ipv4_address[cache_ipv4_slot], address, sizeof *address);
478 cache_ipv4_subnet[cache_ipv4_slot] = r;
479 cache_ipv4_valid[cache_ipv4_slot] = true;
484 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
485 subnet_t *p, *r = NULL;
489 // Check if this address is cached
491 for(i = 0; i < 2; i++) {
492 if(!cache_ipv6_valid[i])
494 if(!memcmp(address, &cache_ipv6_address[i], sizeof *address))
495 return cache_ipv6_subnet[i];
498 // Search all subnets for a matching one
500 for(n = subnet_tree->head; n; n = n->next) {
503 if(!p || p->type != SUBNET_IPV6)
506 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
508 if(p->owner->status.reachable)
515 cache_ipv6_slot = !cache_ipv6_slot;
516 memcpy(&cache_ipv6_address[cache_ipv6_slot], address, sizeof *address);
517 cache_ipv6_subnet[cache_ipv6_slot] = r;
518 cache_ipv6_valid[cache_ipv6_slot] = true;
523 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
526 char *envp[10] = {NULL};
527 char netstr[MAXNETSTR];
528 char *name, *address, *port;
531 // Prepare environment variables to be passed to the script
533 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
534 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
535 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
536 xasprintf(&envp[3], "NODE=%s", owner->name);
538 if(owner != myself) {
539 sockaddr2str(&owner->address, &address, &port);
540 // 4 and 5 are reserved for SUBNET and WEIGHT
541 xasprintf(&envp[6], "REMOTEADDRESS=%s", address);
542 xasprintf(&envp[7], "REMOTEPORT=%s", port);
547 xasprintf(&envp[8], "NAME=%s", myself->name);
549 name = up ? "subnet-up" : "subnet-down";
552 for(node = owner->subnet_tree->head; node; node = node->next) {
554 if(!net2str(netstr, sizeof netstr, subnet))
556 // Strip the weight from the subnet, and put it in its own environment variable
557 char *weight = strchr(netstr, '#');
563 // Prepare the SUBNET and WEIGHT variables
568 xasprintf(&envp[4], "SUBNET=%s", netstr);
569 xasprintf(&envp[5], "WEIGHT=%s", weight);
571 execute_script(name, envp);
574 if(net2str(netstr, sizeof netstr, subnet)) {
575 // Strip the weight from the subnet, and put it in its own environment variable
576 char *weight = strchr(netstr, '#');
582 // Prepare the SUBNET and WEIGHT variables
583 xasprintf(&envp[4], "SUBNET=%s", netstr);
584 xasprintf(&envp[5], "WEIGHT=%s", weight);
586 execute_script(name, envp);
590 for(i = 0; envp[i] && i < 9; i++)
594 void dump_subnets(void) {
595 char netstr[MAXNETSTR];
599 logger(LOG_DEBUG, "Subnet list:");
601 for(node = subnet_tree->head; node; node = node->next) {
603 if(!net2str(netstr, sizeof netstr, subnet))
605 logger(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
608 logger(LOG_DEBUG, "End of subnet list.");