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];
38 if(!net2str(netstr, sizeof(netstr), subnet)) {
42 return send_request(c, "%d %x %s %s", ADD_SUBNET, rand(), subnet->owner->name, netstr);
45 bool add_subnet_h(connection_t *c) {
46 char subnetstr[MAX_STRING_SIZE];
47 char name[MAX_STRING_SIZE];
49 subnet_t s = {NULL}, *new, *old;
51 if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
52 logger(LOG_ERR, "Got bad %s from %s (%s)", "ADD_SUBNET", c->name,
57 /* Check if owner name is valid */
60 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_SUBNET", c->name,
61 c->hostname, "invalid name");
65 /* Check if subnet string is valid */
67 if(!str2net(&s, subnetstr)) {
68 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_SUBNET", c->name,
69 c->hostname, "invalid subnet string");
73 if(seen_request(c->buffer)) {
77 /* Check if the owner of the new subnet is in the connection list */
79 owner = lookup_node(name);
81 if(tunnelserver && owner != myself && owner != c->node) {
82 /* in case of tunnelserver, ignore indirect subnet registrations */
83 ifdebug(PROTOCOL) logger(LOG_WARNING, "Ignoring indirect %s from %s (%s) for %s",
84 "ADD_SUBNET", c->name, c->hostname, subnetstr);
90 owner->name = xstrdup(name);
94 /* Check if we already know this subnet */
96 if(lookup_subnet(owner, &s)) {
100 /* If we don't know this subnet, but we are the owner, retaliate with a DEL_SUBNET */
102 if(owner == myself) {
103 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for ourself",
104 "ADD_SUBNET", c->name, c->hostname);
106 send_del_subnet(c, &s);
110 /* In tunnel server mode, we should already know all allowed subnets */
113 logger(LOG_WARNING, "Ignoring unauthorized %s from %s (%s): %s",
114 "ADD_SUBNET", c->name, c->hostname, subnetstr);
118 /* Ignore if strictsubnets is true, but forward it to others */
121 logger(LOG_WARNING, "Ignoring unauthorized %s from %s (%s): %s",
122 "ADD_SUBNET", c->name, c->hostname, subnetstr);
127 /* If everything is correct, add the subnet to the list of the owner */
129 *(new = new_subnet()) = s;
130 subnet_add(owner, new);
132 if(owner->status.reachable) {
133 subnet_update(owner, new, true);
140 /* Fast handoff of roaming MAC addresses */
142 if(s.type == SUBNET_MAC && owner != myself && (old = lookup_subnet(myself, &s)) && old->expires) {
149 bool send_del_subnet(connection_t *c, const subnet_t *s) {
150 char netstr[MAXNETSTR];
152 if(!net2str(netstr, sizeof(netstr), s)) {
156 return send_request(c, "%d %x %s %s", DEL_SUBNET, rand(), s->owner->name, netstr);
159 bool del_subnet_h(connection_t *c) {
160 char subnetstr[MAX_STRING_SIZE];
161 char name[MAX_STRING_SIZE];
163 subnet_t s = {NULL}, *find;
165 if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
166 logger(LOG_ERR, "Got bad %s from %s (%s)", "DEL_SUBNET", c->name,
171 /* Check if owner name is valid */
173 if(!check_id(name)) {
174 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_SUBNET", c->name,
175 c->hostname, "invalid name");
179 /* Check if subnet string is valid */
181 if(!str2net(&s, subnetstr)) {
182 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_SUBNET", c->name,
183 c->hostname, "invalid subnet string");
187 if(seen_request(c->buffer)) {
191 /* Check if the owner of the subnet being deleted is in the connection list */
193 owner = lookup_node(name);
195 if(tunnelserver && owner != myself && owner != c->node) {
196 /* in case of tunnelserver, ignore indirect subnet deletion */
197 ifdebug(PROTOCOL) logger(LOG_WARNING, "Ignoring indirect %s from %s (%s) for %s",
198 "DEL_SUBNET", c->name, c->hostname, subnetstr);
203 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for %s which is not in our node tree",
204 "DEL_SUBNET", c->name, c->hostname, name);
208 /* If everything is correct, delete the subnet from the list of the owner */
212 find = lookup_subnet(owner, &s);
215 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for %s which does not appear in his subnet tree",
216 "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);
246 /* Finally, delete it. */
248 if(owner->status.reachable) {
249 subnet_update(owner, find, false);
252 subnet_del(owner, find);