2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000-2006 Guus Sliepen <guus@tinc-vpn.org>,
4 2000-2005 Ivo Timmermans
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.
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.
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.
36 /* lists type of subnet */
38 avl_tree_t *subnet_tree;
40 /* Subnet comparison */
42 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b)
46 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
48 if(result || !a->owner || !b->owner)
51 return strcmp(a->owner->name, b->owner->name);
54 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b)
58 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
63 result = a->net.ipv4.prefixlength - b->net.ipv4.prefixlength;
65 if(result || !a->owner || !b->owner)
68 return strcmp(a->owner->name, b->owner->name);
71 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b)
75 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
80 result = a->net.ipv6.prefixlength - b->net.ipv6.prefixlength;
82 if(result || !a->owner || !b->owner)
85 return strcmp(a->owner->name, b->owner->name);
88 int subnet_compare(const subnet_t *a, const subnet_t *b)
92 result = a->type - b->type;
99 return subnet_compare_mac(a, b);
101 return subnet_compare_ipv4(a, b);
103 return subnet_compare_ipv6(a, b);
105 logger(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, exitting!"),
114 /* Initialising trees */
116 void init_subnets(void)
120 subnet_tree = avl_alloc_tree((avl_compare_t) subnet_compare, (avl_action_t) free_subnet);
123 void exit_subnets(void)
127 avl_delete_tree(subnet_tree);
130 avl_tree_t *new_subnet_tree(void)
134 return avl_alloc_tree((avl_compare_t) subnet_compare, NULL);
137 void free_subnet_tree(avl_tree_t *subnet_tree)
141 avl_delete_tree(subnet_tree);
144 /* Allocating and freeing space for subnets */
146 subnet_t *new_subnet(void)
150 return xmalloc_and_zero(sizeof(subnet_t));
153 void free_subnet(subnet_t *subnet)
160 /* Adding and removing subnets */
162 void subnet_add(node_t *n, subnet_t *subnet)
168 avl_insert(subnet_tree, subnet);
169 avl_insert(n->subnet_tree, subnet);
172 void subnet_del(node_t *n, subnet_t *subnet)
176 avl_delete(n->subnet_tree, subnet);
177 avl_delete(subnet_tree, subnet);
180 /* Ascii representation of subnets */
182 bool str2net(subnet_t *subnet, const char *subnetstr)
189 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d",
190 &x[0], &x[1], &x[2], &x[3], &l) == 5) {
191 subnet->type = SUBNET_IPV4;
192 subnet->net.ipv4.prefixlength = l;
194 for(i = 0; i < 4; i++)
195 subnet->net.ipv4.address.x[i] = x[i];
200 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
201 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
203 subnet->type = SUBNET_IPV6;
204 subnet->net.ipv6.prefixlength = l;
206 for(i = 0; i < 8; i++)
207 subnet->net.ipv6.address.x[i] = htons(x[i]);
212 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu", &x[0], &x[1], &x[2], &x[3]) == 4) {
213 subnet->type = SUBNET_IPV4;
214 subnet->net.ipv4.prefixlength = 32;
216 for(i = 0; i < 4; i++)
217 subnet->net.ipv4.address.x[i] = x[i];
222 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
223 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7]) == 8) {
224 subnet->type = SUBNET_IPV6;
225 subnet->net.ipv6.prefixlength = 128;
227 for(i = 0; i < 8; i++)
228 subnet->net.ipv6.address.x[i] = htons(x[i]);
233 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx",
234 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5]) == 6) {
235 subnet->type = SUBNET_MAC;
237 for(i = 0; i < 6; i++)
238 subnet->net.mac.address.x[i] = x[i];
246 bool net2str(char *netstr, int len, const subnet_t *subnet)
250 if(!netstr || !subnet) {
251 logger(LOG_ERR, _("net2str() was called with netstr=%p, subnet=%p!\n"), netstr, subnet);
255 switch (subnet->type) {
257 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx",
258 subnet->net.mac.address.x[0],
259 subnet->net.mac.address.x[1],
260 subnet->net.mac.address.x[2],
261 subnet->net.mac.address.x[3],
262 subnet->net.mac.address.x[4], subnet->net.mac.address.x[5]);
266 snprintf(netstr, len, "%hu.%hu.%hu.%hu/%d",
267 subnet->net.ipv4.address.x[0],
268 subnet->net.ipv4.address.x[1],
269 subnet->net.ipv4.address.x[2],
270 subnet->net.ipv4.address.x[3], subnet->net.ipv4.prefixlength);
274 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
275 ntohs(subnet->net.ipv6.address.x[0]),
276 ntohs(subnet->net.ipv6.address.x[1]),
277 ntohs(subnet->net.ipv6.address.x[2]),
278 ntohs(subnet->net.ipv6.address.x[3]),
279 ntohs(subnet->net.ipv6.address.x[4]),
280 ntohs(subnet->net.ipv6.address.x[5]),
281 ntohs(subnet->net.ipv6.address.x[6]),
282 ntohs(subnet->net.ipv6.address.x[7]),
283 subnet->net.ipv6.prefixlength);
288 _("net2str() was called with unknown subnet type %d, exiting!"),
297 /* Subnet lookup routines */
299 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet)
303 return avl_search(owner->subnet_tree, subnet);
306 subnet_t *lookup_subnet_mac(const mac_t *address)
308 subnet_t *p, subnet = {0};
312 subnet.type = SUBNET_MAC;
313 subnet.net.mac.address = *address;
316 p = avl_search(subnet_tree, &subnet);
321 subnet_t *lookup_subnet_ipv4(const ipv4_t *address)
323 subnet_t *p, subnet = {0};
327 subnet.type = SUBNET_IPV4;
328 subnet.net.ipv4.address = *address;
329 subnet.net.ipv4.prefixlength = 32;
335 p = avl_search_closest_smaller(subnet_tree, &subnet);
337 /* Check if the found subnet REALLY matches */
340 if(p->type != SUBNET_IPV4) {
345 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength))
348 /* Otherwise, see if there is a bigger enclosing subnet */
350 subnet.net.ipv4.prefixlength = p->net.ipv4.prefixlength - 1;
351 maskcpy(&subnet.net.ipv4.address, &p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t));
359 subnet_t *lookup_subnet_ipv6(const ipv6_t *address)
361 subnet_t *p, subnet = {0};
365 subnet.type = SUBNET_IPV6;
366 subnet.net.ipv6.address = *address;
367 subnet.net.ipv6.prefixlength = 128;
373 p = avl_search_closest_smaller(subnet_tree, &subnet);
375 /* Check if the found subnet REALLY matches */
378 if(p->type != SUBNET_IPV6)
381 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength))
384 /* Otherwise, see if there is a bigger enclosing subnet */
386 subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1;
387 maskcpy(&subnet.net.ipv6.address, &p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t));
395 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
399 char netstr[MAXNETSTR + 7] = "SUBNET=";
400 char *name, *address, *port;
402 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
403 asprintf(&envp[1], "DEVICE=%s", device ? : "");
404 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
405 asprintf(&envp[3], "NODE=%s", owner->name);
407 if(owner != myself) {
408 sockaddr2str(&owner->address, &address, &port);
409 asprintf(&envp[4], "REMOTEADDRESS=%s", address);
410 asprintf(&envp[5], "REMOTEPORT=%s", port);
418 name = up ? "subnet-up" : "subnet-down";
421 for(node = owner->subnet_tree->head; node; node = node->next) {
423 if(!net2str(netstr + 7, sizeof netstr - 7, subnet))
425 execute_script(name, envp);
428 if(net2str(netstr + 7, sizeof netstr - 7, subnet))
429 execute_script(name, envp);
432 for(i = 0; i < (owner != myself ? 6 : 4); i++)
435 if(owner != myself) {
441 void dump_subnets(void)
443 char netstr[MAXNETSTR];
449 logger(LOG_DEBUG, _("Subnet list:"));
451 for(node = subnet_tree->head; node; node = node->next) {
453 if(!net2str(netstr, sizeof netstr, subnet))
455 logger(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
458 logger(LOG_DEBUG, _("End of subnet list."));