forked from netsniff-ng/netsniff-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flowtop.c
1967 lines (1608 loc) · 47.1 KB
/
flowtop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* netsniff-ng - the packet sniffing beast
* Copyright 2011 - 2013 Daniel Borkmann.
* Copyright 2011 Emmanuel Roullit.
* Subject to the GPL, version 2.
*/
#define _LGPL_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <signal.h>
#include <getopt.h>
#include <pthread.h>
#include <signal.h>
#include <netdb.h>
#include <ctype.h>
#include <netinet/in.h>
#include <curses.h>
#include <sys/time.h>
#include <sys/fsuid.h>
#include <libgen.h>
#include <inttypes.h>
#include <poll.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <urcu.h>
#include <urcu/list.h>
#include <urcu/rculist.h>
#include "ui.h"
#include "die.h"
#include "xmalloc.h"
#include "conntrack.h"
#include "config.h"
#include "str.h"
#include "sig.h"
#include "lookup.h"
#include "geoip.h"
#include "built_in.h"
#include "pkt_buff.h"
#include "screen.h"
#include "proc.h"
#include "sysctl.h"
#ifndef NSEC_PER_SEC
#define NSEC_PER_SEC 1000000000L
#endif
#ifndef USEC_PER_SEC
#define USEC_PER_SEC 1000000L
#endif
struct flow_stat {
uint64_t pkts_src, bytes_src;
uint64_t pkts_dst, bytes_dst;
double rate_bytes_src;
double rate_bytes_dst;
double rate_pkts_src;
double rate_pkts_dst;
};
struct proc_entry {
struct cds_list_head entry;
struct cds_list_head flows;
struct rcu_head rcu;
struct timeval last_update;
struct flow_stat stat;
unsigned int pid;
char name[256];
int flows_count;
};
struct flow_entry {
struct cds_list_head proc_head;
struct cds_list_head entry;
struct rcu_head rcu;
uint32_t flow_id, use, status;
uint8_t l3_proto, l4_proto;
uint32_t ip4_src_addr, ip4_dst_addr;
uint32_t ip6_src_addr[4], ip6_dst_addr[4];
uint16_t port_src, port_dst;
uint8_t tcp_state, tcp_flags, sctp_state, dccp_state;
uint64_t timestamp_start, timestamp_stop;
char country_src[128], country_dst[128];
char country_code_src[4], country_code_dst[4];
char city_src[128], city_dst[128];
char rev_dns_src[256], rev_dns_dst[256];
struct proc_entry *proc;
int inode;
bool is_visible;
struct nf_conntrack *ct;
struct timeval last_update;
struct flow_stat stat;
};
struct flow_list {
struct cds_list_head head;
};
struct proc_list {
struct cds_list_head head;
};
enum flow_direction {
FLOW_DIR_SRC,
FLOW_DIR_DST,
};
#ifndef ATTR_TIMESTAMP_START
# define ATTR_TIMESTAMP_START 63
#endif
#ifndef ATTR_TIMESTAMP_STOP
# define ATTR_TIMESTAMP_STOP 64
#endif
#define INCLUDE_IPV4 (1 << 0)
#define INCLUDE_IPV6 (1 << 1)
#define INCLUDE_UDP (1 << 2)
#define INCLUDE_TCP (1 << 3)
#define INCLUDE_DCCP (1 << 4)
#define INCLUDE_ICMP (1 << 5)
#define INCLUDE_SCTP (1 << 6)
#define TOGGLE_FLAG(what, flag) \
do { \
if (what & flag) \
what &= ~flag; \
else \
what |= flag; \
} while (0)
struct sysctl_params_ctx {
int nfct_acct;
int nfct_tstamp;
};
enum rate_units {
RATE_BITS,
RATE_BYTES
};
static volatile bool do_reload_flows;
static volatile bool is_flow_collecting;
static volatile sig_atomic_t sigint = 0;
static int what = INCLUDE_IPV4 | INCLUDE_IPV6 | INCLUDE_TCP;
static struct proc_list proc_list;
static struct flow_list flow_list;
static struct sysctl_params_ctx sysctl = { -1, -1 };
static unsigned int cols, rows;
static WINDOW *screen;
static unsigned int interval = 1;
static bool show_src = false;
static bool resolve_dns = true;
static bool resolve_geoip = true;
static enum rate_units rate_type = RATE_BYTES;
static bool show_active_only = false;
enum tbl_flow_col {
TBL_FLOW_PROCESS,
TBL_FLOW_PID,
TBL_FLOW_PROTO,
TBL_FLOW_STATE,
TBL_FLOW_TIME,
TBL_FLOW_ADDRESS,
TBL_FLOW_PORT,
TBL_FLOW_GEO,
TBL_FLOW_BYTES,
TBL_FLOW_RATE,
};
enum tbl_proc_col {
TBL_PROC_NAME,
TBL_PROC_PID,
TBL_PROC_FLOWS,
TBL_PROC_BYTES_SRC,
TBL_PROC_RATE_SRC,
TBL_PROC_BYTES_DST,
TBL_PROC_RATE_DST,
};
static struct ui_table flows_tbl;
static struct ui_table procs_tbl;
static struct ui_table *curr_tbl;
enum tab_entry {
TAB_FLOWS,
TAB_PROCS,
};
#define list_first_or_next(__ptr, __head, __entry) \
({ \
struct cds_list_head *h; \
if (!__ptr) \
h = rcu_dereference((__head)->next); \
else if (rcu_dereference(__ptr->__entry.next) == (__head)) \
return NULL; \
else \
h = rcu_dereference(__ptr->__entry.next); \
cds_list_entry(h, __typeof(* (__ptr)), __entry); \
})
static const char *short_options = "vhTUsDIS46ut:nGb";
static const struct option long_options[] = {
{"ipv4", no_argument, NULL, '4'},
{"ipv6", no_argument, NULL, '6'},
{"tcp", no_argument, NULL, 'T'},
{"udp", no_argument, NULL, 'U'},
{"dccp", no_argument, NULL, 'D'},
{"icmp", no_argument, NULL, 'I'},
{"sctp", no_argument, NULL, 'S'},
{"no-dns", no_argument, NULL, 'n'},
{"no-geoip", no_argument, NULL, 'G'},
{"show-src", no_argument, NULL, 's'},
{"bits", no_argument, NULL, 'b'},
{"update", no_argument, NULL, 'u'},
{"interval", required_argument, NULL, 't'},
{"version", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
static const char *copyright = "Please report bugs to <[email protected]>\n"
"Copyright (C) 2011-2013 Daniel Borkmann <[email protected]>\n"
"Copyright (C) 2011-2012 Emmanuel Roullit <[email protected]>\n"
"Swiss federal institute of technology (ETH Zurich)\n"
"License: GNU GPL version 2.0\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.";
static const char *const l4proto2str[IPPROTO_MAX] = {
[IPPROTO_TCP] = "tcp",
[IPPROTO_UDP] = "udp",
[IPPROTO_UDPLITE] = "udplite",
[IPPROTO_ICMP] = "icmp",
[IPPROTO_ICMPV6] = "icmpv6",
[IPPROTO_SCTP] = "sctp",
[IPPROTO_GRE] = "gre",
[IPPROTO_DCCP] = "dccp",
[IPPROTO_IGMP] = "igmp",
[IPPROTO_IPIP] = "ipip",
[IPPROTO_EGP] = "egp",
[IPPROTO_PUP] = "pup",
[IPPROTO_IDP] = "idp",
[IPPROTO_RSVP] = "rsvp",
[IPPROTO_IPV6] = "ip6tun",
[IPPROTO_ESP] = "esp",
[IPPROTO_AH] = "ah",
[IPPROTO_PIM] = "pim",
[IPPROTO_COMP] = "comp",
};
static const char *const tcp_state2str[TCP_CONNTRACK_MAX] = {
[TCP_CONNTRACK_NONE] = "NONE",
[TCP_CONNTRACK_SYN_SENT] = "SYN-SENT",
[TCP_CONNTRACK_SYN_RECV] = "SYN-RECV",
[TCP_CONNTRACK_ESTABLISHED] = "ESTABLISHED",
[TCP_CONNTRACK_FIN_WAIT] = "FIN-WAIT",
[TCP_CONNTRACK_CLOSE_WAIT] = "CLOSE-WAIT",
[TCP_CONNTRACK_LAST_ACK] = "LAST-ACK",
[TCP_CONNTRACK_TIME_WAIT] = "TIME-WAIT",
[TCP_CONNTRACK_CLOSE] = "CLOSE",
[TCP_CONNTRACK_SYN_SENT2] = "SYN-SENT2",
};
static const char *const dccp_state2str[DCCP_CONNTRACK_MAX] = {
[DCCP_CONNTRACK_NONE] = "NONE",
[DCCP_CONNTRACK_REQUEST] = "REQUEST",
[DCCP_CONNTRACK_RESPOND] = "RESPOND",
[DCCP_CONNTRACK_PARTOPEN] = "PARTOPEN",
[DCCP_CONNTRACK_OPEN] = "OPEN",
[DCCP_CONNTRACK_CLOSEREQ] = "CLOSE-REQ",
[DCCP_CONNTRACK_CLOSING] = "CLOSING",
[DCCP_CONNTRACK_TIMEWAIT] = "TIME-WAIT",
[DCCP_CONNTRACK_IGNORE] = "IGNORE",
[DCCP_CONNTRACK_INVALID] = "INVALID",
};
static const char *const sctp_state2str[SCTP_CONNTRACK_MAX] = {
[SCTP_CONNTRACK_NONE] = "NONE",
[SCTP_CONNTRACK_CLOSED] = "CLOSED",
[SCTP_CONNTRACK_COOKIE_WAIT] = "COOKIE-WAIT",
[SCTP_CONNTRACK_COOKIE_ECHOED] = "COOKIE-ECHO",
[SCTP_CONNTRACK_ESTABLISHED] = "ESTABLISHED",
[SCTP_CONNTRACK_SHUTDOWN_SENT] = "SHUTD-SENT",
[SCTP_CONNTRACK_SHUTDOWN_RECD] = "SHUTD-RCVD",
[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = "SHUTD-ACK",
};
static const struct nfct_filter_ipv4 filter_ipv4 = {
.addr = __constant_htonl(INADDR_LOOPBACK),
.mask = 0xffffffff,
};
static const struct nfct_filter_ipv6 filter_ipv6 = {
.addr = { 0x0, 0x0, 0x0, 0x1 },
.mask = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff },
};
static int64_t time_after_us(struct timeval *tv)
{
struct timeval now;
bug_on(gettimeofday(&now, NULL));
now.tv_sec -= tv->tv_sec;
now.tv_usec -= tv->tv_usec;
return now.tv_sec * USEC_PER_SEC + now.tv_usec;
}
static void signal_handler(int number)
{
switch (number) {
case SIGINT:
case SIGQUIT:
case SIGTERM:
sigint = 1;
break;
case SIGHUP:
default:
break;
}
}
static void flow_entry_from_ct(struct flow_entry *n, const struct nf_conntrack *ct);
static void flow_entry_get_extended(struct flow_entry *n);
static void help(void)
{
printf("flowtop %s, top-like netfilter TCP/UDP/SCTP/.. flow tracking\n",
VERSION_STRING);
puts("http://www.netsniff-ng.org\n\n"
"Usage: flowtop [options]\n"
"Options:\n"
" -4|--ipv4 Show only IPv4 flows (default)\n"
" -6|--ipv6 Show only IPv6 flows (default)\n"
" -T|--tcp Show only TCP flows (default)\n"
" -U|--udp Show only UDP flows\n"
" -D|--dccp Show only DCCP flows\n"
" -I|--icmp Show only ICMP/ICMPv6 flows\n"
" -S|--sctp Show only SCTP flows\n"
" -n|--no-dns Don't perform hostname lookup\n"
" -G|--no-geoip Don't perform GeoIP lookup\n"
" -s|--show-src Also show source, not only dest\n"
" -b|--bits Show rates in bits/s instead of bytes/s\n"
" -u|--update Update GeoIP databases\n"
" -t|--interval <time> Refresh time in seconds (default 1s)\n"
" -v|--version Print version and exit\n"
" -h|--help Print this help and exit\n\n"
"Examples:\n"
" flowtop\n"
" flowtop -46UTDISs\n\n"
"Note:\n"
" If netfilter is not running, you can activate it with e.g.:\n"
" iptables -A INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT\n"
" iptables -A OUTPUT -p tcp -m state --state NEW,ESTABLISHED -j ACCEPT\n");
puts(copyright);
die();
}
static void version(void)
{
printf("flowtop %s, Git id: %s\n", VERSION_LONG, GITVERSION);
puts("top-like netfilter TCP/UDP/SCTP/.. flow tracking\n"
"http://www.netsniff-ng.org\n");
puts(copyright);
die();
}
static void flow_entry_update_time(struct flow_entry *n)
{
bug_on(gettimeofday(&n->last_update, NULL));
}
#define CALC_RATE(fld) do { \
n->stat.rate_##fld = (((fld) > n->stat.fld) ? \
(((fld) - n->stat.fld) / sec) : 0); \
} while (0)
static void flow_entry_calc_rate(struct flow_entry *n, const struct nf_conntrack *ct)
{
uint64_t bytes_src = nfct_get_attr_u64(ct, ATTR_ORIG_COUNTER_BYTES);
uint64_t bytes_dst = nfct_get_attr_u64(ct, ATTR_REPL_COUNTER_BYTES);
uint64_t pkts_src = nfct_get_attr_u64(ct, ATTR_ORIG_COUNTER_PACKETS);
uint64_t pkts_dst = nfct_get_attr_u64(ct, ATTR_REPL_COUNTER_PACKETS);
double sec = (double)time_after_us(&n->last_update) / USEC_PER_SEC;
if (sec < 1)
return;
CALC_RATE(bytes_src);
CALC_RATE(bytes_dst);
CALC_RATE(pkts_src);
CALC_RATE(pkts_dst);
}
static inline struct flow_entry *flow_entry_xalloc(void)
{
return xzmalloc(sizeof(struct flow_entry));
}
static inline void flow_entry_xfree(struct flow_entry *n)
{
if (n->ct)
nfct_destroy(n->ct);
xfree(n);
}
static void flow_entry_xfree_rcu(struct rcu_head *head)
{
struct flow_entry *n = container_of(head, struct flow_entry, rcu);
flow_entry_xfree(n);
}
static inline void flow_list_init(struct flow_list *fl)
{
CDS_INIT_LIST_HEAD(&fl->head);
}
static inline bool nfct_is_dns(const struct nf_conntrack *ct)
{
uint16_t port_src = nfct_get_attr_u16(ct, ATTR_ORIG_PORT_SRC);
uint16_t port_dst = nfct_get_attr_u16(ct, ATTR_ORIG_PORT_DST);
return ntohs(port_src) == 53 || ntohs(port_dst) == 53;
}
static int flow_list_new_entry(struct flow_list *fl, struct nf_conntrack *ct)
{
struct flow_entry *n;
/* We don't want to analyze / display DNS itself, since we
* use it to resolve reverse dns.
*/
if (nfct_is_dns(ct))
return NFCT_CB_CONTINUE;
n = flow_entry_xalloc();
n->ct = ct;
flow_entry_update_time(n);
flow_entry_from_ct(n, ct);
flow_entry_get_extended(n);
cds_list_add_rcu(&n->entry, &fl->head);
n->is_visible = true;
return NFCT_CB_STOLEN;
}
static struct flow_entry *flow_list_find_id(struct flow_list *fl, uint32_t id)
{
struct flow_entry *n;
cds_list_for_each_entry_rcu(n, &fl->head, entry) {
if (n->flow_id == id)
return n;
}
return NULL;
}
static void __flow_list_del_entry(struct flow_list *fl, struct flow_entry *n)
{
if (n->proc) {
cds_list_del_rcu(&n->proc_head);
n->proc->flows_count--;
}
cds_list_del_rcu(&n->entry);
call_rcu(&n->rcu, flow_entry_xfree_rcu);
}
static int flow_list_del_entry(struct flow_list *fl, const struct nf_conntrack *ct)
{
struct flow_entry *n;
n = flow_list_find_id(fl, nfct_get_attr_u32(ct, ATTR_ID));
if (n)
__flow_list_del_entry(fl, n);
return NFCT_CB_CONTINUE;
}
static void flow_list_destroy(struct flow_list *fl)
{
struct flow_entry *n, *tmp;
cds_list_for_each_entry_safe(n, tmp, &fl->head, entry)
__flow_list_del_entry(fl, n);
}
static void proc_list_init(struct proc_list *proc_list)
{
CDS_INIT_LIST_HEAD(&proc_list->head);
}
static struct proc_entry *proc_list_new_entry(unsigned int pid)
{
struct proc_entry *proc;
cds_list_for_each_entry(proc, &proc_list.head, entry) {
if (proc->pid && proc->pid == pid)
return proc;
}
proc = xzmalloc(sizeof(*proc));
bug_on(gettimeofday(&proc->last_update, NULL));
CDS_INIT_LIST_HEAD(&proc->flows);
proc->pid = pid;
cds_list_add_tail(&proc->entry, &proc_list.head);
return proc;
}
static void proc_entry_xfree_rcu(struct rcu_head *head)
{
struct proc_entry *p = container_of(head, struct proc_entry, rcu);
xfree(p);
}
static void proc_list_destroy(struct proc_list *pl)
{
struct proc_entry *p, *tmp;
cds_list_for_each_entry_safe(p, tmp, &pl->head, entry) {
cds_list_del_rcu(&p->entry);
call_rcu(&p->rcu, proc_entry_xfree_rcu);
}
}
static void flow_entry_find_process(struct flow_entry *n)
{
struct proc_entry *p;
char cmdline[512];
pid_t pid;
int ret;
ret = proc_find_by_inode(n->inode, cmdline, sizeof(cmdline), &pid);
if (ret <= 0)
return;
p = proc_list_new_entry(pid);
if (snprintf(p->name, sizeof(p->name), "%s", basename(cmdline)) < 0)
p->name[0] = '\0';
p->stat.pkts_src += n->stat.pkts_src;
p->stat.pkts_dst += n->stat.pkts_dst;
p->stat.bytes_src += n->stat.bytes_src;
p->stat.bytes_dst += n->stat.bytes_dst;
p->flows_count++;
cds_list_add_rcu(&n->proc_head, &p->flows);
n->proc = p;
}
static int get_port_inode(uint16_t port, int proto, bool is_ip6)
{
int ret = -ENOENT;
char path[128], buff[1024];
FILE *proc;
memset(path, 0, sizeof(path));
snprintf(path, sizeof(path), "/proc/net/%s%s",
l4proto2str[proto], is_ip6 ? "6" : "");
proc = fopen(path, "r");
if (!proc)
return -EIO;
memset(buff, 0, sizeof(buff));
while (fgets(buff, sizeof(buff), proc) != NULL) {
int inode = 0;
unsigned int lport = 0;
buff[sizeof(buff) - 1] = 0;
if (sscanf(buff, "%*u: %*X:%X %*X:%*X %*X %*X:%*X %*X:%*X "
"%*X %*u %*u %u", &lport, &inode) == 2) {
if ((uint16_t) lport == port) {
ret = inode;
break;
}
}
memset(buff, 0, sizeof(buff));
}
fclose(proc);
return ret;
}
#define CP_NFCT(elem, attr, x) \
do { n->elem = nfct_get_attr_u##x(ct,(attr)); } while (0)
#define CP_NFCT_BUFF(elem, attr) do { \
const uint8_t *buff = nfct_get_attr(ct,(attr)); \
if (buff != NULL) \
memcpy(n->elem, buff, sizeof(n->elem)); \
} while (0)
static void flow_entry_from_ct(struct flow_entry *n, const struct nf_conntrack *ct)
{
uint64_t bytes_src = nfct_get_attr_u64(ct, ATTR_ORIG_COUNTER_BYTES);
uint64_t bytes_dst = nfct_get_attr_u64(ct, ATTR_REPL_COUNTER_BYTES);
uint64_t pkts_src = nfct_get_attr_u64(ct, ATTR_ORIG_COUNTER_PACKETS);
uint64_t pkts_dst = nfct_get_attr_u64(ct, ATTR_REPL_COUNTER_PACKETS);
/* Update stats diff to the related process entry */
if (n->proc) {
n->proc->stat.pkts_src += pkts_src - n->stat.pkts_src;
n->proc->stat.pkts_dst += pkts_dst - n->stat.pkts_dst;
n->proc->stat.bytes_src += bytes_src - n->stat.bytes_src;
n->proc->stat.bytes_dst += bytes_dst - n->stat.bytes_dst;
}
CP_NFCT(l3_proto, ATTR_ORIG_L3PROTO, 8);
CP_NFCT(l4_proto, ATTR_ORIG_L4PROTO, 8);
CP_NFCT(ip4_src_addr, ATTR_ORIG_IPV4_SRC, 32);
CP_NFCT(ip4_dst_addr, ATTR_ORIG_IPV4_DST, 32);
CP_NFCT(port_src, ATTR_ORIG_PORT_SRC, 16);
CP_NFCT(port_dst, ATTR_ORIG_PORT_DST, 16);
CP_NFCT(status, ATTR_STATUS, 32);
CP_NFCT(tcp_state, ATTR_TCP_STATE, 8);
CP_NFCT(tcp_flags, ATTR_TCP_FLAGS_ORIG, 8);
CP_NFCT(sctp_state, ATTR_SCTP_STATE, 8);
CP_NFCT(dccp_state, ATTR_DCCP_STATE, 8);
CP_NFCT(stat.pkts_src, ATTR_ORIG_COUNTER_PACKETS, 64);
CP_NFCT(stat.bytes_src, ATTR_ORIG_COUNTER_BYTES, 64);
CP_NFCT(stat.pkts_dst, ATTR_REPL_COUNTER_PACKETS, 64);
CP_NFCT(stat.bytes_dst, ATTR_REPL_COUNTER_BYTES, 64);
CP_NFCT(timestamp_start, ATTR_TIMESTAMP_START, 64);
CP_NFCT(timestamp_stop, ATTR_TIMESTAMP_STOP, 64);
CP_NFCT(flow_id, ATTR_ID, 32);
CP_NFCT(use, ATTR_USE, 32);
CP_NFCT_BUFF(ip6_src_addr, ATTR_ORIG_IPV6_SRC);
CP_NFCT_BUFF(ip6_dst_addr, ATTR_ORIG_IPV6_DST);
n->port_src = ntohs(n->port_src);
n->port_dst = ntohs(n->port_dst);
n->ip4_src_addr = ntohl(n->ip4_src_addr);
n->ip4_dst_addr = ntohl(n->ip4_dst_addr);
}
#define SELFLD(dir,src_member,dst_member) \
(((dir) == FLOW_DIR_SRC) ? n->src_member : n->dst_member)
static void flow_entry_get_sain4_obj(const struct flow_entry *n,
enum flow_direction dir,
struct sockaddr_in *sa)
{
memset(sa, 0, sizeof(*sa));
sa->sin_family = PF_INET;
sa->sin_addr.s_addr = htonl(SELFLD(dir, ip4_src_addr, ip4_dst_addr));
}
static void flow_entry_get_sain6_obj(const struct flow_entry *n,
enum flow_direction dir,
struct sockaddr_in6 *sa)
{
memset(sa, 0, sizeof(*sa));
sa->sin6_family = PF_INET6;
memcpy(&sa->sin6_addr, SELFLD(dir, ip6_src_addr, ip6_dst_addr),
sizeof(sa->sin6_addr));
}
static void
flow_entry_geo_city_lookup_generic(struct flow_entry *n,
enum flow_direction dir)
{
struct sockaddr_in sa4;
struct sockaddr_in6 sa6;
char *city = NULL;
switch (n->l3_proto) {
default:
bug();
case AF_INET:
flow_entry_get_sain4_obj(n, dir, &sa4);
city = geoip4_city_name(&sa4);
break;
case AF_INET6:
flow_entry_get_sain6_obj(n, dir, &sa6);
city = geoip6_city_name(&sa6);
break;
}
build_bug_on(sizeof(n->city_src) != sizeof(n->city_dst));
if (city)
strlcpy(SELFLD(dir, city_src, city_dst), city,
sizeof(n->city_src));
else
SELFLD(dir, city_src, city_dst)[0] = '\0';
free(city);
}
static void
flow_entry_geo_country_lookup_generic(struct flow_entry *n,
enum flow_direction dir)
{
struct sockaddr_in sa4;
struct sockaddr_in6 sa6;
const char *country = NULL;
const char *country_code = NULL;
switch (n->l3_proto) {
default:
bug();
case AF_INET:
flow_entry_get_sain4_obj(n, dir, &sa4);
country = geoip4_country_name(&sa4);
country_code = geoip4_country_code3_name(&sa4);
break;
case AF_INET6:
flow_entry_get_sain6_obj(n, dir, &sa6);
country = geoip6_country_name(&sa6);
country_code = geoip6_country_code3_name(&sa6);
break;
}
build_bug_on(sizeof(n->country_src) != sizeof(n->country_dst));
if (country)
strlcpy(SELFLD(dir, country_src, country_dst), country,
sizeof(n->country_src));
else
SELFLD(dir, country_src, country_dst)[0] = '\0';
build_bug_on(sizeof(n->country_code_src) != sizeof(n->country_code_dst));
if (country_code)
strlcpy(SELFLD(dir, country_code_src, country_code_dst),
country_code, sizeof(n->country_code_src));
else
SELFLD(dir, country_code_src, country_code_dst)[0] = '\0';
}
static void flow_entry_get_extended_geo(struct flow_entry *n,
enum flow_direction dir)
{
if (resolve_geoip) {
flow_entry_geo_city_lookup_generic(n, dir);
flow_entry_geo_country_lookup_generic(n, dir);
}
}
static void flow_entry_get_extended_revdns(struct flow_entry *n,
enum flow_direction dir)
{
size_t sa_len;
struct sockaddr_in sa4;
struct sockaddr_in6 sa6;
struct sockaddr *sa;
struct hostent *hent;
build_bug_on(sizeof(n->rev_dns_src) != sizeof(n->rev_dns_dst));
switch (n->l3_proto) {
default:
bug();
case AF_INET:
flow_entry_get_sain4_obj(n, dir, &sa4);
if (!resolve_dns) {
inet_ntop(AF_INET, &sa4.sin_addr,
SELFLD(dir, rev_dns_src, rev_dns_dst),
sizeof(n->rev_dns_src));
return;
}
sa = (struct sockaddr *) &sa4;
sa_len = sizeof(sa4);
hent = gethostbyaddr(&sa4.sin_addr, sizeof(sa4.sin_addr), AF_INET);
break;
case AF_INET6:
flow_entry_get_sain6_obj(n, dir, &sa6);
if (!resolve_dns) {
inet_ntop(AF_INET6, &sa6.sin6_addr,
SELFLD(dir, rev_dns_src, rev_dns_dst),
sizeof(n->rev_dns_src));
return;
}
sa = (struct sockaddr *) &sa6;
sa_len = sizeof(sa6);
hent = gethostbyaddr(&sa6.sin6_addr, sizeof(sa6.sin6_addr), AF_INET6);
break;
}
getnameinfo(sa, sa_len, SELFLD(dir, rev_dns_src, rev_dns_dst),
sizeof(n->rev_dns_src), NULL, 0, NI_NUMERICHOST);
if (hent)
strlcpy(SELFLD(dir, rev_dns_src, rev_dns_dst), hent->h_name,
sizeof(n->rev_dns_src));
}
static void flow_entry_get_extended(struct flow_entry *n)
{
if (n->flow_id == 0)
return;
flow_entry_get_extended_revdns(n, FLOW_DIR_SRC);
flow_entry_get_extended_geo(n, FLOW_DIR_SRC);
flow_entry_get_extended_revdns(n, FLOW_DIR_DST);
flow_entry_get_extended_geo(n, FLOW_DIR_DST);
/* Lookup application */
n->inode = get_port_inode(n->port_src, n->l4_proto,
n->l3_proto == AF_INET6);
if (n->inode > 0)
flow_entry_find_process(n);
}
static char *bandw2str(double bytes, char *buf, size_t len)
{
if (bytes <= 0) {
buf[0] = '\0';
return buf;
}
if (bytes > 1000000000.)
snprintf(buf, len, "%.1fGB", bytes / 1000000000.);
else if (bytes > 1000000.)
snprintf(buf, len, "%.1fMB", bytes / 1000000.);
else if (bytes > 1000.)
snprintf(buf, len, "%.1fkB", bytes / 1000.);
else
snprintf(buf, len, "%.0f", bytes);
return buf;
}
static char *rate2str(double rate, char *buf, size_t len)
{
const char * const unit_fmt[2][4] = {
{ "%.1fGbit/s", "%.1fMbit/s", "%.1fkbit/s", "%.0fbit/s" },
{ "%.1fGB/s", "%.1fMB/s", "%.1fkB/s", "%.0fB/s" }
};
if (rate <= 0) {
buf[0] = '\0';
return buf;
}
if (rate_type == RATE_BITS)
rate *= 8;
if (rate > 1000000000.)
snprintf(buf, len, unit_fmt[rate_type][0], rate / 1000000000.);
else if (rate > 1000000.)
snprintf(buf, len, unit_fmt[rate_type][1], rate / 1000000.);
else if (rate > 1000.)
snprintf(buf, len, unit_fmt[rate_type][2], rate / 1000.);
else
snprintf(buf, len, unit_fmt[rate_type][3], rate);
return buf;
}
static char *time2str(uint64_t tstamp, char *str, size_t len)
{
time_t now;
int v, s;
time(&now);
s = now - (tstamp ? (tstamp / NSEC_PER_SEC) : now);
if (s <= 0) {
str[0] = '\0';
return str;
}
v = s / (3600 * 24);
if (v > 0) {
slprintf(str, len, "%dd", v);
return str;
}
v = s / 3600;
if (v > 0) {
slprintf(str, len, "%dh", v);
return str;
}
v = s / 60;
if (v > 0) {
slprintf(str, len, "%dm", v);
return str;
}
slprintf(str, len, "%ds", s);
return str;
}
static const char *flow_state2str(const struct flow_entry *n)
{
switch (n->l4_proto) {
case IPPROTO_TCP:
return tcp_state2str[n->tcp_state];
case IPPROTO_SCTP:
return sctp_state2str[n->sctp_state];
case IPPROTO_DCCP:
return dccp_state2str[n->dccp_state];
case IPPROTO_UDP:
case IPPROTO_UDPLITE:
case IPPROTO_ICMP:
case IPPROTO_ICMPV6:
default:
return "";
}
}
static char *flow_port2str(const struct flow_entry *n, char *str, size_t len,
enum flow_direction dir)
{
const char *tmp = NULL;
uint16_t port = 0;
port = SELFLD(dir, port_src, port_dst);
tmp = NULL;
switch (n->l4_proto) {
case IPPROTO_TCP:
tmp = lookup_port_tcp(port);
break;
case IPPROTO_UDP:
case IPPROTO_UDPLITE:
tmp = lookup_port_udp(port);
break;
}
if (!tmp && port)
slprintf(str, len, "%d", port);
else
slprintf(str, len, "%s", tmp ? tmp : "");
return str;
}
static void print_flow_peer_info(const struct flow_entry *n, enum flow_direction dir)
{
int counters_color = COLOR(YELLOW, BLACK);
int src_color = COLOR(RED, BLACK);
int dst_color = COLOR(BLUE, BLACK);
int country_color = COLOR(GREEN, BLACK);
int addr_color = dst_color;
int port_color = A_BOLD;
char tmp[128];
if (show_src && dir == FLOW_DIR_SRC) {
country_color = src_color;
counters_color = src_color;
port_color |= src_color;
addr_color = src_color;
} else if (show_src && FLOW_DIR_DST) {
country_color = dst_color;
counters_color = dst_color;
port_color |= dst_color;
addr_color = dst_color;
}
ui_table_col_color_set(&flows_tbl, TBL_FLOW_ADDRESS, addr_color);
ui_table_col_color_set(&flows_tbl, TBL_FLOW_PORT, port_color);