]> git.meshlink.io Git - meshlink/blob - src/pmtu.c
84fd23af8c4290b0a20f9c094852b766e359c62d
[meshlink] / src / pmtu.c
1 /*
2     pmtu.c -- PMTU probing
3     Copyright (C) 2020 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "crypto.h"
23 #include "logger.h"
24 #include "net.h"
25 #include "netutl.h"
26 #include "node.h"
27 #include "pmtu.h"
28 #include "protocol.h"
29 #include "utils.h"
30
31 /* PMTU probing serves two purposes:
32  *
33  * - establishing a working UDP connection between two peers
34  * - determining the path MTU (PMTU) between two peers
35  *
36  * Establishing a working UDP connection requires NAT hole punching and regular
37  * packets to keep the NAT mappings alive.  For this, we can use very small UDP
38  * packets, and send them rather frequently (once every 10 seconds).  This also
39  * allows us to detect connection loss rather quickly.
40  *
41  * For PMTU discovery, we need to send packets of various size, and determine
42  * which ones are received by the other end.  Once the PMTU is established, we
43  * want to keep monitoring that the discovered PMTU value is still valid.
44  * However, we assume PMTU changes are unlikely, so they do not have to be done
45  * very often.
46  *
47  * To keep track of how far we are in the PMTU probing process, the variable
48  * mtuprobes is used. The meaning of its value is:
49  *
50  * - mtuprobes ==    -4: maxmtu no longer valid, reset minmtu and maxmtu and go to 0
51  * - mtuprobes ==-2..-3: send one maxmtu probe every second
52  * - mtuprobes ==    -1: send one maxmtu and one maxmtu + 1 probe every pinginterval
53  * - mtuprobes == 0..19: initial discovery, send three packets per second, mtuprobes++
54  * - mtuprobes ==    20: fix PMTU, and go to -1
55  *
56  * The first probe is always the maximum MTU supported by the interface,
57  * then a binary search is done until the minimum and maximum converge,
58  * or until 20 packets have been sent.
59  *
60  * After the initial discovery, PMTU probing only sends two packets; one with
61  * the same size as the discovered PMTU, and one which has a size slightly
62  * larger than the currently known PMTU, to test if the PMTU has increased.
63  */
64
65 static void try_fix_mtu(meshlink_handle_t *mesh, node_t *n) {
66         if(n->mtuprobes < 0) {
67                 return;
68         }
69
70         if(n->mtuprobes == 20 || n->minmtu >= n->maxmtu) {
71                 if(n->minmtu > n->maxmtu) {
72                         n->minmtu = n->maxmtu;
73                 } else {
74                         n->maxmtu = n->minmtu;
75                 }
76
77                 n->mtu = n->minmtu;
78                 logger(mesh, MESHLINK_INFO, "Fixing PMTU of %s to %d after %d probes", n->name, n->mtu, n->mtuprobes);
79                 n->mtuprobes = -1;
80         }
81 }
82
83 static void udp_probe_timeout_handler(event_loop_t *loop, void *data) {
84         node_t *n = data;
85         meshlink_handle_t *mesh = loop->data;
86
87         if(!n->status.udp_confirmed) {
88                 return;
89         }
90
91         logger(mesh, MESHLINK_INFO, "Too much time has elapsed since last UDP ping response from %s, stopping UDP communication", n->name);
92         n->status.udp_confirmed = false;
93         n->mtuprobes = 0;
94         n->minmtu = 0;
95         n->maxmtu = MTU;
96 }
97
98 static void send_udp_probe_reply(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *packet, uint16_t len) {
99         if(!n->status.validkey) {
100                 logger(mesh, MESHLINK_INFO, "Trying to send UDP probe reply to %s but we don't have his key yet", n->name);
101                 return;
102         }
103
104         packet->data[0] = 1;
105
106         if(packet->data[1]) {
107                 packet->data[1] = 1;
108                 memcpy(packet->data + 2, &len, 2);
109                 len = MIN_PROBE_SIZE;
110         }
111
112         /* Temporarily set udp_confirmed, so that the reply is sent
113            back exactly the way it came in. */
114
115         bool udp_confirmed = n->status.udp_confirmed;
116         n->status.udp_confirmed = true;
117         logger(mesh, MESHLINK_DEBUG, "Sending UDP reply length %d to %s", packet->len, n->name);
118         n->out_meta += packet->len;
119         send_udppacket(mesh, n, packet);
120         n->status.udp_confirmed = udp_confirmed;
121 }
122
123 void udp_probe_h(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *packet, uint16_t len) {
124         if(len < MIN_PROBE_SIZE) {
125                 logger(mesh, MESHLINK_WARNING, "Got too short PMTU probe length %d from %s", packet->len, n->name);
126                 return;
127         }
128
129         n->in_meta += packet->len;
130
131         if(!packet->data[0]) {
132                 /* It's a probe request, send back a reply */
133                 logger(mesh, MESHLINK_DEBUG, "Got PMTU probe length %d from %s", packet->len, n->name);
134                 send_udp_probe_reply(mesh, n, packet, len);
135                 return;
136         }
137
138         if(packet->data[1]) {
139                 memcpy(&len, packet->data + 2, 2);
140         }
141
142         logger(mesh, MESHLINK_DEBUG, "Got PMTU reply length %d from %s", len, n->name);
143
144         /* It's a valid reply: now we know bidirectional communication
145            is possible using the address and socket that the reply
146            packet used. */
147         if(!n->status.udp_confirmed) {
148                 char *address, *port;
149                 sockaddr2str(&n->address, &address, &port);
150                 send_request(mesh, n->nexthop->connection, NULL, "%d %s %s . -1 -1 -1 0 %s %s", ANS_KEY, n->name, n->name, address, port);
151
152                 free(address);
153                 free(port);
154                 n->status.udp_confirmed = true;
155         }
156
157         // Reset the UDP ping timer.
158
159         timeout_del(&mesh->loop, &n->udp_ping_timeout);
160         timeout_add(&mesh->loop, &n->udp_ping_timeout, &udp_probe_timeout_handler, n, &(struct timespec) {
161                 30, 0
162         });
163
164         if(len > n->maxmtu) {
165                 logger(mesh, MESHLINK_INFO, "Increase in PMTU to %s detected, restarting PMTU discovery", n->name);
166                 n->minmtu = len;
167                 n->maxmtu = MTU;
168                 /* Set mtuprobes to 1 so that try_pmtu() doesn't reset maxmtu */
169                 n->mtuprobes = 1;
170                 return;
171         } else if(n->mtuprobes < 0 && len == n->maxmtu) {
172                 /* We got a maxmtu sized packet, confirming the PMTU is still valid. */
173                 n->mtuprobes = -1;
174                 n->last_mtu_probe_sent = mesh->loop.now;
175         }
176
177         /* If applicable, raise the minimum supported PMTU */
178
179         if(n->minmtu < len) {
180                 n->minmtu = len;
181                 update_node_pmtu(mesh, n);
182         }
183
184         try_fix_mtu(mesh, n);
185 }
186
187 static void send_udp_probe_packet(meshlink_handle_t *mesh, node_t *n, int len) {
188         if(len < MIN_PROBE_SIZE) {
189                 len = MIN_PROBE_SIZE;
190         }
191
192         vpn_packet_t packet;
193         memset(packet.data, 0, 4);
194         packet.probe = true;
195         packet.data[0] = 0;
196         packet.data[1] = 1;
197         packet.data[2] = 0;
198         packet.data[3] = 0;
199
200         if(len > 4) {
201                 randomize(packet.data + 4, len - 4);
202         }
203
204         packet.len = len;
205
206         logger(mesh, MESHLINK_DEBUG, "Sending UDP probe length %d to %s", len, n->name);
207
208         n->out_meta += packet.len;
209         send_udppacket(mesh, n, &packet);
210 }
211
212 static void try_udp(meshlink_handle_t *mesh, node_t *n) {
213         /* Probe request */
214
215         struct timespec elapsed;
216         timespec_sub(&mesh->loop.now, &n->last_udp_probe_sent, &elapsed);
217
218         int interval = n->status.udp_confirmed ? 10 : 2;
219
220         logger(mesh, MESHLINK_DEBUG, "try_udp(%s) %d %d\n", n->name, (int)elapsed.tv_sec, interval);
221
222         if(elapsed.tv_sec >= interval) {
223                 n->last_udp_probe_sent = mesh->loop.now;
224                 send_udp_probe_packet(mesh, n, MIN_PROBE_SIZE);
225
226                 if(!n->status.udp_confirmed && n->prevedge) {
227                         n->status.broadcast = true;
228                         send_udp_probe_packet(mesh, n, MIN_PROBE_SIZE);
229                         n->status.broadcast = false;
230                 }
231         }
232 }
233
234 static uint16_t choose_initial_maxmtu(meshlink_handle_t *mesh, node_t *n) {
235 #ifdef IP_MTU
236
237         int sock = -1;
238
239         sockaddr_t sa_buf;
240         const sockaddr_t *sa;
241         int sockindex;
242         choose_udp_address(mesh, n, &sa, &sockindex, &sa_buf);
243
244         if(!sa) {
245                 return MTU;
246         }
247
248         sock = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
249
250         if(sock < 0) {
251                 logger(mesh, MESHLINK_ERROR, "Creating MTU assessment socket for %s failed: %s", n->name, sockstrerror(sockerrno));
252                 return MTU;
253         }
254
255         if(connect(sock, &sa->sa, SALEN(sa->sa))) {
256                 logger(mesh, MESHLINK_ERROR, "Connecting MTU assessment socket for %s failed: %s", n->name, sockstrerror(sockerrno));
257                 close(sock);
258                 return MTU;
259         }
260
261         int ip_mtu;
262         socklen_t ip_mtu_len = sizeof(ip_mtu);
263
264         if(getsockopt(sock, IPPROTO_IP, IP_MTU, &ip_mtu, &ip_mtu_len)) {
265                 logger(mesh, MESHLINK_ERROR, "getsockopt(IP_MTU) on %s failed: %s", n->name, sockstrerror(sockerrno));
266                 close(sock);
267                 return MTU;
268         }
269
270         close(sock);
271
272         /* Calculate the maximum SPTPS payload based on the interface MTU */
273         uint16_t mtu = ip_mtu;
274         mtu -= (sa->sa.sa_family == AF_INET6) ? 40 : 20; /* IPv6 or IPv4 */
275         mtu -= 8; /* UDP */
276         mtu -= 21; /* SPTPS */
277
278         if(mtu < 512) {
279                 logger(mesh, MESHLINK_ERROR, "getsockopt(IP_MTU) on %s returned absurdly small value: %d", n->name, ip_mtu);
280                 return MTU;
281         }
282
283         if(mtu > MTU) {
284                 return MTU;
285         }
286
287         logger(mesh, MESHLINK_INFO, "Using system-provided maximum MTU for %s: %hd", n->name, mtu);
288         return mtu;
289
290 #else
291         (void)n;
292         return MTU;
293 #endif
294 }
295
296 /* This function tries to determines the PMTU of a node.
297    By calling this function repeatedly, n->minmtu will be progressively
298    increased, and at some point, n->mtu will be fixed to n->minmtu.  If the PMTU
299    is already fixed, this function checks if it can be increased.
300 */
301
302 static void try_pmtu(meshlink_handle_t *mesh, node_t *n) {
303         logger(mesh, MESHLINK_DEBUG, "try_pmtu(%s) %d %d\n", n->name, n->mtuprobes, n->status.udp_confirmed);
304
305         if(!n->status.udp_confirmed) {
306                 n->mtuprobes = 0;
307                 n->minmtu = 0;
308                 n->maxmtu = MTU;
309                 return;
310         }
311
312         struct timespec elapsed;
313
314         timespec_sub(&mesh->loop.now, &n->last_mtu_probe_sent, &elapsed);
315
316         if(n->mtuprobes >= 0) {
317                 /* Fast probing, send three packets per second */
318                 if(n->mtuprobes != 0 && elapsed.tv_sec == 0 && elapsed.tv_nsec < 333333333) {
319                         return;
320                 }
321         } else {
322                 if(n->mtuprobes < -1) {
323                         /* We didn't get an answer to the last probe, try again once every second */
324                         if(elapsed.tv_sec < 1) {
325                                 return;
326                         }
327                 } else {
328                         /* Slow probing, send one packet every pinginterval */
329                         int pinginterval = mesh->dev_class_traits[n->devclass].pinginterval;
330
331                         if(elapsed.tv_sec < pinginterval) {
332                                 return;
333                         }
334                 }
335         }
336
337         n->last_mtu_probe_sent = mesh->loop.now;
338
339         if(n->mtuprobes < -3) {
340                 /* We lost three PMTU probes, restart discovery */
341                 logger(mesh, MESHLINK_INFO, "Decrease in PMTU to %s detected, restarting PMTU discovery", n->name);
342                 n->mtuprobes = 0;
343                 n->minmtu = 0;
344         }
345
346         if(n->mtuprobes < 0) {
347                 /* After the initial discovery, we only send one maxmtu and one
348                    maxmtu + 1 probe to detect PMTU increases. */
349                 send_udp_probe_packet(mesh, n, n->maxmtu);
350
351                 if(n->mtuprobes == -1 && n->maxmtu + 1 < MTU) {
352                         send_udp_probe_packet(mesh, n, n->maxmtu + 1);
353                 }
354
355                 n->mtuprobes--;
356         } else {
357                 /* Starting parameters. */
358                 uint16_t len;
359
360                 if(n->mtuprobes == 0) {
361                         /* First packet is always the maximum MTU size */
362                         n->maxmtu = choose_initial_maxmtu(mesh, n);
363                         len = n->maxmtu;
364                 } else {
365                         if(n->last_mtu_len == n->minmtu) {
366                                 /* The previous probe was succesful, increase the size */
367                                 len = n->minmtu + (n->maxmtu - n->minmtu + 1) / 2;
368                         } else {
369                                 /* The previous probe was unsuccesful, decrease the size */
370                                 len = n->minmtu + (n->last_mtu_len - n->minmtu) / 2;
371                         }
372                 }
373
374                 n->last_mtu_len = len;
375                 send_udp_probe_packet(mesh, n, len);
376                 n->mtuprobes++;
377         }
378
379         try_fix_mtu(mesh, n);
380 }
381
382 /* Keep the connection to the given node alive.
383  * Ensure we have a valid key, and check whether UDP is working.
384  */
385
386 void keepalive(meshlink_handle_t *mesh, node_t *n, bool traffic) {
387         logger(mesh, MESHLINK_DEBUG, "keepalive(%s) %d %d\n", n->name, n->status.reachable, n->status.validkey);
388
389         if(!n->status.reachable || !n->status.validkey) {
390                 return;
391         }
392
393         try_udp(mesh, n);
394
395         if(traffic) {
396                 try_pmtu(mesh, n);
397         }
398
399         /* If we want to send traffic but we don't have a working UDP
400          * connection, we are going to forward the traffic to the nexthop, so
401          * try to keep that one alive as well. */
402
403         if(traffic && !n->status.udp_confirmed && n != n->nexthop) {
404                 keepalive(mesh, n->nexthop, traffic);
405         }
406 }
407