Only forward SPTPS packets if Forwarding = internal.
[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
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, rand(),
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, rand(),
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)) {
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();
115                 from->name = xstrdup(from_name);
116                 node_add(from);
117         }
118
119         if(!to) {
120                 to = new_node();
121                 to->name = xstrdup(to_name);
122                 node_add(to);
123         }
124
125
126         /* Convert addresses */
127
128         address = str2sockaddr(to_address, to_port);
129
130         if(parameter_count >= 8) {
131                 local_address = str2sockaddr(address_local, port_local);
132         }
133
134         /* Check if edge already exists */
135
136         e = lookup_edge(from, to);
137
138         if(e) {
139                 bool new_address = sockaddrcmp(&e->address, &address);
140                 // local_address.sa.sa_family will be 0 if we got it from older tinc versions
141                 // local_address.sa.sa_family will be 255 (AF_UNKNOWN) if we got it from newer versions
142                 // but for edge which does not have local_address
143                 bool new_local_address = local_address.sa.sa_family && local_address.sa.sa_family != AF_UNKNOWN &&
144                                          sockaddrcmp(&e->local_address, &local_address);
145
146                 if(e->weight == weight && e->options == options && !new_address && !new_local_address) {
147                         sockaddrfree(&address);
148                         sockaddrfree(&local_address);
149                         return true;
150                 }
151
152                 if(from == myself) {
153                         logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
154                                "ADD_EDGE", c->name, c->hostname);
155                         send_add_edge(c, e);
156                         sockaddrfree(&address);
157                         sockaddrfree(&local_address);
158                         return true;
159                 }
160
161                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not match existing entry",
162                        "ADD_EDGE", c->name, c->hostname);
163
164                 e->options = options;
165
166                 if(new_address) {
167                         sockaddrfree(&e->address);
168                         e->address = address;
169                 } else {
170                         sockaddrfree(&address);
171                 }
172
173                 if(new_local_address) {
174                         sockaddrfree(&e->local_address);
175                         e->local_address = local_address;
176                 } else {
177                         sockaddrfree(&local_address);
178                 }
179
180                 if(e->weight != weight) {
181                         splay_node_t *node = splay_unlink(edge_weight_tree, e);
182                         e->weight = weight;
183                         splay_insert_node(edge_weight_tree, node);
184                 }
185         } else if(from == myself) {
186                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist",
187                        "ADD_EDGE", c->name, c->hostname);
188                 contradicting_add_edge++;
189                 e = new_edge();
190                 e->from = from;
191                 e->to = to;
192                 send_del_edge(c, e);
193                 free_edge(e);
194                 sockaddrfree(&address);
195                 sockaddrfree(&local_address);
196                 return true;
197         } else {
198                 e = new_edge();
199                 e->from = from;
200                 e->to = to;
201                 e->address = address;
202                 e->local_address = local_address;
203                 e->options = options;
204                 e->weight = weight;
205                 edge_add(e);
206         }
207
208         /* Tell the rest about the new edge */
209
210         if(!tunnelserver) {
211                 forward_request(c, request);
212         }
213
214         /* Run MST before or after we tell the rest? */
215
216         graph();
217
218         return true;
219 }
220
221 bool send_del_edge(connection_t *c, const edge_t *e) {
222         return send_request(c, "%d %x %s %s", DEL_EDGE, rand(),
223                             e->from->name, e->to->name);
224 }
225
226 bool del_edge_h(connection_t *c, const char *request) {
227         edge_t *e;
228         char from_name[MAX_STRING_SIZE];
229         char to_name[MAX_STRING_SIZE];
230         node_t *from, *to;
231
232         if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
233                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "DEL_EDGE", c->name,
234                        c->hostname);
235                 return false;
236         }
237
238         /* Check if names are valid */
239
240         if(!check_id(from_name) || !check_id(to_name)) {
241                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_EDGE", c->name,
242                        c->hostname, "invalid name");
243                 return false;
244         }
245
246         if(seen_request(request)) {
247                 return true;
248         }
249
250         /* Lookup nodes */
251
252         from = lookup_node(from_name);
253         to = lookup_node(to_name);
254
255         if(tunnelserver &&
256                         from != myself && from != c->node &&
257                         to != myself && to != c->node) {
258                 /* ignore indirect edge registrations for tunnelserver */
259                 logger(DEBUG_PROTOCOL, LOG_WARNING,
260                        "Ignoring indirect %s from %s (%s)",
261                        "DEL_EDGE", c->name, c->hostname);
262                 return true;
263         }
264
265         if(!from) {
266                 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
267                        "DEL_EDGE", c->name, c->hostname);
268                 return true;
269         }
270
271         if(!to) {
272                 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
273                        "DEL_EDGE", c->name, c->hostname);
274                 return true;
275         }
276
277         /* Check if edge exists */
278
279         e = lookup_edge(from, to);
280
281         if(!e) {
282                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not appear in the edge tree",
283                        "DEL_EDGE", c->name, c->hostname);
284                 return true;
285         }
286
287         if(e->from == myself) {
288                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
289                        "DEL_EDGE", c->name, c->hostname);
290                 contradicting_del_edge++;
291                 send_add_edge(c, e);    /* Send back a correction */
292                 return true;
293         }
294
295         /* Tell the rest about the deleted edge */
296
297         if(!tunnelserver) {
298                 forward_request(c, request);
299         }
300
301         /* Delete the edge */
302
303         edge_del(e);
304
305         /* Run MST before or after we tell the rest? */
306
307         graph();
308
309         /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
310
311         if(!to->status.reachable) {
312                 e = lookup_edge(to, myself);
313
314                 if(e) {
315                         if(!tunnelserver) {
316                                 send_del_edge(everyone, e);
317                         }
318
319                         edge_del(e);
320                 }
321         }
322
323         return true;
324 }