Skip to content

Commit eaefd3b

Browse files
author
Vadim Fedorenko
committed
ipt_NETFLOW: add compatibility with 6.8+
* replace strlcpy with strscpy as strlcpy 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 <vvfedorenko@github.com>
1 parent 0eb2092 commit eaefd3b

File tree

3 files changed

+23
-46
lines changed

3 files changed

+23
-46
lines changed

compat.h

Lines changed: 8 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,10 @@ unsigned long timeval_to_jiffies(const struct timeval *tv)
225225
}
226226
#endif
227227

228+
#if !defined(HAVE_STRSCPY) && !defined(strscpy)
229+
#define strscpy strlcpy
230+
#endif
231+
228232
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
229233
# ifdef ktime_to_timeval
230234
/* ktime_to_timeval is defined on 64bit and inline on 32bit cpu */
@@ -380,10 +384,10 @@ static int sockaddr_cmp(const struct sockaddr_storage *sa1, const struct sockadd
380384
return 0;
381385
}
382386

383-
#ifndef IN6PTON_XDIGIT
387+
#ifndef HAVE_IN6_PTON
384388
#define hex_to_bin compat_hex_to_bin
385389
/* lib/hexdump.c */
386-
int hex_to_bin(char ch)
390+
static inline int hex_to_bin(char ch)
387391
{
388392
if ((ch >= '0') && (ch <= '9'))
389393
return ch - '0';
@@ -593,7 +597,7 @@ int in6_pton(const char *src, int srclen,
593597
*end = s;
594598
return ret;
595599
}
596-
#endif /* IN6PTON_XDIGIT */
600+
#endif /* HAVE_IN6_PTON */
597601

598602
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,2,0)
599603
# define sock_create_kern(f, t, p, s) sock_create_kern(&init_net, f, t, p, s)
@@ -712,40 +716,6 @@ static inline void do_gettimeofday(struct timeval *tv)
712716
}
713717
#endif
714718

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-
749719
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,12,0)
750720
/*
751721
* 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 strscpy 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: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
#include <linux/in6.h>
3030
#include <linux/inet.h>
3131
#include <linux/kernel.h>
32+
#include <linux/version.h>
33+
#if LINUX_VERSION_CODE > KERNEL_VERSION(5,10,0)
34+
#include <linux/kstrtox.h>
35+
#endif
3236
#include <linux/ip.h>
3337
#include <linux/udp.h>
3438
#include <linux/icmp.h>
@@ -67,7 +71,6 @@
6771
# include <net/netfilter/nf_conntrack.h>
6872
# include <net/netfilter/nf_conntrack_core.h>
6973
#endif
70-
#include <linux/version.h>
7174
#include <asm/unaligned.h>
7275
#ifdef HAVE_LLIST
7376
/* llist.h is officially defined since linux 3.1,
@@ -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;
@@ -4087,7 +4090,7 @@ static int ethtool_drvinfo(unsigned char *ptr, size_t size, struct net_device *d
40874090
ops->get_drvinfo(dev, &info);
40884091
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
40894092
else if (dev->dev.parent && dev->dev.parent->driver) {
4090-
strlcpy(info.driver, dev->dev.parent->driver->name, sizeof(info.driver));
4093+
strscpy(info.driver, dev->dev.parent->driver->name, sizeof(info.driver));
40914094
}
40924095
#endif
40934096
n = scnprintf(ptr, len, "%s", info.driver);
@@ -5684,7 +5687,7 @@ static int __init ipt_netflow_init(void)
56845687
if (!destination)
56855688
destination = destination_buf;
56865689
if (destination != destination_buf) {
5687-
strlcpy(destination_buf, destination, sizeof(destination_buf));
5690+
strscpy(destination_buf, destination, sizeof(destination_buf));
56885691
destination = destination_buf;
56895692
}
56905693
if (add_destinations(destination) < 0)
@@ -5694,7 +5697,7 @@ static int __init ipt_netflow_init(void)
56945697
if (!aggregation)
56955698
aggregation = aggregation_buf;
56965699
if (aggregation != aggregation_buf) {
5697-
strlcpy(aggregation_buf, aggregation, sizeof(aggregation_buf));
5700+
strscpy(aggregation_buf, aggregation, sizeof(aggregation_buf));
56985701
aggregation = aggregation_buf;
56995702
}
57005703
add_aggregation(aggregation);
@@ -5704,7 +5707,7 @@ static int __init ipt_netflow_init(void)
57045707
if (!sampler)
57055708
sampler = sampler_buf;
57065709
if (sampler != sampler_buf) {
5707-
strlcpy(sampler_buf, sampler, sizeof(sampler_buf));
5710+
strscpy(sampler_buf, sampler, sizeof(sampler_buf));
57085711
sampler = sampler_buf;
57095712
}
57105713
parse_sampler(sampler);
@@ -5721,7 +5724,7 @@ static int __init ipt_netflow_init(void)
57215724
if (!snmp_rules)
57225725
snmp_rules = snmp_rules_buf;
57235726
if (snmp_rules != snmp_rules_buf) {
5724-
strlcpy(snmp_rules_buf, snmp_rules, sizeof(snmp_rules_buf));
5727+
strscpy(snmp_rules_buf, snmp_rules, sizeof(snmp_rules_buf));
57255728
snmp_rules = snmp_rules_buf;
57265729
}
57275730
add_snmp_rules(snmp_rules);

0 commit comments

Comments
 (0)