2 protocol_subnet.c -- handle the meta-protocol, subnets
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2009 Guus Sliepen <guus@tinc-vpn.org>
5 2009 Michael Tokarev <mjt@tls.msk.ru>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "connection.h"
37 bool send_add_subnet(connection_t *c, const subnet_t *subnet)
39 char netstr[MAXNETSTR];
43 if(!net2str(netstr, sizeof netstr, subnet))
46 return send_request(c, "%d %x %s %s", ADD_SUBNET, rand(), subnet->owner->name, netstr);
49 bool add_subnet_h(connection_t *c)
51 char subnetstr[MAX_STRING_SIZE];
52 char name[MAX_STRING_SIZE];
54 subnet_t s = {0}, *new;
58 if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
59 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_SUBNET", c->name,
64 /* Check if owner name is valid */
67 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
68 c->hostname, _("invalid name"));
72 /* Check if subnet string is valid */
74 if(!str2net(&s, subnetstr)) {
75 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
76 c->hostname, _("invalid subnet string"));
80 if(seen_request(c->buffer))
83 /* Check if the owner of the new subnet is in the connection list */
85 owner = lookup_node(name);
87 if(tunnelserver && owner != myself && owner != c->node) {
88 /* in case of tunnelserver, ignore indirect subnet registrations */
89 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Ignoring indirect %s from %s (%s) for %s"),
90 "ADD_SUBNET", c->name, c->hostname, subnetstr);
96 owner->name = xstrdup(name);
100 /* Check if we already know this subnet */
102 if(lookup_subnet(owner, &s))
105 /* If we don't know this subnet, but we are the owner, retaliate with a DEL_SUBNET */
107 if(owner == myself) {
108 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
109 "ADD_SUBNET", c->name, c->hostname);
111 send_del_subnet(c, &s);
115 /* In tunnel server mode, check if the subnet matches one in the config file of this node */
121 for(cfg = lookup_config(c->config_tree, "Subnet"); cfg; cfg = lookup_config_next(c->config_tree, cfg)) {
122 if(!get_config_subnet(cfg, &allowed))
125 if(!subnet_compare(&s, allowed))
128 free_subnet(allowed);
132 logger(LOG_WARNING, _("Unauthorized %s from %s (%s) for %s"),
133 "ADD_SUBNET", c->name, c->hostname, subnetstr);
137 free_subnet(allowed);
140 /* If everything is correct, add the subnet to the list of the owner */
142 *(new = new_subnet()) = s;
143 subnet_add(owner, new);
145 if(owner->status.reachable)
146 subnet_update(owner, new, true);
156 bool send_del_subnet(connection_t *c, const subnet_t *s)
158 char netstr[MAXNETSTR];
162 if(!net2str(netstr, sizeof netstr, s))
165 return send_request(c, "%d %x %s %s", DEL_SUBNET, rand(), s->owner->name, netstr);
168 bool del_subnet_h(connection_t *c)
170 char subnetstr[MAX_STRING_SIZE];
171 char name[MAX_STRING_SIZE];
173 subnet_t s = {0}, *find;
177 if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
178 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_SUBNET", c->name,
183 /* Check if owner name is valid */
185 if(!check_id(name)) {
186 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
187 c->hostname, _("invalid name"));
191 /* Check if subnet string is valid */
193 if(!str2net(&s, subnetstr)) {
194 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
195 c->hostname, _("invalid subnet string"));
199 if(seen_request(c->buffer))
202 /* Check if the owner of the subnet being deleted is in the connection list */
204 owner = lookup_node(name);
206 if(tunnelserver && owner != myself && owner != c->node) {
207 /* in case of tunnelserver, ignore indirect subnet deletion */
208 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Ignoring indirect %s from %s (%s) for %s"),
209 "DEL_SUBNET", c->name, c->hostname, subnetstr);
214 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which is not in our node tree"),
215 "DEL_SUBNET", c->name, c->hostname, name);
219 /* If everything is correct, delete the subnet from the list of the owner */
223 find = lookup_subnet(owner, &s);
226 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which does not appear in his subnet tree"),
227 "DEL_SUBNET", c->name, c->hostname, name);
231 /* If we are the owner of this subnet, retaliate with an ADD_SUBNET */
233 if(owner == myself) {
234 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
235 "DEL_SUBNET", c->name, c->hostname);
236 send_add_subnet(c, find);
245 /* Finally, delete it. */
247 if(owner->status.reachable)
248 subnet_update(owner, find, false);
250 subnet_del(owner, find);