99d97bcdbb4d4dd2aaa5b6e957ccdc78976a8613
[tinc] / src / subnet.c
1 /*
2     subnet.c -- handle subnet lookups and lists
3     Copyright (C) 2000,2001 Guus Sliepen <guus@sliepen.warande.net>,
4                   2000,2001 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.27 2001/10/28 22:42:49 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <syslog.h>
27 #include <string.h>
28
29 #include "conf.h"
30 #include "net.h"
31 #include "node.h"
32 #include "subnet.h"
33 #include "system.h"
34
35 #include <utils.h>
36 #include <xalloc.h>
37 #include <avl_tree.h>
38
39 /* lists type of subnet */
40
41 avl_tree_t *subnet_tree;
42
43 /* Subnet comparison */
44
45 int subnet_compare_mac(subnet_t *a, subnet_t *b)
46 {
47   int result;
48 cp
49   result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
50   
51   if(result)
52     return result;
53
54   return strcmp(a->owner->name, b->owner->name);
55 }
56
57 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
58 {
59 cp
60   /* We compare as if a subnet is a number that equals (address << 32 + netmask). */
61    
62   if(a->net.ipv4.address < b->net.ipv4.address)
63     return -1;
64   else if(a->net.ipv4.address > b->net.ipv4.address)
65     return 1;
66
67   if(a->net.ipv4.mask < b->net.ipv4.mask)
68     return -1;
69   else if(a->net.ipv4.mask > b->net.ipv4.mask)
70     return 1;
71
72   return strcmp(a->owner->name, b->owner->name);
73 }
74
75 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
76 {
77   int result;
78 cp
79   /* Same as ipv4 case, but with nasty 128 bit addresses */
80   
81   result = memcmp(a->net.ipv6.address.x, b->net.ipv6.address.x, sizeof(ipv6_t));
82   
83   if(result)
84     return result;
85
86   result = memcmp(a->net.ipv6.mask.x, b->net.ipv6.mask.x, sizeof(ipv6_t));
87   
88   if(result)
89     return result;
90
91   return strcmp(a->owner->name, b->owner->name);
92 }
93
94 int subnet_compare(subnet_t *a, subnet_t *b)
95 {
96   int x;
97 cp  
98   x = a->type - b->type;
99   if(x)
100     return x;
101     
102   switch(a->type)
103     {
104       case SUBNET_MAC:
105         return subnet_compare_mac(a, b);
106       case SUBNET_IPV4:
107         return subnet_compare_ipv4(a, b);
108       case SUBNET_IPV6:
109         return subnet_compare_ipv6(a, b);
110       default:
111         syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, restarting!"), a->type);
112         sighup = 1;
113         return 0;
114     }
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 /* Linked list management */
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 type;
188   subnet_t *subnet;
189 cp
190   if(sscanf(subnetstr, "%d,", &type) != 1)
191     return NULL;
192 cp
193   subnet = new_subnet();
194 cp
195   switch(type)
196     {
197       case SUBNET_MAC:
198         if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
199                    &subnet->net.mac.address.x[0],
200                    &subnet->net.mac.address.x[1],
201                    &subnet->net.mac.address.x[2],
202                    &subnet->net.mac.address.x[3],
203                    &subnet->net.mac.address.x[4],
204                    &subnet->net.mac.address.x[5]) != 7)
205           {
206             free_subnet(subnet);
207             return NULL;
208           }
209         break;
210       case SUBNET_IPV4:
211         if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
212           {
213             free_subnet(subnet);
214             return NULL;
215           }
216         break;
217       case SUBNET_IPV6:
218         if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
219                    &subnet->net.ipv6.address.x[0],
220                    &subnet->net.ipv6.address.x[1],
221                    &subnet->net.ipv6.address.x[2],
222                    &subnet->net.ipv6.address.x[3],
223                    &subnet->net.ipv6.address.x[4],
224                    &subnet->net.ipv6.address.x[5],
225                    &subnet->net.ipv6.address.x[6],
226                    &subnet->net.ipv6.address.x[7],
227                    &subnet->net.ipv6.mask.x[0],
228                    &subnet->net.ipv6.mask.x[1],
229                    &subnet->net.ipv6.mask.x[2],
230                    &subnet->net.ipv6.mask.x[3],
231                    &subnet->net.ipv6.mask.x[4],
232                    &subnet->net.ipv6.mask.x[5],
233                    &subnet->net.ipv6.mask.x[6],
234                    &subnet->net.ipv6.mask.x[7]) != 17)
235           {
236             free_subnet(subnet);
237             return NULL;
238           }
239         break;
240       default:
241         free_subnet(subnet);
242         return NULL;
243     }
244 cp
245   return subnet;
246 }
247
248 char *net2str(subnet_t *subnet)
249 {
250   char *netstr;
251 cp
252   switch(subnet->type)
253     {
254       case SUBNET_MAC:
255         asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
256                    subnet->net.mac.address.x[0],
257                    subnet->net.mac.address.x[1],
258                    subnet->net.mac.address.x[2],
259                    subnet->net.mac.address.x[3],
260                    subnet->net.mac.address.x[4],
261                    subnet->net.mac.address.x[5]);
262         break;
263       case SUBNET_IPV4:
264         asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
265         break;
266       case SUBNET_IPV6:
267         asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
268                    subnet->net.ipv6.address.x[0],
269                    subnet->net.ipv6.address.x[1],
270                    subnet->net.ipv6.address.x[2],
271                    subnet->net.ipv6.address.x[3],
272                    subnet->net.ipv6.address.x[4],
273                    subnet->net.ipv6.address.x[5],
274                    subnet->net.ipv6.address.x[6],
275                    subnet->net.ipv6.address.x[7],
276                    subnet->net.ipv6.mask.x[0],
277                    subnet->net.ipv6.mask.x[1],
278                    subnet->net.ipv6.mask.x[2],
279                    subnet->net.ipv6.mask.x[3],
280                    subnet->net.ipv6.mask.x[4],
281                    subnet->net.ipv6.mask.x[5],
282                    subnet->net.ipv6.mask.x[6],
283                    subnet->net.ipv6.mask.x[7]);
284         break;
285       default:
286         asprintf(&netstr, _("unknown subnet type"));
287     }
288 cp
289   return netstr;
290 }
291
292 /* Subnet lookup routines */
293
294 subnet_t *lookup_subnet(node_t *owner, subnet_t *subnet)
295 {
296 cp  
297   return avl_search(owner->subnet_tree, subnet);
298 }
299
300 subnet_t *lookup_subnet_mac(mac_t *address)
301 {
302   subnet_t subnet, *p;
303 cp
304   subnet.type = SUBNET_MAC;
305   memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
306
307   p = (subnet_t *)avl_search(subnet_tree, &subnet);
308 cp
309   return p;
310 }
311
312 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
313 {
314   subnet_t subnet, *p;
315 cp
316   subnet.type = SUBNET_IPV4;
317   subnet.net.ipv4.address = *address;
318   subnet.net.ipv4.mask = 0xFFFFFFFF;
319
320   do
321   {
322     /* Go find subnet */
323   
324     p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
325
326   /* Check if the found subnet REALLY matches */
327 cp
328     if(p)
329       {
330         if ((*address & p->net.ipv4.mask) == p->net.ipv4.address)
331           break;
332         else
333           {
334             /* Otherwise, see if there is a bigger enclosing subnet */
335
336             subnet.net.ipv4.mask = p->net.ipv4.mask << 1;
337             subnet.net.ipv4.address = p->net.ipv4.address & subnet.net.ipv4.mask;
338           }
339       }
340    } while (p);
341    
342    return p;
343 }
344
345 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
346 {
347   subnet_t subnet, *p;
348   int i;
349 cp
350   subnet.type = SUBNET_IPV6;
351   memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
352   memset(&subnet.net.ipv6.mask, 0xFF, 16);
353   
354   p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet);
355   
356   if(p)
357     for(i=0; i<8; i++)
358       if((address->x[i] & p->net.ipv6.address.x[i]) != p->net.ipv6.address.x[i])
359         return NULL;
360
361   return p;
362 }
363
364 void dump_subnets(void)
365 {
366   char *netstr;
367   subnet_t *subnet;
368   avl_node_t *node;
369 cp
370   syslog(LOG_DEBUG, _("Subnet list:"));
371   for(node = subnet_tree->head; node; node = node->next)
372     {
373       subnet = (subnet_t *)node->data;
374       netstr = net2str(subnet);
375       syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
376       free(netstr);
377     }
378   syslog(LOG_DEBUG, _("End of subnet list."));
379 cp
380 }