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"
36 /* lists type of subnet */
38 splay_tree_t subnet_tree = {
39 .compare = (splay_compare_t) subnet_compare,
40 .delete = (splay_action_t) free_subnet,
43 /* Subnet lookup cache */
45 static uint32_t wrapping_add32(uint32_t a, uint32_t b) {
46 return (uint32_t)((uint64_t)a + b);
49 static uint32_t wrapping_mul32(uint32_t a, uint32_t b) {
50 return (uint32_t)((uint64_t)a * b);
53 static uint32_t hash_function_ipv4_t(const ipv4_t *p) {
55 This basic hash works because
56 a) Most IPv4 networks routed via tinc are not /0
57 b) Most IPv4 networks have more unique low order bits
59 uint16_t *halfwidth = (uint16_t *)p;
60 uint32_t hash = hash_seed;
62 #if __BYTE_ORDER == __LITTLE_ENDIAN
64 hash = wrapping_add32(hash, wrapping_mul32(halfwidth[1], 0x9e370001U));
67 #if SUBNET_HASH_SIZE >= 0x10000
68 return hash ^ halfwidth[0];
70 // ensure that we have a /24 with no collisions on 32bit
71 return hash ^ ntohs(halfwidth[0]);
72 #endif // _____LP64_____
75 hash = wrapping_add32(hash, wrapping_mul32(halfwidth[0], 0x9e370001U));
77 // x.x.0.[0-255] part (ntohs is nop on big endian)
78 return hash ^ halfwidth[1];
79 #endif // __BYTE_ORDER == __LITTLE_ENDIAN
83 static uint32_t hash_function_ipv6_t(const ipv6_t *p) {
84 uint32_t *fullwidth = (uint32_t *)p;
85 uint32_t hash = hash_seed;
87 for(int i = 0; i < 4; i++) {
88 hash = wrapping_add32(hash, fullwidth[i]);
89 hash = wrapping_mul32(hash, 0x9e370001U);
95 static uint32_t hash_function_mac_t(const mac_t *p) {
96 uint16_t *halfwidth = (uint16_t *)p;
97 uint32_t hash = hash_seed;
99 for(int i = 0; i < 3; i++) {
100 hash = wrapping_add32(hash, halfwidth[i]);
101 hash = wrapping_mul32(hash, 0x9e370001U);
107 hash_define(ipv4_t, SUBNET_HASH_SIZE)
108 hash_define(ipv6_t, SUBNET_HASH_SIZE)
109 hash_define(mac_t, SUBNET_HASH_SIZE)
111 hash_new(ipv4_t, ipv4_cache);
112 hash_new(ipv6_t, ipv6_cache);
113 hash_new(mac_t, mac_cache);
116 void subnet_cache_flush_table(subnet_type_t stype) {
117 // NOTE: a subnet type of SUBNET_TYPES can be used to clear all hash tables
119 if(stype != SUBNET_IPV6) { // ipv4
120 hash_clear(ipv4_t, &ipv4_cache);
123 if(stype != SUBNET_IPV4) { // ipv6
124 hash_clear(ipv6_t, &ipv6_cache);
127 hash_clear(mac_t, &mac_cache);
130 /* Initialising trees */
132 void init_subnets(void) {
133 hash_seed = prng(UINT32_MAX);
135 // tables need to be cleared on startup
136 subnet_cache_flush_tables();
139 void exit_subnets(void) {
140 splay_empty_tree(&subnet_tree);
141 subnet_cache_flush_tables();
144 void init_subnet_tree(splay_tree_t *tree) {
145 memset(tree, 0, sizeof(*tree));
146 tree->compare = (splay_compare_t) subnet_compare;
149 /* Allocating and freeing space for subnets */
151 subnet_t *new_subnet(void) {
152 return xzalloc(sizeof(subnet_t));
155 void free_subnet(subnet_t *subnet) {
159 void subnet_cache_flush_tables(void) {
160 // flushes all the tables
161 hash_clear(ipv4_t, &ipv4_cache);
162 hash_clear(ipv6_t, &ipv6_cache);
163 hash_clear(mac_t, &mac_cache);
166 static void subnet_cache_flush(subnet_t *subnet) {
167 switch(subnet->type) {
169 if(subnet->net.ipv4.prefixlength == 32) {
170 hash_delete(ipv4_t, &ipv4_cache, &subnet->net.ipv4.address);
177 if(subnet->net.ipv4.prefixlength == 128) {
178 hash_delete(ipv6_t, &ipv6_cache, &subnet->net.ipv6.address);
185 hash_delete(mac_t, &mac_cache, &subnet->net.mac.address);
189 subnet_cache_flush_table(subnet->type);
192 /* Adding and removing subnets */
194 void subnet_add(node_t *n, subnet_t *subnet) {
197 splay_insert(&subnet_tree, subnet);
200 splay_insert(&n->subnet_tree, subnet);
203 subnet_cache_flush(subnet);
206 void subnet_del(node_t *n, subnet_t *subnet) {
208 splay_delete(&n->subnet_tree, subnet);
211 splay_delete(&subnet_tree, subnet);
213 subnet_cache_flush(subnet);
216 /* Subnet lookup routines */
218 subnet_t *lookup_subnet(node_t *owner, const subnet_t *subnet) {
219 return splay_search(&owner->subnet_tree, subnet);
222 subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
225 // Check if this address is cached
227 if((r = hash_search(mac_t, &mac_cache, address))) {
231 // Search all subnets for a matching one
233 for splay_each(subnet_t, p, owner ? &owner->subnet_tree : &subnet_tree) {
234 if(!p || p->type != SUBNET_MAC) {
238 if(!memcmp(address, &p->net.mac.address, sizeof(*address))) {
241 if(!p->owner || p->owner->status.reachable) {
250 hash_insert(mac_t, &mac_cache, address, r);
256 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
259 // Check if this address is cached
261 if((r = hash_search(ipv4_t, &ipv4_cache, address))) {
265 // Search all subnets for a matching one
267 for splay_each(subnet_t, p, &subnet_tree) {
268 if(!p || p->type != SUBNET_IPV4) {
272 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
275 if(!p->owner || p->owner->status.reachable) {
284 hash_insert(ipv4_t, &ipv4_cache, address, r);
290 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
293 // Check if this address is cached
295 if((r = hash_search(ipv6_t, &ipv6_cache, address))) {
299 // Search all subnets for a matching one
301 for splay_each(subnet_t, p, &subnet_tree) {
302 if(!p || p->type != SUBNET_IPV6) {
306 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
309 if(!p->owner || p->owner->status.reachable) {
318 hash_insert(ipv6_t, &ipv6_cache, address, r);
324 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
325 if(!sandbox_can(START_PROCESSES, RIGHT_NOW)) {
329 char netstr[MAXNETSTR];
330 char *address, *port;
333 // Prepare environment variables to be passed to the script
336 environment_init(&env);
337 environment_add(&env, "NODE=%s", owner->name);
339 if(owner != myself) {
340 sockaddr2str(&owner->address, &address, &port);
341 environment_add(&env, "REMOTEADDRESS=%s", address);
342 environment_add(&env, "REMOTEPORT=%s", port);
347 int env_subnet = environment_add(&env, NULL);
348 int env_weight = environment_add(&env, NULL);
350 const char *name = up ? "subnet-up" : "subnet-down";
353 for splay_each(subnet_t, subnet, &owner->subnet_tree) {
354 if(!net2str(netstr, sizeof(netstr), subnet)) {
358 // Strip the weight from the subnet, and put it in its own environment variable
359 char *weight = strchr(netstr, '#');
367 // Prepare the SUBNET and WEIGHT variables
368 environment_update(&env, env_subnet, "SUBNET=%s", netstr);
369 environment_update(&env, env_weight, "WEIGHT=%s", weight);
371 execute_script(name, &env);
374 if(net2str(netstr, sizeof(netstr), subnet)) {
375 // Strip the weight from the subnet, and put it in its own environment variable
376 char *weight = strchr(netstr, '#');
384 // Prepare the SUBNET and WEIGHT variables
385 environment_update(&env, env_subnet, "SUBNET=%s", netstr);
386 environment_update(&env, env_weight, "WEIGHT=%s", weight);
388 execute_script(name, &env);
392 environment_exit(&env);
395 bool dump_subnets(connection_t *c) {
396 for splay_each(subnet_t, subnet, &subnet_tree) {
397 char netstr[MAXNETSTR];
399 if(!net2str(netstr, sizeof(netstr), subnet)) {
403 send_request(c, "%d %d %s %s",
404 CONTROL, REQ_DUMP_SUBNETS,
405 netstr, subnet->owner ? subnet->owner->name : "(broadcast)");
408 return send_request(c, "%d %d", CONTROL, REQ_DUMP_SUBNETS);