Skip to content

Commit 56831a5

Browse files
laarmenaabc
authored andcommitted
ipt_netflow: optionally seed initial template ID from PRNG
If we're always starting from 256, a module reload could result in the client getting data under template IDs that were used under the previous configuration and might not match (or worse, wrongly match). We add a compilation option to start the template ID range from a random number, reducing greatly the chances of such an accident occuring. Since we're starting from a random number, we now have to check for wrapping, as the random number might be close to 0xFFFF, which would mean exporting template with IDs under 256 that are reserved. v2: Fix the log output by keeping a separate count of the templates generated by the module. Signed-off-by: Simon Chopin <[email protected]> [[email protected]: Fixed incorrect if statement] Signed-off-by: ABC <[email protected]>
1 parent 83a20ef commit 56831a5

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

configure

+2
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ show_help() {
302302
echo " --enable-direction enables flowDirection(61) Element"
303303
echo " --enable-sampler enables Flow Sampling"
304304
echo " --enable-sampler=hash enables Hash sampler"
305+
echo " --enable-rand-tpl-id enables seeding the template IDs from a random number"
305306
echo " --enable-aggregation enables aggregation rules"
306307
echo " --enable-promisc enables promisc hack mode"
307308
echo " --promisc-mpls decapsulate MPLS in promisc mode"
@@ -337,6 +338,7 @@ do
337338
--enable-sampl*hash) KOPTS="$KOPTS -DENABLE_SAMPLER -DSAMPLING_HASH" ;;
338339
--enable-sampl*) KOPTS="$KOPTS -DENABLE_SAMPLER" ;;
339340
--enable-aggr*) KOPTS="$KOPTS -DENABLE_AGGR" ;;
341+
--enable-rand-tpl*) KOPTS="$KOPTS -DENABLE_RANDOM_TEMPLATE_IDS" ;;
340342
--enable-promi*) ENABLE_PROMISC=1 ;;
341343
--promisc-mpls*) ENABLE_PROMISC=1; PROMISC_MPLS=1; MPLS_DEPTH=${ac_optarg:-3} ;;
342344
--enable-snmp-r*) KOPTS="$KOPTS -DSNMP_RULES" ;;

ipt_NETFLOW.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ enum {
329329
DONT_FLUSH, AND_FLUSH
330330
};
331331
static int template_ids = FLOWSET_DATA_FIRST;
332+
static int tpl_gen_count = 0; /* how much templates */
332333
static int tpl_count = 0; /* how much active templates */
333334
#define STAT_INTERVAL (1*60)
334335
#define SYSINFO_INTERVAL (5*60)
@@ -704,7 +705,7 @@ static int nf_seq_show(struct seq_file *seq, void *v)
704705
seq_printf(seq, " (netflow)");
705706
if (protocol >= 9)
706707
seq_printf(seq, ", refresh-rate %u, timeout-rate %u, (templates %d, active %d).\n",
707-
refresh_rate, timeout_rate, template_ids - FLOWSET_DATA_FIRST, tpl_count);
708+
refresh_rate, timeout_rate, tpl_gen_count, tpl_count);
708709
else
709710
seq_printf(seq, "\n");
710711

@@ -3393,6 +3394,9 @@ static struct data_template *get_template(const unsigned int tmask)
33933394
tpl->length = length;
33943395
tpl->rec_size = 0;
33953396
tpl->template_id_n = htons(template_ids++);
3397+
tpl_gen_count++;
3398+
if (template_ids >= 0x00010000)
3399+
template_ids = FLOWSET_DATA_FIRST;
33963400
tpl->exported_cnt = 0;
33973401
tpl->exported_ts = 0;
33983402

@@ -5710,6 +5714,10 @@ static int __init ipt_netflow_init(void)
57105714
#endif
57115715
#endif
57125716

5717+
#ifdef ENABLE_RANDOM_TEMPLATE_IDS
5718+
template_ids = FLOWSET_DATA_FIRST | prandom_u32_max(0x00010000);
5719+
#endif
5720+
57135721
#ifdef SNMP_RULES
57145722
if (!snmp_rules)
57155723
snmp_rules = snmp_rules_buf;

0 commit comments

Comments
 (0)