Skip to content

Commit 14f9009

Browse files
committed
fix(nft): route root fake-ip traffic through box_main
1 parent 66dbf73 commit 14f9009

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

lib/firewall/backend_nft.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set -euo pipefail
66

77
BOX_NFT_TABLE_INET="box_mangle"
88
BOX_NFT_TABLE_IP="box_nat"
9+
BOX_MIHOMO_FAKEIP_V4_CIDR="198.18.0.0/16"
910

1011
nft_cmd() {
1112
if [[ -n "${BOX_NFT_CMD:-}" ]]; then
@@ -286,6 +287,8 @@ add chain inet ${BOX_NFT_TABLE_INET} output { type route hook output priority ma
286287
add chain inet ${BOX_NFT_TABLE_INET} box_main
287288
add chain inet ${BOX_NFT_TABLE_INET} box_dns
288289
add rule inet ${BOX_NFT_TABLE_INET} prerouting jump box_main
290+
$(if [[ "${BOX_DNS_ENHANCED_MODE}" == "fake-ip" ]]; then printf 'add rule inet %s output meta skuid 0 ip daddr %s jump box_main\n' "${BOX_NFT_TABLE_INET}" "${BOX_MIHOMO_FAKEIP_V4_CIDR}"; fi)
291+
add rule inet ${BOX_NFT_TABLE_INET} output meta skuid 0 jump box_dns
289292
add rule inet ${BOX_NFT_TABLE_INET} output meta skuid 0 return
290293
add rule inet ${BOX_NFT_TABLE_INET} output jump box_main
291294
add rule inet ${BOX_NFT_TABLE_INET} box_main jump box_dns
@@ -297,6 +300,8 @@ add chain ip ${BOX_NFT_TABLE_IP} output { type nat hook output priority -100; po
297300
add chain ip ${BOX_NFT_TABLE_IP} box_main
298301
add chain ip ${BOX_NFT_TABLE_IP} box_dns
299302
add rule ip ${BOX_NFT_TABLE_IP} prerouting jump box_main
303+
$(if [[ "${BOX_DNS_ENHANCED_MODE}" == "fake-ip" ]]; then printf 'add rule ip %s output meta skuid 0 ip daddr %s jump box_main\n' "${BOX_NFT_TABLE_IP}" "${BOX_MIHOMO_FAKEIP_V4_CIDR}"; fi)
304+
add rule ip ${BOX_NFT_TABLE_IP} output meta skuid 0 jump box_dns
300305
add rule ip ${BOX_NFT_TABLE_IP} output meta skuid 0 return
301306
add rule ip ${BOX_NFT_TABLE_IP} output jump box_main
302307
add rule ip ${BOX_NFT_TABLE_IP} box_main jump box_dns

tests/integration/test_phase2.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,12 @@ assert_contains "${dryrun_output}" "add rule inet box_mangle box_main ip daddr @
449449
assert_contains "${dryrun_output}" "add rule ip box_nat box_main ip daddr @box_cn_v4 return"
450450
assert_line_order "${dryrun_output}" "add rule inet box_mangle box_main ip daddr @box_cn_v4 return" 'add rule inet box_mangle box_main return comment "BOX_POLICY_PLACEHOLDER"'
451451
assert_line_order "${dryrun_output}" "meta mark set 16777216" 'add rule inet box_mangle box_main return comment "BOX_POLICY_PLACEHOLDER"'
452+
assert_contains "${dryrun_output}" "add rule inet box_mangle output meta skuid 0 ip daddr 198.18.0.0/16 jump box_main"
453+
assert_contains "${dryrun_output}" "add rule ip box_nat output meta skuid 0 ip daddr 198.18.0.0/16 jump box_main"
454+
assert_line_order "${dryrun_output}" "add rule inet box_mangle output meta skuid 0 ip daddr 198.18.0.0/16 jump box_main" "add rule inet box_mangle output meta skuid 0 return"
455+
assert_line_order "${dryrun_output}" "add rule ip box_nat output meta skuid 0 ip daddr 198.18.0.0/16 jump box_main" "add rule ip box_nat output meta skuid 0 return"
456+
assert_line_order "${dryrun_output}" "add rule inet box_mangle output meta skuid 0 jump box_dns" "add rule inet box_mangle output meta skuid 0 return"
457+
assert_line_order "${dryrun_output}" "add rule ip box_nat output meta skuid 0 jump box_dns" "add rule ip box_nat output meta skuid 0 return"
452458

453459
printf '[6/13] trace mode logs external commands with action context\n'
454460
BOX_TRACE_COMMANDS=1

0 commit comments

Comments
 (0)