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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 /* lists type of subnet */
38 avl_tree_t *subnet_tree;
40 /* Subnet lookup cache */
42 static ipv4_t cache_ipv4_address[2];
43 static subnet_t *cache_ipv4_subnet[2];
44 static bool cache_ipv4_valid[2];
45 static int cache_ipv4_slot;
47 static ipv6_t cache_ipv6_address[2];
48 static subnet_t *cache_ipv6_subnet[2];
49 static bool cache_ipv6_valid[2];
50 static int cache_ipv6_slot;
52 void subnet_cache_flush() {
53 cache_ipv4_valid[0] = cache_ipv4_valid[1] = false;
54 cache_ipv6_valid[0] = cache_ipv6_valid[1] = false;
57 /* Subnet comparison */
59 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b)
63 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
68 result = a->weight - b->weight;
70 if(result || !a->owner || !b->owner)
73 return strcmp(a->owner->name, b->owner->name);
76 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b)
80 result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
85 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
90 result = a->weight - b->weight;
92 if(result || !a->owner || !b->owner)
95 return strcmp(a->owner->name, b->owner->name);
98 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b)
102 result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
107 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
112 result = a->weight - b->weight;
114 if(result || !a->owner || !b->owner)
117 return strcmp(a->owner->name, b->owner->name);
120 int subnet_compare(const subnet_t *a, const subnet_t *b)
124 result = a->type - b->type;
131 return subnet_compare_mac(a, b);
133 return subnet_compare_ipv4(a, b);
135 return subnet_compare_ipv6(a, b);
137 logger(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, exitting!"),
146 /* Initialising trees */
148 void init_subnets(void)
152 subnet_tree = avl_alloc_tree((avl_compare_t) subnet_compare, (avl_action_t) free_subnet);
154 subnet_cache_flush();
157 void exit_subnets(void)
161 avl_delete_tree(subnet_tree);
164 avl_tree_t *new_subnet_tree(void)
168 return avl_alloc_tree((avl_compare_t) subnet_compare, NULL);
171 void free_subnet_tree(avl_tree_t *subnet_tree)
175 avl_delete_tree(subnet_tree);
178 /* Allocating and freeing space for subnets */
180 subnet_t *new_subnet(void)
184 return xmalloc_and_zero(sizeof(subnet_t));
187 void free_subnet(subnet_t *subnet)
194 /* Adding and removing subnets */
196 void subnet_add(node_t *n, subnet_t *subnet)
202 avl_insert(subnet_tree, subnet);
203 avl_insert(n->subnet_tree, subnet);
205 subnet_cache_flush();
208 void subnet_del(node_t *n, subnet_t *subnet)
212 avl_delete(n->subnet_tree, subnet);
213 avl_delete(subnet_tree, subnet);
215 subnet_cache_flush();
218 /* Ascii representation of subnets */
220 bool str2net(subnet_t *subnet, const char *subnetstr)
228 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d#%d",
229 &x[0], &x[1], &x[2], &x[3], &l, &weight) >= 5) {
233 subnet->type = SUBNET_IPV4;
234 subnet->net.ipv4.prefixlength = l;
235 subnet->weight = weight;
237 for(i = 0; i < 4; i++) {
240 subnet->net.ipv4.address.x[i] = x[i];
246 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
247 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
252 subnet->type = SUBNET_IPV6;
253 subnet->net.ipv6.prefixlength = l;
254 subnet->weight = weight;
256 for(i = 0; i < 8; i++)
257 subnet->net.ipv6.address.x[i] = htons(x[i]);
262 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu#%d", &x[0], &x[1], &x[2], &x[3], &weight) >= 4) {
263 subnet->type = SUBNET_IPV4;
264 subnet->net.ipv4.prefixlength = 32;
265 subnet->weight = weight;
267 for(i = 0; i < 4; i++) {
270 subnet->net.ipv4.address.x[i] = x[i];
276 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx#%d",
277 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &weight) >= 8) {
278 subnet->type = SUBNET_IPV6;
279 subnet->net.ipv6.prefixlength = 128;
280 subnet->weight = weight;
282 for(i = 0; i < 8; i++)
283 subnet->net.ipv6.address.x[i] = htons(x[i]);
288 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
289 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &weight) >= 6) {
290 subnet->type = SUBNET_MAC;
291 subnet->weight = weight;
293 for(i = 0; i < 6; i++)
294 subnet->net.mac.address.x[i] = x[i];
302 bool net2str(char *netstr, int len, const subnet_t *subnet)
306 if(!netstr || !subnet) {
307 logger(LOG_ERR, _("net2str() was called with netstr=%p, subnet=%p!\n"), netstr, subnet);
311 switch (subnet->type) {
313 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
314 subnet->net.mac.address.x[0],
315 subnet->net.mac.address.x[1],
316 subnet->net.mac.address.x[2],
317 subnet->net.mac.address.x[3],
318 subnet->net.mac.address.x[4],
319 subnet->net.mac.address.x[5],
324 snprintf(netstr, len, "%hu.%hu.%hu.%hu/%d#%d",
325 subnet->net.ipv4.address.x[0],
326 subnet->net.ipv4.address.x[1],
327 subnet->net.ipv4.address.x[2],
328 subnet->net.ipv4.address.x[3],
329 subnet->net.ipv4.prefixlength,
334 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
335 ntohs(subnet->net.ipv6.address.x[0]),
336 ntohs(subnet->net.ipv6.address.x[1]),
337 ntohs(subnet->net.ipv6.address.x[2]),
338 ntohs(subnet->net.ipv6.address.x[3]),
339 ntohs(subnet->net.ipv6.address.x[4]),
340 ntohs(subnet->net.ipv6.address.x[5]),
341 ntohs(subnet->net.ipv6.address.x[6]),
342 ntohs(subnet->net.ipv6.address.x[7]),
343 subnet->net.ipv6.prefixlength,
349 _("net2str() was called with unknown subnet type %d, exiting!"),
358 /* Subnet lookup routines */
360 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet)
364 return avl_search(owner->subnet_tree, subnet);
367 subnet_t *lookup_subnet_mac(const mac_t *address)
369 subnet_t *p, subnet = {0};
373 subnet.type = SUBNET_MAC;
374 subnet.net.mac.address = *address;
377 p = avl_search(subnet_tree, &subnet);
382 subnet_t *lookup_subnet_ipv4(const ipv4_t *address)
384 subnet_t *p, *r = NULL, subnet = {0};
390 // Check if this address is cached
392 for(i = 0; i < 2; i++) {
393 if(!cache_ipv4_valid[i])
395 if(!memcmp(address, &cache_ipv4_address[i], sizeof *address))
396 return cache_ipv4_subnet[i];
399 // Search all subnets for a matching one
401 subnet.type = SUBNET_IPV4;
402 subnet.net.ipv4.address = *address;
403 subnet.net.ipv4.prefixlength = 32;
406 for(n = subnet_tree->head; n; n = n->next) {
409 if(!p || p->type != subnet.type)
412 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
414 if(p->owner->status.reachable)
421 cache_ipv4_slot = !cache_ipv4_slot;
422 memcpy(&cache_ipv4_address[cache_ipv4_slot], address, sizeof *address);
423 cache_ipv4_subnet[cache_ipv4_slot] = r;
424 cache_ipv4_valid[cache_ipv4_slot] = true;
429 subnet_t *lookup_subnet_ipv6(const ipv6_t *address)
431 subnet_t *p, *r = NULL, subnet = {0};
437 // Check if this address is cached
439 for(i = 0; i < 2; i++) {
440 if(!cache_ipv6_valid[i])
442 if(!memcmp(address, &cache_ipv6_address[i], sizeof *address))
443 return cache_ipv6_subnet[i];
446 // Search all subnets for a matching one
448 subnet.type = SUBNET_IPV6;
449 subnet.net.ipv6.address = *address;
450 subnet.net.ipv6.prefixlength = 128;
453 for(n = subnet_tree->head; n; n = n->next) {
456 if(!p || p->type != subnet.type)
459 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
461 if(p->owner->status.reachable)
468 cache_ipv6_slot = !cache_ipv6_slot;
469 memcpy(&cache_ipv6_address[cache_ipv6_slot], address, sizeof *address);
470 cache_ipv6_subnet[cache_ipv6_slot] = r;
471 cache_ipv6_valid[cache_ipv6_slot] = true;
476 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
480 char netstr[MAXNETSTR + 7] = "SUBNET=";
481 char *name, *address, *port;
483 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
484 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
485 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
486 xasprintf(&envp[3], "NODE=%s", owner->name);
488 if(owner != myself) {
489 sockaddr2str(&owner->address, &address, &port);
490 xasprintf(&envp[4], "REMOTEADDRESS=%s", address);
491 xasprintf(&envp[5], "REMOTEPORT=%s", port);
499 name = up ? "subnet-up" : "subnet-down";
502 for(node = owner->subnet_tree->head; node; node = node->next) {
504 if(!net2str(netstr + 7, sizeof netstr - 7, subnet))
506 execute_script(name, envp);
509 if(net2str(netstr + 7, sizeof netstr - 7, subnet))
510 execute_script(name, envp);
513 for(i = 0; i < (owner != myself ? 6 : 4); i++)
516 if(owner != myself) {
522 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."));