2 net_packet.c -- Handles in- and outgoing VPN packets
3 Copyright (C) 1998-2003 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2003 Guus Sliepen <guus@sliepen.eu.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.
20 $Id: net_packet.c,v 1.1.2.49 2003/12/27 16:32:52 guus Exp $
32 #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 void send_mtu_probe(node_t *n)
69 if(n->mtuprobes >= 10 && !n->minmtu) {
70 ifdebug(TRAFFIC) logger(LOG_INFO, _("No response to MTU probes from %s (%s)"), n->name, n->hostname);
74 for(i = 0; i < 3; i++) {
75 if(n->mtuprobes >= 30 || n->minmtu >= n->maxmtu) {
77 ifdebug(TRAFFIC) logger(LOG_INFO, _("Fixing MTU of %s (%s) to %d after %d probes"), n->name, n->hostname, n->mtu, n->mtuprobes);
81 len = n->minmtu + 1 + random() % (n->maxmtu - n->minmtu);
85 memset(packet.data, 0, 14);
86 gcry_randomize(packet.data + 14, len - 14, GCRY_WEAK_RANDOM);
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 n->mtuevent = xmalloc(sizeof(*n->mtuevent));
95 n->mtuevent->handler = (event_handler_t)send_mtu_probe;
96 n->mtuevent->data = n;
97 n->mtuevent->time = now + 1;
98 event_add(n->mtuevent);
101 void mtu_probe_h(node_t *n, vpn_packet_t *packet) {
102 ifdebug(TRAFFIC) logger(LOG_INFO, _("Got MTU probe length %d from %s (%s)"), packet->len, n->name, n->hostname);
104 if(!packet->data[0]) {
106 send_packet(n, packet);
108 if(n->minmtu < packet->len)
109 n->minmtu = packet->len;
113 static length_t compress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level)
116 lzo_uint lzolen = MAXSIZE;
117 lzo1x_1_compress(source, len, dest, &lzolen, lzo_wrkmem);
119 } else if(level < 10) {
120 unsigned long destlen = MAXSIZE;
121 if(compress2(dest, &destlen, source, len, level) == Z_OK)
126 lzo_uint lzolen = MAXSIZE;
127 lzo1x_999_compress(source, len, dest, &lzolen, lzo_wrkmem);
134 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)
159 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Received packet of %d bytes from %s (%s)"),
160 packet->len, n->name, n->hostname);
165 static void receive_udppacket(node_t *n, vpn_packet_t *inpkt)
167 vpn_packet_t pkt1, pkt2;
168 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
170 vpn_packet_t *outpkt = pkt[0];
174 static char iv[32] = {0};
178 /* Check packet length */
180 if(inpkt->len < sizeof(inpkt->seqno) + myself->maclength) {
181 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got too short packet from %s (%s)"),
182 n->name, n->hostname);
186 /* Check the message authentication code */
188 if(myself->digest && myself->maclength) {
189 inpkt->len -= myself->maclength;
190 gcry_md_reset(myself->digest_ctx);
191 gcry_md_write(myself->digest_ctx, (char *)&inpkt->seqno, inpkt->len);
192 hmac = gcry_md_read(myself->digest_ctx, 0);
194 if(!hmac || memcmp(hmac, (char *)&inpkt->seqno + inpkt->len, myself->maclength)) {
195 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got unauthenticated packet from %s (%s)"),
196 n->name, n->hostname);
201 /* Decrypt the packet */
204 gcry_cipher_reset(myself->cipher_ctx);
205 //memcpy(iv, &inpkt->seqno, sizeof inpkt->seqno);
206 //gcry_cipher_setiv(myself->cipher_ctx, iv, myself->cipherblklen);
207 result = gcry_cipher_decrypt(myself->cipher_ctx, (char *)&inpkt->seqno, inpkt->len, NULL, 0);
210 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Error decrypting packet from %s (%s): %s"),
211 n->name, n->hostname, gpg_strerror(result));
212 logger(LOG_DEBUG, "%p %p %d", myself->cipher_ctx, (char *)&inpkt->seqno, inpkt->len);
219 if(myself->cipherblklen > 1) {
222 padlen = ((uint8_t *)&inpkt->seqno)[inpkt->len - 1];
224 if(padlen && padlen <= myself->cipherblklen)
225 inpkt->len -= padlen;
228 /* Check the sequence number */
230 inpkt->len -= sizeof(inpkt->seqno);
231 inpkt->seqno = ntohl(inpkt->seqno);
233 if(inpkt->seqno != n->received_seqno + 1) {
234 if(inpkt->seqno >= n->received_seqno + sizeof(n->late) * 8) {
235 logger(LOG_WARNING, _("Lost %u packets from %s (%s)"),
236 inpkt->seqno - n->received_seqno - 1, n->name, n->hostname);
238 memset(n->late, 0, sizeof(n->late));
239 } else if (inpkt->seqno <= n->received_seqno) {
240 if(inpkt->seqno <= n->received_seqno - sizeof(n->late) * 8 || !(n->late[(inpkt->seqno / 8) % sizeof(n->late)] & (1 << inpkt->seqno % 8))) {
241 logger(LOG_WARNING, _("Got late or replayed packet from %s (%s), seqno %u, last received %u"),
242 n->name, n->hostname, inpkt->seqno, n->received_seqno);
244 for(i = n->received_seqno + 1; i < inpkt->seqno; i++)
245 n->late[(inpkt->seqno / 8) % sizeof(n->late)] |= 1 << i % 8;
249 if(inpkt->seqno > n->received_seqno) {
250 n->received_seqno = inpkt->seqno;
251 n->late[(n->received_seqno / 8) % sizeof(n->late)] &= ~(1 << n->received_seqno % 8);
254 if(n->received_seqno > MAX_SEQNO)
257 /* Decompress the packet */
259 if(myself->compression) {
260 outpkt = pkt[nextpkt++];
262 if((outpkt->len = uncompress_packet(outpkt->data, inpkt->data, inpkt->len, myself->compression)) < 0) {
263 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while uncompressing packet from %s (%s)"),
264 n->name, n->hostname);
272 n->connection->last_ping_time = now;
274 if(!inpkt->data[12] && !inpkt->data[13])
275 mtu_probe_h(n, inpkt);
277 receive_packet(n, inpkt);
280 void receive_tcppacket(connection_t *c, char *buffer, int len)
287 memcpy(outpkt.data, buffer, len);
289 receive_packet(c->node, &outpkt);
292 static void send_udppacket(node_t *n, vpn_packet_t *inpkt)
294 vpn_packet_t pkt1, pkt2;
295 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
297 vpn_packet_t *outpkt;
301 static int priority = 0;
305 static char iv[32] = {0};
309 /* Make sure we have a valid key */
311 if(!n->status.validkey) {
312 ifdebug(TRAFFIC) logger(LOG_INFO,
313 _("No valid key known yet for %s (%s), queueing packet"),
314 n->name, n->hostname);
316 /* Since packet is on the stack of handle_tap_input(), we have to make a copy of it first. */
318 *(copy = xmalloc(sizeof(*copy))) = *inpkt;
320 list_insert_tail(n->queue, copy);
322 if(n->queue->count > MAXQUEUELENGTH)
323 list_delete_head(n->queue);
325 if(!n->status.waitingforkey)
326 send_req_key(n->nexthop->connection, myself, n);
328 n->status.waitingforkey = true;
333 origlen = inpkt->len;
334 origpriority = inpkt->priority;
336 /* Compress the packet */
339 outpkt = pkt[nextpkt++];
341 if((outpkt->len = compress_packet(outpkt->data, inpkt->data, inpkt->len, n->compression)) < 0) {
342 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while compressing packet to %s (%s)"),
343 n->name, n->hostname);
350 /* Add sequence number */
352 inpkt->seqno = htonl(++(n->sent_seqno));
353 inpkt->len += sizeof(inpkt->seqno);
355 /* Encrypt the packet */
360 if(n->cipherblklen > 1) {
364 padlen = n->cipherblklen - inpkt->len % n->cipherblklen;
365 p = (char *)&inpkt->seqno + inpkt->len;
367 if(padlen == n->cipherblklen) {
368 if(p[-1] != 0 && p[-1] <= n->cipherblklen) {
369 inpkt->len += n->cipherblklen;
370 for(i = 0; i < n->cipherblklen; i++)
374 inpkt->len += padlen;
375 for(i = 0; i < padlen; i++)
380 gcry_cipher_reset(n->cipher_ctx);
381 //memcpy(iv, &inpkt->seqno, sizeof inpkt->seqno);
382 //gcry_cipher_setiv(n->cipher_ctx, iv, n->cipherblklen);
383 result = gcry_cipher_encrypt(n->cipher_ctx, (char *)&inpkt->seqno, inpkt->len, NULL, 0);
386 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while encrypting packet to %s (%s): %s"),
387 n->name, n->hostname, gpg_strerror(result));
392 /* Add the message authentication code */
394 if(n->digest && n->maclength) {
395 gcry_md_reset(n->digest_ctx);
396 gcry_md_write(n->digest_ctx, (char *)&inpkt->seqno, inpkt->len);
397 hmac = gcry_md_read(n->digest_ctx, 0);
400 ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while authenticating packet to %s (%s)"),
401 n->name, n->hostname);
405 memcpy((char *)&inpkt->seqno + inpkt->len, hmac, n->maclength);
406 inpkt->len += n->maclength;
409 /* Determine which socket we have to use */
411 for(sock = 0; sock < listen_sockets; sock++)
412 if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family)
415 if(sock >= listen_sockets)
416 sock = 0; /* If none is available, just use the first and hope for the best. */
418 /* Send the packet */
420 #if defined(SOL_IP) && defined(IP_TOS)
421 if(priorityinheritance && origpriority != priority
422 && listen_socket[sock].sa.sa.sa_family == AF_INET) {
423 priority = origpriority;
424 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Setting outgoing packet priority to %d"), priority);
425 if(setsockopt(listen_socket[sock].udp, SOL_IP, IP_TOS, &priority, sizeof(priority))) /* SO_PRIORITY doesn't seem to work */
426 logger(LOG_ERR, _("System call `%s' failed: %s"), "setsockopt", strerror(errno));
430 if((sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0) {
431 if(errno == EMSGSIZE) {
432 if(n->maxmtu >= origlen)
433 n->maxmtu = origlen - 1;
434 if(n->mtu >= origlen)
435 n->mtu = origlen - 1;
437 logger(LOG_ERR, _("Error sending packet to %s (%s): %s"), n->name, n->hostname, strerror(errno));
441 inpkt->len = origlen;
445 send a packet to the given vpn ip.
447 void send_packet(const node_t *n, vpn_packet_t *packet)
455 memcpy(packet->data, mymac.x, ETH_ALEN);
456 write_packet(packet);
460 ifdebug(TRAFFIC) logger(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
461 packet->len, n->name, n->hostname);
463 if(!n->status.reachable) {
464 ifdebug(TRAFFIC) logger(LOG_INFO, _("Node %s (%s) is not reachable"),
465 n->name, n->hostname);
469 via = (n->via == myself) ? n->nexthop : n->via;
472 ifdebug(TRAFFIC) logger(LOG_ERR, _("Sending packet to %s via %s (%s)"),
473 n->name, via->name, n->via->hostname);
475 if((myself->options | via->options) & OPTION_TCPONLY) {
476 if(!send_tcppacket(via->connection, packet))
477 terminate_connection(via->connection, true);
479 send_udppacket(via, packet);
482 /* Broadcast a packet using the minimum spanning tree */
484 void broadcast_packet(const node_t *from, vpn_packet_t *packet)
491 ifdebug(TRAFFIC) logger(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
492 packet->len, from->name, from->hostname);
494 for(node = connection_tree->head; node; node = node->next) {
497 if(c->status.active && c->status.mst && c != from->nexthop->connection)
498 send_packet(c->node, packet);
502 void flush_queue(node_t *n)
504 list_node_t *node, *next;
508 ifdebug(TRAFFIC) logger(LOG_INFO, _("Flushing queue for %s (%s)"), n->name, n->hostname);
510 for(node = n->queue->head; node; node = next) {
512 send_udppacket(n, node->data);
513 list_delete_node(n->queue, node);
517 void handle_incoming_vpn_data(int sock)
522 socklen_t fromlen = sizeof(from);
527 pkt.len = recvfrom(sock, (char *) &pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen);
530 logger(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno));
534 sockaddrunmap(&from); /* Some braindead IPv6 implementations do stupid things. */
536 n = lookup_node_udp(&from);
539 hostname = sockaddr2hostname(&from);
540 logger(LOG_WARNING, _("Received UDP packet from unknown source %s"),
546 receive_udppacket(n, &pkt);