Various fixes, tinc is now somewhat capable of actually working again.
[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.28 2001/10/30 12:59:12 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 cp
48   return memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
49 }
50
51 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
52 {
53 cp
54   /* We compare as if a subnet is a number that equals (address << 32 + netmask). */
55    
56   if(a->net.ipv4.address < b->net.ipv4.address)
57     return -1;
58   else if(a->net.ipv4.address > b->net.ipv4.address)
59     return 1;
60
61   if(a->net.ipv4.mask < b->net.ipv4.mask)
62     return -1;
63   else if(a->net.ipv4.mask > b->net.ipv4.mask)
64     return 1;
65
66   return 0;
67 }
68
69 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
70 {
71   int result;
72 cp
73   /* Same as ipv4 case, but with nasty 128 bit addresses */
74   
75   result = memcmp(a->net.ipv6.address.x, b->net.ipv6.address.x, sizeof(ipv6_t));
76   
77   if(result)
78     return result;
79
80   result = memcmp(a->net.ipv6.mask.x, b->net.ipv6.mask.x, sizeof(ipv6_t));
81   
82   if(result)
83     return result;
84
85   return 0;
86 }
87
88 int subnet_compare(subnet_t *a, subnet_t *b)
89 {
90   int x;
91 cp  
92   x = a->type - b->type;
93   if(x)
94     return x;
95     
96   switch(a->type)
97     {
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         syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, restarting!"), a->type);
106         sighup = 1;
107         return 0;
108     }
109 }
110
111 /* Initialising trees */
112
113 void init_subnets(void)
114 {
115 cp
116   subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet);
117 cp
118 }
119
120 void exit_subnets(void)
121 {
122 cp
123   avl_delete_tree(subnet_tree);
124 cp
125 }
126
127 avl_tree_t *new_subnet_tree(void)
128 {
129 cp
130   return avl_alloc_tree((avl_compare_t)subnet_compare, NULL);
131 cp
132 }
133
134 void free_subnet_tree(avl_tree_t *subnet_tree)
135 {
136 cp
137   avl_delete_tree(subnet_tree);
138 cp
139 }
140
141 /* Allocating and freeing space for subnets */
142
143 subnet_t *new_subnet(void)
144 {
145 cp
146   return (subnet_t *)xmalloc(sizeof(subnet_t));
147 }
148
149 void free_subnet(subnet_t *subnet)
150 {
151 cp
152   free(subnet);
153 }
154
155 /* Adding and removing subnets */
156
157 void subnet_add(node_t *n, subnet_t *subnet)
158 {
159 cp
160   subnet->owner = n;
161
162   avl_insert(subnet_tree, subnet);
163 cp
164   avl_insert(n->subnet_tree, subnet);
165 cp
166 }
167
168 void subnet_del(node_t *n, subnet_t *subnet)
169 {
170 cp
171   avl_delete(n->subnet_tree, subnet);
172 cp
173   avl_delete(subnet_tree, subnet);
174 cp
175 }
176
177 /* Ascii representation of subnets */
178
179 subnet_t *str2net(char *subnetstr)
180 {
181   int type;
182   subnet_t *subnet;
183 cp
184   if(sscanf(subnetstr, "%d,", &type) != 1)
185     return NULL;
186 cp
187   subnet = new_subnet();
188 cp
189   switch(type)
190     {
191       case SUBNET_MAC:
192         if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
193                    &subnet->net.mac.address.x[0],
194                    &subnet->net.mac.address.x[1],
195                    &subnet->net.mac.address.x[2],
196                    &subnet->net.mac.address.x[3],
197                    &subnet->net.mac.address.x[4],
198                    &subnet->net.mac.address.x[5]) != 7)
199           {
200             free_subnet(subnet);
201             return NULL;
202           }
203         break;
204       case SUBNET_IPV4:
205         if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
206           {
207             free_subnet(subnet);
208             return NULL;
209           }
210         break;
211       case SUBNET_IPV6:
212         if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
213                    &subnet->net.ipv6.address.x[0],
214                    &subnet->net.ipv6.address.x[1],
215                    &subnet->net.ipv6.address.x[2],
216                    &subnet->net.ipv6.address.x[3],
217                    &subnet->net.ipv6.address.x[4],
218                    &subnet->net.ipv6.address.x[5],
219                    &subnet->net.ipv6.address.x[6],
220                    &subnet->net.ipv6.address.x[7],
221                    &subnet->net.ipv6.mask.x[0],
222                    &subnet->net.ipv6.mask.x[1],
223                    &subnet->net.ipv6.mask.x[2],
224                    &subnet->net.ipv6.mask.x[3],
225                    &subnet->net.ipv6.mask.x[4],
226                    &subnet->net.ipv6.mask.x[5],
227                    &subnet->net.ipv6.mask.x[6],
228                    &subnet->net.ipv6.mask.x[7]) != 17)
229           {
230             free_subnet(subnet);
231             return NULL;
232           }
233         break;
234       default:
235         free_subnet(subnet);
236         return NULL;
237     }
238 cp
239   return subnet;
240 }
241
242 char *net2str(subnet_t *subnet)
243 {
244   char *netstr;
245 cp
246   switch(subnet->type)
247     {
248       case SUBNET_MAC:
249         asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
250                    subnet->net.mac.address.x[0],
251                    subnet->net.mac.address.x[1],
252                    subnet->net.mac.address.x[2],
253                    subnet->net.mac.address.x[3],
254                    subnet->net.mac.address.x[4],
255                    subnet->net.mac.address.x[5]);
256         break;
257       case SUBNET_IPV4:
258         asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
259         break;
260       case SUBNET_IPV6:
261         asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
262                    subnet->net.ipv6.address.x[0],
263                    subnet->net.ipv6.address.x[1],
264                    subnet->net.ipv6.address.x[2],
265                    subnet->net.ipv6.address.x[3],
266                    subnet->net.ipv6.address.x[4],
267                    subnet->net.ipv6.address.x[5],
268                    subnet->net.ipv6.address.x[6],
269                    subnet->net.ipv6.address.x[7],
270                    subnet->net.ipv6.mask.x[0],
271                    subnet->net.ipv6.mask.x[1],
272                    subnet->net.ipv6.mask.x[2],
273                    subnet->net.ipv6.mask.x[3],
274                    subnet->net.ipv6.mask.x[4],
275                    subnet->net.ipv6.mask.x[5],
276                    subnet->net.ipv6.mask.x[6],
277                    subnet->net.ipv6.mask.x[7]);
278         break;
279       default:
280         asprintf(&netstr, _("unknown subnet type"));
281     }
282 cp
283   return netstr;
284 }
285
286 /* Subnet lookup routines */
287
288 subnet_t *lookup_subnet(node_t *owner, subnet_t *subnet)
289 {
290 cp  
291   return avl_search(owner->subnet_tree, subnet);
292 }
293
294 subnet_t *lookup_subnet_mac(mac_t *address)
295 {
296   subnet_t subnet, *p;
297 cp
298   subnet.type = SUBNET_MAC;
299   memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
300
301   p = (subnet_t *)avl_search(subnet_tree, &subnet);
302 cp
303   return p;
304 }
305
306 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
307 {
308   subnet_t subnet, *p;
309 cp
310   subnet.type = SUBNET_IPV4;
311   subnet.net.ipv4.address = *address;
312   subnet.net.ipv4.mask = 0xFFFFFFFF;
313
314   do
315   {
316     /* Go find subnet */
317   
318     p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
319
320   /* Check if the found subnet REALLY matches */
321 cp
322     if(p)
323       {
324         if ((*address & p->net.ipv4.mask) == p->net.ipv4.address)
325           break;
326         else
327           {
328             /* Otherwise, see if there is a bigger enclosing subnet */
329
330             subnet.net.ipv4.mask = p->net.ipv4.mask << 1;
331             subnet.net.ipv4.address = p->net.ipv4.address & subnet.net.ipv4.mask;
332           }
333       }
334    } while (p);
335    
336    return p;
337 }
338
339 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
340 {
341   subnet_t subnet, *p;
342   int i;
343 cp
344   subnet.type = SUBNET_IPV6;
345   memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
346   memset(&subnet.net.ipv6.mask, 0xFF, 16);
347   
348   p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet);
349   
350   if(p)
351     for(i=0; i<8; i++)
352       if((address->x[i] & p->net.ipv6.address.x[i]) != p->net.ipv6.address.x[i])
353         return NULL;
354
355   return p;
356 }
357
358 void dump_subnets(void)
359 {
360   char *netstr;
361   subnet_t *subnet;
362   avl_node_t *node;
363 cp
364   syslog(LOG_DEBUG, _("Subnet list:"));
365   for(node = subnet_tree->head; node; node = node->next)
366     {
367       subnet = (subnet_t *)node->data;
368       netstr = net2str(subnet);
369       syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
370       free(netstr);
371     }
372   syslog(LOG_DEBUG, _("End of subnet list."));
373 cp
374 }