From 27a14a7d116cfd23b215a9b0bf236d44f29aea56 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Thu, 11 Oct 2012 17:07:21 -0500 Subject: [PATCH] core: fix routing if gateway not given in the first address The gateway doesn't have to be there, but can be associated with any address. NM should look through all addresses and find the first usable gateway. Previously it was just using the first address' gateway even if it was 0. Broken by 2384dea3 (policy: split routing and DNS updates) --- src/nm-policy.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/nm-policy.c b/src/nm-policy.c index 6722fb6e72..cd65c436d3 100644 --- a/src/nm-policy.c +++ b/src/nm-policy.c @@ -569,10 +569,11 @@ update_ip4_routing (NMPolicy *policy, gboolean force_update) NMVPNConnection *vpn = NULL; NMActiveConnection *best_ac = NULL; NMIP4Config *ip4_config = NULL, *parent_ip4; - NMIP4Address *addr; + NMIP4Address *addr = NULL, *tmp_addr; const char *ip_iface = NULL; int ip_ifindex = -1; guint32 parent_mss; + guint32 i; /* Note that we might have an IPv4 VPN tunneled over an IPv6-only device, * so we can get (vpn != NULL && best == NULL). @@ -587,8 +588,14 @@ update_ip4_routing (NMPolicy *policy, gboolean force_update) if (!force_update && best && (best == policy->default_device4)) return; - /* We set the default route to the gateway of the first IP address of the config */ - addr = nm_ip4_config_get_address (ip4_config, 0); + /* We set the default route to the first gateway we find */ + for (i = 0; i < nm_ip4_config_get_num_addresses (ip4_config); i++) { + tmp_addr = nm_ip4_config_get_address (ip4_config, i); + if (nm_ip4_address_get_gateway (tmp_addr)) { + addr = tmp_addr; + break; + } + } g_assert (addr); if (vpn) { @@ -725,10 +732,11 @@ update_ip6_routing (NMPolicy *policy, gboolean force_update) NMVPNConnection *vpn = NULL; NMActiveConnection *best_ac = NULL; NMIP6Config *ip6_config = NULL, *parent_ip6; - NMIP6Address *addr; + NMIP6Address *addr = NULL, *tmp_addr; const char *ip_iface = NULL; int ip_ifindex = -1; guint32 parent_mss; + guint32 i; /* Note that we might have an IPv6 VPN tunneled over an IPv4-only device, * so we can get (vpn != NULL && best == NULL). @@ -743,8 +751,14 @@ update_ip6_routing (NMPolicy *policy, gboolean force_update) if (!force_update && best && (best == policy->default_device6)) return; - /* We set the default route to the gateway of the first IP address of the config */ - addr = nm_ip6_config_get_address (ip6_config, 0); + /* We set the default route to the first gateway we find */ + for (i = 0; i < nm_ip6_config_get_num_addresses (ip6_config); i++) { + tmp_addr = nm_ip6_config_get_address (ip6_config, i); + if (nm_ip6_address_get_gateway (tmp_addr)) { + addr = tmp_addr; + break; + } + } g_assert (addr); if (vpn) {