2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 - 2012 Daniel Borkmann.
5 * Copyright 2011 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
8 * A tiny tool to provide top-like netfilter connection tracking information.
10 * The Dark Lord has Nine. But we have One, mightier than they: the White
11 * Rider. He has passed through the fire and the abyss, and they shall
12 * fear him. We will go where he leads.
14 * -- The Lord of the Rings, Aragorn,
15 * Chapter 'The White Rider'.
28 #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
29 #include <libnetfilter_conntrack/libnetfilter_conntrack_tcp.h>
30 #include <libnetfilter_conntrack/libnetfilter_conntrack_dccp.h>
31 #include <libnetfilter_conntrack/libnetfilter_conntrack_sctp.h>
33 #include <GeoIPCity.h>
34 #include <netinet/in.h>
47 #include "dissector_eth.h"
56 uint32_t flow_id
, use
, status
;
57 uint8_t l3_proto
, l4_proto
;
58 uint32_t ip4_src_addr
, ip4_dst_addr
;
59 uint32_t ip6_src_addr
[4], ip6_dst_addr
[4];
60 uint16_t port_src
, port_dst
;
61 uint8_t tcp_state
, tcp_flags
, sctp_state
, dccp_state
;
62 uint64_t counter_pkts
, counter_bytes
;
63 uint64_t timestamp_start
, timestamp_stop
;
64 char country_src
[128], country_dst
[128];
65 char city_src
[128], city_dst
[128];
66 char rev_dns_src
[256], rev_dns_dst
[256];
68 struct flow_entry
*next
;
74 struct flow_entry
*head
;
78 #ifndef ATTR_TIMESTAMP_START
79 # define ATTR_TIMESTAMP_START 63
81 #ifndef ATTR_TIMESTAMP_STOP
82 # define ATTR_TIMESTAMP_STOP 64
85 #define SCROLL_MAX 1000
87 #define INCLUDE_IPV4 (1 << 0)
88 #define INCLUDE_IPV6 (1 << 1)
89 #define INCLUDE_UDP (1 << 2)
90 #define INCLUDE_TCP (1 << 3)
91 #define INCLUDE_DCCP (1 << 4)
92 #define INCLUDE_ICMP (1 << 5)
93 #define INCLUDE_SCTP (1 << 6)
95 #define PRES_FLAG_COUNTERS (1 << 0)
96 #define PRES_FLAG_TCP_STATE (1 << 1)
97 #define PRES_FLAG_DCCP_STATE (1 << 2)
98 #define PRES_FLAG_SCTP_STATE (1 << 3)
100 volatile sig_atomic_t sigint
= 0;
102 static int what
= INCLUDE_IPV4
| INCLUDE_IPV6
| INCLUDE_TCP
, show_src
= 0;
104 struct geo_ip_db geo_country
, geo_city
;
106 static struct flow_list flow_list
;
108 static const char *short_options
= "vhTULKsOPDIS46";
109 static const struct option long_options
[] = {
110 {"ipv4", no_argument
, NULL
, '4'},
111 {"ipv6", no_argument
, NULL
, '6'},
112 {"tcp", no_argument
, NULL
, 'T'},
113 {"udp", no_argument
, NULL
, 'U'},
114 {"dccp", no_argument
, NULL
, 'D'},
115 {"icmp", no_argument
, NULL
, 'I'},
116 {"sctp", no_argument
, NULL
, 'S'},
117 {"show-src", no_argument
, NULL
, 's'},
118 {"city-db4", required_argument
, NULL
, 'L'},
119 {"country-db4", required_argument
, NULL
, 'K'},
120 {"city-db6", required_argument
, NULL
, 'O'},
121 {"country-db6", required_argument
, NULL
, 'P'},
122 {"version", no_argument
, NULL
, 'v'},
123 {"help", no_argument
, NULL
, 'h'},
127 static const char *const l3proto2str
[AF_MAX
] = {
132 static const char *const l4proto2str
[IPPROTO_MAX
] = {
133 [IPPROTO_TCP
] = "tcp",
134 [IPPROTO_UDP
] = "udp",
135 [IPPROTO_UDPLITE
] = "udplite",
136 [IPPROTO_ICMP
] = "icmp",
137 [IPPROTO_ICMPV6
] = "icmpv6",
138 [IPPROTO_SCTP
] = "sctp",
139 [IPPROTO_GRE
] = "gre",
140 [IPPROTO_DCCP
] = "dccp",
141 [IPPROTO_IGMP
] = "igmp",
142 [IPPROTO_IPIP
] = "ipip",
143 [IPPROTO_EGP
] = "egp",
144 [IPPROTO_PUP
] = "pup",
145 [IPPROTO_IDP
] = "idp",
146 [IPPROTO_RSVP
] = "rsvp",
147 [IPPROTO_IPV6
] = "ip6tun",
148 [IPPROTO_ESP
] = "esp",
150 [IPPROTO_PIM
] = "pim",
151 [IPPROTO_COMP
] = "comp",
154 static const char *const tcp_state2str
[TCP_CONNTRACK_MAX
] = {
155 [TCP_CONNTRACK_NONE
] = "NOSTATE",
156 [TCP_CONNTRACK_SYN_SENT
] = "SYN_SENT",
157 [TCP_CONNTRACK_SYN_RECV
] = "SYN_RECV",
158 [TCP_CONNTRACK_ESTABLISHED
] = "ESTABLISHED",
159 [TCP_CONNTRACK_FIN_WAIT
] = "FIN_WAIT",
160 [TCP_CONNTRACK_CLOSE_WAIT
] = "CLOSE_WAIT",
161 [TCP_CONNTRACK_LAST_ACK
] = "LAST_ACK",
162 [TCP_CONNTRACK_TIME_WAIT
] = "TIME_WAIT",
163 [TCP_CONNTRACK_CLOSE
] = "CLOSE",
164 [TCP_CONNTRACK_SYN_SENT2
] = "SYN_SENT2",
167 static const uint8_t tcp_states
[] = {
168 TCP_CONNTRACK_SYN_SENT
,
169 TCP_CONNTRACK_SYN_RECV
,
170 TCP_CONNTRACK_ESTABLISHED
,
171 TCP_CONNTRACK_FIN_WAIT
,
172 TCP_CONNTRACK_CLOSE_WAIT
,
173 TCP_CONNTRACK_LAST_ACK
,
174 TCP_CONNTRACK_TIME_WAIT
,
176 TCP_CONNTRACK_SYN_SENT2
,
180 static const char *const dccp_state2str
[DCCP_CONNTRACK_MAX
] = {
181 [DCCP_CONNTRACK_NONE
] = "NOSTATE",
182 [DCCP_CONNTRACK_REQUEST
] = "REQUEST",
183 [DCCP_CONNTRACK_RESPOND
] = "RESPOND",
184 [DCCP_CONNTRACK_PARTOPEN
] = "PARTOPEN",
185 [DCCP_CONNTRACK_OPEN
] = "OPEN",
186 [DCCP_CONNTRACK_CLOSEREQ
] = "CLOSEREQ",
187 [DCCP_CONNTRACK_CLOSING
] = "CLOSING",
188 [DCCP_CONNTRACK_TIMEWAIT
] = "TIMEWAIT",
189 [DCCP_CONNTRACK_IGNORE
] = "IGNORE",
190 [DCCP_CONNTRACK_INVALID
] = "INVALID",
193 static const uint8_t dccp_states
[] = {
195 DCCP_CONNTRACK_REQUEST
,
196 DCCP_CONNTRACK_RESPOND
,
197 DCCP_CONNTRACK_PARTOPEN
,
199 DCCP_CONNTRACK_CLOSEREQ
,
200 DCCP_CONNTRACK_CLOSING
,
201 DCCP_CONNTRACK_TIMEWAIT
,
202 DCCP_CONNTRACK_IGNORE
,
203 DCCP_CONNTRACK_INVALID
,
206 static const char *const sctp_state2str
[SCTP_CONNTRACK_MAX
] = {
207 [SCTP_CONNTRACK_NONE
] = "NOSTATE",
208 [SCTP_CONNTRACK_CLOSED
] = "CLOSED",
209 [SCTP_CONNTRACK_COOKIE_WAIT
] = "COOKIE_WAIT",
210 [SCTP_CONNTRACK_COOKIE_ECHOED
] = "COOKIE_ECHOED",
211 [SCTP_CONNTRACK_ESTABLISHED
] = "ESTABLISHED",
212 [SCTP_CONNTRACK_SHUTDOWN_SENT
] = "SHUTDOWN_SENT",
213 [SCTP_CONNTRACK_SHUTDOWN_RECD
] = "SHUTDOWN_RECD",
214 [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
] = "SHUTDOWN_ACK_SENT",
217 static const uint8_t sctp_states
[] = {
219 SCTP_CONNTRACK_CLOSED
,
220 SCTP_CONNTRACK_COOKIE_WAIT
,
221 SCTP_CONNTRACK_COOKIE_ECHOED
,
222 SCTP_CONNTRACK_ESTABLISHED
,
223 SCTP_CONNTRACK_SHUTDOWN_SENT
,
224 SCTP_CONNTRACK_SHUTDOWN_RECD
,
225 SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
,
228 static const struct nfct_filter_ipv4 filter_ipv4
= {
229 .addr
= __constant_htonl(INADDR_LOOPBACK
),
233 static const struct nfct_filter_ipv6 filter_ipv6
= {
234 .addr
= { 0x0, 0x0, 0x0, 0x1 },
235 .mask
= { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff },
238 static void signal_handler(int number
)
250 static void flow_entry_from_ct(struct flow_entry
*n
, struct nf_conntrack
*ct
);
251 static void flow_entry_get_extended(struct flow_entry
*n
);
253 static void help(void)
255 printf("\nflowtop %s, top-like netfilter TCP/UDP flow tracking\n",
257 puts("http://www.netsniff-ng.org\n\n"
258 "Usage: flowtop [options]\n"
260 " -4|--ipv4 Show only IPv4 flows (default)\n"
261 " -6|--ipv6 Show only IPv6 flows (default)\n"
262 " -T|--tcp Show only TCP flows (default)\n"
263 " -U|--udp Show only UDP flows\n"
264 " -D|--dccp Show only DCCP flows\n"
265 " -I|--icmp Show only ICMP/ICMPv6 flows\n"
266 " -S|--sctp Show only SCTP flows\n"
267 " -s|--show-src Also show source, not only dest\n"
268 " --city-db4 <path> Specifiy path for geoip4 city database\n"
269 " --country-db4 <path> Specifiy path for geoip4 country database\n"
270 " --city-db6 <path> Specifiy path for geoip6 city database\n"
271 " --country-db6 <path> Specifiy path for geoip6 country database\n"
272 " -v|--version Print version\n"
273 " -h|--help Print this help\n\n"
276 " flowtop -46UTDISs\n\n"
278 " If netfilter is not running, you can activate it with e.g.:\n"
279 " iptables -A INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT\n"
280 " iptables -A OUTPUT -p tcp -m state --state NEW,ESTABLISHED -j ACCEPT\n\n"
281 "Please report bugs to <bugs@netsniff-ng.org>\n"
282 "Copyright (C) 2011-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n"
283 "Copyright (C) 2011-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
284 "License: GNU GPL version 2.0\n"
285 "This is free software: you are free to change and redistribute it.\n"
286 "There is NO WARRANTY, to the extent permitted by law.\n");
290 static void version(void)
292 printf("\nflowtop %s, top-like netfilter TCP/UDP flow tracking\n",
294 puts("http://www.netsniff-ng.org\n\n"
295 "Please report bugs to <bugs@netsniff-ng.org>\n"
296 "Copyright (C) 2011-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n"
297 "Copyright (C) 2011-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
298 "License: GNU GPL version 2.0\n"
299 "This is free software: you are free to change and redistribute it.\n"
300 "There is NO WARRANTY, to the extent permitted by law.\n");
304 static inline struct flow_entry
*flow_entry_xalloc(void)
306 return xzmalloc(sizeof(struct flow_entry
));
309 static inline void flow_entry_xfree(struct flow_entry
*n
)
314 static inline void flow_list_init(struct flow_list
*fl
)
317 spinlock_init(&fl
->lock
);
320 static void flow_list_new_entry(struct flow_list
*fl
, struct nf_conntrack
*ct
)
322 struct flow_entry
*n
= flow_entry_xalloc();
324 flow_entry_from_ct(n
, ct
);
325 flow_entry_get_extended(n
);
327 rcu_assign_pointer(n
->next
, fl
->head
);
328 rcu_assign_pointer(fl
->head
, n
);
331 static struct flow_entry
*flow_list_find_id(struct flow_list
*fl
,
334 struct flow_entry
*n
= rcu_dereference(fl
->head
);
337 if (n
->flow_id
== id
)
340 n
= rcu_dereference(n
->next
);
346 static struct flow_entry
*flow_list_find_prev_id(struct flow_list
*fl
,
349 struct flow_entry
*n
= rcu_dereference(fl
->head
), *tmp
;
351 if (n
->flow_id
== id
)
354 while ((tmp
= rcu_dereference(n
->next
)) != NULL
) {
355 if (tmp
->flow_id
== id
)
364 static void flow_list_update_entry(struct flow_list
*fl
,
365 struct nf_conntrack
*ct
)
368 struct flow_entry
*n
;
370 n
= flow_list_find_id(fl
, nfct_get_attr_u32(ct
, ATTR_ID
));
372 n
= flow_entry_xalloc();
376 flow_entry_from_ct(n
, ct
);
378 flow_entry_get_extended(n
);
380 rcu_assign_pointer(n
->next
, fl
->head
);
381 rcu_assign_pointer(fl
->head
, n
);
385 static void flow_list_destroy_entry(struct flow_list
*fl
,
386 struct nf_conntrack
*ct
)
388 struct flow_entry
*n1
, *n2
;
389 uint32_t id
= nfct_get_attr_u32(ct
, ATTR_ID
);
391 n1
= flow_list_find_id(fl
, id
);
393 n2
= flow_list_find_prev_id(fl
, id
);
395 rcu_assign_pointer(n2
->next
, n1
->next
);
396 rcu_assign_pointer(n1
->next
, NULL
);
398 flow_entry_xfree(n1
);
400 flow_entry_xfree(fl
->head
);
402 rcu_assign_pointer(fl
->head
, NULL
);
407 static void flow_list_destroy(struct flow_list
*fl
)
409 struct flow_entry
*n
;
411 while (fl
->head
!= NULL
) {
412 n
= rcu_dereference(fl
->head
->next
);
413 rcu_assign_pointer(fl
->head
->next
, NULL
);
415 flow_entry_xfree(fl
->head
);
416 rcu_assign_pointer(fl
->head
, n
);
420 spinlock_destroy(&fl
->lock
);
423 static int walk_process(char *process
, struct flow_entry
*n
)
430 if (snprintf(path
, sizeof(path
), "/proc/%s/fd", process
) == -1)
431 panic("giant process name! %s\n", process
);
437 while ((ent
= readdir(dir
))) {
440 if (snprintf(path
, sizeof(path
), "/proc/%s/fd/%s",
441 process
, ent
->d_name
) < 0)
444 if (stat(path
, &statbuf
) < 0)
447 if (S_ISSOCK(statbuf
.st_mode
) && n
->inode
== statbuf
.st_ino
) {
448 memset(n
->cmdline
, 0, sizeof(n
->cmdline
));
450 snprintf(path
, sizeof(path
), "/proc/%s/exe", process
);
452 ret
= readlink(path
, n
->cmdline
,
453 sizeof(n
->cmdline
) - 1);
455 panic("readlink error: %s\n", strerror(errno
));
457 n
->procnum
= atoi(process
);
466 static void walk_processes(struct flow_entry
*n
)
472 /* n->inode must be set */
474 memset(n
->cmdline
, 0, sizeof(n
->cmdline
));
478 dir
= opendir("/proc");
480 panic("Cannot open /proc!\n");
482 while ((ent
= readdir(dir
))) {
483 if (strspn(ent
->d_name
, "0123456789") == strlen(ent
->d_name
)) {
484 ret
= walk_process(ent
->d_name
, n
);
493 static int get_port_inode(uint16_t port
, int proto
, int is_ip6
)
496 char path
[128], buff
[1024];
499 memset(path
, 0, sizeof(path
));
500 snprintf(path
, sizeof(path
), "/proc/net/%s%s",
501 l4proto2str
[proto
], is_ip6
? "6" : "");
503 proc
= fopen(path
, "r");
507 memset(buff
, 0, sizeof(buff
));
509 while (fgets(buff
, sizeof(buff
), proc
) != NULL
) {
511 unsigned int lport
= 0;
513 buff
[sizeof(buff
) - 1] = 0;
514 if (sscanf(buff
, "%*u: %*X:%X %*X:%*X %*X %*X:%*X %*X:%*X "
515 "%*X %*u %*u %u", &lport
, &inode
) == 2) {
516 if ((uint16_t) lport
== port
) {
522 memset(buff
, 0, sizeof(buff
));
529 #define CP_NFCT(elem, attr, x) \
530 do { n->elem = nfct_get_attr_u##x(ct,(attr)); } while (0)
531 #define CP_NFCT_BUFF(elem, attr) do { \
532 const uint8_t *buff = nfct_get_attr(ct,(attr)); \
534 memcpy(n->elem, buff, sizeof(n->elem)); \
537 static void flow_entry_from_ct(struct flow_entry
*n
, struct nf_conntrack
*ct
)
539 CP_NFCT(l3_proto
, ATTR_ORIG_L3PROTO
, 8);
540 CP_NFCT(l4_proto
, ATTR_ORIG_L4PROTO
, 8);
542 CP_NFCT(ip4_src_addr
, ATTR_ORIG_IPV4_SRC
, 32);
543 CP_NFCT(ip4_dst_addr
, ATTR_ORIG_IPV4_DST
, 32);
545 CP_NFCT(port_src
, ATTR_ORIG_PORT_SRC
, 16);
546 CP_NFCT(port_dst
, ATTR_ORIG_PORT_DST
, 16);
548 CP_NFCT(status
, ATTR_STATUS
, 32);
550 CP_NFCT(tcp_state
, ATTR_TCP_STATE
, 8);
551 CP_NFCT(tcp_flags
, ATTR_TCP_FLAGS_ORIG
, 8);
552 CP_NFCT(sctp_state
, ATTR_SCTP_STATE
, 8);
553 CP_NFCT(dccp_state
, ATTR_DCCP_STATE
, 8);
555 CP_NFCT(counter_pkts
, ATTR_ORIG_COUNTER_PACKETS
, 64);
556 CP_NFCT(counter_bytes
, ATTR_ORIG_COUNTER_BYTES
, 64);
558 CP_NFCT(timestamp_start
, ATTR_TIMESTAMP_START
, 64);
559 CP_NFCT(timestamp_stop
, ATTR_TIMESTAMP_STOP
, 64);
561 CP_NFCT(flow_id
, ATTR_ID
, 32);
562 CP_NFCT(use
, ATTR_USE
, 32);
564 CP_NFCT_BUFF(ip6_src_addr
, ATTR_ORIG_IPV6_SRC
);
565 CP_NFCT_BUFF(ip6_dst_addr
, ATTR_ORIG_IPV6_DST
);
567 n
->port_src
= ntohs(n
->port_src
);
568 n
->port_dst
= ntohs(n
->port_dst
);
570 n
->ip4_src_addr
= ntohl(n
->ip4_src_addr
);
571 n
->ip4_dst_addr
= ntohl(n
->ip4_dst_addr
);
574 enum flow_entry_direction
{
579 static inline int flow_entry_get_extended_is_dns(struct flow_entry
*n
)
581 /* We don't want to analyze / display DNS itself, since we
582 * use it to resolve reverse dns.
584 return n
->port_src
== 53 || n
->port_dst
== 53;
587 #define SELFLD(dir,src_member,dst_member) \
588 (((dir) == flow_entry_src) ? n->src_member : n->dst_member)
590 static struct sockaddr_in
*
591 flow_entry_get_sain4_obj(struct flow_entry
*n
, enum flow_entry_direction dir
,
592 struct sockaddr_in
*sa
)
594 memset(sa
, 0, sizeof(*sa
));
595 sa
->sin_family
= PF_INET
;
596 sa
->sin_addr
.s_addr
= htonl(SELFLD(dir
, ip4_src_addr
, ip4_dst_addr
));
601 static struct sockaddr_in6
*
602 flow_entry_get_sain6_obj(struct flow_entry
*n
, enum flow_entry_direction dir
,
603 struct sockaddr_in6
*sa
)
605 memset(sa
, 0, sizeof(*sa
));
606 sa
->sin6_family
= PF_INET6
;
608 memcpy(&sa
->sin6_addr
, SELFLD(dir
, ip6_src_addr
, ip6_dst_addr
),
609 sizeof(SELFLD(dir
, ip6_src_addr
, ip6_dst_addr
)));
615 flow_entry_geo_city_lookup_generic(struct flow_entry
*n
,
616 enum flow_entry_direction dir
)
618 GeoIPRecord
*gir
= NULL
;
619 struct sockaddr_in sa4
;
620 struct sockaddr_in6 sa6
;
621 const char *city
= NULL
;
623 switch (n
->l3_proto
) {
628 flow_entry_get_sain4_obj(n
, dir
, &sa4
);
629 gir
= GeoIP_record_by_ipnum(geo_city
.gi4
,
630 ntohl(sa4
.sin_addr
.s_addr
));
634 flow_entry_get_sain6_obj(n
, dir
, &sa6
);
635 gir
= GeoIP_record_by_ipnum_v6(geo_city
.gi6
, sa6
.sin6_addr
);
642 bug_on(sizeof(n
->city_src
) != sizeof(n
->city_dst
));
645 memcpy(SELFLD(dir
, city_src
, city_dst
), city
,
646 min(sizeof(n
->city_src
), strlen(city
)));
648 memset(SELFLD(dir
, city_src
, city_dst
), 0,
649 sizeof(n
->city_src
));
654 flow_entry_geo_country_lookup_generic(struct flow_entry
*n
,
655 enum flow_entry_direction dir
)
657 struct sockaddr_in sa4
;
658 struct sockaddr_in6 sa6
;
659 inline const char *make_na(const char *p
) { return p
? : "N/A"; }
660 const char *country
= NULL
;
662 switch (n
->l3_proto
) {
667 flow_entry_get_sain4_obj(n
, dir
, &sa4
);
668 country
= GeoIP_country_name_by_ipnum(geo_country
.gi4
,
669 ntohl(sa4
.sin_addr
.s_addr
));
673 flow_entry_get_sain6_obj(n
, dir
, &sa6
);
674 country
= GeoIP_country_name_by_ipnum_v6(geo_country
.gi6
,
679 country
= make_na(country
);
681 bug_on(sizeof(n
->country_src
) != sizeof(n
->country_dst
));
682 memcpy(SELFLD(dir
, country_src
, country_dst
), country
,
683 min(sizeof(n
->country_src
), strlen(country
)));
686 static void flow_entry_get_extended_geo(struct flow_entry
*n
,
687 enum flow_entry_direction dir
)
689 flow_entry_geo_city_lookup_generic(n
, dir
);
690 flow_entry_geo_country_lookup_generic(n
, dir
);
693 static void flow_entry_get_extended_revdns(struct flow_entry
*n
,
694 enum flow_entry_direction dir
)
697 struct sockaddr_in sa4
;
698 struct sockaddr_in6 sa6
;
700 struct hostent
*hent
;
702 switch (n
->l3_proto
) {
707 flow_entry_get_sain4_obj(n
, dir
, &sa4
);
708 sa
= (struct sockaddr
*) &sa4
;
709 sa_len
= sizeof(sa4
);
710 hent
= gethostbyaddr(&sa4
.sin_addr
, sizeof(sa4
.sin_addr
),
715 flow_entry_get_sain6_obj(n
, dir
, &sa6
);
716 sa
= (struct sockaddr
*) &sa6
;
717 sa_len
= sizeof(sa6
);
718 hent
= gethostbyaddr(&sa6
.sin6_addr
, sizeof(sa6
.sin6_addr
),
723 bug_on(sizeof(n
->rev_dns_src
) != sizeof(n
->rev_dns_dst
));
724 getnameinfo(sa
, sa_len
, SELFLD(dir
, rev_dns_src
, rev_dns_dst
),
725 sizeof(n
->rev_dns_src
), NULL
, 0, NI_NUMERICHOST
);
728 memset(n
->rev_dns_dst
, 0, sizeof(n
->rev_dns_dst
));
729 memcpy(SELFLD(dir
, rev_dns_src
, rev_dns_dst
),
730 hent
->h_name
, min(sizeof(n
->rev_dns_src
),
731 strlen(hent
->h_name
)));
735 static void flow_entry_get_extended(struct flow_entry
*n
)
737 if (n
->flow_id
== 0 || flow_entry_get_extended_is_dns(n
))
740 flow_entry_get_extended_revdns(n
, flow_entry_src
);
741 flow_entry_get_extended_geo(n
, flow_entry_src
);
743 flow_entry_get_extended_revdns(n
, flow_entry_dst
);
744 flow_entry_get_extended_geo(n
, flow_entry_dst
);
746 /* Lookup application */
747 n
->inode
= get_port_inode(n
->port_src
, n
->l4_proto
,
748 n
->l3_proto
== AF_INET6
);
753 static uint16_t presenter_get_port(uint16_t src
, uint16_t dst
, int tcp
)
755 if (src
< dst
&& src
< 1024) {
757 } else if (dst
< src
&& dst
< 1024) {
760 const char *tmp1
, *tmp2
;
762 tmp1
= lookup_port_tcp(src
);
763 tmp2
= lookup_port_tcp(dst
);
765 tmp1
= lookup_port_udp(src
);
766 tmp2
= lookup_port_udp(dst
);
770 } else if (!tmp1
&& tmp2
) {
781 static void presenter_screen_init(WINDOW
**screen
)
783 (*screen
) = initscr();
786 keypad(stdscr
, TRUE
);
787 nodelay(*screen
, TRUE
);
792 static void presenter_screen_do_line(WINDOW
*screen
, struct flow_entry
*n
,
795 char tmp
[128], *pname
= NULL
;
798 mvwprintw(screen
, *line
, 2, "");
800 /* PID, application name */
801 if (n
->procnum
> 0) {
802 slprintf(tmp
, sizeof(tmp
), "%s(%u)", basename(n
->cmdline
),
806 attron(COLOR_PAIR(3));
808 attroff(COLOR_PAIR(3));
812 /* L3 protocol, L4 protocol, states */
813 printw("%s:%s", l3proto2str
[n
->l3_proto
], l4proto2str
[n
->l4_proto
]);
815 attron(COLOR_PAIR(3));
816 switch (n
->l4_proto
) {
818 printw("%s", tcp_state2str
[n
->tcp_state
]);
821 printw("%s", sctp_state2str
[n
->sctp_state
]);
824 printw("%s", dccp_state2str
[n
->dccp_state
]);
827 case IPPROTO_UDPLITE
:
833 attroff(COLOR_PAIR(3));
836 /* Guess application port */
837 switch (n
->l4_proto
) {
839 port
= presenter_get_port(n
->port_src
, n
->port_dst
, 1);
840 pname
= lookup_port_tcp(port
);
843 case IPPROTO_UDPLITE
:
844 port
= presenter_get_port(n
->port_src
, n
->port_dst
, 0);
845 pname
= lookup_port_udp(port
);
850 printw(":%s", pname
);
855 /* Number packets, bytes */
856 if (n
->counter_pkts
> 0 && n
->counter_bytes
> 0)
857 printw(" (%llu pkts, %llu bytes) ->",
858 n
->counter_pkts
, n
->counter_bytes
);
860 /* Show source information: reverse DNS, port, country, city */
862 attron(COLOR_PAIR(1));
863 mvwprintw(screen
, ++(*line
), 8, "src: %s", n
->rev_dns_src
);
864 attroff(COLOR_PAIR(1));
866 printw(":%u (", n
->port_src
);
868 attron(COLOR_PAIR(4));
869 printw("%s", n
->country_src
);
870 attroff(COLOR_PAIR(4));
873 printw(", %s", n
->city_src
);
877 /* Show dest information: reverse DNS, port, country, city */
878 attron(COLOR_PAIR(2));
879 mvwprintw(screen
, ++(*line
), 8, "dst: %s", n
->rev_dns_dst
);
880 attroff(COLOR_PAIR(2));
882 printw(":%u (", n
->port_dst
);
884 attron(COLOR_PAIR(4));
885 printw("%s", n
->country_dst
);
886 attroff(COLOR_PAIR(4));
889 printw(", %s", n
->city_dst
);
894 if (n
->tcp_state
!= tcp_states
[i
] ||
895 (i
!= TCP_CONNTRACK_NONE
&&
896 n
->tcp_state
== TCP_CONNTRACK_NONE
) ||
898 presenter_get_port(n
->port_src
,
899 n
->port_dst
, 0) == 53) {
900 n
= rcu_dereference(n
->next
);
905 static inline int presenter_flow_wrong_state(struct flow_entry
*n
, int state
)
909 switch (n
->l4_proto
) {
911 if (n
->tcp_state
== state
)
915 if (n
->sctp_state
== state
)
919 if (n
->dccp_state
== state
)
923 case IPPROTO_UDPLITE
:
933 static void presenter_screen_update(WINDOW
*screen
, struct flow_list
*fl
,
937 unsigned int line
= 3;
938 struct flow_entry
*n
;
939 uint8_t protocols
[] = {
948 size_t protocol_state_size
[] = {
949 [IPPROTO_TCP
] = array_size(tcp_states
),
950 [IPPROTO_DCCP
] = array_size(dccp_states
),
951 [IPPROTO_SCTP
] = array_size(sctp_states
),
953 [IPPROTO_UDPLITE
] = 1,
955 [IPPROTO_ICMPV6
] = 1,
960 maxy
= getmaxy(screen
);
964 init_pair(1, COLOR_RED
, COLOR_BLACK
);
965 init_pair(2, COLOR_BLUE
, COLOR_BLACK
);
966 init_pair(3, COLOR_YELLOW
, COLOR_BLACK
);
967 init_pair(4, COLOR_GREEN
, COLOR_BLACK
);
972 mvwprintw(screen
, 1, 2, "Kernel netfilter TCP/UDP "
973 "flow statistics, [+%d]", skip_lines
);
977 if (rcu_dereference(fl
->head
) == NULL
)
978 mvwprintw(screen
, line
, 2, "(No active sessions! "
979 "Is netfilter running?)");
981 for (i
= 0; i
< array_size(protocols
); i
++) {
982 for (j
= 0; j
< protocol_state_size
[protocols
[i
]]; j
++) {
983 n
= rcu_dereference(fl
->head
);
984 while (n
&& maxy
> 0) {
987 if (n
->l4_proto
!= protocols
[i
])
989 if (presenter_flow_wrong_state(n
, j
))
991 if (presenter_get_port(n
->port_src
,
992 n
->port_dst
, 0) == 53)
995 n
= rcu_dereference(n
->next
);
998 if (skip_lines
> 0) {
999 n
= rcu_dereference(n
->next
);
1004 presenter_screen_do_line(screen
, n
, &line
);
1007 maxy
-= (2 + 1 * show_src
);
1008 n
= rcu_dereference(n
->next
);
1019 static inline void presenter_screen_end(void)
1024 static void presenter(void)
1027 WINDOW
*screen
= NULL
;
1029 dissector_init_ethernet(0);
1030 presenter_screen_init(&screen
);
1032 rcu_register_thread();
1049 if (skip_lines
> SCROLL_MAX
)
1050 skip_lines
= SCROLL_MAX
;
1057 presenter_screen_update(screen
, &flow_list
, skip_lines
);
1060 rcu_unregister_thread();
1062 presenter_screen_end();
1063 dissector_cleanup_ethernet();
1066 static inline int cb_test_bit(int nr
, const u_int32_t
*addr
)
1068 return ((1UL << (nr
& 31)) & (addr
[nr
>> 5])) != 0;
1071 static int collector_cb(enum nf_conntrack_msg_type type
,
1072 struct nf_conntrack
*ct
, void *data
)
1075 return NFCT_CB_STOP
;
1078 spinlock_lock(&flow_list
.lock
);
1082 flow_list_new_entry(&flow_list
, ct
);
1085 flow_list_update_entry(&flow_list
, ct
);
1087 case NFCT_T_DESTROY
:
1088 flow_list_destroy_entry(&flow_list
, ct
);
1094 spinlock_unlock(&flow_list
.lock
);
1096 return NFCT_CB_CONTINUE
;
1099 static inline GeoIP
*collector_geoip_open(const char *path
, int type
)
1102 return GeoIP_open(path
, GEOIP_MMAP_CACHE
);
1104 return GeoIP_open_type(type
, GEOIP_MMAP_CACHE
);
1107 static void collector_load_geoip(void)
1109 geo_country
.gi4
= collector_geoip_open(geo_country
.path4
,
1110 GEOIP_COUNTRY_EDITION
);
1111 if (geo_country
.gi4
== NULL
)
1112 panic("Cannot open GeoIP4 country database!\n");
1114 geo_country
.gi6
= collector_geoip_open(geo_country
.path6
,
1115 GEOIP_COUNTRY_EDITION_V6
);
1116 if (geo_country
.gi6
== NULL
)
1117 panic("Cannot open GeoIP6 country database!\n");
1119 geo_city
.gi4
= collector_geoip_open(geo_city
.path4
,
1120 GEOIP_CITY_EDITION_REV1
);
1121 if (geo_city
.gi4
== NULL
)
1122 panic("Cannot open GeoIP4 city database!\n");
1124 geo_city
.gi6
= collector_geoip_open(geo_city
.path6
,
1125 GEOIP_CITY_EDITION_REV1_V6
);
1126 if (geo_city
.gi6
== NULL
)
1127 panic("Cannot open GeoIP6 city database!\n");
1129 GeoIP_set_charset(geo_country
.gi4
, GEOIP_CHARSET_UTF8
);
1130 GeoIP_set_charset(geo_country
.gi6
, GEOIP_CHARSET_UTF8
);
1132 GeoIP_set_charset(geo_city
.gi4
, GEOIP_CHARSET_UTF8
);
1133 GeoIP_set_charset(geo_city
.gi6
, GEOIP_CHARSET_UTF8
);
1136 static void collector_destroy_geoip(void)
1138 GeoIP_delete(geo_country
.gi4
);
1139 GeoIP_delete(geo_country
.gi6
);
1141 GeoIP_delete(geo_city
.gi4
);
1142 GeoIP_delete(geo_city
.gi6
);
1145 static void inline collector_flush(struct nfct_handle
*handle
, uint8_t family
)
1147 nfct_query(handle
, NFCT_Q_FLUSH
, &family
);
1150 static void *collector(void *null
)
1153 struct nfct_handle
*handle
;
1154 struct nfct_filter
*filter
;
1156 handle
= nfct_open(CONNTRACK
, NF_NETLINK_CONNTRACK_NEW
|
1157 NF_NETLINK_CONNTRACK_UPDATE
|
1158 NF_NETLINK_CONNTRACK_DESTROY
);
1160 panic("Cannot create a nfct handle!\n");
1162 collector_flush(handle
, AF_INET
);
1163 collector_flush(handle
, AF_INET6
);
1165 filter
= nfct_filter_create();
1167 panic("Cannot create a nfct filter!\n");
1169 ret
= nfct_filter_attach(nfct_fd(handle
), filter
);
1171 panic("Cannot attach filter to handle!\n");
1173 if (what
& INCLUDE_UDP
) {
1174 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_UDP
);
1175 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_UDPLITE
);
1177 if (what
& INCLUDE_TCP
)
1178 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_TCP
);
1179 if (what
& INCLUDE_DCCP
)
1180 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_DCCP
);
1181 if (what
& INCLUDE_SCTP
)
1182 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_SCTP
);
1183 if (what
& INCLUDE_ICMP
&& what
& INCLUDE_IPV4
)
1184 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_ICMP
);
1185 if (what
& INCLUDE_ICMP
&& what
& INCLUDE_IPV6
)
1186 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_ICMPV6
);
1187 if (what
& INCLUDE_IPV4
) {
1188 nfct_filter_set_logic(filter
, NFCT_FILTER_SRC_IPV4
,
1189 NFCT_FILTER_LOGIC_NEGATIVE
);
1190 nfct_filter_add_attr(filter
, NFCT_FILTER_SRC_IPV4
, &filter_ipv4
);
1192 if (what
& INCLUDE_IPV6
) {
1193 nfct_filter_set_logic(filter
, NFCT_FILTER_SRC_IPV6
,
1194 NFCT_FILTER_LOGIC_NEGATIVE
);
1195 nfct_filter_add_attr(filter
, NFCT_FILTER_SRC_IPV6
, &filter_ipv6
);
1198 ret
= nfct_filter_attach(nfct_fd(handle
), filter
);
1200 panic("Cannot attach filter to handle!\n");
1202 nfct_callback_register(handle
, NFCT_T_ALL
, collector_cb
, NULL
);
1204 nfct_filter_destroy(filter
);
1206 collector_load_geoip();
1208 flow_list_init(&flow_list
);
1210 rcu_register_thread();
1212 while (!sigint
&& ret
>= 0)
1213 ret
= nfct_catch(handle
);
1215 rcu_unregister_thread();
1217 flow_list_destroy(&flow_list
);
1219 collector_destroy_geoip();
1226 int main(int argc
, char **argv
)
1229 int ret
, c
, opt_index
, what_cmd
= 0;
1231 memset(&geo_country
, 0, sizeof(geo_country
));
1232 memset(&geo_city
, 0, sizeof(geo_city
));
1234 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1235 &opt_index
)) != EOF
) {
1238 what_cmd
|= INCLUDE_IPV4
;
1241 what_cmd
|= INCLUDE_IPV6
;
1244 what_cmd
|= INCLUDE_TCP
;
1247 what_cmd
|= INCLUDE_UDP
;
1250 what_cmd
|= INCLUDE_DCCP
;
1253 what_cmd
|= INCLUDE_ICMP
;
1256 what_cmd
|= INCLUDE_SCTP
;
1262 geo_city
.path4
= xstrdup(optarg
);
1265 geo_country
.path4
= xstrdup(optarg
);
1268 geo_city
.path6
= xstrdup(optarg
);
1271 geo_country
.path6
= xstrdup(optarg
);
1285 panic("Option -%c requires an argument!\n",
1288 if (isprint(optopt
))
1289 whine("Unknown option character "
1290 "`0x%X\'!\n", optopt
);
1303 register_signal(SIGINT
, signal_handler
);
1304 register_signal(SIGHUP
, signal_handler
);
1306 ret
= pthread_create(&tid
, NULL
, collector
, NULL
);
1308 panic("Cannot create phthread!\n");
1312 free(geo_country
.path4
);
1313 free(geo_country
.path6
);
1315 free(geo_city
.path4
);
1316 free(geo_city
.path6
);