2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 - 2013 Daniel Borkmann.
4 * Copyright 2011 Emmanuel Roullit.
5 * Subject to the GPL, version 2.
18 #include <netinet/in.h>
21 #include <sys/fsuid.h>
26 #include <arpa/inet.h>
29 #include <urcu/list.h>
30 #include <urcu/rculist.h>
35 #include "conntrack.h"
48 #define NSEC_PER_SEC 1000000000L
52 #define USEC_PER_SEC 1000000L
56 struct cds_list_head entry
;
57 struct cds_list_head flows
;
60 struct timeval last_update
;
63 uint64_t pkts_src
, bytes_src
;
64 uint64_t pkts_dst
, bytes_dst
;
65 double rate_bytes_src
;
66 double rate_bytes_dst
;
73 struct cds_list_head proc_head
;
74 struct cds_list_head entry
;
77 uint32_t flow_id
, use
, status
;
78 uint8_t l3_proto
, l4_proto
;
79 uint32_t ip4_src_addr
, ip4_dst_addr
;
80 uint32_t ip6_src_addr
[4], ip6_dst_addr
[4];
81 uint16_t port_src
, port_dst
;
82 uint8_t tcp_state
, tcp_flags
, sctp_state
, dccp_state
;
83 uint64_t pkts_src
, bytes_src
;
84 uint64_t pkts_dst
, bytes_dst
;
85 uint64_t timestamp_start
, timestamp_stop
;
86 char country_src
[128], country_dst
[128];
87 char country_code_src
[4], country_code_dst
[4];
88 char city_src
[128], city_dst
[128];
89 char rev_dns_src
[256], rev_dns_dst
[256];
90 struct proc_entry
*proc
;
93 struct nf_conntrack
*ct
;
94 struct timeval last_update
;
95 double rate_bytes_src
;
96 double rate_bytes_dst
;
102 struct cds_list_head head
;
106 struct cds_list_head head
;
109 enum flow_direction
{
114 #ifndef ATTR_TIMESTAMP_START
115 # define ATTR_TIMESTAMP_START 63
117 #ifndef ATTR_TIMESTAMP_STOP
118 # define ATTR_TIMESTAMP_STOP 64
121 #define SCROLL_MAX 1000
123 #define INCLUDE_IPV4 (1 << 0)
124 #define INCLUDE_IPV6 (1 << 1)
125 #define INCLUDE_UDP (1 << 2)
126 #define INCLUDE_TCP (1 << 3)
127 #define INCLUDE_DCCP (1 << 4)
128 #define INCLUDE_ICMP (1 << 5)
129 #define INCLUDE_SCTP (1 << 6)
131 #define TOGGLE_FLAG(what, flag) \
139 struct sysctl_params_ctx
{
149 static volatile bool do_reload_flows
;
150 static volatile bool is_flow_collecting
;
151 static volatile sig_atomic_t sigint
= 0;
152 static int what
= INCLUDE_IPV4
| INCLUDE_IPV6
| INCLUDE_TCP
;
153 static struct proc_list proc_list
;
154 static struct flow_list flow_list
;
155 static struct sysctl_params_ctx sysctl
= { -1, -1 };
157 static unsigned int cols
, rows
;
158 static WINDOW
*screen
;
159 static int skip_lines
;
161 static unsigned int interval
= 1;
162 static bool show_src
= false;
163 static bool resolve_dns
= true;
164 static bool resolve_geoip
= true;
165 static enum rate_units rate_type
= RATE_BYTES
;
166 static bool show_active_only
= false;
191 static struct ui_table flows_tbl
;
192 static struct ui_table procs_tbl
;
199 static const char *short_options
= "vhTUsDIS46ut:nGb";
200 static const struct option long_options
[] = {
201 {"ipv4", no_argument
, NULL
, '4'},
202 {"ipv6", no_argument
, NULL
, '6'},
203 {"tcp", no_argument
, NULL
, 'T'},
204 {"udp", no_argument
, NULL
, 'U'},
205 {"dccp", no_argument
, NULL
, 'D'},
206 {"icmp", no_argument
, NULL
, 'I'},
207 {"sctp", no_argument
, NULL
, 'S'},
208 {"no-dns", no_argument
, NULL
, 'n'},
209 {"no-geoip", no_argument
, NULL
, 'G'},
210 {"show-src", no_argument
, NULL
, 's'},
211 {"bits", no_argument
, NULL
, 'b'},
212 {"update", no_argument
, NULL
, 'u'},
213 {"interval", required_argument
, NULL
, 't'},
214 {"version", no_argument
, NULL
, 'v'},
215 {"help", no_argument
, NULL
, 'h'},
219 static const char *copyright
= "Please report bugs to <netsniff-ng@googlegroups.com>\n"
220 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
221 "Copyright (C) 2011-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
222 "Swiss federal institute of technology (ETH Zurich)\n"
223 "License: GNU GPL version 2.0\n"
224 "This is free software: you are free to change and redistribute it.\n"
225 "There is NO WARRANTY, to the extent permitted by law.";
227 static const char *const l4proto2str
[IPPROTO_MAX
] = {
228 [IPPROTO_TCP
] = "tcp",
229 [IPPROTO_UDP
] = "udp",
230 [IPPROTO_UDPLITE
] = "udplite",
231 [IPPROTO_ICMP
] = "icmp",
232 [IPPROTO_ICMPV6
] = "icmpv6",
233 [IPPROTO_SCTP
] = "sctp",
234 [IPPROTO_GRE
] = "gre",
235 [IPPROTO_DCCP
] = "dccp",
236 [IPPROTO_IGMP
] = "igmp",
237 [IPPROTO_IPIP
] = "ipip",
238 [IPPROTO_EGP
] = "egp",
239 [IPPROTO_PUP
] = "pup",
240 [IPPROTO_IDP
] = "idp",
241 [IPPROTO_RSVP
] = "rsvp",
242 [IPPROTO_IPV6
] = "ip6tun",
243 [IPPROTO_ESP
] = "esp",
245 [IPPROTO_PIM
] = "pim",
246 [IPPROTO_COMP
] = "comp",
249 static const char *const tcp_state2str
[TCP_CONNTRACK_MAX
] = {
250 [TCP_CONNTRACK_NONE
] = "NONE",
251 [TCP_CONNTRACK_SYN_SENT
] = "SYN-SENT",
252 [TCP_CONNTRACK_SYN_RECV
] = "SYN-RECV",
253 [TCP_CONNTRACK_ESTABLISHED
] = "ESTABLISHED",
254 [TCP_CONNTRACK_FIN_WAIT
] = "FIN-WAIT",
255 [TCP_CONNTRACK_CLOSE_WAIT
] = "CLOSE-WAIT",
256 [TCP_CONNTRACK_LAST_ACK
] = "LAST-ACK",
257 [TCP_CONNTRACK_TIME_WAIT
] = "TIME-WAIT",
258 [TCP_CONNTRACK_CLOSE
] = "CLOSE",
259 [TCP_CONNTRACK_SYN_SENT2
] = "SYN-SENT2",
262 static const char *const dccp_state2str
[DCCP_CONNTRACK_MAX
] = {
263 [DCCP_CONNTRACK_NONE
] = "NONE",
264 [DCCP_CONNTRACK_REQUEST
] = "REQUEST",
265 [DCCP_CONNTRACK_RESPOND
] = "RESPOND",
266 [DCCP_CONNTRACK_PARTOPEN
] = "PARTOPEN",
267 [DCCP_CONNTRACK_OPEN
] = "OPEN",
268 [DCCP_CONNTRACK_CLOSEREQ
] = "CLOSE-REQ",
269 [DCCP_CONNTRACK_CLOSING
] = "CLOSING",
270 [DCCP_CONNTRACK_TIMEWAIT
] = "TIME-WAIT",
271 [DCCP_CONNTRACK_IGNORE
] = "IGNORE",
272 [DCCP_CONNTRACK_INVALID
] = "INVALID",
275 static const char *const sctp_state2str
[SCTP_CONNTRACK_MAX
] = {
276 [SCTP_CONNTRACK_NONE
] = "NONE",
277 [SCTP_CONNTRACK_CLOSED
] = "CLOSED",
278 [SCTP_CONNTRACK_COOKIE_WAIT
] = "COOKIE-WAIT",
279 [SCTP_CONNTRACK_COOKIE_ECHOED
] = "COOKIE-ECHO",
280 [SCTP_CONNTRACK_ESTABLISHED
] = "ESTABLISHED",
281 [SCTP_CONNTRACK_SHUTDOWN_SENT
] = "SHUTD-SENT",
282 [SCTP_CONNTRACK_SHUTDOWN_RECD
] = "SHUTD-RCVD",
283 [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
] = "SHUTD-ACK",
286 static const struct nfct_filter_ipv4 filter_ipv4
= {
287 .addr
= __constant_htonl(INADDR_LOOPBACK
),
291 static const struct nfct_filter_ipv6 filter_ipv6
= {
292 .addr
= { 0x0, 0x0, 0x0, 0x1 },
293 .mask
= { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff },
296 static int64_t time_after_us(struct timeval
*tv
)
300 bug_on(gettimeofday(&now
, NULL
));
302 now
.tv_sec
-= tv
->tv_sec
;
303 now
.tv_usec
-= tv
->tv_usec
;
305 return now
.tv_sec
* USEC_PER_SEC
+ now
.tv_usec
;
308 static void signal_handler(int number
)
322 static void flow_entry_from_ct(struct flow_entry
*n
, const struct nf_conntrack
*ct
);
323 static void flow_entry_get_extended(struct flow_entry
*n
);
325 static void help(void)
327 printf("flowtop %s, top-like netfilter TCP/UDP/SCTP/.. flow tracking\n",
329 puts("http://www.netsniff-ng.org\n\n"
330 "Usage: flowtop [options]\n"
332 " -4|--ipv4 Show only IPv4 flows (default)\n"
333 " -6|--ipv6 Show only IPv6 flows (default)\n"
334 " -T|--tcp Show only TCP flows (default)\n"
335 " -U|--udp Show only UDP flows\n"
336 " -D|--dccp Show only DCCP flows\n"
337 " -I|--icmp Show only ICMP/ICMPv6 flows\n"
338 " -S|--sctp Show only SCTP flows\n"
339 " -n|--no-dns Don't perform hostname lookup\n"
340 " -G|--no-geoip Don't perform GeoIP lookup\n"
341 " -s|--show-src Also show source, not only dest\n"
342 " -b|--bits Show rates in bits/s instead of bytes/s\n"
343 " -u|--update Update GeoIP databases\n"
344 " -t|--interval <time> Refresh time in seconds (default 1s)\n"
345 " -v|--version Print version and exit\n"
346 " -h|--help Print this help and exit\n\n"
349 " flowtop -46UTDISs\n\n"
351 " If netfilter is not running, you can activate it with e.g.:\n"
352 " iptables -A INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT\n"
353 " iptables -A OUTPUT -p tcp -m state --state NEW,ESTABLISHED -j ACCEPT\n");
358 static void version(void)
360 printf("flowtop %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
361 puts("top-like netfilter TCP/UDP/SCTP/.. flow tracking\n"
362 "http://www.netsniff-ng.org\n");
367 static void flow_entry_update_time(struct flow_entry
*n
)
369 bug_on(gettimeofday(&n
->last_update
, NULL
));
372 #define CALC_RATE(fld) do { \
373 n->rate_##fld = (((fld) > n->fld) ? (((fld) - n->fld) / sec) : 0); \
376 static void flow_entry_calc_rate(struct flow_entry
*n
, const struct nf_conntrack
*ct
)
378 uint64_t bytes_src
= nfct_get_attr_u64(ct
, ATTR_ORIG_COUNTER_BYTES
);
379 uint64_t bytes_dst
= nfct_get_attr_u64(ct
, ATTR_REPL_COUNTER_BYTES
);
380 uint64_t pkts_src
= nfct_get_attr_u64(ct
, ATTR_ORIG_COUNTER_PACKETS
);
381 uint64_t pkts_dst
= nfct_get_attr_u64(ct
, ATTR_REPL_COUNTER_PACKETS
);
382 double sec
= (double)time_after_us(&n
->last_update
) / USEC_PER_SEC
;
387 CALC_RATE(bytes_src
);
388 CALC_RATE(bytes_dst
);
393 static inline struct flow_entry
*flow_entry_xalloc(void)
395 return xzmalloc(sizeof(struct flow_entry
));
398 static inline void flow_entry_xfree(struct flow_entry
*n
)
406 static void flow_entry_xfree_rcu(struct rcu_head
*head
)
408 struct flow_entry
*n
= container_of(head
, struct flow_entry
, rcu
);
413 static inline void flow_list_init(struct flow_list
*fl
)
415 CDS_INIT_LIST_HEAD(&fl
->head
);
418 static inline bool nfct_is_dns(const struct nf_conntrack
*ct
)
420 uint16_t port_src
= nfct_get_attr_u16(ct
, ATTR_ORIG_PORT_SRC
);
421 uint16_t port_dst
= nfct_get_attr_u16(ct
, ATTR_ORIG_PORT_DST
);
423 return ntohs(port_src
) == 53 || ntohs(port_dst
) == 53;
426 static int flow_list_new_entry(struct flow_list
*fl
, struct nf_conntrack
*ct
)
428 struct flow_entry
*n
;
430 /* We don't want to analyze / display DNS itself, since we
431 * use it to resolve reverse dns.
434 return NFCT_CB_CONTINUE
;
436 n
= flow_entry_xalloc();
440 flow_entry_update_time(n
);
441 flow_entry_from_ct(n
, ct
);
442 flow_entry_get_extended(n
);
444 cds_list_add_rcu(&n
->entry
, &fl
->head
);
446 n
->is_visible
= true;
448 return NFCT_CB_STOLEN
;
451 static struct flow_entry
*flow_list_find_id(struct flow_list
*fl
, uint32_t id
)
453 struct flow_entry
*n
;
455 cds_list_for_each_entry_rcu(n
, &fl
->head
, entry
) {
456 if (n
->flow_id
== id
)
463 static int flow_list_del_entry(struct flow_list
*fl
, const struct nf_conntrack
*ct
)
465 struct flow_entry
*n
;
467 n
= flow_list_find_id(fl
, nfct_get_attr_u32(ct
, ATTR_ID
));
470 cds_list_del(&n
->proc_head
);
471 n
->proc
->flows_count
--;
474 cds_list_del_rcu(&n
->entry
);
475 call_rcu(&n
->rcu
, flow_entry_xfree_rcu
);
478 return NFCT_CB_CONTINUE
;
481 static void flow_list_destroy(struct flow_list
*fl
)
483 struct flow_entry
*n
, *tmp
;
485 cds_list_for_each_entry_safe(n
, tmp
, &fl
->head
, entry
) {
486 cds_list_del_rcu(&n
->entry
);
487 call_rcu(&n
->rcu
, flow_entry_xfree_rcu
);
491 static void proc_list_init(struct proc_list
*proc_list
)
493 CDS_INIT_LIST_HEAD(&proc_list
->head
);
496 static struct proc_entry
*proc_list_new_entry(unsigned int pid
)
498 struct proc_entry
*proc
;
500 cds_list_for_each_entry(proc
, &proc_list
.head
, entry
) {
501 if (proc
->pid
&& proc
->pid
== pid
)
505 proc
= xzmalloc(sizeof(*proc
));
507 bug_on(gettimeofday(&proc
->last_update
, NULL
));
508 CDS_INIT_LIST_HEAD(&proc
->flows
);
511 cds_list_add_tail(&proc
->entry
, &proc_list
.head
);
516 static void proc_entry_xfree_rcu(struct rcu_head
*head
)
518 struct proc_entry
*p
= container_of(head
, struct proc_entry
, rcu
);
523 static void proc_list_destroy(struct proc_list
*pl
)
525 struct proc_entry
*p
, *tmp
;
527 cds_list_for_each_entry_safe(p
, tmp
, &pl
->head
, entry
) {
528 cds_list_del_rcu(&p
->entry
);
529 call_rcu(&p
->rcu
, proc_entry_xfree_rcu
);
533 static void flow_entry_find_process(struct flow_entry
*n
)
535 struct proc_entry
*p
;
540 ret
= proc_find_by_inode(n
->inode
, cmdline
, sizeof(cmdline
), &pid
);
544 p
= proc_list_new_entry(pid
);
546 if (snprintf(p
->name
, sizeof(p
->name
), "%s", basename(cmdline
)) < 0)
549 p
->pkts_src
+= n
->pkts_src
;
550 p
->pkts_dst
+= n
->pkts_dst
;
551 p
->bytes_src
+= n
->bytes_src
;
552 p
->bytes_dst
+= n
->bytes_dst
;
555 cds_list_add(&n
->proc_head
, &p
->flows
);
559 static int get_port_inode(uint16_t port
, int proto
, bool is_ip6
)
562 char path
[128], buff
[1024];
565 memset(path
, 0, sizeof(path
));
566 snprintf(path
, sizeof(path
), "/proc/net/%s%s",
567 l4proto2str
[proto
], is_ip6
? "6" : "");
569 proc
= fopen(path
, "r");
573 memset(buff
, 0, sizeof(buff
));
575 while (fgets(buff
, sizeof(buff
), proc
) != NULL
) {
577 unsigned int lport
= 0;
579 buff
[sizeof(buff
) - 1] = 0;
580 if (sscanf(buff
, "%*u: %*X:%X %*X:%*X %*X %*X:%*X %*X:%*X "
581 "%*X %*u %*u %u", &lport
, &inode
) == 2) {
582 if ((uint16_t) lport
== port
) {
588 memset(buff
, 0, sizeof(buff
));
595 #define CP_NFCT(elem, attr, x) \
596 do { n->elem = nfct_get_attr_u##x(ct,(attr)); } while (0)
597 #define CP_NFCT_BUFF(elem, attr) do { \
598 const uint8_t *buff = nfct_get_attr(ct,(attr)); \
600 memcpy(n->elem, buff, sizeof(n->elem)); \
603 static void flow_entry_from_ct(struct flow_entry
*n
, const struct nf_conntrack
*ct
)
605 uint64_t bytes_src
= nfct_get_attr_u64(ct
, ATTR_ORIG_COUNTER_BYTES
);
606 uint64_t bytes_dst
= nfct_get_attr_u64(ct
, ATTR_REPL_COUNTER_BYTES
);
607 uint64_t pkts_src
= nfct_get_attr_u64(ct
, ATTR_ORIG_COUNTER_PACKETS
);
608 uint64_t pkts_dst
= nfct_get_attr_u64(ct
, ATTR_REPL_COUNTER_PACKETS
);
610 /* Update stats diff to the related process entry */
612 n
->proc
->pkts_src
+= pkts_src
- n
->pkts_src
;
613 n
->proc
->pkts_dst
+= pkts_dst
- n
->pkts_dst
;
614 n
->proc
->bytes_src
+= bytes_src
- n
->bytes_src
;
615 n
->proc
->bytes_dst
+= bytes_dst
- n
->bytes_dst
;
618 CP_NFCT(l3_proto
, ATTR_ORIG_L3PROTO
, 8);
619 CP_NFCT(l4_proto
, ATTR_ORIG_L4PROTO
, 8);
621 CP_NFCT(ip4_src_addr
, ATTR_ORIG_IPV4_SRC
, 32);
622 CP_NFCT(ip4_dst_addr
, ATTR_ORIG_IPV4_DST
, 32);
624 CP_NFCT(port_src
, ATTR_ORIG_PORT_SRC
, 16);
625 CP_NFCT(port_dst
, ATTR_ORIG_PORT_DST
, 16);
627 CP_NFCT(status
, ATTR_STATUS
, 32);
629 CP_NFCT(tcp_state
, ATTR_TCP_STATE
, 8);
630 CP_NFCT(tcp_flags
, ATTR_TCP_FLAGS_ORIG
, 8);
631 CP_NFCT(sctp_state
, ATTR_SCTP_STATE
, 8);
632 CP_NFCT(dccp_state
, ATTR_DCCP_STATE
, 8);
634 CP_NFCT(pkts_src
, ATTR_ORIG_COUNTER_PACKETS
, 64);
635 CP_NFCT(bytes_src
, ATTR_ORIG_COUNTER_BYTES
, 64);
637 CP_NFCT(pkts_dst
, ATTR_REPL_COUNTER_PACKETS
, 64);
638 CP_NFCT(bytes_dst
, ATTR_REPL_COUNTER_BYTES
, 64);
640 CP_NFCT(timestamp_start
, ATTR_TIMESTAMP_START
, 64);
641 CP_NFCT(timestamp_stop
, ATTR_TIMESTAMP_STOP
, 64);
643 CP_NFCT(flow_id
, ATTR_ID
, 32);
644 CP_NFCT(use
, ATTR_USE
, 32);
646 CP_NFCT_BUFF(ip6_src_addr
, ATTR_ORIG_IPV6_SRC
);
647 CP_NFCT_BUFF(ip6_dst_addr
, ATTR_ORIG_IPV6_DST
);
649 n
->port_src
= ntohs(n
->port_src
);
650 n
->port_dst
= ntohs(n
->port_dst
);
652 n
->ip4_src_addr
= ntohl(n
->ip4_src_addr
);
653 n
->ip4_dst_addr
= ntohl(n
->ip4_dst_addr
);
656 #define SELFLD(dir,src_member,dst_member) \
657 (((dir) == FLOW_DIR_SRC) ? n->src_member : n->dst_member)
659 static void flow_entry_get_sain4_obj(const struct flow_entry
*n
,
660 enum flow_direction dir
,
661 struct sockaddr_in
*sa
)
663 memset(sa
, 0, sizeof(*sa
));
664 sa
->sin_family
= PF_INET
;
665 sa
->sin_addr
.s_addr
= htonl(SELFLD(dir
, ip4_src_addr
, ip4_dst_addr
));
668 static void flow_entry_get_sain6_obj(const struct flow_entry
*n
,
669 enum flow_direction dir
,
670 struct sockaddr_in6
*sa
)
672 memset(sa
, 0, sizeof(*sa
));
673 sa
->sin6_family
= PF_INET6
;
675 memcpy(&sa
->sin6_addr
, SELFLD(dir
, ip6_src_addr
, ip6_dst_addr
),
676 sizeof(sa
->sin6_addr
));
680 flow_entry_geo_city_lookup_generic(struct flow_entry
*n
,
681 enum flow_direction dir
)
683 struct sockaddr_in sa4
;
684 struct sockaddr_in6 sa6
;
685 const char *city
= NULL
;
687 switch (n
->l3_proto
) {
692 flow_entry_get_sain4_obj(n
, dir
, &sa4
);
693 city
= geoip4_city_name(&sa4
);
697 flow_entry_get_sain6_obj(n
, dir
, &sa6
);
698 city
= geoip6_city_name(&sa6
);
702 build_bug_on(sizeof(n
->city_src
) != sizeof(n
->city_dst
));
705 strlcpy(SELFLD(dir
, city_src
, city_dst
), city
,
706 sizeof(n
->city_src
));
708 SELFLD(dir
, city_src
, city_dst
)[0] = '\0';
712 flow_entry_geo_country_lookup_generic(struct flow_entry
*n
,
713 enum flow_direction dir
)
715 struct sockaddr_in sa4
;
716 struct sockaddr_in6 sa6
;
717 const char *country
= NULL
;
718 const char *country_code
= NULL
;
720 switch (n
->l3_proto
) {
725 flow_entry_get_sain4_obj(n
, dir
, &sa4
);
726 country
= geoip4_country_name(&sa4
);
727 country_code
= geoip4_country_code3_name(&sa4
);
731 flow_entry_get_sain6_obj(n
, dir
, &sa6
);
732 country
= geoip6_country_name(&sa6
);
733 country_code
= geoip6_country_code3_name(&sa6
);
737 build_bug_on(sizeof(n
->country_src
) != sizeof(n
->country_dst
));
740 strlcpy(SELFLD(dir
, country_src
, country_dst
), country
,
741 sizeof(n
->country_src
));
743 SELFLD(dir
, country_src
, country_dst
)[0] = '\0';
745 build_bug_on(sizeof(n
->country_code_src
) != sizeof(n
->country_code_dst
));
748 strlcpy(SELFLD(dir
, country_code_src
, country_code_dst
),
749 country_code
, sizeof(n
->country_code_src
));
751 SELFLD(dir
, country_code_src
, country_code_dst
)[0] = '\0';
754 static void flow_entry_get_extended_geo(struct flow_entry
*n
,
755 enum flow_direction dir
)
758 flow_entry_geo_city_lookup_generic(n
, dir
);
759 flow_entry_geo_country_lookup_generic(n
, dir
);
763 static void flow_entry_get_extended_revdns(struct flow_entry
*n
,
764 enum flow_direction dir
)
767 struct sockaddr_in sa4
;
768 struct sockaddr_in6 sa6
;
770 struct hostent
*hent
;
772 build_bug_on(sizeof(n
->rev_dns_src
) != sizeof(n
->rev_dns_dst
));
774 switch (n
->l3_proto
) {
779 flow_entry_get_sain4_obj(n
, dir
, &sa4
);
782 inet_ntop(AF_INET
, &sa4
.sin_addr
,
783 SELFLD(dir
, rev_dns_src
, rev_dns_dst
),
784 sizeof(n
->rev_dns_src
));
788 sa
= (struct sockaddr
*) &sa4
;
789 sa_len
= sizeof(sa4
);
790 hent
= gethostbyaddr(&sa4
.sin_addr
, sizeof(sa4
.sin_addr
), AF_INET
);
794 flow_entry_get_sain6_obj(n
, dir
, &sa6
);
797 inet_ntop(AF_INET6
, &sa6
.sin6_addr
,
798 SELFLD(dir
, rev_dns_src
, rev_dns_dst
),
799 sizeof(n
->rev_dns_src
));
803 sa
= (struct sockaddr
*) &sa6
;
804 sa_len
= sizeof(sa6
);
805 hent
= gethostbyaddr(&sa6
.sin6_addr
, sizeof(sa6
.sin6_addr
), AF_INET6
);
809 getnameinfo(sa
, sa_len
, SELFLD(dir
, rev_dns_src
, rev_dns_dst
),
810 sizeof(n
->rev_dns_src
), NULL
, 0, NI_NUMERICHOST
);
813 strlcpy(SELFLD(dir
, rev_dns_src
, rev_dns_dst
), hent
->h_name
,
814 sizeof(n
->rev_dns_src
));
817 static void flow_entry_get_extended(struct flow_entry
*n
)
822 flow_entry_get_extended_revdns(n
, FLOW_DIR_SRC
);
823 flow_entry_get_extended_geo(n
, FLOW_DIR_SRC
);
825 flow_entry_get_extended_revdns(n
, FLOW_DIR_DST
);
826 flow_entry_get_extended_geo(n
, FLOW_DIR_DST
);
828 /* Lookup application */
829 n
->inode
= get_port_inode(n
->port_src
, n
->l4_proto
,
830 n
->l3_proto
== AF_INET6
);
832 flow_entry_find_process(n
);
835 static char *bandw2str(double bytes
, char *buf
, size_t len
)
842 if (bytes
> 1000000000.)
843 snprintf(buf
, len
, "%.1fGB", bytes
/ 1000000000.);
844 else if (bytes
> 1000000.)
845 snprintf(buf
, len
, "%.1fMB", bytes
/ 1000000.);
846 else if (bytes
> 1000.)
847 snprintf(buf
, len
, "%.1fkB", bytes
/ 1000.);
849 snprintf(buf
, len
, "%.0f", bytes
);
854 static char *rate2str(double rate
, char *buf
, size_t len
)
856 const char * const unit_fmt
[2][4] = {
857 { "%.1fGbit/s", "%.1fMbit/s", "%.1fkbit/s", "%.0fbit/s" },
858 { "%.1fGB/s", "%.1fMB/s", "%.1fkB/s", "%.0fB/s" }
866 if (rate_type
== RATE_BITS
)
869 if (rate
> 1000000000.)
870 snprintf(buf
, len
, unit_fmt
[rate_type
][0], rate
/ 1000000000.);
871 else if (rate
> 1000000.)
872 snprintf(buf
, len
, unit_fmt
[rate_type
][1], rate
/ 1000000.);
873 else if (rate
> 1000.)
874 snprintf(buf
, len
, unit_fmt
[rate_type
][2], rate
/ 1000.);
876 snprintf(buf
, len
, unit_fmt
[rate_type
][3], rate
);
881 static char *time2str(uint64_t tstamp
, char *str
, size_t len
)
888 s
= now
- (tstamp
? (tstamp
/ NSEC_PER_SEC
) : now
);
896 slprintf(str
, len
, "%dd", v
);
902 slprintf(str
, len
, "%dh", v
);
908 slprintf(str
, len
, "%dm", v
);
912 slprintf(str
, len
, "%ds", s
);
917 static const char *flow_state2str(const struct flow_entry
*n
)
919 switch (n
->l4_proto
) {
921 return tcp_state2str
[n
->tcp_state
];
923 return sctp_state2str
[n
->sctp_state
];
925 return dccp_state2str
[n
->dccp_state
];
928 case IPPROTO_UDPLITE
:
936 static char *flow_port2str(const struct flow_entry
*n
, char *str
, size_t len
,
937 enum flow_direction dir
)
939 const char *tmp
= NULL
;
942 port
= SELFLD(dir
, port_src
, port_dst
);
945 switch (n
->l4_proto
) {
947 tmp
= lookup_port_tcp(port
);
950 case IPPROTO_UDPLITE
:
951 tmp
= lookup_port_udp(port
);
956 slprintf(str
, len
, "%d", port
);
958 slprintf(str
, len
, "%s", tmp
? tmp
: "");
963 static void print_flow_peer_info(const struct flow_entry
*n
, enum flow_direction dir
)
965 int counters_color
= COLOR(YELLOW
, BLACK
);
966 int src_color
= COLOR(RED
, BLACK
);
967 int dst_color
= COLOR(BLUE
, BLACK
);
968 int country_color
= COLOR(GREEN
, BLACK
);
969 int addr_color
= dst_color
;
970 int port_color
= A_BOLD
;
973 if (show_src
&& dir
== FLOW_DIR_SRC
) {
974 country_color
= src_color
;
975 counters_color
= src_color
;
976 port_color
|= src_color
;
977 addr_color
= src_color
;
978 } else if (show_src
&& FLOW_DIR_DST
) {
979 country_color
= dst_color
;
980 counters_color
= dst_color
;
981 port_color
|= dst_color
;
982 addr_color
= dst_color
;
985 ui_table_col_color_set(&flows_tbl
, TBL_FLOW_ADDRESS
, addr_color
);
986 ui_table_col_color_set(&flows_tbl
, TBL_FLOW_PORT
, port_color
);
987 ui_table_col_color_set(&flows_tbl
, TBL_FLOW_GEO
, country_color
);
988 ui_table_col_color_set(&flows_tbl
, TBL_FLOW_BYTES
, counters_color
);
989 ui_table_col_color_set(&flows_tbl
, TBL_FLOW_RATE
, counters_color
);
992 ui_table_row_col_set(&flows_tbl
, TBL_FLOW_ADDRESS
,
993 SELFLD(dir
, rev_dns_src
, rev_dns_dst
));
995 /* Application port */
996 ui_table_row_col_set(&flows_tbl
, TBL_FLOW_PORT
,
997 flow_port2str(n
, tmp
, sizeof(tmp
), dir
));
1000 ui_table_row_col_set(&flows_tbl
, TBL_FLOW_GEO
,
1001 SELFLD(dir
, country_code_src
, country_code_dst
));
1004 ui_table_row_col_set(&flows_tbl
, TBL_FLOW_BYTES
,
1005 bandw2str(SELFLD(dir
, bytes_src
, bytes_dst
),
1006 tmp
, sizeof(tmp
) - 1));
1009 ui_table_row_col_set(&flows_tbl
, TBL_FLOW_RATE
,
1010 rate2str(SELFLD(dir
, rate_bytes_src
, rate_bytes_dst
),
1011 tmp
, sizeof(tmp
) - 1));
1014 static void draw_flow_entry(const struct flow_entry
*n
)
1018 ui_table_row_add(&flows_tbl
);
1021 ui_table_row_col_set(&flows_tbl
, TBL_FLOW_PROCESS
,
1022 n
->proc
? n
->proc
->name
: "");
1025 slprintf(tmp
, sizeof(tmp
), "%.d", n
->proc
? n
->proc
->pid
: 0);
1026 ui_table_row_col_set(&flows_tbl
, TBL_FLOW_PID
, tmp
);
1029 ui_table_row_col_set(&flows_tbl
, TBL_FLOW_PROTO
, l4proto2str
[n
->l4_proto
]);
1031 /* L4 protocol state */
1032 ui_table_row_col_set(&flows_tbl
, TBL_FLOW_STATE
, flow_state2str(n
));
1035 time2str(n
->timestamp_start
, tmp
, sizeof(tmp
));
1036 ui_table_row_col_set(&flows_tbl
, TBL_FLOW_TIME
, tmp
);
1038 print_flow_peer_info(n
, show_src
? FLOW_DIR_SRC
: FLOW_DIR_DST
);
1040 ui_table_row_show(&flows_tbl
);
1043 ui_table_row_add(&flows_tbl
);
1045 ui_table_row_col_set(&flows_tbl
, TBL_FLOW_PROCESS
, "");
1046 ui_table_row_col_set(&flows_tbl
, TBL_FLOW_PID
, "");
1047 ui_table_row_col_set(&flows_tbl
, TBL_FLOW_PROTO
, "");
1048 ui_table_row_col_set(&flows_tbl
, TBL_FLOW_STATE
, "");
1049 ui_table_row_col_set(&flows_tbl
, TBL_FLOW_TIME
, "");
1051 print_flow_peer_info(n
, FLOW_DIR_DST
);
1052 ui_table_row_show(&flows_tbl
);
1056 static inline bool presenter_flow_wrong_state(struct flow_entry
*n
)
1058 switch (n
->l4_proto
) {
1060 switch (n
->tcp_state
) {
1061 case TCP_CONNTRACK_SYN_SENT
:
1062 case TCP_CONNTRACK_SYN_RECV
:
1063 case TCP_CONNTRACK_ESTABLISHED
:
1064 case TCP_CONNTRACK_FIN_WAIT
:
1065 case TCP_CONNTRACK_CLOSE_WAIT
:
1066 case TCP_CONNTRACK_LAST_ACK
:
1067 case TCP_CONNTRACK_TIME_WAIT
:
1068 case TCP_CONNTRACK_CLOSE
:
1069 case TCP_CONNTRACK_SYN_SENT2
:
1070 case TCP_CONNTRACK_NONE
:
1076 switch (n
->sctp_state
) {
1077 case SCTP_CONNTRACK_NONE
:
1078 case SCTP_CONNTRACK_CLOSED
:
1079 case SCTP_CONNTRACK_COOKIE_WAIT
:
1080 case SCTP_CONNTRACK_COOKIE_ECHOED
:
1081 case SCTP_CONNTRACK_ESTABLISHED
:
1082 case SCTP_CONNTRACK_SHUTDOWN_SENT
:
1083 case SCTP_CONNTRACK_SHUTDOWN_RECD
:
1084 case SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
:
1090 switch (n
->dccp_state
) {
1091 case DCCP_CONNTRACK_NONE
:
1092 case DCCP_CONNTRACK_REQUEST
:
1093 case DCCP_CONNTRACK_RESPOND
:
1094 case DCCP_CONNTRACK_PARTOPEN
:
1095 case DCCP_CONNTRACK_OPEN
:
1096 case DCCP_CONNTRACK_CLOSEREQ
:
1097 case DCCP_CONNTRACK_CLOSING
:
1098 case DCCP_CONNTRACK_TIMEWAIT
:
1099 case DCCP_CONNTRACK_IGNORE
:
1100 case DCCP_CONNTRACK_INVALID
:
1106 case IPPROTO_UDPLITE
:
1108 case IPPROTO_ICMPV6
:
1116 static void draw_filter_status(char *title
, int count
, int skip_lines
)
1118 mvwprintw(screen
, 1, 0, "%*s", COLS
- 1, " ");
1119 mvwprintw(screen
, 1, 2, "%s(%u) for ", title
, count
);
1121 if (what
& INCLUDE_IPV4
)
1123 if (what
& INCLUDE_IPV6
)
1125 if (what
& INCLUDE_TCP
)
1127 if (what
& INCLUDE_UDP
)
1129 if (what
& INCLUDE_SCTP
)
1131 if (what
& INCLUDE_DCCP
)
1133 if (what
& INCLUDE_ICMP
&& what
& INCLUDE_IPV4
)
1135 if (what
& INCLUDE_ICMP
&& what
& INCLUDE_IPV6
)
1137 if (show_active_only
)
1140 printw(" [+%d]", skip_lines
);
1142 if (is_flow_collecting
)
1143 printw(" [Collecting flows ...]");
1147 static void draw_flows(WINDOW
*screen
, struct flow_list
*fl
,
1150 int row_width
= show_src
? 2 : 1;
1151 unsigned int flows
= 0;
1152 unsigned int line
= 4;
1153 int skip
= skip_lines
;
1154 struct flow_entry
*n
;
1158 if (cds_list_empty(&fl
->head
))
1159 mvwprintw(screen
, line
, 2, "(No sessions! "
1160 "Is netfilter running?)");
1162 ui_table_clear(&flows_tbl
);
1163 ui_table_header_print(&flows_tbl
);
1165 cds_list_for_each_entry_rcu(n
, &fl
->head
, entry
) {
1168 if (presenter_flow_wrong_state(n
))
1171 /* count only flows which might be showed */
1174 if (line
+ row_width
>= rows
)
1185 mvwprintw(screen
, 1, 0, "%*s", COLS
- 1, " ");
1186 mvwprintw(screen
, 1, 2, "Kernel netfilter flows(%u) for ", flows
);
1188 draw_filter_status("Kernel netfilter flows", flows
, skip_lines
);
1191 static void draw_proc_entry(struct proc_entry
*p
)
1193 struct ui_table
*tbl
= &procs_tbl
;;
1196 ui_table_row_add(tbl
);
1199 ui_table_row_col_set(tbl
, TBL_PROC_NAME
, p
->name
);
1202 slprintf(tmp
, sizeof(tmp
), "%.d", p
->pid
);
1203 ui_table_row_col_set(tbl
, TBL_PROC_PID
, tmp
);
1206 slprintf(tmp
, sizeof(tmp
), "%.d", p
->flows_count
);
1207 ui_table_row_col_set(tbl
, TBL_PROC_FLOWS
, tmp
);
1210 bandw2str(p
->bytes_src
, tmp
, sizeof(tmp
) - 1);
1211 ui_table_row_col_set(tbl
, TBL_PROC_BYTES_SRC
, tmp
);
1214 rate2str(p
->rate_bytes_src
, tmp
, sizeof(tmp
) - 1);
1215 ui_table_row_col_set(tbl
, TBL_PROC_RATE_SRC
, tmp
);
1218 bandw2str(p
->bytes_dst
, tmp
, sizeof(tmp
) - 1);
1219 ui_table_row_col_set(tbl
, TBL_PROC_BYTES_DST
, tmp
);
1222 rate2str(p
->rate_bytes_dst
, tmp
, sizeof(tmp
) - 1);
1223 ui_table_row_col_set(tbl
, TBL_PROC_RATE_DST
, tmp
);
1225 ui_table_row_show(tbl
);
1228 static void draw_procs(WINDOW
*screen
, struct flow_list
*fl
,
1231 struct proc_entry
*proc
, *tmp
;
1232 unsigned int line
= 4;
1233 int skip
= skip_lines
;
1238 ui_table_clear(&procs_tbl
);
1239 ui_table_header_print(&procs_tbl
);
1241 cds_list_for_each_entry_safe(proc
, tmp
, &proc_list
.head
, entry
) {
1242 if (line
+ 1 >= rows
)
1247 draw_proc_entry(proc
);
1254 draw_filter_status("Processes", procs
, skip_lines
);
1257 static void draw_help(void)
1263 mvaddch(row
, col
, ACS_ULCORNER
);
1264 mvaddch(rows
- row
- 1, col
, ACS_LLCORNER
);
1266 mvaddch(row
, cols
- 1, ACS_URCORNER
);
1267 mvaddch(rows
- row
- 1, cols
- 1, ACS_LRCORNER
);
1269 for (i
= 1; i
< rows
- row
- 2; i
++) {
1270 mvaddch(row
+ i
, 0, ACS_VLINE
);
1271 mvaddch(row
+ i
, cols
- 1, ACS_VLINE
);
1273 for (i
= 1; i
< cols
- col
- 1; i
++) {
1274 mvaddch(row
, col
+ i
, ACS_HLINE
);
1275 mvaddch(rows
- row
- 1, col
+ i
, ACS_HLINE
);
1279 mvaddnstr(row
, cols
/ 2 - 2, "| Help |", -1);
1281 attron(A_UNDERLINE
);
1282 mvaddnstr(row
+ 2, col
+ 2, "Navigation", -1);
1283 attroff(A_BOLD
| A_UNDERLINE
);
1285 mvaddnstr(row
+ 4, col
+ 3, "TAB Go to next tab panel", -1);
1286 mvaddnstr(row
+ 5, col
+ 3, "Up, u, k Move up", -1);
1287 mvaddnstr(row
+ 6, col
+ 3, "Down, d, j Move down", -1);
1288 mvaddnstr(row
+ 7, col
+ 3, "Left,l Scroll left", -1);
1289 mvaddnstr(row
+ 8, col
+ 3, "Right,h Scroll right", -1);
1290 mvaddnstr(row
+ 9, col
+ 3, "? Toggle help window", -1);
1291 mvaddnstr(row
+ 10, col
+ 3, "q, Ctrl+C Quit", -1);
1293 attron(A_BOLD
| A_UNDERLINE
);
1294 mvaddnstr(row
+ 12, col
+ 2, "Display Settings", -1);
1295 attroff(A_BOLD
| A_UNDERLINE
);
1297 mvaddnstr(row
+ 14, col
+ 3, "b Toggle rate units (bits/bytes)", -1);
1298 mvaddnstr(row
+ 15, col
+ 3, "a Toggle display of active flows (rate > 0) only", -1);
1299 mvaddnstr(row
+ 16, col
+ 3, "s Toggle show source peer info", -1);
1301 mvaddnstr(row
+ 18, col
+ 3, "T Toggle display TCP flows", -1);
1302 mvaddnstr(row
+ 19, col
+ 3, "U Toggle display UDP flows", -1);
1303 mvaddnstr(row
+ 20, col
+ 3, "D Toggle display DCCP flows", -1);
1304 mvaddnstr(row
+ 21, col
+ 3, "I Toggle display ICMP flows", -1);
1305 mvaddnstr(row
+ 22, col
+ 3, "S Toggle display SCTP flows", -1);
1308 static void draw_header(WINDOW
*screen
)
1314 for (i
= 0; i
< cols
; i
++)
1317 mvwprintw(screen
, 0, 2, "flowtop %s", VERSION_LONG
);
1318 attroff(A_STANDOUT
);
1321 static void draw_footer(void)
1327 for (i
= 0; i
< cols
; i
++)
1328 mvaddch(rows
- 1, i
, ' ');
1330 mvaddnstr(rows
- 1, 1, "Press '?' for help", -1);
1332 attroff(A_STANDOUT
);
1335 static void show_option_toggle(int opt
)
1339 TOGGLE_FLAG(what
, INCLUDE_TCP
);
1342 TOGGLE_FLAG(what
, INCLUDE_UDP
);
1345 TOGGLE_FLAG(what
, INCLUDE_DCCP
);
1348 TOGGLE_FLAG(what
, INCLUDE_ICMP
);
1351 TOGGLE_FLAG(what
, INCLUDE_SCTP
);
1356 static void flows_table_init(struct ui_table
*tbl
)
1360 ui_table_pos_set(tbl
, 3, 0);
1361 ui_table_height_set(tbl
, LINES
- 3);
1363 ui_table_col_add(tbl
, TBL_FLOW_PROCESS
, "PROCESS", 13);
1364 ui_table_col_add(tbl
, TBL_FLOW_PID
, "PID", 7);
1365 ui_table_col_add(tbl
, TBL_FLOW_PROTO
, "PROTO", 6);
1366 ui_table_col_add(tbl
, TBL_FLOW_STATE
, "STATE", 11);
1367 ui_table_col_add(tbl
, TBL_FLOW_TIME
, "TIME", 4);
1368 ui_table_col_add(tbl
, TBL_FLOW_ADDRESS
, "ADDRESS", 50);
1369 ui_table_col_add(tbl
, TBL_FLOW_PORT
, "PORT", 8);
1370 ui_table_col_add(tbl
, TBL_FLOW_GEO
, "GEO", 3);
1371 ui_table_col_add(tbl
, TBL_FLOW_BYTES
, "BYTES", 10);
1372 ui_table_col_add(tbl
, TBL_FLOW_RATE
, "RATE", 10);
1374 ui_table_col_align_set(tbl
, TBL_FLOW_TIME
, UI_ALIGN_RIGHT
);
1375 ui_table_col_align_set(tbl
, TBL_FLOW_BYTES
, UI_ALIGN_RIGHT
);
1376 ui_table_col_align_set(tbl
, TBL_FLOW_RATE
, UI_ALIGN_RIGHT
);
1378 ui_table_col_color_set(tbl
, TBL_FLOW_PROCESS
, COLOR(YELLOW
, BLACK
));
1379 ui_table_col_color_set(tbl
, TBL_FLOW_PID
, A_BOLD
);
1380 ui_table_col_color_set(tbl
, TBL_FLOW_STATE
, COLOR(YELLOW
, BLACK
));
1382 ui_table_header_color_set(&flows_tbl
, COLOR(BLACK
, GREEN
));
1385 static void procs_table_init(struct ui_table
*tbl
)
1389 ui_table_pos_set(tbl
, 3, 0);
1390 ui_table_height_set(tbl
, LINES
- 3);
1392 ui_table_col_add(tbl
, TBL_PROC_NAME
, "NAME", 13);
1393 ui_table_col_add(tbl
, TBL_PROC_PID
, "PID", 7);
1394 ui_table_col_add(tbl
, TBL_PROC_FLOWS
, "FLOWS", 7);
1395 ui_table_col_add(tbl
, TBL_PROC_BYTES_SRC
, "BYTES_SRC", 10);
1396 ui_table_col_add(tbl
, TBL_PROC_BYTES_DST
, "BYTES_DST", 10);
1397 ui_table_col_add(tbl
, TBL_PROC_RATE_SRC
, "RATE_SRC", 14);
1398 ui_table_col_add(tbl
, TBL_PROC_RATE_DST
, "RATE_DST", 14);
1400 ui_table_col_align_set(tbl
, TBL_PROC_BYTES_SRC
, UI_ALIGN_RIGHT
);
1401 ui_table_col_align_set(tbl
, TBL_PROC_RATE_SRC
, UI_ALIGN_RIGHT
);
1402 ui_table_col_align_set(tbl
, TBL_PROC_BYTES_DST
, UI_ALIGN_RIGHT
);
1403 ui_table_col_align_set(tbl
, TBL_PROC_RATE_DST
, UI_ALIGN_RIGHT
);
1405 ui_table_col_color_set(tbl
, TBL_PROC_NAME
, COLOR(YELLOW
, BLACK
));
1406 ui_table_col_color_set(tbl
, TBL_PROC_PID
, A_BOLD
);
1407 ui_table_col_color_set(tbl
, TBL_PROC_FLOWS
, COLOR(YELLOW
, BLACK
));
1408 ui_table_col_color_set(tbl
, TBL_PROC_BYTES_SRC
, COLOR(RED
, BLACK
));
1409 ui_table_col_color_set(tbl
, TBL_PROC_RATE_SRC
, COLOR(RED
, BLACK
));
1410 ui_table_col_color_set(tbl
, TBL_PROC_BYTES_DST
, COLOR(BLUE
, BLACK
));
1411 ui_table_col_color_set(tbl
, TBL_PROC_RATE_DST
, COLOR(BLUE
, BLACK
));
1413 ui_table_header_color_set(tbl
, COLOR(BLACK
, GREEN
));
1416 static void tab_main_on_open(struct ui_tab
*tab
, enum ui_tab_event_t evt
, uint32_t id
)
1418 if (evt
!= UI_TAB_EVT_OPEN
)
1421 if (id
== TAB_FLOWS
)
1422 draw_flows(screen
, &flow_list
, skip_lines
);
1423 else if (id
== TAB_PROCS
)
1424 draw_procs(screen
, &flow_list
, skip_lines
);
1427 static void presenter(void)
1429 bool show_help
= false;
1430 struct ui_tab
*tab_main
;
1432 lookup_init(LT_PORTS_TCP
);
1433 lookup_init(LT_PORTS_UDP
);
1435 screen
= screen_init(false);
1440 INIT_COLOR(RED
, BLACK
);
1441 INIT_COLOR(BLUE
, BLACK
);
1442 INIT_COLOR(YELLOW
, BLACK
);
1443 INIT_COLOR(GREEN
, BLACK
);
1444 INIT_COLOR(BLACK
, GREEN
);
1446 flows_table_init(&flows_tbl
);
1447 procs_table_init(&procs_tbl
);
1449 tab_main
= ui_tab_create();
1450 ui_tab_event_cb_set(tab_main
, tab_main_on_open
);
1451 ui_tab_pos_set(tab_main
, 2, 0);
1452 ui_tab_active_color_set(tab_main
, COLOR(BLACK
, GREEN
));
1453 ui_tab_entry_add(tab_main
, TAB_FLOWS
, "Flows");
1454 ui_tab_entry_add(tab_main
, TAB_PROCS
, "Process");
1456 rcu_register_thread();
1461 getmaxyx(screen
, rows
, cols
);
1479 if (skip_lines
> SCROLL_MAX
)
1480 skip_lines
= SCROLL_MAX
;
1484 ui_table_event_send(&flows_tbl
, UI_EVT_SCROLL_LEFT
);
1488 ui_table_event_send(&flows_tbl
, UI_EVT_SCROLL_RIGHT
);
1491 if (rate_type
== RATE_BYTES
)
1492 rate_type
= RATE_BITS
;
1494 rate_type
= RATE_BYTES
;
1497 show_active_only
= !show_active_only
;
1500 show_src
= !show_src
;
1503 show_help
= !show_help
;
1512 show_option_toggle(ch
);
1513 do_reload_flows
= true;
1516 ui_tab_event_send(tab_main
, UI_EVT_SELECT_NEXT
);
1523 draw_header(screen
);
1528 ui_tab_show(tab_main
);
1532 rcu_unregister_thread();
1534 ui_table_uninit(&flows_tbl
);
1535 ui_table_uninit(&procs_tbl
);
1536 ui_tab_destroy(tab_main
);
1539 lookup_cleanup(LT_PORTS_UDP
);
1540 lookup_cleanup(LT_PORTS_TCP
);
1543 static void restore_sysctl(void *obj
)
1545 struct sysctl_params_ctx
*sysctl_ctx
= obj
;
1547 if (sysctl_ctx
->nfct_acct
== 0)
1548 sysctl_set_int("net/netfilter/nf_conntrack_acct",
1549 sysctl_ctx
->nfct_acct
);
1551 if (sysctl_ctx
->nfct_tstamp
== 0)
1552 sysctl_set_int("net/netfilter/nf_conntrack_timestamp",
1553 sysctl_ctx
->nfct_tstamp
);
1556 static void on_panic_handler(void *arg
)
1558 restore_sysctl(arg
);
1562 static void conntrack_acct_enable(void)
1564 /* We can still work w/o traffic accounting so just warn about error */
1565 if (sysctl_get_int("net/netfilter/nf_conntrack_acct", &sysctl
.nfct_acct
)) {
1566 fprintf(stderr
, "Can't read net/netfilter/nf_conntrack_acct: %s\n",
1571 if (sysctl
.nfct_acct
== 1)
1574 if (sysctl_set_int("net/netfilter/nf_conntrack_acct", 1)) {
1575 fprintf(stderr
, "Can't write net/netfilter/nf_conntrack_acct: %s\n",
1580 static void conntrack_tstamp_enable(void)
1582 if (sysctl_get_int("net/netfilter/nf_conntrack_timestamp", &sysctl
.nfct_tstamp
)) {
1583 fprintf(stderr
, "Can't read net/netfilter/nf_conntrack_timestamp: %s\n",
1588 if (sysctl
.nfct_tstamp
== 1)
1591 if (sysctl_set_int("net/netfilter/nf_conntrack_timestamp", 1)) {
1592 fprintf(stderr
, "Can't write net/netfilter/nf_conntrack_timestamp: %s\n",
1597 static void flow_entry_filter(struct flow_entry
*n
)
1599 if (show_active_only
&& !n
->rate_bytes_src
&& !n
->rate_bytes_dst
)
1600 n
->is_visible
= false;
1602 n
->is_visible
= true;
1605 static int flow_list_update_entry(struct flow_list
*fl
, struct nf_conntrack
*ct
)
1607 struct flow_entry
*n
;
1609 n
= flow_list_find_id(fl
, nfct_get_attr_u32(ct
, ATTR_ID
));
1611 return NFCT_CB_CONTINUE
;
1613 flow_entry_calc_rate(n
, ct
);
1614 flow_entry_update_time(n
);
1615 flow_entry_from_ct(n
, ct
);
1616 flow_entry_filter(n
);
1618 return NFCT_CB_CONTINUE
;
1621 static int flow_event_cb(enum nf_conntrack_msg_type type
,
1622 struct nf_conntrack
*ct
, void *data __maybe_unused
)
1625 return NFCT_CB_STOP
;
1629 return flow_list_new_entry(&flow_list
, ct
);
1631 return flow_list_update_entry(&flow_list
, ct
);
1632 case NFCT_T_DESTROY
:
1633 return flow_list_del_entry(&flow_list
, ct
);
1635 return NFCT_CB_CONTINUE
;
1639 static void collector_refresh_procs(void)
1641 struct proc_entry
*p
, *tmp
;
1643 cds_list_for_each_entry_safe(p
, tmp
, &proc_list
.head
, entry
) {
1644 double sec
= (double)time_after_us(&p
->last_update
) / USEC_PER_SEC
;
1645 struct flow_entry
*n
;
1650 bug_on(gettimeofday(&p
->last_update
, NULL
));
1652 if (!p
->flows_count
&& !proc_exists(p
->pid
)) {
1653 cds_list_del_rcu(&p
->entry
);
1654 call_rcu(&p
->rcu
, proc_entry_xfree_rcu
);
1658 p
->rate_bytes_src
= 0;
1659 p
->rate_bytes_dst
= 0;
1660 p
->rate_pkts_src
= 0;
1661 p
->rate_pkts_dst
= 0;
1663 cds_list_for_each_entry_rcu(n
, &p
->flows
, proc_head
) {
1664 p
->rate_bytes_src
+= n
->rate_bytes_src
;
1665 p
->rate_bytes_dst
+= n
->rate_bytes_dst
;
1666 p
->rate_pkts_src
+= n
->rate_pkts_src
;
1667 p
->rate_pkts_dst
+= n
->rate_pkts_dst
;
1672 static void collector_refresh_flows(struct nfct_handle
*handle
)
1674 struct flow_entry
*n
;
1676 cds_list_for_each_entry_rcu(n
, &flow_list
.head
, entry
) {
1677 nfct_query(handle
, NFCT_Q_GET
, n
->ct
);
1681 static void collector_create_filter(struct nfct_handle
*nfct
)
1683 struct nfct_filter
*filter
;
1686 filter
= nfct_filter_create();
1688 panic("Cannot create a nfct filter: %s\n", strerror(errno
));
1690 if (what
& INCLUDE_UDP
) {
1691 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_UDP
);
1692 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_UDPLITE
);
1694 if (what
& INCLUDE_TCP
)
1695 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_TCP
);
1696 if (what
& INCLUDE_DCCP
)
1697 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_DCCP
);
1698 if (what
& INCLUDE_SCTP
)
1699 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_SCTP
);
1700 if (what
& INCLUDE_ICMP
&& what
& INCLUDE_IPV4
)
1701 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_ICMP
);
1702 if (what
& INCLUDE_ICMP
&& what
& INCLUDE_IPV6
)
1703 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_ICMPV6
);
1704 if (what
& INCLUDE_IPV4
) {
1705 nfct_filter_set_logic(filter
, NFCT_FILTER_SRC_IPV4
, NFCT_FILTER_LOGIC_NEGATIVE
);
1706 nfct_filter_add_attr(filter
, NFCT_FILTER_SRC_IPV4
, &filter_ipv4
);
1708 if (what
& INCLUDE_IPV6
) {
1709 nfct_filter_set_logic(filter
, NFCT_FILTER_SRC_IPV6
, NFCT_FILTER_LOGIC_NEGATIVE
);
1710 nfct_filter_add_attr(filter
, NFCT_FILTER_SRC_IPV6
, &filter_ipv6
);
1713 ret
= nfct_filter_attach(nfct_fd(nfct
), filter
);
1715 panic("Cannot attach filter to handle: %s\n", strerror(errno
));
1717 nfct_filter_destroy(filter
);
1720 /* This hand-crafted filter looks ugly but it allows to do not
1721 * flush nfct connections & filter them by user specified filter.
1722 * May be it is better to replace this one by nfct_cmp. */
1723 static int flow_dump_cb(enum nf_conntrack_msg_type type __maybe_unused
,
1724 struct nf_conntrack
*ct
, void *data __maybe_unused
)
1726 struct flow_entry fl
;
1727 struct flow_entry
*n
= &fl
;
1730 return NFCT_CB_STOP
;
1732 if (!(what
& ~(INCLUDE_IPV4
| INCLUDE_IPV6
)))
1735 CP_NFCT(l4_proto
, ATTR_ORIG_L4PROTO
, 8);
1737 if (what
& INCLUDE_UDP
) {
1738 if (n
->l4_proto
== IPPROTO_UDP
)
1741 if (n
->l4_proto
== IPPROTO_UDPLITE
)
1745 if ((what
& INCLUDE_TCP
) && n
->l4_proto
== IPPROTO_TCP
)
1748 if ((what
& INCLUDE_DCCP
) && n
->l4_proto
== IPPROTO_DCCP
)
1751 if ((what
& INCLUDE_SCTP
) && n
->l4_proto
== IPPROTO_SCTP
)
1754 if ((what
& INCLUDE_ICMP
) && (what
& INCLUDE_IPV4
) &&
1755 n
->l4_proto
== IPPROTO_ICMP
) {
1759 if ((what
& INCLUDE_ICMP
) && (what
& INCLUDE_IPV6
) &&
1760 n
->l4_proto
== IPPROTO_ICMPV6
) {
1767 /* filter loopback addresses */
1768 if (what
& INCLUDE_IPV4
) {
1769 CP_NFCT(ip4_src_addr
, ATTR_ORIG_IPV4_SRC
, 32);
1771 if (n
->ip4_src_addr
== filter_ipv4
.addr
)
1774 if (what
& INCLUDE_IPV6
) {
1775 CP_NFCT_BUFF(ip6_src_addr
, ATTR_ORIG_IPV6_SRC
);
1777 if (n
->ip6_src_addr
[0] == 0x0 &&
1778 n
->ip6_src_addr
[1] == 0x0 &&
1779 n
->ip6_src_addr
[2] == 0x0 &&
1780 n
->ip6_src_addr
[3] == 0x1)
1784 return flow_list_new_entry(&flow_list
, ct
);
1787 return NFCT_CB_CONTINUE
;
1790 static void collector_dump_flows(void)
1792 struct nfct_handle
*nfct
= nfct_open(CONNTRACK
, 0);
1795 panic("Cannot create a nfct handle: %s\n", strerror(errno
));
1797 nfct_callback_register(nfct
, NFCT_T_ALL
, flow_dump_cb
, NULL
);
1799 is_flow_collecting
= true;
1800 if (what
& INCLUDE_IPV4
) {
1801 int family
= AF_INET
;
1802 nfct_query(nfct
, NFCT_Q_DUMP
, &family
);
1804 if (what
& INCLUDE_IPV6
) {
1805 int family
= AF_INET6
;
1806 nfct_query(nfct
, NFCT_Q_DUMP
, &family
);
1808 is_flow_collecting
= false;
1813 static void *collector(void *null __maybe_unused
)
1815 struct nfct_handle
*ct_event
;
1816 struct pollfd poll_fd
[1];
1818 proc_list_init(&proc_list
);
1819 flow_list_init(&flow_list
);
1821 ct_event
= nfct_open(CONNTRACK
, NF_NETLINK_CONNTRACK_NEW
|
1822 NF_NETLINK_CONNTRACK_UPDATE
|
1823 NF_NETLINK_CONNTRACK_DESTROY
);
1825 panic("Cannot create a nfct handle: %s\n", strerror(errno
));
1827 collector_create_filter(ct_event
);
1829 nfct_callback_register(ct_event
, NFCT_T_ALL
, flow_event_cb
, NULL
);
1831 poll_fd
[0].fd
= nfct_fd(ct_event
);
1832 poll_fd
[0].events
= POLLIN
;
1834 if (fcntl(nfct_fd(ct_event
), F_SETFL
, O_NONBLOCK
) == -1)
1835 panic("Cannot set non-blocking socket: fcntl(): %s\n",
1838 rcu_register_thread();
1840 collector_dump_flows();
1845 if (!do_reload_flows
) {
1846 usleep(USEC_PER_SEC
* interval
);
1848 do_reload_flows
= false;
1850 flow_list_destroy(&flow_list
);
1852 collector_create_filter(ct_event
);
1853 collector_dump_flows();
1856 collector_refresh_procs();
1857 collector_refresh_flows(ct_event
);
1859 status
= poll(poll_fd
, 1, 0);
1861 if (errno
== EAGAIN
|| errno
== EINTR
)
1864 panic("Error while polling: %s\n", strerror(errno
));
1865 } else if (status
!= 0) {
1866 if (poll_fd
[0].revents
& POLLIN
)
1867 nfct_catch(ct_event
);
1871 flow_list_destroy(&flow_list
);
1872 proc_list_destroy(&proc_list
);
1874 rcu_unregister_thread();
1876 nfct_close(ct_event
);
1881 int main(int argc
, char **argv
)
1884 int ret
, c
, what_cmd
= 0;
1889 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1893 what_cmd
|= INCLUDE_IPV4
;
1896 what_cmd
|= INCLUDE_IPV6
;
1899 what_cmd
|= INCLUDE_TCP
;
1902 what_cmd
|= INCLUDE_UDP
;
1905 what_cmd
|= INCLUDE_DCCP
;
1908 what_cmd
|= INCLUDE_ICMP
;
1911 what_cmd
|= INCLUDE_SCTP
;
1917 rate_type
= RATE_BITS
;
1924 interval
= strtoul(optarg
, NULL
, 10);
1927 resolve_dns
= false;
1930 resolve_geoip
= false;
1946 if (!(what
& (INCLUDE_IPV4
| INCLUDE_IPV6
)))
1947 what
|= INCLUDE_IPV4
| INCLUDE_IPV6
;
1952 register_signal(SIGINT
, signal_handler
);
1953 register_signal(SIGQUIT
, signal_handler
);
1954 register_signal(SIGTERM
, signal_handler
);
1955 register_signal(SIGHUP
, signal_handler
);
1957 panic_handler_add(on_panic_handler
, &sysctl
);
1959 conntrack_acct_enable();
1960 conntrack_tstamp_enable();
1965 ret
= pthread_create(&tid
, NULL
, collector
, NULL
);
1967 panic("Cannot create phthread!\n");
1974 restore_sysctl(&sysctl
);