2 protocol_subnet.c -- handle the meta-protocol, subnets
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2012 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"
34 bool send_add_subnet(connection_t *c, const subnet_t *subnet) {
35 char netstr[MAXNETSTR];
37 if(!net2str(netstr, sizeof(netstr), subnet)) {
41 return send_request(c, "%d %x %s %s", ADD_SUBNET, prng(UINT32_MAX), subnet->owner->name, netstr);
44 bool add_subnet_h(connection_t *c, const char *request) {
45 char subnetstr[MAX_STRING_SIZE];
46 char name[MAX_STRING_SIZE];
48 subnet_t s = {0}, *new, *old;
50 if(sscanf(request, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
51 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ADD_SUBNET", c->name,
56 /* Check if owner name is valid */
59 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_SUBNET", c->name,
60 c->hostname, "invalid name");
64 /* Check if subnet string is valid */
66 if(!str2net(&s, subnetstr)) {
67 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_SUBNET", c->name,
68 c->hostname, "invalid subnet string");
72 if(seen_request(request)) {
76 /* Check if the owner of the new subnet is in the connection list */
78 owner = lookup_node(name);
80 if(tunnelserver && owner != myself && owner != c->node) {
81 /* in case of tunnelserver, ignore indirect subnet registrations */
82 logger(DEBUG_PROTOCOL, LOG_WARNING, "Ignoring indirect %s from %s (%s) for %s",
83 "ADD_SUBNET", c->name, c->hostname, subnetstr);
89 owner->name = xstrdup(name);
93 /* Check if we already know this subnet */
95 if(lookup_subnet(owner, &s)) {
99 /* If we don't know this subnet, but we are the owner, retaliate with a DEL_SUBNET */
101 if(owner == myself) {
102 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
103 "ADD_SUBNET", c->name, c->hostname);
105 send_del_subnet(c, &s);
109 /* In tunnel server mode, we should already know all allowed subnets */
112 logger(DEBUG_ALWAYS, LOG_WARNING, "Ignoring unauthorized %s from %s (%s): %s",
113 "ADD_SUBNET", c->name, c->hostname, subnetstr);
117 /* Ignore if strictsubnets is true, but forward it to others */
120 logger(DEBUG_ALWAYS, LOG_WARNING, "Ignoring unauthorized %s from %s (%s): %s",
121 "ADD_SUBNET", c->name, c->hostname, subnetstr);
122 forward_request(c, request);
126 /* If everything is correct, add the subnet to the list of the owner */
128 *(new = new_subnet()) = s;
129 subnet_add(owner, new);
131 if(owner->status.reachable) {
132 subnet_update(owner, new, true);
138 forward_request(c, request);
141 /* Fast handoff of roaming MAC addresses */
143 if(s.type == SUBNET_MAC && owner != myself && (old = lookup_subnet(myself, &s)) && old->expires) {
150 bool send_del_subnet(connection_t *c, const subnet_t *s) {
151 char netstr[MAXNETSTR];
153 if(!net2str(netstr, sizeof(netstr), s)) {
157 return send_request(c, "%d %x %s %s", DEL_SUBNET, prng(UINT32_MAX), s->owner->name, netstr);
160 bool del_subnet_h(connection_t *c, const char *request) {
161 char subnetstr[MAX_STRING_SIZE];
162 char name[MAX_STRING_SIZE];
164 subnet_t s = {0}, *find;
166 if(sscanf(request, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
167 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "DEL_SUBNET", c->name,
172 /* Check if owner name is valid */
174 if(!check_id(name)) {
175 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_SUBNET", c->name,
176 c->hostname, "invalid name");
180 /* Check if subnet string is valid */
182 if(!str2net(&s, subnetstr)) {
183 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_SUBNET", c->name,
184 c->hostname, "invalid subnet string");
188 if(seen_request(request)) {
192 /* Check if the owner of the subnet being deleted is in the connection list */
194 owner = lookup_node(name);
196 if(tunnelserver && owner != myself && owner != c->node) {
197 /* in case of tunnelserver, ignore indirect subnet deletion */
198 logger(DEBUG_PROTOCOL, LOG_WARNING, "Ignoring indirect %s from %s (%s) for %s",
199 "DEL_SUBNET", c->name, c->hostname, subnetstr);
204 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for %s which is not in our node tree",
205 "DEL_SUBNET", c->name, c->hostname, name);
209 /* If everything is correct, delete the subnet from the list of the owner */
213 find = lookup_subnet(owner, &s);
216 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for %s which does not appear in his subnet tree",
217 "DEL_SUBNET", c->name, c->hostname, name);
220 forward_request(c, request);
226 /* If we are the owner of this subnet, retaliate with an ADD_SUBNET */
228 if(owner == myself) {
229 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
230 "DEL_SUBNET", c->name, c->hostname);
231 send_add_subnet(c, find);
242 forward_request(c, request);
249 /* Finally, delete it. */
251 if(owner->status.reachable) {
252 subnet_update(owner, find, false);
255 subnet_del(owner, find);