]> git.meshlink.io Git - meshlink/blob - src/net_packet.c
Add a global metering notification callback.
[meshlink] / src / net_packet.c
1 /*
2     net_packet.c -- Handles in- and outgoing VPN packets
3     Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
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 "conf.h"
23 #include "connection.h"
24 #include "crypto.h"
25 #include "graph.h"
26 #include "logger.h"
27 #include "meshlink_internal.h"
28 #include "net.h"
29 #include "netutl.h"
30 #include "protocol.h"
31 #include "route.h"
32 #include "sptps.h"
33 #include "utils.h"
34 #include "xalloc.h"
35
36 int keylifetime = 0;
37
38 static void send_udppacket(meshlink_handle_t *mesh, node_t *, vpn_packet_t *);
39
40 #define MAX_SEQNO 1073741824
41 #define PROBE_OVERHEAD (SPTPS_DATAGRAM_OVERHEAD + 40)
42
43 /* mtuprobes == 1..30: initial discovery, send bursts with 1 second interval
44    mtuprobes ==    31: sleep pinginterval seconds
45    mtuprobes ==    32: send 1 burst, sleep pingtimeout second
46    mtuprobes ==    33: no response from other side, restart PMTU discovery process
47
48    Probes are sent in batches of at least three, with random sizes between the
49    lower and upper boundaries for the MTU thus far discovered.
50
51    After the initial discovery, a fourth packet is added to each batch with a
52    size larger than the currently known PMTU, to test if the PMTU has increased.
53
54    In case local discovery is enabled, another packet is added to each batch,
55    which will be broadcast to the local network.
56
57 */
58
59 static void send_mtu_probe_handler(event_loop_t *loop, void *data) {
60         meshlink_handle_t *mesh = loop->data;
61         node_t *n = data;
62         int timeout = 1;
63
64         n->mtuprobes++;
65
66         if(!n->status.reachable || !n->status.validkey) {
67                 logger(mesh, MESHLINK_INFO, "Trying to send MTU probe to unreachable or rekeying node %s", n->name);
68                 n->mtuprobes = 0;
69                 return;
70         }
71
72         if(n->mtuprobes > 32) {
73                 if(!n->minmtu) {
74                         n->mtuprobes = 31;
75                         timeout = mesh->dev_class_traits[n->devclass].pinginterval;
76                         goto end;
77                 }
78
79                 logger(mesh, MESHLINK_INFO, "%s did not respond to UDP ping, restarting PMTU discovery", n->name);
80                 n->status.udp_confirmed = false;
81                 n->mtuprobes = 1;
82                 n->minmtu = 0;
83                 n->maxmtu = MTU;
84
85                 update_node_pmtu(mesh, n);
86         }
87
88         if(n->mtuprobes >= 10 && n->mtuprobes < 32 && !n->minmtu) {
89                 logger(mesh, MESHLINK_INFO, "No response to MTU probes from %s", n->name);
90                 n->mtuprobes = 31;
91         }
92
93         if(n->mtuprobes == 30 || (n->mtuprobes < 30 && n->minmtu >= n->maxmtu)) {
94                 if(n->minmtu > n->maxmtu) {
95                         n->minmtu = n->maxmtu;
96                         update_node_pmtu(mesh, n);
97                 } else {
98                         n->maxmtu = n->minmtu;
99                 }
100
101                 n->mtu = n->minmtu;
102                 logger(mesh, MESHLINK_INFO, "Fixing MTU of %s to %d after %d probes", n->name, n->mtu, n->mtuprobes);
103                 n->mtuprobes = 31;
104         }
105
106         if(n->mtuprobes == 31) {
107                 if(!n->minmtu && n->status.want_udp && n->nexthop && n->nexthop->connection) {
108                         /* Send a dummy ANS_KEY to try to update the reflexive UDP address */
109                         send_request(mesh, n->nexthop->connection, NULL, "%d %s %s . -1 -1 -1 0", ANS_KEY, mesh->self->name, n->name);
110                         n->status.want_udp = false;
111                 }
112
113                 timeout = mesh->dev_class_traits[n->devclass].pinginterval;
114                 goto end;
115         } else if(n->mtuprobes == 32) {
116                 timeout = mesh->dev_class_traits[n->devclass].pingtimeout;
117         }
118
119         for(int i = 0; i < 5; i++) {
120                 int len;
121
122                 if(i == 0) {
123                         if(n->mtuprobes < 30 || n->maxmtu + 8 >= MTU) {
124                                 continue;
125                         }
126
127                         len = n->maxmtu + 8;
128                 } else if(n->maxmtu <= n->minmtu) {
129                         len = n->maxmtu;
130                 } else {
131                         len = n->minmtu + 1 + prng(mesh, n->maxmtu - n->minmtu);
132                 }
133
134                 if(len < 64) {
135                         len = 64;
136                 }
137
138                 vpn_packet_t packet;
139                 packet.probe = true;
140                 memset(packet.data, 0, 14);
141                 randomize(packet.data + 14, len - 14);
142                 packet.len = len;
143                 n->status.broadcast = i >= 4 && n->mtuprobes <= 10 && n->prevedge;
144
145                 logger(mesh, MESHLINK_DEBUG, "Sending MTU probe length %d to %s", len, n->name);
146
147                 n->out_meta += packet.len + PROBE_OVERHEAD;
148                 mesh->self->out_meta += packet.len + PROBE_OVERHEAD;
149                 send_udppacket(mesh, n, &packet);
150         }
151
152         n->status.broadcast = false;
153
154 end:
155         timeout_set(&mesh->loop, &n->mtutimeout, &(struct timespec) {
156                 timeout, prng(mesh, TIMER_FUDGE)
157         });
158 }
159
160 void send_mtu_probe(meshlink_handle_t *mesh, node_t *n) {
161         timeout_add(&mesh->loop, &n->mtutimeout, send_mtu_probe_handler, n, &(struct timespec) {
162                 1, 0
163         });
164         send_mtu_probe_handler(&mesh->loop, n);
165 }
166
167 static void mtu_probe_h(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *packet, uint16_t len) {
168         n->in_meta += len + PROBE_OVERHEAD;
169         mesh->self->in_meta += len + PROBE_OVERHEAD;
170
171         if(len < 64) {
172                 logger(mesh, MESHLINK_WARNING, "Got too short MTU probe length %d from %s", packet->len, n->name);
173                 return;
174         }
175
176         logger(mesh, MESHLINK_DEBUG, "Got MTU probe length %d from %s", packet->len, n->name);
177
178         if(!packet->data[0]) {
179                 /* It's a probe request, send back a reply */
180
181                 packet->data[0] = 1;
182
183                 /* Temporarily set udp_confirmed, so that the reply is sent
184                    back exactly the way it came in. */
185
186                 bool udp_confirmed = n->status.udp_confirmed;
187                 n->status.udp_confirmed = true;
188                 logger(mesh, MESHLINK_DEBUG, "Sending MTU probe reply %d to %s", packet->len, n->name);
189                 n->out_meta += packet->len + PROBE_OVERHEAD;
190                 mesh->self->out_meta += packet->len + PROBE_OVERHEAD;
191                 send_udppacket(mesh, n, packet);
192                 n->status.udp_confirmed = udp_confirmed;
193         } else {
194                 /* It's a valid reply: now we know bidirectional communication
195                    is possible using the address and socket that the reply
196                    packet used. */
197
198                 if(!n->status.udp_confirmed) {
199                         char *address, *port;
200                         sockaddr2str(&n->address, &address, &port);
201
202                         if(n->nexthop && n->nexthop->connection) {
203                                 send_request(mesh, n->nexthop->connection, NULL, "%d %s %s . -1 -1 -1 0 %s %s", ANS_KEY, n->name, n->name, address, port);
204                         } else {
205                                 logger(mesh, MESHLINK_WARNING, "Cannot send reflexive address to %s via %s", n->name, n->nexthop ? n->nexthop->name : n->name);
206                         }
207
208                         free(address);
209                         free(port);
210                         n->status.udp_confirmed = true;
211                 }
212
213                 /* If we haven't established the PMTU yet, restart the discovery process. */
214
215                 if(n->mtuprobes > 30) {
216                         if(len == n->maxmtu + 8) {
217                                 logger(mesh, MESHLINK_INFO, "Increase in PMTU to %s detected, restarting PMTU discovery", n->name);
218                                 n->maxmtu = MTU;
219                                 n->mtuprobes = 10;
220                                 return;
221                         }
222
223                         if(n->minmtu) {
224                                 n->mtuprobes = 30;
225                         } else {
226                                 n->mtuprobes = 1;
227                         }
228                 }
229
230                 /* If applicable, raise the minimum supported MTU */
231
232                 if(len > n->maxmtu) {
233                         len = n->maxmtu;
234                 }
235
236                 if(n->minmtu < len) {
237                         n->minmtu = len;
238                         update_node_pmtu(mesh, n);
239                 }
240         }
241 }
242
243 /* VPN packet I/O */
244
245 static void receive_packet(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *packet) {
246         logger(mesh, MESHLINK_DEBUG, "Received packet of %d bytes from %s", packet->len, n->name);
247
248         if(n->status.blacklisted) {
249                 logger(mesh, MESHLINK_WARNING, "Dropping packet from blacklisted node %s", n->name);
250         } else {
251                 route(mesh, n, packet);
252         }
253 }
254
255 static bool try_mac(meshlink_handle_t *mesh, node_t *n, const vpn_packet_t *inpkt) {
256         (void)mesh;
257         return sptps_verify_datagram(&n->sptps, inpkt->data, inpkt->len);
258 }
259
260 static void receive_udppacket(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *inpkt) {
261         if(!n->status.reachable) {
262                 logger(mesh, MESHLINK_ERROR, "Got SPTPS data from unreachable node %s", n->name);
263                 return;
264         }
265
266         if(!n->sptps.state) {
267                 if(!n->status.waitingforkey) {
268                         logger(mesh, MESHLINK_DEBUG, "Got packet from %s but we haven't exchanged keys yet", n->name);
269                         send_req_key(mesh, n);
270                 } else {
271                         logger(mesh, MESHLINK_DEBUG, "Got packet from %s but he hasn't got our key yet", n->name);
272                 }
273
274                 return;
275         }
276
277         if(!sptps_receive_data(&n->sptps, inpkt->data, inpkt->len)) {
278                 logger(mesh, MESHLINK_ERROR, "Could not process SPTPS data from %s: %s", n->name, strerror(errno));
279         }
280 }
281
282 static void send_sptps_packet(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *origpkt) {
283         if(!n->status.reachable) {
284                 logger(mesh, MESHLINK_ERROR, "Trying to send SPTPS data to unreachable node %s", n->name);
285                 return;
286         }
287
288         if(!n->status.validkey) {
289                 if(n->connection && (n->connection->flags & PROTOCOL_TINY) & n->connection->status.active) {
290                         send_raw_packet(mesh, n->connection, origpkt);
291                         return;
292                 }
293
294                 logger(mesh, MESHLINK_INFO, "No valid key known yet for %s", n->name);
295
296                 if(!n->status.waitingforkey) {
297                         send_req_key(mesh, n);
298                 } else if(n->last_req_key + 10 < mesh->loop.now.tv_sec) {
299                         logger(mesh, MESHLINK_DEBUG, "No key from %s after 10 seconds, restarting SPTPS", n->name);
300                         sptps_stop(&n->sptps);
301                         n->status.waitingforkey = false;
302                         send_req_key(mesh, n);
303                 }
304
305                 return;
306         }
307
308         uint8_t type = 0;
309
310         // If it's a probe, send it immediately without trying to compress it.
311         if(origpkt->probe) {
312                 sptps_send_record(&n->sptps, PKT_PROBE, origpkt->data, origpkt->len);
313                 return;
314         }
315
316         sptps_send_record(&n->sptps, type, origpkt->data, origpkt->len);
317         return;
318 }
319
320 static void choose_udp_address(meshlink_handle_t *mesh, const node_t *n, const sockaddr_t **sa, int *sock, sockaddr_t *sa_buf) {
321         /* Latest guess */
322         *sa = &n->address;
323         *sock = n->sock;
324
325         /* If the UDP address is confirmed, use it. */
326         if(n->status.udp_confirmed) {
327                 return;
328         }
329
330         /* Send every third packet to n->address; that could be set
331            to the node's reflexive UDP address discovered during key
332            exchange. */
333
334         if(++mesh->udp_choice >= 3) {
335                 mesh->udp_choice = 0;
336                 return;
337         }
338
339         /* If we have learned an address via Catta, try this once every batch */
340         if(mesh->udp_choice == 1 && n->catta_address.sa.sa_family != AF_UNSPEC) {
341                 *sa = &n->catta_address;
342                 goto check_socket;
343         }
344
345         /* Else, if we have a canonical address, try this once every batch */
346         if(mesh->udp_choice == 1 && n->canonical_address) {
347                 char *host = xstrdup(n->canonical_address);
348                 char *port = strchr(host, ' ');
349
350                 if(port) {
351                         *port++ = 0;
352                         *sa_buf = str2sockaddr_random(mesh, host, port);
353                         *sa = sa_buf;
354
355                         if(sa_buf->sa.sa_family != AF_UNKNOWN) {
356                                 free(host);
357                                 goto check_socket;
358                         }
359                 }
360
361                 free(host);
362         }
363
364         /* Otherwise, address are found in edges to this node.
365            So we pick a random edge and a random socket. */
366
367         edge_t *candidate = NULL;
368
369         {
370                 int i = 0;
371                 int j = prng(mesh, n->edge_tree->count);
372
373                 for splay_each(edge_t, e, n->edge_tree) {
374                         if(i++ == j) {
375                                 candidate = e->reverse;
376                                 break;
377                         }
378                 }
379         }
380
381         if(candidate) {
382                 *sa = &candidate->address;
383                 *sock = prng(mesh, mesh->listen_sockets);
384         }
385
386 check_socket:
387
388         /* Make sure we have a suitable socket for the chosen address */
389         if(mesh->listen_socket[*sock].sa.sa.sa_family != (*sa)->sa.sa_family) {
390                 for(int i = 0; i < mesh->listen_sockets; i++) {
391                         if(mesh->listen_socket[i].sa.sa.sa_family == (*sa)->sa.sa_family) {
392                                 *sock = i;
393                                 break;
394                         }
395                 }
396         }
397 }
398
399 static void choose_broadcast_address(meshlink_handle_t *mesh, const node_t *n, const sockaddr_t **sa, int *sock) {
400         *sock = prng(mesh, mesh->listen_sockets);
401         sockaddr_t *broadcast_sa = &mesh->listen_socket[*sock].broadcast_sa;
402
403         if(broadcast_sa->sa.sa_family == AF_INET6) {
404                 broadcast_sa->in6.sin6_port = n->prevedge->address.in.sin_port;
405         } else {
406                 broadcast_sa->in.sin_port = n->prevedge->address.in.sin_port;
407         }
408
409         *sa = broadcast_sa;
410 }
411
412 static void send_udppacket(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *origpkt) {
413         if(!n->status.reachable) {
414                 logger(mesh, MESHLINK_INFO, "Trying to send UDP packet to unreachable node %s", n->name);
415                 return;
416         }
417
418         send_sptps_packet(mesh, n, origpkt);
419 }
420
421 bool send_sptps_data(void *handle, uint8_t type, const void *data, size_t len) {
422         assert(handle);
423         assert(data);
424         assert(len);
425
426         node_t *to = handle;
427         meshlink_handle_t *mesh = to->mesh;
428
429         if(!to->status.reachable) {
430                 logger(mesh, MESHLINK_ERROR, "Trying to send SPTPS data to unreachable node %s", to->name);
431                 return false;
432         }
433
434         /* Send it via TCP if it is a handshake packet, TCPOnly is in use, or this packet is larger than the MTU. */
435
436         if(type >= SPTPS_HANDSHAKE || (type != PKT_PROBE && (len - 21) > to->minmtu)) {
437                 char buf[len * 4 / 3 + 5];
438                 b64encode(data, buf, len);
439
440                 if(!to->nexthop || !to->nexthop->connection) {
441                         logger(mesh, MESHLINK_WARNING, "Unable to forward SPTPS packet to %s via %s", to->name, to->nexthop ? to->nexthop->name : to->name);
442                         return false;
443                 }
444
445                 /* If no valid key is known yet, send the packets using ANS_KEY requests,
446                    to ensure we get to learn the reflexive UDP address. */
447                 if(!to->status.validkey) {
448                         return send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %s -1 -1 -1 %d", ANS_KEY, mesh->self->name, to->name, buf, 0);
449                 } else {
450                         return send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_SPTPS, buf);
451                 }
452         }
453
454         /* Otherwise, send the packet via UDP */
455
456         sockaddr_t sa_buf;
457         const sockaddr_t *sa;
458         int sock;
459
460         if(to->status.broadcast) {
461                 choose_broadcast_address(mesh, to, &sa, &sock);
462         } else {
463                 choose_udp_address(mesh, to, &sa, &sock, &sa_buf);
464         }
465
466         if(sendto(mesh->listen_socket[sock].udp.fd, data, len, 0, &sa->sa, SALEN(sa->sa)) < 0 && !sockwouldblock(sockerrno)) {
467                 if(sockmsgsize(sockerrno)) {
468                         if(to->maxmtu >= len) {
469                                 to->maxmtu = len - 1;
470                         }
471
472                         if(to->mtu >= len) {
473                                 to->mtu = len - 1;
474                         }
475                 } else {
476                         logger(mesh, MESHLINK_WARNING, "Error sending UDP SPTPS packet to %s: %s", to->name, sockstrerror(sockerrno));
477                         return false;
478                 }
479         }
480
481         return true;
482 }
483
484 bool receive_sptps_record(void *handle, uint8_t type, const void *data, uint16_t len) {
485         assert(handle);
486         assert(!data || len);
487
488         node_t *from = handle;
489         meshlink_handle_t *mesh = from->mesh;
490
491         if(type == SPTPS_HANDSHAKE) {
492                 if(!from->status.validkey) {
493                         logger(mesh, MESHLINK_INFO, "SPTPS key exchange with %s successful", from->name);
494                         from->status.validkey = true;
495                         from->status.waitingforkey = false;
496
497                         if(from->utcp) {
498                                 utcp_reset_timers(from->utcp);
499                         }
500                 }
501
502                 return true;
503         }
504
505         if(len > MAXSIZE) {
506                 logger(mesh, MESHLINK_ERROR, "Packet from %s larger than maximum supported size (%d > %d)", from->name, len, MAXSIZE);
507                 return false;
508         }
509
510         vpn_packet_t inpkt;
511
512         if(type == PKT_PROBE) {
513                 inpkt.len = len;
514                 inpkt.probe = true;
515                 memcpy(inpkt.data, data, len);
516                 mtu_probe_h(mesh, from, &inpkt, len);
517                 return true;
518         } else {
519                 inpkt.probe = false;
520         }
521
522         if(type & ~(PKT_COMPRESSED)) {
523                 logger(mesh, MESHLINK_ERROR, "Unexpected SPTPS record type %d len %d from %s", type, len, from->name);
524                 return false;
525         }
526
527         if(type & PKT_COMPRESSED) {
528                 logger(mesh, MESHLINK_ERROR, "Error while decompressing packet from %s", from->name);
529                 return false;
530         }
531
532         memcpy(inpkt.data, data, len); // TODO: get rid of memcpy
533         inpkt.len = len;
534
535         receive_packet(mesh, from, &inpkt);
536         return true;
537 }
538
539 /*
540   send a packet to the given vpn ip.
541 */
542 void send_packet(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *packet) {
543         if(n == mesh->self) {
544                 // TODO: send to application
545                 return;
546         }
547
548         logger(mesh, MESHLINK_DEBUG, "Sending packet of %d bytes to %s", packet->len, n->name);
549
550         if(!n->status.reachable) {
551                 logger(mesh, MESHLINK_WARNING, "Node %s is not reachable", n->name);
552                 return;
553         }
554
555         n->status.want_udp = true;
556
557         send_sptps_packet(mesh, n, packet);
558         return;
559 }
560
561 static node_t *try_harder(meshlink_handle_t *mesh, const sockaddr_t *from, const vpn_packet_t *pkt) {
562         node_t *n = NULL;
563         bool hard = false;
564
565         for splay_each(edge_t, e, mesh->edges) {
566                 if(!e->to->status.reachable || e->to == mesh->self) {
567                         continue;
568                 }
569
570                 if(sockaddrcmp_noport(from, &e->address)) {
571                         if(mesh->last_hard_try == mesh->loop.now.tv_sec) {
572                                 continue;
573                         }
574
575                         hard = true;
576                 }
577
578                 if(!try_mac(mesh, e->to, pkt)) {
579                         continue;
580                 }
581
582                 n = e->to;
583                 break;
584         }
585
586         if(hard) {
587                 mesh->last_hard_try = mesh->loop.now.tv_sec;
588         }
589
590         return n;
591 }
592
593 void handle_incoming_vpn_data(event_loop_t *loop, void *data, int flags) {
594         (void)flags;
595         meshlink_handle_t *mesh = loop->data;
596         listen_socket_t *ls = data;
597         vpn_packet_t pkt;
598         char *hostname;
599         sockaddr_t from;
600         socklen_t fromlen = sizeof(from);
601         node_t *n;
602         int len;
603
604         memset(&from, 0, sizeof(from));
605
606         len = recvfrom(ls->udp.fd, pkt.data, MAXSIZE, 0, &from.sa, &fromlen);
607
608         if(len <= 0 || len > MAXSIZE) {
609                 if(!sockwouldblock(sockerrno)) {
610                         logger(mesh, MESHLINK_ERROR, "Receiving packet failed: %s", sockstrerror(sockerrno));
611                 }
612
613                 return;
614         }
615
616         pkt.len = len;
617
618         sockaddrunmap(&from); /* Some braindead IPv6 implementations do stupid things. */
619
620         n = lookup_node_udp(mesh, &from);
621
622         if(!n) {
623                 n = try_harder(mesh, &from, &pkt);
624
625                 if(n) {
626                         update_node_udp(mesh, n, &from);
627                 } else if(mesh->log_level <= MESHLINK_WARNING) {
628                         hostname = sockaddr2hostname(&from);
629                         logger(mesh, MESHLINK_WARNING, "Received UDP packet from unknown source %s", hostname);
630                         free(hostname);
631                         return;
632                 } else {
633                         return;
634                 }
635         }
636
637         if(n->status.blacklisted) {
638                 logger(mesh, MESHLINK_WARNING, "Dropping packet from blacklisted node %s", n->name);
639                 return;
640         }
641
642         n->sock = ls - mesh->listen_socket;
643
644         receive_udppacket(mesh, n, &pkt);
645 }