2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 - 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
6 * The road must be trod, but it will be very hard. And neither strength nor
7 * wisdom will carry us far upon it. This quest may be attempted by the weak
8 * with as much hope as the strong. Yet such is oft the course of deeds that
9 * move the wheels of the world: small hands do them because they must,
10 * while the eyes of the great are elsewhere.
12 * -- The Lord of the Rings, Elrond, Chapter 'The Council of Elrond'.
22 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/fsuid.h>
29 #include <asm/byteorder.h>
30 #include <linux/tcp.h>
31 #include <netinet/ip.h>
32 #include <netinet/ip6.h>
33 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <linux/if_ether.h>
39 #include <linux/icmp.h>
40 #include <linux/icmpv6.h>
56 char *host
, *port
, *dev
, *payload
;
57 int init_ttl
, max_ttl
, dns_resolv
, queries
, timeout
, totlen
, rcvlen
;
58 int syn
, ack
, ecn
, fin
, psh
, rst
, urg
, tos
, nofrag
, proto
, show
;
59 int sd_len
, dport
, latitude
;
63 int (*assembler
)(uint8_t *packet
, size_t len
, int ttl
, int proto
,
64 const struct ctx
*ctx
, const struct sockaddr
*dst
,
65 const struct sockaddr
*src
);
66 const struct sock_filter
*filter
;
68 unsigned int min_len_tcp
, min_len_icmp
;
69 int (*check
)(uint8_t *packet
, size_t len
, int ttl
, int id
,
70 const struct sockaddr
*src
);
71 void (*handler
)(uint8_t *packet
, size_t len
, int dns_resolv
,
75 sig_atomic_t sigint
= 0;
77 static int assemble_ipv4(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 assemble_ipv6(uint8_t *packet
, size_t len
, int ttl
, int proto
,
81 const struct ctx
*ctx
, const struct sockaddr
*dst
,
82 const struct sockaddr
*src
);
83 static int check_ipv4(uint8_t *packet
, size_t len
, int ttl
, int id
,
84 const struct sockaddr
*ss
);
85 static void handle_ipv4(uint8_t *packet
, size_t len
, int dns_resolv
,
87 static int check_ipv6(uint8_t *packet
, size_t len
, int ttl
, int id
,
88 const struct sockaddr
*ss
);
89 static void handle_ipv6(uint8_t *packet
, size_t len
, int dns_resolv
,
92 static const char *short_options
= "H:p:nNf:m:i:d:q:x:SAEFPURt:Gl:hv46X:ZuL";
93 static const struct option long_options
[] = {
94 {"host", required_argument
, NULL
, 'H'},
95 {"port", required_argument
, NULL
, 'p'},
96 {"init-ttl", required_argument
, NULL
, 'f'},
97 {"max-ttl", required_argument
, NULL
, 'm'},
98 {"dev", required_argument
, NULL
, 'd'},
99 {"num-probes", required_argument
, NULL
, 'q'},
100 {"timeout", required_argument
, NULL
, 'x'},
101 {"tos", required_argument
, NULL
, 't'},
102 {"payload", required_argument
, NULL
, 'X'},
103 {"totlen", required_argument
, NULL
, 'l'},
104 {"numeric", no_argument
, NULL
, 'n'},
105 {"latitude", no_argument
, NULL
, 'L'},
106 {"update", no_argument
, NULL
, 'u'},
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 const struct sock_filter ipv4_icmp_type_11
[] = {
125 { 0x28, 0, 0, 0x0000000c }, /* ldh [12] */
126 { 0x15, 0, 8, 0x00000800 }, /* jneq #0x800, drop */
127 { 0x30, 0, 0, 0x00000017 }, /* ldb [23] */
128 { 0x15, 0, 6, 0x00000001 }, /* jneq #0x1, drop */
129 { 0x28, 0, 0, 0x00000014 }, /* ldh [20] */
130 { 0x45, 4, 0, 0x00001fff }, /* jset #0x1fff, drop */
131 { 0xb1, 0, 0, 0x0000000e }, /* ldxb 4*([14]&0xf) */
132 { 0x50, 0, 0, 0x0000000e }, /* ldb [x + 14] */
133 { 0x15, 0, 1, 0x0000000b }, /* jneq #0xb, drop */
134 { 0x06, 0, 0, 0xffffffff }, /* ret #-1 */
135 { 0x06, 0, 0, 0x00000000 }, /* drop: ret #0 */
138 static const struct sock_filter ipv6_icmp6_type_3
[] = {
139 { 0x28, 0, 0, 0x0000000c }, /* ldh [12] */
140 { 0x15, 0, 5, 0x000086dd }, /* jneq #0x86dd, drop */
141 { 0x30, 0, 0, 0x00000014 }, /* ldb [20] */
142 { 0x15, 0, 3, 0x0000003a }, /* jneq #0x3a, drop */
143 { 0x30, 0, 0, 0x00000036 }, /* ldb [54] */
144 { 0x15, 0, 1, 0x00000003 }, /* jneq #0x3, drop */
145 { 0x06, 0, 0, 0xffffffff }, /* ret #-1 */
146 { 0x06, 0, 0, 0x00000000 }, /* drop: ret #0 */
149 static const struct proto_ops af_ops
[] = {
151 .assembler
= assemble_ipv4
,
152 .handler
= handle_ipv4
,
154 .filter
= ipv4_icmp_type_11
,
155 .flen
= array_size(ipv4_icmp_type_11
),
156 .min_len_tcp
= sizeof(struct iphdr
) + sizeof(struct tcphdr
),
157 .min_len_icmp
= sizeof(struct iphdr
) + sizeof(struct icmphdr
),
160 .assembler
= assemble_ipv6
,
161 .handler
= handle_ipv6
,
163 .filter
= ipv6_icmp6_type_3
,
164 .flen
= array_size(ipv6_icmp6_type_3
),
165 .min_len_tcp
= sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
),
166 .min_len_icmp
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6hdr
),
170 static void signal_handler(int number
)
180 static void help(void)
182 printf("\nastraceroute %s, autonomous system trace route utility\n", VERSION_STRING
);
183 puts("http://www.netsniff-ng.org\n\n"
184 "Usage: astraceroute [options]\n"
186 " -H|--host <host> Host/IPv4/IPv6 to lookup AS route to\n"
187 " -p|--port <port> Hosts port to lookup AS route to\n"
188 " -i|-d|--dev <device> Networking device, e.g. eth0\n"
189 " -f|--init-ttl <ttl> Set initial TTL\n"
190 " -m|--max-ttl <ttl> Set maximum TTL (def: 30)\n"
191 " -q|--num-probes <num> Number of max probes for each hop (def: 2)\n"
192 " -x|--timeout <sec> Probe response timeout in sec (def: 3)\n"
193 " -X|--payload <string> Specify a payload string to test DPIs\n"
194 " -l|--totlen <len> Specify total packet len\n"
195 " -4|--ipv4 Use IPv4-only requests\n"
196 " -6|--ipv6 Use IPv6-only requests\n"
197 " -n|--numeric Do not do reverse DNS lookup for hops\n"
198 " -u|--update Update GeoIP databases\n"
199 " -L|--latitude Show latitude and longtitude\n"
200 " -N|--dns Do a reverse DNS lookup for hops\n"
201 " -S|--syn Set TCP SYN flag\n"
202 " -A|--ack Set TCP ACK flag\n"
203 " -F|--fin Set TCP FIN flag\n"
204 " -P|--psh Set TCP PSH flag\n"
205 " -U|--urg Set TCP URG flag\n"
206 " -R|--rst Set TCP RST flag\n"
207 " -E|--ecn-syn Send ECN SYN packets (RFC3168)\n"
208 " -t|--tos <tos> Set the IP TOS field\n"
209 " -G|--nofrag Set do not fragment bit\n"
210 " -Z|--show-packet Show returned packet on each hop\n"
211 " -v|--version Print version\n"
212 " -h|--help Print this help\n\n"
214 " IPv4 trace of AS with TCP SYN probe (this will most-likely pass):\n"
215 " astraceroute -i eth0 -N -S -H netsniff-ng.org\n"
216 " IPv4 trace of AS with TCP ECN SYN probe:\n"
217 " astraceroute -i eth0 -N -E -H netsniff-ng.org\n"
218 " IPv4 trace of AS with TCP FIN probe:\n"
219 " astraceroute -i eth0 -N -F -H netsniff-ng.org\n"
220 " IPv4 trace of AS with Xmas probe:\n"
221 " astraceroute -i eth0 -N -FPU -H netsniff-ng.org\n"
222 " IPv4 trace of AS with Null probe with ASCII payload:\n"
223 " astraceroute -i eth0 -N -H netsniff-ng.org -X \"censor-me\" -Z\n"
224 " IPv6 trace of AS up to www.6bone.net:\n"
225 " astraceroute -6 -i eth0 -S -E -N -H www.6bone.net\n\n"
227 " If the TCP probe did not give any results, then astraceroute will\n"
228 " automatically probe for classic ICMP packets! To gather more\n"
229 " information about astraceroute's fetched AS numbers, see e.g.\n"
230 " http://bgp.he.net/AS<number>!\n\n"
231 "Please report bugs to <bugs@netsniff-ng.org>\n"
232 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
233 "Swiss federal institute of technology (ETH Zurich)\n"
234 "License: GNU GPL version 2.0\n"
235 "This is free software: you are free to change and redistribute it.\n"
236 "There is NO WARRANTY, to the extent permitted by law.\n");
240 static void version(void)
242 printf("\nastraceroute %s, autonomous system trace route utility\n", VERSION_STRING
);
243 puts("http://www.netsniff-ng.org\n\n"
244 "Please report bugs to <bugs@netsniff-ng.org>\n"
245 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
246 "Swiss federal institute of technology (ETH Zurich)\n"
247 "License: GNU GPL version 2.0\n"
248 "This is free software: you are free to change and redistribute it.\n"
249 "There is NO WARRANTY, to the extent permitted by law.\n");
253 static void __assemble_data(uint8_t *packet
, size_t len
, const char *payload
)
257 if (payload
== NULL
) {
258 for (i
= 0; i
< len
; ++i
)
259 packet
[i
] = (uint8_t) rand();
261 int lmin
= min(len
, strlen(payload
));
263 for (i
= 0; i
< lmin
; ++i
)
264 packet
[i
] = (uint8_t) payload
[i
];
265 for (i
= lmin
; i
< len
; ++i
)
266 packet
[i
] = (uint8_t) rand();
270 static void __assemble_icmp4(uint8_t *packet
, size_t len
)
272 struct icmphdr
*icmph
= (struct icmphdr
*) packet
;
274 bug_on(len
< sizeof(struct icmphdr
));
276 icmph
->type
= ICMP_ECHO
;
281 static void __assemble_icmp6(uint8_t *packet
, size_t len
)
283 struct icmp6hdr
*icmp6h
= (struct icmp6hdr
*) packet
;
285 bug_on(len
< sizeof(struct icmp6hdr
));
287 icmp6h
->icmp6_type
= ICMPV6_ECHO_REQUEST
;
288 icmp6h
->icmp6_code
= 0;
289 icmp6h
->icmp6_cksum
= 0;
292 static void __assemble_tcp(uint8_t *packet
, size_t len
, int syn
, int ack
,
293 int urg
, int fin
, int rst
, int psh
, int ecn
,
296 struct tcphdr
*tcph
= (struct tcphdr
*) packet
;
298 bug_on(len
< sizeof(struct tcphdr
));
300 tcph
->source
= htons((uint16_t) rand());
301 tcph
->dest
= htons((uint16_t) dport
);
303 tcph
->seq
= htonl(rand());
304 tcph
->ack_seq
= (!!ack
? htonl(rand()) : 0);
317 tcph
->window
= htons((uint16_t) (100 + (rand() % 65435)));
318 tcph
->urg_ptr
= (!!urg
? htons((uint16_t) rand()) : 0);
322 static int assemble_ipv4(uint8_t *packet
, size_t len
, int ttl
, int proto
,
323 const struct ctx
*ctx
, const struct sockaddr
*dst
,
324 const struct sockaddr
*src
)
327 size_t data_len
, off_next
= 0;
328 struct iphdr
*iph
= (struct iphdr
*) packet
;
330 bug_on(!src
|| !dst
);
331 bug_on(src
->sa_family
!= PF_INET
|| dst
->sa_family
!= PF_INET
);
332 bug_on(len
< sizeof(*iph
) + min(sizeof(struct tcphdr
),
333 sizeof(struct icmphdr
)));
337 iph
->tos
= (uint8_t) ctx
->tos
;
339 iph
->tot_len
= htons((uint16_t) len
);
340 iph
->id
= htons((uint16_t) rand());
342 iph
->frag_off
= ctx
->nofrag
? IP_DF
: 0;
343 iph
->ttl
= (uint8_t) ttl
;
345 iph
->saddr
= ((const struct sockaddr_in
*) src
)->sin_addr
.s_addr
;
346 iph
->daddr
= ((const struct sockaddr_in
*) dst
)->sin_addr
.s_addr
;
348 iph
->protocol
= (uint8_t) proto
;
350 data
= packet
+ sizeof(*iph
);
351 data_len
= len
- sizeof(*iph
);
355 __assemble_tcp(data
, data_len
, ctx
->syn
, ctx
->ack
, ctx
->urg
,
356 ctx
->fin
, ctx
->rst
, ctx
->psh
, ctx
->ecn
, ctx
->dport
);
357 off_next
= sizeof(struct tcphdr
);
360 __assemble_icmp4(data
, data_len
);
361 off_next
= sizeof(struct icmphdr
);
367 data
= packet
+ sizeof(*iph
) + off_next
;
368 data_len
= len
- sizeof(*iph
) - off_next
;
370 __assemble_data(data
, data_len
, ctx
->payload
);
372 iph
->check
= csum((unsigned short *) packet
, ntohs(iph
->tot_len
) >> 1);
374 return ntohs(iph
->id
);
377 static int assemble_ipv6(uint8_t *packet
, size_t len
, int ttl
, int proto
,
378 const struct ctx
*ctx
, const struct sockaddr
*dst
,
379 const struct sockaddr
*src
)
382 size_t data_len
, off_next
= 0;
383 struct ip6_hdr
*ip6h
= (struct ip6_hdr
*) packet
;
385 bug_on(!src
|| !dst
);
386 bug_on(src
->sa_family
!= PF_INET6
|| dst
->sa_family
!= PF_INET6
);
387 bug_on(len
< sizeof(*ip6h
) + min(sizeof(struct tcphdr
),
388 sizeof(struct icmp6hdr
)));
390 ip6h
->ip6_flow
= htonl(rand() & 0x000fffff);
391 ip6h
->ip6_vfc
= 0x60;
393 ip6h
->ip6_plen
= htons((uint16_t) len
- sizeof(*ip6h
));
394 ip6h
->ip6_nxt
= (uint8_t) proto
;
395 ip6h
->ip6_hlim
= (uint8_t) ttl
;
397 memcpy(&ip6h
->ip6_src
, &(((const struct sockaddr_in6
*)
398 src
)->sin6_addr
), sizeof(ip6h
->ip6_src
));
399 memcpy(&ip6h
->ip6_dst
, &(((const struct sockaddr_in6
*)
400 dst
)->sin6_addr
), sizeof(ip6h
->ip6_dst
));
402 data
= packet
+ sizeof(*ip6h
);
403 data_len
= len
- sizeof(*ip6h
);
407 __assemble_tcp(data
, data_len
, ctx
->syn
, ctx
->ack
, ctx
->urg
,
408 ctx
->fin
, ctx
->rst
, ctx
->psh
, ctx
->ecn
, ctx
->dport
);
409 off_next
= sizeof(struct tcphdr
);
413 __assemble_icmp6(data
, data_len
);
414 off_next
= sizeof(struct icmp6hdr
);
420 data
= packet
+ sizeof(*ip6h
) + off_next
;
421 data_len
= len
- sizeof(*ip6h
) - off_next
;
423 __assemble_data(data
, data_len
, ctx
->payload
);
425 return ntohl(ip6h
->ip6_flow
) & 0x000fffff;
428 static int check_ipv4(uint8_t *packet
, size_t len
, int ttl
, int id
,
429 const struct sockaddr
*ss
)
431 struct iphdr
*iph
= (struct iphdr
*) packet
;
432 struct iphdr
*iph_inner
;
433 struct icmphdr
*icmph
;
435 if (iph
->protocol
!= IPPROTO_ICMP
)
437 if (iph
->daddr
!= ((const struct sockaddr_in
*) ss
)->sin_addr
.s_addr
)
440 icmph
= (struct icmphdr
*) (packet
+ sizeof(struct iphdr
));
441 if (icmph
->type
!= ICMP_TIME_EXCEEDED
)
443 if (icmph
->code
!= ICMP_EXC_TTL
)
446 iph_inner
= (struct iphdr
*) (packet
+ sizeof(struct iphdr
) +
447 sizeof(struct icmphdr
));
448 if (ntohs(iph_inner
->id
) != id
)
454 static void handle_ipv4(uint8_t *packet
, size_t len
, int dns_resolv
, int latitude
)
457 struct iphdr
*iph
= (struct iphdr
*) packet
;
458 struct sockaddr_in sd
;
459 struct hostent
*hent
;
460 const char *as
, *country
, *city
;
462 memset(hbuff
, 0, sizeof(hbuff
));
463 memset(&sd
, 0, sizeof(sd
));
464 sd
.sin_family
= PF_INET
;
465 sd
.sin_addr
.s_addr
= iph
->saddr
;
467 getnameinfo((struct sockaddr
*) &sd
, sizeof(sd
),
468 hbuff
, NI_MAXHOST
, NULL
, 0, NI_NUMERICHOST
);
470 as
= geoip4_as_name(sd
);
471 country
= geoip4_country_name(sd
);
472 city
= geoip4_city_name(sd
);
475 hent
= gethostbyaddr(&sd
.sin_addr
, sizeof(sd
.sin_addr
), PF_INET
);
477 printf(" %s (%s)", hent
->h_name
, hbuff
);
479 printf(" %s", hbuff
);
481 printf(" %s", hbuff
);
484 printf(" in %s", as
);
486 printf(" in %s", country
);
488 printf(", %s", city
);
491 printf(" (%f/%f)", geoip4_latitude(sd
), geoip4_longitude(sd
));
494 static int check_ipv6(uint8_t *packet
, size_t len
, int ttl
, int id
,
495 const struct sockaddr
*ss
)
497 struct ip6_hdr
*ip6h
= (struct ip6_hdr
*) packet
;
498 struct ip6_hdr
*ip6h_inner
;
499 struct icmp6hdr
*icmp6h
;
501 if (ip6h
->ip6_nxt
!= 0x3a)
503 if (memcmp(&ip6h
->ip6_dst
, &(((const struct sockaddr_in6
*)
504 ss
)->sin6_addr
), sizeof(ip6h
->ip6_dst
)))
507 icmp6h
= (struct icmp6hdr
*) (packet
+ sizeof(*ip6h
));
508 if (icmp6h
->icmp6_type
!= ICMPV6_TIME_EXCEED
)
510 if (icmp6h
->icmp6_code
!= ICMPV6_EXC_HOPLIMIT
)
513 ip6h_inner
= (struct ip6_hdr
*) (packet
+ sizeof(*ip6h
) + sizeof(*icmp6h
));
514 if ((ntohl(ip6h_inner
->ip6_flow
) & 0x000fffff) != id
)
520 static void handle_ipv6(uint8_t *packet
, size_t len
, int dns_resolv
, int latitude
)
523 struct ip6_hdr
*ip6h
= (struct ip6_hdr
*) packet
;
524 struct sockaddr_in6 sd
;
525 struct hostent
*hent
;
526 const char *as
, *country
, *city
;
528 memset(hbuff
, 0, sizeof(hbuff
));
529 memset(&sd
, 0, sizeof(sd
));
530 sd
.sin6_family
= PF_INET6
;
531 memcpy(&sd
.sin6_addr
, &ip6h
->ip6_src
, sizeof(ip6h
->ip6_src
));
533 getnameinfo((struct sockaddr
*) &sd
, sizeof(sd
),
534 hbuff
, NI_MAXHOST
, NULL
, 0, NI_NUMERICHOST
);
536 as
= geoip6_as_name(sd
);
537 country
= geoip6_country_name(sd
);
538 city
= geoip6_city_name(sd
);
541 hent
= gethostbyaddr(&sd
.sin6_addr
, sizeof(sd
.sin6_addr
), PF_INET6
);
543 printf(" %s (%s)", hent
->h_name
, hbuff
);
545 printf(" %s", hbuff
);
547 printf(" %s", hbuff
);
550 printf(" in %s", as
);
552 printf(" in %s", country
);
554 printf(", %s", city
);
557 printf(" (%f/%f)", geoip6_latitude(sd
), geoip6_longitude(sd
));
560 static void show_trace_info(struct ctx
*ctx
, const struct sockaddr_storage
*ss
,
561 const struct sockaddr_storage
*sd
)
563 char hbuffs
[256], hbuffd
[256];
565 memset(hbuffd
, 0, sizeof(hbuffd
));
566 getnameinfo((struct sockaddr
*) sd
, sizeof(*sd
),
567 hbuffd
, sizeof(hbuffd
), NULL
, 0, NI_NUMERICHOST
);
569 memset(hbuffs
, 0, sizeof(hbuffs
));
570 getnameinfo((struct sockaddr
*) ss
, sizeof(*ss
),
571 hbuffs
, sizeof(hbuffs
), NULL
, 0, NI_NUMERICHOST
);
573 printf("AS path IPv%d TCP trace from %s to %s:%s (%s) with len %d "
574 "Bytes, %u max hops\n", ctx
->proto
== IPPROTO_IP
? 4 : 6,
575 hbuffs
, hbuffd
, ctx
->port
, ctx
->host
, ctx
->totlen
, ctx
->max_ttl
);
577 printf("Using flags SYN:%d,ACK:%d,ECN:%d,FIN:%d,PSH:%d,RST:%d,URG:%d\n",
578 ctx
->syn
, ctx
->ack
, ctx
->ecn
, ctx
->fin
, ctx
->psh
, ctx
->rst
, ctx
->urg
);
581 printf("With payload: \'%s\'\n", ctx
->payload
);
584 static int get_remote_fd(struct ctx
*ctx
, struct sockaddr_storage
*ss
,
585 struct sockaddr_storage
*sd
)
587 int fd
= -1, ret
, one
= 1;
588 struct addrinfo hints
, *ahead
, *ai
;
590 memset(&hints
, 0, sizeof(hints
));
591 hints
.ai_family
= PF_UNSPEC
;
592 hints
.ai_socktype
= SOCK_STREAM
;
593 hints
.ai_protocol
= IPPROTO_TCP
;
594 hints
.ai_flags
= AI_NUMERICSERV
;
596 ret
= getaddrinfo(ctx
->host
, ctx
->port
, &hints
, &ahead
);
598 panic("Cannot get address info!\n");
600 for (ai
= ahead
; ai
!= NULL
&& fd
< 0; ai
= ai
->ai_next
) {
601 if (!((ai
->ai_family
== PF_INET6
&& ctx
->proto
== IPPROTO_IPV6
) ||
602 (ai
->ai_family
== PF_INET
&& ctx
->proto
== IPPROTO_IP
)))
605 fd
= socket(ai
->ai_family
, SOCK_RAW
, IPPROTO_RAW
);
609 memset(ss
, 0, sizeof(*ss
));
610 ret
= device_address(ctx
->dev
, ai
->ai_family
, ss
);
612 panic("Cannot get own device address!\n");
614 ret
= bind(fd
, (struct sockaddr
*) ss
, sizeof(*ss
));
616 panic("Cannot bind socket!\n");
618 memset(sd
, 0, sizeof(*sd
));
619 memcpy(sd
, ai
->ai_addr
, ai
->ai_addrlen
);
621 ctx
->sd_len
= ai
->ai_addrlen
;
622 ctx
->dport
= strtoul(ctx
->port
, NULL
, 10);
624 ret
= setsockopt(fd
, ctx
->proto
, IP_HDRINCL
, &one
, sizeof(one
));
626 panic("Kernel does not support IP_HDRINCL!\n");
628 if (ai
->ai_family
== PF_INET6
) {
629 struct sockaddr_in6
*sd6
= (struct sockaddr_in6
*) sd
;
640 panic("Cannot create socket! Does remote "
642 ctx
->proto
== IPPROTO_IP
? 4 : 6);
647 static void inject_filter(struct ctx
*ctx
, int fd
)
649 struct sock_fprog bpf_ops
;
651 enable_kernel_bpf_jit_compiler();
653 memset(&bpf_ops
, 0, sizeof(bpf_ops
));
654 bpf_ops
.filter
= (struct sock_filter
*) af_ops
[ctx
->proto
].filter
;
655 bpf_ops
.len
= af_ops
[ctx
->proto
].flen
;
657 bpf_attach_to_sock(fd
, &bpf_ops
);
660 static int __process_node(struct ctx
*ctx
, int fd
, int fd_cap
, int ttl
,
661 int inner_proto
, uint8_t *pkt_snd
, uint8_t *pkt_rcv
,
662 const struct sockaddr_storage
*ss
,
663 const struct sockaddr_storage
*sd
, struct timeval
*diff
)
665 int pkt_id
, ret
, timeout
;
667 struct timeval start
, end
;
669 prepare_polling(fd_cap
, &pfd
);
671 memset(pkt_snd
, 0, ctx
->totlen
);
672 pkt_id
= af_ops
[ctx
->proto
].assembler(pkt_snd
, ctx
->totlen
, ttl
,
674 (const struct sockaddr
*) sd
,
675 (const struct sockaddr
*) ss
);
677 ret
= sendto(fd
, pkt_snd
, ctx
->totlen
, 0, (struct sockaddr
*) sd
,
680 panic("sendto failed: %s\n", strerror(errno
));
682 bug_on(gettimeofday(&start
, NULL
));
684 timeout
= (ctx
->timeout
> 0 ? ctx
->timeout
: 2) * 1000;
686 ret
= poll(&pfd
, 1, timeout
);
687 if (ret
> 0 && pfd
.revents
& POLLIN
&& sigint
== 0) {
688 bug_on(gettimeofday(&end
, NULL
));
690 timersub(&end
, &start
, diff
);
692 ret
= recvfrom(fd_cap
, pkt_rcv
, ctx
->rcvlen
, 0, NULL
, NULL
);
693 if (ret
< sizeof(struct ethhdr
) + af_ops
[ctx
->proto
].min_len_icmp
)
696 return af_ops
[ctx
->proto
].check(pkt_rcv
+ sizeof(struct ethhdr
),
697 ret
- sizeof(struct ethhdr
), ttl
,
698 pkt_id
, (const struct sockaddr
*) ss
);
706 static void timerdiv(const unsigned long divisor
, const struct timeval
*tv
,
707 struct timeval
*result
)
709 uint64_t x
= ((uint64_t) tv
->tv_sec
* 1000 * 1000 + tv
->tv_usec
) / divisor
;
711 result
->tv_sec
= x
/ 1000 / 1000;
712 result
->tv_usec
= x
% (1000 * 1000);
715 static int timevalcmp(const void *t1
, const void *t2
)
717 if (timercmp((struct timeval
*) t1
, (struct timeval
*) t2
, <))
719 if (timercmp((struct timeval
*) t1
, (struct timeval
*) t2
, >))
725 static int __process_time(struct ctx
*ctx
, int fd
, int fd_cap
, int ttl
,
726 int inner_proto
, uint8_t *pkt_snd
, uint8_t *pkt_rcv
,
727 const struct sockaddr_storage
*ss
,
728 const struct sockaddr_storage
*sd
)
730 int good
= 0, i
, j
= 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
== 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
);
804 static int __probe_remote(struct ctx
*ctx
, int fd
, int fd_cap
, int ttl
,
805 uint8_t *pkt_snd
, uint8_t *pkt_rcv
,
806 const struct sockaddr_storage
*ss
,
807 const struct sockaddr_storage
*sd
,
810 int ret
= -EIO
, tries
= ctx
->queries
;
812 while (tries
-- > 0 && sigint
== 0) {
813 ret
= __process_time(ctx
, fd
, fd_cap
, ttl
, inner_proto
,
814 pkt_snd
, pkt_rcv
, ss
, sd
);
818 af_ops
[ctx
->proto
].handler(pkt_rcv
+ sizeof(struct ethhdr
),
819 ret
- sizeof(struct ethhdr
),
820 ctx
->dns_resolv
, ctx
->latitude
);
822 struct pkt_buff
*pkt
;
825 pkt
= pkt_alloc(pkt_rcv
, ret
);
837 static int __process_ttl(struct ctx
*ctx
, int fd
, int fd_cap
, int ttl
,
838 uint8_t *pkt_snd
, uint8_t *pkt_rcv
,
839 const struct sockaddr_storage
*ss
,
840 const struct sockaddr_storage
*sd
)
843 const int inner_protos
[] = {
848 printf("%2d: ", ttl
);
851 for (i
= 0; i
< array_size(inner_protos
) && sigint
== 0; ++i
) {
852 ret
= __probe_remote(ctx
, fd
, fd_cap
, ttl
, pkt_snd
, pkt_rcv
, ss
, sd
,
859 printf("\r%2d: ?[ no answer]", ttl
);
862 if (ctx
->show
&& ret
<= 0)
869 static int main_trace(struct ctx
*ctx
)
871 int fd
, fd_cap
, ifindex
, ttl
;
872 struct ring dummy_ring
;
873 struct sockaddr_storage ss
, sd
;
874 uint8_t *pkt_snd
, *pkt_rcv
;
876 fd
= get_remote_fd(ctx
, &ss
, &sd
);
877 fd_cap
= pf_socket();
879 inject_filter(ctx
, fd_cap
);
881 ifindex
= device_ifindex(ctx
->dev
);
882 bind_rx_ring(fd_cap
, &dummy_ring
, ifindex
);
884 if (ctx
->totlen
< af_ops
[ctx
->proto
].min_len_tcp
) {
885 ctx
->totlen
= af_ops
[ctx
->proto
].min_len_tcp
;
887 ctx
->totlen
+= strlen(ctx
->payload
);
890 ctx
->rcvlen
= device_mtu(ctx
->dev
) - sizeof(struct ethhdr
);
891 if (ctx
->totlen
>= ctx
->rcvlen
)
892 panic("Packet len exceeds device MTU!\n");
894 pkt_snd
= xmalloc(ctx
->totlen
);
895 pkt_rcv
= xmalloc(ctx
->rcvlen
);
897 show_trace_info(ctx
, &ss
, &sd
);
899 for (ttl
= ctx
->init_ttl
; ttl
<= ctx
->max_ttl
&& sigint
== 0; ++ttl
)
900 __process_ttl(ctx
, fd
, fd_cap
, ttl
, pkt_snd
, pkt_rcv
,
912 int main(int argc
, char **argv
)
914 int c
, opt_index
, ret
;
922 memset(&ctx
, 0, sizeof(ctx
));
927 ctx
.proto
= IPPROTO_IP
;
929 ctx
.dev
= xstrdup("eth0");
930 ctx
.port
= xstrdup("80");
932 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
933 &opt_index
)) != EOF
) {
946 ctx
.host
= xstrdup(optarg
);
951 ctx
.port
= xstrdup(optarg
);
957 ctx
.proto
= IPPROTO_IP
;
960 ctx
.proto
= IPPROTO_IPV6
;
969 ctx
.init_ttl
= atoi(optarg
);
970 if (ctx
.init_ttl
<= 0)
974 ctx
.max_ttl
= atoi(optarg
);
975 if (ctx
.max_ttl
<= 0)
981 ctx
.dev
= xstrdup(optarg
);
984 ctx
.queries
= atoi(optarg
);
985 if (ctx
.queries
<= 0)
989 ctx
.timeout
= atoi(optarg
);
990 if (ctx
.timeout
<= 0)
1019 ctx
.tos
= atoi(optarg
);
1027 ctx
.payload
= xstrdup(optarg
);
1030 ctx
.totlen
= atoi(optarg
);
1031 if (ctx
.totlen
<= 0)
1047 panic("Option -%c requires an argument!\n",
1050 if (isprint(optopt
))
1051 printf("Unknown option character `0x%X\'!\n", optopt
);
1059 if (argc
< 3 || !ctx
.host
|| !ctx
.port
|| ctx
.init_ttl
> ctx
.max_ttl
||
1060 ctx
.init_ttl
> MAXTTL
|| ctx
.max_ttl
> MAXTTL
)
1063 if (!device_up_and_running(ctx
.dev
))
1064 panic("Networking device not up and running!\n");
1065 if (device_mtu(ctx
.dev
) <= ctx
.totlen
)
1066 panic("Packet larger than device MTU!\n");
1068 register_signal(SIGHUP
, signal_handler
);
1069 register_signal(SIGINT
, signal_handler
);
1074 ret
= main_trace(&ctx
);