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