aboutsummaryrefslogtreecommitdiffstats
path: root/unmaintained/ipt-netflow/git.patch
blob: faa49ffdc7f789c8c1d0c2e0ff3c283080016be0 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
diff --git a/Makefile.in b/Makefile.in
index 30ebbfe..0a82c67 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -1,43 +1,73 @@
-#
+# Edit Makefile.in and run ./configure
 
 KVERSION = @KVERSION@
 KDIR = @KDIR@
+KINSTDIR = $(shell dirname @KDIR@)
 IPTABLES_CFLAGS = @IPTABLES_CFLAGS@
 IPTABLES_MODULES = @IPTABLES_MODULES@
+DEPMOD = depmod -a
 
+# https://www.kernel.org/doc/Documentation/kbuild/modules.txt
+# https://www.kernel.org/doc/Documentation/kbuild/makefiles.txt
 obj-m = ipt_NETFLOW.o
 
-ipt_NETFLOW.ko: ipt_NETFLOW.c ipt_NETFLOW.h
+all: ipt_NETFLOW.ko libipt_NETFLOW.so libip6t_NETFLOW.so
+ipt_NETFLOW.ko: version.h ipt_NETFLOW.c ipt_NETFLOW.h Makefile
 	@echo Compiling for kernel $(KVERSION)
 	make -C $(KDIR) M=$(CURDIR) modules
-all: ipt_NETFLOW.ko libipt_NETFLOW.so
-minstall:
-	make -C $(KDIR) M=$(CURDIR) modules_install
+	@touch $@
+sparse: | version.h ipt_NETFLOW.c ipt_NETFLOW.h Makefile
+	@rm -f ipt_NETFLOW.ko ipt_NETFLOW.o
+	@echo Compiling for kernel $(KVERSION)
+	make -C $(KDIR) M=$(CURDIR) modules C=1
+	@touch ipt_NETFLOW.ko
+minstall: | ipt_NETFLOW.ko
+	make -C $(KDIR) M=$(CURDIR) modules_install INSTALL_MOD_PATH=$(DESTDIR)
+	$(DEPMOD)
 mclean:
 	make -C $(KDIR) M=$(CURDIR) clean
 lclean:
 	-rm -f *.so *_sh.o
 clean: mclean lclean
-	-rm -f *.so *.o modules.order
+	-rm -f *.so *.o modules.order version.h
+
+%_sh.o: libipt_NETFLOW.c
+	gcc -O2 -Wall -Wunused $(IPTABLES_CFLAGS) -fPIC -o $@ -c libipt_NETFLOW.c
+
+%.so: %_sh.o
+	gcc -shared -o $@ $<
 
-libipt_NETFLOW.so: libipt_NETFLOW.c
-	gcc -O2 -Wall -Wunused -I$(KDIR)/include $(IPTABLES_CFLAGS) -fPIC -o libipt_NETFLOW_sh.o -c libipt_NETFLOW.c
-	gcc -shared  -o libipt_NETFLOW.so libipt_NETFLOW_sh.o
+version.h: ipt_NETFLOW.c ipt_NETFLOW.h Makefile
+	@if [ -d .git ] && type git >/dev/null 2>&1; then \
+		 echo "#define GITVERSION \"`git describe --dirty`\""; \
+	fi > version.h
 
-linstall: ipt_NETFLOW.ko libipt_NETFLOW.so
-	cp -a libipt_NETFLOW.so $(IPTABLES_MODULES)
+linstall: | libipt_NETFLOW.so libip6t_NETFLOW.so
+	install -D libipt_NETFLOW.so $(DESTDIR)$(IPTABLES_MODULES)/libipt_NETFLOW.so
+	install -D libip6t_NETFLOW.so $(DESTDIR)$(IPTABLES_MODULES)/libip6t_NETFLOW.so
 
 install: minstall linstall
 
+uninstall:
+	-rm -f $(DESTDIR)$(IPTABLES_MODULES)/libipt_NETFLOW.so
+	-rm -f $(DESTDIR)$(IPTABLES_MODULES)/libip6t_NETFLOW.so
+	-rm -f $(DESTDIR)$(KINSTDIR)/extra/ipt_NETFLOW.ko
+
 Makefile: Makefile.in configure
 	./configure --make
 
 load: all
-	insmod ipt_NETFLOW.ko active_timeout=5
-	iptables -A OUTPUT -d 0/0 -j NETFLOW
-	iptables -A INPUT -d 0/0 -j NETFLOW
+	-insmod ipt_NETFLOW.ko active_timeout=5 protocol=9
+	-iptables -I OUTPUT -j NETFLOW
+	-iptables -I INPUT -j NETFLOW
+	-ip6tables -I OUTPUT -j NETFLOW
+	-ip6tables -I INPUT -j NETFLOW
 
 unload:
-	iptables -D OUTPUT -d 0/0 -j NETFLOW
-	iptables -D INPUT -d 0/0 -j NETFLOW
-	rmmod ipt_NETFLOW.ko
+	-iptables -D OUTPUT -j NETFLOW
+	-iptables -D INPUT -j NETFLOW
+	-ip6tables -D OUTPUT -j NETFLOW
+	-ip6tables -D INPUT -j NETFLOW
+	-rmmod ipt_NETFLOW.ko
+
+reload: unload load
diff --git a/README b/README
index 213f02c..56a4fde 100644
--- a/README
+++ b/README
@@ -1,10 +1,17 @@
-ipt_NETFLOW linux 2.6 kernel module by <abc@telekom.ru> -- 11 Feb 2008
+ipt_NETFLOW linux 2.6.x-3.x kernel module by <abc@telekom.ru> -- 2008-2013.
+
+   High performance NetFlow v5, v9, IPFIX flow data export module for Linux
+   kernel. Supporting IPv4 and IPv6. Created to be useful for highly loaded
+   linux router. It should be used as iptables target. Also can export NAT
+   translation events using NetFlow Event Logging (NEL) for v9, IPFIX, or
+   specially crafted v5 flows.
+
 
 ============================
 = OBTAINING LATEST VERSION =
 ============================
 
-   $ git clone git://ipt-netflow.git.sourceforge.net/gitroot/ipt-netflow/ipt-netflow
+   $ git clone git://git.code.sf.net/p/ipt-netflow/code ipt-netflow
    $ cd ipt-netflow
 
 
@@ -12,94 +19,220 @@ ipt_NETFLOW linux 2.6 kernel module by <abc@telekom.ru> -- 11 Feb 2008
 = INSTALLATION =
 ================
 
-1. Besides kernel you will need iptables/netfilter source matching your
-     installation or just fresh install from there: ftp://ftp.netfilter.org/pub/iptables/snapshot/
-   I have this: ftp://ftp.netfilter.org/pub/iptables/snapshot/iptables-1.3.7-20070329.tar.bz2
-   Unpack it somewhere and build with make.
+   Four easy steps.
+
+** 1. Prepare Kernel source
+
+   If you have package system install kernel-devel package, otherwise install
+   raw kernel source from http://kernel.org matching _exactly_ version of your
+   installed kernel.
+
+   a) What to do for Centos:
+
+      ~# yum install kernel-devel
+
+   b) What to do for Debian:
+
+      ~# apt-get install module-assistant
+      ~# m-a prepare
+
+   c) Otherwise, if you downloaded raw kernel sources don't forget to create
+    .config by copying it from your distribution's kernel. Its copy could reside
+    in /boot or sometimes in /proc, examples:
+
+      kernel-src-dir/# cp /boot/config-`uname -r` .config
+    or
+      kernel-src-dir/# zcat /proc/config.gz > .config
+
+    Assuming you unpacked kernel source into `kernel-src-dir/' directory.
+    Then run:
+
+      kernel-src-dir/# make oldconfig
+
+    After that you'll need to prepare kernel for modules build:
+
+      kernel-src-dir/# make prepare modules_prepare
+
+   Note: Don't try to `make prepare' in Centos kernel-devel package directory
+     (which is usually something like /usr/src/kernels/2.6.32-431.el6.x86_64)
+     as this is wrong and meaningless.
+
+** 2. Prepare Iptables
+
+   Before this step it also would be useful to install pkg-config if don't
+   already have.
+
+   If you have package system just install iptables-devel (or iptables-dev)
+   package, otherwise install iptables source matching version of your
+   installation from ftp://ftp.netfilter.org/pub/iptables/
+
+   a) What to do for Centos:
+
+      # yum install iptables-devel
+
+   b) What to do for Debian:
+
+      # apt-get install iptables-dev pkg-config
 
-2. Run ./configure script and it will create Makefile
+   c) Otherwise, for raw iptables source build it and make install.
 
-3. make all install; depmod
-   This will install kernel module and iptable specific library.
+** 3. Now, to actually build the module run:
 
-Troubleshooting:
-   1) Sometimes you will want to add CC=gcc-3 to make command.
-   Example: make CC=gcc-3.3
+      ~/ipt-netflow# ./configure
+      ~/ipt-netflow# make all install
+      ~/ipt-netflow# depmod
 
-   2) Compile module with actual kernel source compiled.
-   I.e. first compile kernel and boot into it, and then compile module.
+   This will install kernel module and iptables specific library.
 
-   3) For autoloading module after reboot: set net.netflow.destination (or load
-   module, if idestination set on load) after interfaces are up. Becasue module
-   needs exporting interface (usually lo) to establish export connection.
+   Troubleshooting:
 
-4. After this point you should be able to load module
-     and use -j NETFLOW target in your iptables. See next section.
+     a) Sometimes you will want to add CC=gcc-3 to make command.
+     Example: make CC=gcc-3.3
+
+     b) Compile module with actual kernel source compiled.
+     I.e. first compile kernel and boot into it, and then compile module.
+     If you are using kernel-devel package check that its version matches
+     your kernel package.
+
+     c) If you have sources in non-standard places or configure isn't able to
+     find something run ./configure --help to see how to specify paths manually.
+
+** 4. After this point you should be able to load module and
+     use -j NETFLOW target in your iptables. See next section.
 
 
 ===========
 = RUNNING =
 ===========
 
-1. You can load module by insmod like this:
-   # insmod ipt_NETFLOW.ko destination=127.0.0.1:2055 debug=1
+1. You can load module directly by insmod like this:
+
+     # insmod ipt_NETFLOW.ko destination=127.0.0.1:2055 debug=1
 
    Or if properly installed (make install; depmod) by this:
-   # modprobe ipt_NETFLOW destination=127.0.0.1:2055
+
+     # modprobe ipt_NETFLOW destination=127.0.0.1:2055
 
    See, you may add options in insmod/modprobe command line, or add
-     them in /etc/ to modules.conf or modprobe.conf like thus:
-   options ipt_NETFLOW destination=127.0.0.1:2055
+   them in /etc/modprobe.conf or /etc/modprobe.d/ipt_NETFLOW.conf
+   like thus:
+
+     options ipt_NETFLOW destination=127.0.0.1:2055 protocol=9 natevents=1
 
 2. Statistics is in /proc/net/stat/ipt_netflow
-   To view slab statistics: grep ipt_netflow /proc/slabinfo
+   To view boring slab statistics: grep ipt_netflow /proc/slabinfo
 
 3. You can view parameters and control them via sysctl, example:
-   # sysctl -w net.netflow.hashsize=32768
 
-4. Example of directing all traffic into module:
-   # iptables -A FORWARD -j NETFLOW
-   # iptables -A INPUT -j NETFLOW
-   # iptables -A OUTPUT -j NETFLOW
+     # sysctl net.netflow
+     # sysctl net.netflow.hashsize=32768
+
+   Note: For after-reboot configuration I recommend to store module parameters
+   in modprobe configs instead of storing them in /etc/sysctl.conf, as it's
+   less clear when init process will apply sysctl.conf, before of after
+   module's load.
+
+4. Example of directing all IPv4 traffic into the module:
+
+     # iptables -I FORWARD -j NETFLOW
+     # iptables -I INPUT -j NETFLOW
+     # iptables -I OUTPUT -j NETFLOW
+
+   Note: It is preferable (because easier to understand) to _insert_
+   NETFLOW target at the top of the chain, otherwise not all traffic may
+   reach NETFLOW if your iptables configuration is complicated and some
+   other rule inadvertently consume the traffic (dropping or acepting before
+   NETFLOW is reached). It's always good to test your configuration.
+   Use  iptables -L -nvx  to check pkts/bytes counters on the rules.
+
+5. If you want to account IPv6 traffic you should use protocol 9 or 10.
+   Example of directing all IPv6 traffic into the module:
 
+     # sysctl net.netflow.protocol=10
+     # ip6tables -I FORWARD -j NETFLOW
+     # ip6tables -I INPUT -j NETFLOW
+     # ip6tables -I OUTPUT -j NETFLOW
+
+   Note: First enable right version of protocol and after that add ip6tables
+     rules, otherwise you will get errors in dmesg.
+
+6. If you want to account NAT events (NEL):
+
+     # sysctl net.netflow.natevents=1
+
+   Note that natevents feature is completely independent from traffic accounting
+   (it's using so called conntrack events), thus you don't need to set or change
+   any iptables rules to use that. You may need to enable kernel config option
+   CONFIG_NF_CONNTRACK_EVENTS though (if it isn't already enabled).
+   For details on how they are exported for different protocol versions see
+   below.
 
 ===========
 = OPTIONS =
 ===========
 
+   protocol=5
+     - what version of NetFlow protocol to use. Default is 5.
+       You can choose from 5, 9, or 10 (where 10 is IPFIX). If you plan
+       to account IPv6 traffic you should use protocol 9 or 10 (IPFIX),
+       because NetFlow v5 isn't compatible with IPv6.
+
    destination=127.0.0.1:2055
      - where to export netflow, to this ip address
        You will see this connection in netstat like this:
        udp 0 0 127.0.0.1:32772 127.0.0.1:2055 ESTABLISHED 
 
    destination=127.0.0.1:2055,192.0.0.1:2055
-     - mirror flows to two (can be more) addresses,
-       separate addresses with comma.
+     - mirror flows to two (can be more) addresses, separate addresses
+       with comma.
+
+   natevents=1
+     - Collect and send NAT translation events as NetFlow Event Logging (NEL)
+       for NetFlow v9/IPFIX, or as dummy flows compatible with NetFlow v5.
+       Default is 0 (don't send).
+
+       For NetFlow v5 protocol meaning of fields in dummy flows are such:
+         Src IP, Src Port  is Pre-nat source address.
+         Dst IP, Dst Port  is Post-nat destination address.
+           - These two fields made equal to data flows catched in FORWARD chain.
+	 Nexthop, Src AS  is Post-nat source address for SNAT. Or,
+	 Nexthop, Dst AS  is Pre-nat destination address for DNAT.
+	 TCP Flags is SYN+SCK for start event, RST+FIN for stop event.
+	 Pkt/Traffic size is 0 (zero), so it won't interfere with accounting.
 
    inactive_timeout=15
      - export flow after it's inactive 15 seconds. Default value is 15.
 
    active_timeout=1800
-     - export flow after it's active 1800 seconds (30 minutes). Default value is 1800.
+     - export flow after it's active 1800 seconds (30 minutes). Default valuae
+       is 1800.
+
+   refresh-rate=20
+     - for NetFlow v9 and IPFIX it's rate how frequently to re-send templates
+       (per packets). You probably don't need to change default (which is 20).
+
+   timeout-rate=30
+     - for NetFlow v9 and IPFIX it's rate when to re-send old templates (in
+       minutes). No need to change it.
 
    debug=0
      - debug level (none).
 
    sndbuf=number
-     - size of output socket buffer in bytes. Recommend you to put
+     - size of output socket buffer in bytes. I recommend you to put
        higher value if you experience netflow packet drops (can be
        seen in statistics as 'sock: fail' number.)
        Default value is system default.
 
    hashsize=number
      - Hash table bucket size. Used for performance tuning.
-       Abstractly speaking, it should be two times bigger than flows
+       Abstractly speaking, it should be minimum two times bigger than flows
        you usually have, but not need to.
        Default is system memory dependent small enough value.
 
    maxflows=2000000
-     - Maximum number of flows to account. It's here to prevent DOS attacks. After
-       this limit reached new flows will not be accounted. Default is
+     - Maximum number of flows to account. It's here to prevent DOS attacks.
+       After this limit reached new flows will not be accounted. Default is
        2000000, zero is unlimited.
 
    aggregation=string..
@@ -130,14 +263,15 @@ Troubleshooting:
 = HOW TO READ STAT =
 ====================
 
-  Statistics is your friend to fine tune and understand netflow module performance.
+  Statistics is your friend to fine tune and understand netflow module
+  performance.
 
   To see stat:
   # cat /proc/net/stat/ipt_netflow
 
   How to interpret the data:
 
-> Flows: active 5187 (peak 83905 reached 0d0h1m ago, maxflows 2000000), mem 283K
+> Flows: active 5187 (peak 83905 reached 0d0h1m ago, maxflows 2000000), mem 283K, worker delay 100/1000.
 
   active X: currently active flows in memory cache.
     - for optimum CPU performance it is recommended to set hash table size to
@@ -146,8 +280,9 @@ Troubleshooting:
   mem XK: how much kilobytes of memory currently taken by active flows.
     - one active flow taking 56 bytes of memory.
     - there is system limit on cache size too.
+  worker delay X/HZ: how frequently exporter scan flows table per second.
 
-> Hash: size 8192 (mem 32K), metric 1.0, 1.0, 1.0, 1.0. MemTraf: 1420 pkt, 364 K (pdu 0, 0).
+> Hash: size 8192 (mem 32K), metric 1.00, [1.00, 1.00, 1.00]. MemTraf: 1420 pkt, 364 K (pdu 0, 0).
 
   Hash: size X: current hash size/limit.
     - you can control this by sysctl net.netflow.hashsize variable.
@@ -156,18 +291,22 @@ Troubleshooting:
     - optimal value is twice of average of active flows.
   mem XK: how much memory occupied by hash table.
     - hash table is fixed size by nature, taking 4 bytes per entry.
-  metric X, X, X, X: how optimal is your hash table being used.
+  metric X, [X, X, X]: how optimal is your hash table being used.
     - lesser value mean more optimal hash table use, min is 1.0.
-    - this is moving average (EWMA) of hash table access divided
-      by match rate (searches / matches) for 4sec, and 1, 5, 15 minutes.
-      Sort of hash table load average.
+    - last three numbers in squares is moving average (EWMA) of hash table
+      access divided by match rate (searches / matches) for 4sec, and 1, 5, and
+      15 minutes. Sort of hash table load average. First value is instantaneous.
+      You can try to increase hashsize if averages more than 1 (increase
+      certainly if >= 2).
   MemTraf: X pkt, X K: how much traffic accounted for flows that are in memory.
     - these flows that are residing in internal hash table.
   pdu X, X: how much traffic in flows preparing to be exported.
     - it is included already in aforementioned MemTraf total.
 
-> Timeout: active 1800, inactive 15. Maxflows 2000000
+> Protocol version 10 (ipfix), refresh-rate 20, timeout-rate 30, (templates 2, active 2). Timeouts: active 5, inactive 15. Maxflows 2000000
 
+  Protocol version currently in use. Refresh-rate and timeout-rate
+      for v9 and IPFIX. Total templates generated and currently active.
   Timeout: active X: how much seconds to wait before exporting active flow.
     - same as sysctl net.netflow.active_timeout variable.
   inactive X: how much seconds to wait before exporting inactive flow.
@@ -180,20 +319,22 @@ Troubleshooting:
 
   - Module throughput values for 1 second, 1 minute, and 5 minutes.
 
-> cpu#  stat: <search found new, trunc frag alloc maxflows>, sock: <ok fail cberr, bytes>, traffic: <pkt, bytes>, drop: <pkt, bytes>
-> cpu0  stat: 980540  10473 180600,    0    0    0    0, sock:   4983 928 0, 7124 K, traffic: 188765, 14 MB, drop: 27863, 1142 K
+> cpu#  stat: <search found new [metric], trunc frag alloc maxflows>, sock: <ok fail cberr, bytes>, traffic: <pkt, bytes>, drop: <pkt, bytes>
+> cpu0  stat: 980540  10473 180600 [1.03],    0    0    0    0, sock:   4983 928 0, 7124 K, traffic: 188765, 14 MB, drop: 27863, 1142 K
 
   cpu#: this is Total and per CPU statistics for:
   stat: <search found new, trunc frag alloc maxflows>: internal stat for:
   search found new: hash table searched, found, and not found counters.
-  trunc: how much truncated packets is ignored
+  [metric]: average hash metric since module load.
+  trunc: how much truncated packets are ignored
     - these are that possible don't have valid IP header.
     - accounted in drop packets counter but not in drop bytes.
   frag: how much fragmented packets have seen.
     - kernel always defragments INPUT/OUTPUT chains for us.
     - these packets are not ignored but not reassembled either, so:
-    - if there is no enough data in fragment (ex. tcp ports) it is considered zero.
-  alloc: how much cache memory allocations is failed.
+    - if there is no enough data in fragment (ex. tcp ports) it is considered
+      zero.
+  alloc: how much cache memory allocations are failed.
     - packets ignored and accounted in drop stat.
     - probably increase system memory if this ever happen.
   maxflows: how much packets ignored on maxflows (maximum active flows reached).
@@ -203,7 +344,8 @@ Troubleshooting:
   sock: <ok fail cberr, bytes>: table of exporting stats for:
   ok: how much Netflow PDUs are exported (i.e. UDP packets sent by module).
   fail: how much socket errors (i.e. packets failed to be sent).
-    - packets dropped and their internal statistics cumulatively accounted in drop stat.
+    - packets dropped and their internal statistics cumulatively accounted in
+      drop stat.
   cberr: how much connection refused ICMP errors we got from export target.
     - probably you not launched collector software on destination,
     - or specified wrong destination address.
@@ -225,20 +367,34 @@ Troubleshooting:
       packet is for new flow but maxflows is already reached,
       all flows in export packets that got socket error.
 
-> sock0: 10.0.0.2:2055, sndbuf 106496, filled 0, peak 106848; err: sndbuf reached 928, other 0
+> Natevents disabled, count start 0, stop 0.
+
+    - Natevents mode disabled or enabled, and how much start or stop events
+      are reported.
+
+> sock0: 10.0.0.2:2055 unconnected (1 attempts).
+
+  If socket is unconnected (for example if module loaded before interfaces is
+  up) it shows now much connection attempts was failed. It will try to connect
+  until success.
+
+> sock0: 10.0.0.2:2055, sndbuf 106496, filled 0, peak 106848; err: sndbuf reached 928, connect 0, other 0
 
   sockX: per destination stats for:
   X.X.X.X:Y: destination ip address and port.
     - controlled by sysctl net.netflow.destination variable.
   sndbuf X: how much data socket can hold in buffers.
     - controlled by sysctl net.netflow.sndbuf variable.
-    - if you have packet drops due to sndbuf reached (error -11) increase this value.
+    - if you have packet drops due to sndbuf reached (error -11) increase this
+      value.
   filled X: how much data in socket buffers right now.
   peak X: peak value of how much data in socket buffers was.
     - you will be interested to keep it below sndbuf value.
   err: how much packets are dropped due to errors.
     - all flows from them will be accounted in drop stat.
-  sndbuf reached X: how much packets dropped due to sndbuf being too small (error -11).
+  sndbuf reached X: how much packets dropped due to sndbuf being too small
+      (error -11).
+  connect X: how much connection attempts was failed.
   other X: dropped due to other possible errors.
 
 > aggr0: ...
diff --git a/README.promisc b/README.promisc
index 60ca922..31d774f 100644
--- a/README.promisc
+++ b/README.promisc
@@ -2,9 +2,14 @@ Hello,
 
 If you wish to account with netflow module traffic mirrored on switch you may follow this example:
 
-**************
-* Solution 1 *
-**************
+
+   Solution 1: General kernel patch.
+   Solution 2: Alternative w/o kernel patch.
+
+
+    **************
+    * Solution 1 *
+    **************
 
 1. Patch your kernel with `raw_promisc.patch' to enable raw table to see promisc traffic.
 
@@ -33,17 +38,7 @@ If you wish to account with netflow module traffic mirrored on switch you may fo
  # /sbin/vconfig add eth1 47
  # /sbin/ifconfig eth1.47 up
 
-5. Recompile ipt_netflow module with #define RAW_PROMISC_HACK uncommented:
-
- Find this line in ipt_NETFLOW.c (should be line 7):
-
-//#define RAW_PROMISC_HACK
-
- And remove two slashes at beginning of the line, so it become like this:
-
-#define RAW_PROMISC_HACK
-
- Re-compile module:
+5. Compile module:
 
  # make clean all install
 
@@ -55,13 +50,14 @@ If you wish to account with netflow module traffic mirrored on switch you may fo
 
  # /sbin/iptables -A PREROUTING -t raw -i eth1.47 -j NETFLOW
 
-
 Voila.
 
+ps. For Debian Squeeze instructions look at raw_promisc_debian_squeeze6.patch
 
-**************
-* Solution 2 *
-**************
+
+    **************
+    * Solution 2 *
+    **************
 
 By Anonymous.
 
@@ -81,4 +77,3 @@ Sometimes you may need to run:
 
 for this scheme to work.
 
-
diff --git a/configure b/configure
index 677dd7f..3f10e2a 100755
--- a/configure
+++ b/configure
@@ -3,7 +3,7 @@
 PATH=$PATH:/bin:/usr/bin:/usr/sbin:/sbin:/usr/local/sbin
 
 error() {
-  echo "! Error: $@"
+  echo -e "! Error: $@"
   exit 1
 }
 
@@ -56,19 +56,20 @@ get_lib_from_lib() {
 }
 
 iptables_inc() {
-  echo -n "Iptables include path: "
+  echo -n "Iptables include flags: "
   if [ "$IPTINC" ]; then
-    echo "$IPTINC (user specified)"
     IPTINC="-I$IPTINC"
+    echo "$IPTINC (user specified)"
+  elif [ "$PKGVER" ]; then
+    IPTINC="$PKGINC"
+    echo "$IPTINC (pkg-config)"
+  elif [ "$NOIPTSRC" ]; then
+    IPTINC=
+    echo "none (default)"
   else
-    if [ "$PKGINC" ]; then
-      IPTINC="$PKGINC"
-      echo "$IPTINC (pkg-config)"
-    else
-      IPTINC="$IPTSRC/include"
-      echo "$IPTINC (from source)"
-      IPTINC="-I$IPTINC"
-    fi
+    IPTINC="$IPTSRC/include"
+    IPTINC="-I$IPTINC"
+    echo "$IPTINC (from source)"
   fi
 }
 
@@ -109,7 +110,16 @@ try_dir2() {
   test -d "$1" && try_dir `dirname $1` && return 0
 }
 
-iptables_ver() {
+check_pkg_config() {
+  test "$PKGWARN" && return 1
+  if ! which pkg-config >/dev/null 2>&1; then
+    echo "! You don't have pkg-config, it may be useful to install it."
+    PKGWARN=1
+    return 1
+  fi
+  return 0
+}
+iptables_find_version() {
   echo -n "Iptables binary version: "
   if [ "$IPTVER" ]; then
     echo "$IPTVER (user specified)"
@@ -121,6 +131,7 @@ iptables_ver() {
     else
       echo "no iptables binary found"
     fi
+    check_pkg_config
     PKGVER=`pkg-config --modversion xtables 2>/dev/null`
     if [ "$PKGVER" ]; then
       IPTVER="$PKGVER"
@@ -131,44 +142,90 @@ iptables_ver() {
   fi
 }
 
-iptables_dir() {
-  test "$IPTINC" && return 1
-  test "$PKGINC" && return 1
-
-  VER="iptables-$IPTVER"
-  if [ "$IPTSRC" ]; then
-    echo "User specified source directory: $IPTSRC"
-    try_dir $IPTSRC || error "Specified directory is not iptables source.."
+compile_libitp_test() {
+  echo -n "Checking for presence of $@... "
+  echo "
+#define __EXPORTED_HEADERS__
+#include <$*>" > test.c
+  gcc -c test.c  >/dev/null 2>&1
+  RET=$?
+  if [ $RET = 0 ]; then
+    echo Yes;
   else
-    echo "Searching for $VER sources.."
-    try_dir "./$VER" && return 0
-    try_dir "../$VER" && return 0
-    try_dir "/usr/src/$VER" && return 0
-    try_dirg "iptables" && return 0
-    try_dirg "../iptables" && return 0
-    try_dirg "/usr/src/iptables" && return 0
-    try_dir2 `locate $VER/extensions | head -1` && return 0
-    error "Can not find iptables source directory, try setting it with --ipt-src="
+    echo No;
   fi
+  rm -f test.c test.o
+  return $RET
 }
 
-iptables_pkg_config() {
+iptables_try_pkgconfig() {
   if [ ! "$PKGVER" ]; then
+    check_pkg_config
+    PKGVER=`pkg-config --modversion xtables 2>/dev/null`
+    TRYPKGVER=`pkg-config --modversion xtables 2>/dev/null`
     echo -n "pkg-config for version $IPTVER exists: "
-    PKGVER=`pkg-config --exact-version=$IPTVER --modversion xtables 2>/dev/null`
+    pkg-config --exact-version=$IPTVER xtables 2>/dev/null
     if [ $? = 0 ]; then
       echo "Yes"
+      PKGVER=$TRYPKGVER
     else
-      echo "No (reported: $PKGVER)"
-      unset PKGVER
+      if [ "$TRYPKGVER" ]; then
+	echo "No (reported: $TRYPKGVER)"
+      else
+	echo "No"
+      fi
     fi
   fi
   if [ "$PKGVER" ]; then
+    check_pkg_config
+    PKGVER=`pkg-config --modversion xtables 2>/dev/null`
     PKGINC=`pkg-config --cflags xtables`
     PKGLIB=`pkg-config --variable=xtlibdir xtables`
-    IPTCFLAGS="-DXTABLES"
   else
-    IPTCFLAGS="-DIPTABLES_VERSION=\\\\\"$IPTVER\\\\\""
+    # Newer versions of iptables should not have -I/kernel/include!
+    # So I assume that newer version will have correct pkg-config set up
+    # and if not, then it's older who need it.
+    IPTCFLAGS="-I$KDIR/include -DIPTABLES_VERSION=\\\\\"$IPTVER\\\\\""
+  fi
+  if compile_libitp_test xtables.h; then
+    IPTCFLAGS="-DXTABLES $IPTCFLAGS"
+  elif ! compile_libitp_test iptables.h; then
+    echo "! Iptables headers not found. You may need to specify --ipt-inc=..."
+    if [ -s /etc/debian_version ]; then
+      echo "! "
+      echo "! Under Debian simply run this:"
+      echo "!   root# apt-get install iptables-dev pkg-config"
+    elif [ -s /etc/redhat-release ]; then
+      echo "! "
+      arch=.`uname -m`
+      echo "! Under Centos simply run this:"
+      echo "!   root# yum install iptables-devel$arch pkgconfig"
+    fi
+    exit 1
+  fi
+
+}
+
+iptables_find_src() {
+  test "$IPTINC" && return 1
+  test "$PKGVER" && return 1
+
+  VER="iptables-$IPTVER"
+  if [ "$IPTSRC" ]; then
+    echo "User specified source directory: $IPTSRC"
+    try_dir $IPTSRC || error "Specified directory is not iptables source.."
+  else
+    echo "Searching for $VER sources.."
+    try_dir "./$VER" && return 0
+    try_dir "../$VER" && return 0
+    try_dir "/usr/src/$VER" && return 0
+    try_dirg "iptables" && return 0
+    try_dirg "../iptables" && return 0
+    try_dirg "/usr/src/iptables" && return 0
+    try_dir2 `locate $VER/extensions 2>/dev/null | head -1` && return 0
+    echo "! Can not find iptables source directory, you may try setting it with --ipt-src="
+    echo "! This is not fatal error, yet. Will be just using default include dir."
+    NOIPTSRC=1
   fi
 }
 
@@ -206,18 +263,110 @@ do
   esac
 done
 
-test "$KVERSION" || KVERSION=`uname -r`
-echo Kernel version: $KVERSION
+kernel_find_version() {
+  KHOW=requested
+  test "$KVERSION" && return 0
+
+  if grep -q '#.*Debian' /proc/version; then
+    KHOW=proc
+    KVERSION=`sed -n 's/.*#.*Debian \([0-9\.]\+\)-.*/\1/p' /proc/version`
+    KLIBMOD=`uname -r`
+  else
+    KHOW=uname
+    KVERSION=`uname -r`
+  fi
+  test "$KDIR" || return 0
+
+  test -s $KDIR/Makefile || return 1
+  test -s $KDIR/include/config/kernel.release || return 1
+  KVERSION=`cat $KDIR/include/config/kernel.release`
+  KHOW=sources
+}
+
+kernel_check_src() {
+  if [ -s "$1/Makefile" ]; then
+    KDIR="$1"
+    return 0
+  fi
+  return 1
+}
+
+kernel_find_source() {
+  KSHOW=requested
+  test "$KDIR" && return 0
+  KSHOW=found
+  kernel_check_src /lib/modules/$KLIBMOD/build && return 0
+  kernel_check_src /lib/modules/$KVERSION/build && return 0
+  kernel_check_src /usr/src/kernels/$KVERSION && return 0
+  kernel_check_src /usr/src/linux-$KVERSION && return 0
+  echo "! Linux source not found. Don't panic. You may specify kernel source"
+  echo "! directory with --kdir=..., or try to install kernel-devel package,"
+  echo "! or just raw sources for linux-$KVERSION from kernel.org."
+  if grep -q -i centos /proc/version 2>/dev/null; then
+    echo "! "
+    arch=.`uname -m`
+    echo "! Under Centos simply run this:"
+    echo "!   root# yum install kernel-devel iptables-devel$arch pkgconfig"
+  fi
+  if grep -q -i debian /proc/version 2>/dev/null; then
+    echo "! "
+    echo "! Under Debian simply run this:"
+    echo "!   root# apt-get install module-assistant iptables-dev pkg-config"
+    echo "!   root# m-a prepare"
+  fi
+  exit 1
+}
+
+kernel_check_consistency() {
+  if test -s $KDIR/include/config/kernel.release; then
+    SRCVER=`cat $KDIR/include/config/kernel.release`
+    test "$KVERSION" != "$SRCVER" && error "$KHOW kernel version ($KVERSION) and $KSHOW version of kernel source ($SRCVER) doesn't match!\n!" \
+      "You may try to specify only kernel source tree with --kdir=$KDIR\n!" \
+      "and configure will pick up version properly."
+  else
+    test -e "$KDIR/.config" || error ".config in kernel source not found, run  make menuconfig  in $KDIR"
+    test -d "$KDIR/include/config" || error "kernel is not prepared, run  make prepare modules_prepare  in $KDIR"
+  fi
+}
+
+kconfig() {
+  KCONFIG=$KDIR/.config
+  if ! grep -q "^$1=" $KCONFIG 2>/dev/null; then
+    if [ "$KCONFIGREPORTED" != true ]; then
+      KCONFIGREPORTED=true
+      echo Kernel config file checked: $KCONFIG
+      echo
+    fi
+    echo "! Attention: $1 is undefined in your kernel configuration"
+    echo "!   Without this option enabled $2 will not work."
+    echo
+  fi
+}
+
+kernel_check_config() {
+  kconfig CONFIG_SYSCTL			"sysctl interface"
+  kconfig CONFIG_PROC_FS		"proc interface"
+  kconfig CONFIG_NF_NAT_NEEDED		"natevents"
+  kconfig CONFIG_NF_CONNTRACK_EVENTS	"natevents"
+  kconfig CONFIG_NF_CONNTRACK_MARK	"connmark tracking"
+  kconfig CONFIG_IPV6			"IPv6"
+  kconfig CONFIG_IP6_NF_IPTABLES	"ip6tables target"
+}
 
-test "$KDIR" || KDIR=/lib/modules/$KVERSION/build
-echo Kernel sources: $KDIR
+kernel_find_version	#KVERSION
+test "$KLIBMOD" || KLIBMOD=$KVERSION
+echo "Kernel version: $KVERSION ($KHOW)"
+kernel_find_source	#KDIR
+echo "Kernel sources: $KDIR ($KSHOW)"
+kernel_check_consistency
+kernel_check_config
 
 test "$IPTBIN" || IPTBIN=`which iptables`
 
-iptables_ver		#IPTVER
-iptables_pkg_config
-iptables_dir		#IPTSRC
-iptables_src_version	#check IPTSRC match to IPTVER
+iptables_find_version	#IPTVER
+iptables_try_pkgconfig	#try to configure from pkg-config
+iptables_find_src	#IPTSRC
+iptables_src_version	#check that IPTSRC match to IPTVER
 iptables_inc		#IPTINC
 iptables_modules	#IPTLIB
 
@@ -225,7 +374,6 @@ REPLACE="\
 s!@KVERSION@!$KVERSION!;\
 s!@KDIR@!$KDIR!;\
 s!@IPTABLES_VERSION@!$IPTVER!;\
-s!@IPTABLES_INCLUDES@!$IPTINC!;\
 s!@IPTABLES_CFLAGS@!$IPTCFLAGS $IPTINC!;\
 s!@IPTABLES_MODULES@!$IPTLIB!"
 
diff --git a/ipt_NETFLOW.c b/ipt_NETFLOW.c
index d4c91e1..ad974c5 100644
--- a/ipt_NETFLOW.c
+++ b/ipt_NETFLOW.c
@@ -1,6 +1,6 @@
 /*
  * This is NetFlow exporting module (NETFLOW target) for linux
- * (c) 2008-2012 <abc@telekom.ru>
+ * (c) 2008-2013 <abc@telekom.ru>
  *
  *
  *   This program is free software: you can redistribute it and/or modify
@@ -18,8 +18,6 @@
  *
  */
 
-//#define RAW_PROMISC_HACK
-
 #include <linux/module.h>
 #include <linux/skbuff.h>
 #include <linux/proc_fs.h>
@@ -31,16 +29,26 @@
 #include <linux/icmp.h>
 #include <linux/igmp.h>
 #include <linux/inetdevice.h>
-#include <linux/jhash.h>
+#include <linux/hash.h>
+#include <linux/delay.h>
+#include <linux/spinlock_types.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/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
@@ -74,41 +82,66 @@
 #define ipt_target xt_target
 #endif
 
-#define IPT_NETFLOW_VERSION "1.8"
+#define IPT_NETFLOW_VERSION "1.8.2" /* 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("<abc@telekom.ru>");
 MODULE_DESCRIPTION("iptables NETFLOW target module");
 MODULE_VERSION(IPT_NETFLOW_VERSION);
+MODULE_ALIAS("ip6t_NETFLOW");
 
 #define DST_SIZE 256
 static char destination_buf[DST_SIZE] = "127.0.0.1:2055";
 static char *destination = destination_buf;
-module_param(destination, charp, 0400);
+module_param(destination, charp, 0444);
 MODULE_PARM_DESC(destination, "export destination ipaddress:port");
 
 static int inactive_timeout = 15;
-module_param(inactive_timeout, int, 0600);
+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, 0600);
+module_param(active_timeout, int, 0644);
 MODULE_PARM_DESC(active_timeout, "active flows timeout in seconds");
 
 static int debug = 0;
-module_param(debug, int, 0600);
+module_param(debug, int, 0644);
 MODULE_PARM_DESC(debug, "debug verbosity level");
 
 static int sndbuf;
-module_param(sndbuf, int, 0400);
+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)");
+
+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)");
+
+#ifdef CONFIG_NF_NAT_NEEDED
+static int natevents = 0;
+module_param(natevents, int, 0444);
+MODULE_PARM_DESC(natevents, "send NAT Events");
+#endif
+
 static int hashsize;
-module_param(hashsize, int, 0400);
+module_param(hashsize, int, 0444);
 MODULE_PARM_DESC(hashsize, "hash table size");
 
 static int maxflows = 2000000;
-module_param(maxflows, int, 0600);
+module_param(maxflows, int, 0644);
 MODULE_PARM_DESC(maxflows, "maximum number of flows");
 static int peakflows = 0;
 static unsigned long peakflows_at;
@@ -121,22 +154,52 @@ 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 DEFINE_MUTEX(sock_lock);
 
+#define LOCK_COUNT (1<<8)
+#define LOCK_COUNT_MASK (LOCK_COUNT-1)
+static spinlock_t htable_locks[LOCK_COUNT] = {
+	[0 ... LOCK_COUNT - 1] = __SPIN_LOCK_UNLOCKED(htable_locks)
+};
+static DEFINE_RWLOCK(htable_rwlock); /* global lock to protect htable_locks change */
 static unsigned int ipt_netflow_hash_rnd;
-struct hlist_head *ipt_netflow_hash __read_mostly; /* hash table memory */
+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); /* should almost always be locked w/o _bh */
 static LIST_HEAD(aggr_n_list);
 static LIST_HEAD(aggr_p_list);
 static DEFINE_RWLOCK(aggr_lock);
+#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 DEFINE_SPINLOCK(ipt_netflow_lock); /* hash table lock */
 
-static long long pdu_packets = 0, pdu_traf = 0;
-static struct netflow5_pdu pdu;
-static unsigned long pdu_ts_mod;
+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);
@@ -146,19 +209,26 @@ 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 = 10, min15_metric = 10, min5_metric = 10, min_metric = 10; /* hash metrics */
+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 void netflow_scan_and_export(int flush);
+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);
@@ -175,28 +245,46 @@ static inline int mask2bits(__be32 mask) {
 /* under that lock worker is always stopped and not rescheduled,
  * and we can call worker sub-functions manually */
 static DEFINE_MUTEX(worker_lock);
-static inline void __start_scan_worker(void)
+#define MIN_DELAY 1
+#define MAX_DELAY (HZ / 10)
+static int worker_delay = HZ / 10;
+static inline void _schedule_scan_worker(const int status)
 {
-	schedule_delayed_work(&netflow_work, HZ / 10);
+	/* 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)
+/* This is only called soon after pause_scan_worker. */
+static inline void cont_scan_worker(void)
 {
-	__start_scan_worker();
+	_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 __stop_scan_worker(void)
+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)
+/* This is only used for quick pause (in procctl). */
+static inline void pause_scan_worker(void)
 {
 	mutex_lock(&worker_lock);
-	__stop_scan_worker();
+	_unschedule_scan_worker();
 }
 
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
@@ -223,11 +311,14 @@ static int nf_seq_show(struct seq_file *seq, void *v)
 	int snum = 0;
 	int peak = (jiffies - peakflows_at) / HZ;
 
-	seq_printf(seq, "Flows: active %u (peak %u reached %ud%uh%um ago), mem %uK\n",
+	seq_printf(seq, "ipt_NETFLOW version " IPT_NETFLOW_VERSION ", srcversion %s\n",
+	    THIS_MODULE->srcversion);
+	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));
+		   (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);
@@ -252,93 +343,123 @@ static int nf_seq_show(struct seq_file *seq, void *v)
 	}
 
 #define FFLOAT(x, prec) (int)(x) / prec, (int)(x) % prec
-	seq_printf(seq, "Hash: size %u (mem %uK), metric %d.%d, %d.%d, %d.%d, %d.%d. 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, 10),
-		   FFLOAT(min_metric, 10),
-		   FFLOAT(min5_metric, 10),
-		   FFLOAT(min15_metric, 10),
-		   pkt_total - pkt_out + pdu_packets,
-		   (traf_total - traf_out + pdu_traf) >> 10,
-		   pdu_packets,
-		   pdu_traf);
-
-	seq_printf(seq, "Timeout: 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, trunc frag alloc maxflows>, sock: <ok fail cberr, bytes>, traffic: <pkt, bytes>, drop: <pkt, bytes>\n");
-
-	seq_printf(seq, "Total stat: %6llu %6llu %6llu, %4u %4u %4u %4u, sock: %6u %u %u, %llu K, traffic: %llu, %llu MB, drop: %llu, %llu K\n",
-		   (unsigned long long)searched,
-		   (unsigned long long)found,
-		   (unsigned long long)notfound,
-		   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);
+	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), Out %llu pkt, %llu K.\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,
+	    pkt_out,
+	    traf_out >> 10);
+
+	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, %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,
-				   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);
+			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);
 		}
 	}
 
-	read_lock(&sock_lock);
+	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);
+
+#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
+
+	mutex_lock(&sock_lock);
 	list_for_each_entry(usock, &usock_list, list) {
-		struct sock *sk = usock->sock->sk;
-
-		seq_printf(seq, "sock%d: %u.%u.%u.%u:%u, sndbuf %u, filled %u, peak %u; err: sndbuf reached %u, other %u\n",
-			   snum,
-			   usock->ipaddr >> 24,
-			   (usock->ipaddr >> 16) & 255,
-			   (usock->ipaddr >> 8) & 255,
-			   usock->ipaddr & 255,
-			   usock->port,
-			   sk->sk_sndbuf,
-			   atomic_read(&sk->sk_wmem_alloc),
-			   atomic_read(&usock->wmem_peak),
-			   atomic_read(&usock->err_full),
-			   atomic_read(&usock->err_other));
+		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);
+	mutex_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\n",
-			   snum,
-			   HIPQUAD(aggr_n->addr),
-			   mask2bits(aggr_n->mask),
-			   mask2bits(aggr_n->aggr_mask));
+		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\n",
-			   snum,
-			   aggr_p->port1,
-			   aggr_p->port2,
-			   aggr_p->aggr_port);
+		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);
@@ -367,8 +488,13 @@ static struct file_operations nf_seq_fops = {
 #define BEFORE2632(x,y)
 #endif
 
+/* PAX need to know that we are allowed to write */
+#ifndef CONSTIFY_PLUGIN
+#define ctl_table_no_const ctl_table
+#endif
+
 /* sysctl /proc/sys/net/netflow */
-static int hsize_procctl(ctl_table *ctl, int write, BEFORE2632(struct file *filp,)
+static int hsize_procctl(ctl_table_no_const *ctl, int write, BEFORE2632(struct file *filp,)
 			 void __user *buffer, size_t *lenp, loff_t *fpos)
 {
 	void *orig = ctl->data;
@@ -386,20 +512,21 @@ static int hsize_procctl(ctl_table *ctl, int write, BEFORE2632(struct file *filp
 		return ret;
 }
 
-static int sndbuf_procctl(ctl_table *ctl, int write, BEFORE2632(struct file *filp,)
+static int sndbuf_procctl(ctl_table_no_const *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);
+
+	mutex_lock(&sock_lock);
 	if (list_empty(&usock_list)) {
-		read_unlock(&sock_lock);
+		mutex_unlock(&sock_lock);
 		return -ENOENT;
 	}
 	usock = list_first_entry(&usock_list, struct ipt_netflow_sock, list);
-	sndbuf = usock->sock->sk->sk_sndbuf;
-	read_unlock(&sock_lock);
+	if (usock->sock)
+		sndbuf = usock->sock->sk->sk_sndbuf;
+	mutex_unlock(&sock_lock);
 
 	ctl->data = &sndbuf;
 	ret = proc_dointvec(ctl, write, BEFORE2632(filp,) buffer, lenp, fpos);
@@ -407,13 +534,14 @@ static int sndbuf_procctl(ctl_table *ctl, int write, BEFORE2632(struct file *fil
 		return ret;
 	if (sndbuf < SOCK_MIN_SNDBUF)
 		sndbuf = SOCK_MIN_SNDBUF;
-	stop_scan_worker();
-	write_lock(&sock_lock);
+	pause_scan_worker();
+	mutex_lock(&sock_lock);
 	list_for_each_entry(usock, &usock_list, list) {
-		usock->sock->sk->sk_sndbuf = sndbuf;
+		if (usock->sock)
+			usock->sock->sk->sk_sndbuf = sndbuf;
 	}
-	write_unlock(&sock_lock);
-	start_scan_worker();
+	mutex_unlock(&sock_lock);
+	cont_scan_worker();
 	return ret;
 }
 
@@ -424,10 +552,10 @@ static int destination_procctl(ctl_table *ctl, int write, BEFORE2632(struct file
 
 	ret = proc_dostring(ctl, write, BEFORE2632(filp,) buffer, lenp, fpos);
 	if (ret >= 0 && write) {
-		stop_scan_worker();
+		pause_scan_worker();
 		destination_removeall();
 		add_destinations(destination_buf);
-		start_scan_worker();
+		cont_scan_worker();
 	}
 	return ret;
 }
@@ -446,13 +574,12 @@ static int aggregation_procctl(ctl_table *ctl, int write, BEFORE2632(struct file
 	return ret;
 }
 
-static int flush_procctl(ctl_table *ctl, int write, BEFORE2632(struct file *filp,)
+static int flush_procctl(ctl_table_no_const *ctl, int write, BEFORE2632(struct file *filp,)
 			 void __user *buffer, size_t *lenp, loff_t *fpos)
 {
 	int ret;
-	int val;
+	int val = 0;
 
-	val = 0;
 	ctl->data = &val;
 	ret = proc_dointvec(ctl, write, BEFORE2632(filp,) buffer, lenp, fpos);
 
@@ -461,14 +588,67 @@ static int flush_procctl(ctl_table *ctl, int write, BEFORE2632(struct file *filp
 
 	if (val > 0) {
 		printk(KERN_INFO "ipt_NETFLOW: forced flush\n");
-		stop_scan_worker();
-		netflow_scan_and_export(1);
-		start_scan_worker();
+		pause_scan_worker();
+		netflow_scan_and_export(AND_FLUSH);
+		cont_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");
+			pause_scan_worker();
+			netflow_scan_and_export(AND_FLUSH);
+			netflow_switch_version(ver);
+			cont_scan_worker();
+			break;
+		default:
+			return -EPERM;
 	}
 
 	return ret;
 }
 
+#ifdef CONFIG_NF_NAT_NEEDED
+static void register_ct_events(void);
+static void unregister_ct_events(void);
+static int natevents_procctl(ctl_table *ctl, int write, BEFORE2632(struct file *filp,)
+			 void __user *buffer, size_t *lenp, loff_t *fpos)
+{
+	int ret;
+	int val = natevents;
+
+	ctl->data = &val;
+	ret = proc_dointvec(ctl, write, BEFORE2632(filp,) buffer, lenp, fpos);
+
+	if (!write)
+		return ret;
+
+	if (natevents && !val)
+		unregister_ct_events();
+	else if (!natevents && val)
+		register_ct_events();
+
+	return ret;
+}
+#endif
+
 static struct ctl_table_header *netflow_sysctl_header;
 
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
@@ -547,6 +727,38 @@ static struct ctl_table netflow_sysctl_table[] = {
 		.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(13)
+		.procname	= "natevents",
+		.mode		= 0644,
+		.maxlen		= sizeof(int),
+		.proc_handler	= &natevents_procctl,
+	},
+#endif
 	{ }
 };
 
@@ -588,18 +800,69 @@ static struct ctl_path netflow_sysctl_path[] = {
 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 "NETFLOW: socket error <%d>\n", sk->sk_err);
+		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(const __be32 ipaddr, const 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, const 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 int netflow_send_pdu(void *buffer, int len)
+static void netflow_sendmsg(void *buffer, const int len)
 {
 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL };
 	struct kvec iov = { buffer, len };
@@ -607,9 +870,16 @@ static int netflow_send_pdu(void *buffer, int len)
 	int snum = 0;
 	struct ipt_netflow_sock *usock;
 
+	mutex_lock(&sock_lock);
 	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_send_pdu: sendmsg(%d, %d) [%u %u]\n",
+			printk(KERN_INFO "netflow_sendmsg: sendmsg(%d, %d) [%u %u]\n",
 			       snum,
 			       len,
 			       atomic_read(&usock->sock->sk->sk_wmem_alloc),
@@ -624,7 +894,7 @@ static int netflow_send_pdu(void *buffer, int len)
 				suggestion = ": increase sndbuf!";
 			} else
 				atomic_inc(&usock->err_other);
-			printk(KERN_ERR "netflow_send_pdu[%d]: sendmsg error %d: data loss %llu pkt, %llu bytes%s\n",
+			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);
@@ -636,98 +906,67 @@ static int netflow_send_pdu(void *buffer, int len)
 		}
 		snum++;
 	}
-	return retok;
+	mutex_unlock(&sock_lock);
+	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_free(struct ipt_netflow_sock *usock)
+static void usock_close_free(struct ipt_netflow_sock *usock)
 {
-	printk(KERN_INFO "netflow: remove destination %u.%u.%u.%u:%u (%p)\n",
+	printk(KERN_INFO "ipt_NETFLOW: removed destination %u.%u.%u.%u:%u\n",
 	       HIPQUAD(usock->ipaddr),
-	       usock->port,
-	       usock->sock);
+	       usock->port);
 	if (usock->sock)
 		sock_release(usock->sock);
 	usock->sock = NULL;
-	vfree(usock); 
+	vfree(usock);
 }
 
 static void destination_removeall(void)
 {
-	write_lock(&sock_lock);
+	mutex_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_free(usock);
-		write_lock(&sock_lock);
+		mutex_unlock(&sock_lock);
+		usock_close_free(usock);
+		mutex_lock(&sock_lock);
 	}
-	write_unlock(&sock_lock);
+	mutex_unlock(&sock_lock);
 }
 
 static void add_usock(struct ipt_netflow_sock *usock)
 {
 	struct ipt_netflow_sock *sk;
 
-	/* don't need empty sockets */
-	if (!usock->sock) {
-		usock_free(usock);
-		return;
-	}
-
-	write_lock(&sock_lock);
+	mutex_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_free(usock);
+			mutex_unlock(&sock_lock);
+			usock_close_free(usock);
 			return;
 		}
 	}
 	list_add_tail(&usock->list, &usock_list);
-	printk(KERN_INFO "netflow: added destination %u.%u.%u.%u:%u\n",
+	printk(KERN_INFO "ipt_NETFLOW: added destination %u.%u.%u.%u:%u%s\n",
 	       HIPQUAD(usock->ipaddr),
-	       usock->port);
-	write_unlock(&sock_lock);
-}
-
-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 "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 "netflow: error connecting UDP socket %d\n", error);
-		sock_release(sock);
-		return NULL;
-	}
-	return sock;
+	       usock->port,
+	       (!usock->sock)? " (unconnected)" : "");
+	mutex_unlock(&sock_lock);
 }
 
 #define SEPARATORS " ,;\t\n"
 static int add_destinations(char *ptr)
 {
 	while (ptr) {
-		unsigned char ip[4]; 
+		unsigned char ip[4];
 		unsigned short port;
 
 		ptr += strspn(ptr, SEPARATORS);
@@ -737,17 +976,15 @@ static int add_destinations(char *ptr)
 			struct ipt_netflow_sock *usock;
 
 			if (!(usock = vmalloc(sizeof(*usock)))) {
-				printk(KERN_ERR "netflow: can't vmalloc socket\n");
+				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->sock = usock_alloc(usock->ipaddr, port);
-			atomic_set(&usock->wmem_peak, 0);
-			atomic_set(&usock->err_full, 0);
-			atomic_set(&usock->err_other, 0);
+			usock_connect(usock, 0);
 			add_usock(usock);
 		} else
 			break;
@@ -781,7 +1018,7 @@ static int add_aggregation(char *ptr)
 	LIST_HEAD(old_aggr_list);
 
 	while (ptr && *ptr) {
-		unsigned char ip[4]; 
+		unsigned char ip[4];
 		unsigned int mask;
 		unsigned int port1, port2;
 		unsigned int aggr_to;
@@ -792,16 +1029,16 @@ static int add_aggregation(char *ptr)
 			   ip, ip + 1, ip + 2, ip + 3, &mask, &aggr_to) == 6) {
 
 			if (!(aggr_n = vmalloc(sizeof(*aggr_n)))) {
-				printk(KERN_ERR "netflow: can't vmalloc aggr\n");
+				printk(KERN_ERR "ipt_NETFLOW: can't vmalloc aggr\n");
 				return -ENOMEM;
 			}
 			memset(aggr_n, 0, sizeof(*aggr_n));
 
-			aggr_n->addr = ntohl(*(__be32 *)ip);
 			aggr_n->mask = bits2mask(mask);
+			aggr_n->addr = ntohl(*(__be32 *)ip) & aggr_n->mask;
 			aggr_n->aggr_mask = bits2mask(aggr_to);
 			aggr_n->prefix = mask;
-			printk(KERN_INFO "netflow: add aggregation [%u.%u.%u.%u/%u=%u]\n",
+			printk(KERN_INFO "ipt_NETFLOW: add aggregation [%u.%u.%u.%u/%u=%u]\n",
 			       HIPQUAD(aggr_n->addr), mask, aggr_to);
 			list_add_tail(&aggr_n->list, &new_aggr_n_list);
 
@@ -809,7 +1046,7 @@ static int add_aggregation(char *ptr)
 			   sscanf(ptr, "%u=%u", &port2, &aggr_to) == 2) {
 
 			if (!(aggr_p = vmalloc(sizeof(*aggr_p)))) {
-				printk(KERN_ERR "netflow: can't vmalloc aggr\n");
+				printk(KERN_ERR "ipt_NETFLOW: can't vmalloc aggr\n");
 				return -ENOMEM;
 			}
 			memset(aggr_p, 0, sizeof(*aggr_p));
@@ -817,11 +1054,11 @@ static int add_aggregation(char *ptr)
 			aggr_p->port1 = port1;
 			aggr_p->port2 = port2;
 			aggr_p->aggr_port = aggr_to;
-			printk(KERN_INFO "netflow: add aggregation [%u-%u=%u]\n",
+			printk(KERN_INFO "ipt_NETFLOW: add aggregation [%u-%u=%u]\n",
 			       port1, port2, aggr_to);
 			list_add_tail(&aggr_p->list, &new_aggr_p_list);
 		} else {
-			printk(KERN_ERR "netflow: bad aggregation rule: %s (ignoring)\n", ptr);
+			printk(KERN_ERR "ipt_NETFLOW: bad aggregation rule: %s (ignoring)\n", ptr);
 			break;
 		}
 
@@ -846,17 +1083,23 @@ static int add_aggregation(char *ptr)
 
 static inline u_int32_t hash_netflow(const struct ipt_netflow_tuple *tuple)
 {
-	/* tuple is rounded to u32s */
-	return jhash2((u32 *)tuple, NETFLOW_TUPLE_SIZE, ipt_netflow_hash_rnd) % ipt_netflow_hash_size;
+	return murmur3(tuple, sizeof(struct ipt_netflow_tuple), ipt_netflow_hash_rnd) % ipt_netflow_hash_size;
 }
 
 static struct ipt_netflow *
-ipt_netflow_find(const struct ipt_netflow_tuple *tuple, unsigned int hash)
+ipt_netflow_find(const struct ipt_netflow_tuple *tuple, const unsigned int hash)
 {
 	struct ipt_netflow *nf;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
+#define compat_hlist_for_each_entry		      hlist_for_each_entry
+#define compat_hlist_for_each_entry_safe	      hlist_for_each_entry_safe
 	struct hlist_node *pos;
+#else /* since 3.9.0 */
+#define compat_hlist_for_each_entry(a,pos,c,d)	      hlist_for_each_entry(a,c,d)
+#define compat_hlist_for_each_entry_safe(a,pos,c,d,e) hlist_for_each_entry_safe(a,c,d,e)
+#endif
 
-	hlist_for_each_entry(nf, pos, &ipt_netflow_hash[hash], hlist) {
+	compat_hlist_for_each_entry(nf, pos, &ipt_netflow_hash[hash], hlist) {
 		if (ipt_netflow_tuple_equal(tuple, &nf->tuple) &&
 		    nf->nr_bytes < FLOW_FULL_WATERMARK) {
 			NETFLOW_STAT_INC(found);
@@ -868,7 +1111,7 @@ ipt_netflow_find(const struct ipt_netflow_tuple *tuple, unsigned int hash)
 	return NULL;
 }
 
-static struct hlist_head *alloc_hashtable(int size)
+static struct hlist_head *alloc_hashtable(const int size)
 {
 	struct hlist_head *hash;
 
@@ -879,19 +1122,18 @@ static struct hlist_head *alloc_hashtable(int size)
 		for (i = 0; i < size; i++)
 			INIT_HLIST_HEAD(&hash[i]);
 	} else
-		printk(KERN_ERR "netflow: unable to vmalloc hash table.\n");
+		printk(KERN_ERR "ipt_NETFLOW: unable to vmalloc hash table.\n");
 
 	return hash;
 }
 
-static int set_hashsize(int new_size)
+static int set_hashsize(const int new_size)
 {
 	struct hlist_head *new_hash, *old_hash;
-	unsigned int hash;
 	struct ipt_netflow *nf;
 	int rnd;
 
-	printk(KERN_INFO "netflow: allocating new hash table %u -> %u buckets\n",
+	printk(KERN_INFO "ipt_NETFLOW: allocating new hash table %u -> %u buckets\n",
 	       ipt_netflow_hash_size, new_size);
 	new_hash = alloc_hashtable(new_size);
 	if (!new_hash)
@@ -900,19 +1142,24 @@ static int set_hashsize(int new_size)
 	get_random_bytes(&rnd, 4);
 
 	/* rehash */
-	spin_lock_bh(&ipt_netflow_lock);
+	write_lock_bh(&htable_rwlock);
 	old_hash = ipt_netflow_hash;
 	ipt_netflow_hash = new_hash;
 	ipt_netflow_hash_size = new_size;
 	ipt_netflow_hash_rnd = rnd;
 	/* hash_netflow() is dependent on ipt_netflow_hash_* values */
+	spin_lock(&hlist_lock);
 	list_for_each_entry(nf, &ipt_netflow_list, list) {
+		unsigned int hash;
+
 		hash = hash_netflow(&nf->tuple);
 		/* hlist_add_head overwrites hlist pointers for this node
 		 * so it's good */
 		hlist_add_head(&nf->hlist, &new_hash[hash]);
+		nf->lock = &htable_locks[hash & LOCK_COUNT_MASK];
 	}
-	spin_unlock_bh(&ipt_netflow_lock);
+	spin_unlock(&hlist_lock);
+	write_unlock_bh(&htable_rwlock);
 
 	vfree(old_hash);
 
@@ -920,14 +1167,14 @@ static int set_hashsize(int new_size)
 }
 
 static struct ipt_netflow *
-ipt_netflow_alloc(struct ipt_netflow_tuple *tuple)
+ipt_netflow_alloc(const struct ipt_netflow_tuple *tuple)
 {
 	struct ipt_netflow *nf;
 	long count;
 
 	nf = kmem_cache_alloc(ipt_netflow_cachep, GFP_ATOMIC);
 	if (!nf) {
-		printk(KERN_ERR "Can't allocate netflow.\n");
+		printk(KERN_ERR "ipt_NETFLOW: Can't allocate flow.\n");
 		return NULL;
 	}
 
@@ -945,13 +1192,15 @@ ipt_netflow_alloc(struct ipt_netflow_tuple *tuple)
 
 static void ipt_netflow_free(struct ipt_netflow *nf)
 {
+	if (IS_DUMMY_FLOW(nf))
+		return;
 	atomic_dec(&ipt_netflow_count);
 	kmem_cache_free(ipt_netflow_cachep, nf);
 }
 
 static struct ipt_netflow *
-init_netflow(struct ipt_netflow_tuple *tuple,
-	     struct sk_buff *skb, unsigned int hash)
+init_netflow(const struct ipt_netflow_tuple *tuple,
+	     const struct sk_buff *skb, const unsigned int hash)
 {
 	struct ipt_netflow *nf;
 
@@ -959,93 +1208,774 @@ init_netflow(struct ipt_netflow_tuple *tuple,
 	if (!nf)
 		return NULL;
 
+	nf->lock = &htable_locks[hash & LOCK_COUNT_MASK];
 	hlist_add_head(&nf->hlist, &ipt_netflow_hash[hash]);
+	spin_lock(&hlist_lock);
 	list_add(&nf->list, &ipt_netflow_list);
+	spin_unlock(&hlist_lock);
 
 	return nf;
 }
 
 /* cook pdu, send, and clean */
 /* only called in scan worker path */
-static void netflow_export_pdu(void)
+static void netflow_export_pdu_v5(void)
 {
 	struct timeval tv;
 	int pdusize;
 
-	if (!pdu.nr_records)
+	if (!pdu_data_records)
 		return;
 
 	if (debug > 1)
-		printk(KERN_INFO "netflow_export_pdu with %d records\n", pdu.nr_records);
-	do_gettimeofday(&tv);
-
-	pdu.version	= htons(5);
-	pdu.ts_uptime	= htonl(jiffies_to_msecs(jiffies));
-	pdu.ts_usecs	= htonl(tv.tv_sec);
-	pdu.ts_unsecs	= htonl(tv.tv_usec);
-	//pdu.eng_type	= 0;
-	//pdu.eng_id	= 0;
-	//pdu.padding	= 0;
+		printk(KERN_INFO "netflow_export_pdu_v5 with %d records\n", pdu_data_records);
 
-	pdusize = NETFLOW5_HEADER_SIZE + sizeof(struct netflow5_record) * pdu.nr_records;
-
-	/* especially fix nr_records before export */
-	pdu.nr_records	= htons(pdu.nr_records);
+	pdu.v5.version		= htons(5);
+	pdu.v5.nr_records	= htons(pdu_data_records);
+	pdu.v5.ts_uptime	= htonl(jiffies_to_msecs(jiffies));
+	do_gettimeofday(&tv);
+	pdu.v5.ts_usecs		= htonl(tv.tv_sec);
+	pdu.v5.ts_unsecs	= htonl(tv.tv_usec);
+	pdu.v5.seq		= htonl(pdu_seq);
+	//pdu.v5.eng_type	= 0;
+	pdu.v5.eng_id		= engine_id;
+	//pdu.v5.padding	= 0;
 
-	if (netflow_send_pdu(&pdu, pdusize) == 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);
-	}
+	pdusize = NETFLOW5_HEADER_SIZE + sizeof(struct netflow5_record) * pdu_data_records;
 
-	pdu.seq = htonl(ntohl(pdu.seq) + ntohs(pdu.nr_records));
+	netflow_sendmsg(&pdu.v5, pdusize);
 
-	pdu.nr_records	= 0;
 	pdu_packets = 0;
-	pdu_traf = 0;
+	pdu_traf    = 0;
+
+	pdu_seq += pdu_data_records;
+	pdu_count++;
+	pdu_data_records = 0;
 }
 
 /* only called in scan worker path */
-static void netflow_export_flow(struct ipt_netflow *nf)
+static void netflow_export_flow_v5(struct ipt_netflow *nf)
 {
 	struct netflow5_record *rec;
 
-	if (debug > 2)
-		printk(KERN_INFO "adding flow to export (%d)\n", pdu.nr_records);
+	if (unlikely(debug > 2))
+		printk(KERN_INFO "adding flow to export (%d)\n", pdu_data_records);
 
 	pdu_packets += nf->nr_packets;
 	pdu_traf += nf->nr_bytes;
 	pdu_ts_mod = jiffies;
-	rec = &pdu.flow[pdu.nr_records++];
+	rec = &pdu.v5.flow[pdu_data_records++];
 
 	/* make V5 flow record */
-	rec->s_addr	= nf->tuple.s_addr;
-	rec->d_addr	= nf->tuple.d_addr;
-	//rec->nexthop	= 0;
+	rec->s_addr	= nf->tuple.src.ip;
+	rec->d_addr	= nf->tuple.dst.ip;
+	rec->nexthop	= nf->nh.ip;
 	rec->i_ifc	= htons(nf->tuple.i_ifc);
 	rec->o_ifc	= htons(nf->o_ifc);
 	rec->nr_packets = htonl(nf->nr_packets);
 	rec->nr_octets	= htonl(nf->nr_bytes);
-	rec->ts_first	= htonl(jiffies_to_msecs(nf->ts_first));
-	rec->ts_last	= htonl(jiffies_to_msecs(nf->ts_last));
+	rec->first_ms	= htonl(jiffies_to_msecs(nf->ts_first));
+	rec->last_ms	= htonl(jiffies_to_msecs(nf->ts_last));
 	rec->s_port	= nf->tuple.s_port;
 	rec->d_port	= nf->tuple.d_port;
-	//rec->reserved	= 0;
+	//rec->reserved	= 0; /* pdu is always zeroized for v5 in netflow_switch_version */
 	rec->tcp_flags	= nf->tcp_flags;
 	rec->protocol	= nf->tuple.protocol;
 	rec->tos	= nf->tuple.tos;
-	//rec->s_as	= 0;
-	//rec->d_as	= 0;
+#ifdef CONFIG_NF_NAT_NEEDED
+	rec->s_as	= nf->s_as;
+	rec->d_as	= nf->d_as;
+#endif
 	rec->s_mask	= nf->s_mask;
 	rec->d_mask	= nf->d_mask;
 	//rec->padding	= 0;
 	ipt_netflow_free(nf);
 
-	if (pdu.nr_records == NETFLOW5_RECORDS_MAX)
+	if (pdu_data_records == NETFLOW5_RECORDS_MAX)
+		netflow_export_pdu_v5();
+}
+
+/* pdu is initially blank, export current pdu, and prepare next for filling. */
+static void netflow_export_pdu_v9(void)
+{
+	struct timeval tv;
+	int pdusize;
+
+	if (pdu_data_used <= pdu.v9.data)
+		return;
+
+	if (debug > 1)
+		printk(KERN_INFO "netflow_export_pdu_v9 with %d records\n",
+		    pdu_data_records + pdu_tpl_records);
+
+	pdu.v9.version		= htons(9);
+	pdu.v9.nr_records	= htons(pdu_data_records + pdu_tpl_records);
+	pdu.v9.sys_uptime_ms	= htonl(jiffies_to_msecs(jiffies));
+	do_gettimeofday(&tv);
+	pdu.v9.export_time_s	= htonl(tv.tv_sec);
+	pdu.v9.seq		= htonl(pdu_seq);
+	pdu.v9.source_id	= engine_id;
+
+	pdusize = pdu_data_used - (unsigned char *)&pdu.v9;
+
+	netflow_sendmsg(&pdu.v9, pdusize);
+
+	pdu_packets = 0;
+	pdu_traf    = 0;
+
+	pdu_seq++;
+	pdu_count++;
+	pdu_data_records = pdu_tpl_records = 0;
+	pdu_data_used = pdu.v9.data;
+	pdu_flowset = NULL;
+}
+
+static void netflow_export_pdu_ipfix(void)
+{
+	struct timeval tv;
+	int pdusize;
+
+	if (pdu_data_used <= pdu.ipfix.data)
+		return;
+
+	if (debug > 1)
+		printk(KERN_INFO "netflow_export_pduX with %d records\n",
+		    pdu_data_records);
+
+	pdu.ipfix.version	= htons(10);
+	do_gettimeofday(&tv);
+	pdu.ipfix.export_time_s	= htonl(tv.tv_sec);
+	pdu.ipfix.seq		= htonl(pdu_seq);
+	pdu.ipfix.odomain_id	= engine_id;
+	pdusize = pdu_data_used - (unsigned char *)&pdu;
+	pdu.ipfix.length	= htons(pdusize);
+
+	netflow_sendmsg(&pdu.ipfix, pdusize);
+
+	pdu_packets = 0;
+	pdu_traf    = 0;
+
+	pdu_seq += pdu_data_records;
+	pdu_count++;
+	pdu_data_records = pdu_tpl_records = 0;
+	pdu_data_used = pdu.ipfix.data;
+	pdu_flowset = NULL;
+}
+
+static inline int pdu_have_space(const size_t size)
+{
+	return ((pdu_data_used + size) <= pdu_high_wm);
+}
+
+static inline unsigned char *pdu_grab_space(const size_t size)
+{
+	unsigned char *ptr = pdu_data_used;
+	pdu_data_used += size;
+	return ptr;
+}
+
+// allocate data space in pdu, or fail if pdu is reallocated.
+static inline unsigned char *pdu_alloc_fail(const size_t size)
+{
+	if (!pdu_have_space(size)) {
 		netflow_export_pdu();
+		return NULL;
+	}
+	return pdu_grab_space(size);
+}
+
+/* doesn't fail, but can provide empty pdu. */
+static unsigned char *pdu_alloc(const size_t size)
+{
+	return pdu_alloc_fail(size) ?: pdu_grab_space(size);
+}
+
+/* global table of sizes of template field types */
+static u_int8_t tpl_element_sizes[] = {
+	[IN_BYTES]	= 4,
+	[IN_PKTS]	= 4,
+	[PROTOCOL]	= 1,
+	[TOS]		= 1,
+	[TCP_FLAGS]	= 1,
+	[L4_SRC_PORT]	= 2,
+	[IPV4_SRC_ADDR]	= 4,
+	[SRC_MASK]	= 1,
+	[INPUT_SNMP]	= 2,
+	[L4_DST_PORT]	= 2,
+	[IPV4_DST_ADDR]	= 4,
+	[DST_MASK]	= 1,
+	[OUTPUT_SNMP]	= 2,
+	[IPV4_NEXT_HOP]	= 4,
+	//[SRC_AS]		= 2,
+	//[DST_AS]		= 2,
+	//[BGP_IPV4_NEXT_HOP]	= 4,
+	//[MUL_DST_PKTS]	= 4,
+	//[MUL_DST_BYTES]	= 4,
+	[LAST_SWITCHED]	= 4,
+	[FIRST_SWITCHED]= 4,
+	[IPV6_SRC_ADDR]	= 16,
+	[IPV6_DST_ADDR]	= 16,
+	[IPV6_FLOW_LABEL] = 3,
+	[ICMP_TYPE]	= 2,
+	[MUL_IGMP_TYPE]	= 1,
+	//[TOTAL_BYTES_EXP]	= 4,
+	//[TOTAL_PKTS_EXP]	= 4,
+	//[TOTAL_FLOWS_EXP]	= 4,
+	[IPV6_NEXT_HOP]			   = 16,
+	[IPV6_OPTION_HEADERS]		   = 2,
+	[commonPropertiesId]		   = 4,
+	[ipv4Options]			   = 4,
+	[tcpOptions]			   = 4,
+	[postNATSourceIPv4Address]	   = 4,
+	[postNATDestinationIPv4Address]	   = 4,
+	[postNAPTSourceTransportPort]	   = 2,
+	[postNAPTDestinationTransportPort] = 2,
+	[natEvent]			   = 1,
+	[postNATSourceIPv6Address]	   = 16,
+	[postNATDestinationIPv6Address]	   = 16,
+	[IPSecSPI]			   = 4,
+	[observationTimeMilliseconds]	   = 8,
+	[observationTimeMicroseconds]	   = 8,
+	[observationTimeNanoseconds]	   = 8,
+};
+
+#define TEMPLATES_HASH_BSIZE	8
+#define TEMPLATES_HASH_SIZE	(1<<TEMPLATES_HASH_BSIZE)
+static struct hlist_head templates_hash[TEMPLATES_HASH_SIZE];
+
+struct base_template {
+	int length; /* number of elements in template */
+	u_int16_t types[]; /* {type, size} pairs */
+};
+
+/* base templates */
+#define BTPL_BASE	0x00000001	/* base stat */
+#define BTPL_IP4	0x00000002	/* IPv4 */
+#define BTPL_MASK4	0x00000004	/* Aggregated */
+#define BTPL_PORTS	0x00000008	/* UDP&TCP */
+#define BTPL_IP6	0x00000010	/* IPv6 */
+#define BTPL_ICMP	0x00000020	/* ICMP */
+#define BTPL_IGMP	0x00000040	/* IGMP */
+#define BTPL_IPSEC	0x00000080	/* AH&ESP */
+#define BTPL_NAT4	0x00000100	/* NAT IPv4 */
+#define BTPL_MARK	0x00000400	/* connmark */
+#define BTPL_LABEL6	0x00000800	/* IPv6 flow label */
+#define BTPL_OPTIONS4	0x00001000	/* IPv4 Options */
+#define BTPL_OPTIONS6	0x00002000	/* IPv6 Options */
+#define BTPL_TCPOPTIONS	0x00004000	/* TCP Options */
+#define BTPL_MAX	32
+
+static struct base_template template_base = {
+	.types = {
+		INPUT_SNMP,
+		OUTPUT_SNMP,
+		IN_PKTS,
+		IN_BYTES,
+		FIRST_SWITCHED,
+		LAST_SWITCHED,
+		PROTOCOL,
+		TOS,
+		0
+	}
+};
+static struct base_template template_ipv4 = {
+	.types = {
+		IPV4_SRC_ADDR,
+		IPV4_DST_ADDR,
+		IPV4_NEXT_HOP,
+		0
+	}
+};
+static struct base_template template_options4 = {
+	.types = { ipv4Options, 0 }
+};
+static struct base_template template_tcpoptions = {
+	.types = { tcpOptions, 0 }
+};
+static struct base_template template_ipv6 = {
+	.types = {
+		IPV6_SRC_ADDR,
+		IPV6_DST_ADDR,
+		IPV6_NEXT_HOP,
+		0
+	}
+};
+static struct base_template template_options6 = {
+	.types = { IPV6_OPTION_HEADERS, 0 }
+};
+static struct base_template template_label6 = {
+	.types = { IPV6_FLOW_LABEL, 0 }
+};
+static struct base_template template_ipv4_mask = {
+	.types = {
+		SRC_MASK,
+		DST_MASK,
+		0
+	}
+};
+static struct base_template template_ports = {
+	.types = {
+		L4_SRC_PORT,
+		L4_DST_PORT,
+		TCP_FLAGS,
+		0
+	}
+};
+static struct base_template template_icmp = {
+	.types = { ICMP_TYPE, 0 }
+};
+static struct base_template template_igmp = {
+	.types = { MUL_IGMP_TYPE, 0 }
+};
+static struct base_template template_ipsec = {
+	.types = { IPSecSPI, 0 }
+};
+static struct base_template template_nat4 = {
+	.types = {
+		observationTimeMilliseconds,
+		IPV4_SRC_ADDR,
+		IPV4_DST_ADDR,
+		postNATSourceIPv4Address,
+		postNATDestinationIPv4Address,
+		L4_SRC_PORT,
+		L4_DST_PORT,
+		postNAPTSourceTransportPort,
+		postNAPTDestinationTransportPort,
+		PROTOCOL,
+		natEvent,
+		0
+	}
+};
+static struct base_template template_mark = {
+	.types = { commonPropertiesId, 0 }
+};
+
+struct data_template {
+	struct hlist_node hlist;
+	int tpl_mask;
+
+	int length; /* number of elements in template */
+	int tpl_size; /* summary size of template with flowset header */
+	int rec_size; /* summary size of all recods of template (w/o flowset header) */
+	int template_id_n; /* assigned from template_ids, network order. */
+	int		exported_cnt;
+	unsigned long	exported_ts; /* jiffies */
+	u_int16_t fields[]; /* {type, size} pairs */
+} __attribute__ ((packed));
+
+#define TPL_FIELD_NSIZE 4 /* one complete template field's network size */
+
+static void free_templates(void)
+{
+	int i;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
+	struct hlist_node *pos;
+#endif
+	struct hlist_node *tmp;
+
+	for (i = 0; i < TEMPLATES_HASH_SIZE; i++) {
+		struct hlist_head *thead = &templates_hash[i];
+		struct data_template *tpl;
+
+		compat_hlist_for_each_entry_safe(tpl, pos, tmp, thead, hlist)
+			kfree(tpl);
+		INIT_HLIST_HEAD(thead);
+	}
+	tpl_count = 0;
+}
+
+/* create combined template from mask */
+static struct data_template *get_template(const int tmask)
+{
+	struct base_template *tlist[BTPL_MAX];
+	struct data_template *tpl;
+	int tnum;
+	int length;
+	int i, j, k;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
+	struct hlist_node *pos;
+#endif
+	int hash = hash_long(tmask, TEMPLATES_HASH_BSIZE);
+
+	compat_hlist_for_each_entry(tpl, pos, &templates_hash[hash], hlist)
+		if (tpl->tpl_mask == tmask)
+			return tpl;
+
+	tnum = 0;
+	if (tmask & BTPL_IP4) {
+		tlist[tnum++] = &template_ipv4;
+		if (tmask & BTPL_OPTIONS4)
+			tlist[tnum++] = &template_options4;
+		if (tmask & BTPL_MASK4)
+			tlist[tnum++] = &template_ipv4_mask;
+	} else if (tmask & BTPL_IP6) {
+		tlist[tnum++] = &template_ipv6;
+		if (tmask & BTPL_LABEL6)
+			tlist[tnum++] = &template_label6;
+		if (tmask & BTPL_OPTIONS6)
+			tlist[tnum++] = &template_options6;
+	} else if (tmask & BTPL_NAT4)
+		tlist[tnum++] = &template_nat4;
+	if (tmask & BTPL_PORTS)
+		tlist[tnum++] = &template_ports;
+	if (tmask & BTPL_BASE)
+		tlist[tnum++] = &template_base;
+	if (tmask & BTPL_TCPOPTIONS)
+		tlist[tnum++] = &template_tcpoptions;
+	if (tmask & BTPL_ICMP)
+		tlist[tnum++] = &template_icmp;
+	if (tmask & BTPL_IGMP)
+		tlist[tnum++] = &template_igmp;
+	if (tmask & BTPL_IPSEC)
+		tlist[tnum++] = &template_ipsec;
+	if (tmask & BTPL_MARK)
+		tlist[tnum++] = &template_mark;
+
+	/* calc memory size */
+	length = 0;
+	for (i = 0; i < tnum; i++) {
+		if (!tlist[i]->length) {
+			for (k = 0; tlist[i]->types[k]; k++);
+			tlist[i]->length = k;
+		}
+		length += tlist[i]->length;
+	}
+	/* elements are pairs + one termiantor */
+	tpl = kmalloc(sizeof(struct data_template) + (length * 2 + 1) * sizeof(u_int16_t), GFP_KERNEL);
+	if (!tpl) {
+		printk(KERN_ERR "ipt_NETFLOW: unable to kmalloc template.\n");
+		return NULL;
+	}
+	tpl->tpl_mask = tmask;
+	tpl->length = length;
+	tpl->tpl_size = sizeof(struct flowset_template);
+	tpl->rec_size = 0;
+	tpl->template_id_n = htons(template_ids++);
+	tpl->exported_cnt = 0;
+	tpl->exported_ts = 0;
+
+	j = 0;
+	for (i = 0; i < tnum; i++) {
+		struct base_template *btpl = tlist[i];
+
+		for (k = 0; k < btpl->length; k++) {
+			int size;
+			int type = btpl->types[k];
+
+			tpl->fields[j++] = type;
+			size = tpl_element_sizes[type];
+			tpl->fields[j++] = size;
+			tpl->rec_size += size;
+		}
+		tpl->tpl_size += btpl->length * TPL_FIELD_NSIZE;
+	}
+	tpl->fields[j++] = 0;
+
+	hlist_add_head(&tpl->hlist, &templates_hash[hash]);
+	tpl_count++;
+
+	return tpl;
+}
+
+static void pdu_add_template(struct data_template *tpl)
+{
+	int i;
+	unsigned char *ptr;
+	struct flowset_template *ntpl;
+	__be16 *sptr;
+
+	ptr = pdu_alloc(tpl->tpl_size);
+	ntpl = (struct flowset_template *)ptr;
+	ntpl->flowset_id  = protocol == 9? htons(FLOWSET_TEMPLATE) : htons(IPFIX_TEMPLATE);
+	ntpl->length	  = htons(tpl->tpl_size);
+	ntpl->template_id = tpl->template_id_n;
+	ntpl->field_count = htons(tpl->length);
+	ptr += sizeof(struct flowset_template);
+	sptr = (__be16 *)ptr;
+	for (i = 0; ; ) {
+		int type = tpl->fields[i++];
+		if (!type)
+			break;
+		*sptr++ = htons(type);
+		*sptr++ = htons(tpl->fields[i++]);
+	}
+
+	tpl->exported_cnt = pdu_count;
+	tpl->exported_ts = jiffies;
+
+	pdu_flowset = NULL;
+	pdu_tpl_records++;
+}
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
+static inline s64 portable_ktime_to_ms(const ktime_t kt)
+{
+	struct timeval tv = ktime_to_timeval(kt);
+	return (s64) tv.tv_sec * MSEC_PER_SEC + tv.tv_usec / USEC_PER_MSEC;
+}
+#define ktime_to_ms portable_ktime_to_ms
+#endif
+
+/* encode one field */
+typedef struct in6_addr in6_t;
+static inline void add_ipv4_field(__u8 *ptr, const int type, const struct ipt_netflow *nf)
+{
+	switch (type) {
+		case IN_BYTES:	     *(__be32 *)ptr = htonl(nf->nr_bytes); break;
+		case IN_PKTS:	     *(__be32 *)ptr = htonl(nf->nr_packets); break;
+		case FIRST_SWITCHED: *(__be32 *)ptr = htonl(jiffies_to_msecs(nf->ts_first)); break;
+		case LAST_SWITCHED:  *(__be32 *)ptr = htonl(jiffies_to_msecs(nf->ts_last)); break;
+		case IPV4_SRC_ADDR:  *(__be32 *)ptr = nf->tuple.src.ip; break;
+		case IPV4_DST_ADDR:  *(__be32 *)ptr = nf->tuple.dst.ip; break;
+		case IPV4_NEXT_HOP:  *(__be32 *)ptr = nf->nh.ip; break;
+		case L4_SRC_PORT:    *(__be16 *)ptr = nf->tuple.s_port; break;
+		case L4_DST_PORT:    *(__be16 *)ptr = nf->tuple.d_port; break;
+		case INPUT_SNMP:     *(__be16 *)ptr = htons(nf->tuple.i_ifc); break;
+		case OUTPUT_SNMP:    *(__be16 *)ptr = htons(nf->o_ifc); break;
+		case PROTOCOL:	               *ptr = nf->tuple.protocol; break;
+		case TCP_FLAGS:	               *ptr = nf->tcp_flags; break;
+		case TOS:	               *ptr = nf->tuple.tos; break;
+		case IPV6_SRC_ADDR:   *(in6_t *)ptr = nf->tuple.src.in6; break;
+		case IPV6_DST_ADDR:   *(in6_t *)ptr = nf->tuple.dst.in6; break;
+		case IPV6_NEXT_HOP:   *(in6_t *)ptr = nf->nh.in6; break;
+		case IPV6_FLOW_LABEL:        *ptr++ = nf->flow_label >> 16;
+				     *(__be16 *)ptr = nf->flow_label;
+				      break;
+		case tcpOptions:     *(__be32 *)ptr = htonl(nf->tcpoptions); break;
+		case ipv4Options:    *(__be32 *)ptr = htonl(nf->options); break;
+		case IPV6_OPTION_HEADERS: *(__be16 *)ptr = htons(nf->options); break;
+#ifdef CONFIG_NF_CONNTRACK_MARK
+		case commonPropertiesId:
+				     *(__be32 *)ptr = htonl(nf->mark); break;
+#endif
+		case SRC_MASK:	               *ptr = nf->s_mask; break;
+		case DST_MASK:	               *ptr = nf->d_mask; break;
+		case ICMP_TYPE:	     *(__be16 *)ptr = nf->tuple.d_port; break;
+		case MUL_IGMP_TYPE:            *ptr = nf->tuple.d_port; break;
+#ifdef CONFIG_NF_NAT_NEEDED
+		case postNATSourceIPv4Address:	       *(__be32 *)ptr = nf->nat->post.s_addr; break;
+		case postNATDestinationIPv4Address:    *(__be32 *)ptr = nf->nat->post.d_addr; break;
+		case postNAPTSourceTransportPort:      *(__be16 *)ptr = nf->nat->post.s_port; break;
+		case postNAPTDestinationTransportPort: *(__be16 *)ptr = nf->nat->post.d_port; break;
+		case natEvent:				         *ptr = nf->nat->nat_event; break;
+#endif
+		case IPSecSPI:        *(__u32 *)ptr = (nf->tuple.s_port << 16) | nf->tuple.d_port; break;
+		case observationTimeMilliseconds:
+				      *(__be64 *)ptr = cpu_to_be64(ktime_to_ms(nf->ts_obs)); break;
+		case observationTimeMicroseconds:
+				      *(__be64 *)ptr = cpu_to_be64(ktime_to_us(nf->ts_obs)); break;
+		case observationTimeNanoseconds:
+				      *(__be64 *)ptr = cpu_to_be64(ktime_to_ns(nf->ts_obs)); break;
+		default:
+					memset(ptr, 0, tpl_element_sizes[type]);
+	}
+}
+
+#define PAD_SIZE 4 /* rfc prescribes flowsets to be padded */
+
+/* cache timeout_rate in jiffies */
+static inline unsigned long timeout_rate_j(void)
+{
+	static unsigned int t_rate = 0;
+	static unsigned long t_rate_j = 0;
+
+	if (unlikely(timeout_rate != t_rate)) {
+		struct timeval tv = { .tv_sec = timeout_rate * 60, .tv_usec = 0 };
+
+		t_rate = timeout_rate;
+		t_rate_j = timeval_to_jiffies(&tv);
+	}
+	return t_rate_j;
+}
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
+#define IPPROTO_UDPLITE 136
+#endif
+
+#ifndef time_is_before_jiffies
+#define time_is_before_jiffies(a) time_after(jiffies, a)
+#endif
+
+static void netflow_export_flow_tpl(struct ipt_netflow *nf)
+{
+	unsigned char *ptr;
+	int i;
+	struct data_template *tpl;
+	int tpl_mask = BTPL_BASE;
+
+	if (unlikely(debug > 2))
+		printk(KERN_INFO "adding flow to export (%d)\n",
+		    pdu_data_records + pdu_tpl_records);
+
+	if (likely(nf->tuple.l3proto == AF_INET)) {
+		tpl_mask |= BTPL_IP4;
+		if (unlikely(nf->options))
+			tpl_mask |= BTPL_OPTIONS4;
+	} else {
+		tpl_mask |= BTPL_IP6;
+		if (unlikely(nf->options))
+			tpl_mask |= BTPL_OPTIONS6;
+		if (unlikely(nf->flow_label))
+			tpl_mask |= BTPL_LABEL6;
+	}
+	if (unlikely(nf->tcpoptions))
+		tpl_mask |= BTPL_TCPOPTIONS;
+	if (unlikely(nf->s_mask || nf->d_mask))
+		tpl_mask |= BTPL_MASK4;
+	if (likely(nf->tuple.protocol == IPPROTO_TCP ||
+		    nf->tuple.protocol == IPPROTO_UDP ||
+		    nf->tuple.protocol == IPPROTO_SCTP ||
+		    nf->tuple.protocol == IPPROTO_UDPLITE))
+		tpl_mask |= BTPL_PORTS;
+	else if (nf->tuple.protocol == IPPROTO_ICMP)
+		tpl_mask |= BTPL_ICMP;
+	else if (nf->tuple.protocol == IPPROTO_IGMP)
+		tpl_mask |= BTPL_IGMP;
+#ifdef CONFIG_NF_CONNTRACK_MARK
+	if (nf->mark)
+		tpl_mask |= BTPL_MARK;
+#endif
+#ifdef CONFIG_NF_NAT_NEEDED
+	if (nf->nat)
+		tpl_mask = BTPL_NAT4;
+#endif
+
+	tpl = get_template(tpl_mask);
+	if (unlikely(!tpl)) {
+		printk(KERN_INFO "ipt_NETFLOW: template allocation failed.\n");
+		NETFLOW_STAT_INC(alloc_err);
+		NETFLOW_STAT_ADD_ATOMIC(pkt_drop, nf->nr_packets);
+		NETFLOW_STAT_ADD_ATOMIC(traf_drop, nf->nr_bytes);
+		ipt_netflow_free(nf);
+		return;
+	}
+
+	if (unlikely(!pdu_flowset ||
+	    pdu_flowset->flowset_id != tpl->template_id_n ||
+	    !(ptr = pdu_alloc_fail(tpl->rec_size)))) {
+
+		/* if there was previous data template we should pad it to 4 bytes */
+		if (pdu_flowset) {
+			int padding = (PAD_SIZE - ntohs(pdu_flowset->length) % PAD_SIZE) % PAD_SIZE;
+			if (padding && (ptr = pdu_alloc_fail(padding))) {
+				pdu_flowset->length = htons(ntohs(pdu_flowset->length) + padding);
+				for (; padding; padding--)
+					*ptr++ = 0;
+			}
+		}
+
+		if (!tpl->exported_ts ||
+		    pdu_count > (tpl->exported_cnt + refresh_rate) ||
+		    time_is_before_jiffies(tpl->exported_ts + timeout_rate_j())) {
+			pdu_add_template(tpl);
+		}
+
+		ptr = pdu_alloc(sizeof(struct flowset_data) + tpl->rec_size);
+		pdu_flowset = (struct flowset_data *)ptr;
+		pdu_flowset->flowset_id = tpl->template_id_n;
+		pdu_flowset->length     = htons(sizeof(struct flowset_data));
+		ptr += sizeof(struct flowset_data);
+	}
+
+	/* encode all fields */
+	for (i = 0; ; ) {
+		int type = tpl->fields[i++];
+
+		if (!type)
+			break;
+		add_ipv4_field(ptr, type, nf);
+		ptr += tpl->fields[i++];
+	}
+
+	pdu_data_records++;
+	pdu_flowset->length = htons(ntohs(pdu_flowset->length) + tpl->rec_size);
+
+	pdu_packets += nf->nr_packets;
+	pdu_traf    += nf->nr_bytes;
+
+	ipt_netflow_free(nf);
+	pdu_ts_mod = jiffies;
+}
+
+static void netflow_switch_version(const int ver)
+{
+	protocol = ver;
+	if (protocol == 5) {
+		memset(&pdu, 0, sizeof(pdu));
+		netflow_export_flow = &netflow_export_flow_v5;
+		netflow_export_pdu = &netflow_export_pdu_v5;
+	} else if (protocol == 9) {
+		pdu_data_used = pdu.v9.data;
+		pdu_max_size = sizeof(pdu.v9);
+		pdu_high_wm = (unsigned char *)&pdu + pdu_max_size;
+		netflow_export_flow = &netflow_export_flow_tpl;
+		netflow_export_pdu = &netflow_export_pdu_v9;
+	} else { /* IPFIX */
+		pdu_data_used = pdu.ipfix.data;
+		pdu_max_size = sizeof(pdu.ipfix);
+		pdu_high_wm = (unsigned char *)&pdu + pdu_max_size;
+		netflow_export_flow = &netflow_export_flow_tpl;
+		netflow_export_pdu = &netflow_export_pdu_ipfix;
+	}
+	if (protocol != 5)
+		free_templates();
+	pdu_data_records = pdu_tpl_records = 0;
+	pdu_flowset = NULL;
+	printk(KERN_INFO "ipt_NETFLOW protocol version %d (%s) enabled.\n",
+	    protocol, protocol == 10? "IPFIX" : "NetFlow");
+}
+
+#ifdef CONFIG_NF_NAT_NEEDED
+static void export_nat_event(struct nat_event *nel)
+{
+	static struct ipt_netflow nf = { { NULL } };
+
+	nf.tuple.l3proto = AF_INET;
+	nf.tuple.protocol = nel->protocol;
+	nf.nat = nel; /* this is also flag of dummy flow */
+	nf.tcp_flags = (nel->nat_event == NAT_DESTROY)? TCP_FIN_RST : TCP_SYN_ACK;
+	if (protocol >= 9) {
+		nf.ts_obs = nel->ts_ktime;
+		nf.tuple.src.ip = nel->pre.s_addr;
+		nf.tuple.dst.ip = nel->pre.d_addr;
+		nf.tuple.s_port = nel->pre.s_port;
+		nf.tuple.d_port = nel->pre.d_port;
+		netflow_export_flow(&nf);
+	} else { /* v5 */
+		/* The weird v5 packet(s).
+		 * src and dst will be same as in data flow from the FORWARD chain
+		 * where src is pre-nat src ip and dst is post-nat dst ip.
+		 * What we lacking here is external src ip for SNAT, or
+		 * pre-nat dst ip for DNAT. We will put this into Nexthop field
+		 * with port into src/dst AS field. tcp_flags will distinguish it's
+		 * start or stop event. Two flows in case of full nat. */
+		nf.tuple.src.ip = nel->pre.s_addr;
+		nf.tuple.s_port = nel->pre.s_port;
+		nf.tuple.dst.ip = nel->post.d_addr;
+		nf.tuple.d_port = nel->post.d_port;
+
+		nf.ts_first = nel->ts_jiffies;
+		nf.ts_last = nel->ts_jiffies;
+		if (nel->pre.s_addr != nel->post.s_addr ||
+		    nel->pre.s_port != nel->post.s_port) {
+			nf.nh.ip = nel->post.s_addr;
+			nf.s_as  = nel->post.s_port;
+			nf.d_as  = 0;
+			netflow_export_flow(&nf);
+		}
+		if (nel->pre.d_addr != nel->post.d_addr ||
+		    nel->pre.d_port != nel->post.d_port) {
+			nf.nh.ip = nel->pre.d_addr;
+			nf.s_as  = 0;
+			nf.d_as  = nel->pre.d_port;
+			netflow_export_flow(&nf);
+		}
+	}
+	kfree(nel);
 }
+#endif /* CONFIG_NF_NAT_NEEDED */
 
-static inline int active_needs_export(struct ipt_netflow *nf, long a_timeout)
+static inline int active_needs_export(const struct ipt_netflow *nf, const long a_timeout)
 {
 	/* active too long, finishing, or having too much bytes */
 	return ((jiffies - nf->ts_first) > a_timeout) ||
@@ -1057,42 +1987,77 @@ static inline int active_needs_export(struct ipt_netflow *nf, long a_timeout)
 
 /* could be called with zero to flush cache and pdu */
 /* this function is guaranteed to be called non-concurrently */
-static void netflow_scan_and_export(int flush)
+/* return -1 is trylockfailed, 0 if nothin gexported, >=1 if exported something */
+static int netflow_scan_and_export(const int flush)
 {
 	long i_timeout = inactive_timeout * HZ;
 	long a_timeout = active_timeout * HZ;
+	int trylock_failed = 0;
+	int pdu_c = pdu_count;
 
 	if (flush)
 		i_timeout = 0;
 
-	spin_lock_bh(&ipt_netflow_lock);
-	while (!list_empty(&ipt_netflow_list)) {
+	local_bh_disable();
+	spin_lock(&hlist_lock);
+	/* This is different order of locking than elsewhere,
+	 * so we trylock&break to avoid deadlock. */
+
+	while (likely(!list_empty(&ipt_netflow_list))) {
 		struct ipt_netflow *nf;
-	       
+
+		/* Last entry, which is usually oldest. */
 		nf = list_entry(ipt_netflow_list.prev, struct ipt_netflow, list);
+		if (!spin_trylock(nf->lock)) {
+			trylock_failed = 1;
+			break;
+		}
 		/* Note: i_timeout checked with >= to allow specifying zero timeout
 		 * to purge all flows on module unload */
 		if (((jiffies - nf->ts_last) >= i_timeout) ||
 		    active_needs_export(nf, a_timeout)) {
 			hlist_del(&nf->hlist);
+			spin_unlock(nf->lock);
+
 			list_del(&nf->list);
+			spin_unlock(&hlist_lock);
+			local_bh_enable();
+
 			NETFLOW_STAT_ADD(pkt_out, nf->nr_packets);
 			NETFLOW_STAT_ADD(traf_out, nf->nr_bytes);
-			spin_unlock_bh(&ipt_netflow_lock);
 			netflow_export_flow(nf);
-			spin_lock_bh(&ipt_netflow_lock);
+
+			local_bh_disable();
+			spin_lock(&hlist_lock);
 		} else {
+			spin_unlock(nf->lock);
 			/* all flows which need to be exported is always at the tail
 			 * so if no more exportable flows we can break */
 			break;
 		}
 	}
-	spin_unlock_bh(&ipt_netflow_lock);
-
+	spin_unlock(&hlist_lock);
+	local_bh_enable();
+
+#ifdef CONFIG_NF_NAT_NEEDED
+	spin_lock_bh(&nat_lock);
+	while (!list_empty(&nat_list)) {
+		struct nat_event *nel;
+
+		nel = list_entry(nat_list.next, struct nat_event, list);
+		list_del(&nel->list);
+		spin_unlock_bh(&nat_lock);
+		export_nat_event(nel);
+		spin_lock_bh(&nat_lock);
+	}
+	spin_unlock_bh(&nat_lock);
+#endif
 	/* flush flows stored in pdu if there no new flows for too long */
 	/* Note: using >= to allow flow purge on zero timeout */
 	if ((jiffies - pdu_ts_mod) >= i_timeout)
 		netflow_export_pdu();
+
+	return trylock_failed? -1 : pdu_count - pdu_c;
 }
 
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
@@ -1101,8 +2066,10 @@ static void netflow_work_fn(void *dummy)
 static void netflow_work_fn(struct work_struct *dummy)
 #endif
 {
-	netflow_scan_and_export(0);
-	__start_scan_worker();
+	int status;
+
+	status = netflow_scan_and_export(DONT_FLUSH);
+	_schedule_scan_worker(status);
 }
 
 #define RATESHIFT 2
@@ -1154,7 +2121,7 @@ static void rate_timer_calc(unsigned long dummy)
 	old_found = found;
 	old_notfound = notfound;
 	/* if there is no access to hash keep rate steady */
-	metric = (dfnd + dnfnd)? 10 * (dsrch + dfnd + dnfnd) / (dfnd + dnfnd) : metric;
+	metric = (dfnd + dnfnd)? 100 * (dsrch + dfnd + dnfnd) / (dfnd + dnfnd) : metric;
 	CALC_RATE(min15_metric, (unsigned long long)metric, 15);
 	CALC_RATE(min5_metric, (unsigned long long)metric, 5);
 	CALC_RATE(min_metric, (unsigned long long)metric, 1);
@@ -1162,6 +2129,262 @@ static void rate_timer_calc(unsigned long dummy)
 	mod_timer(&rate_timer, jiffies + (HZ * SAMPLERATE));
 }
 
+#ifdef CONFIG_NF_NAT_NEEDED
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
+static struct nf_ct_event_notifier *saved_event_cb __read_mostly = NULL;
+static int netflow_conntrack_event(const unsigned int events, struct nf_ct_event *item)
+#else
+static int netflow_conntrack_event(struct notifier_block *this, unsigned long events, void *ptr)
+#endif
+{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
+	struct nf_conn *ct = item->ct;
+#else
+	struct nf_conn *ct = (struct nf_conn *)ptr;
+#endif
+	struct nat_event *nel;
+	const struct nf_conntrack_tuple *t;
+	int ret = NOTIFY_DONE;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
+	struct nf_ct_event_notifier *notifier;
+
+	/* Call netlink first. */
+	notifier = rcu_dereference(saved_event_cb);
+	if (likely(notifier))
+		ret = notifier->fcn(events, item);
+#endif
+	if (unlikely(!natevents))
+		return ret;
+
+	if (!(events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED) | (1 << IPCT_DESTROY))))
+		return ret;
+
+	if (!(ct->status & IPS_NAT_MASK))
+		return ret;
+
+	if (unlikely(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num != AF_INET ||
+		    ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num != AF_INET)) {
+		/* Well, there is no linux NAT for IPv6 anyway. */
+		return ret;
+	}
+
+	if (!(nel = kmalloc(sizeof(struct nat_event), GFP_ATOMIC))) {
+		printk(KERN_ERR "ipt_NETFLOW: can't kmalloc nat event\n");
+		return ret;
+	}
+	memset(nel, 0, sizeof(struct nat_event));
+	nel->ts_ktime = ktime_get_real();
+	nel->ts_jiffies = jiffies;
+	t = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
+	nel->protocol = t->dst.protonum;
+	nel->pre.s_addr = t->src.u3.ip;
+	nel->pre.d_addr = t->dst.u3.ip;
+	nel->pre.s_port = t->src.u.all;
+	nel->pre.d_port = t->dst.u.all;
+	t = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
+	/* reply is reversed */
+	nel->post.s_addr = t->dst.u3.ip;
+	nel->post.d_addr = t->src.u3.ip;
+	nel->post.s_port = t->dst.u.all;
+	nel->post.d_port = t->src.u.all;
+	if (events & (1 << IPCT_DESTROY)) {
+		nel->nat_event = NAT_DESTROY;
+		nat_events_stop++;
+	} else {
+		nel->nat_event = NAT_CREATE;
+		nat_events_start++;
+	}
+
+	spin_lock_bh(&nat_lock);
+	list_add_tail(&nel->list, &nat_list);
+	spin_unlock_bh(&nat_lock);
+
+	return ret;
+}
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31)
+static struct notifier_block ctnl_notifier = {
+	.notifier_call = netflow_conntrack_event
+};
+#else
+static struct nf_ct_event_notifier ctnl_notifier = {
+	.fcn = netflow_conntrack_event
+};
+#endif /* since 2.6.31 */
+#endif /* CONFIG_NF_NAT_NEEDED */
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) && \
+    LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
+static bool
+#else
+static int
+#endif
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
+netflow_target_check(const char *tablename, const void *entry, const struct xt_target *target,
+    void *targinfo,
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
+    unsigned int targinfosize,
+#endif
+    unsigned int hook_mask)
+{
+#else
+netflow_target_check(const struct xt_tgchk_param *par)
+{
+	const char *tablename = par->table;
+	const struct xt_target *target = par->target;
+#endif
+	if (strcmp("nat", tablename) == 0) {
+		/* In the nat table we only see single packet per flow, which is useless. */
+		printk(KERN_ERR "%s target: is not valid in %s table\n", target->name, tablename);
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
+#define CHECK_FAIL	0
+#define CHECK_OK	1
+#else
+#define CHECK_FAIL	-EINVAL
+#define CHECK_OK	0
+#endif
+		return CHECK_FAIL;
+	}
+	if (target->family == AF_INET6 && protocol == 5) {
+		printk(KERN_ERR "ip6tables NETFLOW target is meaningful for protocol 9 or 10 only.\n");
+		return CHECK_FAIL;
+	}
+	return CHECK_OK;
+}
+
+#define SetXBit(x) (0x8000 >> (x)) /* Proper bit for htons later. */
+#ifndef IPPROTO_MH
+#define IPPROTO_MH	135
+#endif
+static inline __u16 observed_hdrs(const __u8 currenthdr)
+{
+	switch (currenthdr) {
+	case IPPROTO_TCP:
+	case IPPROTO_UDP:
+		/* For speed, in case switch is not optimized. */
+		return 0;
+	case IPPROTO_DSTOPTS:  return SetXBit(0);
+	case IPPROTO_HOPOPTS:  return SetXBit(1);
+	case IPPROTO_ROUTING:  return SetXBit(5);
+	case IPPROTO_MH:       return SetXBit(12);
+	case IPPROTO_ESP:      return SetXBit(13);
+	case IPPROTO_AH:       return SetXBit(14);
+	case IPPROTO_COMP:     return SetXBit(15);
+	case IPPROTO_FRAGMENT: /* Handled elsewhere. */
+		/* Next is known headers. */
+	case IPPROTO_ICMPV6:
+	case IPPROTO_UDPLITE:
+	case IPPROTO_IPIP:
+	case IPPROTO_PIM:
+	case IPPROTO_GRE:
+	case IPPROTO_SCTP:
+#ifdef IPPROTO_L2TP
+	case IPPROTO_L2TP:
+#endif
+	case IPPROTO_DCCP:
+	       return 0;
+	}
+	return SetXBit(3); /* Unknown header. */
+}
+
+/* http://www.iana.org/assignments/ip-parameters/ip-parameters.xhtml */
+static const __u8 ip4_opt_table[] = {
+	[7]	= 0,	/* RR */ /* parsed manually becasue of 0 */
+	[134]	= 1,	/* CIPSO */
+	[133]	= 2,	/* E-SEC */
+	[68]	= 3,	/* TS */
+	[131]	= 4,	/* LSR */
+	[130]	= 5,	/* SEC */
+	[1]	= 6,	/* NOP */
+	[0]	= 7,	/* EOOL */
+	[15]	= 8,	/* ENCODE */
+	[142]	= 9,	/* VISA */
+	[205]	= 10,	/* FINN */
+	[12]	= 11,	/* MTUR */
+	[11]	= 12,	/* MTUP */
+	[10]	= 13,	/* ZSU */
+	[137]	= 14,	/* SSR */
+	[136]	= 15,	/* SID */
+	[151]	= 16,	/* DPS */
+	[150]	= 17,	/* NSAPA */
+	[149]	= 18,	/* SDB */
+	[147]	= 19,	/* ADDEXT */
+	[148]	= 20,	/* RTRALT */
+	[82]	= 21,	/* TR */
+	[145]	= 22,	/* EIP */
+	[144]	= 23,	/* IMITD */
+	[30]	= 25,	/* EXP */
+	[94]	= 25,	/* EXP */
+	[158]	= 25,	/* EXP */
+	[222]	= 25,	/* EXP */
+	[25]	= 30,	/* QS */
+	[152]	= 31,	/* UMP */
+};
+/* Parse IPv4 Options array int ipv4Options IPFIX value. */
+static inline __u32 ip4_options(const u_int8_t *p, const unsigned int optsize)
+{
+	__u32 ret = 0;
+	unsigned int i;
+
+	for (i = 0; likely(i < optsize); ) {
+		u_int8_t op = p[i++];
+
+		if (op == 7) /* RR: bit 0 */
+			ret |= 1;
+		else if (likely(op < ARRAY_SIZE(ip4_opt_table))) {
+			/* Btw, IANA doc is messed up in a crazy way:
+			 *   http://www.ietf.org/mail-archive/web/ipfix/current/msg06008.html (2011)
+			 * I decided to follow IANA _text_ description from
+			 *   http://www.iana.org/assignments/ipfix/ipfix.xhtml (2013-09-18)
+			 *
+			 * Set proper bit for htonl later. */
+			if (ip4_opt_table[op])
+				ret |= 1 << (32 - ip4_opt_table[op]);
+		}
+		if (likely(i >= optsize || op == 0))
+			break;
+		else if (unlikely(op == 1))
+			continue;
+		else if (unlikely(p[i] < 2))
+			break;
+		else
+			i += p[i] - 1;
+	}
+	return ret;
+}
+
+#define TCPHDR_MAXSIZE (4 * 15)
+/* List of options: http://www.iana.org/assignments/tcp-parameters/tcp-parameters.xhtml */
+static inline __u32 tcp_options(const struct sk_buff *skb, const unsigned int ptr, const struct tcphdr *th)
+{
+	const unsigned int optsize = th->doff * 4 - sizeof(struct tcphdr);
+	__u8 _opt[TCPHDR_MAXSIZE];
+	const u_int8_t *p;
+	__u32 ret;
+	unsigned int i;
+
+	p = skb_header_pointer(skb, ptr + sizeof(struct tcphdr), optsize, _opt);
+	if (unlikely(!p))
+		return 0;
+	ret = 0;
+	for (i = 0; likely(i < optsize); ) {
+		u_int8_t opt = p[i++];
+
+		if (likely(opt < 32)) {
+			/* IANA doc is messed up, see above. */
+			ret |= 1 << (32 - opt);
+		}
+		if (likely(i >= optsize || opt == 0))
+			break;
+		else if (unlikely(opt == 1))
+			continue;
+		else if (unlikely(p[i] < 2)) /* "silly options" */
+			break;
+		else
+			i += p[i] - 1;
+	}
+	return ret;
+}
 /* packet receiver */
 static unsigned int netflow_target(
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
@@ -1192,27 +2415,38 @@ static unsigned int netflow_target(
 		)
 {
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
-	struct sk_buff *skb = *pskb;
+	const struct sk_buff *skb = *pskb;
+#endif
+	union {
+		struct iphdr ip;
+		struct ipv6hdr ip6;
+	} _iph, *iph;
+	unsigned int hash;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
+	const int family = target->family;
+#else
+	const int family = par->family;
 #endif
-	struct iphdr _iph, *iph;
 	struct ipt_netflow_tuple tuple;
 	struct ipt_netflow *nf;
 	__u8 tcp_flags;
 	struct netflow_aggr_n *aggr_n;
 	struct netflow_aggr_p *aggr_p;
 	__u8 s_mask, d_mask;
-	unsigned int hash;
-
-	iph = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); //iph = ip_hdr(skb);
-
-	if (iph == NULL) {
+	unsigned int ptr;
+	int fragment;
+	size_t pkt_len;
+	int options = 0;
+	int tcpoptions = 0;
+
+	iph = skb_header_pointer(skb, 0, (likely(family == AF_INET))? sizeof(_iph.ip) : sizeof(_iph.ip6), &iph);
+	if (unlikely(iph == NULL)) {
 		NETFLOW_STAT_INC(truncated);
 		NETFLOW_STAT_INC(pkt_drop);
 		return IPT_CONTINUE;
 	}
 
-	tuple.s_addr	= iph->saddr;
-	tuple.d_addr	= iph->daddr;
+	tuple.l3proto	= family;
 	tuple.s_port	= 0;
 	tuple.d_port	= 0;
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
@@ -1220,30 +2454,118 @@ static unsigned int netflow_target(
 #else
 	tuple.i_ifc	= par->in? par->in->ifindex : -1;
 #endif
-	tuple.protocol	= iph->protocol;
-	tuple.tos	= iph->tos;
 	tcp_flags	= 0; /* Cisco sometimes have TCP ACK for non TCP packets, don't get it */
 	s_mask		= 0;
 	d_mask		= 0;
 
-	if (iph->frag_off & htons(IP_OFFSET))
+	if (likely(family == AF_INET)) {
+		tuple.src	= (union nf_inet_addr){ .ip = iph->ip.saddr };
+		tuple.dst	= (union nf_inet_addr){ .ip = iph->ip.daddr };
+		tuple.tos	= iph->ip.tos;
+		tuple.protocol	= iph->ip.protocol;
+		fragment	= unlikely(iph->ip.frag_off & htons(IP_OFFSET));
+		ptr		= iph->ip.ihl * 4;
+		pkt_len		= ntohs(iph->ip.tot_len);
+
+#define IPHDR_MAXSIZE (4 * 15)
+		if (unlikely(iph->ip.ihl * 4 > sizeof(struct iphdr))) {
+			u_int8_t _opt[IPHDR_MAXSIZE - sizeof(struct iphdr)];
+			const u_int8_t *op;
+			unsigned int optsize = iph->ip.ihl * 4 - sizeof(struct iphdr);
+
+			op = skb_header_pointer(skb, sizeof(struct iphdr), optsize, _opt);
+			if (likely(op))
+				options = ip4_options(op, optsize);
+		}
+	} else {
+		__u8 currenthdr;
+
+		tuple.src.in6	= iph->ip6.saddr;
+		tuple.dst.in6	= iph->ip6.daddr;
+		tuple.tos	= iph->ip6.priority;
+		fragment	= 0;
+		ptr		= sizeof(struct ipv6hdr);
+		pkt_len		= ntohs(iph->ip6.payload_len) + sizeof(struct ipv6hdr);
+
+		currenthdr	= iph->ip6.nexthdr;
+		while (currenthdr != NEXTHDR_NONE && ipv6_ext_hdr(currenthdr)) {
+			struct ipv6_opt_hdr _hdr;
+			const struct ipv6_opt_hdr *hp;
+			unsigned int hdrlen = 0;
+
+			options |= observed_hdrs(currenthdr);
+			hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
+			if (hp == NULL) {
+				/* We have src/dst, so must account something. */
+				tuple.protocol = currenthdr;
+				fragment = 3;
+				goto do_protocols;
+			}
+
+			switch (currenthdr) {
+			case IPPROTO_FRAGMENT: {
+				struct frag_hdr _fhdr;
+				const struct frag_hdr *fh;
+
+				fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
+						&_fhdr);
+				if (fh == NULL) {
+					tuple.protocol = currenthdr;
+					fragment = 2;
+					goto do_protocols;
+				}
+				fragment = 1;
+#define FRA0 SetXBit(4) /* Fragment header - first fragment */
+#define FRA1 SetXBit(6) /* Fragmentation header - not first fragment */
+				options |= (ntohs(fh->frag_off) & 0xFFF8)? FRA1 : FRA0;
+				hdrlen = 8;
+				break;
+			}
+			case IPPROTO_AH: {
+				struct ip_auth_hdr _hdr, *hp;
+
+				if (likely(hp = skb_header_pointer(skb, ptr, 8, &_hdr))) {
+					tuple.s_port = hp->spi >> 16;
+					tuple.d_port = hp->spi;
+				}
+				hdrlen = (hp->hdrlen + 2) << 2;
+				break;
+			}
+			default:
+				hdrlen = ipv6_optlen(hp);
+			}
+			currenthdr = hp->nexthdr;
+			ptr += hdrlen;
+		}
+		tuple.protocol	= currenthdr;
+		options |= observed_hdrs(currenthdr);
+	}
+
+do_protocols:
+	if (fragment) {
+		/* if conntrack is enabled it should defrag on pre-routing and local-out */
 		NETFLOW_STAT_INC(frags);
-	else {
+	} else {
 		switch (tuple.protocol) {
 		    case IPPROTO_TCP: {
 			struct tcphdr _hdr, *hp;
 
-			if ((hp = skb_header_pointer(skb, iph->ihl * 4, 14, &_hdr))) {
+			if (likely(hp = skb_header_pointer(skb, ptr, 14, &_hdr))) {
 				tuple.s_port = hp->source;
 				tuple.d_port = hp->dest;
 				tcp_flags = (u_int8_t)(ntohl(tcp_flag_word(hp)) >> 16);
+
+				if (unlikely(hp->doff * 4 > sizeof(struct tcphdr)))
+					tcpoptions = tcp_options(skb, ptr, hp);
 			}
 			break;
 		    }
-		    case IPPROTO_UDP: {
+		    case IPPROTO_UDP:
+		    case IPPROTO_UDPLITE:
+		    case IPPROTO_SCTP: {
 			struct udphdr _hdr, *hp;
 
-			if ((hp = skb_header_pointer(skb, iph->ihl * 4, 4, &_hdr))) {
+			if (likely(hp = skb_header_pointer(skb, ptr, 4, &_hdr))) {
 				tuple.s_port = hp->source;
 				tuple.d_port = hp->dest;
 			}
@@ -1252,72 +2574,111 @@ static unsigned int netflow_target(
 		    case IPPROTO_ICMP: {
 			struct icmphdr _hdr, *hp;
 
-			if ((hp = skb_header_pointer(skb, iph->ihl * 4, 2, &_hdr)))
-				tuple.d_port = (hp->type << 8) | hp->code;
+			if (likely(family == AF_INET &&
+				    (hp = skb_header_pointer(skb, ptr, 2, &_hdr))))
+				tuple.d_port = htons((hp->type << 8) | hp->code);
 			break;
 		    }
+		    case IPPROTO_ICMPV6: {
+			    struct icmp6hdr _icmp6h, *ic;
+
+			    if (likely(family == AF_INET6 &&
+					(ic = skb_header_pointer(skb, ptr, 2, &_icmp6h))))
+				    tuple.d_port = htons((ic->icmp6_type << 8) | ic->icmp6_code);
+			    break;
+		    }
 		    case IPPROTO_IGMP: {
-			struct igmphdr *_hdr, *hp;
+			struct igmphdr _hdr, *hp;
 
-			if ((hp = skb_header_pointer(skb, iph->ihl * 4, 1, &_hdr)))
+			if (likely(hp = skb_header_pointer(skb, ptr, 1, &_hdr)))
 				tuple.d_port = hp->type;
 			}
 			break;
+		    case IPPROTO_AH: { /* IPSEC */
+			struct ip_auth_hdr _hdr, *hp;
+
+			if (likely(family == AF_INET && /* For IPv6 it's parsed above. */
+				    (hp = skb_header_pointer(skb, ptr, 8, &_hdr)))) {
+				tuple.s_port = hp->spi >> 16;
+				tuple.d_port = hp->spi;
+			}
+			break;
+		    }
+		    case IPPROTO_ESP: {
+			struct ip_esp_hdr _hdr, *hp;
+
+			if (likely(hp = skb_header_pointer(skb, ptr, 4, &_hdr)))
+				tuple.s_port = hp->spi >> 16;
+				tuple.d_port = hp->spi;
+			}
+			break;
 	       	}
 	} /* not fragmented */
 
 	/* aggregate networks */
 	read_lock_bh(&aggr_lock);
-	list_for_each_entry(aggr_n, &aggr_n_list, list)
-		if ((ntohl(tuple.s_addr) & aggr_n->mask) == aggr_n->addr) {
-			tuple.s_addr &= htonl(aggr_n->aggr_mask);
-			s_mask = aggr_n->prefix;
-			break; 
-		}
-	list_for_each_entry(aggr_n, &aggr_n_list, list)
-		if ((ntohl(tuple.d_addr) & aggr_n->mask) == aggr_n->addr) {
-			tuple.d_addr &= htonl(aggr_n->aggr_mask);
-			d_mask = aggr_n->prefix;
-			break; 
-		}
+	if (family == AF_INET) {
+		list_for_each_entry(aggr_n, &aggr_n_list, list)
+			if (unlikely((ntohl(tuple.src.ip) & aggr_n->mask) == aggr_n->addr)) {
+				tuple.src.ip &= htonl(aggr_n->aggr_mask);
+				s_mask = aggr_n->prefix;
+				atomic_inc(&aggr_n->usage);
+				break;
+			}
+		list_for_each_entry(aggr_n, &aggr_n_list, list)
+			if (unlikely((ntohl(tuple.dst.ip) & aggr_n->mask) == aggr_n->addr)) {
+				tuple.dst.ip &= htonl(aggr_n->aggr_mask);
+				d_mask = aggr_n->prefix;
+				atomic_inc(&aggr_n->usage);
+				break;
+			}
+	}
 
-	/* aggregate ports */
-	list_for_each_entry(aggr_p, &aggr_p_list, list)
-		if (ntohs(tuple.s_port) >= aggr_p->port1 &&
-		    ntohs(tuple.s_port) <= aggr_p->port2) {
-			tuple.s_port = htons(aggr_p->aggr_port);
-			break;
-		}
+	if (tuple.protocol == IPPROTO_TCP ||
+	    tuple.protocol == IPPROTO_UDP ||
+	    tuple.protocol == IPPROTO_SCTP ||
+	    tuple.protocol == IPPROTO_UDPLITE) {
+		/* aggregate ports */
+		list_for_each_entry(aggr_p, &aggr_p_list, list)
+			if (unlikely(ntohs(tuple.s_port) >= aggr_p->port1 &&
+			    ntohs(tuple.s_port) <= aggr_p->port2)) {
+				tuple.s_port = htons(aggr_p->aggr_port);
+				atomic_inc(&aggr_p->usage);
+				break;
+			}
 
-	list_for_each_entry(aggr_p, &aggr_p_list, list)
-		if (ntohs(tuple.d_port) >= aggr_p->port1 &&
-		    ntohs(tuple.d_port) <= aggr_p->port2) {
-			tuple.d_port = htons(aggr_p->aggr_port);
-			break;
-		}
+		list_for_each_entry(aggr_p, &aggr_p_list, list)
+			if (unlikely(ntohs(tuple.d_port) >= aggr_p->port1 &&
+			    ntohs(tuple.d_port) <= aggr_p->port2)) {
+				tuple.d_port = htons(aggr_p->aggr_port);
+				atomic_inc(&aggr_p->usage);
+				break;
+			}
+	}
 	read_unlock_bh(&aggr_lock);
 
 	hash = hash_netflow(&tuple);
-	spin_lock_bh(&ipt_netflow_lock);
+	read_lock_bh(&htable_rwlock);
+	spin_lock(&htable_locks[hash & LOCK_COUNT_MASK]);
 	/* record */
 	nf = ipt_netflow_find(&tuple, hash);
-	if (!nf) {
-		if (maxflows > 0 && atomic_read(&ipt_netflow_count) >= maxflows) {
+	if (unlikely(!nf)) {
+		struct rtable *rt;
+
+		if (unlikely(maxflows > 0 && atomic_read(&ipt_netflow_count) >= maxflows)) {
 			/* This is DOS attack prevention */
 			NETFLOW_STAT_INC(maxflows_err);
 			NETFLOW_STAT_INC(pkt_drop);
-			NETFLOW_STAT_ADD(traf_drop, ntohs(iph->tot_len));
-			spin_unlock_bh(&ipt_netflow_lock);
-			return IPT_CONTINUE;
+			NETFLOW_STAT_ADD(traf_drop, pkt_len);
+			goto unlock_return;
 		}
 
 		nf = init_netflow(&tuple, skb, hash);
-		if (!nf || IS_ERR(nf)) {
+		if (unlikely(!nf || IS_ERR(nf))) {
 			NETFLOW_STAT_INC(alloc_err);
 			NETFLOW_STAT_INC(pkt_drop);
-			NETFLOW_STAT_ADD(traf_drop, ntohs(iph->tot_len));
-			spin_unlock_bh(&ipt_netflow_lock);
-			return IPT_CONTINUE;
+			NETFLOW_STAT_ADD(traf_drop, pkt_len);
+			goto unlock_return;
 		}
 
 		nf->ts_first = jiffies;
@@ -1330,31 +2691,68 @@ static unsigned int netflow_target(
 		nf->s_mask = s_mask;
 		nf->d_mask = d_mask;
 
-		if (debug > 2)
-			printk(KERN_INFO "ipt_netflow: new (%u) %hd:%hd SRC=%u.%u.%u.%u:%u DST=%u.%u.%u.%u:%u\n",
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
+		rt = (struct rtable *)skb->dst;
+#else /* since 2.6.26 */
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31)
+		rt = skb->rtable;
+#else /* since 2.6.31 */
+		rt = skb_rtable(skb);
+#endif
+#endif
+		if (likely(family == AF_INET)) {
+			if (rt)
+				nf->nh.ip = rt->rt_gateway;
+		} else {
+			if (rt)
+				nf->nh.in6 = ((struct rt6_info *)rt)->rt6i_gateway;
+			nf->flow_label = (iph->ip6.flow_lbl[0] << 16) |
+			       	(iph->ip6.flow_lbl[1] << 8) | (iph->ip6.flow_lbl[2]);
+		}
+#if 0
+		if (unlikely(debug > 2))
+			printk(KERN_INFO "ipt_NETFLOW: new (%u) %hd:%hd SRC=%u.%u.%u.%u:%u DST=%u.%u.%u.%u:%u\n",
 			       atomic_read(&ipt_netflow_count),
 			       tuple.i_ifc, nf->o_ifc,
 			       NIPQUAD(tuple.s_addr), ntohs(tuple.s_port),
 			       NIPQUAD(tuple.d_addr), ntohs(tuple.d_port));
+#endif
 	} else {
 		/* ipt_netflow_list is sorted by access time:
 		 * most recently accessed flows are at head, old flows remain at tail
 		 * this function bubble up flow to the head */
+		spin_lock(&hlist_lock);
 		list_move(&nf->list, &ipt_netflow_list);
+		spin_unlock(&hlist_lock);
 	}
 
+#ifdef CONFIG_NF_CONNTRACK_MARK
+	{
+		struct nf_conn *ct;
+		enum ip_conntrack_info ctinfo;
+		ct = nf_ct_get(skb, &ctinfo);
+		if (ct)
+			nf->mark = ct->mark;
+	}
+#endif
+
 	nf->nr_packets++;
-	nf->nr_bytes += ntohs(iph->tot_len);
+	nf->nr_bytes += pkt_len;
 	nf->ts_last = jiffies;
 	nf->tcp_flags |= tcp_flags;
+	nf->options |= options;
+	if (tuple.protocol == IPPROTO_TCP)
+		nf->tcpoptions |= tcpoptions;
 
 	NETFLOW_STAT_INC(pkt_total);
-	NETFLOW_STAT_ADD(traf_total, ntohs(iph->tot_len));
+	NETFLOW_STAT_ADD(traf_total, pkt_len);
 
-	if (active_needs_export(nf, active_timeout * HZ)) {
+	if (likely(active_needs_export(nf, active_timeout * HZ))) {
 		/* ok, if this active flow to be exported
 		 * bubble it to the tail */
+		spin_lock(&hlist_lock);
 		list_move_tail(&nf->list, &ipt_netflow_list);
+		spin_unlock(&hlist_lock);
 
 		/* Blog: I thought about forcing timer to wake up sooner if we have
 		 * enough exportable flows, but in fact this doesn't have much sense,
@@ -1363,35 +2761,194 @@ static unsigned int netflow_target(
 		 * limited size). But yes, this is disputable. */
 	}
 
-	spin_unlock_bh(&ipt_netflow_lock);
+unlock_return:
+	spin_unlock(&htable_locks[hash & LOCK_COUNT_MASK]);
+	read_unlock_bh(&htable_rwlock);
 
 	return IPT_CONTINUE;
 }
 
-static struct ipt_target ipt_netflow_reg = {
-	.name		= "NETFLOW",
-	.target		= netflow_target,
-	.family		= AF_INET,
-#ifndef RAW_PROMISC_HACK
-	.table		= "filter",
-#ifndef NF_IP_LOCAL_IN /* 2.6.25 */
-	.hooks		= (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) |
-				(1 << NF_INET_LOCAL_OUT),
-#else
-	.hooks		= (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) |
-				(1 << NF_IP_LOCAL_OUT),
-#endif /* NF_IP_LOCAL_IN */
+#ifdef CONFIG_NF_NAT_NEEDED
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
+	/* Below 2.6.31 we don't need to handle callback chain manually. */
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0)
+#define NET_STRUCT struct net *net
+#define NET_ARG net,
+#define nf_conntrack_event_cb net->ct.nf_conntrack_event_cb
 #else
-	.table          = "raw",
-#ifndef NF_IP_LOCAL_IN
-	.hooks          = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) |
-				(1 << NF_INET_LOCAL_OUT) | (1 << NF_INET_PRE_ROUTING),
+#define NET_STRUCT void
+#define NET_ARG
+#endif
+static int set_notifier_cb(NET_STRUCT)
+{
+	struct nf_ct_event_notifier *notifier;
+
+	notifier = rcu_dereference(nf_conntrack_event_cb);
+	if (notifier == NULL) {
+		/* Polite mode. */
+		nf_conntrack_register_notifier(NET_ARG &ctnl_notifier);
+	} else if (notifier != &ctnl_notifier) {
+		if (!saved_event_cb)
+			saved_event_cb = notifier;
+		else if (saved_event_cb != notifier)
+			printk(KERN_ERR "natevents_net_init: %p != %p (report error.)\n",
+			    saved_event_cb, notifier);
+		rcu_assign_pointer(nf_conntrack_event_cb, &ctnl_notifier);
+	} else
+		printk(KERN_ERR "ipt_NETFLOW: natevents already enabled.\n");
+	return 0;
+}
+static void unset_notifier_cb(NET_STRUCT)
+{
+	struct nf_ct_event_notifier *notifier;
+
+	notifier = rcu_dereference(nf_conntrack_event_cb);
+	if (notifier == &ctnl_notifier) {
+		if (saved_event_cb == NULL)
+			nf_conntrack_unregister_notifier(NET_ARG &ctnl_notifier);
+		else
+			rcu_assign_pointer(nf_conntrack_event_cb, saved_event_cb);
+	} else
+		printk(KERN_ERR "ipt_NETFLOW: natevents already disabled.\n");
+}
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0)
+#undef nf_conntrack_event_cb
+static struct pernet_operations natevents_net_ops = {
+	.init = set_notifier_cb,
+	.exit = unset_notifier_cb
+};
+#endif
+#endif /* since 2.6.31 */
+
+static DEFINE_MUTEX(events_lock);
+/* Both functions may be called multiple times. */
+static void register_ct_events(void)
+{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
+#define NETLINK_M "nf_conntrack_netlink"
+	struct module *netlink_m;
+	static int referenced = 0;
+#endif
+
+	printk(KERN_INFO "ipt_NETFLOW: enable natevents.\n");
+	mutex_lock(&events_lock);
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
+	/* Pre-load netlink module who will be first notifier
+	 * user, and then hijack nf_conntrack_event_cb from it. */
+	if (
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0)
+	    !rcu_dereference(nf_conntrack_event_cb) ||
+#endif
+	    !(netlink_m = find_module(NETLINK_M))) {
+		printk("Loading " NETLINK_M "\n");
+		request_module(NETLINK_M);
+	}
+	/* Reference netlink module to prevent it's unsafe unload before us. */
+	if (!referenced && (netlink_m = find_module(NETLINK_M))) {
+		referenced++;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35)
+#define use_module ref_module
+#endif
+		use_module(THIS_MODULE, netlink_m);
+	}
+
+	/* Register ct events callback. */
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0)
+	register_pernet_subsys(&natevents_net_ops);
 #else
-	.hooks          = (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) |
-				(1 << NF_IP_LOCAL_OUT) | (1 << NF_IP_PRE_ROUTING),
-#endif /* NF_IP_LOCAL_IN */
-#endif /* !RAW_PROMISC_HACK */
-	.me		= THIS_MODULE
+	set_notifier_cb();
+#endif
+#else /* below v2.6.31 */
+	if (!natevents && nf_conntrack_register_notifier(&ctnl_notifier) < 0)
+		printk(KERN_ERR "Can't register conntrack notifier, natevents disabled.\n");
+	else
+#endif
+	natevents = 1;
+	mutex_unlock(&events_lock);
+}
+
+static void unregister_ct_events(void)
+{
+	printk(KERN_INFO "ipt_NETFLOW: disable natevents.\n");
+	mutex_lock(&events_lock);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0)
+	unregister_pernet_subsys(&natevents_net_ops);
+#else /* < v3.2 */
+	unset_notifier_cb();
+#endif /* v3.2 */
+	rcu_assign_pointer(saved_event_cb, NULL);
+#else /* < v2.6.31 */
+	nf_conntrack_unregister_notifier(&ctnl_notifier);
+#endif
+	natevents = 0;
+	mutex_unlock(&events_lock);
+}
+#endif /* CONFIG_NF_NAT_NEEDED */
+
+#ifndef NF_IP_LOCAL_IN /* 2.6.25 */
+#define NF_IP_PRE_ROUTING	NF_INET_PRE_ROUTING
+#define NF_IP_LOCAL_IN		NF_INET_LOCAL_IN
+#define NF_IP_FORWARD		NF_INET_FORWARD
+#define NF_IP_LOCAL_OUT		NF_INET_LOCAL_OUT
+#define NF_IP_POST_ROUTING	NF_INET_POST_ROUTING
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
+/* net/netfilter/x_tables.c */
+static void xt_unregister_targets(struct xt_target *target, unsigned int n)
+{
+	unsigned int i;
+
+	for (i = 0; i < n; i++)
+		xt_unregister_target(&target[i]);
+}
+static int xt_register_targets(struct xt_target *target, unsigned int n)
+{
+	unsigned int i;
+
+	int err = 0;
+	for (i = 0; i < n; i++)
+		if ((err = xt_register_target(&target[i])))
+			goto err;
+	return err;
+err:
+	if (i > 0)
+		xt_unregister_targets(target, i);
+	return err;
+}
+#endif
+
+static struct ipt_target ipt_netflow_reg[] __read_mostly = {
+	{
+		.name		= "NETFLOW",
+		.target		= netflow_target,
+		.checkentry	= netflow_target_check,
+		.family		= AF_INET,
+		.hooks		=
+		       	(1 << NF_IP_PRE_ROUTING) |
+		       	(1 << NF_IP_LOCAL_IN) |
+		       	(1 << NF_IP_FORWARD) |
+			(1 << NF_IP_LOCAL_OUT) |
+			(1 << NF_IP_POST_ROUTING),
+		.me		= THIS_MODULE
+	},
+	{
+		.name		= "NETFLOW",
+		.target		= netflow_target,
+		.checkentry	= netflow_target_check,
+		.family		= AF_INET6,
+		.hooks		=
+		       	(1 << NF_IP_PRE_ROUTING) |
+		       	(1 << NF_IP_LOCAL_IN) |
+		       	(1 << NF_IP_FORWARD) |
+			(1 << NF_IP_LOCAL_OUT) |
+			(1 << NF_IP_POST_ROUTING),
+		.me		= THIS_MODULE
+	},
 };
 
 static int __init ipt_netflow_init(void)
@@ -1399,11 +2956,16 @@ static int __init ipt_netflow_init(void)
 #ifdef CONFIG_PROC_FS
 	struct proc_dir_entry *proc_stat;
 #endif
+	printk(KERN_INFO "ipt_NETFLOW version %s, srcversion %s\n",
+		IPT_NETFLOW_VERSION, THIS_MODULE->srcversion);
 
 	get_random_bytes(&ipt_netflow_hash_rnd, 4);
 
 	/* determine hash size (idea from nf_conntrack_core.c) */
 	if (!hashsize) {
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
+#define num_physpages totalram_pages
+#endif
 		hashsize = (((num_physpages << PAGE_SHIFT) / 16384)
 					 / sizeof(struct hlist_head));
 		if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
@@ -1411,8 +2973,7 @@ static int __init ipt_netflow_init(void)
 	}
 	if (hashsize < 16)
 		hashsize = 16;
-	printk(KERN_INFO "ipt_netflow version %s (%u buckets)\n",
-		IPT_NETFLOW_VERSION, hashsize);
+	printk(KERN_INFO "ipt_NETFLOW: hashsize %u\n", hashsize);
 
 	ipt_netflow_hash_size = hashsize;
 	ipt_netflow_hash = alloc_hashtable(ipt_netflow_hash_size);
@@ -1434,12 +2995,18 @@ static int __init ipt_netflow_init(void)
 	}
 
 #ifdef CONFIG_PROC_FS
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3,10,0)
 	proc_stat = create_proc_entry("ipt_netflow", S_IRUGO, INIT_NET(proc_net_stat));
+#else
+	proc_stat = proc_create("ipt_netflow", S_IRUGO, INIT_NET(proc_net_stat), &nf_seq_fops);
+#endif
 	if (!proc_stat) {
 		printk(KERN_ERR "Unable to create /proc/net/stat/ipt_netflow entry\n");
 		goto err_free_netflow_slab;
 	}
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3,10,0)
 	proc_stat->proc_fops = &nf_seq_fops;
+#endif
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
 	proc_stat->owner = THIS_MODULE;
 #endif
@@ -1480,21 +3047,28 @@ static int __init ipt_netflow_init(void)
 	}
 	add_aggregation(aggregation);
 
-	__start_scan_worker();
+	netflow_switch_version(protocol);
+	_schedule_scan_worker(0);
 	setup_timer(&rate_timer, rate_timer_calc, 0);
 	mod_timer(&rate_timer, jiffies + (HZ * SAMPLERATE));
 
-	if (xt_register_target(&ipt_netflow_reg))
+	peakflows_at = jiffies;
+	if (xt_register_targets(ipt_netflow_reg, ARRAY_SIZE(ipt_netflow_reg)))
 		goto err_stop_timer;
 
-	peakflows_at = jiffies;
+#ifdef CONFIG_NF_NAT_NEEDED
+	if (natevents)
+		register_ct_events();
+#endif
 
-	printk(KERN_INFO "ipt_netflow loaded.\n");
+	printk(KERN_INFO "ipt_NETFLOW is loaded.\n");
 	return 0;
 
 err_stop_timer:
-	__stop_scan_worker();
+	_unschedule_scan_worker();
+	netflow_scan_and_export(AND_FLUSH);
 	del_timer_sync(&rate_timer);
+	free_templates();
 	destination_removeall();
 	aggregation_remove(&aggr_n_list);
 	aggregation_remove(&aggr_p_list);
@@ -1506,17 +3080,18 @@ err_free_proc_stat:
 #ifdef CONFIG_PROC_FS
 	remove_proc_entry("ipt_netflow", INIT_NET(proc_net_stat));
 err_free_netflow_slab:
-#endif  
+#endif
 	kmem_cache_destroy(ipt_netflow_cachep);
 err_free_hash:
 	vfree(ipt_netflow_hash);
 err:
+	printk(KERN_INFO "ipt_NETFLOW is not loaded.\n");
 	return -ENOMEM;
 }
 
 static void __exit ipt_netflow_fini(void)
 {
-	printk(KERN_INFO "ipt_netflow unloading..\n");
+	printk(KERN_INFO "ipt_NETFLOW unloading..\n");
 
 #ifdef CONFIG_SYSCTL
 	unregister_sysctl_table(netflow_sysctl_header);
@@ -1524,14 +3099,18 @@ static void __exit ipt_netflow_fini(void)
 #ifdef CONFIG_PROC_FS
 	remove_proc_entry("ipt_netflow", INIT_NET(proc_net_stat));
 #endif
-
-	xt_unregister_target(&ipt_netflow_reg);
-	__stop_scan_worker();
-	netflow_scan_and_export(1);
+	xt_unregister_targets(ipt_netflow_reg, ARRAY_SIZE(ipt_netflow_reg));
+#ifdef CONFIG_NF_NAT_NEEDED
+	if (natevents)
+		unregister_ct_events();
+#endif
+	_unschedule_scan_worker();
+	netflow_scan_and_export(AND_FLUSH);
 	del_timer_sync(&rate_timer);
 
 	synchronize_sched();
 
+	free_templates();
 	destination_removeall();
 	aggregation_remove(&aggr_n_list);
 	aggregation_remove(&aggr_p_list);
@@ -1539,7 +3118,7 @@ static void __exit ipt_netflow_fini(void)
 	kmem_cache_destroy(ipt_netflow_cachep);
 	vfree(ipt_netflow_hash);
 
-	printk(KERN_INFO "ipt_netflow unloaded.\n");
+	printk(KERN_INFO "ipt_NETFLOW unloaded.\n");
 }
 
 module_init(ipt_netflow_init);
diff --git a/ipt_NETFLOW.h b/ipt_NETFLOW.h
index 4a7b645..749f985 100644
--- a/ipt_NETFLOW.h
+++ b/ipt_NETFLOW.h
@@ -35,8 +35,8 @@ struct netflow5_record {
 	__be16		o_ifc;
 	__be32		nr_packets;
 	__be32		nr_octets;
-	__be32		ts_first;
-	__be32		ts_last;
+	__be32		first_ms;
+	__be32		last_ms;
 	__be16		s_port;
 	__be16		d_port;
 	__u8		reserved;
@@ -54,9 +54,9 @@ struct netflow5_record {
 struct netflow5_pdu {
 	__be16			version;
 	__be16			nr_records;
-	__be32			ts_uptime;
-	__be32			ts_usecs;
-	__be32			ts_unsecs;
+	__be32			ts_uptime; /* ms */
+	__be32			ts_usecs;  /* s  */
+	__be32			ts_unsecs; /* ns */
 	__be32			seq;
 	__u8			eng_type;
 	__u8			eng_id;
@@ -65,42 +65,185 @@ struct netflow5_pdu {
 } __attribute__ ((packed));
 #define NETFLOW5_HEADER_SIZE (sizeof(struct netflow5_pdu) - NETFLOW5_RECORDS_MAX * sizeof(struct netflow5_record))
 
+/* NetFlow v9 RFC http://www.ietf.org/rfc/rfc3954.txt */
+enum {
+	IN_BYTES = 1,
+	IN_PKTS = 2,
+	PROTOCOL = 4,
+	TOS = 5,
+	TCP_FLAGS = 6,
+	L4_SRC_PORT = 7,
+	IPV4_SRC_ADDR = 8,
+	SRC_MASK = 9,
+	INPUT_SNMP = 10,
+	L4_DST_PORT = 11,
+	IPV4_DST_ADDR = 12,
+	DST_MASK = 13,
+	OUTPUT_SNMP = 14,
+	IPV4_NEXT_HOP = 15,
+	//SRC_AS = 16,
+	//DST_AS = 17,
+	//BGP_IPV4_NEXT_HOP = 18,
+	//MUL_DST_PKTS = 19,
+	//MUL_DST_BYTES = 20,
+	LAST_SWITCHED = 21,
+	FIRST_SWITCHED = 22,
+	IPV6_SRC_ADDR = 27,
+	IPV6_DST_ADDR = 28,
+	IPV6_FLOW_LABEL = 31,
+	ICMP_TYPE = 32,
+	MUL_IGMP_TYPE = 33,
+	//TOTAL_BYTES_EXP = 40,
+	//TOTAL_PKTS_EXP = 41,
+	//TOTAL_FLOWS_EXP = 42,
+	IPV6_NEXT_HOP = 62,
+	IPV6_OPTION_HEADERS = 64,
+	commonPropertiesId = 137, /* for MARK */
+	ipv4Options = 208,
+	tcpOptions = 209,
+	postNATSourceIPv4Address = 225,
+	postNATDestinationIPv4Address = 226,
+	postNAPTSourceTransportPort = 227,
+	postNAPTDestinationTransportPort = 228,
+	natEvent = 230,
+	postNATSourceIPv6Address = 281,
+	postNATDestinationIPv6Address = 282,
+	IPSecSPI = 295,
+	observationTimeMilliseconds = 323,
+	observationTimeMicroseconds = 324,
+	observationTimeNanoseconds = 325,
+};
+
+enum {
+	FLOWSET_TEMPLATE = 0,
+	FLOWSET_OPTIONS = 1,
+	IPFIX_TEMPLATE = 2,
+	IPFIX_OPTIONS = 3,
+	FLOWSET_DATA_FIRST = 256,
+};
+
+struct flowset_template {
+	__be16	flowset_id;
+	__be16	length;
+	__be16	template_id;
+	__be16	field_count;
+} __attribute__ ((packed));
+
+struct flowset_data {
+	__be16	flowset_id;
+	__be16	length;
+} __attribute__ ((packed));
+
+/* NetFlow v9 packet. */
+struct netflow9_pdu {
+	__be16		version;
+	__be16		nr_records;
+	__be32		sys_uptime_ms;
+	__be32		export_time_s;
+	__be32		seq;
+	__be32		source_id; /* Exporter Observation Domain */
+	__u8		data[1400];
+} __attribute__ ((packed));
+
+/* IPFIX packet. */
+struct ipfix_pdu {
+	__be16		version;
+	__be16		length;
+	__be32		export_time_s;
+	__be32		seq;
+	__be32		odomain_id; /* Observation Domain ID */
+	__u8		data[1400];
+} __attribute__ ((packed));
+
+/* Maximum bytes flow can have, after it's reached flow will become
+ * not searchable and will be exported soon. */
+#define FLOW_FULL_WATERMARK 0xffefffff
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
+union nf_inet_addr {
+	__be32          ip;
+	__be32          ip6[4];
+	struct in_addr  in;
+	struct in6_addr in6;
+};
+#endif
+
 /* hashed data which identify unique flow */
+/* 16+16 + 2+2 + 2+1+1+1 = 41 */
 struct ipt_netflow_tuple {
-	__be32		s_addr;	// Network byte order
-	__be32		d_addr; // -"-
-	__be16		s_port; // -"-
+	union nf_inet_addr src;
+	union nf_inet_addr dst;
+	__be16		s_port; // Network byte order
 	__be16		d_port; // -"-
-	__be16		i_ifc;	// Local byte order
+	__u16		i_ifc;	// Host byte order
 	__u8		protocol;
 	__u8		tos;
+	__u8		l3proto;
 };
-/* tuple size is rounded to u32s */
-#define NETFLOW_TUPLE_SIZE (sizeof(struct ipt_netflow_tuple) / 4)
-
-/* maximum bytes flow can have, after it reached flow become not searchable and will be exported soon */
-#define FLOW_FULL_WATERMARK 0xffefffff
 
-/* flow entry */
+/* hlist[2] + tuple[]: 8+8 + 41 = 57 (less than usual cache line, 64) */
 struct ipt_netflow {
 	struct hlist_node hlist; // hashtable search chain
-	struct list_head list; // all flows chain
 
 	/* unique per flow data (hashed, NETFLOW_TUPLE_SIZE) */
 	struct ipt_netflow_tuple tuple;
 
 	/* volatile data */
-	__be16		o_ifc;
+	union nf_inet_addr nh;
+	__u16		o_ifc;
 	__u8		s_mask;
 	__u8		d_mask;
+	__u8		tcp_flags; /* `OR' of all tcp flags */
 
 	/* flow statistics */
 	u_int32_t	nr_packets;
 	u_int32_t	nr_bytes;
-	unsigned long	ts_first;
-	unsigned long	ts_last;
-	__u8		tcp_flags; /* `OR' of all tcp flags */
+	union {
+		struct {
+			unsigned long first;
+			unsigned long last;
+		} ts;
+		ktime_t	ts_obs;
+	} _ts_un;
+#define ts_first _ts_un.ts.first
+#define ts_last  _ts_un.ts.last
+#define ts_obs   _ts_un.ts_obs
+	u_int32_t	flow_label; /* IPv6 */
+	u_int32_t	options; /* IPv4(16) & IPv6(32) Options */
+	u_int32_t	tcpoptions;
+#ifdef CONFIG_NF_CONNTRACK_MARK
+	u_int32_t	mark; /* Exported as commonPropertiesId */
+#endif
+#ifdef CONFIG_NF_NAT_NEEDED
+	__be32		s_as;
+	__be32		d_as;
+	struct nat_event *nat;
+#endif
+	struct list_head list; // all flows chain
+	spinlock_t	*lock;
+};
+
+#ifdef CONFIG_NF_NAT_NEEDED
+enum {
+	NAT_CREATE, NAT_DESTROY, NAT_POOLEXHAUSTED
 };
+struct nat_event {
+	struct list_head list;
+	struct {
+		__be32	s_addr;
+		__be32	d_addr;
+		__be16	s_port;
+		__be16	d_port;
+	} pre, post;
+	ktime_t		ts_ktime;
+	unsigned long	ts_jiffies;
+	__u8	protocol;
+	__u8	nat_event;
+};
+#define IS_DUMMY_FLOW(nf) (nf->nat)
+#else
+#define IS_DUMMY_FLOW(nf) 0
+#endif
 
 static inline int ipt_netflow_tuple_equal(const struct ipt_netflow_tuple *t1,
 				    const struct ipt_netflow_tuple *t2)
@@ -115,11 +258,13 @@ struct ipt_netflow_sock {
 	unsigned short port;
 	atomic_t wmem_peak;	// sk_wmem_alloc peak value
 	atomic_t err_full;	// socket filled error
+	atomic_t err_connect;	// connect errors
 	atomic_t err_other;	// other socket errors
 };
 
 struct netflow_aggr_n {
 	struct list_head list;
+	atomic_t usage;
 	__u32 mask;
 	__u32 addr;
 	__u32 aggr_mask;
@@ -128,6 +273,7 @@ struct netflow_aggr_n {
 
 struct netflow_aggr_p {
 	struct list_head list;
+	atomic_t usage;
 	__u16 port1;
 	__u16 port2;
 	__u16 aggr_port;
diff --git a/libipt_NETFLOW.c b/libipt_NETFLOW.c
index d85b6d9..a0f9e5d 100644
--- a/libipt_NETFLOW.c
+++ b/libipt_NETFLOW.c
@@ -58,24 +58,24 @@
 #define _IPT_IP struct ipt_ip
 #endif
 
+#ifndef IPTABLES_VERSION
+#define IPTABLES_VERSION XTABLES_VERSION
+#endif
+
 static struct option opts[] = {
-  {0}
+  { 0 }
 };
 
 static void help(void)
 {
-	printf( "NETFLOW target\n");
+	printf("NETFLOW target\n");
 }
 
-//static int parse(int c, char **argv, int invert, unsigned int *flags,
-//      const _IPT_ENTRY *entry,
-//      struct ipt_entry_target **target)
 static int parse(int c, char **argv, int invert, unsigned int *flags,
 	     const _IPT_ENTRY  *entry,
 	     struct ipt_entry_target **targetinfo)
 
 {
-
 	return 1;
 }
 
@@ -95,16 +95,9 @@ static void print(const _IPT_IP *ip,
 }
 
 static struct iptables_target netflow = { 
-#ifdef MOD140
-	.family		= AF_INET,
-#endif
 	.next		= NULL,
 	.name		= "NETFLOW",
-#ifdef XTABLES_VERSION
-	.version	= XTABLES_VERSION,
-#else
 	.version	= IPTABLES_VERSION,
-#endif
 	.size           = IPT_ALIGN(0),
 	.userspacesize  = IPT_ALIGN(0),
 	.help		= &help,
diff --git a/murmur3.h b/murmur3.h
new file mode 100644
index 0000000..57a6006
--- /dev/null
+++ b/murmur3.h
@@ -0,0 +1,42 @@
+/* MurmurHash3, based on https://code.google.com/p/smhasher of Austin Appleby. */
+
+static __always_inline uint32_t rotl32(const uint32_t x, const int8_t r)
+{
+	return (x << r) | (x >> (32 - r));
+}
+
+static __always_inline uint32_t fmix32(register uint32_t h)
+{
+	h ^= h >> 16;
+	h *= 0x85ebca6b;
+	h ^= h >> 13;
+	h *= 0xc2b2ae35;
+	h ^= h >> 16;
+	return h;
+}
+
+static inline uint32_t murmur3(const void *key, const uint32_t len, const uint32_t seed)
+{
+	const uint32_t c1 = 0xcc9e2d51;
+	const uint32_t c2 = 0x1b873593;
+	const uint32_t *blocks;
+	const uint8_t *tail;
+	register uint32_t h1 = seed;
+	uint32_t k1 = 0;
+	uint32_t i;
+
+	blocks = (const uint32_t *)key;
+	for (i = len / 4; i; --i) {
+		h1 ^= rotl32(*blocks++ * c1, 15) * c2;
+		h1 = rotl32(h1, 13) * 5 + 0xe6546b64;
+	}
+	tail = (const uint8_t*)blocks;
+	switch (len & 3) {
+		case 3: k1 ^= tail[2] << 16;
+		case 2: k1 ^= tail[1] << 8;
+		case 1: k1 ^= tail[0];
+			h1 ^= rotl32(k1 * c1, 15) * c2;
+	}
+	return fmix32(h1^ len);
+}
+
diff --git a/raw_promisc_debian_squeeze6.patch b/raw_promisc_debian_squeeze6.patch
new file mode 100644
index 0000000..69d0d35
--- /dev/null
+++ b/raw_promisc_debian_squeeze6.patch
@@ -0,0 +1,37 @@
+
+ Short manual and patch for Debian Squeeze
+ suggested by Pavel Odintsov:
+
+On Thu, Dec 27, 2012 at 07:46:30PM +0400, Pavel Odintsov wrote:
+>
+> ËÒÁÔËÉÊ ÍÁÎÕÁÌ ÄÌÑ ÐÁÔÞÉÎÇÁ Debian Squeeze ÑÄÒÁ ÐÁÔÞÅÍ promisc.
+> 
+> cd /usr/src
+> apt-get install -y dpkg-dev
+> apt-get build-dep  linux-image-2.6.32-5-amd64
+> cd linux-2.6-2.6.32/
+> apt-get source  linux-image-2.6.32-5-amd64
+> 
+> wget .... /root/raw_promisc_debian_squeeze6.patch
+> patch -p1 < raw_promisc_debian_squeeze6.patch
+> îÁËÌÁÄÙ×ÁÅÍ ÐÁÔÞÉ ÄÅÂÉÑÎÁ:
+> debian/rules source
+> 
+> úÁÐÕÓËÁÅÍ ÓÂÏÒËÕ:
+> debian/rules binary
+> 
+
+diff -rupN linux-2.6-2.6.32/net/ipv4/ip_input.c linux-2.6-2.6.32_promisc_raw//net/ipv4/ip_input.c
+--- linux-2.6-2.6.32/net/ipv4/ip_input.c	2009-12-03 04:51:21.000000000 +0100
++++ linux-2.6-2.6.32_promisc_raw//net/ipv4/ip_input.c	2012-06-25 19:13:49.000000000 +0200
+@@ -383,8 +383,8 @@ int ip_rcv(struct sk_buff *skb, struct n
+ 	/* When the interface is in promisc. mode, drop all the crap
+ 	 * that it receives, do not try to analyse it.
+ 	 */
+-	if (skb->pkt_type == PACKET_OTHERHOST)
+-		goto drop;
++	//if (skb->pkt_type == PACKET_OTHERHOST)
++	//	goto drop;
+ 
+ 
+ 	IP_UPD_PO_STATS_BH(dev_net(dev), IPSTATS_MIB_IN, skb->len);