forked from aabc/ipt-netflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipt_NETFLOW.c
5836 lines (5288 loc) · 157 KB
/
ipt_NETFLOW.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
/* SPDX-License-Identifier: GPL-2.0-only
*
* This is NetFlow exporting module (NETFLOW target) for linux
* (c) 2008-2021 <[email protected]>
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <linux/module.h>
#include <linux/ctype.h>
#include <linux/skbuff.h>
#include <linux/proc_fs.h>
#include <linux/vmalloc.h>
#include <linux/seq_file.h>
#include <linux/random.h>
#include <linux/in6.h>
#include <linux/inet.h>
#include <linux/kernel.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <linux/icmp.h>
#include <linux/igmp.h>
#include <linux/inetdevice.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/hash.h>
#include <linux/delay.h>
#include <linux/spinlock_types.h>
#include <linux/ktime.h>
#include <linux/if_arp.h>
#include <net/icmp.h>
#include <net/ip.h>
#include <net/ipv6.h>
#include <net/tcp.h>
#include <net/route.h>
#include <net/ip6_fib.h>
#include <net/addrconf.h>
#include <net/dst.h>
#include <linux/netfilter_ipv4/ip_tables.h>
#include <linux/netfilter_bridge.h>
#ifndef ENABLE_NAT
# undef CONFIG_NF_NAT_NEEDED
#endif
#if defined(ENABLE_VLAN) || defined(ENABLE_PROMISC)
# include <linux/if_vlan.h>
#endif
#ifdef ENABLE_MAC
# include <linux/if_ether.h>
# include <linux/etherdevice.h>
#endif
#if defined(CONFIG_NF_NAT_NEEDED)
# include <linux/notifier.h>
# include <net/netfilter/nf_conntrack.h>
# include <net/netfilter/nf_conntrack_core.h>
#endif
#include <linux/version.h>
#include <asm/unaligned.h>
#ifdef HAVE_LLIST
/* llist.h is officially defined since linux 3.1,
* but centos6 have it backported on its 2.6.32.el6 */
# include <linux/llist.h>
#endif
#include "compat.h"
#include "ipt_NETFLOW.h"
#include "murmur3.h"
#ifdef CONFIG_SYSCTL
# include <linux/sysctl.h>
#endif
#ifndef CONFIG_NF_CONNTRACK_EVENTS
/* No conntrack events in the kernel imply no natevents. */
# undef CONFIG_NF_NAT_NEEDED
#endif
#if defined(CONFIG_NF_NAT_NEEDED) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
# include <net/netfilter/nf_conntrack_timestamp.h>
#endif
#if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
# ifdef ENABLE_PHYSDEV_OVER
# warning "Requested physdev override is not compiled."
# undef ENABLE_PHYSDEV_OVER
# endif
# ifdef ENABLE_PHYSDEV
# warning "Requested physdev is not compiled."
# undef ENABLE_PHYSDEV
# endif
#endif
#define IPT_NETFLOW_VERSION "2.6" /* Note that if you are using git, you
will see version in other format. */
#include "version.h"
#ifdef GITVERSION
#undef IPT_NETFLOW_VERSION
#define IPT_NETFLOW_VERSION GITVERSION
#endif
MODULE_LICENSE("GPL");
MODULE_AUTHOR("<[email protected]>");
MODULE_DESCRIPTION("iptables NETFLOW target module");
MODULE_VERSION(IPT_NETFLOW_VERSION);
MODULE_ALIAS("ip6t_NETFLOW");
static char version_string[128];
static int version_string_size;
static struct duration start_ts; /* ts of module start (ktime) */
#define DST_SIZE 256
static char destination_buf[DST_SIZE] = "127.0.0.1:2055";
static char *destination = destination_buf;
module_param(destination, charp, 0444);
MODULE_PARM_DESC(destination, "export destination ipaddress:port");
#ifdef ENABLE_SAMPLER
static char sampler_buf[128] = "";
static char *sampler = sampler_buf;
module_param(sampler, charp, 0444);
MODULE_PARM_DESC(sampler, "flow sampler parameters");
static atomic_t flow_count = ATOMIC_INIT(0); /* flow counter for deterministic sampler */
static atomic64_t flows_observed = ATOMIC_INIT(0);
static atomic64_t flows_selected = ATOMIC_INIT(0);
#define SAMPLER_INFO_INTERVAL (5*60)
static unsigned long ts_sampler_last = 0; /* template send time (jiffies) */
static struct duration sampling_ts; /* ts of sampling start (ktime) */
#define SAMPLER_SHIFT 14
#define SAMPLER_INTERVAL_M ((1 << SAMPLER_SHIFT) - 1)
enum {
SAMPLER_DETERMINISTIC = 1,
SAMPLER_RANDOM = 2,
SAMPLER_HASH = 3
};
struct sampling {
union {
u32 v32;
struct {
u8 mode;
u16 interval;
};
};
} samp;
#endif
static int inactive_timeout = 15;
module_param(inactive_timeout, int, 0644);
MODULE_PARM_DESC(inactive_timeout, "inactive flows timeout in seconds");
static int active_timeout = 30 * 60;
module_param(active_timeout, int, 0644);
MODULE_PARM_DESC(active_timeout, "active flows timeout in seconds");
static int exportcpu = -1;
module_param(exportcpu, int, 0644);
MODULE_PARM_DESC(exportcpu, "lock exporter to this cpu");
#ifdef ENABLE_PROMISC
static int promisc = 0;
module_param(promisc, int, 0444);
MODULE_PARM_DESC(promisc, "enable promisc hack (0=default, 1)");
static DEFINE_MUTEX(promisc_lock);
#endif
static int debug = 0;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "debug verbosity level");
static int sndbuf;
module_param(sndbuf, int, 0444);
MODULE_PARM_DESC(sndbuf, "udp socket SNDBUF size");
static int protocol = 5;
module_param(protocol, int, 0444);
MODULE_PARM_DESC(protocol, "netflow protocol version (5, 9, 10=IPFIX)");
static unsigned int refresh_rate = 20;
module_param(refresh_rate, uint, 0644);
MODULE_PARM_DESC(refresh_rate, "NetFlow v9/IPFIX refresh rate (packets)");
static unsigned int timeout_rate = 30;
module_param(timeout_rate, uint, 0644);
MODULE_PARM_DESC(timeout_rate, "NetFlow v9/IPFIX timeout rate (minutes)");
static int one = 1;
static unsigned int scan_min = 1;
static unsigned int scan_max = HZ / 10;
module_param(scan_min, uint, 0644);
MODULE_PARM_DESC(scan_min, "Minimal interval between export scans (jiffies)");
#ifdef SNMP_RULES
static char snmp_rules_buf[DST_SIZE] = "";
static char *snmp_rules = snmp_rules_buf;
module_param(snmp_rules, charp, 0444);
MODULE_PARM_DESC(snmp_rules, "SNMP-index conversion rules");
static unsigned char *snmp_ruleset;
static DEFINE_SPINLOCK(snmp_lock);
#endif
#ifdef CONFIG_NF_NAT_NEEDED
static int natevents = 0;
module_param(natevents, int, 0444);
MODULE_PARM_DESC(natevents, "enable NAT Events");
#endif
static int hashsize;
module_param(hashsize, int, 0444);
MODULE_PARM_DESC(hashsize, "hash table size");
static int maxflows = 2000000;
module_param(maxflows, int, 0644);
MODULE_PARM_DESC(maxflows, "maximum number of flows");
static int peakflows = 0;
static unsigned long peakflows_at; /* jfffies */
static int engine_id = 0;
module_param(engine_id, int, 0644);
MODULE_PARM_DESC(engine_id, "Observation Domain ID");
#ifdef ENABLE_AGGR
#define AGGR_SIZE 1024
static char aggregation_buf[AGGR_SIZE] = "";
static char *aggregation = aggregation_buf;
module_param(aggregation, charp, 0400);
MODULE_PARM_DESC(aggregation, "aggregation ruleset");
static LIST_HEAD(aggr_n_list);
static LIST_HEAD(aggr_p_list);
static DEFINE_RWLOCK(aggr_lock);
static void aggregation_remove(struct list_head *list);
static int add_aggregation(char *ptr);
#endif
static DEFINE_PER_CPU(struct ipt_netflow_stat, ipt_netflow_stat);
static LIST_HEAD(usock_list);
static DEFINE_MUTEX(sock_lock);
#define LOCK_COUNT (1<<8)
#define LOCK_COUNT_MASK (LOCK_COUNT-1)
struct stripe_entry {
struct list_head list; /* struct ipt_netflow, list for export */
spinlock_t lock; /* this locks both: hash table stripe & list above */
};
static struct stripe_entry htable_stripes[LOCK_COUNT];
static DEFINE_RWLOCK(htable_rwlock); /* global rwlock to protect htable[] resize */
static struct hlist_head *htable __read_mostly; /* hash table memory */
static unsigned int htable_size __read_mostly = 0; /* buckets */
/* How it's organized:
* htable_rwlock locks access to htable[hash], where
* htable[htable_size] is big/resizable hash table, which is striped into
* htable_stripes[LOCK_COUNT] smaller/static hash table, which contains
* .list - list of flows ordered by exportability (usually it's access time)
* .lock - lock to both: that .list and to htable[hash], where
* hash to the htable[] is hash_netflow(&tuple) % htable_size
* hash to the htable_stripes[] is hash & LOCK_COUNT_MASK
*/
#ifdef HAVE_LLIST
static LLIST_HEAD(export_llist); /* flows to purge */
#endif
#ifdef CONFIG_NF_NAT_NEEDED
static LIST_HEAD(nat_list); /* nat events */
static DEFINE_SPINLOCK(nat_lock);
static unsigned long nat_events_start = 0;
static unsigned long nat_events_stop = 0;
#endif
static struct kmem_cache *ipt_netflow_cachep __read_mostly; /* ipt_netflow memory */
static atomic_t ipt_netflow_count = ATOMIC_INIT(0);
static long long pdu_packets = 0, pdu_traf = 0; /* how much accounted traffic in pdu */
static unsigned int pdu_count = 0;
static unsigned int pdu_seq = 0;
static unsigned int pdu_data_records = 0; /* Data records */
static unsigned int pdu_flow_records = 0; /* Data records with flows (for stat only) */
static unsigned int pdu_tpl_records = 0;
static unsigned long pdu_ts_mod; /* ts(jiffies) of last flow */
static unsigned int pdu_needs_export = 0;
static union {
__be16 version;
struct netflow5_pdu v5;
struct netflow9_pdu v9;
struct ipfix_pdu ipfix;
} pdu;
static __u8 *pdu_data_used;
static __u8 *pdu_high_wm; /* high watermark */
static struct flowset_data *pdu_flowset = NULL; /* current data flowset */
static unsigned long wk_start; /* last start of worker (jiffies) */
static unsigned long wk_busy; /* last work busy time (jiffies) */
static unsigned int wk_count; /* how much is scanned */
static unsigned int wk_cpu;
static unsigned int wk_trylock;
static unsigned int wk_llist;
static void (*netflow_export_flow)(struct ipt_netflow *nf);
static void (*netflow_export_pdu)(void); /* called on timeout */
static void netflow_switch_version(int ver);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
static void netflow_work_fn(void *work);
static DECLARE_WORK(netflow_work, netflow_work_fn, NULL);
#else
static void netflow_work_fn(struct work_struct *work);
static DECLARE_DELAYED_WORK(netflow_work, netflow_work_fn);
#endif
static struct timer_list rate_timer;
#define TCP_SYN_ACK 0x12
#define TCP_FIN_RST 0x05
static long long sec_prate = 0, sec_brate = 0;
static long long min_prate = 0, min_brate = 0;
static long long min5_prate = 0, min5_brate = 0;
#define METRIC_DFL 100
static int metric = METRIC_DFL,
min15_metric = METRIC_DFL,
min5_metric = METRIC_DFL,
min_metric = METRIC_DFL; /* hash metrics */
static int set_hashsize(int new_size);
static void destination_removeall(void);
static int add_destinations(const char *ptr);
static int netflow_scan_and_export(int flush);
enum {
DONT_FLUSH, AND_FLUSH
};
static int template_ids = FLOWSET_DATA_FIRST;
static int tpl_gen_count = 0; /* how much templates */
static int tpl_count = 0; /* how much active templates */
#define STAT_INTERVAL (1*60)
#define SYSINFO_INTERVAL (5*60)
static unsigned long ts_stat_last = 0; /* (jiffies) */
static unsigned long ts_sysinf_last = 0; /* (jiffies) */
static unsigned long ts_ifnames_last = 0; /* (jiffies) */
static inline __be32 bits2mask(int bits) {
return (bits? 0xffffffff << (32 - bits) : 0);
}
static inline int mask2bits(__be32 mask) {
int n;
for (n = 0; mask; n++)
mask = (mask << 1) & 0xffffffff;
return n;
}
/* under that lock worker is always stopped and not rescheduled,
* and we can call worker sub-functions manually */
static DEFINE_MUTEX(worker_lock);
static int worker_delay = HZ / 10;
static inline void _schedule_scan_worker(const int pdus)
{
int cpu = exportcpu;
/* rudimentary congestion avoidance */
if (pdus > 0)
worker_delay /= pdus;
else
worker_delay *= 2;
if (worker_delay < scan_min)
worker_delay = scan_min;
else if (worker_delay > scan_max)
worker_delay = scan_max;
if (cpu >= 0) {
if (cpu < NR_CPUS &&
cpu_online(cpu)) {
schedule_delayed_work_on(cpu, &netflow_work, worker_delay);
return;
}
printk(KERN_WARNING "ipt_NETFLOW: can't schedule exporter on cpu %d. Disabling cpu lock.\n",
cpu);
exportcpu = -1;
}
schedule_delayed_work(&netflow_work, worker_delay);
}
/* This is only called soon after pause_scan_worker. */
static inline void cont_scan_worker(void)
{
_schedule_scan_worker(0);
mutex_unlock(&worker_lock);
}
static inline void _unschedule_scan_worker(void)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
cancel_rearming_delayed_work(&netflow_work);
#else
cancel_delayed_work_sync(&netflow_work);
#endif
}
/* This is only used for quick pause (in procctl). */
static inline void pause_scan_worker(void)
{
mutex_lock(&worker_lock);
_unschedule_scan_worker();
}
#ifdef ENABLE_SAMPLER
static inline unsigned char get_sampler_mode(void)
{
return samp.mode;
}
static inline unsigned short get_sampler_interval(void)
{
return samp.interval;
}
static inline const char *sampler_mode_string(void)
{
const unsigned char mode = get_sampler_mode();
return mode == SAMPLER_DETERMINISTIC? "deterministic" :
mode == SAMPLER_RANDOM? "random" : "hash";
}
/* map SAMPLER_HASH into SAMPLER_RANDOM */
static unsigned char get_sampler_mode_nf(void)
{
const unsigned char mode = get_sampler_mode();
return (mode == SAMPLER_HASH)? SAMPLER_RANDOM : mode;
}
static inline unsigned short sampler_nf_v5(void)
{
return (get_sampler_mode_nf() << SAMPLER_SHIFT) | get_sampler_interval();
}
#endif
/* return value is different from usual snprintf */
static char *snprintf_sockaddr(char *buf, size_t len, const struct sockaddr_storage *ss)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
if (ss->ss_family == AF_INET) {
const struct sockaddr_in *sin = (struct sockaddr_in *)ss;
snprintf(buf, len, "%u.%u.%u.%u:%u",
NIPQUAD(sin->sin_addr.s_addr),
ntohs(sin->sin_port));
} else if (ss->ss_family == AF_INET6) {
const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ss;
snprintf(buf, len, "[%x:%x:%x:%x:%x:%x:%x:%x]:%u",
ntohs(sin6->sin6_addr.s6_addr16[0]),
ntohs(sin6->sin6_addr.s6_addr16[1]),
ntohs(sin6->sin6_addr.s6_addr16[2]),
ntohs(sin6->sin6_addr.s6_addr16[3]),
ntohs(sin6->sin6_addr.s6_addr16[4]),
ntohs(sin6->sin6_addr.s6_addr16[5]),
ntohs(sin6->sin6_addr.s6_addr16[6]),
ntohs(sin6->sin6_addr.s6_addr16[7]),
ntohs(sin6->sin6_port));
} else
snprintf(buf, len, "(invalid address)");
#elif LINUX_VERSION_CODE < KERNEL_VERSION(3,11,0)
if (ss->ss_family == AF_INET)
snprintf(buf, len, "%pI4:%u",
&((const struct sockaddr_in *)ss)->sin_addr,
ntohs(((const struct sockaddr_in *)ss)->sin_port));
else if (ss->ss_family == AF_INET6)
snprintf(buf, len, "[%pI6c]:%u",
&((const struct sockaddr_in6 *)ss)->sin6_addr,
ntohs(((const struct sockaddr_in6 *)ss)->sin6_port));
else
snprintf(buf, len, "(invalid address)");
#else
snprintf(buf, len, "%pISpc", ss);
#endif
return buf;
}
static char *print_sockaddr(const struct sockaddr_storage *ss)
{
static char buf[64];
return snprintf_sockaddr(buf, sizeof(buf), ss);
}
static int is_zero_addr(const struct sockaddr_storage *ss)
{
if (ss->ss_family == AF_INET)
return ((const struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
else if (ss->ss_family == AF_INET6)
return ((const struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
((const struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
((const struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
((const struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
else /* AF_UNSPEC */
return 1;
}
static char *print_usock_addr(struct ipt_netflow_sock *usock)
{
static char buf[128];
size_t len;
snprintf(buf, sizeof(buf), "%s", print_sockaddr(&usock->addr));
if (!is_zero_addr(&usock->saddr)) {
len = strlen(buf);
snprintf(buf + len, sizeof(buf) - len, "@%s",
print_sockaddr(&usock->saddr));
len = strlen(buf);
/* strip zero port */
if (len > 2 && buf[len - 1] == '0' && buf[len - 2] == ':')
buf[len - 2] = '\0';
}
if (usock->sdev[0]) {
len = strlen(buf);
snprintf(buf + len, sizeof(buf) - len, "%%%s", usock->sdev);
}
return buf;
}
#ifdef CONFIG_PROC_FS
static inline int ABS(int x) { return x >= 0 ? x : -x; }
#define SAFEDIV(x,y) ((y)? ({ u64 __tmp = x; do_div(__tmp, y); (int)__tmp; }) : 0)
#define FFLOAT(x, prec) (int)(x) / prec, ABS((int)(x) % prec)
static int snmp_seq_show(struct seq_file *seq, void *v)
{
int cpu;
unsigned int nr_flows = atomic_read(&ipt_netflow_count);
struct ipt_netflow_stat t = { 0 };
struct ipt_netflow_sock *usock;
unsigned int sndbuf_peak = 0;
int snum = 0;
for_each_present_cpu(cpu) {
struct ipt_netflow_stat *st = &per_cpu(ipt_netflow_stat, cpu);
t.notfound += st->notfound;
t.pkt_total += st->pkt_total;
t.traf_total += st->traf_total;
t.send_failed += st->send_failed;
t.sock_cberr += st->sock_cberr;
t.exported_rate += st->exported_rate;
t.exported_pkt += st->exported_pkt;
t.exported_flow += st->exported_flow;
t.exported_traf += st->exported_traf;
t.pkt_drop += st->pkt_drop;
t.traf_drop += st->traf_drop;
t.pkt_lost += st->pkt_lost;
t.traf_lost += st->traf_lost;
t.flow_lost += st->flow_lost;
}
seq_printf(seq,
"inBitRate %llu\n"
"inPacketRate %llu\n"
"inFlows %llu\n"
"inPackets %llu\n"
"inBytes %llu\n"
"hashMetric %d.%02d\n"
"hashMemory %lu\n"
"hashFlows %u\n"
"hashPackets %llu\n"
"hashBytes %llu\n"
"dropPackets %llu\n"
"dropBytes %llu\n"
"outByteRate %u\n"
"outFlows %llu\n"
"outPackets %llu\n"
"outBytes %llu\n"
"lostFlows %llu\n"
"lostPackets %llu\n"
"lostBytes %llu\n"
"errTotal %u\n",
sec_brate,
sec_prate,
t.notfound,
t.pkt_total,
t.traf_total,
FFLOAT(SAFEDIV(100LL * (t.searched + t.found + t.notfound), (t.found + t.notfound)), 100),
(unsigned long)nr_flows * sizeof(struct ipt_netflow) +
(unsigned long)htable_size * sizeof(struct hlist_head),
nr_flows,
t.pkt_total - t.pkt_out,
t.traf_total - t.traf_out,
t.pkt_drop,
t.traf_drop,
t.exported_rate,
t.exported_flow,
t.exported_pkt,
t.exported_traf,
t.flow_lost,
t.pkt_lost,
t.traf_lost,
t.send_failed + t.sock_cberr);
for_each_present_cpu(cpu) {
struct ipt_netflow_stat *st = &per_cpu(ipt_netflow_stat, cpu);
seq_printf(seq,
"cpu%u %u %llu %llu %llu %d.%02d %llu %llu %u %u %u %u\n",
cpu,
st->pkt_total_rate,
st->notfound,
st->pkt_total,
st->traf_total,
FFLOAT(st->metric, 100),
st->pkt_drop,
st->traf_drop,
st->truncated,
st->frags,
st->alloc_err,
st->maxflows_err);
}
mutex_lock(&sock_lock);
list_for_each_entry(usock, &usock_list, list) {
int wmem_peak = atomic_read(&usock->wmem_peak);
if (sndbuf_peak < wmem_peak)
sndbuf_peak = wmem_peak;
seq_printf(seq, "sock%d %s %d %u %u %u %u",
snum,
print_usock_addr(usock),
!!usock->sock,
usock->err_connect,
usock->err_full,
usock->err_cberr,
usock->err_other);
if (usock->sock) {
struct sock *sk = usock->sock->sk;
seq_printf(seq, " %u %u %u\n",
sk->sk_sndbuf,
compat_refcount_read(&sk->sk_wmem_alloc),
wmem_peak);
} else
seq_printf(seq, " 0 0 %u\n", wmem_peak);
snum++;
}
mutex_unlock(&sock_lock);
seq_printf(seq, "sndbufPeak %u\n", sndbuf_peak);
return 0;
}
/* procfs statistics /proc/net/stat/ipt_netflow */
static int nf_seq_show(struct seq_file *seq, void *v)
{
unsigned int nr_flows = atomic_read(&ipt_netflow_count);
int cpu;
struct ipt_netflow_stat t = { 0 };
struct ipt_netflow_sock *usock;
#ifdef ENABLE_AGGR
struct netflow_aggr_n *aggr_n;
struct netflow_aggr_p *aggr_p;
#endif
int snum = 0;
int peak = (jiffies - peakflows_at) / HZ;
seq_printf(seq, "ipt_NETFLOW " IPT_NETFLOW_VERSION ", srcversion %s;"
#ifdef ENABLE_AGGR
" aggr"
#endif
#ifdef ENABLE_DIRECTION
" dir"
#endif
#ifdef HAVE_LLIST
" llist"
#endif
#ifdef ENABLE_MAC
" mac"
#endif
#ifdef CONFIG_NF_NAT_NEEDED
" nel"
#endif
#ifdef ENABLE_PROMISC
" promisc"
# ifdef PROMISC_MPLS
"+mpls"
# endif
#endif
#ifdef ENABLE_SAMPLER
" samp"
# ifdef SAMPLING_HASH
"-h"
# endif
#endif
#ifdef SNMP_RULES
" snmp"
#endif
#ifdef ENABLE_VLAN
" vlan"
#endif
"\n",
THIS_MODULE->srcversion);
seq_printf(seq, "Protocol version %d", protocol);
if (protocol == 10)
seq_printf(seq, " (ipfix)");
else
seq_printf(seq, " (netflow)");
if (protocol >= 9)
seq_printf(seq, ", refresh-rate %u, timeout-rate %u, (templates %d, active %d).\n",
refresh_rate, timeout_rate, tpl_gen_count, tpl_count);
else
seq_printf(seq, "\n");
seq_printf(seq, "Timeouts: active %ds, inactive %ds. Maxflows %u\n",
active_timeout,
inactive_timeout,
maxflows);
for_each_present_cpu(cpu) {
struct ipt_netflow_stat *st = &per_cpu(ipt_netflow_stat, cpu);
t.searched += st->searched;
t.found += st->found;
t.notfound += st->notfound;
t.pkt_total += st->pkt_total;
t.traf_total += st->traf_total;
#ifdef ENABLE_PROMISC
t.pkt_promisc += st->pkt_promisc;
t.pkt_promisc_drop += st->pkt_promisc_drop;
#endif
t.truncated += st->truncated;
t.frags += st->frags;
t.maxflows_err += st->maxflows_err;
t.alloc_err += st->alloc_err;
t.send_failed += st->send_failed;
t.sock_cberr += st->sock_cberr;
t.exported_rate += st->exported_rate;
t.exported_pkt += st->exported_pkt;
t.exported_flow += st->exported_flow;
t.exported_traf += st->exported_traf;
t.pkt_total_rate += st->pkt_total_rate;
t.pkt_drop += st->pkt_drop;
t.traf_drop += st->traf_drop;
t.pkt_lost += st->pkt_lost;
t.traf_lost += st->traf_lost;
t.flow_lost += st->flow_lost;
t.pkt_out += st->pkt_out;
t.traf_out += st->traf_out;
#ifdef ENABLE_SAMPLER
t.pkts_observed += st->pkts_observed;
t.pkts_selected += st->pkts_selected;
#endif
}
#ifdef ENABLE_SAMPLER
if (get_sampler_mode()) {
seq_printf(seq, "Flow sampling mode %s one-out-of %u.",
sampler_mode_string(),
get_sampler_interval());
if (get_sampler_mode() != SAMPLER_HASH)
seq_printf(seq, " Flows selected %lu, discarded %lu.",
atomic64_read(&flows_selected),
atomic64_read(&flows_observed) - atomic64_read(&flows_selected));
else
seq_printf(seq, " Flows selected %lu.", atomic64_read(&flows_selected));
seq_printf(seq, " Pkts selected %llu, discarded %llu.\n",
t.pkts_selected,
t.pkts_observed - t.pkts_selected);
} else
seq_printf(seq, "Flow sampling is disabled.\n");
#endif
#ifdef ENABLE_PROMISC
seq_printf(seq, "Promisc hack is %s (observed %llu packets, discarded %llu).\n",
promisc? "enabled" : "disabled",
t.pkt_promisc,
t.pkt_promisc_drop);
#endif
#ifdef CONFIG_NF_NAT_NEEDED
seq_printf(seq, "Natevents %s, count start %lu, stop %lu.\n", natevents? "enabled" : "disabled",
nat_events_start, nat_events_stop);
#endif
seq_printf(seq, "Flows: active %u (peak %u reached %ud%uh%um ago), mem %uK, worker delay %d/%d"
" [%d..%d] (%u ms, %u us, %u:%u"
#ifdef HAVE_LLIST
" %u"
#endif
" [cpu%u]).\n",
nr_flows,
peakflows,
peak / (60 * 60 * 24), (peak / (60 * 60)) % 24, (peak / 60) % 60,
(unsigned int)(((unsigned long)nr_flows * sizeof(struct ipt_netflow) +
(unsigned long)htable_size * sizeof(struct hlist_head)) >> 10),
worker_delay, HZ,
scan_min, scan_max,
jiffies_to_msecs(jiffies - wk_start),
jiffies_to_usecs(wk_busy),
wk_count,
wk_trylock,
#ifdef HAVE_LLIST
wk_llist,
#endif
wk_cpu);
seq_printf(seq, "Hash: size %u (mem %uK), metric %d.%02d [%d.%02d, %d.%02d, %d.%02d]."
" InHash: %llu pkt, %llu K, InPDU %llu, %llu.\n",
htable_size,
(unsigned int)((htable_size * sizeof(struct hlist_head)) >> 10),
FFLOAT(metric, 100),
FFLOAT(min_metric, 100),
FFLOAT(min5_metric, 100),
FFLOAT(min15_metric, 100),
t.pkt_total - t.pkt_out,
(t.traf_total - t.traf_out) >> 10,
pdu_packets,
pdu_traf);
seq_printf(seq, "Rate: %llu bits/sec, %llu packets/sec;"
" Avg 1 min: %llu bps, %llu pps; 5 min: %llu bps, %llu pps\n",
sec_brate, sec_prate, min_brate, min_prate, min5_brate, min5_prate);
seq_printf(seq, "cpu# pps; <search found new [metric], trunc frag alloc maxflows>,"
" traffic: <pkt, bytes>, drop: <pkt, bytes>\n");
seq_printf(seq, "Total %6u; %6llu %6llu %6llu [%d.%02d], %4u %4u %4u %4u,"
" traffic: %llu, %llu MB, drop: %llu, %llu K\n",
t.pkt_total_rate,
t.searched,
t.found,
t.notfound,
FFLOAT(SAFEDIV(100LL * (t.searched + t.found + t.notfound), (t.found + t.notfound)), 100),
t.truncated, t.frags, t.alloc_err, t.maxflows_err,
t.pkt_total, t.traf_total >> 20,
t.pkt_drop, t.traf_drop >> 10);
if (num_present_cpus() > 1) {
for_each_present_cpu(cpu) {
struct ipt_netflow_stat *st;
st = &per_cpu(ipt_netflow_stat, cpu);
seq_printf(seq, "cpu%-2u %6u; %6llu %6llu %6llu [%d.%02d], %4u %4u %4u %4u,"
" traffic: %llu, %llu MB, drop: %llu, %llu K\n",
cpu,
st->pkt_total_rate,
st->searched,
st->found,
st->notfound,
FFLOAT(st->metric, 100),
st->truncated, st->frags, st->alloc_err, st->maxflows_err,
st->pkt_total, st->traf_total >> 20,
st->pkt_drop, st->traf_drop >> 10);
}
}
seq_printf(seq, "Export: Rate %u bytes/s; Total %llu pkts, %llu MB, %llu flows;"
" Errors %u pkts; Traffic lost %llu pkts, %llu Kbytes, %llu flows.\n",
t.exported_rate,
t.exported_pkt,
t.exported_traf >> 20,
t.exported_flow,
t.send_failed,
t.pkt_lost,
t.traf_lost >> 10,
t.flow_lost);
mutex_lock(&sock_lock);
list_for_each_entry(usock, &usock_list, list) {
seq_printf(seq, "sock%d: %s",
snum,
print_usock_addr(usock));
if (usock->sock) {
struct sock *sk = usock->sock->sk;
seq_printf(seq, ", sndbuf %u, filled %u, peak %u;"
" err: sndbuf reached %u, connect %u, cberr %u, other %u\n",
sk->sk_sndbuf,
compat_refcount_read(&sk->sk_wmem_alloc),
atomic_read(&usock->wmem_peak),
usock->err_full,
usock->err_connect,
usock->err_cberr,
usock->err_other);
} else
seq_printf(seq, " unconnected (%u attempts).\n",
usock->err_connect);
snum++;
}
mutex_unlock(&sock_lock);
#ifdef ENABLE_AGGR
read_lock_bh(&aggr_lock);
snum = 0;
list_for_each_entry(aggr_n, &aggr_n_list, list) {
seq_printf(seq, "aggr#%d net: match %u.%u.%u.%u/%d strip %d (usage %u)\n",
snum,
HIPQUAD(aggr_n->addr),
mask2bits(aggr_n->mask),
mask2bits(aggr_n->aggr_mask),
atomic_read(&aggr_n->usage));
snum++;
}
snum = 0;
list_for_each_entry(aggr_p, &aggr_p_list, list) {
seq_printf(seq, "aggr#%d port: ports %u-%u replace %u (usage %u)\n",
snum,
aggr_p->port1,
aggr_p->port2,
aggr_p->aggr_port,
atomic_read(&aggr_p->usage));
snum++;
}
read_unlock_bh(&aggr_lock);
#endif
#ifdef SNMP_RULES
{
const unsigned char *rules;
snum = 0;
rcu_read_lock();
rules = rcu_dereference(snmp_ruleset);
if (rules)
while (*rules) {
const unsigned int len = *rules++;
seq_printf(seq, "SNMP-rule#%d: prefix '%.*s' map to %d\n",
snum, len, rules, (rules[len] << 8) + rules[len + 1]);
rules += len + 2;
++snum;
}
rcu_read_unlock();
}
#endif
return 0;
}
static int nf_seq_open(struct inode *inode, struct file *file)
{
return single_open(file, nf_seq_show, NULL);
}
static int snmp_seq_open(struct inode *inode, struct file *file)
{
return single_open(file, snmp_seq_show, NULL);
}
#ifdef HAVE_PROC_OPS
static struct proc_ops nf_seq_fops = {
.proc_open = nf_seq_open,
.proc_read = seq_read,
.proc_lseek = seq_lseek,
.proc_release = single_release,
};
#else
static struct file_operations nf_seq_fops = {
.owner = THIS_MODULE,
.open = nf_seq_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
#endif
#ifdef HAVE_PROC_OPS
static struct proc_ops snmp_seq_fops = {
.proc_open = snmp_seq_open,
.proc_read = seq_read,
.proc_lseek = seq_lseek,
.proc_release = single_release,
};
#else
static struct file_operations snmp_seq_fops = {
.owner = THIS_MODULE,
.open = snmp_seq_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
#endif
static inline int inactive_needs_export(const struct ipt_netflow *nf, const long i_timeout,
const unsigned long jiff);
static inline int active_needs_export(const struct ipt_netflow *nf, const long a_timeout,
const unsigned long jiff);
static inline u_int32_t hash_netflow(const struct ipt_netflow_tuple *tuple);
struct flows_dump_private {
int pcache; /* pos */
void *vcache; /* corresponding pointer for pos */
int stripe; /* current stripe */
struct list_head list; /* copy of stripe */
int alloc_errors;
};
/* deallocate copied stripe */
static void nf_free_stripe(struct list_head *list)
{
struct ipt_netflow *cf, *tmp;