Move all functions related to subnet parsing to subnet_parse.c.
[tinc] / src / subnet.c
1 /*
2     subnet.c -- handle subnet lookups and lists
3     Copyright (C) 2000-2012 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 "device.h"
26 #include "logger.h"
27 #include "net.h"
28 #include "netutl.h"
29 #include "node.h"
30 #include "process.h"
31 #include "subnet.h"
32 #include "utils.h"
33 #include "xalloc.h"
34
35 /* lists type of subnet */
36
37 splay_tree_t *subnet_tree;
38
39 /* Subnet lookup cache */
40
41 static ipv4_t cache_ipv4_address[2];
42 static subnet_t *cache_ipv4_subnet[2];
43 static bool cache_ipv4_valid[2];
44 static int cache_ipv4_slot;
45
46 static ipv6_t cache_ipv6_address[2];
47 static subnet_t *cache_ipv6_subnet[2];
48 static bool cache_ipv6_valid[2];
49 static int cache_ipv6_slot;
50
51 static mac_t cache_mac_address[2];
52 static subnet_t *cache_mac_subnet[2];
53 static bool cache_mac_valid[2];
54 static int cache_mac_slot;
55
56 void subnet_cache_flush(void) {
57         cache_ipv4_valid[0] = cache_ipv4_valid[1] = false;
58         cache_ipv6_valid[0] = cache_ipv6_valid[1] = false;
59         cache_mac_valid[0] = cache_mac_valid[1] = false;
60 }
61
62 /* Initialising trees */
63
64 void init_subnets(void) {
65         subnet_tree = splay_alloc_tree((splay_compare_t) subnet_compare, (splay_action_t) free_subnet);
66
67         subnet_cache_flush();
68 }
69
70 void exit_subnets(void) {
71         splay_delete_tree(subnet_tree);
72 }
73
74 splay_tree_t *new_subnet_tree(void) {
75         return splay_alloc_tree((splay_compare_t) subnet_compare, NULL);
76 }
77
78 void free_subnet_tree(splay_tree_t *subnet_tree) {
79         splay_delete_tree(subnet_tree);
80 }
81
82 /* Allocating and freeing space for subnets */
83
84 subnet_t *new_subnet(void) {
85         return xmalloc_and_zero(sizeof(subnet_t));
86 }
87
88 void free_subnet(subnet_t *subnet) {
89         free(subnet);
90 }
91
92 /* Adding and removing subnets */
93
94 void subnet_add(node_t *n, subnet_t *subnet) {
95         subnet->owner = n;
96
97         splay_insert(subnet_tree, subnet);
98         splay_insert(n->subnet_tree, subnet);
99
100         subnet_cache_flush();
101 }
102
103 void subnet_del(node_t *n, subnet_t *subnet) {
104         splay_delete(n->subnet_tree, subnet);
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 *p, *r = NULL;
118         splay_node_t *n;
119         int i;
120
121         // Check if this address is cached
122
123         for(i = 0; i < 2; i++) {
124                 if(!cache_mac_valid[i])
125                         continue;
126                 if(owner && cache_mac_subnet[i] && cache_mac_subnet[i]->owner != owner)
127                         continue;
128                 if(!memcmp(address, &cache_mac_address[i], sizeof *address))
129                         return cache_mac_subnet[i];
130         }
131
132         // Search all subnets for a matching one
133
134         for(n = owner ? owner->subnet_tree->head : subnet_tree->head; n; n = n->next) {
135                 p = n->data;
136                 
137                 if(!p || p->type != SUBNET_MAC)
138                         continue;
139
140                 if(!memcmp(address, &p->net.mac.address, sizeof *address)) {
141                         r = p;
142                         if(p->owner->status.reachable)
143                                 break;
144                 }
145         }
146
147         // Cache the result
148
149         cache_mac_slot = !cache_mac_slot;
150         memcpy(&cache_mac_address[cache_mac_slot], address, sizeof *address);
151         cache_mac_subnet[cache_mac_slot] = r;
152         cache_mac_valid[cache_mac_slot] = true;
153
154         return r;
155 }
156
157 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
158         subnet_t *p, *r = NULL;
159         splay_node_t *n;
160         int i;
161
162         // Check if this address is cached
163
164         for(i = 0; i < 2; i++) {
165                 if(!cache_ipv4_valid[i])
166                         continue;
167                 if(!memcmp(address, &cache_ipv4_address[i], sizeof *address))
168                         return cache_ipv4_subnet[i];
169         }
170
171         // Search all subnets for a matching one
172
173         for(n = subnet_tree->head; n; n = n->next) {
174                 p = n->data;
175                 
176                 if(!p || p->type != SUBNET_IPV4)
177                         continue;
178
179                 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
180                         r = p;
181                         if(p->owner->status.reachable)
182                                 break;
183                 }
184         }
185
186         // Cache the result
187
188         cache_ipv4_slot = !cache_ipv4_slot;
189         memcpy(&cache_ipv4_address[cache_ipv4_slot], address, sizeof *address);
190         cache_ipv4_subnet[cache_ipv4_slot] = r;
191         cache_ipv4_valid[cache_ipv4_slot] = true;
192
193         return r;
194 }
195
196 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
197         subnet_t *p, *r = NULL;
198         splay_node_t *n;
199         int i;
200
201         // Check if this address is cached
202
203         for(i = 0; i < 2; i++) {
204                 if(!cache_ipv6_valid[i])
205                         continue;
206                 if(!memcmp(address, &cache_ipv6_address[i], sizeof *address))
207                         return cache_ipv6_subnet[i];
208         }
209
210         // Search all subnets for a matching one
211
212         for(n = subnet_tree->head; n; n = n->next) {
213                 p = n->data;
214                 
215                 if(!p || p->type != SUBNET_IPV6)
216                         continue;
217
218                 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
219                         r = p;
220                         if(p->owner->status.reachable)
221                                 break;
222                 }
223         }
224
225         // Cache the result
226
227         cache_ipv6_slot = !cache_ipv6_slot;
228         memcpy(&cache_ipv6_address[cache_ipv6_slot], address, sizeof *address);
229         cache_ipv6_subnet[cache_ipv6_slot] = r;
230         cache_ipv6_valid[cache_ipv6_slot] = true;
231
232         return r;
233 }
234
235 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
236         splay_node_t *node;
237         int i;
238         char *envp[9] = {NULL};
239         char netstr[MAXNETSTR];
240         char *name, *address, *port;
241         char empty[] = "";
242
243         // Prepare environment variables to be passed to the script
244
245         xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
246         xasprintf(&envp[1], "DEVICE=%s", device ? : "");
247         xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
248         xasprintf(&envp[3], "NODE=%s", owner->name);
249
250         if(owner != myself) {
251                 sockaddr2str(&owner->address, &address, &port);
252                 // 4 and 5 are reserved for SUBNET and WEIGHT
253                 xasprintf(&envp[6], "REMOTEADDRESS=%s", address);
254                 xasprintf(&envp[7], "REMOTEPORT=%s", port);
255                 free(port);
256                 free(address);
257         }
258
259         name = up ? "subnet-up" : "subnet-down";
260
261         if(!subnet) {
262                 for(node = owner->subnet_tree->head; node; node = node->next) {
263                         subnet = node->data;
264                         if(!net2str(netstr, sizeof netstr, subnet))
265                                 continue;
266                         // Strip the weight from the subnet, and put it in its own environment variable
267                         char *weight = strchr(netstr, '#');
268                         if(weight)
269                                 *weight++ = 0;
270                         else
271                                 weight = empty;
272
273                         // Prepare the SUBNET and WEIGHT variables
274                         if(envp[4])
275                                 free(envp[4]);
276                         if(envp[5])
277                                 free(envp[5]);
278                         xasprintf(&envp[4], "SUBNET=%s", netstr);
279                         xasprintf(&envp[5], "WEIGHT=%s", weight);
280
281                         execute_script(name, envp);
282                 }
283         } else {
284                 if(net2str(netstr, sizeof netstr, subnet)) {
285                         // Strip the weight from the subnet, and put it in its own environment variable
286                         char *weight = strchr(netstr, '#');
287                         if(weight)
288                                 *weight++ = 0;
289                         else
290                                 weight = empty;
291
292                         // Prepare the SUBNET and WEIGHT variables
293                         xasprintf(&envp[4], "SUBNET=%s", netstr);
294                         xasprintf(&envp[5], "WEIGHT=%s", weight);
295
296                         execute_script(name, envp);
297                 }
298         }
299
300         for(i = 0; envp[i] && i < 8; i++)
301                 free(envp[i]);
302 }
303
304 bool dump_subnets(connection_t *c) {
305         char netstr[MAXNETSTR];
306         subnet_t *subnet;
307         splay_node_t *node;
308
309         for(node = subnet_tree->head; node; node = node->next) {
310                 subnet = node->data;
311                 if(!net2str(netstr, sizeof netstr, subnet))
312                         continue;
313                 send_request(c, "%d %d %s owner %s",
314                                 CONTROL, REQ_DUMP_SUBNETS,
315                                 netstr, subnet->owner->name);
316         }
317
318         return send_request(c, "%d %d", CONTROL, REQ_DUMP_SUBNETS);
319 }