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
20G_BEGIN_DECLS
21
22/*****************************************************************************/
23
24typedef struct _NMVariantAttributeSpec NMVariantAttributeSpec;
25
26/* SSID helpers */
27gboolean nm_utils_is_empty_ssid(const guint8 *ssid, gsize len);
28const char *nm_utils_escape_ssid(const guint8 *ssid, gsize len);
29gboolean nm_utils_same_ssid(const guint8 *ssid1,
30 gsize len1,
31 const guint8 *ssid2,
32 gsize len2,
33 gboolean ignore_trailing_null);
34char *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 **/
57typedef 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
72gboolean 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
80gboolean nm_utils_ap_mode_security_valid(NMUtilsSecurityType type,
81 NMDeviceWifiCapabilities wifi_caps);
82
83gboolean nm_utils_wep_key_valid(const char *key, NMWepKeyType wep_type);
84gboolean nm_utils_wpa_psk_valid(const char *psk);
85
86NM_AVAILABLE_IN_1_6
87gboolean nm_utils_is_json_object(const char *str, GError **error);
88
89GVariant *nm_utils_ip4_dns_to_variant(char **dns);
90char **nm_utils_ip4_dns_from_variant(GVariant *value);
91GVariant *nm_utils_ip4_addresses_to_variant(GPtrArray *addresses, const char *gateway);
92GPtrArray *nm_utils_ip4_addresses_from_variant(GVariant *value, char **out_gateway);
93GVariant *nm_utils_ip4_routes_to_variant(GPtrArray *routes);
94GPtrArray *nm_utils_ip4_routes_from_variant(GVariant *value);
95
96guint32 nm_utils_ip4_netmask_to_prefix(guint32 netmask);
97guint32 nm_utils_ip4_prefix_to_netmask(guint32 prefix);
98guint32 nm_utils_ip4_get_default_prefix(guint32 ip);
99
100GVariant *nm_utils_ip6_dns_to_variant(char **dns);
101char **nm_utils_ip6_dns_from_variant(GVariant *value);
102GVariant *nm_utils_ip6_addresses_to_variant(GPtrArray *addresses, const char *gateway);
103GPtrArray *nm_utils_ip6_addresses_from_variant(GVariant *value, char **out_gateway);
104GVariant *nm_utils_ip6_routes_to_variant(GPtrArray *routes);
105GPtrArray *nm_utils_ip6_routes_from_variant(GVariant *value);
106
107GVariant *nm_utils_ip_addresses_to_variant(GPtrArray *addresses);
108GPtrArray *nm_utils_ip_addresses_from_variant(GVariant *value, int family);
109GVariant *nm_utils_ip_routes_to_variant(GPtrArray *routes);
110GPtrArray *nm_utils_ip_routes_from_variant(GVariant *value, int family);
111
112char *nm_utils_uuid_generate(void);
113
114gboolean nm_utils_file_is_certificate(const char *filename);
115gboolean nm_utils_file_is_private_key(const char *filename, gboolean *out_encrypted);
116gboolean nm_utils_file_is_pkcs12(const char *filename);
117
118typedef gboolean (*NMUtilsFileSearchInPathsPredicate)(const char *filename, gpointer user_data);
119
120struct stat;
121
122typedef gboolean (*NMUtilsCheckFilePredicate)(const char *filename,
123 const struct stat *stat,
124 gpointer user_data,
125 GError **error);
126
127const 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
135guint32 nm_utils_wifi_freq_to_channel(guint32 freq);
136guint32 nm_utils_wifi_channel_to_freq(guint32 channel, const char *band);
137guint32 nm_utils_wifi_find_next_channel(guint32 channel, int direction, char *band);
138gboolean nm_utils_wifi_is_channel_valid(guint32 channel, const char *band);
139NM_AVAILABLE_IN_1_2
140const guint *nm_utils_wifi_2ghz_freqs(void);
141NM_AVAILABLE_IN_1_2
142const guint *nm_utils_wifi_5ghz_freqs(void);
143
144const 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
154gsize nm_utils_hwaddr_len(int type) G_GNUC_PURE;
155
156char *nm_utils_hwaddr_ntoa(gconstpointer addr, gsize length);
157GByteArray *nm_utils_hwaddr_atoba(const char *asc, gsize length);
158guint8 *nm_utils_hwaddr_aton(const char *asc, gpointer buffer, gsize length);
159
160gboolean nm_utils_hwaddr_valid(const char *asc, gssize length);
161char *nm_utils_hwaddr_canonical(const char *asc, gssize length);
162gboolean nm_utils_hwaddr_matches(gconstpointer hwaddr1,
163 gssize hwaddr1_len,
164 gconstpointer hwaddr2,
165 gssize hwaddr2_len);
166
167char *nm_utils_bin2hexstr(gconstpointer src, gsize len, int final_len);
168GBytes *nm_utils_hexstr2bin(const char *hex);
169
170NM_DEPRECATED_IN_1_6_FOR(nm_utils_is_valid_iface_name)
171gboolean nm_utils_iface_valid_name(const char *name);
172NM_AVAILABLE_IN_1_6
173gboolean nm_utils_is_valid_iface_name(const char *name, GError **error);
174
175gboolean 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
185const char *nm_utils_inet4_ntop(guint32 inaddr, char *dst);
186
187struct in6_addr;
188const char *nm_utils_inet6_ntop(const struct in6_addr *in6addr, char *dst);
189
190gboolean nm_utils_ipaddr_valid(int family, const char *ip);
191
192gboolean nm_utils_check_virtual_device_compatibility(GType virtual_type, GType other_type);
193
194NM_AVAILABLE_IN_1_2
195int nm_utils_bond_mode_string_to_int(const char *mode);
196NM_AVAILABLE_IN_1_2
197const char *nm_utils_bond_mode_int_to_string(int mode);
198
199NM_AVAILABLE_IN_1_2
200char *nm_utils_enum_to_str(GType type, int value);
201
202NM_AVAILABLE_IN_1_2
203gboolean nm_utils_enum_from_str(GType type, const char *str, int *out_value, char **err_token);
204
205NM_AVAILABLE_IN_1_2
206const char **nm_utils_enum_get_values(GType type, int from, int to);
207
208NM_AVAILABLE_IN_1_6
209guint nm_utils_version(void);
210
211NM_AVAILABLE_IN_1_8
212GHashTable *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
219NM_AVAILABLE_IN_1_8
220char *nm_utils_format_variant_attributes(GHashTable *attributes,
221 char attr_separator,
222 char key_value_separator);
223
224/*****************************************************************************/
225
226NM_AVAILABLE_IN_1_12
227NMTCQdisc *nm_utils_tc_qdisc_from_str(const char *str, GError **error);
228NM_AVAILABLE_IN_1_12
229char *nm_utils_tc_qdisc_to_str(NMTCQdisc *qdisc, GError **error);
230
231NM_AVAILABLE_IN_1_12
232NMTCAction *nm_utils_tc_action_from_str(const char *str, GError **error);
233NM_AVAILABLE_IN_1_12
234char *nm_utils_tc_action_to_str(NMTCAction *action, GError **error);
235
236NM_AVAILABLE_IN_1_12
237NMTCTfilter *nm_utils_tc_tfilter_from_str(const char *str, GError **error);
238NM_AVAILABLE_IN_1_12
239char *nm_utils_tc_tfilter_to_str(NMTCTfilter *tfilter, GError **error);
240
241/*****************************************************************************/
242
243NM_AVAILABLE_IN_1_14
244char *nm_utils_sriov_vf_to_str(const NMSriovVF *vf, gboolean omit_index, GError **error);
245NM_AVAILABLE_IN_1_14
246NMSriovVF *nm_utils_sriov_vf_from_str(const char *str, GError **error);
247
248/*****************************************************************************/
249
250NM_AVAILABLE_IN_1_12
251gint64 nm_utils_get_timestamp_msec(void);
252
253NM_AVAILABLE_IN_1_16
254gboolean
255nm_utils_base64secret_decode(const char *base64_key, gsize required_key_len, guint8 *out_key);
256
257G_END_DECLS
258
259#endif /* __NM_UTILS_H__ */
260

source code of include/libnm/nm-utils.h