build: Find libncurses by using pkg-config
[netsniff-ng.git] / flowtop.c
blob5418aaf5579ef7698a4e9b7b6c0c8078b8e60a42
1 /*
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.
6 */
8 #define _LGPL_SOURCE
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <stdlib.h>
12 #include <signal.h>
13 #include <getopt.h>
14 #include <pthread.h>
15 #include <signal.h>
16 #include <netdb.h>
17 #include <ctype.h>
18 #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
19 #include <libnetfilter_conntrack/libnetfilter_conntrack_tcp.h>
20 #include <libnetfilter_conntrack/libnetfilter_conntrack_dccp.h>
21 #include <libnetfilter_conntrack/libnetfilter_conntrack_sctp.h>
22 #include <netinet/in.h>
23 #include <curses.h>
24 #include <dirent.h>
25 #include <sys/stat.h>
26 #include <sys/fsuid.h>
27 #include <urcu.h>
28 #include <libgen.h>
30 #include "die.h"
31 #include "xmalloc.h"
32 #include "xio.h"
33 #include "geoip.h"
34 #include "xutils.h"
35 #include "built_in.h"
36 #include "locking.h"
37 #include "dissector_eth.h"
38 #include "pkt_buff.h"
40 struct flow_entry {
41 uint32_t flow_id, use, status;
42 uint8_t l3_proto, l4_proto;
43 uint32_t ip4_src_addr, ip4_dst_addr;
44 uint32_t ip6_src_addr[4], ip6_dst_addr[4];
45 uint16_t port_src, port_dst;
46 uint8_t tcp_state, tcp_flags, sctp_state, dccp_state;
47 uint64_t counter_pkts, counter_bytes;
48 uint64_t timestamp_start, timestamp_stop;
49 char country_src[128], country_dst[128];
50 char city_src[128], city_dst[128];
51 char rev_dns_src[256], rev_dns_dst[256];
52 char cmdline[256];
53 struct flow_entry *next;
54 int procnum, inode;
57 struct flow_list {
58 struct flow_entry *head;
59 struct spinlock lock;
62 #ifndef ATTR_TIMESTAMP_START
63 # define ATTR_TIMESTAMP_START 63
64 #endif
65 #ifndef ATTR_TIMESTAMP_STOP
66 # define ATTR_TIMESTAMP_STOP 64
67 #endif
69 #define SCROLL_MAX 1000
71 #define INCLUDE_IPV4 (1 << 0)
72 #define INCLUDE_IPV6 (1 << 1)
73 #define INCLUDE_UDP (1 << 2)
74 #define INCLUDE_TCP (1 << 3)
75 #define INCLUDE_DCCP (1 << 4)
76 #define INCLUDE_ICMP (1 << 5)
77 #define INCLUDE_SCTP (1 << 6)
79 volatile sig_atomic_t sigint = 0;
81 static int what = INCLUDE_IPV4 | INCLUDE_IPV6 | INCLUDE_TCP, show_src = 0;
83 static struct flow_list flow_list;
85 static const char *short_options = "vhTUsDIS46u";
86 static const struct option long_options[] = {
87 {"ipv4", no_argument, NULL, '4'},
88 {"ipv6", no_argument, NULL, '6'},
89 {"tcp", no_argument, NULL, 'T'},
90 {"udp", no_argument, NULL, 'U'},
91 {"dccp", no_argument, NULL, 'D'},
92 {"icmp", no_argument, NULL, 'I'},
93 {"sctp", no_argument, NULL, 'S'},
94 {"show-src", no_argument, NULL, 's'},
95 {"update", no_argument, NULL, 'u'},
96 {"version", no_argument, NULL, 'v'},
97 {"help", no_argument, NULL, 'h'},
98 {NULL, 0, NULL, 0}
101 static const char *const l3proto2str[AF_MAX] = {
102 [AF_INET] = "ipv4",
103 [AF_INET6] = "ipv6",
106 static const char *const l4proto2str[IPPROTO_MAX] = {
107 [IPPROTO_TCP] = "tcp",
108 [IPPROTO_UDP] = "udp",
109 [IPPROTO_UDPLITE] = "udplite",
110 [IPPROTO_ICMP] = "icmp",
111 [IPPROTO_ICMPV6] = "icmpv6",
112 [IPPROTO_SCTP] = "sctp",
113 [IPPROTO_GRE] = "gre",
114 [IPPROTO_DCCP] = "dccp",
115 [IPPROTO_IGMP] = "igmp",
116 [IPPROTO_IPIP] = "ipip",
117 [IPPROTO_EGP] = "egp",
118 [IPPROTO_PUP] = "pup",
119 [IPPROTO_IDP] = "idp",
120 [IPPROTO_RSVP] = "rsvp",
121 [IPPROTO_IPV6] = "ip6tun",
122 [IPPROTO_ESP] = "esp",
123 [IPPROTO_AH] = "ah",
124 [IPPROTO_PIM] = "pim",
125 [IPPROTO_COMP] = "comp",
128 static const char *const tcp_state2str[TCP_CONNTRACK_MAX] = {
129 [TCP_CONNTRACK_NONE] = "NOSTATE",
130 [TCP_CONNTRACK_SYN_SENT] = "SYN_SENT",
131 [TCP_CONNTRACK_SYN_RECV] = "SYN_RECV",
132 [TCP_CONNTRACK_ESTABLISHED] = "ESTABLISHED",
133 [TCP_CONNTRACK_FIN_WAIT] = "FIN_WAIT",
134 [TCP_CONNTRACK_CLOSE_WAIT] = "CLOSE_WAIT",
135 [TCP_CONNTRACK_LAST_ACK] = "LAST_ACK",
136 [TCP_CONNTRACK_TIME_WAIT] = "TIME_WAIT",
137 [TCP_CONNTRACK_CLOSE] = "CLOSE",
138 [TCP_CONNTRACK_SYN_SENT2] = "SYN_SENT2",
141 static const uint8_t tcp_states[] = {
142 TCP_CONNTRACK_SYN_SENT,
143 TCP_CONNTRACK_SYN_RECV,
144 TCP_CONNTRACK_ESTABLISHED,
145 TCP_CONNTRACK_FIN_WAIT,
146 TCP_CONNTRACK_CLOSE_WAIT,
147 TCP_CONNTRACK_LAST_ACK,
148 TCP_CONNTRACK_TIME_WAIT,
149 TCP_CONNTRACK_CLOSE,
150 TCP_CONNTRACK_SYN_SENT2,
151 TCP_CONNTRACK_NONE,
154 static const char *const dccp_state2str[DCCP_CONNTRACK_MAX] = {
155 [DCCP_CONNTRACK_NONE] = "NOSTATE",
156 [DCCP_CONNTRACK_REQUEST] = "REQUEST",
157 [DCCP_CONNTRACK_RESPOND] = "RESPOND",
158 [DCCP_CONNTRACK_PARTOPEN] = "PARTOPEN",
159 [DCCP_CONNTRACK_OPEN] = "OPEN",
160 [DCCP_CONNTRACK_CLOSEREQ] = "CLOSEREQ",
161 [DCCP_CONNTRACK_CLOSING] = "CLOSING",
162 [DCCP_CONNTRACK_TIMEWAIT] = "TIMEWAIT",
163 [DCCP_CONNTRACK_IGNORE] = "IGNORE",
164 [DCCP_CONNTRACK_INVALID] = "INVALID",
167 static const uint8_t dccp_states[] = {
168 DCCP_CONNTRACK_NONE,
169 DCCP_CONNTRACK_REQUEST,
170 DCCP_CONNTRACK_RESPOND,
171 DCCP_CONNTRACK_PARTOPEN,
172 DCCP_CONNTRACK_OPEN,
173 DCCP_CONNTRACK_CLOSEREQ,
174 DCCP_CONNTRACK_CLOSING,
175 DCCP_CONNTRACK_TIMEWAIT,
176 DCCP_CONNTRACK_IGNORE,
177 DCCP_CONNTRACK_INVALID,
180 static const char *const sctp_state2str[SCTP_CONNTRACK_MAX] = {
181 [SCTP_CONNTRACK_NONE] = "NOSTATE",
182 [SCTP_CONNTRACK_CLOSED] = "CLOSED",
183 [SCTP_CONNTRACK_COOKIE_WAIT] = "COOKIE_WAIT",
184 [SCTP_CONNTRACK_COOKIE_ECHOED] = "COOKIE_ECHOED",
185 [SCTP_CONNTRACK_ESTABLISHED] = "ESTABLISHED",
186 [SCTP_CONNTRACK_SHUTDOWN_SENT] = "SHUTDOWN_SENT",
187 [SCTP_CONNTRACK_SHUTDOWN_RECD] = "SHUTDOWN_RECD",
188 [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = "SHUTDOWN_ACK_SENT",
191 static const uint8_t sctp_states[] = {
192 SCTP_CONNTRACK_NONE,
193 SCTP_CONNTRACK_CLOSED,
194 SCTP_CONNTRACK_COOKIE_WAIT,
195 SCTP_CONNTRACK_COOKIE_ECHOED,
196 SCTP_CONNTRACK_ESTABLISHED,
197 SCTP_CONNTRACK_SHUTDOWN_SENT,
198 SCTP_CONNTRACK_SHUTDOWN_RECD,
199 SCTP_CONNTRACK_SHUTDOWN_ACK_SENT,
202 static const struct nfct_filter_ipv4 filter_ipv4 = {
203 .addr = __constant_htonl(INADDR_LOOPBACK),
204 .mask = 0xffffffff,
207 static const struct nfct_filter_ipv6 filter_ipv6 = {
208 .addr = { 0x0, 0x0, 0x0, 0x1 },
209 .mask = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff },
212 static void signal_handler(int number)
214 switch (number) {
215 case SIGINT:
216 sigint = 1;
217 break;
218 case SIGHUP:
219 default:
220 break;
224 static void flow_entry_from_ct(struct flow_entry *n, struct nf_conntrack *ct);
225 static void flow_entry_get_extended(struct flow_entry *n);
227 static void help(void)
229 printf("\nflowtop %s, top-like netfilter TCP/UDP flow tracking\n",
230 VERSION_STRING);
231 puts("http://www.netsniff-ng.org\n\n"
232 "Usage: flowtop [options]\n"
233 "Options:\n"
234 " -4|--ipv4 Show only IPv4 flows (default)\n"
235 " -6|--ipv6 Show only IPv6 flows (default)\n"
236 " -T|--tcp Show only TCP flows (default)\n"
237 " -U|--udp Show only UDP flows\n"
238 " -D|--dccp Show only DCCP flows\n"
239 " -I|--icmp Show only ICMP/ICMPv6 flows\n"
240 " -S|--sctp Show only SCTP flows\n"
241 " -s|--show-src Also show source, not only dest\n"
242 " -u|--update Update GeoIP databases\n"
243 " -v|--version Print version\n"
244 " -h|--help Print this help\n\n"
245 "Examples:\n"
246 " flowtop\n"
247 " flowtop -46UTDISs\n\n"
248 "Note:\n"
249 " If netfilter is not running, you can activate it with e.g.:\n"
250 " iptables -A INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT\n"
251 " iptables -A OUTPUT -p tcp -m state --state NEW,ESTABLISHED -j ACCEPT\n\n"
252 "Please report bugs to <bugs@netsniff-ng.org>\n"
253 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
254 "Copyright (C) 2011-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
255 "Swiss federal institute of technology (ETH Zurich)\n"
256 "License: GNU GPL version 2.0\n"
257 "This is free software: you are free to change and redistribute it.\n"
258 "There is NO WARRANTY, to the extent permitted by law.\n");
259 die();
262 static void version(void)
264 printf("\nflowtop %s, top-like netfilter TCP/UDP flow tracking\n",
265 VERSION_STRING);
266 puts("http://www.netsniff-ng.org\n\n"
267 "Please report bugs to <bugs@netsniff-ng.org>\n"
268 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
269 "Copyright (C) 2011-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
270 "Swiss federal institute of technology (ETH Zurich)\n"
271 "License: GNU GPL version 2.0\n"
272 "This is free software: you are free to change and redistribute it.\n"
273 "There is NO WARRANTY, to the extent permitted by law.\n");
274 die();
277 static inline struct flow_entry *flow_entry_xalloc(void)
279 return xzmalloc(sizeof(struct flow_entry));
282 static inline void flow_entry_xfree(struct flow_entry *n)
284 xfree(n);
287 static inline void flow_list_init(struct flow_list *fl)
289 fl->head = NULL;
290 spinlock_init(&fl->lock);
293 static void flow_list_new_entry(struct flow_list *fl, struct nf_conntrack *ct)
295 struct flow_entry *n = flow_entry_xalloc();
297 flow_entry_from_ct(n, ct);
298 flow_entry_get_extended(n);
300 rcu_assign_pointer(n->next, fl->head);
301 rcu_assign_pointer(fl->head, n);
304 static struct flow_entry *flow_list_find_id(struct flow_list *fl,
305 uint32_t id)
307 struct flow_entry *n = rcu_dereference(fl->head);
309 while (n != NULL) {
310 if (n->flow_id == id)
311 return n;
313 n = rcu_dereference(n->next);
316 return NULL;
319 static struct flow_entry *flow_list_find_prev_id(struct flow_list *fl,
320 uint32_t id)
322 struct flow_entry *n = rcu_dereference(fl->head), *tmp;
324 if (n->flow_id == id)
325 return NULL;
327 while ((tmp = rcu_dereference(n->next)) != NULL) {
328 if (tmp->flow_id == id)
329 return n;
331 n = tmp;
334 return NULL;
337 static void flow_list_update_entry(struct flow_list *fl,
338 struct nf_conntrack *ct)
340 int do_ext = 0;
341 struct flow_entry *n;
343 n = flow_list_find_id(fl, nfct_get_attr_u32(ct, ATTR_ID));
344 if (n == NULL) {
345 n = flow_entry_xalloc();
346 do_ext = 1;
349 flow_entry_from_ct(n, ct);
350 if (do_ext) {
351 flow_entry_get_extended(n);
353 rcu_assign_pointer(n->next, fl->head);
354 rcu_assign_pointer(fl->head, n);
358 static void flow_list_destroy_entry(struct flow_list *fl,
359 struct nf_conntrack *ct)
361 struct flow_entry *n1, *n2;
362 uint32_t id = nfct_get_attr_u32(ct, ATTR_ID);
364 n1 = flow_list_find_id(fl, id);
365 if (n1) {
366 n2 = flow_list_find_prev_id(fl, id);
367 if (n2) {
368 rcu_assign_pointer(n2->next, n1->next);
369 rcu_assign_pointer(n1->next, NULL);
371 flow_entry_xfree(n1);
372 } else {
373 flow_entry_xfree(fl->head);
375 rcu_assign_pointer(fl->head, NULL);
380 static void flow_list_destroy(struct flow_list *fl)
382 struct flow_entry *n;
384 while (fl->head != NULL) {
385 n = rcu_dereference(fl->head->next);
386 rcu_assign_pointer(fl->head->next, NULL);
388 flow_entry_xfree(fl->head);
389 rcu_assign_pointer(fl->head, n);
392 synchronize_rcu();
393 spinlock_destroy(&fl->lock);
396 static int walk_process(char *process, struct flow_entry *n)
398 int ret;
399 DIR *dir;
400 struct dirent *ent;
401 char path[1024];
403 if (snprintf(path, sizeof(path), "/proc/%s/fd", process) == -1)
404 panic("giant process name! %s\n", process);
406 dir = opendir(path);
407 if (!dir)
408 return 0;
410 while ((ent = readdir(dir))) {
411 struct stat statbuf;
413 if (snprintf(path, sizeof(path), "/proc/%s/fd/%s",
414 process, ent->d_name) < 0)
415 continue;
417 if (stat(path, &statbuf) < 0)
418 continue;
420 if (S_ISSOCK(statbuf.st_mode) && n->inode == statbuf.st_ino) {
421 memset(n->cmdline, 0, sizeof(n->cmdline));
423 snprintf(path, sizeof(path), "/proc/%s/exe", process);
425 ret = readlink(path, n->cmdline,
426 sizeof(n->cmdline) - 1);
427 if (ret < 0)
428 panic("readlink error: %s\n", strerror(errno));
430 n->procnum = atoi(process);
431 return 1;
435 closedir(dir);
436 return 0;
439 static void walk_processes(struct flow_entry *n)
441 int ret;
442 DIR *dir;
443 struct dirent *ent;
445 /* n->inode must be set */
446 if (n->inode <= 0) {
447 memset(n->cmdline, 0, sizeof(n->cmdline));
448 return;
451 dir = opendir("/proc");
452 if (!dir)
453 panic("Cannot open /proc!\n");
455 while ((ent = readdir(dir))) {
456 if (strspn(ent->d_name, "0123456789") == strlen(ent->d_name)) {
457 ret = walk_process(ent->d_name, n);
458 if (ret > 0)
459 break;
463 closedir(dir);
466 static int get_port_inode(uint16_t port, int proto, int is_ip6)
468 int ret = -ENOENT;
469 char path[128], buff[1024];
470 FILE *proc;
472 memset(path, 0, sizeof(path));
473 snprintf(path, sizeof(path), "/proc/net/%s%s",
474 l4proto2str[proto], is_ip6 ? "6" : "");
476 proc = fopen(path, "r");
477 if (!proc)
478 return -EIO;
480 memset(buff, 0, sizeof(buff));
482 while (fgets(buff, sizeof(buff), proc) != NULL) {
483 int inode = 0;
484 unsigned int lport = 0;
486 buff[sizeof(buff) - 1] = 0;
487 if (sscanf(buff, "%*u: %*X:%X %*X:%*X %*X %*X:%*X %*X:%*X "
488 "%*X %*u %*u %u", &lport, &inode) == 2) {
489 if ((uint16_t) lport == port) {
490 ret = inode;
491 break;
495 memset(buff, 0, sizeof(buff));
498 fclose(proc);
499 return ret;
502 #define CP_NFCT(elem, attr, x) \
503 do { n->elem = nfct_get_attr_u##x(ct,(attr)); } while (0)
504 #define CP_NFCT_BUFF(elem, attr) do { \
505 const uint8_t *buff = nfct_get_attr(ct,(attr)); \
506 if (buff != NULL) \
507 memcpy(n->elem, buff, sizeof(n->elem)); \
508 } while (0)
510 static void flow_entry_from_ct(struct flow_entry *n, struct nf_conntrack *ct)
512 CP_NFCT(l3_proto, ATTR_ORIG_L3PROTO, 8);
513 CP_NFCT(l4_proto, ATTR_ORIG_L4PROTO, 8);
515 CP_NFCT(ip4_src_addr, ATTR_ORIG_IPV4_SRC, 32);
516 CP_NFCT(ip4_dst_addr, ATTR_ORIG_IPV4_DST, 32);
518 CP_NFCT(port_src, ATTR_ORIG_PORT_SRC, 16);
519 CP_NFCT(port_dst, ATTR_ORIG_PORT_DST, 16);
521 CP_NFCT(status, ATTR_STATUS, 32);
523 CP_NFCT(tcp_state, ATTR_TCP_STATE, 8);
524 CP_NFCT(tcp_flags, ATTR_TCP_FLAGS_ORIG, 8);
525 CP_NFCT(sctp_state, ATTR_SCTP_STATE, 8);
526 CP_NFCT(dccp_state, ATTR_DCCP_STATE, 8);
528 CP_NFCT(counter_pkts, ATTR_ORIG_COUNTER_PACKETS, 64);
529 CP_NFCT(counter_bytes, ATTR_ORIG_COUNTER_BYTES, 64);
531 CP_NFCT(timestamp_start, ATTR_TIMESTAMP_START, 64);
532 CP_NFCT(timestamp_stop, ATTR_TIMESTAMP_STOP, 64);
534 CP_NFCT(flow_id, ATTR_ID, 32);
535 CP_NFCT(use, ATTR_USE, 32);
537 CP_NFCT_BUFF(ip6_src_addr, ATTR_ORIG_IPV6_SRC);
538 CP_NFCT_BUFF(ip6_dst_addr, ATTR_ORIG_IPV6_DST);
540 n->port_src = ntohs(n->port_src);
541 n->port_dst = ntohs(n->port_dst);
543 n->ip4_src_addr = ntohl(n->ip4_src_addr);
544 n->ip4_dst_addr = ntohl(n->ip4_dst_addr);
547 enum flow_entry_direction {
548 flow_entry_src,
549 flow_entry_dst,
552 static inline int flow_entry_get_extended_is_dns(struct flow_entry *n)
554 /* We don't want to analyze / display DNS itself, since we
555 * use it to resolve reverse dns.
557 return n->port_src == 53 || n->port_dst == 53;
560 #define SELFLD(dir,src_member,dst_member) \
561 (((dir) == flow_entry_src) ? n->src_member : n->dst_member)
563 static struct sockaddr_in *
564 flow_entry_get_sain4_obj(struct flow_entry *n, enum flow_entry_direction dir,
565 struct sockaddr_in *sa)
567 memset(sa, 0, sizeof(*sa));
568 sa->sin_family = PF_INET;
569 sa->sin_addr.s_addr = htonl(SELFLD(dir, ip4_src_addr, ip4_dst_addr));
571 return sa;
574 static struct sockaddr_in6 *
575 flow_entry_get_sain6_obj(struct flow_entry *n, enum flow_entry_direction dir,
576 struct sockaddr_in6 *sa)
578 memset(sa, 0, sizeof(*sa));
579 sa->sin6_family = PF_INET6;
581 memcpy(&sa->sin6_addr, SELFLD(dir, ip6_src_addr, ip6_dst_addr),
582 sizeof(SELFLD(dir, ip6_src_addr, ip6_dst_addr)));
584 return sa;
587 static void
588 flow_entry_geo_city_lookup_generic(struct flow_entry *n,
589 enum flow_entry_direction dir)
591 struct sockaddr_in sa4;
592 struct sockaddr_in6 sa6;
593 const char *city = NULL;
595 switch (n->l3_proto) {
596 default:
597 bug();
599 case AF_INET:
600 flow_entry_get_sain4_obj(n, dir, &sa4);
601 city = geoip4_city_name(sa4);
602 break;
604 case AF_INET6:
605 flow_entry_get_sain6_obj(n, dir, &sa6);
606 city = geoip6_city_name(sa6);
607 break;
610 bug_on(sizeof(n->city_src) != sizeof(n->city_dst));
612 if (city) {
613 memcpy(SELFLD(dir, city_src, city_dst), city,
614 min(sizeof(n->city_src), strlen(city)));
615 } else {
616 memset(SELFLD(dir, city_src, city_dst), 0,
617 sizeof(n->city_src));
621 static void
622 flow_entry_geo_country_lookup_generic(struct flow_entry *n,
623 enum flow_entry_direction dir)
625 struct sockaddr_in sa4;
626 struct sockaddr_in6 sa6;
627 const char *country = NULL;
629 switch (n->l3_proto) {
630 default:
631 bug();
633 case AF_INET:
634 flow_entry_get_sain4_obj(n, dir, &sa4);
635 country = geoip4_country_name(sa4);
636 break;
638 case AF_INET6:
639 flow_entry_get_sain6_obj(n, dir, &sa6);
640 country = geoip6_country_name(sa6);
641 break;
644 bug_on(sizeof(n->country_src) != sizeof(n->country_dst));
646 if (country) {
647 memcpy(SELFLD(dir, country_src, country_dst), country,
648 min(sizeof(n->country_src), strlen(country)));
649 } else {
650 memset(SELFLD(dir, country_src, country_dst), 0,
651 sizeof(n->country_src));
655 static void flow_entry_get_extended_geo(struct flow_entry *n,
656 enum flow_entry_direction dir)
658 flow_entry_geo_city_lookup_generic(n, dir);
659 flow_entry_geo_country_lookup_generic(n, dir);
662 static void flow_entry_get_extended_revdns(struct flow_entry *n,
663 enum flow_entry_direction dir)
665 size_t sa_len;
666 struct sockaddr_in sa4;
667 struct sockaddr_in6 sa6;
668 struct sockaddr *sa;
669 struct hostent *hent;
671 switch (n->l3_proto) {
672 default:
673 bug();
675 case AF_INET:
676 flow_entry_get_sain4_obj(n, dir, &sa4);
677 sa = (struct sockaddr *) &sa4;
678 sa_len = sizeof(sa4);
679 hent = gethostbyaddr(&sa4.sin_addr, sizeof(sa4.sin_addr), AF_INET);
680 break;
682 case AF_INET6:
683 flow_entry_get_sain6_obj(n, dir, &sa6);
684 sa = (struct sockaddr *) &sa6;
685 sa_len = sizeof(sa6);
686 hent = gethostbyaddr(&sa6.sin6_addr, sizeof(sa6.sin6_addr), AF_INET6);
687 break;
690 bug_on(sizeof(n->rev_dns_src) != sizeof(n->rev_dns_dst));
691 getnameinfo(sa, sa_len, SELFLD(dir, rev_dns_src, rev_dns_dst),
692 sizeof(n->rev_dns_src), NULL, 0, NI_NUMERICHOST);
694 if (hent) {
695 memset(n->rev_dns_dst, 0, sizeof(n->rev_dns_dst));
696 memcpy(SELFLD(dir, rev_dns_src, rev_dns_dst),
697 hent->h_name, min(sizeof(n->rev_dns_src),
698 strlen(hent->h_name)));
702 static void flow_entry_get_extended(struct flow_entry *n)
704 if (n->flow_id == 0 || flow_entry_get_extended_is_dns(n))
705 return;
707 flow_entry_get_extended_revdns(n, flow_entry_src);
708 flow_entry_get_extended_geo(n, flow_entry_src);
710 flow_entry_get_extended_revdns(n, flow_entry_dst);
711 flow_entry_get_extended_geo(n, flow_entry_dst);
713 /* Lookup application */
714 n->inode = get_port_inode(n->port_src, n->l4_proto,
715 n->l3_proto == AF_INET6);
716 if (n->inode > 0)
717 walk_processes(n);
720 static uint16_t presenter_get_port(uint16_t src, uint16_t dst, int tcp)
722 if (src < dst && src < 1024) {
723 return src;
724 } else if (dst < src && dst < 1024) {
725 return dst;
726 } else {
727 const char *tmp1, *tmp2;
728 if (tcp) {
729 tmp1 = lookup_port_tcp(src);
730 tmp2 = lookup_port_tcp(dst);
731 } else {
732 tmp1 = lookup_port_udp(src);
733 tmp2 = lookup_port_udp(dst);
735 if (tmp1 && !tmp2) {
736 return src;
737 } else if (!tmp1 && tmp2) {
738 return dst;
739 } else {
740 if (src < dst)
741 return src;
742 else
743 return dst;
748 static void presenter_screen_init(WINDOW **screen)
750 (*screen) = initscr();
751 noecho();
752 cbreak();
753 keypad(stdscr, TRUE);
754 nodelay(*screen, TRUE);
755 refresh();
756 wrefresh(*screen);
759 static void presenter_screen_do_line(WINDOW *screen, struct flow_entry *n,
760 unsigned int *line)
762 char tmp[128], *pname = NULL;
763 uint16_t port;
765 mvwprintw(screen, *line, 2, "");
767 /* PID, application name */
768 if (n->procnum > 0) {
769 slprintf(tmp, sizeof(tmp), "%s(%u)", basename(n->cmdline),
770 n->procnum);
772 printw("[");
773 attron(COLOR_PAIR(3));
774 printw("%s", tmp);
775 attroff(COLOR_PAIR(3));
776 printw("]:");
779 /* L3 protocol, L4 protocol, states */
780 printw("%s:%s", l3proto2str[n->l3_proto], l4proto2str[n->l4_proto]);
781 printw("[");
782 attron(COLOR_PAIR(3));
783 switch (n->l4_proto) {
784 case IPPROTO_TCP:
785 printw("%s", tcp_state2str[n->tcp_state]);
786 break;
787 case IPPROTO_SCTP:
788 printw("%s", sctp_state2str[n->sctp_state]);
789 break;
790 case IPPROTO_DCCP:
791 printw("%s", dccp_state2str[n->dccp_state]);
792 break;
793 case IPPROTO_UDP:
794 case IPPROTO_UDPLITE:
795 case IPPROTO_ICMP:
796 case IPPROTO_ICMPV6:
797 printw("NOSTATE");
798 break;
800 attroff(COLOR_PAIR(3));
801 printw("]");
803 /* Guess application port */
804 switch (n->l4_proto) {
805 case IPPROTO_TCP:
806 port = presenter_get_port(n->port_src, n->port_dst, 1);
807 pname = lookup_port_tcp(port);
808 break;
809 case IPPROTO_UDP:
810 case IPPROTO_UDPLITE:
811 port = presenter_get_port(n->port_src, n->port_dst, 0);
812 pname = lookup_port_udp(port);
813 break;
815 if (pname) {
816 attron(A_BOLD);
817 printw(":%s", pname);
818 attroff(A_BOLD);
820 printw(" ->");
822 /* Number packets, bytes */
823 if (n->counter_pkts > 0 && n->counter_bytes > 0)
824 printw(" (%llu pkts, %llu bytes) ->",
825 n->counter_pkts, n->counter_bytes);
827 /* Show source information: reverse DNS, port, country, city */
828 if (show_src) {
829 attron(COLOR_PAIR(1));
830 mvwprintw(screen, ++(*line), 8, "src: %s", n->rev_dns_src);
831 attroff(COLOR_PAIR(1));
833 printw(":%u", n->port_src);
835 if (n->country_src[0]) {
836 printw(" (");
838 attron(COLOR_PAIR(4));
839 printw("%s", n->country_src);
840 attroff(COLOR_PAIR(4));
842 if (n->city_src[0])
843 printw(", %s", n->city_src);
845 printw(")");
848 printw(" => ");
851 /* Show dest information: reverse DNS, port, country, city */
852 attron(COLOR_PAIR(2));
853 mvwprintw(screen, ++(*line), 8, "dst: %s", n->rev_dns_dst);
854 attroff(COLOR_PAIR(2));
856 printw(":%u", n->port_dst);
858 if (n->country_dst[0]) {
859 printw(" (");
861 attron(COLOR_PAIR(4));
862 printw("%s", n->country_dst);
863 attroff(COLOR_PAIR(4));
865 if (n->city_dst[0])
866 printw(", %s", n->city_dst);
868 printw(")");
872 static inline int presenter_flow_wrong_state(struct flow_entry *n, int state)
874 int ret = 1;
876 switch (n->l4_proto) {
877 case IPPROTO_TCP:
878 if (n->tcp_state == state)
879 ret = 0;
880 break;
881 case IPPROTO_SCTP:
882 if (n->sctp_state == state)
883 ret = 0;
884 break;
885 case IPPROTO_DCCP:
886 if (n->dccp_state == state)
887 ret = 0;
888 break;
889 case IPPROTO_UDP:
890 case IPPROTO_UDPLITE:
891 case IPPROTO_ICMP:
892 case IPPROTO_ICMPV6:
893 ret = 0;
894 break;
897 return ret;
900 static void presenter_screen_update(WINDOW *screen, struct flow_list *fl,
901 int skip_lines)
903 int i, j, maxy;
904 unsigned int line = 3;
905 struct flow_entry *n;
906 uint8_t protocols[] = {
907 IPPROTO_TCP,
908 IPPROTO_DCCP,
909 IPPROTO_SCTP,
910 IPPROTO_UDP,
911 IPPROTO_UDPLITE,
912 IPPROTO_ICMP,
913 IPPROTO_ICMPV6,
915 size_t protocol_state_size[] = {
916 [IPPROTO_TCP] = array_size(tcp_states),
917 [IPPROTO_DCCP] = array_size(dccp_states),
918 [IPPROTO_SCTP] = array_size(sctp_states),
919 [IPPROTO_UDP] = 1,
920 [IPPROTO_UDPLITE] = 1,
921 [IPPROTO_ICMP] = 1,
922 [IPPROTO_ICMPV6] = 1,
925 curs_set(0);
927 maxy = getmaxy(screen);
928 maxy -= 6;
930 start_color();
931 init_pair(1, COLOR_RED, COLOR_BLACK);
932 init_pair(2, COLOR_BLUE, COLOR_BLACK);
933 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
934 init_pair(4, COLOR_GREEN, COLOR_BLACK);
936 wclear(screen);
937 clear();
939 mvwprintw(screen, 1, 2, "Kernel netfilter flows for %s%s%s%s%s%s"
940 "[+%d]", what & INCLUDE_TCP ? "TCP, " : "" ,
941 what & INCLUDE_UDP ? "UDP, " : "",
942 what & INCLUDE_SCTP ? "SCTP, " : "",
943 what & INCLUDE_DCCP ? "DCCP, " : "",
944 what & INCLUDE_ICMP && what & INCLUDE_IPV4 ? "ICMP, " : "",
945 what & INCLUDE_ICMP && what & INCLUDE_IPV6 ? "ICMP6, " : "",
946 skip_lines);
948 rcu_read_lock();
950 if (rcu_dereference(fl->head) == NULL)
951 mvwprintw(screen, line, 2, "(No active sessions! "
952 "Is netfilter running?)");
954 for (i = 0; i < array_size(protocols); i++) {
955 for (j = 0; j < protocol_state_size[protocols[i]]; j++) {
956 n = rcu_dereference(fl->head);
957 while (n && maxy > 0) {
958 int skip_entry = 0;
960 if (n->l4_proto != protocols[i])
961 skip_entry = 1;
962 if (presenter_flow_wrong_state(n, j))
963 skip_entry = 1;
964 if (presenter_get_port(n->port_src,
965 n->port_dst, 0) == 53)
966 skip_entry = 1;
967 if (skip_entry) {
968 n = rcu_dereference(n->next);
969 continue;
971 if (skip_lines > 0) {
972 n = rcu_dereference(n->next);
973 skip_lines--;
974 continue;
977 presenter_screen_do_line(screen, n, &line);
979 line++;
980 maxy -= (2 + 1 * show_src);
981 n = rcu_dereference(n->next);
986 rcu_read_unlock();
988 wrefresh(screen);
989 refresh();
992 static inline void presenter_screen_end(void)
994 endwin();
997 static void presenter(void)
999 int skip_lines = 0;
1000 WINDOW *screen = NULL;
1002 dissector_init_ethernet(0);
1003 presenter_screen_init(&screen);
1005 rcu_register_thread();
1006 while (!sigint) {
1007 switch (getch()) {
1008 case 'q':
1009 sigint = 1;
1010 break;
1011 case KEY_UP:
1012 case 'u':
1013 case 'k':
1014 skip_lines--;
1015 if (skip_lines < 0)
1016 skip_lines = 0;
1017 break;
1018 case KEY_DOWN:
1019 case 'd':
1020 case 'j':
1021 skip_lines++;
1022 if (skip_lines > SCROLL_MAX)
1023 skip_lines = SCROLL_MAX;
1024 break;
1025 default:
1026 fflush(stdin);
1027 break;
1030 presenter_screen_update(screen, &flow_list, skip_lines);
1031 usleep(100000);
1033 rcu_unregister_thread();
1035 presenter_screen_end();
1036 dissector_cleanup_ethernet();
1039 static int collector_cb(enum nf_conntrack_msg_type type,
1040 struct nf_conntrack *ct, void *data)
1042 if (sigint)
1043 return NFCT_CB_STOP;
1045 synchronize_rcu();
1046 spinlock_lock(&flow_list.lock);
1048 switch (type) {
1049 case NFCT_T_NEW:
1050 flow_list_new_entry(&flow_list, ct);
1051 break;
1052 case NFCT_T_UPDATE:
1053 flow_list_update_entry(&flow_list, ct);
1054 break;
1055 case NFCT_T_DESTROY:
1056 flow_list_destroy_entry(&flow_list, ct);
1057 break;
1058 default:
1059 break;
1062 spinlock_unlock(&flow_list.lock);
1064 return NFCT_CB_CONTINUE;
1067 static inline void collector_flush(struct nfct_handle *handle, uint8_t family)
1069 nfct_query(handle, NFCT_Q_FLUSH, &family);
1072 static void *collector(void *null)
1074 int ret;
1075 struct nfct_handle *handle;
1076 struct nfct_filter *filter;
1078 handle = nfct_open(CONNTRACK, NF_NETLINK_CONNTRACK_NEW |
1079 NF_NETLINK_CONNTRACK_UPDATE |
1080 NF_NETLINK_CONNTRACK_DESTROY);
1081 if (!handle)
1082 panic("Cannot create a nfct handle!\n");
1084 collector_flush(handle, AF_INET);
1085 collector_flush(handle, AF_INET6);
1087 filter = nfct_filter_create();
1088 if (!filter)
1089 panic("Cannot create a nfct filter!\n");
1091 ret = nfct_filter_attach(nfct_fd(handle), filter);
1092 if (ret < 0)
1093 panic("Cannot attach filter to handle!\n");
1095 if (what & INCLUDE_UDP) {
1096 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_UDP);
1097 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_UDPLITE);
1099 if (what & INCLUDE_TCP)
1100 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_TCP);
1101 if (what & INCLUDE_DCCP)
1102 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_DCCP);
1103 if (what & INCLUDE_SCTP)
1104 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_SCTP);
1105 if (what & INCLUDE_ICMP && what & INCLUDE_IPV4)
1106 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_ICMP);
1107 if (what & INCLUDE_ICMP && what & INCLUDE_IPV6)
1108 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_ICMPV6);
1109 if (what & INCLUDE_IPV4) {
1110 nfct_filter_set_logic(filter, NFCT_FILTER_SRC_IPV4, NFCT_FILTER_LOGIC_NEGATIVE);
1111 nfct_filter_add_attr(filter, NFCT_FILTER_SRC_IPV4, &filter_ipv4);
1113 if (what & INCLUDE_IPV6) {
1114 nfct_filter_set_logic(filter, NFCT_FILTER_SRC_IPV6, NFCT_FILTER_LOGIC_NEGATIVE);
1115 nfct_filter_add_attr(filter, NFCT_FILTER_SRC_IPV6, &filter_ipv6);
1118 ret = nfct_filter_attach(nfct_fd(handle), filter);
1119 if (ret < 0)
1120 panic("Cannot attach filter to handle!\n");
1122 nfct_callback_register(handle, NFCT_T_ALL, collector_cb, NULL);
1123 nfct_filter_destroy(filter);
1124 flow_list_init(&flow_list);
1126 rcu_register_thread();
1128 while (!sigint && ret >= 0)
1129 ret = nfct_catch(handle);
1131 rcu_unregister_thread();
1133 flow_list_destroy(&flow_list);
1134 nfct_close(handle);
1136 pthread_exit(0);
1139 int main(int argc, char **argv)
1141 pthread_t tid;
1142 int ret, c, opt_index, what_cmd = 0;
1144 setfsuid(getuid());
1145 setfsgid(getgid());
1147 while ((c = getopt_long(argc, argv, short_options, long_options,
1148 &opt_index)) != EOF) {
1149 switch (c) {
1150 case '4':
1151 what_cmd |= INCLUDE_IPV4;
1152 break;
1153 case '6':
1154 what_cmd |= INCLUDE_IPV6;
1155 break;
1156 case 'T':
1157 what_cmd |= INCLUDE_TCP;
1158 break;
1159 case 'U':
1160 what_cmd |= INCLUDE_UDP;
1161 break;
1162 case 'D':
1163 what_cmd |= INCLUDE_DCCP;
1164 break;
1165 case 'I':
1166 what_cmd |= INCLUDE_ICMP;
1167 break;
1168 case 'S':
1169 what_cmd |= INCLUDE_SCTP;
1170 break;
1171 case 's':
1172 show_src = 1;
1173 break;
1174 case 'u':
1175 update_geoip();
1176 die();
1177 break;
1178 case 'h':
1179 help();
1180 break;
1181 case 'v':
1182 version();
1183 break;
1184 default:
1185 break;
1189 if (what_cmd > 0)
1190 what = what_cmd;
1192 rcu_init();
1194 register_signal(SIGINT, signal_handler);
1195 register_signal(SIGHUP, signal_handler);
1197 init_geoip(1);
1199 ret = pthread_create(&tid, NULL, collector, NULL);
1200 if (ret < 0)
1201 panic("Cannot create phthread!\n");
1203 presenter();
1205 destroy_geoip();
1207 return 0;