Keep link-local-only services from displacing the default
cover Keep link-local-only services from displacing the default
When DHCP gets no lease, connman falls back to IPv4 link-local addressing and the service becomes "ready" with a "default dev <interface> scope link" route and no gateway. Ethernet cabled directly to a phone or laptop, or to a switch with nothing else on it, ends up this way. It reaches only its own link and never passes an online check.
With PreferredTechnologies listing ethernet first, such a service outranks an "online" Wi-Fi or cellular service: 'service_compare' and 'service_update_preferred_order' consult PreferredTechnologies before online-over-ready. Ethernet takes the default route, Wi-Fi is demoted to "ready", and only Ethernet is online checked. The system is offline until Ethernet meets the online check failures threshold, every time such a cable is plugged in, and at every start with one plugged in.
This series ranks a service connected only by link-local addressing below any connected service that can reach beyond its link, whatever its technology, in both places. For IPv4 that means connected with the "auto" (IPv4 link-local) method; for IPv6 it means not connected, since connman drops IPv6 link-local addresses as they arrive and becomes "ready" for IPv6 only with some other address. It also re-sorts when a connected service gains or loses that reach with no change in its state, as when a DHCP server later appears on the link of a link-local-only Ethernet service. A link-local-only service alone is still the default, as before.
The IPv4 test keys on the method, not on the gateway address, because point-to-point links such as many cellular bearers have no gateway either but reach beyond their link.
Note: This adds a new log entry: "Interface <name> [ <type> ] is now link-local only" and "... is no longer link-local only" once per change.
Grant Erickson (4):
service: Rank link-local-only services below others in
'service_compare'
service: Rank link-local-only services below others when reordering
service: Re-sort when a connected service's link-local-only state
changes
doc: Describe link-local-only services under PreferredTechnologies
doc/connman.conf.5.in | 7 +- src/service.c | 276 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 276 insertions(+), 7 deletions(-)
Signature · 1 line
2.52.0
PATCH 1/4 service: Rank link-local-only services below others in 'service_compare'
When DHCP gets no lease, connman falls back to IPv4 link-local addressing (RFC 3927) and the service still becomes "ready", with a "default dev <interface> scope link" route and no gateway. Such a service, for example Ethernet cabled directly to a phone or laptop, or to a switch with nothing else on it, reaches only the hosts on its own link and can never pass an online check.
However, when both services are connected, 'service_compare' ranks by service order and the PreferredTechnologies list before preferring "online" over "ready". With the common PreferredTechnologies of ethernet before wifi and cellular, a "ready" link-local Ethernet service therefore sorts before an "online" Wi-Fi or cellular service, takes the default route, and is the only service online checked. The system is offline until that service meets the online check failures threshold, and back offline each time the cable is plugged in.
Add 'is_link_local_only', true for a connected service with neither an IPv4 nor an IPv6 configuration other than link-local addressing, built on one test per family:
- 'has_non_link_local_ipv4': connected for IPv4 with a method other than "auto". connman assigns an IPv4 link-local address itself, with the "auto" method, and indicates IPv4 "ready" with it, so the method is what tells it apart.
- 'has_non_link_local_ipv6': connected for IPv6. 'process_newaddr' drops IPv6 link-local (fe80::/10) addresses, which every IPv6 interface always has, so they never reach the IP configuration; IPv6 becomes "ready" only with some other address.
Add 'service_compare_link_local_only', which ranks a service that can reach beyond its own link before one that cannot, and apply it in 'service_compare' for two connected services after the online check failures threshold, VPN and order comparisons, immediately ahead of the PreferredTechnologies comparison it corrects. The VPN comparison recurses into 'service_compare' for the VPN's transport, so the rule applies to it there too.
The IPv4 test keys on the configuration method rather than on the gateway address: a point-to-point link, such as many cellular bearers, has no gateway either, but it reaches beyond its link. A service connected only by link-local addressing is still ranked first, and so is the default, when no other service is connected.
--- src/service.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+)
src/service.c +137 -0
@@ -1143,6 +1143,92 @@ static bool is_online(enum connman_service_state state)return state ==CONNMAN_SERVICE_STATE_ONLINE ; } +/** +* @brief +* Determine whether a network service has an IPv4 configuration +* other than IPv4 link-local addressing. +* +* This determines whether the specified network service is connected +* for IPv4 with a configuration method other than "auto". For IPv4, +* "auto" is IPv4 link-local (RFC 3927) addressing, to which DHCP +* falls back when no lease is available. connman assigns that +* address itself and indicates the IPv4 configuration "ready" with +* it, so the method is what tells it apart. +* +* @param[in] service A pointer to the immutable service to check. +* +* @returns +* True if @a service is connected for IPv4 with a configuration +* method other than "auto"; otherwise, false. +* +*/ +static bool has_non_link_local_ipv4 (const struct connman_service *service) +{ +return is_connected (service->state_ipv4) && +__connman_ipconfig_get_method (service->ipconfig_ipv4) != +CONNMAN_IPCONFIG_METHOD_AUTO ; +} + +/** +* @brief +* Determine whether a network service has an IPv6 configuration +* other than IPv6 link-local addressing. +* +* This determines whether the specified network service is connected +* for IPv6. Every IPv6 interface always has an IPv6 link-local +* (fe80::/10) address, but connman never counts one: the routing +* netlink new address handler drops IPv6 link-local addresses, so +* they never reach the IP configuration, and the IPv6 configuration +* becomes "ready" only with some other address (from router +* advertisement, DHCPv6 or manual configuration). Being connected +* for IPv6 therefore already implies a non-link-local address. +* +* @param[in] service A pointer to the immutable service to check. +* +* @returns +* True if @a service is connected for IPv6; otherwise, false. +* +*/ +static bool has_non_link_local_ipv6 (const struct connman_service *service) +{ +return is_connected (service->state_ipv6); +} + +/** +* @brief +* Determine whether a network service is connected only by +* link-local addressing. +* +* This determines whether the specified network service is connected, +* yet has neither an IPv4 nor an IPv6 configuration other than +* link-local addressing. +* +* Such a service, for example Ethernet cabled directly to a phone or +* laptop, or to a switch with no DHCP server or IPv6 router behind +* it, reaches only the hosts on its own link. Its default route, +* "default dev <interface> scope link", leads to no gateway. +* +* @param[in] service A pointer to the immutable service to check. +* +* @returns +* True if @a service is connected for IPv4 or IPv6 but has no +* configuration of either other than link-local addressing; +* otherwise, false. +* +* @sa has_non_link_local_ipv4 +* @sa has_non_link_local_ipv6 +* +*/ +static bool is_link_local_only (const struct connman_service *service) +{ +if (!is_connected (service->state_ipv4) && + !is_connected (service->state_ipv6)) +return false; + +return !has_non_link_local_ipv4 (service) && + !has_non_link_local_ipv6 (service); +} +static bool is_idle (enum connman_service_state state) {switch (state) {
@@ -8283,6 +8369,45 @@ static gint service_compare_vpn(const struct connman_service *a,return service_compare (transport, service); } +/** +* @brief +* Compare two network services by whether each is connected only +* by link-local addressing. +* +* This compares the two specified network services by whether each +* is connected only by link-local addressing, giving priority to the +* service that can reach beyond its own link. +* +* @param[in] service_a A pointer to the first immutable service +* to compare. +* @param[in] service_b A pointer to the second immutable service +* to compare. +* +* @retval 0 If both or neither of the services are connected only +* by link-local addressing. +* @retval -1 If only @a service_b is connected only by link-local +* addressing and @a service_a should sort @b before +* @a service_b. +* @retval 1 If only @a service_a is connected only by link-local +* addressing and @a service_b should sort @b before +* @a service_a. +* +* @sa is_link_local_only +* +*/ +static gint service_compare_link_local_only ( +const struct connman_service *service_a, +const struct connman_service *service_b) +{ +const bool a_link_local_only =is_link_local_only (service_a); +const bool b_link_local_only =is_link_local_only (service_b); + +if (a_link_local_only == b_link_local_only) +return 0 ; + +return a_link_local_only ?1 :-1 ; +} +/** * @brief * Compare two network services against the @a
@@ -8352,6 +8477,7 @@ static gint service_compare_preferred(const struct connman_service *service_a,* @retval < 0 If service @a a should sort @b before service @a b. * @retval > 0 If service @a b should sort @b before service @a a. * +* @sa service_compare_link_local_only * @sa service_compare_preferred * @sa __connman_service_compare *
@@ -8409,6 +8535,17 @@ static gint service_compare(gconstpointer a, gconstpointer b)if (service_a->order < service_b->order)return 1 ; +/* +* If at this point the services are still comparing as +* equivalent, then give priority to the service that can +* reach beyond its own link over one connected only by +* link-local addressing, ahead of the PreferredTechnologies +* priority list. +*/ + rval =service_compare_link_local_only (service_a, service_b); +if (rval) +return rval; + rval =service_compare_preferred (service_a, service_b);if (rval)return rval;
Signature · 1 line
2.52.0
PATCH 2/4 service: Rank link-local-only services below others when reordering
'service_update_preferred_order' runs when a service becomes "ready" or "online". It switches the service ahead of the default service, downgrading an "online" default to "ready", whenever the PreferredTechnologies list prefers the new service's technology.
So when a link-local-only Ethernet service becomes "ready" next to an "online" Wi-Fi default, with ethernet listed first, Wi-Fi is demoted to "ready" at once and Ethernet becomes the default, whatever 'service_compare' later says. In the other direction, when Wi-Fi becomes "ready" while a link-local-only Ethernet service is the default, the list prefers Ethernet and Wi-Fi is left behind it.
Decide by 'service_compare_link_local_only' first and fall back to the PreferredTechnologies list only when it ties. A link-local-only service then never displaces a default that can reach beyond its link, and is always displaced by one, consistent with 'service_compare'.
--- src/service.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
src/service.c +16 -3
@@ -9286,8 +9286,9 @@ static void downgrade_connected_services(void)* This attempts to switch the order of the specified services in the * network service list if and only if a) the services are non-null, * b) do not have pointer equivalence, and c) if @a new_service -* should sort before @a default_service according to the @a -* PreferredTechnologies list. +* should sort before @a default_service, first according to whether +* either is connected only by link-local addressing and then +* according to the @a PreferredTechnologies list. * * @param[in,out] default_service A pointer to the mutable, default * network service to potentially
@@ -9312,6 +9313,8 @@ static int service_update_preferred_order(struct connman_service *default_servicstruct connman_service *new_service, enum connman_service_state new_state) { + gint rval; + DBG("default_service %p (%s) new_service %p (%s) new_state %d (%s)", default_service, connman_service_get_identifier(default_service), new_service, connman_service_get_identifier(new_service),
@@ -9320,7 +9323,17 @@ static int service_update_preferred_order(struct connman_service *default_servicif (!default_service || default_service == new_service)return 0 ; -if (service_compare_preferred (default_service, new_service) >0 ) { +/* +* A service connected only by link-local addressing never +* displaces a default service that can reach beyond its own +* link, and is always displaced by one, whatever the +* PreferredTechnologies list says. +*/ + rval =service_compare_link_local_only (default_service, new_service); +if (!rval) + rval =service_compare_preferred (default_service, new_service); + +if (rval >0 ) {switch_service_order (default_service, new_service);__connman_gateway_update ();
Signature · 1 line
2.52.0
PATCH 3/4 service: Re-sort when a connected service's link-local-only state changes
A connected service can become, or stop being, connected only by IPv4 link-local addressing without a change in its combined state, so without passing through 'service_indicate_state' and the reordering it does on becoming "ready":
- An Ethernet service that fell back to IPv4 link-local addressing stays "ready" when a DHCP server later appears on its link (an unmanaged switch that gets a router plugged in, for example). 'lease_available_cb' moves the IPv4 method back to "dhcp" and the new gateway is added, but '__connman_service_ipconfig_indicate_state' sees "ready" again and returns -EALREADY.
- A service whose lease is lost falls back to IPv4 link-local addressing while staying "ready".
- IPv6 can become connected next to IPv4 link-local addressing, leaving the combined state "ready".
Now that 'service_compare' ranks link-local-only services below the others, each of these leaves the service list and the default service stale. In the first, the most likely, a link-local-only Ethernet service is never the default and so is never online checked; once its lease arrives, nothing re-ranks it, and it stays behind Wi-Fi or cellular despite PreferredTechnologies.
Track whether each service is connected only by link-local addressing, setting it whenever the combined state changes and logging each change while connected:
Interface eth0 [ ethernet ] is now link-local only Interface eth0 [ ethernet ] is no longer link-local only
When an IP configuration state indication for a connected service leaves the combined state unchanged but the tracked state changed, re-sort the service list, update the gateways, and signal the default service, as becoming "ready" does. The service ranked first then gets the high-priority default route, is signaled as the default, and is online checked.
--- src/service.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 117 insertions(+), 3 deletions(-)
src/service.c +117 -3
@@ -219,6 +219,13 @@ struct connman_service {* "continuous" online check mode. */ bool online_check_failures_met_threshold; + +/** +* Tracks whether the service, while connected, is connected only +* by link-local addressing. This is false while the service is +* not connected. +*/ +bool link_local_only;bool do_split_routing;bool new_service;bool hidden_service;
@@ -9381,6 +9388,93 @@ static const char *get_dbus_sender(struct connman_service *service)return dbus_message_get_sender (service->pending); } +/** +* @brief +* Update whether a network service is connected only by +* link-local addressing. +* +* This updates the tracked state of whether the specified network +* service is connected only by link-local addressing and, if the +* service is connected and that state changed, logs it. +* +* @param[in,out] service A pointer to the mutable service for +* which to update the tracked state. +* +* @returns +* True if the tracked state changed; otherwise, false. +* +* @sa is_link_local_only +* +*/ +static bool service_update_link_local_only (struct connman_service *service) +{ + g_autofree char *interface = NULL; +bool link_local_only; + + link_local_only =is_connected (service->state) && +is_link_local_only (service); + +if (link_local_only == service->link_local_only) +return false; + + service->link_local_only = link_local_only; + +if (is_connected (service->state)) { + interface =connman_service_get_interface (service); + +connman_info ("Interface %s [ %s ] is %s link-local only" , + interface, +__connman_service_type2string (service->type), + link_local_only ?"now" :"no longer" ); + } + +return true; +} + +/** +* @brief +* Re-sort the network services if a connected network service +* became, or stopped being, connected only by link-local +* addressing. +* +* A connected service can become, or stop being, connected only by +* link-local addressing with no change in its combined state. For +* example, an Ethernet service that fell back to IPv4 link-local +* addressing stays "ready" when a DHCP lease later arrives or when +* IPv6 becomes connected, and a service that loses its lease falls +* back to IPv4 link-local addressing while staying "ready". +* +* Because #service_compare ranks such a service below the others, +* the network service list and the default service may then be +* stale. If so, this re-sorts the network service list and updates +* the gateways, so that the service now first gets the high-priority +* default route, is signaled as the default service, and is online +* checked. +* +* @param[in,out] service A pointer to the mutable service that +* may have become, or stopped being, +* connected only by link-local +* addressing. +* +* @sa service_update_link_local_only +* +*/ +static void service_resort_if_link_local_only_changed ( +struct connman_service *service) +{ +if (!service_update_link_local_only (service)) +return ; + +if (!is_connected (service->state)) +return ; + +SERVICE_LIST_SORT (); + +__connman_gateway_update (); + +DEFAULT_CHANGED (); +} +static int service_indicate_state (struct connman_service *service) {enum connman_service_state old_state, new_state;
@@ -9423,6 +9517,8 @@ static int service_indicate_state(struct connman_service *service) service->state = new_state;state_changed (service); +service_update_link_local_only (service); +if (!is_connected (old_state) &&is_connected (new_state))searchdomain_add_all (service);
@@ -9742,6 +9838,7 @@ int __connman_service_ipconfig_indicate_state(struct connman_service *service,struct connman_ipconfig *ipconfig =NULL ;enum connman_service_state old_state;enum connman_ipconfig_method method; +int err;if (!service)return -EINVAL ;
@@ -9787,9 +9884,16 @@ int __connman_service_ipconfig_indicate_state(struct connman_service *service, } -/* Any change? */ -if (old_state == new_state) +/* +* Any change? If not, the IP configuration may still have +* changed, for example from IPv4 link-local addressing to a DHCP +* lease, so re-sort if that changed the service's reach. +*/ +if (old_state == new_state) { +service_resort_if_link_local_only_changed (service); +return -EALREADY ; + } DBG("service %p (%s) type %d (%s) old state %d (%s) new state %d (%s)", service, connman_service_get_identifier(service),
@@ -9860,7 +9964,17 @@ int __connman_service_ipconfig_indicate_state(struct connman_service *service, CONNMAN_TIMESERVER_SYNC_REASON_STATE_UPDATE); } -return service_indicate_state (service); +/* +* If the combined service state is unchanged, a change in this +* IP configuration type's state may still have changed the +* service's reach, for example IPv6 becoming connected next to +* IPv4 link-local addressing, so re-sort if so. +*/ + err =service_indicate_state (service); +if (err == -EALREADY ) +service_resort_if_link_local_only_changed (service); + +return err; }static bool prepare_network (struct connman_service *service)
Signature · 1 line
2.52.0
PATCH 4/4 doc: Describe link-local-only services under PreferredTechnologies
Note in connman.conf(5) that a service connected only by link-local addressing is not preferred over a connected service that can reach beyond its link, whatever the PreferredTechnologies list says, and gets the default route only when no such service is connected.
--- doc/connman.conf.5.in | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
doc/connman.conf.5.in +6 -1
@@ -105,7 +105,12 @@ compared to another preferred type further down the list with state 'ready' or with a non-preferred type; a service of a preferred technology type in state 'online' will get the default route when compared to either a non-preferred -type or a preferred type further down in the list. +type or a preferred type further down in the list. A +service connected only by link-local addressing (for +example, Ethernet with no DHCP server or IPv6 router on +its link) is not preferred over a connected service that +can reach beyond its link, whatever its type; it gets the +default route only when no such service is connected. .TP .BI NetworkInterfaceBlacklist= interface\fR[,...] List of blacklisted network interfaces separated by ",".
Signature · 1 line
2.52.0
Quoted 42 lines · Grant Erickson
When DHCP gets no lease, connman falls back to IPv4 link-local addressing and the service becomes "ready" with a "default dev <interface> scope link" route and no gateway. Ethernet cabled directly to a phone or laptop, or to a switch with nothing else on it, ends up this way. It reaches only its own link and never passes an online check.
With PreferredTechnologies listing ethernet first, such a service outranks an "online" Wi-Fi or cellular service: '
service_compare' and 'service_update_preferred_order' consult PreferredTechnologies before online-over-ready. Ethernet takes the default route, Wi-Fi is demoted to "ready", and only Ethernet is online checked. The system is offline until Ethernet meets the online check failures threshold, every time such a cable is plugged in, and at every start with one plugged in.This series ranks a service connected only by link-local addressing below any connected service that can reach beyond its link, whatever its technology, in both places. For IPv4 that means connected with the "auto" (IPv4 link-local) method; for IPv6 it means not connected, since connman drops IPv6 link-local addresses as they arrive and becomes "ready" for IPv6 only with some other address. It also re-sorts when a connected service gains or loses that reach with no change in its state, as when a DHCP server later appears on the link of a link-local-only Ethernet service. A link-local-only service alone is still the default, as before.
The IPv4 test keys on the method, not on the gateway address, because point-to-point links such as many cellular bearers have no gateway either but reach beyond their link.
Note: This adds a new log entry: "Interface <name> [ <type> ] is now link-local only" and "... is no longer link-local only" once per change.
Grant Erickson (4):
service: Rank link-local-only services below others in
'service_compare'
service: Rank link-local-only services below others when reordering
service: Re-sort when a connected service's link-local-only state
changes
doc: Describe link-local-only services under PreferredTechnologies
Quoted 3 lines
doc/connman.conf.5.in | 7 +-
src/service.c| 276 +++++++++++++++++++++++++++++++++++++++++-2 files changed, 276 insertions(+), 7 deletions(-)
Johannes:
After working on and submitting this, I was reviewing patchwork and saw you had a submission, https://patchwork.kernel.org/series/1081540/, that might be very-closely related. I’ll take a look at your patch. To the extent we have arrived at functionally-equivalent solutions, would you be up for collaborating on a merged/aligned approach?
Best,
Grant
Signature · 5 lines
Principal Nuovations [email protected] https://www.nuovations.com/
Johannes Emerich
Tested-byYes, thank you, very closely related and I support your submission.
Looking through your series, it seems like a more general treatment that mostly subsumes my patch. We have been running my patch for close to a year now, and a previously cumbersome problem in some institutiognal networks has gone away. So I would be very happy to see it in upstream ConnMan, and happy to collaborate on it.
What your series adds:
- IPv6 support (and more robust v4/v6 dual stack handling)
- hooking into more paths and invoking DEFAULT_CHANGED()
- better observability
The main functional difference with respect to my patch is that yours applies the link-local criterion earlier in service_compare(), so that a link-local service no longer wins out just because it uses a preferred technology. I actually prefer this and as implied in my submission was simply being conservative when I applied the link-local criterion later/with less weight.
Our VM-based test for a dual-Ethernet delayed-DHCP scenario passes with your patch applied to ConnMan 2.0.
Let me know if there's particular angles you'd like more input on.
Quoted 62 lines · Grant Erickson
Quoted 42 lines · Grant Erickson
When DHCP gets no lease, connman falls back to IPv4 link-local addressing and the service becomes "ready" with a "default dev <interface> scope link" route and no gateway. Ethernet cabled directly to a phone or laptop, or to a switch with nothing else on it, ends up this way. It reaches only its own link and never passes an online check.
With PreferredTechnologies listing ethernet first, such a service outranks an "online" Wi-Fi or cellular service: '
service_compare' and 'service_update_preferred_order' consult PreferredTechnologies before online-over-ready. Ethernet takes the default route, Wi-Fi is demoted to "ready", and only Ethernet is online checked. The system is offline until Ethernet meets the online check failures threshold, every time such a cable is plugged in, and at every start with one plugged in.This series ranks a service connected only by link-local addressing below any connected service that can reach beyond its link, whatever its technology, in both places. For IPv4 that means connected with the "auto" (IPv4 link-local) method; for IPv6 it means not connected, since connman drops IPv6 link-local addresses as they arrive and becomes "ready" for IPv6 only with some other address. It also re-sorts when a connected service gains or loses that reach with no change in its state, as when a DHCP server later appears on the link of a link-local-only Ethernet service. A link-local-only service alone is still the default, as before.
The IPv4 test keys on the method, not on the gateway address, because point-to-point links such as many cellular bearers have no gateway either but reach beyond their link.
Note: This adds a new log entry: "Interface <name> [ <type> ] is now link-local only" and "... is no longer link-local only" once per change.
Grant Erickson (4):
service: Rank link-local-only services below others in
'service_compare'
service: Rank link-local-only services below others when reordering
service: Re-sort when a connected service's link-local-only state
changes
doc: Describe link-local-only services under PreferredTechnologiesQuoted 3 lines
doc/connman.conf.5.in | 7 +-
src/service.c| 276 +++++++++++++++++++++++++++++++++++++++++-2 files changed, 276 insertions(+), 7 deletions(-)Johannes:
After working on and submitting this, I was reviewing patchwork and saw you had a submission, https://patchwork.kernel.org/series/1081540/, that might be very-closely related. I’ll take a look at your patch. To the extent we have arrived at functionally-equivalent solutions, would you be up for collaborating on a merged/aligned approach?
Best,
Grant
--
Principal
Nuovations