2 net_packet.c -- Handles in- and outgoing VPN packets
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2014 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.
35 #include "connection.h"
50 #define MAX(a, b) ((a) > (b) ? (a) : (b))
55 static char lzo_wrkmem[LZO1X_999_MEM_COMPRESS > LZO1X_1_MEM_COMPRESS ? LZO1X_999_MEM_COMPRESS : LZO1X_1_MEM_COMPRESS];
58 static void send_udppacket(node_t *, vpn_packet_t *);
60 unsigned replaywin = 16;
61 bool localdiscovery = true;
63 #define MAX_SEQNO 1073741824
65 static void send_mtu_probe_packet(node_t *n, int len) {
67 packet.offset = DEFAULT_PACKET_OFFSET;
68 memset(DATA(&packet), 0, 14);
69 randomize(DATA(&packet) + 14, len - 14);
73 logger(DEBUG_TRAFFIC, LOG_INFO, "Sending MTU probe length %d to %s (%s)", len, n->name, n->hostname);
75 send_udppacket(n, &packet);
78 static void send_mtu_probe_handler(void *data) {
81 if(!n->status.reachable || !n->status.validkey) {
82 logger(DEBUG_TRAFFIC, LOG_INFO, "Trying to send MTU probe to unreachable or rekeying node %s (%s)", n->name, n->hostname);
87 /* mtuprobes == 1..30: initial discovery, send bursts with 1 second interval
88 mtuprobes == 31: sleep pinginterval seconds
89 mtuprobes == 32: send 1 burst, sleep pingtimeout second
90 mtuprobes == 33: no response from other side, restart PMTU discovery process */
95 if(n->mtuprobes > 32) {
98 timeout = pinginterval;
102 logger(DEBUG_TRAFFIC, LOG_INFO, "%s (%s) did not respond to UDP ping, restarting PMTU discovery", n->name, n->hostname);
103 n->status.udp_confirmed = false;
109 if(n->mtuprobes >= 10 && n->mtuprobes < 32 && !n->minmtu) {
110 logger(DEBUG_TRAFFIC, LOG_INFO, "No response to MTU probes from %s (%s)", n->name, n->hostname);
114 if(n->mtuprobes == 30 || (n->mtuprobes < 30 && n->minmtu >= n->maxmtu)) {
115 if(n->minmtu > n->maxmtu)
116 n->minmtu = n->maxmtu;
118 n->maxmtu = n->minmtu;
120 logger(DEBUG_TRAFFIC, LOG_INFO, "Fixing MTU of %s (%s) to %d after %d probes", n->name, n->hostname, n->mtu, n->mtuprobes);
124 if(n->mtuprobes == 31) {
125 timeout = pinginterval;
127 } else if(n->mtuprobes == 32) {
128 timeout = pingtimeout;
131 /* After the initial discovery, a fourth packet is added to each batch with a
132 size larger than the currently known PMTU, to test if the PMTU has increased. */
133 if (n->mtuprobes >= 30 && n->maxmtu + 8 < MTU)
134 send_mtu_probe_packet(n, n->maxmtu + 8);
136 /* Probes are sent in batches of three, with random sizes between the
137 lower and upper boundaries for the MTU thus far discovered. */
138 for (int i = 0; i < 3; i++) {
140 if(n->minmtu < n->maxmtu)
141 len = n->minmtu + 1 + rand() % (n->maxmtu - n->minmtu);
143 send_mtu_probe_packet(n, MAX(len, 64));
146 /* In case local discovery is enabled, another packet is added to each batch,
147 which will be broadcast to the local network. */
148 if(localdiscovery && n->mtuprobes <= 10 && n->prevedge) {
149 n->status.send_locally = true;
150 send_mtu_probe_packet(n, 16);
151 n->status.send_locally = false;
154 n->probe_counter = 0;
155 gettimeofday(&n->probe_time, NULL);
157 /* Calculate the packet loss of incoming traffic by comparing the rate of
158 packets received to the rate with which the sequence number has increased.
161 if(n->received > n->prev_received)
162 n->packetloss = 1.0 - (n->received - n->prev_received) / (float)(n->received_seqno - n->prev_received_seqno);
164 n->packetloss = n->received_seqno <= n->prev_received_seqno;
166 n->prev_received_seqno = n->received_seqno;
167 n->prev_received = n->received;
170 timeout_set(&n->mtutimeout, &(struct timeval){timeout, rand() % 100000});
173 void send_mtu_probe(node_t *n) {
174 timeout_add(&n->mtutimeout, send_mtu_probe_handler, n, &(struct timeval){1, 0});
175 send_mtu_probe_handler(n);
178 static void mtu_probe_h(node_t *n, vpn_packet_t *packet, length_t len) {
179 if(!DATA(packet)[0]) {
180 logger(DEBUG_TRAFFIC, LOG_INFO, "Got MTU probe request %d from %s (%s)", packet->len, n->name, n->hostname);
182 /* It's a probe request, send back a reply */
184 /* Type 2 probe replies were introduced in protocol 17.3 */
185 if ((n->options >> 24) >= 3) {
186 uint8_t *data = DATA(packet);
188 uint16_t len16 = htons(len); memcpy(data, &len16, 2); data += 2;
190 gettimeofday(&now, NULL);
191 uint32_t sec = htonl(now.tv_sec); memcpy(data, &sec, 4); data += 4;
192 uint32_t usec = htonl(now.tv_usec); memcpy(data, &usec, 4); data += 4;
195 /* Legacy protocol: n won't understand type 2 probe replies. */
199 /* Temporarily set udp_confirmed, so that the reply is sent
200 back exactly the way it came in. */
202 bool udp_confirmed = n->status.udp_confirmed;
203 n->status.udp_confirmed = true;
204 send_udppacket(n, packet);
205 n->status.udp_confirmed = udp_confirmed;
207 length_t probelen = len;
208 if (DATA(packet)[0] == 2) {
210 logger(DEBUG_TRAFFIC, LOG_WARNING, "Received invalid (too short) MTU probe reply from %s (%s)", n->name, n->hostname);
212 uint16_t probelen16; memcpy(&probelen16, DATA(packet) + 1, 2); probelen = ntohs(probelen16);
215 logger(DEBUG_TRAFFIC, LOG_INFO, "Got type %d MTU probe reply %d from %s (%s)", DATA(packet)[0], probelen, n->name, n->hostname);
217 /* It's a valid reply: now we know bidirectional communication
218 is possible using the address and socket that the reply
221 n->status.udp_confirmed = true;
223 /* If we haven't established the PMTU yet, restart the discovery process. */
225 if(n->mtuprobes > 30) {
226 if (probelen == n->maxmtu + 8) {
227 logger(DEBUG_TRAFFIC, LOG_INFO, "Increase in PMTU to %s (%s) detected, restarting PMTU discovery", n->name, n->hostname);
239 /* If applicable, raise the minimum supported MTU */
241 if(probelen > n->maxmtu)
242 probelen = n->maxmtu;
243 if(n->minmtu < probelen)
244 n->minmtu = probelen;
246 /* Calculate RTT and bandwidth.
247 The RTT is the time between the MTU probe burst was sent and the first
248 reply is received. The bandwidth is measured using the time between the
249 arrival of the first and third probe reply (or type 2 probe requests).
252 struct timeval now, diff;
253 gettimeofday(&now, NULL);
254 timersub(&now, &n->probe_time, &diff);
256 struct timeval probe_timestamp = now;
257 if (DATA(packet)[0] == 2 && packet->len >= 11) {
258 uint32_t sec; memcpy(&sec, DATA(packet) + 3, 4);
259 uint32_t usec; memcpy(&usec, DATA(packet) + 7, 4);
260 probe_timestamp.tv_sec = ntohl(sec);
261 probe_timestamp.tv_usec = ntohl(usec);
266 if(n->probe_counter == 1) {
267 n->rtt = diff.tv_sec + diff.tv_usec * 1e-6;
268 n->probe_time = probe_timestamp;
269 } else if(n->probe_counter == 3) {
270 struct timeval probe_timestamp_diff;
271 timersub(&probe_timestamp, &n->probe_time, &probe_timestamp_diff);
272 n->bandwidth = 2.0 * probelen / (probe_timestamp_diff.tv_sec + probe_timestamp_diff.tv_usec * 1e-6);
273 logger(DEBUG_TRAFFIC, LOG_DEBUG, "%s (%s) RTT %.2f ms, burst bandwidth %.3f Mbit/s, rx packet loss %.2f %%", n->name, n->hostname, n->rtt * 1e3, n->bandwidth * 8e-6, n->packetloss * 1e2);
278 static length_t compress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
280 memcpy(dest, source, len);
282 } else if(level == 10) {
284 lzo_uint lzolen = MAXSIZE;
285 lzo1x_1_compress(source, len, dest, &lzolen, lzo_wrkmem);
290 } else if(level < 10) {
292 unsigned long destlen = MAXSIZE;
293 if(compress2(dest, &destlen, source, len, level) == Z_OK)
300 lzo_uint lzolen = MAXSIZE;
301 lzo1x_999_compress(source, len, dest, &lzolen, lzo_wrkmem);
311 static length_t uncompress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
313 memcpy(dest, source, len);
315 } else if(level > 9) {
317 lzo_uint lzolen = MAXSIZE;
318 if(lzo1x_decompress_safe(source, len, dest, &lzolen, NULL) == LZO_E_OK)
326 unsigned long destlen = MAXSIZE;
327 if(uncompress(dest, &destlen, source, len) == Z_OK)
339 static void receive_packet(node_t *n, vpn_packet_t *packet) {
340 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Received packet of %d bytes from %s (%s)",
341 packet->len, n->name, n->hostname);
344 n->in_bytes += packet->len;
349 static bool try_mac(node_t *n, const vpn_packet_t *inpkt) {
351 return sptps_verify_datagram(&n->sptps, DATA(inpkt), inpkt->len);
353 #ifdef DISABLE_LEGACY
356 if(!digest_active(n->indigest) || inpkt->len < sizeof(seqno_t) + digest_length(n->indigest))
359 return digest_verify(n->indigest, SEQNO(inpkt), inpkt->len - digest_length(n->indigest), DATA(inpkt) + inpkt->len - digest_length(n->indigest));
363 static bool receive_udppacket(node_t *n, vpn_packet_t *inpkt) {
364 vpn_packet_t pkt1, pkt2;
365 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
368 pkt1.offset = DEFAULT_PACKET_OFFSET;
369 pkt2.offset = DEFAULT_PACKET_OFFSET;
371 if(n->status.sptps) {
372 if(!n->sptps.state) {
373 if(!n->status.waitingforkey) {
374 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Got packet from %s (%s) but we haven't exchanged keys yet", n->name, n->hostname);
377 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Got packet from %s (%s) but he hasn't got our key yet", n->name, n->hostname);
381 inpkt->offset += 2 * sizeof(node_id_t);
382 if(!sptps_receive_data(&n->sptps, DATA(inpkt), inpkt->len - 2 * sizeof(node_id_t))) {
383 logger(DEBUG_TRAFFIC, LOG_ERR, "Got bad packet from %s (%s)", n->name, n->hostname);
389 #ifdef DISABLE_LEGACY
392 if(!n->status.validkey) {
393 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Got packet from %s (%s) but he hasn't got our key yet", n->name, n->hostname);
397 /* Check packet length */
399 if(inpkt->len < sizeof(seqno_t) + digest_length(n->indigest)) {
400 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Got too short packet from %s (%s)",
401 n->name, n->hostname);
405 /* It's a legacy UDP packet, the data starts after the seqno */
407 inpkt->offset += sizeof(seqno_t);
409 /* Check the message authentication code */
411 if(digest_active(n->indigest)) {
412 inpkt->len -= digest_length(n->indigest);
413 if(!digest_verify(n->indigest, SEQNO(inpkt), inpkt->len, SEQNO(inpkt) + inpkt->len)) {
414 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Got unauthenticated packet from %s (%s)", n->name, n->hostname);
418 /* Decrypt the packet */
420 if(cipher_active(n->incipher)) {
421 vpn_packet_t *outpkt = pkt[nextpkt++];
424 if(!cipher_decrypt(n->incipher, SEQNO(inpkt), inpkt->len, SEQNO(outpkt), &outlen, true)) {
425 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Error decrypting packet from %s (%s)", n->name, n->hostname);
429 outpkt->len = outlen;
433 /* Check the sequence number */
436 memcpy(&seqno, SEQNO(inpkt), sizeof seqno);
437 seqno = ntohl(seqno);
438 inpkt->len -= sizeof seqno;
441 if(seqno != n->received_seqno + 1) {
442 if(seqno >= n->received_seqno + replaywin * 8) {
443 if(n->farfuture++ < replaywin >> 2) {
444 logger(DEBUG_ALWAYS, LOG_WARNING, "Packet from %s (%s) is %d seqs in the future, dropped (%u)",
445 n->name, n->hostname, seqno - n->received_seqno - 1, n->farfuture);
448 logger(DEBUG_ALWAYS, LOG_WARNING, "Lost %d packets from %s (%s)",
449 seqno - n->received_seqno - 1, n->name, n->hostname);
450 memset(n->late, 0, replaywin);
451 } else if (seqno <= n->received_seqno) {
452 if((n->received_seqno >= replaywin * 8 && seqno <= n->received_seqno - replaywin * 8) || !(n->late[(seqno / 8) % replaywin] & (1 << seqno % 8))) {
453 logger(DEBUG_ALWAYS, LOG_WARNING, "Got late or replayed packet from %s (%s), seqno %d, last received %d",
454 n->name, n->hostname, seqno, n->received_seqno);
458 for(int i = n->received_seqno + 1; i < seqno; i++)
459 n->late[(i / 8) % replaywin] |= 1 << i % 8;
464 n->late[(seqno / 8) % replaywin] &= ~(1 << seqno % 8);
467 if(seqno > n->received_seqno)
468 n->received_seqno = seqno;
472 if(n->received_seqno > MAX_SEQNO)
475 /* Decompress the packet */
477 length_t origlen = inpkt->len;
479 if(n->incompression) {
480 vpn_packet_t *outpkt = pkt[nextpkt++];
482 if((outpkt->len = uncompress_packet(DATA(outpkt), DATA(inpkt), inpkt->len, n->incompression)) < 0) {
483 logger(DEBUG_TRAFFIC, LOG_ERR, "Error while uncompressing packet from %s (%s)",
484 n->name, n->hostname);
490 origlen -= MTU/64 + 20;
495 if(!DATA(inpkt)[12] && !DATA(inpkt)[13])
496 mtu_probe_h(n, inpkt, origlen);
498 receive_packet(n, inpkt);
503 void receive_tcppacket(connection_t *c, const char *buffer, int len) {
505 outpkt.offset = DEFAULT_PACKET_OFFSET;
507 if(len > sizeof outpkt.data - outpkt.offset)
511 if(c->options & OPTION_TCPONLY)
514 outpkt.priority = -1;
515 memcpy(DATA(&outpkt), buffer, len);
517 receive_packet(c->node, &outpkt);
520 static bool try_sptps(node_t *n) {
521 if(n->status.validkey)
524 /* If n is a TCP-only neighbor, we'll only use "cleartext" PACKET
525 messages anyway, so there's no need for SPTPS at all. */
526 if(n->connection && ((myself->options | n->options) & OPTION_TCPONLY))
529 logger(DEBUG_TRAFFIC, LOG_INFO, "No valid key known yet for %s (%s)", n->name, n->hostname);
531 if(!n->status.waitingforkey)
533 else if(n->last_req_key + 10 < now.tv_sec) {
534 logger(DEBUG_ALWAYS, LOG_DEBUG, "No key from %s after 10 seconds, restarting SPTPS", n->name);
535 sptps_stop(&n->sptps);
536 n->status.waitingforkey = false;
543 static void send_sptps_packet(node_t *n, vpn_packet_t *origpkt) {
544 /* Note: condition order is as intended - even if we have a direct
545 metaconnection, we want to try SPTPS anyway as it's the only way to
547 if(!try_sptps(n) && !n->connection)
553 if(!(DATA(origpkt)[12] | DATA(origpkt)[13])) {
554 sptps_send_record(&n->sptps, PKT_PROBE, (char *)DATA(origpkt), origpkt->len);
558 if(routing_mode == RMODE_ROUTER)
563 if(origpkt->len < offset)
568 if(n->outcompression) {
570 int len = compress_packet(DATA(&outpkt) + offset, DATA(origpkt) + offset, origpkt->len - offset, n->outcompression);
572 logger(DEBUG_TRAFFIC, LOG_ERR, "Error while compressing packet to %s (%s)", n->name, n->hostname);
573 } else if(len < origpkt->len - offset) {
574 outpkt.len = len + offset;
576 type |= PKT_COMPRESSED;
580 /* If we have a direct metaconnection to n, and we can't use UDP, then
581 don't bother with SPTPS and just use a "plaintext" PACKET message.
582 We don't really care about end-to-end security since we're not
583 sending the message through any intermediate nodes. */
584 if(n->connection && origpkt->len > n->minmtu)
585 send_tcppacket(n->connection, origpkt);
587 sptps_send_record(&n->sptps, type, DATA(origpkt) + offset, origpkt->len - offset);
591 static void adapt_socket(const sockaddr_t *sa, int *sock) {
592 /* Make sure we have a suitable socket for the chosen address */
593 if(listen_socket[*sock].sa.sa.sa_family != sa->sa.sa_family) {
594 for(int i = 0; i < listen_sockets; i++) {
595 if(listen_socket[i].sa.sa.sa_family == sa->sa.sa_family) {
603 static void choose_udp_address(const node_t *n, const sockaddr_t **sa, int *sock) {
608 /* If the UDP address is confirmed, use it. */
609 if(n->status.udp_confirmed)
612 /* Send every third packet to n->address; that could be set
613 to the node's reflexive UDP address discovered during key
622 /* Otherwise, address are found in edges to this node.
623 So we pick a random edge and a random socket. */
626 int j = rand() % n->edge_tree->count;
627 edge_t *candidate = NULL;
629 for splay_each(edge_t, e, n->edge_tree) {
631 candidate = e->reverse;
637 *sa = &candidate->address;
638 *sock = rand() % listen_sockets;
641 adapt_socket(*sa, sock);
644 static void choose_local_address(const node_t *n, const sockaddr_t **sa, int *sock) {
647 /* Pick one of the edges from this node at random, then use its local address. */
650 int j = rand() % n->edge_tree->count;
651 edge_t *candidate = NULL;
653 for splay_each(edge_t, e, n->edge_tree) {
660 if (candidate && candidate->local_address.sa.sa_family) {
661 *sa = &candidate->local_address;
662 *sock = rand() % listen_sockets;
663 adapt_socket(*sa, sock);
667 static void send_udppacket(node_t *n, vpn_packet_t *origpkt) {
668 vpn_packet_t pkt1, pkt2;
669 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
670 vpn_packet_t *inpkt = origpkt;
672 vpn_packet_t *outpkt;
673 int origlen = origpkt->len;
675 #if defined(SOL_IP) && defined(IP_TOS)
676 static int priority = 0;
677 int origpriority = origpkt->priority;
680 pkt1.offset = DEFAULT_PACKET_OFFSET;
681 pkt2.offset = DEFAULT_PACKET_OFFSET;
683 if(!n->status.reachable) {
684 logger(DEBUG_TRAFFIC, LOG_INFO, "Trying to send UDP packet to unreachable node %s (%s)", n->name, n->hostname);
689 return send_sptps_packet(n, origpkt);
691 #ifdef DISABLE_LEGACY
694 /* Make sure we have a valid key */
696 if(!n->status.validkey) {
697 logger(DEBUG_TRAFFIC, LOG_INFO,
698 "No valid key known yet for %s (%s), forwarding via TCP",
699 n->name, n->hostname);
701 if(n->last_req_key + 10 <= now.tv_sec) {
703 n->last_req_key = now.tv_sec;
706 send_tcppacket(n->nexthop->connection, origpkt);
711 if(n->options & OPTION_PMTU_DISCOVERY && inpkt->len > n->minmtu && (DATA(inpkt)[12] | DATA(inpkt)[13])) {
712 logger(DEBUG_TRAFFIC, LOG_INFO,
713 "Packet for %s (%s) larger than minimum MTU, forwarding via %s",
714 n->name, n->hostname, n != n->nexthop ? n->nexthop->name : "TCP");
717 send_packet(n->nexthop, origpkt);
719 send_tcppacket(n->nexthop->connection, origpkt);
724 /* Compress the packet */
726 if(n->outcompression) {
727 outpkt = pkt[nextpkt++];
729 if((outpkt->len = compress_packet(DATA(outpkt), DATA(inpkt), inpkt->len, n->outcompression)) < 0) {
730 logger(DEBUG_TRAFFIC, LOG_ERR, "Error while compressing packet to %s (%s)",
731 n->name, n->hostname);
738 /* Add sequence number */
740 seqno_t seqno = htonl(++(n->sent_seqno));
741 memcpy(SEQNO(inpkt), &seqno, sizeof seqno);
742 inpkt->len += sizeof seqno;
744 /* Encrypt the packet */
746 if(cipher_active(n->outcipher)) {
747 outpkt = pkt[nextpkt++];
750 if(!cipher_encrypt(n->outcipher, SEQNO(inpkt), inpkt->len, SEQNO(outpkt), &outlen, true)) {
751 logger(DEBUG_TRAFFIC, LOG_ERR, "Error while encrypting packet to %s (%s)", n->name, n->hostname);
755 outpkt->len = outlen;
759 /* Add the message authentication code */
761 if(digest_active(n->outdigest)) {
762 if(!digest_create(n->outdigest, SEQNO(inpkt), inpkt->len, SEQNO(inpkt) + inpkt->len)) {
763 logger(DEBUG_TRAFFIC, LOG_ERR, "Error while encrypting packet to %s (%s)", n->name, n->hostname);
767 inpkt->len += digest_length(n->outdigest);
770 /* Send the packet */
772 const sockaddr_t *sa = NULL;
775 if(n->status.send_locally)
776 choose_local_address(n, &sa, &sock);
778 choose_udp_address(n, &sa, &sock);
780 #if defined(SOL_IP) && defined(IP_TOS)
781 if(priorityinheritance && origpriority != priority
782 && listen_socket[n->sock].sa.sa.sa_family == AF_INET) {
783 priority = origpriority;
784 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Setting outgoing packet priority to %d", priority);
785 if(setsockopt(listen_socket[n->sock].udp.fd, SOL_IP, IP_TOS, &priority, sizeof(priority))) /* SO_PRIORITY doesn't seem to work */
786 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "setsockopt", sockstrerror(sockerrno));
790 if(sendto(listen_socket[sock].udp.fd, SEQNO(inpkt), inpkt->len, 0, &sa->sa, SALEN(sa->sa)) < 0 && !sockwouldblock(sockerrno)) {
791 if(sockmsgsize(sockerrno)) {
792 if(n->maxmtu >= origlen)
793 n->maxmtu = origlen - 1;
794 if(n->mtu >= origlen)
795 n->mtu = origlen - 1;
797 logger(DEBUG_TRAFFIC, LOG_WARNING, "Error sending packet to %s (%s): %s", n->name, n->hostname, sockstrerror(sockerrno));
801 origpkt->len = origlen;
805 static bool send_sptps_data_priv(node_t *to, node_t *from, int type, const void *data, size_t len) {
806 node_t *relay = (to->via != myself && (type == PKT_PROBE || (len - SPTPS_DATAGRAM_OVERHEAD) <= to->via->minmtu)) ? to->via : to->nexthop;
807 bool direct = from == myself && to == relay;
808 bool relay_supported = (relay->options >> 24) >= 4;
809 bool tcponly = (myself->options | relay->options) & OPTION_TCPONLY;
811 /* We don't really need the relay's key, but we need to establish a UDP tunnel with it and discover its MTU. */
812 if (!direct && relay_supported && !tcponly)
815 /* Send it via TCP if it is a handshake packet, TCPOnly is in use, this is a relay packet that the other node cannot understand, or this packet is larger than the MTU.
816 TODO: When relaying, the original sender does not know the end-to-end PMTU (it only knows the PMTU of the first hop).
817 This can lead to scenarios where large packets are sent over UDP to relay, but then relay has no choice but fall back to TCP. */
819 if(type == SPTPS_HANDSHAKE || tcponly || (!direct && !relay_supported) || (type != PKT_PROBE && (len - SPTPS_DATAGRAM_OVERHEAD) > relay->minmtu)) {
820 char buf[len * 4 / 3 + 5];
821 b64encode(data, buf, len);
822 /* If no valid key is known yet, send the packets using ANS_KEY requests,
823 to ensure we get to learn the reflexive UDP address. */
824 if(from == myself && !to->status.validkey) {
825 to->incompression = myself->incompression;
826 return send_request(to->nexthop->connection, "%d %s %s %s -1 -1 -1 %d", ANS_KEY, from->name, to->name, buf, to->incompression);
828 return send_request(to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, from->name, to->name, REQ_SPTPS, buf);
833 if(relay_supported) overhead += sizeof to->id + sizeof from->id;
834 char buf[len + overhead]; char* buf_ptr = buf;
835 if(relay_supported) {
837 /* Inform the recipient that this packet was sent directly. */
838 node_id_t nullid = {};
839 memcpy(buf_ptr, &nullid, sizeof nullid); buf_ptr += sizeof nullid;
841 memcpy(buf_ptr, &to->id, sizeof to->id); buf_ptr += sizeof to->id;
843 memcpy(buf_ptr, &from->id, sizeof from->id); buf_ptr += sizeof from->id;
846 /* TODO: if this copy turns out to be a performance concern, change sptps_send_record() to add some "pre-padding" to the buffer and use that instead */
847 memcpy(buf_ptr, data, len); buf_ptr += len;
849 const sockaddr_t *sa = NULL;
851 if(relay->status.send_locally)
852 choose_local_address(relay, &sa, &sock);
854 choose_udp_address(relay, &sa, &sock);
855 logger(DEBUG_TRAFFIC, LOG_INFO, "Sending packet from %s (%s) to %s (%s) via %s (%s)", from->name, from->hostname, to->name, to->hostname, relay->name, relay->hostname);
856 if(sendto(listen_socket[sock].udp.fd, buf, buf_ptr - buf, 0, &sa->sa, SALEN(sa->sa)) < 0 && !sockwouldblock(sockerrno)) {
857 if(sockmsgsize(sockerrno)) {
858 // Compensate for SPTPS overhead
859 len -= SPTPS_DATAGRAM_OVERHEAD;
860 if(relay->maxmtu >= len)
861 relay->maxmtu = len - 1;
862 if(relay->mtu >= len)
863 relay->mtu = len - 1;
865 logger(DEBUG_TRAFFIC, LOG_WARNING, "Error sending UDP SPTPS packet to %s (%s): %s", relay->name, relay->hostname, sockstrerror(sockerrno));
873 bool send_sptps_data(void *handle, uint8_t type, const void *data, size_t len) {
874 return send_sptps_data_priv(handle, myself, type, data, len);
877 bool receive_sptps_record(void *handle, uint8_t type, const void *data, uint16_t len) {
878 node_t *from = handle;
880 if(type == SPTPS_HANDSHAKE) {
881 if(!from->status.validkey) {
882 from->status.validkey = true;
883 from->status.waitingforkey = false;
884 logger(DEBUG_META, LOG_INFO, "SPTPS key exchange with %s (%s) succesful", from->name, from->hostname);
890 logger(DEBUG_ALWAYS, LOG_ERR, "Packet from %s (%s) larger than maximum supported size (%d > %d)", from->name, from->hostname, len, MTU);
895 inpkt.offset = DEFAULT_PACKET_OFFSET;
897 if(type == PKT_PROBE) {
899 memcpy(DATA(&inpkt), data, len);
900 mtu_probe_h(from, &inpkt, len);
904 if(type & ~(PKT_COMPRESSED | PKT_MAC)) {
905 logger(DEBUG_ALWAYS, LOG_ERR, "Unexpected SPTPS record type %d len %d from %s (%s)", type, len, from->name, from->hostname);
909 /* Check if we have the headers we need */
910 if(routing_mode != RMODE_ROUTER && !(type & PKT_MAC)) {
911 logger(DEBUG_TRAFFIC, LOG_ERR, "Received packet from %s (%s) without MAC header (maybe Mode is not set correctly)", from->name, from->hostname);
913 } else if(routing_mode == RMODE_ROUTER && (type & PKT_MAC)) {
914 logger(DEBUG_TRAFFIC, LOG_WARNING, "Received packet from %s (%s) with MAC header (maybe Mode is not set correctly)", from->name, from->hostname);
917 int offset = (type & PKT_MAC) ? 0 : 14;
918 if(type & PKT_COMPRESSED) {
919 length_t ulen = uncompress_packet(DATA(&inpkt) + offset, (const uint8_t *)data, len, from->incompression);
923 inpkt.len = ulen + offset;
925 if(inpkt.len > MAXSIZE)
928 memcpy(DATA(&inpkt) + offset, data, len);
929 inpkt.len = len + offset;
932 /* Generate the Ethernet packet type if necessary */
934 switch(DATA(&inpkt)[14] >> 4) {
936 DATA(&inpkt)[12] = 0x08;
937 DATA(&inpkt)[13] = 0x00;
940 DATA(&inpkt)[12] = 0x86;
941 DATA(&inpkt)[13] = 0xDD;
944 logger(DEBUG_TRAFFIC, LOG_ERR,
945 "Unknown IP version %d while reading packet from %s (%s)",
946 DATA(&inpkt)[14] >> 4, from->name, from->hostname);
951 receive_packet(from, &inpkt);
956 send a packet to the given vpn ip.
958 void send_packet(node_t *n, vpn_packet_t *packet) {
963 memcpy(DATA(packet), mymac.x, ETH_ALEN);
965 n->out_bytes += packet->len;
966 devops.write(packet);
970 logger(DEBUG_TRAFFIC, LOG_ERR, "Sending packet of %d bytes to %s (%s)",
971 packet->len, n->name, n->hostname);
973 if(!n->status.reachable) {
974 logger(DEBUG_TRAFFIC, LOG_INFO, "Node %s (%s) is not reachable",
975 n->name, n->hostname);
980 n->out_bytes += packet->len;
982 if(n->status.sptps) {
983 send_sptps_packet(n, packet);
987 via = (packet->priority == -1 || n->via == myself) ? n->nexthop : n->via;
990 logger(DEBUG_TRAFFIC, LOG_INFO, "Sending packet to %s via %s (%s)",
991 n->name, via->name, n->via->hostname);
993 if(packet->priority == -1 || ((myself->options | via->options) & OPTION_TCPONLY)) {
994 if(!send_tcppacket(via->connection, packet))
995 terminate_connection(via->connection, true);
997 send_udppacket(via, packet);
1000 /* Broadcast a packet using the minimum spanning tree */
1002 void broadcast_packet(const node_t *from, vpn_packet_t *packet) {
1003 // Always give ourself a copy of the packet.
1005 send_packet(myself, packet);
1007 // In TunnelServer mode, do not forward broadcast packets.
1008 // The MST might not be valid and create loops.
1009 if(tunnelserver || broadcast_mode == BMODE_NONE)
1012 logger(DEBUG_TRAFFIC, LOG_INFO, "Broadcasting packet of %d bytes from %s (%s)",
1013 packet->len, from->name, from->hostname);
1015 switch(broadcast_mode) {
1016 // In MST mode, broadcast packets travel via the Minimum Spanning Tree.
1017 // This guarantees all nodes receive the broadcast packet, and
1018 // usually distributes the sending of broadcast packets over all nodes.
1020 for list_each(connection_t, c, connection_list)
1021 if(c->edge && c->status.mst && c != from->nexthop->connection)
1022 send_packet(c->node, packet);
1025 // In direct mode, we send copies to each node we know of.
1026 // However, this only reaches nodes that can be reached in a single hop.
1027 // We don't have enough information to forward broadcast packets in this case.
1032 for splay_each(node_t, n, node_tree)
1033 if(n->status.reachable && n != myself && ((n->via == myself && n->nexthop == n) || n->via == n))
1034 send_packet(n, packet);
1042 static node_t *try_harder(const sockaddr_t *from, const vpn_packet_t *pkt) {
1045 static time_t last_hard_try = 0;
1047 for splay_each(edge_t, e, edge_weight_tree) {
1048 if(!e->to->status.reachable || e->to == myself)
1051 if(sockaddrcmp_noport(from, &e->address)) {
1052 if(last_hard_try == now.tv_sec)
1057 if(!try_mac(e->to, pkt))
1065 last_hard_try = now.tv_sec;
1067 last_hard_try = now.tv_sec;
1071 void handle_incoming_vpn_data(void *data, int flags) {
1072 listen_socket_t *ls = data;
1075 node_id_t nullid = {};
1076 sockaddr_t addr = {};
1077 socklen_t addrlen = sizeof addr;
1079 bool direct = false;
1082 int len = recvfrom(ls->udp.fd, DATA(&pkt), MAXSIZE, 0, &addr.sa, &addrlen);
1084 if(len <= 0 || len > MAXSIZE) {
1085 if(!sockwouldblock(sockerrno))
1086 logger(DEBUG_ALWAYS, LOG_ERR, "Receiving packet failed: %s", sockstrerror(sockerrno));
1092 sockaddrunmap(&addr); /* Some braindead IPv6 implementations do stupid things. */
1094 // Try to figure out who sent this packet.
1096 node_t *n = lookup_node_udp(&addr);
1099 // It might be from a 1.1 node, which might have a source ID in the packet.
1100 pkt.offset = 2 * sizeof(node_id_t);
1101 from = lookup_node_id(SRCID(&pkt));
1102 if(from && !memcmp(DSTID(&pkt), &nullid, sizeof nullid) && from->status.sptps) {
1103 if(sptps_verify_datagram(&from->sptps, DATA(&pkt), pkt.len - 2 * sizeof(node_id_t)))
1112 n = try_harder(&addr, &pkt);
1117 if(debug_level >= DEBUG_PROTOCOL) {
1118 hostname = sockaddr2hostname(&addr);
1119 logger(DEBUG_PROTOCOL, LOG_WARNING, "Received UDP packet from unknown source %s", hostname);
1125 if(n->status.sptps) {
1126 pkt.offset = 2 * sizeof(node_id_t);
1128 if(!memcmp(DSTID(&pkt), &nullid, sizeof nullid)) {
1133 from = lookup_node_id(SRCID(&pkt));
1134 to = lookup_node_id(DSTID(&pkt));
1137 logger(DEBUG_PROTOCOL, LOG_WARNING, "Received UDP packet from %s (%s) with unknown source and/or destination ID", n->name, n->hostname);
1142 send_sptps_data_priv(to, n, 0, DATA(&pkt), pkt.len - 2 * sizeof(node_id_t));
1151 if(!receive_udppacket(from, &pkt))
1154 n->sock = ls - listen_socket;
1155 if(direct && sockaddrcmp(&addr, &n->address))
1156 update_node_udp(n, &addr);
1159 void handle_device_data(void *data, int flags) {
1160 vpn_packet_t packet;
1161 packet.offset = DEFAULT_PACKET_OFFSET;
1162 packet.priority = 0;
1164 if(devops.read(&packet)) {
1165 myself->in_packets++;
1166 myself->in_bytes += packet.len;
1167 route(myself, &packet);