13c4603018b74070fd7887fae502c2f8fb2da66c
[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
36 splay_tree_t *subnet_tree;
37
38 /* Subnet lookup cache */
39
40 static hash_t *ipv4_cache;
41 static hash_t *ipv6_cache;
42 static hash_t *mac_cache;
43
44 void subnet_cache_flush(void) {
45         hash_clear(ipv4_cache);
46         hash_clear(ipv6_cache);
47         hash_clear(mac_cache);
48 }
49
50 /* Initialising trees */
51
52 void init_subnets(void) {
53         subnet_tree = splay_alloc_tree((splay_compare_t) subnet_compare, (splay_action_t) free_subnet);
54
55         ipv4_cache = hash_alloc(0x100, sizeof(ipv4_t));
56         ipv6_cache = hash_alloc(0x100, sizeof(ipv6_t));
57         mac_cache = hash_alloc(0x100, sizeof(mac_t));
58 }
59
60 void exit_subnets(void) {
61         splay_delete_tree(subnet_tree);
62
63         hash_free(ipv4_cache);
64         hash_free(ipv6_cache);
65         hash_free(mac_cache);
66 }
67
68 splay_tree_t *new_subnet_tree(void) {
69         return splay_alloc_tree((splay_compare_t) subnet_compare, NULL);
70 }
71
72 void free_subnet_tree(splay_tree_t *subnet_tree) {
73         splay_delete_tree(subnet_tree);
74 }
75
76 /* Allocating and freeing space for subnets */
77
78 subnet_t *new_subnet(void) {
79         return xzalloc(sizeof(subnet_t));
80 }
81
82 void free_subnet(subnet_t *subnet) {
83         free(subnet);
84 }
85
86 /* Adding and removing subnets */
87
88 void subnet_add(node_t *n, subnet_t *subnet) {
89         subnet->owner = n;
90
91         splay_insert(subnet_tree, subnet);
92
93         if(n) {
94                 splay_insert(n->subnet_tree, subnet);
95         }
96
97         subnet_cache_flush();
98 }
99
100 void subnet_del(node_t *n, subnet_t *subnet) {
101         if(n) {
102                 splay_delete(n->subnet_tree, subnet);
103         }
104
105         splay_delete(subnet_tree, subnet);
106
107         subnet_cache_flush();
108 }
109
110 /* Subnet lookup routines */
111
112 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet) {
113         return splay_search(owner->subnet_tree, subnet);
114 }
115
116 subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
117         subnet_t *r = NULL;
118
119         // Check if this address is cached
120
121         if((r = hash_search(mac_cache, address))) {
122                 return r;
123         }
124
125         // Search all subnets for a matching one
126
127         for splay_each(subnet_t, p, owner ? owner->subnet_tree : subnet_tree) {
128                 if(!p || p->type != SUBNET_MAC) {
129                         continue;
130                 }
131
132                 if(!memcmp(address, &p->net.mac.address, sizeof(*address))) {
133                         r = p;
134
135                         if(!p->owner || p->owner->status.reachable) {
136                                 break;
137                         }
138                 }
139         }
140
141         // Cache the result
142
143         if(r) {
144                 hash_insert(mac_cache, address, r);
145         }
146
147         return r;
148 }
149
150 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
151         subnet_t *r = NULL;
152
153         // Check if this address is cached
154
155         if((r = hash_search(ipv4_cache, address))) {
156                 return r;
157         }
158
159         // Search all subnets for a matching one
160
161         for splay_each(subnet_t, p, subnet_tree) {
162                 if(!p || p->type != SUBNET_IPV4) {
163                         continue;
164                 }
165
166                 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
167                         r = p;
168
169                         if(!p->owner || p->owner->status.reachable) {
170                                 break;
171                         }
172                 }
173         }
174
175         // Cache the result
176
177         if(r) {
178                 hash_insert(ipv4_cache, address, r);
179         }
180
181         return r;
182 }
183
184 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
185         subnet_t *r = NULL;
186
187         // Check if this address is cached
188
189         if((r = hash_search(ipv6_cache, address))) {
190                 return r;
191         }
192
193         // Search all subnets for a matching one
194
195         for splay_each(subnet_t, p, subnet_tree) {
196                 if(!p || p->type != SUBNET_IPV6) {
197                         continue;
198                 }
199
200                 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
201                         r = p;
202
203                         if(!p->owner || p->owner->status.reachable) {
204                                 break;
205                         }
206                 }
207         }
208
209         // Cache the result
210
211         if(r) {
212                 hash_insert(ipv6_cache, address, r);
213         }
214
215         return r;
216 }
217
218 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
219         char netstr[MAXNETSTR];
220         char *name, *address, *port;
221         char empty[] = "";
222
223         // Prepare environment variables to be passed to the script
224
225         environment_t env;
226         environment_init(&env);
227         environment_add(&env, "NODE=%s", owner->name);
228
229         if(owner != myself) {
230                 sockaddr2str(&owner->address, &address, &port);
231                 environment_add(&env, "REMOTEADDRESS=%s", address);
232                 environment_add(&env, "REMOTEPORT=%s", port);
233                 free(port);
234                 free(address);
235         }
236
237         int env_subnet = environment_add(&env, NULL);
238         int env_weight = environment_add(&env, NULL);
239
240         name = up ? "subnet-up" : "subnet-down";
241
242         if(!subnet) {
243                 for splay_each(subnet_t, subnet, owner->subnet_tree) {
244                         if(!net2str(netstr, sizeof(netstr), subnet)) {
245                                 continue;
246                         }
247
248                         // Strip the weight from the subnet, and put it in its own environment variable
249                         char *weight = strchr(netstr, '#');
250
251                         if(weight) {
252                                 *weight++ = 0;
253                         } else {
254                                 weight = empty;
255                         }
256
257                         // Prepare the SUBNET and WEIGHT variables
258                         environment_update(&env, env_subnet, "SUBNET=%s", netstr);
259                         environment_update(&env, env_weight, "WEIGHT=%s", weight);
260
261                         execute_script(name, &env);
262                 }
263         } else {
264                 if(net2str(netstr, sizeof(netstr), subnet)) {
265                         // Strip the weight from the subnet, and put it in its own environment variable
266                         char *weight = strchr(netstr, '#');
267
268                         if(weight) {
269                                 *weight++ = 0;
270                         } else {
271                                 weight = empty;
272                         }
273
274                         // Prepare the SUBNET and WEIGHT variables
275                         environment_update(&env, env_subnet, "SUBNET=%s", netstr);
276                         environment_update(&env, env_weight, "WEIGHT=%s", weight);
277
278                         execute_script(name, &env);
279                 }
280         }
281
282         environment_exit(&env);
283 }
284
285 bool dump_subnets(connection_t *c) {
286         for splay_each(subnet_t, subnet, subnet_tree) {
287                 char netstr[MAXNETSTR];
288
289                 if(!net2str(netstr, sizeof(netstr), subnet)) {
290                         continue;
291                 }
292
293                 send_request(c, "%d %d %s %s",
294                              CONTROL, REQ_DUMP_SUBNETS,
295                              netstr, subnet->owner ? subnet->owner->name : "(broadcast)");
296         }
297
298         return send_request(c, "%d %d", CONTROL, REQ_DUMP_SUBNETS);
299 }