2 net_packet.c -- Handles in- and outgoing VPN packets
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2006 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 void receive_udppacket(node_t *n, vpn_packet_t *inpkt)
173 vpn_packet_t pkt1, pkt2;
174 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
176 vpn_packet_t *outpkt = pkt[0];
178 unsigned char hmac[EVP_MAX_MD_SIZE];
183 /* Check packet length */
185 if(inpkt->len < sizeof(inpkt->seqno) + myself->maclength) {
186 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got too short packet from %s (%s)"),
187 n->name, n->hostname);
191 /* Check the message authentication code */
193 if(myself->digest && myself->maclength) {
194 inpkt->len -= myself->maclength;
195 HMAC(myself->digest, myself->key, myself->keylength,
196 (unsigned char *) &inpkt->seqno, inpkt->len, (unsigned char *)hmac, NULL);
198 if(memcmp(hmac, (char *) &inpkt->seqno + inpkt->len, myself->maclength)) {
199 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got unauthenticated packet from %s (%s)"),
200 n->name, n->hostname);
205 /* Decrypt the packet */
208 outpkt = pkt[nextpkt++];
210 if(!EVP_DecryptInit_ex(&packet_ctx, NULL, NULL, NULL, NULL)
211 || !EVP_DecryptUpdate(&packet_ctx, (unsigned char *) &outpkt->seqno, &outlen,
212 (unsigned char *) &inpkt->seqno, inpkt->len)
213 || !EVP_DecryptFinal_ex(&packet_ctx, (unsigned char *) &outpkt->seqno + outlen, &outpad)) {
214 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Error decrypting packet from %s (%s): %s"),
215 n->name, n->hostname, ERR_error_string(ERR_get_error(), NULL));
219 outpkt->len = outlen + outpad;
223 /* Check the sequence number */
225 inpkt->len -= sizeof(inpkt->seqno);
226 inpkt->seqno = ntohl(inpkt->seqno);
228 if(inpkt->seqno != n->received_seqno + 1) {
229 if(inpkt->seqno >= n->received_seqno + sizeof(n->late) * 8) {
230 logger(LOG_WARNING, _("Lost %d packets from %s (%s)"),
231 inpkt->seqno - n->received_seqno - 1, n->name, n->hostname);
233 memset(n->late, 0, sizeof(n->late));
234 } else if (inpkt->seqno <= n->received_seqno) {
235 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))) {
236 logger(LOG_WARNING, _("Got late or replayed packet from %s (%s), seqno %d, last received %d"),
237 n->name, n->hostname, inpkt->seqno, n->received_seqno);
241 for(i = n->received_seqno + 1; i < inpkt->seqno; i++)
242 n->late[(i / 8) % sizeof(n->late)] |= 1 << i % 8;
246 n->late[(inpkt->seqno / 8) % sizeof(n->late)] &= ~(1 << inpkt->seqno % 8);
248 if(inpkt->seqno > n->received_seqno)
249 n->received_seqno = inpkt->seqno;
251 if(n->received_seqno > MAX_SEQNO)
254 /* Decompress the packet */
256 if(myself->compression) {
257 outpkt = pkt[nextpkt++];
259 if((outpkt->len = uncompress_packet(outpkt->data, inpkt->data, inpkt->len, myself->compression)) < 0) {
260 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while uncompressing packet from %s (%s)"),
261 n->name, n->hostname);
271 n->connection->last_ping_time = now;
273 if(!inpkt->data[12] && !inpkt->data[13])
274 mtu_probe_h(n, inpkt);
276 receive_packet(n, inpkt);
279 void receive_tcppacket(connection_t *c, char *buffer, int len)
286 if(c->options & OPTION_TCPONLY)
289 outpkt.priority = -1;
290 memcpy(outpkt.data, buffer, len);
292 receive_packet(c->node, &outpkt);
295 static void send_udppacket(node_t *n, vpn_packet_t *origpkt)
297 vpn_packet_t pkt1, pkt2;
298 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
299 vpn_packet_t *inpkt = origpkt;
301 vpn_packet_t *outpkt;
304 static int priority = 0;
310 /* Make sure we have a valid key */
312 if(!n->status.validkey) {
313 ifdebug(TRAFFIC) logger(LOG_INFO,
314 _("No valid key known yet for %s (%s), forwarding via TCP"),
315 n->name, n->hostname);
317 if(!n->status.waitingforkey)
318 send_req_key(n->nexthop->connection, myself, n);
320 n->status.waitingforkey = true;
322 send_tcppacket(n->nexthop->connection, origpkt);
327 if(!n->minmtu && (inpkt->data[12] | inpkt->data[13])) {
328 ifdebug(TRAFFIC) logger(LOG_INFO,
329 _("No minimum MTU established yet for %s (%s), forwarding via TCP"),
330 n->name, n->hostname);
332 send_tcppacket(n->nexthop->connection, origpkt);
335 origlen = inpkt->len;
336 origpriority = inpkt->priority;
338 /* Compress the packet */
341 outpkt = pkt[nextpkt++];
343 if((outpkt->len = compress_packet(outpkt->data, inpkt->data, inpkt->len, n->compression)) < 0) {
344 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while compressing packet to %s (%s)"),
345 n->name, n->hostname);
352 /* Add sequence number */
354 inpkt->seqno = htonl(++(n->sent_seqno));
355 inpkt->len += sizeof(inpkt->seqno);
357 /* Encrypt the packet */
360 outpkt = pkt[nextpkt++];
362 if(!EVP_EncryptInit_ex(&n->packet_ctx, NULL, NULL, NULL, NULL)
363 || !EVP_EncryptUpdate(&n->packet_ctx, (unsigned char *) &outpkt->seqno, &outlen,
364 (unsigned char *) &inpkt->seqno, inpkt->len)
365 || !EVP_EncryptFinal_ex(&n->packet_ctx, (unsigned char *) &outpkt->seqno + outlen, &outpad)) {
366 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while encrypting packet to %s (%s): %s"),
367 n->name, n->hostname, ERR_error_string(ERR_get_error(), NULL));
371 outpkt->len = outlen + outpad;
375 /* Add the message authentication code */
377 if(n->digest && n->maclength) {
378 HMAC(n->digest, n->key, n->keylength, (unsigned char *) &inpkt->seqno,
379 inpkt->len, (unsigned char *) &inpkt->seqno + inpkt->len, NULL);
380 inpkt->len += n->maclength;
383 /* Determine which socket we have to use */
385 for(sock = 0; sock < listen_sockets; sock++)
386 if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family)
389 if(sock >= listen_sockets)
390 sock = 0; /* If none is available, just use the first and hope for the best. */
392 /* Send the packet */
394 #if defined(SOL_IP) && defined(IP_TOS)
395 if(priorityinheritance && origpriority != priority
396 && listen_socket[sock].sa.sa.sa_family == AF_INET) {
397 priority = origpriority;
398 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Setting outgoing packet priority to %d"), priority);
399 if(setsockopt(listen_socket[sock].udp, SOL_IP, IP_TOS, &priority, sizeof(priority))) /* SO_PRIORITY doesn't seem to work */
400 logger(LOG_ERR, _("System call `%s' failed: %s"), "setsockopt", strerror(errno));
404 if((sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0) {
405 if(errno == EMSGSIZE) {
406 if(n->maxmtu >= origlen)
407 n->maxmtu = origlen - 1;
408 if(n->mtu >= origlen)
409 n->mtu = origlen - 1;
411 logger(LOG_ERR, _("Error sending packet to %s (%s): %s"), n->name, n->hostname, strerror(errno));
415 origpkt->len = origlen;
419 send a packet to the given vpn ip.
421 void send_packet(const node_t *n, vpn_packet_t *packet)
429 memcpy(packet->data, mymac.x, ETH_ALEN);
430 write_packet(packet);
434 ifdebug(TRAFFIC) logger(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
435 packet->len, n->name, n->hostname);
437 if(!n->status.reachable) {
438 ifdebug(TRAFFIC) logger(LOG_INFO, _("Node %s (%s) is not reachable"),
439 n->name, n->hostname);
443 via = (packet->priority == -1 || n->via == myself) ? n->nexthop : n->via;
446 ifdebug(TRAFFIC) logger(LOG_INFO, _("Sending packet to %s via %s (%s)"),
447 n->name, via->name, n->via->hostname);
449 if(packet->priority == -1 || ((myself->options | via->options) & OPTION_TCPONLY)) {
450 if(!send_tcppacket(via->connection, packet))
451 terminate_connection(via->connection, true);
453 send_udppacket(via, packet);
456 /* Broadcast a packet using the minimum spanning tree */
458 void broadcast_packet(const node_t *from, vpn_packet_t *packet)
465 ifdebug(TRAFFIC) logger(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
466 packet->len, from->name, from->hostname);
469 send_packet(myself, packet);
471 for(node = connection_tree->head; node; node = node->next) {
474 if(c->status.active && c->status.mst && c != from->nexthop->connection)
475 send_packet(c->node, packet);
479 void handle_incoming_vpn_data(int sock)
484 socklen_t fromlen = sizeof(from);
489 pkt.len = recvfrom(sock, (char *) &pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen);
492 logger(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno));
496 sockaddrunmap(&from); /* Some braindead IPv6 implementations do stupid things. */
498 n = lookup_node_udp(&from);
501 hostname = sockaddr2hostname(&from);
502 logger(LOG_WARNING, _("Received UDP packet from unknown source %s"),
508 receive_udppacket(n, &pkt);