2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000-2010 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.
23 #include "splay_tree.h"
24 #include "control_common.h"
35 /* lists type of subnet */
37 splay_tree_t *subnet_tree;
39 /* Subnet lookup cache */
41 static ipv4_t cache_ipv4_address[2];
42 static subnet_t *cache_ipv4_subnet[2];
43 static bool cache_ipv4_valid[2];
44 static int cache_ipv4_slot;
46 static ipv6_t cache_ipv6_address[2];
47 static subnet_t *cache_ipv6_subnet[2];
48 static bool cache_ipv6_valid[2];
49 static int cache_ipv6_slot;
51 static mac_t cache_mac_address[2];
52 static subnet_t *cache_mac_subnet[2];
53 static bool cache_mac_valid[2];
54 static int cache_mac_slot;
56 void subnet_cache_flush(void) {
57 cache_ipv4_valid[0] = cache_ipv4_valid[1] = false;
58 cache_ipv6_valid[0] = cache_ipv6_valid[1] = false;
59 cache_mac_valid[0] = cache_mac_valid[1] = false;
62 /* Subnet comparison */
64 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
67 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof a->net.mac.address);
72 result = a->weight - b->weight;
74 if(result || !a->owner || !b->owner)
77 return strcmp(a->owner->name, b->owner->name);
80 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
83 result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
88 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
93 result = a->weight - b->weight;
95 if(result || !a->owner || !b->owner)
98 return strcmp(a->owner->name, b->owner->name);
101 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
104 result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
109 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
114 result = a->weight - b->weight;
116 if(result || !a->owner || !b->owner)
119 return strcmp(a->owner->name, b->owner->name);
122 int subnet_compare(const subnet_t *a, const subnet_t *b) {
125 result = a->type - b->type;
132 return subnet_compare_mac(a, b);
134 return subnet_compare_ipv4(a, b);
136 return subnet_compare_ipv6(a, b);
138 logger(DEBUG_ALWAYS, LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!",
146 /* Initialising trees */
148 void init_subnets(void) {
149 subnet_tree = splay_alloc_tree((splay_compare_t) subnet_compare, (splay_action_t) free_subnet);
151 subnet_cache_flush();
154 void exit_subnets(void) {
155 splay_delete_tree(subnet_tree);
158 splay_tree_t *new_subnet_tree(void) {
159 return splay_alloc_tree((splay_compare_t) subnet_compare, NULL);
162 void free_subnet_tree(splay_tree_t *subnet_tree) {
163 splay_delete_tree(subnet_tree);
166 /* Allocating and freeing space for subnets */
168 subnet_t *new_subnet(void) {
169 return xmalloc_and_zero(sizeof(subnet_t));
172 void free_subnet(subnet_t *subnet) {
176 /* Adding and removing subnets */
178 void subnet_add(node_t *n, subnet_t *subnet) {
181 splay_insert(subnet_tree, subnet);
182 splay_insert(n->subnet_tree, subnet);
184 subnet_cache_flush();
187 void subnet_del(node_t *n, subnet_t *subnet) {
188 splay_delete(n->subnet_tree, subnet);
189 splay_delete(subnet_tree, subnet);
191 subnet_cache_flush();
194 /* Ascii representation of subnets */
196 bool str2net(subnet_t *subnet, const char *subnetstr) {
201 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d#%d",
202 &x[0], &x[1], &x[2], &x[3], &l, &weight) >= 5) {
206 subnet->type = SUBNET_IPV4;
207 subnet->net.ipv4.prefixlength = l;
208 subnet->weight = weight;
210 for(i = 0; i < 4; i++) {
213 subnet->net.ipv4.address.x[i] = x[i];
219 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
220 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
225 subnet->type = SUBNET_IPV6;
226 subnet->net.ipv6.prefixlength = l;
227 subnet->weight = weight;
229 for(i = 0; i < 8; i++)
230 subnet->net.ipv6.address.x[i] = htons(x[i]);
235 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu#%d", &x[0], &x[1], &x[2], &x[3], &weight) >= 4) {
236 subnet->type = SUBNET_IPV4;
237 subnet->net.ipv4.prefixlength = 32;
238 subnet->weight = weight;
240 for(i = 0; i < 4; i++) {
243 subnet->net.ipv4.address.x[i] = x[i];
249 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx#%d",
250 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &weight) >= 8) {
251 subnet->type = SUBNET_IPV6;
252 subnet->net.ipv6.prefixlength = 128;
253 subnet->weight = weight;
255 for(i = 0; i < 8; i++)
256 subnet->net.ipv6.address.x[i] = htons(x[i]);
261 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
262 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &weight) >= 6) {
263 subnet->type = SUBNET_MAC;
264 subnet->weight = weight;
266 for(i = 0; i < 6; i++)
267 subnet->net.mac.address.x[i] = x[i];
273 if(strstr(subnetstr, "::")) {
278 // Count number of colons
279 for(p = subnetstr; *p; p++)
286 // Scan numbers before the double colon
288 for(i = 0; i < colons; i++) {
291 x[i] = strtoul(p, &q, 0x10);
292 if(!q || p == q || *q != ':')
304 if(!*p || *p == '/' || *p == '#')
307 // Fill in the blanks
308 for(; i < 8 - colons; i++)
311 // Scan the remaining numbers
313 x[i] = strtoul(p, &q, 0x10);
327 sscanf(p, "/%d#%d", &l, &weight);
329 sscanf(p, "#%d", &weight);
334 subnet->type = SUBNET_IPV6;
335 subnet->net.ipv6.prefixlength = l;
336 subnet->weight = weight;
338 for(i = 0; i < 8; i++)
339 subnet->net.ipv6.address.x[i] = htons(x[i]);
347 bool net2str(char *netstr, int len, const subnet_t *subnet) {
348 if(!netstr || !subnet) {
349 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", netstr, subnet);
353 switch (subnet->type) {
355 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
356 subnet->net.mac.address.x[0],
357 subnet->net.mac.address.x[1],
358 subnet->net.mac.address.x[2],
359 subnet->net.mac.address.x[3],
360 subnet->net.mac.address.x[4],
361 subnet->net.mac.address.x[5],
366 snprintf(netstr, len, "%hu.%hu.%hu.%hu/%d#%d",
367 subnet->net.ipv4.address.x[0],
368 subnet->net.ipv4.address.x[1],
369 subnet->net.ipv4.address.x[2],
370 subnet->net.ipv4.address.x[3],
371 subnet->net.ipv4.prefixlength,
376 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
377 ntohs(subnet->net.ipv6.address.x[0]),
378 ntohs(subnet->net.ipv6.address.x[1]),
379 ntohs(subnet->net.ipv6.address.x[2]),
380 ntohs(subnet->net.ipv6.address.x[3]),
381 ntohs(subnet->net.ipv6.address.x[4]),
382 ntohs(subnet->net.ipv6.address.x[5]),
383 ntohs(subnet->net.ipv6.address.x[6]),
384 ntohs(subnet->net.ipv6.address.x[7]),
385 subnet->net.ipv6.prefixlength,
390 logger(DEBUG_ALWAYS, LOG_ERR,
391 "net2str() was called with unknown subnet type %d, exiting!",
399 /* Subnet lookup routines */
401 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet) {
402 return splay_search(owner->subnet_tree, subnet);
405 subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
406 subnet_t *p, *r = NULL;
410 // Check if this address is cached
412 for(i = 0; i < 2; i++) {
413 if(!cache_mac_valid[i])
415 if(owner && cache_mac_subnet[i] && cache_mac_subnet[i]->owner != owner)
417 if(!memcmp(address, &cache_mac_address[i], sizeof *address))
418 return cache_mac_subnet[i];
421 // Search all subnets for a matching one
423 for(n = owner ? owner->subnet_tree->head : subnet_tree->head; n; n = n->next) {
426 if(!p || p->type != SUBNET_MAC)
429 if(!memcmp(address, &p->net.mac.address, sizeof *address)) {
431 if(p->owner->status.reachable)
438 cache_mac_slot = !cache_mac_slot;
439 memcpy(&cache_mac_address[cache_mac_slot], address, sizeof *address);
440 cache_mac_subnet[cache_mac_slot] = r;
441 cache_mac_valid[cache_mac_slot] = true;
446 subnet_t *lookup_subnet_ipv4(const ipv4_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_ipv4_valid[i])
456 if(!memcmp(address, &cache_ipv4_address[i], sizeof *address))
457 return cache_ipv4_subnet[i];
460 // Search all subnets for a matching one
462 for(n = subnet_tree->head; n; n = n->next) {
465 if(!p || p->type != SUBNET_IPV4)
468 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
470 if(p->owner->status.reachable)
477 cache_ipv4_slot = !cache_ipv4_slot;
478 memcpy(&cache_ipv4_address[cache_ipv4_slot], address, sizeof *address);
479 cache_ipv4_subnet[cache_ipv4_slot] = r;
480 cache_ipv4_valid[cache_ipv4_slot] = true;
485 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
486 subnet_t *p, *r = NULL;
490 // Check if this address is cached
492 for(i = 0; i < 2; i++) {
493 if(!cache_ipv6_valid[i])
495 if(!memcmp(address, &cache_ipv6_address[i], sizeof *address))
496 return cache_ipv6_subnet[i];
499 // Search all subnets for a matching one
501 for(n = subnet_tree->head; n; n = n->next) {
504 if(!p || p->type != SUBNET_IPV6)
507 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
509 if(p->owner->status.reachable)
516 cache_ipv6_slot = !cache_ipv6_slot;
517 memcpy(&cache_ipv6_address[cache_ipv6_slot], address, sizeof *address);
518 cache_ipv6_subnet[cache_ipv6_slot] = r;
519 cache_ipv6_valid[cache_ipv6_slot] = true;
524 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
527 char *envp[9] = {NULL};
528 char netstr[MAXNETSTR];
529 char *name, *address, *port;
532 // Prepare environment variables to be passed to the script
534 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
535 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
536 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
537 xasprintf(&envp[3], "NODE=%s", owner->name);
539 if(owner != myself) {
540 sockaddr2str(&owner->address, &address, &port);
541 // 4 and 5 are reserved for SUBNET and WEIGHT
542 xasprintf(&envp[6], "REMOTEADDRESS=%s", address);
543 xasprintf(&envp[7], "REMOTEPORT=%s", port);
548 name = up ? "subnet-up" : "subnet-down";
551 for(node = owner->subnet_tree->head; node; node = node->next) {
553 if(!net2str(netstr, sizeof netstr, subnet))
555 // Strip the weight from the subnet, and put it in its own environment variable
556 char *weight = strchr(netstr, '#');
562 // Prepare the SUBNET and WEIGHT variables
567 xasprintf(&envp[4], "SUBNET=%s", netstr);
568 xasprintf(&envp[5], "WEIGHT=%s", weight);
570 execute_script(name, envp);
573 if(net2str(netstr, sizeof netstr, subnet)) {
574 // Strip the weight from the subnet, and put it in its own environment variable
575 char *weight = strchr(netstr, '#');
581 // Prepare the SUBNET and WEIGHT variables
582 xasprintf(&envp[4], "SUBNET=%s", netstr);
583 xasprintf(&envp[5], "WEIGHT=%s", weight);
585 execute_script(name, envp);
589 for(i = 0; envp[i] && i < 8; i++)
593 bool dump_subnets(connection_t *c) {
594 char netstr[MAXNETSTR];
598 for(node = subnet_tree->head; node; node = node->next) {
600 if(!net2str(netstr, sizeof netstr, subnet))
602 send_request(c, "%d %d %s owner %s",
603 CONTROL, REQ_DUMP_SUBNETS,
604 netstr, subnet->owner->name);
607 return send_request(c, "%d %d", CONTROL, REQ_DUMP_SUBNETS);