2019-08-26 11:44:36 +00:00
|
|
|
/*
|
|
|
|
protocol_subnet.c -- handle the meta-protocol, subnets
|
2019-08-26 11:44:37 +00:00
|
|
|
Copyright (C) 1999-2005 Ivo Timmermans,
|
2019-08-26 11:44:49 +00:00
|
|
|
2000-2012 Guus Sliepen <guus@tinc-vpn.org>
|
2019-08-26 11:44:38 +00:00
|
|
|
2009 Michael Tokarev <mjt@tls.msk.ru>
|
2019-08-26 11:44:36 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2019-08-26 11:44:36 +00:00
|
|
|
*/
|
|
|
|
|
2019-08-26 11:44:36 +00:00
|
|
|
#include "system.h"
|
2019-08-26 11:44:36 +00:00
|
|
|
|
|
|
|
#include "conf.h"
|
2019-08-26 11:44:36 +00:00
|
|
|
#include "connection.h"
|
|
|
|
#include "logger.h"
|
2019-08-26 11:44:36 +00:00
|
|
|
#include "net.h"
|
|
|
|
#include "netutl.h"
|
|
|
|
#include "node.h"
|
2019-08-26 11:44:36 +00:00
|
|
|
#include "protocol.h"
|
|
|
|
#include "subnet.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "xalloc.h"
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
bool send_add_subnet(connection_t *c, const subnet_t *subnet) {
|
2019-08-26 11:44:36 +00:00
|
|
|
char netstr[MAXNETSTR];
|
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
if(!net2str(netstr, sizeof netstr, subnet))
|
2019-08-26 11:44:36 +00:00
|
|
|
return false;
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
return send_request(c, "%d %x %s %s", ADD_SUBNET, rand(), subnet->owner->name, netstr);
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
bool add_subnet_h(connection_t *c, const char *request) {
|
2019-08-26 11:44:36 +00:00
|
|
|
char subnetstr[MAX_STRING_SIZE];
|
|
|
|
char name[MAX_STRING_SIZE];
|
|
|
|
node_t *owner;
|
2019-08-26 11:44:49 +00:00
|
|
|
subnet_t s = {NULL}, *new, *old;
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
if(sscanf(request, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
|
2019-08-26 11:44:49 +00:00
|
|
|
logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ADD_SUBNET", c->name,
|
2019-08-26 11:44:49 +00:00
|
|
|
c->hostname);
|
2019-08-26 11:44:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
/* Check if owner name is valid */
|
2019-08-26 11:44:36 +00:00
|
|
|
|
|
|
|
if(!check_id(name)) {
|
2019-08-26 11:44:49 +00:00
|
|
|
logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_SUBNET", c->name,
|
2019-08-26 11:44:49 +00:00
|
|
|
c->hostname, "invalid name");
|
2019-08-26 11:44:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if subnet string is valid */
|
|
|
|
|
|
|
|
if(!str2net(&s, subnetstr)) {
|
2019-08-26 11:44:49 +00:00
|
|
|
logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_SUBNET", c->name,
|
2019-08-26 11:44:49 +00:00
|
|
|
c->hostname, "invalid subnet string");
|
2019-08-26 11:44:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
if(seen_request(request))
|
2019-08-26 11:44:36 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
/* Check if the owner of the new subnet is in the connection list */
|
|
|
|
|
|
|
|
owner = lookup_node(name);
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
if(tunnelserver && owner != myself && owner != c->node) {
|
|
|
|
/* in case of tunnelserver, ignore indirect subnet registrations */
|
2019-08-26 11:44:49 +00:00
|
|
|
logger(DEBUG_PROTOCOL, LOG_WARNING, "Ignoring indirect %s from %s (%s) for %s",
|
2019-08-26 11:44:49 +00:00
|
|
|
"ADD_SUBNET", c->name, c->hostname, subnetstr);
|
2019-08-26 11:44:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:36 +00:00
|
|
|
if(!owner) {
|
|
|
|
owner = new_node();
|
|
|
|
owner->name = xstrdup(name);
|
|
|
|
node_add(owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we already know this subnet */
|
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
if(lookup_subnet(owner, &s))
|
2019-08-26 11:44:36 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
/* If we don't know this subnet, but we are the owner, retaliate with a DEL_SUBNET */
|
|
|
|
|
|
|
|
if(owner == myself) {
|
2019-08-26 11:44:49 +00:00
|
|
|
logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
|
2019-08-26 11:44:49 +00:00
|
|
|
"ADD_SUBNET", c->name, c->hostname);
|
2019-08-26 11:44:36 +00:00
|
|
|
s.owner = myself;
|
|
|
|
send_del_subnet(c, &s);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:40 +00:00
|
|
|
/* In tunnel server mode, we should already know all allowed subnets */
|
2019-08-26 11:44:36 +00:00
|
|
|
|
|
|
|
if(tunnelserver) {
|
2019-08-26 11:44:49 +00:00
|
|
|
logger(DEBUG_ALWAYS, LOG_WARNING, "Ignoring unauthorized %s from %s (%s): %s",
|
2019-08-26 11:44:49 +00:00
|
|
|
"ADD_SUBNET", c->name, c->hostname, subnetstr);
|
2019-08-26 11:44:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:40 +00:00
|
|
|
/* Ignore if strictsubnets is true, but forward it to others */
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:40 +00:00
|
|
|
if(strictsubnets) {
|
2019-08-26 11:44:49 +00:00
|
|
|
logger(DEBUG_ALWAYS, LOG_WARNING, "Ignoring unauthorized %s from %s (%s): %s",
|
2019-08-26 11:44:49 +00:00
|
|
|
"ADD_SUBNET", c->name, c->hostname, subnetstr);
|
|
|
|
forward_request(c, request);
|
2019-08-26 11:44:40 +00:00
|
|
|
return true;
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If everything is correct, add the subnet to the list of the owner */
|
|
|
|
|
|
|
|
*(new = new_subnet()) = s;
|
|
|
|
subnet_add(owner, new);
|
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
if(owner->status.reachable)
|
2019-08-26 11:44:36 +00:00
|
|
|
subnet_update(owner, new, true);
|
|
|
|
|
2019-08-26 11:44:36 +00:00
|
|
|
/* Tell the rest */
|
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
if(!tunnelserver)
|
|
|
|
forward_request(c, request);
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
/* Fast handoff of roaming MAC addresses */
|
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
if(s.type == SUBNET_MAC && owner != myself && (old = lookup_subnet(myself, &s)) && old->expires)
|
|
|
|
old->expires = 1;
|
2019-08-26 11:44:38 +00:00
|
|
|
|
2019-08-26 11:44:36 +00:00
|
|
|
return true;
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
bool send_del_subnet(connection_t *c, const subnet_t *s) {
|
2019-08-26 11:44:36 +00:00
|
|
|
char netstr[MAXNETSTR];
|
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
if(!net2str(netstr, sizeof netstr, s))
|
2019-08-26 11:44:36 +00:00
|
|
|
return false;
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
return send_request(c, "%d %x %s %s", DEL_SUBNET, rand(), s->owner->name, netstr);
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
bool del_subnet_h(connection_t *c, const char *request) {
|
2019-08-26 11:44:36 +00:00
|
|
|
char subnetstr[MAX_STRING_SIZE];
|
|
|
|
char name[MAX_STRING_SIZE];
|
|
|
|
node_t *owner;
|
2019-08-26 11:44:49 +00:00
|
|
|
subnet_t s = {NULL}, *find;
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
if(sscanf(request, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
|
2019-08-26 11:44:49 +00:00
|
|
|
logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "DEL_SUBNET", c->name,
|
2019-08-26 11:44:49 +00:00
|
|
|
c->hostname);
|
2019-08-26 11:44:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
/* Check if owner name is valid */
|
2019-08-26 11:44:36 +00:00
|
|
|
|
|
|
|
if(!check_id(name)) {
|
2019-08-26 11:44:49 +00:00
|
|
|
logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_SUBNET", c->name,
|
2019-08-26 11:44:49 +00:00
|
|
|
c->hostname, "invalid name");
|
2019-08-26 11:44:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
/* Check if subnet string is valid */
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
if(!str2net(&s, subnetstr)) {
|
2019-08-26 11:44:49 +00:00
|
|
|
logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_SUBNET", c->name,
|
2019-08-26 11:44:49 +00:00
|
|
|
c->hostname, "invalid subnet string");
|
2019-08-26 11:44:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
if(seen_request(request))
|
2019-08-26 11:44:36 +00:00
|
|
|
return true;
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
/* Check if the owner of the subnet being deleted is in the connection list */
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
owner = lookup_node(name);
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
if(tunnelserver && owner != myself && owner != c->node) {
|
|
|
|
/* in case of tunnelserver, ignore indirect subnet deletion */
|
2019-08-26 11:44:49 +00:00
|
|
|
logger(DEBUG_PROTOCOL, LOG_WARNING, "Ignoring indirect %s from %s (%s) for %s",
|
|
|
|
"DEL_SUBNET", c->name, c->hostname, subnetstr);
|
2019-08-26 11:44:38 +00:00
|
|
|
return true;
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
if(!owner) {
|
2019-08-26 11:44:49 +00:00
|
|
|
logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for %s which is not in our node tree",
|
2019-08-26 11:44:49 +00:00
|
|
|
"DEL_SUBNET", c->name, c->hostname, name);
|
2019-08-26 11:44:36 +00:00
|
|
|
return true;
|
2019-08-26 11:44:38 +00:00
|
|
|
}
|
2019-08-26 11:44:36 +00:00
|
|
|
|
|
|
|
/* If everything is correct, delete the subnet from the list of the owner */
|
|
|
|
|
|
|
|
s.owner = owner;
|
|
|
|
|
|
|
|
find = lookup_subnet(owner, &s);
|
|
|
|
|
|
|
|
if(!find) {
|
2019-08-26 11:44:49 +00:00
|
|
|
logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for %s which does not appear in his subnet tree",
|
2019-08-26 11:44:49 +00:00
|
|
|
"DEL_SUBNET", c->name, c->hostname, name);
|
|
|
|
if(strictsubnets)
|
|
|
|
forward_request(c, request);
|
2019-08-26 11:44:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are the owner of this subnet, retaliate with an ADD_SUBNET */
|
|
|
|
|
|
|
|
if(owner == myself) {
|
2019-08-26 11:44:49 +00:00
|
|
|
logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
|
2019-08-26 11:44:49 +00:00
|
|
|
"DEL_SUBNET", c->name, c->hostname);
|
2019-08-26 11:44:36 +00:00
|
|
|
send_add_subnet(c, find);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
if(tunnelserver)
|
2019-08-26 11:44:40 +00:00
|
|
|
return true;
|
|
|
|
|
2019-08-26 11:44:36 +00:00
|
|
|
/* Tell the rest */
|
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
if(!tunnelserver)
|
|
|
|
forward_request(c, request);
|
|
|
|
if(strictsubnets)
|
2019-08-26 11:44:40 +00:00
|
|
|
return true;
|
2019-08-26 11:44:36 +00:00
|
|
|
|
|
|
|
/* Finally, delete it. */
|
|
|
|
|
2019-08-26 11:44:49 +00:00
|
|
|
if(owner->status.reachable)
|
2019-08-26 11:44:36 +00:00
|
|
|
subnet_update(owner, find, false);
|
|
|
|
|
2019-08-26 11:44:36 +00:00
|
|
|
subnet_del(owner, find);
|
|
|
|
|
|
|
|
return true;
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|