Skip to content

Commit 3069463

Browse files
author
Vadim Fedorenko
committed
ipt_NETFLOW: add compatibility with 6.8+
* add strlcpy definition as it was removed in 6.8 * replace strtoul with simple_strtoul which exists in all kernels and is proper interface to use * inline timeval_to_jiffies to follow new kernel build rules * replace check for in{4,6}_pton to remove unneeded functions Signed-off-by: Vadim Fedorenko <[email protected]>
1 parent 0eb2092 commit 3069463

File tree

3 files changed

+27
-40
lines changed

3 files changed

+27
-40
lines changed

compat.h

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ struct timeval {
216216
long tv_usec; /* microseconds */
217217
};
218218

219-
unsigned long timeval_to_jiffies(const struct timeval *tv)
219+
static inline unsigned long timeval_to_jiffies(const struct timeval *tv)
220220
{
221221
return timespec64_to_jiffies(&(struct timespec64){
222222
tv->tv_sec,
@@ -225,6 +225,20 @@ unsigned long timeval_to_jiffies(const struct timeval *tv)
225225
}
226226
#endif
227227

228+
#ifndef HAVE_STRLCPY
229+
static inline size_t strlcpy(char *dest, const char *src, size_t size)
230+
{
231+
size_t ret = strlen(src);
232+
233+
if (size) {
234+
size_t len = (ret >= size) ? size - 1 : ret;
235+
__builtin_memcpy(dest, src, len);
236+
dest[len] = '\0';
237+
}
238+
return ret;
239+
}
240+
#endif
241+
228242
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
229243
# ifdef ktime_to_timeval
230244
/* ktime_to_timeval is defined on 64bit and inline on 32bit cpu */
@@ -380,10 +394,10 @@ static int sockaddr_cmp(const struct sockaddr_storage *sa1, const struct sockadd
380394
return 0;
381395
}
382396

383-
#ifndef IN6PTON_XDIGIT
397+
#ifndef HAVE_IN6_PTON
384398
#define hex_to_bin compat_hex_to_bin
385399
/* lib/hexdump.c */
386-
int hex_to_bin(char ch)
400+
static inline int hex_to_bin(char ch)
387401
{
388402
if ((ch >= '0') && (ch <= '9'))
389403
return ch - '0';
@@ -593,7 +607,7 @@ int in6_pton(const char *src, int srclen,
593607
*end = s;
594608
return ret;
595609
}
596-
#endif /* IN6PTON_XDIGIT */
610+
#endif /* HAVE_IN6_PTON */
597611

598612
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,2,0)
599613
# define sock_create_kern(f, t, p, s) sock_create_kern(&init_net, f, t, p, s)
@@ -712,40 +726,6 @@ static inline void do_gettimeofday(struct timeval *tv)
712726
}
713727
#endif
714728

715-
#define TOLOWER(x) ((x) | 0x20)
716-
unsigned long long strtoul(const char *cp, char **endp, unsigned int base)
717-
{
718-
unsigned long long result = 0;
719-
720-
if (!base) {
721-
if (cp[0] == '0') {
722-
if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
723-
base = 16;
724-
else
725-
base = 8;
726-
} else {
727-
base = 10;
728-
}
729-
}
730-
731-
if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
732-
cp += 2;
733-
734-
while (isxdigit(*cp)) {
735-
unsigned int value;
736-
737-
value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
738-
if (value >= base)
739-
break;
740-
result = result * base + value;
741-
cp++;
742-
}
743-
if (endp)
744-
*endp = (char *)cp;
745-
746-
return result;
747-
}
748-
749729
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,12,0)
750730
/*
751731
* find_module() is unexported in v5.12:

gen_compat_def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ kbuild_test_ref totalram_pages linux/mm.h
129129
kbuild_test_member nf_ct_event_notifier.ct_event net/netfilter/nf_conntrack_ecache.h
130130
# 6.4: 0199849acd07 ("sysctl: remove register_sysctl_paths()")
131131
kbuild_test_symbol register_sysctl_paths linux/sysctl.h
132+
# 6.8: d26270061ae6 ("string: Remove strlcpy()")
133+
kbuild_test_symbol strlcpy linux/string.h
134+
# 2.6.18 lacks in6_pton and in4_pton
135+
kbuild_test_symbol in6_pton linux/inet.h
132136

133137
echo "// End of compat_def.h"
134138

ipt_NETFLOW.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#include <linux/in6.h>
3030
#include <linux/inet.h>
3131
#include <linux/kernel.h>
32+
#if LINUX_VERSION_CODE > KERNEL_VERSION(5,10,0)
33+
#include <linux/kstrtox.h>
34+
#endif
3235
#include <linux/ip.h>
3336
#include <linux/udp.h>
3437
#include <linux/icmp.h>
@@ -2396,7 +2399,7 @@ static int add_destinations(const char *ptr)
23962399
++end;
23972400
if (succ &&
23982401
(*end == ':' || *end == '.' || *end == 'p' || *end == '#'))
2399-
sin6->sin6_port = htons(strtoul(++end, (char **)&end, 0));
2402+
sin6->sin6_port = htons(simple_strtoul(++end, (char **)&end, 0));
24002403
if (succ && *end == '@') {
24012404
++end;
24022405
sout->sin6_family = AF_INET6;
@@ -2411,7 +2414,7 @@ static int add_destinations(const char *ptr)
24112414
sin->sin_port = htons(2055);
24122415
succ = in4_pton(ptr, len, (u8 *)&sin->sin_addr, -1, &end);
24132416
if (succ && *end == ':')
2414-
sin->sin_port = htons(strtoul(++end, (char **)&end, 0));
2417+
sin->sin_port = htons(simple_strtoul(++end, (char **)&end, 0));
24152418
if (succ && *end == '@') {
24162419
++end;
24172420
sout->sin_family = AF_INET;

0 commit comments

Comments
 (0)