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