2 protocol_misc.c -- handle the meta-protocol, miscellaneous functions
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2022 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
23 #include "address_cache.h"
24 #include "connection.h"
33 int maxoutbufsize = 0;
34 int mtu_info_interval = 5;
35 int udp_info_interval = 5;
37 bool send_termreq(connection_t *c) {
38 return send_request(c, "%d", TERMREQ);
41 bool termreq_h(connection_t *c, const char *request) {
47 bool send_ping(connection_t *c) {
48 c->status.pinged = true;
49 c->last_ping_time = now.tv_sec;
51 return send_request(c, "%d", PING);
54 bool ping_h(connection_t *c, const char *request) {
59 bool send_pong(connection_t *c) {
60 return send_request(c, "%d", PONG);
63 bool pong_h(connection_t *c, const char *request) {
65 c->status.pinged = false;
67 /* Successful connection, reset timeout if this is an outgoing connection. */
69 if(c->outgoing && c->outgoing->timeout) {
70 c->outgoing->timeout = 0;
71 reset_address_cache(c->node->address_cache);
72 add_recent_address(c->node->address_cache, &c->address);
78 static bool random_early_drop(connection_t *c) {
79 if(c->outbuf.len > (size_t)maxoutbufsize / 2) {
80 if((c->outbuf.len - (size_t)maxoutbufsize / 2) > prng((size_t)maxoutbufsize / 2)) {
88 /* Sending and receiving packets via TCP */
90 bool send_tcppacket(connection_t *c, const vpn_packet_t *packet) {
91 /* If there already is a lot of data in the outbuf buffer, discard this packet.
92 We use a very simple Random Early Drop algorithm. */
94 if(random_early_drop(c)) {
98 if(!send_request(c, "%d %d", PACKET, packet->len)) {
102 return send_meta(c, DATA(packet), packet->len);
105 bool tcppacket_h(connection_t *c, const char *request) {
108 if(sscanf(request, "%*d %hd", &len) != 1 || len < 0) {
109 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "PACKET", c->name,
114 /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
121 bool send_sptps_tcppacket(connection_t *c, const void *packet, size_t len) {
122 /* If there already is a lot of data in the outbuf buffer, discard this packet.
123 We use a very simple Random Early Drop algorithm. */
125 if(random_early_drop(c)) {
129 if(!send_request(c, "%d %lu", SPTPS_PACKET, (unsigned long)len)) {
133 send_meta_raw(c, packet, len);
137 bool sptps_tcppacket_h(connection_t *c, const char *request) {
140 if(sscanf(request, "%*d %hd", &len) != 1 || len < 0) {
141 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "SPTPS_PACKET", c->name,
146 /* Set sptpslen to len, this will tell receive_meta() that a SPTPS packet is coming. */
153 /* Transmitting UDP information */
155 bool send_udp_info(node_t *from, node_t *to) {
156 /* If there's a static relay in the path, there's no point in sending the message
157 farther than the static relay. */
158 to = (to->via == myself) ? to->nexthop : to->via;
161 logger(DEBUG_ALWAYS, LOG_ERR, "Something went wrong when selecting relay - possible fake UDP_INFO");
165 /* Skip cases where sending UDP info messages doesn't make sense.
166 This is done here in order to avoid repeating the same logic in multiple callsites. */
172 if(!to->status.reachable) {
181 struct timeval elapsed;
183 timersub(&now, &to->udp_info_sent, &elapsed);
185 if(elapsed.tv_sec < udp_info_interval) {
190 if((myself->options | from->options | to->options) & OPTION_TCPONLY) {
194 if((to->nexthop->options >> 24) < 5) {
198 char *from_address, *from_port;
199 /* If we're the originator, the address we use is irrelevant
200 because the first intermediate node will ignore it.
201 We use our local address as it somewhat makes sense
202 and it's simpler than introducing an encoding for "null" addresses anyway. */
203 sockaddr2str((from != myself) ? &from->address : &to->nexthop->connection->edge->local_address, &from_address, &from_port);
205 bool x = send_request(to->nexthop->connection, "%d %s %s %s %s", UDP_INFO, from->name, to->name, from_address, from_port);
211 to->udp_info_sent = now;
217 bool udp_info_h(connection_t *c, const char *request) {
218 char from_name[MAX_STRING_SIZE];
219 char to_name[MAX_STRING_SIZE];
220 char from_address[MAX_STRING_SIZE];
221 char from_port[MAX_STRING_SIZE];
223 if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING, from_name, to_name, from_address, from_port) != 4) {
224 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "UDP_INFO", c->name, c->hostname);
228 if(!check_id(from_name) || !check_id(to_name)) {
229 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "UDP_INFO", c->name, c->hostname, "invalid name");
233 node_t *from = lookup_node(from_name);
236 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);
240 if(from != from->via) {
241 /* Not supposed to happen, as it means the message wandered past a static relay */
242 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got UDP info message from %s (%s) which we can't reach directly", from->name, from->hostname);
246 /* If we have a direct edge to "from", we are in a better position
247 to guess its address than it is itself. */
248 if(!from->connection && !from->status.udp_confirmed) {
249 sockaddr_t from_addr = str2sockaddr(from_address, from_port);
251 if(sockaddrcmp(&from_addr, &from->address)) {
252 update_node_udp(from, &from_addr);
256 node_t *to = lookup_node(to_name);
259 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);
263 /* Send our own data (which could be what we just received) up the chain. */
265 return send_udp_info(from, to);
268 /* Transmitting MTU information */
270 bool send_mtu_info(node_t *from, node_t *to, int mtu) {
271 /* Skip cases where sending MTU info messages doesn't make sense.
272 This is done here in order to avoid repeating the same logic in multiple callsites. */
278 if(!to->status.reachable) {
287 struct timeval elapsed;
289 timersub(&now, &to->mtu_info_sent, &elapsed);
291 if(elapsed.tv_sec < mtu_info_interval) {
296 if((to->nexthop->options >> 24) < 6) {
300 /* We will send the passed-in MTU value, unless we believe ours is better. */
302 node_t *via = (from->via == myself) ? from->nexthop : from->via;
304 if(from->minmtu == from->maxmtu && from->via == myself) {
305 /* We have a direct measurement. Override the value entirely.
306 Note that we only do that if we are sitting as a static relay in the path;
307 otherwise, we can't guarantee packets will flow through us, and increasing
308 MTU could therefore end up being too optimistic. */
310 } else if(via->minmtu == via->maxmtu) {
311 /* Static relay. Ensure packets will make it through the entire relay path. */
312 mtu = MIN(mtu, via->minmtu);
313 } else if(via->nexthop->minmtu == via->nexthop->maxmtu) {
314 /* Dynamic relay. Ensure packets will make it through the entire relay path. */
315 mtu = MIN(mtu, via->nexthop->minmtu);
319 to->mtu_info_sent = now;
322 /* If none of the conditions above match in the steady state, it means we're using TCP,
323 so the MTU is irrelevant. That said, it is still important to honor the MTU that was passed in,
324 because other parts of the relay path might be able to use UDP, which means they care about the MTU. */
326 return send_request(to->nexthop->connection, "%d %s %s %d", MTU_INFO, from->name, to->name, mtu);
329 bool mtu_info_h(connection_t *c, const char *request) {
330 char from_name[MAX_STRING_SIZE];
331 char to_name[MAX_STRING_SIZE];
334 if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" %d", from_name, to_name, &mtu) != 3) {
335 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "MTU_INFO", c->name, c->hostname);
340 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "MTU_INFO", c->name, c->hostname, "invalid MTU");
346 if(!check_id(from_name) || !check_id(to_name)) {
347 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "MTU_INFO", c->name, c->hostname, "invalid name");
351 node_t *from = lookup_node(from_name);
354 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);
358 /* If we don't know the current MTU for that node, use the one we received.
359 Even if we're about to make our own measurements, the value we got from downstream nodes should be pretty close
360 so it's a good idea to use it in the mean time. */
361 if(from->mtu != mtu && from->minmtu != from->maxmtu) {
362 logger(DEBUG_TRAFFIC, LOG_INFO, "Using provisional MTU %d for node %s (%s)", mtu, from->name, from->hostname);
366 node_t *to = lookup_node(to_name);
369 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);
373 /* Continue passing the MTU value (or a better one if we have it) up the chain. */
375 return send_mtu_info(from, to, mtu);