537be117520913413a295a7200e4a7dc23ad5415
[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.22 2001/06/06 19:11:16 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <syslog.h>
27
28 #include "conf.h"
29 #include "net.h"
30 #include "connection.h"
31 #include "subnet.h"
32 #include "system.h"
33
34 #include <utils.h>
35 #include <xalloc.h>
36 #include <avl_tree.h>
37
38 /* lists type of subnet */
39
40 avl_tree_t *subnet_tree;
41
42 void init_subnets(void)
43 {
44 cp
45   subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet);
46 cp
47 }
48
49 /* Subnet comparison */
50
51 int subnet_compare_mac(subnet_t *a, subnet_t *b)
52 {
53 cp
54   return memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
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 a->net.ipv4.mask - b->net.ipv4.mask;
64   else
65     return a->net.ipv4.address - b->net.ipv4.address;
66 }
67
68 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
69 {
70 cp
71   /* Same as ipv4 case, but with nasty 128 bit addresses */
72   
73   if(memcmp(&a->net.ipv6.mask, &b->net.ipv6.mask, sizeof(ipv6_t)) > 0)
74     if((a->net.ipv6.address.x[0] & b->net.ipv6.mask.x[0]) == b->net.ipv6.address.x[0] &&
75        (a->net.ipv6.address.x[1] & b->net.ipv6.mask.x[1]) == b->net.ipv6.address.x[1] &&
76        (a->net.ipv6.address.x[2] & b->net.ipv6.mask.x[2]) == b->net.ipv6.address.x[2] &&
77        (a->net.ipv6.address.x[3] & b->net.ipv6.mask.x[3]) == b->net.ipv6.address.x[3] &&
78        (a->net.ipv6.address.x[4] & b->net.ipv6.mask.x[4]) == b->net.ipv6.address.x[4] &&
79        (a->net.ipv6.address.x[5] & b->net.ipv6.mask.x[5]) == b->net.ipv6.address.x[5] &&
80        (a->net.ipv6.address.x[6] & b->net.ipv6.mask.x[6]) == b->net.ipv6.address.x[6] &&
81        (a->net.ipv6.address.x[7] & b->net.ipv6.mask.x[7]) == b->net.ipv6.address.x[7])
82       return -1;
83   
84   return memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
85 }
86
87 int subnet_compare(subnet_t *a, subnet_t *b)
88 {
89   int x;
90 cp  
91   x = a->type - b->type;
92   if(x)
93     return x;
94     
95   switch(a->type)
96     {
97       case SUBNET_MAC:
98         return subnet_compare_mac(a, b);
99       case SUBNET_IPV4:
100         return subnet_compare_ipv4(a, b);
101       case SUBNET_IPV6:
102         return subnet_compare_ipv6(a, b);
103       default:
104         syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, restarting!"), a->type);
105         sighup = 1;
106         return 0;
107     }
108 }
109
110 /* Allocating and freeing space for subnets */
111
112 subnet_t *new_subnet(void)
113 {
114 cp
115   return (subnet_t *)xmalloc(sizeof(subnet_t));
116 }
117
118 void free_subnet(subnet_t *subnet)
119 {
120 cp
121   free(subnet);
122 }
123
124 /* Linked list management */
125
126 void subnet_add(connection_t *cl, subnet_t *subnet)
127 {
128 cp
129   subnet->owner = cl;
130
131   while(!avl_insert(subnet_tree, subnet))
132     {
133       subnet_t *old;
134       
135       old = (subnet_t *)avl_search(subnet_tree, subnet);
136
137       if(debug_lvl >= DEBUG_PROTOCOL)
138         {
139           char *subnetstr;
140           subnetstr = net2str(subnet);
141           syslog(LOG_WARNING, _("Duplicate subnet %s for %s (%s), previous owner %s (%s)!"),
142                  subnetstr, cl->name, cl->hostname, old->owner->name, old->owner->hostname);
143           free(subnetstr);
144         }
145
146       subnet_del(old);
147     }
148
149   avl_insert(cl->subnet_tree, subnet);
150 cp
151 }
152
153 void subnet_del(subnet_t *subnet)
154 {
155 cp
156   avl_delete(subnet->owner->subnet_tree, subnet);
157 cp
158   avl_delete(subnet_tree, subnet);
159 cp
160 }
161
162 /* Ascii representation of subnets */
163
164 subnet_t *str2net(char *subnetstr)
165 {
166   int type;
167   subnet_t *subnet;
168 cp
169   if(sscanf(subnetstr, "%d,", &type) != 1)
170     return NULL;
171 cp
172   subnet = new_subnet();
173 cp
174   switch(type)
175     {
176       case SUBNET_MAC:
177         if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
178                    &subnet->net.mac.address.x[0],
179                    &subnet->net.mac.address.x[1],
180                    &subnet->net.mac.address.x[2],
181                    &subnet->net.mac.address.x[3],
182                    &subnet->net.mac.address.x[4],
183                    &subnet->net.mac.address.x[5]) != 7)
184           {
185             free_subnet(subnet);
186             return NULL;
187           }
188         break;
189       case SUBNET_IPV4:
190         if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
191           {
192             free_subnet(subnet);
193             return NULL;
194           }
195         break;
196       case SUBNET_IPV6:
197         if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
198                    &subnet->net.ipv6.address.x[0],
199                    &subnet->net.ipv6.address.x[1],
200                    &subnet->net.ipv6.address.x[2],
201                    &subnet->net.ipv6.address.x[3],
202                    &subnet->net.ipv6.address.x[4],
203                    &subnet->net.ipv6.address.x[5],
204                    &subnet->net.ipv6.address.x[6],
205                    &subnet->net.ipv6.address.x[7],
206                    &subnet->net.ipv6.mask.x[0],
207                    &subnet->net.ipv6.mask.x[1],
208                    &subnet->net.ipv6.mask.x[2],
209                    &subnet->net.ipv6.mask.x[3],
210                    &subnet->net.ipv6.mask.x[4],
211                    &subnet->net.ipv6.mask.x[5],
212                    &subnet->net.ipv6.mask.x[6],
213                    &subnet->net.ipv6.mask.x[7]) != 17)
214           {
215             free_subnet(subnet);
216             return NULL;
217           }
218         break;
219       default:
220         free_subnet(subnet);
221         return NULL;
222     }
223 cp
224   return subnet;
225 }
226
227 char *net2str(subnet_t *subnet)
228 {
229   char *netstr;
230 cp
231   switch(subnet->type)
232     {
233       case SUBNET_MAC:
234         asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
235                    subnet->net.mac.address.x[0],
236                    subnet->net.mac.address.x[1],
237                    subnet->net.mac.address.x[2],
238                    subnet->net.mac.address.x[3],
239                    subnet->net.mac.address.x[4],
240                    subnet->net.mac.address.x[5]);
241         break;
242       case SUBNET_IPV4:
243         asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
244         break;
245       case SUBNET_IPV6:
246         asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
247                    subnet->net.ipv6.address.x[0],
248                    subnet->net.ipv6.address.x[1],
249                    subnet->net.ipv6.address.x[2],
250                    subnet->net.ipv6.address.x[3],
251                    subnet->net.ipv6.address.x[4],
252                    subnet->net.ipv6.address.x[5],
253                    subnet->net.ipv6.address.x[6],
254                    subnet->net.ipv6.address.x[7],
255                    subnet->net.ipv6.mask.x[0],
256                    subnet->net.ipv6.mask.x[1],
257                    subnet->net.ipv6.mask.x[2],
258                    subnet->net.ipv6.mask.x[3],
259                    subnet->net.ipv6.mask.x[4],
260                    subnet->net.ipv6.mask.x[5],
261                    subnet->net.ipv6.mask.x[6],
262                    subnet->net.ipv6.mask.x[7]);
263         break;
264       default:
265         asprintf(&netstr, _("unknown subnet type"));
266     }
267 cp
268   return netstr;
269 }
270
271 /* Subnet lookup routines */
272
273 subnet_t *lookup_subnet_mac(mac_t *address)
274 {
275   subnet_t subnet, *p;
276 cp
277   subnet.type = SUBNET_MAC;
278   memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
279
280   p = (subnet_t *)avl_search(subnet_tree, &subnet);
281 cp
282   return p;
283 }
284
285 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
286 {
287   subnet_t subnet, *p;
288 cp
289   subnet.type = SUBNET_IPV4;
290   subnet.net.ipv4.address = *address;
291   subnet.net.ipv4.mask = 0xFFFFFFFF;
292
293   do
294   {
295     /* Go find subnet */
296   
297     p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
298
299   /* Check if the found subnet REALLY matches */
300 cp
301     if(p)
302       {
303         if ((*address & p->net.ipv4.mask) == p->net.ipv4.address)
304           break;
305         else
306           {
307             /* Otherwise, see if there is a bigger enclosing subnet */
308
309             subnet.net.ipv4.mask = p->net.ipv4.mask << 1;
310             subnet.net.ipv4.address = p->net.ipv4.address & subnet.net.ipv4.mask;
311           }
312       }
313    } while (p);
314    
315    return p;
316 }
317
318 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
319 {
320   subnet_t subnet, *p;
321   int i;
322 cp
323   subnet.type = SUBNET_IPV6;
324   memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
325   memset(&subnet.net.ipv6.mask, 0xFF, 16);
326   
327   p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet);
328   
329   if(p)
330     for(i=0; i<8; i++)
331       if((address->x[i] & p->net.ipv6.address.x[i]) != p->net.ipv6.address.x[i])
332         return NULL;
333
334   return p;
335 }
336
337 void dump_subnet_list(void)
338 {
339   char *netstr;
340   subnet_t *subnet;
341   avl_node_t *node;
342 cp
343   syslog(LOG_DEBUG, _("Subnet list:"));
344   for(node = subnet_tree->head; node; node = node->next)
345     {
346       subnet = (subnet_t *)node->data;
347       netstr = net2str(subnet);
348       syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
349       free(netstr);
350     }
351   syslog(LOG_DEBUG, _("End of subnet list."));
352 cp
353 }