2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000-2022 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 = {
38 .compare = (splay_compare_t) subnet_compare,
39 .delete = (splay_action_t) free_subnet,
42 /* Subnet lookup cache */
44 static uint32_t wrapping_add32(uint32_t a, uint32_t b) {
45 return (uint32_t)((uint64_t)a + b);
48 static uint32_t wrapping_mul32(uint32_t a, uint32_t b) {
49 return (uint32_t)((uint64_t)a * b);
52 static uint32_t hash_function_ipv4_t(const ipv4_t *p) {
54 This basic hash works because
55 a) Most IPv4 networks routed via tinc are not /0
56 b) Most IPv4 networks have more unique low order bits
58 uint16_t *halfwidth = (uint16_t *)p;
59 uint32_t hash = hash_seed;
61 #if __BYTE_ORDER == __LITTLE_ENDIAN
63 hash = wrapping_add32(hash, wrapping_mul32(halfwidth[1], 0x9e370001U));
66 #if SUBNET_HASH_SIZE >= 0x10000
67 return hash ^ halfwidth[0];
69 // ensure that we have a /24 with no collisions on 32bit
70 return hash ^ ntohs(halfwidth[0]);
71 #endif // _____LP64_____
74 hash = wrapping_add32(hash, wrapping_mul32(halfwidth[0], 0x9e370001U));
76 // x.x.0.[0-255] part (ntohs is nop on big endian)
77 return hash ^ halfwidth[1];
78 #endif // __BYTE_ORDER == __LITTLE_ENDIAN
82 static uint32_t hash_function_ipv6_t(const ipv6_t *p) {
83 uint32_t *fullwidth = (uint32_t *)p;
84 uint32_t hash = hash_seed;
86 for(int i = 0; i < 4; i++) {
87 hash = wrapping_add32(hash, fullwidth[i]);
88 hash = wrapping_mul32(hash, 0x9e370001U);
94 static uint32_t hash_function_mac_t(const mac_t *p) {
95 uint16_t *halfwidth = (uint16_t *)p;
96 uint32_t hash = hash_seed;
98 for(int i = 0; i < 3; i++) {
99 hash = wrapping_add32(hash, halfwidth[i]);
100 hash = wrapping_mul32(hash, 0x9e370001U);
106 hash_define(ipv4_t, SUBNET_HASH_SIZE)
107 hash_define(ipv6_t, SUBNET_HASH_SIZE)
108 hash_define(mac_t, SUBNET_HASH_SIZE)
110 hash_new(ipv4_t, ipv4_cache);
111 hash_new(ipv6_t, ipv6_cache);
112 hash_new(mac_t, mac_cache);
115 void subnet_cache_flush_table(subnet_type_t stype) {
116 // NOTE: a subnet type of SUBNET_TYPES can be used to clear all hash tables
118 if(stype != SUBNET_IPV6) { // ipv4
119 hash_clear(ipv4_t, &ipv4_cache);
122 if(stype != SUBNET_IPV4) { // ipv6
123 hash_clear(ipv6_t, &ipv6_cache);
126 hash_clear(mac_t, &mac_cache);
129 /* Initialising trees */
131 void init_subnets(void) {
132 hash_seed = prng(UINT32_MAX);
134 // tables need to be cleared on startup
135 subnet_cache_flush_tables();
138 void exit_subnets(void) {
139 splay_empty_tree(&subnet_tree);
140 subnet_cache_flush_tables();
143 void init_subnet_tree(splay_tree_t *tree) {
144 memset(tree, 0, sizeof(*tree));
145 tree->compare = (splay_compare_t) subnet_compare;
148 /* Allocating and freeing space for subnets */
150 subnet_t *new_subnet(void) {
151 return xzalloc(sizeof(subnet_t));
154 void free_subnet(subnet_t *subnet) {
158 void subnet_cache_flush_tables(void) {
159 // flushes all the tables
160 hash_clear(ipv4_t, &ipv4_cache);
161 hash_clear(ipv6_t, &ipv6_cache);
162 hash_clear(mac_t, &mac_cache);
165 static void subnet_cache_flush(subnet_t *subnet) {
166 switch(subnet->type) {
168 if(subnet->net.ipv4.prefixlength == 32) {
169 hash_delete(ipv4_t, &ipv4_cache, &subnet->net.ipv4.address);
176 if(subnet->net.ipv4.prefixlength == 128) {
177 hash_delete(ipv6_t, &ipv6_cache, &subnet->net.ipv6.address);
184 hash_delete(mac_t, &mac_cache, &subnet->net.mac.address);
188 subnet_cache_flush_table(subnet->type);
191 /* Adding and removing subnets */
193 void subnet_add(node_t *n, subnet_t *subnet) {
196 splay_insert(&subnet_tree, subnet);
199 splay_insert(&n->subnet_tree, subnet);
202 subnet_cache_flush(subnet);
205 void subnet_del(node_t *n, subnet_t *subnet) {
207 splay_delete(&n->subnet_tree, subnet);
210 splay_delete(&subnet_tree, subnet);
212 subnet_cache_flush(subnet);
215 /* Subnet lookup routines */
217 subnet_t *lookup_subnet(node_t *owner, const subnet_t *subnet) {
218 return splay_search(&owner->subnet_tree, subnet);
221 subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
224 // Check if this address is cached
226 if((r = hash_search(mac_t, &mac_cache, address))) {
230 // Search all subnets for a matching one
232 for splay_each(subnet_t, p, owner ? &owner->subnet_tree : &subnet_tree) {
233 if(!p || p->type != SUBNET_MAC) {
237 if(!memcmp(address, &p->net.mac.address, sizeof(*address))) {
240 if(!p->owner || p->owner->status.reachable) {
249 hash_insert(mac_t, &mac_cache, address, r);
255 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
258 // Check if this address is cached
260 if((r = hash_search(ipv4_t, &ipv4_cache, address))) {
264 // Search all subnets for a matching one
266 for splay_each(subnet_t, p, &subnet_tree) {
267 if(!p || p->type != SUBNET_IPV4) {
271 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
274 if(!p->owner || p->owner->status.reachable) {
283 hash_insert(ipv4_t, &ipv4_cache, address, r);
289 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
292 // Check if this address is cached
294 if((r = hash_search(ipv6_t, &ipv6_cache, address))) {
298 // Search all subnets for a matching one
300 for splay_each(subnet_t, p, &subnet_tree) {
301 if(!p || p->type != SUBNET_IPV6) {
305 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
308 if(!p->owner || p->owner->status.reachable) {
317 hash_insert(ipv6_t, &ipv6_cache, address, r);
323 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
324 char netstr[MAXNETSTR];
325 char *address, *port;
328 // Prepare environment variables to be passed to the script
331 environment_init(&env);
332 environment_add(&env, "NODE=%s", owner->name);
334 if(owner != myself) {
335 sockaddr2str(&owner->address, &address, &port);
336 environment_add(&env, "REMOTEADDRESS=%s", address);
337 environment_add(&env, "REMOTEPORT=%s", port);
342 int env_subnet = environment_add(&env, NULL);
343 int env_weight = environment_add(&env, NULL);
345 const char *name = up ? "subnet-up" : "subnet-down";
348 for splay_each(subnet_t, subnet, &owner->subnet_tree) {
349 if(!net2str(netstr, sizeof(netstr), subnet)) {
353 // Strip the weight from the subnet, and put it in its own environment variable
354 char *weight = strchr(netstr, '#');
362 // Prepare the SUBNET and WEIGHT variables
363 environment_update(&env, env_subnet, "SUBNET=%s", netstr);
364 environment_update(&env, env_weight, "WEIGHT=%s", weight);
366 execute_script(name, &env);
369 if(net2str(netstr, sizeof(netstr), subnet)) {
370 // Strip the weight from the subnet, and put it in its own environment variable
371 char *weight = strchr(netstr, '#');
379 // Prepare the SUBNET and WEIGHT variables
380 environment_update(&env, env_subnet, "SUBNET=%s", netstr);
381 environment_update(&env, env_weight, "WEIGHT=%s", weight);
383 execute_script(name, &env);
387 environment_exit(&env);
390 bool dump_subnets(connection_t *c) {
391 for splay_each(subnet_t, subnet, &subnet_tree) {
392 char netstr[MAXNETSTR];
394 if(!net2str(netstr, sizeof(netstr), subnet)) {
398 send_request(c, "%d %d %s %s",
399 CONTROL, REQ_DUMP_SUBNETS,
400 netstr, subnet->owner ? subnet->owner->name : "(broadcast)");
403 return send_request(c, "%d %d", CONTROL, REQ_DUMP_SUBNETS);