Skip to content

Commit 71bec28

Browse files
committed
hm2_eth: print iptables commands when running unprivileged
Previously when iptables was unavailable (rootless via 'make setcap', or no_iptables=1), the driver logged a single line saying isolation was disabled and nothing else. The user had no way to recover the exact rules they would need to install manually, and hm2_eth(9) does not document them. Refactor install_iptables_rule() to a print-or-exec dual mode: build the command unconditionally, then either shell it (when iptables is available) or LL_PRINT it as advice (when not). Drop the outer use_iptables() gates on the setup paths in start_board() and rtapi_app_main() so the print branch is reached. Cleanup paths keep their gate since there is nothing to print on teardown. The probe in use_iptables() now also emits the chain-setup commands (iptables -N <CHAIN>; iptables -I OUTPUT 1 -j <CHAIN>) at the time it discovers iptables is unreachable, so the user gets a complete recipe: chain setup once, plus per-board / per-interface rules logged inline as boards come up. Reported by @hdiethelm in PR #3964 review while testing 7I96S under 'make setcap'.
1 parent 5118840 commit 71bec28

1 file changed

Lines changed: 27 additions & 12 deletions

File tree

src/hal/drivers/mesa-hostmot2/hm2_eth.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,13 @@ static bool use_iptables() {
489489
// leaving side effects.
490490
if(shell(IPTABLES " -n -L INPUT > /dev/null 2>&1") != EXIT_SUCCESS) {
491491
LL_PRINT("iptables is not available to this process "
492-
"(running unprivileged?); skipping automatic rule "
493-
"installation. Configure firewall externally to "
494-
"isolate the hm2-eth interface.\n");
492+
"(running unprivileged?); automatic rule "
493+
"installation will be skipped. To enable hm2-eth "
494+
"isolation manually, run the following as root, "
495+
"plus the per-board and per-interface rules logged "
496+
"below as boards come up:\n"
497+
" " IPTABLES " -N " CHAIN "\n"
498+
" " IPTABLES " -I OUTPUT 1 -j " CHAIN "\n");
495499
return (iptables_state = 0);
496500
}
497501
if(!chain_exists()) {
@@ -585,6 +589,13 @@ static int install_iptables_rule(const char *fmt, ...) {
585589
return -ENOSPC;
586590
}
587591

592+
// When iptables is unavailable (rootless / no_iptables=1), emit the
593+
// command for the user to run manually rather than skipping silently.
594+
if(!use_iptables()) {
595+
LL_PRINT(" %s\n", commandbuf);
596+
return 0;
597+
}
598+
588599
int res = shell(commandbuf);
589600
if(res == EXIT_SUCCESS) return 0;
590601

@@ -629,8 +640,12 @@ static int install_iptables_perinterface(const char *ifbuf) {
629640
ifbuf);
630641
if(res < 0) return res;
631642

632-
res = eshellf(HM2_LLIO_NAME, "/sbin/sysctl -q net.ipv6.conf.%s.disable_ipv6=1", ifbuf);
633-
if(res < 0) return res;
643+
if(use_iptables()) {
644+
res = eshellf(HM2_LLIO_NAME, "/sbin/sysctl -q net.ipv6.conf.%s.disable_ipv6=1", ifbuf);
645+
if(res < 0) return res;
646+
} else {
647+
LL_PRINT(" /sbin/sysctl -q net.ipv6.conf.%s.disable_ipv6=1\n", ifbuf);
648+
}
634649

635650
return 0;
636651
}
@@ -734,11 +749,11 @@ static int init_board(hm2_eth_t *board, const char *board_ip) {
734749
return -errno;
735750
}
736751

737-
if(use_iptables())
738-
{
739-
ret = install_iptables_board(board->sockfd);
740-
if(ret < 0) return ret;
741-
}
752+
// install_iptables_board() falls through to a print-only mode when
753+
// iptables is not available (see install_iptables_rule()), so it is
754+
// safe to call unconditionally.
755+
ret = install_iptables_board(board->sockfd);
756+
if(ret < 0) return ret;
742757

743758
board->write_packet_ptr = board->write_packet;
744759
board->read_packet_ptr = board->read_packet;
@@ -1620,8 +1635,8 @@ int rtapi_app_main(void) {
16201635
if(!added)
16211636
goto error;
16221637
if(*added) continue;
1623-
if(use_iptables())
1624-
install_iptables_perinterface(ifptr);
1638+
// Print-or-exec depending on iptables availability; see install_iptables_rule().
1639+
install_iptables_perinterface(ifptr);
16251640
*added = 1;
16261641
}
16271642

0 commit comments

Comments
 (0)