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>
22 #include <sys/fsuid.h>
29 #include "conntrack.h"
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];
53 struct flow_entry
*next
;
58 struct flow_entry
*head
;
62 #ifndef ATTR_TIMESTAMP_START
63 # define ATTR_TIMESTAMP_START 63
65 #ifndef ATTR_TIMESTAMP_STOP
66 # define ATTR_TIMESTAMP_STOP 64
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 static volatile sig_atomic_t sigint
= 0;
80 static int what
= INCLUDE_IPV4
| INCLUDE_IPV6
| INCLUDE_TCP
, show_src
= 0;
81 static struct flow_list flow_list
;
83 static const char *short_options
= "vhTUsDIS46u";
84 static const struct option long_options
[] = {
85 {"ipv4", no_argument
, NULL
, '4'},
86 {"ipv6", no_argument
, NULL
, '6'},
87 {"tcp", no_argument
, NULL
, 'T'},
88 {"udp", no_argument
, NULL
, 'U'},
89 {"dccp", no_argument
, NULL
, 'D'},
90 {"icmp", no_argument
, NULL
, 'I'},
91 {"sctp", no_argument
, NULL
, 'S'},
92 {"show-src", no_argument
, NULL
, 's'},
93 {"update", no_argument
, NULL
, 'u'},
94 {"version", no_argument
, NULL
, 'v'},
95 {"help", no_argument
, NULL
, 'h'},
99 static const char *copyright
= "Please report bugs to <bugs@netsniff-ng.org>\n"
100 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
101 "Copyright (C) 2011-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
102 "Swiss federal institute of technology (ETH Zurich)\n"
103 "License: GNU GPL version 2.0\n"
104 "This is free software: you are free to change and redistribute it.\n"
105 "There is NO WARRANTY, to the extent permitted by law.\n";
107 static const char *const l3proto2str
[AF_MAX
] = {
112 static const char *const l4proto2str
[IPPROTO_MAX
] = {
113 [IPPROTO_TCP
] = "tcp",
114 [IPPROTO_UDP
] = "udp",
115 [IPPROTO_UDPLITE
] = "udplite",
116 [IPPROTO_ICMP
] = "icmp",
117 [IPPROTO_ICMPV6
] = "icmpv6",
118 [IPPROTO_SCTP
] = "sctp",
119 [IPPROTO_GRE
] = "gre",
120 [IPPROTO_DCCP
] = "dccp",
121 [IPPROTO_IGMP
] = "igmp",
122 [IPPROTO_IPIP
] = "ipip",
123 [IPPROTO_EGP
] = "egp",
124 [IPPROTO_PUP
] = "pup",
125 [IPPROTO_IDP
] = "idp",
126 [IPPROTO_RSVP
] = "rsvp",
127 [IPPROTO_IPV6
] = "ip6tun",
128 [IPPROTO_ESP
] = "esp",
130 [IPPROTO_PIM
] = "pim",
131 [IPPROTO_COMP
] = "comp",
134 static const char *const tcp_state2str
[TCP_CONNTRACK_MAX
] = {
135 [TCP_CONNTRACK_NONE
] = "NOSTATE",
136 [TCP_CONNTRACK_SYN_SENT
] = "SYN_SENT",
137 [TCP_CONNTRACK_SYN_RECV
] = "SYN_RECV",
138 [TCP_CONNTRACK_ESTABLISHED
] = "ESTABLISHED",
139 [TCP_CONNTRACK_FIN_WAIT
] = "FIN_WAIT",
140 [TCP_CONNTRACK_CLOSE_WAIT
] = "CLOSE_WAIT",
141 [TCP_CONNTRACK_LAST_ACK
] = "LAST_ACK",
142 [TCP_CONNTRACK_TIME_WAIT
] = "TIME_WAIT",
143 [TCP_CONNTRACK_CLOSE
] = "CLOSE",
144 [TCP_CONNTRACK_SYN_SENT2
] = "SYN_SENT2",
147 static const uint8_t tcp_states
[] = {
148 TCP_CONNTRACK_SYN_SENT
,
149 TCP_CONNTRACK_SYN_RECV
,
150 TCP_CONNTRACK_ESTABLISHED
,
151 TCP_CONNTRACK_FIN_WAIT
,
152 TCP_CONNTRACK_CLOSE_WAIT
,
153 TCP_CONNTRACK_LAST_ACK
,
154 TCP_CONNTRACK_TIME_WAIT
,
156 TCP_CONNTRACK_SYN_SENT2
,
160 static const char *const dccp_state2str
[DCCP_CONNTRACK_MAX
] = {
161 [DCCP_CONNTRACK_NONE
] = "NOSTATE",
162 [DCCP_CONNTRACK_REQUEST
] = "REQUEST",
163 [DCCP_CONNTRACK_RESPOND
] = "RESPOND",
164 [DCCP_CONNTRACK_PARTOPEN
] = "PARTOPEN",
165 [DCCP_CONNTRACK_OPEN
] = "OPEN",
166 [DCCP_CONNTRACK_CLOSEREQ
] = "CLOSEREQ",
167 [DCCP_CONNTRACK_CLOSING
] = "CLOSING",
168 [DCCP_CONNTRACK_TIMEWAIT
] = "TIMEWAIT",
169 [DCCP_CONNTRACK_IGNORE
] = "IGNORE",
170 [DCCP_CONNTRACK_INVALID
] = "INVALID",
173 static const uint8_t dccp_states
[] = {
175 DCCP_CONNTRACK_REQUEST
,
176 DCCP_CONNTRACK_RESPOND
,
177 DCCP_CONNTRACK_PARTOPEN
,
179 DCCP_CONNTRACK_CLOSEREQ
,
180 DCCP_CONNTRACK_CLOSING
,
181 DCCP_CONNTRACK_TIMEWAIT
,
182 DCCP_CONNTRACK_IGNORE
,
183 DCCP_CONNTRACK_INVALID
,
186 static const char *const sctp_state2str
[SCTP_CONNTRACK_MAX
] = {
187 [SCTP_CONNTRACK_NONE
] = "NOSTATE",
188 [SCTP_CONNTRACK_CLOSED
] = "CLOSED",
189 [SCTP_CONNTRACK_COOKIE_WAIT
] = "COOKIE_WAIT",
190 [SCTP_CONNTRACK_COOKIE_ECHOED
] = "COOKIE_ECHOED",
191 [SCTP_CONNTRACK_ESTABLISHED
] = "ESTABLISHED",
192 [SCTP_CONNTRACK_SHUTDOWN_SENT
] = "SHUTDOWN_SENT",
193 [SCTP_CONNTRACK_SHUTDOWN_RECD
] = "SHUTDOWN_RECD",
194 [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
] = "SHUTDOWN_ACK_SENT",
197 static const uint8_t sctp_states
[] = {
199 SCTP_CONNTRACK_CLOSED
,
200 SCTP_CONNTRACK_COOKIE_WAIT
,
201 SCTP_CONNTRACK_COOKIE_ECHOED
,
202 SCTP_CONNTRACK_ESTABLISHED
,
203 SCTP_CONNTRACK_SHUTDOWN_SENT
,
204 SCTP_CONNTRACK_SHUTDOWN_RECD
,
205 SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
,
208 static const struct nfct_filter_ipv4 filter_ipv4
= {
209 .addr
= __constant_htonl(INADDR_LOOPBACK
),
213 static const struct nfct_filter_ipv6 filter_ipv6
= {
214 .addr
= { 0x0, 0x0, 0x0, 0x1 },
215 .mask
= { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff },
218 static void signal_handler(int number
)
232 static void flow_entry_from_ct(struct flow_entry
*n
, struct nf_conntrack
*ct
);
233 static void flow_entry_get_extended(struct flow_entry
*n
);
235 static void help(void)
237 printf("\nflowtop %s, top-like netfilter TCP/UDP/SCTP/.. flow tracking\n",
239 puts("http://www.netsniff-ng.org\n\n"
240 "Usage: flowtop [options]\n"
242 " -4|--ipv4 Show only IPv4 flows (default)\n"
243 " -6|--ipv6 Show only IPv6 flows (default)\n"
244 " -T|--tcp Show only TCP flows (default)\n"
245 " -U|--udp Show only UDP flows\n"
246 " -D|--dccp Show only DCCP flows\n"
247 " -I|--icmp Show only ICMP/ICMPv6 flows\n"
248 " -S|--sctp Show only SCTP flows\n"
249 " -s|--show-src Also show source, not only dest\n"
250 " -u|--update Update GeoIP databases\n"
251 " -v|--version Print version and exit\n"
252 " -h|--help Print this help and exit\n\n"
255 " flowtop -46UTDISs\n\n"
257 " If netfilter is not running, you can activate it with e.g.:\n"
258 " iptables -A INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT\n"
259 " iptables -A OUTPUT -p tcp -m state --state NEW,ESTABLISHED -j ACCEPT\n\n");
264 static void version(void)
266 printf("\nflowtop %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
267 puts("top-like netfilter TCP/UDP/SCTP/.. flow tracking\n"
268 "http://www.netsniff-ng.org\n\n");
273 static inline struct flow_entry
*flow_entry_xalloc(void)
275 return xzmalloc(sizeof(struct flow_entry
));
278 static inline void flow_entry_xfree(struct flow_entry
*n
)
283 static inline void flow_list_init(struct flow_list
*fl
)
286 spinlock_init(&fl
->lock
);
289 static void flow_list_new_entry(struct flow_list
*fl
, struct nf_conntrack
*ct
)
291 struct flow_entry
*n
= flow_entry_xalloc();
293 flow_entry_from_ct(n
, ct
);
294 flow_entry_get_extended(n
);
296 rcu_assign_pointer(n
->next
, fl
->head
);
297 rcu_assign_pointer(fl
->head
, n
);
300 static struct flow_entry
*flow_list_find_id(struct flow_list
*fl
,
303 struct flow_entry
*n
= rcu_dereference(fl
->head
);
306 if (n
->flow_id
== id
)
309 n
= rcu_dereference(n
->next
);
315 static struct flow_entry
*flow_list_find_prev_id(struct flow_list
*fl
,
318 struct flow_entry
*n
= rcu_dereference(fl
->head
), *tmp
;
320 if (n
->flow_id
== id
)
323 while ((tmp
= rcu_dereference(n
->next
)) != NULL
) {
324 if (tmp
->flow_id
== id
)
333 static void flow_list_update_entry(struct flow_list
*fl
,
334 struct nf_conntrack
*ct
)
337 struct flow_entry
*n
;
339 n
= flow_list_find_id(fl
, nfct_get_attr_u32(ct
, ATTR_ID
));
341 n
= flow_entry_xalloc();
345 flow_entry_from_ct(n
, ct
);
347 flow_entry_get_extended(n
);
349 rcu_assign_pointer(n
->next
, fl
->head
);
350 rcu_assign_pointer(fl
->head
, n
);
354 static void flow_list_destroy_entry(struct flow_list
*fl
,
355 struct nf_conntrack
*ct
)
357 struct flow_entry
*n1
, *n2
;
358 uint32_t id
= nfct_get_attr_u32(ct
, ATTR_ID
);
360 n1
= flow_list_find_id(fl
, id
);
362 n2
= flow_list_find_prev_id(fl
, id
);
364 rcu_assign_pointer(n2
->next
, n1
->next
);
367 flow_entry_xfree(n1
);
369 flow_entry_xfree(fl
->head
);
375 static void flow_list_destroy(struct flow_list
*fl
)
377 struct flow_entry
*n
;
379 while (fl
->head
!= NULL
) {
380 n
= rcu_dereference(fl
->head
->next
);
381 fl
->head
->next
= NULL
;
383 flow_entry_xfree(fl
->head
);
384 rcu_assign_pointer(fl
->head
, n
);
388 spinlock_destroy(&fl
->lock
);
391 static int walk_process(const char *process
, struct flow_entry
*n
)
398 if (snprintf(path
, sizeof(path
), "/proc/%s/fd", process
) == -1)
399 panic("giant process name! %s\n", process
);
405 while ((ent
= readdir(dir
))) {
408 if (snprintf(path
, sizeof(path
), "/proc/%s/fd/%s",
409 process
, ent
->d_name
) < 0)
412 if (stat(path
, &statbuf
) < 0)
415 if (S_ISSOCK(statbuf
.st_mode
) && (ino_t
) n
->inode
== statbuf
.st_ino
) {
416 memset(n
->cmdline
, 0, sizeof(n
->cmdline
));
418 snprintf(path
, sizeof(path
), "/proc/%s/exe", process
);
420 ret
= readlink(path
, n
->cmdline
,
421 sizeof(n
->cmdline
) - 1);
423 panic("readlink error: %s\n", strerror(errno
));
425 n
->procnum
= atoi(process
);
435 static void walk_processes(struct flow_entry
*n
)
441 /* n->inode must be set */
443 memset(n
->cmdline
, 0, sizeof(n
->cmdline
));
447 dir
= opendir("/proc");
449 panic("Cannot open /proc!\n");
451 while ((ent
= readdir(dir
))) {
452 if (strspn(ent
->d_name
, "0123456789") == strlen(ent
->d_name
)) {
453 ret
= walk_process(ent
->d_name
, n
);
462 static int get_port_inode(uint16_t port
, int proto
, int is_ip6
)
465 char path
[128], buff
[1024];
468 memset(path
, 0, sizeof(path
));
469 snprintf(path
, sizeof(path
), "/proc/net/%s%s",
470 l4proto2str
[proto
], is_ip6
? "6" : "");
472 proc
= fopen(path
, "r");
476 memset(buff
, 0, sizeof(buff
));
478 while (fgets(buff
, sizeof(buff
), proc
) != NULL
) {
480 unsigned int lport
= 0;
482 buff
[sizeof(buff
) - 1] = 0;
483 if (sscanf(buff
, "%*u: %*X:%X %*X:%*X %*X %*X:%*X %*X:%*X "
484 "%*X %*u %*u %u", &lport
, &inode
) == 2) {
485 if ((uint16_t) lport
== port
) {
491 memset(buff
, 0, sizeof(buff
));
498 #define CP_NFCT(elem, attr, x) \
499 do { n->elem = nfct_get_attr_u##x(ct,(attr)); } while (0)
500 #define CP_NFCT_BUFF(elem, attr) do { \
501 const uint8_t *buff = nfct_get_attr(ct,(attr)); \
503 memcpy(n->elem, buff, sizeof(n->elem)); \
506 static void flow_entry_from_ct(struct flow_entry
*n
, struct nf_conntrack
*ct
)
508 CP_NFCT(l3_proto
, ATTR_ORIG_L3PROTO
, 8);
509 CP_NFCT(l4_proto
, ATTR_ORIG_L4PROTO
, 8);
511 CP_NFCT(ip4_src_addr
, ATTR_ORIG_IPV4_SRC
, 32);
512 CP_NFCT(ip4_dst_addr
, ATTR_ORIG_IPV4_DST
, 32);
514 CP_NFCT(port_src
, ATTR_ORIG_PORT_SRC
, 16);
515 CP_NFCT(port_dst
, ATTR_ORIG_PORT_DST
, 16);
517 CP_NFCT(status
, ATTR_STATUS
, 32);
519 CP_NFCT(tcp_state
, ATTR_TCP_STATE
, 8);
520 CP_NFCT(tcp_flags
, ATTR_TCP_FLAGS_ORIG
, 8);
521 CP_NFCT(sctp_state
, ATTR_SCTP_STATE
, 8);
522 CP_NFCT(dccp_state
, ATTR_DCCP_STATE
, 8);
524 CP_NFCT(counter_pkts
, ATTR_ORIG_COUNTER_PACKETS
, 64);
525 CP_NFCT(counter_bytes
, ATTR_ORIG_COUNTER_BYTES
, 64);
527 CP_NFCT(timestamp_start
, ATTR_TIMESTAMP_START
, 64);
528 CP_NFCT(timestamp_stop
, ATTR_TIMESTAMP_STOP
, 64);
530 CP_NFCT(flow_id
, ATTR_ID
, 32);
531 CP_NFCT(use
, ATTR_USE
, 32);
533 CP_NFCT_BUFF(ip6_src_addr
, ATTR_ORIG_IPV6_SRC
);
534 CP_NFCT_BUFF(ip6_dst_addr
, ATTR_ORIG_IPV6_DST
);
536 n
->port_src
= ntohs(n
->port_src
);
537 n
->port_dst
= ntohs(n
->port_dst
);
539 n
->ip4_src_addr
= ntohl(n
->ip4_src_addr
);
540 n
->ip4_dst_addr
= ntohl(n
->ip4_dst_addr
);
543 enum flow_entry_direction
{
548 static inline int flow_entry_get_extended_is_dns(struct flow_entry
*n
)
550 /* We don't want to analyze / display DNS itself, since we
551 * use it to resolve reverse dns.
553 return n
->port_src
== 53 || n
->port_dst
== 53;
556 #define SELFLD(dir,src_member,dst_member) \
557 (((dir) == flow_entry_src) ? n->src_member : n->dst_member)
559 static void flow_entry_get_sain4_obj(struct flow_entry
*n
,
560 enum flow_entry_direction dir
,
561 struct sockaddr_in
*sa
)
563 memset(sa
, 0, sizeof(*sa
));
564 sa
->sin_family
= PF_INET
;
565 sa
->sin_addr
.s_addr
= htonl(SELFLD(dir
, ip4_src_addr
, ip4_dst_addr
));
568 static void flow_entry_get_sain6_obj(struct flow_entry
*n
,
569 enum flow_entry_direction dir
,
570 struct sockaddr_in6
*sa
)
572 memset(sa
, 0, sizeof(*sa
));
573 sa
->sin6_family
= PF_INET6
;
575 memcpy(&sa
->sin6_addr
, SELFLD(dir
, ip6_src_addr
, ip6_dst_addr
),
576 sizeof(sa
->sin6_addr
));
580 flow_entry_geo_city_lookup_generic(struct flow_entry
*n
,
581 enum flow_entry_direction dir
)
583 struct sockaddr_in sa4
;
584 struct sockaddr_in6 sa6
;
585 const char *city
= NULL
;
587 switch (n
->l3_proto
) {
592 flow_entry_get_sain4_obj(n
, dir
, &sa4
);
593 city
= geoip4_city_name(&sa4
);
597 flow_entry_get_sain6_obj(n
, dir
, &sa6
);
598 city
= geoip6_city_name(&sa6
);
602 bug_on(sizeof(n
->city_src
) != sizeof(n
->city_dst
));
605 memcpy(SELFLD(dir
, city_src
, city_dst
), city
,
606 min(sizeof(n
->city_src
), strlen(city
)));
608 memset(SELFLD(dir
, city_src
, city_dst
), 0,
609 sizeof(n
->city_src
));
614 flow_entry_geo_country_lookup_generic(struct flow_entry
*n
,
615 enum flow_entry_direction dir
)
617 struct sockaddr_in sa4
;
618 struct sockaddr_in6 sa6
;
619 const char *country
= NULL
;
621 switch (n
->l3_proto
) {
626 flow_entry_get_sain4_obj(n
, dir
, &sa4
);
627 country
= geoip4_country_name(&sa4
);
631 flow_entry_get_sain6_obj(n
, dir
, &sa6
);
632 country
= geoip6_country_name(&sa6
);
636 bug_on(sizeof(n
->country_src
) != sizeof(n
->country_dst
));
639 memcpy(SELFLD(dir
, country_src
, country_dst
), country
,
640 min(sizeof(n
->country_src
), strlen(country
)));
642 memset(SELFLD(dir
, country_src
, country_dst
), 0,
643 sizeof(n
->country_src
));
647 static void flow_entry_get_extended_geo(struct flow_entry
*n
,
648 enum flow_entry_direction dir
)
650 flow_entry_geo_city_lookup_generic(n
, dir
);
651 flow_entry_geo_country_lookup_generic(n
, dir
);
654 static void flow_entry_get_extended_revdns(struct flow_entry
*n
,
655 enum flow_entry_direction dir
)
658 struct sockaddr_in sa4
;
659 struct sockaddr_in6 sa6
;
661 struct hostent
*hent
;
663 switch (n
->l3_proto
) {
668 flow_entry_get_sain4_obj(n
, dir
, &sa4
);
669 sa
= (struct sockaddr
*) &sa4
;
670 sa_len
= sizeof(sa4
);
671 hent
= gethostbyaddr(&sa4
.sin_addr
, sizeof(sa4
.sin_addr
), AF_INET
);
675 flow_entry_get_sain6_obj(n
, dir
, &sa6
);
676 sa
= (struct sockaddr
*) &sa6
;
677 sa_len
= sizeof(sa6
);
678 hent
= gethostbyaddr(&sa6
.sin6_addr
, sizeof(sa6
.sin6_addr
), AF_INET6
);
682 bug_on(sizeof(n
->rev_dns_src
) != sizeof(n
->rev_dns_dst
));
683 getnameinfo(sa
, sa_len
, SELFLD(dir
, rev_dns_src
, rev_dns_dst
),
684 sizeof(n
->rev_dns_src
), NULL
, 0, NI_NUMERICHOST
);
687 memset(n
->rev_dns_dst
, 0, sizeof(n
->rev_dns_dst
));
688 memcpy(SELFLD(dir
, rev_dns_src
, rev_dns_dst
),
689 hent
->h_name
, min(sizeof(n
->rev_dns_src
),
690 strlen(hent
->h_name
)));
694 static void flow_entry_get_extended(struct flow_entry
*n
)
696 if (n
->flow_id
== 0 || flow_entry_get_extended_is_dns(n
))
699 flow_entry_get_extended_revdns(n
, flow_entry_src
);
700 flow_entry_get_extended_geo(n
, flow_entry_src
);
702 flow_entry_get_extended_revdns(n
, flow_entry_dst
);
703 flow_entry_get_extended_geo(n
, flow_entry_dst
);
705 /* Lookup application */
706 n
->inode
= get_port_inode(n
->port_src
, n
->l4_proto
,
707 n
->l3_proto
== AF_INET6
);
712 static uint16_t presenter_get_port(uint16_t src
, uint16_t dst
, int tcp
)
714 if (src
< dst
&& src
< 1024) {
716 } else if (dst
< src
&& dst
< 1024) {
719 const char *tmp1
, *tmp2
;
721 tmp1
= lookup_port_tcp(src
);
722 tmp2
= lookup_port_tcp(dst
);
724 tmp1
= lookup_port_udp(src
);
725 tmp2
= lookup_port_udp(dst
);
729 } else if (!tmp1
&& tmp2
) {
740 static void presenter_screen_do_line(WINDOW
*screen
, struct flow_entry
*n
,
743 char tmp
[128], *pname
= NULL
;
746 mvwprintw(screen
, *line
, 2, "");
748 /* PID, application name */
749 if (n
->procnum
> 0) {
750 slprintf(tmp
, sizeof(tmp
), "%s(%d)", basename(n
->cmdline
),
754 attron(COLOR_PAIR(3));
756 attroff(COLOR_PAIR(3));
760 /* L3 protocol, L4 protocol, states */
761 printw("%s:%s", l3proto2str
[n
->l3_proto
], l4proto2str
[n
->l4_proto
]);
763 attron(COLOR_PAIR(3));
764 switch (n
->l4_proto
) {
766 printw("%s", tcp_state2str
[n
->tcp_state
]);
769 printw("%s", sctp_state2str
[n
->sctp_state
]);
772 printw("%s", dccp_state2str
[n
->dccp_state
]);
775 case IPPROTO_UDPLITE
:
781 attroff(COLOR_PAIR(3));
784 /* Guess application port */
785 switch (n
->l4_proto
) {
787 port
= presenter_get_port(n
->port_src
, n
->port_dst
, 1);
788 pname
= lookup_port_tcp(port
);
791 case IPPROTO_UDPLITE
:
792 port
= presenter_get_port(n
->port_src
, n
->port_dst
, 0);
793 pname
= lookup_port_udp(port
);
798 printw(":%s", pname
);
803 /* Number packets, bytes */
804 if (n
->counter_pkts
> 0 && n
->counter_bytes
> 0)
805 printw(" (%"PRIu64
" pkts, %"PRIu64
" bytes) ->",
806 n
->counter_pkts
, n
->counter_bytes
);
808 /* Show source information: reverse DNS, port, country, city */
810 attron(COLOR_PAIR(1));
811 mvwprintw(screen
, ++(*line
), 8, "src: %s", n
->rev_dns_src
);
812 attroff(COLOR_PAIR(1));
814 printw(":%"PRIu16
, n
->port_src
);
816 if (n
->country_src
[0]) {
819 attron(COLOR_PAIR(4));
820 printw("%s", n
->country_src
);
821 attroff(COLOR_PAIR(4));
824 printw(", %s", n
->city_src
);
832 /* Show dest information: reverse DNS, port, country, city */
833 attron(COLOR_PAIR(2));
834 mvwprintw(screen
, ++(*line
), 8, "dst: %s", n
->rev_dns_dst
);
835 attroff(COLOR_PAIR(2));
837 printw(":%"PRIu16
, n
->port_dst
);
839 if (n
->country_dst
[0]) {
842 attron(COLOR_PAIR(4));
843 printw("%s", n
->country_dst
);
844 attroff(COLOR_PAIR(4));
847 printw(", %s", n
->city_dst
);
853 static inline int presenter_flow_wrong_state(struct flow_entry
*n
, int state
)
857 switch (n
->l4_proto
) {
859 if (n
->tcp_state
== state
)
863 if (n
->sctp_state
== state
)
867 if (n
->dccp_state
== state
)
871 case IPPROTO_UDPLITE
:
881 static void presenter_screen_update(WINDOW
*screen
, struct flow_list
*fl
,
886 unsigned int line
= 3;
887 struct flow_entry
*n
;
888 uint8_t protocols
[] = {
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
),
902 [IPPROTO_UDPLITE
] = 1,
904 [IPPROTO_ICMPV6
] = 1,
909 maxy
= getmaxy(screen
);
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
);
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, " : "",
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) {
942 if (n
->l4_proto
!= protocols
[i
])
944 if (presenter_flow_wrong_state(n
, j
))
946 if (presenter_get_port(n
->port_src
,
947 n
->port_dst
, 0) == 53)
950 n
= rcu_dereference(n
->next
);
953 if (skip_lines
> 0) {
954 n
= rcu_dereference(n
->next
);
959 presenter_screen_do_line(screen
, n
, &line
);
962 maxy
-= (2 + 1 * show_src
);
963 n
= rcu_dereference(n
->next
);
974 static void presenter(void)
979 lookup_init_ports(PORTS_TCP
);
980 lookup_init_ports(PORTS_UDP
);
981 screen
= screen_init(false);
983 rcu_register_thread();
1000 if (skip_lines
> SCROLL_MAX
)
1001 skip_lines
= SCROLL_MAX
;
1008 presenter_screen_update(screen
, &flow_list
, skip_lines
);
1011 rcu_unregister_thread();
1014 lookup_cleanup_ports(PORTS_UDP
);
1015 lookup_cleanup_ports(PORTS_TCP
);
1018 static int collector_cb(enum nf_conntrack_msg_type type
,
1019 struct nf_conntrack
*ct
, void *data __maybe_unused
)
1022 return NFCT_CB_STOP
;
1025 spinlock_lock(&flow_list
.lock
);
1029 flow_list_new_entry(&flow_list
, ct
);
1032 flow_list_update_entry(&flow_list
, ct
);
1034 case NFCT_T_DESTROY
:
1035 flow_list_destroy_entry(&flow_list
, ct
);
1041 spinlock_unlock(&flow_list
.lock
);
1043 return NFCT_CB_CONTINUE
;
1046 static inline void collector_flush(struct nfct_handle
*handle
, uint8_t family
)
1048 nfct_query(handle
, NFCT_Q_FLUSH
, &family
);
1051 static void *collector(void *null __maybe_unused
)
1054 struct nfct_handle
*handle
;
1055 struct nfct_filter
*filter
;
1057 handle
= nfct_open(CONNTRACK
, NF_NETLINK_CONNTRACK_NEW
|
1058 NF_NETLINK_CONNTRACK_UPDATE
|
1059 NF_NETLINK_CONNTRACK_DESTROY
);
1061 panic("Cannot create a nfct handle!\n");
1063 collector_flush(handle
, AF_INET
);
1064 collector_flush(handle
, AF_INET6
);
1066 filter
= nfct_filter_create();
1068 panic("Cannot create a nfct filter!\n");
1070 ret
= nfct_filter_attach(nfct_fd(handle
), filter
);
1072 panic("Cannot attach filter to handle!\n");
1074 if (what
& INCLUDE_UDP
) {
1075 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_UDP
);
1076 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_UDPLITE
);
1078 if (what
& INCLUDE_TCP
)
1079 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_TCP
);
1080 if (what
& INCLUDE_DCCP
)
1081 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_DCCP
);
1082 if (what
& INCLUDE_SCTP
)
1083 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_SCTP
);
1084 if (what
& INCLUDE_ICMP
&& what
& INCLUDE_IPV4
)
1085 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_ICMP
);
1086 if (what
& INCLUDE_ICMP
&& what
& INCLUDE_IPV6
)
1087 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_ICMPV6
);
1088 if (what
& INCLUDE_IPV4
) {
1089 nfct_filter_set_logic(filter
, NFCT_FILTER_SRC_IPV4
, NFCT_FILTER_LOGIC_NEGATIVE
);
1090 nfct_filter_add_attr(filter
, NFCT_FILTER_SRC_IPV4
, &filter_ipv4
);
1092 if (what
& INCLUDE_IPV6
) {
1093 nfct_filter_set_logic(filter
, NFCT_FILTER_SRC_IPV6
, NFCT_FILTER_LOGIC_NEGATIVE
);
1094 nfct_filter_add_attr(filter
, NFCT_FILTER_SRC_IPV6
, &filter_ipv6
);
1097 ret
= nfct_filter_attach(nfct_fd(handle
), filter
);
1099 panic("Cannot attach filter to handle!\n");
1101 nfct_callback_register(handle
, NFCT_T_ALL
, collector_cb
, NULL
);
1102 nfct_filter_destroy(filter
);
1103 flow_list_init(&flow_list
);
1105 rcu_register_thread();
1107 while (!sigint
&& ret
>= 0)
1108 ret
= nfct_catch(handle
);
1110 rcu_unregister_thread();
1112 flow_list_destroy(&flow_list
);
1118 int main(int argc
, char **argv
)
1121 int ret
, c
, opt_index
, what_cmd
= 0;
1126 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1127 &opt_index
)) != EOF
) {
1130 what_cmd
|= INCLUDE_IPV4
;
1133 what_cmd
|= INCLUDE_IPV6
;
1136 what_cmd
|= INCLUDE_TCP
;
1139 what_cmd
|= INCLUDE_UDP
;
1142 what_cmd
|= INCLUDE_DCCP
;
1145 what_cmd
|= INCLUDE_ICMP
;
1148 what_cmd
|= INCLUDE_SCTP
;
1173 register_signal(SIGINT
, signal_handler
);
1174 register_signal(SIGQUIT
, signal_handler
);
1175 register_signal(SIGTERM
, signal_handler
);
1176 register_signal(SIGHUP
, signal_handler
);
1180 ret
= pthread_create(&tid
, NULL
, collector
, NULL
);
1182 panic("Cannot create phthread!\n");