Skip to content

Commit 7cc2a84

Browse files
rsalvaterrajow-
authored andcommitted
defaults: robustify flow table detection.
The flow table detection fails if the respective target module is built into the kernel, since it's looking for the module itself. Create a generic helper and instead check for existence of the FLOWOFFLOAD target in /proc/net/ip_tables_targets. Signed-off-by: Rui Salvaterra <[email protected]> [slightly reword commit message] Signed-off-by: Jo-Philipp Wich <[email protected]>
1 parent 8174814 commit 7cc2a84

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

defaults.c

+7-18
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,14 @@ check_policy(struct uci_element *e, enum fw3_flag *pol, const char *name)
8585
}
8686

8787
static void
88-
check_kmod(struct uci_element *e, bool *module, const char *name)
88+
check_target(struct uci_element *e, bool *available, const char *target, const bool ipv6)
8989
{
90-
FILE *f;
91-
char buf[128];
92-
93-
if (!*module)
94-
return;
95-
96-
snprintf(buf, sizeof(buf), "/sys/module/%s/refcnt", name);
97-
98-
f = fopen(buf, "r");
99-
100-
if (f)
90+
const bool b = fw3_has_target(ipv6, target);
91+
if (!b)
10192
{
102-
fclose(f);
103-
return;
93+
warn_elem(e, "requires unavailable target extension %s, disabling", target);
10494
}
105-
106-
warn_elem(e, "requires not available kernel module %s, disabling", name);
107-
*module = false;
95+
*available = b;
10896
}
10997

11098
static void
@@ -171,7 +159,8 @@ fw3_load_defaults(struct fw3_state *state, struct uci_package *p)
171159

172160
check_any_reject_code(e, &defs->any_reject_code);
173161

174-
check_kmod(e, &defs->flow_offloading, "xt_FLOWOFFLOAD");
162+
/* exists in both ipv4 and ipv6, if at all, so only check ipv4 */
163+
check_target(e, &defs->flow_offloading, "FLOWOFFLOAD", false);
175164
}
176165
}
177166

utils.c

+27
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,33 @@ fw3_has_table(bool ipv6, const char *table)
344344
return seen;
345345
}
346346

347+
bool
348+
fw3_has_target(const bool ipv6, const char *target)
349+
{
350+
FILE *f;
351+
352+
char line[12];
353+
bool seen = false;
354+
355+
const char *path = ipv6
356+
? "/proc/net/ip6_tables_targets" : "/proc/net/ip_tables_targets";
357+
358+
if (!(f = fopen(path, "r")))
359+
return false;
360+
361+
while (fgets(line, sizeof(line), f))
362+
{
363+
if (!strcmp(line, target))
364+
{
365+
seen = true;
366+
break;
367+
}
368+
}
369+
370+
fclose(f);
371+
372+
return seen;
373+
}
347374

348375
bool
349376
fw3_lock_path(int *fd, const char *path)

utils.h

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ void fw3_pr(const char *fmt, ...)
105105

106106
bool fw3_has_table(bool ipv6, const char *table);
107107

108+
bool fw3_has_target(const bool ipv6, const char *target);
109+
108110
bool fw3_lock(void);
109111
void fw3_unlock(void);
110112
bool fw3_lock_path(int *fw3_lock_fd, const char *path);

0 commit comments

Comments
 (0)