2e1a24af6e218305f62ce4667f60fa97ced929b3
[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.21 2001/06/05 18:07:14 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   avl_insert(subnet_tree, subnet);
131   avl_insert(cl->subnet_tree, subnet);
132 cp
133 }
134
135 void subnet_del(subnet_t *subnet)
136 {
137 cp
138   avl_delete(subnet->owner->subnet_tree, subnet);
139 cp
140   avl_delete(subnet_tree, subnet);
141 cp
142 }
143
144 /* Ascii representation of subnets */
145
146 subnet_t *str2net(char *subnetstr)
147 {
148   int type;
149   subnet_t *subnet;
150 cp
151   if(sscanf(subnetstr, "%d,", &type) != 1)
152     return NULL;
153 cp
154   subnet = new_subnet();
155 cp
156   switch(type)
157     {
158       case SUBNET_MAC:
159         if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
160                    &subnet->net.mac.address.x[0],
161                    &subnet->net.mac.address.x[1],
162                    &subnet->net.mac.address.x[2],
163                    &subnet->net.mac.address.x[3],
164                    &subnet->net.mac.address.x[4],
165                    &subnet->net.mac.address.x[5]) != 7)
166           {
167             free_subnet(subnet);
168             return NULL;
169           }
170         break;
171       case SUBNET_IPV4:
172         if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
173           {
174             free_subnet(subnet);
175             return NULL;
176           }
177         break;
178       case SUBNET_IPV6:
179         if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
180                    &subnet->net.ipv6.address.x[0],
181                    &subnet->net.ipv6.address.x[1],
182                    &subnet->net.ipv6.address.x[2],
183                    &subnet->net.ipv6.address.x[3],
184                    &subnet->net.ipv6.address.x[4],
185                    &subnet->net.ipv6.address.x[5],
186                    &subnet->net.ipv6.address.x[6],
187                    &subnet->net.ipv6.address.x[7],
188                    &subnet->net.ipv6.mask.x[0],
189                    &subnet->net.ipv6.mask.x[1],
190                    &subnet->net.ipv6.mask.x[2],
191                    &subnet->net.ipv6.mask.x[3],
192                    &subnet->net.ipv6.mask.x[4],
193                    &subnet->net.ipv6.mask.x[5],
194                    &subnet->net.ipv6.mask.x[6],
195                    &subnet->net.ipv6.mask.x[7]) != 17)
196           {
197             free_subnet(subnet);
198             return NULL;
199           }
200         break;
201       default:
202         free_subnet(subnet);
203         return NULL;
204     }
205 cp
206   return subnet;
207 }
208
209 char *net2str(subnet_t *subnet)
210 {
211   char *netstr;
212 cp
213   switch(subnet->type)
214     {
215       case SUBNET_MAC:
216         asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
217                    subnet->net.mac.address.x[0],
218                    subnet->net.mac.address.x[1],
219                    subnet->net.mac.address.x[2],
220                    subnet->net.mac.address.x[3],
221                    subnet->net.mac.address.x[4],
222                    subnet->net.mac.address.x[5]);
223         break;
224       case SUBNET_IPV4:
225         asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
226         break;
227       case SUBNET_IPV6:
228         asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
229                    subnet->net.ipv6.address.x[0],
230                    subnet->net.ipv6.address.x[1],
231                    subnet->net.ipv6.address.x[2],
232                    subnet->net.ipv6.address.x[3],
233                    subnet->net.ipv6.address.x[4],
234                    subnet->net.ipv6.address.x[5],
235                    subnet->net.ipv6.address.x[6],
236                    subnet->net.ipv6.address.x[7],
237                    subnet->net.ipv6.mask.x[0],
238                    subnet->net.ipv6.mask.x[1],
239                    subnet->net.ipv6.mask.x[2],
240                    subnet->net.ipv6.mask.x[3],
241                    subnet->net.ipv6.mask.x[4],
242                    subnet->net.ipv6.mask.x[5],
243                    subnet->net.ipv6.mask.x[6],
244                    subnet->net.ipv6.mask.x[7]);
245         break;
246       default:
247         asprintf(&netstr, _("unknown subnet type"));
248     }
249 cp
250   return netstr;
251 }
252
253 /* Subnet lookup routines */
254
255 subnet_t *lookup_subnet_mac(mac_t *address)
256 {
257   subnet_t subnet, *p;
258 cp
259   subnet.type = SUBNET_MAC;
260   memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
261
262   p = (subnet_t *)avl_search(subnet_tree, &subnet);
263 cp
264   return p;
265 }
266
267 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
268 {
269   subnet_t subnet, *p;
270 cp
271   subnet.type = SUBNET_IPV4;
272   subnet.net.ipv4.address = *address;
273   subnet.net.ipv4.mask = 0xFFFFFFFF;
274
275   do
276   {
277     /* Go find subnet */
278   
279     p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
280
281   /* Check if the found subnet REALLY matches */
282 cp
283     if(p)
284       {
285         if ((*address & p->net.ipv4.mask) == p->net.ipv4.address)
286           break;
287         else
288           {
289             /* Otherwise, see if there is a bigger enclosing subnet */
290
291             subnet.net.ipv4.mask = p->net.ipv4.mask << 1;
292             subnet.net.ipv4.address = p->net.ipv4.address & subnet.net.ipv4.mask;
293           }
294       }
295    } while (p);
296    
297    return p;
298 }
299
300 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
301 {
302   subnet_t subnet, *p;
303   int i;
304 cp
305   subnet.type = SUBNET_IPV6;
306   memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
307   memset(&subnet.net.ipv6.mask, 0xFF, 16);
308   
309   p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet);
310   
311   if(p)
312     for(i=0; i<8; i++)
313       if((address->x[i] & p->net.ipv6.address.x[i]) != p->net.ipv6.address.x[i])
314         return NULL;
315
316   return p;
317 }
318
319 void dump_subnet_list(void)
320 {
321   char *netstr;
322   subnet_t *subnet;
323   avl_node_t *node;
324 cp
325   syslog(LOG_DEBUG, _("Subnet list:"));
326   for(node = subnet_tree->head; node; node = node->next)
327     {
328       subnet = (subnet_t *)node->data;
329       netstr = net2str(subnet);
330       syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
331       free(netstr);
332     }
333   syslog(LOG_DEBUG, _("End of subnet list."));
334 cp
335 }