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