diff options
Diffstat (limited to 'src/dhcp/nm-dhcp-systemd.c')
-rw-r--r-- | src/dhcp/nm-dhcp-systemd.c | 152 |
1 files changed, 80 insertions, 72 deletions
diff --git a/src/dhcp/nm-dhcp-systemd.c b/src/dhcp/nm-dhcp-systemd.c index a30a5e5883..ee403b3600 100644 --- a/src/dhcp/nm-dhcp-systemd.c +++ b/src/dhcp/nm-dhcp-systemd.c @@ -17,6 +17,7 @@ #include "nm-std-aux/unaligned.h" #include "nm-utils.h" +#include "nm-l3-config-data.h" #include "nm-dhcp-utils.h" #include "nm-dhcp-options.h" #include "nm-core-utils.h" @@ -67,7 +68,7 @@ G_DEFINE_TYPE (NMDhcpSystemd, nm_dhcp_systemd, NM_TYPE_DHCP_CLIENT) /*****************************************************************************/ -static NMIP4Config * +static NML3ConfigData * lease_to_ip4_config (NMDedupMultiIndex *multi_idx, const char *iface, int ifindex, @@ -77,7 +78,7 @@ lease_to_ip4_config (NMDedupMultiIndex *multi_idx, GHashTable **out_options, GError **error) { - gs_unref_object NMIP4Config *ip4_config = NULL; + nm_auto_unref_l3cd_init NML3ConfigData *l3cd = NULL; gs_unref_hashtable GHashTable *options = NULL; const struct in_addr *addr_list; char addr_str[NM_UTILS_INET_ADDRSTRLEN]; @@ -89,7 +90,6 @@ lease_to_ip4_config (NMDedupMultiIndex *multi_idx, int i, num; const void *data; gsize data_len; - gboolean metered = FALSE; gboolean has_router_from_classless = FALSE; gboolean has_classless_route = FALSE; gboolean has_static_route = FALSE; @@ -124,7 +124,9 @@ lease_to_ip4_config (NMDedupMultiIndex *multi_idx, return NULL; } - ip4_config = nm_ip4_config_new (multi_idx, ifindex); + l3cd = nm_l3_config_data_new (multi_idx, ifindex); + + nm_l3_config_data_set_source (l3cd, NM_IP_CONFIG_SOURCE_DHCP); options = out_options ? nm_dhcp_option_create_options_dict () : NULL; @@ -157,16 +159,16 @@ lease_to_ip4_config (NMDedupMultiIndex *multi_idx, addr_str); } - nm_ip4_config_add_address (ip4_config, - &((const NMPlatformIP4Address) { - .address = a_address.s_addr, - .peer_address = a_address.s_addr, - .plen = a_plen, - .addr_source = NM_IP_CONFIG_SOURCE_DHCP, - .timestamp = ts, - .lifetime = a_lifetime, - .preferred = a_lifetime, - })); + nm_l3_config_data_add_address_4 (l3cd, + &((const NMPlatformIP4Address) { + .address = a_address.s_addr, + .peer_address = a_address.s_addr, + .plen = a_plen, + .addr_source = NM_IP_CONFIG_SOURCE_DHCP, + .timestamp = ts, + .lifetime = a_lifetime, + .preferred = a_lifetime, + })); if (sd_dhcp_lease_get_server_identifier (lease, &server_id) >= 0) { _nm_utils_inet4_ntop (server_id.s_addr, addr_str); @@ -197,7 +199,7 @@ lease_to_ip4_config (NMDedupMultiIndex *multi_idx, * See https://github.com/systemd/systemd/issues/4524. */ continue; } - nm_ip4_config_add_nameserver (ip4_config, addr_list[i].s_addr); + nm_l3_config_data_add_nameserver (l3cd, AF_INET, &addr_list[i].s_addr); } nm_dhcp_option_add_option (options, _nm_dhcp_option_dhcp4_options, @@ -210,7 +212,7 @@ lease_to_ip4_config (NMDedupMultiIndex *multi_idx, nm_gstring_prepare (&str); for (i = 0; i < num; i++) { g_string_append (nm_gstring_add_space_delimiter (str), search_domains[i]); - nm_ip4_config_add_search (ip4_config, search_domains[i]); + nm_l3_config_data_add_search (l3cd, AF_INET, search_domains[i]); } nm_dhcp_option_add_option (options, _nm_dhcp_option_dhcp4_options, @@ -231,7 +233,7 @@ lease_to_ip4_config (NMDedupMultiIndex *multi_idx, * As systemd escapes such characters, split them at \\032. */ domains = g_strsplit (s, "\\032", 0); for (d = domains; *d; d++) - nm_ip4_config_add_domain (ip4_config, *d); + nm_l3_config_data_add_domain (l3cd, AF_INET, *d); } if (sd_dhcp_lease_get_hostname (lease, &s) >= 0) { @@ -327,16 +329,15 @@ lease_to_ip4_config (NMDedupMultiIndex *multi_idx, } else m = route_metric; - nm_ip4_config_add_route (ip4_config, - &((const NMPlatformIP4Route) { - .network = network_net, - .plen = r_plen, - .gateway = r_gateway.s_addr, - .rt_source = NM_IP_CONFIG_SOURCE_DHCP, - .metric = m, - .table_coerced = nm_platform_route_table_coerce (route_table), - }), - NULL); + nm_l3_config_data_add_route_4 (l3cd, + &((const NMPlatformIP4Route) { + .network = network_net, + .plen = r_plen, + .gateway = r_gateway.s_addr, + .rt_source = NM_IP_CONFIG_SOURCE_DHCP, + .metric = m, + .table_coerced = nm_platform_route_table_coerce (route_table), + })); } if (str_classless && str_classless->len > 0) @@ -383,14 +384,13 @@ lease_to_ip4_config (NMDedupMultiIndex *multi_idx, if (default_route_metric < G_MAXUINT32) default_route_metric++; - nm_ip4_config_add_route (ip4_config, - &((const NMPlatformIP4Route) { - .rt_source = NM_IP_CONFIG_SOURCE_DHCP, - .gateway = a_router[i].s_addr, - .table_coerced = nm_platform_route_table_coerce (route_table), - .metric = m, - }), - NULL); + nm_l3_config_data_add_route_4 (l3cd, + &((const NMPlatformIP4Route) { + .rt_source = NM_IP_CONFIG_SOURCE_DHCP, + .gateway = a_router[i].s_addr, + .table_coerced = nm_platform_route_table_coerce (route_table), + .metric = m, + })); } nm_dhcp_option_add_option (options, _nm_dhcp_option_dhcp4_options, @@ -404,7 +404,7 @@ lease_to_ip4_config (NMDedupMultiIndex *multi_idx, _nm_dhcp_option_dhcp4_options, NM_DHCP_OPTION_DHCP4_INTERFACE_MTU, mtu); - nm_ip4_config_set_mtu (ip4_config, mtu, NM_IP_CONFIG_SOURCE_DHCP); + nm_l3_config_data_set_mtu (l3cd, mtu); } num = sd_dhcp_lease_get_ntp (lease, &addr_list); @@ -448,9 +448,10 @@ lease_to_ip4_config (NMDedupMultiIndex *multi_idx, s); } - if (sd_dhcp_lease_get_vendor_specific (lease, &data, &data_len) >= 0) - metered = !!memmem (data, data_len, "ANDROID_METERED", NM_STRLEN ("ANDROID_METERED")); - nm_ip4_config_set_metered (ip4_config, metered); + if (sd_dhcp_lease_get_vendor_specific (lease, &data, &data_len) >= 0) { + if (!!memmem (data, data_len, "ANDROID_METERED", NM_STRLEN ("ANDROID_METERED"))) + nm_l3_config_data_set_metered (l3cd, TRUE); + } num = nm_sd_dhcp_lease_get_private_options (lease, &private_options); if (num > 0) { @@ -471,7 +472,7 @@ lease_to_ip4_config (NMDedupMultiIndex *multi_idx, } } NM_SET_OUT (out_options, g_steal_pointer (&options)); - return g_steal_pointer (&ip4_config); + return g_steal_pointer (&l3cd); } /*****************************************************************************/ @@ -482,7 +483,7 @@ bound4_handle (NMDhcpSystemd *self, gboolean extended) NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (self); const char *iface = nm_dhcp_client_get_iface (NM_DHCP_CLIENT (self)); sd_dhcp_lease *lease; - gs_unref_object NMIP4Config *ip4_config = NULL; + nm_auto_unref_l3cd_init NML3ConfigData *l3cd = NULL; gs_unref_hashtable GHashTable *options = NULL; GError *error = NULL; @@ -495,15 +496,15 @@ bound4_handle (NMDhcpSystemd *self, gboolean extended) _LOGD ("lease available"); - ip4_config = lease_to_ip4_config (nm_dhcp_client_get_multi_idx (NM_DHCP_CLIENT (self)), - iface, - nm_dhcp_client_get_ifindex (NM_DHCP_CLIENT (self)), - lease, - nm_dhcp_client_get_route_table (NM_DHCP_CLIENT (self)), - nm_dhcp_client_get_route_metric (NM_DHCP_CLIENT (self)), - &options, - &error); - if (!ip4_config) { + l3cd = lease_to_ip4_config (nm_dhcp_client_get_multi_idx (NM_DHCP_CLIENT (self)), + iface, + nm_dhcp_client_get_ifindex (NM_DHCP_CLIENT (self)), + lease, + nm_dhcp_client_get_route_table (NM_DHCP_CLIENT (self)), + nm_dhcp_client_get_route_metric (NM_DHCP_CLIENT (self)), + &options, + &error); + if (!l3cd) { _LOGW ("%s", error->message); g_clear_error (&error); nm_dhcp_client_set_state (NM_DHCP_CLIENT (self), NM_DHCP_STATE_FAIL, NULL, NULL); @@ -515,7 +516,7 @@ bound4_handle (NMDhcpSystemd *self, gboolean extended) nm_dhcp_client_set_state (NM_DHCP_CLIENT (self), extended ? NM_DHCP_STATE_EXTENDED : NM_DHCP_STATE_BOUND, - NM_IP_CONFIG_CAST (ip4_config), + l3cd, options); } @@ -723,7 +724,7 @@ ip4_start (NMDhcpClient *client, return TRUE; } -static NMIP6Config * +static NML3ConfigData * lease_to_ip6_config (NMDedupMultiIndex *multi_idx, const char *iface, int ifindex, @@ -733,7 +734,7 @@ lease_to_ip6_config (NMDedupMultiIndex *multi_idx, gint32 ts, GError **error) { - gs_unref_object NMIP6Config *ip6_config = NULL; + nm_auto_unref_l3cd_init NML3ConfigData *l3cd = NULL; gs_unref_hashtable GHashTable *options = NULL; struct in6_addr tmp_addr; const struct in6_addr *dns; @@ -741,11 +742,14 @@ lease_to_ip6_config (NMDedupMultiIndex *multi_idx, char addr_str[NM_UTILS_INET_ADDRSTRLEN]; char **domains; nm_auto_free_gstring GString *str = NULL; + gboolean has_any_addresses = FALSE; int num, i; g_return_val_if_fail (lease, NULL); - ip6_config = nm_ip6_config_new (multi_idx, ifindex); + l3cd = nm_l3_config_data_new (multi_idx, ifindex); + + nm_l3_config_data_set_source (l3cd, NM_IP_CONFIG_SOURCE_DHCP); options = out_options ? nm_dhcp_option_create_options_dict () : NULL; @@ -761,19 +765,23 @@ lease_to_ip6_config (NMDedupMultiIndex *multi_idx, .addr_source = NM_IP_CONFIG_SOURCE_DHCP, }; - nm_ip6_config_add_address (ip6_config, &address); + nm_l3_config_data_add_address_6 (l3cd, &address); _nm_utils_inet6_ntop (&tmp_addr, addr_str); g_string_append (nm_gstring_add_space_delimiter (str), addr_str); - }; - if (str->len) + + has_any_addresses = TRUE; + } + + if (str->len) { nm_dhcp_option_add_option (options, _nm_dhcp_option_dhcp6_options, NM_DHCP_OPTION_DHCP6_NM_IP_ADDRESS, str->str); + } if ( !info_only - && nm_ip6_config_get_num_addresses (ip6_config) == 0) { + && !has_any_addresses) { g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED, @@ -787,7 +795,7 @@ lease_to_ip6_config (NMDedupMultiIndex *multi_idx, for (i = 0; i < num; i++) { _nm_utils_inet6_ntop (&dns[i], addr_str); g_string_append (nm_gstring_add_space_delimiter (str), addr_str); - nm_ip6_config_add_nameserver (ip6_config, &dns[i]); + nm_l3_config_data_add_nameserver (l3cd, AF_INET6, &dns[i]); } nm_dhcp_option_add_option (options, _nm_dhcp_option_dhcp6_options, @@ -800,7 +808,7 @@ lease_to_ip6_config (NMDedupMultiIndex *multi_idx, nm_gstring_prepare (&str); for (i = 0; i < num; i++) { g_string_append (nm_gstring_add_space_delimiter (str), domains[i]); - nm_ip6_config_add_search (ip6_config, domains[i]); + nm_l3_config_data_add_search (l3cd, AF_INET6, domains[i]); } nm_dhcp_option_add_option (options, _nm_dhcp_option_dhcp6_options, @@ -809,7 +817,7 @@ lease_to_ip6_config (NMDedupMultiIndex *multi_idx, } NM_SET_OUT (out_options, g_steal_pointer (&options)); - return g_steal_pointer (&ip6_config); + return g_steal_pointer (&l3cd); } static void @@ -818,7 +826,7 @@ bound6_handle (NMDhcpSystemd *self) NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (self); const gint32 ts = nm_utils_get_monotonic_timestamp_sec (); const char *iface = nm_dhcp_client_get_iface (NM_DHCP_CLIENT (self)); - gs_unref_object NMIP6Config *ip6_config = NULL; + nm_auto_unref_l3cd_init NML3ConfigData *l3cd = NULL; gs_unref_hashtable GHashTable *options = NULL; gs_free_error GError *error = NULL; NMPlatformIP6Address prefix = { 0 }; @@ -833,16 +841,16 @@ bound6_handle (NMDhcpSystemd *self) _LOGD ("lease available"); - ip6_config = lease_to_ip6_config (nm_dhcp_client_get_multi_idx (NM_DHCP_CLIENT (self)), - iface, - nm_dhcp_client_get_ifindex (NM_DHCP_CLIENT (self)), - lease, - nm_dhcp_client_get_info_only (NM_DHCP_CLIENT (self)), - &options, - ts, - &error); + l3cd = lease_to_ip6_config (nm_dhcp_client_get_multi_idx (NM_DHCP_CLIENT (self)), + iface, + nm_dhcp_client_get_ifindex (NM_DHCP_CLIENT (self)), + lease, + nm_dhcp_client_get_info_only (NM_DHCP_CLIENT (self)), + &options, + ts, + &error); - if (!ip6_config) { + if (!l3cd) { _LOGW ("%s", error->message); nm_dhcp_client_set_state (NM_DHCP_CLIENT (self), NM_DHCP_STATE_FAIL, NULL, NULL); return; @@ -850,7 +858,7 @@ bound6_handle (NMDhcpSystemd *self) nm_dhcp_client_set_state (NM_DHCP_CLIENT (self), NM_DHCP_STATE_BOUND, - NM_IP_CONFIG_CAST (ip6_config), + l3cd, options); sd_dhcp6_lease_reset_pd_prefix_iter (lease); |