984cc7f8ece644ff2f2ec80cd6054366efe3fba1
[tinc] / src / subnet.c
1 /*
2     subnet.c -- handle subnet lookups and lists
3     Copyright (C) 2000-2004 Guus Sliepen <guus@tinc-vpn.org>,
4                   2000-2004 Ivo Timmermans <ivo@tinc-vpn.org>
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                 subnet->type = SUBNET_IPV4;
192                 subnet->net.ipv4.prefixlength = l;
193
194                 for(i = 0; i < 4; i++)
195                         subnet->net.ipv4.address.x[i] = x[i];
196
197                 return true;
198         }
199
200         if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
201                           &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
202                           &l) == 9) {
203                 subnet->type = SUBNET_IPV6;
204                 subnet->net.ipv6.prefixlength = l;
205
206                 for(i = 0; i < 8; i++)
207                         subnet->net.ipv6.address.x[i] = htons(x[i]);
208
209                 return true;
210         }
211
212         if(sscanf(subnetstr, "%hu.%hu.%hu.%hu", &x[0], &x[1], &x[2], &x[3]) == 4) {
213                 subnet->type = SUBNET_IPV4;
214                 subnet->net.ipv4.prefixlength = 32;
215
216                 for(i = 0; i < 4; i++)
217                         subnet->net.ipv4.address.x[i] = x[i];
218
219                 return true;
220         }
221
222         if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
223                           &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7]) == 8) {
224                 subnet->type = SUBNET_IPV6;
225                 subnet->net.ipv6.prefixlength = 128;
226
227                 for(i = 0; i < 8; i++)
228                         subnet->net.ipv6.address.x[i] = htons(x[i]);
229
230                 return true;
231         }
232
233         if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx",
234                           &x[0], &x[1], &x[2], &x[3], &x[4], &x[5]) == 6) {
235                 subnet->type = SUBNET_MAC;
236
237                 for(i = 0; i < 6; i++)
238                         subnet->net.mac.address.x[i] = x[i];
239
240                 return true;
241         }
242
243         return false;
244 }
245
246 bool net2str(char *netstr, int len, const subnet_t *subnet)
247 {
248         cp();
249
250         switch (subnet->type) {
251                 case SUBNET_MAC:
252                         snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx",
253                                          subnet->net.mac.address.x[0],
254                                          subnet->net.mac.address.x[1],
255                                          subnet->net.mac.address.x[2],
256                                          subnet->net.mac.address.x[3],
257                                          subnet->net.mac.address.x[4], subnet->net.mac.address.x[5]);
258                         break;
259
260                 case SUBNET_IPV4:
261                         snprintf(netstr, len, "%hu.%hu.%hu.%hu/%d",
262                                          subnet->net.ipv4.address.x[0],
263                                          subnet->net.ipv4.address.x[1],
264                                          subnet->net.ipv4.address.x[2],
265                                          subnet->net.ipv4.address.x[3], subnet->net.ipv4.prefixlength);
266                         break;
267
268                 case SUBNET_IPV6:
269                         snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
270                                          ntohs(subnet->net.ipv6.address.x[0]),
271                                          ntohs(subnet->net.ipv6.address.x[1]),
272                                          ntohs(subnet->net.ipv6.address.x[2]),
273                                          ntohs(subnet->net.ipv6.address.x[3]),
274                                          ntohs(subnet->net.ipv6.address.x[4]),
275                                          ntohs(subnet->net.ipv6.address.x[5]),
276                                          ntohs(subnet->net.ipv6.address.x[6]),
277                                          ntohs(subnet->net.ipv6.address.x[7]),
278                                          subnet->net.ipv6.prefixlength);
279                         break;
280
281                 default:
282                         logger(LOG_ERR,
283                                    _("net2str() was called with unknown subnet type %d, exiting!"),
284                                    subnet->type);
285                         cp_trace();
286                         exit(0);
287         }
288
289         return true;
290 }
291
292 /* Subnet lookup routines */
293
294 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet)
295 {
296         cp();
297
298         return avl_search(owner->subnet_tree, subnet);
299 }
300
301 subnet_t *lookup_subnet_mac(const mac_t *address)
302 {
303         subnet_t *p, subnet = {0};
304
305         cp();
306
307         subnet.type = SUBNET_MAC;
308         subnet.net.mac.address = *address;
309         subnet.owner = NULL;
310
311         p = avl_search(subnet_tree, &subnet);
312
313         return p;
314 }
315
316 subnet_t *lookup_subnet_ipv4(const ipv4_t *address)
317 {
318         subnet_t *p, subnet = {0};
319
320         cp();
321
322         subnet.type = SUBNET_IPV4;
323         subnet.net.ipv4.address = *address;
324         subnet.net.ipv4.prefixlength = 32;
325         subnet.owner = NULL;
326
327         do {
328                 /* Go find subnet */
329
330                 p = avl_search_closest_smaller(subnet_tree, &subnet);
331
332                 /* Check if the found subnet REALLY matches */
333
334                 if(p) {
335                         if(p->type != SUBNET_IPV4) {
336                                 p = NULL;
337                                 break;
338                         }
339
340                         if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength, sizeof(ipv4_t)))
341                                 break;
342                         else {
343                                 /* Otherwise, see if there is a bigger enclosing subnet */
344
345                                 subnet.net.ipv4.prefixlength = p->net.ipv4.prefixlength - 1;
346                                 maskcpy(&subnet.net.ipv4.address, &p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t));
347                         }
348                 }
349         } while(p);
350
351         return p;
352 }
353
354 subnet_t *lookup_subnet_ipv6(const ipv6_t *address)
355 {
356         subnet_t *p, subnet = {0};
357
358         cp();
359
360         subnet.type = SUBNET_IPV6;
361         subnet.net.ipv6.address = *address;
362         subnet.net.ipv6.prefixlength = 128;
363         subnet.owner = NULL;
364
365         do {
366                 /* Go find subnet */
367
368                 p = avl_search_closest_smaller(subnet_tree, &subnet);
369
370                 /* Check if the found subnet REALLY matches */
371
372                 if(p) {
373                         if(p->type != SUBNET_IPV6)
374                                 return NULL;
375
376                         if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength, sizeof(ipv6_t)))
377                                 break;
378                         else {
379                                 /* Otherwise, see if there is a bigger enclosing subnet */
380
381                                 subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1;
382                                 maskcpy(&subnet.net.ipv6.address, &p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t));
383                         }
384                 }
385         } while(p);
386
387         return p;
388 }
389
390 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
391         avl_node_t *node;
392         int i;
393         char *envp[8];
394         char netstr[MAXNETSTR + 7] = "SUBNET=";
395         char *name, *address, *port;
396
397         asprintf(&envp[0], "NETNAME=%s", netname ? : "");
398         asprintf(&envp[1], "DEVICE=%s", device ? : "");
399         asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
400         asprintf(&envp[3], "NODE=%s", owner->name);
401         if(owner != myself) {
402                 sockaddr2str(&owner->address, &address, &port);
403                 asprintf(&envp[4], "REMOTEADDRESS=%s", address);
404                 asprintf(&envp[5], "REMOTEPORT=%s", port);
405                 envp[6] = netstr;
406                 envp[7] = NULL;
407         } else {
408                 envp[4] = netstr;
409                 envp[5] = NULL;
410         }
411
412         name = up ? "subnet-up" : "subnet-down";
413
414         if(!subnet) {
415                 for(node = owner->subnet_tree->head; node; node = node->next) {
416                         subnet = node->data;
417                         if(!net2str(netstr + 7, sizeof netstr - 7, subnet))
418                                 continue;
419                         execute_script(name, envp);
420                 }
421         } else {
422                 if(net2str(netstr + 7, sizeof netstr - 7, subnet))
423                         execute_script(name, envp);
424         }
425
426         net2str(netstr, sizeof netstr, subnet);
427         envp[6] = envp[7] = NULL;
428         
429         for(i = 0; i < (owner != myself ? 6 : 4); i++)
430                 free(envp[i]);
431
432         free(address);
433         free(port);
434 }
435
436 void dump_subnets(void)
437 {
438         char netstr[MAXNETSTR];
439         subnet_t *subnet;
440         avl_node_t *node;
441
442         cp();
443
444         logger(LOG_DEBUG, _("Subnet list:"));
445
446         for(node = subnet_tree->head; node; node = node->next) {
447                 subnet = node->data;
448                 if(!net2str(netstr, sizeof netstr, subnet))
449                         continue;
450                 logger(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
451         }
452
453         logger(LOG_DEBUG, _("End of subnet list."));
454 }