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