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 static char lzo_wrkmem[LZO1X_999_MEM_COMPRESS > LZO1X_1_MEM_COMPRESS ? LZO1X_999_MEM_COMPRESS : LZO1X_1_MEM_COMPRESS];
59 static void send_udppacket(node_t *, vpn_packet_t *);
61 #define MAX_SEQNO 1073741824
63 void send_mtu_probe(node_t *n)
73 if(n->mtuprobes >= 10 && !n->minmtu) {
74 ifdebug(TRAFFIC) logger(LOG_INFO, _("No response to MTU probes from %s (%s)"), n->name, n->hostname);
78 for(i = 0; i < 3; i++) {
79 if(n->mtuprobes >= 30 || n->minmtu >= n->maxmtu) {
81 ifdebug(TRAFFIC) logger(LOG_INFO, _("Fixing MTU of %s (%s) to %d after %d probes"), n->name, n->hostname, n->mtu, n->mtuprobes);
85 len = n->minmtu + 1 + random() % (n->maxmtu - n->minmtu);
89 memset(packet.data, 0, 14);
90 RAND_pseudo_bytes(packet.data + 14, len - 14);
94 ifdebug(TRAFFIC) logger(LOG_INFO, _("Sending MTU probe length %d to %s (%s)"), len, n->name, n->hostname);
96 send_udppacket(n, &packet);
99 n->mtuevent = new_event();
100 n->mtuevent->handler = (event_handler_t)send_mtu_probe;
101 n->mtuevent->data = n;
102 n->mtuevent->time = now + 1;
103 event_add(n->mtuevent);
106 void mtu_probe_h(node_t *n, vpn_packet_t *packet) {
107 ifdebug(TRAFFIC) logger(LOG_INFO, _("Got MTU probe length %d from %s (%s)"), packet->len, n->name, n->hostname);
109 if(!packet->data[0]) {
111 send_packet(n, packet);
113 if(n->minmtu < packet->len)
114 n->minmtu = packet->len;
118 static length_t compress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level)
121 lzo_uint lzolen = MAXSIZE;
122 lzo1x_1_compress(source, len, dest, &lzolen, lzo_wrkmem);
124 } else if(level < 10) {
125 unsigned long destlen = MAXSIZE;
126 if(compress2(dest, &destlen, source, len, level) == Z_OK)
131 lzo_uint lzolen = MAXSIZE;
132 lzo1x_999_compress(source, len, dest, &lzolen, lzo_wrkmem);
139 static length_t uncompress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level)
142 lzo_uint lzolen = MAXSIZE;
143 if(lzo1x_decompress_safe(source, len, dest, &lzolen, NULL) == LZO_E_OK)
148 unsigned long destlen = MAXSIZE;
149 if(uncompress(dest, &destlen, source, len) == Z_OK)
160 static void receive_packet(node_t *n, vpn_packet_t *packet)
164 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Received packet of %d bytes from %s (%s)"),
165 packet->len, n->name, n->hostname);
170 static bool try_mac(const node_t *n, const vpn_packet_t *inpkt)
172 unsigned char hmac[EVP_MAX_MD_SIZE];
174 if(!n->indigest || !n->inmaclength || !n->inkey || inpkt->len < sizeof inpkt->seqno + n->inmaclength)
177 HMAC(n->indigest, n->inkey, n->inkeylength, (unsigned char *) &inpkt->seqno, inpkt->len - n->inmaclength, (unsigned char *)hmac, NULL);
179 return !memcmp(hmac, (char *) &inpkt->seqno + inpkt->len - n->inmaclength, n->inmaclength);
182 static void receive_udppacket(node_t *n, vpn_packet_t *inpkt)
184 vpn_packet_t pkt1, pkt2;
185 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
187 vpn_packet_t *outpkt = pkt[0];
189 unsigned char hmac[EVP_MAX_MD_SIZE];
195 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got packet from %s (%s) but he hasn't got our key yet"),
196 n->name, n->hostname);
200 /* Check packet length */
202 if(inpkt->len < sizeof(inpkt->seqno) + n->inmaclength) {
203 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got too short packet from %s (%s)"),
204 n->name, n->hostname);
208 /* Check the message authentication code */
210 if(n->indigest && n->inmaclength) {
211 inpkt->len -= n->inmaclength;
212 HMAC(n->indigest, n->inkey, n->inkeylength,
213 (unsigned char *) &inpkt->seqno, inpkt->len, (unsigned char *)hmac, NULL);
215 if(memcmp(hmac, (char *) &inpkt->seqno + inpkt->len, n->inmaclength)) {
216 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got unauthenticated packet from %s (%s)"),
217 n->name, n->hostname);
222 /* Decrypt the packet */
225 outpkt = pkt[nextpkt++];
227 if(!EVP_DecryptInit_ex(&n->inctx, NULL, NULL, NULL, NULL)
228 || !EVP_DecryptUpdate(&n->inctx, (unsigned char *) &outpkt->seqno, &outlen,
229 (unsigned char *) &inpkt->seqno, inpkt->len)
230 || !EVP_DecryptFinal_ex(&n->inctx, (unsigned char *) &outpkt->seqno + outlen, &outpad)) {
231 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Error decrypting packet from %s (%s): %s"),
232 n->name, n->hostname, ERR_error_string(ERR_get_error(), NULL));
236 outpkt->len = outlen + outpad;
240 /* Check the sequence number */
242 inpkt->len -= sizeof(inpkt->seqno);
243 inpkt->seqno = ntohl(inpkt->seqno);
245 if(inpkt->seqno != n->received_seqno + 1) {
246 if(inpkt->seqno >= n->received_seqno + sizeof(n->late) * 8) {
247 logger(LOG_WARNING, _("Lost %d packets from %s (%s)"),
248 inpkt->seqno - n->received_seqno - 1, n->name, n->hostname);
250 memset(n->late, 0, sizeof(n->late));
251 } else if (inpkt->seqno <= n->received_seqno) {
252 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))) {
253 logger(LOG_WARNING, _("Got late or replayed packet from %s (%s), seqno %d, last received %d"),
254 n->name, n->hostname, inpkt->seqno, n->received_seqno);
258 for(i = n->received_seqno + 1; i < inpkt->seqno; i++)
259 n->late[(i / 8) % sizeof(n->late)] |= 1 << i % 8;
263 n->late[(inpkt->seqno / 8) % sizeof(n->late)] &= ~(1 << inpkt->seqno % 8);
265 if(inpkt->seqno > n->received_seqno)
266 n->received_seqno = inpkt->seqno;
268 if(n->received_seqno > MAX_SEQNO)
271 /* Decompress the packet */
273 if(n->incompression) {
274 outpkt = pkt[nextpkt++];
276 if((outpkt->len = uncompress_packet(outpkt->data, inpkt->data, inpkt->len, n->incompression)) < 0) {
277 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while uncompressing packet from %s (%s)"),
278 n->name, n->hostname);
288 n->connection->last_ping_time = now;
290 if(!inpkt->data[12] && !inpkt->data[13])
291 mtu_probe_h(n, inpkt);
293 receive_packet(n, inpkt);
296 void receive_tcppacket(connection_t *c, char *buffer, int len)
303 if(c->options & OPTION_TCPONLY)
306 outpkt.priority = -1;
307 memcpy(outpkt.data, buffer, len);
309 receive_packet(c->node, &outpkt);
312 static void send_udppacket(node_t *n, vpn_packet_t *origpkt)
314 vpn_packet_t pkt1, pkt2;
315 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
316 vpn_packet_t *inpkt = origpkt;
318 vpn_packet_t *outpkt;
321 static int priority = 0;
327 /* Make sure we have a valid key */
329 if(!n->status.validkey) {
330 ifdebug(TRAFFIC) logger(LOG_INFO,
331 _("No valid key known yet for %s (%s), forwarding via TCP"),
332 n->name, n->hostname);
334 if(!n->status.waitingforkey)
337 n->status.waitingforkey = true;
339 send_tcppacket(n->nexthop->connection, origpkt);
344 if(n->options & OPTION_PMTU_DISCOVERY && !n->minmtu && (inpkt->data[12] | inpkt->data[13])) {
345 ifdebug(TRAFFIC) logger(LOG_INFO,
346 _("No minimum MTU established yet for %s (%s), forwarding via TCP"),
347 n->name, n->hostname);
349 send_tcppacket(n->nexthop->connection, origpkt);
354 origlen = inpkt->len;
355 origpriority = inpkt->priority;
357 /* Compress the packet */
359 if(n->outcompression) {
360 outpkt = pkt[nextpkt++];
362 if((outpkt->len = compress_packet(outpkt->data, inpkt->data, inpkt->len, n->outcompression)) < 0) {
363 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while compressing packet to %s (%s)"),
364 n->name, n->hostname);
371 /* Add sequence number */
373 inpkt->seqno = htonl(++(n->sent_seqno));
374 inpkt->len += sizeof(inpkt->seqno);
376 /* Encrypt the packet */
379 outpkt = pkt[nextpkt++];
381 if(!EVP_EncryptInit_ex(&n->outctx, NULL, NULL, NULL, NULL)
382 || !EVP_EncryptUpdate(&n->outctx, (unsigned char *) &outpkt->seqno, &outlen,
383 (unsigned char *) &inpkt->seqno, inpkt->len)
384 || !EVP_EncryptFinal_ex(&n->outctx, (unsigned char *) &outpkt->seqno + outlen, &outpad)) {
385 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while encrypting packet to %s (%s): %s"),
386 n->name, n->hostname, ERR_error_string(ERR_get_error(), NULL));
390 outpkt->len = outlen + outpad;
394 /* Add the message authentication code */
396 if(n->outdigest && n->outmaclength) {
397 HMAC(n->outdigest, n->outkey, n->outkeylength, (unsigned char *) &inpkt->seqno,
398 inpkt->len, (unsigned char *) &inpkt->seqno + inpkt->len, NULL);
399 inpkt->len += n->outmaclength;
402 /* Determine which socket we have to use */
404 for(sock = 0; sock < listen_sockets; sock++)
405 if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family)
408 if(sock >= listen_sockets)
409 sock = 0; /* If none is available, just use the first and hope for the best. */
411 /* Send the packet */
413 #if defined(SOL_IP) && defined(IP_TOS)
414 if(priorityinheritance && origpriority != priority
415 && listen_socket[sock].sa.sa.sa_family == AF_INET) {
416 priority = origpriority;
417 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Setting outgoing packet priority to %d"), priority);
418 if(setsockopt(listen_socket[sock].udp, SOL_IP, IP_TOS, &priority, sizeof(priority))) /* SO_PRIORITY doesn't seem to work */
419 logger(LOG_ERR, _("System call `%s' failed: %s"), "setsockopt", strerror(errno));
423 if((sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0) {
424 if(errno == EMSGSIZE) {
425 if(n->maxmtu >= origlen)
426 n->maxmtu = origlen - 1;
427 if(n->mtu >= origlen)
428 n->mtu = origlen - 1;
430 logger(LOG_ERR, _("Error sending packet to %s (%s): %s"), n->name, n->hostname, strerror(errno));
434 origpkt->len = origlen;
438 send a packet to the given vpn ip.
440 void send_packet(const node_t *n, vpn_packet_t *packet)
448 memcpy(packet->data, mymac.x, ETH_ALEN);
449 write_packet(packet);
453 ifdebug(TRAFFIC) logger(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
454 packet->len, n->name, n->hostname);
456 if(!n->status.reachable) {
457 ifdebug(TRAFFIC) logger(LOG_INFO, _("Node %s (%s) is not reachable"),
458 n->name, n->hostname);
462 via = (packet->priority == -1 || n->via == myself) ? n->nexthop : n->via;
465 ifdebug(TRAFFIC) logger(LOG_INFO, _("Sending packet to %s via %s (%s)"),
466 n->name, via->name, n->via->hostname);
468 if(packet->priority == -1 || ((myself->options | via->options) & OPTION_TCPONLY)) {
469 if(!send_tcppacket(via->connection, packet))
470 terminate_connection(via->connection, true);
472 send_udppacket(via, packet);
475 /* Broadcast a packet using the minimum spanning tree */
477 void broadcast_packet(const node_t *from, vpn_packet_t *packet)
484 ifdebug(TRAFFIC) logger(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
485 packet->len, from->name, from->hostname);
488 send_packet(myself, packet);
490 for(node = connection_tree->head; node; node = node->next) {
493 if(c->status.active && c->status.mst && c != from->nexthop->connection)
494 send_packet(c->node, packet);
498 static node_t *try_harder(const sockaddr_t *from, const vpn_packet_t *pkt) {
503 for(node = edge_weight_tree->head; node; node = node->next) {
506 if(sockaddrcmp_noport(from, &e->address))
512 if(!try_mac(e->to, pkt))
522 void handle_incoming_vpn_data(int sock)
527 socklen_t fromlen = sizeof(from);
532 pkt.len = recvfrom(sock, (char *) &pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen);
535 logger(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno));
539 sockaddrunmap(&from); /* Some braindead IPv6 implementations do stupid things. */
541 n = lookup_node_udp(&from);
544 n = try_harder(&from, &pkt);
546 update_node_udp(n, &from);
547 else ifdebug(PROTOCOL) {
548 hostname = sockaddr2hostname(&from);
549 logger(LOG_WARNING, _("Received UDP packet from unknown source %s"), hostname);
557 receive_udppacket(n, &pkt);