From b4529398b0f85e9dc2ef32b7410bbca03d72d12b Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Sun, 1 Nov 2026 11:07:36 +0800 Subject: [PATCH] fix: align macOS default route detection across network setup --- crates/gpvpnc/src/macos-vpnc-script.sh | 25 ++- crates/gpvpnc/src/macos_dns_tests.rs | 210 ++++++++++++++++++++++++- 2 files changed, 228 insertions(+), 7 deletions(-) diff --git a/crates/gpvpnc/src/macos-vpnc-script.sh b/crates/gpvpnc/src/macos-vpnc-script.sh index 29a6e35..d68bea7 100644 --- a/crates/gpvpnc/src/macos-vpnc-script.sh +++ b/crates/gpvpnc/src/macos-vpnc-script.sh @@ -259,6 +259,10 @@ do_ifconfig() { # =========== route handling ==================================== +is_ipv4_default_route() { + [ "$1" = "0.0.0.0" ] && [ "$2" = "0" ] +} + if [ -n "$IPROUTE" ]; then fix_ip_get_output () { sed -e 's/ /\n/g' | \ @@ -641,7 +645,7 @@ macos_dns_source_service() { } configure_macos_dns() { - local search_domains ipv4 + local search_domains ipv4 default_route i network prefix if [ -z "$INTERNAL_IP4_DNS" ]; then remove_macos_dns return $? @@ -650,7 +654,22 @@ configure_macos_dns() { if [ -n "$CISCO_DEF_DOMAIN" ]; then search_domains="d.add SearchDomains * $CISCO_DEF_DOMAIN" fi + # Match do_connect: an include list can itself contain the default route. + default_route=1 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 source=$(macos_dns_source_service) || return 1 # A split tunnel has no default router. Like GlobalProtect, register @@ -1004,7 +1023,7 @@ do_connect() { eval NETWORK="\${CISCO_SPLIT_INC_${i}_ADDR}" eval NETMASK="\${CISCO_SPLIT_INC_${i}_MASK}" 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" else set_default_route @@ -1043,7 +1062,7 @@ do_disconnect() { eval NETWORK="\${CISCO_SPLIT_INC_${i}_ADDR}" eval NETMASK="\${CISCO_SPLIT_INC_${i}_MASK}" 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 # routes. del_network_route "$NETWORK" "$NETMASK" "$NETMASKLEN" "$TUNDEV" diff --git a/crates/gpvpnc/src/macos_dns_tests.rs b/crates/gpvpnc/src/macos_dns_tests.rs index e23e2f5..bbee9be 100644 --- a/crates/gpvpnc/src/macos_dns_tests.rs +++ b/crates/gpvpnc/src/macos_dns_tests.rs @@ -19,6 +19,13 @@ fn dns_script() -> String { .split_once("# =========== resolv.conf handling for other OSes =========================") .unwrap() .0; + let route_detection = VPNC_SCRIPT + .split_once("# =========== route handling ====================================") + .unwrap() + .1 + .split_once("if [ -n \"$IPROUTE\" ]; then") + .unwrap() + .0; format!( r#" state_dir=$(mktemp -d) || exit 1 @@ -28,7 +35,10 @@ OS=Darwin INTERNAL_IP4_ADDRESS=10.1.2.3 INTERNAL_IP4_DNS='10.0.0.53 10.0.0.54' 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 '%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" @@ -65,10 +75,17 @@ $command" ;; sed -n 's/^d.add GPConnectSourceService / GPConnectSourceService : /p' "$state_dir/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/'*) - if [ "$scutil_failure" = remove ]; then + if [ "$scutil_failure" = remove ] || [ "$scutil_failure" = "remove-${{command##*/}}" ]; then echo ' Permission denied' else rm "$state_dir/${{command##*/}}" || return 1 @@ -80,6 +97,7 @@ $command" ;; done }} {selection} +{route_detection} {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] fn replaces_dns_and_removes_stale_suffixes_and_owned_state() { assert_success(run_dns_test( @@ -170,6 +226,80 @@ remove_macos_dns 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 // check DNS dispatch, failure propagation, and interface cleanup after failure. fn lifecycle_script() -> String { @@ -180,7 +310,14 @@ fn lifecycle_script() -> String { do_ifconfig() {{ :; }} set_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_syntax_del() {{ printf '%s\n' "$1"; }} 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] fn lifecycle_reapplies_dns_on_reconnect_and_propagates_failures() { // Source just the production lifecycle definitions, then run its dispatch.