Don't keep an address cache in an outgoing_t.
[tinc] / src / protocol_misc.c
1 /*
2     protocol_misc.c -- handle the meta-protocol, miscellaneous functions
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2013 Guus Sliepen <guus@tinc-vpn.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "address_cache.h"
24 #include "conf.h"
25 #include "connection.h"
26 #include "logger.h"
27 #include "meta.h"
28 #include "net.h"
29 #include "netutl.h"
30 #include "protocol.h"
31 #include "utils.h"
32 #include "xalloc.h"
33
34 #ifndef MIN
35 #define MIN(x, y) (((x)<(y))?(x):(y))
36 #endif
37
38 int maxoutbufsize = 0;
39 int mtu_info_interval = 5;
40 int udp_info_interval = 5;
41
42 bool send_termreq(connection_t *c) {
43         return send_request(c, "%d", TERMREQ);
44 }
45
46 bool termreq_h(connection_t *c, const char *request) {
47         (void)c;
48         (void)request;
49         return false;
50 }
51
52 bool send_ping(connection_t *c) {
53         c->status.pinged = true;
54         c->last_ping_time = now.tv_sec;
55
56         return send_request(c, "%d", PING);
57 }
58
59 bool ping_h(connection_t *c, const char *request) {
60         (void)request;
61         return send_pong(c);
62 }
63
64 bool send_pong(connection_t *c) {
65         return send_request(c, "%d", PONG);
66 }
67
68 bool pong_h(connection_t *c, const char *request) {
69         (void)request;
70         c->status.pinged = false;
71
72         /* Successful connection, reset timeout if this is an outgoing connection. */
73
74         if(c->outgoing && c->outgoing->timeout) {
75                 c->outgoing->timeout = 0;
76                 reset_address_cache(c->outgoing->node->address_cache, &c->address);
77         }
78
79         return true;
80 }
81
82 /* Sending and receiving packets via TCP */
83
84 bool send_tcppacket(connection_t *c, const vpn_packet_t *packet) {
85         /* If there already is a lot of data in the outbuf buffer, discard this packet.
86            We use a very simple Random Early Drop algorithm. */
87
88         if(2.0 * c->outbuf.len / (float)maxoutbufsize - 1 > (float)rand() / (float)RAND_MAX) {
89                 return true;
90         }
91
92         if(!send_request(c, "%d %d", PACKET, packet->len)) {
93                 return false;
94         }
95
96         return send_meta(c, (char *)DATA(packet), packet->len);
97 }
98
99 bool tcppacket_h(connection_t *c, const char *request) {
100         short int len;
101
102         if(sscanf(request, "%*d %hd", &len) != 1) {
103                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "PACKET", c->name,
104                        c->hostname);
105                 return false;
106         }
107
108         /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
109
110         c->tcplen = len;
111
112         return true;
113 }
114
115 bool send_sptps_tcppacket(connection_t *c, const char *packet, int len) {
116         /* If there already is a lot of data in the outbuf buffer, discard this packet.
117            We use a very simple Random Early Drop algorithm. */
118
119         if(2.0 * c->outbuf.len / (float)maxoutbufsize - 1 > (float)rand() / (float)RAND_MAX) {
120                 return true;
121         }
122
123         if(!send_request(c, "%d %d", SPTPS_PACKET, len)) {
124                 return false;
125         }
126
127         send_meta_raw(c, packet, len);
128         return true;
129 }
130
131 bool sptps_tcppacket_h(connection_t *c, const char *request) {
132         short int len;
133
134         if(sscanf(request, "%*d %hd", &len) != 1) {
135                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "SPTPS_PACKET", c->name,
136                        c->hostname);
137                 return false;
138         }
139
140         /* Set sptpslen to len, this will tell receive_meta() that a SPTPS packet is coming. */
141
142         c->sptpslen = len;
143
144         return true;
145 }
146
147 /* Transmitting UDP information */
148
149 bool send_udp_info(node_t *from, node_t *to) {
150         /* If there's a static relay in the path, there's no point in sending the message
151            farther than the static relay. */
152         to = (to->via == myself) ? to->nexthop : to->via;
153
154         if(to == NULL) {
155                 logger(DEBUG_ALWAYS, LOG_ERR, "Something went wrong when selecting relay - possible fake UDP_INFO");
156                 return false;
157         }
158
159         /* Skip cases where sending UDP info messages doesn't make sense.
160            This is done here in order to avoid repeating the same logic in multiple callsites. */
161
162         if(to == myself) {
163                 return true;
164         }
165
166         if(!to->status.reachable) {
167                 return true;
168         }
169
170         if(from == myself) {
171                 if(to->connection) {
172                         return true;
173                 }
174
175                 struct timeval elapsed;
176
177                 timersub(&now, &to->udp_info_sent, &elapsed);
178
179                 if(elapsed.tv_sec < udp_info_interval) {
180                         return true;
181                 }
182         }
183
184         if((myself->options | from->options | to->options) & OPTION_TCPONLY) {
185                 return true;
186         }
187
188         if((to->nexthop->options >> 24) < 5) {
189                 return true;
190         }
191
192         char *from_address, *from_port;
193         /* If we're the originator, the address we use is irrelevant
194            because the first intermediate node will ignore it.
195            We use our local address as it somewhat makes sense
196            and it's simpler than introducing an encoding for "null" addresses anyway. */
197         sockaddr2str((from != myself) ? &from->address : &to->nexthop->connection->edge->local_address, &from_address, &from_port);
198
199         bool x = send_request(to->nexthop->connection, "%d %s %s %s %s", UDP_INFO, from->name, to->name, from_address, from_port);
200
201         free(from_address);
202         free(from_port);
203
204         if(from == myself) {
205                 to->udp_info_sent = now;
206         }
207
208         return x;
209 }
210
211 bool udp_info_h(connection_t *c, const char *request) {
212         char from_name[MAX_STRING_SIZE];
213         char to_name[MAX_STRING_SIZE];
214         char from_address[MAX_STRING_SIZE];
215         char from_port[MAX_STRING_SIZE];
216
217         if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING, from_name, to_name, from_address, from_port) != 4) {
218                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "UDP_INFO", c->name, c->hostname);
219                 return false;
220         }
221
222         if(!check_id(from_name) || !check_id(to_name)) {
223                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "UDP_INFO", c->name, c->hostname, "invalid name");
224                 return false;
225         }
226
227         node_t *from = lookup_node(from_name);
228
229         if(!from) {
230                 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list", "UDP_INFO", c->name, c->hostname, from_name);
231                 return true;
232         }
233
234         if(from != from->via) {
235                 /* Not supposed to happen, as it means the message wandered past a static relay */
236                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got UDP info message from %s (%s) which we can't reach directly", from->name, from->hostname);
237                 return true;
238         }
239
240         /* If we have a direct edge to "from", we are in a better position
241            to guess its address than it is itself. */
242         if(!from->connection && !from->status.udp_confirmed) {
243                 sockaddr_t from_addr = str2sockaddr(from_address, from_port);
244
245                 if(sockaddrcmp(&from_addr, &from->address)) {
246                         update_node_udp(from, &from_addr);
247                 }
248         }
249
250         node_t *to = lookup_node(to_name);
251
252         if(!to) {
253                 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list", "UDP_INFO", c->name, c->hostname, to_name);
254                 return true;
255         }
256
257         /* Send our own data (which could be what we just received) up the chain. */
258
259         return send_udp_info(from, to);
260 }
261
262 /* Transmitting MTU information */
263
264 bool send_mtu_info(node_t *from, node_t *to, int mtu) {
265         /* Skip cases where sending MTU info messages doesn't make sense.
266            This is done here in order to avoid repeating the same logic in multiple callsites. */
267
268         if(to == myself) {
269                 return true;
270         }
271
272         if(!to->status.reachable) {
273                 return true;
274         }
275
276         if(from == myself) {
277                 if(to->connection) {
278                         return true;
279                 }
280
281                 struct timeval elapsed;
282
283                 timersub(&now, &to->mtu_info_sent, &elapsed);
284
285                 if(elapsed.tv_sec < mtu_info_interval) {
286                         return true;
287                 }
288         }
289
290         if((to->nexthop->options >> 24) < 6) {
291                 return true;
292         }
293
294         /* We will send the passed-in MTU value, unless we believe ours is better. */
295
296         node_t *via = (from->via == myself) ? from->nexthop : from->via;
297
298         if(from->minmtu == from->maxmtu && from->via == myself) {
299                 /* We have a direct measurement. Override the value entirely.
300                    Note that we only do that if we are sitting as a static relay in the path;
301                    otherwise, we can't guarantee packets will flow through us, and increasing
302                    MTU could therefore end up being too optimistic. */
303                 mtu = from->minmtu;
304         } else if(via->minmtu == via->maxmtu) {
305                 /* Static relay. Ensure packets will make it through the entire relay path. */
306                 mtu = MIN(mtu, via->minmtu);
307         } else if(via->nexthop->minmtu == via->nexthop->maxmtu) {
308                 /* Dynamic relay. Ensure packets will make it through the entire relay path. */
309                 mtu = MIN(mtu, via->nexthop->minmtu);
310         }
311
312         if(from == myself) {
313                 to->mtu_info_sent = now;
314         }
315
316         /* If none of the conditions above match in the steady state, it means we're using TCP,
317            so the MTU is irrelevant. That said, it is still important to honor the MTU that was passed in,
318            because other parts of the relay path might be able to use UDP, which means they care about the MTU. */
319
320         return send_request(to->nexthop->connection, "%d %s %s %d", MTU_INFO, from->name, to->name, mtu);
321 }
322
323 bool mtu_info_h(connection_t *c, const char *request) {
324         char from_name[MAX_STRING_SIZE];
325         char to_name[MAX_STRING_SIZE];
326         int mtu;
327
328         if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" %d", from_name, to_name, &mtu) != 3) {
329                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "MTU_INFO", c->name, c->hostname);
330                 return false;
331         }
332
333         if(mtu < 512) {
334                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "MTU_INFO", c->name, c->hostname, "invalid MTU");
335                 return false;
336         }
337
338         mtu = MIN(mtu, MTU);
339
340         if(!check_id(from_name) || !check_id(to_name)) {
341                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "MTU_INFO", c->name, c->hostname, "invalid name");
342                 return false;
343         }
344
345         node_t *from = lookup_node(from_name);
346
347         if(!from) {
348                 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list", "MTU_INFO", c->name, c->hostname, from_name);
349                 return true;
350         }
351
352         /* If we don't know the current MTU for that node, use the one we received.
353            Even if we're about to make our own measurements, the value we got from downstream nodes should be pretty close
354            so it's a good idea to use it in the mean time. */
355         if(from->mtu != mtu && from->minmtu != from->maxmtu) {
356                 logger(DEBUG_TRAFFIC, LOG_INFO, "Using provisional MTU %d for node %s (%s)", mtu, from->name, from->hostname);
357                 from->mtu = mtu;
358         }
359
360         node_t *to = lookup_node(to_name);
361
362         if(!to) {
363                 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list", "MTU_INFO", c->name, c->hostname, to_name);
364                 return true;
365         }
366
367         /* Continue passing the MTU value (or a better one if we have it) up the chain. */
368
369         return send_mtu_info(from, to, mtu);
370 }