Improved handling of queue-jumping packets on receive
[tinc] / src / net_packet.c
1 /*
2     net_packet.c -- Handles in- and outgoing VPN packets
3     Copyright (C) 1998-2005 Ivo Timmermans,
4                   2000-2010 Guus Sliepen <guus@tinc-vpn.org>
5                   2010      Timothy Redaelli <timothy@redaelli.eu>
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 <openssl/rand.h>
25 #include <openssl/err.h>
26 #include <openssl/evp.h>
27 #include <openssl/pem.h>
28 #include <openssl/hmac.h>
29
30 #ifdef HAVE_ZLIB
31 #include <zlib.h>
32 #endif
33
34 #ifdef HAVE_LZO
35 #include LZO1X_H
36 #endif
37
38 #include "avl_tree.h"
39 #include "conf.h"
40 #include "connection.h"
41 #include "device.h"
42 #include "ethernet.h"
43 #include "event.h"
44 #include "graph.h"
45 #include "list.h"
46 #include "logger.h"
47 #include "net.h"
48 #include "netutl.h"
49 #include "protocol.h"
50 #include "process.h"
51 #include "route.h"
52 #include "utils.h"
53 #include "xalloc.h"
54
55 int keylifetime = 0;
56 int keyexpires = 0;
57 #ifdef HAVE_LZO
58 static char lzo_wrkmem[LZO1X_999_MEM_COMPRESS > LZO1X_1_MEM_COMPRESS ? LZO1X_999_MEM_COMPRESS : LZO1X_1_MEM_COMPRESS];
59 #endif
60
61 static void send_udppacket(node_t *, vpn_packet_t *);
62
63 unsigned replaywin = 16;
64
65 #define MAX_SEQNO 1073741824
66
67 // mtuprobes == 1..30: initial discovery, send bursts with 1 second interval
68 // mtuprobes ==    31: sleep pinginterval seconds
69 // mtuprobes ==    32: send 1 burst, sleep pingtimeout second
70 // mtuprobes ==    33: no response from other side, restart PMTU discovery process
71
72 void send_mtu_probe(node_t *n) {
73         vpn_packet_t packet;
74         int len, i;
75         int timeout = 1;
76         
77         n->mtuprobes++;
78         n->mtuevent = NULL;
79
80         if(!n->status.reachable || !n->status.validkey) {
81                 ifdebug(TRAFFIC) logger(LOG_INFO, "Trying to send MTU probe to unreachable or rekeying node %s (%s)", n->name, n->hostname);
82                 n->mtuprobes = 0;
83                 return;
84         }
85
86         if(n->mtuprobes > 32) {
87                 ifdebug(TRAFFIC) logger(LOG_INFO, "%s (%s) did not respond to UDP ping, restarting PMTU discovery", n->name, n->hostname);
88                 n->mtuprobes = 1;
89                 n->minmtu = 0;
90                 n->maxmtu = MTU;
91         }
92
93         if(n->mtuprobes >= 10 && !n->minmtu) {
94                 ifdebug(TRAFFIC) logger(LOG_INFO, "No response to MTU probes from %s (%s)", n->name, n->hostname);
95                 n->mtuprobes = 0;
96                 return;
97         }
98
99         if(n->mtuprobes == 30 || (n->mtuprobes < 30 && n->minmtu >= n->maxmtu)) {
100                 if(n->minmtu > n->maxmtu)
101                         n->minmtu = n->maxmtu;
102                 else
103                         n->maxmtu = n->minmtu;
104                 n->mtu = n->minmtu;
105                 ifdebug(TRAFFIC) logger(LOG_INFO, "Fixing MTU of %s (%s) to %d after %d probes", n->name, n->hostname, n->mtu, n->mtuprobes);
106                 n->mtuprobes = 31;
107         }
108
109         if(n->mtuprobes == 31) {
110                 timeout = pinginterval;
111                 goto end;
112         } else if(n->mtuprobes == 32) {
113                 timeout = pingtimeout;
114         }
115
116         for(i = 0; i < 3; i++) {
117                 if(n->maxmtu <= n->minmtu)
118                         len = n->maxmtu;
119                 else
120                         len = n->minmtu + 1 + rand() % (n->maxmtu - n->minmtu);
121
122                 if(len < 64)
123                         len = 64;
124                 
125                 memset(packet.data, 0, 14);
126                 RAND_pseudo_bytes(packet.data + 14, len - 14);
127                 packet.len = len;
128                 packet.priority = 0;
129
130                 ifdebug(TRAFFIC) logger(LOG_INFO, "Sending MTU probe length %d to %s (%s)", len, n->name, n->hostname);
131
132                 send_udppacket(n, &packet);
133         }
134
135 end:
136         n->mtuevent = new_event();
137         n->mtuevent->handler = (event_handler_t)send_mtu_probe;
138         n->mtuevent->data = n;
139         n->mtuevent->time = now + timeout;
140         event_add(n->mtuevent);
141 }
142
143 void mtu_probe_h(node_t *n, vpn_packet_t *packet, length_t len) {
144         ifdebug(TRAFFIC) logger(LOG_INFO, "Got MTU probe length %d from %s (%s)", packet->len, n->name, n->hostname);
145
146         if(!packet->data[0]) {
147                 packet->data[0] = 1;
148                 send_udppacket(n, packet);
149         } else {
150                 if(len > n->maxmtu)
151                         len = n->maxmtu;
152                 if(n->minmtu < len)
153                         n->minmtu = len;
154                 if(n->mtuprobes > 30)
155                         n->mtuprobes = 30;
156         }
157 }
158
159 static length_t compress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
160         if(level == 0) {
161                 memcpy(dest, source, len);
162                 return len;
163         } else if(level == 10) {
164 #ifdef HAVE_LZO
165                 lzo_uint lzolen = MAXSIZE;
166                 lzo1x_1_compress(source, len, dest, &lzolen, lzo_wrkmem);
167                 return lzolen;
168 #else
169                 return -1;
170 #endif
171         } else if(level < 10) {
172 #ifdef HAVE_ZLIB
173                 unsigned long destlen = MAXSIZE;
174                 if(compress2(dest, &destlen, source, len, level) == Z_OK)
175                         return destlen;
176                 else
177 #endif
178                         return -1;
179         } else {
180 #ifdef HAVE_LZO
181                 lzo_uint lzolen = MAXSIZE;
182                 lzo1x_999_compress(source, len, dest, &lzolen, lzo_wrkmem);
183                 return lzolen;
184 #else
185                 return -1;
186 #endif
187         }
188         
189         return -1;
190 }
191
192 static length_t uncompress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
193         if(level == 0) {
194                 memcpy(dest, source, len);
195                 return len;
196         } else if(level > 9) {
197 #ifdef HAVE_LZO
198                 lzo_uint lzolen = MAXSIZE;
199                 if(lzo1x_decompress_safe(source, len, dest, &lzolen, NULL) == LZO_E_OK)
200                         return lzolen;
201                 else
202 #endif
203                         return -1;
204         }
205 #ifdef HAVE_ZLIB
206         else {
207                 unsigned long destlen = MAXSIZE;
208                 if(uncompress(dest, &destlen, source, len) == Z_OK)
209                         return destlen;
210                 else
211                         return -1;
212         }
213 #endif
214
215         return -1;
216 }
217
218 /* VPN packet I/O */
219
220 static void receive_packet(node_t *n, vpn_packet_t *packet) {
221         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Received packet of %d bytes from %s (%s)",
222                            packet->len, n->name, n->hostname);
223
224         route(n, packet);
225 }
226
227 static bool try_mac(const node_t *n, const vpn_packet_t *inpkt) {
228         unsigned char hmac[EVP_MAX_MD_SIZE];
229
230         if(!n->indigest || !n->inmaclength || !n->inkey || inpkt->len < sizeof inpkt->seqno + n->inmaclength)
231                 return false;
232
233         HMAC(n->indigest, n->inkey, n->inkeylength, (unsigned char *) &inpkt->seqno, inpkt->len - n->inmaclength, (unsigned char *)hmac, NULL);
234
235         return !memcmp(hmac, (char *) &inpkt->seqno + inpkt->len - n->inmaclength, n->inmaclength);
236 }
237
238 static void receive_udppacket(node_t *n, vpn_packet_t *inpkt) {
239         vpn_packet_t pkt1, pkt2;
240         vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
241         int nextpkt = 0;
242         vpn_packet_t *outpkt = pkt[0];
243         int outlen, outpad;
244         unsigned char hmac[EVP_MAX_MD_SIZE];
245         int i;
246
247         if(!n->inkey) {
248                 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Got packet from %s (%s) but he hasn't got our key yet",
249                                         n->name, n->hostname);
250                 return;
251         }
252
253         /* Check packet length */
254
255         if(inpkt->len < sizeof(inpkt->seqno) + n->inmaclength) {
256                 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Got too short packet from %s (%s)",
257                                         n->name, n->hostname);
258                 return;
259         }
260
261         /* Check the message authentication code */
262
263         if(n->indigest && n->inmaclength) {
264                 inpkt->len -= n->inmaclength;
265                 HMAC(n->indigest, n->inkey, n->inkeylength,
266                          (unsigned char *) &inpkt->seqno, inpkt->len, (unsigned char *)hmac, NULL);
267
268                 if(memcmp(hmac, (char *) &inpkt->seqno + inpkt->len, n->inmaclength)) {
269                         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Got unauthenticated packet from %s (%s)",
270                                            n->name, n->hostname);
271                         return;
272                 }
273         }
274
275         /* Decrypt the packet */
276
277         if(n->incipher) {
278                 outpkt = pkt[nextpkt++];
279
280                 if(!EVP_DecryptInit_ex(&n->inctx, NULL, NULL, NULL, NULL)
281                                 || !EVP_DecryptUpdate(&n->inctx, (unsigned char *) &outpkt->seqno, &outlen,
282                                         (unsigned char *) &inpkt->seqno, inpkt->len)
283                                 || !EVP_DecryptFinal_ex(&n->inctx, (unsigned char *) &outpkt->seqno + outlen, &outpad)) {
284                         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Error decrypting packet from %s (%s): %s",
285                                                 n->name, n->hostname, ERR_error_string(ERR_get_error(), NULL));
286                         return;
287                 }
288                 
289                 outpkt->len = outlen + outpad;
290                 inpkt = outpkt;
291         }
292
293         /* Check the sequence number */
294
295         inpkt->len -= sizeof(inpkt->seqno);
296         inpkt->seqno = ntohl(inpkt->seqno);
297
298         if(replaywin) {
299                 if(inpkt->seqno != n->received_seqno + 1) {
300                         if(inpkt->seqno >= n->received_seqno + replaywin * 8) {
301                                 if(n->farfuture++ < replaywin >> 2) {
302                                         logger(LOG_WARNING, "Packet from %s (%s) is %d seqs in the future, dropped (%u)",
303                                                 n->name, n->hostname, inpkt->seqno - n->received_seqno - 1, n->farfuture);
304                                         return;
305                                 }
306                                 logger(LOG_WARNING, "Lost %d packets from %s (%s)",
307                                                 inpkt->seqno - n->received_seqno - 1, n->name, n->hostname);
308                                 memset(n->late, 0, replaywin);
309                         } else if (inpkt->seqno <= n->received_seqno) {
310                                 if((n->received_seqno >= replaywin * 8 && inpkt->seqno <= n->received_seqno - replaywin * 8) || !(n->late[(inpkt->seqno / 8) % replaywin] & (1 << inpkt->seqno % 8))) {
311                                         logger(LOG_WARNING, "Got late or replayed packet from %s (%s), seqno %d, last received %d",
312                                                 n->name, n->hostname, inpkt->seqno, n->received_seqno);
313                                         return;
314                                 }
315                         } else {
316                                 for(i = n->received_seqno + 1; i < inpkt->seqno; i++)
317                                         n->late[(i / 8) % replaywin] |= 1 << i % 8;
318                         }
319                 }
320
321                 n->farfuture = 0;
322                 n->late[(inpkt->seqno / 8) % replaywin] &= ~(1 << inpkt->seqno % 8);
323         }
324
325         if(inpkt->seqno > n->received_seqno)
326                 n->received_seqno = inpkt->seqno;
327                         
328         if(n->received_seqno > MAX_SEQNO)
329                 keyexpires = 0;
330
331         /* Decompress the packet */
332
333         length_t origlen = inpkt->len;
334
335         if(n->incompression) {
336                 outpkt = pkt[nextpkt++];
337
338                 if((outpkt->len = uncompress_packet(outpkt->data, inpkt->data, inpkt->len, n->incompression)) < 0) {
339                         ifdebug(TRAFFIC) logger(LOG_ERR, "Error while uncompressing packet from %s (%s)",
340                                                  n->name, n->hostname);
341                         return;
342                 }
343
344                 inpkt = outpkt;
345
346                 origlen -= MTU/64 + 20;
347         }
348
349         inpkt->priority = 0;
350
351         if(!inpkt->data[12] && !inpkt->data[13])
352                 mtu_probe_h(n, inpkt, origlen);
353         else
354                 receive_packet(n, inpkt);
355 }
356
357 void receive_tcppacket(connection_t *c, char *buffer, int len) {
358         vpn_packet_t outpkt;
359
360         outpkt.len = len;
361         if(c->options & OPTION_TCPONLY)
362                 outpkt.priority = 0;
363         else
364                 outpkt.priority = -1;
365         memcpy(outpkt.data, buffer, len);
366
367         receive_packet(c->node, &outpkt);
368 }
369
370 static void send_udppacket(node_t *n, vpn_packet_t *origpkt) {
371         vpn_packet_t pkt1, pkt2;
372         vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
373         vpn_packet_t *inpkt = origpkt;
374         int nextpkt = 0;
375         vpn_packet_t *outpkt;
376         int origlen;
377         int outlen, outpad;
378 #if defined(SOL_IP) && defined(IP_TOS)
379         static int priority = 0;
380 #endif
381         int origpriority;
382         int sock;
383
384         if(!n->status.reachable) {
385                 ifdebug(TRAFFIC) logger(LOG_INFO, "Trying to send UDP packet to unreachable node %s (%s)", n->name, n->hostname);
386                 return;
387         }
388
389         /* Make sure we have a valid key */
390
391         if(!n->status.validkey) {
392                 ifdebug(TRAFFIC) logger(LOG_INFO,
393                                    "No valid key known yet for %s (%s), forwarding via TCP",
394                                    n->name, n->hostname);
395
396                 if(n->last_req_key + 10 < now) {
397                         send_req_key(n);
398                         n->last_req_key = now;
399                 }
400
401                 send_tcppacket(n->nexthop->connection, origpkt);
402
403                 return;
404         }
405
406         if(n->options & OPTION_PMTU_DISCOVERY && inpkt->len > n->minmtu && (inpkt->data[12] | inpkt->data[13])) {
407                 ifdebug(TRAFFIC) logger(LOG_INFO,
408                                 "Packet for %s (%s) larger than minimum MTU, forwarding via %s",
409                                 n->name, n->hostname, n != n->nexthop ? n->nexthop->name : "TCP");
410
411                 if(n != n->nexthop)
412                         send_packet(n->nexthop, origpkt);
413                 else
414                         send_tcppacket(n->nexthop->connection, origpkt);
415
416                 return;
417         }
418
419         origlen = inpkt->len;
420         origpriority = inpkt->priority;
421
422         /* Compress the packet */
423
424         if(n->outcompression) {
425                 outpkt = pkt[nextpkt++];
426
427                 if((outpkt->len = compress_packet(outpkt->data, inpkt->data, inpkt->len, n->outcompression)) < 0) {
428                         ifdebug(TRAFFIC) logger(LOG_ERR, "Error while compressing packet to %s (%s)",
429                                    n->name, n->hostname);
430                         return;
431                 }
432
433                 inpkt = outpkt;
434         }
435
436         /* Add sequence number */
437
438         inpkt->seqno = htonl(++(n->sent_seqno));
439         inpkt->len += sizeof(inpkt->seqno);
440
441         /* Encrypt the packet */
442
443         if(n->outcipher) {
444                 outpkt = pkt[nextpkt++];
445
446                 if(!EVP_EncryptInit_ex(&n->outctx, NULL, NULL, NULL, NULL)
447                                 || !EVP_EncryptUpdate(&n->outctx, (unsigned char *) &outpkt->seqno, &outlen,
448                                         (unsigned char *) &inpkt->seqno, inpkt->len)
449                                 || !EVP_EncryptFinal_ex(&n->outctx, (unsigned char *) &outpkt->seqno + outlen, &outpad)) {
450                         ifdebug(TRAFFIC) logger(LOG_ERR, "Error while encrypting packet to %s (%s): %s",
451                                                 n->name, n->hostname, ERR_error_string(ERR_get_error(), NULL));
452                         goto end;
453                 }
454
455                 outpkt->len = outlen + outpad;
456                 inpkt = outpkt;
457         }
458
459         /* Add the message authentication code */
460
461         if(n->outdigest && n->outmaclength) {
462                 HMAC(n->outdigest, n->outkey, n->outkeylength, (unsigned char *) &inpkt->seqno,
463                          inpkt->len, (unsigned char *) &inpkt->seqno + inpkt->len, NULL);
464                 inpkt->len += n->outmaclength;
465         }
466
467         /* Determine which socket we have to use */
468
469         for(sock = 0; sock < listen_sockets; sock++)
470                 if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family)
471                         break;
472
473         if(sock >= listen_sockets)
474                 sock = 0;                               /* If none is available, just use the first and hope for the best. */
475
476         /* Send the packet */
477
478 #if defined(SOL_IP) && defined(IP_TOS)
479         if(priorityinheritance && origpriority != priority
480            && listen_socket[sock].sa.sa.sa_family == AF_INET) {
481                 priority = origpriority;
482                 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Setting outgoing packet priority to %d", priority);
483                 if(setsockopt(listen_socket[sock].udp, SOL_IP, IP_TOS, &priority, sizeof(priority)))    /* SO_PRIORITY doesn't seem to work */
484                         logger(LOG_ERR, "System call `%s' failed: %s", "setsockopt", strerror(errno));
485         }
486 #endif
487
488         if(sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa)) < 0 && !sockwouldblock(sockerrno)) {
489                 if(sockmsgsize(sockerrno)) {
490                         if(n->maxmtu >= origlen)
491                                 n->maxmtu = origlen - 1;
492                         if(n->mtu >= origlen)
493                                 n->mtu = origlen - 1;
494                 } else
495                         logger(LOG_ERR, "Error sending packet to %s (%s): %s", n->name, n->hostname, sockstrerror(sockerrno));
496         }
497
498 end:
499         origpkt->len = origlen;
500 }
501
502 /*
503   send a packet to the given vpn ip.
504 */
505 void send_packet(const node_t *n, vpn_packet_t *packet) {
506         node_t *via;
507
508         if(n == myself) {
509                 if(overwrite_mac)
510                          memcpy(packet->data, mymac.x, ETH_ALEN);
511                 write_packet(packet);
512                 return;
513         }
514
515         ifdebug(TRAFFIC) logger(LOG_ERR, "Sending packet of %d bytes to %s (%s)",
516                            packet->len, n->name, n->hostname);
517
518         if(!n->status.reachable) {
519                 ifdebug(TRAFFIC) logger(LOG_INFO, "Node %s (%s) is not reachable",
520                                    n->name, n->hostname);
521                 return;
522         }
523
524         via = (packet->priority == -1 || n->via == myself) ? n->nexthop : n->via;
525
526         if(via != n)
527                 ifdebug(TRAFFIC) logger(LOG_INFO, "Sending packet to %s via %s (%s)",
528                            n->name, via->name, n->via->hostname);
529
530         if(packet->priority == -1 || ((myself->options | via->options) & OPTION_TCPONLY)) {
531                 if(!send_tcppacket(via->connection, packet))
532                         terminate_connection(via->connection, true);
533         } else
534                 send_udppacket(via, packet);
535 }
536
537 /* Broadcast a packet using the minimum spanning tree */
538
539 void broadcast_packet(const node_t *from, vpn_packet_t *packet) {
540         avl_node_t *node;
541         connection_t *c;
542
543         ifdebug(TRAFFIC) logger(LOG_INFO, "Broadcasting packet of %d bytes from %s (%s)",
544                            packet->len, from->name, from->hostname);
545
546         if(from != myself) {
547                 send_packet(myself, packet);
548
549                 // In TunnelServer mode, do not forward broadcast packets.
550                 // The MST might not be valid and create loops.
551                 if(tunnelserver)
552                         return;
553         }
554
555         for(node = connection_tree->head; node; node = node->next) {
556                 c = node->data;
557
558                 if(c->status.active && c->status.mst && c != from->nexthop->connection)
559                         send_packet(c->node, packet);
560         }
561 }
562
563 static node_t *try_harder(const sockaddr_t *from, const vpn_packet_t *pkt) {
564         avl_node_t *node;
565         edge_t *e;
566         node_t *n = NULL;
567         static time_t last_hard_try = 0;
568
569         for(node = edge_weight_tree->head; node; node = node->next) {
570                 e = node->data;
571
572                 if(sockaddrcmp_noport(from, &e->address)) {
573                         if(last_hard_try == now)
574                                 continue;
575                         last_hard_try = now;
576                 }
577
578                 if(!n)
579                         n = e->to;
580
581                 if(!try_mac(e->to, pkt))
582                         continue;
583
584                 n = e->to;
585                 break;
586         }
587
588         return n;
589 }
590
591 void handle_incoming_vpn_data(int sock) {
592         vpn_packet_t pkt;
593         char *hostname;
594         sockaddr_t from;
595         socklen_t fromlen = sizeof(from);
596         node_t *n;
597
598         pkt.len = recvfrom(sock, (char *) &pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen);
599
600         if(pkt.len < 0) {
601                 if(!sockwouldblock(sockerrno))
602                         logger(LOG_ERR, "Receiving packet failed: %s", sockstrerror(sockerrno));
603                 return;
604         }
605
606         sockaddrunmap(&from);           /* Some braindead IPv6 implementations do stupid things. */
607
608         n = lookup_node_udp(&from);
609
610         if(!n) {
611                 n = try_harder(&from, &pkt);
612                 if(n)
613                         update_node_udp(n, &from);
614                 else ifdebug(PROTOCOL) {
615                         hostname = sockaddr2hostname(&from);
616                         logger(LOG_WARNING, "Received UDP packet from unknown source %s", hostname);
617                         free(hostname);
618                         return;
619                 }
620                 else
621                         return;
622         }
623
624         receive_udppacket(n, &pkt);
625 }