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!"),
139 /* Initialising trees */
141 void init_subnets(void) {
142 subnet_tree = avl_alloc_tree((avl_compare_t) subnet_compare, (avl_action_t) free_subnet);
144 subnet_cache_flush();
147 void exit_subnets(void) {
148 avl_delete_tree(subnet_tree);
151 avl_tree_t *new_subnet_tree(void) {
152 return avl_alloc_tree((avl_compare_t) subnet_compare, NULL);
155 void free_subnet_tree(avl_tree_t *subnet_tree) {
156 avl_delete_tree(subnet_tree);
159 /* Allocating and freeing space for subnets */
161 subnet_t *new_subnet(void) {
162 return xmalloc_and_zero(sizeof(subnet_t));
165 void free_subnet(subnet_t *subnet) {
169 /* Adding and removing subnets */
171 void subnet_add(node_t *n, subnet_t *subnet) {
174 avl_insert(subnet_tree, subnet);
175 avl_insert(n->subnet_tree, subnet);
177 subnet_cache_flush();
180 void subnet_del(node_t *n, subnet_t *subnet) {
181 avl_delete(n->subnet_tree, subnet);
182 avl_delete(subnet_tree, subnet);
184 subnet_cache_flush();
187 /* Ascii representation of subnets */
189 bool str2net(subnet_t *subnet, const char *subnetstr) {
194 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d#%d",
195 &x[0], &x[1], &x[2], &x[3], &l, &weight) >= 5) {
199 subnet->type = SUBNET_IPV4;
200 subnet->net.ipv4.prefixlength = l;
201 subnet->weight = weight;
203 for(i = 0; i < 4; i++) {
206 subnet->net.ipv4.address.x[i] = x[i];
212 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
213 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
218 subnet->type = SUBNET_IPV6;
219 subnet->net.ipv6.prefixlength = l;
220 subnet->weight = weight;
222 for(i = 0; i < 8; i++)
223 subnet->net.ipv6.address.x[i] = htons(x[i]);
228 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu#%d", &x[0], &x[1], &x[2], &x[3], &weight) >= 4) {
229 subnet->type = SUBNET_IPV4;
230 subnet->net.ipv4.prefixlength = 32;
231 subnet->weight = weight;
233 for(i = 0; i < 4; i++) {
236 subnet->net.ipv4.address.x[i] = x[i];
242 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx#%d",
243 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &weight) >= 8) {
244 subnet->type = SUBNET_IPV6;
245 subnet->net.ipv6.prefixlength = 128;
246 subnet->weight = weight;
248 for(i = 0; i < 8; i++)
249 subnet->net.ipv6.address.x[i] = htons(x[i]);
254 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
255 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &weight) >= 6) {
256 subnet->type = SUBNET_MAC;
257 subnet->weight = weight;
259 for(i = 0; i < 6; i++)
260 subnet->net.mac.address.x[i] = x[i];
268 bool net2str(char *netstr, int len, const subnet_t *subnet) {
269 if(!netstr || !subnet) {
270 logger(LOG_ERR, _("net2str() was called with netstr=%p, subnet=%p!\n"), netstr, subnet);
274 switch (subnet->type) {
276 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
277 subnet->net.mac.address.x[0],
278 subnet->net.mac.address.x[1],
279 subnet->net.mac.address.x[2],
280 subnet->net.mac.address.x[3],
281 subnet->net.mac.address.x[4],
282 subnet->net.mac.address.x[5],
287 snprintf(netstr, len, "%hu.%hu.%hu.%hu/%d#%d",
288 subnet->net.ipv4.address.x[0],
289 subnet->net.ipv4.address.x[1],
290 subnet->net.ipv4.address.x[2],
291 subnet->net.ipv4.address.x[3],
292 subnet->net.ipv4.prefixlength,
297 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
298 ntohs(subnet->net.ipv6.address.x[0]),
299 ntohs(subnet->net.ipv6.address.x[1]),
300 ntohs(subnet->net.ipv6.address.x[2]),
301 ntohs(subnet->net.ipv6.address.x[3]),
302 ntohs(subnet->net.ipv6.address.x[4]),
303 ntohs(subnet->net.ipv6.address.x[5]),
304 ntohs(subnet->net.ipv6.address.x[6]),
305 ntohs(subnet->net.ipv6.address.x[7]),
306 subnet->net.ipv6.prefixlength,
312 _("net2str() was called with unknown subnet type %d, exiting!"),
320 /* Subnet lookup routines */
322 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet) {
323 return avl_search(owner->subnet_tree, subnet);
326 subnet_t *lookup_subnet_mac(const mac_t *address) {
327 subnet_t *p, subnet = {0};
329 subnet.type = SUBNET_MAC;
330 subnet.net.mac.address = *address;
333 p = avl_search(subnet_tree, &subnet);
338 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
339 subnet_t *p, *r = NULL, subnet = {0};
343 // Check if this address is cached
345 for(i = 0; i < 2; i++) {
346 if(!cache_ipv4_valid[i])
348 if(!memcmp(address, &cache_ipv4_address[i], sizeof *address))
349 return cache_ipv4_subnet[i];
352 // Search all subnets for a matching one
354 subnet.type = SUBNET_IPV4;
355 subnet.net.ipv4.address = *address;
356 subnet.net.ipv4.prefixlength = 32;
359 for(n = subnet_tree->head; n; n = n->next) {
362 if(!p || p->type != subnet.type)
365 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
367 if(p->owner->status.reachable)
374 cache_ipv4_slot = !cache_ipv4_slot;
375 memcpy(&cache_ipv4_address[cache_ipv4_slot], address, sizeof *address);
376 cache_ipv4_subnet[cache_ipv4_slot] = r;
377 cache_ipv4_valid[cache_ipv4_slot] = true;
382 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
383 subnet_t *p, *r = NULL, subnet = {0};
387 // Check if this address is cached
389 for(i = 0; i < 2; i++) {
390 if(!cache_ipv6_valid[i])
392 if(!memcmp(address, &cache_ipv6_address[i], sizeof *address))
393 return cache_ipv6_subnet[i];
396 // Search all subnets for a matching one
398 subnet.type = SUBNET_IPV6;
399 subnet.net.ipv6.address = *address;
400 subnet.net.ipv6.prefixlength = 128;
403 for(n = subnet_tree->head; n; n = n->next) {
406 if(!p || p->type != subnet.type)
409 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
411 if(p->owner->status.reachable)
418 cache_ipv6_slot = !cache_ipv6_slot;
419 memcpy(&cache_ipv6_address[cache_ipv6_slot], address, sizeof *address);
420 cache_ipv6_subnet[cache_ipv6_slot] = r;
421 cache_ipv6_valid[cache_ipv6_slot] = true;
426 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
430 char netstr[MAXNETSTR];
431 char *name, *address, *port;
434 // Prepare environment variables to be passed to the script
436 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
437 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
438 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
439 xasprintf(&envp[3], "NODE=%s", owner->name);
441 if(owner != myself) {
442 sockaddr2str(&owner->address, &address, &port);
443 // 4 and 5 are reserved for SUBNET and WEIGHT
444 xasprintf(&envp[6], "REMOTEADDRESS=%s", address);
445 xasprintf(&envp[7], "REMOTEPORT=%s", port);
448 name = up ? "subnet-up" : "subnet-down";
451 for(node = owner->subnet_tree->head; node; node = node->next) {
453 if(!net2str(netstr, sizeof netstr, subnet))
455 // Strip the weight from the subnet, and put it in its own environment variable
456 char *weight = strchr(netstr + 7, '#');
462 // Prepare the SUBNET and WEIGHT variables
467 xasprintf(&envp[4], "SUBNET=%s", netstr);
468 xasprintf(&envp[5], "WEIGHT=%s", weight);
470 execute_script(name, envp);
473 if(net2str(netstr + 7, sizeof netstr - 7, subnet)) {
474 // Strip the weight from the subnet, and put it in its own environment variable
475 char *weight = strchr(netstr + 7, '#');
481 // Prepare the SUBNET and WEIGHT variables
482 xasprintf(&envp[4], "SUBNET=%s", netstr);
483 xasprintf(&envp[5], "WEIGHT=%s", weight);
485 execute_script(name, envp);
489 for(i = 0; envp[i] && i < 9; i++)
493 void dump_subnets(void) {
494 char netstr[MAXNETSTR];
498 logger(LOG_DEBUG, _("Subnet list:"));
500 for(node = subnet_tree->head; node; node = node->next) {
502 if(!net2str(netstr, sizeof netstr, subnet))
504 logger(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
507 logger(LOG_DEBUG, _("End of subnet list."));