- Proper initialization of rbltree structures.
[tinc] / 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.13 2000/11/20 19:41:13 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 <rbl.h>
37
38 /* lists type of subnet */
39
40 rbltree_t *subnet_tree;
41
42 void init_subnets(void)
43 {
44 cp
45   subnet_tree = new_rbltree((rbl_compare_t)subnet_compare, (rbl_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   rbl_insert(subnet_tree, subnet);
134   rbl_insert(cl->subnet_tree, subnet);
135 cp
136 }
137
138 void subnet_del(subnet_t *subnet)
139 {
140 cp
141   rbl_delete(subnet->owner->subnet_tree, subnet);
142   rbl_delete(subnet_tree, subnet);
143 cp
144 }
145
146 /* Ascii representation of subnets */
147
148 subnet_t *str2net(char *subnetstr)
149 {
150   int type;
151   subnet_t *subnet;
152 cp
153   if(sscanf(subnetstr, "%d,", &type) != 1)
154     return NULL;
155 cp
156   subnet = new_subnet();
157 cp
158   switch(type)
159     {
160       case SUBNET_MAC:
161         if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
162                    &subnet->net.mac.address.x[0],
163                    &subnet->net.mac.address.x[1],
164                    &subnet->net.mac.address.x[2],
165                    &subnet->net.mac.address.x[3],
166                    &subnet->net.mac.address.x[4],
167                    &subnet->net.mac.address.x[5]) != 7)
168           {
169             free_subnet(subnet);
170             return NULL;
171           }
172         break;
173       case SUBNET_IPV4:
174         if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
175           {
176             free_subnet(subnet);
177             return NULL;
178           }
179         break;
180       case SUBNET_IPV6:
181         if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
182                    &subnet->net.ipv6.address.x[0],
183                    &subnet->net.ipv6.address.x[1],
184                    &subnet->net.ipv6.address.x[2],
185                    &subnet->net.ipv6.address.x[3],
186                    &subnet->net.ipv6.address.x[4],
187                    &subnet->net.ipv6.address.x[5],
188                    &subnet->net.ipv6.address.x[6],
189                    &subnet->net.ipv6.address.x[7],
190                    &subnet->net.ipv6.mask.x[0],
191                    &subnet->net.ipv6.mask.x[1],
192                    &subnet->net.ipv6.mask.x[2],
193                    &subnet->net.ipv6.mask.x[3],
194                    &subnet->net.ipv6.mask.x[4],
195                    &subnet->net.ipv6.mask.x[5],
196                    &subnet->net.ipv6.mask.x[6],
197                    &subnet->net.ipv6.mask.x[7]) != 17)
198           {
199             free_subnet(subnet);
200             return NULL;
201           }
202         break;
203       default:
204         free_subnet(subnet);
205         return NULL;
206     }
207 cp
208   return subnet;
209 }
210
211 char *net2str(subnet_t *subnet)
212 {
213   char *netstr;
214 cp
215   switch(subnet->type)
216     {
217       case SUBNET_MAC:
218         asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
219                    subnet->net.mac.address.x[0],
220                    subnet->net.mac.address.x[1],
221                    subnet->net.mac.address.x[2],
222                    subnet->net.mac.address.x[3],
223                    subnet->net.mac.address.x[4],
224                    subnet->net.mac.address.x[5]);
225         break;
226       case SUBNET_IPV4:
227         asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
228         break;
229       case SUBNET_IPV6:
230         asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
231                    subnet->net.ipv6.address.x[0],
232                    subnet->net.ipv6.address.x[1],
233                    subnet->net.ipv6.address.x[2],
234                    subnet->net.ipv6.address.x[3],
235                    subnet->net.ipv6.address.x[4],
236                    subnet->net.ipv6.address.x[5],
237                    subnet->net.ipv6.address.x[6],
238                    subnet->net.ipv6.address.x[7],
239                    subnet->net.ipv6.mask.x[0],
240                    subnet->net.ipv6.mask.x[1],
241                    subnet->net.ipv6.mask.x[2],
242                    subnet->net.ipv6.mask.x[3],
243                    subnet->net.ipv6.mask.x[4],
244                    subnet->net.ipv6.mask.x[5],
245                    subnet->net.ipv6.mask.x[6],
246                    subnet->net.ipv6.mask.x[7]);
247         break;
248       default:
249         asprintf(&netstr, _("unknown"));
250     }
251 cp
252   return netstr;
253 }
254
255 /* Subnet lookup routines */
256
257 subnet_t *lookup_subnet_mac(mac_t address)
258 {
259   subnet_t subnet;
260 cp
261   subnet.type = SUBNET_MAC;
262   subnet.net.mac.address = address;
263   return (subnet_t *)rbl_search_closest(subnet_tree, &subnet);
264 }
265
266 subnet_t *lookup_subnet_ipv4(ipv4_t address)
267 {
268   subnet_t subnet;
269 cp
270   subnet.type = SUBNET_IPV4;
271   subnet.net.ipv4.address = address;
272   subnet.net.ipv4.mask = 0xFFFFFFFF;
273   return (subnet_t *)rbl_search_closest(subnet_tree, &subnet);
274 }
275
276 subnet_t *lookup_subnet_ipv6(ipv6_t address)
277 {
278   subnet_t subnet;
279 cp
280   subnet.type = SUBNET_IPV6;
281   subnet.net.ipv6.address = address;
282   memset(&subnet.net.ipv6.mask, 0xFF, 16);
283   return (subnet_t *)rbl_search_closest(subnet_tree, &subnet);
284 }
285
286 void dump_subnet_list(void)
287 {
288   char *netstr;
289   subnet_t *subnet;
290   rbl_t *rbl;
291 cp
292   syslog(LOG_DEBUG, _("Subnet list:"));
293   RBL_FOREACH(subnet_tree, rbl)
294     {
295       subnet = (subnet_t *)rbl->data;
296       netstr = net2str(subnet);
297       syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
298       free(netstr);
299     }
300   syslog(LOG_DEBUG, _("End of subnet list."));
301 cp
302 }