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

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