diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index 8d0131a78..ca0762668 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -2942,6 +2942,8 @@ nm_utils_parse_debug_string (const char *string, return result; } +/*****************************************************************************/ + void nm_utils_ifname_cpy (char *dst, const char *name) { @@ -2954,3 +2956,100 @@ nm_utils_ifname_cpy (char *dst, const char *name) g_return_if_reached (); } +/*****************************************************************************/ + +#define IPV4LL_NETWORK (htonl (0xA9FE0000L)) +#define IPV4LL_NETMASK (htonl (0xFFFF0000L)) + +gboolean +nm_utils_ip4_address_is_link_local (in_addr_t addr) +{ + return (addr & IPV4LL_NETMASK) == IPV4LL_NETWORK; +} + +/*****************************************************************************/ + +/** + * Takes a pair @timestamp and @duration, and returns the remaining duration based + * on the new timestamp @now. + */ +guint32 +nm_utils_lifetime_rebase_relative_time_on_now (guint32 timestamp, + guint32 duration, + guint32 now, + guint32 padding) +{ + gint64 t; + + if (duration == NM_PLATFORM_LIFETIME_PERMANENT) + return NM_PLATFORM_LIFETIME_PERMANENT; + + if (timestamp == 0) { + /* if the @timestamp is zero, assume it was just left unset and that the relative + * @duration starts counting from @now. This is convenient to construct an address + * and print it in nm_platform_ip4_address_to_string(). + * + * In general it does not make sense to set the @duration without anchoring at + * @timestamp because you don't know the absolute expiration time when looking + * at the address at a later moment. */ + timestamp = now; + } + + /* For timestamp > now, just accept it and calculate the expected(?) result. */ + t = (gint64) timestamp + (gint64) duration - (gint64) now; + + /* Optional padding to avoid potential races. */ + t += (gint64) padding; + + if (t <= 0) + return 0; + if (t >= NM_PLATFORM_LIFETIME_PERMANENT) + return NM_PLATFORM_LIFETIME_PERMANENT - 1; + return t; +} + +gboolean +nm_utils_lifetime_get (guint32 timestamp, + guint32 lifetime, + guint32 preferred, + guint32 now, + guint32 padding, + guint32 *out_lifetime, + guint32 *out_preferred) +{ + guint32 t_lifetime, t_preferred; + + if (lifetime == 0) { + *out_lifetime = NM_PLATFORM_LIFETIME_PERMANENT; + *out_preferred = NM_PLATFORM_LIFETIME_PERMANENT; + + /* We treat lifetime==0 as permanent addresses to allow easy creation of such addresses + * (without requiring to set the lifetime fields to NM_PLATFORM_LIFETIME_PERMANENT). + * In that case we also expect that the other fields (timestamp and preferred) are left unset. */ + g_return_val_if_fail (timestamp == 0 && preferred == 0, TRUE); + } else { + if (!now) + now = nm_utils_get_monotonic_timestamp_s (); + t_lifetime = nm_utils_lifetime_rebase_relative_time_on_now (timestamp, lifetime, now, padding); + if (!t_lifetime) { + *out_lifetime = 0; + *out_preferred = 0; + return FALSE; + } + t_preferred = nm_utils_lifetime_rebase_relative_time_on_now (timestamp, preferred, now, padding); + + *out_lifetime = t_lifetime; + *out_preferred = MIN (t_preferred, t_lifetime); + + /* Assert that non-permanent addresses have a (positive) @timestamp. nm_utils_lifetime_rebase_relative_time_on_now() + * treats addresses with timestamp 0 as *now*. Addresses passed to _address_get_lifetime() always + * should have a valid @timestamp, otherwise on every re-sync, their lifetime will be extended anew. + */ + g_return_val_if_fail ( timestamp != 0 + || ( lifetime == NM_PLATFORM_LIFETIME_PERMANENT + && preferred == NM_PLATFORM_LIFETIME_PERMANENT), TRUE); + g_return_val_if_fail (t_preferred <= t_lifetime, TRUE); + } + return TRUE; +} + diff --git a/src/nm-core-utils.h b/src/nm-core-utils.h index 91a089969..81cc39d58 100644 --- a/src/nm-core-utils.h +++ b/src/nm-core-utils.h @@ -30,6 +30,8 @@ /*****************************************************************************/ +#define NM_PLATFORM_LIFETIME_PERMANENT G_MAXUINT32 + #define NM_DEFINE_SINGLETON_INSTANCE(TYPE) \ static TYPE *singleton_instance @@ -414,4 +416,19 @@ guint nm_utils_parse_debug_string (const char *string, void nm_utils_ifname_cpy (char *dst, const char *name); +guint32 nm_utils_lifetime_rebase_relative_time_on_now (guint32 timestamp, + guint32 duration, + guint32 now, + guint32 padding); + +gboolean nm_utils_lifetime_get (guint32 timestamp, + guint32 lifetime, + guint32 preferred, + guint32 now, + guint32 padding, + guint32 *out_lifetime, + guint32 *out_preferred); + +gboolean nm_utils_ip4_address_is_link_local (in_addr_t addr); + #endif /* __NM_CORE_UTILS_H__ */ diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index 9c2557f93..f556bd283 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -188,7 +188,7 @@ routes_are_duplicate (const NMPlatformIP4Route *a, const NMPlatformIP4Route *b, static gint _addresses_sort_cmp_get_prio (in_addr_t addr) { - if (nmp_utils_ip4_address_is_link_local (addr)) + if (nm_utils_ip4_address_is_link_local (addr)) return 0; return 1; } diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index eb90211fe..33e3ddf8e 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -5196,7 +5196,7 @@ ip4_address_add (NMPlatform *platform, plen, &peer_addr, flags, - nmp_utils_ip4_address_is_link_local (addr) ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE, + nm_utils_ip4_address_is_link_local (addr) ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE, lifetime, preferred, label); diff --git a/src/platform/nm-platform-utils.c b/src/platform/nm-platform-utils.c index 6020a8f3b..d0c92a787 100644 --- a/src/platform/nm-platform-utils.c +++ b/src/platform/nm-platform-utils.c @@ -412,103 +412,6 @@ out: return g_intern_string (driver); } -/****************************************************************** - * utils - ******************************************************************/ - -#define IPV4LL_NETWORK (htonl (0xA9FE0000L)) -#define IPV4LL_NETMASK (htonl (0xFFFF0000L)) - -gboolean -nmp_utils_ip4_address_is_link_local (in_addr_t addr) -{ - return (addr & IPV4LL_NETMASK) == IPV4LL_NETWORK; -} - -/** - * Takes a pair @timestamp and @duration, and returns the remaining duration based - * on the new timestamp @now. - */ -guint32 -nmp_utils_lifetime_rebase_relative_time_on_now (guint32 timestamp, - guint32 duration, - guint32 now, - guint32 padding) -{ - gint64 t; - - if (duration == NM_PLATFORM_LIFETIME_PERMANENT) - return NM_PLATFORM_LIFETIME_PERMANENT; - - if (timestamp == 0) { - /* if the @timestamp is zero, assume it was just left unset and that the relative - * @duration starts counting from @now. This is convenient to construct an address - * and print it in nm_platform_ip4_address_to_string(). - * - * In general it does not make sense to set the @duration without anchoring at - * @timestamp because you don't know the absolute expiration time when looking - * at the address at a later moment. */ - timestamp = now; - } - - /* For timestamp > now, just accept it and calculate the expected(?) result. */ - t = (gint64) timestamp + (gint64) duration - (gint64) now; - - /* Optional padding to avoid potential races. */ - t += (gint64) padding; - - if (t <= 0) - return 0; - if (t >= NM_PLATFORM_LIFETIME_PERMANENT) - return NM_PLATFORM_LIFETIME_PERMANENT - 1; - return t; -} - -gboolean -nmp_utils_lifetime_get (guint32 timestamp, - guint32 lifetime, - guint32 preferred, - guint32 now, - guint32 padding, - guint32 *out_lifetime, - guint32 *out_preferred) -{ - guint32 t_lifetime, t_preferred; - - if (lifetime == 0) { - *out_lifetime = NM_PLATFORM_LIFETIME_PERMANENT; - *out_preferred = NM_PLATFORM_LIFETIME_PERMANENT; - - /* We treat lifetime==0 as permanent addresses to allow easy creation of such addresses - * (without requiring to set the lifetime fields to NM_PLATFORM_LIFETIME_PERMANENT). - * In that case we also expect that the other fields (timestamp and preferred) are left unset. */ - g_return_val_if_fail (timestamp == 0 && preferred == 0, TRUE); - } else { - if (!now) - now = nm_utils_get_monotonic_timestamp_s (); - t_lifetime = nmp_utils_lifetime_rebase_relative_time_on_now (timestamp, lifetime, now, padding); - if (!t_lifetime) { - *out_lifetime = 0; - *out_preferred = 0; - return FALSE; - } - t_preferred = nmp_utils_lifetime_rebase_relative_time_on_now (timestamp, preferred, now, padding); - - *out_lifetime = t_lifetime; - *out_preferred = MIN (t_preferred, t_lifetime); - - /* Assert that non-permanent addresses have a (positive) @timestamp. nmp_utils_lifetime_rebase_relative_time_on_now() - * treats addresses with timestamp 0 as *now*. Addresses passed to _address_get_lifetime() always - * should have a valid @timestamp, otherwise on every re-sync, their lifetime will be extended anew. - */ - g_return_val_if_fail ( timestamp != 0 - || ( lifetime == NM_PLATFORM_LIFETIME_PERMANENT - && preferred == NM_PLATFORM_LIFETIME_PERMANENT), TRUE); - g_return_val_if_fail (t_preferred <= t_lifetime, TRUE); - } - return TRUE; -} - gboolean nmp_utils_device_exists (const char *name) { diff --git a/src/platform/nm-platform-utils.h b/src/platform/nm-platform-utils.h index a9d29c047..976bd8db4 100644 --- a/src/platform/nm-platform-utils.h +++ b/src/platform/nm-platform-utils.h @@ -52,21 +52,6 @@ gboolean nmp_utils_mii_supports_carrier_detect (const char *ifname); const char *nmp_utils_udev_get_driver (GUdevDevice *device); -guint32 nmp_utils_lifetime_rebase_relative_time_on_now (guint32 timestamp, - guint32 duration, - guint32 now, - guint32 padding); - -gboolean nmp_utils_lifetime_get (guint32 timestamp, - guint32 lifetime, - guint32 preferred, - guint32 now, - guint32 padding, - guint32 *out_lifetime, - guint32 *out_preferred); - gboolean nmp_utils_device_exists (const char *name); -gboolean nmp_utils_ip4_address_is_link_local (in_addr_t addr); - #endif /* __NM_PLATFORM_UTILS_H__ */ diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index e31196b5e..f67bc3980 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -2588,8 +2588,8 @@ array_contains_ip4_address (const GArray *addresses, const NMPlatformIP4Address && ((candidate->peer_address ^ address->peer_address) & nm_utils_ip4_prefix_to_netmask (address->plen)) == 0) { guint32 lifetime, preferred; - if (nmp_utils_lifetime_get (candidate->timestamp, candidate->lifetime, candidate->preferred, - now, padding, &lifetime, &preferred)) + if (nm_utils_lifetime_get (candidate->timestamp, candidate->lifetime, candidate->preferred, + now, padding, &lifetime, &preferred)) return TRUE; } } @@ -2609,8 +2609,8 @@ array_contains_ip6_address (const GArray *addresses, const NMPlatformIP6Address if (IN6_ARE_ADDR_EQUAL (&candidate->address, &address->address) && candidate->plen == address->plen) { guint32 lifetime, preferred; - if (nmp_utils_lifetime_get (candidate->timestamp, candidate->lifetime, candidate->preferred, - now, padding, &lifetime, &preferred)) + if (nm_utils_lifetime_get (candidate->timestamp, candidate->lifetime, candidate->preferred, + now, padding, &lifetime, &preferred)) return TRUE; } } @@ -2665,8 +2665,8 @@ nm_platform_ip4_address_sync (NMPlatform *self, int ifindex, const GArray *known const NMPlatformIP4Address *known_address = &g_array_index (known_addresses, NMPlatformIP4Address, i); guint32 lifetime, preferred; - if (!nmp_utils_lifetime_get (known_address->timestamp, known_address->lifetime, known_address->preferred, - now, ADDRESS_LIFETIME_PADDING, &lifetime, &preferred)) + if (!nm_utils_lifetime_get (known_address->timestamp, known_address->lifetime, known_address->preferred, + now, ADDRESS_LIFETIME_PADDING, &lifetime, &preferred)) continue; if (!nm_platform_ip4_address_add (self, ifindex, known_address->address, known_address->plen, @@ -2727,8 +2727,8 @@ nm_platform_ip6_address_sync (NMPlatform *self, int ifindex, const GArray *known const NMPlatformIP6Address *known_address = &g_array_index (known_addresses, NMPlatformIP6Address, i); guint32 lifetime, preferred; - if (!nmp_utils_lifetime_get (known_address->timestamp, known_address->lifetime, known_address->preferred, - now, ADDRESS_LIFETIME_PADDING, &lifetime, &preferred)) + if (!nm_utils_lifetime_get (known_address->timestamp, known_address->lifetime, known_address->preferred, + now, ADDRESS_LIFETIME_PADDING, &lifetime, &preferred)) continue; if (!nm_platform_ip6_address_add (self, ifindex, known_address->address, @@ -2972,7 +2972,7 @@ _lifetime_to_string (guint32 timestamp, guint32 lifetime, gint32 now, char *buf, return "forever"; g_snprintf (buf, buf_size, "%usec", - nmp_utils_lifetime_rebase_relative_time_on_now (timestamp, lifetime, now, 0)); + nm_utils_lifetime_rebase_relative_time_on_now (timestamp, lifetime, now, 0)); return buf; } diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index eadc37865..cbc13e27f 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -178,8 +178,6 @@ typedef enum { NM_PLATFORM_SIGNAL_REMOVED, } NMPlatformSignalChangeType; -#define NM_PLATFORM_LIFETIME_PERMANENT G_MAXUINT32 - typedef enum { /*< skip >*/ NM_PLATFORM_GET_ROUTE_FLAGS_NONE = 0,