1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2017 - 2020 Red Hat, Inc.
*/
#include "nm-default.h"
#include "nm-setting-ovs-external-ids.h"
#include "nm-setting-private.h"
#include "nm-utils-private.h"
#include "nm-connection-private.h"
#define MAX_NUM_KEYS 256
/*****************************************************************************/
/**
* SECTION:nm-setting-ovs-external-ids
* @short_description: External-IDs for OVS database
*
* The #NMSettingOvsExternalIDs object is a #NMSetting subclass that allow to
* configure external ids for OVS.
**/
/*****************************************************************************/
NM_GOBJECT_PROPERTIES_DEFINE(NMSettingOvsExternalIDs, PROP_DATA, );
typedef struct {
GHashTable * data;
const char **data_keys;
} NMSettingOvsExternalIDsPrivate;
/**
* NMSettingOvsExternalIDs:
*
* OVS External IDs Settings
*/
struct _NMSettingOvsExternalIDs {
NMSetting parent;
NMSettingOvsExternalIDsPrivate _priv;
};
struct _NMSettingOvsExternalIDsClass {
NMSettingClass parent;
};
G_DEFINE_TYPE(NMSettingOvsExternalIDs, nm_setting_ovs_external_ids, NM_TYPE_SETTING)
#define NM_SETTING_OVS_EXTERNAL_IDS_GET_PRIVATE(self) \
_NM_GET_PRIVATE(self, NMSettingOvsExternalIDs, NM_IS_SETTING_OVS_EXTERNAL_IDS)
/*****************************************************************************/
static gboolean
_exid_key_char_is_regular(char ch)
{
/* allow words of printable characters, plus some
* special characters, for example to support base64 encoding. */
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')
|| NM_IN_SET(ch, '-', '_', '+', '/', '=', '.');
}
/**
* nm_setting_ovs_external_ids_check_key:
* @key: (allow-none): the key to check
* @error: a #GError, %NULL to ignore.
*
* Checks whether @key is a valid key for OVS' external-ids.
* This means, the key cannot be %NULL, not too large and valid ASCII.
* Also, only digits and numbers are allowed with a few special
* characters. They key must also not start with "NM.".
*
* Since: 1.30
*
* Returns: %TRUE if @key is a valid user data key.
*/
gboolean
nm_setting_ovs_external_ids_check_key(const char *key, GError **error)
{
gsize len;
g_return_val_if_fail(!error || !*error, FALSE);
if (!key || !key[0]) {
g_set_error_literal(error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("missing key"));
return FALSE;
}
len = strlen(key);
if (len > 255u) {
g_set_error_literal(error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("key is too long"));
return FALSE;
}
if (!g_utf8_validate(key, len, NULL)) {
g_set_error_literal(error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("key must be UTF8"));
return FALSE;
}
if (!NM_STRCHAR_ALL(key, ch, _exid_key_char_is_regular(ch))) {
/* Probably OVS is more forgiving about what makes a valid key for
* an external-id. However, we are strict (at least, for now). */
g_set_error_literal(error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("key contains invalid characters"));
return FALSE;
}
if (NM_STR_HAS_PREFIX(key, NM_OVS_EXTERNAL_ID_NM_PREFIX)) {
/* these keys are reserved. */
g_set_error_literal(error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("key cannot start with \"NM.\""));
return FALSE;
}
return TRUE;
}
/**
* nm_setting_ovs_external_ids_check_val:
* @val: (allow-none): the value to check
* @error: a #GError, %NULL to ignore.
*
* Checks whether @val is a valid user data value. This means,
* value is not %NULL, not too large and valid UTF-8.
*
* Since: 1.30
*
* Returns: %TRUE if @val is a valid user data value.
*/
gboolean
nm_setting_ovs_external_ids_check_val(const char *val, GError **error)
{
gsize len;
g_return_val_if_fail(!error || !*error, FALSE);
if (!val) {
g_set_error_literal(error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("value is missing"));
return FALSE;
}
len = strlen(val);
if (len > (8u * 1024u)) {
g_set_error_literal(error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("value is too large"));
return FALSE;
}
if (!g_utf8_validate(val, len, NULL)) {
g_set_error_literal(error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("value is not valid UTF8"));
return FALSE;
}
return TRUE;
}
/*****************************************************************************/
static GHashTable *
_create_data_hash(void)
{
return g_hash_table_new_full(nm_str_hash, g_str_equal, g_free, g_free);
}
GHashTable *
_nm_setting_ovs_external_ids_get_data(NMSettingOvsExternalIDs *self)
{
return NM_SETTING_OVS_EXTERNAL_IDS_GET_PRIVATE(self)->data;
}
/**
* nm_setting_ovs_external_ids_get_data_keys:
* @setting: the #NMSettingOvsExternalIDs
* @out_len: (out): the length of the returned array
*
* Returns: (array length=out_len) (transfer none): a
* %NULL-terminated array containing each key from the table.
**/
const char *const *
nm_setting_ovs_external_ids_get_data_keys(NMSettingOvsExternalIDs *setting, guint *out_len)
{
NMSettingOvsExternalIDs * self = setting;
NMSettingOvsExternalIDsPrivate *priv;
g_return_val_if_fail(NM_IS_SETTING_OVS_EXTERNAL_IDS(self), NULL);
priv = NM_SETTING_OVS_EXTERNAL_IDS_GET_PRIVATE(self);
if (priv->data_keys) {
NM_SET_OUT(out_len, g_hash_table_size(priv->data));
return priv->data_keys;
}
priv->data_keys = nm_utils_strdict_get_keys(priv->data, TRUE, out_len);
/* don't return %NULL, but hijack the @data_keys fields as a pseudo
* empty strv array. */
return priv->data_keys ?: ((const char **) &priv->data_keys);
}
/*****************************************************************************/
/**
* nm_setting_ovs_external_ids_get_data:
* @setting: the #NMSettingOvsExternalIDs instance
* @key: the external-id to lookup
*
* Since: 1.30
*
* Returns: (transfer none): the value associated with @key or %NULL if no such
* value exists.
*/
const char *
nm_setting_ovs_external_ids_get_data(NMSettingOvsExternalIDs *setting, const char *key)
{
NMSettingOvsExternalIDs * self = setting;
NMSettingOvsExternalIDsPrivate *priv;
g_return_val_if_fail(NM_IS_SETTING_OVS_EXTERNAL_IDS(self), NULL);
g_return_val_if_fail(key, NULL);
priv = NM_SETTING_OVS_EXTERNAL_IDS_GET_PRIVATE(self);
if (!priv->data)
return NULL;
return g_hash_table_lookup(priv->data, key);
}
/**
* nm_setting_ovs_external_ids_set_data:
* @setting: the #NMSettingOvsExternalIDs instance
* @key: the key to set
* @val: (allow-none): the value to set or %NULL to clear a key.
*
* Since: 1.30
*/
void
nm_setting_ovs_external_ids_set_data(NMSettingOvsExternalIDs *setting,
const char * key,
const char * val)
{
NMSettingOvsExternalIDs * self = setting;
NMSettingOvsExternalIDsPrivate *priv;
g_return_if_fail(NM_IS_SETTING_OVS_EXTERNAL_IDS(self));
priv = NM_SETTING_OVS_EXTERNAL_IDS_GET_PRIVATE(self);
if (!val) {
if (priv->data && g_hash_table_remove(priv->data, key))
goto out_changed;
return;
}
if (priv->data) {
const char *val2;
if (g_hash_table_lookup_extended(priv->data, key, NULL, (gpointer *) &val2)) {
if (nm_streq(val, val2))
return;
}
} else
priv->data = _create_data_hash();
g_hash_table_insert(priv->data, g_strdup(key), g_strdup(val));
out_changed:
nm_clear_g_free(&priv->data_keys);
_notify(self, PROP_DATA);
}
/*****************************************************************************/
static gboolean
verify(NMSetting *setting, NMConnection *connection, GError **error)
{
NMSettingOvsExternalIDs * self = NM_SETTING_OVS_EXTERNAL_IDS(setting);
NMSettingOvsExternalIDsPrivate *priv = NM_SETTING_OVS_EXTERNAL_IDS_GET_PRIVATE(self);
if (priv->data) {
gs_free_error GError *local = NULL;
GHashTableIter iter;
const char * key;
const char * val;
g_hash_table_iter_init(&iter, priv->data);
while (g_hash_table_iter_next(&iter, (gpointer *) &key, (gpointer *) &val)) {
if (!nm_setting_ovs_external_ids_check_key(key, &local)) {
g_set_error(error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_FAILED,
_("invalid key \"%s\": %s"),
key,
local->message);
} else if (!nm_setting_ovs_external_ids_check_val(val, &local)) {
g_set_error(error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_FAILED,
_("invalid value for \"%s\": %s"),
key,
local->message);
} else
continue;
g_prefix_error(error,
"%s.%s: ",
NM_SETTING_OVS_EXTERNAL_IDS_SETTING_NAME,
NM_SETTING_OVS_EXTERNAL_IDS_DATA);
return FALSE;
}
}
if (priv->data && g_hash_table_size(priv->data) > MAX_NUM_KEYS) {
g_set_error(error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("maximum number of user data entries reached (%u instead of %u)"),
g_hash_table_size(priv->data),
(unsigned) MAX_NUM_KEYS);
g_prefix_error(error,
"%s.%s: ",
NM_SETTING_OVS_EXTERNAL_IDS_SETTING_NAME,
NM_SETTING_OVS_EXTERNAL_IDS_DATA);
return FALSE;
}
if (connection) {
NMSettingConnection *s_con;
const char * type;
const char * slave_type;
type = nm_connection_get_connection_type(connection);
if (!type) {
NMSetting *s_base;
s_base = _nm_connection_find_base_type_setting(connection);
if (s_base)
type = nm_setting_get_name(s_base);
}
if (NM_IN_STRSET(type,
NM_SETTING_OVS_BRIDGE_SETTING_NAME,
NM_SETTING_OVS_PORT_SETTING_NAME,
NM_SETTING_OVS_INTERFACE_SETTING_NAME))
goto connection_type_is_good;
if ((s_con = nm_connection_get_setting_connection(connection))
&& _nm_connection_detect_slave_type_full(s_con,
connection,
&slave_type,
NULL,
NULL,
NULL,
NULL)
&& nm_streq0(slave_type, NM_SETTING_OVS_PORT_SETTING_NAME))
goto connection_type_is_good;
g_set_error_literal(error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("OVS external IDs can only be added to a profile of type OVS "
"bridge/port/interface or to OVS system interface"));
return FALSE;
}
connection_type_is_good:
return TRUE;
}
static NMTernary
compare_property(const NMSettInfoSetting *sett_info,
guint property_idx,
NMConnection * con_a,
NMSetting * set_a,
NMConnection * con_b,
NMSetting * set_b,
NMSettingCompareFlags flags)
{
NMSettingOvsExternalIDsPrivate *priv;
NMSettingOvsExternalIDsPrivate *pri2;
if (nm_streq(sett_info->property_infos[property_idx].name, NM_SETTING_OVS_EXTERNAL_IDS_DATA)) {
if (NM_FLAGS_HAS(flags, NM_SETTING_COMPARE_FLAG_INFERRABLE))
return NM_TERNARY_DEFAULT;
if (!set_b)
return TRUE;
priv = NM_SETTING_OVS_EXTERNAL_IDS_GET_PRIVATE(NM_SETTING_OVS_EXTERNAL_IDS(set_a));
pri2 = NM_SETTING_OVS_EXTERNAL_IDS_GET_PRIVATE(NM_SETTING_OVS_EXTERNAL_IDS(set_b));
return nm_utils_hashtable_equal(priv->data, pri2->data, TRUE, g_str_equal);
}
return NM_SETTING_CLASS(nm_setting_ovs_external_ids_parent_class)
->compare_property(sett_info, property_idx, con_a, set_a, con_b, set_b, flags);
}
/*****************************************************************************/
static void
get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
NMSettingOvsExternalIDs * self = NM_SETTING_OVS_EXTERNAL_IDS(object);
NMSettingOvsExternalIDsPrivate *priv = NM_SETTING_OVS_EXTERNAL_IDS_GET_PRIVATE(self);
GHashTableIter iter;
GHashTable * data;
const char * key;
const char * val;
switch (prop_id) {
case PROP_DATA:
data = _create_data_hash();
if (priv->data) {
g_hash_table_iter_init(&iter, priv->data);
while (g_hash_table_iter_next(&iter, (gpointer *) &key, (gpointer *) &val))
g_hash_table_insert(data, g_strdup(key), g_strdup(val));
}
g_value_take_boxed(value, data);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
NMSettingOvsExternalIDs * self = NM_SETTING_OVS_EXTERNAL_IDS(object);
NMSettingOvsExternalIDsPrivate *priv = NM_SETTING_OVS_EXTERNAL_IDS_GET_PRIVATE(self);
switch (prop_id) {
case PROP_DATA:
{
gs_unref_hashtable GHashTable *old = NULL;
GHashTableIter iter;
GHashTable * data;
const char * key;
const char * val;
nm_clear_g_free(&priv->data_keys);
old = g_steal_pointer(&priv->data);
data = g_value_get_boxed(value);
if (nm_g_hash_table_size(data) <= 0)
return;
priv->data = _create_data_hash();
g_hash_table_iter_init(&iter, data);
while (g_hash_table_iter_next(&iter, (gpointer *) &key, (gpointer *) &val))
g_hash_table_insert(priv->data, g_strdup(key), g_strdup(val));
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
/*****************************************************************************/
static void
nm_setting_ovs_external_ids_init(NMSettingOvsExternalIDs *self)
{}
/**
* nm_setting_ovs_external_ids_new:
*
* Creates a new #NMSettingOvsExternalIDs object with default values.
*
* Returns: (transfer full) (type NMSettingOvsExternalIDs): the new empty
* #NMSettingOvsExternalIDs object
*
* Since: 1.30
*/
NMSetting *
nm_setting_ovs_external_ids_new(void)
{
return g_object_new(NM_TYPE_SETTING_OVS_EXTERNAL_IDS, NULL);
}
static void
finalize(GObject *object)
{
NMSettingOvsExternalIDs * self = NM_SETTING_OVS_EXTERNAL_IDS(object);
NMSettingOvsExternalIDsPrivate *priv = NM_SETTING_OVS_EXTERNAL_IDS_GET_PRIVATE(self);
g_free(priv->data_keys);
if (priv->data)
g_hash_table_unref(priv->data);
G_OBJECT_CLASS(nm_setting_ovs_external_ids_parent_class)->finalize(object);
}
static void
nm_setting_ovs_external_ids_class_init(NMSettingOvsExternalIDsClass *klass)
{
GObjectClass * object_class = G_OBJECT_CLASS(klass);
NMSettingClass *setting_class = NM_SETTING_CLASS(klass);
GArray * properties_override = _nm_sett_info_property_override_create_array();
object_class->get_property = get_property;
object_class->set_property = set_property;
object_class->finalize = finalize;
setting_class->compare_property = compare_property;
setting_class->verify = verify;
/**
* NMSettingOvsExternalIDs:data: (type GHashTable(utf8,utf8))
*
* A dictionary of key/value pairs with exernal-ids for OVS.
*
* Since: 1.30
**/
obj_properties[PROP_DATA] = g_param_spec_boxed(NM_SETTING_OVS_EXTERNAL_IDS_DATA,
"",
"",
G_TYPE_HASH_TABLE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
_nm_properties_override_gobj(properties_override,
obj_properties[PROP_DATA],
&nm_sett_info_propert_type_strdict);
g_object_class_install_properties(object_class, _PROPERTY_ENUMS_LAST, obj_properties);
_nm_setting_class_commit_full(setting_class,
NM_META_SETTING_TYPE_OVS_EXTERNAL_IDS,
NULL,
properties_override);
}
|