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
5 changes: 4 additions & 1 deletion files/etc/init.d/pbr
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@ start_service() {
# _pbr_ifaces_triggers — space-separated iface list, consumed by service_triggers()
# _pbr_svc_data — json_add_* command block, eval'd by service_data() (empty on degraded start)
# _pbr_deferred_to_boot — '1' when ucode bailed due to uplink-not-ready
# _pbr_retry_on_boot — '1' when the environment check failed: its
# errors are in _pbr_svc_data, and the start is
# retried like a bail-out (boot-mode trigger)
# All must be set BEFORE procd invokes service_triggers / service_data
# so the trigger registration and data block see them.
eval "$_pbr_svc_defs"
if [ -n "$_pbr_deferred_to_boot" ]; then
if [ -n "$_pbr_deferred_to_boot" ] || [ -n "$_pbr_retry_on_boot" ]; then
[ ! -s "/var/run/${packageName}.lock" ] && touch "/var/run/${packageName}.boot"
fi
# Degraded start: drop the service_data hook so procd's _procd_close_service
Expand Down
4 changes: 3 additions & 1 deletion files/lib/pbr/nft.uc
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,10 @@ function create_nft(fs_mod, config, sh, output, pkg, platform, network, V, state
}

function nft_check_element(element_type, name) {
// stderr is dropped: with 2>&1 a missing table left nft's error text
// in the dump, which made the table look present.
if (!nft_fw4_dump)
nft_fw4_dump = sh.exec('nft list table inet fw4 2>&1');
nft_fw4_dump = sh.exec('nft list table inet fw4 2>/dev/null');
if (element_type == 'table' && name == 'fw4')
return !!(nft_fw4_dump && nft_fw4_dump != '');
let lines = split(nft_fw4_dump, '\n');
Expand Down
46 changes: 33 additions & 13 deletions files/lib/pbr/pbr.uc
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ function create_pbr(fs_mod, uci_mod, ubus_mod) {
let _config_loaded = false;
let _loaded = false;
let _deferred_to_boot = false;
let _health_failed = false;

// ── Interface Registry Helpers ──────────────────────────────────────

function reset() {
state.errors = [];
state.warnings = [];
_deferred_to_boot = false;
_health_failed = false;
for (let k in keys(iface_registry))
delete iface_registry[k];
}
Expand Down Expand Up @@ -163,20 +165,22 @@ function create_pbr(fs_mod, uci_mod, ubus_mod) {
health_fail = true;
}
if (!nft.nft_check_element('table', 'fw4')) {
// Without the table its chains are missing too; one error says it.
push(state.errors, { code: 'errorDefaultFw4TableMissing', info: 'fw4' });
health_fail = true;
}
if (net.is_config_enabled('dns_policy') || net.is_tor_configured()) {
if (!nft.nft_check_element('chain', 'dstnat')) {
push(state.errors, { code: 'errorDefaultFw4ChainMissing', info: 'dstnat' });
health_fail = true;
} else {
if (net.is_config_enabled('dns_policy') || net.is_tor_configured()) {
if (!nft.nft_check_element('chain', 'dstnat')) {
push(state.errors, { code: 'errorDefaultFw4ChainMissing', info: 'dstnat' });
health_fail = true;
}
}
}
let chains = split(pkg.chains_list, ' ');
for (let c in chains) {
if (!nft.nft_check_element('chain', 'mangle_' + c)) {
push(state.errors, { code: 'errorDefaultFw4ChainMissing', info: 'mangle_' + c });
health_fail = true;
let chains = split(pkg.chains_list, ' ');
for (let c in chains) {
if (!nft.nft_check_element('chain', 'mangle_' + c)) {
push(state.errors, { code: 'errorDefaultFw4ChainMissing', info: 'mangle_' + c });
health_fail = true;
}
}
}
if (cfg.resolver_set == 'dnsmasq.nftset' && !env.resolver_set_supported) {
Expand Down Expand Up @@ -238,6 +242,7 @@ function create_pbr(fs_mod, uci_mod, ubus_mod) {
output.logger_debug(cfg.debug_performance, '[PERF-DEBUG] Detecting environment took ' + (end_time - start_time) + 's');
if (!_check_system_health()) {
output.failn();
_health_failed = true;
return false;
}
start_time = time();
Expand Down Expand Up @@ -2006,6 +2011,18 @@ function create_pbr(fs_mod, uci_mod, ubus_mod) {

reset();
if (!load(param)) {
// A failed environment check: hand its errors to procd, so that
// service_started(), status and the WebUI can say what is wrong
// (null left them in state.errors only), and keep the boot-mode
// retry the wrapper sets up for a null result.
if (_health_failed)
return {
packageCompat: +pkg.compat,
version: pkg.version,
retryOnBoot: true,
errors: state.errors,
warnings: state.warnings,
};
return null;
}
// Enumerate interfaces BEFORE is_wan_up — interface_enumerate /
Expand Down Expand Up @@ -2309,7 +2326,9 @@ function create_pbr(fs_mod, uci_mod, ubus_mod) {
output.verbose.write(pkg.service_name + ' is monitoring interfaces:\\n' + gw_summary + '\\n');
} else {
output.print(pkg.service_name + ' FAILED TO START!!!\\n');
output.print('Check the output of nft -c -f ' + pkg.nft_temp_file + '\\n');
// The errors below say why; the nft hint is for when none do.
if (!length(svc_data?.errors || []))
output.print('Check the output of nft -c -f ' + pkg.nft_temp_file + '\\n');
}

let warnings = svc_data?.warnings || [];
Expand Down Expand Up @@ -2344,7 +2363,8 @@ function create_pbr(fs_mod, uci_mod, ubus_mod) {
// procd triggers can be registered on every start, including the
// degraded path where ucode bailed because the uplink wasn't ready.
let preamble = '_pbr_ifaces_triggers=' + sh.quote(data.ifacesTriggers || '') + '\n' +
'_pbr_deferred_to_boot=' + sh.quote(data.deferredToBoot ? '1' : '') + '\n';
'_pbr_deferred_to_boot=' + sh.quote(data.deferredToBoot ? '1' : '') + '\n' +
'_pbr_retry_on_boot=' + sh.quote(data.retryOnBoot ? '1' : '') + '\n';

// Degraded start: skip the per-instance data block entirely
// (empty _pbr_svc_data — wrapper will unset its service_data hook,
Expand Down
59 changes: 59 additions & 0 deletions tests/02_config/12_fw4_table_missing
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Testing that a missing fw4 table is reported, reported once, and reaches procd.

With no 'inet fw4' table (the firewall is not loaded, or firewall3 replaced
firewall4), 'nft list table inet fw4' prints nothing on stdout and an error
on stderr. Two bugs hid the cause:

1. The table check captured the dump with 2>&1, so the error text made the
table look present and the chain checks failed on it instead (three
"Default fw4 chain 'mangle_...' is missing"). stderr is now dropped, and
the chain checks are skipped when the table itself is missing.
2. A failed environment check made start_service() return null, so its
errors stayed in state.errors: the user saw "Loading environment [x]" and
"FAILED TO START" with no reason, in the log, in status and in the WebUI.
It now returns the errors, which emit_procd_shell() writes into the
service data, plus retryOnBoot so the wrapper still sets up the
boot-mode retry it used to set up for null.

The empty fixture below is what the check reads when there is no table.

-- File fs/popen~nft_list_table_inet_fw4_2_dev_null.txt --
-- End --

-- Testcase --
import create_pbr from 'pbr';
let pbr = create_pbr();

let result = pbr.start_service(['on_start']);
let errors = result?.errors || [];
let shell = pbr.emit_procd_shell(result) || '';

function count(code) {
return length(filter(errors, e => e.code == code));
}

let checks = [
['result_returned', type(result) == 'object'],
['table_missing_reported', count('errorDefaultFw4TableMissing') == 1],
['no_chain_errors', count('errorDefaultFw4ChainMissing') == 0],
['retry_on_boot', result?.retryOnBoot == true],
['not_deferred', !result?.deferredToBoot],
['shell_retry_flag', index(shell, "_pbr_retry_on_boot='1'") >= 0],
['shell_has_error', index(shell, 'errorDefaultFw4TableMissing') >= 0],
];

let pass = 0, fail = 0;
for (let c in checks) {
if (c[1]) {
pass++;
} else {
fail++;
printf("FAIL: %s\n", c[0]);
}
}
printf("fw4_table_missing: %d/%d passed\n", pass, pass + fail);
-- End --

-- Expect stdout --
fw4_table_missing: 7/7 passed
-- End --
17 changes: 17 additions & 0 deletions tests/03_nft_rules/08_set_reload_window
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ policy that no longer exists.

-- File fs/popen~nft_list_table_inet_fw4_2_dev_null.txt --
table inet fw4 {
chain dstnat {
type nat hook prerouting priority dstnat; policy accept;
}


chain mangle_prerouting {
type filter hook prerouting priority mangle; policy accept;
}

chain mangle_output {
type route hook output priority mangle; policy accept;
}

chain mangle_forward {
type filter hook forward priority mangle; policy accept;
}

chain forward {
type filter hook forward priority filter; policy drop;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/03_nft_rules/14_names_from_rule_comments
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ table below carries one real orphan set, so the reap is known to have run.

-- File fs/popen~nft_list_table_inet_fw4_2_dev_null.txt --
table inet fw4 {
chain dstnat {
type nat hook prerouting priority dstnat; policy accept;
}


chain mangle_prerouting {
type filter hook prerouting priority mangle; policy accept;
}

chain mangle_output {
type route hook output priority mangle; policy accept;
}

chain mangle_forward {
type filter hook forward priority mangle; policy accept;
}

set pbr_wan_4_dst_ip_policy_gone {
type ipv4_addr
elements = { 203.0.113.7 }
Expand Down
Loading