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.
7 * The Dark Lord has Nine. But we have One, mightier than they: the White
8 * Rider. He has passed through the fire and the abyss, and they shall
9 * fear him. We will go where he leads.
11 * -- The Lord of the Rings, Aragorn,
12 * Chapter 'The White Rider'.
25 #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
26 #include <libnetfilter_conntrack/libnetfilter_conntrack_tcp.h>
27 #include <libnetfilter_conntrack/libnetfilter_conntrack_dccp.h>
28 #include <libnetfilter_conntrack/libnetfilter_conntrack_sctp.h>
29 #include <netinet/in.h>
33 #include <sys/fsuid.h>
44 #include "dissector_eth.h"
48 uint32_t flow_id
, use
, status
;
49 uint8_t l3_proto
, l4_proto
;
50 uint32_t ip4_src_addr
, ip4_dst_addr
;
51 uint32_t ip6_src_addr
[4], ip6_dst_addr
[4];
52 uint16_t port_src
, port_dst
;
53 uint8_t tcp_state
, tcp_flags
, sctp_state
, dccp_state
;
54 uint64_t counter_pkts
, counter_bytes
;
55 uint64_t timestamp_start
, timestamp_stop
;
56 char country_src
[128], country_dst
[128];
57 char city_src
[128], city_dst
[128];
58 char rev_dns_src
[256], rev_dns_dst
[256];
60 struct flow_entry
*next
;
65 struct flow_entry
*head
;
69 #ifndef ATTR_TIMESTAMP_START
70 # define ATTR_TIMESTAMP_START 63
72 #ifndef ATTR_TIMESTAMP_STOP
73 # define ATTR_TIMESTAMP_STOP 64
76 #define SCROLL_MAX 1000
78 #define INCLUDE_IPV4 (1 << 0)
79 #define INCLUDE_IPV6 (1 << 1)
80 #define INCLUDE_UDP (1 << 2)
81 #define INCLUDE_TCP (1 << 3)
82 #define INCLUDE_DCCP (1 << 4)
83 #define INCLUDE_ICMP (1 << 5)
84 #define INCLUDE_SCTP (1 << 6)
86 volatile sig_atomic_t sigint
= 0;
88 static int what
= INCLUDE_IPV4
| INCLUDE_IPV6
| INCLUDE_TCP
, show_src
= 0;
90 static struct flow_list flow_list
;
92 static const char *short_options
= "vhTUsDIS46u";
93 static const struct option long_options
[] = {
94 {"ipv4", no_argument
, NULL
, '4'},
95 {"ipv6", no_argument
, NULL
, '6'},
96 {"tcp", no_argument
, NULL
, 'T'},
97 {"udp", no_argument
, NULL
, 'U'},
98 {"dccp", no_argument
, NULL
, 'D'},
99 {"icmp", no_argument
, NULL
, 'I'},
100 {"sctp", no_argument
, NULL
, 'S'},
101 {"show-src", no_argument
, NULL
, 's'},
102 {"update", no_argument
, NULL
, 'u'},
103 {"version", no_argument
, NULL
, 'v'},
104 {"help", no_argument
, NULL
, 'h'},
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
)
231 static void flow_entry_from_ct(struct flow_entry
*n
, struct nf_conntrack
*ct
);
232 static void flow_entry_get_extended(struct flow_entry
*n
);
234 static void help(void)
236 printf("\nflowtop %s, top-like netfilter TCP/UDP flow tracking\n",
238 puts("http://www.netsniff-ng.org\n\n"
239 "Usage: flowtop [options]\n"
241 " -4|--ipv4 Show only IPv4 flows (default)\n"
242 " -6|--ipv6 Show only IPv6 flows (default)\n"
243 " -T|--tcp Show only TCP flows (default)\n"
244 " -U|--udp Show only UDP flows\n"
245 " -D|--dccp Show only DCCP flows\n"
246 " -I|--icmp Show only ICMP/ICMPv6 flows\n"
247 " -S|--sctp Show only SCTP flows\n"
248 " -s|--show-src Also show source, not only dest\n"
249 " -u|--update Update GeoIP databases\n"
250 " -v|--version Print version\n"
251 " -h|--help Print this help\n\n"
254 " flowtop -46UTDISs\n\n"
256 " If netfilter is not running, you can activate it with e.g.:\n"
257 " iptables -A INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT\n"
258 " iptables -A OUTPUT -p tcp -m state --state NEW,ESTABLISHED -j ACCEPT\n\n"
259 "Please report bugs to <bugs@netsniff-ng.org>\n"
260 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
261 "Copyright (C) 2011-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
262 "Swiss federal institute of technology (ETH Zurich)\n"
263 "License: GNU GPL version 2.0\n"
264 "This is free software: you are free to change and redistribute it.\n"
265 "There is NO WARRANTY, to the extent permitted by law.\n");
269 static void version(void)
271 printf("\nflowtop %s, top-like netfilter TCP/UDP flow tracking\n",
273 puts("http://www.netsniff-ng.org\n\n"
274 "Please report bugs to <bugs@netsniff-ng.org>\n"
275 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
276 "Copyright (C) 2011-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
277 "Swiss federal institute of technology (ETH Zurich)\n"
278 "License: GNU GPL version 2.0\n"
279 "This is free software: you are free to change and redistribute it.\n"
280 "There is NO WARRANTY, to the extent permitted by law.\n");
284 static inline struct flow_entry
*flow_entry_xalloc(void)
286 return xzmalloc(sizeof(struct flow_entry
));
289 static inline void flow_entry_xfree(struct flow_entry
*n
)
294 static inline void flow_list_init(struct flow_list
*fl
)
297 spinlock_init(&fl
->lock
);
300 static void flow_list_new_entry(struct flow_list
*fl
, struct nf_conntrack
*ct
)
302 struct flow_entry
*n
= flow_entry_xalloc();
304 flow_entry_from_ct(n
, ct
);
305 flow_entry_get_extended(n
);
307 rcu_assign_pointer(n
->next
, fl
->head
);
308 rcu_assign_pointer(fl
->head
, n
);
311 static struct flow_entry
*flow_list_find_id(struct flow_list
*fl
,
314 struct flow_entry
*n
= rcu_dereference(fl
->head
);
317 if (n
->flow_id
== id
)
320 n
= rcu_dereference(n
->next
);
326 static struct flow_entry
*flow_list_find_prev_id(struct flow_list
*fl
,
329 struct flow_entry
*n
= rcu_dereference(fl
->head
), *tmp
;
331 if (n
->flow_id
== id
)
334 while ((tmp
= rcu_dereference(n
->next
)) != NULL
) {
335 if (tmp
->flow_id
== id
)
344 static void flow_list_update_entry(struct flow_list
*fl
,
345 struct nf_conntrack
*ct
)
348 struct flow_entry
*n
;
350 n
= flow_list_find_id(fl
, nfct_get_attr_u32(ct
, ATTR_ID
));
352 n
= flow_entry_xalloc();
356 flow_entry_from_ct(n
, ct
);
358 flow_entry_get_extended(n
);
360 rcu_assign_pointer(n
->next
, fl
->head
);
361 rcu_assign_pointer(fl
->head
, n
);
365 static void flow_list_destroy_entry(struct flow_list
*fl
,
366 struct nf_conntrack
*ct
)
368 struct flow_entry
*n1
, *n2
;
369 uint32_t id
= nfct_get_attr_u32(ct
, ATTR_ID
);
371 n1
= flow_list_find_id(fl
, id
);
373 n2
= flow_list_find_prev_id(fl
, id
);
375 rcu_assign_pointer(n2
->next
, n1
->next
);
376 rcu_assign_pointer(n1
->next
, NULL
);
378 flow_entry_xfree(n1
);
380 flow_entry_xfree(fl
->head
);
382 rcu_assign_pointer(fl
->head
, NULL
);
387 static void flow_list_destroy(struct flow_list
*fl
)
389 struct flow_entry
*n
;
391 while (fl
->head
!= NULL
) {
392 n
= rcu_dereference(fl
->head
->next
);
393 rcu_assign_pointer(fl
->head
->next
, NULL
);
395 flow_entry_xfree(fl
->head
);
396 rcu_assign_pointer(fl
->head
, n
);
400 spinlock_destroy(&fl
->lock
);
403 static int walk_process(char *process
, struct flow_entry
*n
)
410 if (snprintf(path
, sizeof(path
), "/proc/%s/fd", process
) == -1)
411 panic("giant process name! %s\n", process
);
417 while ((ent
= readdir(dir
))) {
420 if (snprintf(path
, sizeof(path
), "/proc/%s/fd/%s",
421 process
, ent
->d_name
) < 0)
424 if (stat(path
, &statbuf
) < 0)
427 if (S_ISSOCK(statbuf
.st_mode
) && n
->inode
== statbuf
.st_ino
) {
428 memset(n
->cmdline
, 0, sizeof(n
->cmdline
));
430 snprintf(path
, sizeof(path
), "/proc/%s/exe", process
);
432 ret
= readlink(path
, n
->cmdline
,
433 sizeof(n
->cmdline
) - 1);
435 panic("readlink error: %s\n", strerror(errno
));
437 n
->procnum
= atoi(process
);
446 static void walk_processes(struct flow_entry
*n
)
452 /* n->inode must be set */
454 memset(n
->cmdline
, 0, sizeof(n
->cmdline
));
458 dir
= opendir("/proc");
460 panic("Cannot open /proc!\n");
462 while ((ent
= readdir(dir
))) {
463 if (strspn(ent
->d_name
, "0123456789") == strlen(ent
->d_name
)) {
464 ret
= walk_process(ent
->d_name
, n
);
473 static int get_port_inode(uint16_t port
, int proto
, int is_ip6
)
476 char path
[128], buff
[1024];
479 memset(path
, 0, sizeof(path
));
480 snprintf(path
, sizeof(path
), "/proc/net/%s%s",
481 l4proto2str
[proto
], is_ip6
? "6" : "");
483 proc
= fopen(path
, "r");
487 memset(buff
, 0, sizeof(buff
));
489 while (fgets(buff
, sizeof(buff
), proc
) != NULL
) {
491 unsigned int lport
= 0;
493 buff
[sizeof(buff
) - 1] = 0;
494 if (sscanf(buff
, "%*u: %*X:%X %*X:%*X %*X %*X:%*X %*X:%*X "
495 "%*X %*u %*u %u", &lport
, &inode
) == 2) {
496 if ((uint16_t) lport
== port
) {
502 memset(buff
, 0, sizeof(buff
));
509 #define CP_NFCT(elem, attr, x) \
510 do { n->elem = nfct_get_attr_u##x(ct,(attr)); } while (0)
511 #define CP_NFCT_BUFF(elem, attr) do { \
512 const uint8_t *buff = nfct_get_attr(ct,(attr)); \
514 memcpy(n->elem, buff, sizeof(n->elem)); \
517 static void flow_entry_from_ct(struct flow_entry
*n
, struct nf_conntrack
*ct
)
519 CP_NFCT(l3_proto
, ATTR_ORIG_L3PROTO
, 8);
520 CP_NFCT(l4_proto
, ATTR_ORIG_L4PROTO
, 8);
522 CP_NFCT(ip4_src_addr
, ATTR_ORIG_IPV4_SRC
, 32);
523 CP_NFCT(ip4_dst_addr
, ATTR_ORIG_IPV4_DST
, 32);
525 CP_NFCT(port_src
, ATTR_ORIG_PORT_SRC
, 16);
526 CP_NFCT(port_dst
, ATTR_ORIG_PORT_DST
, 16);
528 CP_NFCT(status
, ATTR_STATUS
, 32);
530 CP_NFCT(tcp_state
, ATTR_TCP_STATE
, 8);
531 CP_NFCT(tcp_flags
, ATTR_TCP_FLAGS_ORIG
, 8);
532 CP_NFCT(sctp_state
, ATTR_SCTP_STATE
, 8);
533 CP_NFCT(dccp_state
, ATTR_DCCP_STATE
, 8);
535 CP_NFCT(counter_pkts
, ATTR_ORIG_COUNTER_PACKETS
, 64);
536 CP_NFCT(counter_bytes
, ATTR_ORIG_COUNTER_BYTES
, 64);
538 CP_NFCT(timestamp_start
, ATTR_TIMESTAMP_START
, 64);
539 CP_NFCT(timestamp_stop
, ATTR_TIMESTAMP_STOP
, 64);
541 CP_NFCT(flow_id
, ATTR_ID
, 32);
542 CP_NFCT(use
, ATTR_USE
, 32);
544 CP_NFCT_BUFF(ip6_src_addr
, ATTR_ORIG_IPV6_SRC
);
545 CP_NFCT_BUFF(ip6_dst_addr
, ATTR_ORIG_IPV6_DST
);
547 n
->port_src
= ntohs(n
->port_src
);
548 n
->port_dst
= ntohs(n
->port_dst
);
550 n
->ip4_src_addr
= ntohl(n
->ip4_src_addr
);
551 n
->ip4_dst_addr
= ntohl(n
->ip4_dst_addr
);
554 enum flow_entry_direction
{
559 static inline int flow_entry_get_extended_is_dns(struct flow_entry
*n
)
561 /* We don't want to analyze / display DNS itself, since we
562 * use it to resolve reverse dns.
564 return n
->port_src
== 53 || n
->port_dst
== 53;
567 #define SELFLD(dir,src_member,dst_member) \
568 (((dir) == flow_entry_src) ? n->src_member : n->dst_member)
570 static struct sockaddr_in
*
571 flow_entry_get_sain4_obj(struct flow_entry
*n
, enum flow_entry_direction dir
,
572 struct sockaddr_in
*sa
)
574 memset(sa
, 0, sizeof(*sa
));
575 sa
->sin_family
= PF_INET
;
576 sa
->sin_addr
.s_addr
= htonl(SELFLD(dir
, ip4_src_addr
, ip4_dst_addr
));
581 static struct sockaddr_in6
*
582 flow_entry_get_sain6_obj(struct flow_entry
*n
, enum flow_entry_direction dir
,
583 struct sockaddr_in6
*sa
)
585 memset(sa
, 0, sizeof(*sa
));
586 sa
->sin6_family
= PF_INET6
;
588 memcpy(&sa
->sin6_addr
, SELFLD(dir
, ip6_src_addr
, ip6_dst_addr
),
589 sizeof(SELFLD(dir
, ip6_src_addr
, ip6_dst_addr
)));
595 flow_entry_geo_city_lookup_generic(struct flow_entry
*n
,
596 enum flow_entry_direction dir
)
598 struct sockaddr_in sa4
;
599 struct sockaddr_in6 sa6
;
600 const char *city
= NULL
;
602 switch (n
->l3_proto
) {
607 flow_entry_get_sain4_obj(n
, dir
, &sa4
);
608 city
= geoip4_city_name(sa4
);
612 flow_entry_get_sain6_obj(n
, dir
, &sa6
);
613 city
= geoip6_city_name(sa6
);
617 bug_on(sizeof(n
->city_src
) != sizeof(n
->city_dst
));
620 memcpy(SELFLD(dir
, city_src
, city_dst
), city
,
621 min(sizeof(n
->city_src
), strlen(city
)));
623 memset(SELFLD(dir
, city_src
, city_dst
), 0,
624 sizeof(n
->city_src
));
629 flow_entry_geo_country_lookup_generic(struct flow_entry
*n
,
630 enum flow_entry_direction dir
)
632 struct sockaddr_in sa4
;
633 struct sockaddr_in6 sa6
;
634 const char *country
= NULL
;
636 switch (n
->l3_proto
) {
641 flow_entry_get_sain4_obj(n
, dir
, &sa4
);
642 country
= geoip4_country_name(sa4
);
646 flow_entry_get_sain6_obj(n
, dir
, &sa6
);
647 country
= geoip6_country_name(sa6
);
651 bug_on(sizeof(n
->country_src
) != sizeof(n
->country_dst
));
654 memcpy(SELFLD(dir
, country_src
, country_dst
), country
,
655 min(sizeof(n
->country_src
), strlen(country
)));
657 memset(SELFLD(dir
, country_src
, country_dst
), 0,
658 sizeof(n
->country_src
));
662 static void flow_entry_get_extended_geo(struct flow_entry
*n
,
663 enum flow_entry_direction dir
)
665 flow_entry_geo_city_lookup_generic(n
, dir
);
666 flow_entry_geo_country_lookup_generic(n
, dir
);
669 static void flow_entry_get_extended_revdns(struct flow_entry
*n
,
670 enum flow_entry_direction dir
)
673 struct sockaddr_in sa4
;
674 struct sockaddr_in6 sa6
;
676 struct hostent
*hent
;
678 switch (n
->l3_proto
) {
683 flow_entry_get_sain4_obj(n
, dir
, &sa4
);
684 sa
= (struct sockaddr
*) &sa4
;
685 sa_len
= sizeof(sa4
);
686 hent
= gethostbyaddr(&sa4
.sin_addr
, sizeof(sa4
.sin_addr
), AF_INET
);
690 flow_entry_get_sain6_obj(n
, dir
, &sa6
);
691 sa
= (struct sockaddr
*) &sa6
;
692 sa_len
= sizeof(sa6
);
693 hent
= gethostbyaddr(&sa6
.sin6_addr
, sizeof(sa6
.sin6_addr
), AF_INET6
);
697 bug_on(sizeof(n
->rev_dns_src
) != sizeof(n
->rev_dns_dst
));
698 getnameinfo(sa
, sa_len
, SELFLD(dir
, rev_dns_src
, rev_dns_dst
),
699 sizeof(n
->rev_dns_src
), NULL
, 0, NI_NUMERICHOST
);
702 memset(n
->rev_dns_dst
, 0, sizeof(n
->rev_dns_dst
));
703 memcpy(SELFLD(dir
, rev_dns_src
, rev_dns_dst
),
704 hent
->h_name
, min(sizeof(n
->rev_dns_src
),
705 strlen(hent
->h_name
)));
709 static void flow_entry_get_extended(struct flow_entry
*n
)
711 if (n
->flow_id
== 0 || flow_entry_get_extended_is_dns(n
))
714 flow_entry_get_extended_revdns(n
, flow_entry_src
);
715 flow_entry_get_extended_geo(n
, flow_entry_src
);
717 flow_entry_get_extended_revdns(n
, flow_entry_dst
);
718 flow_entry_get_extended_geo(n
, flow_entry_dst
);
720 /* Lookup application */
721 n
->inode
= get_port_inode(n
->port_src
, n
->l4_proto
,
722 n
->l3_proto
== AF_INET6
);
727 static uint16_t presenter_get_port(uint16_t src
, uint16_t dst
, int tcp
)
729 if (src
< dst
&& src
< 1024) {
731 } else if (dst
< src
&& dst
< 1024) {
734 const char *tmp1
, *tmp2
;
736 tmp1
= lookup_port_tcp(src
);
737 tmp2
= lookup_port_tcp(dst
);
739 tmp1
= lookup_port_udp(src
);
740 tmp2
= lookup_port_udp(dst
);
744 } else if (!tmp1
&& tmp2
) {
755 static void presenter_screen_init(WINDOW
**screen
)
757 (*screen
) = initscr();
760 keypad(stdscr
, TRUE
);
761 nodelay(*screen
, TRUE
);
766 static void presenter_screen_do_line(WINDOW
*screen
, struct flow_entry
*n
,
769 char tmp
[128], *pname
= NULL
;
772 mvwprintw(screen
, *line
, 2, "");
774 /* PID, application name */
775 if (n
->procnum
> 0) {
776 slprintf(tmp
, sizeof(tmp
), "%s(%u)", basename(n
->cmdline
),
780 attron(COLOR_PAIR(3));
782 attroff(COLOR_PAIR(3));
786 /* L3 protocol, L4 protocol, states */
787 printw("%s:%s", l3proto2str
[n
->l3_proto
], l4proto2str
[n
->l4_proto
]);
789 attron(COLOR_PAIR(3));
790 switch (n
->l4_proto
) {
792 printw("%s", tcp_state2str
[n
->tcp_state
]);
795 printw("%s", sctp_state2str
[n
->sctp_state
]);
798 printw("%s", dccp_state2str
[n
->dccp_state
]);
801 case IPPROTO_UDPLITE
:
807 attroff(COLOR_PAIR(3));
810 /* Guess application port */
811 switch (n
->l4_proto
) {
813 port
= presenter_get_port(n
->port_src
, n
->port_dst
, 1);
814 pname
= lookup_port_tcp(port
);
817 case IPPROTO_UDPLITE
:
818 port
= presenter_get_port(n
->port_src
, n
->port_dst
, 0);
819 pname
= lookup_port_udp(port
);
824 printw(":%s", pname
);
829 /* Number packets, bytes */
830 if (n
->counter_pkts
> 0 && n
->counter_bytes
> 0)
831 printw(" (%llu pkts, %llu bytes) ->",
832 n
->counter_pkts
, n
->counter_bytes
);
834 /* Show source information: reverse DNS, port, country, city */
836 attron(COLOR_PAIR(1));
837 mvwprintw(screen
, ++(*line
), 8, "src: %s", n
->rev_dns_src
);
838 attroff(COLOR_PAIR(1));
840 printw(":%u", n
->port_src
);
842 if (n
->country_src
[0]) {
845 attron(COLOR_PAIR(4));
846 printw("%s", n
->country_src
);
847 attroff(COLOR_PAIR(4));
850 printw(", %s", n
->city_src
);
858 /* Show dest information: reverse DNS, port, country, city */
859 attron(COLOR_PAIR(2));
860 mvwprintw(screen
, ++(*line
), 8, "dst: %s", n
->rev_dns_dst
);
861 attroff(COLOR_PAIR(2));
863 printw(":%u", n
->port_dst
);
865 if (n
->country_dst
[0]) {
868 attron(COLOR_PAIR(4));
869 printw("%s", n
->country_dst
);
870 attroff(COLOR_PAIR(4));
873 printw(", %s", n
->city_dst
);
879 static inline int presenter_flow_wrong_state(struct flow_entry
*n
, int state
)
883 switch (n
->l4_proto
) {
885 if (n
->tcp_state
== state
)
889 if (n
->sctp_state
== state
)
893 if (n
->dccp_state
== state
)
897 case IPPROTO_UDPLITE
:
907 static void presenter_screen_update(WINDOW
*screen
, struct flow_list
*fl
,
911 unsigned int line
= 3;
912 struct flow_entry
*n
;
913 uint8_t protocols
[] = {
922 size_t protocol_state_size
[] = {
923 [IPPROTO_TCP
] = array_size(tcp_states
),
924 [IPPROTO_DCCP
] = array_size(dccp_states
),
925 [IPPROTO_SCTP
] = array_size(sctp_states
),
927 [IPPROTO_UDPLITE
] = 1,
929 [IPPROTO_ICMPV6
] = 1,
934 maxy
= getmaxy(screen
);
938 init_pair(1, COLOR_RED
, COLOR_BLACK
);
939 init_pair(2, COLOR_BLUE
, COLOR_BLACK
);
940 init_pair(3, COLOR_YELLOW
, COLOR_BLACK
);
941 init_pair(4, COLOR_GREEN
, COLOR_BLACK
);
946 mvwprintw(screen
, 1, 2, "Kernel netfilter flows for %s%s%s%s%s%s"
947 "[+%d]", what
& INCLUDE_TCP
? "TCP, " : "" ,
948 what
& INCLUDE_UDP
? "UDP, " : "",
949 what
& INCLUDE_SCTP
? "SCTP, " : "",
950 what
& INCLUDE_DCCP
? "DCCP, " : "",
951 what
& INCLUDE_ICMP
&& what
& INCLUDE_IPV4
? "ICMP, " : "",
952 what
& INCLUDE_ICMP
&& what
& INCLUDE_IPV6
? "ICMP6, " : "",
957 if (rcu_dereference(fl
->head
) == NULL
)
958 mvwprintw(screen
, line
, 2, "(No active sessions! "
959 "Is netfilter running?)");
961 for (i
= 0; i
< array_size(protocols
); i
++) {
962 for (j
= 0; j
< protocol_state_size
[protocols
[i
]]; j
++) {
963 n
= rcu_dereference(fl
->head
);
964 while (n
&& maxy
> 0) {
967 if (n
->l4_proto
!= protocols
[i
])
969 if (presenter_flow_wrong_state(n
, j
))
971 if (presenter_get_port(n
->port_src
,
972 n
->port_dst
, 0) == 53)
975 n
= rcu_dereference(n
->next
);
978 if (skip_lines
> 0) {
979 n
= rcu_dereference(n
->next
);
984 presenter_screen_do_line(screen
, n
, &line
);
987 maxy
-= (2 + 1 * show_src
);
988 n
= rcu_dereference(n
->next
);
999 static inline void presenter_screen_end(void)
1004 static void presenter(void)
1007 WINDOW
*screen
= NULL
;
1009 dissector_init_ethernet(0);
1010 presenter_screen_init(&screen
);
1012 rcu_register_thread();
1029 if (skip_lines
> SCROLL_MAX
)
1030 skip_lines
= SCROLL_MAX
;
1037 presenter_screen_update(screen
, &flow_list
, skip_lines
);
1040 rcu_unregister_thread();
1042 presenter_screen_end();
1043 dissector_cleanup_ethernet();
1046 static int collector_cb(enum nf_conntrack_msg_type type
,
1047 struct nf_conntrack
*ct
, void *data
)
1050 return NFCT_CB_STOP
;
1053 spinlock_lock(&flow_list
.lock
);
1057 flow_list_new_entry(&flow_list
, ct
);
1060 flow_list_update_entry(&flow_list
, ct
);
1062 case NFCT_T_DESTROY
:
1063 flow_list_destroy_entry(&flow_list
, ct
);
1069 spinlock_unlock(&flow_list
.lock
);
1071 return NFCT_CB_CONTINUE
;
1074 static inline void collector_flush(struct nfct_handle
*handle
, uint8_t family
)
1076 nfct_query(handle
, NFCT_Q_FLUSH
, &family
);
1079 static void *collector(void *null
)
1082 struct nfct_handle
*handle
;
1083 struct nfct_filter
*filter
;
1085 handle
= nfct_open(CONNTRACK
, NF_NETLINK_CONNTRACK_NEW
|
1086 NF_NETLINK_CONNTRACK_UPDATE
|
1087 NF_NETLINK_CONNTRACK_DESTROY
);
1089 panic("Cannot create a nfct handle!\n");
1091 collector_flush(handle
, AF_INET
);
1092 collector_flush(handle
, AF_INET6
);
1094 filter
= nfct_filter_create();
1096 panic("Cannot create a nfct filter!\n");
1098 ret
= nfct_filter_attach(nfct_fd(handle
), filter
);
1100 panic("Cannot attach filter to handle!\n");
1102 if (what
& INCLUDE_UDP
) {
1103 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_UDP
);
1104 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_UDPLITE
);
1106 if (what
& INCLUDE_TCP
)
1107 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_TCP
);
1108 if (what
& INCLUDE_DCCP
)
1109 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_DCCP
);
1110 if (what
& INCLUDE_SCTP
)
1111 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_SCTP
);
1112 if (what
& INCLUDE_ICMP
&& what
& INCLUDE_IPV4
)
1113 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_ICMP
);
1114 if (what
& INCLUDE_ICMP
&& what
& INCLUDE_IPV6
)
1115 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_ICMPV6
);
1116 if (what
& INCLUDE_IPV4
) {
1117 nfct_filter_set_logic(filter
, NFCT_FILTER_SRC_IPV4
, NFCT_FILTER_LOGIC_NEGATIVE
);
1118 nfct_filter_add_attr(filter
, NFCT_FILTER_SRC_IPV4
, &filter_ipv4
);
1120 if (what
& INCLUDE_IPV6
) {
1121 nfct_filter_set_logic(filter
, NFCT_FILTER_SRC_IPV6
, NFCT_FILTER_LOGIC_NEGATIVE
);
1122 nfct_filter_add_attr(filter
, NFCT_FILTER_SRC_IPV6
, &filter_ipv6
);
1125 ret
= nfct_filter_attach(nfct_fd(handle
), filter
);
1127 panic("Cannot attach filter to handle!\n");
1129 nfct_callback_register(handle
, NFCT_T_ALL
, collector_cb
, NULL
);
1130 nfct_filter_destroy(filter
);
1131 flow_list_init(&flow_list
);
1133 rcu_register_thread();
1135 while (!sigint
&& ret
>= 0)
1136 ret
= nfct_catch(handle
);
1138 rcu_unregister_thread();
1140 flow_list_destroy(&flow_list
);
1146 int main(int argc
, char **argv
)
1149 int ret
, c
, opt_index
, what_cmd
= 0;
1154 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1155 &opt_index
)) != EOF
) {
1158 what_cmd
|= INCLUDE_IPV4
;
1161 what_cmd
|= INCLUDE_IPV6
;
1164 what_cmd
|= INCLUDE_TCP
;
1167 what_cmd
|= INCLUDE_UDP
;
1170 what_cmd
|= INCLUDE_DCCP
;
1173 what_cmd
|= INCLUDE_ICMP
;
1176 what_cmd
|= INCLUDE_SCTP
;
1201 register_signal(SIGINT
, signal_handler
);
1202 register_signal(SIGHUP
, signal_handler
);
1206 ret
= pthread_create(&tid
, NULL
, collector
, NULL
);
1208 panic("Cannot create phthread!\n");