2 net_packet.c -- Handles in- and outgoing VPN packets
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2009 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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>
36 #include "connection.h"
52 #define EMSGSIZE WSAEMSGSIZE
57 EVP_CIPHER_CTX packet_ctx;
58 static char lzo_wrkmem[LZO1X_999_MEM_COMPRESS > LZO1X_1_MEM_COMPRESS ? LZO1X_999_MEM_COMPRESS : LZO1X_1_MEM_COMPRESS];
60 static void send_udppacket(node_t *, vpn_packet_t *);
62 #define MAX_SEQNO 1073741824
64 void send_mtu_probe(node_t *n)
74 if(n->mtuprobes >= 10 && !n->minmtu) {
75 ifdebug(TRAFFIC) logger(LOG_INFO, _("No response to MTU probes from %s (%s)"), n->name, n->hostname);
79 for(i = 0; i < 3; i++) {
80 if(n->mtuprobes >= 30 || n->minmtu >= n->maxmtu) {
82 ifdebug(TRAFFIC) logger(LOG_INFO, _("Fixing MTU of %s (%s) to %d after %d probes"), n->name, n->hostname, n->mtu, n->mtuprobes);
86 len = n->minmtu + 1 + random() % (n->maxmtu - n->minmtu);
90 memset(packet.data, 0, 14);
91 RAND_pseudo_bytes(packet.data + 14, len - 14);
95 ifdebug(TRAFFIC) logger(LOG_INFO, _("Sending MTU probe length %d to %s (%s)"), len, n->name, n->hostname);
97 send_udppacket(n, &packet);
100 n->mtuevent = new_event();
101 n->mtuevent->handler = (event_handler_t)send_mtu_probe;
102 n->mtuevent->data = n;
103 n->mtuevent->time = now + 1;
104 event_add(n->mtuevent);
107 void mtu_probe_h(node_t *n, vpn_packet_t *packet) {
108 ifdebug(TRAFFIC) logger(LOG_INFO, _("Got MTU probe length %d from %s (%s)"), packet->len, n->name, n->hostname);
110 if(!packet->data[0]) {
112 send_packet(n, packet);
114 if(n->minmtu < packet->len)
115 n->minmtu = packet->len;
119 static length_t compress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level)
122 lzo_uint lzolen = MAXSIZE;
123 lzo1x_1_compress(source, len, dest, &lzolen, lzo_wrkmem);
125 } else if(level < 10) {
126 unsigned long destlen = MAXSIZE;
127 if(compress2(dest, &destlen, source, len, level) == Z_OK)
132 lzo_uint lzolen = MAXSIZE;
133 lzo1x_999_compress(source, len, dest, &lzolen, lzo_wrkmem);
140 static length_t uncompress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level)
143 lzo_uint lzolen = MAXSIZE;
144 if(lzo1x_decompress_safe(source, len, dest, &lzolen, NULL) == LZO_E_OK)
149 unsigned long destlen = MAXSIZE;
150 if(uncompress(dest, &destlen, source, len) == Z_OK)
161 static void receive_packet(node_t *n, vpn_packet_t *packet)
165 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Received packet of %d bytes from %s (%s)"),
166 packet->len, n->name, n->hostname);
171 static bool try_mac(const node_t *n, const vpn_packet_t *inpkt)
173 unsigned char hmac[EVP_MAX_MD_SIZE];
175 if(!n->indigest || !n->inmaclength || !n->inkey || inpkt->len < sizeof inpkt->seqno + n->inmaclength)
178 HMAC(n->indigest, n->inkey, n->inkeylength, (unsigned char *) &inpkt->seqno, inpkt->len - n->inmaclength, (unsigned char *)hmac, NULL);
180 return !memcmp(hmac, (char *) &inpkt->seqno + inpkt->len - n->inmaclength, n->inmaclength);
183 static void receive_udppacket(node_t *n, vpn_packet_t *inpkt)
185 vpn_packet_t pkt1, pkt2;
186 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
188 vpn_packet_t *outpkt = pkt[0];
190 unsigned char hmac[EVP_MAX_MD_SIZE];
196 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got packet from %s (%s) but he hasn't got our key yet"),
197 n->name, n->hostname);
201 /* Check packet length */
203 if(inpkt->len < sizeof(inpkt->seqno) + n->inmaclength) {
204 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got too short packet from %s (%s)"),
205 n->name, n->hostname);
209 /* Check the message authentication code */
211 if(n->indigest && n->inmaclength) {
212 inpkt->len -= n->inmaclength;
213 HMAC(n->indigest, n->inkey, n->inkeylength,
214 (unsigned char *) &inpkt->seqno, inpkt->len, (unsigned char *)hmac, NULL);
216 if(memcmp(hmac, (char *) &inpkt->seqno + inpkt->len, n->inmaclength)) {
217 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got unauthenticated packet from %s (%s)"),
218 n->name, n->hostname);
223 /* Decrypt the packet */
226 outpkt = pkt[nextpkt++];
228 if(!EVP_DecryptInit_ex(&n->inctx, NULL, NULL, NULL, NULL)
229 || !EVP_DecryptUpdate(&n->inctx, (unsigned char *) &outpkt->seqno, &outlen,
230 (unsigned char *) &inpkt->seqno, inpkt->len)
231 || !EVP_DecryptFinal_ex(&n->inctx, (unsigned char *) &outpkt->seqno + outlen, &outpad)) {
232 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Error decrypting packet from %s (%s): %s"),
233 n->name, n->hostname, ERR_error_string(ERR_get_error(), NULL));
237 outpkt->len = outlen + outpad;
241 /* Check the sequence number */
243 inpkt->len -= sizeof(inpkt->seqno);
244 inpkt->seqno = ntohl(inpkt->seqno);
246 if(inpkt->seqno != n->received_seqno + 1) {
247 if(inpkt->seqno >= n->received_seqno + sizeof(n->late) * 8) {
248 logger(LOG_WARNING, _("Lost %d packets from %s (%s)"),
249 inpkt->seqno - n->received_seqno - 1, n->name, n->hostname);
251 memset(n->late, 0, sizeof(n->late));
252 } else if (inpkt->seqno <= n->received_seqno) {
253 if((n->received_seqno >= sizeof(n->late) * 8 && inpkt->seqno <= n->received_seqno - sizeof(n->late) * 8) || !(n->late[(inpkt->seqno / 8) % sizeof(n->late)] & (1 << inpkt->seqno % 8))) {
254 logger(LOG_WARNING, _("Got late or replayed packet from %s (%s), seqno %d, last received %d"),
255 n->name, n->hostname, inpkt->seqno, n->received_seqno);
259 for(i = n->received_seqno + 1; i < inpkt->seqno; i++)
260 n->late[(i / 8) % sizeof(n->late)] |= 1 << i % 8;
264 n->late[(inpkt->seqno / 8) % sizeof(n->late)] &= ~(1 << inpkt->seqno % 8);
266 if(inpkt->seqno > n->received_seqno)
267 n->received_seqno = inpkt->seqno;
269 if(n->received_seqno > MAX_SEQNO)
272 /* Decompress the packet */
274 if(n->incompression) {
275 outpkt = pkt[nextpkt++];
277 if((outpkt->len = uncompress_packet(outpkt->data, inpkt->data, inpkt->len, n->incompression)) < 0) {
278 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while uncompressing packet from %s (%s)"),
279 n->name, n->hostname);
289 n->connection->last_ping_time = now;
291 if(!inpkt->data[12] && !inpkt->data[13])
292 mtu_probe_h(n, inpkt);
294 receive_packet(n, inpkt);
297 void receive_tcppacket(connection_t *c, char *buffer, int len)
304 if(c->options & OPTION_TCPONLY)
307 outpkt.priority = -1;
308 memcpy(outpkt.data, buffer, len);
310 receive_packet(c->node, &outpkt);
313 static void send_udppacket(node_t *n, vpn_packet_t *origpkt)
315 vpn_packet_t pkt1, pkt2;
316 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
317 vpn_packet_t *inpkt = origpkt;
319 vpn_packet_t *outpkt;
322 static int priority = 0;
328 /* Make sure we have a valid key */
330 if(!n->status.validkey) {
331 ifdebug(TRAFFIC) logger(LOG_INFO,
332 _("No valid key known yet for %s (%s), forwarding via TCP"),
333 n->name, n->hostname);
335 if(!n->status.waitingforkey)
338 n->status.waitingforkey = true;
340 send_tcppacket(n->nexthop->connection, origpkt);
345 if(!n->minmtu && (inpkt->data[12] | inpkt->data[13])) {
346 ifdebug(TRAFFIC) logger(LOG_INFO,
347 _("No minimum MTU established yet for %s (%s), forwarding via TCP"),
348 n->name, n->hostname);
350 send_tcppacket(n->nexthop->connection, origpkt);
355 origlen = inpkt->len;
356 origpriority = inpkt->priority;
358 /* Compress the packet */
360 if(n->outcompression) {
361 outpkt = pkt[nextpkt++];
363 if((outpkt->len = compress_packet(outpkt->data, inpkt->data, inpkt->len, n->outcompression)) < 0) {
364 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while compressing packet to %s (%s)"),
365 n->name, n->hostname);
372 /* Add sequence number */
374 inpkt->seqno = htonl(++(n->sent_seqno));
375 inpkt->len += sizeof(inpkt->seqno);
377 /* Encrypt the packet */
380 outpkt = pkt[nextpkt++];
382 if(!EVP_EncryptInit_ex(&n->outctx, NULL, NULL, NULL, NULL)
383 || !EVP_EncryptUpdate(&n->outctx, (unsigned char *) &outpkt->seqno, &outlen,
384 (unsigned char *) &inpkt->seqno, inpkt->len)
385 || !EVP_EncryptFinal_ex(&n->outctx, (unsigned char *) &outpkt->seqno + outlen, &outpad)) {
386 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while encrypting packet to %s (%s): %s"),
387 n->name, n->hostname, ERR_error_string(ERR_get_error(), NULL));
391 outpkt->len = outlen + outpad;
395 /* Add the message authentication code */
397 if(n->outdigest && n->outmaclength) {
398 HMAC(n->outdigest, n->outkey, n->outkeylength, (unsigned char *) &inpkt->seqno,
399 inpkt->len, (unsigned char *) &inpkt->seqno + inpkt->len, NULL);
400 inpkt->len += n->outmaclength;
403 /* Determine which socket we have to use */
405 for(sock = 0; sock < listen_sockets; sock++)
406 if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family)
409 if(sock >= listen_sockets)
410 sock = 0; /* If none is available, just use the first and hope for the best. */
412 /* Send the packet */
414 #if defined(SOL_IP) && defined(IP_TOS)
415 if(priorityinheritance && origpriority != priority
416 && listen_socket[sock].sa.sa.sa_family == AF_INET) {
417 priority = origpriority;
418 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Setting outgoing packet priority to %d"), priority);
419 if(setsockopt(listen_socket[sock].udp, SOL_IP, IP_TOS, &priority, sizeof(priority))) /* SO_PRIORITY doesn't seem to work */
420 logger(LOG_ERR, _("System call `%s' failed: %s"), "setsockopt", strerror(errno));
424 if((sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0) {
425 if(errno == EMSGSIZE) {
426 if(n->maxmtu >= origlen)
427 n->maxmtu = origlen - 1;
428 if(n->mtu >= origlen)
429 n->mtu = origlen - 1;
431 logger(LOG_ERR, _("Error sending packet to %s (%s): %s"), n->name, n->hostname, strerror(errno));
435 origpkt->len = origlen;
439 send a packet to the given vpn ip.
441 void send_packet(const node_t *n, vpn_packet_t *packet)
449 memcpy(packet->data, mymac.x, ETH_ALEN);
450 write_packet(packet);
454 ifdebug(TRAFFIC) logger(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
455 packet->len, n->name, n->hostname);
457 if(!n->status.reachable) {
458 ifdebug(TRAFFIC) logger(LOG_INFO, _("Node %s (%s) is not reachable"),
459 n->name, n->hostname);
463 via = (packet->priority == -1 || n->via == myself) ? n->nexthop : n->via;
466 ifdebug(TRAFFIC) logger(LOG_INFO, _("Sending packet to %s via %s (%s)"),
467 n->name, via->name, n->via->hostname);
469 if(packet->priority == -1 || ((myself->options | via->options) & OPTION_TCPONLY)) {
470 if(!send_tcppacket(via->connection, packet))
471 terminate_connection(via->connection, true);
473 send_udppacket(via, packet);
476 /* Broadcast a packet using the minimum spanning tree */
478 void broadcast_packet(const node_t *from, vpn_packet_t *packet)
485 ifdebug(TRAFFIC) logger(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
486 packet->len, from->name, from->hostname);
489 send_packet(myself, packet);
491 for(node = connection_tree->head; node; node = node->next) {
494 if(c->status.active && c->status.mst && c != from->nexthop->connection)
495 send_packet(c->node, packet);
499 static node_t *try_harder(const sockaddr_t *from, const vpn_packet_t *pkt) {
504 for(node = edge_weight_tree->head; node; node = node->next) {
507 if(sockaddrcmp_noport(from, &e->address))
513 if(!try_mac(e->to, pkt))
523 void handle_incoming_vpn_data(int sock)
528 socklen_t fromlen = sizeof(from);
533 pkt.len = recvfrom(sock, (char *) &pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen);
536 logger(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno));
540 sockaddrunmap(&from); /* Some braindead IPv6 implementations do stupid things. */
542 n = lookup_node_udp(&from);
545 n = try_harder(&from, &pkt);
547 update_node_udp(n, &from);
549 hostname = sockaddr2hostname(&from);
550 logger(LOG_WARNING, _("Received UDP packet from unknown source %s"), hostname);
556 receive_udppacket(n, &pkt);