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