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