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