| 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | /* |
| 3 | * Copyright (C) 2005 - 2017 Red Hat, Inc. |
| 4 | */ |
| 5 | |
| 6 | #ifndef __NM_UTILS_H__ |
| 7 | #define __NM_UTILS_H__ |
| 8 | |
| 9 | #if !defined(__NETWORKMANAGER_H_INSIDE__) && !defined(NETWORKMANAGER_COMPILATION) |
| 10 | #error "Only <NetworkManager.h> can be included directly." |
| 11 | #endif |
| 12 | |
| 13 | #include <glib.h> |
| 14 | |
| 15 | #include "nm-core-enum-types.h" |
| 16 | #include "nm-setting-sriov.h" |
| 17 | #include "nm-setting-tc-config.h" |
| 18 | #include "nm-setting-wireless-security.h" |
| 19 | |
| 20 | G_BEGIN_DECLS |
| 21 | |
| 22 | /*****************************************************************************/ |
| 23 | |
| 24 | typedef struct _NMVariantAttributeSpec NMVariantAttributeSpec; |
| 25 | |
| 26 | /* SSID helpers */ |
| 27 | gboolean nm_utils_is_empty_ssid(const guint8 *ssid, gsize len); |
| 28 | |
| 29 | NM_DEPRECATED_IN_1_46 |
| 30 | const char *nm_utils_escape_ssid(const guint8 *ssid, gsize len); |
| 31 | |
| 32 | gboolean nm_utils_same_ssid(const guint8 *ssid1, |
| 33 | gsize len1, |
| 34 | const guint8 *ssid2, |
| 35 | gsize len2, |
| 36 | gboolean ignore_trailing_null); |
| 37 | char *nm_utils_ssid_to_utf8(const guint8 *ssid, gsize len); |
| 38 | |
| 39 | /** |
| 40 | * NMUtilsSecurityType: |
| 41 | * @NMU_SEC_INVALID: unknown or invalid security, placeholder and not used |
| 42 | * @NMU_SEC_NONE: unencrypted and open |
| 43 | * @NMU_SEC_STATIC_WEP: static WEP keys are used for encryption |
| 44 | * @NMU_SEC_LEAP: Cisco LEAP is used for authentication and for generating the |
| 45 | * dynamic WEP keys automatically |
| 46 | * @NMU_SEC_DYNAMIC_WEP: standard 802.1x is used for authentication and |
| 47 | * generating the dynamic WEP keys automatically |
| 48 | * @NMU_SEC_WPA_PSK: WPA1 is used with Pre-Shared Keys (PSK) |
| 49 | * @NMU_SEC_WPA_ENTERPRISE: WPA1 is used with 802.1x authentication |
| 50 | * @NMU_SEC_WPA2_PSK: WPA2/RSN is used with Pre-Shared Keys (PSK) |
| 51 | * @NMU_SEC_WPA2_ENTERPRISE: WPA2 is used with 802.1x authentication |
| 52 | * @NMU_SEC_SAE: is used with WPA3 Enterprise |
| 53 | * @NMU_SEC_OWE: is used with Enhanced Open |
| 54 | * @NMU_SEC_WPA3_SUITE_B_192: is used with WPA3 Enterprise Suite-B 192 bit mode. Since: 1.30. |
| 55 | * |
| 56 | * Describes generic security mechanisms that 802.11 access points may offer. |
| 57 | * Used with nm_utils_security_valid() for checking whether a given access |
| 58 | * point is compatible with a network device. |
| 59 | **/ |
| 60 | typedef enum { |
| 61 | NMU_SEC_INVALID = 0, |
| 62 | NMU_SEC_NONE, |
| 63 | NMU_SEC_STATIC_WEP, |
| 64 | NMU_SEC_LEAP, |
| 65 | NMU_SEC_DYNAMIC_WEP, |
| 66 | NMU_SEC_WPA_PSK, |
| 67 | NMU_SEC_WPA_ENTERPRISE, |
| 68 | NMU_SEC_WPA2_PSK, |
| 69 | NMU_SEC_WPA2_ENTERPRISE, |
| 70 | NMU_SEC_SAE, |
| 71 | NMU_SEC_OWE, |
| 72 | NMU_SEC_WPA3_SUITE_B_192, |
| 73 | } NMUtilsSecurityType; |
| 74 | |
| 75 | gboolean nm_utils_security_valid(NMUtilsSecurityType type, |
| 76 | NMDeviceWifiCapabilities wifi_caps, |
| 77 | gboolean have_ap, |
| 78 | gboolean adhoc, |
| 79 | NM80211ApFlags ap_flags, |
| 80 | NM80211ApSecurityFlags ap_wpa, |
| 81 | NM80211ApSecurityFlags ap_rsn); |
| 82 | |
| 83 | gboolean nm_utils_ap_mode_security_valid(NMUtilsSecurityType type, |
| 84 | NMDeviceWifiCapabilities wifi_caps); |
| 85 | |
| 86 | gboolean nm_utils_wep_key_valid(const char *key, NMWepKeyType wep_type); |
| 87 | gboolean nm_utils_wpa_psk_valid(const char *psk); |
| 88 | |
| 89 | NM_AVAILABLE_IN_1_6 |
| 90 | gboolean nm_utils_is_json_object(const char *str, GError **error); |
| 91 | |
| 92 | GVariant *nm_utils_ip4_dns_to_variant(char **dns); |
| 93 | char **nm_utils_ip4_dns_from_variant(GVariant *value); |
| 94 | GVariant *nm_utils_ip4_addresses_to_variant(GPtrArray *addresses, const char *gateway); |
| 95 | GPtrArray *nm_utils_ip4_addresses_from_variant(GVariant *value, char **out_gateway); |
| 96 | GVariant *nm_utils_ip4_routes_to_variant(GPtrArray *routes); |
| 97 | GPtrArray *nm_utils_ip4_routes_from_variant(GVariant *value); |
| 98 | |
| 99 | guint32 nm_utils_ip4_netmask_to_prefix(guint32 netmask); |
| 100 | guint32 nm_utils_ip4_prefix_to_netmask(guint32 prefix); |
| 101 | guint32 nm_utils_ip4_get_default_prefix(guint32 ip); |
| 102 | |
| 103 | GVariant *nm_utils_ip6_dns_to_variant(char **dns); |
| 104 | char **nm_utils_ip6_dns_from_variant(GVariant *value); |
| 105 | GVariant *nm_utils_ip6_addresses_to_variant(GPtrArray *addresses, const char *gateway); |
| 106 | GPtrArray *nm_utils_ip6_addresses_from_variant(GVariant *value, char **out_gateway); |
| 107 | GVariant *nm_utils_ip6_routes_to_variant(GPtrArray *routes); |
| 108 | GPtrArray *nm_utils_ip6_routes_from_variant(GVariant *value); |
| 109 | |
| 110 | NM_AVAILABLE_IN_1_42 |
| 111 | GVariant *nm_utils_ip_addresses_to_variant(GPtrArray *addresses); |
| 112 | NM_AVAILABLE_IN_1_42 |
| 113 | GPtrArray *nm_utils_ip_addresses_from_variant(GVariant *value, int family); |
| 114 | NM_AVAILABLE_IN_1_42 |
| 115 | GVariant *nm_utils_ip_routes_to_variant(GPtrArray *routes); |
| 116 | NM_AVAILABLE_IN_1_42 |
| 117 | GPtrArray *nm_utils_ip_routes_from_variant(GVariant *value, int family); |
| 118 | |
| 119 | char *nm_utils_uuid_generate(void); |
| 120 | |
| 121 | typedef gboolean (*NMUtilsFileSearchInPathsPredicate)(const char *filename, gpointer user_data); |
| 122 | |
| 123 | struct stat; |
| 124 | |
| 125 | typedef gboolean (*NMUtilsCheckFilePredicate)(const char *filename, |
| 126 | const struct stat *stat, |
| 127 | gpointer user_data, |
| 128 | GError **error); |
| 129 | |
| 130 | const char *nm_utils_file_search_in_paths(const char *progname, |
| 131 | const char *try_first, |
| 132 | const char *const *paths, |
| 133 | GFileTest file_test_flags, |
| 134 | NMUtilsFileSearchInPathsPredicate predicate, |
| 135 | gpointer user_data, |
| 136 | GError **error); |
| 137 | |
| 138 | guint32 nm_utils_wifi_freq_to_channel(guint32 freq); |
| 139 | guint32 nm_utils_wifi_channel_to_freq(guint32 channel, const char *band); |
| 140 | guint32 nm_utils_wifi_find_next_channel(guint32 channel, int direction, char *band); |
| 141 | gboolean nm_utils_wifi_is_channel_valid(guint32 channel, const char *band); |
| 142 | NM_AVAILABLE_IN_1_2 |
| 143 | const guint *nm_utils_wifi_2ghz_freqs(void); |
| 144 | NM_AVAILABLE_IN_1_2 |
| 145 | const guint *nm_utils_wifi_5ghz_freqs(void); |
| 146 | |
| 147 | const char *nm_utils_wifi_strength_bars(guint8 strength); |
| 148 | |
| 149 | /** |
| 150 | * NM_UTILS_HWADDR_LEN_MAX: |
| 151 | * |
| 152 | * The maximum length of hardware addresses handled by NetworkManager itself, |
| 153 | * nm_utils_hwaddr_len(), and nm_utils_hwaddr_aton(). |
| 154 | */ |
| 155 | #define NM_UTILS_HWADDR_LEN_MAX 20 /* INFINIBAND_ALEN */ |
| 156 | |
| 157 | gsize nm_utils_hwaddr_len(int type) G_GNUC_PURE; |
| 158 | |
| 159 | char *nm_utils_hwaddr_ntoa(gconstpointer addr, gsize length); |
| 160 | GByteArray *nm_utils_hwaddr_atoba(const char *asc, gsize length); |
| 161 | guint8 *nm_utils_hwaddr_aton(const char *asc, gpointer buffer, gsize length); |
| 162 | |
| 163 | gboolean nm_utils_hwaddr_valid(const char *asc, gssize length); |
| 164 | char *nm_utils_hwaddr_canonical(const char *asc, gssize length); |
| 165 | gboolean nm_utils_hwaddr_matches(gconstpointer hwaddr1, |
| 166 | gssize hwaddr1_len, |
| 167 | gconstpointer hwaddr2, |
| 168 | gssize hwaddr2_len); |
| 169 | |
| 170 | char *nm_utils_bin2hexstr(gconstpointer src, gsize len, int final_len); |
| 171 | GBytes *nm_utils_hexstr2bin(const char *hex); |
| 172 | |
| 173 | NM_DEPRECATED_IN_1_6_FOR(nm_utils_is_valid_iface_name) |
| 174 | gboolean nm_utils_iface_valid_name(const char *name); |
| 175 | NM_AVAILABLE_IN_1_6 |
| 176 | gboolean nm_utils_is_valid_iface_name(const char *name, GError **error); |
| 177 | |
| 178 | NM_DEPRECATED_IN_1_32 |
| 179 | gboolean nm_utils_is_uuid(const char *str); |
| 180 | |
| 181 | /** |
| 182 | * NM_UTILS_INET_ADDRSTRLEN: |
| 183 | * |
| 184 | * Defines the minimal length for a char buffer that is suitable as @dst argument |
| 185 | * for both nm_utils_inet4_ntop() and nm_utils_inet6_ntop(). |
| 186 | **/ |
| 187 | #define NM_UTILS_INET_ADDRSTRLEN INET6_ADDRSTRLEN |
| 188 | |
| 189 | const char *nm_utils_inet4_ntop(guint32 inaddr, char *dst); |
| 190 | |
| 191 | struct in6_addr; |
| 192 | const char *nm_utils_inet6_ntop(const struct in6_addr *in6addr, char *dst); |
| 193 | |
| 194 | gboolean nm_utils_ipaddr_valid(int family, const char *ip); |
| 195 | |
| 196 | gboolean nm_utils_check_virtual_device_compatibility(GType virtual_type, GType other_type); |
| 197 | |
| 198 | NM_AVAILABLE_IN_1_2 |
| 199 | int nm_utils_bond_mode_string_to_int(const char *mode); |
| 200 | NM_AVAILABLE_IN_1_2 |
| 201 | const char *nm_utils_bond_mode_int_to_string(int mode); |
| 202 | |
| 203 | NM_AVAILABLE_IN_1_2 |
| 204 | char *nm_utils_enum_to_str(GType type, int value); |
| 205 | |
| 206 | NM_AVAILABLE_IN_1_2 |
| 207 | gboolean nm_utils_enum_from_str(GType type, const char *str, int *out_value, char **err_token); |
| 208 | |
| 209 | NM_AVAILABLE_IN_1_2 |
| 210 | const char **nm_utils_enum_get_values(GType type, int from, int to); |
| 211 | |
| 212 | NM_AVAILABLE_IN_1_6 |
| 213 | guint nm_utils_version(void); |
| 214 | |
| 215 | NM_AVAILABLE_IN_1_8 |
| 216 | GHashTable *nm_utils_parse_variant_attributes(const char *string, |
| 217 | char attr_separator, |
| 218 | char key_value_separator, |
| 219 | gboolean ignore_unknown, |
| 220 | const NMVariantAttributeSpec *const *spec, |
| 221 | GError **error); |
| 222 | |
| 223 | NM_AVAILABLE_IN_1_8 |
| 224 | char *nm_utils_format_variant_attributes(GHashTable *attributes, |
| 225 | char attr_separator, |
| 226 | char key_value_separator); |
| 227 | |
| 228 | /*****************************************************************************/ |
| 229 | |
| 230 | NM_AVAILABLE_IN_1_12 |
| 231 | NMTCQdisc *nm_utils_tc_qdisc_from_str(const char *str, GError **error); |
| 232 | NM_AVAILABLE_IN_1_12 |
| 233 | char *nm_utils_tc_qdisc_to_str(NMTCQdisc *qdisc, GError **error); |
| 234 | |
| 235 | NM_AVAILABLE_IN_1_12 |
| 236 | NMTCAction *nm_utils_tc_action_from_str(const char *str, GError **error); |
| 237 | NM_AVAILABLE_IN_1_12 |
| 238 | char *nm_utils_tc_action_to_str(NMTCAction *action, GError **error); |
| 239 | |
| 240 | NM_AVAILABLE_IN_1_12 |
| 241 | NMTCTfilter *nm_utils_tc_tfilter_from_str(const char *str, GError **error); |
| 242 | NM_AVAILABLE_IN_1_12 |
| 243 | char *nm_utils_tc_tfilter_to_str(NMTCTfilter *tfilter, GError **error); |
| 244 | |
| 245 | /*****************************************************************************/ |
| 246 | |
| 247 | NM_AVAILABLE_IN_1_14 |
| 248 | char *nm_utils_sriov_vf_to_str(const NMSriovVF *vf, gboolean omit_index, GError **error); |
| 249 | NM_AVAILABLE_IN_1_14 |
| 250 | NMSriovVF *nm_utils_sriov_vf_from_str(const char *str, GError **error); |
| 251 | |
| 252 | /*****************************************************************************/ |
| 253 | |
| 254 | NM_AVAILABLE_IN_1_12 |
| 255 | gint64 nm_utils_get_timestamp_msec(void); |
| 256 | |
| 257 | NM_AVAILABLE_IN_1_16 |
| 258 | gboolean |
| 259 | nm_utils_base64secret_decode(const char *base64_key, gsize required_key_len, guint8 *out_key); |
| 260 | |
| 261 | NM_AVAILABLE_IN_1_42 |
| 262 | void nm_utils_ensure_gtypes(void); |
| 263 | |
| 264 | G_END_DECLS |
| 265 | |
| 266 | #endif /* __NM_UTILS_H__ */ |
| 267 | |