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