2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 Daniel Borkmann.
5 * Subject to the GPL, version 2.
7 * An Autonomous System trace route utility based on TCP instead of ICMP for
8 * a better passing of firewalls. Supports IPv4 and IPv6. Based on the idea
9 * of tcptraceroute (http://michael.toren.net/code/tcptraceroute/), but hacked
10 * for Autonomous Systems tracing, thus you will know an approximate path of
11 * your curvetun tunneled packets, for instance. However, astraceroute was
12 * written from scratch and does not use any libraries. Special thanks to
15 * The road must be trod, but it will be very hard. And neither strength nor
16 * wisdom will carry us far upon it. This quest may be attempted by the weak
17 * with as much hope as the strong. Yet such is oft the course of deeds that
18 * move the wheels of the world: small hands do them because they must,
19 * while the eyes of the great are elsewhere.
21 * -- The Lord of the Rings, Elrond, Chapter 'The Council of Elrond'.
30 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/fsuid.h>
37 #include <asm/byteorder.h>
38 #include <linux/tcp.h>
39 #include <netinet/ip.h>
40 #include <netinet/ip6.h>
41 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <linux/if_ether.h>
46 #include <linux/icmp.h>
47 #include <linux/icmpv6.h>
49 #include <GeoIPCity.h>
63 #define WHOIS_SERVER_SOURCE "/etc/netsniff-ng/whois.conf"
74 int syn
, ack
, ecn
, fin
, psh
, rst
, urg
;
83 volatile sig_atomic_t sigint
= 0;
85 static int show_pkt
= 0;
87 static GeoIP
*gi_country
= NULL
;
88 static GeoIP
*gi_city
= NULL
;
90 static const char *short_options
= "H:p:nNf:m:i:d:q:x:SAEFPURt:Gl:w:W:hv46X:ZLK";
91 static const struct option long_options
[] = {
92 {"host", required_argument
, NULL
, 'H'},
93 {"port", required_argument
, NULL
, 'p'},
94 {"init-ttl", required_argument
, NULL
, 'f'},
95 {"max-ttl", required_argument
, NULL
, 'm'},
96 {"dev", required_argument
, NULL
, 'd'},
97 {"num-probes", required_argument
, NULL
, 'q'},
98 {"timeout", required_argument
, NULL
, 'x'},
99 {"tos", required_argument
, NULL
, 't'},
100 {"payload", required_argument
, NULL
, 'X'},
101 {"totlen", required_argument
, NULL
, 'l'},
102 {"whois", required_argument
, NULL
, 'w'},
103 {"wport", required_argument
, NULL
, 'W'},
104 {"city-db", required_argument
, NULL
, 'L'},
105 {"country-db", required_argument
, NULL
, 'K'},
106 {"numeric", no_argument
, NULL
, 'n'},
107 {"dns", no_argument
, NULL
, 'N'},
108 {"ipv4", no_argument
, NULL
, '4'},
109 {"ipv6", no_argument
, NULL
, '6'},
110 {"syn", no_argument
, NULL
, 'S'},
111 {"ack", no_argument
, NULL
, 'A'},
112 {"urg", no_argument
, NULL
, 'U'},
113 {"fin", no_argument
, NULL
, 'F'},
114 {"psh", no_argument
, NULL
, 'P'},
115 {"rst", no_argument
, NULL
, 'R'},
116 {"ecn-syn", no_argument
, NULL
, 'E'},
117 {"show-packet", no_argument
, NULL
, 'Z'},
118 {"nofrag", no_argument
, NULL
, 'G'},
119 {"version", no_argument
, NULL
, 'v'},
120 {"help", no_argument
, NULL
, 'h'},
124 static struct sock_filter ipv4_icmp_type_11
[] = {
126 { 0x28, 0, 0, 0x0000000c },
127 /* (001) jeq #0x800 jt 2 jf 10 */
128 { 0x15, 0, 8, 0x00000800 },
130 { 0x30, 0, 0, 0x00000017 },
131 /* (003) jeq #0x1 jt 4 jf 10 */
132 { 0x15, 0, 6, 0x00000001 },
134 { 0x28, 0, 0, 0x00000014 },
135 /* (005) jset #0x1fff jt 10 jf 6 */
136 { 0x45, 4, 0, 0x00001fff },
137 /* (006) ldxb 4*([14]&0xf) */
138 { 0xb1, 0, 0, 0x0000000e },
139 /* (007) ldb [x + 14] */
140 { 0x50, 0, 0, 0x0000000e },
141 /* (008) jeq #0xb jt 9 jf 10 */
142 { 0x15, 0, 1, 0x0000000b },
143 /* (009) ret #65535 */
144 { 0x06, 0, 0, 0xffffffff },
146 { 0x06, 0, 0, 0x00000000 },
149 static struct sock_filter ipv6_icmp6_type_3
[] = {
151 { 0x28, 0, 0, 0x0000000c },
152 /* (001) jeq #0x86dd jt 2 jf 7 */
153 { 0x15, 0, 5, 0x000086dd },
155 { 0x30, 0, 0, 0x00000014 },
156 /* (003) jeq #0x3a jt 4 jf 7 */
157 { 0x15, 0, 3, 0x0000003a },
159 { 0x30, 0, 0, 0x00000036 },
160 /* (005) jeq #0x3 jt 6 jf 7 */
161 { 0x15, 0, 1, 0x00000003 },
162 /* (006) ret #65535 */
163 { 0x06, 0, 0, 0xffffffff },
165 { 0x06, 0, 0, 0x00000000 },
168 #define PKT_NOT_FOR_US 0
171 static inline const char *make_n_a(const char *p
)
176 static void signal_handler(int number
)
187 static void header(void)
189 printf("%s%s%s\n", colorize_start(bold
), "astraceroute "
190 VERSION_STRING
, colorize_end());
193 static void help(void)
196 printf("\nastraceroute %s, autonomous system trace route utility\n",
198 puts("http://www.netsniff-ng.org\n\n"
199 "Usage: astraceroute [options]\n"
201 " -H|--host <host> Host/IPv4/IPv6 to lookup AS route to\n"
202 " -p|--port <port> Hosts port to lookup AS route to\n"
203 " -i|-d|--dev <device> Networking device, i.e. eth0\n"
204 " -4|--ipv4 Use IPv4 requests (default)\n"
205 " -6|--ipv6 Use IPv6 requests\n"
206 " -n|--numeric Do not do reverse DNS lookup for hops\n"
207 " -N|--dns Do a reverse DNS lookup for hops\n"
208 " -f|--init-ttl <ttl> Set initial TTL\n"
209 " -m|--max-ttl <ttl> Set maximum TTL (default: 30)\n"
210 " -q|--num-probes <num> Number of max probes for each hop (default: 3)\n"
211 " -x|--timeout <sec> Probe response timeout in sec (default: 3)\n"
212 " -S|--syn Set TCP SYN flag in packets\n"
213 " -A|--ack Set TCP ACK flag in packets\n"
214 " -F|--fin Set TCP FIN flag in packets\n"
215 " -P|--psh Set TCP PSH flag in packets\n"
216 " -U|--urg Set TCP URG flag in packets\n"
217 " -R|--rst Set TCP RST flag in packets\n"
218 " -E|--ecn-syn Send ECN SYN packets (RFC3168)\n"
219 " -t|--tos <tos> Set the IP TOS field\n"
220 " -G|--nofrag Set do not fragment bit\n"
221 " -X|--payload <string> Specify a payload string to test DPIs\n"
222 " -Z|--show-packet Show returned packet on each hop\n"
223 " -l|--totlen <len> Specify total packet len\n"
224 " -w|--whois <server> Use a different AS whois DB server\n"
225 " (default: /etc/netsniff-ng/whois.conf)\n"
226 " -W|--wport <port> Use a different port to AS whois server\n"
227 " (default: /etc/netsniff-ng/whois.conf)\n"
228 " --city-db <path> Specifiy path for geoip city database\n"
229 " --country-db <path> Specifiy path for geoip country database\n"
230 " -v|--version Print version\n"
231 " -h|--help Print this help\n\n"
233 " IPv4 trace of AS with TCP SYN probe (this will most-likely pass):\n"
234 " astraceroute -i eth0 -N -S -H netsniff-ng.org\n"
235 " IPv4 trace of AS with TCP ECN SYN probe:\n"
236 " astraceroute -i eth0 -N -E -H netsniff-ng.org\n"
237 " IPv4 trace of AS with TCP FIN probe:\n"
238 " astraceroute -i eth0 -N -F -H netsniff-ng.org\n"
239 " IPv4 trace of AS with Xmas probe:\n"
240 " astraceroute -i eth0 -N -FPU -H netsniff-ng.org\n"
241 " IPv4 trace of AS with Null probe with ASCII payload:\n"
242 " astraceroute -i eth0 -N -H netsniff-ng.org -X \"censor-me\" -Z\n"
243 " IPv6 trace of AS up to www.6bone.net:\n"
244 " astraceroute -6 -i eth0 -S -E -N -H www.6bone.net\n\n"
246 " If the TCP probe did not give any results, then astraceroute will\n"
247 " automatically probe for classic ICMP packets! To gather more\n"
248 " information about astraceroute's fetched AS numbers, see i.e.\n"
249 " http://bgp.he.net/AS<number>!\n\n"
250 "Please report bugs to <bugs@netsniff-ng.org>\n"
251 "Copyright (C) 2011-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n"
252 "Swiss federal institute of technology (ETH Zurich)\n"
253 "License: GNU GPL version 2.0\n"
254 "This is free software: you are free to change and redistribute it.\n"
255 "There is NO WARRANTY, to the extent permitted by law.\n");
259 static void version(void)
261 printf("\nastraceroute %s, autonomous system trace route utility\n",
263 puts("http://www.netsniff-ng.org\n\n"
264 "Please report bugs to <bugs@netsniff-ng.org>\n"
265 "Copyright (C) 2011-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n"
266 "Swiss federal institute of technology (ETH Zurich)\n"
267 "License: GNU GPL version 2.0\n"
268 "This is free software: you are free to change and redistribute it.\n"
269 "There is NO WARRANTY, to the extent permitted by law.\n");
273 static inline unsigned short csum(unsigned short *buf
, int nwords
)
277 for (sum
= 0; nwords
> 0; nwords
--)
279 sum
= (sum
>> 16) + (sum
& 0xffff);
285 static void assemble_data(uint8_t *packet
, size_t len
, const char *payload
)
289 if (payload
== NULL
) {
290 for (i
= 0; i
< len
; ++i
)
291 packet
[i
] = (uint8_t) rand();
293 int lmin
= min(len
, strlen(payload
));
294 for (i
= 0; i
< lmin
; ++i
)
295 packet
[i
] = (uint8_t) payload
[i
];
296 for (i
= lmin
; i
< len
; ++i
)
297 packet
[i
] = (uint8_t) rand();
301 static void assemble_icmp4(uint8_t *packet
, size_t len
)
303 struct icmphdr
*icmph
= (struct icmphdr
*) packet
;
305 bug_on(len
< sizeof(struct icmphdr
));
307 icmph
->type
= ICMP_ECHO
;
312 static void assemble_icmp6(uint8_t *packet
, size_t len
)
314 struct icmp6hdr
*icmp6h
= (struct icmp6hdr
*) packet
;
316 bug_on(len
< sizeof(struct icmp6hdr
));
318 icmp6h
->icmp6_type
= ICMPV6_ECHO_REQUEST
;
319 icmp6h
->icmp6_code
= 0;
320 icmp6h
->icmp6_cksum
= 0;
323 static void assemble_tcp(uint8_t *packet
, size_t len
, int syn
, int ack
,
324 int urg
, int fin
, int rst
, int psh
, int ecn
, int dport
)
326 struct tcphdr
*tcph
= (struct tcphdr
*) packet
;
328 bug_on(len
< sizeof(struct tcphdr
));
330 tcph
->source
= htons((uint16_t) rand());
331 tcph
->dest
= htons((uint16_t) dport
);
332 tcph
->seq
= htonl(rand());
333 tcph
->ack_seq
= (!!ack
? htonl(rand()) : 0);
343 tcph
->window
= htons((uint16_t) (100 + (rand() % 65435)));
345 tcph
->urg_ptr
= (!!urg
? htons((uint16_t) rand()) : 0);
348 static int assemble_ipv4_tcp(uint8_t *packet
, size_t len
, int ttl
,
349 int tos
, const struct sockaddr
*dst
,
350 const struct sockaddr
*src
, int syn
, int ack
,
351 int urg
, int fin
, int rst
, int psh
, int ecn
,
352 int nofrag
, int dport
, const char *payload
)
354 struct iphdr
*iph
= (struct iphdr
*) packet
;
358 bug_on(!src
|| !dst
);
359 bug_on(src
->sa_family
!= PF_INET
|| dst
->sa_family
!= PF_INET
);
360 bug_on(len
< sizeof(*iph
) + sizeof(struct tcphdr
));
364 iph
->tos
= (uint8_t) tos
;
365 iph
->tot_len
= htons((uint16_t) len
);
366 iph
->id
= htons((uint16_t) rand());
367 iph
->frag_off
= nofrag
? IP_DF
: 0;
368 iph
->ttl
= (uint8_t) ttl
;
369 iph
->protocol
= 6; /* TCP */
370 iph
->saddr
= ((const struct sockaddr_in
*) src
)->sin_addr
.s_addr
;
371 iph
->daddr
= ((const struct sockaddr_in
*) dst
)->sin_addr
.s_addr
;
373 data
= packet
+ sizeof(*iph
);
374 data_len
= len
- sizeof(*iph
);
375 assemble_tcp(data
, data_len
, syn
, ack
, urg
, fin
, rst
, psh
, ecn
, dport
);
377 data
= packet
+ sizeof(*iph
) + sizeof(struct tcphdr
);
378 data_len
= len
- sizeof(*iph
) - sizeof(struct tcphdr
);
379 assemble_data(data
, data_len
, payload
);
381 iph
->check
= csum((unsigned short *) packet
, ntohs(iph
->tot_len
) >> 1);
383 return ntohs(iph
->id
);
386 static int assemble_ipv6_tcp(uint8_t *packet
, size_t len
, int ttl
,
387 const struct sockaddr
*dst
,
388 const struct sockaddr
*src
, int syn
, int ack
,
389 int urg
, int fin
, int rst
, int psh
, int ecn
,
390 int dport
, const char *payload
)
392 struct ip6_hdr
*ip6h
= (struct ip6_hdr
*) packet
;
396 bug_on(!src
|| !dst
);
397 bug_on(src
->sa_family
!= PF_INET6
|| dst
->sa_family
!= PF_INET6
);
398 bug_on(len
< sizeof(*ip6h
) + sizeof(struct tcphdr
));
400 ip6h
->ip6_flow
= htonl(rand() & 0x000fffff);
401 ip6h
->ip6_vfc
= 0x60;
402 ip6h
->ip6_plen
= htons((uint16_t) len
- sizeof(*ip6h
));
403 ip6h
->ip6_nxt
= 6; /* TCP */
404 ip6h
->ip6_hlim
= (uint8_t) ttl
;
405 memcpy(&ip6h
->ip6_src
, &(((const struct sockaddr_in6
*)
406 src
)->sin6_addr
), sizeof(ip6h
->ip6_src
));
407 memcpy(&ip6h
->ip6_dst
, &(((const struct sockaddr_in6
*)
408 dst
)->sin6_addr
), sizeof(ip6h
->ip6_dst
));
410 data
= packet
+ sizeof(*ip6h
);
411 data_len
= len
- sizeof(*ip6h
);
412 assemble_tcp(data
, data_len
, syn
, ack
, urg
, fin
, rst
, psh
, ecn
, dport
);
414 data
= packet
+ sizeof(*ip6h
) + sizeof(struct tcphdr
);
415 data_len
= len
- sizeof(*ip6h
) - sizeof(struct tcphdr
);
416 assemble_data(data
, data_len
, payload
);
418 return ntohl(ip6h
->ip6_flow
) & 0x000fffff;
421 static int assemble_ipv6_icmp6(uint8_t *packet
, size_t len
, int ttl
,
422 const struct sockaddr
*dst
,
423 const struct sockaddr
*src
,
426 struct ip6_hdr
*ip6h
= (struct ip6_hdr
*) packet
;
430 bug_on(!src
|| !dst
);
431 bug_on(src
->sa_family
!= PF_INET6
|| dst
->sa_family
!= PF_INET6
);
432 bug_on(len
< sizeof(*ip6h
) + sizeof(struct icmp6hdr
));
434 ip6h
->ip6_flow
= htonl(rand() & 0x000fffff);
435 ip6h
->ip6_vfc
= 0x60;
436 ip6h
->ip6_plen
= htons((uint16_t) len
- sizeof(*ip6h
));
437 ip6h
->ip6_nxt
= 0x3a; /* ICMP6 */
438 ip6h
->ip6_hlim
= (uint8_t) ttl
;
439 memcpy(&ip6h
->ip6_src
, &(((const struct sockaddr_in6
*)
440 src
)->sin6_addr
), sizeof(ip6h
->ip6_src
));
441 memcpy(&ip6h
->ip6_dst
, &(((const struct sockaddr_in6
*)
442 dst
)->sin6_addr
), sizeof(ip6h
->ip6_dst
));
444 data
= packet
+ sizeof(*ip6h
);
445 data_len
= len
- sizeof(*ip6h
);
446 assemble_icmp6(data
, data_len
);
448 data
= packet
+ sizeof(*ip6h
) + sizeof(struct icmp6hdr
);
449 data_len
= len
- sizeof(*ip6h
) - sizeof(struct icmp6hdr
);
450 assemble_data(data
, data_len
, payload
);
452 return ntohl(ip6h
->ip6_flow
) & 0x000fffff;
455 static int assemble_ipv4_icmp4(uint8_t *packet
, size_t len
, int ttl
,
456 int tos
, const struct sockaddr
*dst
,
457 const struct sockaddr
*src
, int nofrag
,
460 struct iphdr
*iph
= (struct iphdr
*) packet
;
464 bug_on(!src
|| !dst
);
465 bug_on(src
->sa_family
!= PF_INET
|| dst
->sa_family
!= PF_INET
);
466 bug_on(len
< sizeof(struct iphdr
) + sizeof(struct icmphdr
));
471 iph
->tot_len
= htons((uint16_t) len
);
472 iph
->id
= htons((uint16_t) rand());
473 iph
->frag_off
= nofrag
? IP_DF
: 0;
474 iph
->ttl
= (uint8_t) ttl
;
475 iph
->protocol
= 1; /* ICMP4 */
476 iph
->saddr
= ((const struct sockaddr_in
*) src
)->sin_addr
.s_addr
;
477 iph
->daddr
= ((const struct sockaddr_in
*) dst
)->sin_addr
.s_addr
;
479 data
= packet
+ sizeof(*iph
);
480 data_len
= len
- sizeof(*iph
);
481 assemble_icmp4(data
, data_len
);
483 data
= packet
+ sizeof(*iph
) + sizeof(struct icmphdr
);
484 data_len
= len
- sizeof(*iph
) - sizeof(struct icmphdr
);
485 assemble_data(data
, data_len
, payload
);
487 iph
->check
= csum((unsigned short *) packet
, ntohs(iph
->tot_len
) >> 1);
489 return ntohs(iph
->id
);
492 static int assemble_packet_or_die(uint8_t *packet
, size_t len
,
494 const struct ash_cfg
*cfg
,
495 const struct sockaddr
*dst
,
496 const struct sockaddr
*src
)
500 return assemble_ipv4_icmp4(packet
, len
, ttl
, cfg
->tos
,
501 dst
, src
, cfg
->nofrag
,
504 return assemble_ipv6_icmp6(packet
, len
, ttl
, dst
, src
,
509 return assemble_ipv4_tcp(packet
, len
, ttl
, cfg
->tos
,
510 dst
, src
, cfg
->syn
, cfg
->ack
,
511 cfg
->urg
, cfg
->fin
, cfg
->rst
,
513 cfg
->nofrag
, atoi(cfg
->port
),
516 return assemble_ipv6_tcp(packet
, len
, ttl
, dst
, src
,
517 cfg
->syn
, cfg
->ack
, cfg
->urg
,
518 cfg
->fin
, cfg
->rst
, cfg
->psh
,
519 cfg
->ecn
, atoi(cfg
->port
),
527 static int handle_ipv4_icmp(uint8_t *packet
, size_t len
, int ttl
, int id
,
528 const struct sockaddr
*own
, int dns_resolv
)
531 struct iphdr
*iph
= (struct iphdr
*) packet
;
532 struct iphdr
*iph_inner
;
533 struct icmphdr
*icmph
;
535 struct sockaddr_in sa
;
539 if (iph
->protocol
!= 1)
540 return PKT_NOT_FOR_US
;
541 if (iph
->daddr
!= ((const struct sockaddr_in
*) own
)->sin_addr
.s_addr
)
542 return PKT_NOT_FOR_US
;
544 icmph
= (struct icmphdr
*) (packet
+ sizeof(struct iphdr
));
545 if (icmph
->type
!= ICMP_TIME_EXCEEDED
)
546 return PKT_NOT_FOR_US
;
547 if (icmph
->code
!= ICMP_EXC_TTL
)
548 return PKT_NOT_FOR_US
;
550 iph_inner
= (struct iphdr
*) (packet
+ sizeof(struct iphdr
) +
551 sizeof(struct icmphdr
));
552 if (ntohs(iph_inner
->id
) != id
)
553 return PKT_NOT_FOR_US
;
555 hbuff
= xzmalloc(NI_MAXHOST
);
557 memset(&sa
, 0, sizeof(sa
));
558 sa
.sin_family
= PF_INET
;
559 sa
.sin_addr
.s_addr
= iph
->saddr
;
561 getnameinfo((struct sockaddr
*) &sa
, sizeof(sa
), hbuff
, NI_MAXHOST
,
562 NULL
, 0, NI_NUMERICHOST
);
564 memset(&rec
, 0, sizeof(rec
));
565 ret
= aslookup(hbuff
, &rec
);
567 panic("AS lookup error %d!\n", ret
);
569 gir
= GeoIP_record_by_ipnum(gi_city
, ntohl(iph
->saddr
));
571 if (strlen(rec
.country
) > 0 && gir
) {
572 const char *city
= make_n_a(gir
->city
);
574 printf("%s in AS%s (%s, %s, %s, %f, %f), %s %s (%s), %s", hbuff
,
575 rec
.number
, rec
.country
,
576 GeoIP_country_name_by_ipnum(gi_country
, ntohl(iph
->saddr
)),
577 city
, gir
->latitude
, gir
->longitude
,
578 rec
.prefix
, rec
.registry
, rec
.since
, rec
.name
);
579 } else if (strlen(rec
.country
) > 0 && !gir
) {
580 printf("%s in AS%s (%s, %s), %s %s (%s), %s", hbuff
,
581 rec
.number
, rec
.country
,
582 GeoIP_country_name_by_ipnum(gi_country
, ntohl(iph
->saddr
)),
583 rec
.prefix
, rec
.registry
, rec
.since
, rec
.name
);
585 printf("%s in unknown AS", hbuff
);
588 struct hostent
*hent
= gethostbyaddr(&sa
.sin_addr
,
592 if (strlen(rec
.country
) > 0 && gir
) {
593 const char *city
= make_n_a(gir
->city
);
594 printf("%s (%s) in AS%s (%s, %s, %s, %f, %f), %s %s (%s), %s",
595 (hent
? hent
->h_name
: hbuff
), hbuff
,
596 rec
.number
, rec
.country
,
597 GeoIP_country_name_by_ipnum(gi_country
, ntohl(iph
->saddr
)),
598 city
, gir
->latitude
, gir
->longitude
,
599 rec
.prefix
, rec
.registry
,
600 rec
.since
, rec
.name
);
601 } else if (strlen(rec
.country
) > 0 && !gir
) {
602 printf("%s (%s) in AS%s (%s, %s), %s %s (%s), %s",
603 (hent
? hent
->h_name
: hbuff
), hbuff
,
604 rec
.number
, rec
.country
,
605 GeoIP_country_name_by_ipnum(gi_country
, ntohl(iph
->saddr
)),
606 rec
.prefix
, rec
.registry
,
607 rec
.since
, rec
.name
);
609 printf("%s (%s) in unknown AS",
610 (hent
? hent
->h_name
: hbuff
), hbuff
);
619 static int handle_ipv6_icmp(uint8_t *packet
, size_t len
, int ttl
, int id
,
620 const struct sockaddr
*own
, int dns_resolv
)
623 struct ip6_hdr
*ip6h
= (struct ip6_hdr
*) packet
;
624 struct ip6_hdr
*ip6h_inner
;
625 struct icmp6hdr
*icmp6h
;
627 struct sockaddr_in6 sa
;
631 if (ip6h
->ip6_nxt
!= 0x3a)
632 return PKT_NOT_FOR_US
;
633 if (memcmp(&ip6h
->ip6_dst
, &(((const struct sockaddr_in6
*)
634 own
)->sin6_addr
), sizeof(ip6h
->ip6_dst
)))
635 return PKT_NOT_FOR_US
;
637 icmp6h
= (struct icmp6hdr
*) (packet
+ sizeof(*ip6h
));
638 if (icmp6h
->icmp6_type
!= ICMPV6_TIME_EXCEED
)
639 return PKT_NOT_FOR_US
;
640 if (icmp6h
->icmp6_code
!= ICMPV6_EXC_HOPLIMIT
)
641 return PKT_NOT_FOR_US
;
643 ip6h_inner
= (struct ip6_hdr
*) (packet
+ sizeof(*ip6h
) + sizeof(*icmp6h
));
644 if ((ntohl(ip6h_inner
->ip6_flow
) & 0x000fffff) != id
)
645 return PKT_NOT_FOR_US
;
647 hbuff
= xzmalloc(NI_MAXHOST
);
649 memset(&sa
, 0, sizeof(sa
));
650 sa
.sin6_family
= PF_INET6
;
651 memcpy(&sa
.sin6_addr
, &ip6h
->ip6_src
, sizeof(ip6h
->ip6_src
));
653 getnameinfo((struct sockaddr
*) &sa
, sizeof(sa
), hbuff
, NI_MAXHOST
,
654 NULL
, 0, NI_NUMERICHOST
);
656 memset(&rec
, 0, sizeof(rec
));
657 ret
= aslookup(hbuff
, &rec
);
659 panic("AS lookup error %d!\n", ret
);
661 gir
= GeoIP_record_by_ipnum_v6(gi_city
, sa
.sin6_addr
);
663 if (strlen(rec
.country
) > 0 && gir
) {
664 const char *city
= make_n_a(gir
->city
);
666 printf("%s in AS%s (%s, %s, %s, %f, %f), %s %s (%s), %s", hbuff
,
667 rec
.number
, rec
.country
,
668 GeoIP_country_name_by_ipnum_v6(gi_country
, sa
.sin6_addr
),
669 city
, gir
->latitude
, gir
->longitude
,
670 rec
.prefix
, rec
.registry
, rec
.since
, rec
.name
);
671 } else if (strlen(rec
.country
) > 0 && !gir
) {
672 printf("%s in AS%s (%s, %s), %s %s (%s), %s", hbuff
,
673 rec
.number
, rec
.country
,
674 GeoIP_country_name_by_ipnum_v6(gi_country
, sa
.sin6_addr
),
675 rec
.prefix
, rec
.registry
, rec
.since
, rec
.name
);
677 printf("%s in unknown AS", hbuff
);
680 struct hostent
*hent
= gethostbyaddr(&sa
.sin6_addr
,
681 sizeof(sa
.sin6_addr
),
684 if (strlen(rec
.country
) > 0 && gir
) {
685 const char *city
= make_n_a(gir
->city
);
686 printf("%s (%s) in AS%s (%s, %s, %s, %f, %f), %s %s (%s), %s",
687 (hent
? hent
->h_name
: hbuff
), hbuff
,
688 rec
.number
, rec
.country
,
689 GeoIP_country_name_by_ipnum_v6(gi_country
, sa
.sin6_addr
),
690 city
, gir
->latitude
, gir
->longitude
,
691 rec
.prefix
, rec
.registry
,
692 rec
.since
, rec
.name
);
693 } else if (strlen(rec
.country
) > 0 && !gir
) {
694 printf("%s (%s) in AS%s (%s, %s), %s %s (%s), %s",
695 (hent
? hent
->h_name
: hbuff
), hbuff
,
696 rec
.number
, rec
.country
,
697 GeoIP_country_name_by_ipnum_v6(gi_country
, sa
.sin6_addr
),
698 rec
.prefix
, rec
.registry
,
699 rec
.since
, rec
.name
);
701 printf("%s (%s) in unknown AS",
702 (hent
? hent
->h_name
: hbuff
), hbuff
);
711 static int handle_packet(uint8_t *packet
, size_t len
, int ip
, int ttl
, int id
,
712 struct sockaddr
*own
, int dns_resolv
)
715 return handle_ipv4_icmp(packet
, len
, ttl
, id
, own
, dns_resolv
);
717 return handle_ipv6_icmp(packet
, len
, ttl
, id
, own
, dns_resolv
);
720 static int do_trace(const struct ash_cfg
*cfg
)
722 int ttl
, query
, fd
= -1, one
= 1, ret
, fd_cap
, ifindex
;
723 int is_okay
= 0, id
, timeout_poll
;
724 uint8_t *packet
, *packet_rcv
;
725 ssize_t err
, real_len
, sd_len
;
727 struct addrinfo hints
, *ahead
, *ai
;
728 char *hbuff1
, *hbuff2
;
729 struct sockaddr_storage ss
, sd
;
730 struct sock_fprog bpf_ops
;
731 struct ring dummy_ring
;
736 memset(&hints
, 0, sizeof(hints
));
737 hints
.ai_family
= PF_UNSPEC
;
738 hints
.ai_socktype
= SOCK_STREAM
;
739 hints
.ai_protocol
= IPPROTO_TCP
;
740 hints
.ai_flags
= AI_NUMERICSERV
;
742 ret
= getaddrinfo(cfg
->host
, cfg
->port
, &hints
, &ahead
);
744 whine("Cannot get address info!\n");
748 for (ai
= ahead
; ai
!= NULL
&& fd
< 0; ai
= ai
->ai_next
) {
749 if (!((ai
->ai_family
== PF_INET6
&& cfg
->ip
== 6) ||
750 (ai
->ai_family
== PF_INET
&& cfg
->ip
== 4)))
752 fd
= socket(ai
->ai_family
, SOCK_RAW
, IPPROTO_RAW
);
755 fd_cap
= pf_socket();
757 memset(&ss
, 0, sizeof(ss
));
758 ret
= device_address(cfg
->dev
, ai
->ai_family
, &ss
);
760 panic("Cannot get own device address!\n");
762 ret
= bind(fd
, (struct sockaddr
*) &ss
, sizeof(ss
));
764 panic("Cannot bind socket!\n");
766 memset(&sd
, 0, sizeof(sd
));
767 memcpy(&sd
, ai
->ai_addr
, ai
->ai_addrlen
);
768 if (ai
->ai_family
== PF_INET6
) {
769 struct sockaddr_in6
*sd6
= (struct sockaddr_in6
*) &sd
;
770 sd6
->sin6_port
= htons(0);
772 sd_len
= ai
->ai_addrlen
;
780 whine("Cannot create socket! Does remote support IPv%d?!\n",
787 if (len
< sizeof(struct iphdr
) + sizeof(struct tcphdr
)) {
788 len
= sizeof(struct iphdr
) + sizeof(struct tcphdr
);
790 len
+= strlen(cfg
->payload
);
793 if (len
< sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
)) {
794 len
= sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
);
796 len
+= strlen(cfg
->payload
);
800 len_rcv
= device_mtu(cfg
->dev
);
802 panic("Packet len exceeds device MTU!\n");
804 packet
= xmalloc(len
);
805 packet_rcv
= xmalloc(len_rcv
);
807 hbuff1
= xzmalloc(256);
808 getnameinfo((struct sockaddr
*) &sd
, sizeof(sd
), hbuff1
, 256,
809 NULL
, 0, NI_NUMERICHOST
);
811 hbuff2
= xzmalloc(256);
812 getnameinfo((struct sockaddr
*) &ss
, sizeof(ss
), hbuff2
, 256,
813 NULL
, 0, NI_NUMERICHOST
);
815 ret
= setsockopt(fd
, cfg
->ip
== 4 ? IPPROTO_IP
: IPPROTO_IPV6
,
816 IP_HDRINCL
, &one
, sizeof(one
));
818 panic("Kernel does not support IP_HDRINCL!\n");
820 printf("AS path IPv%d TCP trace from %s to %s:%s (%s) with len %zu "
821 "Bytes, %u max hops\n", cfg
->ip
, hbuff2
, hbuff1
, cfg
->port
,
822 cfg
->host
, len
, cfg
->max_ttl
);
824 printf("Using flags SYN:%d,ACK:%d,ECN:%d,FIN:%d,PSH:%d,RST:%d,URG:%d\n",
825 cfg
->syn
, cfg
->ack
, cfg
->ecn
, cfg
->fin
, cfg
->psh
, cfg
->rst
,
829 printf("With payload: \'%s\'\n", cfg
->payload
);
836 hbuff1
= hbuff2
= NULL
;
838 enable_kernel_bpf_jit_compiler();
840 memset(&bpf_ops
, 0, sizeof(bpf_ops
));
842 bpf_ops
.filter
= ipv4_icmp_type_11
;
843 bpf_ops
.len
= (sizeof(ipv4_icmp_type_11
) /
844 sizeof(ipv4_icmp_type_11
[0]));
846 bpf_ops
.filter
= ipv6_icmp6_type_3
;
847 bpf_ops
.len
= (sizeof(ipv6_icmp6_type_3
) /
848 sizeof(ipv6_icmp6_type_3
[0]));
851 bpf_attach_to_sock(fd_cap
, &bpf_ops
);
852 ifindex
= device_ifindex(cfg
->dev
);
853 bind_rx_ring(fd_cap
, &dummy_ring
, ifindex
);
854 prepare_polling(fd_cap
, &pfd
);
856 timeout_poll
= (cfg
->timeout
> 0 ? cfg
->timeout
: 3) * 1000;
858 for (ttl
= cfg
->init_ttl
; ttl
<= cfg
->max_ttl
; ++ttl
) {
862 if ((ttl
== cfg
->init_ttl
&& !show_pkt
) ||
863 (ttl
> cfg
->init_ttl
)) {
864 printf("%2d: ", ttl
);
868 for (query
= 0; query
< cfg
->queries
&& !is_okay
; ++query
) {
869 id
= assemble_packet_or_die(packet
, len
, ttl
, icmp
, cfg
,
870 (struct sockaddr
*) &sd
,
871 (struct sockaddr
*) &ss
);
872 if (ttl
== cfg
->init_ttl
&& query
== 0 && show_pkt
) {
873 struct pkt_buff
*pkt
;
875 printf("Original packet:\n");
877 pkt
= pkt_alloc(packet
, len
);
882 printf("\n%2d: ", ttl
);
886 err
= sendto(fd
, packet
, len
, 0, (struct sockaddr
*) &sd
,
889 panic("sendto failed: %s\n", strerror(errno
));
891 err
= poll(&pfd
, 1, timeout_poll
);
892 if (err
> 0 && pfd
.revents
& POLLIN
) {
893 real_len
= recvfrom(fd_cap
, packet_rcv
, len_rcv
,
895 if (real_len
< sizeof(struct ethhdr
) +
896 (cfg
->ip
? sizeof(struct iphdr
) +
897 sizeof(struct icmphdr
) :
898 sizeof(struct ip6_hdr
) +
899 sizeof(struct icmp6hdr
)))
902 is_okay
= handle_packet(packet_rcv
+ sizeof(struct ethhdr
),
903 real_len
- sizeof(struct ethhdr
),
905 (struct sockaddr
*) &ss
,
907 if (is_okay
&& show_pkt
) {
908 struct pkt_buff
*pkt
;
910 printf("\n Received packet:\n");
912 pkt
= pkt_alloc(packet_rcv
, real_len
);
924 if (is_okay
== 0 && icmp
== 0) {
942 static void parse_whois_or_die(struct ash_cfg
*cfg
)
946 char tmp
[512], *ptr
, *ptr2
;
948 fd
= open_or_die(WHOIS_SERVER_SOURCE
, O_RDONLY
);
950 memset(tmp
, 0, sizeof(tmp
));
951 while ((ret
= read(fd
, tmp
, sizeof(tmp
))) > 0) {
952 tmp
[sizeof(tmp
) - 1] = 0;
955 while (*ptr2
!= ' ' && ptr2
< &tmp
[sizeof(tmp
) - 1])
958 panic("Parser error!\n");
960 cfg
->whois
= xstrdup(ptr
);
962 if (ptr
>= &tmp
[sizeof(tmp
) - 1])
963 panic("Parser error!\n");
965 ptr
[strlen(ptr
) - 1] = 0;
966 cfg
->whois_port
= xstrdup(ptr
);
973 int main(int argc
, char **argv
)
975 int c
, opt_index
, ret
;
977 char *path_city_db
= NULL
, *path_country_db
= NULL
;
982 memset(&cfg
, 0, sizeof(cfg
));
989 cfg
.dev
= xstrdup("eth0");
990 cfg
.port
= xstrdup("80");
992 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
993 &opt_index
)) != EOF
) {
1002 cfg
.host
= xstrdup(optarg
);
1007 cfg
.port
= xstrdup(optarg
);
1025 cfg
.init_ttl
= atoi(optarg
);
1026 if (cfg
.init_ttl
<= 0)
1030 cfg
.max_ttl
= atoi(optarg
);
1031 if (cfg
.max_ttl
<= 0)
1038 cfg
.dev
= xstrdup(optarg
);
1041 cfg
.queries
= atoi(optarg
);
1042 if (cfg
.queries
<= 0)
1046 cfg
.timeout
= atoi(optarg
);
1047 if (cfg
.timeout
<= 0)
1073 cfg
.tos
= atoi(optarg
);
1081 cfg
.payload
= xstrdup(optarg
);
1084 cfg
.totlen
= atoi(optarg
);
1085 if (cfg
.totlen
<= 0)
1089 cfg
.whois
= xstrdup(optarg
);
1092 cfg
.whois_port
= xstrdup(optarg
);
1095 path_city_db
= xstrdup(optarg
);
1098 path_country_db
= xstrdup(optarg
);
1117 panic("Option -%c requires an argument!\n",
1120 if (isprint(optopt
))
1121 whine("Unknown option character "
1122 "`0x%X\'!\n", optopt
);
1131 !cfg
.host
|| !cfg
.port
||
1132 cfg
.init_ttl
> cfg
.max_ttl
||
1133 cfg
.init_ttl
> MAXTTL
||
1134 cfg
.max_ttl
> MAXTTL
)
1137 if (!device_up_and_running(cfg
.dev
))
1138 panic("Networking device not up and running!\n");
1139 if (!cfg
.whois
|| !cfg
.whois_port
)
1140 parse_whois_or_die(&cfg
);
1141 if (device_mtu(cfg
.dev
) <= cfg
.totlen
)
1142 panic("Packet larger than device MTU!\n");
1144 register_signal(SIGHUP
, signal_handler
);
1150 ret
= aslookup_prepare(cfg
.whois
, cfg
.whois_port
);
1152 panic("Cannot resolve whois server!\n");
1154 if (path_country_db
)
1155 gi_country
= GeoIP_open(path_country_db
, GEOIP_MMAP_CACHE
);
1157 gi_country
= GeoIP_open_type(cfg
.ip
== 4 ?
1158 GEOIP_COUNTRY_EDITION
:
1159 GEOIP_COUNTRY_EDITION_V6
,
1163 gi_city
= GeoIP_open(path_city_db
, GEOIP_MMAP_CACHE
);
1165 gi_city
= GeoIP_open_type(cfg
.ip
== 4 ?
1166 GEOIP_CITY_EDITION_REV1
:
1167 GEOIP_CITY_EDITION_REV1_V6
,
1170 if (!gi_country
|| !gi_city
)
1171 panic("Cannot open GeoIP database! Wrong path?!\n");
1173 GeoIP_set_charset(gi_country
, GEOIP_CHARSET_UTF8
);
1174 GeoIP_set_charset(gi_city
, GEOIP_CHARSET_UTF8
);
1176 ret
= do_trace(&cfg
);
1178 GeoIP_delete(gi_city
);
1179 GeoIP_delete(gi_country
);
1183 free(cfg
.whois_port
);
1190 free(path_country_db
);