Hm.
[oweals/tinc.git] / lib / subnet.c
1 /*
2     subnet.c -- handle subnet lookups and lists
3     Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.warande.net>,
4                   2000-2002 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 2002/04/28 12:46:26 zarq Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <netdb.h>
30 #include <netinet/in.h>
31
32 #include <hooks.h>
33 #include <utils.h>
34 #include <xalloc.h>
35 #include <avl_tree.h>
36
37 #include "conf.h"
38 #include "net.h"
39 #include "node.h"
40 #include "subnet.h"
41 #include "netutl.h"
42 #include "logging.h"
43
44 #include "system.h"
45
46 /* lists type of subnet */
47
48 avl_tree_t *subnet_tree;
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   int result;
61 cp
62   result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
63   
64   if(result)
65     return result;
66
67   return a->net.ipv4.prefixlength - b->net.ipv4.prefixlength;
68 }
69
70 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
71 {
72   int result;
73 cp
74   result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
75   
76   if(result)
77     return result;
78
79   return a->net.ipv6.prefixlength - b->net.ipv6.prefixlength;
80 }
81
82 int subnet_compare(subnet_t *a, subnet_t *b)
83 {
84   int result;
85 cp  
86   result = a->type - b->type;
87  
88   if(result)
89     return result;
90     
91   switch(a->type)
92     {
93       case SUBNET_MAC:
94         return subnet_compare_mac(a, b);
95       case SUBNET_IPV4:
96         return subnet_compare_ipv4(a, b);
97       case SUBNET_IPV6:
98         return subnet_compare_ipv6(a, b);
99       default:
100         syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, exitting!"), a->type);
101         cp_trace();
102         exit(0);
103     }
104
105   return 0;
106 }
107
108 /* Initialising trees */
109
110 void init_subnets(void)
111 {
112 cp
113   subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet);
114 cp
115 }
116
117 void exit_subnets(void)
118 {
119 cp
120   avl_delete_tree(subnet_tree);
121 cp
122 }
123
124 avl_tree_t *new_subnet_tree(void)
125 {
126 cp
127   return avl_alloc_tree((avl_compare_t)subnet_compare, NULL);
128 cp
129 }
130
131 void free_subnet_tree(avl_tree_t *subnet_tree)
132 {
133 cp
134   avl_delete_tree(subnet_tree);
135 cp
136 }
137
138 /* Allocating and freeing space for subnets */
139
140 subnet_t *new_subnet(void)
141 {
142 cp
143   return (subnet_t *)xmalloc(sizeof(subnet_t));
144 }
145
146 void free_subnet(subnet_t *subnet)
147 {
148 cp
149   free(subnet);
150 }
151
152 /* Adding and removing subnets */
153
154 void subnet_add(node_t *n, subnet_t *subnet)
155 {
156 cp
157   subnet->owner = n;
158
159   avl_insert(subnet_tree, subnet);
160 cp
161   avl_insert(n->subnet_tree, subnet);
162
163   run_hooks("subnet-add", subnet);
164 cp
165 }
166
167 void subnet_del(node_t *n, subnet_t *subnet)
168 {
169 cp
170   avl_delete(n->subnet_tree, subnet);
171 cp
172   avl_delete(subnet_tree, subnet);
173
174   run_hooks("subnet-del", subnet);
175 cp
176 }
177
178 /* Ascii representation of subnets */
179
180 subnet_t *str2net(char *subnetstr)
181 {
182   int i, l;
183   subnet_t *subnet;
184   unsigned short int x[8];
185 cp
186   subnet = new_subnet();
187 cp
188   if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d",
189               &x[0], &x[1], &x[2], &x[3],
190               &l) == 5)
191     {
192       subnet->type = SUBNET_IPV4;
193       subnet->net.ipv4.prefixlength = l;
194       for(i = 0; i < 4; i++)
195         subnet->net.ipv4.address.x[i] = x[i];
196       return subnet;
197     }
198               
199   if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
200              &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
201              &l) == 9)
202     {
203       subnet->type = SUBNET_IPV6;
204       subnet->net.ipv6.prefixlength = l;
205       for(i = 0; i < 8; i++)
206         subnet->net.ipv6.address.x[i] = htons(x[i]);
207       return subnet;
208     }
209
210   if(sscanf(subnetstr, "%hu.%hu.%hu.%hu",
211               &x[0], &x[1], &x[2], &x[3]) == 4)
212     {
213       subnet->type = SUBNET_IPV4;
214       subnet->net.ipv4.prefixlength = 32;
215       for(i = 0; i < 4; i++)
216         subnet->net.ipv4.address.x[i] = x[i];
217       return subnet;
218     }
219               
220   if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
221              &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7]) == 8)
222     {
223       subnet->type = SUBNET_IPV6;
224       subnet->net.ipv6.prefixlength = 128;
225       for(i = 0; i < 8; i++)
226         subnet->net.ipv6.address.x[i] = htons(x[i]);
227       return subnet;
228     }
229
230   if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx",
231               &x[0], &x[1], &x[2], &x[3], &x[4], &x[5]) == 6)
232     {
233       subnet->type = SUBNET_MAC;
234       for(i = 0; i < 6; i++)
235         subnet->net.mac.address.x[i] = x[i];
236       return subnet;
237     }
238
239   free(subnet);
240   return NULL;
241 }
242
243 char *net2str(subnet_t *subnet)
244 {
245   char *netstr;
246 cp
247   switch(subnet->type)
248     {
249       case SUBNET_MAC:
250         asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx",
251                    subnet->net.mac.address.x[0],
252                    subnet->net.mac.address.x[1],
253                    subnet->net.mac.address.x[2],
254                    subnet->net.mac.address.x[3],
255                    subnet->net.mac.address.x[4],
256                    subnet->net.mac.address.x[5]);
257         break;
258       case SUBNET_IPV4:
259         asprintf(&netstr, "%hu.%hu.%hu.%hu/%d",
260                    subnet->net.ipv4.address.x[0],
261                    subnet->net.ipv4.address.x[1],
262                    subnet->net.ipv4.address.x[2],
263                    subnet->net.ipv4.address.x[3],
264                    subnet->net.ipv4.prefixlength);
265         break;
266       case SUBNET_IPV6:
267         asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
268                    ntohs(subnet->net.ipv6.address.x[0]),
269                    ntohs(subnet->net.ipv6.address.x[1]),
270                    ntohs(subnet->net.ipv6.address.x[2]),
271                    ntohs(subnet->net.ipv6.address.x[3]),
272                    ntohs(subnet->net.ipv6.address.x[4]),
273                    ntohs(subnet->net.ipv6.address.x[5]),
274                    ntohs(subnet->net.ipv6.address.x[6]),
275                    ntohs(subnet->net.ipv6.address.x[7]),
276                    subnet->net.ipv6.prefixlength);
277         break;
278       default:
279         syslog(LOG_ERR, _("net2str() was called with unknown subnet type %d, exitting!"), subnet->type);
280         cp_trace();
281         exit(0);
282     }
283 cp
284   return netstr;
285 }
286
287 /* Subnet lookup routines */
288
289 subnet_t *lookup_subnet(node_t *owner, subnet_t *subnet)
290 {
291 cp  
292   return avl_search(owner->subnet_tree, subnet);
293 }
294
295 subnet_t *lookup_subnet_mac(mac_t *address)
296 {
297   subnet_t subnet, *p;
298 cp
299   subnet.type = SUBNET_MAC;
300   memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
301
302   p = (subnet_t *)avl_search(subnet_tree, &subnet);
303 cp
304   return p;
305 }
306
307 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
308 {
309   subnet_t subnet, *p;
310 cp
311   subnet.type = SUBNET_IPV4;
312   memcpy(&subnet.net.ipv4.address, address, sizeof(ipv4_t));
313   subnet.net.ipv4.prefixlength = 32;
314
315   do
316   {
317     /* Go find subnet */
318   
319     p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
320
321   /* Check if the found subnet REALLY matches */
322 cp
323     if(p)
324       {
325         if(p->type != SUBNET_IPV4)
326           {
327             p = NULL;
328             break;
329           }
330
331         if (!maskcmp((char *)address, (char *)&p->net.ipv4.address, p->net.ipv4.prefixlength, sizeof(ipv4_t)))
332           break;
333         else
334           {
335             /* Otherwise, see if there is a bigger enclosing subnet */
336
337             subnet.net.ipv4.prefixlength = p->net.ipv4.prefixlength - 1;
338             maskcpy((char *)&subnet.net.ipv4.address, (char *)&p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t));
339           }
340       }
341   } while (p);
342 cp
343   return p;
344 }
345
346 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
347 {
348   subnet_t subnet, *p;
349 cp
350   subnet.type = SUBNET_IPV6;
351   memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
352   subnet.net.ipv6.prefixlength = 128;
353   
354   do
355   {
356     /* Go find subnet */
357   
358     p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
359
360     /* Check if the found subnet REALLY matches */
361
362 cp
363     if(p)
364       {
365         if(p->type != SUBNET_IPV6)
366           return NULL;
367
368         if (!maskcmp((char *)address, (char *)&p->net.ipv6.address, p->net.ipv6.prefixlength, sizeof(ipv6_t)))
369           break;
370         else
371           {
372             /* Otherwise, see if there is a bigger enclosing subnet */
373
374             subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1;
375             maskcpy((char *)&subnet.net.ipv6.address, (char *)&p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t));
376           }
377       }
378    } while (p);
379 cp   
380   return p;
381 }
382
383 void dump_subnets(void)
384 {
385   char *netstr;
386   subnet_t *subnet;
387   avl_node_t *node;
388 cp
389   syslog(LOG_DEBUG, _("Subnet list:"));
390   for(node = subnet_tree->head; node; node = node->next)
391     {
392       subnet = (subnet_t *)node->data;
393       netstr = net2str(subnet);
394       syslog(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
395       free(netstr);
396     }
397   syslog(LOG_DEBUG, _("End of subnet list."));
398 cp
399 }