Skip to content

Commit 0c5759d

Browse files
committed
Fix prandom_u32{,_max} => get_random_u32{,_below}
For backwards compatiblity this gets pretty nasty. This should work fairly well. Signed-off-by: Jaco Kroon <jaco@uls.co.za>
1 parent 5fb1aed commit 0c5759d

File tree

3 files changed

+66
-17
lines changed

3 files changed

+66
-17
lines changed

compat.h

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,34 @@ union nf_inet_addr {
108108
# define time_is_after_jiffies(a) time_before(jiffies, a)
109109
#endif
110110

111-
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,14,0)
112-
# if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
113-
# define prandom_u32 get_random_int
114-
# elif LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
115-
# define prandom_u32 random32
111+
#ifndef HAVE_GET_RANDOM_U32
112+
# ifdef HAVE_PRANDOM_U32
113+
# ifdef HAVE_PRANDOM_H
114+
# include <linux/prandom.h>
115+
# endif
116+
static inline u32 get_random_u32() {
117+
return prandom_u32();
118+
}
119+
# else
120+
# pragma error Need fallback for get_random_u32
121+
# endif
116122
#endif
117-
#define prandom_u32_max compat_prandom_u32_max
118-
static inline u32 prandom_u32_max(u32 ep_ro)
123+
124+
#ifndef HAVE_GET_RANDOM_U32_BELOW
125+
# ifdef HAVE_PRANDOM_U32_MAX
126+
# ifdef HAVE_PRANDOM_H
127+
# include <linux/prandom.h>
128+
# endif
129+
static inline u32 get_random_u32_below(u32 ep_ro)
119130
{
120-
return (u32)(((u64) prandom_u32() * ep_ro) >> 32);
131+
return prandom_u32_max(ep_ro);
121132
}
133+
# else
134+
static inline u32 get_random_u32_below(u32 ep_ro)
135+
{
136+
return (u32)(((u64) get_random_u32() * ep_ro) >> 32);
137+
}
138+
# endif
122139
#endif
123140

124141
#ifndef min_not_zero

gen_compat_def

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash -efu
1+
#!/bin/bash -fu
22
# SPDX-License-Identifier: GPL-2.0-only
33
#
44
# Generate defines based on kernel having some symbols declared.
@@ -21,7 +21,7 @@ WD=cc-test-build
2121
mkdir -p $WD
2222
cd ./$WD || fatal "cannot cd to $WD"
2323

24-
# args: HAVE_SUMBOL symbol include
24+
# args: HAVE_SYMBOL symbol [include] [success] [failure]
2525
kbuild_test_compile() {
2626
local cmd
2727

@@ -30,14 +30,15 @@ kbuild_test_compile() {
3030
cmd="make -s -B -C $KDIR M=$PWD modules"
3131
echo "$cmd" > log
3232
if $cmd >> log 2>&1; then
33-
echo " declared" >&2
34-
[ "$2" ] && echo "// $2 is declared ${3:+in <$3>}"
33+
echo " ${4-declared}" >&2
34+
[ "$2" ] && echo "// $2 ${4-is declared}${3:+ in <$3>}"
3535
echo "#define HAVE_$1"
3636
echo
37+
return 0
3738
else
38-
echo " undeclared" >&2
39+
echo " ${5-undeclared}" >&2
3940
echo "#undef HAVE_$1"
40-
echo "// ${2:-symbol} is undeclared${3:+ in <$3>}. Compile:"
41+
echo "// ${2:-symbol} ${5-is undeclared}${3:+ in <$3>}. Compile:"
4142
sed "s/^/\/\/ /" test.c
4243
echo "// Output:"
4344
sed "s/^/\/\/ /" log
@@ -56,6 +57,7 @@ kbuild_test_compile() {
5657
echo >&2
5758
exit 3
5859
fi
60+
return 1
5961
fi
6062
}
6163

@@ -105,6 +107,22 @@ kbuild_test_member() {
105107
typeof(((struct $structname*)0)->$member) test;
106108
EOF
107109
}
110+
111+
# Test that a header is available/exist
112+
kbuild_test_header() {
113+
echo -n "Test header $*" >&2
114+
structname=${1%.*}
115+
member=${1#*.}
116+
def=${1^^}
117+
def=${def##*/}
118+
def=${def//./_}
119+
kbuild_test_compile $def "header $1" "" "exists" "doesn't exist" <<-EOF
120+
#include <linux/module.h>
121+
#include <$1>
122+
MODULE_LICENSE("GPL");
123+
EOF
124+
}
125+
108126
echo "// Autogenerated for $KDIR"
109127
echo
110128

@@ -136,6 +154,20 @@ kbuild_test_member nf_ct_event_notifier.ct_event net/netfilter/nf_conntrack_ecac
136154
kbuild_test_symbol register_sysctl_paths linux/sysctl.h
137155
# If we have strscpy, we can use that (more optimal compared to strlcpy).
138156
kbuild_test_symbol sized_strscpy linux/string.h
157+
# Do we have get_random_u32_below
158+
kbuild_test_symbol get_random_u32_below linux/random.h
159+
# Do we have get_random_u32
160+
kbuild_test_symbol get_random_u32 linux/random.h
161+
162+
# prandom functions moved from random.h to prandom.h recentish.
163+
# We use these for fallback for the above only.
164+
if kbuild_test_header linux/prandom.h; then
165+
prand_h=linux/prandom.h
166+
else
167+
prand_h=linux/random.h
168+
fi
169+
kbuild_test_symbol prandom_u32 $prand_h
170+
kbuild_test_symbol prandom_u32_max $prand_h
139171

140172
echo "// End of compat_def.h"
141173

ipt_NETFLOW.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4460,7 +4460,7 @@ static int netflow_scan_and_export(const int flush)
44604460
val = nf->sampler_count % interval;
44614461
break;
44624462
case SAMPLER_RANDOM:
4463-
val = prandom_u32_max(interval);
4463+
val = get_random_u32_below(interval);
44644464
break;
44654465
default: /* SAMPLER_HASH */
44664466
val = 0;
@@ -5717,12 +5717,12 @@ static int __init ipt_netflow_init(void)
57175717
}
57185718
parse_sampler(sampler);
57195719
#ifdef SAMPLING_HASH
5720-
hash_seed = prandom_u32();
5720+
hash_seed = get_random_u32();
57215721
#endif
57225722
#endif
57235723

57245724
#ifdef ENABLE_RANDOM_TEMPLATE_IDS
5725-
template_ids = FLOWSET_DATA_FIRST | prandom_u32_max(0x00010000);
5725+
template_ids = FLOWSET_DATA_FIRST | get_random_u32_below(0x00010000);
57265726
#endif
57275727

57285728
#ifdef SNMP_RULES

0 commit comments

Comments
 (0)