forked from nmap/nmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraceroute.cc
1664 lines (1459 loc) · 57.2 KB
/
traceroute.cc
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
/***************************************************************************
* traceroute.cc -- Parallel multi-protocol traceroute feature *
* *
***********************IMPORTANT NMAP LICENSE TERMS************************
* *
* The Nmap Security Scanner is (C) 1996-2019 Insecure.Com LLC ("The Nmap *
* Project"). Nmap is also a registered trademark of the Nmap Project. *
* This program is free software; you may redistribute and/or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE *
* CLARIFICATIONS AND EXCEPTIONS DESCRIBED HEREIN. This guarantees your *
* right to use, modify, and redistribute this software under certain *
* conditions. If you wish to embed Nmap technology into proprietary *
* software, we sell alternative licenses (contact [email protected]). *
* Dozens of software vendors already license Nmap technology such as *
* host discovery, port scanning, OS detection, version detection, and *
* the Nmap Scripting Engine. *
* *
* Note that the GPL places important restrictions on "derivative works", *
* yet it does not provide a detailed definition of that term. To avoid *
* misunderstandings, we interpret that term as broadly as copyright law *
* allows. For example, we consider an application to constitute a *
* derivative work for the purpose of this license if it does any of the *
* following with any software or content covered by this license *
* ("Covered Software"): *
* *
* o Integrates source code from Covered Software. *
* *
* o Reads or includes copyrighted data files, such as Nmap's nmap-os-db *
* or nmap-service-probes. *
* *
* o Is designed specifically to execute Covered Software and parse the *
* results (as opposed to typical shell or execution-menu apps, which will *
* execute anything you tell them to). *
* *
* o Includes Covered Software in a proprietary executable installer. The *
* installers produced by InstallShield are an example of this. Including *
* Nmap with other software in compressed or archival form does not *
* trigger this provision, provided appropriate open source decompression *
* or de-archiving software is widely available for no charge. For the *
* purposes of this license, an installer is considered to include Covered *
* Software even if it actually retrieves a copy of Covered Software from *
* another source during runtime (such as by downloading it from the *
* Internet). *
* *
* o Links (statically or dynamically) to a library which does any of the *
* above. *
* *
* o Executes a helper program, module, or script to do any of the above. *
* *
* This list is not exclusive, but is meant to clarify our interpretation *
* of derived works with some common examples. Other people may interpret *
* the plain GPL differently, so we consider this a special exception to *
* the GPL that we apply to Covered Software. Works which meet any of *
* these conditions must conform to all of the terms of this license, *
* particularly including the GPL Section 3 requirements of providing *
* source code and allowing free redistribution of the work as a whole. *
* *
* As another special exception to the GPL terms, the Nmap Project grants *
* permission to link the code of this program with any version of the *
* OpenSSL library which is distributed under a license identical to that *
* listed in the included docs/licenses/OpenSSL.txt file, and distribute *
* linked combinations including the two. *
* *
* The Nmap Project has permission to redistribute Npcap, a packet *
* capturing driver and library for the Microsoft Windows platform. *
* Npcap is a separate work with it's own license rather than this Nmap *
* license. Since the Npcap license does not permit redistribution *
* without special permission, our Nmap Windows binary packages which *
* contain Npcap may not be redistributed without special permission. *
* *
* Any redistribution of Covered Software, including any derived works, *
* must obey and carry forward all of the terms of this license, including *
* obeying all GPL rules and restrictions. For example, source code of *
* the whole work must be provided and free redistribution must be *
* allowed. All GPL references to "this License", are to be treated as *
* including the terms and conditions of this license text as well. *
* *
* Because this license imposes special exceptions to the GPL, Covered *
* Work may not be combined (even as part of a larger work) with plain GPL *
* software. The terms, conditions, and exceptions of this license must *
* be included as well. This license is incompatible with some other open *
* source licenses as well. In some cases we can relicense portions of *
* Nmap or grant special permissions to use it in other open source *
* software. Please contact [email protected] with any such requests. *
* Similarly, we don't incorporate incompatible open source software into *
* Covered Software without special permission from the copyright holders. *
* *
* If you have any questions about the licensing restrictions on using *
* Nmap in other works, we are happy to help. As mentioned above, we also *
* offer an alternative license to integrate Nmap into proprietary *
* applications and appliances. These contracts have been sold to dozens *
* of software vendors, and generally include a perpetual license as well *
* as providing support and updates. They also fund the continued *
* development of Nmap. Please email [email protected] for further *
* information. *
* *
* If you have received a written license agreement or contract for *
* Covered Software stating terms other than these, you may choose to use *
* and redistribute Covered Software under those terms instead of these. *
* *
* Source is provided to this software because we believe users have a *
* right to know exactly what a program is going to do before they run it. *
* This also allows you to audit the software for security holes. *
* *
* Source code also allows you to port Nmap to new platforms, fix bugs, *
* and add new features. You are highly encouraged to send your changes *
* to the [email protected] mailing list for possible incorporation into the *
* main distribution. By sending these changes to Fyodor or one of the *
* Insecure.Org development mailing lists, or checking them into the Nmap *
* source code repository, it is understood (unless you specify *
* otherwise) that you are offering the Nmap Project the unlimited, *
* non-exclusive right to reuse, modify, and relicense the code. Nmap *
* will always be available Open Source, but this is important because *
* the inability to relicense code has caused devastating problems for *
* other Free Software projects (such as KDE and NASM). We also *
* occasionally relicense the code to third parties as discussed above. *
* If you wish to specify special license conditions of your *
* contributions, just say so when you send them. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Nmap *
* license file for more details (it's in a COPYING file included with *
* Nmap, and also available from https://svn.nmap.org/nmap/COPYING) *
* *
***************************************************************************/
/*
Traceroute for Nmap. This traceroute is faster than a traditional traceroute
because it sends several probes in parallel and detects shared traces.
The algorithm works by sending probes with varying TTL values and waiting for
TTL_EXCEEDED messages. As intermediate hops are discovered, they are entered
into a global hop cache that is shared between targets and across host groups.
When a hop is discovered and is found to be already in the cache, the trace for
that target is linked into the cached trace and there is no need to try lower
TTLs. The process results in the building of a tree of Hop structures.
The order in which probes are sent does not matter to the accuracy of the
algorithm but it does matter to the speed. The sooner a shared trace can be
detected, and the higher the TTL at which it is detected, the fewer probes need
to be sent. The ideal situation is to start sending probes with a TTL equal to
the true distance and count downward from there. In that case it may only be
necessary to send two probes per target: one at the distance of the target to
get a response, and one at distance - 1 to get a cache hit. When the distance
isn't known in advance, the algorithm arbitrarily starts at a TTL of 10 and
counts downward, then counts upward from 11 until it reaches the target. So a
typical trace may look like
TTL 10 -> TTL_EXCEEDED
TTL 9 -> TTL_EXCEEDED
TTL 8 -> TTL_EXCEEDED
TTL 7 -> cache hit
TTL 11 -> TTL_EXCEEDED
TTL 12 -> TTL_EXCEEDED
TTL 13 -> SYN/ACK, or whatever is the target's response to the probe
The output for this host would then say "Hops 1-7 are the same as for ...".
The detection of shared traces rests on the assumption that all paths going
through a router at a certain TTL will be identical up to and including the
router. This assumption is not always true. Even if two targets are each one hop
past router X at TTL 10, packets may follow different paths to each host (and
those paths may even change over time). This traceroute algorithm will be fooled
by such a situation, and will report that the paths are identical up to
router X. The only way to be sure is to do a complete trace for each target
individually.
*/
#include "nmap_dns.h"
#include "nmap_error.h"
#include "nmap_tty.h"
#include "osscan2.h"
#include "payload.h"
#include "timing.h"
#include "NmapOps.h"
#include "Target.h"
#include "tcpip.h"
#include "struct_ip.h"
#ifndef IPPROTO_SCTP
#include "libnetutil/netutil.h"
#endif
#include <dnet.h>
#include <algorithm>
#include <list>
#include <map>
#include <set>
#include <vector>
extern NmapOps o;
/* The highest TTL we go up to if the target itself doesn't respond. */
#define MAX_TTL 30
#define MAX_OUTSTANDING_PROBES 10
#define MAX_RESENDS 2
/* In milliseconds. */
#define PROBE_TIMEOUT 1000
/* If the hop cache (including timed-out hops) is bigger than this after a
round, the hop is cleared and rebuilt from scratch. */
#define MAX_HOP_CACHE_SIZE 1000
struct Hop;
class HostState;
class Probe;
/* An object of this class is a (TTL, address) pair that uniquely identifies a
hop. Hops in the hop_cache are indexed by this type. */
struct HopIdent {
u8 ttl;
struct sockaddr_storage addr;
HopIdent(u8 ttl, const struct sockaddr_storage &addr) {
this->addr = addr;
this->ttl = ttl;
}
bool operator<(const struct HopIdent &other) const {
if (ttl < other.ttl)
return true;
else if (ttl > other.ttl)
return false;
else
return sockaddr_storage_cmp(&addr, &other.addr) < 0;
}
};
/* A global random token used to distinguish this traceroute's probes from
those of other traceroutes possibly running on the same machine. */
static u16 global_id;
/* A global cache of known hops, indexed by TTL and address. */
static std::map<struct HopIdent, Hop *> hop_cache;
/* A list of timedout hops, which are not kept in hop_cache, so we can delete
all hops on occasion. */
/* This would be stack-allocated except for a weird bug on AIX that causes
* infinite loops when trying to traverse the list. For some reason,
* dynamically allocating it fixes the bug. */
static std::list<Hop *> *timedout_hops = NULL;
/* The TTL at which we start sending probes if we don't have a distance
estimate. This is updated after each host group on the assumption that hosts
across groups will not differ much in distance. Having this closer to the
true distance makes the trace faster but is not needed for accuracy. */
static u8 initial_ttl = 10;
static struct timeval get_now(struct timeval *now = NULL);
static const char *ss_to_string(const struct sockaddr_storage *ss);
struct Hop {
Hop *parent;
struct sockaddr_storage tag;
/* When addr.ss_family == 0, this hop represents a timeout. */
struct sockaddr_storage addr;
u8 ttl;
float rtt; /* In milliseconds. */
std::string hostname;
Hop() {
this->parent = NULL;
this->addr.ss_family = 0;
this->ttl = 0;
this->rtt = -1.0;
this->tag.ss_family = 0;
}
Hop(u8 ttl, const struct sockaddr_storage &addr, float rtt) {
this->parent = NULL;
this->addr = addr;
this->ttl = ttl;
this->rtt = rtt;
this->tag.ss_family = 0;
}
};
class HostState {
public:
enum counting_state { COUNTING_DOWN, COUNTING_UP };
Target *target;
/* A bitmap of TTLs that have been sent, to avoid duplicates when we switch
around the order counting up or down. */
std::vector<bool> sent_ttls;
u8 current_ttl;
enum counting_state state;
/* If nonzero, the known hop distance to the target. */
int reached_target;
struct probespec pspec;
std::list<Probe *> unanswered_probes;
std::list<Probe *> active_probes;
std::list<Probe *> pending_resends;
Hop *hops;
HostState(Target *target);
~HostState();
bool has_more_probes() const;
bool is_finished() const;
bool send_next_probe(int rawsd, eth_t *ethsd);
void next_ttl();
void count_up();
int cancel_probe(std::list<Probe *>::iterator it);
int cancel_probes_below(u8 ttl);
int cancel_probes_above(u8 ttl);
Hop *insert_hop(u8 ttl, const struct sockaddr_storage *addr, float rtt);
void link_to(Hop *hop);
double completion_fraction() const;
private:
void child_parent_ttl(u8 ttl, Hop **child, Hop **parent);
static u8 distance_guess(const Target *target);
static struct probespec get_probe(const Target *target);
};
class Probe {
private:
/* This is incremented with each instantiated probe. */
static u16 token_counter;
unsigned int num_resends;
public:
HostState *host;
struct probespec pspec;
u8 ttl;
/* The token is used to match up probe replies. */
u16 token;
struct timeval sent_time;
Probe(HostState *host, struct probespec pspec, u8 ttl);
virtual ~Probe();
void send(int rawsd, eth_t *ethsd, struct timeval *now = NULL);
void resend(int rawsd, eth_t *ethsd, struct timeval *now = NULL);
bool is_timedout(struct timeval *now = NULL) const;
bool may_resend() const;
virtual unsigned char *build_packet(const struct sockaddr_storage *source,
u32 *len) const = 0;
static Probe *make(HostState *host, struct probespec pspec, u8 ttl);
};
u16 Probe::token_counter = 0x0000;
class TracerouteState {
public:
std::list<HostState *> active_hosts;
/* The next send time for enforcing scan delay. */
struct timeval next_send_time;
TracerouteState(std::vector<Target *> &targets);
~TracerouteState();
void send_new_probes();
void read_replies(long timeout);
void cull_timeouts();
void remove_finished_hosts();
void resolve_hops();
void transfer_hops();
double completion_fraction() const;
private:
eth_t *ethsd;
int rawsd;
pcap_t *pd;
int num_active_probes;
std::vector<HostState *> hosts;
std::list<HostState *>::iterator next_sending_host;
void next_active_host();
Probe *lookup_probe(const struct sockaddr_storage *target_addr, u16 token);
void set_host_hop(HostState *host, u8 ttl,
const struct sockaddr_storage *from_addr, float rtt);
void set_host_hop_timedout(HostState *host, u8 ttl);
};
static Hop *merge_hops(const struct sockaddr_storage *tag, Hop *a, Hop *b);
static Hop *hop_cache_lookup(u8 ttl, const struct sockaddr_storage *addr);
static void hop_cache_insert(Hop *hop);
static unsigned int hop_cache_size();
HostState::HostState(Target *target) : sent_ttls(MAX_TTL + 1, false) {
this->target = target;
current_ttl = MIN(MAX(1, HostState::distance_guess(target)), MAX_TTL);
state = HostState::COUNTING_DOWN;
reached_target = 0;
pspec = HostState::get_probe(target);
hops = NULL;
}
HostState::~HostState() {
/* active_probes and pending_resends are subsets of unanswered_probes, so we
delete the allocated probes in unanswered_probes only. */
while (!unanswered_probes.empty()) {
delete *unanswered_probes.begin();
unanswered_probes.pop_front();
}
while (!active_probes.empty())
active_probes.pop_front();
while (!pending_resends.empty())
pending_resends.pop_front();
}
bool HostState::has_more_probes() const {
/* We are done if we are counting up and
1. we've reached and exceeded the target, or
2. we've exceeded MAX_TTL. */
return !(state == HostState::COUNTING_UP
&& ((reached_target > 0 && current_ttl >= reached_target)
|| current_ttl > MAX_TTL));
}
bool HostState::is_finished() const {
return !this->has_more_probes()
&& active_probes.empty() && pending_resends.empty();
}
bool HostState::send_next_probe(int rawsd, eth_t *ethsd) {
Probe *probe;
/* Do a resend if possible. */
if (!pending_resends.empty()) {
probe = pending_resends.front();
pending_resends.pop_front();
active_probes.push_back(probe);
probe->resend(rawsd, ethsd);
return true;
}
this->next_ttl();
if (!this->has_more_probes())
return false;
probe = Probe::make(this, pspec, current_ttl);
unanswered_probes.push_back(probe);
active_probes.push_back(probe);
probe->send(rawsd, ethsd);
sent_ttls[current_ttl] = true;
return true;
}
/* Find the next TTL we should send to. */
void HostState::next_ttl() {
assert(current_ttl > 0);
if (state == HostState::COUNTING_DOWN) {
while (current_ttl > 1 && sent_ttls[current_ttl])
current_ttl--;
if (current_ttl == 1)
state = HostState::COUNTING_UP;
}
/* Note no "else". */
if (state == HostState::COUNTING_UP) {
while (current_ttl <= MAX_TTL && sent_ttls[current_ttl])
current_ttl++;
}
}
int HostState::cancel_probe(std::list<Probe *>::iterator it) {
int count;
count = active_probes.size();
active_probes.remove(*it);
count -= active_probes.size();
pending_resends.remove(*it);
delete *it;
unanswered_probes.erase(it);
return count;
}
int HostState::cancel_probes_below(u8 ttl) {
std::list<Probe *>::iterator it, next;
int count;
count = 0;
for (it = unanswered_probes.begin(); it != unanswered_probes.end(); it = next) {
next = it;
next++;
if ((*it)->ttl < ttl)
count += this->cancel_probe(it);
}
return count;
}
int HostState::cancel_probes_above(u8 ttl) {
std::list<Probe *>::iterator it, next;
int count;
count = 0;
for (it = unanswered_probes.begin(); it != unanswered_probes.end(); it = next) {
next = it;
next++;
if ((*it)->ttl > ttl)
count += this->cancel_probe(it);
}
return count;
}
Hop *HostState::insert_hop(u8 ttl, const struct sockaddr_storage *addr,
float rtt) {
Hop *hop, *prev, *p;
this->child_parent_ttl(ttl, &prev, &p);
if (p != NULL && p->ttl == ttl) {
hop = p;
/* Collision with the same TTL and a different address. */
if (hop->addr.ss_family == 0) {
/* Hit a timed-out hop. Fill in the missing address and RTT. */
hop->addr = *addr;
hop->rtt = rtt;
} else {
if (o.debugging) {
log_write(LOG_STDOUT, "Found existing %s", ss_to_string(&hop->addr));
log_write(LOG_STDOUT, " while inserting %s at TTL %d for %s\n",
ss_to_string(addr), ttl, target->targetipstr());
}
}
} else {
hop = new Hop(ttl, *addr, rtt);
hop->parent = p;
if (prev == NULL) {
size_t sslen;
this->hops = hop;
sslen = sizeof(hop->tag);
target->TargetSockAddr(&hop->tag, &sslen);
} else {
prev->parent = hop;
hop->tag = prev->tag;
}
hop_cache_insert(hop);
}
return hop;
}
void HostState::link_to(Hop *hop) {
Hop *prev, *p;
this->child_parent_ttl(hop->ttl, &prev, &p);
if (hop == p) {
/* Already linked for this host. This can happen a reply for a higher TTL
results in a merge, and later a reply for a lower TTL comes back. */
return;
}
if (o.debugging > 1) {
log_write(LOG_STDOUT, "Merging traces below TTL %d for %s",
hop->ttl, ss_to_string(&hop->tag));
log_write(LOG_STDOUT, " and %s\n", target->targetipstr());
}
hop = merge_hops(&hop->tag, hop, p);
if (prev == NULL)
this->hops = hop;
else
prev->parent = hop;
}
double HostState::completion_fraction() const {
std::vector<bool>::iterator it;
unsigned int i, n;
if (this->is_finished())
return 1.0;
n = 0;
for (i = 0; i < sent_ttls.size(); i++) {
if (sent_ttls[i])
n++;
}
return (double) n / sent_ttls.size();
}
void HostState::child_parent_ttl(u8 ttl, Hop **child, Hop **parent) {
*child = NULL;
*parent = this->hops;
while (*parent != NULL && (*parent)->ttl > ttl) {
*child = *parent;
*parent = (*parent)->parent;
}
}
u8 HostState::distance_guess(const Target *target) {
/* Use the distance from OS detection if we have it. */
if (target->distance != -1)
return target->distance;
else
/* initial_ttl is a variable with file-level scope. */
return initial_ttl;
}
/* Get the probe that will be used for the traceroute. This is the
highest-quality probe found in ping or port scanning, or ICMP echo if no
responsive probe is known. */
struct probespec HostState::get_probe(const Target *target) {
struct probespec probe;
probe = target->pingprobe;
if (target->af() == AF_INET &&
(probe.type == PS_TCP || probe.type == PS_UDP || probe.type == PS_SCTP || probe.type == PS_ICMP)) {
/* Nothing needed. */
} else if (target->af() == AF_INET6 &&
(probe.type == PS_TCP || probe.type == PS_UDP || probe.type == PS_SCTP || probe.type == PS_ICMPV6)) {
/* Nothing needed. */
} else if (probe.type == PS_PROTO) {
/* If this is an IP protocol probe, fill in some fields for some common
protocols. We cheat and store them in the TCP-, UDP-, SCTP- and
ICMP-specific fields. */
if (probe.proto == IPPROTO_TCP) {
probe.pd.tcp.flags = TH_ACK;
probe.pd.tcp.dport = get_random_u16();
} else if (probe.proto == IPPROTO_UDP) {
probe.pd.udp.dport = get_random_u16();
} else if (probe.proto == IPPROTO_SCTP) {
probe.pd.sctp.dport = get_random_u16();
} else if (probe.proto == IPPROTO_ICMP) {
probe.pd.icmp.type = ICMP_ECHO;
} else if (probe.proto == IPPROTO_ICMPV6) {
probe.pd.icmp.type = ICMPV6_ECHO;
} else {
fatal("Unknown protocol %d", probe.proto);
}
} else {
/* No responsive probe known? The user probably skipped both ping and
port scan. Guess ICMP echo as the most likely to get a response. */
if (target->af() == AF_INET) {
probe.type = PS_ICMP;
probe.proto = IPPROTO_ICMP;
probe.pd.icmp.type = ICMP_ECHO;
probe.pd.icmp.code = 0;
} else if (target->af() == AF_INET6) {
probe.type = PS_ICMPV6;
probe.proto = IPPROTO_ICMPV6;
probe.pd.icmp.type = ICMPV6_ECHO;
probe.pd.icmp.code = 0;
} else {
fatal("Unknown address family %d", target->af());
}
}
return probe;
}
Probe::Probe(HostState *host, struct probespec pspec, u8 ttl) {
this->host = host;
this->pspec = pspec;
this->ttl = ttl;
token = Probe::token_counter++;
sent_time.tv_sec = 0;
sent_time.tv_usec = 0;
num_resends = 0;
}
Probe::~Probe() {
}
void Probe::send(int rawsd, eth_t *ethsd, struct timeval *now) {
struct eth_nfo eth;
struct eth_nfo *ethp;
int decoy;
/* Set up the Ethernet handle if we're using that. */
if (ethsd != NULL) {
memcpy(eth.srcmac, host->target->SrcMACAddress(), 6);
memcpy(eth.dstmac, host->target->NextHopMACAddress(), 6);
eth.ethsd = ethsd;
eth.devname[0] = '\0';
ethp = ð
} else {
ethp = NULL;
}
for (decoy = 0; decoy < o.numdecoys; decoy++) {
struct sockaddr_storage source;
size_t source_len;
unsigned char *packet;
u32 packetlen;
if (decoy == o.decoyturn) {
source_len = sizeof(source);
host->target->SourceSockAddr(&source, &source_len);
sent_time = get_now(now);
} else {
source = o.decoys[decoy];
}
packet = this->build_packet(&source, &packetlen);
send_ip_packet(rawsd, ethp, host->target->TargetSockAddr(), packet, packetlen);
free(packet);
}
}
void Probe::resend(int rawsd, eth_t *ethsd, struct timeval *now) {
num_resends++;
this->send(rawsd, ethsd, now);
}
bool Probe::is_timedout(struct timeval *now) const {
struct timeval tv;
tv = get_now(now);
return TIMEVAL_MSEC_SUBTRACT(tv, sent_time) > PROBE_TIMEOUT;
}
bool Probe::may_resend() const {
return num_resends < MIN(o.getMaxRetransmissions(), MAX_RESENDS);
}
/* Probe is an abstract class with a missing build_packet method. These concrete
subclasses implement the method for different probe types. */
class ICMPProbe : public Probe
{
public:
ICMPProbe(HostState *host, struct probespec pspec, u8 ttl)
: Probe(host, pspec, ttl) {
}
unsigned char *build_packet(const struct sockaddr_storage *source, u32 *len) const {
const struct sockaddr_in *sin;
assert(source->ss_family == AF_INET);
sin = (struct sockaddr_in *) source;
return build_icmp_raw(&sin->sin_addr, host->target->v4hostip(), ttl,
0x0000, 0x00, false, NULL, 0, token, global_id,
pspec.pd.icmp.type, pspec.pd.icmp.code,
o.extra_payload, o.extra_payload_length, len);
}
};
class TCPProbe : public Probe
{
public:
TCPProbe(HostState *host, struct probespec pspec, u8 ttl)
: Probe(host, pspec, ttl) {
}
unsigned char *build_packet(const struct sockaddr_storage *source, u32 *len) const {
const char *tcpopts;
int tcpoptslen;
u32 ack;
tcpopts = NULL;
tcpoptslen = 0;
ack = 0;
if ((pspec.pd.tcp.flags & TH_SYN) == TH_SYN) {
/* MSS 1460 bytes. */
tcpopts = TCP_SYN_PROBE_OPTIONS;
tcpoptslen = TCP_SYN_PROBE_OPTIONS_LEN;
} else if ((pspec.pd.tcp.flags & TH_ACK) == TH_ACK) {
ack = get_random_u32();
}
/* For TCP we encode the token in the source port. */
if (source->ss_family == AF_INET) {
const struct sockaddr_in *sin = (struct sockaddr_in *) source;
return build_tcp_raw(&sin->sin_addr, host->target->v4hostip(), ttl,
get_random_u16(), get_random_u8(), false, NULL, 0,
token ^ global_id, pspec.pd.tcp.dport, get_random_u32(), ack, 0x00,
pspec.pd.tcp.flags, get_random_u16(), 0, (const u8 *) tcpopts, tcpoptslen,
o.extra_payload, o.extra_payload_length, len);
} else if (source->ss_family == AF_INET6) {
const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) source;
return build_tcp_raw_ipv6(&sin6->sin6_addr, host->target->v6hostip(),
0, 0, ttl,
token ^ global_id, pspec.pd.tcp.dport, get_random_u32(), ack, 0x00,
pspec.pd.tcp.flags, get_random_u16(), 0, (const u8 *) tcpopts, tcpoptslen,
o.extra_payload, o.extra_payload_length, len);
} else {
fatal("Unknown address family %u in %s.", source->ss_family, __func__);
}
}
};
class UDPProbe : public Probe
{
public:
UDPProbe(HostState *host, struct probespec pspec, u8 ttl)
: Probe(host, pspec, ttl) {
}
unsigned char *build_packet(const struct sockaddr_storage *source, u32 *len) const {
const char *payload;
size_t payload_length;
payload = get_udp_payload(pspec.pd.udp.dport, &payload_length);
/* For UDP we encode the token in the source port. */
if (source->ss_family == AF_INET) {
const struct sockaddr_in *sin = (struct sockaddr_in *) source;
return build_udp_raw(&sin->sin_addr, host->target->v4hostip(), ttl,
get_random_u16(), get_random_u8(), false, NULL, 0,
token ^ global_id, pspec.pd.udp.dport,
payload, payload_length, len);
} else if (source->ss_family == AF_INET6) {
const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) source;
return build_udp_raw_ipv6(&sin6->sin6_addr, host->target->v6hostip(),
0, 0, ttl,
token ^ global_id, pspec.pd.udp.dport,
payload, payload_length, len);
} else {
fatal("Unknown address family %u in %s.", source->ss_family, __func__);
}
}
};
class SCTPProbe : public Probe
{
public:
SCTPProbe(HostState *host, struct probespec pspec, u8 ttl)
: Probe(host, pspec, ttl) {
}
unsigned char *build_packet(const struct sockaddr_storage *source, u32 *len) const {
struct sctp_chunkhdr_init chunk;
sctp_pack_chunkhdr_init(&chunk, SCTP_INIT, 0, sizeof(chunk),
get_random_u32() /*itag*/, 32768, 10, 2048, get_random_u32() /*itsn*/);
if (source->ss_family == AF_INET) {
const struct sockaddr_in *sin = (struct sockaddr_in *) source;
return build_sctp_raw(&sin->sin_addr, host->target->v4hostip(), ttl,
get_random_u16(), get_random_u8(), false, NULL, 0,
token ^ global_id, pspec.pd.sctp.dport, 0UL,
(char *) &chunk, sizeof(chunk),
o.extra_payload, o.extra_payload_length, len);
} else if (source->ss_family == AF_INET6) {
const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) source;
return build_sctp_raw_ipv6(&sin6->sin6_addr, host->target->v6hostip(),
0, 0, ttl,
token ^ global_id, pspec.pd.sctp.dport, 0UL,
(char *) &chunk, sizeof(chunk),
o.extra_payload, o.extra_payload_length, len);
} else {
fatal("Unknown address family %u in %s.", source->ss_family, __func__);
}
}
};
class IPProtoProbe : public Probe
{
public:
IPProtoProbe(HostState *host, struct probespec pspec, u8 ttl)
: Probe(host, pspec, ttl) {
}
unsigned char *build_packet(const struct sockaddr_storage *source, u32 *len) const {
/* For IP proto scan the token is put in the IP ID or flow label. */
if (source->ss_family == AF_INET) {
const struct sockaddr_in *sin = (struct sockaddr_in *) source;
return build_ip_raw(&sin->sin_addr, host->target->v4hostip(), pspec.proto, ttl,
token ^ global_id, get_random_u8(), false, NULL, 0,
o.extra_payload, o.extra_payload_length, len);
} else if (source->ss_family == AF_INET6) {
const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) source;
return build_ipv6_raw(&sin6->sin6_addr, host->target->v6hostip(),
0, token ^ global_id, pspec.proto, ttl,
o.extra_payload, o.extra_payload_length, len);
} else {
fatal("Unknown address family %u in %s.", source->ss_family, __func__);
}
}
};
class ICMPv6Probe : public Probe
{
public:
ICMPv6Probe(HostState *host, struct probespec pspec, u8 ttl)
: Probe(host, pspec, ttl) {
}
unsigned char *build_packet(const struct sockaddr_storage *source, u32 *len) const {
const struct sockaddr_in6 *sin6;
assert(source->ss_family == AF_INET6);
sin6 = (struct sockaddr_in6 *) source;
return build_icmpv6_raw(&sin6->sin6_addr, host->target->v6hostip(), 0x00, 0x0000,
ttl, token, global_id, pspec.pd.icmp.type, pspec.pd.icmp.code,
o.extra_payload, o.extra_payload_length, len);
}
};
Probe *Probe::make(HostState *host, struct probespec pspec, u8 ttl)
{
if (pspec.type == PS_ICMP || (pspec.type == PS_PROTO && pspec.proto == IPPROTO_ICMP))
return new ICMPProbe(host, pspec, ttl);
else if (pspec.type == PS_TCP || (pspec.type == PS_PROTO && pspec.proto == IPPROTO_TCP))
return new TCPProbe(host, pspec, ttl);
else if (pspec.type == PS_UDP || (pspec.type == PS_PROTO && pspec.proto == IPPROTO_UDP))
return new UDPProbe(host, pspec, ttl);
else if (pspec.type == PS_SCTP || (pspec.type == PS_PROTO && pspec.proto == IPPROTO_SCTP))
return new SCTPProbe(host, pspec, ttl);
else if (pspec.type == PS_PROTO)
return new IPProtoProbe(host, pspec, ttl);
else if (pspec.type == PS_ICMPV6)
return new ICMPv6Probe(host, pspec, ttl);
else
fatal("Unknown probespec type in traceroute");
return NULL;
}
TracerouteState::TracerouteState(std::vector<Target *> &targets) {
std::vector<Target *>::iterator it;
struct sockaddr_storage srcaddr;
size_t sslen;
char pcap_filter[128];
int n;
assert(targets.size() > 0);
if ((o.sendpref & PACKET_SEND_ETH) && targets[0]->ifType() == devt_ethernet) {
/* No need to check for g_has_npcap_loopback on WIN32 because devt_loopback
* is checked earlier. */
ethsd = eth_open_cached(targets[0]->deviceName());
if (ethsd == NULL)
fatal("dnet: failed to open device %s", targets[0]->deviceName());
rawsd = -1;
} else {
#ifdef WIN32
win32_fatal_raw_sockets(targets[0]->deviceName());
#endif
rawsd = nmap_raw_socket();
if (rawsd < 0)
pfatal("traceroute: socket troubles");
ethsd = NULL;
}
/* Assume that all the targets share the same device. */
if((pd=my_pcap_open_live(targets[0]->deviceName(), 128, o.spoofsource, 2))==NULL)
fatal("%s", PCAP_OPEN_ERRMSG);
sslen = sizeof(srcaddr);
targets[0]->SourceSockAddr(&srcaddr, &sslen);
n = Snprintf(pcap_filter, sizeof(pcap_filter), "(ip or ip6) and dst host %s",
ss_to_string(&srcaddr));
assert(n < (int) sizeof(pcap_filter));
set_pcap_filter(targets[0]->deviceFullName(), pd, pcap_filter);
if (o.debugging)
log_write(LOG_STDOUT, "Packet capture filter (device %s): %s\n", targets[0]->deviceFullName(), pcap_filter);
for (it = targets.begin(); it != targets.end(); it++) {
HostState *state = new HostState(*it);
hosts.push_back(state);
active_hosts.push_back(state);
}
num_active_probes = 0;
next_sending_host = active_hosts.begin();
next_send_time = get_now();
}
TracerouteState::~TracerouteState() {
std::vector<HostState *>::iterator it;
if (rawsd != -1)
close(rawsd);
pcap_close(pd);
for (it = hosts.begin(); it != hosts.end(); it++)
delete *it;
}
void TracerouteState::next_active_host() {
assert(next_sending_host != active_hosts.end());
next_sending_host++;
if (next_sending_host == active_hosts.end())
next_sending_host = active_hosts.begin();
}
void TracerouteState::send_new_probes() {
std::list<HostState *>::iterator failed_host;
struct timeval now;
now = get_now();
assert(!active_hosts.empty());
failed_host = active_hosts.end();
while (next_sending_host != failed_host
&& num_active_probes < MAX_OUTSTANDING_PROBES
&& !TIMEVAL_BEFORE(now, next_send_time)) {
if ((*next_sending_host)->send_next_probe(rawsd, ethsd)) {
num_active_probes++;
TIMEVAL_MSEC_ADD(next_send_time, next_send_time, o.scan_delay);
if (TIMEVAL_BEFORE(next_send_time, now))
next_send_time = now;
failed_host = active_hosts.end();
} else if (failed_host == active_hosts.end()) {
failed_host = next_sending_host;
}
next_active_host();
}
}
static Hop *hop_cache_lookup(u8 ttl, const struct sockaddr_storage *addr) {
std::map<struct HopIdent, Hop *>::iterator it;
HopIdent ident(ttl, *addr);
it = hop_cache.find(ident);
if (it == hop_cache.end())
return NULL;
else
return it->second;