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.
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() {
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(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];
275 bool net2str(char *netstr, int len, const subnet_t *subnet) {
276 if(!netstr || !subnet) {
277 logger(LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", netstr, subnet);
281 switch (subnet->type) {
283 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
284 subnet->net.mac.address.x[0],
285 subnet->net.mac.address.x[1],
286 subnet->net.mac.address.x[2],
287 subnet->net.mac.address.x[3],
288 subnet->net.mac.address.x[4],
289 subnet->net.mac.address.x[5],
294 snprintf(netstr, len, "%hu.%hu.%hu.%hu/%d#%d",
295 subnet->net.ipv4.address.x[0],
296 subnet->net.ipv4.address.x[1],
297 subnet->net.ipv4.address.x[2],
298 subnet->net.ipv4.address.x[3],
299 subnet->net.ipv4.prefixlength,
304 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
305 ntohs(subnet->net.ipv6.address.x[0]),
306 ntohs(subnet->net.ipv6.address.x[1]),
307 ntohs(subnet->net.ipv6.address.x[2]),
308 ntohs(subnet->net.ipv6.address.x[3]),
309 ntohs(subnet->net.ipv6.address.x[4]),
310 ntohs(subnet->net.ipv6.address.x[5]),
311 ntohs(subnet->net.ipv6.address.x[6]),
312 ntohs(subnet->net.ipv6.address.x[7]),
313 subnet->net.ipv6.prefixlength,
319 "net2str() was called with unknown subnet type %d, exiting!",
327 /* Subnet lookup routines */
329 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet) {
330 return splay_search(owner->subnet_tree, subnet);
333 subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
334 subnet_t *p, *r = NULL;
338 // Check if this address is cached
340 for(i = 0; i < 2; i++) {
341 if(!cache_mac_valid[i])
343 if(owner && cache_mac_subnet[i] && cache_mac_subnet[i]->owner != owner)
345 if(!memcmp(address, &cache_mac_address[i], sizeof *address))
346 return cache_mac_subnet[i];
349 // Search all subnets for a matching one
351 for(n = owner ? owner->subnet_tree->head : subnet_tree->head; n; n = n->next) {
354 if(!p || p->type != SUBNET_MAC)
357 if(!memcmp(address, &p->net.mac.address, sizeof *address)) {
359 if(p->owner->status.reachable)
366 cache_mac_slot = !cache_mac_slot;
367 memcpy(&cache_mac_address[cache_mac_slot], address, sizeof *address);
368 cache_mac_subnet[cache_mac_slot] = r;
369 cache_mac_valid[cache_mac_slot] = true;
374 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
375 subnet_t *p, *r = NULL;
379 // Check if this address is cached
381 for(i = 0; i < 2; i++) {
382 if(!cache_ipv4_valid[i])
384 if(!memcmp(address, &cache_ipv4_address[i], sizeof *address))
385 return cache_ipv4_subnet[i];
388 // Search all subnets for a matching one
390 for(n = subnet_tree->head; n; n = n->next) {
393 if(!p || p->type != SUBNET_IPV4)
396 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
398 if(p->owner->status.reachable)
405 cache_ipv4_slot = !cache_ipv4_slot;
406 memcpy(&cache_ipv4_address[cache_ipv4_slot], address, sizeof *address);
407 cache_ipv4_subnet[cache_ipv4_slot] = r;
408 cache_ipv4_valid[cache_ipv4_slot] = true;
413 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
414 subnet_t *p, *r = NULL;
418 // Check if this address is cached
420 for(i = 0; i < 2; i++) {
421 if(!cache_ipv6_valid[i])
423 if(!memcmp(address, &cache_ipv6_address[i], sizeof *address))
424 return cache_ipv6_subnet[i];
427 // Search all subnets for a matching one
429 for(n = subnet_tree->head; n; n = n->next) {
432 if(!p || p->type != SUBNET_IPV6)
435 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
437 if(p->owner->status.reachable)
444 cache_ipv6_slot = !cache_ipv6_slot;
445 memcpy(&cache_ipv6_address[cache_ipv6_slot], address, sizeof *address);
446 cache_ipv6_subnet[cache_ipv6_slot] = r;
447 cache_ipv6_valid[cache_ipv6_slot] = true;
452 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
456 char netstr[MAXNETSTR];
457 char *name, *address, *port;
460 // Prepare environment variables to be passed to the script
462 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
463 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
464 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
465 xasprintf(&envp[3], "NODE=%s", owner->name);
467 if(owner != myself) {
468 sockaddr2str(&owner->address, &address, &port);
469 // 4 and 5 are reserved for SUBNET and WEIGHT
470 xasprintf(&envp[6], "REMOTEADDRESS=%s", address);
471 xasprintf(&envp[7], "REMOTEPORT=%s", port);
474 name = up ? "subnet-up" : "subnet-down";
477 for(node = owner->subnet_tree->head; node; node = node->next) {
479 if(!net2str(netstr, sizeof netstr, subnet))
481 // Strip the weight from the subnet, and put it in its own environment variable
482 char *weight = strchr(netstr + 7, '#');
488 // Prepare the SUBNET and WEIGHT variables
493 xasprintf(&envp[4], "SUBNET=%s", netstr);
494 xasprintf(&envp[5], "WEIGHT=%s", weight);
496 execute_script(name, envp);
499 if(net2str(netstr + 7, sizeof netstr - 7, subnet)) {
500 // Strip the weight from the subnet, and put it in its own environment variable
501 char *weight = strchr(netstr + 7, '#');
507 // Prepare the SUBNET and WEIGHT variables
508 xasprintf(&envp[4], "SUBNET=%s", netstr);
509 xasprintf(&envp[5], "WEIGHT=%s", weight);
511 execute_script(name, envp);
515 for(i = 0; envp[i] && i < 9; i++)
519 bool dump_subnets(connection_t *c) {
520 char netstr[MAXNETSTR];
524 for(node = subnet_tree->head; node; node = node->next) {
526 if(!net2str(netstr, sizeof netstr, subnet))
528 send_request(c, "%d %d %s owner %s",
529 CONTROL, REQ_DUMP_SUBNETS,
530 netstr, subnet->owner->name);
533 return send_request(c, "%d %d", CONTROL, REQ_DUMP_SUBNETS);