Be more liberal accepting ADD_EDGE messages with conflicting local address information.
[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 "connection.h"
26 #include "edge.h"
27 #include "graph.h"
28 #include "logger.h"
29 #include "meta.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         char *local_address, *local_port;
41
42         sockaddr2str(&e->address, &address, &port);
43         sockaddr2str(&e->local_address, &local_address, &local_port);
44
45         x = send_request(c, "%d %x %s %s %s %s %x %d %s %s", ADD_EDGE, rand(),
46                                          e->from->name, e->to->name, address, port,
47                                          e->options, e->weight, local_address, local_port);
48
49         free(address);
50         free(port);
51         free(local_address);
52         free(local_port);
53
54         return x;
55 }
56
57 bool add_edge_h(connection_t *c, const char *request) {
58         edge_t *e;
59         node_t *from, *to;
60         char from_name[MAX_STRING_SIZE];
61         char to_name[MAX_STRING_SIZE];
62         char to_address[MAX_STRING_SIZE];
63         char to_port[MAX_STRING_SIZE];
64         char address_local[MAX_STRING_SIZE];
65         char port_local[MAX_STRING_SIZE];
66         sockaddr_t address, local_address = {{0}};
67         uint32_t options;
68         int weight;
69
70         int parameter_count = sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %x %d "MAX_STRING" "MAX_STRING,
71                                               from_name, to_name, to_address, to_port, &options, &weight, address_local, port_local);
72         if (parameter_count != 6 && parameter_count != 8) {
73                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ADD_EDGE", c->name,
74                            c->hostname);
75                 return false;
76         }
77
78         /* Check if names are valid */
79
80         if(!check_id(from_name) || !check_id(to_name)) {
81                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name,
82                            c->hostname, "invalid name");
83                 return false;
84         }
85
86         if(seen_request(request))
87                 return true;
88
89         /* Lookup nodes */
90
91         from = lookup_node(from_name);
92         to = lookup_node(to_name);
93
94         if(tunnelserver &&
95            from != myself && from != c->node &&
96            to != myself && to != c->node) {
97                 /* ignore indirect edge registrations for tunnelserver */
98                 logger(DEBUG_PROTOCOL, LOG_WARNING,
99                    "Ignoring indirect %s from %s (%s)",
100                    "ADD_EDGE", c->name, c->hostname);
101                 return true;
102         }
103
104         if(!from) {
105                 from = new_node();
106                 from->name = xstrdup(from_name);
107                 node_add(from);
108         }
109
110         if(!to) {
111                 to = new_node();
112                 to->name = xstrdup(to_name);
113                 node_add(to);
114         }
115
116
117         /* Convert addresses */
118
119         address = str2sockaddr(to_address, to_port);
120         if(parameter_count >= 8)
121                 local_address = str2sockaddr(address_local, port_local);
122
123         /* Check if edge already exists */
124
125         e = lookup_edge(from, to);
126
127         if(e) {
128                 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
129                         if(from == myself) {
130                                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
131                                                    "ADD_EDGE", c->name, c->hostname);
132                                 send_add_edge(c, e);
133                                 return true;
134                         } else {
135                                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not match existing entry",
136                                                    "ADD_EDGE", c->name, c->hostname);
137                                 edge_del(e);
138                                 graph();
139                         }
140                 } else if(sockaddrcmp(&e->local_address, &local_address)) {
141                         if(from == myself) {
142                                 if(e->local_address.sa.sa_family && local_address.sa.sa_family) {
143                                         // Someone has the wrong local address for ourself. Correct then.
144                                         logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
145                                                            "ADD_EDGE", c->name, c->hostname);
146                                         send_add_edge(c, e);
147                                         return true;
148                                 }
149                                 // Otherwise, just ignore it.
150                                 return true;
151                         } else if(local_address.sa.sa_family) {
152                                 // We learned a new local address for this edge.
153                                 sockaddrfree(&e->local_address);
154                                 e->local_address = local_address;
155
156                                 // Tell others about it.
157                                 if(!tunnelserver)
158                                         forward_request(c, request);
159
160                                 return true;
161                         }
162                 } else
163                         return true;
164         } else if(from == myself) {
165                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist",
166                                    "ADD_EDGE", c->name, c->hostname);
167                 contradicting_add_edge++;
168                 e = new_edge();
169                 e->from = from;
170                 e->to = to;
171                 send_del_edge(c, e);
172                 free_edge(e);
173                 return true;
174         }
175
176         e = new_edge();
177         e->from = from;
178         e->to = to;
179         e->address = address;
180         e->local_address = local_address;
181         e->options = options;
182         e->weight = weight;
183         edge_add(e);
184
185         /* Tell the rest about the new edge */
186
187         if(!tunnelserver)
188                 forward_request(c, request);
189
190         /* Run MST before or after we tell the rest? */
191
192         graph();
193
194         return true;
195 }
196
197 bool send_del_edge(connection_t *c, const edge_t *e) {
198         return send_request(c, "%d %x %s %s", DEL_EDGE, rand(),
199                                                 e->from->name, e->to->name);
200 }
201
202 bool del_edge_h(connection_t *c, const char *request) {
203         edge_t *e;
204         char from_name[MAX_STRING_SIZE];
205         char to_name[MAX_STRING_SIZE];
206         node_t *from, *to;
207
208         if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
209                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "DEL_EDGE", c->name,
210                            c->hostname);
211                 return false;
212         }
213
214         /* Check if names are valid */
215
216         if(!check_id(from_name) || !check_id(to_name)) {
217                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_EDGE", c->name,
218                            c->hostname, "invalid name");
219                 return false;
220         }
221
222         if(seen_request(request))
223                 return true;
224
225         /* Lookup nodes */
226
227         from = lookup_node(from_name);
228         to = lookup_node(to_name);
229
230         if(tunnelserver &&
231            from != myself && from != c->node &&
232            to != myself && to != c->node) {
233                 /* ignore indirect edge registrations for tunnelserver */
234                 logger(DEBUG_PROTOCOL, LOG_WARNING,
235                    "Ignoring indirect %s from %s (%s)",
236                    "DEL_EDGE", c->name, c->hostname);
237                 return true;
238         }
239
240         if(!from) {
241                 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
242                                    "DEL_EDGE", c->name, c->hostname);
243                 return true;
244         }
245
246         if(!to) {
247                 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
248                                    "DEL_EDGE", c->name, c->hostname);
249                 return true;
250         }
251
252         /* Check if edge exists */
253
254         e = lookup_edge(from, to);
255
256         if(!e) {
257                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not appear in the edge tree",
258                                    "DEL_EDGE", c->name, c->hostname);
259                 return true;
260         }
261
262         if(e->from == myself) {
263                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
264                                    "DEL_EDGE", c->name, c->hostname);
265                 contradicting_del_edge++;
266                 send_add_edge(c, e);    /* Send back a correction */
267                 return true;
268         }
269
270         /* Tell the rest about the deleted edge */
271
272         if(!tunnelserver)
273                 forward_request(c, request);
274
275         /* Delete the edge */
276
277         edge_del(e);
278
279         /* Run MST before or after we tell the rest? */
280
281         graph();
282
283         /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
284
285         if(!to->status.reachable) {
286                 e = lookup_edge(to, myself);
287                 if(e) {
288                         if(!tunnelserver)
289                                 send_del_edge(everyone, e);
290                         edge_del(e);
291                 }
292         }
293
294         return true;
295 }