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