2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000-2007 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) {
194 subnet->type = SUBNET_IPV4;
195 subnet->net.ipv4.prefixlength = l;
197 for(i = 0; i < 4; i++) {
200 subnet->net.ipv4.address.x[i] = x[i];
206 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
207 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
212 subnet->type = SUBNET_IPV6;
213 subnet->net.ipv6.prefixlength = l;
215 for(i = 0; i < 8; i++)
216 subnet->net.ipv6.address.x[i] = htons(x[i]);
221 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu", &x[0], &x[1], &x[2], &x[3]) == 4) {
222 subnet->type = SUBNET_IPV4;
223 subnet->net.ipv4.prefixlength = 32;
225 for(i = 0; i < 4; i++) {
228 subnet->net.ipv4.address.x[i] = x[i];
234 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
235 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7]) == 8) {
236 subnet->type = SUBNET_IPV6;
237 subnet->net.ipv6.prefixlength = 128;
239 for(i = 0; i < 8; i++)
240 subnet->net.ipv6.address.x[i] = htons(x[i]);
245 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx",
246 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5]) == 6) {
247 subnet->type = SUBNET_MAC;
249 for(i = 0; i < 6; i++)
250 subnet->net.mac.address.x[i] = x[i];
258 bool net2str(char *netstr, int len, const subnet_t *subnet)
262 if(!netstr || !subnet) {
263 logger(LOG_ERR, _("net2str() was called with netstr=%p, subnet=%p!\n"), netstr, subnet);
267 switch (subnet->type) {
269 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx",
270 subnet->net.mac.address.x[0],
271 subnet->net.mac.address.x[1],
272 subnet->net.mac.address.x[2],
273 subnet->net.mac.address.x[3],
274 subnet->net.mac.address.x[4], subnet->net.mac.address.x[5]);
278 snprintf(netstr, len, "%hu.%hu.%hu.%hu/%d",
279 subnet->net.ipv4.address.x[0],
280 subnet->net.ipv4.address.x[1],
281 subnet->net.ipv4.address.x[2],
282 subnet->net.ipv4.address.x[3], subnet->net.ipv4.prefixlength);
286 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
287 ntohs(subnet->net.ipv6.address.x[0]),
288 ntohs(subnet->net.ipv6.address.x[1]),
289 ntohs(subnet->net.ipv6.address.x[2]),
290 ntohs(subnet->net.ipv6.address.x[3]),
291 ntohs(subnet->net.ipv6.address.x[4]),
292 ntohs(subnet->net.ipv6.address.x[5]),
293 ntohs(subnet->net.ipv6.address.x[6]),
294 ntohs(subnet->net.ipv6.address.x[7]),
295 subnet->net.ipv6.prefixlength);
300 _("net2str() was called with unknown subnet type %d, exiting!"),
309 /* Subnet lookup routines */
311 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet)
315 return avl_search(owner->subnet_tree, subnet);
318 subnet_t *lookup_subnet_mac(const mac_t *address)
320 subnet_t *p, subnet = {0};
324 subnet.type = SUBNET_MAC;
325 subnet.net.mac.address = *address;
328 p = avl_search(subnet_tree, &subnet);
333 subnet_t *lookup_subnet_ipv4(const ipv4_t *address)
335 subnet_t *p, subnet = {0};
339 subnet.type = SUBNET_IPV4;
340 subnet.net.ipv4.address = *address;
341 subnet.net.ipv4.prefixlength = 32;
347 p = avl_search_closest_smaller(subnet_tree, &subnet);
349 /* Check if the found subnet REALLY matches */
352 if(p->type != SUBNET_IPV4) {
357 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength))
360 /* Otherwise, see if there is a bigger enclosing subnet */
362 subnet.net.ipv4.prefixlength = p->net.ipv4.prefixlength - 1;
363 if(subnet.net.ipv4.prefixlength < 0 || subnet.net.ipv4.prefixlength > 32)
365 maskcpy(&subnet.net.ipv4.address, &p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t));
373 subnet_t *lookup_subnet_ipv6(const ipv6_t *address)
375 subnet_t *p, subnet = {0};
379 subnet.type = SUBNET_IPV6;
380 subnet.net.ipv6.address = *address;
381 subnet.net.ipv6.prefixlength = 128;
387 p = avl_search_closest_smaller(subnet_tree, &subnet);
389 /* Check if the found subnet REALLY matches */
392 if(p->type != SUBNET_IPV6)
395 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength))
398 /* Otherwise, see if there is a bigger enclosing subnet */
400 subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1;
401 if(subnet.net.ipv6.prefixlength < 0 || subnet.net.ipv6.prefixlength > 128)
403 maskcpy(&subnet.net.ipv6.address, &p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t));
411 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
415 char netstr[MAXNETSTR + 7] = "SUBNET=";
416 char *name, *address, *port;
418 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
419 asprintf(&envp[1], "DEVICE=%s", device ? : "");
420 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
421 asprintf(&envp[3], "NODE=%s", owner->name);
423 if(owner != myself) {
424 sockaddr2str(&owner->address, &address, &port);
425 asprintf(&envp[4], "REMOTEADDRESS=%s", address);
426 asprintf(&envp[5], "REMOTEPORT=%s", port);
434 name = up ? "subnet-up" : "subnet-down";
437 for(node = owner->subnet_tree->head; node; node = node->next) {
439 if(!net2str(netstr + 7, sizeof netstr - 7, subnet))
441 execute_script(name, envp);
444 if(net2str(netstr + 7, sizeof netstr - 7, subnet))
445 execute_script(name, envp);
448 for(i = 0; i < (owner != myself ? 6 : 4); i++)
451 if(owner != myself) {
457 void dump_subnets(void)
459 char netstr[MAXNETSTR];
465 logger(LOG_DEBUG, _("Subnet list:"));
467 for(node = subnet_tree->head; node; node = node->next) {
469 if(!net2str(netstr, sizeof netstr, subnet))
471 logger(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
474 logger(LOG_DEBUG, _("End of subnet list."));