mz: Allow to print packet types by '-t help'
[netsniff-ng.git] / flowtop.c
blob2db57727c04504888abbd5ab635fdb25d9127b5a
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>
25 #include <inttypes.h>
27 #include "die.h"
28 #include "xmalloc.h"
29 #include "conntrack.h"
30 #include "config.h"
31 #include "str.h"
32 #include "sig.h"
33 #include "lookup.h"
34 #include "geoip.h"
35 #include "built_in.h"
36 #include "locking.h"
37 #include "pkt_buff.h"
38 #include "screen.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 inode;
55 unsigned int procnum;
58 struct flow_list {
59 struct flow_entry *head;
60 struct spinlock lock;
63 #ifndef ATTR_TIMESTAMP_START
64 # define ATTR_TIMESTAMP_START 63
65 #endif
66 #ifndef ATTR_TIMESTAMP_STOP
67 # define ATTR_TIMESTAMP_STOP 64
68 #endif
70 #define SCROLL_MAX 1000
72 #define INCLUDE_IPV4 (1 << 0)
73 #define INCLUDE_IPV6 (1 << 1)
74 #define INCLUDE_UDP (1 << 2)
75 #define INCLUDE_TCP (1 << 3)
76 #define INCLUDE_DCCP (1 << 4)
77 #define INCLUDE_ICMP (1 << 5)
78 #define INCLUDE_SCTP (1 << 6)
80 static volatile sig_atomic_t sigint = 0;
81 static int what = INCLUDE_IPV4 | INCLUDE_IPV6 | INCLUDE_TCP, show_src = 0;
82 static struct flow_list flow_list;
84 static const char *short_options = "vhTUsDIS46u";
85 static const struct option long_options[] = {
86 {"ipv4", no_argument, NULL, '4'},
87 {"ipv6", no_argument, NULL, '6'},
88 {"tcp", no_argument, NULL, 'T'},
89 {"udp", no_argument, NULL, 'U'},
90 {"dccp", no_argument, NULL, 'D'},
91 {"icmp", no_argument, NULL, 'I'},
92 {"sctp", no_argument, NULL, 'S'},
93 {"show-src", no_argument, NULL, 's'},
94 {"update", no_argument, NULL, 'u'},
95 {"version", no_argument, NULL, 'v'},
96 {"help", no_argument, NULL, 'h'},
97 {NULL, 0, NULL, 0}
100 static const char *copyright = "Please report bugs to <bugs@netsniff-ng.org>\n"
101 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
102 "Copyright (C) 2011-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
103 "Swiss federal institute of technology (ETH Zurich)\n"
104 "License: GNU GPL version 2.0\n"
105 "This is free software: you are free to change and redistribute it.\n"
106 "There is NO WARRANTY, to the extent permitted by law.";
108 static const char *const l3proto2str[AF_MAX] = {
109 [AF_INET] = "ipv4",
110 [AF_INET6] = "ipv6",
113 static const char *const l4proto2str[IPPROTO_MAX] = {
114 [IPPROTO_TCP] = "tcp",
115 [IPPROTO_UDP] = "udp",
116 [IPPROTO_UDPLITE] = "udplite",
117 [IPPROTO_ICMP] = "icmp",
118 [IPPROTO_ICMPV6] = "icmpv6",
119 [IPPROTO_SCTP] = "sctp",
120 [IPPROTO_GRE] = "gre",
121 [IPPROTO_DCCP] = "dccp",
122 [IPPROTO_IGMP] = "igmp",
123 [IPPROTO_IPIP] = "ipip",
124 [IPPROTO_EGP] = "egp",
125 [IPPROTO_PUP] = "pup",
126 [IPPROTO_IDP] = "idp",
127 [IPPROTO_RSVP] = "rsvp",
128 [IPPROTO_IPV6] = "ip6tun",
129 [IPPROTO_ESP] = "esp",
130 [IPPROTO_AH] = "ah",
131 [IPPROTO_PIM] = "pim",
132 [IPPROTO_COMP] = "comp",
135 static const char *const tcp_state2str[TCP_CONNTRACK_MAX] = {
136 [TCP_CONNTRACK_NONE] = "NOSTATE",
137 [TCP_CONNTRACK_SYN_SENT] = "SYN_SENT",
138 [TCP_CONNTRACK_SYN_RECV] = "SYN_RECV",
139 [TCP_CONNTRACK_ESTABLISHED] = "ESTABLISHED",
140 [TCP_CONNTRACK_FIN_WAIT] = "FIN_WAIT",
141 [TCP_CONNTRACK_CLOSE_WAIT] = "CLOSE_WAIT",
142 [TCP_CONNTRACK_LAST_ACK] = "LAST_ACK",
143 [TCP_CONNTRACK_TIME_WAIT] = "TIME_WAIT",
144 [TCP_CONNTRACK_CLOSE] = "CLOSE",
145 [TCP_CONNTRACK_SYN_SENT2] = "SYN_SENT2",
148 static const uint8_t tcp_states[] = {
149 TCP_CONNTRACK_SYN_SENT,
150 TCP_CONNTRACK_SYN_RECV,
151 TCP_CONNTRACK_ESTABLISHED,
152 TCP_CONNTRACK_FIN_WAIT,
153 TCP_CONNTRACK_CLOSE_WAIT,
154 TCP_CONNTRACK_LAST_ACK,
155 TCP_CONNTRACK_TIME_WAIT,
156 TCP_CONNTRACK_CLOSE,
157 TCP_CONNTRACK_SYN_SENT2,
158 TCP_CONNTRACK_NONE,
161 static const char *const dccp_state2str[DCCP_CONNTRACK_MAX] = {
162 [DCCP_CONNTRACK_NONE] = "NOSTATE",
163 [DCCP_CONNTRACK_REQUEST] = "REQUEST",
164 [DCCP_CONNTRACK_RESPOND] = "RESPOND",
165 [DCCP_CONNTRACK_PARTOPEN] = "PARTOPEN",
166 [DCCP_CONNTRACK_OPEN] = "OPEN",
167 [DCCP_CONNTRACK_CLOSEREQ] = "CLOSEREQ",
168 [DCCP_CONNTRACK_CLOSING] = "CLOSING",
169 [DCCP_CONNTRACK_TIMEWAIT] = "TIMEWAIT",
170 [DCCP_CONNTRACK_IGNORE] = "IGNORE",
171 [DCCP_CONNTRACK_INVALID] = "INVALID",
174 static const uint8_t dccp_states[] = {
175 DCCP_CONNTRACK_NONE,
176 DCCP_CONNTRACK_REQUEST,
177 DCCP_CONNTRACK_RESPOND,
178 DCCP_CONNTRACK_PARTOPEN,
179 DCCP_CONNTRACK_OPEN,
180 DCCP_CONNTRACK_CLOSEREQ,
181 DCCP_CONNTRACK_CLOSING,
182 DCCP_CONNTRACK_TIMEWAIT,
183 DCCP_CONNTRACK_IGNORE,
184 DCCP_CONNTRACK_INVALID,
187 static const char *const sctp_state2str[SCTP_CONNTRACK_MAX] = {
188 [SCTP_CONNTRACK_NONE] = "NOSTATE",
189 [SCTP_CONNTRACK_CLOSED] = "CLOSED",
190 [SCTP_CONNTRACK_COOKIE_WAIT] = "COOKIE_WAIT",
191 [SCTP_CONNTRACK_COOKIE_ECHOED] = "COOKIE_ECHOED",
192 [SCTP_CONNTRACK_ESTABLISHED] = "ESTABLISHED",
193 [SCTP_CONNTRACK_SHUTDOWN_SENT] = "SHUTDOWN_SENT",
194 [SCTP_CONNTRACK_SHUTDOWN_RECD] = "SHUTDOWN_RECD",
195 [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = "SHUTDOWN_ACK_SENT",
198 static const uint8_t sctp_states[] = {
199 SCTP_CONNTRACK_NONE,
200 SCTP_CONNTRACK_CLOSED,
201 SCTP_CONNTRACK_COOKIE_WAIT,
202 SCTP_CONNTRACK_COOKIE_ECHOED,
203 SCTP_CONNTRACK_ESTABLISHED,
204 SCTP_CONNTRACK_SHUTDOWN_SENT,
205 SCTP_CONNTRACK_SHUTDOWN_RECD,
206 SCTP_CONNTRACK_SHUTDOWN_ACK_SENT,
209 static const struct nfct_filter_ipv4 filter_ipv4 = {
210 .addr = __constant_htonl(INADDR_LOOPBACK),
211 .mask = 0xffffffff,
214 static const struct nfct_filter_ipv6 filter_ipv6 = {
215 .addr = { 0x0, 0x0, 0x0, 0x1 },
216 .mask = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff },
219 static void signal_handler(int number)
221 switch (number) {
222 case SIGINT:
223 case SIGQUIT:
224 case SIGTERM:
225 sigint = 1;
226 break;
227 case SIGHUP:
228 default:
229 break;
233 static void flow_entry_from_ct(struct flow_entry *n, struct nf_conntrack *ct);
234 static void flow_entry_get_extended(struct flow_entry *n);
236 static void help(void)
238 printf("flowtop %s, top-like netfilter TCP/UDP/SCTP/.. flow tracking\n",
239 VERSION_STRING);
240 puts("http://www.netsniff-ng.org\n\n"
241 "Usage: flowtop [options]\n"
242 "Options:\n"
243 " -4|--ipv4 Show only IPv4 flows (default)\n"
244 " -6|--ipv6 Show only IPv6 flows (default)\n"
245 " -T|--tcp Show only TCP flows (default)\n"
246 " -U|--udp Show only UDP flows\n"
247 " -D|--dccp Show only DCCP flows\n"
248 " -I|--icmp Show only ICMP/ICMPv6 flows\n"
249 " -S|--sctp Show only SCTP flows\n"
250 " -s|--show-src Also show source, not only dest\n"
251 " -u|--update Update GeoIP databases\n"
252 " -v|--version Print version and exit\n"
253 " -h|--help Print this help and exit\n\n"
254 "Examples:\n"
255 " flowtop\n"
256 " flowtop -46UTDISs\n\n"
257 "Note:\n"
258 " If netfilter is not running, you can activate it with e.g.:\n"
259 " iptables -A INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT\n"
260 " iptables -A OUTPUT -p tcp -m state --state NEW,ESTABLISHED -j ACCEPT\n");
261 puts(copyright);
262 die();
265 static void version(void)
267 printf("flowtop %s, Git id: %s\n", VERSION_LONG, GITVERSION);
268 puts("top-like netfilter TCP/UDP/SCTP/.. flow tracking\n"
269 "http://www.netsniff-ng.org\n");
270 puts(copyright);
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(unsigned int pid, 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/%u/fd", pid) == -1)
400 panic("giant process name! %u\n", pid);
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/%u/fd/%s",
410 pid, ent->d_name) < 0)
411 continue;
413 if (stat(path, &statbuf) < 0)
414 continue;
416 if (S_ISSOCK(statbuf.st_mode) && (ino_t) n->inode == statbuf.st_ino) {
417 memset(n->cmdline, 0, sizeof(n->cmdline));
419 snprintf(path, sizeof(path), "/proc/%u/exe", pid);
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 = pid;
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 const char *name = ent->d_name;
454 char *end;
455 unsigned int pid = strtoul(name, &end, 10);
457 /* not a PID */
458 if (pid == 0 && end == name)
459 continue;
461 ret = walk_process(pid, n);
462 if (ret > 0)
463 break;
466 closedir(dir);
469 static int get_port_inode(uint16_t port, int proto, int is_ip6)
471 int ret = -ENOENT;
472 char path[128], buff[1024];
473 FILE *proc;
475 memset(path, 0, sizeof(path));
476 snprintf(path, sizeof(path), "/proc/net/%s%s",
477 l4proto2str[proto], is_ip6 ? "6" : "");
479 proc = fopen(path, "r");
480 if (!proc)
481 return -EIO;
483 memset(buff, 0, sizeof(buff));
485 while (fgets(buff, sizeof(buff), proc) != NULL) {
486 int inode = 0;
487 unsigned int lport = 0;
489 buff[sizeof(buff) - 1] = 0;
490 if (sscanf(buff, "%*u: %*X:%X %*X:%*X %*X %*X:%*X %*X:%*X "
491 "%*X %*u %*u %u", &lport, &inode) == 2) {
492 if ((uint16_t) lport == port) {
493 ret = inode;
494 break;
498 memset(buff, 0, sizeof(buff));
501 fclose(proc);
502 return ret;
505 #define CP_NFCT(elem, attr, x) \
506 do { n->elem = nfct_get_attr_u##x(ct,(attr)); } while (0)
507 #define CP_NFCT_BUFF(elem, attr) do { \
508 const uint8_t *buff = nfct_get_attr(ct,(attr)); \
509 if (buff != NULL) \
510 memcpy(n->elem, buff, sizeof(n->elem)); \
511 } while (0)
513 static void flow_entry_from_ct(struct flow_entry *n, struct nf_conntrack *ct)
515 CP_NFCT(l3_proto, ATTR_ORIG_L3PROTO, 8);
516 CP_NFCT(l4_proto, ATTR_ORIG_L4PROTO, 8);
518 CP_NFCT(ip4_src_addr, ATTR_ORIG_IPV4_SRC, 32);
519 CP_NFCT(ip4_dst_addr, ATTR_ORIG_IPV4_DST, 32);
521 CP_NFCT(port_src, ATTR_ORIG_PORT_SRC, 16);
522 CP_NFCT(port_dst, ATTR_ORIG_PORT_DST, 16);
524 CP_NFCT(status, ATTR_STATUS, 32);
526 CP_NFCT(tcp_state, ATTR_TCP_STATE, 8);
527 CP_NFCT(tcp_flags, ATTR_TCP_FLAGS_ORIG, 8);
528 CP_NFCT(sctp_state, ATTR_SCTP_STATE, 8);
529 CP_NFCT(dccp_state, ATTR_DCCP_STATE, 8);
531 CP_NFCT(counter_pkts, ATTR_ORIG_COUNTER_PACKETS, 64);
532 CP_NFCT(counter_bytes, ATTR_ORIG_COUNTER_BYTES, 64);
534 CP_NFCT(timestamp_start, ATTR_TIMESTAMP_START, 64);
535 CP_NFCT(timestamp_stop, ATTR_TIMESTAMP_STOP, 64);
537 CP_NFCT(flow_id, ATTR_ID, 32);
538 CP_NFCT(use, ATTR_USE, 32);
540 CP_NFCT_BUFF(ip6_src_addr, ATTR_ORIG_IPV6_SRC);
541 CP_NFCT_BUFF(ip6_dst_addr, ATTR_ORIG_IPV6_DST);
543 n->port_src = ntohs(n->port_src);
544 n->port_dst = ntohs(n->port_dst);
546 n->ip4_src_addr = ntohl(n->ip4_src_addr);
547 n->ip4_dst_addr = ntohl(n->ip4_dst_addr);
550 enum flow_entry_direction {
551 flow_entry_src,
552 flow_entry_dst,
555 static inline int flow_entry_get_extended_is_dns(struct flow_entry *n)
557 /* We don't want to analyze / display DNS itself, since we
558 * use it to resolve reverse dns.
560 return n->port_src == 53 || n->port_dst == 53;
563 #define SELFLD(dir,src_member,dst_member) \
564 (((dir) == flow_entry_src) ? n->src_member : n->dst_member)
566 static void flow_entry_get_sain4_obj(struct flow_entry *n,
567 enum flow_entry_direction dir,
568 struct sockaddr_in *sa)
570 memset(sa, 0, sizeof(*sa));
571 sa->sin_family = PF_INET;
572 sa->sin_addr.s_addr = htonl(SELFLD(dir, ip4_src_addr, ip4_dst_addr));
575 static void flow_entry_get_sain6_obj(struct flow_entry *n,
576 enum flow_entry_direction dir,
577 struct sockaddr_in6 *sa)
579 memset(sa, 0, sizeof(*sa));
580 sa->sin6_family = PF_INET6;
582 memcpy(&sa->sin6_addr, SELFLD(dir, ip6_src_addr, ip6_dst_addr),
583 sizeof(sa->sin6_addr));
586 static void
587 flow_entry_geo_city_lookup_generic(struct flow_entry *n,
588 enum flow_entry_direction dir)
590 struct sockaddr_in sa4;
591 struct sockaddr_in6 sa6;
592 const char *city = NULL;
594 switch (n->l3_proto) {
595 default:
596 bug();
598 case AF_INET:
599 flow_entry_get_sain4_obj(n, dir, &sa4);
600 city = geoip4_city_name(&sa4);
601 break;
603 case AF_INET6:
604 flow_entry_get_sain6_obj(n, dir, &sa6);
605 city = geoip6_city_name(&sa6);
606 break;
609 bug_on(sizeof(n->city_src) != sizeof(n->city_dst));
611 if (city) {
612 memcpy(SELFLD(dir, city_src, city_dst), city,
613 min(sizeof(n->city_src), strlen(city)));
614 } else {
615 memset(SELFLD(dir, city_src, city_dst), 0,
616 sizeof(n->city_src));
620 static void
621 flow_entry_geo_country_lookup_generic(struct flow_entry *n,
622 enum flow_entry_direction dir)
624 struct sockaddr_in sa4;
625 struct sockaddr_in6 sa6;
626 const char *country = NULL;
628 switch (n->l3_proto) {
629 default:
630 bug();
632 case AF_INET:
633 flow_entry_get_sain4_obj(n, dir, &sa4);
634 country = geoip4_country_name(&sa4);
635 break;
637 case AF_INET6:
638 flow_entry_get_sain6_obj(n, dir, &sa6);
639 country = geoip6_country_name(&sa6);
640 break;
643 bug_on(sizeof(n->country_src) != sizeof(n->country_dst));
645 if (country) {
646 memcpy(SELFLD(dir, country_src, country_dst), country,
647 min(sizeof(n->country_src), strlen(country)));
648 } else {
649 memset(SELFLD(dir, country_src, country_dst), 0,
650 sizeof(n->country_src));
654 static void flow_entry_get_extended_geo(struct flow_entry *n,
655 enum flow_entry_direction dir)
657 flow_entry_geo_city_lookup_generic(n, dir);
658 flow_entry_geo_country_lookup_generic(n, dir);
661 static void flow_entry_get_extended_revdns(struct flow_entry *n,
662 enum flow_entry_direction dir)
664 size_t sa_len;
665 struct sockaddr_in sa4;
666 struct sockaddr_in6 sa6;
667 struct sockaddr *sa;
668 struct hostent *hent;
670 switch (n->l3_proto) {
671 default:
672 bug();
674 case AF_INET:
675 flow_entry_get_sain4_obj(n, dir, &sa4);
676 sa = (struct sockaddr *) &sa4;
677 sa_len = sizeof(sa4);
678 hent = gethostbyaddr(&sa4.sin_addr, sizeof(sa4.sin_addr), AF_INET);
679 break;
681 case AF_INET6:
682 flow_entry_get_sain6_obj(n, dir, &sa6);
683 sa = (struct sockaddr *) &sa6;
684 sa_len = sizeof(sa6);
685 hent = gethostbyaddr(&sa6.sin6_addr, sizeof(sa6.sin6_addr), AF_INET6);
686 break;
689 bug_on(sizeof(n->rev_dns_src) != sizeof(n->rev_dns_dst));
690 getnameinfo(sa, sa_len, SELFLD(dir, rev_dns_src, rev_dns_dst),
691 sizeof(n->rev_dns_src), NULL, 0, NI_NUMERICHOST);
693 if (hent) {
694 memset(n->rev_dns_dst, 0, sizeof(n->rev_dns_dst));
695 memcpy(SELFLD(dir, rev_dns_src, rev_dns_dst),
696 hent->h_name, min(sizeof(n->rev_dns_src),
697 strlen(hent->h_name)));
701 static void flow_entry_get_extended(struct flow_entry *n)
703 if (n->flow_id == 0 || flow_entry_get_extended_is_dns(n))
704 return;
706 flow_entry_get_extended_revdns(n, flow_entry_src);
707 flow_entry_get_extended_geo(n, flow_entry_src);
709 flow_entry_get_extended_revdns(n, flow_entry_dst);
710 flow_entry_get_extended_geo(n, flow_entry_dst);
712 /* Lookup application */
713 n->inode = get_port_inode(n->port_src, n->l4_proto,
714 n->l3_proto == AF_INET6);
715 if (n->inode > 0)
716 walk_processes(n);
719 static uint16_t presenter_get_port(uint16_t src, uint16_t dst, int tcp)
721 if (src < dst && src < 1024) {
722 return src;
723 } else if (dst < src && dst < 1024) {
724 return dst;
725 } else {
726 const char *tmp1, *tmp2;
727 if (tcp) {
728 tmp1 = lookup_port_tcp(src);
729 tmp2 = lookup_port_tcp(dst);
730 } else {
731 tmp1 = lookup_port_udp(src);
732 tmp2 = lookup_port_udp(dst);
734 if (tmp1 && !tmp2) {
735 return src;
736 } else if (!tmp1 && tmp2) {
737 return dst;
738 } else {
739 if (src < dst)
740 return src;
741 else
742 return dst;
747 static void presenter_screen_do_line(WINDOW *screen, struct flow_entry *n,
748 unsigned int *line)
750 char tmp[128], *pname = NULL;
751 uint16_t port;
753 mvwprintw(screen, *line, 2, "");
755 /* PID, application name */
756 if (n->procnum > 0) {
757 slprintf(tmp, sizeof(tmp), "%s(%d)", basename(n->cmdline),
758 n->procnum);
760 printw("[");
761 attron(COLOR_PAIR(3));
762 printw("%s", tmp);
763 attroff(COLOR_PAIR(3));
764 printw("]:");
767 /* L3 protocol, L4 protocol, states */
768 printw("%s:%s", l3proto2str[n->l3_proto], l4proto2str[n->l4_proto]);
769 printw("[");
770 attron(COLOR_PAIR(3));
771 switch (n->l4_proto) {
772 case IPPROTO_TCP:
773 printw("%s", tcp_state2str[n->tcp_state]);
774 break;
775 case IPPROTO_SCTP:
776 printw("%s", sctp_state2str[n->sctp_state]);
777 break;
778 case IPPROTO_DCCP:
779 printw("%s", dccp_state2str[n->dccp_state]);
780 break;
781 case IPPROTO_UDP:
782 case IPPROTO_UDPLITE:
783 case IPPROTO_ICMP:
784 case IPPROTO_ICMPV6:
785 printw("NOSTATE");
786 break;
788 attroff(COLOR_PAIR(3));
789 printw("]");
791 /* Guess application port */
792 switch (n->l4_proto) {
793 case IPPROTO_TCP:
794 port = presenter_get_port(n->port_src, n->port_dst, 1);
795 pname = lookup_port_tcp(port);
796 break;
797 case IPPROTO_UDP:
798 case IPPROTO_UDPLITE:
799 port = presenter_get_port(n->port_src, n->port_dst, 0);
800 pname = lookup_port_udp(port);
801 break;
803 if (pname) {
804 attron(A_BOLD);
805 printw(":%s", pname);
806 attroff(A_BOLD);
808 printw(" ->");
810 /* Number packets, bytes */
811 if (n->counter_pkts > 0 && n->counter_bytes > 0)
812 printw(" (%"PRIu64" pkts, %"PRIu64" bytes) ->",
813 n->counter_pkts, n->counter_bytes);
815 /* Show source information: reverse DNS, port, country, city */
816 if (show_src) {
817 attron(COLOR_PAIR(1));
818 mvwprintw(screen, ++(*line), 8, "src: %s", n->rev_dns_src);
819 attroff(COLOR_PAIR(1));
821 printw(":%"PRIu16, n->port_src);
823 if (n->country_src[0]) {
824 printw(" (");
826 attron(COLOR_PAIR(4));
827 printw("%s", n->country_src);
828 attroff(COLOR_PAIR(4));
830 if (n->city_src[0])
831 printw(", %s", n->city_src);
833 printw(")");
836 printw(" => ");
839 /* Show dest information: reverse DNS, port, country, city */
840 attron(COLOR_PAIR(2));
841 mvwprintw(screen, ++(*line), 8, "dst: %s", n->rev_dns_dst);
842 attroff(COLOR_PAIR(2));
844 printw(":%"PRIu16, n->port_dst);
846 if (n->country_dst[0]) {
847 printw(" (");
849 attron(COLOR_PAIR(4));
850 printw("%s", n->country_dst);
851 attroff(COLOR_PAIR(4));
853 if (n->city_dst[0])
854 printw(", %s", n->city_dst);
856 printw(")");
860 static inline int presenter_flow_wrong_state(struct flow_entry *n, int state)
862 int ret = 1;
864 switch (n->l4_proto) {
865 case IPPROTO_TCP:
866 if (n->tcp_state == state)
867 ret = 0;
868 break;
869 case IPPROTO_SCTP:
870 if (n->sctp_state == state)
871 ret = 0;
872 break;
873 case IPPROTO_DCCP:
874 if (n->dccp_state == state)
875 ret = 0;
876 break;
877 case IPPROTO_UDP:
878 case IPPROTO_UDPLITE:
879 case IPPROTO_ICMP:
880 case IPPROTO_ICMPV6:
881 ret = 0;
882 break;
885 return ret;
888 static void presenter_screen_update(WINDOW *screen, struct flow_list *fl,
889 int skip_lines)
891 int maxy;
892 size_t i, j;
893 unsigned int line = 3;
894 struct flow_entry *n;
895 uint8_t protocols[] = {
896 IPPROTO_TCP,
897 IPPROTO_DCCP,
898 IPPROTO_SCTP,
899 IPPROTO_UDP,
900 IPPROTO_UDPLITE,
901 IPPROTO_ICMP,
902 IPPROTO_ICMPV6,
904 size_t protocol_state_size[] = {
905 [IPPROTO_TCP] = array_size(tcp_states),
906 [IPPROTO_DCCP] = array_size(dccp_states),
907 [IPPROTO_SCTP] = array_size(sctp_states),
908 [IPPROTO_UDP] = 1,
909 [IPPROTO_UDPLITE] = 1,
910 [IPPROTO_ICMP] = 1,
911 [IPPROTO_ICMPV6] = 1,
914 curs_set(0);
916 maxy = getmaxy(screen);
917 maxy -= 6;
919 start_color();
920 init_pair(1, COLOR_RED, COLOR_BLACK);
921 init_pair(2, COLOR_BLUE, COLOR_BLACK);
922 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
923 init_pair(4, COLOR_GREEN, COLOR_BLACK);
925 wclear(screen);
926 clear();
928 mvwprintw(screen, 1, 2, "Kernel netfilter flows for %s%s%s%s%s%s"
929 "[+%d]", what & INCLUDE_TCP ? "TCP, " : "" ,
930 what & INCLUDE_UDP ? "UDP, " : "",
931 what & INCLUDE_SCTP ? "SCTP, " : "",
932 what & INCLUDE_DCCP ? "DCCP, " : "",
933 what & INCLUDE_ICMP && what & INCLUDE_IPV4 ? "ICMP, " : "",
934 what & INCLUDE_ICMP && what & INCLUDE_IPV6 ? "ICMP6, " : "",
935 skip_lines);
937 rcu_read_lock();
939 if (rcu_dereference(fl->head) == NULL)
940 mvwprintw(screen, line, 2, "(No active sessions! "
941 "Is netfilter running?)");
943 for (i = 0; i < array_size(protocols); i++) {
944 for (j = 0; j < protocol_state_size[protocols[i]]; j++) {
945 n = rcu_dereference(fl->head);
946 while (n && maxy > 0) {
947 if (n->l4_proto != protocols[i] ||
948 presenter_flow_wrong_state(n, j) ||
949 presenter_get_port(n->port_src,
950 n->port_dst, 0) == 53) {
951 /* skip entry */
952 n = rcu_dereference(n->next);
953 continue;
955 if (skip_lines > 0) {
956 n = rcu_dereference(n->next);
957 skip_lines--;
958 continue;
961 presenter_screen_do_line(screen, n, &line);
963 line++;
964 maxy -= (2 + 1 * show_src);
965 n = rcu_dereference(n->next);
970 rcu_read_unlock();
972 wrefresh(screen);
973 refresh();
976 static void presenter(void)
978 int skip_lines = 0;
979 WINDOW *screen;
981 lookup_init_ports(PORTS_TCP);
982 lookup_init_ports(PORTS_UDP);
983 screen = screen_init(false);
985 rcu_register_thread();
986 while (!sigint) {
987 switch (getch()) {
988 case 'q':
989 sigint = 1;
990 break;
991 case KEY_UP:
992 case 'u':
993 case 'k':
994 skip_lines--;
995 if (skip_lines < 0)
996 skip_lines = 0;
997 break;
998 case KEY_DOWN:
999 case 'd':
1000 case 'j':
1001 skip_lines++;
1002 if (skip_lines > SCROLL_MAX)
1003 skip_lines = SCROLL_MAX;
1004 break;
1005 default:
1006 fflush(stdin);
1007 break;
1010 presenter_screen_update(screen, &flow_list, skip_lines);
1011 usleep(100000);
1013 rcu_unregister_thread();
1015 screen_end();
1016 lookup_cleanup_ports(PORTS_UDP);
1017 lookup_cleanup_ports(PORTS_TCP);
1020 static int collector_cb(enum nf_conntrack_msg_type type,
1021 struct nf_conntrack *ct, void *data __maybe_unused)
1023 if (sigint)
1024 return NFCT_CB_STOP;
1026 synchronize_rcu();
1027 spinlock_lock(&flow_list.lock);
1029 switch (type) {
1030 case NFCT_T_NEW:
1031 flow_list_new_entry(&flow_list, ct);
1032 break;
1033 case NFCT_T_UPDATE:
1034 flow_list_update_entry(&flow_list, ct);
1035 break;
1036 case NFCT_T_DESTROY:
1037 flow_list_destroy_entry(&flow_list, ct);
1038 break;
1039 default:
1040 break;
1043 spinlock_unlock(&flow_list.lock);
1045 return NFCT_CB_CONTINUE;
1048 static inline void collector_flush(struct nfct_handle *handle, uint8_t family)
1050 nfct_query(handle, NFCT_Q_FLUSH, &family);
1053 static void *collector(void *null __maybe_unused)
1055 int ret;
1056 struct nfct_handle *handle;
1057 struct nfct_filter *filter;
1059 handle = nfct_open(CONNTRACK, NF_NETLINK_CONNTRACK_NEW |
1060 NF_NETLINK_CONNTRACK_UPDATE |
1061 NF_NETLINK_CONNTRACK_DESTROY);
1062 if (!handle)
1063 panic("Cannot create a nfct handle!\n");
1065 collector_flush(handle, AF_INET);
1066 collector_flush(handle, AF_INET6);
1068 filter = nfct_filter_create();
1069 if (!filter)
1070 panic("Cannot create a nfct filter!\n");
1072 ret = nfct_filter_attach(nfct_fd(handle), filter);
1073 if (ret < 0)
1074 panic("Cannot attach filter to handle!\n");
1076 if (what & INCLUDE_UDP) {
1077 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_UDP);
1078 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_UDPLITE);
1080 if (what & INCLUDE_TCP)
1081 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_TCP);
1082 if (what & INCLUDE_DCCP)
1083 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_DCCP);
1084 if (what & INCLUDE_SCTP)
1085 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_SCTP);
1086 if (what & INCLUDE_ICMP && what & INCLUDE_IPV4)
1087 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_ICMP);
1088 if (what & INCLUDE_ICMP && what & INCLUDE_IPV6)
1089 nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_ICMPV6);
1090 if (what & INCLUDE_IPV4) {
1091 nfct_filter_set_logic(filter, NFCT_FILTER_SRC_IPV4, NFCT_FILTER_LOGIC_NEGATIVE);
1092 nfct_filter_add_attr(filter, NFCT_FILTER_SRC_IPV4, &filter_ipv4);
1094 if (what & INCLUDE_IPV6) {
1095 nfct_filter_set_logic(filter, NFCT_FILTER_SRC_IPV6, NFCT_FILTER_LOGIC_NEGATIVE);
1096 nfct_filter_add_attr(filter, NFCT_FILTER_SRC_IPV6, &filter_ipv6);
1099 ret = nfct_filter_attach(nfct_fd(handle), filter);
1100 if (ret < 0)
1101 panic("Cannot attach filter to handle!\n");
1103 nfct_callback_register(handle, NFCT_T_ALL, collector_cb, NULL);
1104 nfct_filter_destroy(filter);
1105 flow_list_init(&flow_list);
1107 rcu_register_thread();
1109 while (!sigint && ret >= 0)
1110 ret = nfct_catch(handle);
1112 rcu_unregister_thread();
1114 flow_list_destroy(&flow_list);
1115 nfct_close(handle);
1117 pthread_exit(NULL);
1120 int main(int argc, char **argv)
1122 pthread_t tid;
1123 int ret, c, opt_index, what_cmd = 0;
1125 setfsuid(getuid());
1126 setfsgid(getgid());
1128 while ((c = getopt_long(argc, argv, short_options, long_options,
1129 &opt_index)) != EOF) {
1130 switch (c) {
1131 case '4':
1132 what_cmd |= INCLUDE_IPV4;
1133 break;
1134 case '6':
1135 what_cmd |= INCLUDE_IPV6;
1136 break;
1137 case 'T':
1138 what_cmd |= INCLUDE_TCP;
1139 break;
1140 case 'U':
1141 what_cmd |= INCLUDE_UDP;
1142 break;
1143 case 'D':
1144 what_cmd |= INCLUDE_DCCP;
1145 break;
1146 case 'I':
1147 what_cmd |= INCLUDE_ICMP;
1148 break;
1149 case 'S':
1150 what_cmd |= INCLUDE_SCTP;
1151 break;
1152 case 's':
1153 show_src = 1;
1154 break;
1155 case 'u':
1156 update_geoip();
1157 die();
1158 break;
1159 case 'h':
1160 help();
1161 break;
1162 case 'v':
1163 version();
1164 break;
1165 default:
1166 break;
1170 if (what_cmd > 0)
1171 what = what_cmd;
1173 rcu_init();
1175 register_signal(SIGINT, signal_handler);
1176 register_signal(SIGQUIT, signal_handler);
1177 register_signal(SIGTERM, signal_handler);
1178 register_signal(SIGHUP, signal_handler);
1180 init_geoip(1);
1182 ret = pthread_create(&tid, NULL, collector, NULL);
1183 if (ret < 0)
1184 panic("Cannot create phthread!\n");
1186 presenter();
1188 destroy_geoip();
1190 return 0;