Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 63 additions & 46 deletions compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,34 @@ union nf_inet_addr {
# define time_is_after_jiffies(a) time_before(jiffies, a)
#endif

#if LINUX_VERSION_CODE < KERNEL_VERSION(3,14,0)
# if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
# define prandom_u32 get_random_int
# elif LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
# define prandom_u32 random32
#ifndef HAVE_GET_RANDOM_U32
# ifdef HAVE_PRANDOM_U32
# ifdef HAVE_PRANDOM_H
# include <linux/prandom.h>
# endif
static inline u32 get_random_u32() {
return prandom_u32();
}
# else
# pragma error Need fallback for get_random_u32
# endif
#endif
#define prandom_u32_max compat_prandom_u32_max
static inline u32 prandom_u32_max(u32 ep_ro)

#ifndef HAVE_GET_RANDOM_U32_BELOW
# ifdef HAVE_PRANDOM_U32_MAX
# ifdef HAVE_PRANDOM_H
# include <linux/prandom.h>
# endif
static inline u32 get_random_u32_below(u32 ep_ro)
{
return prandom_u32_max(ep_ro);
}
# else
static inline u32 get_random_u32_below(u32 ep_ro)
{
return (u32)(((u64) prandom_u32() * ep_ro) >> 32);
return (u32)(((u64) get_random_u32() * ep_ro) >> 32);
}
# endif
#endif

#ifndef min_not_zero
Expand Down Expand Up @@ -178,6 +195,10 @@ static int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cm
# define NF_IP_POST_ROUTING NF_INET_POST_ROUTING
#endif

#ifndef HAVE_SIZED_STRSCPY
#define strscpy strlcpy
#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)
Expand Down Expand Up @@ -216,7 +237,7 @@ struct timeval {
long tv_usec; /* microseconds */
};

unsigned long timeval_to_jiffies(const struct timeval *tv)
static inline unsigned long timeval_to_jiffies(const struct timeval *tv)
{
return timespec64_to_jiffies(&(struct timespec64){
tv->tv_sec,
Expand Down Expand Up @@ -383,7 +404,7 @@ static int sockaddr_cmp(const struct sockaddr_storage *sa1, const struct sockadd
#ifndef IN6PTON_XDIGIT
#define hex_to_bin compat_hex_to_bin
/* lib/hexdump.c */
int hex_to_bin(char ch)
static inline int hex_to_bin(char ch)
{
if ((ch >= '0') && (ch <= '9'))
return ch - '0';
Expand Down Expand Up @@ -712,43 +733,10 @@ static inline void do_gettimeofday(struct timeval *tv)
}
#endif

#define TOLOWER(x) ((x) | 0x20)
unsigned long long strtoul(const char *cp, char **endp, unsigned int base)
{
unsigned long long result = 0;

if (!base) {
if (cp[0] == '0') {
if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
base = 16;
else
base = 8;
} else {
base = 10;
}
}

if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
cp += 2;

while (isxdigit(*cp)) {
unsigned int value;

value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
if (value >= base)
break;
result = result * base + value;
cp++;
}
if (endp)
*endp = (char *)cp;

return result;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,12,0)
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,12,0)) \
|| ((LINUX_VERSION_CODE >= KERNEL_VERSION(5,10,220)) && (LINUX_VERSION_CODE < KERNEL_VERSION(5,11,0)))
/*
* find_module() is unexported in v5.12:
* find_module() is unexported in v5.12 (backported to 5.10.220):
* 089049f6c9956 ("module: unexport find_module and module_mutex")
* and module_mutex is replaced with RCU in
* a006050575745 ("module: use RCU to synchronize find_module")
Expand All @@ -771,6 +759,35 @@ struct module *find_module(const char *name)
}
#endif

/* Copy from 294f69e662d1 ("compiler_attributes.h: Add 'fallthrough' pseudo
* keyword for switch/case use") */
#if !defined(fallthrough) && defined(__has_attribute)
# if __has_attribute(__fallthrough__)
# define fallthrough __attribute__((__fallthrough__))
# endif
#endif
#ifndef fallthrough
# define fallthrough do {} while (0) /* fallthrough */
#endif

#ifndef HAVE_NF_CT_EVENT_NOTIFIER_CT_EVENT
/*
* nat event callback parameter is constified in 5.15+
* but it prevents module building with previous kernel versions
*/
# define NF_CT_EVENT struct nf_ct_event
# define ct_event fcn
#else
# define NF_CT_EVENT const struct nf_ct_event
#endif

#if LINUX_VERSION_CODE < KERNEL_VERSION(4,4,0)
static inline bool netif_is_bridge_port(const struct net_device *dev)
{
return dev->priv_flags & IFF_BRIDGE_PORT;
}
#endif

/* Copy from 294f69e662d1 ("compiler_attributes.h: Add 'fallthrough' pseudo
* keyword for switch/case use") */
#ifndef fallthrough
Expand Down
5 changes: 3 additions & 2 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ PATH=$PATH:/bin:/usr/bin:/usr/sbin:/sbin:/usr/local/sbin
case "$1" in
--from-dkms-conf*)
KDKMS=`echo "$1" | sed 's/[^=]*.//'`
KDIR="$KDKMS"
# restore options from existing Makefile, if present
if [ -e Makefile ]; then
set -- `sed -n 's/^CARGS = \(.*\)/\1/p' Makefile`
Expand Down Expand Up @@ -348,7 +349,7 @@ do
--disable-snmp-a*) SKIPSNMP=1 ;;
--disable-net-snmp*) SKIPSNMP=1 ;;
--disable-dkms*) SKIPDKMS=1 ;;
--from-dkms-conf*) ;;
--from-dkms-conf*) SKIPDKMS=1 ;;
--make) echo called from make; CARGS=`echo $CARGS | sed s/--make//g` ;;
-Werror) KOPTS="$KOPTS -Werror" ;;
--help|-h) show_help ;;
Expand Down Expand Up @@ -614,7 +615,7 @@ dkms_check() {
echo Yes.
DKMSINSTALL=dinstall
test "$FROMDKMSCONF" && return
if dkms status | grep ^ipt-netflow, >/dev/null; then
if dkms status ipt-netflow | grep ^ipt-netflow/ >/dev/null; then
echo "! You are already have module installed via DKMS"
echo "! it will be uninstalled on 'make install' and"
echo "! current version of module installed afterwards."
Expand Down
4 changes: 4 additions & 0 deletions dkms.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ PACKAGE_NAME="ipt-netflow"
pushd `dirname $BASH_SOURCE`
PACKAGE_VERSION=`./version.sh`
popd

# skb_reset_mac_len() was introduced in v3.0-rc3
BUILD_EXCLUSIVE_KERNEL_MIN="3"

BUILT_MODULE_NAME[0]=ipt_NETFLOW
DEST_MODULE_LOCATION[0]=/kernel/extra
STRIP[0]=no
Expand Down
71 changes: 62 additions & 9 deletions gen_compat_def
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash -efu
#!/bin/bash -fu
# SPDX-License-Identifier: GPL-2.0-only
#
# Generate defines based on kernel having some symbols declared.
Expand All @@ -21,7 +21,7 @@ WD=cc-test-build
mkdir -p $WD
cd ./$WD || fatal "cannot cd to $WD"

# args: HAVE_SUMBOL symbol include
# args: HAVE_SYMBOL symbol [include] [success] [failure]
kbuild_test_compile() {
local cmd

Expand All @@ -30,14 +30,15 @@ kbuild_test_compile() {
cmd="make -s -B -C $KDIR M=$PWD modules"
echo "$cmd" > log
if $cmd >> log 2>&1; then
echo " declared" >&2
[ "$2" ] && echo "// $2 is declared ${3:+in <$3>}"
echo " ${4-declared}" >&2
[ "$2" ] && echo "// $2 ${4-is declared}${3:+ in <$3>}"
echo "#define HAVE_$1"
echo
return 0
else
echo " undeclared" >&2
echo " ${5-undeclared}" >&2
echo "#undef HAVE_$1"
echo "// ${2:-symbol} is undeclared${3:+ in <$3>}. Compile:"
echo "// ${2:-symbol} ${5-is undeclared}${3:+ in <$3>}. Compile:"
sed "s/^/\/\/ /" test.c
echo "// Output:"
sed "s/^/\/\/ /" log
Expand All @@ -56,15 +57,16 @@ kbuild_test_compile() {
echo >&2
exit 3
fi
return 1
fi
}

# Test that symbol is defined (will catch functions mostly).
kbuild_test_symbol() {
echo -n "Test function $* " >&2
echo -n "Test function $1 $2" >&2
kbuild_test_compile ${1^^} $1 ${2-} <<-EOF
#include <linux/module.h>
${2:+#include <$2>}
${3:-${2:+#include <$2>}}
MODULE_LICENSE("GPL");
void *test = $1;
EOF
Expand All @@ -91,6 +93,36 @@ kbuild_test_struct() {
EOF
}

# Test that struct have member
kbuild_test_member() {
echo -n "Test member $* " >&2
structname=${1%.*}
member=${1#*.}
def=${1^^}
def=${def//./_}
kbuild_test_compile $def "struct $1" ${2-} <<-EOF
#include <linux/module.h>
${2:+#include <$2>}
MODULE_LICENSE("GPL");
typeof(((struct $structname*)0)->$member) test;
EOF
}

# Test that a header is available/exist
kbuild_test_header() {
echo -n "Test header $*" >&2
structname=${1%.*}
member=${1#*.}
def=${1^^}
def=${def##*/}
def=${def//./_}
kbuild_test_compile $def "header $1" "" "exists" "doesn't exist" <<-EOF
#include <linux/module.h>
#include <$1>
MODULE_LICENSE("GPL");
EOF
}

# Test that struct have member
kbuild_test_member() {
echo -n "Test member $* " >&2
Expand Down Expand Up @@ -121,14 +153,35 @@ kbuild_test_symbol nf_bridge_info_get linux/netfilter_bridge.h
# Stumbled on 5.9
kbuild_test_struct vlan_dev_priv linux/if_vlan.h
# Kernel version check broken by centos8
kbuild_test_symbol put_unaligned_be24 asm/unaligned.h
kbuild_test_symbol put_unaligned_be24 '???/unaligned.h' '#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,12,0)
#include <linux/unaligned.h>
#else
#include <asm/unaligned.h>
#endif'
# totalram_pages changed from atomic to inline function.
kbuild_test_symbol totalram_pages linux/mm.h
kbuild_test_ref totalram_pages linux/mm.h
# b86c0e6429da ("netfilter: ecache: prepare for event notifier merge")
kbuild_test_member nf_ct_event_notifier.ct_event net/netfilter/nf_conntrack_ecache.h
# 6.4: 0199849acd07 ("sysctl: remove register_sysctl_paths()")
kbuild_test_symbol register_sysctl_paths linux/sysctl.h
# If we have strscpy, we can use that (more optimal compared to strlcpy).
kbuild_test_symbol sized_strscpy linux/string.h
# Do we have get_random_u32_below
kbuild_test_symbol get_random_u32_below linux/random.h
# Do we have get_random_u32
kbuild_test_symbol get_random_u32 linux/random.h

# prandom functions moved from random.h to prandom.h recentish.
# We use these for fallback for the above only.
if kbuild_test_header linux/prandom.h; then
prand_h=linux/prandom.h
else
prand_h=linux/random.h
fi
kbuild_test_symbol prandom_u32 $prand_h
kbuild_test_symbol prandom_u32_max $prand_h

echo "// End of compat_def.h"

Expand Down
Loading