b2ced415934514b3636b3dbf60c91fe111f2ede8
[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.12 2000/11/20 19:12:17 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 = { NULL };
41 rbltree_t *subnet_tree = &_subnet_tree;
42
43 /* Subnet comparison */
44
45 int subnet_compare_mac(subnet_t *a, subnet_t *b)
46 {
47 cp
48   return memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
49 }
50
51 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
52 {
53 cp
54   /* If the subnet of a falls within the range of subnet b,
55      then we consider a smaller then b.
56      Otherwise, the addresses alone (and not the subnet masks) will be compared.
57    */
58    
59   if(a->net.ipv4.mask > b->net.ipv4.mask)
60     if((a->net.ipv4.address & b->net.ipv4.mask) == b->net.ipv4.address)
61       return -1;
62
63   return a->net.ipv4.address - b->net.ipv4.address;
64 }
65
66 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
67 {
68 cp
69   /* Same as ipv4 case, but with nasty 128 bit addresses */
70   
71   if(memcmp(&a->net.ipv6.mask, &b->net.ipv6.mask, sizeof(ipv6_t)) > 0)
72     if((a->net.ipv6.address.x[0] & b->net.ipv6.mask.x[0]) == b->net.ipv6.address.x[0] &&
73        (a->net.ipv6.address.x[1] & b->net.ipv6.mask.x[1]) == b->net.ipv6.address.x[1] &&
74        (a->net.ipv6.address.x[2] & b->net.ipv6.mask.x[2]) == b->net.ipv6.address.x[2] &&
75        (a->net.ipv6.address.x[3] & b->net.ipv6.mask.x[3]) == b->net.ipv6.address.x[3] &&
76        (a->net.ipv6.address.x[4] & b->net.ipv6.mask.x[4]) == b->net.ipv6.address.x[4] &&
77        (a->net.ipv6.address.x[5] & b->net.ipv6.mask.x[5]) == b->net.ipv6.address.x[5] &&
78        (a->net.ipv6.address.x[6] & b->net.ipv6.mask.x[6]) == b->net.ipv6.address.x[6] &&
79        (a->net.ipv6.address.x[7] & b->net.ipv6.mask.x[7]) == b->net.ipv6.address.x[7])
80       return -1;
81   
82   return memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
83 }
84
85 int subnet_compare(subnet_t *a, subnet_t *b)
86 {
87   int x;
88 cp  
89   x = a->type - b->type;
90   if(x)
91     return x;
92     
93   switch(a->type)
94     {
95       case SUBNET_MAC:
96         return subnet_compare_mac(a, b);
97       case SUBNET_IPV4:
98         return subnet_compare_ipv4(a, b);
99       case SUBNET_IPV6:
100         return subnet_compare_ipv6(a, b);
101       default:
102         syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, restarting!"), a->type);
103         sighup = 1;
104         return 0;
105     }
106 }
107
108 /* Allocating and freeing space for subnets */
109
110 subnet_t *new_subnet(void)
111 {
112 cp
113   return (subnet_t *)xmalloc(sizeof(subnet_t));
114 }
115
116 void free_subnet(subnet_t *subnet)
117 {
118 cp
119   free(subnet);
120 }
121
122 /* Linked list management */
123
124 void subnet_add(connection_t *cl, subnet_t *subnet)
125 {
126 cp
127   rbl_insert(subnet_tree, subnet);
128   rbl_insert(cl->subnet_tree, subnet);
129 cp
130 }
131
132 void subnet_del(subnet_t *subnet)
133 {
134 cp
135   free_rbl(rbl_unlink(subnet->owner->subnet_tree, subnet));
136   rbl_delete(subnet_tree, subnet);
137 cp
138 }
139
140 /* Ascii representation of subnets */
141
142 subnet_t *str2net(char *subnetstr)
143 {
144   int type;
145   subnet_t *subnet;
146 cp
147   if(sscanf(subnetstr, "%d,", &type) != 1)
148     return NULL;
149 cp
150   subnet = new_subnet();
151 cp
152   switch(type)
153     {
154       case SUBNET_MAC:
155         if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
156                    &subnet->net.mac.address.x[0],
157                    &subnet->net.mac.address.x[1],
158                    &subnet->net.mac.address.x[2],
159                    &subnet->net.mac.address.x[3],
160                    &subnet->net.mac.address.x[4],
161                    &subnet->net.mac.address.x[5]) != 7)
162           {
163             free_subnet(subnet);
164             return NULL;
165           }
166         break;
167       case SUBNET_IPV4:
168         if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
169           {
170             free_subnet(subnet);
171             return NULL;
172           }
173         break;
174       case SUBNET_IPV6:
175         if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
176                    &subnet->net.ipv6.address.x[0],
177                    &subnet->net.ipv6.address.x[1],
178                    &subnet->net.ipv6.address.x[2],
179                    &subnet->net.ipv6.address.x[3],
180                    &subnet->net.ipv6.address.x[4],
181                    &subnet->net.ipv6.address.x[5],
182                    &subnet->net.ipv6.address.x[6],
183                    &subnet->net.ipv6.address.x[7],
184                    &subnet->net.ipv6.mask.x[0],
185                    &subnet->net.ipv6.mask.x[1],
186                    &subnet->net.ipv6.mask.x[2],
187                    &subnet->net.ipv6.mask.x[3],
188                    &subnet->net.ipv6.mask.x[4],
189                    &subnet->net.ipv6.mask.x[5],
190                    &subnet->net.ipv6.mask.x[6],
191                    &subnet->net.ipv6.mask.x[7]) != 17)
192           {
193             free_subnet(subnet);
194             return NULL;
195           }
196         break;
197       default:
198         free_subnet(subnet);
199         return NULL;
200     }
201 cp
202   return subnet;
203 }
204
205 char *net2str(subnet_t *subnet)
206 {
207   char *netstr;
208 cp
209   switch(subnet->type)
210     {
211       case SUBNET_MAC:
212         asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
213                    subnet->net.mac.address.x[0],
214                    subnet->net.mac.address.x[1],
215                    subnet->net.mac.address.x[2],
216                    subnet->net.mac.address.x[3],
217                    subnet->net.mac.address.x[4],
218                    subnet->net.mac.address.x[5]);
219         break;
220       case SUBNET_IPV4:
221         asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
222         break;
223       case SUBNET_IPV6:
224         asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
225                    subnet->net.ipv6.address.x[0],
226                    subnet->net.ipv6.address.x[1],
227                    subnet->net.ipv6.address.x[2],
228                    subnet->net.ipv6.address.x[3],
229                    subnet->net.ipv6.address.x[4],
230                    subnet->net.ipv6.address.x[5],
231                    subnet->net.ipv6.address.x[6],
232                    subnet->net.ipv6.address.x[7],
233                    subnet->net.ipv6.mask.x[0],
234                    subnet->net.ipv6.mask.x[1],
235                    subnet->net.ipv6.mask.x[2],
236                    subnet->net.ipv6.mask.x[3],
237                    subnet->net.ipv6.mask.x[4],
238                    subnet->net.ipv6.mask.x[5],
239                    subnet->net.ipv6.mask.x[6],
240                    subnet->net.ipv6.mask.x[7]);
241         break;
242       default:
243         asprintf(&netstr, _("unknown"));
244     }
245 cp
246   return netstr;
247 }
248
249 /* Subnet lookup routines */
250
251 subnet_t *lookup_subnet_mac(mac_t address)
252 {
253   subnet_t subnet;
254 cp
255   subnet.type = SUBNET_MAC;
256   subnet.net.mac.address = address;
257   return (subnet_t *)rbl_search_closest(subnet_tree, &subnet);
258 }
259
260 subnet_t *lookup_subnet_ipv4(ipv4_t address)
261 {
262   subnet_t subnet;
263 cp
264   subnet.type = SUBNET_IPV4;
265   subnet.net.ipv4.address = address;
266   subnet.net.ipv4.mask = 0xFFFFFFFF;
267   return (subnet_t *)rbl_search_closest(subnet_tree, &subnet);
268 }
269
270 subnet_t *lookup_subnet_ipv6(ipv6_t address)
271 {
272   subnet_t subnet;
273 cp
274   subnet.type = SUBNET_IPV6;
275   subnet.net.ipv6.address = address;
276   memset(&subnet.net.ipv6.mask, 0xFF, 16);
277   return (subnet_t *)rbl_search_closest(subnet_tree, &subnet);
278 }
279
280 void dump_subnet_list(void)
281 {
282   char *netstr;
283   subnet_t *subnet;
284   rbl_t *rbl;
285 cp
286   syslog(LOG_DEBUG, _("Subnet list:"));
287   RBL_FOREACH(subnet_tree, rbl)
288     {
289       subnet = (subnet_t *)rbl->data;
290       netstr = net2str(subnet);
291       syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
292       free(netstr);
293     }
294   syslog(LOG_DEBUG, _("End of subnet list."));
295 cp
296 }