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)
61 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
66 result = a->weight - b->weight;
68 if(result || !a->owner || !b->owner)
71 return strcmp(a->owner->name, b->owner->name);
74 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b)
78 result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
83 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
88 result = a->weight - b->weight;
90 if(result || !a->owner || !b->owner)
93 return strcmp(a->owner->name, b->owner->name);
96 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b)
100 result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
105 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
110 result = a->weight - b->weight;
112 if(result || !a->owner || !b->owner)
115 return strcmp(a->owner->name, b->owner->name);
118 int subnet_compare(const subnet_t *a, const subnet_t *b)
122 result = a->type - b->type;
129 return subnet_compare_mac(a, b);
131 return subnet_compare_ipv4(a, b);
133 return subnet_compare_ipv6(a, b);
135 logger(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, exitting!"),
144 /* Initialising trees */
146 void init_subnets(void)
150 subnet_tree = avl_alloc_tree((avl_compare_t) subnet_compare, (avl_action_t) free_subnet);
152 subnet_cache_flush();
155 void exit_subnets(void)
159 avl_delete_tree(subnet_tree);
162 avl_tree_t *new_subnet_tree(void)
166 return avl_alloc_tree((avl_compare_t) subnet_compare, NULL);
169 void free_subnet_tree(avl_tree_t *subnet_tree)
173 avl_delete_tree(subnet_tree);
176 /* Allocating and freeing space for subnets */
178 subnet_t *new_subnet(void)
182 return xmalloc_and_zero(sizeof(subnet_t));
185 void free_subnet(subnet_t *subnet)
192 /* Adding and removing subnets */
194 void subnet_add(node_t *n, subnet_t *subnet)
200 avl_insert(subnet_tree, subnet);
201 avl_insert(n->subnet_tree, subnet);
203 subnet_cache_flush();
206 void subnet_del(node_t *n, subnet_t *subnet)
210 avl_delete(n->subnet_tree, subnet);
211 avl_delete(subnet_tree, subnet);
213 subnet_cache_flush();
216 /* Ascii representation of subnets */
218 bool str2net(subnet_t *subnet, const char *subnetstr)
226 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d#%d",
227 &x[0], &x[1], &x[2], &x[3], &l, &weight) >= 5) {
231 subnet->type = SUBNET_IPV4;
232 subnet->net.ipv4.prefixlength = l;
233 subnet->weight = weight;
235 for(i = 0; i < 4; i++) {
238 subnet->net.ipv4.address.x[i] = x[i];
244 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
245 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
250 subnet->type = SUBNET_IPV6;
251 subnet->net.ipv6.prefixlength = l;
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, "%hu.%hu.%hu.%hu#%d", &x[0], &x[1], &x[2], &x[3], &weight) >= 4) {
261 subnet->type = SUBNET_IPV4;
262 subnet->net.ipv4.prefixlength = 32;
263 subnet->weight = weight;
265 for(i = 0; i < 4; i++) {
268 subnet->net.ipv4.address.x[i] = x[i];
274 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx#%d",
275 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &weight) >= 8) {
276 subnet->type = SUBNET_IPV6;
277 subnet->net.ipv6.prefixlength = 128;
278 subnet->weight = weight;
280 for(i = 0; i < 8; i++)
281 subnet->net.ipv6.address.x[i] = htons(x[i]);
286 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
287 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &weight) >= 6) {
288 subnet->type = SUBNET_MAC;
289 subnet->weight = weight;
291 for(i = 0; i < 6; i++)
292 subnet->net.mac.address.x[i] = x[i];
300 bool net2str(char *netstr, int len, const subnet_t *subnet)
304 if(!netstr || !subnet) {
305 logger(LOG_ERR, _("net2str() was called with netstr=%p, subnet=%p!\n"), netstr, subnet);
309 switch (subnet->type) {
311 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
312 subnet->net.mac.address.x[0],
313 subnet->net.mac.address.x[1],
314 subnet->net.mac.address.x[2],
315 subnet->net.mac.address.x[3],
316 subnet->net.mac.address.x[4],
317 subnet->net.mac.address.x[5],
322 snprintf(netstr, len, "%hu.%hu.%hu.%hu/%d#%d",
323 subnet->net.ipv4.address.x[0],
324 subnet->net.ipv4.address.x[1],
325 subnet->net.ipv4.address.x[2],
326 subnet->net.ipv4.address.x[3],
327 subnet->net.ipv4.prefixlength,
332 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
333 ntohs(subnet->net.ipv6.address.x[0]),
334 ntohs(subnet->net.ipv6.address.x[1]),
335 ntohs(subnet->net.ipv6.address.x[2]),
336 ntohs(subnet->net.ipv6.address.x[3]),
337 ntohs(subnet->net.ipv6.address.x[4]),
338 ntohs(subnet->net.ipv6.address.x[5]),
339 ntohs(subnet->net.ipv6.address.x[6]),
340 ntohs(subnet->net.ipv6.address.x[7]),
341 subnet->net.ipv6.prefixlength,
347 _("net2str() was called with unknown subnet type %d, exiting!"),
356 /* Subnet lookup routines */
358 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet)
362 return avl_search(owner->subnet_tree, subnet);
365 subnet_t *lookup_subnet_mac(const mac_t *address)
367 subnet_t *p, subnet = {0};
371 subnet.type = SUBNET_MAC;
372 subnet.net.mac.address = *address;
375 p = avl_search(subnet_tree, &subnet);
380 subnet_t *lookup_subnet_ipv4(const ipv4_t *address)
382 subnet_t *p, *r = NULL, subnet = {0};
388 // Check if this address is cached
390 for(i = 0; i < 2; i++) {
391 if(!cache_ipv4_valid[i])
393 if(!memcmp(address, &cache_ipv4_address[i], sizeof *address))
394 return cache_ipv4_subnet[i];
397 // Search all subnets for a matching one
399 subnet.type = SUBNET_IPV4;
400 subnet.net.ipv4.address = *address;
401 subnet.net.ipv4.prefixlength = 32;
404 for(n = subnet_tree->head; n; n = n->next) {
407 if(!p || p->type != subnet.type)
410 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
412 if(p->owner->status.reachable)
419 cache_ipv4_slot = !cache_ipv4_slot;
420 memcpy(&cache_ipv4_address[cache_ipv4_slot], address, sizeof *address);
421 cache_ipv4_subnet[cache_ipv4_slot] = r;
422 cache_ipv4_valid[cache_ipv4_slot] = true;
427 subnet_t *lookup_subnet_ipv6(const ipv6_t *address)
429 subnet_t *p, *r = NULL, subnet = {0};
435 // Check if this address is cached
437 for(i = 0; i < 2; i++) {
438 if(!cache_ipv6_valid[i])
440 if(!memcmp(address, &cache_ipv6_address[i], sizeof *address))
441 return cache_ipv6_subnet[i];
444 // Search all subnets for a matching one
446 subnet.type = SUBNET_IPV6;
447 subnet.net.ipv6.address = *address;
448 subnet.net.ipv6.prefixlength = 128;
451 for(n = subnet_tree->head; n; n = n->next) {
454 if(!p || p->type != subnet.type)
457 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
459 if(p->owner->status.reachable)
466 cache_ipv6_slot = !cache_ipv6_slot;
467 memcpy(&cache_ipv6_address[cache_ipv6_slot], address, sizeof *address);
468 cache_ipv6_subnet[cache_ipv6_slot] = r;
469 cache_ipv6_valid[cache_ipv6_slot] = true;
474 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
478 char netstr[MAXNETSTR];
479 char *name, *address, *port;
482 // Prepare environment variables to be passed to the script
484 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
485 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
486 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
487 xasprintf(&envp[3], "NODE=%s", owner->name);
489 if(owner != myself) {
490 sockaddr2str(&owner->address, &address, &port);
491 // 4 and 5 are reserved for SUBNET and WEIGHT
492 xasprintf(&envp[6], "REMOTEADDRESS=%s", address);
493 xasprintf(&envp[7], "REMOTEPORT=%s", port);
496 name = up ? "subnet-up" : "subnet-down";
499 for(node = owner->subnet_tree->head; node; node = node->next) {
501 if(!net2str(netstr, sizeof netstr, subnet))
503 // Strip the weight from the subnet, and put it in its own environment variable
504 char *weight = strchr(netstr + 7, '#');
510 // Prepare the SUBNET and WEIGHT variables
515 xasprintf(&envp[4], "SUBNET=%s", netstr);
516 xasprintf(&envp[5], "WEIGHT=%s", weight);
518 execute_script(name, envp);
521 if(net2str(netstr + 7, sizeof netstr - 7, subnet)) {
522 // Strip the weight from the subnet, and put it in its own environment variable
523 char *weight = strchr(netstr + 7, '#');
529 // Prepare the SUBNET and WEIGHT variables
530 xasprintf(&envp[4], "SUBNET=%s", netstr);
531 xasprintf(&envp[5], "WEIGHT=%s", weight);
533 execute_script(name, envp);
537 for(i = 0; envp[i] && i < 9; i++)
541 void dump_subnets(void)
543 char netstr[MAXNETSTR];
549 logger(LOG_DEBUG, _("Subnet list:"));
551 for(node = subnet_tree->head; node; node = node->next) {
553 if(!net2str(netstr, sizeof netstr, subnet))
555 logger(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
558 logger(LOG_DEBUG, _("End of subnet list."));