Make maxmtu equal to minmtu when fixing the path MTU to a node.
[oweals/tinc.git] / src / net_packet.c
1 /*
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>
5
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.
10
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.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include <openssl/rand.h>
24 #include <openssl/err.h>
25 #include <openssl/evp.h>
26 #include <openssl/pem.h>
27 #include <openssl/hmac.h>
28
29 #include <zlib.h>
30 #include LZO1X_H
31
32 #include "avl_tree.h"
33 #include "conf.h"
34 #include "connection.h"
35 #include "device.h"
36 #include "ethernet.h"
37 #include "event.h"
38 #include "graph.h"
39 #include "list.h"
40 #include "logger.h"
41 #include "net.h"
42 #include "netutl.h"
43 #include "protocol.h"
44 #include "process.h"
45 #include "route.h"
46 #include "utils.h"
47 #include "xalloc.h"
48
49 #ifdef WSAEMSGSIZE
50 #define EMSGSIZE WSAEMSGSIZE
51 #endif
52
53 int keylifetime = 0;
54 int keyexpires = 0;
55 static char lzo_wrkmem[LZO1X_999_MEM_COMPRESS > LZO1X_1_MEM_COMPRESS ? LZO1X_999_MEM_COMPRESS : LZO1X_1_MEM_COMPRESS];
56
57 static void send_udppacket(node_t *, vpn_packet_t *);
58
59 #define MAX_SEQNO 1073741824
60
61 // mtuprobes == 1..30: initial discovery, send bursts with 1 second interval
62 // mtuprobes ==    31: sleep pinginterval seconds
63 // mtuprobes ==    32: send 1 burst, sleep pingtimeout second
64 // mtuprobes ==    33: no response from other side, restart PMTU discovery process
65
66 void send_mtu_probe(node_t *n) {
67         vpn_packet_t packet;
68         int len, i;
69         int timeout = 1;
70         
71         n->mtuprobes++;
72         n->mtuevent = NULL;
73
74         if(!n->status.reachable || !n->status.validkey) {
75                 ifdebug(TRAFFIC) logger(LOG_INFO, "Trying to send MTU probe to unreachable or rekeying node %s (%s)", n->name, n->hostname);
76                 n->mtuprobes = 0;
77                 return;
78         }
79
80         if(n->mtuprobes > 32) {
81                 ifdebug(TRAFFIC) logger(LOG_INFO, "%s (%s) did not respond to UDP ping, restarting PMTU discovery", n->name, n->hostname);
82                 n->mtuprobes = 1;
83                 n->minmtu = 0;
84                 n->maxmtu = MTU;
85         }
86
87         if(n->mtuprobes >= 10 && !n->minmtu) {
88                 ifdebug(TRAFFIC) logger(LOG_INFO, "No response to MTU probes from %s (%s)", n->name, n->hostname);
89                 n->mtuprobes = 0;
90                 return;
91         }
92
93         if(n->mtuprobes == 30 || (n->mtuprobes < 30 && n->minmtu >= n->maxmtu)) {
94                 if(n->minmtu > n->maxmtu)
95                         n->minmtu = n->maxmtu;
96                 else
97                         n->maxmtu = n->minmtu;
98                 n->mtu = n->minmtu;
99                 ifdebug(TRAFFIC) logger(LOG_INFO, "Fixing MTU of %s (%s) to %d after %d probes", n->name, n->hostname, n->mtu, n->mtuprobes);
100                 n->mtuprobes = 31;
101         }
102
103         if(n->mtuprobes == 31) {
104                 timeout = pinginterval;
105                 goto end;
106         } else if(n->mtuprobes == 32) {
107                 timeout = pingtimeout;
108         }
109
110         for(i = 0; i < 3; i++) {
111                 if(n->maxmtu <= n->minmtu)
112                         len = n->maxmtu;
113                 else
114                         len = n->minmtu + 1 + rand() % (n->maxmtu - n->minmtu);
115
116                 if(len < 64)
117                         len = 64;
118                 
119                 memset(packet.data, 0, 14);
120                 RAND_pseudo_bytes(packet.data + 14, len - 14);
121                 packet.len = len;
122                 packet.priority = 0;
123
124                 ifdebug(TRAFFIC) logger(LOG_INFO, "Sending MTU probe length %d to %s (%s)", len, n->name, n->hostname);
125
126                 send_udppacket(n, &packet);
127         }
128
129 end:
130         n->mtuevent = new_event();
131         n->mtuevent->handler = (event_handler_t)send_mtu_probe;
132         n->mtuevent->data = n;
133         n->mtuevent->time = now + timeout;
134         event_add(n->mtuevent);
135 }
136
137 void mtu_probe_h(node_t *n, vpn_packet_t *packet, length_t len) {
138         ifdebug(TRAFFIC) logger(LOG_INFO, "Got MTU probe length %d from %s (%s)", packet->len, n->name, n->hostname);
139
140         if(!packet->data[0]) {
141                 packet->data[0] = 1;
142                 send_udppacket(n, packet);
143         } else {
144                 if(len > n->maxmtu)
145                         len = n->maxmtu;
146                 if(n->minmtu < len)
147                         n->minmtu = len;
148                 if(n->mtuprobes > 30)
149                         n->mtuprobes = 30;
150         }
151 }
152
153 static length_t compress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
154         if(level == 10) {
155                 lzo_uint lzolen = MAXSIZE;
156                 lzo1x_1_compress(source, len, dest, &lzolen, lzo_wrkmem);
157                 return lzolen;
158         } else if(level < 10) {
159                 unsigned long destlen = MAXSIZE;
160                 if(compress2(dest, &destlen, source, len, level) == Z_OK)
161                         return destlen;
162                 else
163                         return -1;
164         } else {
165                 lzo_uint lzolen = MAXSIZE;
166                 lzo1x_999_compress(source, len, dest, &lzolen, lzo_wrkmem);
167                 return lzolen;
168         }
169         
170         return -1;
171 }
172
173 static length_t uncompress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
174         if(level > 9) {
175                 lzo_uint lzolen = MAXSIZE;
176                 if(lzo1x_decompress_safe(source, len, dest, &lzolen, NULL) == LZO_E_OK)
177                         return lzolen;
178                 else
179                         return -1;
180         } else {
181                 unsigned long destlen = MAXSIZE;
182                 if(uncompress(dest, &destlen, source, len) == Z_OK)
183                         return destlen;
184                 else
185                         return -1;
186         }
187         
188         return -1;
189 }
190
191 /* VPN packet I/O */
192
193 static void receive_packet(node_t *n, vpn_packet_t *packet) {
194         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Received packet of %d bytes from %s (%s)",
195                            packet->len, n->name, n->hostname);
196
197         route(n, packet);
198 }
199
200 static bool try_mac(const node_t *n, const vpn_packet_t *inpkt) {
201         unsigned char hmac[EVP_MAX_MD_SIZE];
202
203         if(!n->indigest || !n->inmaclength || !n->inkey || inpkt->len < sizeof inpkt->seqno + n->inmaclength)
204                 return false;
205
206         HMAC(n->indigest, n->inkey, n->inkeylength, (unsigned char *) &inpkt->seqno, inpkt->len - n->inmaclength, (unsigned char *)hmac, NULL);
207
208         return !memcmp(hmac, (char *) &inpkt->seqno + inpkt->len - n->inmaclength, n->inmaclength);
209 }
210
211 static void receive_udppacket(node_t *n, vpn_packet_t *inpkt) {
212         vpn_packet_t pkt1, pkt2;
213         vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
214         int nextpkt = 0;
215         vpn_packet_t *outpkt = pkt[0];
216         int outlen, outpad;
217         unsigned char hmac[EVP_MAX_MD_SIZE];
218         int i;
219
220         if(!n->inkey) {
221                 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Got packet from %s (%s) but he hasn't got our key yet",
222                                         n->name, n->hostname);
223                 return;
224         }
225
226         /* Check packet length */
227
228         if(inpkt->len < sizeof(inpkt->seqno) + n->inmaclength) {
229                 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Got too short packet from %s (%s)",
230                                         n->name, n->hostname);
231                 return;
232         }
233
234         /* Check the message authentication code */
235
236         if(n->indigest && n->inmaclength) {
237                 inpkt->len -= n->inmaclength;
238                 HMAC(n->indigest, n->inkey, n->inkeylength,
239                          (unsigned char *) &inpkt->seqno, inpkt->len, (unsigned char *)hmac, NULL);
240
241                 if(memcmp(hmac, (char *) &inpkt->seqno + inpkt->len, n->inmaclength)) {
242                         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Got unauthenticated packet from %s (%s)",
243                                            n->name, n->hostname);
244                         return;
245                 }
246         }
247
248         /* Decrypt the packet */
249
250         if(n->incipher) {
251                 outpkt = pkt[nextpkt++];
252
253                 if(!EVP_DecryptInit_ex(&n->inctx, NULL, NULL, NULL, NULL)
254                                 || !EVP_DecryptUpdate(&n->inctx, (unsigned char *) &outpkt->seqno, &outlen,
255                                         (unsigned char *) &inpkt->seqno, inpkt->len)
256                                 || !EVP_DecryptFinal_ex(&n->inctx, (unsigned char *) &outpkt->seqno + outlen, &outpad)) {
257                         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Error decrypting packet from %s (%s): %s",
258                                                 n->name, n->hostname, ERR_error_string(ERR_get_error(), NULL));
259                         return;
260                 }
261                 
262                 outpkt->len = outlen + outpad;
263                 inpkt = outpkt;
264         }
265
266         /* Check the sequence number */
267
268         inpkt->len -= sizeof(inpkt->seqno);
269         inpkt->seqno = ntohl(inpkt->seqno);
270
271         if(inpkt->seqno != n->received_seqno + 1) {
272                 if(inpkt->seqno >= n->received_seqno + sizeof(n->late) * 8) {
273                         logger(LOG_WARNING, "Lost %d packets from %s (%s)",
274                                            inpkt->seqno - n->received_seqno - 1, n->name, n->hostname);
275                         
276                         memset(n->late, 0, sizeof(n->late));
277                 } else if (inpkt->seqno <= n->received_seqno) {
278                         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))) {
279                                 logger(LOG_WARNING, "Got late or replayed packet from %s (%s), seqno %d, last received %d",
280                                            n->name, n->hostname, inpkt->seqno, n->received_seqno);
281                                 return;
282                         }
283                 } else {
284                         for(i = n->received_seqno + 1; i < inpkt->seqno; i++)
285                                 n->late[(i / 8) % sizeof(n->late)] |= 1 << i % 8;
286                 }
287         }
288         
289         n->late[(inpkt->seqno / 8) % sizeof(n->late)] &= ~(1 << inpkt->seqno % 8);
290
291         if(inpkt->seqno > n->received_seqno)
292                 n->received_seqno = inpkt->seqno;
293                         
294         if(n->received_seqno > MAX_SEQNO)
295                 keyexpires = 0;
296
297         /* Decompress the packet */
298
299         length_t origlen = inpkt->len;
300
301         if(n->incompression) {
302                 outpkt = pkt[nextpkt++];
303
304                 if((outpkt->len = uncompress_packet(outpkt->data, inpkt->data, inpkt->len, n->incompression)) < 0) {
305                         ifdebug(TRAFFIC) logger(LOG_ERR, "Error while uncompressing packet from %s (%s)",
306                                                  n->name, n->hostname);
307                         return;
308                 }
309
310                 inpkt = outpkt;
311
312                 origlen -= MTU/64 + 20;
313         }
314
315         inpkt->priority = 0;
316
317         if(!inpkt->data[12] && !inpkt->data[13])
318                 mtu_probe_h(n, inpkt, origlen);
319         else
320                 receive_packet(n, inpkt);
321 }
322
323 void receive_tcppacket(connection_t *c, char *buffer, int len) {
324         vpn_packet_t outpkt;
325
326         outpkt.len = len;
327         if(c->options & OPTION_TCPONLY)
328                 outpkt.priority = 0;
329         else
330                 outpkt.priority = -1;
331         memcpy(outpkt.data, buffer, len);
332
333         receive_packet(c->node, &outpkt);
334 }
335
336 static void send_udppacket(node_t *n, vpn_packet_t *origpkt) {
337         vpn_packet_t pkt1, pkt2;
338         vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
339         vpn_packet_t *inpkt = origpkt;
340         int nextpkt = 0;
341         vpn_packet_t *outpkt;
342         int origlen;
343         int outlen, outpad;
344         static int priority = 0;
345         int origpriority;
346         int sock;
347
348         if(!n->status.reachable) {
349                 ifdebug(TRAFFIC) logger(LOG_INFO, "Trying to send UDP packet to unreachable node %s (%s)", n->name, n->hostname);
350                 return;
351         }
352
353         /* Make sure we have a valid key */
354
355         if(!n->status.validkey) {
356                 ifdebug(TRAFFIC) logger(LOG_INFO,
357                                    "No valid key known yet for %s (%s), forwarding via TCP",
358                                    n->name, n->hostname);
359
360                 if(!n->status.waitingforkey)
361                         send_req_key(n);
362
363                 n->status.waitingforkey = true;
364
365                 send_tcppacket(n->nexthop->connection, origpkt);
366
367                 return;
368         }
369
370         if(n->options & OPTION_PMTU_DISCOVERY && inpkt->len > n->minmtu && (inpkt->data[12] | inpkt->data[13])) {
371                 ifdebug(TRAFFIC) logger(LOG_INFO,
372                                 "Packet for %s (%s) larger than minimum MTU, forwarding via TCP",
373                                 n->name, n->hostname);
374
375                 send_tcppacket(n->nexthop->connection, origpkt);
376
377                 return;
378         }
379
380         origlen = inpkt->len;
381         origpriority = inpkt->priority;
382
383         /* Compress the packet */
384
385         if(n->outcompression) {
386                 outpkt = pkt[nextpkt++];
387
388                 if((outpkt->len = compress_packet(outpkt->data, inpkt->data, inpkt->len, n->outcompression)) < 0) {
389                         ifdebug(TRAFFIC) logger(LOG_ERR, "Error while compressing packet to %s (%s)",
390                                    n->name, n->hostname);
391                         return;
392                 }
393
394                 inpkt = outpkt;
395         }
396
397         /* Add sequence number */
398
399         inpkt->seqno = htonl(++(n->sent_seqno));
400         inpkt->len += sizeof(inpkt->seqno);
401
402         /* Encrypt the packet */
403
404         if(n->outcipher) {
405                 outpkt = pkt[nextpkt++];
406
407                 if(!EVP_EncryptInit_ex(&n->outctx, NULL, NULL, NULL, NULL)
408                                 || !EVP_EncryptUpdate(&n->outctx, (unsigned char *) &outpkt->seqno, &outlen,
409                                         (unsigned char *) &inpkt->seqno, inpkt->len)
410                                 || !EVP_EncryptFinal_ex(&n->outctx, (unsigned char *) &outpkt->seqno + outlen, &outpad)) {
411                         ifdebug(TRAFFIC) logger(LOG_ERR, "Error while encrypting packet to %s (%s): %s",
412                                                 n->name, n->hostname, ERR_error_string(ERR_get_error(), NULL));
413                         goto end;
414                 }
415
416                 outpkt->len = outlen + outpad;
417                 inpkt = outpkt;
418         }
419
420         /* Add the message authentication code */
421
422         if(n->outdigest && n->outmaclength) {
423                 HMAC(n->outdigest, n->outkey, n->outkeylength, (unsigned char *) &inpkt->seqno,
424                          inpkt->len, (unsigned char *) &inpkt->seqno + inpkt->len, NULL);
425                 inpkt->len += n->outmaclength;
426         }
427
428         /* Determine which socket we have to use */
429
430         for(sock = 0; sock < listen_sockets; sock++)
431                 if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family)
432                         break;
433
434         if(sock >= listen_sockets)
435                 sock = 0;                               /* If none is available, just use the first and hope for the best. */
436
437         /* Send the packet */
438
439 #if defined(SOL_IP) && defined(IP_TOS)
440         if(priorityinheritance && origpriority != priority
441            && listen_socket[sock].sa.sa.sa_family == AF_INET) {
442                 priority = origpriority;
443                 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Setting outgoing packet priority to %d", priority);
444                 if(setsockopt(listen_socket[sock].udp, SOL_IP, IP_TOS, &priority, sizeof(priority)))    /* SO_PRIORITY doesn't seem to work */
445                         logger(LOG_ERR, "System call `%s' failed: %s", "setsockopt", strerror(errno));
446         }
447 #endif
448
449         if((sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0) {
450                 if(errno == EMSGSIZE) {
451                         if(n->maxmtu >= origlen)
452                                 n->maxmtu = origlen - 1;
453                         if(n->mtu >= origlen)
454                                 n->mtu = origlen - 1;
455                 } else
456                         logger(LOG_ERR, "Error sending packet to %s (%s): %s", n->name, n->hostname, strerror(errno));
457         }
458
459 end:
460         origpkt->len = origlen;
461 }
462
463 /*
464   send a packet to the given vpn ip.
465 */
466 void send_packet(const node_t *n, vpn_packet_t *packet) {
467         node_t *via;
468
469         if(n == myself) {
470                 if(overwrite_mac)
471                          memcpy(packet->data, mymac.x, ETH_ALEN);
472                 write_packet(packet);
473                 return;
474         }
475
476         ifdebug(TRAFFIC) logger(LOG_ERR, "Sending packet of %d bytes to %s (%s)",
477                            packet->len, n->name, n->hostname);
478
479         if(!n->status.reachable) {
480                 ifdebug(TRAFFIC) logger(LOG_INFO, "Node %s (%s) is not reachable",
481                                    n->name, n->hostname);
482                 return;
483         }
484
485         via = (packet->priority == -1 || n->via == myself) ? n->nexthop : n->via;
486
487         if(via != n)
488                 ifdebug(TRAFFIC) logger(LOG_INFO, "Sending packet to %s via %s (%s)",
489                            n->name, via->name, n->via->hostname);
490
491         if(packet->priority == -1 || ((myself->options | via->options) & OPTION_TCPONLY)) {
492                 if(!send_tcppacket(via->connection, packet))
493                         terminate_connection(via->connection, true);
494         } else
495                 send_udppacket(via, packet);
496 }
497
498 /* Broadcast a packet using the minimum spanning tree */
499
500 void broadcast_packet(const node_t *from, vpn_packet_t *packet) {
501         avl_node_t *node;
502         connection_t *c;
503
504         ifdebug(TRAFFIC) logger(LOG_INFO, "Broadcasting packet of %d bytes from %s (%s)",
505                            packet->len, from->name, from->hostname);
506
507         if(from != myself) {
508                 send_packet(myself, packet);
509
510                 // In TunnelServer mode, do not forward broadcast packets.
511                 // The MST might not be valid and create loops.
512                 if(tunnelserver)
513                         return;
514         }
515
516         for(node = connection_tree->head; node; node = node->next) {
517                 c = node->data;
518
519                 if(c->status.active && c->status.mst && c != from->nexthop->connection)
520                         send_packet(c->node, packet);
521         }
522 }
523
524 static node_t *try_harder(const sockaddr_t *from, const vpn_packet_t *pkt) {
525         avl_node_t *node;
526         edge_t *e;
527         node_t *n = NULL;
528         static time_t last_hard_try = 0;
529
530         for(node = edge_weight_tree->head; node; node = node->next) {
531                 e = node->data;
532
533                 if(sockaddrcmp_noport(from, &e->address)) {
534                         if(last_hard_try == now)
535                                 continue;
536                         last_hard_try = now;
537                 }
538
539                 if(!n)
540                         n = e->to;
541
542                 if(!try_mac(e->to, pkt))
543                         continue;
544
545                 n = e->to;
546                 break;
547         }
548
549         return n;
550 }
551
552 void handle_incoming_vpn_data(int sock) {
553         vpn_packet_t pkt;
554         char *hostname;
555         sockaddr_t from;
556         socklen_t fromlen = sizeof(from);
557         node_t *n;
558
559         pkt.len = recvfrom(sock, (char *) &pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen);
560
561         if(pkt.len < 0) {
562                 if(errno != EAGAIN && errno != EINTR)
563                         logger(LOG_ERR, "Receiving packet failed: %s", strerror(errno));
564                 return;
565         }
566
567         sockaddrunmap(&from);           /* Some braindead IPv6 implementations do stupid things. */
568
569         n = lookup_node_udp(&from);
570
571         if(!n) {
572                 n = try_harder(&from, &pkt);
573                 if(n)
574                         update_node_udp(n, &from);
575                 else ifdebug(PROTOCOL) {
576                         hostname = sockaddr2hostname(&from);
577                         logger(LOG_WARNING, "Received UDP packet from unknown source %s", hostname);
578                         free(hostname);
579                         return;
580                 }
581                 else
582                         return;
583         }
584
585         receive_udppacket(n, &pkt);
586 }