Fix signed comparison bug in lookup_subnet_ipv4().
[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.24 2001/08/28 20:52:39 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 "connection.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 void init_subnets(void)
44 {
45 cp
46   subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet);
47 cp
48 }
49
50 /* Subnet comparison */
51
52 int subnet_compare_mac(subnet_t *a, subnet_t *b)
53 {
54 cp
55   return memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
56 }
57
58 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
59 {
60 cp
61   /* We compare as if a subnet is a number that equals (address << 32 + netmask). */
62    
63   if(a->net.ipv4.address == b->net.ipv4.address)
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     else
70       return 0;
71   }
72   else
73   {
74     if(a->net.ipv4.address < b->net.ipv4.address)
75       return -1;
76     else if(a->net.ipv4.address > b->net.ipv4.address)
77       return 1;
78     else
79       return 0;
80   }
81 }
82
83 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
84 {
85 cp
86   /* Same as ipv4 case, but with nasty 128 bit addresses */
87   
88   if(memcmp(&a->net.ipv6.mask, &b->net.ipv6.mask, sizeof(ipv6_t)) > 0)
89     if((a->net.ipv6.address.x[0] & b->net.ipv6.mask.x[0]) == b->net.ipv6.address.x[0] &&
90        (a->net.ipv6.address.x[1] & b->net.ipv6.mask.x[1]) == b->net.ipv6.address.x[1] &&
91        (a->net.ipv6.address.x[2] & b->net.ipv6.mask.x[2]) == b->net.ipv6.address.x[2] &&
92        (a->net.ipv6.address.x[3] & b->net.ipv6.mask.x[3]) == b->net.ipv6.address.x[3] &&
93        (a->net.ipv6.address.x[4] & b->net.ipv6.mask.x[4]) == b->net.ipv6.address.x[4] &&
94        (a->net.ipv6.address.x[5] & b->net.ipv6.mask.x[5]) == b->net.ipv6.address.x[5] &&
95        (a->net.ipv6.address.x[6] & b->net.ipv6.mask.x[6]) == b->net.ipv6.address.x[6] &&
96        (a->net.ipv6.address.x[7] & b->net.ipv6.mask.x[7]) == b->net.ipv6.address.x[7])
97       return -1;
98   
99   return memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
100 }
101
102 int subnet_compare(subnet_t *a, subnet_t *b)
103 {
104   int x;
105 cp  
106   x = a->type - b->type;
107   if(x)
108     return x;
109     
110   switch(a->type)
111     {
112       case SUBNET_MAC:
113         return subnet_compare_mac(a, b);
114       case SUBNET_IPV4:
115         return subnet_compare_ipv4(a, b);
116       case SUBNET_IPV6:
117         return subnet_compare_ipv6(a, b);
118       default:
119         syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, restarting!"), a->type);
120         sighup = 1;
121         return 0;
122     }
123 }
124
125 /* Allocating and freeing space for subnets */
126
127 subnet_t *new_subnet(void)
128 {
129 cp
130   return (subnet_t *)xmalloc(sizeof(subnet_t));
131 }
132
133 void free_subnet(subnet_t *subnet)
134 {
135 cp
136   free(subnet);
137 }
138
139 /* Linked list management */
140
141 void subnet_add(connection_t *cl, subnet_t *subnet)
142 {
143 cp
144   subnet->owner = cl;
145
146   while(!avl_insert(subnet_tree, subnet))
147     {
148       subnet_t *old;
149       
150       old = (subnet_t *)avl_search(subnet_tree, subnet);
151
152       if(debug_lvl >= DEBUG_PROTOCOL)
153         {
154           char *subnetstr;
155           subnetstr = net2str(subnet);
156           syslog(LOG_WARNING, _("Duplicate subnet %s for %s (%s), previous owner %s (%s)!"),
157                  subnetstr, cl->name, cl->hostname, old->owner->name, old->owner->hostname);
158           free(subnetstr);
159         }
160
161       subnet_del(old);
162     }
163
164   avl_insert(cl->subnet_tree, subnet);
165 cp
166 }
167
168 void subnet_del(subnet_t *subnet)
169 {
170 cp
171   avl_delete(subnet->owner->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_mac(mac_t *address)
289 {
290   subnet_t subnet, *p;
291 cp
292   subnet.type = SUBNET_MAC;
293   memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
294
295   p = (subnet_t *)avl_search(subnet_tree, &subnet);
296 cp
297   return p;
298 }
299
300 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
301 {
302   subnet_t subnet, *p;
303 cp
304   subnet.type = SUBNET_IPV4;
305   subnet.net.ipv4.address = *address;
306   subnet.net.ipv4.mask = 0xFFFFFFFF;
307
308   do
309   {
310     /* Go find subnet */
311   
312     p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
313
314   /* Check if the found subnet REALLY matches */
315 cp
316     if(p)
317       {
318         if ((*address & p->net.ipv4.mask) == p->net.ipv4.address)
319           break;
320         else
321           {
322             /* Otherwise, see if there is a bigger enclosing subnet */
323
324             subnet.net.ipv4.mask = p->net.ipv4.mask << 1;
325             subnet.net.ipv4.address = p->net.ipv4.address & subnet.net.ipv4.mask;
326           }
327       }
328    } while (p);
329    
330    return p;
331 }
332
333 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
334 {
335   subnet_t subnet, *p;
336   int i;
337 cp
338   subnet.type = SUBNET_IPV6;
339   memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
340   memset(&subnet.net.ipv6.mask, 0xFF, 16);
341   
342   p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet);
343   
344   if(p)
345     for(i=0; i<8; i++)
346       if((address->x[i] & p->net.ipv6.address.x[i]) != p->net.ipv6.address.x[i])
347         return NULL;
348
349   return p;
350 }
351
352 void dump_subnet_list(void)
353 {
354   char *netstr;
355   subnet_t *subnet;
356   avl_node_t *node;
357 cp
358   syslog(LOG_DEBUG, _("Subnet list:"));
359   for(node = subnet_tree->head; node; node = node->next)
360     {
361       subnet = (subnet_t *)node->data;
362       netstr = net2str(subnet);
363       syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
364       free(netstr);
365     }
366   syslog(LOG_DEBUG, _("End of subnet list."));
367 cp
368 }