K&R style braces.
[oweals/tinc.git] / src / subnet.c
1 /*
2     subnet.c -- handle subnet lookups and lists
3     Copyright (C) 2000-2009 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 "avl_tree.h"
24 #include "device.h"
25 #include "logger.h"
26 #include "net.h"
27 #include "netutl.h"
28 #include "node.h"
29 #include "process.h"
30 #include "subnet.h"
31 #include "utils.h"
32 #include "xalloc.h"
33
34 /* lists type of subnet */
35
36 avl_tree_t *subnet_tree;
37
38 /* Subnet lookup cache */
39
40 static ipv4_t cache_ipv4_address[2];
41 static subnet_t *cache_ipv4_subnet[2];
42 static bool cache_ipv4_valid[2];
43 static int cache_ipv4_slot;
44
45 static ipv6_t cache_ipv6_address[2];
46 static subnet_t *cache_ipv6_subnet[2];
47 static bool cache_ipv6_valid[2];
48 static int cache_ipv6_slot;
49
50 void subnet_cache_flush() {
51         cache_ipv4_valid[0] = cache_ipv4_valid[1] = false;
52         cache_ipv6_valid[0] = cache_ipv6_valid[1] = false;
53 }
54
55 /* Subnet comparison */
56
57 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
58         int result;
59
60         result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
61
62         if(result)
63                 return result;
64         
65         result = a->weight - b->weight;
66
67         if(result || !a->owner || !b->owner)
68                 return result;
69
70         return strcmp(a->owner->name, b->owner->name);
71 }
72
73 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
74         int result;
75
76         result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
77
78         if(result)
79                 return result;
80
81         result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
82
83         if(result)
84                 return result;
85         
86         result = a->weight - b->weight;
87
88         if(result || !a->owner || !b->owner)
89                 return result;
90
91         return strcmp(a->owner->name, b->owner->name);
92 }
93
94 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
95         int result;
96
97         result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
98
99         if(result)
100                 return result;
101         
102         result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
103
104         if(result)
105                 return result;
106         
107         result = a->weight - b->weight;
108
109         if(result || !a->owner || !b->owner)
110                 return result;
111
112         return strcmp(a->owner->name, b->owner->name);
113 }
114
115 int subnet_compare(const subnet_t *a, const subnet_t *b) {
116         int result;
117
118         result = a->type - b->type;
119
120         if(result)
121                 return result;
122
123         switch (a->type) {
124         case SUBNET_MAC:
125                 return subnet_compare_mac(a, b);
126         case SUBNET_IPV4:
127                 return subnet_compare_ipv4(a, b);
128         case SUBNET_IPV6:
129                 return subnet_compare_ipv6(a, b);
130         default:
131                 logger(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, exitting!"),
132                            a->type);
133                 cp_trace();
134                 exit(0);
135         }
136
137         return 0;
138 }
139
140 /* Initialising trees */
141
142 void init_subnets(void) {
143         cp();
144
145         subnet_tree = avl_alloc_tree((avl_compare_t) subnet_compare, (avl_action_t) free_subnet);
146
147         subnet_cache_flush();
148 }
149
150 void exit_subnets(void) {
151         cp();
152
153         avl_delete_tree(subnet_tree);
154 }
155
156 avl_tree_t *new_subnet_tree(void) {
157         cp();
158
159         return avl_alloc_tree((avl_compare_t) subnet_compare, NULL);
160 }
161
162 void free_subnet_tree(avl_tree_t *subnet_tree) {
163         cp();
164
165         avl_delete_tree(subnet_tree);
166 }
167
168 /* Allocating and freeing space for subnets */
169
170 subnet_t *new_subnet(void) {
171         cp();
172
173         return xmalloc_and_zero(sizeof(subnet_t));
174 }
175
176 void free_subnet(subnet_t *subnet) {
177         cp();
178
179         free(subnet);
180 }
181
182 /* Adding and removing subnets */
183
184 void subnet_add(node_t *n, subnet_t *subnet) {
185         cp();
186
187         subnet->owner = n;
188
189         avl_insert(subnet_tree, subnet);
190         avl_insert(n->subnet_tree, subnet);
191
192         subnet_cache_flush();
193 }
194
195 void subnet_del(node_t *n, subnet_t *subnet) {
196         cp();
197
198         avl_delete(n->subnet_tree, subnet);
199         avl_delete(subnet_tree, subnet);
200
201         subnet_cache_flush();
202 }
203
204 /* Ascii representation of subnets */
205
206 bool str2net(subnet_t *subnet, const char *subnetstr) {
207         int i, l;
208         uint16_t x[8];
209         int weight = 10;
210
211         cp();
212
213         if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d#%d",
214                           &x[0], &x[1], &x[2], &x[3], &l, &weight) >= 5) {
215                 if(l < 0 || l > 32)
216                         return false;
217
218                 subnet->type = SUBNET_IPV4;
219                 subnet->net.ipv4.prefixlength = l;
220                 subnet->weight = weight;
221
222                 for(i = 0; i < 4; i++) {
223                         if(x[i] > 255)
224                                 return false;
225                         subnet->net.ipv4.address.x[i] = x[i];
226                 }
227
228                 return true;
229         }
230
231         if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
232                           &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
233                           &l, &weight) >= 9) {
234                 if(l < 0 || l > 128)
235                         return false;
236
237                 subnet->type = SUBNET_IPV6;
238                 subnet->net.ipv6.prefixlength = l;
239                 subnet->weight = weight;
240
241                 for(i = 0; i < 8; i++)
242                         subnet->net.ipv6.address.x[i] = htons(x[i]);
243
244                 return true;
245         }
246
247         if(sscanf(subnetstr, "%hu.%hu.%hu.%hu#%d", &x[0], &x[1], &x[2], &x[3], &weight) >= 4) {
248                 subnet->type = SUBNET_IPV4;
249                 subnet->net.ipv4.prefixlength = 32;
250                 subnet->weight = weight;
251
252                 for(i = 0; i < 4; i++) {
253                         if(x[i] > 255)
254                                 return false;
255                         subnet->net.ipv4.address.x[i] = x[i];
256                 }
257
258                 return true;
259         }
260
261         if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx#%d",
262                           &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &weight) >= 8) {
263                 subnet->type = SUBNET_IPV6;
264                 subnet->net.ipv6.prefixlength = 128;
265                 subnet->weight = weight;
266
267                 for(i = 0; i < 8; i++)
268                         subnet->net.ipv6.address.x[i] = htons(x[i]);
269
270                 return true;
271         }
272
273         if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
274                           &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &weight) >= 6) {
275                 subnet->type = SUBNET_MAC;
276                 subnet->weight = weight;
277
278                 for(i = 0; i < 6; i++)
279                         subnet->net.mac.address.x[i] = x[i];
280
281                 return true;
282         }
283
284         return false;
285 }
286
287 bool net2str(char *netstr, int len, const subnet_t *subnet) {
288         cp();
289
290         if(!netstr || !subnet) {
291                 logger(LOG_ERR, _("net2str() was called with netstr=%p, subnet=%p!\n"), netstr, subnet);
292                 return false;
293         }
294
295         switch (subnet->type) {
296                 case SUBNET_MAC:
297                         snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
298                                          subnet->net.mac.address.x[0],
299                                          subnet->net.mac.address.x[1],
300                                          subnet->net.mac.address.x[2],
301                                          subnet->net.mac.address.x[3],
302                                          subnet->net.mac.address.x[4],
303                                          subnet->net.mac.address.x[5],
304                                          subnet->weight);
305                         break;
306
307                 case SUBNET_IPV4:
308                         snprintf(netstr, len, "%hu.%hu.%hu.%hu/%d#%d",
309                                          subnet->net.ipv4.address.x[0],
310                                          subnet->net.ipv4.address.x[1],
311                                          subnet->net.ipv4.address.x[2],
312                                          subnet->net.ipv4.address.x[3],
313                                          subnet->net.ipv4.prefixlength,
314                                          subnet->weight);
315                         break;
316
317                 case SUBNET_IPV6:
318                         snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
319                                          ntohs(subnet->net.ipv6.address.x[0]),
320                                          ntohs(subnet->net.ipv6.address.x[1]),
321                                          ntohs(subnet->net.ipv6.address.x[2]),
322                                          ntohs(subnet->net.ipv6.address.x[3]),
323                                          ntohs(subnet->net.ipv6.address.x[4]),
324                                          ntohs(subnet->net.ipv6.address.x[5]),
325                                          ntohs(subnet->net.ipv6.address.x[6]),
326                                          ntohs(subnet->net.ipv6.address.x[7]),
327                                          subnet->net.ipv6.prefixlength,
328                                          subnet->weight);
329                         break;
330
331                 default:
332                         logger(LOG_ERR,
333                                    _("net2str() was called with unknown subnet type %d, exiting!"),
334                                    subnet->type);
335                         cp_trace();
336                         exit(0);
337         }
338
339         return true;
340 }
341
342 /* Subnet lookup routines */
343
344 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet) {
345         cp();
346
347         return avl_search(owner->subnet_tree, subnet);
348 }
349
350 subnet_t *lookup_subnet_mac(const mac_t *address) {
351         subnet_t *p, subnet = {0};
352
353         cp();
354
355         subnet.type = SUBNET_MAC;
356         subnet.net.mac.address = *address;
357         subnet.owner = NULL;
358
359         p = avl_search(subnet_tree, &subnet);
360
361         return p;
362 }
363
364 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
365         subnet_t *p, *r = NULL, subnet = {0};
366         avl_node_t *n;
367         int i;
368
369         cp();
370
371         // Check if this address is cached
372
373         for(i = 0; i < 2; i++) {
374                 if(!cache_ipv4_valid[i])
375                         continue;
376                 if(!memcmp(address, &cache_ipv4_address[i], sizeof *address))
377                         return cache_ipv4_subnet[i];
378         }
379
380         // Search all subnets for a matching one
381
382         subnet.type = SUBNET_IPV4;
383         subnet.net.ipv4.address = *address;
384         subnet.net.ipv4.prefixlength = 32;
385         subnet.owner = NULL;
386
387         for(n = subnet_tree->head; n; n = n->next) {
388                 p = n->data;
389                 
390                 if(!p || p->type != subnet.type)
391                         continue;
392
393                 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
394                         r = p;
395                         if(p->owner->status.reachable)
396                                 break;
397                 }
398         }
399
400         // Cache the result
401
402         cache_ipv4_slot = !cache_ipv4_slot;
403         memcpy(&cache_ipv4_address[cache_ipv4_slot], address, sizeof *address);
404         cache_ipv4_subnet[cache_ipv4_slot] = r;
405         cache_ipv4_valid[cache_ipv4_slot] = true;
406
407         return r;
408 }
409
410 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
411         subnet_t *p, *r = NULL, subnet = {0};
412         avl_node_t *n;
413         int i;
414
415         cp();
416
417         // Check if this address is cached
418
419         for(i = 0; i < 2; i++) {
420                 if(!cache_ipv6_valid[i])
421                         continue;
422                 if(!memcmp(address, &cache_ipv6_address[i], sizeof *address))
423                         return cache_ipv6_subnet[i];
424         }
425
426         // Search all subnets for a matching one
427
428         subnet.type = SUBNET_IPV6;
429         subnet.net.ipv6.address = *address;
430         subnet.net.ipv6.prefixlength = 128;
431         subnet.owner = NULL;
432
433         for(n = subnet_tree->head; n; n = n->next) {
434                 p = n->data;
435                 
436                 if(!p || p->type != subnet.type)
437                         continue;
438
439                 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
440                         r = p;
441                         if(p->owner->status.reachable)
442                                 break;
443                 }
444         }
445
446         // Cache the result
447
448         cache_ipv6_slot = !cache_ipv6_slot;
449         memcpy(&cache_ipv6_address[cache_ipv6_slot], address, sizeof *address);
450         cache_ipv6_subnet[cache_ipv6_slot] = r;
451         cache_ipv6_valid[cache_ipv6_slot] = true;
452
453         return r;
454 }
455
456 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
457         avl_node_t *node;
458         int i;
459         char *envp[9] = {0};
460         char netstr[MAXNETSTR];
461         char *name, *address, *port;
462         char empty[] = "";
463
464         // Prepare environment variables to be passed to the script
465
466         xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
467         xasprintf(&envp[1], "DEVICE=%s", device ? : "");
468         xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
469         xasprintf(&envp[3], "NODE=%s", owner->name);
470
471         if(owner != myself) {
472                 sockaddr2str(&owner->address, &address, &port);
473                 // 4 and 5 are reserved for SUBNET and WEIGHT
474                 xasprintf(&envp[6], "REMOTEADDRESS=%s", address);
475                 xasprintf(&envp[7], "REMOTEPORT=%s", port);
476         }
477
478         name = up ? "subnet-up" : "subnet-down";
479
480         if(!subnet) {
481                 for(node = owner->subnet_tree->head; node; node = node->next) {
482                         subnet = node->data;
483                         if(!net2str(netstr, sizeof netstr, subnet))
484                                 continue;
485                         // Strip the weight from the subnet, and put it in its own environment variable
486                         char *weight = strchr(netstr + 7, '#');
487                         if(weight)
488                                 *weight++ = 0;
489                         else
490                                 weight = empty;
491
492                         // Prepare the SUBNET and WEIGHT variables
493                         if(envp[4])
494                                 free(envp[4]);
495                         if(envp[5])
496                                 free(envp[5]);
497                         xasprintf(&envp[4], "SUBNET=%s", netstr);
498                         xasprintf(&envp[5], "WEIGHT=%s", weight);
499
500                         execute_script(name, envp);
501                 }
502         } else {
503                 if(net2str(netstr + 7, sizeof netstr - 7, subnet)) {
504                         // Strip the weight from the subnet, and put it in its own environment variable
505                         char *weight = strchr(netstr + 7, '#');
506                         if(weight)
507                                 *weight++ = 0;
508                         else
509                                 weight = empty;
510
511                         // Prepare the SUBNET and WEIGHT variables
512                         xasprintf(&envp[4], "SUBNET=%s", netstr);
513                         xasprintf(&envp[5], "WEIGHT=%s", weight);
514
515                         execute_script(name, envp);
516                 }
517         }
518
519         for(i = 0; envp[i] && i < 9; i++)
520                 free(envp[i]);
521 }
522
523 void dump_subnets(void) {
524         char netstr[MAXNETSTR];
525         subnet_t *subnet;
526         avl_node_t *node;
527
528         cp();
529
530         logger(LOG_DEBUG, _("Subnet list:"));
531
532         for(node = subnet_tree->head; node; node = node->next) {
533                 subnet = node->data;
534                 if(!net2str(netstr, sizeof netstr, subnet))
535                         continue;
536                 logger(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
537         }
538
539         logger(LOG_DEBUG, _("End of subnet list."));
540 }