-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
probe.c
1517 lines (1435 loc) · 41.1 KB
/
probe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* probe.c - dnssec-trigger DNSSEC probes implementation
*
* Copyright (c) 2011, NLnet Labs. All rights reserved.
*
* This software is open source.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the NLNET LABS nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* \file
*
* This file contains the probe implementation.
*/
#include "config.h"
#include "probe.h"
#include "svr.h"
#include "cfg.h"
#include "log.h"
#include "netevent.h"
#include "net_help.h"
#include "ubhook.h"
#include "reshook.h"
#include "http.h"
#include "update.h"
#include <ldns/ldns.h>
/* create probes for the ip addresses in the string */
static void probe_spawn(const char* ip, int recurse, int dnstcp,
struct ssllist* ssldns, int port);
/* set timeout on outq and create UDP query and send it */
static int outq_settimeout_and_send(struct outq* outq);
/* send outq over tcp */
static int outq_send_tcp(struct outq* outq);
/* a query is done, check probe to see if failed, succeed or wait */
static void probe_partial_done(struct probe_ip* p, const char* in,
const char* reason);
/* a probe is done (fail or success) see global progress */
static void probe_done(struct probe_ip* p);
void probe_start(char* ips)
{
char* next;
struct svr* svr = global_svr;
if(svr->http) {
http_general_delete(svr->http);
svr->http = NULL;
}
if(svr->probes) {
/* clear existing probe list */
probe_list_delete(svr->probes);
svr->probes = NULL;
if(svr->num_probes_done < svr->num_probes) {
verbose(VERB_QUERY, "probes cancelled due to fast "
"net change");
}
svr->num_probes_done = 0;
svr->num_probes = 0;
}
/* spawn a probe for every IP address in the list */
svr->saw_first_working = 0;
svr->saw_direct_work = 0;
svr->saw_dnstcp_work = 0;
svr->probe_direct = 0;
svr->probe_dnstcp = 0;
while(*ips == ' ')
ips++;
while(ips && *ips) {
if((next = strchr(ips, ' ')) != NULL) {
*next++ = 0;
while(*next == ' ')
next++;
}
probe_spawn(ips, 1, 0, 0, DNS_PORT);
ips = next;
}
svr->num_probes_to_cache = svr->num_probes;
/* (if no resulting probes), check result now */
if(!svr->probes)
probe_cache_done();
else if(svr->forced_insecure) {
verbose(VERB_OPS, "probe started: but still forced insecure");
/* call it right away, so the user does not have to wait */
probe_setup_hotspot_signon(svr);
} else {
/* there are cache DNS, and not forced insecure: check HTTP */
if(!svr->http && svr->cfg->num_http_urls != 0) {
svr->http = http_general_start(svr);
if(!svr->http) log_err("out of memory");
svr->http->saw_http_work = 0;
}
}
}
void probe_delete(struct probe_ip* p)
{
if(!p) return;
free(p->name);
free(p->reason);
free(p->http_desc);
SSL_CTX_free(p->sslctx);
outq_delete(p->ds_c);
outq_delete(p->dnskey_c);
outq_delete(p->nsec3_c);
outq_delete(p->host_c);
http_get_delete(p->http);
free(p);
}
void probe_list_delete(struct probe_ip* list)
{
struct probe_ip* p=list, *np;
while(p) {
np = p->next;
probe_delete(p);
p = np;
}
}
/** get random signed TLD */
static const char*
get_random_dest(void)
{
const char* choices[] = { "se.", "uk.", "nl.", "de." };
return choices[ ldns_get_random() % 4 ];
}
/** get random NSEC3 signed TLD */
static const char*
get_random_nsec3_dest(void)
{
const char* choices[] = { "_probe.us.com.", "_probe.uk.com.", "_probe.uk.uk.", "_probe.uk.net." };
return choices[ ldns_get_random() % 4 ];
}
/** the NSEC3 qtype to elicit it (a nodata answer) */
#define PROBE_NSEC3_QTYPE LDNS_RR_TYPE_NULL
/** get random authority server */
static const char*
get_random_auth_ip4(void)
{
/* list of root servers */
const char* choices[] = {
"198.41.0.4", /* a */
"192.228.79.201", /* b */
"192.33.4.12", /* c */
"199.7.91.13", /* d */
"192.203.230.10", /* e */
"192.5.5.241", /* f */
"192.112.36.4", /* g */
"198.97.190.53", /* h */
"192.36.148.17", /* i */
"192.58.128.30", /* j */
"193.0.14.129", /* k */
"199.7.83.42", /* l */
"202.12.27.33" /* m */
};
return choices[ ldns_get_random() % 13 ];
}
/** get random authority server */
static const char*
get_random_auth_ip6(void)
{
/* list of root servers */
const char* choices[] = {
"2001:503:ba3e::2:30", /* a */
"2001:500:200::b", /* b */
"2001:500:2::c", /* c */
"2001:500:2d::d", /* d */
"2001:500:a8::e", /* e */
"2001:500:2f::f", /* f */
"2001:500:12::d0d", /* g */
"2001:500:1::53", /* h */
"2001:7fe::53", /* i */
"2001:503:c27::2:30", /* j */
"2001:7fd::1", /* k */
"2001:500:9f::42", /* l */
"2001:dc3::35" /* m */
};
return choices[ ldns_get_random() % 13 ];
}
static const char* get_random_tcp80_ip4(struct cfg* cfg)
{
if(cfg->num_tcp80_ip4 == 0)
return NULL;
return strlist_get_num(cfg->tcp80_ip4,
((unsigned)ldns_get_random())%((unsigned)cfg->num_tcp80_ip4));
}
static const char* get_random_tcp80_ip6(struct cfg* cfg)
{
if(cfg->num_tcp80_ip6 == 0)
return NULL;
return strlist_get_num(cfg->tcp80_ip6,
((unsigned)ldns_get_random())%((unsigned)cfg->num_tcp80_ip6));
}
static const char* get_random_tcp443_ip4(struct cfg* cfg)
{
if(cfg->num_tcp443_ip4 == 0)
return NULL;
return strlist_get_num(cfg->tcp443_ip4,
((unsigned)ldns_get_random())%((unsigned)cfg->num_tcp443_ip4));
}
static const char* get_random_tcp443_ip6(struct cfg* cfg)
{
if(cfg->num_tcp443_ip6 == 0)
return NULL;
return strlist_get_num(cfg->tcp443_ip6,
((unsigned)ldns_get_random())%((unsigned)cfg->num_tcp443_ip6));
}
static struct ssllist* get_random_ssl443_ip4(struct cfg* cfg)
{
if(cfg->num_ssl443_ip4 == 0)
return NULL;
return ssllist_get_num(cfg->ssl443_ip4,
((unsigned)ldns_get_random())%((unsigned)cfg->num_ssl443_ip4));
}
static struct ssllist* get_random_ssl443_ip6(struct cfg* cfg)
{
if(cfg->num_ssl443_ip6 == 0)
return NULL;
return ssllist_get_num(cfg->ssl443_ip6,
((unsigned)ldns_get_random())%((unsigned)cfg->num_ssl443_ip6));
}
int probe_is_cache(struct probe_ip* p)
{
return !p->to_auth && !p->ssldns && !p->dnstcp && !p->to_http;
}
/** check if hash of x is the given hash, error or NULL on success. */
static char*
match_hash(struct hashlist* he, X509* x)
{
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int len = (unsigned int)sizeof(hash);
/* always sha256 now */
if(!X509_digest(x, EVP_sha256(), hash, &len)) {
log_err("out of memory");
return "out of memory in X509_digest";
}
if(verbosity >= 3) {
char buf[1024];
char* at = buf;
size_t blen = sizeof(buf);
unsigned int i;
for(i=0; i<len; i++) {
snprintf(at, blen, "%2.2X%s",
(unsigned int)hash[i], (i==len-1)?"":":");
blen -= strlen(at);
at += strlen(at);
}
log_info("the ssl fingerprint of the server is %s", buf);
}
if(len != he->hashlen) {
/* should not happen, since we test sha256 length of
* the cfg hash we read in */
return "SSL certificate hash length is wrong";
}
if(memcmp(hash, he->hash, len) != 0) {
return "SSL certificate on internet does not match stored hash (ssl intercepted?)";
}
return 0;
}
/** check list of hashes, error string or NULL on success */
static char*
match_hashes(struct hashlist* list, X509* x)
{
struct hashlist* h;
char* reason = "Internal error: no hashlist but comparison";
for(h=list; h; h=h->next)
if( (reason=match_hash(h, x)) == NULL)
return NULL;
return reason;
}
/** check SSL certificate on probe (that is otherwise DNSSEC OK) */
static const char*
check_ssl(struct outq* outq)
{
/* verify the ssl connection */
/* there is also the SSL_get_verify_result, but that does PKIX
* verification, we simply check the entire self-signed cert with
* a stored hash, you can compute this hash with the command
* openssl x509 -sha256 -fingerprint -in server.pem */
X509* x = SSL_get_peer_certificate(outq->c->ssl);
if(!outq->probe->ssldns->hashes) {
/* no stored hash */
X509_free(x);
} else if(x) {
char* reason = match_hashes(outq->probe->ssldns->hashes, x);
X509_free(x);
if(reason)
return reason;
} else {
return "No SSL certificate on internet but have stored hash (ssl intercepted?)";
}
/* all OK */
return NULL;
}
/** outq is done, NULL reason for success */
static void
outq_done(struct outq* outq, const char* reason)
{
struct probe_ip* p = outq->probe;
const char* in = NULL;
if(!p) {
selfupdate_outq_done(global_svr->update, outq, NULL, reason);
return;
}
if(p->sslctx && !reason) {
reason = check_ssl(outq);
}
if(p->nsec3_c == outq) {
outq_delete(p->nsec3_c);
p->nsec3_c = NULL;
in = "NSEC3";
} else if(p->host_c == outq) {
http_host_outq_done(p, reason);
return;
} else if(p->ds_c == outq) {
outq_delete(p->ds_c);
p->ds_c = NULL;
in = "DS";
} else {
outq_delete(p->dnskey_c);
p->dnskey_c = NULL;
in = "DNSKEY";
}
/* This is a good place for test code.
if(!p->ssldns && !reason)
reason = "failed for test purposes";
*/
probe_partial_done(p, in, reason);
}
/** test if type is present in authority section of returned packet */
static int
check_type_in_authority(ldns_pkt* p, int t)
{
ldns_rr_list *l = ldns_pkt_rr_list_by_type(p, t,
LDNS_SECTION_AUTHORITY);
if(!l) {
return 0;
}
ldns_rr_list_deep_free(l);
return 1;
}
/** test if type is present in returned packet */
static int
check_type_in_answer(ldns_pkt* p, int t)
{
ldns_rr_list *l = ldns_pkt_rr_list_by_type(p, t, LDNS_SECTION_ANSWER);
if(!l) {
return 0;
}
ldns_rr_list_deep_free(l);
return 1;
}
/** test if the right denial (from parent) is in the returned packet */
static int
check_denial_in_answer(ldns_pkt* p, const char* dname)
{
size_t i;
ldns_rdf* d = ldns_dname_new_frm_str(dname);
ldns_rr_list *l = ldns_pkt_rr_list_by_type(p, LDNS_RR_TYPE_SOA,
LDNS_SECTION_AUTHORITY);
if(!d) {
ldns_rr_list_deep_free(l);
return 0; /* robustness, the name should parse */
}
if(!l) {
ldns_rdf_deep_free(d);
return 0;
}
for(i=0; i<ldns_rr_list_rr_count(l); i++) {
/* note that subdomain test is false if names are equal,
* the SOA must be from a parent server */
if(ldns_dname_is_subdomain(d, ldns_rr_owner(ldns_rr_list_rr(
l, i)))) {
ldns_rr_list_deep_free(l);
ldns_rdf_deep_free(d);
return 1;
}
}
ldns_rr_list_deep_free(l);
ldns_rdf_deep_free(d);
return 0;
}
static void
outq_check_packet(struct outq* outq, uint8_t* wire, size_t len)
{
char reason[512];
int rrsig_in_auth = 0;
ldns_pkt *p = NULL;
ldns_status s;
if(verbosity >= VERB_ALGO) {
if(!outq->probe) {
verbose(VERB_ALGO, "%s %s received", outq->qname,
outq->qtype==LDNS_RR_TYPE_TXT?"TXT":(
outq->qtype==LDNS_RR_TYPE_A?"A":(
outq->qtype==LDNS_RR_TYPE_AAAA?"AAAA":"other"
)));
} else if(outq->probe->to_http) {
verbose(VERB_ALGO, "from %s %s received",
outq->probe->name, outq->probe->http_ip6?
"HTTPip6":"HTTPip4");
} else
verbose(VERB_ALGO, "from %s %s %s received",
outq->probe->name,
outq->qtype==PROBE_NSEC3_QTYPE?"NSEC3":(
outq->qtype==LDNS_RR_TYPE_DNSKEY?"DNSKEY":"DS"),
outq->probe->ssldns?"SSLprobe":(
outq->probe->dnstcp?"TCPprobe":(
outq->probe->to_auth?"AUTHprobe":"cacheprobe"
)));
log_hex("packet", wire, len);
}
if(outq->probe)
outq->probe->got_packet = 1;
if(!LDNS_QR_WIRE(wire)) {
outq_done(outq, "reply without QR flag");
return;
}
if(LDNS_TC_WIRE(wire)) {
/* start TCP query and wait for it */
verbose(VERB_ALGO, "%s: TC flag, switching to TCP",
outq->probe?outq->probe->name:outq->qname);
if(!outq_send_tcp(outq)) {
outq_done(outq, "cannot send TCP query after TC flag");
}
return;
}
if( (s=ldns_wire2pkt(&p, wire, len)) != LDNS_STATUS_OK) {
snprintf(reason, sizeof(reason), "cannot disassemble reply: %s",
ldns_get_errorstr_by_id(s));
outq_done(outq, reason);
return;
}
if(!p) {
outq_done(outq, "out of memory");
return;
}
if(verbosity >= VERB_ALGO) {
char* desc = ldns_pkt2str(p);
if(desc) verbose(VERB_ALGO, "%s", desc);
free(desc);
}
/* does DNS work? */
if(ldns_pkt_get_rcode(p) != LDNS_RCODE_NOERROR &&
ldns_pkt_get_rcode(p) != LDNS_RCODE_NXDOMAIN) {
char* r = ldns_pkt_rcode2str(ldns_pkt_get_rcode(p));
snprintf(reason, sizeof(reason), "no answer, %s",
r?r:"(out of memory)");
outq_done(outq, reason);
LDNS_FREE(r);
ldns_pkt_free(p);
return;
}
if(!outq->probe) {
selfupdate_outq_done(global_svr->update, outq, p, NULL);
return;
}
/* if this all OK, and addr, then use http addr process */
if(outq == outq->probe->host_c) {
/* this routine frees pkt */
http_host_outq_result(outq->probe, p);
return;
}
/* test EDNS0 presence, of OPT record */
/* LDNS forgets during pkt parse, but we test the ARCOUNT;
* 0 additionals means no EDNS(on the wire), and after parsing the
* same additional RRs as before means no EDNS OPT */
if(LDNS_ARCOUNT(wire) == 0 ||
ldns_pkt_arcount(p) == LDNS_ARCOUNT(wire)) {
outq_done(outq, "no EDNS");
ldns_pkt_free(p);
return;
}
/* test if the type, RRSIG present */
if(outq->qtype == PROBE_NSEC3_QTYPE) {
if(!check_type_in_authority(p, LDNS_RR_TYPE_NSEC3)) {
outq_done(outq, "no NSEC3 in nodata reply");
ldns_pkt_free(p);
return;
}
rrsig_in_auth = 1;
} else if(!check_type_in_answer(p, (int)outq->qtype)) {
if(outq->qtype == LDNS_RR_TYPE_DS) {
/* if type DS, and it is not present, it is OK if
* we get a proper denial from the parent with NSEC */
if(!check_denial_in_answer(p, outq->qname)) {
outq_done(outq, "no DS and no proper "
"denial in reply");
ldns_pkt_free(p);
return;
}
if(!check_type_in_authority(p, LDNS_RR_TYPE_NSEC)) {
outq_done(outq, "no NSEC in denial reply");
ldns_pkt_free(p);
return;
}
rrsig_in_auth = 1;
} else {
/* failed to find type */
char* r = ldns_rr_type2str(outq->qtype);
snprintf(reason, sizeof(reason),
"no %s in reply", r?r:"DNSSEC-RRTYPE");
outq_done(outq, reason);
LDNS_FREE(r);
ldns_pkt_free(p);
return;
}
}
if(rrsig_in_auth) {
if(!check_type_in_authority(p, LDNS_RR_TYPE_RRSIG)) {
outq_done(outq, "no RRSIGs in reply");
ldns_pkt_free(p);
return;
}
} else {
if(!check_type_in_answer(p, LDNS_RR_TYPE_RRSIG)) {
outq_done(outq, "no RRSIGs in reply");
ldns_pkt_free(p);
return;
}
}
/* for authoritative probes we try to detect transparent proxies
* that mess with the answer, we have to avoid them, if they
* worked well, the DHCP DNS cache would work well. */
if(!outq->recurse) {
if(LDNS_RA_WIRE(wire)) {
outq_done(outq, "authority response has RA flag");
ldns_pkt_free(p);
return;
}
if(!LDNS_AA_WIRE(wire)) {
outq_done(outq, "authority response misses AA flag");
ldns_pkt_free(p);
return;
}
}
outq_done(outq, NULL);
ldns_pkt_free(p);
}
int outq_handle_udp(struct comm_point* c, void* my_arg, int error,
struct comm_reply *reply_info)
{
struct outq* outq = (struct outq*)my_arg;
uint8_t* wire = ldns_buffer_begin(c->buffer);
size_t len = ldns_buffer_limit(c->buffer);
if(error != NETEVENT_NOERROR) {
verbose(VERB_ALGO, "udp receive error");
return 0;
}
if(sockaddr_cmp(&outq->addr, outq->addrlen, &reply_info->addr,
reply_info->addrlen) != 0) {
/* from wrong source, keep listening for the real one */
log_addr(VERB_ALGO, "reply from wrong source",
&reply_info->addr, reply_info->addrlen);
return 0;
}
/* quick sanity check */
if(len < LDNS_HEADER_SIZE || LDNS_ID_WIRE(wire) != outq->qid
|| !LDNS_QR_WIRE(wire)) {
verbose(VERB_ALGO, "%4.4x wire, qid %4.4x, qr %s",
LDNS_ID_WIRE(wire), outq->qid,
LDNS_QR_WIRE(wire)?"yes":"no");
/* wait for the real reply */
verbose(VERB_ALGO, "ignored bad reply (tooshort, wrong qid or noQR)");
return 0;
}
comm_timer_disable(outq->timer);
outq_check_packet(outq, wire, len);
return 0;
}
static int
create_probe_query(struct outq* outq, ldns_buffer* buffer)
{
uint16_t flags = 0;
ldns_pkt* pkt = NULL;
ldns_status status;
if(outq->recurse)
flags |= LDNS_RD;
if(outq->cdflag)
flags |= LDNS_CD;
status = ldns_pkt_query_new_frm_str(&pkt, outq->qname, outq->qtype,
LDNS_RR_CLASS_IN, flags);
if(status != LDNS_STATUS_OK) {
log_err("could not pkt_query_new %s",
ldns_get_errorstr_by_id(status));
return 0;
}
if(outq->edns) {
ldns_pkt_set_edns_do(pkt, 1);
ldns_pkt_set_edns_udp_size(pkt, 4096);
} else {
ldns_pkt_set_edns_do(pkt, 0);
}
ldns_pkt_set_id(pkt, outq->qid);
ldns_buffer_clear(buffer);
status = ldns_pkt2buffer_wire(buffer, pkt);
if(status != LDNS_STATUS_OK) {
log_err("could not host2wire packet %s",
ldns_get_errorstr_by_id(status));
ldns_pkt_free(pkt);
return 0;
}
ldns_buffer_flip(buffer);
ldns_pkt_free(pkt);
return 1;
}
struct outq*
outq_create(const char* ip, int tp, const char* domain, int recurse,
struct probe_ip* p, int tcp, int onssl, int port, int edns, int cdflag)
{
int fd;
struct outq* outq = (struct outq*)calloc(1, sizeof(*outq));
/* open UDP socket */
if(!outq) {
log_err("out of memory");
return NULL;
}
outq->qname = domain;
outq->probe = p;
outq->qtype = (uint16_t)tp;
outq->qid = (uint16_t)ldns_get_random();
outq->recurse = recurse;
outq->on_ssl = onssl;
outq->port = port;
outq->edns = edns;
outq->cdflag = cdflag;
if(!ipstrtoaddr(ip, port, &outq->addr, &outq->addrlen)) {
log_err("could not parse ip %s", ip);
free(outq);
return NULL;
}
outq->timer = comm_timer_create(global_svr->base, &outq_timeout, outq);
if(!outq->timer) {
log_err("cannot create timer");
outq_delete(outq);
return NULL;
}
if(tcp || onssl){
/* also sets timeout, timer */
/* we test if it works, because if it were to call outq_done
* with an error reason the query counts go wrong and the
* probe does not work correctly. */
if(!outq_send_tcp(outq)) {
outq_delete(outq);
return NULL;
}
return outq;
}
fd = socket(strchr(ip, ':')?PF_INET6:PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(fd == -1) {
#ifndef USE_WINSOCK
if(errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
if(verbosity <= 2) {
free(outq);
return NULL;
}
}
#else
if(WSAGetLastError() == WSAEAFNOSUPPORT ||
WSAGetLastError() == WSAEPROTONOSUPPORT) {
if(verbosity <= 2) {
free(outq);
return NULL;
}
}
#endif
log_err("socket %s udp: %s", strchr(ip, ':')?"ip6":"ip4",
strerror(errno));
free(outq);
return NULL;
}
outq->c = comm_point_create_udp(global_svr->base, fd,
global_svr->udp_buffer, &outq_handle_udp, outq);
if(!outq->c) {
#ifndef USE_WINSOCK
close(fd);
#else
closesocket(fd);
#endif
free(outq);
return NULL;
}
/* set timeout on commpoint */
outq->timeout = QUERY_START_TIMEOUT; /* msec */
if(!outq_settimeout_and_send(outq)) {
outq_delete(outq);
return NULL;
}
return outq;
}
void outq_delete(struct outq* outq)
{
if(!outq) return;
comm_timer_delete(outq->timer);
comm_point_delete(outq->c);
free(outq);
}
static void outq_settimer(struct outq* outq)
{
struct timeval tv;
tv.tv_sec = outq->timeout/1000;
tv.tv_usec = (outq->timeout%1000)*1000;
comm_timer_set(outq->timer, &tv);
}
static int outq_settimeout_and_send(struct outq* outq)
{
ldns_buffer* udpbuf = global_svr->udp_buffer;
outq_settimer(outq);
/* create and send a message over the fd */
if(!create_probe_query(outq, udpbuf)) {
log_err("cannot create probe query");
return 0;
}
/* send it */
if(!comm_point_send_udp_msg(outq->c, udpbuf,
(struct sockaddr*)&outq->addr, outq->addrlen)) {
log_err("could not UDP send to ip %s", outq->probe->name);
return 0;
}
return 1;
}
void outq_timeout(void* arg)
{
struct outq* outq = (struct outq*)arg;
char *t = ldns_rr_type2str(outq->qtype);
verbose(VERB_ALGO, "%s %s: UDP timeout after %d msec",
outq->probe?outq->probe->name:outq->qname, t, outq->timeout);
free(t);
if(outq->timeout > QUERY_END_TIMEOUT) {
/* too many timeouts */
outq_done(outq, "timeout");
return;
}
/* resend */
outq->timeout *= 2;
if(!outq_settimeout_and_send(outq)) {
outq_done(outq, "could not resend after timeout");
return;
}
}
/** use next free buffer to service a tcp query */
static int
outq_tcp_take_into_use(struct outq* outq)
{
int s;
/* open socket */
#ifdef INET6
if(strchr(outq->probe->name, ':'))
s = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
else
#endif
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(s == -1) {
#ifndef USE_WINSOCK
if(errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
if(verbosity <= 2) {
return 0;
}
}
log_err("outgoing tcp: socket: %s", strerror(errno));
#else
if(WSAGetLastError() == WSAEAFNOSUPPORT ||
WSAGetLastError() == WSAEPROTONOSUPPORT) {
if(verbosity <= 2) {
return 0;
}
}
log_err("outgoing tcp: socket: %s",
wsa_strerror(WSAGetLastError()));
#endif
log_addr(VERB_QUERY, "failed address",
&outq->addr, outq->addrlen);
return 0;
}
fd_set_nonblock(s);
if(connect(s, (struct sockaddr*)&outq->addr, outq->addrlen) == -1) {
#ifndef USE_WINSOCK
#ifdef EINPROGRESS
if(errno != EINPROGRESS) {
#else
if(1) {
#endif
log_err("outgoing tcp: connect: %s", strerror(errno));
close(s);
#else /* USE_WINSOCK */
if(WSAGetLastError() != WSAEINPROGRESS &&
WSAGetLastError() != WSAEWOULDBLOCK) {
closesocket(s);
#endif
log_addr(VERB_OPS, "failed address",
&outq->addr, outq->addrlen);
return 0;
}
}
if(outq->on_ssl) {
outq->c->ssl = outgoing_ssl_fd(outq->probe->sslctx, s);
if(!outq->c->ssl) {
outq->c->fd = s;
comm_point_close(outq->c);
return 0;
}
#ifdef USE_WINSOCK
comm_point_tcp_win_bio_cb(outq->c, outq->c->ssl);
#endif
outq->c->ssl_shake_state = comm_ssl_shake_write;
}
outq->c->repinfo.addrlen = outq->addrlen;
memcpy(&outq->c->repinfo.addr, &outq->addr, outq->addrlen);
outq->c->tcp_is_reading = 0;
outq->c->tcp_byte_count = 0;
comm_point_start_listening(outq->c, s, -1);
return 1;
}
static int outq_send_tcp(struct outq* outq)
{
/* send outq over tcp, stop UDP in progress (if any) */
if(outq->c) comm_point_delete(outq->c);
outq->timeout = QUERY_TCP_TIMEOUT;
outq->on_tcp = 1;
outq->qid = (uint16_t)ldns_get_random();
outq->c = comm_point_create_tcp_out(global_svr->base, 65553,
outq_handle_tcp, outq);
if(!outq->c) {
log_err("cannot create tcp comm point, out of memory");
return 0;
}
if(!create_probe_query(outq, outq->c->buffer))
return 0;
if(!outq_tcp_take_into_use(outq))
return 0;
outq_settimer(outq);
return 1;
}
int outq_handle_tcp(struct comm_point* c, void* my_arg, int error,
struct comm_reply* ATTR_UNUSED(reply_info))
{
struct outq* outq = (struct outq*)my_arg;
uint8_t* wire = ldns_buffer_begin(c->buffer);
size_t len = ldns_buffer_limit(c->buffer);
if(error != NETEVENT_NOERROR) {
if(error == NETEVENT_CLOSED)
outq_done(outq, "TCP connection failure");
else outq_done(outq, "TCP receive error");
return 0;
}
/* quick sanity check */
if(len < LDNS_HEADER_SIZE) {
outq_done(outq, "TCP reply with short header");
return 0;
}
if(LDNS_ID_WIRE(wire) != outq->qid) {
outq_done(outq, "TCP reply with wrong ID");
return 0;
}
comm_timer_disable(outq->timer);
outq_check_packet(outq, wire, len);
return 0;
}
static int addr_is_localhost(const char* ip)
{
struct sockaddr_storage addr;
socklen_t len;
struct sockaddr_storage lo;
socklen_t lolen;
/* unified print format for ipv4 addresses */
if(strcmp(ip, "127.0.0.1") == 0)
return 1;
/* only for IPv6 do we need more tests */
if(!strchr(ip, ':'))
return 0;
/* detect ::ffff:127.0.0.1 */
if(strstr(ip, "127.0.0.1"))
return 1;
/* detect ::1 but there are many ways to denote that IPv6 */
if(!ipstrtoaddr(ip, DNS_PORT, &addr, &len)) {
return 0; /* it is not localhost, but unparseable */
}
if(!ipstrtoaddr("::1", DNS_PORT, &lo, &lolen)) {
return 0; /* internal error or no IPv6 */
}
return (sockaddr_cmp_addr(&lo, lolen, &addr, len) == 0);
}
static void probe_spawn(const char* ip, int recurse, int dnstcp,
struct ssllist* ssldns, int port)
{
const char* dest;
struct probe_ip* p;
if(!ip || ip[0]==0) return;
/* create a probe for this IP */
p = (struct probe_ip*)calloc(1, sizeof(*p));
if(!p) {
log_err("out of memory");
return;
}
/* make sure the IP address is not 127.0.0.1 or ::1, that would
* create a forward-loop for the resolver */
if(addr_is_localhost(ip)) {
free(p);
verbose(VERB_ALGO, "skip localhost address %s", ip);
return;
}
/* create probe structure and register it */
p->to_auth = !recurse;
p->dnstcp = dnstcp;
p->ssldns = ssldns;
p->port = port;
p->got_packet = 0;
p->name = strdup(ip);
if(!p->name) {
free(p);
log_err("out of memory");
return;
}
if(p->ssldns) {
/* this could contain verification certificates */
p->sslctx = connect_sslctx_create(NULL, NULL, NULL);
if(!p->sslctx) {
log_err("could not create sslctx for %s", p->name);
probe_delete(p);
return;
}
}
/* send the queries */
dest = get_random_dest();
verbose(VERB_ALGO, "probe %s %s %s (tld %s)",
p->name, (recurse?"rec":"norec"), ssldns?"ssl":(dnstcp?"tcp":"udp"), dest);