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->mtuprobes >= 10 && !n->minmtu) {
69 ifdebug(TRAFFIC) logger(LOG_INFO, _("No response to MTU probes from %s (%s)"), n->name, n->hostname);
73 for(i = 0; i < 3; i++) {
74 if(n->mtuprobes >= 30 || n->minmtu >= n->maxmtu) {
76 ifdebug(TRAFFIC) logger(LOG_INFO, _("Fixing MTU of %s (%s) to %d after %d probes"), n->name, n->hostname, n->mtu, n->mtuprobes);
80 len = n->minmtu + 1 + random() % (n->maxmtu - n->minmtu);
84 memset(packet.data, 0, 14);
85 randomize(packet.data + 14, len - 14);
89 ifdebug(TRAFFIC) logger(LOG_INFO, _("Sending MTU probe length %d to %s (%s)"), len, n->name, n->hostname);
91 send_udppacket(n, &packet);
94 event_add(&n->mtuevent, &(struct timeval){1, 0});
97 void send_mtu_probe(node_t *n) {
98 if(!timeout_initialized(&n->mtuevent))
99 timeout_set(&n->mtuevent, send_mtu_probe_handler, n);
100 send_mtu_probe_handler(0, 0, n);
103 void mtu_probe_h(node_t *n, vpn_packet_t *packet, length_t len) {
104 ifdebug(TRAFFIC) logger(LOG_INFO, _("Got MTU probe length %d from %s (%s)"), packet->len, n->name, n->hostname);
106 if(!packet->data[0]) {
108 send_packet(n, packet);
115 static length_t compress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
117 lzo_uint lzolen = MAXSIZE;
118 lzo1x_1_compress(source, len, dest, &lzolen, lzo_wrkmem);
120 } else if(level < 10) {
121 unsigned long destlen = MAXSIZE;
122 if(compress2(dest, &destlen, source, len, level) == Z_OK)
127 lzo_uint lzolen = MAXSIZE;
128 lzo1x_999_compress(source, len, dest, &lzolen, lzo_wrkmem);
135 static length_t uncompress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
137 lzo_uint lzolen = MAXSIZE;
138 if(lzo1x_decompress_safe(source, len, dest, &lzolen, NULL) == LZO_E_OK)
143 unsigned long destlen = MAXSIZE;
144 if(uncompress(dest, &destlen, source, len) == Z_OK)
155 static void receive_packet(node_t *n, vpn_packet_t *packet) {
158 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Received packet of %d bytes from %s (%s)"),
159 packet->len, n->name, n->hostname);
164 static bool try_mac(node_t *n, const vpn_packet_t *inpkt)
166 if(!digest_active(&n->indigest) || !n->inmaclength || inpkt->len < sizeof inpkt->seqno + n->inmaclength)
169 return digest_verify(&n->indigest, &inpkt->seqno, inpkt->len, &inpkt->seqno + inpkt->len);
172 static void receive_udppacket(node_t *n, vpn_packet_t *inpkt)
174 vpn_packet_t pkt1, pkt2;
175 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
177 vpn_packet_t *outpkt = pkt[0];
183 if(!cipher_active(&n->incipher)) {
184 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got packet from %s (%s) but he hasn't got our key yet"),
185 n->name, n->hostname);
189 /* Check packet length */
191 if(inpkt->len < sizeof inpkt->seqno + digest_length(&n->indigest)) {
192 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got too short packet from %s (%s)"),
193 n->name, n->hostname);
197 /* Check the message authentication code */
199 if(digest_active(&n->indigest) && !digest_verify(&n->indigest, &inpkt->seqno, inpkt->len, &inpkt->seqno + inpkt->len)) {
200 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got unauthenticated packet from %s (%s)"), n->name, n->hostname);
204 /* Decrypt the packet */
206 if(cipher_active(&n->incipher)) {
207 outpkt = pkt[nextpkt++];
210 if(!cipher_decrypt(&n->incipher, &inpkt->seqno, inpkt->len, &outpkt->seqno, &outlen, true)) {
211 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Error decrypting packet from %s (%s)"), n->name, n->hostname);
215 outpkt->len = outlen;
219 /* Check the sequence number */
221 inpkt->len -= sizeof inpkt->seqno;
222 inpkt->seqno = ntohl(inpkt->seqno);
224 if(inpkt->seqno != n->received_seqno + 1) {
225 if(inpkt->seqno >= n->received_seqno + sizeof n->late * 8) {
226 logger(LOG_WARNING, _("Lost %d packets from %s (%s)"),
227 inpkt->seqno - n->received_seqno - 1, n->name, n->hostname);
229 memset(n->late, 0, sizeof n->late);
230 } else if (inpkt->seqno <= n->received_seqno) {
231 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))) {
232 logger(LOG_WARNING, _("Got late or replayed packet from %s (%s), seqno %d, last received %d"),
233 n->name, n->hostname, inpkt->seqno, n->received_seqno);
237 for(i = n->received_seqno + 1; i < inpkt->seqno; i++)
238 n->late[(i / 8) % sizeof n->late] |= 1 << i % 8;
242 n->late[(inpkt->seqno / 8) % sizeof n->late] &= ~(1 << inpkt->seqno % 8);
244 if(inpkt->seqno > n->received_seqno)
245 n->received_seqno = inpkt->seqno;
247 if(n->received_seqno > MAX_SEQNO)
250 /* Decompress the packet */
252 length_t origlen = inpkt->len;
254 if(n->incompression) {
255 outpkt = pkt[nextpkt++];
257 if((outpkt->len = uncompress_packet(outpkt->data, inpkt->data, inpkt->len, n->incompression)) < 0) {
258 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while uncompressing packet from %s (%s)"),
259 n->name, n->hostname);
265 origlen -= MTU/64 + 20;
270 if(!inpkt->data[12] && !inpkt->data[13])
271 mtu_probe_h(n, inpkt, origlen);
273 receive_packet(n, inpkt);
276 void receive_tcppacket(connection_t *c, char *buffer, int len) {
282 if(c->options & OPTION_TCPONLY)
285 outpkt.priority = -1;
286 memcpy(outpkt.data, buffer, len);
288 receive_packet(c->node, &outpkt);
291 static void send_udppacket(node_t *n, vpn_packet_t *origpkt) {
292 vpn_packet_t pkt1, pkt2;
293 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
294 vpn_packet_t *inpkt = origpkt;
296 vpn_packet_t *outpkt;
299 static int priority = 0;
305 /* Make sure we have a valid key */
307 if(!n->status.validkey) {
308 ifdebug(TRAFFIC) logger(LOG_INFO,
309 _("No valid key known yet for %s (%s), forwarding via TCP"),
310 n->name, n->hostname);
312 if(!n->status.waitingforkey)
315 n->status.waitingforkey = true;
317 send_tcppacket(n->nexthop->connection, origpkt);
322 if(n->options & OPTION_PMTU_DISCOVERY && !n->minmtu && (inpkt->data[12] | inpkt->data[13])) {
323 ifdebug(TRAFFIC) logger(LOG_INFO,
324 _("No minimum MTU established yet for %s (%s), forwarding via TCP"),
325 n->name, n->hostname);
327 send_tcppacket(n->nexthop->connection, origpkt);
332 origlen = inpkt->len;
333 origpriority = inpkt->priority;
335 /* Compress the packet */
337 if(n->outcompression) {
338 outpkt = pkt[nextpkt++];
340 if((outpkt->len = compress_packet(outpkt->data, inpkt->data, inpkt->len, n->outcompression)) < 0) {
341 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while compressing packet to %s (%s)"),
342 n->name, n->hostname);
349 /* Add sequence number */
351 inpkt->seqno = htonl(++(n->sent_seqno));
352 inpkt->len += sizeof inpkt->seqno;
354 /* Encrypt the packet */
356 if(cipher_active(&n->outcipher)) {
357 outpkt = pkt[nextpkt++];
360 if(!cipher_encrypt(&n->outcipher, &inpkt->seqno, inpkt->len, &outpkt->seqno, &outlen, true)) {
361 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while encrypting packet to %s (%s)"), n->name, n->hostname);
365 outpkt->len = outlen;
369 /* Add the message authentication code */
371 if(digest_active(&n->outdigest)) {
372 digest_create(&n->outdigest, &inpkt->seqno, inpkt->len, &inpkt->seqno + inpkt->len);
373 inpkt->len += digest_length(&n->outdigest);
376 /* Determine which socket we have to use */
378 for(sock = 0; sock < listen_sockets; sock++)
379 if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family)
382 if(sock >= listen_sockets)
383 sock = 0; /* If none is available, just use the first and hope for the best. */
385 /* Send the packet */
387 #if defined(SOL_IP) && defined(IP_TOS)
388 if(priorityinheritance && origpriority != priority
389 && listen_socket[sock].sa.sa.sa_family == AF_INET) {
390 priority = origpriority;
391 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Setting outgoing packet priority to %d"), priority);
392 if(setsockopt(listen_socket[sock].udp, SOL_IP, IP_TOS, &priority, sizeof priority)) /* SO_PRIORITY doesn't seem to work */
393 logger(LOG_ERR, _("System call `%s' failed: %s"), "setsockopt", strerror(errno));
397 if((sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0) {
398 if(errno == EMSGSIZE) {
399 if(n->maxmtu >= origlen)
400 n->maxmtu = origlen - 1;
401 if(n->mtu >= origlen)
402 n->mtu = origlen - 1;
404 logger(LOG_ERR, _("Error sending packet to %s (%s): %s"), n->name, n->hostname, strerror(errno));
408 origpkt->len = origlen;
412 send a packet to the given vpn ip.
414 void send_packet(const node_t *n, vpn_packet_t *packet) {
421 memcpy(packet->data, mymac.x, ETH_ALEN);
422 write_packet(packet);
426 ifdebug(TRAFFIC) logger(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
427 packet->len, n->name, n->hostname);
429 if(!n->status.reachable) {
430 ifdebug(TRAFFIC) logger(LOG_INFO, _("Node %s (%s) is not reachable"),
431 n->name, n->hostname);
435 via = (packet->priority == -1 || n->via == myself) ? n->nexthop : n->via;
438 ifdebug(TRAFFIC) logger(LOG_INFO, _("Sending packet to %s via %s (%s)"),
439 n->name, via->name, n->via->hostname);
441 if(packet->priority == -1 || ((myself->options | via->options) & OPTION_TCPONLY)) {
442 if(!send_tcppacket(via->connection, packet))
443 terminate_connection(via->connection, true);
445 send_udppacket(via, packet);
448 /* Broadcast a packet using the minimum spanning tree */
450 void broadcast_packet(const node_t *from, vpn_packet_t *packet) {
456 ifdebug(TRAFFIC) logger(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
457 packet->len, from->name, from->hostname);
460 send_packet(myself, packet);
462 // In TunnelServer mode, do not forward broadcast packets.
463 // The MST might not be valid and create loops.
468 for(node = connection_tree->head; node; node = node->next) {
471 if(c->status.active && c->status.mst && c != from->nexthop->connection)
472 send_packet(c->node, packet);
476 static node_t *try_harder(const sockaddr_t *from, const vpn_packet_t *pkt) {
481 for(node = edge_weight_tree->head; node; node = node->next) {
484 if(sockaddrcmp_noport(from, &e->address))
490 if(!try_mac(e->to, pkt))
500 void handle_incoming_vpn_data(int sock, short events, void *data)
505 socklen_t fromlen = sizeof from;
510 pkt.len = recvfrom(sock, (char *) &pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen);
513 logger(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno));
517 sockaddrunmap(&from); /* Some braindead IPv6 implementations do stupid things. */
519 n = lookup_node_udp(&from);
522 n = try_harder(&from, &pkt);
524 update_node_udp(n, &from);
525 else ifdebug(PROTOCOL) {
526 hostname = sockaddr2hostname(&from);
527 logger(LOG_WARNING, _("Received UDP packet from unknown source %s"), hostname);
535 receive_udppacket(n, &pkt);
538 void handle_device_data(int sock, short events, void *data) {
541 if(read_packet(&packet))
542 route(myself, &packet);