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