]> git.meshlink.io Git - meshlink/blob - src/net_packet.c
Merge branch 'master' of git://tinc-vpn.org/tinc 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-2012 Guus Sliepen <guus@tinc-vpn.org>
5                   2010      Timothy Redaelli <timothy@redaelli.eu>
6                   2010      Brandon Black <blblack@gmail.com>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "system.h"
24
25 #include <openssl/rand.h>
26 #include <openssl/err.h>
27 #include <openssl/evp.h>
28 #include <openssl/pem.h>
29 #include <openssl/hmac.h>
30
31 #ifdef HAVE_ZLIB
32 #include <zlib.h>
33 #endif
34
35 #ifdef HAVE_LZO
36 #include LZO1X_H
37 #endif
38
39 #include "splay_tree.h"
40 #include "cipher.h"
41 #include "conf.h"
42 #include "connection.h"
43 #include "crypto.h"
44 #include "digest.h"
45 #include "device.h"
46 #include "ethernet.h"
47 #include "graph.h"
48 #include "logger.h"
49 #include "net.h"
50 #include "netutl.h"
51 #include "protocol.h"
52 #include "process.h"
53 #include "route.h"
54 #include "utils.h"
55 #include "xalloc.h"
56
57 int keylifetime = 0;
58 #ifdef HAVE_LZO
59 static char lzo_wrkmem[LZO1X_999_MEM_COMPRESS > LZO1X_1_MEM_COMPRESS ? LZO1X_999_MEM_COMPRESS : LZO1X_1_MEM_COMPRESS];
60 #endif
61
62 static void send_udppacket(node_t *, vpn_packet_t *);
63
64 unsigned replaywin = 16;
65 bool localdiscovery = false;
66
67 #define MAX_SEQNO 1073741824
68
69 /* mtuprobes == 1..30: initial discovery, send bursts with 1 second interval
70    mtuprobes ==    31: sleep pinginterval seconds
71    mtuprobes ==    32: send 1 burst, sleep pingtimeout second
72    mtuprobes ==    33: no response from other side, restart PMTU discovery process
73
74    Probes are sent in batches of three, with random sizes between the lower and
75    upper boundaries for the MTU thus far discovered.
76
77    In case local discovery is enabled, a fourth packet is added to each batch,
78    which will be broadcast to the local network.
79 */
80
81 static void send_mtu_probe_handler(int fd, short events, void *data) {
82         node_t *n = data;
83         vpn_packet_t packet;
84         int len, i;
85         int timeout = 1;
86         
87         n->mtuprobes++;
88
89         if(!n->status.reachable || !n->status.validkey) {
90                 logger(DEBUG_TRAFFIC, LOG_INFO, "Trying to send MTU probe to unreachable or rekeying node %s (%s)", n->name, n->hostname);
91                 n->mtuprobes = 0;
92                 return;
93         }
94
95         if(n->mtuprobes > 32) {
96                 if(!n->minmtu) {
97                         n->mtuprobes = 31;
98                         timeout = pinginterval;
99                         goto end;
100                 }
101
102                 logger(DEBUG_TRAFFIC, LOG_INFO, "%s (%s) did not respond to UDP ping, restarting PMTU discovery", n->name, n->hostname);
103                 n->mtuprobes = 1;
104                 n->minmtu = 0;
105                 n->maxmtu = MTU;
106         }
107
108         if(n->mtuprobes >= 10 && n->mtuprobes < 32 && !n->minmtu) {
109                 logger(DEBUG_TRAFFIC, LOG_INFO, "No response to MTU probes from %s (%s)", n->name, n->hostname);
110                 n->mtuprobes = 31;
111         }
112
113         if(n->mtuprobes == 30 || (n->mtuprobes < 30 && n->minmtu >= n->maxmtu)) {
114                 if(n->minmtu > n->maxmtu)
115                         n->minmtu = n->maxmtu;
116                 else
117                         n->maxmtu = n->minmtu;
118                 n->mtu = n->minmtu;
119                 logger(DEBUG_TRAFFIC, LOG_INFO, "Fixing MTU of %s (%s) to %d after %d probes", n->name, n->hostname, n->mtu, n->mtuprobes);
120                 n->mtuprobes = 31;
121         }
122
123         if(n->mtuprobes == 31) {
124                 timeout = pinginterval;
125                 goto end;
126         } else if(n->mtuprobes == 32) {
127                 timeout = pingtimeout;
128         }
129
130         for(i = 0; i < 3 + localdiscovery; i++) {
131                 if(n->maxmtu <= n->minmtu)
132                         len = n->maxmtu;
133                 else
134                         len = n->minmtu + 1 + rand() % (n->maxmtu - n->minmtu);
135
136                 if(len < 64)
137                         len = 64;
138                 
139                 memset(packet.data, 0, 14);
140                 randomize(packet.data + 14, len - 14);
141                 packet.len = len;
142                 if(i >= 3 && n->mtuprobes <= 10)
143                         packet.priority = -1;
144                 else
145                         packet.priority = 0;
146
147                 logger(DEBUG_TRAFFIC, LOG_INFO, "Sending MTU probe length %d to %s (%s)", len, n->name, n->hostname);
148
149                 send_udppacket(n, &packet);
150         }
151
152 end:
153         event_add(&n->mtuevent, &(struct timeval){timeout, 0});
154 }
155
156 void send_mtu_probe(node_t *n) {
157         if(!timeout_initialized(&n->mtuevent))
158                 timeout_set(&n->mtuevent, send_mtu_probe_handler, n);
159         send_mtu_probe_handler(0, 0, n);
160 }
161
162 static void mtu_probe_h(node_t *n, vpn_packet_t *packet, length_t len) {
163         logger(DEBUG_TRAFFIC, LOG_INFO, "Got MTU probe length %d from %s (%s)", packet->len, n->name, n->hostname);
164
165         if(!packet->data[0]) {
166                 packet->data[0] = 1;
167                 send_udppacket(n, packet);
168         } else {
169                 if(n->mtuprobes > 30) {
170                         if(n->minmtu)
171                                 n->mtuprobes = 30;
172                         else
173                                 n->mtuprobes = 1;
174                 }
175
176                 if(len > n->maxmtu)
177                         len = n->maxmtu;
178                 if(n->minmtu < len)
179                         n->minmtu = len;
180         }
181 }
182
183 static length_t compress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
184         if(level == 0) {
185                 memcpy(dest, source, len);
186                 return len;
187         } else if(level == 10) {
188 #ifdef HAVE_LZO
189                 lzo_uint lzolen = MAXSIZE;
190                 lzo1x_1_compress(source, len, dest, &lzolen, lzo_wrkmem);
191                 return lzolen;
192 #else
193                 return -1;
194 #endif
195         } else if(level < 10) {
196 #ifdef HAVE_ZLIB
197                 unsigned long destlen = MAXSIZE;
198                 if(compress2(dest, &destlen, source, len, level) == Z_OK)
199                         return destlen;
200                 else
201 #endif
202                         return -1;
203         } else {
204 #ifdef HAVE_LZO
205                 lzo_uint lzolen = MAXSIZE;
206                 lzo1x_999_compress(source, len, dest, &lzolen, lzo_wrkmem);
207                 return lzolen;
208 #else
209                 return -1;
210 #endif
211         }
212         
213         return -1;
214 }
215
216 static length_t uncompress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) {
217         if(level == 0) {
218                 memcpy(dest, source, len);
219                 return len;
220         } else if(level > 9) {
221 #ifdef HAVE_LZO
222                 lzo_uint lzolen = MAXSIZE;
223                 if(lzo1x_decompress_safe(source, len, dest, &lzolen, NULL) == LZO_E_OK)
224                         return lzolen;
225                 else
226 #endif
227                         return -1;
228         }
229 #ifdef HAVE_ZLIB
230         else {
231                 unsigned long destlen = MAXSIZE;
232                 if(uncompress(dest, &destlen, source, len) == Z_OK)
233                         return destlen;
234                 else
235                         return -1;
236         }
237 #endif
238
239         return -1;
240 }
241
242 /* VPN packet I/O */
243
244 static void receive_packet(node_t *n, vpn_packet_t *packet) {
245         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Received packet of %d bytes from %s (%s)",
246                            packet->len, n->name, n->hostname);
247
248         n->in_packets++;
249         n->in_bytes += packet->len;
250
251         route(n, packet);
252 }
253
254 static bool try_mac(node_t *n, const vpn_packet_t *inpkt) {
255         if(!digest_active(&n->indigest) || inpkt->len < sizeof inpkt->seqno + digest_length(&n->indigest))
256                 return false;
257
258         return digest_verify(&n->indigest, &inpkt->seqno, inpkt->len - n->indigest.maclength, (const char *)&inpkt->seqno + inpkt->len - n->indigest.maclength);
259 }
260
261 static void receive_udppacket(node_t *n, vpn_packet_t *inpkt) {
262         vpn_packet_t pkt1, pkt2;
263         vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
264         int nextpkt = 0;
265         vpn_packet_t *outpkt = pkt[0];
266         size_t outlen;
267
268         if(!cipher_active(&n->incipher)) {
269                 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Got packet from %s (%s) but he hasn't got our key yet",
270                                         n->name, n->hostname);
271                 return;
272         }
273
274         /* Check packet length */
275
276         if(inpkt->len < sizeof inpkt->seqno + digest_length(&n->indigest)) {
277                 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Got too short packet from %s (%s)",
278                                         n->name, n->hostname);
279                 return;
280         }
281
282         /* Check the message authentication code */
283
284         if(digest_active(&n->indigest)) {
285                 inpkt->len -= n->indigest.maclength;
286                 if(!digest_verify(&n->indigest, &inpkt->seqno, inpkt->len, (const char *)&inpkt->seqno + inpkt->len)) {
287                         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Got unauthenticated packet from %s (%s)", n->name, n->hostname);
288                         return;
289                 }
290         }
291         /* Decrypt the packet */
292
293         if(cipher_active(&n->incipher)) {
294                 outpkt = pkt[nextpkt++];
295                 outlen = MAXSIZE;
296
297                 if(!cipher_decrypt(&n->incipher, &inpkt->seqno, inpkt->len, &outpkt->seqno, &outlen, true)) {
298                         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Error decrypting packet from %s (%s)", n->name, n->hostname);
299                         return;
300                 }
301                 
302                 outpkt->len = outlen;
303                 inpkt = outpkt;
304         }
305
306         /* Check the sequence number */
307
308         inpkt->len -= sizeof inpkt->seqno;
309         inpkt->seqno = ntohl(inpkt->seqno);
310
311         if(replaywin) {
312                 if(inpkt->seqno != n->received_seqno + 1) {
313                         if(inpkt->seqno >= n->received_seqno + replaywin * 8) {
314                                 if(n->farfuture++ < replaywin >> 2) {
315                                         logger(DEBUG_ALWAYS, LOG_WARNING, "Packet from %s (%s) is %d seqs in the future, dropped (%u)",
316                                                 n->name, n->hostname, inpkt->seqno - n->received_seqno - 1, n->farfuture);
317                                         return;
318                                 }
319                                 logger(DEBUG_ALWAYS, LOG_WARNING, "Lost %d packets from %s (%s)",
320                                                 inpkt->seqno - n->received_seqno - 1, n->name, n->hostname);
321                                 memset(n->late, 0, replaywin);
322                         } else if (inpkt->seqno <= n->received_seqno) {
323                                 if((n->received_seqno >= replaywin * 8 && inpkt->seqno <= n->received_seqno - replaywin * 8) || !(n->late[(inpkt->seqno / 8) % replaywin] & (1 << inpkt->seqno % 8))) {
324                                         logger(DEBUG_ALWAYS, LOG_WARNING, "Got late or replayed packet from %s (%s), seqno %d, last received %d",
325                                                 n->name, n->hostname, inpkt->seqno, n->received_seqno);
326                                         return;
327                                 }
328                         } else {
329                                 for(int i = n->received_seqno + 1; i < inpkt->seqno; i++)
330                                         n->late[(i / 8) % replaywin] |= 1 << i % 8;
331                         }
332                 }
333
334                 n->farfuture = 0;
335                 n->late[(inpkt->seqno / 8) % replaywin] &= ~(1 << inpkt->seqno % 8);
336         }
337
338         if(inpkt->seqno > n->received_seqno)
339                 n->received_seqno = inpkt->seqno;
340                         
341         if(n->received_seqno > MAX_SEQNO)
342                 regenerate_key();
343
344         /* Decompress the packet */
345
346         length_t origlen = inpkt->len;
347
348         if(n->incompression) {
349                 outpkt = pkt[nextpkt++];
350
351                 if((outpkt->len = uncompress_packet(outpkt->data, inpkt->data, inpkt->len, n->incompression)) < 0) {
352                         logger(DEBUG_TRAFFIC, LOG_ERR, "Error while uncompressing packet from %s (%s)",
353                                                  n->name, n->hostname);
354                         return;
355                 }
356
357                 inpkt = outpkt;
358
359                 origlen -= MTU/64 + 20;
360         }
361
362         inpkt->priority = 0;
363
364         if(!inpkt->data[12] && !inpkt->data[13])
365                 mtu_probe_h(n, inpkt, origlen);
366         else
367                 receive_packet(n, inpkt);
368 }
369
370 void receive_tcppacket(connection_t *c, const char *buffer, int len) {
371         vpn_packet_t outpkt;
372
373         outpkt.len = len;
374         if(c->options & OPTION_TCPONLY)
375                 outpkt.priority = 0;
376         else
377                 outpkt.priority = -1;
378         memcpy(outpkt.data, buffer, len);
379
380         receive_packet(c->node, &outpkt);
381 }
382
383 static void send_udppacket(node_t *n, vpn_packet_t *origpkt) {
384         vpn_packet_t pkt1, pkt2;
385         vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
386         vpn_packet_t *inpkt = origpkt;
387         int nextpkt = 0;
388         vpn_packet_t *outpkt;
389         int origlen = origpkt->len;
390         size_t outlen;
391 #if defined(SOL_IP) && defined(IP_TOS)
392         static int priority = 0;
393         int origpriority = origpkt->priority;
394 #endif
395
396         if(!n->status.reachable) {
397                 logger(DEBUG_TRAFFIC, LOG_INFO, "Trying to send UDP packet to unreachable node %s (%s)", n->name, n->hostname);
398                 return;
399         }
400
401         /* Make sure we have a valid key */
402
403         if(!n->status.validkey) {
404                 time_t now = time(NULL);
405
406                 logger(DEBUG_TRAFFIC, LOG_INFO,
407                                    "No valid key known yet for %s (%s), forwarding via TCP",
408                                    n->name, n->hostname);
409
410                 if(n->last_req_key + 10 <= now) {
411                         send_req_key(n);
412                         n->last_req_key = now;
413                 }
414
415                 send_tcppacket(n->nexthop->connection, origpkt);
416
417                 return;
418         }
419
420         if(n->options & OPTION_PMTU_DISCOVERY && inpkt->len > n->minmtu && (inpkt->data[12] | inpkt->data[13])) {
421                 logger(DEBUG_TRAFFIC, LOG_INFO,
422                                 "Packet for %s (%s) larger than minimum MTU, forwarding via %s",
423                                 n->name, n->hostname, n != n->nexthop ? n->nexthop->name : "TCP");
424
425                 if(n != n->nexthop)
426                         send_packet(n->nexthop, origpkt);
427                 else
428                         send_tcppacket(n->nexthop->connection, origpkt);
429
430                 return;
431         }
432
433         /* Compress the packet */
434
435         if(n->outcompression) {
436                 outpkt = pkt[nextpkt++];
437
438                 if((outpkt->len = compress_packet(outpkt->data, inpkt->data, inpkt->len, n->outcompression)) < 0) {
439                         logger(DEBUG_TRAFFIC, LOG_ERR, "Error while compressing packet to %s (%s)",
440                                    n->name, n->hostname);
441                         return;
442                 }
443
444                 inpkt = outpkt;
445         }
446
447         /* Add sequence number */
448
449         inpkt->seqno = htonl(++(n->sent_seqno));
450         inpkt->len += sizeof inpkt->seqno;
451
452         /* Encrypt the packet */
453
454         if(cipher_active(&n->outcipher)) {
455                 outpkt = pkt[nextpkt++];
456                 outlen = MAXSIZE;
457
458                 if(!cipher_encrypt(&n->outcipher, &inpkt->seqno, inpkt->len, &outpkt->seqno, &outlen, true)) {
459                         logger(DEBUG_TRAFFIC, LOG_ERR, "Error while encrypting packet to %s (%s)", n->name, n->hostname);
460                         goto end;
461                 }
462
463                 outpkt->len = outlen;
464                 inpkt = outpkt;
465         }
466
467         /* Add the message authentication code */
468
469         if(digest_active(&n->outdigest)) {
470                 digest_create(&n->outdigest, &inpkt->seqno, inpkt->len, (char *)&inpkt->seqno + inpkt->len);
471                 inpkt->len += digest_length(&n->outdigest);
472         }
473
474         /* Determine which socket we have to use */
475
476         if(n->address.sa.sa_family != listen_socket[n->sock].sa.sa.sa_family) {
477                 for(int sock = 0; sock < listen_sockets; sock++) {
478                         if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family) {
479                                 n->sock = sock;
480                                 break;
481                         }
482                 }
483         }
484
485         /* Send the packet */
486
487         struct sockaddr *sa;
488         socklen_t sl;
489         int sock;
490
491         /* Overloaded use of priority field: -1 means local broadcast */
492
493         if(origpriority == -1 && n->prevedge) {
494                 struct sockaddr_in in;
495                 in.sin_family = AF_INET;
496                 in.sin_addr.s_addr = -1;
497                 in.sin_port = n->prevedge->address.in.sin_port;
498                 sa = (struct sockaddr *)&in;
499                 sl = sizeof in;
500                 sock = 0;
501         } else {
502                 if(origpriority == -1)
503                         origpriority = 0;
504
505                 sa = &(n->address.sa);
506                 sl = SALEN(n->address.sa);
507                 sock = n->sock;
508         }
509
510 #if defined(SOL_IP) && defined(IP_TOS)
511         if(priorityinheritance && origpriority != priority
512            && listen_socket[n->sock].sa.sa.sa_family == AF_INET) {
513                 priority = origpriority;
514                 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Setting outgoing packet priority to %d", priority);
515                 if(setsockopt(listen_socket[n->sock].udp, SOL_IP, IP_TOS, &priority, sizeof(priority))) /* SO_PRIORITY doesn't seem to work */
516                         logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "setsockopt", strerror(errno));
517         }
518 #endif
519
520         if(sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, sa, sl) < 0 && !sockwouldblock(sockerrno)) {
521                 if(sockmsgsize(sockerrno)) {
522                         if(n->maxmtu >= origlen)
523                                 n->maxmtu = origlen - 1;
524                         if(n->mtu >= origlen)
525                                 n->mtu = origlen - 1;
526                 } else
527                         logger(DEBUG_TRAFFIC, LOG_WARNING, "Error sending packet to %s (%s): %s", n->name, n->hostname, sockstrerror(sockerrno));
528         }
529
530 end:
531         origpkt->len = origlen;
532 }
533
534 /*
535   send a packet to the given vpn ip.
536 */
537 void send_packet(node_t *n, vpn_packet_t *packet) {
538         node_t *via;
539
540         if(n == myself) {
541                 if(overwrite_mac)
542                          memcpy(packet->data, mymac.x, ETH_ALEN);
543                 n->out_packets++;
544                 n->out_bytes += packet->len;
545                 devops.write(packet);
546                 return;
547         }
548
549         logger(DEBUG_TRAFFIC, LOG_ERR, "Sending packet of %d bytes to %s (%s)",
550                            packet->len, n->name, n->hostname);
551
552         if(!n->status.reachable) {
553                 logger(DEBUG_TRAFFIC, LOG_INFO, "Node %s (%s) is not reachable",
554                                    n->name, n->hostname);
555                 return;
556         }
557
558         n->out_packets++;
559         n->out_bytes += packet->len;
560
561         via = (packet->priority == -1 || n->via == myself) ? n->nexthop : n->via;
562
563         if(via != n)
564                 logger(DEBUG_TRAFFIC, LOG_INFO, "Sending packet to %s via %s (%s)",
565                            n->name, via->name, n->via->hostname);
566
567         if(packet->priority == -1 || ((myself->options | via->options) & OPTION_TCPONLY)) {
568                 if(!send_tcppacket(via->connection, packet))
569                         terminate_connection(via->connection, true);
570         } else
571                 send_udppacket(via, packet);
572 }
573
574 /* Broadcast a packet using the minimum spanning tree */
575
576 void broadcast_packet(const node_t *from, vpn_packet_t *packet) {
577         splay_node_t *node;
578         connection_t *c;
579         node_t *n;
580
581         // Always give ourself a copy of the packet.
582         if(from != myself)
583                 send_packet(myself, packet);
584
585         // In TunnelServer mode, do not forward broadcast packets.
586         // The MST might not be valid and create loops.
587         if(tunnelserver || broadcast_mode == BMODE_NONE)
588                 return;
589
590         logger(DEBUG_TRAFFIC, LOG_INFO, "Broadcasting packet of %d bytes from %s (%s)",
591                            packet->len, from->name, from->hostname);
592
593         switch(broadcast_mode) {
594                 // In MST mode, broadcast packets travel via the Minimum Spanning Tree.
595                 // This guarantees all nodes receive the broadcast packet, and
596                 // usually distributes the sending of broadcast packets over all nodes.
597                 case BMODE_MST:
598                         for(node = connection_tree->head; node; node = node->next) {
599                                 c = node->data;
600
601                                 if(c->status.active && c->status.mst && c != from->nexthop->connection)
602                                         send_packet(c->node, packet);
603                         }
604                         break;
605
606                 // In direct mode, we send copies to each node we know of.
607                 // However, this only reaches nodes that can be reached in a single hop.
608                 // We don't have enough information to forward broadcast packets in this case.
609                 case BMODE_DIRECT:
610                         if(from != myself)
611                                 break;
612
613                         for(node = node_udp_tree->head; node; node = node->next) {
614                                 n = node->data;
615
616                                 if(n->status.reachable && ((n->via == myself && n->nexthop == n) || n->via == n))
617                                         send_packet(n, packet);
618                         }
619                         break;
620
621                 default:
622                         break;
623         }
624 }
625
626 static node_t *try_harder(const sockaddr_t *from, const vpn_packet_t *pkt) {
627         splay_node_t *node;
628         edge_t *e;
629         node_t *n = NULL;
630         bool hard = false;
631         static time_t last_hard_try = 0;
632         time_t now = time(NULL);
633
634         for(node = edge_weight_tree->head; node; node = node->next) {
635                 e = node->data;
636
637                 if(e->to == myself)
638                         continue;
639
640                 if(sockaddrcmp_noport(from, &e->address)) {
641                         if(last_hard_try == now)
642                                 continue;
643                         hard = true;
644                 }
645
646                 if(!try_mac(e->to, pkt))
647                         continue;
648
649                 n = e->to;
650                 break;
651         }
652
653         if(hard)
654                 last_hard_try = now;
655
656         last_hard_try = now;
657         return n;
658 }
659
660 void handle_incoming_vpn_data(int sock, short events, void *data) {
661         vpn_packet_t pkt;
662         char *hostname;
663         sockaddr_t from;
664         socklen_t fromlen = sizeof from;
665         node_t *n;
666         int len;
667
668         len = recvfrom(sock, (char *) &pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen);
669
670         if(len <= 0 || len > MAXSIZE) {
671                 if(!sockwouldblock(sockerrno))
672                         logger(DEBUG_ALWAYS, LOG_ERR, "Receiving packet failed: %s", sockstrerror(sockerrno));
673                 return;
674         }
675
676         pkt.len = len;
677
678         sockaddrunmap(&from);           /* Some braindead IPv6 implementations do stupid things. */
679
680         n = lookup_node_udp(&from);
681
682         if(!n) {
683                 n = try_harder(&from, &pkt);
684                 if(n)
685                         update_node_udp(n, &from);
686                 else if(debug_level >= DEBUG_PROTOCOL) {
687                         hostname = sockaddr2hostname(&from);
688                         logger(DEBUG_PROTOCOL, LOG_WARNING, "Received UDP packet from unknown source %s", hostname);
689                         free(hostname);
690                         return;
691                 }
692                 else
693                         return;
694         }
695
696         n->sock = (intptr_t)data;
697
698         receive_udppacket(n, &pkt);
699 }
700
701 void handle_device_data(int sock, short events, void *data) {
702         vpn_packet_t packet;
703
704         packet.priority = 0;
705
706         if(devops.read(&packet)) {
707                 myself->in_packets++;
708                 myself->in_bytes += packet.len;
709                 route(myself, &packet);
710         }
711 }