fix: align macOS default route detection across network setup

This commit is contained in:
Kevin Yue committed 2026-11-01 23:35:57 +08:00
1 parent c53911e7b8
commit b4529398b0
2 files changed
+228 -7

No files matched your search

+22 -3
View File
@@ -259,6 +259,10 @@ do_ifconfig() {
# =========== route handling ==================================== # =========== route handling ====================================
is_ipv4_default_route() {
[ "$1" = "0.0.0.0" ] && [ "$2" = "0" ]
}
if [ -n "$IPROUTE" ]; then if [ -n "$IPROUTE" ]; then
fix_ip_get_output () { fix_ip_get_output () {
sed -e 's/ /\n/g' | \ sed -e 's/ /\n/g' | \
@@ -641,7 +645,7 @@ macos_dns_source_service() {
} }
configure_macos_dns() { configure_macos_dns() {
local search_domains ipv4 local search_domains ipv4 default_route i network prefix
if [ -z "$INTERNAL_IP4_DNS" ]; then if [ -z "$INTERNAL_IP4_DNS" ]; then
remove_macos_dns remove_macos_dns
return $? return $?
@@ -650,7 +654,22 @@ configure_macos_dns() {
if [ -n "$CISCO_DEF_DOMAIN" ]; then if [ -n "$CISCO_DEF_DOMAIN" ]; then
search_domains="d.add SearchDomains * $CISCO_DEF_DOMAIN" search_domains="d.add SearchDomains * $CISCO_DEF_DOMAIN"
fi fi
# Match do_connect: an include list can itself contain the default route.
default_route=1
if [ -n "$CISCO_SPLIT_INC" ]; then if [ -n "$CISCO_SPLIT_INC" ]; then
default_route=0
i=0
while [ "$i" -lt "$CISCO_SPLIT_INC" ]; do
eval network="\${CISCO_SPLIT_INC_${i}_ADDR}"
eval prefix="\${CISCO_SPLIT_INC_${i}_MASKLEN}"
if is_ipv4_default_route "$network" "$prefix"; then
default_route=1
break
fi
i=$((i + 1))
done
fi
if [ "$default_route" -eq 0 ]; then
local source local source
source=$(macos_dns_source_service) || return 1 source=$(macos_dns_source_service) || return 1
# A split tunnel has no default router. Like GlobalProtect, register # A split tunnel has no default router. Like GlobalProtect, register
@@ -1004,7 +1023,7 @@ do_connect() {
eval NETWORK="\${CISCO_SPLIT_INC_${i}_ADDR}" eval NETWORK="\${CISCO_SPLIT_INC_${i}_ADDR}"
eval NETMASK="\${CISCO_SPLIT_INC_${i}_MASK}" eval NETMASK="\${CISCO_SPLIT_INC_${i}_MASK}"
eval NETMASKLEN="\${CISCO_SPLIT_INC_${i}_MASKLEN}" eval NETMASKLEN="\${CISCO_SPLIT_INC_${i}_MASKLEN}"
if [ "$NETWORK" != "0.0.0.0" ]; then if ! is_ipv4_default_route "$NETWORK" "$NETMASKLEN"; then
set_network_route "$NETWORK" "$NETMASK" "$NETMASKLEN" "$TUNDEV" set_network_route "$NETWORK" "$NETMASK" "$NETMASKLEN" "$TUNDEV"
else else
set_default_route set_default_route
@@ -1043,7 +1062,7 @@ do_disconnect() {
eval NETWORK="\${CISCO_SPLIT_INC_${i}_ADDR}" eval NETWORK="\${CISCO_SPLIT_INC_${i}_ADDR}"
eval NETMASK="\${CISCO_SPLIT_INC_${i}_MASK}" eval NETMASK="\${CISCO_SPLIT_INC_${i}_MASK}"
eval NETMASKLEN="\${CISCO_SPLIT_INC_${i}_MASKLEN}" eval NETMASKLEN="\${CISCO_SPLIT_INC_${i}_MASKLEN}"
if [ "$NETWORK" != "0.0.0.0" ]; then if ! is_ipv4_default_route "$NETWORK" "$NETMASKLEN"; then
# FIXME: This doesn't restore previously overwritten # FIXME: This doesn't restore previously overwritten
# routes. # routes.
del_network_route "$NETWORK" "$NETMASK" "$NETMASKLEN" "$TUNDEV" del_network_route "$NETWORK" "$NETMASK" "$NETMASKLEN" "$TUNDEV"
+206 -4
View File
@@ -19,6 +19,13 @@ fn dns_script() -> String {
.split_once("# =========== resolv.conf handling for other OSes =========================") .split_once("# =========== resolv.conf handling for other OSes =========================")
.unwrap() .unwrap()
.0; .0;
let route_detection = VPNC_SCRIPT
.split_once("# =========== route handling ====================================")
.unwrap()
.1
.split_once("if [ -n \"$IPROUTE\" ]; then")
.unwrap()
.0;
format!( format!(
r#" r#"
state_dir=$(mktemp -d) || exit 1 state_dir=$(mktemp -d) || exit 1
@@ -28,7 +35,10 @@ OS=Darwin
INTERNAL_IP4_ADDRESS=10.1.2.3 INTERNAL_IP4_ADDRESS=10.1.2.3
INTERNAL_IP4_DNS='10.0.0.53 10.0.0.54' INTERNAL_IP4_DNS='10.0.0.53 10.0.0.54'
CISCO_DEF_DOMAIN='corp.example.com internal.example.com' CISCO_DEF_DOMAIN='corp.example.com internal.example.com'
CISCO_SPLIT_INC=0 CISCO_SPLIT_INC=1
CISCO_SPLIT_INC_0_ADDR=10.0.0.0
CISCO_SPLIT_INC_0_MASK=255.0.0.0
CISCO_SPLIT_INC_0_MASKLEN=8
printf 'physical-service\n' > "$state_dir/primary-service" printf 'physical-service\n' > "$state_dir/primary-service"
printf '%s\n' 'd.add Addresses * 192.168.1.100' 'd.add SubnetMasks * 255.255.255.0' \ printf '%s\n' 'd.add Addresses * 192.168.1.100' 'd.add SubnetMasks * 255.255.255.0' \
'd.add InterfaceName en0' 'd.add Router 192.168.1.1' > "$state_dir/physical-ipv4" 'd.add InterfaceName en0' 'd.add Router 192.168.1.1' > "$state_dir/physical-ipv4"
@@ -65,10 +75,17 @@ $command" ;;
sed -n 's/^d.add GPConnectSourceService / GPConnectSourceService : /p' "$state_dir/IPv4" sed -n 's/^d.add GPConnectSourceService / GPConnectSourceService : /p' "$state_dir/IPv4"
;; ;;
'get State:/Network/Service/physical-service/IPv4') 'get State:/Network/Service/physical-service/IPv4')
dictionary=$(cat "$state_dir/physical-ipv4") if [ -f "$state_dir/physical-ipv4" ]; then
dictionary=$(cat "$state_dir/physical-ipv4")
else
echo ' No such key'
fi
;;
'get State:/Network/Service/ethernet-service/IPv4')
dictionary=$(cat "$state_dir/ethernet-ipv4")
;; ;;
'remove State:/Network/Service/utun42/'*) 'remove State:/Network/Service/utun42/'*)
if [ "$scutil_failure" = remove ]; then if [ "$scutil_failure" = remove ] || [ "$scutil_failure" = "remove-${{command##*/}}" ]; then
echo ' Permission denied' echo ' Permission denied'
else else
rm "$state_dir/${{command##*/}}" || return 1 rm "$state_dir/${{command##*/}}" || return 1
@@ -80,6 +97,7 @@ $command" ;;
done done
}} }}
{selection} {selection}
{route_detection}
{functions} {functions}
"# "#
) )
@@ -131,6 +149,44 @@ grep -Fx 'd.add Router 10.1.2.3' "$state_dir/IPv4" || exit 12
)); ));
} }
#[test]
fn included_default_route_registers_tunnel_ipv4_for_dns() {
for default_index in [0, 1] {
assert_success(run_dns_test(&format!(
r#"
CISCO_SPLIT_INC=2
CISCO_SPLIT_INC_0_ADDR=10.0.0.0
CISCO_SPLIT_INC_1_ADDR=192.168.50.0
CISCO_SPLIT_INC_{default_index}_ADDR=0.0.0.0
CISCO_SPLIT_INC_{default_index}_MASK=0.0.0.0
CISCO_SPLIT_INC_{default_index}_MASKLEN=0
configure_macos_dns || exit 1
grep -Fx 'd.add InterfaceName utun42' "$state_dir/IPv4" || exit 2
grep -Fx 'd.add Router 10.1.2.3' "$state_dir/IPv4" || exit 3
grep -Fx 'd.add Addresses * 10.1.2.3' "$state_dir/IPv4" || exit 4
grep -q GPConnectSourceService "$state_dir/IPv4" && exit 5
cmp "$state_dir/physical-dns" "$state_dir/original-physical-dns" || exit 6
"#,
)));
}
}
#[test]
fn included_subnet_routes_preserve_physical_ipv4_for_dns() {
assert_success(run_dns_test(
r#"
CISCO_SPLIT_INC=1
CISCO_SPLIT_INC_0_ADDR=10.0.0.0
CISCO_SPLIT_INC_0_MASK=255.0.0.0
CISCO_SPLIT_INC_0_MASKLEN=8
configure_macos_dns || exit 1
grep -Fx 'd.add InterfaceName en0' "$state_dir/IPv4" || exit 2
grep -Fx 'd.add Router 192.168.1.1' "$state_dir/IPv4" || exit 3
grep -Fx 'd.add GPConnectSourceService physical-service' "$state_dir/IPv4" || exit 4
"#,
));
}
#[test] #[test]
fn replaces_dns_and_removes_stale_suffixes_and_owned_state() { fn replaces_dns_and_removes_stale_suffixes_and_owned_state() {
assert_success(run_dns_test( assert_success(run_dns_test(
@@ -170,6 +226,80 @@ remove_macos_dns
assert_eq!(errors.matches("macOS network configuration failed").count(), 2); assert_eq!(errors.matches("macOS network configuration failed").count(), 2);
} }
#[test]
fn refreshes_dns_registration_from_a_new_primary_service() {
assert_success(run_dns_test(
r#"
configure_macos_dns || exit 1
printf '%s\n' 'd.add Addresses * 192.168.2.100' 'd.add SubnetMasks * 255.255.255.0' \
'd.add InterfaceName en1' 'd.add Router 192.168.2.1' > "$state_dir/ethernet-ipv4"
printf 'ethernet-service\n' > "$state_dir/primary-service"
configure_macos_dns || exit 2
grep -Fx 'd.add InterfaceName en1' "$state_dir/IPv4" || exit 3
grep -Fx 'd.add Router 192.168.2.1' "$state_dir/IPv4" || exit 4
grep -Fx 'd.add GPConnectSourceService ethernet-service' "$state_dir/IPv4" || exit 5
if grep -Eq 'physical-service|192.168.1.1|InterfaceName en0' "$state_dir/IPv4"; then exit 6; fi
"#,
));
}
#[test]
fn missing_primary_service_or_source_dictionary_fails_configuration() {
for setup in [
"printf '' > \"$state_dir/primary-service\"",
"rm \"$state_dir/physical-ipv4\"",
] {
let output = run_dns_test(&format!("{setup}\nconfigure_macos_dns"));
assert!(!output.status.success());
let errors = String::from_utf8_lossy(&output.stderr);
assert!(errors.contains("Cannot identify") || errors.contains("macOS network configuration failed"));
}
}
#[test]
fn handles_absent_mixed_and_replaced_dns_servers() {
assert_success(run_dns_test(
r#"
INTERNAL_IP4_DNS=''
configure_macos_dns || exit 1
[ ! -f "$state_dir/DNS" ] && [ ! -f "$state_dir/IPv4" ] || exit 2
# The native producer exports both address families through INTERNAL_IP4_DNS.
INTERNAL_IP4_DNS='10.0.0.53 2001:db8::53'
configure_macos_dns || exit 3
grep -Fx 'd.add ServerAddresses * 10.0.0.53 2001:db8::53' "$state_dir/DNS" || exit 4
INTERNAL_IP4_DNS=2001:db8::54
configure_macos_dns || exit 5
grep -Fx 'd.add ServerAddresses * 2001:db8::54' "$state_dir/DNS" || exit 6
grep -Eq '10.0.0.53|2001:db8::53' "$state_dir/DNS" && exit 7
cmp "$state_dir/physical-dns" "$state_dir/original-physical-dns" || exit 8
"#,
));
}
#[test]
fn cleanup_attempts_the_other_key_when_one_removal_fails() {
for failed_key in ["DNS", "IPv4"] {
let removed_key = if failed_key == "DNS" { "IPv4" } else { "DNS" };
let output = run_dns_test(&format!(
r#"
configure_macos_dns || exit 1
scutil_failure=remove-{failed_key}
remove_macos_dns
[ "$?" -ne 0 ] || exit 2
[ -f "$state_dir/{failed_key}" ] || exit 3
[ ! -f "$state_dir/{removed_key}" ] || exit 4
"#
));
assert!(output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stderr)
.matches("macOS network configuration failed")
.count(),
1
);
}
}
// Stub route/interface operations and use production lifecycle functions to // Stub route/interface operations and use production lifecycle functions to
// check DNS dispatch, failure propagation, and interface cleanup after failure. // check DNS dispatch, failure propagation, and interface cleanup after failure.
fn lifecycle_script() -> String { fn lifecycle_script() -> String {
@@ -180,7 +310,14 @@ fn lifecycle_script() -> String {
do_ifconfig() {{ :; }} do_ifconfig() {{ :; }}
set_vpngateway_route() {{ :; }} set_vpngateway_route() {{ :; }}
del_vpngateway_route() {{ :; }} del_vpngateway_route() {{ :; }}
reset_default_route() {{ :; }} set_default_route() {{ echo add-default4 >> "$state_dir/routes"; }}
set_network_route() {{ echo "add4 $1/$3" >> "$state_dir/routes"; }}
del_network_route() {{ echo "remove4 $1/$3" >> "$state_dir/routes"; }}
reset_default_route() {{ echo remove-default4 >> "$state_dir/routes"; }}
set_ipv6_default_route() {{ echo add-default6 >> "$state_dir/routes"; }}
reset_ipv6_default_route() {{ echo remove-default6 >> "$state_dir/routes"; }}
set_ipv6_network_route() {{ echo "add6 $1/$2" >> "$state_dir/routes"; }}
del_ipv6_network_route() {{ echo "remove6 $1/$2" >> "$state_dir/routes"; }}
ifconfig() {{ printf '%s\n' "$*" >> "$state_dir/interface-operations"; }} ifconfig() {{ printf '%s\n' "$*" >> "$state_dir/interface-operations"; }}
ifconfig_syntax_del() {{ printf '%s\n' "$1"; }} ifconfig_syntax_del() {{ printf '%s\n' "$1"; }}
do_connect() {{{lifecycle} do_connect() {{{lifecycle}
@@ -189,6 +326,71 @@ do_connect() {{{lifecycle}
) )
} }
#[test]
fn ipv4_prefix_controls_setup_cleanup_and_dns_registration() {
let script = lifecycle_script();
let (definitions, _) = script.split_once("#### Main").unwrap();
for (prefix, mask) in [
(0, "0.0.0.0"),
(1, "128.0.0.0"),
(8, "255.0.0.0"),
(32, "255.255.255.255"),
] {
let expected_routes = if prefix == 0 {
"add-default4\nremove-default4"
} else {
&format!("add4 0.0.0.0/{prefix}\nremove4 0.0.0.0/{prefix}")
};
let interface = if prefix == 0 { "utun42" } else { "en0" };
let script = format!(
r#"
{definitions}
CISCO_SPLIT_INC=1
CISCO_SPLIT_INC_0_ADDR=0.0.0.0
CISCO_SPLIT_INC_0_MASK={mask}
CISCO_SPLIT_INC_0_MASKLEN={prefix}
do_connect || exit 1
grep -Fx 'd.add InterfaceName {interface}' "$state_dir/IPv4" || exit 2
do_disconnect || exit 3
printf '%s\n' '{expected_routes}' > "$state_dir/expected-routes"
diff -u "$state_dir/expected-routes" "$state_dir/routes" || exit 4
[ ! -f "$state_dir/DNS" ] && [ ! -f "$state_dir/IPv4" ] || exit 5
"#
);
assert_success(Command::new("/bin/sh").args(["-c", &script]).output().unwrap());
}
}
#[test]
fn ipv6_prefix_controls_setup_and_cleanup_independently_of_ipv4_dns() {
let script = lifecycle_script();
let (definitions, _) = script.split_once("#### Main").unwrap();
for address in ["::", "0:0:0:0:0:0:0:0"] {
for prefix in [0, 1, 64, 128] {
let expected_routes = if prefix == 0 {
"add4 10.0.0.0/8\nadd-default6\nremove4 10.0.0.0/8\nremove-default6"
} else {
&format!("add4 10.0.0.0/8\nadd6 {address}/{prefix}\nremove4 10.0.0.0/8\nremove6 {address}/{prefix}")
};
let script = format!(
r#"
{definitions}
CISCO_IPV6_SPLIT_INC=1
CISCO_IPV6_SPLIT_INC_0_ADDR={address}
CISCO_IPV6_SPLIT_INC_0_MASKLEN={prefix}
INTERNAL_IP6_ADDRESS=2001:db8::42
do_connect || exit 1
grep -Fx 'd.add InterfaceName en0' "$state_dir/IPv4" || exit 2
do_disconnect || exit 3
printf '%s\n' '{expected_routes}' > "$state_dir/expected-routes"
diff -u "$state_dir/expected-routes" "$state_dir/routes" || exit 4
"#
);
assert_success(Command::new("/bin/sh").args(["-c", &script]).output().unwrap());
}
}
}
#[test] #[test]
fn lifecycle_reapplies_dns_on_reconnect_and_propagates_failures() { fn lifecycle_reapplies_dns_on_reconnect_and_propagates_failures() {
// Source just the production lifecycle definitions, then run its dispatch. // Source just the production lifecycle definitions, then run its dispatch.