GitHub CI: update list of container images
[tinc] / src / protocol_edge.c
1 /*
2     protocol_edge.c -- handle the meta-protocol, edges
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2012 Guus Sliepen <guus@tinc-vpn.org>
5                   2009      Michael Tokarev <mjt@corpit.ru>
6
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.
11
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.
16
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.
20 */
21
22 #include "system.h"
23
24 #include "conf.h"
25 #include "crypto.h"
26 #include "connection.h"
27 #include "edge.h"
28 #include "graph.h"
29 #include "logger.h"
30 #include "net.h"
31 #include "netutl.h"
32 #include "node.h"
33 #include "protocol.h"
34 #include "utils.h"
35 #include "xalloc.h"
36
37 bool send_add_edge(connection_t *c, const edge_t *e) {
38         bool x;
39         char *address, *port;
40
41         sockaddr2str(&e->address, &address, &port);
42
43         if(e->local_address.sa.sa_family) {
44                 char *local_address, *local_port;
45                 sockaddr2str(&e->local_address, &local_address, &local_port);
46
47                 x = send_request(c, "%d %x %s %s %s %s %x %d %s %s", ADD_EDGE, prng(UINT32_MAX),
48                                  e->from->name, e->to->name, address, port,
49                                  e->options, e->weight, local_address, local_port);
50                 free(local_address);
51                 free(local_port);
52         } else {
53                 x = send_request(c, "%d %x %s %s %s %s %x %d", ADD_EDGE, prng(UINT32_MAX),
54                                  e->from->name, e->to->name, address, port,
55                                  e->options, e->weight);
56         }
57
58         free(address);
59         free(port);
60
61         return x;
62 }
63
64 bool add_edge_h(connection_t *c, const char *request) {
65         edge_t *e;
66         node_t *from, *to;
67         char from_name[MAX_STRING_SIZE];
68         char to_name[MAX_STRING_SIZE];
69         char to_address[MAX_STRING_SIZE];
70         char to_port[MAX_STRING_SIZE];
71         char address_local[MAX_STRING_SIZE];
72         char port_local[MAX_STRING_SIZE];
73         sockaddr_t address, local_address = {0};
74         uint32_t options;
75         int weight;
76
77         int parameter_count = sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %x %d "MAX_STRING" "MAX_STRING,
78                                      from_name, to_name, to_address, to_port, &options, &weight, address_local, port_local);
79
80         if(parameter_count != 6 && parameter_count != 8) {
81                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ADD_EDGE", c->name,
82                        c->hostname);
83                 return false;
84         }
85
86         /* Check if names are valid */
87
88         if(!check_id(from_name) || !check_id(to_name) || !strcmp(from_name, to_name)) {
89                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name,
90                        c->hostname, "invalid name");
91                 return false;
92         }
93
94         if(seen_request(request)) {
95                 return true;
96         }
97
98         /* Lookup nodes */
99
100         from = lookup_node(from_name);
101         to = lookup_node(to_name);
102
103         if(tunnelserver &&
104                         from != myself && from != c->node &&
105                         to != myself && to != c->node) {
106                 /* ignore indirect edge registrations for tunnelserver */
107                 logger(DEBUG_PROTOCOL, LOG_WARNING,
108                        "Ignoring indirect %s from %s (%s)",
109                        "ADD_EDGE", c->name, c->hostname);
110                 return true;
111         }
112
113         if(!from) {
114                 from = new_node(from_name);
115                 node_add(from);
116         }
117
118         if(!to) {
119                 to = new_node(to_name);
120                 node_add(to);
121         }
122
123
124         /* Convert addresses */
125
126         address = str2sockaddr(to_address, to_port);
127
128         if(parameter_count >= 8) {
129                 local_address = str2sockaddr(address_local, port_local);
130         }
131
132         /* Check if edge already exists */
133
134         e = lookup_edge(from, to);
135
136         if(e) {
137                 bool new_address = sockaddrcmp(&e->address, &address);
138                 // local_address.sa.sa_family will be 0 if we got it from older tinc versions
139                 // local_address.sa.sa_family will be 255 (AF_UNKNOWN) if we got it from newer versions
140                 // but for edge which does not have local_address
141                 bool new_local_address = local_address.sa.sa_family && local_address.sa.sa_family != AF_UNKNOWN &&
142                                          sockaddrcmp(&e->local_address, &local_address);
143
144                 if(e->weight == weight && e->options == options && !new_address && !new_local_address) {
145                         sockaddrfree(&address);
146                         sockaddrfree(&local_address);
147                         return true;
148                 }
149
150                 if(from == myself) {
151                         logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
152                                "ADD_EDGE", c->name, c->hostname);
153                         send_add_edge(c, e);
154                         sockaddrfree(&address);
155                         sockaddrfree(&local_address);
156                         return true;
157                 }
158
159                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not match existing entry",
160                        "ADD_EDGE", c->name, c->hostname);
161
162                 e->options = options;
163
164                 if(new_address) {
165                         sockaddrfree(&e->address);
166                         e->address = address;
167                 } else {
168                         sockaddrfree(&address);
169                 }
170
171                 if(new_local_address) {
172                         sockaddrfree(&e->local_address);
173                         e->local_address = local_address;
174                 } else {
175                         sockaddrfree(&local_address);
176                 }
177
178                 if(e->weight != weight) {
179                         splay_node_t *node = splay_unlink(&edge_weight_tree, e);
180                         e->weight = weight;
181                         splay_insert_node(&edge_weight_tree, node);
182                 }
183         } else if(from == myself) {
184                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist",
185                        "ADD_EDGE", c->name, c->hostname);
186                 contradicting_add_edge++;
187                 e = new_edge();
188                 e->from = from;
189                 e->to = to;
190                 send_del_edge(c, e);
191                 free_edge(e);
192                 sockaddrfree(&address);
193                 sockaddrfree(&local_address);
194                 return true;
195         } else {
196                 e = new_edge();
197                 e->from = from;
198                 e->to = to;
199                 e->address = address;
200                 e->local_address = local_address;
201                 e->options = options;
202                 e->weight = weight;
203                 edge_add(e);
204         }
205
206         /* Tell the rest about the new edge */
207
208         if(!tunnelserver) {
209                 forward_request(c, request);
210         }
211
212         /* Run MST before or after we tell the rest? */
213
214         graph();
215
216         return true;
217 }
218
219 bool send_del_edge(connection_t *c, const edge_t *e) {
220         return send_request(c, "%d %x %s %s", DEL_EDGE, prng(UINT32_MAX),
221                             e->from->name, e->to->name);
222 }
223
224 bool del_edge_h(connection_t *c, const char *request) {
225         edge_t *e;
226         char from_name[MAX_STRING_SIZE];
227         char to_name[MAX_STRING_SIZE];
228         node_t *from, *to;
229
230         if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
231                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "DEL_EDGE", c->name,
232                        c->hostname);
233                 return false;
234         }
235
236         /* Check if names are valid */
237
238         if(!check_id(from_name) || !check_id(to_name) || !strcmp(from_name, to_name)) {
239                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_EDGE", c->name,
240                        c->hostname, "invalid name");
241                 return false;
242         }
243
244         if(seen_request(request)) {
245                 return true;
246         }
247
248         /* Lookup nodes */
249
250         from = lookup_node(from_name);
251         to = lookup_node(to_name);
252
253         if(tunnelserver &&
254                         from != myself && from != c->node &&
255                         to != myself && to != c->node) {
256                 /* ignore indirect edge registrations for tunnelserver */
257                 logger(DEBUG_PROTOCOL, LOG_WARNING,
258                        "Ignoring indirect %s from %s (%s)",
259                        "DEL_EDGE", c->name, c->hostname);
260                 return true;
261         }
262
263         if(!from) {
264                 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
265                        "DEL_EDGE", c->name, c->hostname);
266                 return true;
267         }
268
269         if(!to) {
270                 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
271                        "DEL_EDGE", c->name, c->hostname);
272                 return true;
273         }
274
275         /* Check if edge exists */
276
277         e = lookup_edge(from, to);
278
279         if(!e) {
280                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not appear in the edge tree",
281                        "DEL_EDGE", c->name, c->hostname);
282                 return true;
283         }
284
285         if(e->from == myself) {
286                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
287                        "DEL_EDGE", c->name, c->hostname);
288                 contradicting_del_edge++;
289                 send_add_edge(c, e);    /* Send back a correction */
290                 return true;
291         }
292
293         /* Tell the rest about the deleted edge */
294
295         if(!tunnelserver) {
296                 forward_request(c, request);
297         }
298
299         /* Delete the edge */
300
301         edge_del(e);
302
303         /* Run MST before or after we tell the rest? */
304
305         graph();
306
307         /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
308
309         if(!to->status.reachable) {
310                 e = lookup_edge(to, myself);
311
312                 if(e) {
313                         if(!tunnelserver) {
314                                 send_del_edge(everyone, e);
315                         }
316
317                         edge_del(e);
318                 }
319         }
320
321         return true;
322 }