forked from aabc/ipt-netflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipt_NETFLOW.c
2635 lines (2335 loc) · 71.1 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
/*
* This is NetFlow exporting module (NETFLOW target) for linux
* (c) 2008-2013 <[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 3 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/skbuff.h>
#include <linux/proc_fs.h>
#include <linux/vmalloc.h>
#include <linux/seq_file.h>
#include <linux/random.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <linux/icmp.h>
#include <linux/igmp.h>
#include <linux/inetdevice.h>
#include <linux/hash.h>
#include <linux/delay.h>
#include <linux/spinlock_types.h>
#include <net/icmp.h>
#include <net/ip.h>
#include <net/tcp.h>
#include <net/route.h>
#include <net/dst.h>
#include <linux/netfilter_ipv4/ip_tables.h>
#if defined(CONFIG_NF_NAT_NEEDED) || defined(CONFIG_NF_CONNTRACK_MARK)
#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>
#include "ipt_NETFLOW.h"
#include "murmur3.h"
#ifdef CONFIG_BRIDGE_NETFILTER
#include <linux/netfilter_bridge.h>
#endif
#ifdef CONFIG_SYSCTL
#include <linux/sysctl.h>
#endif
#ifndef NIPQUAD
#define NIPQUAD(addr) \
((unsigned char *)&addr)[0], \
((unsigned char *)&addr)[1], \
((unsigned char *)&addr)[2], \
((unsigned char *)&addr)[3]
#endif
#ifndef HIPQUAD
#if defined(__LITTLE_ENDIAN)
#define HIPQUAD(addr) \
((unsigned char *)&addr)[3], \
((unsigned char *)&addr)[2], \
((unsigned char *)&addr)[1], \
((unsigned char *)&addr)[0]
#elif defined(__BIG_ENDIAN)
#define HIPQUAD NIPQUAD
#else
#error "Please fix asm/byteorder.h"
#endif /* __LITTLE_ENDIAN */
#endif
#ifndef IPT_CONTINUE
#define IPT_CONTINUE XT_CONTINUE
#define ipt_target xt_target
#endif
#define IPT_NETFLOW_VERSION "1.8.1"
MODULE_LICENSE("GPL");
MODULE_AUTHOR("<[email protected]>");
MODULE_DESCRIPTION("iptables NETFLOW target module");
MODULE_VERSION(IPT_NETFLOW_VERSION);
#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");
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 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)");
static unsigned int refresh_rate = 20;
module_param(refresh_rate, uint, 0644);
MODULE_PARM_DESC(refresh_rate, "netflow v9 refresh rate (packets)");
static unsigned int timeout_rate = 30;
module_param(timeout_rate, uint, 0644);
MODULE_PARM_DESC(timeout_rate, "netflow v9 timeout rate (minutes)");
#ifdef CONFIG_NF_NAT_NEEDED
static int natevents = 0;
module_param(natevents, int, 0644);
MODULE_PARM_DESC(natevents, "send 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;
#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 DEFINE_PER_CPU(struct ipt_netflow_stat, ipt_netflow_stat);
static LIST_HEAD(usock_list);
static DEFINE_RWLOCK(sock_lock);
static unsigned int ipt_netflow_hash_rnd;
#define LOCK_COUNT (1<<9)
#define LOCK_COUNT_MASK (LOCK_COUNT-1)
static spinlock_t htable_locks[LOCK_COUNT] = {
[0 ... LOCK_COUNT - 1] = __SPIN_LOCK_UNLOCKED(htable_locks)
};
static struct hlist_head *ipt_netflow_hash __read_mostly; /* hash table memory */
static unsigned int ipt_netflow_hash_size __read_mostly = 0; /* buckets */
static LIST_HEAD(ipt_netflow_list); /* all flows */
static DEFINE_SPINLOCK(hlist_lock);
static LIST_HEAD(aggr_n_list);
static LIST_HEAD(aggr_p_list);
static DEFINE_RWLOCK(aggr_lock);
#if defined(CONFIG_NF_NAT_NEEDED) && LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0)
#undef CONFIG_NF_NAT_NEEDED
#endif
#ifdef CONFIG_NF_NAT_NEEDED
static LIST_HEAD(nat_list); /* nat events */
static DEFINE_RWLOCK(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;
static unsigned int pdu_tpl_records = 0;
static unsigned long pdu_ts_mod; /* ts of last flow */
static union {
struct netflow5_pdu v5;
struct netflow9_pdu v9;
struct ipfix_pdu ipfix;
} pdu;
static int engine_id = 0; /* Observation Domain */
static __u8 *pdu_data_used;
static __u8 *pdu_high_wm; /* high watermark */
static unsigned int pdu_max_size; /* sizeof pdu */
static struct flowset_data *pdu_flowset = NULL; /* current data flowset */
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;
static unsigned int metric = 100, min15_metric = 100, min5_metric = 100, min_metric = 100; /* hash metrics */
static int set_hashsize(int new_size);
static void destination_removeall(void);
static int add_destinations(char *ptr);
static void aggregation_remove(struct list_head *list);
static int add_aggregation(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_count = 0; /* how much active templates */
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);
#define MIN_DELAY 1
#define MAX_DELAY (HZ / 10)
static int worker_delay = HZ / 10;
static inline void _schedule_scan_worker(int status)
{
/* rudimentary congestion avoidance */
if (status > 0)
worker_delay -= status;
else if (status < 0)
worker_delay /= 2;
else
worker_delay++;
if (worker_delay < MIN_DELAY)
worker_delay = MIN_DELAY;
else if (worker_delay > MAX_DELAY)
worker_delay = MAX_DELAY;
schedule_delayed_work(&netflow_work, worker_delay);
}
static inline void start_scan_worker(void)
{
_schedule_scan_worker(0);
mutex_unlock(&worker_lock);
}
/* we always stop scanner before write_lock(&sock_lock)
* to let it never hold that spin 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
}
static inline void stop_scan_worker(void)
{
mutex_lock(&worker_lock);
_unschedule_scan_worker();
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
#define INIT_NET(x) x
#else
#define INIT_NET(x) init_net.x
#endif
#ifdef CONFIG_PROC_FS
/* 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;
unsigned long long searched = 0, found = 0, notfound = 0;
unsigned int truncated = 0, frags = 0, alloc_err = 0, maxflows_err = 0;
unsigned int sock_errors = 0, send_failed = 0, send_success = 0;
unsigned long long pkt_total = 0, traf_total = 0, exported_size = 0;
unsigned long long pkt_drop = 0, traf_drop = 0;
unsigned long long pkt_out = 0, traf_out = 0;
struct ipt_netflow_sock *usock;
struct netflow_aggr_n *aggr_n;
struct netflow_aggr_p *aggr_p;
int snum = 0;
int peak = (jiffies - peakflows_at) / HZ;
seq_printf(seq, "Flows: active %u (peak %u reached %ud%uh%um ago), mem %uK, worker delay %d/%d.\n",
nr_flows,
peakflows,
peak / (60 * 60 * 24), (peak / (60 * 60)) % 24, (peak / 60) % 60,
(unsigned int)((nr_flows * sizeof(struct ipt_netflow)) >> 10),
worker_delay, HZ);
for_each_present_cpu(cpu) {
struct ipt_netflow_stat *st = &per_cpu(ipt_netflow_stat, cpu);
searched += st->searched;
found += st->found;
notfound += st->notfound;
truncated += st->truncated;
frags += st->frags;
alloc_err += st->alloc_err;
maxflows_err += st->maxflows_err;
send_success += st->send_success;
send_failed += st->send_failed;
sock_errors += st->sock_errors;
exported_size += st->exported_size;
pkt_total += st->pkt_total;
traf_total += st->traf_total;
pkt_drop += st->pkt_drop;
traf_drop += st->traf_drop;
pkt_out += st->pkt_out;
traf_out += st->traf_out;
}
#define FFLOAT(x, prec) (int)(x) / prec, (int)(x) % prec
seq_printf(seq, "Hash: size %u (mem %uK), metric %d.%02d [%d.%02d, %d.%02d, %d.%02d]."
" MemTraf: %llu pkt, %llu K (pdu %llu, %llu).\n",
ipt_netflow_hash_size,
(unsigned int)((ipt_netflow_hash_size * sizeof(struct hlist_head)) >> 10),
FFLOAT(metric, 100),
FFLOAT(min_metric, 100),
FFLOAT(min5_metric, 100),
FFLOAT(min15_metric, 100),
pkt_total - pkt_out + pdu_packets,
(traf_total - traf_out + pdu_traf) >> 10,
pdu_packets,
pdu_traf);
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)",
refresh_rate, timeout_rate, template_ids - FLOWSET_DATA_FIRST, tpl_count);
seq_printf(seq, ". Timeouts: active %d, inactive %d. Maxflows %u\n",
active_timeout,
inactive_timeout,
maxflows);
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# stat: <search found new [metric], trunc frag alloc maxflows>,"
" sock: <ok fail cberr, bytes>, traffic: <pkt, bytes>, drop: <pkt, bytes>\n");
#define SAFEDIV(x,y) ((y)? ({ u64 __tmp = x; do_div(__tmp, y); (int)__tmp; }) : 0)
seq_printf(seq, "Total stat: %6llu %6llu %6llu [%d.%02d], %4u %4u %4u %4u,"
" sock: %6u %u %u, %llu K, traffic: %llu, %llu MB, drop: %llu, %llu K\n",
searched,
(unsigned long long)found,
(unsigned long long)notfound,
FFLOAT(SAFEDIV(100LL * (searched + found + notfound), (found + notfound)), 100),
truncated, frags, alloc_err, maxflows_err,
send_success, send_failed, sock_errors,
(unsigned long long)exported_size >> 10,
(unsigned long long)pkt_total, (unsigned long long)traf_total >> 20,
(unsigned long long)pkt_drop, (unsigned long long)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%u stat: %6llu %6llu %6llu [%d.%02d], %4u %4u %4u %4u,"
" sock: %6u %u %u, %llu K, traffic: %llu, %llu MB, drop: %llu, %llu K\n",
cpu,
(unsigned long long)st->searched,
(unsigned long long)st->found,
(unsigned long long)st->notfound,
FFLOAT(SAFEDIV(100LL * (st->searched + st->found + st->notfound), (st->found + st->notfound)), 100),
st->truncated, st->frags, st->alloc_err, st->maxflows_err,
st->send_success, st->send_failed, st->sock_errors,
(unsigned long long)st->exported_size >> 10,
(unsigned long long)st->pkt_total, (unsigned long long)st->traf_total >> 20,
(unsigned long long)st->pkt_drop, (unsigned long long)st->traf_drop >> 10);
}
}
#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
read_lock(&sock_lock);
list_for_each_entry(usock, &usock_list, list) {
seq_printf(seq, "sock%d: %u.%u.%u.%u:%u",
snum,
HIPQUAD(usock->ipaddr),
usock->port);
if (usock->sock) {
struct sock *sk = usock->sock->sk;
seq_printf(seq, ", sndbuf %u, filled %u, peak %u;"
" err: sndbuf reached %u, connect %u, other %u\n",
sk->sk_sndbuf,
atomic_read(&sk->sk_wmem_alloc),
atomic_read(&usock->wmem_peak),
atomic_read(&usock->err_full),
atomic_read(&usock->err_connect),
atomic_read(&usock->err_other));
} else
seq_printf(seq, " unconnected (%u attempts).\n",
atomic_read(&usock->err_connect));
snum++;
}
read_unlock(&sock_lock);
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);
return 0;
}
static int nf_seq_open(struct inode *inode, struct file *file)
{
return single_open(file, nf_seq_show, NULL);
}
static struct file_operations nf_seq_fops = {
.owner = THIS_MODULE,
.open = nf_seq_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
#endif /* CONFIG_PROC_FS */
#ifdef CONFIG_SYSCTL
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
#define BEFORE2632(x,y) x,y
#else /* since 2.6.32 */
#define BEFORE2632(x,y)
#endif
/* sysctl /proc/sys/net/netflow */
static int hsize_procctl(ctl_table *ctl, int write, BEFORE2632(struct file *filp,)
void __user *buffer, size_t *lenp, loff_t *fpos)
{
void *orig = ctl->data;
int ret, hsize;
if (write)
ctl->data = &hsize;
ret = proc_dointvec(ctl, write, BEFORE2632(filp,) buffer, lenp, fpos);
if (write) {
ctl->data = orig;
if (hsize < 1)
return -EPERM;
return set_hashsize(hsize)?:ret;
} else
return ret;
}
static int sndbuf_procctl(ctl_table *ctl, int write, BEFORE2632(struct file *filp,)
void __user *buffer, size_t *lenp, loff_t *fpos)
{
int ret;
struct ipt_netflow_sock *usock;
read_lock(&sock_lock);
if (list_empty(&usock_list)) {
read_unlock(&sock_lock);
return -ENOENT;
}
usock = list_first_entry(&usock_list, struct ipt_netflow_sock, list);
if (usock->sock)
sndbuf = usock->sock->sk->sk_sndbuf;
read_unlock(&sock_lock);
ctl->data = &sndbuf;
ret = proc_dointvec(ctl, write, BEFORE2632(filp,) buffer, lenp, fpos);
if (!write)
return ret;
if (sndbuf < SOCK_MIN_SNDBUF)
sndbuf = SOCK_MIN_SNDBUF;
stop_scan_worker();
write_lock(&sock_lock);
list_for_each_entry(usock, &usock_list, list) {
if (usock->sock)
usock->sock->sk->sk_sndbuf = sndbuf;
}
write_unlock(&sock_lock);
start_scan_worker();
return ret;
}
static int destination_procctl(ctl_table *ctl, int write, BEFORE2632(struct file *filp,)
void __user *buffer, size_t *lenp, loff_t *fpos)
{
int ret;
ret = proc_dostring(ctl, write, BEFORE2632(filp,) buffer, lenp, fpos);
if (ret >= 0 && write) {
stop_scan_worker();
destination_removeall();
add_destinations(destination_buf);
start_scan_worker();
}
return ret;
}
static int aggregation_procctl(ctl_table *ctl, int write, BEFORE2632(struct file *filp,)
void __user *buffer, size_t *lenp, loff_t *fpos)
{
int ret;
if (debug > 1)
printk(KERN_INFO "aggregation_procctl (%d) %u %llu\n", write, (unsigned int)(*lenp), *fpos);
ret = proc_dostring(ctl, write, BEFORE2632(filp,) buffer, lenp, fpos);
if (ret >= 0 && write) {
add_aggregation(aggregation_buf);
}
return ret;
}
static int flush_procctl(ctl_table *ctl, int write, BEFORE2632(struct file *filp,)
void __user *buffer, size_t *lenp, loff_t *fpos)
{
int ret;
int val;
val = 0;
ctl->data = &val;
ret = proc_dointvec(ctl, write, BEFORE2632(filp,) buffer, lenp, fpos);
if (!write)
return ret;
if (val > 0) {
printk(KERN_INFO "ipt_NETFLOW: forced flush\n");
stop_scan_worker();
netflow_scan_and_export(AND_FLUSH);
start_scan_worker();
}
return ret;
}
static int protocol_procctl(ctl_table *ctl, int write, BEFORE2632(struct file *filp,)
void __user *buffer, size_t *lenp, loff_t *fpos)
{
int ret;
int ver = protocol;
ctl->data = &ver;
ret = proc_dointvec(ctl, write, BEFORE2632(filp,) buffer, lenp, fpos);
if (!write)
return ret;
switch (ver) {
case 5:
case 9:
case 10:
printk(KERN_INFO "ipt_NETFLOW: forced flush (protocol version change)\n");
stop_scan_worker();
netflow_scan_and_export(AND_FLUSH);
netflow_switch_version(ver);
start_scan_worker();
break;
default:
return -EPERM;
}
return ret;
}
static struct ctl_table_header *netflow_sysctl_header;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
#define _CTL_NAME(x) .ctl_name = x,
#else
#define _CTL_NAME(x)
#endif
static struct ctl_table netflow_sysctl_table[] = {
{
_CTL_NAME(1)
.procname = "active_timeout",
.mode = 0644,
.data = &active_timeout,
.maxlen = sizeof(int),
.proc_handler = &proc_dointvec,
},
{
_CTL_NAME(2)
.procname = "inactive_timeout",
.mode = 0644,
.data = &inactive_timeout,
.maxlen = sizeof(int),
.proc_handler = &proc_dointvec,
},
{
_CTL_NAME(3)
.procname = "debug",
.mode = 0644,
.data = &debug,
.maxlen = sizeof(int),
.proc_handler = &proc_dointvec,
},
{
_CTL_NAME(4)
.procname = "hashsize",
.mode = 0644,
.data = &ipt_netflow_hash_size,
.maxlen = sizeof(int),
.proc_handler = &hsize_procctl,
},
{
_CTL_NAME(5)
.procname = "sndbuf",
.mode = 0644,
.maxlen = sizeof(int),
.proc_handler = &sndbuf_procctl,
},
{
_CTL_NAME(6)
.procname = "destination",
.mode = 0644,
.data = &destination_buf,
.maxlen = sizeof(destination_buf),
.proc_handler = &destination_procctl,
},
{
_CTL_NAME(7)
.procname = "aggregation",
.mode = 0644,
.data = &aggregation_buf,
.maxlen = sizeof(aggregation_buf),
.proc_handler = &aggregation_procctl,
},
{
_CTL_NAME(8)
.procname = "maxflows",
.mode = 0644,
.data = &maxflows,
.maxlen = sizeof(int),
.proc_handler = &proc_dointvec,
},
{
_CTL_NAME(9)
.procname = "flush",
.mode = 0644,
.maxlen = sizeof(int),
.proc_handler = &flush_procctl,
},
{
_CTL_NAME(10)
.procname = "protocol",
.mode = 0644,
.maxlen = sizeof(int),
.proc_handler = &protocol_procctl,
},
{
_CTL_NAME(11)
.procname = "refresh-rate",
.mode = 0644,
.data = &refresh_rate,
.maxlen = sizeof(int),
.proc_handler = &proc_dointvec,
},
{
_CTL_NAME(12)
.procname = "timeout-rate",
.mode = 0644,
.data = &timeout_rate,
.maxlen = sizeof(int),
.proc_handler = &proc_dointvec,
},
#ifdef CONFIG_NF_NAT_NEEDED
{
_CTL_NAME(12)
.procname = "natevents",
.mode = 0644,
.data = &natevents,
.maxlen = sizeof(int),
.proc_handler = &proc_dointvec,
},
#endif
{ }
};
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
static struct ctl_table netflow_sysctl_root[] = {
{
_CTL_NAME(33)
.procname = "netflow",
.mode = 0555,
.child = netflow_sysctl_table,
},
{ }
};
static struct ctl_table netflow_net_table[] = {
{
.ctl_name = CTL_NET,
.procname = "net",
.mode = 0555,
.child = netflow_sysctl_root,
},
{ }
};
#else /* >= 2.6.25 */
static struct ctl_path netflow_sysctl_path[] = {
{
.procname = "net",
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
.ctl_name = CTL_NET
#endif
},
{ .procname = "netflow" },
{ }
};
#endif /* 2.6.25 */
#endif /* CONFIG_SYSCTL */
/* socket code */
static void sk_error_report(struct sock *sk)
{
/* clear connection refused errors if any */
write_lock_bh(&sk->sk_callback_lock);
if (debug > 1)
printk(KERN_INFO "ipt_NETFLOW: socket error <%d>\n", sk->sk_err);
sk->sk_err = 0;
NETFLOW_STAT_INC(sock_errors);
write_unlock_bh(&sk->sk_callback_lock);
return;
}
static struct socket *_usock_alloc(__be32 ipaddr, unsigned short port)
{
struct sockaddr_in sin;
struct socket *sock;
int error;
if ((error = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
printk(KERN_ERR "ipt_NETFLOW: sock_create_kern error %d\n", -error);
return NULL;
}
sock->sk->sk_allocation = GFP_ATOMIC;
sock->sk->sk_prot->unhash(sock->sk); /* hidden from input */
sock->sk->sk_error_report = &sk_error_report; /* clear ECONNREFUSED */
if (sndbuf)
sock->sk->sk_sndbuf = sndbuf;
else
sndbuf = sock->sk->sk_sndbuf;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(ipaddr);
sin.sin_port = htons(port);
if ((error = sock->ops->connect(sock, (struct sockaddr *)&sin,
sizeof(sin), 0)) < 0) {
printk(KERN_ERR "ipt_NETFLOW: error connecting UDP socket %d,"
" don't worry, will try reconnect later.\n", -error);
/* ENETUNREACH when no interfaces */
sock_release(sock);
return NULL;
}
return sock;
}
static void usock_connect(struct ipt_netflow_sock *usock, int sendmsg)
{
usock->sock = _usock_alloc(usock->ipaddr, usock->port);
if (usock->sock) {
if (sendmsg || debug)
printk(KERN_INFO "ipt_NETFLOW: connected %u.%u.%u.%u:%u\n",
HIPQUAD(usock->ipaddr),
usock->port);
} else {
atomic_inc(&usock->err_connect);
if (debug)
printk(KERN_INFO "ipt_NETFLOW: connect to %u.%u.%u.%u:%u failed%s.\n",
HIPQUAD(usock->ipaddr),
usock->port,
(sendmsg)? " (pdu lost)" : "");
}
atomic_set(&usock->wmem_peak, 0);
atomic_set(&usock->err_full, 0);
atomic_set(&usock->err_other, 0);
}
// return numbers of sends succeded, 0 if none
/* only called in scan worker path */
static void netflow_sendmsg(void *buffer, int len)
{
struct msghdr msg = { .msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL };
struct kvec iov = { buffer, len };
int retok = 0, ret;
int snum = 0;
struct ipt_netflow_sock *usock;
list_for_each_entry(usock, &usock_list, list) {
if (!usock->sock)
usock_connect(usock, 1);
if (!usock->sock) {
NETFLOW_STAT_INC_ATOMIC(send_failed);
continue;
}
if (debug)
printk(KERN_INFO "netflow_sendmsg: sendmsg(%d, %d) [%u %u]\n",
snum,
len,
atomic_read(&usock->sock->sk->sk_wmem_alloc),
usock->sock->sk->sk_sndbuf);
ret = kernel_sendmsg(usock->sock, &msg, &iov, 1, (size_t)len);
if (ret < 0) {
char *suggestion = "";
NETFLOW_STAT_INC_ATOMIC(send_failed);
if (ret == -EAGAIN) {
atomic_inc(&usock->err_full);
suggestion = ": increase sndbuf!";
} else
atomic_inc(&usock->err_other);
printk(KERN_ERR "ipt_NETFLOW: sendmsg[%d] error %d: data loss %llu pkt, %llu bytes%s\n",
snum, ret, pdu_packets, pdu_traf, suggestion);
} else {
unsigned int wmem = atomic_read(&usock->sock->sk->sk_wmem_alloc);
if (wmem > atomic_read(&usock->wmem_peak))
atomic_set(&usock->wmem_peak, wmem);
NETFLOW_STAT_INC_ATOMIC(send_success);
NETFLOW_STAT_ADD_ATOMIC(exported_size, ret);
retok++;
}
snum++;
}
if (retok == 0) {
/* not least one send succeded, account stat for dropped packets */
NETFLOW_STAT_ADD_ATOMIC(pkt_drop, pdu_packets);
NETFLOW_STAT_ADD_ATOMIC(traf_drop, pdu_traf);
}
}
static void usock_close_free(struct ipt_netflow_sock *usock)
{
printk(KERN_INFO "ipt_NETFLOW: removed destination %u.%u.%u.%u:%u\n",
HIPQUAD(usock->ipaddr),
usock->port);
if (usock->sock)
sock_release(usock->sock);
usock->sock = NULL;
vfree(usock);
}
static void destination_removeall(void)
{
write_lock(&sock_lock);
while (!list_empty(&usock_list)) {
struct ipt_netflow_sock *usock;
usock = list_entry(usock_list.next, struct ipt_netflow_sock, list);
list_del(&usock->list);
write_unlock(&sock_lock);
usock_close_free(usock);
write_lock(&sock_lock);
}
write_unlock(&sock_lock);
}
static void add_usock(struct ipt_netflow_sock *usock)
{
struct ipt_netflow_sock *sk;
write_lock(&sock_lock);
/* don't need duplicated sockets */
list_for_each_entry(sk, &usock_list, list) {
if (sk->ipaddr == usock->ipaddr &&
sk->port == usock->port) {
write_unlock(&sock_lock);
usock_close_free(usock);
return;
}
}
list_add_tail(&usock->list, &usock_list);
printk(KERN_INFO "ipt_NETFLOW: added destination %u.%u.%u.%u:%u%s\n",
HIPQUAD(usock->ipaddr),
usock->port,
(!usock->sock)? " (unconnected)" : "");
write_unlock(&sock_lock);
}
#define SEPARATORS " ,;\t\n"
static int add_destinations(char *ptr)
{
while (ptr) {
unsigned char ip[4];
unsigned short port;
ptr += strspn(ptr, SEPARATORS);
if (sscanf(ptr, "%hhu.%hhu.%hhu.%hhu:%hu",
ip, ip + 1, ip + 2, ip + 3, &port) == 5) {
struct ipt_netflow_sock *usock;
if (!(usock = vmalloc(sizeof(*usock)))) {
printk(KERN_ERR "ipt_NETFLOW: can't vmalloc socket\n");
return -ENOMEM;
}
memset(usock, 0, sizeof(*usock));
atomic_set(&usock->err_connect, 0);
usock->ipaddr = ntohl(*(__be32 *)ip);
usock->port = port;
usock_connect(usock, 0);
add_usock(usock);
} else
break;
ptr = strpbrk(ptr, SEPARATORS);
}
return 0;
}
static void aggregation_remove(struct list_head *list)
{
write_lock_bh(&aggr_lock);
while (!list_empty(list)) {
struct netflow_aggr_n *aggr; /* match netflow_aggr_p too */
aggr = list_entry(list->next, struct netflow_aggr_n, list);
list_del(&aggr->list);
write_unlock_bh(&aggr_lock);
vfree(aggr);
write_lock_bh(&aggr_lock);
}
write_unlock_bh(&aggr_lock);
}
static int add_aggregation(char *ptr)
{
struct netflow_aggr_n *aggr_n, *aggr, *tmp;
struct netflow_aggr_p *aggr_p;
LIST_HEAD(new_aggr_n_list);
LIST_HEAD(new_aggr_p_list);
LIST_HEAD(old_aggr_list);
while (ptr && *ptr) {
unsigned char ip[4];
unsigned int mask;
unsigned int port1, port2;
unsigned int aggr_to;
ptr += strspn(ptr, SEPARATORS);
if (sscanf(ptr, "%hhu.%hhu.%hhu.%hhu/%u=%u",
ip, ip + 1, ip + 2, ip + 3, &mask, &aggr_to) == 6) {
if (!(aggr_n = vmalloc(sizeof(*aggr_n)))) {
printk(KERN_ERR "ipt_NETFLOW: can't vmalloc aggr\n");
return -ENOMEM;
}
memset(aggr_n, 0, sizeof(*aggr_n));
aggr_n->mask = bits2mask(mask);
aggr_n->addr = ntohl(*(__be32 *)ip) & aggr_n->mask;