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>
28 #include "conntrack.h"
35 #include "dissector_eth.h"
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];
52 struct flow_entry
*next
;
57 struct flow_entry
*head
;
61 #ifndef ATTR_TIMESTAMP_START
62 # define ATTR_TIMESTAMP_START 63
64 #ifndef ATTR_TIMESTAMP_STOP
65 # define ATTR_TIMESTAMP_STOP 64
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'},
98 static const char *const l3proto2str
[AF_MAX
] = {
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",
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
,
147 TCP_CONNTRACK_SYN_SENT2
,
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
[] = {
166 DCCP_CONNTRACK_REQUEST
,
167 DCCP_CONNTRACK_RESPOND
,
168 DCCP_CONNTRACK_PARTOPEN
,
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
[] = {
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
),
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
)
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",
228 puts("http://www.netsniff-ng.org\n\n"
229 "Usage: flowtop [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"
244 " flowtop -46UTDISs\n\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");
259 static void version(void)
261 printf("\nflowtop %s, top-like netfilter TCP/UDP flow tracking\n",
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");
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(char *process
, struct flow_entry
*n
)
399 if (snprintf(path
, sizeof(path
), "/proc/%s/fd", process
) == -1)
400 panic("giant process name! %s\n", process
);
406 while ((ent
= readdir(dir
))) {
409 if (snprintf(path
, sizeof(path
), "/proc/%s/fd/%s",
410 process
, ent
->d_name
) < 0)
413 if (stat(path
, &statbuf
) < 0)
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);
424 panic("readlink error: %s\n", strerror(errno
));
426 n
->procnum
= atoi(process
);
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 if (strspn(ent
->d_name
, "0123456789") == strlen(ent
->d_name
)) {
454 ret
= walk_process(ent
->d_name
, n
);
463 static int get_port_inode(uint16_t port
, int proto
, int is_ip6
)
466 char path
[128], buff
[1024];
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");
477 memset(buff
, 0, sizeof(buff
));
479 while (fgets(buff
, sizeof(buff
), proc
) != NULL
) {
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
) {
492 memset(buff
, 0, sizeof(buff
));
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)); \
504 memcpy(n->elem, buff, sizeof(n->elem)); \
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
{
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
));
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
) {
593 flow_entry_get_sain4_obj(n
, dir
, &sa4
);
594 city
= geoip4_city_name(sa4
);
598 flow_entry_get_sain6_obj(n
, dir
, &sa6
);
599 city
= geoip6_city_name(sa6
);
603 bug_on(sizeof(n
->city_src
) != sizeof(n
->city_dst
));
606 memcpy(SELFLD(dir
, city_src
, city_dst
), city
,
607 min(sizeof(n
->city_src
), strlen(city
)));
609 memset(SELFLD(dir
, city_src
, city_dst
), 0,
610 sizeof(n
->city_src
));
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
) {
627 flow_entry_get_sain4_obj(n
, dir
, &sa4
);
628 country
= geoip4_country_name(sa4
);
632 flow_entry_get_sain6_obj(n
, dir
, &sa6
);
633 country
= geoip6_country_name(sa6
);
637 bug_on(sizeof(n
->country_src
) != sizeof(n
->country_dst
));
640 memcpy(SELFLD(dir
, country_src
, country_dst
), country
,
641 min(sizeof(n
->country_src
), strlen(country
)));
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
)
659 struct sockaddr_in sa4
;
660 struct sockaddr_in6 sa6
;
662 struct hostent
*hent
;
664 switch (n
->l3_proto
) {
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
);
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
);
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
);
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
))
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
);
713 static uint16_t presenter_get_port(uint16_t src
, uint16_t dst
, int tcp
)
715 if (src
< dst
&& src
< 1024) {
717 } else if (dst
< src
&& dst
< 1024) {
720 const char *tmp1
, *tmp2
;
722 tmp1
= lookup_port_tcp(src
);
723 tmp2
= lookup_port_tcp(dst
);
725 tmp1
= lookup_port_udp(src
);
726 tmp2
= lookup_port_udp(dst
);
730 } else if (!tmp1
&& tmp2
) {
741 static void presenter_screen_do_line(WINDOW
*screen
, struct flow_entry
*n
,
744 char tmp
[128], *pname
= NULL
;
747 mvwprintw(screen
, *line
, 2, "");
749 /* PID, application name */
750 if (n
->procnum
> 0) {
751 slprintf(tmp
, sizeof(tmp
), "%s(%u)", basename(n
->cmdline
),
755 attron(COLOR_PAIR(3));
757 attroff(COLOR_PAIR(3));
761 /* L3 protocol, L4 protocol, states */
762 printw("%s:%s", l3proto2str
[n
->l3_proto
], l4proto2str
[n
->l4_proto
]);
764 attron(COLOR_PAIR(3));
765 switch (n
->l4_proto
) {
767 printw("%s", tcp_state2str
[n
->tcp_state
]);
770 printw("%s", sctp_state2str
[n
->sctp_state
]);
773 printw("%s", dccp_state2str
[n
->dccp_state
]);
776 case IPPROTO_UDPLITE
:
782 attroff(COLOR_PAIR(3));
785 /* Guess application port */
786 switch (n
->l4_proto
) {
788 port
= presenter_get_port(n
->port_src
, n
->port_dst
, 1);
789 pname
= lookup_port_tcp(port
);
792 case IPPROTO_UDPLITE
:
793 port
= presenter_get_port(n
->port_src
, n
->port_dst
, 0);
794 pname
= lookup_port_udp(port
);
799 printw(":%s", pname
);
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 */
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]) {
820 attron(COLOR_PAIR(4));
821 printw("%s", n
->country_src
);
822 attroff(COLOR_PAIR(4));
825 printw(", %s", n
->city_src
);
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]) {
843 attron(COLOR_PAIR(4));
844 printw("%s", n
->country_dst
);
845 attroff(COLOR_PAIR(4));
848 printw(", %s", n
->city_dst
);
854 static inline int presenter_flow_wrong_state(struct flow_entry
*n
, int state
)
858 switch (n
->l4_proto
) {
860 if (n
->tcp_state
== state
)
864 if (n
->sctp_state
== state
)
868 if (n
->dccp_state
== state
)
872 case IPPROTO_UDPLITE
:
882 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 dissector_init_ethernet(0);
980 screen
= screen_init(false);
982 rcu_register_thread();
999 if (skip_lines
> SCROLL_MAX
)
1000 skip_lines
= SCROLL_MAX
;
1007 presenter_screen_update(screen
, &flow_list
, skip_lines
);
1010 rcu_unregister_thread();
1013 dissector_cleanup_ethernet();
1016 static int collector_cb(enum nf_conntrack_msg_type type
,
1017 struct nf_conntrack
*ct
, void *data
)
1020 return NFCT_CB_STOP
;
1023 spinlock_lock(&flow_list
.lock
);
1027 flow_list_new_entry(&flow_list
, ct
);
1030 flow_list_update_entry(&flow_list
, ct
);
1032 case NFCT_T_DESTROY
:
1033 flow_list_destroy_entry(&flow_list
, ct
);
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
)
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
);
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();
1066 panic("Cannot create a nfct filter!\n");
1068 ret
= nfct_filter_attach(nfct_fd(handle
), filter
);
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
);
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
);
1116 int main(int argc
, char **argv
)
1119 int ret
, c
, opt_index
, what_cmd
= 0;
1124 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1125 &opt_index
)) != EOF
) {
1128 what_cmd
|= INCLUDE_IPV4
;
1131 what_cmd
|= INCLUDE_IPV6
;
1134 what_cmd
|= INCLUDE_TCP
;
1137 what_cmd
|= INCLUDE_UDP
;
1140 what_cmd
|= INCLUDE_DCCP
;
1143 what_cmd
|= INCLUDE_ICMP
;
1146 what_cmd
|= INCLUDE_SCTP
;
1171 register_signal(SIGINT
, signal_handler
);
1172 register_signal(SIGHUP
, signal_handler
);
1176 ret
= pthread_create(&tid
, NULL
, collector
, NULL
);
1178 panic("Cannot create phthread!\n");