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.
28 #include "splay_tree.h"
31 #include "connection.h"
48 #define EMSGSIZE WSAEMSGSIZE
53 static char lzo_wrkmem[LZO1X_999_MEM_COMPRESS > LZO1X_1_MEM_COMPRESS ? LZO1X_999_MEM_COMPRESS : LZO1X_1_MEM_COMPRESS];
55 static void send_udppacket(node_t *, vpn_packet_t *);
57 #define MAX_SEQNO 1073741824
59 static void send_mtu_probe_handler(int fd, short events, void *data) {
68 if(!n->status.reachable) {
69 logger(LOG_DEBUG, _("Trying to send MTU probe to unreachable node %s (%s)"), n->name, n->hostname);
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 + rand() % (n->maxmtu - n->minmtu);
89 memset(packet.data, 0, 14);
90 randomize(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 event_add(&n->mtuevent, &(struct timeval){1, 0});
102 void send_mtu_probe(node_t *n) {
103 if(!timeout_initialized(&n->mtuevent))
104 timeout_set(&n->mtuevent, send_mtu_probe_handler, n);
105 send_mtu_probe_handler(0, 0, n);
108 void mtu_probe_h(node_t *n, vpn_packet_t *packet, length_t len) {
109 ifdebug(TRAFFIC) logger(LOG_INFO, _("Got MTU probe length %d from %s (%s)"), packet->len, n->name, n->hostname);
111 if(!packet->data[0]) {
113 send_packet(n, packet);
120 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) {
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) {
163 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Received packet of %d bytes from %s (%s)"),
164 packet->len, n->name, n->hostname);
169 static bool try_mac(node_t *n, const vpn_packet_t *inpkt)
171 if(!digest_active(&n->indigest) || inpkt->len < sizeof inpkt->seqno + digest_length(&n->indigest))
174 return digest_verify(&n->indigest, &inpkt->seqno, inpkt->len, (const char *)&inpkt->seqno + inpkt->len);
177 static void receive_udppacket(node_t *n, vpn_packet_t *inpkt)
179 vpn_packet_t pkt1, pkt2;
180 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
182 vpn_packet_t *outpkt = pkt[0];
188 if(!cipher_active(&n->incipher)) {
189 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got packet from %s (%s) but he hasn't got our key yet"),
190 n->name, n->hostname);
194 /* Check packet length */
196 if(inpkt->len < sizeof inpkt->seqno + digest_length(&n->indigest)) {
197 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got too short packet from %s (%s)"),
198 n->name, n->hostname);
202 /* Check the message authentication code */
204 if(digest_active(&n->indigest) && !digest_verify(&n->indigest, &inpkt->seqno, inpkt->len, (const char *)&inpkt->seqno + inpkt->len)) {
205 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got unauthenticated packet from %s (%s)"), n->name, n->hostname);
209 /* Decrypt the packet */
211 if(cipher_active(&n->incipher)) {
212 outpkt = pkt[nextpkt++];
215 if(!cipher_decrypt(&n->incipher, &inpkt->seqno, inpkt->len, &outpkt->seqno, &outlen, true)) {
216 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Error decrypting packet from %s (%s)"), n->name, n->hostname);
220 outpkt->len = outlen;
224 /* Check the sequence number */
226 inpkt->len -= sizeof inpkt->seqno;
227 inpkt->seqno = ntohl(inpkt->seqno);
229 if(inpkt->seqno != n->received_seqno + 1) {
230 if(inpkt->seqno >= n->received_seqno + sizeof n->late * 8) {
231 logger(LOG_WARNING, _("Lost %d packets from %s (%s)"),
232 inpkt->seqno - n->received_seqno - 1, n->name, n->hostname);
234 memset(n->late, 0, sizeof n->late);
235 } else if (inpkt->seqno <= n->received_seqno) {
236 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))) {
237 logger(LOG_WARNING, _("Got late or replayed packet from %s (%s), seqno %d, last received %d"),
238 n->name, n->hostname, inpkt->seqno, n->received_seqno);
242 for(i = n->received_seqno + 1; i < inpkt->seqno; i++)
243 n->late[(i / 8) % sizeof n->late] |= 1 << i % 8;
247 n->late[(inpkt->seqno / 8) % sizeof n->late] &= ~(1 << inpkt->seqno % 8);
249 if(inpkt->seqno > n->received_seqno)
250 n->received_seqno = inpkt->seqno;
252 if(n->received_seqno > MAX_SEQNO)
255 /* Decompress the packet */
257 length_t origlen = inpkt->len;
259 if(n->incompression) {
260 outpkt = pkt[nextpkt++];
262 if((outpkt->len = uncompress_packet(outpkt->data, inpkt->data, inpkt->len, n->incompression)) < 0) {
263 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while uncompressing packet from %s (%s)"),
264 n->name, n->hostname);
270 origlen -= MTU/64 + 20;
275 if(!inpkt->data[12] && !inpkt->data[13])
276 mtu_probe_h(n, inpkt, origlen);
278 receive_packet(n, inpkt);
281 void receive_tcppacket(connection_t *c, char *buffer, int len) {
287 if(c->options & OPTION_TCPONLY)
290 outpkt.priority = -1;
291 memcpy(outpkt.data, buffer, len);
293 receive_packet(c->node, &outpkt);
296 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 if(!n->status.reachable) {
311 ifdebug(TRAFFIC) logger(LOG_INFO, _("Trying to send UDP packet to unreachable node %s (%s)"), n->name, n->hostname);
315 /* Make sure we have a valid key */
317 if(!n->status.validkey) {
318 ifdebug(TRAFFIC) logger(LOG_INFO,
319 _("No valid key known yet for %s (%s), forwarding via TCP"),
320 n->name, n->hostname);
322 if(!n->status.waitingforkey)
325 n->status.waitingforkey = true;
327 send_tcppacket(n->nexthop->connection, origpkt);
332 if(n->options & OPTION_PMTU_DISCOVERY && inpkt->len > n->minmtu && (inpkt->data[12] | inpkt->data[13])) {
333 ifdebug(TRAFFIC) logger(LOG_INFO,
334 _("Packet for %s (%s) larger than minimum MTU, forwarding via TCP"),
335 n->name, n->hostname);
337 send_tcppacket(n->nexthop->connection, origpkt);
342 origlen = inpkt->len;
343 origpriority = inpkt->priority;
345 /* Compress the packet */
347 if(n->outcompression) {
348 outpkt = pkt[nextpkt++];
350 if((outpkt->len = compress_packet(outpkt->data, inpkt->data, inpkt->len, n->outcompression)) < 0) {
351 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while compressing packet to %s (%s)"),
352 n->name, n->hostname);
359 /* Add sequence number */
361 inpkt->seqno = htonl(++(n->sent_seqno));
362 inpkt->len += sizeof inpkt->seqno;
364 /* Encrypt the packet */
366 if(cipher_active(&n->outcipher)) {
367 outpkt = pkt[nextpkt++];
370 if(!cipher_encrypt(&n->outcipher, &inpkt->seqno, inpkt->len, &outpkt->seqno, &outlen, true)) {
371 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while encrypting packet to %s (%s)"), n->name, n->hostname);
375 outpkt->len = outlen;
379 /* Add the message authentication code */
381 if(digest_active(&n->outdigest)) {
382 digest_create(&n->outdigest, &inpkt->seqno, inpkt->len, (char *)&inpkt->seqno + inpkt->len);
383 inpkt->len += digest_length(&n->outdigest);
386 /* Determine which socket we have to use */
388 for(sock = 0; sock < listen_sockets; sock++)
389 if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family)
392 if(sock >= listen_sockets)
393 sock = 0; /* If none is available, just use the first and hope for the best. */
395 /* Send the packet */
397 #if defined(SOL_IP) && defined(IP_TOS)
398 if(priorityinheritance && origpriority != priority
399 && listen_socket[sock].sa.sa.sa_family == AF_INET) {
400 priority = origpriority;
401 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Setting outgoing packet priority to %d"), priority);
402 if(setsockopt(listen_socket[sock].udp, SOL_IP, IP_TOS, &priority, sizeof priority)) /* SO_PRIORITY doesn't seem to work */
403 logger(LOG_ERR, _("System call `%s' failed: %s"), "setsockopt", strerror(errno));
407 if((sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0) {
408 if(errno == EMSGSIZE) {
409 if(n->maxmtu >= origlen)
410 n->maxmtu = origlen - 1;
411 if(n->mtu >= origlen)
412 n->mtu = origlen - 1;
414 logger(LOG_ERR, _("Error sending packet to %s (%s): %s"), n->name, n->hostname, strerror(errno));
418 origpkt->len = origlen;
422 send a packet to the given vpn ip.
424 void send_packet(const node_t *n, vpn_packet_t *packet) {
431 memcpy(packet->data, mymac.x, ETH_ALEN);
432 write_packet(packet);
436 ifdebug(TRAFFIC) logger(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
437 packet->len, n->name, n->hostname);
439 if(!n->status.reachable) {
440 ifdebug(TRAFFIC) logger(LOG_INFO, _("Node %s (%s) is not reachable"),
441 n->name, n->hostname);
445 via = (packet->priority == -1 || n->via == myself) ? n->nexthop : n->via;
448 ifdebug(TRAFFIC) logger(LOG_INFO, _("Sending packet to %s via %s (%s)"),
449 n->name, via->name, n->via->hostname);
451 if(packet->priority == -1 || ((myself->options | via->options) & OPTION_TCPONLY)) {
452 if(!send_tcppacket(via->connection, packet))
453 terminate_connection(via->connection, true);
455 send_udppacket(via, packet);
458 /* Broadcast a packet using the minimum spanning tree */
460 void broadcast_packet(const node_t *from, vpn_packet_t *packet) {
466 ifdebug(TRAFFIC) logger(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
467 packet->len, from->name, from->hostname);
470 send_packet(myself, packet);
472 // In TunnelServer mode, do not forward broadcast packets.
473 // The MST might not be valid and create loops.
478 for(node = connection_tree->head; node; node = node->next) {
481 if(c->status.active && c->status.mst && c != from->nexthop->connection)
482 send_packet(c->node, packet);
486 static node_t *try_harder(const sockaddr_t *from, const vpn_packet_t *pkt) {
491 for(node = edge_weight_tree->head; node; node = node->next) {
494 if(sockaddrcmp_noport(from, &e->address))
500 if(!try_mac(e->to, pkt))
510 void handle_incoming_vpn_data(int sock, short events, void *data)
515 socklen_t fromlen = sizeof from;
520 pkt.len = recvfrom(sock, (char *) &pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen);
523 if(errno != EAGAIN && errno != EINTR)
524 logger(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno));
528 sockaddrunmap(&from); /* Some braindead IPv6 implementations do stupid things. */
530 n = lookup_node_udp(&from);
533 n = try_harder(&from, &pkt);
535 update_node_udp(n, &from);
536 else ifdebug(PROTOCOL) {
537 hostname = sockaddr2hostname(&from);
538 logger(LOG_WARNING, _("Received UDP packet from unknown source %s"), hostname);
546 receive_udppacket(n, &pkt);
549 void handle_device_data(int sock, short events, void *data) {
552 if(read_packet(&packet))
553 route(myself, &packet);