CI: add libgcrypt to sanitizer run
[tinc] / src / subnet.c
1 /*
2     subnet.c -- handle subnet lookups and lists
3     Copyright (C) 2000-2022 Guus Sliepen <guus@tinc-vpn.org>,
4                   2000-2005 Ivo Timmermans
5
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.
10
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.
15
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.
19 */
20
21 #include "system.h"
22
23 #include "splay_tree.h"
24 #include "control_common.h"
25 #include "crypto.h"
26 #include "hash.h"
27 #include "logger.h"
28 #include "net.h"
29 #include "netutl.h"
30 #include "node.h"
31 #include "script.h"
32 #include "subnet.h"
33 #include "xalloc.h"
34
35 /* lists type of subnet */
36 uint32_t hash_seed;
37 splay_tree_t subnet_tree = {
38         .compare = (splay_compare_t) subnet_compare,
39         .delete = (splay_action_t) free_subnet,
40 };
41
42 /* Subnet lookup cache */
43
44 static uint32_t wrapping_add32(uint32_t a, uint32_t b) {
45         return (uint32_t)((uint64_t)a + b);
46 }
47
48 static uint32_t wrapping_mul32(uint32_t a, uint32_t b) {
49         return (uint32_t)((uint64_t)a * b);
50 }
51
52 static uint32_t hash_function_ipv4_t(const ipv4_t *p) {
53         /*
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
57         */
58         uint16_t *halfwidth = (uint16_t *)p;
59         uint32_t hash = hash_seed;
60
61 #if __BYTE_ORDER == __LITTLE_ENDIAN
62         // 10.0.x.x/16 part
63         hash = wrapping_add32(hash, wrapping_mul32(halfwidth[1], 0x9e370001U));
64
65         // x.x.0.[0-255] part
66 #if SUBNET_HASH_SIZE >= 0x10000
67         return hash ^ halfwidth[0];
68 #else
69         // ensure that we have a /24 with no collisions on 32bit
70         return hash ^ ntohs(halfwidth[0]);
71 #endif // _____LP64_____
72 #else
73         // 10.0.x.x/16 part
74         hash = wrapping_add32(hash, wrapping_mul32(halfwidth[0], 0x9e370001U));
75
76         // x.x.0.[0-255] part (ntohs is nop on big endian)
77         return hash ^ halfwidth[1];
78 #endif // __BYTE_ORDER == __LITTLE_ENDIAN
79 }
80
81
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;
85
86         for(int i = 0; i < 4; i++) {
87                 hash = wrapping_add32(hash, fullwidth[i]);
88                 hash = wrapping_mul32(hash, 0x9e370001U);
89         }
90
91         return hash;
92 }
93
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;
97
98         for(int i = 0; i < 3; i++) {
99                 hash = wrapping_add32(hash, halfwidth[i]);
100                 hash = wrapping_mul32(hash, 0x9e370001U);
101         }
102
103         return hash;
104 }
105
106 hash_define(ipv4_t, SUBNET_HASH_SIZE)
107 hash_define(ipv6_t, SUBNET_HASH_SIZE)
108 hash_define(mac_t, SUBNET_HASH_SIZE)
109
110 hash_new(ipv4_t, ipv4_cache);
111 hash_new(ipv6_t, ipv6_cache);
112 hash_new(mac_t, mac_cache);
113
114
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
117
118         if(stype != SUBNET_IPV6) { // ipv4
119                 hash_clear(ipv4_t, &ipv4_cache);
120         }
121
122         if(stype != SUBNET_IPV4) { // ipv6
123                 hash_clear(ipv6_t, &ipv6_cache);
124         }
125
126         hash_clear(mac_t, &mac_cache);
127 }
128
129 /* Initialising trees */
130
131 void init_subnets(void) {
132         hash_seed = prng(UINT32_MAX);
133
134         // tables need to be cleared on startup
135         subnet_cache_flush_tables();
136 }
137
138 void exit_subnets(void) {
139         splay_empty_tree(&subnet_tree);
140         subnet_cache_flush_tables();
141 }
142
143 void init_subnet_tree(splay_tree_t *tree) {
144         memset(tree, 0, sizeof(*tree));
145         tree->compare = (splay_compare_t) subnet_compare;
146 }
147
148 /* Allocating and freeing space for subnets */
149
150 subnet_t *new_subnet(void) {
151         return xzalloc(sizeof(subnet_t));
152 }
153
154 void free_subnet(subnet_t *subnet) {
155         free(subnet);
156 }
157
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);
163 }
164
165 static void subnet_cache_flush(subnet_t *subnet) {
166         switch(subnet->type) {
167         case SUBNET_IPV4:
168                 if(subnet->net.ipv4.prefixlength == 32) {
169                         hash_delete(ipv4_t, &ipv4_cache, &subnet->net.ipv4.address);
170                         return;
171                 }
172
173                 break;
174
175         case SUBNET_IPV6:
176                 if(subnet->net.ipv4.prefixlength == 128) {
177                         hash_delete(ipv6_t, &ipv6_cache, &subnet->net.ipv6.address);
178                         return;
179                 }
180
181                 break;
182
183         case SUBNET_MAC:
184                 hash_delete(mac_t, &mac_cache, &subnet->net.mac.address);
185                 return;
186         }
187
188         subnet_cache_flush_table(subnet->type);
189 }
190
191 /* Adding and removing subnets */
192
193 void subnet_add(node_t *n, subnet_t *subnet) {
194         subnet->owner = n;
195
196         splay_insert(&subnet_tree, subnet);
197
198         if(n) {
199                 splay_insert(&n->subnet_tree, subnet);
200         }
201
202         subnet_cache_flush(subnet);
203 }
204
205 void subnet_del(node_t *n, subnet_t *subnet) {
206         if(n) {
207                 splay_delete(&n->subnet_tree, subnet);
208         }
209
210         splay_delete(&subnet_tree, subnet);
211
212         subnet_cache_flush(subnet);
213 }
214
215 /* Subnet lookup routines */
216
217 subnet_t *lookup_subnet(node_t *owner, const subnet_t *subnet) {
218         return splay_search(&owner->subnet_tree, subnet);
219 }
220
221 subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
222         subnet_t *r = NULL;
223
224         // Check if this address is cached
225
226         if((r = hash_search(mac_t, &mac_cache, address))) {
227                 return r;
228         }
229
230         // Search all subnets for a matching one
231
232         for splay_each(subnet_t, p, owner ? &owner->subnet_tree : &subnet_tree) {
233                 if(!p || p->type != SUBNET_MAC) {
234                         continue;
235                 }
236
237                 if(!memcmp(address, &p->net.mac.address, sizeof(*address))) {
238                         r = p;
239
240                         if(!p->owner || p->owner->status.reachable) {
241                                 break;
242                         }
243                 }
244         }
245
246         // Cache the result
247
248         if(r) {
249                 hash_insert(mac_t, &mac_cache, address, r);
250         }
251
252         return r;
253 }
254
255 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
256         subnet_t *r = NULL;
257
258         // Check if this address is cached
259
260         if((r = hash_search(ipv4_t, &ipv4_cache, address))) {
261                 return r;
262         }
263
264         // Search all subnets for a matching one
265
266         for splay_each(subnet_t, p, &subnet_tree) {
267                 if(!p || p->type != SUBNET_IPV4) {
268                         continue;
269                 }
270
271                 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
272                         r = p;
273
274                         if(!p->owner || p->owner->status.reachable) {
275                                 break;
276                         }
277                 }
278         }
279
280         // Cache the result
281
282         if(r) {
283                 hash_insert(ipv4_t, &ipv4_cache, address, r);
284         }
285
286         return r;
287 }
288
289 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
290         subnet_t *r = NULL;
291
292         // Check if this address is cached
293
294         if((r = hash_search(ipv6_t, &ipv6_cache, address))) {
295                 return r;
296         }
297
298         // Search all subnets for a matching one
299
300         for splay_each(subnet_t, p, &subnet_tree) {
301                 if(!p || p->type != SUBNET_IPV6) {
302                         continue;
303                 }
304
305                 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
306                         r = p;
307
308                         if(!p->owner || p->owner->status.reachable) {
309                                 break;
310                         }
311                 }
312         }
313
314         // Cache the result
315
316         if(r) {
317                 hash_insert(ipv6_t, &ipv6_cache, address, r);
318         }
319
320         return r;
321 }
322
323 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
324         char netstr[MAXNETSTR];
325         char *address, *port;
326         char empty[] = "";
327
328         // Prepare environment variables to be passed to the script
329
330         environment_t env;
331         environment_init(&env);
332         environment_add(&env, "NODE=%s", owner->name);
333
334         if(owner != myself) {
335                 sockaddr2str(&owner->address, &address, &port);
336                 environment_add(&env, "REMOTEADDRESS=%s", address);
337                 environment_add(&env, "REMOTEPORT=%s", port);
338                 free(port);
339                 free(address);
340         }
341
342         int env_subnet = environment_add(&env, NULL);
343         int env_weight = environment_add(&env, NULL);
344
345         const char *name = up ? "subnet-up" : "subnet-down";
346
347         if(!subnet) {
348                 for splay_each(subnet_t, subnet, &owner->subnet_tree) {
349                         if(!net2str(netstr, sizeof(netstr), subnet)) {
350                                 continue;
351                         }
352
353                         // Strip the weight from the subnet, and put it in its own environment variable
354                         char *weight = strchr(netstr, '#');
355
356                         if(weight) {
357                                 *weight++ = 0;
358                         } else {
359                                 weight = empty;
360                         }
361
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);
365
366                         execute_script(name, &env);
367                 }
368         } else {
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, '#');
372
373                         if(weight) {
374                                 *weight++ = 0;
375                         } else {
376                                 weight = empty;
377                         }
378
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);
382
383                         execute_script(name, &env);
384                 }
385         }
386
387         environment_exit(&env);
388 }
389
390 bool dump_subnets(connection_t *c) {
391         for splay_each(subnet_t, subnet, &subnet_tree) {
392                 char netstr[MAXNETSTR];
393
394                 if(!net2str(netstr, sizeof(netstr), subnet)) {
395                         continue;
396                 }
397
398                 send_request(c, "%d %d %s %s",
399                              CONTROL, REQ_DUMP_SUBNETS,
400                              netstr, subnet->owner ? subnet->owner->name : "(broadcast)");
401         }
402
403         return send_request(c, "%d %d", CONTROL, REQ_DUMP_SUBNETS);
404 }