2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 - 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
14 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <sys/fsuid.h>
21 #include <asm/byteorder.h>
22 #include <linux/tcp.h>
23 #include <netinet/ip.h>
24 #include <netinet/ip6.h>
25 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <linux/if_ether.h>
31 #include <linux/icmp.h>
32 #include <linux/icmpv6.h>
51 char *host
, *port
, *dev
, *payload
;
52 size_t totlen
, rcvlen
;
53 int init_ttl
, max_ttl
, dns_resolv
, queries
, timeout
;
54 int syn
, ack
, ecn
, fin
, psh
, rst
, urg
, tos
, nofrag
, proto
, show
;
55 int sd_len
, dport
, latitude
;
59 int (*assembler
)(uint8_t *packet
, size_t len
, int ttl
, int proto
,
60 const struct ctx
*ctx
, const struct sockaddr
*dst
,
61 const struct sockaddr
*src
);
62 const struct sock_filter
*filter
;
64 size_t min_len_tcp
, min_len_icmp
;
65 int (*check
)(uint8_t *packet
, size_t len
, int ttl
, int id
,
66 const struct sockaddr
*src
);
67 void (*handler
)(uint8_t *packet
, size_t len
, int dns_resolv
,
71 static sig_atomic_t sigint
= 0;
73 static int assemble_ipv4(uint8_t *packet
, size_t len
, int ttl
, int proto
,
74 const struct ctx
*ctx
, const struct sockaddr
*dst
,
75 const struct sockaddr
*src
);
76 static int assemble_ipv6(uint8_t *packet
, size_t len
, int ttl
, int proto
,
77 const struct ctx
*ctx
, const struct sockaddr
*dst
,
78 const struct sockaddr
*src
);
79 static int check_ipv4(uint8_t *packet
, size_t len
, int ttl
, int id
,
80 const struct sockaddr
*ss
);
81 static void handle_ipv4(uint8_t *packet
, size_t len
, int dns_resolv
,
83 static int check_ipv6(uint8_t *packet
, size_t len
, int ttl
, int id
,
84 const struct sockaddr
*ss
);
85 static void handle_ipv6(uint8_t *packet
, size_t len
, int dns_resolv
,
88 static const char *short_options
= "H:p:nNf:m:i:d:q:x:SAEFPURt:Gl:hv46X:ZuL";
89 static const struct option long_options
[] = {
90 {"host", required_argument
, NULL
, 'H'},
91 {"port", required_argument
, NULL
, 'p'},
92 {"init-ttl", required_argument
, NULL
, 'f'},
93 {"max-ttl", required_argument
, NULL
, 'm'},
94 {"dev", required_argument
, NULL
, 'd'},
95 {"num-probes", required_argument
, NULL
, 'q'},
96 {"timeout", required_argument
, NULL
, 'x'},
97 {"tos", required_argument
, NULL
, 't'},
98 {"payload", required_argument
, NULL
, 'X'},
99 {"totlen", required_argument
, NULL
, 'l'},
100 {"numeric", no_argument
, NULL
, 'n'},
101 {"latitude", no_argument
, NULL
, 'L'},
102 {"update", no_argument
, NULL
, 'u'},
103 {"dns", no_argument
, NULL
, 'N'},
104 {"ipv4", no_argument
, NULL
, '4'},
105 {"ipv6", no_argument
, NULL
, '6'},
106 {"syn", no_argument
, NULL
, 'S'},
107 {"ack", no_argument
, NULL
, 'A'},
108 {"urg", no_argument
, NULL
, 'U'},
109 {"fin", no_argument
, NULL
, 'F'},
110 {"psh", no_argument
, NULL
, 'P'},
111 {"rst", no_argument
, NULL
, 'R'},
112 {"ecn-syn", no_argument
, NULL
, 'E'},
113 {"show-packet", no_argument
, NULL
, 'Z'},
114 {"nofrag", no_argument
, NULL
, 'G'},
115 {"version", no_argument
, NULL
, 'v'},
116 {"help", no_argument
, NULL
, 'h'},
120 static const struct sock_filter ipv4_icmp_type_11
[] = {
121 { 0x28, 0, 0, 0x0000000c }, /* ldh [12] */
122 { 0x15, 0, 8, 0x00000800 }, /* jneq #0x800, drop */
123 { 0x30, 0, 0, 0x00000017 }, /* ldb [23] */
124 { 0x15, 0, 6, 0x00000001 }, /* jneq #0x1, drop */
125 { 0x28, 0, 0, 0x00000014 }, /* ldh [20] */
126 { 0x45, 4, 0, 0x00001fff }, /* jset #0x1fff, drop */
127 { 0xb1, 0, 0, 0x0000000e }, /* ldxb 4*([14]&0xf) */
128 { 0x50, 0, 0, 0x0000000e }, /* ldb [x + 14] */
129 { 0x15, 0, 1, 0x0000000b }, /* jneq #0xb, drop */
130 { 0x06, 0, 0, 0xffffffff }, /* ret #-1 */
131 { 0x06, 0, 0, 0x00000000 }, /* drop: ret #0 */
134 static const struct sock_filter ipv6_icmp6_type_3
[] = {
135 { 0x28, 0, 0, 0x0000000c }, /* ldh [12] */
136 { 0x15, 0, 5, 0x000086dd }, /* jneq #0x86dd, drop */
137 { 0x30, 0, 0, 0x00000014 }, /* ldb [20] */
138 { 0x15, 0, 3, 0x0000003a }, /* jneq #0x3a, drop */
139 { 0x30, 0, 0, 0x00000036 }, /* ldb [54] */
140 { 0x15, 0, 1, 0x00000003 }, /* jneq #0x3, drop */
141 { 0x06, 0, 0, 0xffffffff }, /* ret #-1 */
142 { 0x06, 0, 0, 0x00000000 }, /* drop: ret #0 */
145 static const struct proto_ops af_ops
[] = {
147 .assembler
= assemble_ipv4
,
148 .handler
= handle_ipv4
,
150 .filter
= ipv4_icmp_type_11
,
151 .flen
= array_size(ipv4_icmp_type_11
),
152 .min_len_tcp
= sizeof(struct iphdr
) + sizeof(struct tcphdr
),
153 .min_len_icmp
= sizeof(struct iphdr
) + sizeof(struct icmphdr
),
156 .assembler
= assemble_ipv6
,
157 .handler
= handle_ipv6
,
159 .filter
= ipv6_icmp6_type_3
,
160 .flen
= array_size(ipv6_icmp6_type_3
),
161 .min_len_tcp
= sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
),
162 .min_len_icmp
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6hdr
),
166 static void signal_handler(int number
)
176 static void __noreturn
help(void)
178 printf("\nastraceroute %s, autonomous system trace route utility\n", VERSION_STRING
);
179 puts("http://www.netsniff-ng.org\n\n"
180 "Usage: astraceroute [options]\n"
182 " -H|--host <host> Host/IPv4/IPv6 to lookup AS route to\n"
183 " -p|--port <port> Hosts port to lookup AS route to\n"
184 " -i|-d|--dev <device> Networking device, e.g. eth0\n"
185 " -f|--init-ttl <ttl> Set initial TTL\n"
186 " -m|--max-ttl <ttl> Set maximum TTL (def: 30)\n"
187 " -q|--num-probes <num> Number of max probes for each hop (def: 2)\n"
188 " -x|--timeout <sec> Probe response timeout in sec (def: 3)\n"
189 " -X|--payload <string> Specify a payload string to test DPIs\n"
190 " -l|--totlen <len> Specify total packet len\n"
191 " -4|--ipv4 Use IPv4-only requests\n"
192 " -6|--ipv6 Use IPv6-only requests\n"
193 " -n|--numeric Do not do reverse DNS lookup for hops\n"
194 " -u|--update Update GeoIP databases\n"
195 " -L|--latitude Show latitude and longtitude\n"
196 " -N|--dns Do a reverse DNS lookup for hops\n"
197 " -S|--syn Set TCP SYN flag\n"
198 " -A|--ack Set TCP ACK flag\n"
199 " -F|--fin Set TCP FIN flag\n"
200 " -P|--psh Set TCP PSH flag\n"
201 " -U|--urg Set TCP URG flag\n"
202 " -R|--rst Set TCP RST flag\n"
203 " -E|--ecn-syn Send ECN SYN packets (RFC3168)\n"
204 " -t|--tos <tos> Set the IP TOS field\n"
205 " -G|--nofrag Set do not fragment bit\n"
206 " -Z|--show-packet Show returned packet on each hop\n"
207 " -v|--version Print version and exit\n"
208 " -h|--help Print this help and exit\n\n"
210 " IPv4 trace of AS with TCP SYN probe (this will most-likely pass):\n"
211 " astraceroute -i eth0 -N -S -H netsniff-ng.org\n"
212 " IPv4 trace of AS with TCP ECN SYN probe:\n"
213 " astraceroute -i eth0 -N -E -H netsniff-ng.org\n"
214 " IPv4 trace of AS with TCP FIN probe:\n"
215 " astraceroute -i eth0 -N -F -H netsniff-ng.org\n"
216 " IPv4 trace of AS with Xmas probe:\n"
217 " astraceroute -i eth0 -N -FPU -H netsniff-ng.org\n"
218 " IPv4 trace of AS with Null probe with ASCII payload:\n"
219 " astraceroute -i eth0 -N -H netsniff-ng.org -X \"censor-me\" -Z\n"
220 " IPv6 trace of AS up to www.6bone.net:\n"
221 " astraceroute -6 -i eth0 -S -E -N -H www.6bone.net\n\n"
223 " If the TCP probe did not give any results, then astraceroute will\n"
224 " automatically probe for classic ICMP packets! To gather more\n"
225 " information about astraceroute's fetched AS numbers, see e.g.\n"
226 " http://bgp.he.net/AS<number>!\n\n"
227 "Please report bugs to <bugs@netsniff-ng.org>\n"
228 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
229 "Swiss federal institute of technology (ETH Zurich)\n"
230 "License: GNU GPL version 2.0\n"
231 "This is free software: you are free to change and redistribute it.\n"
232 "There is NO WARRANTY, to the extent permitted by law.\n");
236 static void __noreturn
version(void)
238 printf("\nastraceroute %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
239 puts("autonomous system trace route utility\n"
240 "http://www.netsniff-ng.org\n\n"
241 "Please report bugs to <bugs@netsniff-ng.org>\n"
242 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
243 "Swiss federal institute of technology (ETH Zurich)\n"
244 "License: GNU GPL version 2.0\n"
245 "This is free software: you are free to change and redistribute it.\n"
246 "There is NO WARRANTY, to the extent permitted by law.\n");
250 static void __assemble_data(uint8_t *packet
, size_t len
, const char *payload
)
254 if (payload
== NULL
) {
255 for (i
= 0; i
< len
; ++i
)
256 packet
[i
] = (uint8_t) rand();
258 size_t lmin
= min(len
, strlen(payload
));
260 for (i
= 0; i
< lmin
; ++i
)
261 packet
[i
] = (uint8_t) payload
[i
];
262 for (i
= lmin
; i
< len
; ++i
)
263 packet
[i
] = (uint8_t) rand();
267 static void __assemble_icmp4(uint8_t *packet
, size_t len
)
269 struct icmphdr
*icmph
= (struct icmphdr
*) packet
;
271 bug_on(len
< sizeof(struct icmphdr
));
273 icmph
->type
= ICMP_ECHO
;
278 static void __assemble_icmp6(uint8_t *packet
, size_t len
)
280 struct icmp6hdr
*icmp6h
= (struct icmp6hdr
*) packet
;
282 bug_on(len
< sizeof(struct icmp6hdr
));
284 icmp6h
->icmp6_type
= ICMPV6_ECHO_REQUEST
;
285 icmp6h
->icmp6_code
= 0;
286 icmp6h
->icmp6_cksum
= 0;
289 static void __assemble_tcp(uint8_t *packet
, size_t len
, int syn
, int ack
,
290 int urg
, int fin
, int rst
, int psh
, int ecn
,
293 struct tcphdr
*tcph
= (struct tcphdr
*) packet
;
295 bug_on(len
< sizeof(struct tcphdr
));
297 tcph
->source
= htons((uint16_t) rand());
298 tcph
->dest
= htons((uint16_t) dport
);
300 tcph
->seq
= htonl(rand());
301 tcph
->ack_seq
= (!!ack
? htonl(rand()) : 0);
314 tcph
->window
= htons((uint16_t) (100 + (rand() % 65435)));
315 tcph
->urg_ptr
= (!!urg
? htons((uint16_t) rand()) : 0);
319 static int assemble_ipv4(uint8_t *packet
, size_t len
, int ttl
, int proto
,
320 const struct ctx
*ctx
, const struct sockaddr
*dst
,
321 const struct sockaddr
*src
)
324 size_t data_len
, off_next
= 0;
325 struct iphdr
*iph
= (struct iphdr
*) packet
;
327 bug_on(!src
|| !dst
);
328 bug_on(src
->sa_family
!= PF_INET
|| dst
->sa_family
!= PF_INET
);
329 bug_on(len
< sizeof(*iph
) + min(sizeof(struct tcphdr
),
330 sizeof(struct icmphdr
)));
334 iph
->tos
= (uint8_t) ctx
->tos
;
336 iph
->tot_len
= htons((uint16_t) len
);
337 iph
->id
= htons((uint16_t) rand());
339 iph
->frag_off
= ctx
->nofrag
? IP_DF
: 0;
340 iph
->ttl
= (uint8_t) ttl
;
342 iph
->saddr
= ((const struct sockaddr_in
*) src
)->sin_addr
.s_addr
;
343 iph
->daddr
= ((const struct sockaddr_in
*) dst
)->sin_addr
.s_addr
;
345 iph
->protocol
= (uint8_t) proto
;
347 data
= packet
+ sizeof(*iph
);
348 data_len
= len
- sizeof(*iph
);
352 __assemble_tcp(data
, data_len
, ctx
->syn
, ctx
->ack
, ctx
->urg
,
353 ctx
->fin
, ctx
->rst
, ctx
->psh
, ctx
->ecn
, ctx
->dport
);
354 off_next
= sizeof(struct tcphdr
);
357 __assemble_icmp4(data
, data_len
);
358 off_next
= sizeof(struct icmphdr
);
364 data
= packet
+ sizeof(*iph
) + off_next
;
365 data_len
= len
- sizeof(*iph
) - off_next
;
367 __assemble_data(data
, data_len
, ctx
->payload
);
369 iph
->check
= csum((unsigned short *) packet
, ntohs(iph
->tot_len
) >> 1);
371 return ntohs(iph
->id
);
374 static int assemble_ipv6(uint8_t *packet
, size_t len
, int ttl
, int proto
,
375 const struct ctx
*ctx
, const struct sockaddr
*dst
,
376 const struct sockaddr
*src
)
379 size_t data_len
, off_next
= 0;
380 struct ip6_hdr
*ip6h
= (struct ip6_hdr
*) packet
;
382 bug_on(!src
|| !dst
);
383 bug_on(src
->sa_family
!= PF_INET6
|| dst
->sa_family
!= PF_INET6
);
384 bug_on(len
< sizeof(*ip6h
) + min(sizeof(struct tcphdr
),
385 sizeof(struct icmp6hdr
)));
387 ip6h
->ip6_flow
= htonl(rand() & 0x000fffff);
388 ip6h
->ip6_vfc
= 0x60;
390 ip6h
->ip6_plen
= htons((uint16_t) len
- sizeof(*ip6h
));
391 ip6h
->ip6_nxt
= (uint8_t) proto
;
392 ip6h
->ip6_hlim
= (uint8_t) ttl
;
394 memcpy(&ip6h
->ip6_src
, &(((const struct sockaddr_in6
*)
395 src
)->sin6_addr
), sizeof(ip6h
->ip6_src
));
396 memcpy(&ip6h
->ip6_dst
, &(((const struct sockaddr_in6
*)
397 dst
)->sin6_addr
), sizeof(ip6h
->ip6_dst
));
399 data
= packet
+ sizeof(*ip6h
);
400 data_len
= len
- sizeof(*ip6h
);
404 __assemble_tcp(data
, data_len
, ctx
->syn
, ctx
->ack
, ctx
->urg
,
405 ctx
->fin
, ctx
->rst
, ctx
->psh
, ctx
->ecn
, ctx
->dport
);
406 off_next
= sizeof(struct tcphdr
);
410 __assemble_icmp6(data
, data_len
);
411 off_next
= sizeof(struct icmp6hdr
);
417 data
= packet
+ sizeof(*ip6h
) + off_next
;
418 data_len
= len
- sizeof(*ip6h
) - off_next
;
420 __assemble_data(data
, data_len
, ctx
->payload
);
422 return ntohl(ip6h
->ip6_flow
) & 0x000fffff;
425 static int check_ipv4(uint8_t *packet
, size_t len
, int ttl __maybe_unused
,
426 int id
, const struct sockaddr
*ss
)
428 struct iphdr
*iph
= (struct iphdr
*) packet
;
429 struct iphdr
*iph_inner
;
430 struct icmphdr
*icmph
;
432 if (iph
->protocol
!= IPPROTO_ICMP
)
434 if (iph
->daddr
!= ((const struct sockaddr_in
*) ss
)->sin_addr
.s_addr
)
437 icmph
= (struct icmphdr
*) (packet
+ sizeof(struct iphdr
));
438 if (icmph
->type
!= ICMP_TIME_EXCEEDED
)
440 if (icmph
->code
!= ICMP_EXC_TTL
)
443 iph_inner
= (struct iphdr
*) (packet
+ sizeof(struct iphdr
) +
444 sizeof(struct icmphdr
));
445 if (ntohs(iph_inner
->id
) != id
)
451 static void handle_ipv4(uint8_t *packet
, size_t len __maybe_unused
,
452 int dns_resolv
, int latitude
)
454 char hbuff
[NI_MAXHOST
];
455 struct iphdr
*iph
= (struct iphdr
*) packet
;
456 struct sockaddr_in sd
;
457 struct hostent
*hent
;
458 const char *as
, *country
, *city
;
460 memset(hbuff
, 0, sizeof(hbuff
));
461 memset(&sd
, 0, sizeof(sd
));
462 sd
.sin_family
= PF_INET
;
463 sd
.sin_addr
.s_addr
= iph
->saddr
;
465 getnameinfo((struct sockaddr
*) &sd
, sizeof(sd
),
466 hbuff
, sizeof(hbuff
), NULL
, 0, NI_NUMERICHOST
);
468 as
= geoip4_as_name(sd
);
469 country
= geoip4_country_name(sd
);
470 city
= geoip4_city_name(sd
);
473 hent
= gethostbyaddr(&sd
.sin_addr
, sizeof(sd
.sin_addr
), PF_INET
);
475 printf(" %s (%s)", hent
->h_name
, hbuff
);
477 printf(" %s", hbuff
);
479 printf(" %s", hbuff
);
482 printf(" in %s", as
);
484 printf(" in %s", country
);
486 printf(", %s", city
);
489 printf(" (%f/%f)", geoip4_latitude(sd
), geoip4_longitude(sd
));
492 static int check_ipv6(uint8_t *packet
, size_t len
, int ttl __maybe_unused
,
493 int id
, const struct sockaddr
*ss
)
495 struct ip6_hdr
*ip6h
= (struct ip6_hdr
*) packet
;
496 struct ip6_hdr
*ip6h_inner
;
497 struct icmp6hdr
*icmp6h
;
499 if (ip6h
->ip6_nxt
!= 0x3a)
501 if (memcmp(&ip6h
->ip6_dst
, &(((const struct sockaddr_in6
*)
502 ss
)->sin6_addr
), sizeof(ip6h
->ip6_dst
)))
505 icmp6h
= (struct icmp6hdr
*) (packet
+ sizeof(*ip6h
));
506 if (icmp6h
->icmp6_type
!= ICMPV6_TIME_EXCEED
)
508 if (icmp6h
->icmp6_code
!= ICMPV6_EXC_HOPLIMIT
)
511 ip6h_inner
= (struct ip6_hdr
*) (packet
+ sizeof(*ip6h
) + sizeof(*icmp6h
));
512 if ((ntohl(ip6h_inner
->ip6_flow
) & 0x000fffff) != (uint32_t) id
)
518 static void handle_ipv6(uint8_t *packet
, size_t len __maybe_unused
,
519 int dns_resolv
, int latitude
)
521 char hbuff
[NI_MAXHOST
];
522 struct ip6_hdr
*ip6h
= (struct ip6_hdr
*) packet
;
523 struct sockaddr_in6 sd
;
524 struct hostent
*hent
;
525 const char *as
, *country
, *city
;
527 memset(hbuff
, 0, sizeof(hbuff
));
528 memset(&sd
, 0, sizeof(sd
));
529 sd
.sin6_family
= PF_INET6
;
530 memcpy(&sd
.sin6_addr
, &ip6h
->ip6_src
, sizeof(ip6h
->ip6_src
));
532 getnameinfo((struct sockaddr
*) &sd
, sizeof(sd
),
533 hbuff
, sizeof(hbuff
), NULL
, 0, NI_NUMERICHOST
);
535 as
= geoip6_as_name(sd
);
536 country
= geoip6_country_name(sd
);
537 city
= geoip6_city_name(sd
);
540 hent
= gethostbyaddr(&sd
.sin6_addr
, sizeof(sd
.sin6_addr
), PF_INET6
);
542 printf(" %s (%s)", hent
->h_name
, hbuff
);
544 printf(" %s", hbuff
);
546 printf(" %s", hbuff
);
549 printf(" in %s", as
);
551 printf(" in %s", country
);
553 printf(", %s", city
);
556 printf(" (%f/%f)", geoip6_latitude(sd
), geoip6_longitude(sd
));
559 static void show_trace_info(struct ctx
*ctx
, const struct sockaddr_storage
*ss
,
560 const struct sockaddr_storage
*sd
)
562 char hbuffs
[256], hbuffd
[256];
564 memset(hbuffd
, 0, sizeof(hbuffd
));
565 getnameinfo((struct sockaddr
*) sd
, sizeof(*sd
),
566 hbuffd
, sizeof(hbuffd
), NULL
, 0, NI_NUMERICHOST
);
568 memset(hbuffs
, 0, sizeof(hbuffs
));
569 getnameinfo((struct sockaddr
*) ss
, sizeof(*ss
),
570 hbuffs
, sizeof(hbuffs
), NULL
, 0, NI_NUMERICHOST
);
572 printf("AS path IPv%d TCP trace from %s to %s:%s (%s) with len %zu "
573 "Bytes, %u max hops\n", ctx
->proto
== IPPROTO_IP
? 4 : 6,
574 hbuffs
, hbuffd
, ctx
->port
, ctx
->host
, ctx
->totlen
, ctx
->max_ttl
);
576 printf("Using flags SYN:%d,ACK:%d,ECN:%d,FIN:%d,PSH:%d,RST:%d,URG:%d\n",
577 ctx
->syn
, ctx
->ack
, ctx
->ecn
, ctx
->fin
, ctx
->psh
, ctx
->rst
, ctx
->urg
);
580 printf("With payload: \'%s\'\n", ctx
->payload
);
583 static int get_remote_fd(struct ctx
*ctx
, struct sockaddr_storage
*ss
,
584 struct sockaddr_storage
*sd
)
586 int fd
= -1, ret
, one
= 1;
587 struct addrinfo hints
, *ahead
, *ai
;
589 memset(&hints
, 0, sizeof(hints
));
590 hints
.ai_family
= PF_UNSPEC
;
591 hints
.ai_socktype
= SOCK_STREAM
;
592 hints
.ai_protocol
= IPPROTO_TCP
;
593 hints
.ai_flags
= AI_NUMERICSERV
;
595 ret
= getaddrinfo(ctx
->host
, ctx
->port
, &hints
, &ahead
);
597 panic("Cannot get address info!\n");
599 for (ai
= ahead
; ai
!= NULL
&& fd
< 0; ai
= ai
->ai_next
) {
600 if (!((ai
->ai_family
== PF_INET6
&& ctx
->proto
== IPPROTO_IPV6
) ||
601 (ai
->ai_family
== PF_INET
&& ctx
->proto
== IPPROTO_IP
)))
604 fd
= socket(ai
->ai_family
, SOCK_RAW
, IPPROTO_RAW
);
608 memset(ss
, 0, sizeof(*ss
));
609 ret
= device_address(ctx
->dev
, ai
->ai_family
, ss
);
611 panic("Cannot get own device address!\n");
613 ret
= bind(fd
, (struct sockaddr
*) ss
, sizeof(*ss
));
615 panic("Cannot bind socket!\n");
617 memset(sd
, 0, sizeof(*sd
));
618 memcpy(sd
, ai
->ai_addr
, ai
->ai_addrlen
);
620 ctx
->sd_len
= ai
->ai_addrlen
;
621 ctx
->dport
= strtoul(ctx
->port
, NULL
, 10);
623 ret
= setsockopt(fd
, ctx
->proto
, IP_HDRINCL
, &one
, sizeof(one
));
625 panic("Kernel does not support IP_HDRINCL!\n");
627 if (ai
->ai_family
== PF_INET6
) {
628 struct sockaddr_in6
*sd6
= (struct sockaddr_in6
*) sd
;
639 panic("Cannot create socket! Does remote "
641 ctx
->proto
== IPPROTO_IP
? 4 : 6);
646 static void inject_filter(struct ctx
*ctx
, int fd
)
648 struct sock_fprog bpf_ops
;
650 enable_kernel_bpf_jit_compiler();
652 memset(&bpf_ops
, 0, sizeof(bpf_ops
));
653 bpf_ops
.filter
= (struct sock_filter
*) af_ops
[ctx
->proto
].filter
;
654 bpf_ops
.len
= af_ops
[ctx
->proto
].flen
;
656 bpf_attach_to_sock(fd
, &bpf_ops
);
659 static int __process_node(struct ctx
*ctx
, int fd
, int fd_cap
, int ttl
,
660 int inner_proto
, uint8_t *pkt_snd
, uint8_t *pkt_rcv
,
661 const struct sockaddr_storage
*ss
,
662 const struct sockaddr_storage
*sd
, struct timeval
*diff
)
664 int pkt_id
, ret
, timeout
;
666 struct timeval start
, end
;
668 prepare_polling(fd_cap
, &pfd
);
670 memset(pkt_snd
, 0, ctx
->totlen
);
671 pkt_id
= af_ops
[ctx
->proto
].assembler(pkt_snd
, ctx
->totlen
, ttl
,
673 (const struct sockaddr
*) sd
,
674 (const struct sockaddr
*) ss
);
676 ret
= sendto(fd
, pkt_snd
, ctx
->totlen
, 0, (struct sockaddr
*) sd
,
679 panic("sendto failed: %s\n", strerror(errno
));
681 bug_on(gettimeofday(&start
, NULL
));
683 timeout
= (ctx
->timeout
> 0 ? ctx
->timeout
: 2) * 1000;
685 ret
= poll(&pfd
, 1, timeout
);
686 if (ret
> 0 && pfd
.revents
& POLLIN
&& sigint
== 0) {
687 bug_on(gettimeofday(&end
, NULL
));
689 timersub(&end
, &start
, diff
);
691 ret
= recvfrom(fd_cap
, pkt_rcv
, ctx
->rcvlen
, 0, NULL
, NULL
);
692 if (ret
< (int) (sizeof(struct ethhdr
) + af_ops
[ctx
->proto
].min_len_icmp
))
695 return af_ops
[ctx
->proto
].check(pkt_rcv
+ sizeof(struct ethhdr
),
696 ret
- sizeof(struct ethhdr
), ttl
,
697 pkt_id
, (const struct sockaddr
*) ss
);
705 static void timerdiv(const unsigned long divisor
, const struct timeval
*tv
,
706 struct timeval
*result
)
708 uint64_t x
= ((uint64_t) tv
->tv_sec
* 1000 * 1000 + tv
->tv_usec
) / divisor
;
710 result
->tv_sec
= x
/ 1000 / 1000;
711 result
->tv_usec
= x
% (1000 * 1000);
714 static int timevalcmp(const void *t1
, const void *t2
)
716 if (timercmp((struct timeval
*) t1
, (struct timeval
*) t2
, <))
718 if (timercmp((struct timeval
*) t1
, (struct timeval
*) t2
, >))
724 static int __process_time(struct ctx
*ctx
, int fd
, int fd_cap
, int ttl
,
725 int inner_proto
, uint8_t *pkt_snd
, uint8_t *pkt_rcv
,
726 const struct sockaddr_storage
*ss
,
727 const struct sockaddr_storage
*sd
)
730 int good
= 0, ret
= -EIO
, idx
, ret_good
= -EIO
;
731 struct timeval probes
[9], *tmp
, sum
, res
;
732 uint8_t *trash
= xmalloc(ctx
->rcvlen
);
733 char *cwait
[] = { "-", "\\", "|", "/" };
734 const char *proto_short
[] = {
736 [IPPROTO_ICMP
] = "i",
737 [IPPROTO_ICMPV6
] = "i",
740 memset(probes
, 0, sizeof(probes
));
741 for (i
= 0; i
< array_size(probes
) && sigint
== 0; ++i
) {
742 ret
= __process_node(ctx
, fd
, fd_cap
, ttl
, inner_proto
,
743 pkt_snd
, good
== 0 ? pkt_rcv
: trash
,
751 if (good
== 0 && ctx
->queries
== (int) i
)
756 printf("\r%2d: %s", ttl
, cwait
[j
++]);
758 if (j
>= array_size(cwait
))
767 tmp
= xmalloc(sizeof(struct timeval
) * good
);
768 for (i
= j
= 0; i
< array_size(probes
); ++i
) {
769 if (probes
[i
].tv_sec
== 0 && probes
[i
].tv_usec
== 0)
771 tmp
[j
].tv_sec
= probes
[i
].tv_sec
;
772 tmp
[j
].tv_usec
= probes
[i
].tv_usec
;
776 qsort(tmp
, j
, sizeof(struct timeval
), timevalcmp
);
778 printf("\r%2d: %s[", ttl
, proto_short
[inner_proto
]);
782 timeradd(&tmp
[idx
], &tmp
[idx
- 1], &sum
);
783 timerdiv(2, &sum
, &res
);
785 printf("%lu sec ", res
.tv_sec
);
786 printf("%7lu us", res
.tv_usec
);
789 if (tmp
[idx
].tv_sec
> 0)
790 printf("%lu sec ", tmp
[idx
].tv_sec
);
791 printf("%7lu us", tmp
[idx
].tv_usec
);
802 static int __probe_remote(struct ctx
*ctx
, int fd
, int fd_cap
, int ttl
,
803 uint8_t *pkt_snd
, uint8_t *pkt_rcv
,
804 const struct sockaddr_storage
*ss
,
805 const struct sockaddr_storage
*sd
,
808 int ret
= -EIO
, tries
= ctx
->queries
;
810 while (tries
-- > 0 && sigint
== 0) {
811 ret
= __process_time(ctx
, fd
, fd_cap
, ttl
, inner_proto
,
812 pkt_snd
, pkt_rcv
, ss
, sd
);
816 af_ops
[ctx
->proto
].handler(pkt_rcv
+ sizeof(struct ethhdr
),
817 ret
- sizeof(struct ethhdr
),
818 ctx
->dns_resolv
, ctx
->latitude
);
820 struct pkt_buff
*pkt
;
823 pkt
= pkt_alloc(pkt_rcv
, ret
);
835 static int __process_ttl(struct ctx
*ctx
, int fd
, int fd_cap
, int ttl
,
836 uint8_t *pkt_snd
, uint8_t *pkt_rcv
,
837 const struct sockaddr_storage
*ss
,
838 const struct sockaddr_storage
*sd
)
842 const int inner_protos
[] = {
847 printf("%2d: ", ttl
);
850 for (i
= 0; i
< array_size(inner_protos
) && sigint
== 0; ++i
) {
851 ret
= __probe_remote(ctx
, fd
, fd_cap
, ttl
, pkt_snd
, pkt_rcv
, ss
, sd
,
858 printf("\r%2d: ?[ no answer]", ttl
);
861 if (ctx
->show
&& ret
<= 0)
868 static int main_trace(struct ctx
*ctx
)
870 int fd
, fd_cap
, ifindex
, ttl
;
871 struct ring dummy_ring
;
872 struct sockaddr_storage ss
, sd
;
873 uint8_t *pkt_snd
, *pkt_rcv
;
875 fd
= get_remote_fd(ctx
, &ss
, &sd
);
876 fd_cap
= pf_socket();
878 inject_filter(ctx
, fd_cap
);
880 ifindex
= device_ifindex(ctx
->dev
);
881 bind_rx_ring(fd_cap
, &dummy_ring
, ifindex
);
883 if (ctx
->totlen
< af_ops
[ctx
->proto
].min_len_tcp
) {
884 ctx
->totlen
= af_ops
[ctx
->proto
].min_len_tcp
;
886 ctx
->totlen
+= strlen(ctx
->payload
);
889 ctx
->rcvlen
= device_mtu(ctx
->dev
) - sizeof(struct ethhdr
);
890 if (ctx
->totlen
>= ctx
->rcvlen
)
891 panic("Packet len exceeds device MTU!\n");
893 pkt_snd
= xmalloc(ctx
->totlen
);
894 pkt_rcv
= xmalloc(ctx
->rcvlen
);
896 show_trace_info(ctx
, &ss
, &sd
);
898 for (ttl
= ctx
->init_ttl
; ttl
<= ctx
->max_ttl
&& sigint
== 0; ++ttl
)
899 __process_ttl(ctx
, fd
, fd_cap
, ttl
, pkt_snd
, pkt_rcv
,
911 int main(int argc
, char **argv
)
913 int c
, opt_index
, ret
;
921 memset(&ctx
, 0, sizeof(ctx
));
926 ctx
.proto
= IPPROTO_IP
;
928 ctx
.dev
= xstrdup("eth0");
929 ctx
.port
= xstrdup("80");
931 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
932 &opt_index
)) != EOF
) {
945 ctx
.host
= xstrdup(optarg
);
950 ctx
.port
= xstrdup(optarg
);
956 ctx
.proto
= IPPROTO_IP
;
959 ctx
.proto
= IPPROTO_IPV6
;
968 ctx
.init_ttl
= atoi(optarg
);
969 if (ctx
.init_ttl
<= 0)
973 ctx
.max_ttl
= atoi(optarg
);
974 if (ctx
.max_ttl
<= 0)
980 ctx
.dev
= xstrdup(optarg
);
983 ctx
.queries
= atoi(optarg
);
984 if (ctx
.queries
<= 0)
988 ctx
.timeout
= atoi(optarg
);
989 if (ctx
.timeout
<= 0)
1018 ctx
.tos
= atoi(optarg
);
1026 ctx
.payload
= xstrdup(optarg
);
1029 ctx
.totlen
= strtoul(optarg
, NULL
, 10);
1030 if (ctx
.totlen
== 0)
1046 panic("Option -%c requires an argument!\n",
1049 if (isprint(optopt
))
1050 printf("Unknown option character `0x%X\'!\n", optopt
);
1058 if (argc
< 3 || !ctx
.host
|| !ctx
.port
|| ctx
.init_ttl
> ctx
.max_ttl
||
1059 ctx
.init_ttl
> MAXTTL
|| ctx
.max_ttl
> MAXTTL
)
1062 if (!device_up_and_running(ctx
.dev
))
1063 panic("Networking device not up and running!\n");
1064 if (device_mtu(ctx
.dev
) <= ctx
.totlen
)
1065 panic("Packet larger than device MTU!\n");
1067 register_signal(SIGHUP
, signal_handler
);
1068 register_signal(SIGINT
, signal_handler
);
1073 ret
= main_trace(&ctx
);