From e74c568b07b50b97873fb4ee1d776dedefbd54d6 Mon Sep 17 00:00:00 2001 From: Michael Biebl Date: Fri, 1 Oct 2021 23:05:04 +0200 Subject: New upstream version 1.32.12 --- src/core/NetworkManagerUtils.c | 270 +++++++++++------------------------------ 1 file changed, 68 insertions(+), 202 deletions(-) (limited to 'src/core/NetworkManagerUtils.c') diff --git a/src/core/NetworkManagerUtils.c b/src/core/NetworkManagerUtils.c index 54072210..7d3e80be 100644 --- a/src/core/NetworkManagerUtils.c +++ b/src/core/NetworkManagerUtils.c @@ -12,19 +12,23 @@ #include #include -#include "nm-glib-aux/nm-c-list.h" +#include "libnm-glib-aux/nm-c-list.h" -#include "nm-libnm-core-intern/nm-common-macros.h" +#include "libnm-glib-aux/nm-uuid.h" +#include "libnm-glib-aux/nm-str-buf.h" +#include "libnm-base/nm-net-aux.h" +#include "libnm-core-aux-intern/nm-common-macros.h" #include "nm-utils.h" #include "nm-setting-connection.h" #include "nm-setting-ip4-config.h" #include "nm-setting-ip6-config.h" -#include "nm-core-internal.h" -#include "platform/nmp-object.h" +#include "libnm-core-intern/nm-core-internal.h" +#include "libnm-platform/nmp-object.h" -#include "platform/nm-platform.h" +#include "libnm-platform/nm-platform.h" +#include "libnm-platform/nm-linux-platform.h" #include "nm-auth-utils.h" -#include "systemd/nm-sd-utils-shared.h" +#include "libnm-systemd-shared/nm-sd-utils-shared.h" /*****************************************************************************/ @@ -252,7 +256,7 @@ nm_utils_complete_generic(NMPlatform * platform, g_object_set(G_OBJECT(s_con), NM_SETTING_CONNECTION_UUID, - nm_utils_uuid_generate_buf(uuid), + nm_uuid_generate_random_str_arr(uuid), NULL); } @@ -902,10 +906,16 @@ nm_match_spec_device_by_pllink(const NMPlatformLink *pllink, NMPlatformRoutingRule * nm_ip_routing_rule_to_platform(const NMIPRoutingRule *rule, NMPlatformRoutingRule *out_pl) { + gboolean uid_range_has; + guint32 uid_range_start = 0; + guint32 uid_range_end = 0; + nm_assert(rule); nm_assert(nm_ip_routing_rule_validate(rule, NULL)); nm_assert(out_pl); + uid_range_has = nm_ip_routing_rule_get_uid_range(rule, &uid_range_start, &uid_range_end); + *out_pl = (NMPlatformRoutingRule){ .addr_family = nm_ip_routing_rule_get_addr_family(rule), .flags = (nm_ip_routing_rule_get_invert(rule) ? FIB_RULE_INVERT : 0), @@ -932,6 +942,12 @@ nm_ip_routing_rule_to_platform(const NMIPRoutingRule *rule, NMPlatformRoutingRul .table = nm_ip_routing_rule_get_table(rule), .suppress_prefixlen_inverse = ~((guint32) nm_ip_routing_rule_get_suppress_prefixlength(rule)), + .uid_range_has = uid_range_has, + .uid_range = + { + .start = uid_range_start, + .end = uid_range_end, + }, }; nm_ip_routing_rule_get_xifname_bin(rule, TRUE, out_pl->iifname); @@ -1293,9 +1309,9 @@ nm_utils_ip_route_attribute_to_platform(int addr_family, if ((variant = nm_ip_route_get_attribute(s_route, NM_IP_ROUTE_ATTRIBUTE_TYPE)) && g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) { - guint8 type; + int type; - type = nm_utils_route_type_by_name(g_variant_get_string(variant, NULL)); + type = nm_net_aux_rtnl_rtntype_a2n(g_variant_get_string(variant, NULL)); nm_assert(NM_IN_SET(type, RTN_UNICAST, RTN_LOCAL)); r->type_coerced = nm_platform_route_type_coerce(type); @@ -1482,12 +1498,7 @@ nm_utils_ip_addresses_to_dbus(int addr_family, : (guint32) 0, }; - g_variant_builder_add(&builder_legacy, - "@au", - g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - dbus_addr, - 3, - sizeof(guint32))); + g_variant_builder_add(&builder_legacy, "@au", nm_g_variant_new_au(dbus_addr, 3)); } else { g_variant_builder_add( &builder_legacy, @@ -1609,12 +1620,7 @@ nm_utils_ip_routes_to_dbus(int addr_family, r->r4.metric, }; - g_variant_builder_add(&builder_legacy, - "@au", - g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - dbus_route, - 4, - sizeof(guint32))); + g_variant_builder_add(&builder_legacy, "@au", nm_g_variant_new_au(dbus_route, 4)); } else { g_variant_builder_add(&builder_legacy, "(@ayu@ayu)", @@ -1632,200 +1638,60 @@ nm_utils_ip_routes_to_dbus(int addr_family, /*****************************************************************************/ -typedef struct { - char *table; - char *rule; -} ShareRule; +/* Singleton NMPlatform subclass instance and cached class object */ +NM_DEFINE_SINGLETON_INSTANCE(NMPlatform); -struct _NMUtilsShareRules { - GArray *rules; -}; - -static void -_share_rule_clear(gpointer data) -{ - ShareRule *rule = data; - - g_free(rule->table); - g_free(rule->rule); -} - -NMUtilsShareRules * -nm_utils_share_rules_new(void) -{ - NMUtilsShareRules *self; - - self = g_slice_new(NMUtilsShareRules); - *self = (NMUtilsShareRules){ - .rules = g_array_sized_new(FALSE, FALSE, sizeof(ShareRule), 10), - }; - - g_array_set_clear_func(self->rules, _share_rule_clear); - return self; -} +NM_DEFINE_SINGLETON_REGISTER(NMPlatform); +/** + * nm_platform_setup: + * @instance: the #NMPlatform instance + * + * Failing to set up #NMPlatform singleton results in a fatal error, + * as well as trying to initialize it multiple times without freeing + * it. + * + * NetworkManager will typically use only one platform object during + * its run. Test programs might want to switch platform implementations, + * though. + */ void -nm_utils_share_rules_free(NMUtilsShareRules *self) +nm_platform_setup(NMPlatform *instance) { - if (!self) - return; + g_return_if_fail(NM_IS_PLATFORM(instance)); + g_return_if_fail(!singleton_instance); - g_array_unref(self->rules); - nm_g_slice_free(self); -} - -void -nm_utils_share_rules_add_rule_take(NMUtilsShareRules *self, const char *table, char *rule_take) -{ - ShareRule *rule; + singleton_instance = instance; - g_return_if_fail(self); - g_return_if_fail(table); - g_return_if_fail(rule_take); + nm_singleton_instance_register(); - rule = nm_g_array_append_new(self->rules, ShareRule); - *rule = (ShareRule){ - .table = g_strdup(table), - .rule = g_steal_pointer(&rule_take), - }; + nm_log_dbg(LOGD_CORE, + "setup %s singleton (" NM_HASH_OBFUSCATE_PTR_FMT ")", + "NMPlatform", + NM_HASH_OBFUSCATE_PTR(instance)); } -void -nm_utils_share_rules_apply(NMUtilsShareRules *self, gboolean shared) +/** + * nm_platform_get: + * @self: platform instance + * + * Retrieve #NMPlatform singleton. Use this whenever you want to connect to + * #NMPlatform signals. It is an error to call it before nm_platform_setup(). + * + * Returns: (transfer none): The #NMPlatform singleton reference. + */ +NMPlatform * +nm_platform_get() { - guint i; - - g_return_if_fail(self); + g_assert(singleton_instance); - if (self->rules->len == 0) - return; - - /* depending on whether we share or unshare, we add/remote the rules - * in opposite order. */ - if (shared) - i = self->rules->len - 1; - else - i = 0; - - for (;;) { - gs_free_error GError *error = NULL; - ShareRule * rule; - gs_free const char ** argv = NULL; - gs_free char * cmd = NULL; - int status; - - rule = &g_array_index(self->rules, ShareRule, i); - - cmd = g_strdup_printf("%s --table %s %s %s", - IPTABLES_PATH, - rule->table, - shared ? "--insert" : "--delete", - rule->rule); - argv = nm_utils_strsplit_set(cmd, " "); - - nm_log_info(LOGD_SHARING, "Executing: %s", cmd); - if (!g_spawn_sync("/", - (char **) argv, - (char **) NM_PTRARRAY_EMPTY(const char *), - G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL, - NULL, - NULL, - NULL, - NULL, - &status, - &error)) { - nm_log_warn(LOGD_SHARING, "Error executing command: %s", error->message); - goto next; - } - if (WEXITSTATUS(status)) { - nm_log_warn(LOGD_SHARING, "** Command returned exit status %d.", WEXITSTATUS(status)); - } - -next: - if (shared) { - if (i == 0) - break; - i--; - } else { - i++; - if (i >= self->rules->len) - break; - } - } + return singleton_instance; } +/*****************************************************************************/ + void -nm_utils_share_rules_add_all_rules(NMUtilsShareRules *self, - const char * ip_iface, - in_addr_t addr, - guint plen) +nm_linux_platform_setup(void) { - in_addr_t netmask; - in_addr_t network; - char str_mask[NM_UTILS_INET_ADDRSTRLEN]; - char str_addr[NM_UTILS_INET_ADDRSTRLEN]; - - nm_assert(self); - - netmask = _nm_utils_ip4_prefix_to_netmask(plen); - _nm_utils_inet4_ntop(netmask, str_mask); - - network = addr & netmask; - _nm_utils_inet4_ntop(network, str_addr); - - nm_utils_share_rules_add_rule_v( - self, - "nat", - "POSTROUTING --source %s/%s ! --destination %s/%s --jump MASQUERADE", - str_addr, - str_mask, - str_addr, - str_mask); - nm_utils_share_rules_add_rule_v( - self, - "filter", - "FORWARD --destination %s/%s --out-interface %s --match state --state " - "ESTABLISHED,RELATED --jump ACCEPT", - str_addr, - str_mask, - ip_iface); - nm_utils_share_rules_add_rule_v(self, - "filter", - "FORWARD --source %s/%s --in-interface %s --jump ACCEPT", - str_addr, - str_mask, - ip_iface); - nm_utils_share_rules_add_rule_v(self, - "filter", - "FORWARD --in-interface %s --out-interface %s --jump ACCEPT", - ip_iface, - ip_iface); - nm_utils_share_rules_add_rule_v(self, - "filter", - "FORWARD --out-interface %s --jump REJECT", - ip_iface); - nm_utils_share_rules_add_rule_v(self, - "filter", - "FORWARD --in-interface %s --jump REJECT", - ip_iface); - nm_utils_share_rules_add_rule_v( - self, - "filter", - "INPUT --in-interface %s --protocol udp --destination-port 67 --jump ACCEPT", - ip_iface); - nm_utils_share_rules_add_rule_v( - self, - "filter", - "INPUT --in-interface %s --protocol tcp --destination-port 67 --jump ACCEPT", - ip_iface); - nm_utils_share_rules_add_rule_v( - self, - "filter", - "INPUT --in-interface %s --protocol udp --destination-port 53 --jump ACCEPT", - ip_iface); - nm_utils_share_rules_add_rule_v( - self, - "filter", - "INPUT --in-interface %s --protocol tcp --destination-port 53 --jump ACCEPT", - ip_iface); + nm_platform_setup(nm_linux_platform_new(FALSE, FALSE)); } -- cgit 1.3.0-6-gf8a5 From 88c227d90a6b7b388c5c85d72802a0ca8f05ed5c Mon Sep 17 00:00:00 2001 From: Michael Biebl Date: Thu, 13 Jan 2022 22:30:39 +0100 Subject: New upstream version 1.34.0 --- src/core/NetworkManagerUtils.c | 72 +++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 25 deletions(-) (limited to 'src/core/NetworkManagerUtils.c') diff --git a/src/core/NetworkManagerUtils.c b/src/core/NetworkManagerUtils.c index 7d3e80be..0da8e0a9 100644 --- a/src/core/NetworkManagerUtils.c +++ b/src/core/NetworkManagerUtils.c @@ -17,6 +17,7 @@ #include "libnm-glib-aux/nm-uuid.h" #include "libnm-glib-aux/nm-str-buf.h" #include "libnm-base/nm-net-aux.h" +#include "libnm-core-aux-intern/nm-libnm-core-utils.h" #include "libnm-core-aux-intern/nm-common-macros.h" #include "nm-utils.h" #include "nm-setting-connection.h" @@ -112,7 +113,7 @@ get_new_connection_name(NMConnection *const *existing_connections, * connection id. */ temp = g_strdup_printf(C_("connection id fallback", "%s %u"), fallback_prefix, i); - if (nm_utils_strv_find_first((char **) existing_names, existing_len, temp) < 0) + if (nm_strv_find_first(existing_names, existing_len, temp) < 0) return temp; g_free(temp); @@ -227,28 +228,28 @@ out: /*****************************************************************************/ void -nm_utils_complete_generic(NMPlatform * platform, - NMConnection * connection, - const char * ctype, - NMConnection *const *existing_connections, - const char * preferred_id, - const char * fallback_id_prefix, - const char * ifname_prefix, - const char * ifname, - gboolean default_enable_ipv6) +_nm_utils_complete_generic_with_params(NMPlatform * platform, + NMConnection * connection, + const char * ctype, + NMConnection *const *existing_connections, + const char * preferred_id, + const char * fallback_id_prefix, + const char * ifname_prefix, + const char * ifname, + ...) { NMSettingConnection *s_con; - char * id, *generated_ifname; - GHashTable * parameters; + char * id; + char * generated_ifname; + gs_unref_hashtable GHashTable *parameters = NULL; + va_list ap; + const char * p_val; + const char * p_key; g_assert(fallback_id_prefix); g_return_if_fail(ifname_prefix == NULL || ifname == NULL); - s_con = nm_connection_get_setting_connection(connection); - if (!s_con) { - s_con = (NMSettingConnection *) nm_setting_connection_new(); - nm_connection_add_setting(connection, NM_SETTING(s_con)); - } + s_con = _nm_connection_ensure_setting(connection, NM_TYPE_SETTING_CONNECTION); g_object_set(G_OBJECT(s_con), NM_SETTING_CONNECTION_TYPE, ctype, NULL); if (!nm_setting_connection_get_uuid(s_con)) { @@ -268,7 +269,9 @@ nm_utils_complete_generic(NMPlatform * platform, } /* Add an interface name, if requested */ - if (ifname) { + if (nm_setting_connection_get_interface_name(s_con)) { + /* pass */ + } else if (ifname) { g_object_set(G_OBJECT(s_con), NM_SETTING_CONNECTION_INTERFACE_NAME, ifname, NULL); } else if (ifname_prefix && !nm_setting_connection_get_interface_name(s_con)) { generated_ifname = get_new_connection_ifname(platform, existing_connections, ifname_prefix); @@ -277,13 +280,20 @@ nm_utils_complete_generic(NMPlatform * platform, } /* Normalize */ - parameters = g_hash_table_new(nm_str_hash, g_str_equal); - g_hash_table_insert(parameters, - NM_CONNECTION_NORMALIZE_PARAM_IP6_CONFIG_METHOD, - default_enable_ipv6 ? NM_SETTING_IP6_CONFIG_METHOD_AUTO - : NM_SETTING_IP6_CONFIG_METHOD_IGNORE); + va_start(ap, ifname); + while ((p_key = va_arg(ap, const char *))) { + p_val = va_arg(ap, const char *); + if (!p_val) { + if (parameters) + g_hash_table_remove(parameters, p_key); + continue; + } + if (!parameters) + parameters = g_hash_table_new(nm_str_hash, g_str_equal); + g_hash_table_insert(parameters, (char *) p_key, (char *) p_val); + } + va_end(ap); nm_connection_normalize(connection, parameters, NULL, NULL); - g_hash_table_destroy(parameters); } /*****************************************************************************/ @@ -1051,6 +1061,12 @@ nm_shutdown_wait_obj_register_full(gpointer watched_obj, if (G_UNLIKELY(!_shutdown_waitobj_lst_head.next)) c_list_init(&_shutdown_waitobj_lst_head); + /* Beware: there are callers with g_main_context_get_thread_default() + * not being g_main_context_get_default(). For example _fw_nft_call(). + * + * If you schedule any sources or async operations, you probably need to + * make sure to use the default context. */ + handle = g_slice_new(NMShutdownWaitObjHandle); *handle = (NMShutdownWaitObjHandle){ /* depending on @free_msg_reason, we take ownership of @msg_reason. @@ -1693,5 +1709,11 @@ nm_platform_get() void nm_linux_platform_setup(void) { - nm_platform_setup(nm_linux_platform_new(FALSE, FALSE)); + nm_platform_setup(nm_linux_platform_new(FALSE, FALSE, FALSE)); +} + +void +nm_linux_platform_setup_with_tc_cache(void) +{ + nm_platform_setup(nm_linux_platform_new(FALSE, FALSE, TRUE)); } -- cgit 1.3.0-6-gf8a5