28a203dc21ca66b2e2f91a4db3c7aef49a3d44e5
[oweals/tinc.git] / src / subnet.c
1 /*
2     subnet.c -- handle subnet lookups and lists
3     Copyright (C) 2000 Guus Sliepen <guus@sliepen.warande.net>,
4                   2000 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.16 2001/01/05 23:53:53 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   /* If the subnet of a falls within the range of subnet b,
61      then we consider a smaller then b.
62      Otherwise, the addresses alone (and not the subnet masks) will be compared.
63    */
64    
65   if(a->net.ipv4.mask > b->net.ipv4.mask)
66     if((a->net.ipv4.address & b->net.ipv4.mask) == b->net.ipv4.address)
67       return -1;
68
69   return a->net.ipv4.address - b->net.ipv4.address;
70 }
71
72 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
73 {
74 cp
75   /* Same as ipv4 case, but with nasty 128 bit addresses */
76   
77   if(memcmp(&a->net.ipv6.mask, &b->net.ipv6.mask, sizeof(ipv6_t)) > 0)
78     if((a->net.ipv6.address.x[0] & b->net.ipv6.mask.x[0]) == b->net.ipv6.address.x[0] &&
79        (a->net.ipv6.address.x[1] & b->net.ipv6.mask.x[1]) == b->net.ipv6.address.x[1] &&
80        (a->net.ipv6.address.x[2] & b->net.ipv6.mask.x[2]) == b->net.ipv6.address.x[2] &&
81        (a->net.ipv6.address.x[3] & b->net.ipv6.mask.x[3]) == b->net.ipv6.address.x[3] &&
82        (a->net.ipv6.address.x[4] & b->net.ipv6.mask.x[4]) == b->net.ipv6.address.x[4] &&
83        (a->net.ipv6.address.x[5] & b->net.ipv6.mask.x[5]) == b->net.ipv6.address.x[5] &&
84        (a->net.ipv6.address.x[6] & b->net.ipv6.mask.x[6]) == b->net.ipv6.address.x[6] &&
85        (a->net.ipv6.address.x[7] & b->net.ipv6.mask.x[7]) == b->net.ipv6.address.x[7])
86       return -1;
87   
88   return memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
89 }
90
91 int subnet_compare(subnet_t *a, subnet_t *b)
92 {
93   int x;
94 cp  
95   x = a->type - b->type;
96   if(x)
97     return x;
98     
99   switch(a->type)
100     {
101       case SUBNET_MAC:
102         return subnet_compare_mac(a, b);
103       case SUBNET_IPV4:
104         return subnet_compare_ipv4(a, b);
105       case SUBNET_IPV6:
106         return subnet_compare_ipv6(a, b);
107       default:
108         syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, restarting!"), a->type);
109         sighup = 1;
110         return 0;
111     }
112 }
113
114 /* Allocating and freeing space for subnets */
115
116 subnet_t *new_subnet(void)
117 {
118 cp
119   return (subnet_t *)xmalloc(sizeof(subnet_t));
120 }
121
122 void free_subnet(subnet_t *subnet)
123 {
124 cp
125   free(subnet);
126 }
127
128 /* Linked list management */
129
130 void subnet_add(connection_t *cl, subnet_t *subnet)
131 {
132 cp
133   subnet->owner = cl;
134   avl_insert(subnet_tree, subnet);
135   avl_insert(cl->subnet_tree, subnet);
136 cp
137 }
138
139 void subnet_del(subnet_t *subnet)
140 {
141 cp
142   avl_delete(subnet->owner->subnet_tree, subnet);
143 cp
144   avl_delete(subnet_tree, subnet);
145 cp
146 }
147
148 /* Ascii representation of subnets */
149
150 subnet_t *str2net(char *subnetstr)
151 {
152   int type;
153   subnet_t *subnet;
154 cp
155   if(sscanf(subnetstr, "%d,", &type) != 1)
156     return NULL;
157 cp
158   subnet = new_subnet();
159 cp
160   switch(type)
161     {
162       case SUBNET_MAC:
163         if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
164                    &subnet->net.mac.address.x[0],
165                    &subnet->net.mac.address.x[1],
166                    &subnet->net.mac.address.x[2],
167                    &subnet->net.mac.address.x[3],
168                    &subnet->net.mac.address.x[4],
169                    &subnet->net.mac.address.x[5]) != 7)
170           {
171             free_subnet(subnet);
172             return NULL;
173           }
174         break;
175       case SUBNET_IPV4:
176         if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
177           {
178             free_subnet(subnet);
179             return NULL;
180           }
181         break;
182       case SUBNET_IPV6:
183         if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
184                    &subnet->net.ipv6.address.x[0],
185                    &subnet->net.ipv6.address.x[1],
186                    &subnet->net.ipv6.address.x[2],
187                    &subnet->net.ipv6.address.x[3],
188                    &subnet->net.ipv6.address.x[4],
189                    &subnet->net.ipv6.address.x[5],
190                    &subnet->net.ipv6.address.x[6],
191                    &subnet->net.ipv6.address.x[7],
192                    &subnet->net.ipv6.mask.x[0],
193                    &subnet->net.ipv6.mask.x[1],
194                    &subnet->net.ipv6.mask.x[2],
195                    &subnet->net.ipv6.mask.x[3],
196                    &subnet->net.ipv6.mask.x[4],
197                    &subnet->net.ipv6.mask.x[5],
198                    &subnet->net.ipv6.mask.x[6],
199                    &subnet->net.ipv6.mask.x[7]) != 17)
200           {
201             free_subnet(subnet);
202             return NULL;
203           }
204         break;
205       default:
206         free_subnet(subnet);
207         return NULL;
208     }
209 cp
210   return subnet;
211 }
212
213 char *net2str(subnet_t *subnet)
214 {
215   char *netstr;
216 cp
217   switch(subnet->type)
218     {
219       case SUBNET_MAC:
220         asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
221                    subnet->net.mac.address.x[0],
222                    subnet->net.mac.address.x[1],
223                    subnet->net.mac.address.x[2],
224                    subnet->net.mac.address.x[3],
225                    subnet->net.mac.address.x[4],
226                    subnet->net.mac.address.x[5]);
227         break;
228       case SUBNET_IPV4:
229         asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
230         break;
231       case SUBNET_IPV6:
232         asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
233                    subnet->net.ipv6.address.x[0],
234                    subnet->net.ipv6.address.x[1],
235                    subnet->net.ipv6.address.x[2],
236                    subnet->net.ipv6.address.x[3],
237                    subnet->net.ipv6.address.x[4],
238                    subnet->net.ipv6.address.x[5],
239                    subnet->net.ipv6.address.x[6],
240                    subnet->net.ipv6.address.x[7],
241                    subnet->net.ipv6.mask.x[0],
242                    subnet->net.ipv6.mask.x[1],
243                    subnet->net.ipv6.mask.x[2],
244                    subnet->net.ipv6.mask.x[3],
245                    subnet->net.ipv6.mask.x[4],
246                    subnet->net.ipv6.mask.x[5],
247                    subnet->net.ipv6.mask.x[6],
248                    subnet->net.ipv6.mask.x[7]);
249         break;
250       default:
251         asprintf(&netstr, _("unknown"));
252     }
253 cp
254   return netstr;
255 }
256
257 /* Subnet lookup routines */
258
259 subnet_t *lookup_subnet_mac(mac_t *address)
260 {
261   subnet_t subnet, *p;
262 cp
263   subnet.type = SUBNET_MAC;
264   memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
265
266   p = (subnet_t *)avl_search(subnet_tree, &subnet);
267 cp
268   return p;
269 }
270
271 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
272 {
273   subnet_t subnet, *p;
274 cp
275   subnet.type = SUBNET_IPV4;
276   subnet.net.ipv4.address = *address;
277   subnet.net.ipv4.mask = 0xFFFFFFFF;
278
279   p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet);
280
281   /* Check if the found subnet REALLY matches */
282 cp
283   if(p && ((*address & p->net.ipv4.mask) == p->net.ipv4.address))
284     return p;
285   else
286     return NULL;
287 }
288
289 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
290 {
291   subnet_t subnet;
292 cp
293   subnet.type = SUBNET_IPV6;
294   memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
295   memset(&subnet.net.ipv6.mask, 0xFF, 16);
296   
297 /* FIXME: check if it REALLY matches */
298
299   return (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet);
300 }
301
302 void dump_subnet_list(void)
303 {
304   char *netstr;
305   subnet_t *subnet;
306   avl_node_t *node;
307 cp
308   syslog(LOG_DEBUG, _("Subnet list:"));
309   for(node = subnet_tree->head; node; node = node->next)
310     {
311       subnet = (subnet_t *)node->data;
312       netstr = net2str(subnet);
313       syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
314       free(netstr);
315     }
316   syslog(LOG_DEBUG, _("End of subnet list."));
317 cp
318 }