2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000-2009 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 void subnet_cache_flush() {
51 cache_ipv4_valid[0] = cache_ipv4_valid[1] = false;
52 cache_ipv6_valid[0] = cache_ipv6_valid[1] = false;
55 /* Subnet comparison */
57 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
60 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
65 result = a->weight - b->weight;
67 if(result || !a->owner || !b->owner)
70 return strcmp(a->owner->name, b->owner->name);
73 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
76 result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
81 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
86 result = a->weight - b->weight;
88 if(result || !a->owner || !b->owner)
91 return strcmp(a->owner->name, b->owner->name);
94 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
97 result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
102 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
107 result = a->weight - b->weight;
109 if(result || !a->owner || !b->owner)
112 return strcmp(a->owner->name, b->owner->name);
115 int subnet_compare(const subnet_t *a, const subnet_t *b) {
118 result = a->type - b->type;
125 return subnet_compare_mac(a, b);
127 return subnet_compare_ipv4(a, b);
129 return subnet_compare_ipv6(a, b);
131 logger(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, exitting!"),
140 /* Initialising trees */
142 void init_subnets(void) {
145 subnet_tree = avl_alloc_tree((avl_compare_t) subnet_compare, (avl_action_t) free_subnet);
147 subnet_cache_flush();
150 void exit_subnets(void) {
153 avl_delete_tree(subnet_tree);
156 avl_tree_t *new_subnet_tree(void) {
159 return avl_alloc_tree((avl_compare_t) subnet_compare, NULL);
162 void free_subnet_tree(avl_tree_t *subnet_tree) {
165 avl_delete_tree(subnet_tree);
168 /* Allocating and freeing space for subnets */
170 subnet_t *new_subnet(void) {
173 return xmalloc_and_zero(sizeof(subnet_t));
176 void free_subnet(subnet_t *subnet) {
182 /* Adding and removing subnets */
184 void subnet_add(node_t *n, subnet_t *subnet) {
189 avl_insert(subnet_tree, subnet);
190 avl_insert(n->subnet_tree, subnet);
192 subnet_cache_flush();
195 void subnet_del(node_t *n, subnet_t *subnet) {
198 avl_delete(n->subnet_tree, subnet);
199 avl_delete(subnet_tree, subnet);
201 subnet_cache_flush();
204 /* Ascii representation of subnets */
206 bool str2net(subnet_t *subnet, const char *subnetstr) {
213 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d#%d",
214 &x[0], &x[1], &x[2], &x[3], &l, &weight) >= 5) {
218 subnet->type = SUBNET_IPV4;
219 subnet->net.ipv4.prefixlength = l;
220 subnet->weight = weight;
222 for(i = 0; i < 4; i++) {
225 subnet->net.ipv4.address.x[i] = x[i];
231 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
232 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
237 subnet->type = SUBNET_IPV6;
238 subnet->net.ipv6.prefixlength = l;
239 subnet->weight = weight;
241 for(i = 0; i < 8; i++)
242 subnet->net.ipv6.address.x[i] = htons(x[i]);
247 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu#%d", &x[0], &x[1], &x[2], &x[3], &weight) >= 4) {
248 subnet->type = SUBNET_IPV4;
249 subnet->net.ipv4.prefixlength = 32;
250 subnet->weight = weight;
252 for(i = 0; i < 4; i++) {
255 subnet->net.ipv4.address.x[i] = x[i];
261 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx#%d",
262 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &weight) >= 8) {
263 subnet->type = SUBNET_IPV6;
264 subnet->net.ipv6.prefixlength = 128;
265 subnet->weight = weight;
267 for(i = 0; i < 8; i++)
268 subnet->net.ipv6.address.x[i] = htons(x[i]);
273 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
274 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &weight) >= 6) {
275 subnet->type = SUBNET_MAC;
276 subnet->weight = weight;
278 for(i = 0; i < 6; i++)
279 subnet->net.mac.address.x[i] = x[i];
287 bool net2str(char *netstr, int len, const subnet_t *subnet) {
290 if(!netstr || !subnet) {
291 logger(LOG_ERR, _("net2str() was called with netstr=%p, subnet=%p!\n"), netstr, subnet);
295 switch (subnet->type) {
297 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
298 subnet->net.mac.address.x[0],
299 subnet->net.mac.address.x[1],
300 subnet->net.mac.address.x[2],
301 subnet->net.mac.address.x[3],
302 subnet->net.mac.address.x[4],
303 subnet->net.mac.address.x[5],
308 snprintf(netstr, len, "%hu.%hu.%hu.%hu/%d#%d",
309 subnet->net.ipv4.address.x[0],
310 subnet->net.ipv4.address.x[1],
311 subnet->net.ipv4.address.x[2],
312 subnet->net.ipv4.address.x[3],
313 subnet->net.ipv4.prefixlength,
318 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
319 ntohs(subnet->net.ipv6.address.x[0]),
320 ntohs(subnet->net.ipv6.address.x[1]),
321 ntohs(subnet->net.ipv6.address.x[2]),
322 ntohs(subnet->net.ipv6.address.x[3]),
323 ntohs(subnet->net.ipv6.address.x[4]),
324 ntohs(subnet->net.ipv6.address.x[5]),
325 ntohs(subnet->net.ipv6.address.x[6]),
326 ntohs(subnet->net.ipv6.address.x[7]),
327 subnet->net.ipv6.prefixlength,
333 _("net2str() was called with unknown subnet type %d, exiting!"),
342 /* Subnet lookup routines */
344 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet) {
347 return avl_search(owner->subnet_tree, subnet);
350 subnet_t *lookup_subnet_mac(const mac_t *address) {
351 subnet_t *p, subnet = {0};
355 subnet.type = SUBNET_MAC;
356 subnet.net.mac.address = *address;
359 p = avl_search(subnet_tree, &subnet);
364 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
365 subnet_t *p, *r = NULL, subnet = {0};
371 // Check if this address is cached
373 for(i = 0; i < 2; i++) {
374 if(!cache_ipv4_valid[i])
376 if(!memcmp(address, &cache_ipv4_address[i], sizeof *address))
377 return cache_ipv4_subnet[i];
380 // Search all subnets for a matching one
382 subnet.type = SUBNET_IPV4;
383 subnet.net.ipv4.address = *address;
384 subnet.net.ipv4.prefixlength = 32;
387 for(n = subnet_tree->head; n; n = n->next) {
390 if(!p || p->type != subnet.type)
393 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
395 if(p->owner->status.reachable)
402 cache_ipv4_slot = !cache_ipv4_slot;
403 memcpy(&cache_ipv4_address[cache_ipv4_slot], address, sizeof *address);
404 cache_ipv4_subnet[cache_ipv4_slot] = r;
405 cache_ipv4_valid[cache_ipv4_slot] = true;
410 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
411 subnet_t *p, *r = NULL, subnet = {0};
417 // Check if this address is cached
419 for(i = 0; i < 2; i++) {
420 if(!cache_ipv6_valid[i])
422 if(!memcmp(address, &cache_ipv6_address[i], sizeof *address))
423 return cache_ipv6_subnet[i];
426 // Search all subnets for a matching one
428 subnet.type = SUBNET_IPV6;
429 subnet.net.ipv6.address = *address;
430 subnet.net.ipv6.prefixlength = 128;
433 for(n = subnet_tree->head; n; n = n->next) {
436 if(!p || p->type != subnet.type)
439 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
441 if(p->owner->status.reachable)
448 cache_ipv6_slot = !cache_ipv6_slot;
449 memcpy(&cache_ipv6_address[cache_ipv6_slot], address, sizeof *address);
450 cache_ipv6_subnet[cache_ipv6_slot] = r;
451 cache_ipv6_valid[cache_ipv6_slot] = true;
456 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
460 char netstr[MAXNETSTR];
461 char *name, *address, *port;
464 // Prepare environment variables to be passed to the script
466 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
467 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
468 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
469 xasprintf(&envp[3], "NODE=%s", owner->name);
471 if(owner != myself) {
472 sockaddr2str(&owner->address, &address, &port);
473 // 4 and 5 are reserved for SUBNET and WEIGHT
474 xasprintf(&envp[6], "REMOTEADDRESS=%s", address);
475 xasprintf(&envp[7], "REMOTEPORT=%s", port);
478 name = up ? "subnet-up" : "subnet-down";
481 for(node = owner->subnet_tree->head; node; node = node->next) {
483 if(!net2str(netstr, sizeof netstr, subnet))
485 // Strip the weight from the subnet, and put it in its own environment variable
486 char *weight = strchr(netstr + 7, '#');
492 // Prepare the SUBNET and WEIGHT variables
497 xasprintf(&envp[4], "SUBNET=%s", netstr);
498 xasprintf(&envp[5], "WEIGHT=%s", weight);
500 execute_script(name, envp);
503 if(net2str(netstr + 7, sizeof netstr - 7, subnet)) {
504 // Strip the weight from the subnet, and put it in its own environment variable
505 char *weight = strchr(netstr + 7, '#');
511 // Prepare the SUBNET and WEIGHT variables
512 xasprintf(&envp[4], "SUBNET=%s", netstr);
513 xasprintf(&envp[5], "WEIGHT=%s", weight);
515 execute_script(name, envp);
519 for(i = 0; envp[i] && i < 9; i++)
523 void dump_subnets(void) {
524 char netstr[MAXNETSTR];
530 logger(LOG_DEBUG, _("Subnet list:"));
532 for(node = subnet_tree->head; node; node = node->next) {
534 if(!net2str(netstr, sizeof netstr, subnet))
536 logger(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
539 logger(LOG_DEBUG, _("End of subnet list."));