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