2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000-2012 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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "splay_tree.h"
24 #include "control_common.h"
35 /* lists type of subnet */
37 splay_tree_t *subnet_tree;
39 /* Subnet lookup cache */
41 static ipv4_t cache_ipv4_address[2];
42 static subnet_t *cache_ipv4_subnet[2];
43 static bool cache_ipv4_valid[2];
44 static int cache_ipv4_slot;
46 static ipv6_t cache_ipv6_address[2];
47 static subnet_t *cache_ipv6_subnet[2];
48 static bool cache_ipv6_valid[2];
49 static int cache_ipv6_slot;
51 static mac_t cache_mac_address[2];
52 static subnet_t *cache_mac_subnet[2];
53 static bool cache_mac_valid[2];
54 static int cache_mac_slot;
56 void subnet_cache_flush(void) {
57 cache_ipv4_valid[0] = cache_ipv4_valid[1] = false;
58 cache_ipv6_valid[0] = cache_ipv6_valid[1] = false;
59 cache_mac_valid[0] = cache_mac_valid[1] = false;
62 /* Initialising trees */
64 void init_subnets(void) {
65 subnet_tree = splay_alloc_tree((splay_compare_t) subnet_compare, (splay_action_t) free_subnet);
70 void exit_subnets(void) {
71 splay_delete_tree(subnet_tree);
74 splay_tree_t *new_subnet_tree(void) {
75 return splay_alloc_tree((splay_compare_t) subnet_compare, NULL);
78 void free_subnet_tree(splay_tree_t *subnet_tree) {
79 splay_delete_tree(subnet_tree);
82 /* Allocating and freeing space for subnets */
84 subnet_t *new_subnet(void) {
85 return xmalloc_and_zero(sizeof(subnet_t));
88 void free_subnet(subnet_t *subnet) {
92 /* Adding and removing subnets */
94 void subnet_add(node_t *n, subnet_t *subnet) {
97 splay_insert(subnet_tree, subnet);
98 splay_insert(n->subnet_tree, subnet);
100 subnet_cache_flush();
103 void subnet_del(node_t *n, subnet_t *subnet) {
104 splay_delete(n->subnet_tree, subnet);
105 splay_delete(subnet_tree, subnet);
107 subnet_cache_flush();
110 /* Subnet lookup routines */
112 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet) {
113 return splay_search(owner->subnet_tree, subnet);
116 subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
117 subnet_t *p, *r = NULL;
121 // Check if this address is cached
123 for(i = 0; i < 2; i++) {
124 if(!cache_mac_valid[i])
126 if(owner && cache_mac_subnet[i] && cache_mac_subnet[i]->owner != owner)
128 if(!memcmp(address, &cache_mac_address[i], sizeof *address))
129 return cache_mac_subnet[i];
132 // Search all subnets for a matching one
134 for(n = owner ? owner->subnet_tree->head : subnet_tree->head; n; n = n->next) {
137 if(!p || p->type != SUBNET_MAC)
140 if(!memcmp(address, &p->net.mac.address, sizeof *address)) {
142 if(p->owner->status.reachable)
149 cache_mac_slot = !cache_mac_slot;
150 memcpy(&cache_mac_address[cache_mac_slot], address, sizeof *address);
151 cache_mac_subnet[cache_mac_slot] = r;
152 cache_mac_valid[cache_mac_slot] = true;
157 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
158 subnet_t *p, *r = NULL;
162 // Check if this address is cached
164 for(i = 0; i < 2; i++) {
165 if(!cache_ipv4_valid[i])
167 if(!memcmp(address, &cache_ipv4_address[i], sizeof *address))
168 return cache_ipv4_subnet[i];
171 // Search all subnets for a matching one
173 for(n = subnet_tree->head; n; n = n->next) {
176 if(!p || p->type != SUBNET_IPV4)
179 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
181 if(p->owner->status.reachable)
188 cache_ipv4_slot = !cache_ipv4_slot;
189 memcpy(&cache_ipv4_address[cache_ipv4_slot], address, sizeof *address);
190 cache_ipv4_subnet[cache_ipv4_slot] = r;
191 cache_ipv4_valid[cache_ipv4_slot] = true;
196 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
197 subnet_t *p, *r = NULL;
201 // Check if this address is cached
203 for(i = 0; i < 2; i++) {
204 if(!cache_ipv6_valid[i])
206 if(!memcmp(address, &cache_ipv6_address[i], sizeof *address))
207 return cache_ipv6_subnet[i];
210 // Search all subnets for a matching one
212 for(n = subnet_tree->head; n; n = n->next) {
215 if(!p || p->type != SUBNET_IPV6)
218 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
220 if(p->owner->status.reachable)
227 cache_ipv6_slot = !cache_ipv6_slot;
228 memcpy(&cache_ipv6_address[cache_ipv6_slot], address, sizeof *address);
229 cache_ipv6_subnet[cache_ipv6_slot] = r;
230 cache_ipv6_valid[cache_ipv6_slot] = true;
235 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
238 char *envp[9] = {NULL};
239 char netstr[MAXNETSTR];
240 char *name, *address, *port;
243 // Prepare environment variables to be passed to the script
245 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
246 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
247 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
248 xasprintf(&envp[3], "NODE=%s", owner->name);
250 if(owner != myself) {
251 sockaddr2str(&owner->address, &address, &port);
252 // 4 and 5 are reserved for SUBNET and WEIGHT
253 xasprintf(&envp[6], "REMOTEADDRESS=%s", address);
254 xasprintf(&envp[7], "REMOTEPORT=%s", port);
259 name = up ? "subnet-up" : "subnet-down";
262 for(node = owner->subnet_tree->head; node; node = node->next) {
264 if(!net2str(netstr, sizeof netstr, subnet))
266 // Strip the weight from the subnet, and put it in its own environment variable
267 char *weight = strchr(netstr, '#');
273 // Prepare the SUBNET and WEIGHT variables
278 xasprintf(&envp[4], "SUBNET=%s", netstr);
279 xasprintf(&envp[5], "WEIGHT=%s", weight);
281 execute_script(name, envp);
284 if(net2str(netstr, sizeof netstr, subnet)) {
285 // Strip the weight from the subnet, and put it in its own environment variable
286 char *weight = strchr(netstr, '#');
292 // Prepare the SUBNET and WEIGHT variables
293 xasprintf(&envp[4], "SUBNET=%s", netstr);
294 xasprintf(&envp[5], "WEIGHT=%s", weight);
296 execute_script(name, envp);
300 for(i = 0; envp[i] && i < 8; i++)
304 bool dump_subnets(connection_t *c) {
305 char netstr[MAXNETSTR];
309 for(node = subnet_tree->head; node; node = node->next) {
311 if(!net2str(netstr, sizeof netstr, subnet))
313 send_request(c, "%d %d %s owner %s",
314 CONTROL, REQ_DUMP_SUBNETS,
315 netstr, subnet->owner->name);
318 return send_request(c, "%d %d", CONTROL, REQ_DUMP_SUBNETS);