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