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