2 net_packet.c -- Handles in- and outgoing VPN packets
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2016 Guus Sliepen <guus@tinc-vpn.org>
5 2010 Timothy Redaelli <timothy@redaelli.eu>
6 2010 Brandon Black <blblack@gmail.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <openssl/rand.h>
26 #include <openssl/err.h>
27 #include <openssl/evp.h>
28 #include <openssl/pem.h>
29 #include <openssl/hmac.h>
41 #include "connection.h"
58 static char lzo_wrkmem[LZO1X_999_MEM_COMPRESS > LZO1X_1_MEM_COMPRESS ? LZO1X_999_MEM_COMPRESS : LZO1X_1_MEM_COMPRESS];
61 static void send_udppacket(node_t *, vpn_packet_t *);
63 unsigned replaywin = 16;
64 bool localdiscovery = false;
66 #define MAX_SEQNO 1073741824
68 /* mtuprobes == 1..30: initial discovery, send bursts with 1 second interval
69 mtuprobes == 31: sleep pinginterval seconds
70 mtuprobes == 32: send 1 burst, sleep pingtimeout second
71 mtuprobes == 33: no response from other side, restart PMTU discovery process
73 Probes are sent in batches of at least three, with random sizes between the
74 lower and upper boundaries for the MTU thus far discovered.
76 After the initial discovery, a fourth packet is added to each batch with a
77 size larger than the currently known PMTU, to test if the PMTU has increased.
79 In case local discovery is enabled, another packet is added to each batch,
80 which will be broadcast to the local network.
84 void send_mtu_probe(node_t *n) {
92 if(!n->status.reachable || !n->status.validkey) {
93 ifdebug(TRAFFIC) logger(LOG_INFO, "Trying to send MTU probe to unreachable or rekeying node %s (%s)", n->name, n->hostname);
98 if(n->mtuprobes > 32) {
101 timeout = pinginterval;
105 ifdebug(TRAFFIC) logger(LOG_INFO, "%s (%s) did not respond to UDP ping, restarting PMTU discovery", n->name, n->hostname);
111 if(n->mtuprobes >= 10 && n->mtuprobes < 32 && !n->minmtu) {
112 ifdebug(TRAFFIC) logger(LOG_INFO, "No response to MTU probes from %s (%s)", n->name, n->hostname);
116 if(n->mtuprobes == 30 || (n->mtuprobes < 30 && n->minmtu >= n->maxmtu)) {
117 if(n->minmtu > n->maxmtu) {
118 n->minmtu = n->maxmtu;
120 n->maxmtu = n->minmtu;
124 ifdebug(TRAFFIC) logger(LOG_INFO, "Fixing MTU of %s (%s) to %d after %d probes", n->name, n->hostname, n->mtu, n->mtuprobes);
128 if(n->mtuprobes == 31) {
129 timeout = pinginterval;
131 } else if(n->mtuprobes == 32) {
132 timeout = pingtimeout;
135 for(i = 0; i < 4 + localdiscovery; i++) {
137 if(n->mtuprobes < 30 || n->maxmtu + 8 >= MTU) {
142 } else if(n->maxmtu <= n->minmtu) {
145 len = n->minmtu + 1 + rand() % (n->maxmtu - n->minmtu);
152 memset(packet.data, 0, 14);
153 RAND_bytes(packet.data + 14, len - 14);
156 if(i >= 4 && n->mtuprobes <= 10) {
157 packet.priority = -1;
162 ifdebug(TRAFFIC) logger(LOG_INFO, "Sending MTU probe length %d to %s (%s)", len, n->name, n->hostname);
164 send_udppacket(n, &packet);
168 n->mtuevent = new_event();
169 n->mtuevent->handler = (event_handler_t)send_mtu_probe;
170 n->mtuevent->data = n;
171 n->mtuevent->time = now + timeout;
172 event_add(n->mtuevent);
175 void mtu_probe_h(node_t *n, vpn_packet_t *packet, length_t len) {
176 ifdebug(TRAFFIC) logger(LOG_INFO, "Got MTU probe length %d from %s (%s)", packet->len, n->name, n->hostname);
178 if(!packet->data[0]) {
180 send_udppacket(n, packet);
182 if(n->mtuprobes > 30) {
183 if(len == n->maxmtu + 8) {
184 ifdebug(TRAFFIC) logger(LOG_INFO, "Increase in PMTU to %s (%s) detected, restarting PMTU discovery", n->name, n->hostname);
197 if(len > n->maxmtu) {
201 if(n->minmtu < len) {
207 static length_t compress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
209 memcpy(dest, source, len);
211 } else if(level == 10) {
213 lzo_uint lzolen = MAXSIZE;
214 lzo1x_1_compress(source, len, dest, &lzolen, lzo_wrkmem);
219 } else if(level < 10) {
221 unsigned long destlen = MAXSIZE;
223 if(compress2(dest, &destlen, source, len, level) == Z_OK) {
230 lzo_uint lzolen = MAXSIZE;
231 lzo1x_999_compress(source, len, dest, &lzolen, lzo_wrkmem);
241 static length_t uncompress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
243 memcpy(dest, source, len);
245 } else if(level > 9) {
247 lzo_uint lzolen = MAXSIZE;
249 if(lzo1x_decompress_safe(source, len, dest, &lzolen, NULL) == LZO_E_OK) {
258 unsigned long destlen = MAXSIZE;
260 if(uncompress(dest, &destlen, source, len) == Z_OK) {
274 static void receive_packet(node_t *n, vpn_packet_t *packet) {
275 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Received packet of %d bytes from %s (%s)",
276 packet->len, n->name, n->hostname);
281 static bool try_mac(const node_t *n, const vpn_packet_t *inpkt) {
282 unsigned char hmac[EVP_MAX_MD_SIZE];
284 if(!n->indigest || !n->inmaclength || !n->inkey || inpkt->len < sizeof(inpkt->seqno) + n->inmaclength) {
288 HMAC(n->indigest, n->inkey, n->inkeylength, (unsigned char *) &inpkt->seqno, inpkt->len - n->inmaclength, (unsigned char *)hmac, NULL);
290 return !memcmp_constant_time(hmac, (char *) &inpkt->seqno + inpkt->len - n->inmaclength, n->inmaclength);
293 static void receive_udppacket(node_t *n, vpn_packet_t *inpkt) {
294 vpn_packet_t pkt1, pkt2;
295 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
297 vpn_packet_t *outpkt;
299 unsigned char hmac[EVP_MAX_MD_SIZE];
303 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Got packet from %s (%s) but he hasn't got our key yet",
304 n->name, n->hostname);
308 /* Check packet length */
310 if(inpkt->len < sizeof(inpkt->seqno) + n->inmaclength) {
311 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Got too short packet from %s (%s)",
312 n->name, n->hostname);
316 /* Check the message authentication code */
318 if(n->indigest && n->inmaclength) {
319 inpkt->len -= n->inmaclength;
320 HMAC(n->indigest, n->inkey, n->inkeylength,
321 (unsigned char *) &inpkt->seqno, inpkt->len, (unsigned char *)hmac, NULL);
323 if(memcmp_constant_time(hmac, (char *) &inpkt->seqno + inpkt->len, n->inmaclength)) {
324 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Got unauthenticated packet from %s (%s)",
325 n->name, n->hostname);
330 /* Decrypt the packet */
333 outpkt = pkt[nextpkt++];
335 if(!EVP_DecryptInit_ex(n->inctx, NULL, NULL, NULL, NULL)
336 || !EVP_DecryptUpdate(n->inctx, (unsigned char *) &outpkt->seqno, &outlen,
337 (unsigned char *) &inpkt->seqno, inpkt->len)
338 || !EVP_DecryptFinal_ex(n->inctx, (unsigned char *) &outpkt->seqno + outlen, &outpad)) {
339 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Error decrypting packet from %s (%s): %s",
340 n->name, n->hostname, ERR_error_string(ERR_get_error(), NULL));
344 outpkt->len = outlen + outpad;
348 /* Check the sequence number */
350 inpkt->len -= sizeof(inpkt->seqno);
351 inpkt->seqno = ntohl(inpkt->seqno);
354 if(inpkt->seqno != n->received_seqno + 1) {
355 if(inpkt->seqno >= n->received_seqno + replaywin * 8) {
356 if(n->farfuture++ < replaywin >> 2) {
357 ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet from %s (%s) is %d seqs in the future, dropped (%u)",
358 n->name, n->hostname, inpkt->seqno - n->received_seqno - 1, n->farfuture);
362 ifdebug(TRAFFIC) logger(LOG_WARNING, "Lost %d packets from %s (%s)",
363 inpkt->seqno - n->received_seqno - 1, n->name, n->hostname);
364 memset(n->late, 0, replaywin);
365 } else if(inpkt->seqno <= n->received_seqno) {
366 if((n->received_seqno >= replaywin * 8 && inpkt->seqno <= n->received_seqno - replaywin * 8) || !(n->late[(inpkt->seqno / 8) % replaywin] & (1 << inpkt->seqno % 8))) {
367 ifdebug(TRAFFIC) logger(LOG_WARNING, "Got late or replayed packet from %s (%s), seqno %d, last received %d",
368 n->name, n->hostname, inpkt->seqno, n->received_seqno);
372 for(i = n->received_seqno + 1; i < inpkt->seqno; i++) {
373 n->late[(i / 8) % replaywin] |= 1 << i % 8;
379 n->late[(inpkt->seqno / 8) % replaywin] &= ~(1 << inpkt->seqno % 8);
382 if(inpkt->seqno > n->received_seqno) {
383 n->received_seqno = inpkt->seqno;
386 if(n->received_seqno > MAX_SEQNO) {
390 /* Decompress the packet */
392 length_t origlen = inpkt->len;
394 if(n->incompression) {
395 outpkt = pkt[nextpkt++];
397 if((outpkt->len = uncompress_packet(outpkt->data, inpkt->data, inpkt->len, n->incompression)) < 0) {
398 ifdebug(TRAFFIC) logger(LOG_ERR, "Error while uncompressing packet from %s (%s)",
399 n->name, n->hostname);
405 origlen -= MTU / 64 + 20;
410 if(!inpkt->data[12] && !inpkt->data[13]) {
411 mtu_probe_h(n, inpkt, origlen);
413 receive_packet(n, inpkt);
417 void receive_tcppacket(connection_t *c, const char *buffer, int len) {
420 if(len > sizeof(outpkt.data)) {
426 if(c->options & OPTION_TCPONLY) {
429 outpkt.priority = -1;
432 memcpy(outpkt.data, buffer, len);
434 receive_packet(c->node, &outpkt);
437 static void send_udppacket(node_t *n, vpn_packet_t *origpkt) {
438 vpn_packet_t pkt1, pkt2;
439 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
440 vpn_packet_t *inpkt = origpkt;
442 vpn_packet_t *outpkt;
447 if(!n->status.reachable) {
448 ifdebug(TRAFFIC) logger(LOG_INFO, "Trying to send UDP packet to unreachable node %s (%s)", n->name, n->hostname);
452 /* Make sure we have a valid key */
454 if(!n->status.validkey) {
455 ifdebug(TRAFFIC) logger(LOG_INFO,
456 "No valid key known yet for %s (%s), forwarding via TCP",
457 n->name, n->hostname);
459 if(n->last_req_key + 10 <= now) {
461 n->last_req_key = now;
464 send_tcppacket(n->nexthop->connection, origpkt);
469 if(n->options & OPTION_PMTU_DISCOVERY && inpkt->len > n->minmtu && (inpkt->data[12] | inpkt->data[13])) {
470 ifdebug(TRAFFIC) logger(LOG_INFO,
471 "Packet for %s (%s) larger than minimum MTU, forwarding via %s",
472 n->name, n->hostname, n != n->nexthop ? n->nexthop->name : "TCP");
474 if(n != n->nexthop) {
475 send_packet(n->nexthop, origpkt);
477 send_tcppacket(n->nexthop->connection, origpkt);
483 origlen = inpkt->len;
484 origpriority = inpkt->priority;
486 /* Compress the packet */
488 if(n->outcompression) {
489 outpkt = pkt[nextpkt++];
491 if((outpkt->len = compress_packet(outpkt->data, inpkt->data, inpkt->len, n->outcompression)) < 0) {
492 ifdebug(TRAFFIC) logger(LOG_ERR, "Error while compressing packet to %s (%s)",
493 n->name, n->hostname);
500 /* Add sequence number */
502 inpkt->seqno = htonl(++(n->sent_seqno));
503 inpkt->len += sizeof(inpkt->seqno);
505 /* Encrypt the packet */
508 outpkt = pkt[nextpkt++];
510 if(!EVP_EncryptInit_ex(n->outctx, NULL, NULL, NULL, NULL)
511 || !EVP_EncryptUpdate(n->outctx, (unsigned char *) &outpkt->seqno, &outlen,
512 (unsigned char *) &inpkt->seqno, inpkt->len)
513 || !EVP_EncryptFinal_ex(n->outctx, (unsigned char *) &outpkt->seqno + outlen, &outpad)) {
514 ifdebug(TRAFFIC) logger(LOG_ERR, "Error while encrypting packet to %s (%s): %s",
515 n->name, n->hostname, ERR_error_string(ERR_get_error(), NULL));
519 outpkt->len = outlen + outpad;
523 /* Add the message authentication code */
525 if(n->outdigest && n->outmaclength) {
526 HMAC(n->outdigest, n->outkey, n->outkeylength, (unsigned char *) &inpkt->seqno,
527 inpkt->len, (unsigned char *) &inpkt->seqno + inpkt->len, NULL);
528 inpkt->len += n->outmaclength;
531 /* Determine which socket we have to use */
533 if(n->address.sa.sa_family != listen_socket[n->sock].sa.sa.sa_family) {
534 for(int sock = 0; sock < listen_sockets; sock++) {
535 if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family) {
542 /* Send the packet */
547 sockaddr_t broadcast;
549 /* Overloaded use of priority field: -1 means local broadcast */
551 if(origpriority == -1 && n->prevedge) {
552 sock = rand() % listen_sockets;
553 memset(&broadcast, 0, sizeof(broadcast));
555 if(listen_socket[sock].sa.sa.sa_family == AF_INET6) {
556 broadcast.in6.sin6_family = AF_INET6;
557 broadcast.in6.sin6_addr.s6_addr[0x0] = 0xff;
558 broadcast.in6.sin6_addr.s6_addr[0x1] = 0x02;
559 broadcast.in6.sin6_addr.s6_addr[0xf] = 0x01;
560 broadcast.in6.sin6_port = n->prevedge->address.in.sin_port;
561 broadcast.in6.sin6_scope_id = listen_socket[sock].sa.in6.sin6_scope_id;
563 broadcast.in.sin_family = AF_INET;
564 broadcast.in.sin_addr.s_addr = -1;
565 broadcast.in.sin_port = n->prevedge->address.in.sin_port;
569 sl = SALEN(broadcast.sa);
571 if(origpriority == -1) {
575 sa = &(n->address.sa);
576 sl = SALEN(n->address.sa);
580 if(priorityinheritance && origpriority != listen_socket[n->sock].priority) {
581 listen_socket[n->sock].priority = origpriority;
583 switch(listen_socket[n->sock].sa.sa.sa_family) {
584 #if defined(SOL_IP) && defined(IP_TOS)
587 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Setting IPv4 outgoing packet priority to %d", origpriority);
589 if(setsockopt(listen_socket[n->sock].udp, SOL_IP, IP_TOS, (void *)&origpriority, sizeof(origpriority))) { /* SO_PRIORITY doesn't seem to work */
590 logger(LOG_ERR, "System call `%s' failed: %s", "setsockopt", strerror(errno));
595 #if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS)
598 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Setting IPv6 outgoing packet priority to %d", origpriority);
600 if(setsockopt(listen_socket[n->sock].udp, IPPROTO_IPV6, IPV6_TCLASS, (void *)&origpriority, sizeof(origpriority))) {
601 logger(LOG_ERR, "System call `%s' failed: %s", "setsockopt", strerror(errno));
612 if(sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, sa, sl) < 0 && !sockwouldblock(sockerrno)) {
613 if(sockmsgsize(sockerrno)) {
614 if(n->maxmtu >= origlen) {
615 n->maxmtu = origlen - 1;
618 if(n->mtu >= origlen) {
619 n->mtu = origlen - 1;
622 ifdebug(TRAFFIC) logger(LOG_WARNING, "Error sending packet to %s (%s): %s", n->name, n->hostname, sockstrerror(sockerrno));
627 origpkt->len = origlen;
631 send a packet to the given vpn ip.
633 void send_packet(const node_t *n, vpn_packet_t *packet) {
638 memcpy(packet->data, mymac.x, ETH_ALEN);
641 devops.write(packet);
645 ifdebug(TRAFFIC) logger(LOG_ERR, "Sending packet of %d bytes to %s (%s)",
646 packet->len, n->name, n->hostname);
648 if(!n->status.reachable) {
649 ifdebug(TRAFFIC) logger(LOG_INFO, "Node %s (%s) is not reachable",
650 n->name, n->hostname);
654 via = (packet->priority == -1 || n->via == myself) ? n->nexthop : n->via;
657 ifdebug(TRAFFIC) logger(LOG_INFO, "Sending packet to %s via %s (%s)",
658 n->name, via->name, n->via->hostname);
660 if(packet->priority == -1 || ((myself->options | via->options) & OPTION_TCPONLY)) {
661 if(!send_tcppacket(via->connection, packet)) {
662 terminate_connection(via->connection, true);
665 send_udppacket(via, packet);
669 /* Broadcast a packet using the minimum spanning tree */
671 void broadcast_packet(const node_t *from, vpn_packet_t *packet) {
676 // Always give ourself a copy of the packet.
678 send_packet(myself, packet);
681 // In TunnelServer mode, do not forward broadcast packets.
682 // The MST might not be valid and create loops.
683 if(tunnelserver || broadcast_mode == BMODE_NONE) {
687 ifdebug(TRAFFIC) logger(LOG_INFO, "Broadcasting packet of %d bytes from %s (%s)",
688 packet->len, from->name, from->hostname);
690 switch(broadcast_mode) {
691 // In MST mode, broadcast packets travel via the Minimum Spanning Tree.
692 // This guarantees all nodes receive the broadcast packet, and
693 // usually distributes the sending of broadcast packets over all nodes.
695 for(node = connection_tree->head; node; node = node->next) {
698 if(c->status.active && c->status.mst && c != from->nexthop->connection) {
699 send_packet(c->node, packet);
705 // In direct mode, we send copies to each node we know of.
706 // However, this only reaches nodes that can be reached in a single hop.
707 // We don't have enough information to forward broadcast packets in this case.
713 for(node = node_udp_tree->head; node; node = node->next) {
716 if(n->status.reachable && n != myself && ((n->via == myself && n->nexthop == n) || n->via == n)) {
717 send_packet(n, packet);
728 static node_t *try_harder(const sockaddr_t *from, const vpn_packet_t *pkt) {
732 static time_t last_hard_try = 0;
734 for(node = edge_weight_tree->head; node; node = node->next) {
737 if(e->to == myself) {
741 if(last_hard_try == now && sockaddrcmp_noport(from, &e->address)) {
745 if(!try_mac(e->to, pkt)) {
757 void handle_incoming_vpn_data(int sock) {
761 socklen_t fromlen = sizeof(from);
764 pkt.len = recvfrom(listen_socket[sock].udp, (char *) &pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen);
767 if(!sockwouldblock(sockerrno)) {
768 logger(LOG_ERR, "Receiving packet failed: %s", sockstrerror(sockerrno));
774 sockaddrunmap(&from); /* Some braindead IPv6 implementations do stupid things. */
776 n = lookup_node_udp(&from);
779 n = try_harder(&from, &pkt);
782 update_node_udp(n, &from);
783 } else ifdebug(PROTOCOL) {
784 hostname = sockaddr2hostname(&from);
785 logger(LOG_WARNING, "Received UDP packet from unknown source %s", hostname);
795 receive_udppacket(n, &pkt);