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
;
59 struct flow_entry
*head
;
63 #ifndef ATTR_TIMESTAMP_START
64 # define ATTR_TIMESTAMP_START 63
66 #ifndef ATTR_TIMESTAMP_STOP
67 # define ATTR_TIMESTAMP_STOP 64
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'},
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
] = {
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",
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
,
157 TCP_CONNTRACK_SYN_SENT2
,
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
[] = {
176 DCCP_CONNTRACK_REQUEST
,
177 DCCP_CONNTRACK_RESPOND
,
178 DCCP_CONNTRACK_PARTOPEN
,
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
[] = {
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
),
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
)
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",
240 puts("http://www.netsniff-ng.org\n\n"
241 "Usage: flowtop [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"
256 " flowtop -46UTDISs\n\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");
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");
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
)
284 static inline void flow_list_init(struct flow_list
*fl
)
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
,
304 struct flow_entry
*n
= rcu_dereference(fl
->head
);
307 if (n
->flow_id
== id
)
310 n
= rcu_dereference(n
->next
);
316 static struct flow_entry
*flow_list_find_prev_id(struct flow_list
*fl
,
319 struct flow_entry
*n
= rcu_dereference(fl
->head
), *tmp
;
321 if (n
->flow_id
== id
)
324 while ((tmp
= rcu_dereference(n
->next
)) != NULL
) {
325 if (tmp
->flow_id
== id
)
334 static void flow_list_update_entry(struct flow_list
*fl
,
335 struct nf_conntrack
*ct
)
338 struct flow_entry
*n
;
340 n
= flow_list_find_id(fl
, nfct_get_attr_u32(ct
, ATTR_ID
));
342 n
= flow_entry_xalloc();
346 flow_entry_from_ct(n
, ct
);
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
);
363 n2
= flow_list_find_prev_id(fl
, id
);
365 rcu_assign_pointer(n2
->next
, n1
->next
);
368 flow_entry_xfree(n1
);
370 flow_entry_xfree(fl
->head
);
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
);
389 spinlock_destroy(&fl
->lock
);
392 static int walk_process(unsigned int pid
, struct flow_entry
*n
)
399 if (snprintf(path
, sizeof(path
), "/proc/%u/fd", pid
) == -1)
400 panic("giant process name! %u\n", pid
);
406 while ((ent
= readdir(dir
))) {
409 if (snprintf(path
, sizeof(path
), "/proc/%u/fd/%s",
410 pid
, ent
->d_name
) < 0)
413 if (stat(path
, &statbuf
) < 0)
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);
424 panic("readlink error: %s\n", strerror(errno
));
436 static void walk_processes(struct flow_entry
*n
)
442 /* n->inode must be set */
444 memset(n
->cmdline
, 0, sizeof(n
->cmdline
));
448 dir
= opendir("/proc");
450 panic("Cannot open /proc!\n");
452 while ((ent
= readdir(dir
))) {
453 const char *name
= ent
->d_name
;
455 unsigned int pid
= strtoul(name
, &end
, 10);
458 if (pid
== 0 && end
== name
)
461 ret
= walk_process(pid
, n
);
469 static int get_port_inode(uint16_t port
, int proto
, int is_ip6
)
472 char path
[128], buff
[1024];
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");
483 memset(buff
, 0, sizeof(buff
));
485 while (fgets(buff
, sizeof(buff
), proc
) != NULL
) {
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
) {
498 memset(buff
, 0, sizeof(buff
));
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)); \
510 memcpy(n->elem, buff, sizeof(n->elem)); \
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
{
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
));
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
) {
599 flow_entry_get_sain4_obj(n
, dir
, &sa4
);
600 city
= geoip4_city_name(&sa4
);
604 flow_entry_get_sain6_obj(n
, dir
, &sa6
);
605 city
= geoip6_city_name(&sa6
);
609 bug_on(sizeof(n
->city_src
) != sizeof(n
->city_dst
));
612 memcpy(SELFLD(dir
, city_src
, city_dst
), city
,
613 min(sizeof(n
->city_src
), strlen(city
)));
615 memset(SELFLD(dir
, city_src
, city_dst
), 0,
616 sizeof(n
->city_src
));
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
) {
633 flow_entry_get_sain4_obj(n
, dir
, &sa4
);
634 country
= geoip4_country_name(&sa4
);
638 flow_entry_get_sain6_obj(n
, dir
, &sa6
);
639 country
= geoip6_country_name(&sa6
);
643 bug_on(sizeof(n
->country_src
) != sizeof(n
->country_dst
));
646 memcpy(SELFLD(dir
, country_src
, country_dst
), country
,
647 min(sizeof(n
->country_src
), strlen(country
)));
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
)
665 struct sockaddr_in sa4
;
666 struct sockaddr_in6 sa6
;
668 struct hostent
*hent
;
670 switch (n
->l3_proto
) {
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
);
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
);
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
);
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
))
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
);
719 static uint16_t presenter_get_port(uint16_t src
, uint16_t dst
, int tcp
)
721 if (src
< dst
&& src
< 1024) {
723 } else if (dst
< src
&& dst
< 1024) {
726 const char *tmp1
, *tmp2
;
728 tmp1
= lookup_port_tcp(src
);
729 tmp2
= lookup_port_tcp(dst
);
731 tmp1
= lookup_port_udp(src
);
732 tmp2
= lookup_port_udp(dst
);
736 } else if (!tmp1
&& tmp2
) {
747 static void presenter_screen_do_line(WINDOW
*screen
, struct flow_entry
*n
,
750 char tmp
[128], *pname
= NULL
;
753 mvwprintw(screen
, *line
, 2, "");
755 /* PID, application name */
756 if (n
->procnum
> 0) {
757 slprintf(tmp
, sizeof(tmp
), "%s(%d)", basename(n
->cmdline
),
761 attron(COLOR_PAIR(3));
763 attroff(COLOR_PAIR(3));
767 /* L3 protocol, L4 protocol, states */
768 printw("%s:%s", l3proto2str
[n
->l3_proto
], l4proto2str
[n
->l4_proto
]);
770 attron(COLOR_PAIR(3));
771 switch (n
->l4_proto
) {
773 printw("%s", tcp_state2str
[n
->tcp_state
]);
776 printw("%s", sctp_state2str
[n
->sctp_state
]);
779 printw("%s", dccp_state2str
[n
->dccp_state
]);
782 case IPPROTO_UDPLITE
:
788 attroff(COLOR_PAIR(3));
791 /* Guess application port */
792 switch (n
->l4_proto
) {
794 port
= presenter_get_port(n
->port_src
, n
->port_dst
, 1);
795 pname
= lookup_port_tcp(port
);
798 case IPPROTO_UDPLITE
:
799 port
= presenter_get_port(n
->port_src
, n
->port_dst
, 0);
800 pname
= lookup_port_udp(port
);
805 printw(":%s", pname
);
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 */
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]) {
826 attron(COLOR_PAIR(4));
827 printw("%s", n
->country_src
);
828 attroff(COLOR_PAIR(4));
831 printw(", %s", n
->city_src
);
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]) {
849 attron(COLOR_PAIR(4));
850 printw("%s", n
->country_dst
);
851 attroff(COLOR_PAIR(4));
854 printw(", %s", n
->city_dst
);
860 static inline int presenter_flow_wrong_state(struct flow_entry
*n
, int state
)
864 switch (n
->l4_proto
) {
866 if (n
->tcp_state
== state
)
870 if (n
->sctp_state
== state
)
874 if (n
->dccp_state
== state
)
878 case IPPROTO_UDPLITE
:
888 static void presenter_screen_update(WINDOW
*screen
, struct flow_list
*fl
,
893 unsigned int line
= 3;
894 struct flow_entry
*n
;
895 uint8_t protocols
[] = {
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
),
909 [IPPROTO_UDPLITE
] = 1,
911 [IPPROTO_ICMPV6
] = 1,
916 maxy
= getmaxy(screen
);
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
);
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, " : "",
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) {
952 n
= rcu_dereference(n
->next
);
955 if (skip_lines
> 0) {
956 n
= rcu_dereference(n
->next
);
961 presenter_screen_do_line(screen
, n
, &line
);
964 maxy
-= (2 + 1 * show_src
);
965 n
= rcu_dereference(n
->next
);
976 static void presenter(void)
981 lookup_init_ports(PORTS_TCP
);
982 lookup_init_ports(PORTS_UDP
);
983 screen
= screen_init(false);
985 rcu_register_thread();
1002 if (skip_lines
> SCROLL_MAX
)
1003 skip_lines
= SCROLL_MAX
;
1010 presenter_screen_update(screen
, &flow_list
, skip_lines
);
1013 rcu_unregister_thread();
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
)
1024 return NFCT_CB_STOP
;
1027 spinlock_lock(&flow_list
.lock
);
1031 flow_list_new_entry(&flow_list
, ct
);
1034 flow_list_update_entry(&flow_list
, ct
);
1036 case NFCT_T_DESTROY
:
1037 flow_list_destroy_entry(&flow_list
, ct
);
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
)
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
);
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();
1070 panic("Cannot create a nfct filter!\n");
1072 ret
= nfct_filter_attach(nfct_fd(handle
), filter
);
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
);
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
);
1120 int main(int argc
, char **argv
)
1123 int ret
, c
, opt_index
, what_cmd
= 0;
1128 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1129 &opt_index
)) != EOF
) {
1132 what_cmd
|= INCLUDE_IPV4
;
1135 what_cmd
|= INCLUDE_IPV6
;
1138 what_cmd
|= INCLUDE_TCP
;
1141 what_cmd
|= INCLUDE_UDP
;
1144 what_cmd
|= INCLUDE_DCCP
;
1147 what_cmd
|= INCLUDE_ICMP
;
1150 what_cmd
|= INCLUDE_SCTP
;
1175 register_signal(SIGINT
, signal_handler
);
1176 register_signal(SIGQUIT
, signal_handler
);
1177 register_signal(SIGTERM
, signal_handler
);
1178 register_signal(SIGHUP
, signal_handler
);
1182 ret
= pthread_create(&tid
, NULL
, collector
, NULL
);
1184 panic("Cannot create phthread!\n");