2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 - 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
8 #define _DEFAULT_SOURCE
15 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <sys/fsuid.h>
22 #include <asm/byteorder.h>
23 #include <linux/tcp.h>
24 #include <netinet/if_ether.h>
25 #include <netinet/ip.h>
26 #include <netinet/ip6.h>
27 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <linux/icmp.h>
33 #include <linux/icmpv6.h>
51 char *host
, *port
, *dev
, *payload
, *bind_addr
;
52 size_t totlen
, rcvlen
;
54 int init_ttl
, max_ttl
, dns_resolv
, queries
, timeout
;
55 int syn
, ack
, ecn
, fin
, psh
, rst
, urg
, tos
, nofrag
, proto
, show
;
60 int (*assembler
)(uint8_t *packet
, size_t len
, int ttl
, int proto
,
61 const struct ctx
*ctx
, const struct sockaddr
*dst
,
62 const struct sockaddr
*src
);
63 const struct sock_filter
*filter
;
65 size_t min_len_tcp
, min_len_icmp
;
66 int (*check
)(uint8_t *packet
, size_t len
, int ttl
, int id
,
67 const struct sockaddr
*src
);
68 void (*handler
)(uint8_t *packet
, size_t len
, int dns_resolv
,
72 static sig_atomic_t sigint
= 0;
74 static int assemble_ipv4(uint8_t *packet
, size_t len
, int ttl
, int proto
,
75 const struct ctx
*ctx
, const struct sockaddr
*dst
,
76 const struct sockaddr
*src
);
77 static int assemble_ipv6(uint8_t *packet
, size_t len
, int ttl
, int proto
,
78 const struct ctx
*ctx
, const struct sockaddr
*dst
,
79 const struct sockaddr
*src
);
80 static int check_ipv4(uint8_t *packet
, size_t len
, int ttl
, int id
,
81 const struct sockaddr
*ss
);
82 static void handle_ipv4(uint8_t *packet
, size_t len
, int dns_resolv
,
84 static int check_ipv6(uint8_t *packet
, size_t len
, int ttl
, int id
,
85 const struct sockaddr
*ss
);
86 static void handle_ipv6(uint8_t *packet
, size_t len
, int dns_resolv
,
89 static const char *short_options
= "H:p:nNf:m:b:i:d:q:x:SAEFPURt:Gl:hv46X:ZuL";
90 static const struct option long_options
[] = {
91 {"host", required_argument
, NULL
, 'H'},
92 {"port", required_argument
, NULL
, 'p'},
93 {"init-ttl", required_argument
, NULL
, 'f'},
94 {"max-ttl", required_argument
, NULL
, 'm'},
95 {"bind", required_argument
, NULL
, 'b'},
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 {"numeric", no_argument
, NULL
, 'n'},
103 {"latitude", no_argument
, NULL
, 'L'},
104 {"update", no_argument
, NULL
, 'u'},
105 {"dns", no_argument
, NULL
, 'N'},
106 {"ipv4", no_argument
, NULL
, '4'},
107 {"ipv6", no_argument
, NULL
, '6'},
108 {"syn", no_argument
, NULL
, 'S'},
109 {"ack", no_argument
, NULL
, 'A'},
110 {"urg", no_argument
, NULL
, 'U'},
111 {"fin", no_argument
, NULL
, 'F'},
112 {"psh", no_argument
, NULL
, 'P'},
113 {"rst", no_argument
, NULL
, 'R'},
114 {"ecn-syn", no_argument
, NULL
, 'E'},
115 {"show-packet", no_argument
, NULL
, 'Z'},
116 {"nofrag", no_argument
, NULL
, 'G'},
117 {"version", no_argument
, NULL
, 'v'},
118 {"help", no_argument
, NULL
, 'h'},
122 static const char *copyright
= "Please report bugs to <netsniff-ng@googlegroups.com>\n"
123 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
124 "Swiss federal institute of technology (ETH Zurich)\n"
125 "License: GNU GPL version 2.0\n"
126 "This is free software: you are free to change and redistribute it.\n"
127 "There is NO WARRANTY, to the extent permitted by law.";
129 static const struct sock_filter ipv4_icmp_type_11
[] = {
130 { 0x28, 0, 0, 0x0000000c }, /* ldh [12] */
131 { 0x15, 0, 8, 0x00000800 }, /* jneq #0x800, drop */
132 { 0x30, 0, 0, 0x00000017 }, /* ldb [23] */
133 { 0x15, 0, 6, 0x00000001 }, /* jneq #0x1, drop */
134 { 0x28, 0, 0, 0x00000014 }, /* ldh [20] */
135 { 0x45, 4, 0, 0x00001fff }, /* jset #0x1fff, drop */
136 { 0xb1, 0, 0, 0x0000000e }, /* ldxb 4*([14]&0xf) */
137 { 0x50, 0, 0, 0x0000000e }, /* ldb [x + 14] */
138 { 0x15, 0, 1, 0x0000000b }, /* jneq #0xb, drop */
139 { 0x06, 0, 0, 0xffffffff }, /* ret #-1 */
140 { 0x06, 0, 0, 0x00000000 }, /* drop: ret #0 */
143 static const struct sock_filter ipv6_icmp6_type_3
[] = {
144 { 0x28, 0, 0, 0x0000000c }, /* ldh [12] */
145 { 0x15, 0, 5, 0x000086dd }, /* jneq #0x86dd, drop */
146 { 0x30, 0, 0, 0x00000014 }, /* ldb [20] */
147 { 0x15, 0, 3, 0x0000003a }, /* jneq #0x3a, drop */
148 { 0x30, 0, 0, 0x00000036 }, /* ldb [54] */
149 { 0x15, 0, 1, 0x00000003 }, /* jneq #0x3, drop */
150 { 0x06, 0, 0, 0xffffffff }, /* ret #-1 */
151 { 0x06, 0, 0, 0x00000000 }, /* drop: ret #0 */
154 static const struct proto_ops af_ops
[] = {
156 .assembler
= assemble_ipv4
,
157 .handler
= handle_ipv4
,
159 .filter
= ipv4_icmp_type_11
,
160 .flen
= array_size(ipv4_icmp_type_11
),
161 .min_len_tcp
= sizeof(struct iphdr
) + sizeof(struct tcphdr
),
162 .min_len_icmp
= sizeof(struct iphdr
) + sizeof(struct icmphdr
),
165 .assembler
= assemble_ipv6
,
166 .handler
= handle_ipv6
,
168 .filter
= ipv6_icmp6_type_3
,
169 .flen
= array_size(ipv6_icmp6_type_3
),
170 .min_len_tcp
= sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
),
171 .min_len_icmp
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6hdr
),
175 static void signal_handler(int number
)
187 static void __noreturn
help(void)
189 printf("astraceroute %s, autonomous system trace route utility\n", VERSION_STRING
);
190 puts("http://www.netsniff-ng.org\n\n"
191 "Usage: astraceroute [options]\n"
193 " -H|--host <host> Host/IPv4/IPv6 to lookup AS route to\n"
194 " -p|--port <port> Hosts port to lookup AS route to\n"
195 " -i|-d|--dev <device> Networking device, e.g. eth0\n"
196 " -b|--bind <IP> IP address to bind to, Must specify -6 for an IPv6 address\n"
197 " -f|--init-ttl <ttl> Set initial TTL\n"
198 " -m|--max-ttl <ttl> Set maximum TTL (def: 30)\n"
199 " -q|--num-probes <num> Number of max probes for each hop (def: 2)\n"
200 " -x|--timeout <sec> Probe response timeout in sec (def: 3)\n"
201 " -X|--payload <string> Specify a payload string to test DPIs\n"
202 " -l|--totlen <len> Specify total packet len\n"
203 " -4|--ipv4 Use IPv4-only requests\n"
204 " -6|--ipv6 Use IPv6-only requests\n"
205 " -n|--numeric Do not do reverse DNS lookup for hops\n"
206 " -u|--update Update GeoIP databases\n"
207 " -L|--latitude Show latitude and longitude\n"
208 " -N|--dns Do a reverse DNS lookup for hops\n"
209 " -S|--syn Set TCP SYN flag\n"
210 " -A|--ack Set TCP ACK flag\n"
211 " -F|--fin Set TCP FIN flag\n"
212 " -P|--psh Set TCP PSH flag\n"
213 " -U|--urg Set TCP URG flag\n"
214 " -R|--rst Set TCP RST flag\n"
215 " -E|--ecn-syn Send ECN SYN packets (RFC3168)\n"
216 " -t|--tos <tos> Set the IP TOS field\n"
217 " -G|--nofrag Set do not fragment bit\n"
218 " -Z|--show-packet Show returned packet on each hop\n"
219 " -v|--version Print version and exit\n"
220 " -h|--help Print this help and exit\n\n"
222 " IPv4 trace of AS with TCP SYN probe (this will most-likely pass):\n"
223 " astraceroute -i eth0 -N -S -H netsniff-ng.org\n"
224 " IPv4 trace of AS with TCP ECN SYN probe:\n"
225 " astraceroute -i eth0 -N -E -H netsniff-ng.org\n"
226 " IPv4 trace of AS with TCP FIN probe:\n"
227 " astraceroute -i eth0 -N -F -H netsniff-ng.org\n"
228 " IPv4 trace of AS with Xmas probe:\n"
229 " astraceroute -i eth0 -N -FPU -H netsniff-ng.org\n"
230 " IPv4 trace of AS with Null probe with ASCII payload:\n"
231 " astraceroute -i eth0 -N -H netsniff-ng.org -X \"censor-me\" -Z\n"
232 " IPv6 trace of AS up to www.6bone.net:\n"
233 " astraceroute -6 -i eth0 -S -E -N -H www.6bone.net\n\n"
235 " If the TCP probe did not give any results, then astraceroute will\n"
236 " automatically probe for classic ICMP packets! To gather more\n"
237 " information about astraceroute's fetched AS numbers, see e.g.\n"
238 " http://bgp.he.net/AS<number>!\n");
243 static void __noreturn
version(void)
245 printf("astraceroute %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
246 puts("autonomous system trace route utility\n"
247 "http://www.netsniff-ng.org\n");
252 static void __assemble_data(uint8_t *packet
, size_t len
, const char *payload
)
256 if (payload
== NULL
) {
257 for (i
= 0; i
< len
; ++i
)
258 packet
[i
] = (uint8_t) rand();
260 size_t lmin
= min(len
, strlen(payload
));
262 for (i
= 0; i
< lmin
; ++i
)
263 packet
[i
] = (uint8_t) payload
[i
];
264 for (i
= lmin
; i
< len
; ++i
)
265 packet
[i
] = (uint8_t) rand();
269 static void __assemble_icmp4(uint8_t *packet
, size_t len
, const struct ctx
*ctx
)
273 struct icmphdr
*icmph
= (struct icmphdr
*) packet
;
275 bug_on(len
< sizeof(*icmph
));
277 icmph
->type
= ICMP_ECHO
;
281 data
= packet
+ sizeof(*icmph
);
282 data_len
= len
- sizeof(*icmph
);
284 __assemble_data(data
, data_len
, ctx
->payload
);
286 icmph
->checksum
= csum((unsigned short *)packet
, len
/ 2);
289 static void __assemble_icmp6(uint8_t *packet
, size_t len
, const struct ctx
*ctx
,
290 const struct sockaddr
*dst
, const struct sockaddr
*src
)
294 struct ip6_hdr ip6hdr
;
295 struct icmp6hdr
*icmp6h
= (struct icmp6hdr
*) packet
;
297 bug_on(len
< sizeof(*icmp6h
));
299 icmp6h
->icmp6_type
= ICMPV6_ECHO_REQUEST
;
300 icmp6h
->icmp6_code
= 0;
301 icmp6h
->icmp6_cksum
= 0;
303 data
= packet
+ sizeof(*icmp6h
);
304 data_len
= len
- sizeof(*icmp6h
);
306 __assemble_data(data
, data_len
, ctx
->payload
);
308 memcpy(&ip6hdr
.ip6_src
, &((const struct sockaddr_in6
*) src
)->sin6_addr
,
309 sizeof(ip6hdr
.ip6_src
));
310 memcpy(&ip6hdr
.ip6_dst
, &((const struct sockaddr_in6
*) dst
)->sin6_addr
,
311 sizeof(ip6hdr
.ip6_dst
));
313 icmp6h
->icmp6_cksum
= p6_csum(&ip6hdr
, packet
, sizeof(*icmp6h
) + data_len
, IPPROTO_ICMPV6
);
316 static size_t __assemble_tcp_header(struct tcphdr
*tcph
, const struct ctx
*ctx
)
318 tcph
->source
= htons((uint16_t) rand());
319 tcph
->dest
= htons((uint16_t) ctx
->dport
);
321 tcph
->seq
= htonl(rand());
322 tcph
->ack_seq
= (!!ctx
->ack
? htonl(rand()) : 0);
326 tcph
->syn
= !!ctx
->syn
;
327 tcph
->ack
= !!ctx
->ack
;
328 tcph
->urg
= !!ctx
->urg
;
329 tcph
->fin
= !!ctx
->fin
;
330 tcph
->rst
= !!ctx
->rst
;
331 tcph
->psh
= !!ctx
->psh
;
332 tcph
->ece
= !!ctx
->ecn
;
333 tcph
->cwr
= !!ctx
->ecn
;
335 tcph
->window
= htons((uint16_t) (100 + (rand() % 65435)));
337 tcph
->urg_ptr
= (!!ctx
->urg
? htons((uint16_t) rand()) : 0);
339 return tcph
->doff
* 4;
342 static void __assemble_tcp(uint8_t *packet
, size_t len
, const struct ctx
*ctx
,
343 const struct sockaddr
*dst
, const struct sockaddr
*src
)
347 size_t tcp_len
, data_len
;
349 struct tcphdr
*tcph
= (struct tcphdr
*) packet
;
351 bug_on(len
< sizeof(*tcph
));
353 tcp_len
= __assemble_tcp_header(tcph
, ctx
);
355 data
= packet
+ tcp_len
;
356 data_len
= len
- tcp_len
;
358 __assemble_data(data
, data_len
, ctx
->payload
);
360 memcpy(&iphdr
.ip_src
, &((const struct sockaddr_in
*) src
)->sin_addr
.s_addr
,
361 sizeof(iphdr
.ip_src
));
362 memcpy(&iphdr
.ip_dst
, &((const struct sockaddr_in
*) dst
)->sin_addr
.s_addr
,
363 sizeof(iphdr
.ip_dst
));
365 tcph
->check
= p4_csum(&iphdr
, packet
, tcp_len
+ data_len
, IPPROTO_TCP
);
368 static void __assemble_tcp6(uint8_t *packet
, size_t len
, const struct ctx
*ctx
,
369 const struct sockaddr
*dst
, const struct sockaddr
*src
)
372 size_t tcp_len
, data_len
;
373 struct ip6_hdr ip6hdr
;
374 struct tcphdr
*tcph
= (struct tcphdr
*) packet
;
376 bug_on(len
< sizeof(*tcph
));
378 tcp_len
= __assemble_tcp_header(tcph
, ctx
);
380 data
= packet
+ tcp_len
;
381 data_len
= len
- tcp_len
;
383 __assemble_data(data
, data_len
, ctx
->payload
);
385 memcpy(&ip6hdr
.ip6_src
, &((const struct sockaddr_in6
*) src
)->sin6_addr
,
386 sizeof(ip6hdr
.ip6_src
));
387 memcpy(&ip6hdr
.ip6_dst
, &((const struct sockaddr_in6
*) dst
)->sin6_addr
,
388 sizeof(ip6hdr
.ip6_dst
));
390 tcph
->check
= p6_csum(&ip6hdr
, packet
, tcp_len
+ data_len
, IPPROTO_TCP
);
393 static int assemble_ipv4(uint8_t *packet
, size_t len
, int ttl
, int proto
,
394 const struct ctx
*ctx
, const struct sockaddr
*dst
,
395 const struct sockaddr
*src
)
399 struct iphdr
*iph
= (struct iphdr
*) packet
;
401 bug_on(!src
|| !dst
);
402 bug_on(src
->sa_family
!= PF_INET
|| dst
->sa_family
!= PF_INET
);
403 bug_on(len
< sizeof(*iph
) + min(sizeof(struct tcphdr
),
404 sizeof(struct icmphdr
)));
408 iph
->tos
= (uint8_t) ctx
->tos
;
410 iph
->tot_len
= htons((uint16_t) len
);
411 iph
->id
= htons((uint16_t) rand());
413 iph
->frag_off
= ctx
->nofrag
? IP_DF
: 0;
414 iph
->ttl
= (uint8_t) ttl
;
416 iph
->saddr
= ((const struct sockaddr_in
*) src
)->sin_addr
.s_addr
;
417 iph
->daddr
= ((const struct sockaddr_in
*) dst
)->sin_addr
.s_addr
;
419 iph
->protocol
= (uint8_t) proto
;
422 data
= packet
+ sizeof(*iph
);
423 data_len
= len
- sizeof(*iph
);
427 __assemble_tcp(data
, data_len
, ctx
, dst
, src
);
431 __assemble_icmp4(data
, data_len
, ctx
);
439 iph
->check
= csum((unsigned short *) packet
, ntohs(iph
->tot_len
) >> 1);
441 return ntohs(iph
->id
);
444 static int assemble_ipv6(uint8_t *packet
, size_t len
, int ttl
, int proto
,
445 const struct ctx
*ctx
, const struct sockaddr
*dst
,
446 const struct sockaddr
*src
)
450 struct ip6_hdr
*ip6h
= (struct ip6_hdr
*) packet
;
452 bug_on(!src
|| !dst
);
453 bug_on(src
->sa_family
!= PF_INET6
|| dst
->sa_family
!= PF_INET6
);
454 bug_on(len
< sizeof(*ip6h
) + min(sizeof(struct tcphdr
),
455 sizeof(struct icmp6hdr
)));
457 ip6h
->ip6_flow
= htonl(rand() & 0x000fffff);
458 ip6h
->ip6_vfc
= 0x60;
460 ip6h
->ip6_plen
= htons((uint16_t) len
- sizeof(*ip6h
));
461 ip6h
->ip6_nxt
= (uint8_t) proto
;
462 ip6h
->ip6_hlim
= (uint8_t) ttl
;
464 memcpy(&ip6h
->ip6_src
, &(((const struct sockaddr_in6
*)
465 src
)->sin6_addr
), sizeof(ip6h
->ip6_src
));
466 memcpy(&ip6h
->ip6_dst
, &(((const struct sockaddr_in6
*)
467 dst
)->sin6_addr
), sizeof(ip6h
->ip6_dst
));
469 data
= packet
+ sizeof(*ip6h
);
470 data_len
= len
- sizeof(*ip6h
);
474 __assemble_tcp6(data
, data_len
, ctx
, dst
, src
);
479 __assemble_icmp6(data
, data_len
, ctx
, dst
, src
);
486 return ntohl(ip6h
->ip6_flow
) & 0x000fffff;
489 static int check_ipv4(uint8_t *packet
, size_t len
, int ttl __maybe_unused
,
490 int id
, const struct sockaddr
*ss
)
492 struct iphdr
*iph
= (struct iphdr
*) packet
;
493 struct iphdr
*iph_inner
;
494 struct icmphdr
*icmph
;
496 if (iph
->protocol
!= IPPROTO_ICMP
)
498 if (iph
->daddr
!= ((const struct sockaddr_in
*) ss
)->sin_addr
.s_addr
)
501 icmph
= (struct icmphdr
*) (packet
+ sizeof(struct iphdr
));
502 if (icmph
->type
!= ICMP_TIME_EXCEEDED
)
504 if (icmph
->code
!= ICMP_EXC_TTL
)
507 iph_inner
= (struct iphdr
*) (packet
+ sizeof(struct iphdr
) +
508 sizeof(struct icmphdr
));
509 if (ntohs(iph_inner
->id
) != id
)
515 static void handle_ipv4(uint8_t *packet
, size_t len __maybe_unused
,
516 int dns_resolv
, int latitude
)
518 char hbuff
[NI_MAXHOST
];
519 struct iphdr
*iph
= (struct iphdr
*) packet
;
520 struct sockaddr_in sd
;
521 struct hostent
*hent
;
522 const char *as
, *country
;
525 memset(hbuff
, 0, sizeof(hbuff
));
526 memset(&sd
, 0, sizeof(sd
));
527 sd
.sin_family
= PF_INET
;
528 sd
.sin_addr
.s_addr
= iph
->saddr
;
530 getnameinfo((struct sockaddr
*) &sd
, sizeof(sd
),
531 hbuff
, sizeof(hbuff
), NULL
, 0, NI_NUMERICHOST
);
533 as
= geoip4_as_name(&sd
);
534 country
= geoip4_country_name(&sd
);
535 city
= geoip4_city_name(&sd
);
538 hent
= gethostbyaddr(&sd
.sin_addr
, sizeof(sd
.sin_addr
), PF_INET
);
540 printf(" %s (%s)", hent
->h_name
, hbuff
);
542 printf(" %s", hbuff
);
544 printf(" %s", hbuff
);
547 printf(" in %s", as
);
549 printf(" in %s", country
);
551 printf(", %s", city
);
554 printf(" (%f/%f)", geoip4_latitude(&sd
), geoip4_longitude(&sd
));
559 static int check_ipv6(uint8_t *packet
, size_t len
, int ttl __maybe_unused
,
560 int id
, const struct sockaddr
*ss
)
562 struct ip6_hdr
*ip6h
= (struct ip6_hdr
*) packet
;
563 struct ip6_hdr
*ip6h_inner
;
564 struct icmp6hdr
*icmp6h
;
566 if (ip6h
->ip6_nxt
!= IPPROTO_ICMPV6
)
568 if (memcmp(&ip6h
->ip6_dst
, &(((const struct sockaddr_in6
*)
569 ss
)->sin6_addr
), sizeof(ip6h
->ip6_dst
)))
572 icmp6h
= (struct icmp6hdr
*) (packet
+ sizeof(*ip6h
));
573 if (icmp6h
->icmp6_type
!= ICMPV6_TIME_EXCEED
)
575 if (icmp6h
->icmp6_code
!= ICMPV6_EXC_HOPLIMIT
)
578 ip6h_inner
= (struct ip6_hdr
*) (packet
+ sizeof(*ip6h
) + sizeof(*icmp6h
));
579 if ((ntohl(ip6h_inner
->ip6_flow
) & 0x000fffff) != (uint32_t) id
)
585 static void handle_ipv6(uint8_t *packet
, size_t len __maybe_unused
,
586 int dns_resolv
, int latitude
)
588 char hbuff
[NI_MAXHOST
];
589 struct ip6_hdr
*ip6h
= (struct ip6_hdr
*) packet
;
590 struct sockaddr_in6 sd
;
591 struct hostent
*hent
;
592 const char *as
, *country
;
595 memset(hbuff
, 0, sizeof(hbuff
));
596 memset(&sd
, 0, sizeof(sd
));
597 sd
.sin6_family
= PF_INET6
;
598 memcpy(&sd
.sin6_addr
, &ip6h
->ip6_src
, sizeof(ip6h
->ip6_src
));
600 getnameinfo((struct sockaddr
*) &sd
, sizeof(sd
),
601 hbuff
, sizeof(hbuff
), NULL
, 0, NI_NUMERICHOST
);
603 as
= geoip6_as_name(&sd
);
604 country
= geoip6_country_name(&sd
);
605 city
= geoip6_city_name(&sd
);
608 hent
= gethostbyaddr(&sd
.sin6_addr
, sizeof(sd
.sin6_addr
), PF_INET6
);
610 printf(" %s (%s)", hent
->h_name
, hbuff
);
612 printf(" %s", hbuff
);
614 printf(" %s", hbuff
);
617 printf(" in %s", as
);
619 printf(" in %s", country
);
621 printf(", %s", city
);
624 printf(" (%f/%f)", geoip6_latitude(&sd
), geoip6_longitude(&sd
));
629 static void show_trace_info(struct ctx
*ctx
, const struct sockaddr_storage
*ss
,
630 const struct sockaddr_storage
*sd
)
632 char hbuffs
[256], hbuffd
[256];
634 memset(hbuffd
, 0, sizeof(hbuffd
));
635 getnameinfo((struct sockaddr
*) sd
, sizeof(*sd
),
636 hbuffd
, sizeof(hbuffd
), NULL
, 0, NI_NUMERICHOST
);
638 memset(hbuffs
, 0, sizeof(hbuffs
));
639 getnameinfo((struct sockaddr
*) ss
, sizeof(*ss
),
640 hbuffs
, sizeof(hbuffs
), NULL
, 0, NI_NUMERICHOST
);
642 printf("AS path IPv%d TCP trace from %s to %s:%s (%s) with len %zu "
643 "Bytes, %u max hops\n", ctx
->proto
== IPPROTO_IP
? 4 : 6,
644 hbuffs
, hbuffd
, ctx
->port
, ctx
->host
, ctx
->totlen
, ctx
->max_ttl
);
646 printf("Using flags SYN:%d,ACK:%d,ECN:%d,FIN:%d,PSH:%d,RST:%d,URG:%d\n",
647 ctx
->syn
, ctx
->ack
, ctx
->ecn
, ctx
->fin
, ctx
->psh
, ctx
->rst
, ctx
->urg
);
650 printf("With payload: \'%s\'\n", ctx
->payload
);
653 static int get_remote_fd(struct ctx
*ctx
, struct sockaddr_storage
*ss
,
654 struct sockaddr_storage
*sd
)
656 int fd
= -1, ret
, one
= 1, af
= AF_INET
;
657 struct addrinfo hints
, *ahead
, *ai
;
658 unsigned char bind_ip
[sizeof(struct in6_addr
)];
660 memset(&hints
, 0, sizeof(hints
));
661 hints
.ai_family
= PF_UNSPEC
;
662 hints
.ai_socktype
= SOCK_STREAM
;
663 hints
.ai_protocol
= IPPROTO_TCP
;
664 hints
.ai_flags
= AI_NUMERICSERV
;
666 ret
= getaddrinfo(ctx
->host
, ctx
->port
, &hints
, &ahead
);
668 panic("Cannot get address info!\n");
670 for (ai
= ahead
; ai
!= NULL
&& fd
< 0; ai
= ai
->ai_next
) {
671 if (!((ai
->ai_family
== PF_INET6
&& ctx
->proto
== IPPROTO_IPV6
) ||
672 (ai
->ai_family
== PF_INET
&& ctx
->proto
== IPPROTO_IP
)))
675 fd
= socket(ai
->ai_family
, SOCK_RAW
, IPPROTO_RAW
);
679 memset(ss
, 0, sizeof(*ss
));
680 ret
= device_address(ctx
->dev
, ai
->ai_family
, ss
);
681 if (ret
< 0 && !ctx
->bind_addr
)
682 panic("Cannot get own device address!\n");
684 if (ctx
->bind_addr
) {
685 if (ctx
->proto
== IPPROTO_IPV6
)
688 if (inet_pton(af
, ctx
->bind_addr
, &bind_ip
) != 1)
689 panic("Address is invalid!\n");
691 if (af
== AF_INET6
) {
692 struct sockaddr_in6
*ss6
= (struct sockaddr_in6
*) ss
;
693 memcpy(&ss6
->sin6_addr
.s6_addr
, &bind_ip
, sizeof(struct in6_addr
));
695 struct sockaddr_in
*ss4
= (struct sockaddr_in
*) ss
;
696 memcpy(&ss4
->sin_addr
.s_addr
, &bind_ip
, sizeof(struct in_addr
));
700 ret
= bind(fd
, (struct sockaddr
*) ss
, sizeof(*ss
));
702 panic("Cannot bind socket!\n");
704 memset(sd
, 0, sizeof(*sd
));
705 memcpy(sd
, ai
->ai_addr
, ai
->ai_addrlen
);
707 ctx
->sd_len
= ai
->ai_addrlen
;
708 ctx
->dport
= strtoul(ctx
->port
, NULL
, 10);
710 ret
= setsockopt(fd
, ctx
->proto
, IP_HDRINCL
, &one
, sizeof(one
));
712 panic("Kernel does not support IP_HDRINCL!\n");
714 if (ai
->ai_family
== PF_INET6
) {
715 struct sockaddr_in6
*sd6
= (struct sockaddr_in6
*) sd
;
726 panic("Cannot create socket! Does remote "
728 ctx
->proto
== IPPROTO_IP
? 4 : 6);
733 static void inject_filter(struct ctx
*ctx
, int fd
)
735 struct sock_fprog bpf_ops
;
737 enable_kernel_bpf_jit_compiler();
739 memset(&bpf_ops
, 0, sizeof(bpf_ops
));
740 bpf_ops
.filter
= (struct sock_filter
*) af_ops
[ctx
->proto
].filter
;
741 bpf_ops
.len
= af_ops
[ctx
->proto
].flen
;
743 bpf_attach_to_sock(fd
, &bpf_ops
);
746 static int __process_node(struct ctx
*ctx
, int fd
, int fd_cap
, int ttl
,
747 int inner_proto
, uint8_t *pkt_snd
, uint8_t *pkt_rcv
,
748 const struct sockaddr_storage
*ss
,
749 const struct sockaddr_storage
*sd
, struct timeval
*diff
)
751 int pkt_id
, ret
, timeout
;
753 struct timeval start
, end
;
755 prepare_polling(fd_cap
, &pfd
);
757 memset(pkt_snd
, 0, ctx
->totlen
);
758 pkt_id
= af_ops
[ctx
->proto
].assembler(pkt_snd
, ctx
->totlen
, ttl
,
760 (const struct sockaddr
*) sd
,
761 (const struct sockaddr
*) ss
);
763 ret
= sendto(fd
, pkt_snd
, ctx
->totlen
, 0, (struct sockaddr
*) sd
,
766 panic("sendto failed: %s\n", strerror(errno
));
768 bug_on(gettimeofday(&start
, NULL
));
770 timeout
= (ctx
->timeout
> 0 ? ctx
->timeout
: 2) * 1000;
772 ret
= poll(&pfd
, 1, timeout
);
773 if (ret
> 0 && pfd
.revents
& POLLIN
&& sigint
== 0) {
774 bug_on(gettimeofday(&end
, NULL
));
776 timersub(&end
, &start
, diff
);
778 ret
= recvfrom(fd_cap
, pkt_rcv
, ctx
->rcvlen
, 0, NULL
, NULL
);
779 if (ret
< (int) (sizeof(struct ethhdr
) + af_ops
[ctx
->proto
].min_len_icmp
))
782 return af_ops
[ctx
->proto
].check(pkt_rcv
+ sizeof(struct ethhdr
),
783 ret
- sizeof(struct ethhdr
), ttl
,
784 pkt_id
, (const struct sockaddr
*) ss
);
792 static void timerdiv(const unsigned long divisor
, const struct timeval
*tv
,
793 struct timeval
*result
)
795 uint64_t x
= ((uint64_t) tv
->tv_sec
* 1000 * 1000 + tv
->tv_usec
) / divisor
;
797 result
->tv_sec
= x
/ 1000 / 1000;
798 result
->tv_usec
= x
% (1000 * 1000);
801 static int timevalcmp(const void *t1
, const void *t2
)
803 if (timercmp((struct timeval
*) t1
, (struct timeval
*) t2
, <))
805 if (timercmp((struct timeval
*) t1
, (struct timeval
*) t2
, >))
811 static const char *proto_short(int proto
)
824 static int __process_time(struct ctx
*ctx
, int fd
, int fd_cap
, int ttl
,
825 int inner_proto
, uint8_t *pkt_snd
, uint8_t *pkt_rcv
,
826 const struct sockaddr_storage
*ss
,
827 const struct sockaddr_storage
*sd
)
830 int good
= 0, ret
= -EIO
, idx
, ret_good
= -EIO
;
831 struct timeval probes
[9], *tmp
, sum
, res
;
832 uint8_t *trash
= xmalloc(ctx
->rcvlen
);
833 char *cwait
[] = { "-", "\\", "|", "/" };
835 memset(probes
, 0, sizeof(probes
));
836 for (i
= 0; i
< array_size(probes
) && sigint
== 0; ++i
) {
837 ret
= __process_node(ctx
, fd
, fd_cap
, ttl
, inner_proto
,
838 pkt_snd
, good
== 0 ? pkt_rcv
: trash
,
846 if (good
== 0 && ctx
->queries
== (int) i
)
851 printf("\r%2d: %s", ttl
, cwait
[j
++]);
853 if (j
>= array_size(cwait
))
862 tmp
= xcalloc(good
, sizeof(struct timeval
));
863 for (i
= j
= 0; i
< array_size(probes
); ++i
) {
864 if (probes
[i
].tv_sec
== 0 && probes
[i
].tv_usec
== 0)
866 tmp
[j
].tv_sec
= probes
[i
].tv_sec
;
867 tmp
[j
].tv_usec
= probes
[i
].tv_usec
;
871 qsort(tmp
, j
, sizeof(struct timeval
), timevalcmp
);
873 printf("\r%2d: %s[", ttl
, proto_short(inner_proto
));
877 timeradd(&tmp
[idx
], &tmp
[idx
- 1], &sum
);
878 timerdiv(2, &sum
, &res
);
880 printf("%lu sec ", res
.tv_sec
);
881 printf("%7lu us", res
.tv_usec
);
884 if (tmp
[idx
].tv_sec
> 0)
885 printf("%lu sec ", tmp
[idx
].tv_sec
);
886 printf("%7lu us", tmp
[idx
].tv_usec
);
897 static int __probe_remote(struct ctx
*ctx
, int fd
, int fd_cap
, int ttl
,
898 uint8_t *pkt_snd
, uint8_t *pkt_rcv
,
899 const struct sockaddr_storage
*ss
,
900 const struct sockaddr_storage
*sd
,
903 int ret
= -EIO
, tries
= ctx
->queries
;
905 while (tries
-- > 0 && sigint
== 0) {
906 ret
= __process_time(ctx
, fd
, fd_cap
, ttl
, inner_proto
,
907 pkt_snd
, pkt_rcv
, ss
, sd
);
911 af_ops
[ctx
->proto
].handler(pkt_rcv
+ sizeof(struct ethhdr
),
912 ret
- sizeof(struct ethhdr
),
913 ctx
->dns_resolv
, ctx
->latitude
);
915 struct pkt_buff
*pkt
;
918 pkt
= pkt_alloc(pkt_rcv
, ret
);
930 static int __process_ttl(struct ctx
*ctx
, int fd
, int fd_cap
, int ttl
,
931 uint8_t *pkt_snd
, uint8_t *pkt_rcv
,
932 const struct sockaddr_storage
*ss
,
933 const struct sockaddr_storage
*sd
)
937 const int inner_protos
[] = {
942 printf("%2d: ", ttl
);
945 for (i
= 0; i
< array_size(inner_protos
) && sigint
== 0; ++i
) {
946 ret
= __probe_remote(ctx
, fd
, fd_cap
, ttl
, pkt_snd
, pkt_rcv
, ss
, sd
,
953 printf("\r%2d: ?[ no answer]", ttl
);
956 if (ctx
->show
&& ret
<= 0)
963 static int main_trace(struct ctx
*ctx
)
965 int fd
, fd_cap
, ifindex
, ttl
;
966 struct ring dummy_ring
;
967 struct sockaddr_storage ss
, sd
;
968 uint8_t *pkt_snd
, *pkt_rcv
;
970 fd
= get_remote_fd(ctx
, &ss
, &sd
);
971 fd_cap
= pf_socket();
973 inject_filter(ctx
, fd_cap
);
975 ifindex
= device_ifindex(ctx
->dev
);
976 bind_ring_generic(fd_cap
, &dummy_ring
, ifindex
, false);
978 if (ctx
->totlen
< af_ops
[ctx
->proto
].min_len_tcp
) {
979 ctx
->totlen
= af_ops
[ctx
->proto
].min_len_tcp
;
981 ctx
->totlen
+= strlen(ctx
->payload
);
984 ctx
->rcvlen
= device_mtu(ctx
->dev
) - sizeof(struct ethhdr
);
985 if (ctx
->totlen
>= ctx
->rcvlen
)
986 panic("Packet len exceeds device MTU!\n");
988 pkt_snd
= xmalloc(ctx
->totlen
);
989 pkt_rcv
= xmalloc(ctx
->rcvlen
);
991 show_trace_info(ctx
, &ss
, &sd
);
993 for (ttl
= ctx
->init_ttl
; ttl
<= ctx
->max_ttl
&& sigint
== 0; ++ttl
)
994 __process_ttl(ctx
, fd
, fd_cap
, ttl
, pkt_snd
, pkt_rcv
,
1006 int main(int argc
, char **argv
)
1016 memset(&ctx
, 0, sizeof(ctx
));
1021 ctx
.proto
= IPPROTO_IP
;
1023 ctx
.dev
= xstrdup("eth0");
1024 ctx
.port
= xstrdup("80");
1025 ctx
.bind_addr
= NULL
;
1027 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1041 ctx
.host
= xstrdup(optarg
);
1046 ctx
.port
= xstrdup(optarg
);
1052 ctx
.proto
= IPPROTO_IP
;
1055 ctx
.proto
= IPPROTO_IPV6
;
1064 ctx
.init_ttl
= atoi(optarg
);
1065 if (ctx
.init_ttl
<= 0)
1069 ctx
.max_ttl
= atoi(optarg
);
1070 if (ctx
.max_ttl
<= 0)
1074 ctx
.bind_addr
= xstrdup(optarg
);
1079 ctx
.dev
= xstrdup(optarg
);
1082 ctx
.queries
= atoi(optarg
);
1083 if (ctx
.queries
<= 0)
1087 ctx
.timeout
= atoi(optarg
);
1088 if (ctx
.timeout
<= 0)
1117 ctx
.tos
= atoi(optarg
);
1125 ctx
.payload
= xstrdup(optarg
);
1128 ctx
.totlen
= strtoul(optarg
, NULL
, 10);
1129 if (ctx
.totlen
== 0)
1145 panic("Option -%c requires an argument!\n",
1148 if (isprint(optopt
))
1149 printf("Unknown option character `0x%X\'!\n", optopt
);
1157 if (argc
< 3 || !ctx
.host
|| !ctx
.port
|| ctx
.init_ttl
> ctx
.max_ttl
||
1158 ctx
.init_ttl
> MAXTTL
|| ctx
.max_ttl
> MAXTTL
)
1161 if (!device_up_and_running(ctx
.dev
))
1162 panic("Networking device not up and running!\n");
1163 if (device_mtu(ctx
.dev
) <= ctx
.totlen
)
1164 panic("Packet larger than device MTU!\n");
1166 register_signal(SIGHUP
, signal_handler
);
1167 register_signal(SIGINT
, signal_handler
);
1168 register_signal(SIGQUIT
, signal_handler
);
1169 register_signal(SIGTERM
, signal_handler
);
1174 ret
= main_trace(&ctx
);
1182 free(ctx
.bind_addr
);