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
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <linux/genetlink.h>
#include "sd-netlink.h"
#include "alloc-util.h"
#include "netlink-genl.h"
#include "netlink-internal.h"
#include "netlink-types.h"
typedef struct GenericNetlinkFamily {
sd_netlink *genl;
const NLAPolicySet *policy_set;
uint16_t id; /* a.k.a nlmsg_type */
char *name;
uint32_t version;
uint32_t additional_header_size;
Hashmap *multicast_group_by_name;
} GenericNetlinkFamily;
static const GenericNetlinkFamily nlctrl_static = {
.id = GENL_ID_CTRL,
.name = (char*) CTRL_GENL_NAME,
.version = 0x01,
};
static GenericNetlinkFamily *genl_family_free(GenericNetlinkFamily *f) {
if (!f)
return NULL;
if (f->genl) {
if (f->id > 0)
hashmap_remove(f->genl->genl_family_by_id, UINT_TO_PTR(f->id));
if (f->name)
hashmap_remove(f->genl->genl_family_by_name, f->name);
}
free(f->name);
hashmap_free(f->multicast_group_by_name);
return mfree(f);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(GenericNetlinkFamily*, genl_family_free);
void genl_clear_family(sd_netlink *nl) {
assert(nl);
nl->genl_family_by_name = hashmap_free_with_destructor(nl->genl_family_by_name, genl_family_free);
nl->genl_family_by_id = hashmap_free_with_destructor(nl->genl_family_by_id, genl_family_free);
}
static int genl_family_new_unsupported(
sd_netlink *nl,
const char *family_name,
const NLAPolicySet *policy_set) {
_cleanup_(genl_family_freep) GenericNetlinkFamily *f = NULL;
int r;
assert(nl);
assert(family_name);
assert(policy_set);
/* Kernel does not support the genl family? To prevent from resolving the family name again,
* let's store the family with zero id to indicate that. */
f = new(GenericNetlinkFamily, 1);
if (!f)
return -ENOMEM;
*f = (GenericNetlinkFamily) {
.policy_set = policy_set,
};
f->name = strdup(family_name);
if (!f->name)
return -ENOMEM;
r = hashmap_ensure_put(&nl->genl_family_by_name, &string_hash_ops, f->name, f);
if (r < 0)
return r;
f->genl = nl;
TAKE_PTR(f);
return 0;
}
static int genl_family_new(
sd_netlink *nl,
const char *expected_family_name,
const NLAPolicySet *policy_set,
sd_netlink_message *message,
const GenericNetlinkFamily **ret) {
_cleanup_(genl_family_freep) GenericNetlinkFamily *f = NULL;
const char *family_name;
uint8_t cmd;
int r;
assert(nl);
assert(expected_family_name);
assert(policy_set);
assert(message);
assert(ret);
f = new(GenericNetlinkFamily, 1);
if (!f)
return -ENOMEM;
*f = (GenericNetlinkFamily) {
.policy_set = policy_set,
};
r = sd_genl_message_get_family_name(nl, message, &family_name);
if (r < 0)
return r;
if (!streq(family_name, CTRL_GENL_NAME))
return -EINVAL;
r = sd_genl_message_get_command(nl, message, &cmd);
if (r < 0)
return r;
if (cmd != CTRL_CMD_NEWFAMILY)
return -EINVAL;
r = sd_netlink_message_read_u16(message, CTRL_ATTR_FAMILY_ID, &f->id);
if (r < 0)
return r;
r = sd_netlink_message_read_string_strdup(message, CTRL_ATTR_FAMILY_NAME, &f->name);
if (r < 0)
return r;
if (!streq(f->name, expected_family_name))
return -EINVAL;
r = sd_netlink_message_read_u32(message, CTRL_ATTR_VERSION, &f->version);
if (r < 0)
return r;
r = sd_netlink_message_read_u32(message, CTRL_ATTR_HDRSIZE, &f->additional_header_size);
if (r < 0)
return r;
r = sd_netlink_message_enter_container(message, CTRL_ATTR_MCAST_GROUPS);
if (r >= 0) {
for (uint16_t i = 0; i < UINT16_MAX; i++) {
_cleanup_free_ char *group_name = NULL;
uint32_t group_id;
r = sd_netlink_message_enter_array(message, i + 1);
if (r == -ENODATA)
break;
if (r < 0)
return r;
r = sd_netlink_message_read_u32(message, CTRL_ATTR_MCAST_GRP_ID, &group_id);
if (r < 0)
return r;
r = sd_netlink_message_read_string_strdup(message, CTRL_ATTR_MCAST_GRP_NAME, &group_name);
if (r < 0)
return r;
r = sd_netlink_message_exit_container(message);
if (r < 0)
return r;
if (group_id == 0) {
log_debug("sd-netlink: received multicast group '%s' for generic netlink family '%s' with id == 0, ignoring",
group_name, f->name);
continue;
}
r = hashmap_ensure_put(&f->multicast_group_by_name, &string_hash_ops_free, group_name, UINT32_TO_PTR(group_id));
if (r < 0)
return r;
TAKE_PTR(group_name);
}
r = sd_netlink_message_exit_container(message);
if (r < 0)
return r;
}
r = hashmap_ensure_put(&nl->genl_family_by_id, NULL, UINT_TO_PTR(f->id), f);
if (r < 0)
return r;
r = hashmap_ensure_put(&nl->genl_family_by_name, &string_hash_ops, f->name, f);
if (r < 0) {
hashmap_remove(nl->genl_family_by_id, UINT_TO_PTR(f->id));
return r;
}
f->genl = nl;
*ret = TAKE_PTR(f);
return 0;
}
static const NLAPolicySet *genl_family_get_policy_set(const GenericNetlinkFamily *family) {
assert(family);
if (family->policy_set)
return family->policy_set;
return genl_get_policy_set_by_name(family->name);
}
static int genl_message_new(
sd_netlink *nl,
const GenericNetlinkFamily *family,
uint8_t cmd,
sd_netlink_message **ret) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
const NLAPolicySet *policy_set;
int r;
assert(nl);
assert(nl->protocol == NETLINK_GENERIC);
assert(family);
assert(ret);
policy_set = genl_family_get_policy_set(family);
if (!policy_set)
return -EOPNOTSUPP;
r = message_new_full(nl, family->id, policy_set,
sizeof(struct genlmsghdr) + family->additional_header_size, &m);
if (r < 0)
return r;
*(struct genlmsghdr *) NLMSG_DATA(m->hdr) = (struct genlmsghdr) {
.cmd = cmd,
.version = family->version,
};
*ret = TAKE_PTR(m);
return 0;
}
static int genl_family_get_by_name_internal(
sd_netlink *nl,
const GenericNetlinkFamily *ctrl,
const char *name,
const GenericNetlinkFamily **ret) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
const NLAPolicySet *policy_set;
int r;
assert(nl);
assert(nl->protocol == NETLINK_GENERIC);
assert(ctrl);
assert(name);
assert(ret);
policy_set = genl_get_policy_set_by_name(name);
if (!policy_set)
return -EOPNOTSUPP;
r = genl_message_new(nl, ctrl, CTRL_CMD_GETFAMILY, &req);
if (r < 0)
return r;
r = sd_netlink_message_append_string(req, CTRL_ATTR_FAMILY_NAME, name);
if (r < 0)
return r;
if (sd_netlink_call(nl, req, 0, &reply) < 0) {
(void) genl_family_new_unsupported(nl, name, policy_set);
return -EOPNOTSUPP;
}
return genl_family_new(nl, name, policy_set, reply, ret);
}
static int genl_family_get_by_name(sd_netlink *nl, const char *name, const GenericNetlinkFamily **ret) {
const GenericNetlinkFamily *f, *ctrl;
int r;
assert(nl);
assert(nl->protocol == NETLINK_GENERIC);
assert(name);
assert(ret);
f = hashmap_get(nl->genl_family_by_name, name);
if (f) {
if (f->id == 0) /* kernel does not support the family. */
return -EOPNOTSUPP;
*ret = f;
return 0;
}
if (streq(name, CTRL_GENL_NAME))
return genl_family_get_by_name_internal(nl, &nlctrl_static, CTRL_GENL_NAME, ret);
ctrl = hashmap_get(nl->genl_family_by_name, CTRL_GENL_NAME);
if (!ctrl) {
r = genl_family_get_by_name_internal(nl, &nlctrl_static, CTRL_GENL_NAME, &ctrl);
if (r < 0)
return r;
}
return genl_family_get_by_name_internal(nl, ctrl, name, ret);
}
static int genl_family_get_by_id(sd_netlink *nl, uint16_t id, const GenericNetlinkFamily **ret) {
const GenericNetlinkFamily *f;
assert(nl);
assert(nl->protocol == NETLINK_GENERIC);
assert(ret);
f = hashmap_get(nl->genl_family_by_id, UINT_TO_PTR(id));
if (f) {
*ret = f;
return 0;
}
if (id == GENL_ID_CTRL) {
*ret = &nlctrl_static;
return 0;
}
return -ENOENT;
}
int genl_get_policy_set_and_header_size(
sd_netlink *nl,
uint16_t id,
const NLAPolicySet **ret_policy_set,
size_t *ret_header_size) {
const GenericNetlinkFamily *f;
int r;
assert(nl);
assert(nl->protocol == NETLINK_GENERIC);
r = genl_family_get_by_id(nl, id, &f);
if (r < 0)
return r;
if (ret_policy_set) {
const NLAPolicySet *p;
p = genl_family_get_policy_set(f);
if (!p)
return -EOPNOTSUPP;
*ret_policy_set = p;
}
if (ret_header_size)
*ret_header_size = sizeof(struct genlmsghdr) + f->additional_header_size;
return 0;
}
int sd_genl_message_new(sd_netlink *nl, const char *family_name, uint8_t cmd, sd_netlink_message **ret) {
const GenericNetlinkFamily *family;
int r;
assert_return(nl, -EINVAL);
assert_return(nl->protocol == NETLINK_GENERIC, -EINVAL);
assert_return(family_name, -EINVAL);
assert_return(ret, -EINVAL);
r = genl_family_get_by_name(nl, family_name, &family);
if (r < 0)
return r;
return genl_message_new(nl, family, cmd, ret);
}
int sd_genl_message_get_family_name(sd_netlink *nl, sd_netlink_message *m, const char **ret) {
const GenericNetlinkFamily *family;
uint16_t nlmsg_type;
int r;
assert_return(nl, -EINVAL);
assert_return(nl->protocol == NETLINK_GENERIC, -EINVAL);
assert_return(m, -EINVAL);
assert_return(ret, -EINVAL);
r = sd_netlink_message_get_type(m, &nlmsg_type);
if (r < 0)
return r;
r = genl_family_get_by_id(nl, nlmsg_type, &family);
if (r < 0)
return r;
*ret = family->name;
return 0;
}
int sd_genl_message_get_command(sd_netlink *nl, sd_netlink_message *m, uint8_t *ret) {
struct genlmsghdr *h;
uint16_t nlmsg_type;
size_t size;
int r;
assert_return(nl, -EINVAL);
assert_return(nl->protocol == NETLINK_GENERIC, -EINVAL);
assert_return(m, -EINVAL);
assert_return(m->protocol == NETLINK_GENERIC, -EINVAL);
assert_return(m->hdr, -EINVAL);
assert_return(ret, -EINVAL);
r = sd_netlink_message_get_type(m, &nlmsg_type);
if (r < 0)
return r;
r = genl_get_policy_set_and_header_size(nl, nlmsg_type, NULL, &size);
if (r < 0)
return r;
if (m->hdr->nlmsg_len < NLMSG_LENGTH(size))
return -EBADMSG;
h = NLMSG_DATA(m->hdr);
*ret = h->cmd;
return 0;
}
static int genl_family_get_multicast_group_id_by_name(const GenericNetlinkFamily *f, const char *name, uint32_t *ret) {
void *p;
assert(f);
assert(name);
p = hashmap_get(f->multicast_group_by_name, name);
if (!p)
return -ENOENT;
if (ret)
*ret = PTR_TO_UINT32(p);
return 0;
}
int sd_genl_add_match(
sd_netlink *nl,
sd_netlink_slot **ret_slot,
const char *family_name,
const char *multicast_group_name,
uint8_t command,
sd_netlink_message_handler_t callback,
sd_netlink_destroy_t destroy_callback,
void *userdata,
const char *description) {
const GenericNetlinkFamily *f;
uint32_t multicast_group_id;
int r;
assert_return(nl, -EINVAL);
assert_return(nl->protocol == NETLINK_GENERIC, -EINVAL);
assert_return(callback, -EINVAL);
assert_return(family_name, -EINVAL);
assert_return(multicast_group_name, -EINVAL);
/* If command == 0, then all commands belonging to the multicast group trigger the callback. */
r = genl_family_get_by_name(nl, family_name, &f);
if (r < 0)
return r;
r = genl_family_get_multicast_group_id_by_name(f, multicast_group_name, &multicast_group_id);
if (r < 0)
return r;
return netlink_add_match_internal(nl, ret_slot, &multicast_group_id, 1, f->id, command,
callback, destroy_callback, userdata, description);
}
int sd_genl_socket_open(sd_netlink **ret) {
return netlink_open_family(ret, NETLINK_GENERIC);
}
|