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