config: check for available databases
[netsniff-ng.git] / flowtop.c
blobef691aa6e7f6cbff9a88c5a81bf3c8cc52ffffbb
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 <netinet/in.h>
19 #include <curses.h>
20 #include <dirent.h>
21 #include <sys/stat.h>
22 #include <sys/fsuid.h>
23 #include <urcu.h>
24 #include <libgen.h>
26 #include "die.h"
27 #include "xmalloc.h"
28 #include "conntrack.h"
29 #include "ioops.h"
30 #include "str.h"
31 #include "sig.h"
32 #include "geoip.h"
33 #include "built_in.h"
34 #include "locking.h"
35 #include "dissector_eth.h"
36 #include "pkt_buff.h"
37 #include "screen.h"
39 struct flow_entry {
40 uint32_t flow_id, use, status;
41 uint8_t l3_proto, l4_proto;
42 uint32_t ip4_src_addr, ip4_dst_addr;
43 uint32_t ip6_src_addr[4], ip6_dst_addr[4];
44 uint16_t port_src, port_dst;
45 uint8_t tcp_state, tcp_flags, sctp_state, dccp_state;
46 uint64_t counter_pkts, counter_bytes;
47 uint64_t timestamp_start, timestamp_stop;
48 char country_src[128], country_dst[128];
49 char city_src[128], city_dst[128];
50 char rev_dns_src[256], rev_dns_dst[256];
51 char cmdline[256];
52 struct flow_entry *next;
53 int procnum, inode;
56 struct flow_list {
57 struct flow_entry *head;
58 struct spinlock lock;
61 #ifndef ATTR_TIMESTAMP_START
62 # define ATTR_TIMESTAMP_START 63
63 #endif
64 #ifndef ATTR_TIMESTAMP_STOP
65 # define ATTR_TIMESTAMP_STOP 64
66 #endif
68 #define SCROLL_MAX 1000
70 #define INCLUDE_IPV4 (1 << 0)
71 #define INCLUDE_IPV6 (1 << 1)
72 #define INCLUDE_UDP (1 << 2)
73 #define INCLUDE_TCP (1 << 3)
74 #define INCLUDE_DCCP (1 << 4)
75 #define INCLUDE_ICMP (1 << 5)
76 #define INCLUDE_SCTP (1 << 6)
78 static volatile sig_atomic_t sigint = 0;
79 static int what = INCLUDE_IPV4 | INCLUDE_IPV6 | INCLUDE_TCP, show_src = 0;
80 static struct flow_list flow_list;
82 static const char *short_options = "vhTUsDIS46u";
83 static const struct option long_options[] = {
84 {"ipv4", no_argument, NULL, '4'},
85 {"ipv6", no_argument, NULL, '6'},
86 {"tcp", no_argument, NULL, 'T'},
87 {"udp", no_argument, NULL, 'U'},
88 {"dccp", no_argument, NULL, 'D'},
89 {"icmp", no_argument, NULL, 'I'},
90 {"sctp", no_argument, NULL, 'S'},
91 {"show-src", no_argument, NULL, 's'},
92 {"update", no_argument, NULL, 'u'},
93 {"version", no_argument, NULL, 'v'},
94 {"help", no_argument, NULL, 'h'},
95 {NULL, 0, NULL, 0}
98 static const char *const l3proto2str[AF_MAX] = {
99 [AF_INET] = "ipv4",
100 [AF_INET6] = "ipv6",
103 static const char *const l4proto2str[IPPROTO_MAX] = {
104 [IPPROTO_TCP] = "tcp",
105 [IPPROTO_UDP] = "udp",
106 [IPPROTO_UDPLITE] = "udplite",
107 [IPPROTO_ICMP] = "icmp",
108 [IPPROTO_ICMPV6] = "icmpv6",
109 [IPPROTO_SCTP] = "sctp",
110 [IPPROTO_GRE] = "gre",
111 [IPPROTO_DCCP] = "dccp",
112 [IPPROTO_IGMP] = "igmp",
113 [IPPROTO_IPIP] = "ipip",
114 [IPPROTO_EGP] = "egp",
115 [IPPROTO_PUP] = "pup",
116 [IPPROTO_IDP] = "idp",
117 [IPPROTO_RSVP] = "rsvp",
118 [IPPROTO_IPV6] = "ip6tun",
119 [IPPROTO_ESP] = "esp",
120 [IPPROTO_AH] = "ah",
121 [IPPROTO_PIM] = "pim",
122 [IPPROTO_COMP] = "comp",
125 static const char *const tcp_state2str[TCP_CONNTRACK_MAX] = {
126 [TCP_CONNTRACK_NONE] = "NOSTATE",
127 [TCP_CONNTRACK_SYN_SENT] = "SYN_SENT",
128 [TCP_CONNTRACK_SYN_RECV] = "SYN_RECV",
129 [TCP_CONNTRACK_ESTABLISHED] = "ESTABLISHED",
130 [TCP_CONNTRACK_FIN_WAIT] = "FIN_WAIT",
131 [TCP_CONNTRACK_CLOSE_WAIT] = "CLOSE_WAIT",
132 [TCP_CONNTRACK_LAST_ACK] = "LAST_ACK",
133 [TCP_CONNTRACK_TIME_WAIT] = "TIME_WAIT",
134 [TCP_CONNTRACK_CLOSE] = "CLOSE",
135 [TCP_CONNTRACK_SYN_SENT2] = "SYN_SENT2",
138 static const uint8_t tcp_states[] = {
139 TCP_CONNTRACK_SYN_SENT,
140 TCP_CONNTRACK_SYN_RECV,
141 TCP_CONNTRACK_ESTABLISHED,
142 TCP_CONNTRACK_FIN_WAIT,
143 TCP_CONNTRACK_CLOSE_WAIT,
144 TCP_CONNTRACK_LAST_ACK,
145 TCP_CONNTRACK_TIME_WAIT,
146 TCP_CONNTRACK_CLOSE,
147 TCP_CONNTRACK_SYN_SENT2,
148 TCP_CONNTRACK_NONE,
151 static const char *const dccp_state2str[DCCP_CONNTRACK_MAX] = {
152 [DCCP_CONNTRACK_NONE] = "NOSTATE",
153 [DCCP_CONNTRACK_REQUEST] = "REQUEST",
154 [DCCP_CONNTRACK_RESPOND] = "RESPOND",
155 [DCCP_CONNTRACK_PARTOPEN] = "PARTOPEN",
156 [DCCP_CONNTRACK_OPEN] = "OPEN",
157 [DCCP_CONNTRACK_CLOSEREQ] = "CLOSEREQ",
158 [DCCP_CONNTRACK_CLOSING] = "CLOSING",
159 [DCCP_CONNTRACK_TIMEWAIT] = "TIMEWAIT",
160 [DCCP_CONNTRACK_IGNORE] = "IGNORE",
161 [DCCP_CONNTRACK_INVALID] = "INVALID",
164 static const uint8_t dccp_states[] = {
165 DCCP_CONNTRACK_NONE,
166 DCCP_CONNTRACK_REQUEST,
167 DCCP_CONNTRACK_RESPOND,
168 DCCP_CONNTRACK_PARTOPEN,
169 DCCP_CONNTRACK_OPEN,
170 DCCP_CONNTRACK_CLOSEREQ,
171 DCCP_CONNTRACK_CLOSING,
172 DCCP_CONNTRACK_TIMEWAIT,
173 DCCP_CONNTRACK_IGNORE,
174 DCCP_CONNTRACK_INVALID,
177 static const char *const sctp_state2str[SCTP_CONNTRACK_MAX] = {
178 [SCTP_CONNTRACK_NONE] = "NOSTATE",
179 [SCTP_CONNTRACK_CLOSED] = "CLOSED",
180 [SCTP_CONNTRACK_COOKIE_WAIT] = "COOKIE_WAIT",
181 [SCTP_CONNTRACK_COOKIE_ECHOED] = "COOKIE_ECHOED",
182 [SCTP_CONNTRACK_ESTABLISHED] = "ESTABLISHED",
183 [SCTP_CONNTRACK_SHUTDOWN_SENT] = "SHUTDOWN_SENT",
184 [SCTP_CONNTRACK_SHUTDOWN_RECD] = "SHUTDOWN_RECD",
185 [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = "SHUTDOWN_ACK_SENT",
188 static const uint8_t sctp_states[] = {
189 SCTP_CONNTRACK_NONE,
190 SCTP_CONNTRACK_CLOSED,
191 SCTP_CONNTRACK_COOKIE_WAIT,
192 SCTP_CONNTRACK_COOKIE_ECHOED,
193 SCTP_CONNTRACK_ESTABLISHED,
194 SCTP_CONNTRACK_SHUTDOWN_SENT,
195 SCTP_CONNTRACK_SHUTDOWN_RECD,
196 SCTP_CONNTRACK_SHUTDOWN_ACK_SENT,
199 static const struct nfct_filter_ipv4 filter_ipv4 = {
200 .addr = __constant_htonl(INADDR_LOOPBACK),
201 .mask = 0xffffffff,
204 static const struct nfct_filter_ipv6 filter_ipv6 = {
205 .addr = { 0x0, 0x0, 0x0, 0x1 },
206 .mask = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff },
209 static void signal_handler(int number)
211 switch (number) {
212 case SIGINT:
213 sigint = 1;
214 break;
215 case SIGHUP:
216 default:
217 break;
221 static void flow_entry_from_ct(struct flow_entry *n, struct nf_conntrack *ct);
222 static void flow_entry_get_extended(struct flow_entry *n);
224 static void help(void)
226 printf("\nflowtop %s, top-like netfilter TCP/UDP flow tracking\n",
227 VERSION_STRING);
228 puts("http://www.netsniff-ng.org\n\n"
229 "Usage: flowtop [options]\n"
230 "Options:\n"
231 " -4|--ipv4 Show only IPv4 flows (default)\n"
232 " -6|--ipv6 Show only IPv6 flows (default)\n"
233 " -T|--tcp Show only TCP flows (default)\n"
234 " -U|--udp Show only UDP flows\n"
235 " -D|--dccp Show only DCCP flows\n"
236 " -I|--icmp Show only ICMP/ICMPv6 flows\n"
237 " -S|--sctp Show only SCTP flows\n"
238 " -s|--show-src Also show source, not only dest\n"
239 " -u|--update Update GeoIP databases\n"
240 " -v|--version Print version and exit\n"
241 " -h|--help Print this help and exit\n\n"
242 "Examples:\n"
243 " flowtop\n"
244 " flowtop -46UTDISs\n\n"
245 "Note:\n"
246 " If netfilter is not running, you can activate it with e.g.:\n"
247 " iptables -A INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT\n"
248 " iptables -A OUTPUT -p tcp -m state --state NEW,ESTABLISHED -j ACCEPT\n\n"
249 "Please report bugs to <bugs@netsniff-ng.org>\n"
250 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
251 "Copyright (C) 2011-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
252 "Swiss federal institute of technology (ETH Zurich)\n"
253 "License: GNU GPL version 2.0\n"
254 "This is free software: you are free to change and redistribute it.\n"
255 "There is NO WARRANTY, to the extent permitted by law.\n");
256 die();
259 static void version(void)
261 printf("\nflowtop %s, top-like netfilter TCP/UDP flow tracking\n",
262 VERSION_LONG);
263 puts("http://www.netsniff-ng.org\n\n"
264 "Please report bugs to <bugs@netsniff-ng.org>\n"
265 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
266 "Copyright (C) 2011-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
267 "Swiss federal institute of technology (ETH Zurich)\n"
268 "License: GNU GPL version 2.0\n"
269 "This is free software: you are free to change and redistribute it.\n"
270 "There is NO WARRANTY, to the extent permitted by law.\n");
271 die();
274 static inline struct flow_entry *flow_entry_xalloc(void)
276 return xzmalloc(sizeof(struct flow_entry));
279 static inline void flow_entry_xfree(struct flow_entry *n)
281 xfree(n);
284 static inline void flow_list_init(struct flow_list *fl)
286 fl->head = NULL;
287 spinlock_init(&fl->lock);
290 static void flow_list_new_entry(struct flow_list *fl, struct nf_conntrack *ct)
292 struct flow_entry *n = flow_entry_xalloc();
294 flow_entry_from_ct(n, ct);
295 flow_entry_get_extended(n);
297 rcu_assign_pointer(n->next, fl->head);
298 rcu_assign_pointer(fl->head, n);
301 static struct flow_entry *flow_list_find_id(struct flow_list *fl,
302 uint32_t id)
304 struct flow_entry *n = rcu_dereference(fl->head);
306 while (n != NULL) {
307 if (n->flow_id == id)
308 return n;
310 n = rcu_dereference(n->next);
313 return NULL;
316 static struct flow_entry *flow_list_find_prev_id(struct flow_list *fl,
317 uint32_t id)
319 struct flow_entry *n = rcu_dereference(fl->head), *tmp;
321 if (n->flow_id == id)
322 return NULL;
324 while ((tmp = rcu_dereference(n->next)) != NULL) {
325 if (tmp->flow_id == id)
326 return n;
328 n = tmp;
331 return NULL;
334 static void flow_list_update_entry(struct flow_list *fl,
335 struct nf_conntrack *ct)
337 int do_ext = 0;
338 struct flow_entry *n;
340 n = flow_list_find_id(fl, nfct_get_attr_u32(ct, ATTR_ID));
341 if (n == NULL) {
342 n = flow_entry_xalloc();
343 do_ext = 1;
346 flow_entry_from_ct(n, ct);
347 if (do_ext) {
348 flow_entry_get_extended(n);
350 rcu_assign_pointer(n->next, fl->head);
351 rcu_assign_pointer(fl->head, n);
355 static void flow_list_destroy_entry(struct flow_list *fl,
356 struct nf_conntrack *ct)
358 struct flow_entry *n1, *n2;
359 uint32_t id = nfct_get_attr_u32(ct, ATTR_ID);
361 n1 = flow_list_find_id(fl, id);
362 if (n1) {
363 n2 = flow_list_find_prev_id(fl, id);
364 if (n2) {
365 rcu_assign_pointer(n2->next, n1->next);
366 n1->next = NULL;
368 flow_entry_xfree(n1);
369 } else {
370 flow_entry_xfree(fl->head);
371 fl->head = NULL;
376 static void flow_list_destroy(struct flow_list *fl)
378 struct flow_entry *n;
380 while (fl->head != NULL) {
381 n = rcu_dereference(fl->head->next);
382 fl->head->next = NULL;
384 flow_entry_xfree(fl->head);
385 rcu_assign_pointer(fl->head, n);
388 synchronize_rcu();
389 spinlock_destroy(&fl->lock);
392 static int walk_process(char *process, struct flow_entry *n)
394 int ret;
395 DIR *dir;
396 struct dirent *ent;
397 char path[1024];
399 if (snprintf(path, sizeof(path), "/proc/%s/fd", process) == -1)
400 panic("giant process name! %s\n", process);
402 dir = opendir(path);
403 if (!dir)
404 return 0;
406 while ((ent = readdir(dir))) {
407 struct stat statbuf;
409 if (snprintf(path, sizeof(path), "/proc/%s/fd/%s",
410 process, ent->d_name) < 0)
411 continue;
413 if (stat(path, &statbuf) < 0)
414 continue;
416 if (S_ISSOCK(statbuf.st_mode) && n->inode == statbuf.st_ino) {
417 memset(n->cmdline, 0, sizeof(n->cmdline));
419 snprintf(path, sizeof(path), "/proc/%s/exe", process);
421 ret = readlink(path, n->cmdline,
422 sizeof(n->cmdline) - 1);
423 if (ret < 0)
424 panic("readlink error: %s\n", strerror(errno));
426 n->procnum = atoi(process);
427 closedir(dir);
428 return 1;
432 closedir(dir);
433 return 0;
436 static void walk_processes(struct flow_entry *n)
438 int ret;
439 DIR *dir;
440 struct dirent *ent;
442 /* n->inode must be set */
443 if (n->inode <= 0) {
444 memset(n->cmdline, 0, sizeof(n->cmdline));
445 return;
448 dir = opendir("/proc");
449 if (!dir)
450 panic("Cannot open /proc!\n");
452 while ((ent = readdir(dir))) {
453 if (strspn(ent->d_name, "0123456789") == strlen(ent->d_name)) {
454 ret = walk_process(ent->d_name, n);
455 if (ret > 0)
456 break;
460 closedir(dir);
463 static int get_port_inode(uint16_t port, int proto, int is_ip6)
465 int ret = -ENOENT;
466 char path[128], buff[1024];
467 FILE *proc;
469 memset(path, 0, sizeof(path));
470 snprintf(path, sizeof(path), "/proc/net/%s%s",
471 l4proto2str[proto], is_ip6 ? "6" : "");
473 proc = fopen(path, "r");
474 if (!proc)
475 return -EIO;
477 memset(buff, 0, sizeof(buff));
479 while (fgets(buff, sizeof(buff), proc) != NULL) {
480 int inode = 0;
481 unsigned int lport = 0;
483 buff[sizeof(buff) - 1] = 0;
484 if (sscanf(buff, "%*u: %*X:%X %*X:%*X %*X %*X:%*X %*X:%*X "
485 "%*X %*u %*u %u", &lport, &inode) == 2) {
486 if ((uint16_t) lport == port) {
487 ret = inode;
488 break;
492 memset(buff, 0, sizeof(buff));
495 fclose(proc);
496 return ret;
499 #define CP_NFCT(elem, attr, x) \
500 do { n->elem = nfct_get_attr_u##x(ct,(attr)); } while (0)
501 #define CP_NFCT_BUFF(elem, attr) do { \
502 const uint8_t *buff = nfct_get_attr(ct,(attr)); \
503 if (buff != NULL) \
504 memcpy(n->elem, buff, sizeof(n->elem)); \
505 } while (0)
507 static void flow_entry_from_ct(struct flow_entry *n, struct nf_conntrack *ct)
509 CP_NFCT(l3_proto, ATTR_ORIG_L3PROTO, 8);
510 CP_NFCT(l4_proto, ATTR_ORIG_L4PROTO, 8);
512 CP_NFCT(ip4_src_addr, ATTR_ORIG_IPV4_SRC, 32);
513 CP_NFCT(ip4_dst_addr, ATTR_ORIG_IPV4_DST, 32);
515 CP_NFCT(port_src, ATTR_ORIG_PORT_SRC, 16);
516 CP_NFCT(port_dst, ATTR_ORIG_PORT_DST, 16);
518 CP_NFCT(status, ATTR_STATUS, 32);
520 CP_NFCT(tcp_state, ATTR_TCP_STATE, 8);
521 CP_NFCT(tcp_flags, ATTR_TCP_FLAGS_ORIG, 8);
522 CP_NFCT(sctp_state, ATTR_SCTP_STATE, 8);
523 CP_NFCT(dccp_state, ATTR_DCCP_STATE, 8);
525 CP_NFCT(counter_pkts, ATTR_ORIG_COUNTER_PACKETS, 64);
526 CP_NFCT(counter_bytes, ATTR_ORIG_COUNTER_BYTES, 64);
528 CP_NFCT(timestamp_start, ATTR_TIMESTAMP_START, 64);
529 CP_NFCT(timestamp_stop, ATTR_TIMESTAMP_STOP, 64);
531 CP_NFCT(flow_id, ATTR_ID, 32);
532 CP_NFCT(use, ATTR_USE, 32);
534 CP_NFCT_BUFF(ip6_src_addr, ATTR_ORIG_IPV6_SRC);
535 CP_NFCT_BUFF(ip6_dst_addr, ATTR_ORIG_IPV6_DST);
537 n->port_src = ntohs(n->port_src);
538 n->port_dst = ntohs(n->port_dst);
540 n->ip4_src_addr = ntohl(n->ip4_src_addr);
541 n->ip4_dst_addr = ntohl(n->ip4_dst_addr);
544 enum flow_entry_direction {
545 flow_entry_src,
546 flow_entry_dst,
549 static inline int flow_entry_get_extended_is_dns(struct flow_entry *n)
551 /* We don't want to analyze / display DNS itself, since we
552 * use it to resolve reverse dns.
554 return n->port_src == 53 || n->port_dst == 53;
557 #define SELFLD(dir,src_member,dst_member) \
558 (((dir) == flow_entry_src) ? n->src_member : n->dst_member)
560 static void flow_entry_get_sain4_obj(struct flow_entry *n,
561 enum flow_entry_direction dir,
562 struct sockaddr_in *sa)
564 memset(sa, 0, sizeof(*sa));
565 sa->sin_family = PF_INET;
566 sa->sin_addr.s_addr = htonl(SELFLD(dir, ip4_src_addr, ip4_dst_addr));
569 static void flow_entry_get_sain6_obj(struct flow_entry *n,
570 enum flow_entry_direction dir,
571 struct sockaddr_in6 *sa)
573 memset(sa, 0, sizeof(*sa));
574 sa->sin6_family = PF_INET6;
576 memcpy(&sa->sin6_addr, SELFLD(dir, ip6_src_addr, ip6_dst_addr),
577 sizeof(sa->sin6_addr));
580 static void
581 flow_entry_geo_city_lookup_generic(struct flow_entry *n,
582 enum flow_entry_direction dir)
584 struct sockaddr_in sa4;
585 struct sockaddr_in6 sa6;
586 const char *city = NULL;
588 switch (n->l3_proto) {
589 default:
590 bug();
592 case AF_INET:
593 flow_entry_get_sain4_obj(n, dir, &sa4);
594 city = geoip4_city_name(sa4);
595 break;
597 case AF_INET6:
598 flow_entry_get_sain6_obj(n, dir, &sa6);
599 city = geoip6_city_name(sa6);
600 break;
603 bug_on(sizeof(n->city_src) != sizeof(n->city_dst));
605 if (city) {
606 memcpy(SELFLD(dir, city_src, city_dst), city,
607 min(sizeof(n->city_src), strlen(city)));
608 } else {
609 memset(SELFLD(dir, city_src, city_dst), 0,
610 sizeof(n->city_src));
614 static void
615 flow_entry_geo_country_lookup_generic(struct flow_entry *n,
616 enum flow_entry_direction dir)
618 struct sockaddr_in sa4;
619 struct sockaddr_in6 sa6;
620 const char *country = NULL;
622 switch (n->l3_proto) {
623 default:
624 bug();
626 case AF_INET:
627 flow_entry_get_sain4_obj(n, dir, &sa4);
628 country = geoip4_country_name(sa4);
629 break;
631 case AF_INET6:
632 flow_entry_get_sain6_obj(n, dir, &sa6);
633 country = geoip6_country_name(sa6);
634 break;
637 bug_on(sizeof(n->country_src) != sizeof(n->country_dst));
639 if (country) {
640 memcpy(SELFLD(dir, country_src, country_dst), country,
641 min(sizeof(n->country_src), strlen(country)));
642 } else {
643 memset(SELFLD(dir, country_src, country_dst), 0,
644 sizeof(n->country_src));
648 static void flow_entry_get_extended_geo(struct flow_entry *n,
649 enum flow_entry_direction dir)
651 flow_entry_geo_city_lookup_generic(n, dir);
652 flow_entry_geo_country_lookup_generic(n, dir);
655 static void flow_entry_get_extended_revdns(struct flow_entry *n,
656 enum flow_entry_direction dir)
658 size_t sa_len;
659 struct sockaddr_in sa4;
660 struct sockaddr_in6 sa6;
661 struct sockaddr *sa;
662 struct hostent *hent;
664 switch (n->l3_proto) {
665 default:
666 bug();
668 case AF_INET:
669 flow_entry_get_sain4_obj(n, dir, &sa4);
670 sa = (struct sockaddr *) &sa4;
671 sa_len = sizeof(sa4);
672 hent = gethostbyaddr(&sa4.sin_addr, sizeof(sa4.sin_addr), AF_INET);
673 break;
675 case AF_INET6:
676 flow_entry_get_sain6_obj(n, dir, &sa6);
677 sa = (struct sockaddr *) &sa6;
678 sa_len = sizeof(sa6);
679 hent = gethostbyaddr(&sa6.sin6_addr, sizeof(sa6.sin6_addr), AF_INET6);
680 break;
683 bug_on(sizeof(n->rev_dns_src) != sizeof(n->rev_dns_dst));
684 getnameinfo(sa, sa_len, SELFLD(dir, rev_dns_src, rev_dns_dst),
685 sizeof(n->rev_dns_src), NULL, 0, NI_NUMERICHOST);
687 if (hent) {
688 memset(n->rev_dns_dst, 0, sizeof(n->rev_dns_dst));
689 memcpy(SELFLD(dir, rev_dns_src, rev_dns_dst),
690 hent->h_name, min(sizeof(n->rev_dns_src),
691 strlen(hent->h_name)));
695 static void flow_entry_get_extended(struct flow_entry *n)
697 if (n->flow_id == 0 || flow_entry_get_extended_is_dns(n))
698 return;
700 flow_entry_get_extended_revdns(n, flow_entry_src);
701 flow_entry_get_extended_geo(n, flow_entry_src);
703 flow_entry_get_extended_revdns(n, flow_entry_dst);
704 flow_entry_get_extended_geo(n, flow_entry_dst);
706 /* Lookup application */
707 n->inode = get_port_inode(n->port_src, n->l4_proto,
708 n->l3_proto == AF_INET6);
709 if (n->inode > 0)
710 walk_processes(n);
713 static uint16_t presenter_get_port(uint16_t src, uint16_t dst, int tcp)
715 if (src < dst && src < 1024) {
716 return src;
717 } else if (dst < src && dst < 1024) {
718 return dst;
719 } else {
720 const char *tmp1, *tmp2;
721 if (tcp) {
722 tmp1 = lookup_port_tcp(src);
723 tmp2 = lookup_port_tcp(dst);
724 } else {
725 tmp1 = lookup_port_udp(src);
726 tmp2 = lookup_port_udp(dst);
728 if (tmp1 && !tmp2) {
729 return src;
730 } else if (!tmp1 && tmp2) {
731 return dst;
732 } else {
733 if (src < dst)
734 return src;
735 else
736 return dst;
741 static void presenter_screen_do_line(WINDOW *screen, struct flow_entry *n,
742 unsigned int *line)
744 char tmp[128], *pname = NULL;
745 uint16_t port;
747 mvwprintw(screen, *line, 2, "");
749 /* PID, application name */
750 if (n->procnum > 0) {
751 slprintf(tmp, sizeof(tmp), "%s(%u)", basename(n->cmdline),
752 n->procnum);
754 printw("[");
755 attron(COLOR_PAIR(3));
756 printw("%s", tmp);
757 attroff(COLOR_PAIR(3));
758 printw("]:");
761 /* L3 protocol, L4 protocol, states */
762 printw("%s:%s", l3proto2str[n->l3_proto], l4proto2str[n->l4_proto]);
763 printw("[");
764 attron(COLOR_PAIR(3));
765 switch (n->l4_proto) {
766 case IPPROTO_TCP:
767 printw("%s", tcp_state2str[n->tcp_state]);
768 break;
769 case IPPROTO_SCTP:
770 printw("%s", sctp_state2str[n->sctp_state]);
771 break;
772 case IPPROTO_DCCP:
773 printw("%s", dccp_state2str[n->dccp_state]);
774 break;
775 case IPPROTO_UDP:
776 case IPPROTO_UDPLITE:
777 case IPPROTO_ICMP:
778 case IPPROTO_ICMPV6:
779 printw("NOSTATE");
780 break;
782 attroff(COLOR_PAIR(3));
783 printw("]");
785 /* Guess application port */
786 switch (n->l4_proto) {
787 case IPPROTO_TCP:
788 port = presenter_get_port(n->port_src, n->port_dst, 1);
789 pname = lookup_port_tcp(port);
790 break;
791 case IPPROTO_UDP:
792 case IPPROTO_UDPLITE:
793 port = presenter_get_port(n->port_src, n->port_dst, 0);
794 pname = lookup_port_udp(port);
795 break;
797 if (pname) {
798 attron(A_BOLD);
799 printw(":%s", pname);
800 attroff(A_BOLD);
802 printw(" ->");
804 /* Number packets, bytes */
805 if (n->counter_pkts > 0 && n->counter_bytes > 0)
806 printw(" (%llu pkts, %llu bytes) ->",
807 n->counter_pkts, n->counter_bytes);
809 /* Show source information: reverse DNS, port, country, city */
810 if (show_src) {
811 attron(COLOR_PAIR(1));
812 mvwprintw(screen, ++(*line), 8, "src: %s", n->rev_dns_src);
813 attroff(COLOR_PAIR(1));
815 printw(":%u", n->port_src);
817 if (n->country_src[0]) {
818 printw(" (");
820 attron(COLOR_PAIR(4));
821 printw("%s", n->country_src);
822 attroff(COLOR_PAIR(4));
824 if (n->city_src[0])
825 printw(", %s", n->city_src);
827 printw(")");
830 printw(" => ");
833 /* Show dest information: reverse DNS, port, country, city */
834 attron(COLOR_PAIR(2));
835 mvwprintw(screen, ++(*line), 8, "dst: %s", n->rev_dns_dst);
836 attroff(COLOR_PAIR(2));
838 printw(":%u", n->port_dst);
840 if (n->country_dst[0]) {
841 printw(" (");
843 attron(COLOR_PAIR(4));
844 printw("%s", n->country_dst);
845 attroff(COLOR_PAIR(4));
847 if (n->city_dst[0])
848 printw(", %s", n->city_dst);
850 printw(")");
854 static inline int presenter_flow_wrong_state(struct flow_entry *n, int state)
856 int ret = 1;
858 switch (n->l4_proto) {
859 case IPPROTO_TCP:
860 if (n->tcp_state == state)
861 ret = 0;
862 break;
863 case IPPROTO_SCTP:
864 if (n->sctp_state == state)
865 ret = 0;
866 break;
867 case IPPROTO_DCCP:
868 if (n->dccp_state == state)
869 ret = 0;
870 break;
871 case IPPROTO_UDP:
872 case IPPROTO_UDPLITE:
873 case IPPROTO_ICMP:
874 case IPPROTO_ICMPV6:
875 ret = 0;
876 break;
879 return ret;
882 static void presenter_screen_update(WINDOW *screen, struct flow_list *fl,
883 int skip_lines)
885 int i, j, maxy;
886 unsigned int line = 3;
887 struct flow_entry *n;
888 uint8_t protocols[] = {
889 IPPROTO_TCP,
890 IPPROTO_DCCP,
891 IPPROTO_SCTP,
892 IPPROTO_UDP,
893 IPPROTO_UDPLITE,
894 IPPROTO_ICMP,
895 IPPROTO_ICMPV6,
897 size_t protocol_state_size[] = {
898 [IPPROTO_TCP] = array_size(tcp_states),
899 [IPPROTO_DCCP] = array_size(dccp_states),
900 [IPPROTO_SCTP] = array_size(sctp_states),
901 [IPPROTO_UDP] = 1,
902 [IPPROTO_UDPLITE] = 1,
903 [IPPROTO_ICMP] = 1,
904 [IPPROTO_ICMPV6] = 1,
907 curs_set(0);
909 maxy = getmaxy(screen);
910 maxy -= 6;
912 start_color();
913 init_pair(1, COLOR_RED, COLOR_BLACK);
914 init_pair(2, COLOR_BLUE, COLOR_BLACK);
915 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
916 init_pair(4, COLOR_GREEN, COLOR_BLACK);
918 wclear(screen);
919 clear();
921 mvwprintw(screen, 1, 2, "Kernel netfilter flows for %s%s%s%s%s%s"
922 "[+%d]", what & INCLUDE_TCP ? "TCP, " : "" ,
923 what & INCLUDE_UDP ? "UDP, " : "",
924 what & INCLUDE_SCTP ? "SCTP, " : "",
925 what & INCLUDE_DCCP ? "DCCP, " : "",
926 what & INCLUDE_ICMP && what & INCLUDE_IPV4 ? "ICMP, " : "",
927 what & INCLUDE_ICMP && what & INCLUDE_IPV6 ? "ICMP6, " : "",
928 skip_lines);
930 rcu_read_lock();
932 if (rcu_dereference(fl->head) == NULL)
933 mvwprintw(screen, line, 2, "(No active sessions! "
934 "Is netfilter running?)");
936 for (i = 0; i < array_size(protocols); i++) {
937 for (j = 0; j < protocol_state_size[protocols[i]]; j++) {
938 n = rcu_dereference(fl->head);
939 while (n && maxy > 0) {
940 int skip_entry = 0;
942 if (n->l4_proto != protocols[i])
943 skip_entry = 1;
944 if (presenter_flow_wrong_state(n, j))
945 skip_entry = 1;
946 if (presenter_get_port(n->port_src,
947 n->port_dst, 0) == 53)
948 skip_entry = 1;
949 if (skip_entry) {
950 n = rcu_dereference(n->next);
951 continue;
953 if (skip_lines > 0) {
954 n = rcu_dereference(n->next);
955 skip_lines--;
956 continue;
959 presenter_screen_do_line(screen, n, &line);
961 line++;
962 maxy -= (2 + 1 * show_src);
963 n = rcu_dereference(n->next);
968 rcu_read_unlock();
970 wrefresh(screen);
971 refresh();
974 static void presenter(void)
976 int skip_lines = 0;
977 WINDOW *screen;
979 dissector_init_ethernet(0);
980 screen = screen_init(false);
982 rcu_register_thread();
983 while (!sigint) {
984 switch (getch()) {
985 case 'q':
986 sigint = 1;
987 break;
988 case KEY_UP:
989 case 'u':
990 case 'k':
991 skip_lines--;
992 if (skip_lines < 0)
993 skip_lines = 0;
994 break;
995 case KEY_DOWN:
996 case 'd':
997 case 'j':
998 skip_lines++;
999 if (skip_lines > SCROLL_MAX)
1000 skip_lines = SCROLL_MAX;
1001 break;
1002 default:
1003 fflush(stdin);
1004 break;
1007 presenter_screen_update(screen, &flow_list, skip_lines);
1008 usleep(100000);
1010 rcu_unregister_thread();
1012 screen_end();
1013 dissector_cleanup_ethernet();
1016 static int collector_cb(enum nf_conntrack_msg_type type,
1017 struct nf_conntrack *ct, void *data)
1019 if (sigint)
1020 return NFCT_CB_STOP;
1022 synchronize_rcu();
1023 spinlock_lock(&flow_list.lock);
1025 switch (type) {
1026 case NFCT_T_NEW:
1027 flow_list_new_entry(&flow_list, ct);
1028 break;
1029 case NFCT_T_UPDATE:
1030 flow_list_update_entry(&flow_list, ct);
1031 break;
1032 case NFCT_T_DESTROY:
1033 flow_list_destroy_entry(&flow_list, ct);
1034 break;
1035 default:
1036 break;
1039 spinlock_unlock(&flow_list.lock);
1041 return NFCT_CB_CONTINUE;
1044 static inline void collector_flush(struct nfct_handle *handle, uint8_t family)
1046 nfct_query(handle, NFCT_Q_FLUSH, &family);
1049 static void *collector(void *null)
1051 int ret;
1052 struct nfct_handle *handle;
1053 struct nfct_filter *filter;
1055 handle = nfct_open(CONNTRACK, NF_NETLINK_CONNTRACK_NEW |
1056 NF_NETLINK_CONNTRACK_UPDATE |
1057 NF_NETLINK_CONNTRACK_DESTROY);
1058 if (!handle)
1059 panic("Cannot create a nfct handle!\n");
1061 collector_flush(handle, AF_INET);
1062 collector_flush(handle, AF_INET6);
1064 filter = nfct_filter_create();
1065 if (!filter)
1066 panic("Cannot create a nfct filter!\n");
1068 ret = nfct_filter_attach(nfct_fd(handle), filter);
1069 if (ret < 0)
1070 panic("Cannot attach filter to handle!\n");
1072 if (what & INCLUDE_UDP) {
1073 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_UDP);
1074 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_UDPLITE);
1076 if (what & INCLUDE_TCP)
1077 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_TCP);
1078 if (what & INCLUDE_DCCP)
1079 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_DCCP);
1080 if (what & INCLUDE_SCTP)
1081 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_SCTP);
1082 if (what & INCLUDE_ICMP && what & INCLUDE_IPV4)
1083 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_ICMP);
1084 if (what & INCLUDE_ICMP && what & INCLUDE_IPV6)
1085 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_ICMPV6);
1086 if (what & INCLUDE_IPV4) {
1087 nfct_filter_set_logic(filter, NFCT_FILTER_SRC_IPV4, NFCT_FILTER_LOGIC_NEGATIVE);
1088 nfct_filter_add_attr(filter, NFCT_FILTER_SRC_IPV4, &filter_ipv4);
1090 if (what & INCLUDE_IPV6) {
1091 nfct_filter_set_logic(filter, NFCT_FILTER_SRC_IPV6, NFCT_FILTER_LOGIC_NEGATIVE);
1092 nfct_filter_add_attr(filter, NFCT_FILTER_SRC_IPV6, &filter_ipv6);
1095 ret = nfct_filter_attach(nfct_fd(handle), filter);
1096 if (ret < 0)
1097 panic("Cannot attach filter to handle!\n");
1099 nfct_callback_register(handle, NFCT_T_ALL, collector_cb, NULL);
1100 nfct_filter_destroy(filter);
1101 flow_list_init(&flow_list);
1103 rcu_register_thread();
1105 while (!sigint && ret >= 0)
1106 ret = nfct_catch(handle);
1108 rcu_unregister_thread();
1110 flow_list_destroy(&flow_list);
1111 nfct_close(handle);
1113 pthread_exit(0);
1116 int main(int argc, char **argv)
1118 pthread_t tid;
1119 int ret, c, opt_index, what_cmd = 0;
1121 setfsuid(getuid());
1122 setfsgid(getgid());
1124 while ((c = getopt_long(argc, argv, short_options, long_options,
1125 &opt_index)) != EOF) {
1126 switch (c) {
1127 case '4':
1128 what_cmd |= INCLUDE_IPV4;
1129 break;
1130 case '6':
1131 what_cmd |= INCLUDE_IPV6;
1132 break;
1133 case 'T':
1134 what_cmd |= INCLUDE_TCP;
1135 break;
1136 case 'U':
1137 what_cmd |= INCLUDE_UDP;
1138 break;
1139 case 'D':
1140 what_cmd |= INCLUDE_DCCP;
1141 break;
1142 case 'I':
1143 what_cmd |= INCLUDE_ICMP;
1144 break;
1145 case 'S':
1146 what_cmd |= INCLUDE_SCTP;
1147 break;
1148 case 's':
1149 show_src = 1;
1150 break;
1151 case 'u':
1152 update_geoip();
1153 die();
1154 break;
1155 case 'h':
1156 help();
1157 break;
1158 case 'v':
1159 version();
1160 break;
1161 default:
1162 break;
1166 if (what_cmd > 0)
1167 what = what_cmd;
1169 rcu_init();
1171 register_signal(SIGINT, signal_handler);
1172 register_signal(SIGHUP, signal_handler);
1174 init_geoip(1);
1176 ret = pthread_create(&tid, NULL, collector, NULL);
1177 if (ret < 0)
1178 panic("Cannot create phthread!\n");
1180 presenter();
1182 destroy_geoip();
1184 return 0;