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