]> git.meshlink.io Git - meshlink/blob - src/net_packet.c
Move RSA key generation into the wrappers.
[meshlink] / src / net_packet.c
1 /*
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>
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
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id$
21 */
22
23 #include "system.h"
24
25 #include <zlib.h>
26 #include LZO1X_H
27
28 #include "splay_tree.h"
29 #include "cipher.h"
30 #include "conf.h"
31 #include "connection.h"
32 #include "crypto.h"
33 #include "digest.h"
34 #include "device.h"
35 #include "ethernet.h"
36 #include "graph.h"
37 #include "list.h"
38 #include "logger.h"
39 #include "net.h"
40 #include "netutl.h"
41 #include "protocol.h"
42 #include "process.h"
43 #include "route.h"
44 #include "utils.h"
45 #include "xalloc.h"
46
47 #ifdef WSAEMSGSIZE
48 #define EMSGSIZE WSAEMSGSIZE
49 #endif
50
51 int keylifetime = 0;
52 static char lzo_wrkmem[LZO1X_999_MEM_COMPRESS > LZO1X_1_MEM_COMPRESS ? LZO1X_999_MEM_COMPRESS : LZO1X_1_MEM_COMPRESS];
53
54 static void send_udppacket(node_t *, vpn_packet_t *);
55
56 #define MAX_SEQNO 1073741824
57
58 static void send_mtu_probe_handler(int fd, short events, void *data) {
59         node_t *n = data;
60         vpn_packet_t packet;
61         int len, i;
62         
63         cp();
64
65         n->mtuprobes++;
66
67         if(n->mtuprobes >= 10 && !n->minmtu) {
68                 ifdebug(TRAFFIC) logger(LOG_INFO, _("No response to MTU probes from %s (%s)"), n->name, n->hostname);
69                 return;
70         }
71
72         for(i = 0; i < 3; i++) {
73                 if(n->mtuprobes >= 30 || n->minmtu >= n->maxmtu) {
74                         n->mtu = n->minmtu;
75                         ifdebug(TRAFFIC) logger(LOG_INFO, _("Fixing MTU of %s (%s) to %d after %d probes"), n->name, n->hostname, n->mtu, n->mtuprobes);
76                         return;
77                 }
78
79                 len = n->minmtu + 1 + random() % (n->maxmtu - n->minmtu);
80                 if(len < 64)
81                         len = 64;
82                 
83                 memset(packet.data, 0, 14);
84                 randomize(packet.data + 14, len - 14);
85                 packet.len = len;
86
87                 ifdebug(TRAFFIC) logger(LOG_INFO, _("Sending MTU probe length %d to %s (%s)"), len, n->name, n->hostname);
88
89                 send_udppacket(n, &packet);
90         }
91
92         event_add(&n->mtuevent, &(struct timeval){1, 0});
93 }
94
95 void send_mtu_probe(node_t *n) {
96         if(!timeout_initialized(&n->mtuevent))
97                 timeout_set(&n->mtuevent, send_mtu_probe_handler, n);
98         send_mtu_probe_handler(0, 0, n);
99 }
100
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);
103
104         if(!packet->data[0]) {
105                 packet->data[0] = 1;
106                 send_packet(n, packet);
107         } else {
108                 if(n->minmtu < packet->len)
109                         n->minmtu = packet->len;
110         }
111 }
112
113 static length_t compress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
114         if(level == 10) {
115                 lzo_uint lzolen = MAXSIZE;
116                 lzo1x_1_compress(source, len, dest, &lzolen, lzo_wrkmem);
117                 return lzolen;
118         } else if(level < 10) {
119                 unsigned long destlen = MAXSIZE;
120                 if(compress2(dest, &destlen, source, len, level) == Z_OK)
121                         return destlen;
122                 else
123                         return -1;
124         } else {
125                 lzo_uint lzolen = MAXSIZE;
126                 lzo1x_999_compress(source, len, dest, &lzolen, lzo_wrkmem);
127                 return lzolen;
128         }
129         
130         return -1;
131 }
132
133 static length_t uncompress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
134         if(level > 9) {
135                 lzo_uint lzolen = MAXSIZE;
136                 if(lzo1x_decompress_safe(source, len, dest, &lzolen, NULL) == LZO_E_OK)
137                         return lzolen;
138                 else
139                         return -1;
140         } else {
141                 unsigned long destlen = MAXSIZE;
142                 if(uncompress(dest, &destlen, source, len) == Z_OK)
143                         return destlen;
144                 else
145                         return -1;
146         }
147         
148         return -1;
149 }
150
151 /* VPN packet I/O */
152
153 static void receive_packet(node_t *n, vpn_packet_t *packet) {
154         cp();
155
156         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Received packet of %d bytes from %s (%s)"),
157                            packet->len, n->name, n->hostname);
158
159         route(n, packet);
160 }
161
162 static void receive_udppacket(node_t *n, vpn_packet_t *inpkt) {
163         vpn_packet_t pkt1, pkt2;
164         vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
165         int nextpkt = 0;
166         vpn_packet_t *outpkt = pkt[0];
167         size_t outlen;
168         int i;
169
170         cp();
171
172         /* Check packet length */
173
174         if(inpkt->len < sizeof inpkt->seqno + digest_length(&myself->digest)) {
175                 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got too short packet from %s (%s)"),
176                                         n->name, n->hostname);
177                 return;
178         }
179
180         /* Check the message authentication code */
181
182         if(digest_active(&myself->digest) && !digest_verify(&myself->digest, &inpkt->seqno, inpkt->len, &inpkt->seqno + inpkt->len)) {
183                 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Got unauthenticated packet from %s (%s)"), n->name, n->hostname);
184                 return;
185         }
186
187         /* Decrypt the packet */
188
189         if(cipher_active(&myself->cipher)) {
190                 outpkt = pkt[nextpkt++];
191                 outlen = MAXSIZE;
192
193                 if(!cipher_decrypt(&myself->cipher, &inpkt->seqno, inpkt->len, &outpkt->seqno, &outlen, true)) {
194                         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Error decrypting packet from %s (%s)"), n->name, n->hostname);
195                         return;
196                 }
197                 
198                 outpkt->len = outlen;
199                 inpkt = outpkt;
200         }
201
202         /* Check the sequence number */
203
204         inpkt->len -= sizeof inpkt->seqno;
205         inpkt->seqno = ntohl(inpkt->seqno);
206
207         if(inpkt->seqno != n->received_seqno + 1) {
208                 if(inpkt->seqno >= n->received_seqno + sizeof n->late * 8) {
209                         logger(LOG_WARNING, _("Lost %d packets from %s (%s)"),
210                                            inpkt->seqno - n->received_seqno - 1, n->name, n->hostname);
211                         
212                         memset(n->late, 0, sizeof n->late);
213                 } else if (inpkt->seqno <= n->received_seqno) {
214                         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))) {
215                                 logger(LOG_WARNING, _("Got late or replayed packet from %s (%s), seqno %d, last received %d"),
216                                            n->name, n->hostname, inpkt->seqno, n->received_seqno);
217                                 return;
218                         }
219                 } else {
220                         for(i = n->received_seqno + 1; i < inpkt->seqno; i++)
221                                 n->late[(i / 8) % sizeof n->late] |= 1 << i % 8;
222                 }
223         }
224         
225         n->late[(inpkt->seqno / 8) % sizeof n->late] &= ~(1 << inpkt->seqno % 8);
226
227         if(inpkt->seqno > n->received_seqno)
228                 n->received_seqno = inpkt->seqno;
229                         
230         if(n->received_seqno > MAX_SEQNO)
231                 regenerate_key();
232
233         /* Decompress the packet */
234
235         if(myself->compression) {
236                 outpkt = pkt[nextpkt++];
237
238                 if((outpkt->len = uncompress_packet(outpkt->data, inpkt->data, inpkt->len, myself->compression)) < 0) {
239                         ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while uncompressing packet from %s (%s)"),
240                                                  n->name, n->hostname);
241                         return;
242                 }
243
244                 inpkt = outpkt;
245         }
246
247         if(!inpkt->data[12] && !inpkt->data[13])
248                 mtu_probe_h(n, inpkt);
249         else
250                 receive_packet(n, inpkt);
251 }
252
253 void receive_tcppacket(connection_t *c, char *buffer, int len) {
254         vpn_packet_t outpkt;
255
256         cp();
257
258         outpkt.len = len;
259         memcpy(outpkt.data, buffer, len);
260
261         receive_packet(c->node, &outpkt);
262 }
263
264 static void send_udppacket(node_t *n, vpn_packet_t *origpkt) {
265         vpn_packet_t pkt1, pkt2;
266         vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
267         vpn_packet_t *inpkt = origpkt;
268         int nextpkt = 0;
269         vpn_packet_t *outpkt;
270         int origlen;
271         size_t outlen;
272         vpn_packet_t *copy;
273         static int priority = 0;
274         int origpriority;
275         int sock;
276
277         cp();
278
279         /* Make sure we have a valid key */
280
281         if(!n->status.validkey) {
282                 ifdebug(TRAFFIC) logger(LOG_INFO,
283                                    _("No valid key known yet for %s (%s), queueing packet"),
284                                    n->name, n->hostname);
285
286                 /* Since packet is on the stack of handle_tap_input(), we have to make a copy of it first. */
287
288                 *(copy = xmalloc(sizeof *copy)) = *inpkt;
289
290                 list_insert_tail(n->queue, copy);
291
292                 if(n->queue->count > MAXQUEUELENGTH)
293                         list_delete_head(n->queue);
294
295                 if(!n->status.waitingforkey)
296                         send_req_key(n->nexthop->connection, myself, n);
297
298                 n->status.waitingforkey = true;
299
300                 return;
301         }
302
303         origlen = inpkt->len;
304         origpriority = inpkt->priority;
305
306         /* Compress the packet */
307
308         if(n->compression) {
309                 outpkt = pkt[nextpkt++];
310
311                 if((outpkt->len = compress_packet(outpkt->data, inpkt->data, inpkt->len, n->compression)) < 0) {
312                         ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while compressing packet to %s (%s)"),
313                                    n->name, n->hostname);
314                         return;
315                 }
316
317                 inpkt = outpkt;
318         }
319
320         /* Add sequence number */
321
322         inpkt->seqno = htonl(++(n->sent_seqno));
323         inpkt->len += sizeof inpkt->seqno;
324
325         /* Encrypt the packet */
326
327         if(cipher_active(&n->cipher)) {
328                 outpkt = pkt[nextpkt++];
329                 outlen = MAXSIZE;
330
331                 if(!cipher_encrypt(&n->cipher, &inpkt->seqno, inpkt->len, &outpkt->seqno, &outlen, true)) {
332                         ifdebug(TRAFFIC) logger(LOG_ERR, _("Error while encrypting packet to %s (%s)"), n->name, n->hostname);
333                         goto end;
334                 }
335
336                 outpkt->len = outlen;
337                 inpkt = outpkt;
338         }
339
340         /* Add the message authentication code */
341
342         if(digest_active(&n->digest)) {
343                 digest_create(&n->digest, &inpkt->seqno, inpkt->len, &inpkt->seqno + inpkt->len);
344                 inpkt->len += digest_length(&n->digest);
345         }
346
347         /* Determine which socket we have to use */
348
349         for(sock = 0; sock < listen_sockets; sock++)
350                 if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family)
351                         break;
352
353         if(sock >= listen_sockets)
354                 sock = 0;                               /* If none is available, just use the first and hope for the best. */
355
356         /* Send the packet */
357
358 #if defined(SOL_IP) && defined(IP_TOS)
359         if(priorityinheritance && origpriority != priority
360            && listen_socket[sock].sa.sa.sa_family == AF_INET) {
361                 priority = origpriority;
362                 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Setting outgoing packet priority to %d"), priority);
363                 if(setsockopt(listen_socket[sock].udp, SOL_IP, IP_TOS, &priority, sizeof priority))     /* SO_PRIORITY doesn't seem to work */
364                         logger(LOG_ERR, _("System call `%s' failed: %s"), "setsockopt", strerror(errno));
365         }
366 #endif
367
368         if((sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0) {
369                 if(errno == EMSGSIZE) {
370                         if(n->maxmtu >= origlen)
371                                 n->maxmtu = origlen - 1;
372                         if(n->mtu >= origlen)
373                                 n->mtu = origlen - 1;
374                 } else
375                         logger(LOG_ERR, _("Error sending packet to %s (%s): %s"), n->name, n->hostname, strerror(errno));
376         }
377
378 end:
379         origpkt->len = origlen;
380 }
381
382 /*
383   send a packet to the given vpn ip.
384 */
385 void send_packet(const node_t *n, vpn_packet_t *packet) {
386         node_t *via;
387
388         cp();
389
390         if(n == myself) {
391                 if(overwrite_mac)
392                          memcpy(packet->data, mymac.x, ETH_ALEN);
393                 write_packet(packet);
394                 return;
395         }
396
397         ifdebug(TRAFFIC) logger(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
398                            packet->len, n->name, n->hostname);
399
400         if(!n->status.reachable) {
401                 ifdebug(TRAFFIC) logger(LOG_INFO, _("Node %s (%s) is not reachable"),
402                                    n->name, n->hostname);
403                 return;
404         }
405
406         via = (n->via == myself) ? n->nexthop : n->via;
407
408         if(via != n)
409                 ifdebug(TRAFFIC) logger(LOG_ERR, _("Sending packet to %s via %s (%s)"),
410                            n->name, via->name, n->via->hostname);
411
412         if((myself->options | via->options) & OPTION_TCPONLY) {
413                 if(!send_tcppacket(via->connection, packet))
414                         terminate_connection(via->connection, true);
415         } else
416                 send_udppacket(via, packet);
417 }
418
419 /* Broadcast a packet using the minimum spanning tree */
420
421 void broadcast_packet(const node_t *from, vpn_packet_t *packet) {
422         splay_node_t *node;
423         connection_t *c;
424
425         cp();
426
427         ifdebug(TRAFFIC) logger(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
428                            packet->len, from->name, from->hostname);
429
430         if(from != myself)
431                 send_packet(myself, packet);
432
433         for(node = connection_tree->head; node; node = node->next) {
434                 c = node->data;
435
436                 if(c->status.active && c->status.mst && c != from->nexthop->connection)
437                         send_packet(c->node, packet);
438         }
439 }
440
441 void flush_queue(node_t *n) {
442         list_node_t *node, *next;
443
444         cp();
445
446         ifdebug(TRAFFIC) logger(LOG_INFO, _("Flushing queue for %s (%s)"), n->name, n->hostname);
447
448         for(node = n->queue->head; node; node = next) {
449                 next = node->next;
450                 send_udppacket(n, node->data);
451                 list_delete_node(n->queue, node);
452         }
453 }
454
455 void handle_incoming_vpn_data(int sock, short events, void *data) {
456         vpn_packet_t pkt;
457         char *hostname;
458         sockaddr_t from;
459         socklen_t fromlen = sizeof from;
460         node_t *n;
461
462         cp();
463
464         pkt.len = recvfrom(sock, (char *) &pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen);
465
466         if(pkt.len < 0) {
467                 logger(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno));
468                 return;
469         }
470
471         sockaddrunmap(&from);           /* Some braindead IPv6 implementations do stupid things. */
472
473         n = lookup_node_udp(&from);
474
475         if(!n) {
476                 hostname = sockaddr2hostname(&from);
477                 logger(LOG_WARNING, _("Received UDP packet from unknown source %s"),
478                            hostname);
479                 free(hostname);
480                 return;
481         }
482
483         receive_udppacket(n, &pkt);
484 }
485
486 void handle_device_data(int sock, short events, void *data) {
487         vpn_packet_t packet;
488
489         if(read_packet(&packet))
490                 route(myself, &packet);
491 }