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 along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "connection.h"
35 bool send_add_subnet(connection_t *c, const subnet_t *subnet) {
36 char netstr[MAXNETSTR];
40 if(!net2str(netstr, sizeof netstr, subnet))
43 return send_request(c, "%d %x %s %s", ADD_SUBNET, rand(), subnet->owner->name, netstr);
46 bool add_subnet_h(connection_t *c) {
47 char subnetstr[MAX_STRING_SIZE];
48 char name[MAX_STRING_SIZE];
50 subnet_t s = {0}, *new;
54 if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
55 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_SUBNET", c->name,
60 /* Check if owner name is valid */
63 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
64 c->hostname, _("invalid name"));
68 /* Check if subnet string is valid */
70 if(!str2net(&s, subnetstr)) {
71 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
72 c->hostname, _("invalid subnet string"));
76 if(seen_request(c->buffer))
79 /* Check if the owner of the new subnet is in the connection list */
81 owner = lookup_node(name);
83 if(tunnelserver && owner != myself && owner != c->node) {
84 /* in case of tunnelserver, ignore indirect subnet registrations */
85 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Ignoring indirect %s from %s (%s) for %s"),
86 "ADD_SUBNET", c->name, c->hostname, subnetstr);
92 owner->name = xstrdup(name);
96 /* Check if we already know this subnet */
98 if(lookup_subnet(owner, &s))
101 /* If we don't know this subnet, but we are the owner, retaliate with a DEL_SUBNET */
103 if(owner == myself) {
104 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
105 "ADD_SUBNET", c->name, c->hostname);
107 send_del_subnet(c, &s);
111 /* In tunnel server mode, check if the subnet matches one in the config file of this node */
117 for(cfg = lookup_config(c->config_tree, "Subnet"); cfg; cfg = lookup_config_next(c->config_tree, cfg)) {
118 if(!get_config_subnet(cfg, &allowed))
121 if(!subnet_compare(&s, allowed))
124 free_subnet(allowed);
128 logger(LOG_WARNING, _("Unauthorized %s from %s (%s) for %s"),
129 "ADD_SUBNET", c->name, c->hostname, subnetstr);
133 free_subnet(allowed);
136 /* If everything is correct, add the subnet to the list of the owner */
138 *(new = new_subnet()) = s;
139 subnet_add(owner, new);
141 if(owner->status.reachable)
142 subnet_update(owner, new, true);
152 bool send_del_subnet(connection_t *c, const subnet_t *s) {
153 char netstr[MAXNETSTR];
157 if(!net2str(netstr, sizeof netstr, s))
160 return send_request(c, "%d %x %s %s", DEL_SUBNET, rand(), s->owner->name, netstr);
163 bool del_subnet_h(connection_t *c) {
164 char subnetstr[MAX_STRING_SIZE];
165 char name[MAX_STRING_SIZE];
167 subnet_t s = {0}, *find;
171 if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
172 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_SUBNET", c->name,
177 /* Check if owner name is valid */
179 if(!check_id(name)) {
180 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
181 c->hostname, _("invalid name"));
185 /* Check if subnet string is valid */
187 if(!str2net(&s, subnetstr)) {
188 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
189 c->hostname, _("invalid subnet string"));
193 if(seen_request(c->buffer))
196 /* Check if the owner of the subnet being deleted is in the connection list */
198 owner = lookup_node(name);
200 if(tunnelserver && owner != myself && owner != c->node) {
201 /* in case of tunnelserver, ignore indirect subnet deletion */
202 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Ignoring indirect %s from %s (%s) for %s"),
203 "DEL_SUBNET", c->name, c->hostname, subnetstr);
208 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which is not in our node tree"),
209 "DEL_SUBNET", c->name, c->hostname, name);
213 /* If everything is correct, delete the subnet from the list of the owner */
217 find = lookup_subnet(owner, &s);
220 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which does not appear in his subnet tree"),
221 "DEL_SUBNET", c->name, c->hostname, name);
225 /* If we are the owner of this subnet, retaliate with an ADD_SUBNET */
227 if(owner == myself) {
228 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
229 "DEL_SUBNET", c->name, c->hostname);
230 send_add_subnet(c, find);
239 /* Finally, delete it. */
241 if(owner->status.reachable)
242 subnet_update(owner, find, false);
244 subnet_del(owner, find);