Backport fixes from trunk since revision 1555.
[tinc] / src / subnet.c
1 /*
2     subnet.c -- handle subnet lookups and lists
3     Copyright (C) 2000-2006 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 "splay_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 splay_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 = splay_alloc_tree((splay_compare_t) subnet_compare, (splay_action_t) free_subnet);
121 }
122
123 void exit_subnets(void)
124 {
125         cp();
126
127         splay_delete_tree(subnet_tree);
128 }
129
130 splay_tree_t *new_subnet_tree(void)
131 {
132         cp();
133
134         return splay_alloc_tree((splay_compare_t) subnet_compare, NULL);
135 }
136
137 void free_subnet_tree(splay_tree_t *subnet_tree)
138 {
139         cp();
140
141         splay_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         splay_insert(subnet_tree, subnet);
169         splay_insert(n->subnet_tree, subnet);
170 }
171
172 void subnet_del(node_t *n, subnet_t *subnet)
173 {
174         cp();
175
176         splay_delete(n->subnet_tree, subnet);
177         splay_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                 if(l < 0 || l > 32)
223                         return false;
224
225                 subnet->type = SUBNET_IPV4;
226                 subnet->net.ipv4.prefixlength = 32;
227
228                 for(i = 0; i < 4; i++) {
229                         if(x[i] > 255)
230                                 return false;
231                         subnet->net.ipv4.address.x[i] = x[i];
232                 }
233
234                 return true;
235         }
236
237         if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
238                           &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7]) == 8) {
239                 if(l < 0 || l > 128)
240                         return false;
241
242                 subnet->type = SUBNET_IPV6;
243                 subnet->net.ipv6.prefixlength = 128;
244
245                 for(i = 0; i < 8; i++)
246                         subnet->net.ipv6.address.x[i] = htons(x[i]);
247
248                 return true;
249         }
250
251         if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx",
252                           &x[0], &x[1], &x[2], &x[3], &x[4], &x[5]) == 6) {
253                 subnet->type = SUBNET_MAC;
254
255                 for(i = 0; i < 6; i++)
256                         subnet->net.mac.address.x[i] = x[i];
257
258                 return true;
259         }
260
261         return false;
262 }
263
264 bool net2str(char *netstr, int len, const subnet_t *subnet)
265 {
266         cp();
267
268         if(!netstr || !subnet) {
269                 logger(LOG_ERR, _("net2str() was called with netstr=%p, subnet=%p!\n"), netstr, subnet);
270                 return false;
271         }
272
273         switch (subnet->type) {
274                 case SUBNET_MAC:
275                         snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx",
276                                          subnet->net.mac.address.x[0],
277                                          subnet->net.mac.address.x[1],
278                                          subnet->net.mac.address.x[2],
279                                          subnet->net.mac.address.x[3],
280                                          subnet->net.mac.address.x[4], subnet->net.mac.address.x[5]);
281                         break;
282
283                 case SUBNET_IPV4:
284                         snprintf(netstr, len, "%hu.%hu.%hu.%hu/%d",
285                                          subnet->net.ipv4.address.x[0],
286                                          subnet->net.ipv4.address.x[1],
287                                          subnet->net.ipv4.address.x[2],
288                                          subnet->net.ipv4.address.x[3], subnet->net.ipv4.prefixlength);
289                         break;
290
291                 case SUBNET_IPV6:
292                         snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
293                                          ntohs(subnet->net.ipv6.address.x[0]),
294                                          ntohs(subnet->net.ipv6.address.x[1]),
295                                          ntohs(subnet->net.ipv6.address.x[2]),
296                                          ntohs(subnet->net.ipv6.address.x[3]),
297                                          ntohs(subnet->net.ipv6.address.x[4]),
298                                          ntohs(subnet->net.ipv6.address.x[5]),
299                                          ntohs(subnet->net.ipv6.address.x[6]),
300                                          ntohs(subnet->net.ipv6.address.x[7]),
301                                          subnet->net.ipv6.prefixlength);
302                         break;
303
304                 default:
305                         logger(LOG_ERR,
306                                    _("net2str() was called with unknown subnet type %d, exiting!"),
307                                    subnet->type);
308                         cp_trace();
309                         exit(0);
310         }
311
312         return true;
313 }
314
315 /* Subnet lookup routines */
316
317 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet)
318 {
319         cp();
320
321         return splay_search(owner->subnet_tree, subnet);
322 }
323
324 subnet_t *lookup_subnet_mac(const mac_t *address)
325 {
326         subnet_t *p, subnet = {0};
327
328         cp();
329
330         subnet.type = SUBNET_MAC;
331         subnet.net.mac.address = *address;
332         subnet.owner = NULL;
333
334         p = splay_search(subnet_tree, &subnet);
335
336         return p;
337 }
338
339 subnet_t *lookup_subnet_ipv4(const ipv4_t *address)
340 {
341         subnet_t *p, subnet = {0};
342
343         cp();
344
345         subnet.type = SUBNET_IPV4;
346         subnet.net.ipv4.address = *address;
347         subnet.net.ipv4.prefixlength = 32;
348         subnet.owner = NULL;
349
350         do {
351                 /* Go find subnet */
352
353                 p = splay_search_closest_smaller(subnet_tree, &subnet);
354
355                 /* Check if the found subnet REALLY matches */
356
357                 if(p) {
358                         if(p->type != SUBNET_IPV4) {
359                                 p = NULL;
360                                 break;
361                         }
362
363                         if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength))
364                                 break;
365                         else {
366                                 /* Otherwise, see if there is a bigger enclosing subnet */
367
368                                 subnet.net.ipv4.prefixlength = p->net.ipv4.prefixlength - 1;
369                                 if(subnet.net.ipv4.prefixlength < 0 || subnet.net.ipv4.prefixlength > 32)
370                                         return NULL;
371                                 maskcpy(&subnet.net.ipv4.address, &p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t));
372                         }
373                 }
374         } while(p);
375
376         return p;
377 }
378
379 subnet_t *lookup_subnet_ipv6(const ipv6_t *address)
380 {
381         subnet_t *p, subnet = {0};
382
383         cp();
384
385         subnet.type = SUBNET_IPV6;
386         subnet.net.ipv6.address = *address;
387         subnet.net.ipv6.prefixlength = 128;
388         subnet.owner = NULL;
389
390         do {
391                 /* Go find subnet */
392
393                 p = splay_search_closest_smaller(subnet_tree, &subnet);
394
395                 /* Check if the found subnet REALLY matches */
396
397                 if(p) {
398                         if(p->type != SUBNET_IPV6)
399                                 return NULL;
400
401                         if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength))
402                                 break;
403                         else {
404                                 /* Otherwise, see if there is a bigger enclosing subnet */
405
406                                 subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1;
407                                 if(subnet.net.ipv6.prefixlength < 0 || subnet.net.ipv6.prefixlength > 128)
408                                         return NULL;
409                                 maskcpy(&subnet.net.ipv6.address, &p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t));
410                         }
411                 }
412         } while(p);
413
414         return p;
415 }
416
417 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
418         splay_node_t *node;
419         int i;
420         char *envp[8];
421         char netstr[MAXNETSTR + 7] = "SUBNET=";
422         char *name, *address, *port;
423
424         asprintf(&envp[0], "NETNAME=%s", netname ? : "");
425         asprintf(&envp[1], "DEVICE=%s", device ? : "");
426         asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
427         asprintf(&envp[3], "NODE=%s", owner->name);
428
429         if(owner != myself) {
430                 sockaddr2str(&owner->address, &address, &port);
431                 asprintf(&envp[4], "REMOTEADDRESS=%s", address);
432                 asprintf(&envp[5], "REMOTEPORT=%s", port);
433                 envp[6] = netstr;
434                 envp[7] = NULL;
435         } else {
436                 envp[4] = netstr;
437                 envp[5] = NULL;
438         }
439
440         name = up ? "subnet-up" : "subnet-down";
441
442         if(!subnet) {
443                 for(node = owner->subnet_tree->head; node; node = node->next) {
444                         subnet = node->data;
445                         if(!net2str(netstr + 7, sizeof netstr - 7, subnet))
446                                 continue;
447                         execute_script(name, envp);
448                 }
449         } else {
450                 if(net2str(netstr + 7, sizeof netstr - 7, subnet))
451                         execute_script(name, envp);
452         }
453
454         for(i = 0; i < (owner != myself ? 6 : 4); i++)
455                 free(envp[i]);
456
457         if(owner != myself) {
458                 free(address);
459                 free(port);
460         }
461 }
462
463 int dump_subnets(struct evbuffer *out)
464 {
465         char netstr[MAXNETSTR];
466         subnet_t *subnet;
467         splay_node_t *node;
468
469         cp();
470
471         for(node = subnet_tree->head; node; node = node->next) {
472                 subnet = node->data;
473                 if(!net2str(netstr, sizeof netstr, subnet))
474                         continue;
475                 if(evbuffer_add_printf(out, _(" %s owner %s\n"),
476                                                            netstr, subnet->owner->name) == -1)
477                         return errno;
478         }
479
480         return 0;
481 }