| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/genetlink.h> |
| 4 |
|
| 5 |
#include "sd-netlink.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "hashmap.h" |
| 9 |
#include "log.h" |
| 10 |
#include "netlink-genl.h" |
| 11 |
#include "netlink-internal.h" |
| 12 |
#include "netlink-types.h" |
| 13 |
#include "string-util.h" |
| 14 |
|
| 15 |
typedef struct GenericNetlinkFamily { |
| 16 |
sd_netlink *genl; |
| 17 |
|
| 18 |
const NLAPolicySet *policy_set; |
| 19 |
|
| 20 |
uint16_t id; /* a.k.a nlmsg_type */ |
| 21 |
char *name; |
| 22 |
uint32_t version; |
| 23 |
uint32_t additional_header_size; |
| 24 |
Hashmap *multicast_group_by_name; |
| 25 |
} GenericNetlinkFamily; |
| 26 |
|
| 27 |
static const GenericNetlinkFamily nlctrl_static = { |
| 28 |
.id = GENL_ID_CTRL, |
| 29 |
.name = (char*) CTRL_GENL_NAME, |
| 30 |
.version = 0x01, |
| 31 |
}; |
| 32 |
|
| 33 |
static GenericNetlinkFamily *genl_family_free(GenericNetlinkFamily *f) { |
| 34 |
if (!f) |
| 35 |
return NULL; |
| 36 |
|
| 37 |
if (f->genl) { |
| 38 |
if (f->id > 0) |
| 39 |
hashmap_remove(f->genl->genl_family_by_id, UINT_TO_PTR(f->id)); |
| 40 |
if (f->name) |
| 41 |
hashmap_remove(f->genl->genl_family_by_name, f->name); |
| 42 |
} |
| 43 |
|
| 44 |
free(f->name); |
| 45 |
hashmap_free(f->multicast_group_by_name); |
| 46 |
|
| 47 |
return mfree(f); |
| 48 |
} |
| 49 |
|
| 50 |
DEFINE_TRIVIAL_CLEANUP_FUNC(GenericNetlinkFamily*, genl_family_free); |
| 51 |
|
| 52 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 53 |
genl_family_hash_ops_by_name, |
| 54 |
char, string_hash_func, string_compare_func, |
| 55 |
GenericNetlinkFamily, genl_family_free); |
| 56 |
|
| 57 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 58 |
genl_family_hash_ops_by_id, |
| 59 |
void, trivial_hash_func, trivial_compare_func, |
| 60 |
GenericNetlinkFamily, genl_family_free); |
| 61 |
|
| 62 |
void genl_clear_family(sd_netlink *nl) { |
| 63 |
assert(nl); |
| 64 |
|
| 65 |
nl->genl_family_by_name = hashmap_free(nl->genl_family_by_name); |
| 66 |
nl->genl_family_by_id = hashmap_free(nl->genl_family_by_id); |
| 67 |
} |
| 68 |
|
| 69 |
static int genl_family_new_unsupported( |
| 70 |
sd_netlink *nl, |
| 71 |
const char *family_name, |
| 72 |
const NLAPolicySet *policy_set) { |
| 73 |
|
| 74 |
_cleanup_(genl_family_freep) GenericNetlinkFamily *f = NULL; |
| 75 |
int r; |
| 76 |
|
| 77 |
assert(nl); |
| 78 |
assert(family_name); |
| 79 |
assert(policy_set); |
| 80 |
|
| 81 |
/* Kernel does not support the genl family? To prevent from resolving the family name again, |
| 82 |
* let's store the family with zero id to indicate that. */ |
| 83 |
|
| 84 |
f = new(GenericNetlinkFamily, 1); |
| 85 |
if (!f) |
| 86 |
return -ENOMEM; |
| 87 |
|
| 88 |
*f = (GenericNetlinkFamily) { |
| 89 |
.policy_set = policy_set, |
| 90 |
}; |
| 91 |
|
| 92 |
f->name = strdup(family_name); |
| 93 |
if (!f->name) |
| 94 |
return -ENOMEM; |
| 95 |
|
| 96 |
r = hashmap_ensure_put(&nl->genl_family_by_name, &genl_family_hash_ops_by_name, f->name, f); |
| 97 |
if (r < 0) |
| 98 |
return r; |
| 99 |
|
| 100 |
f->genl = nl; |
| 101 |
TAKE_PTR(f); |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
static int genl_family_new( |
| 106 |
sd_netlink *nl, |
| 107 |
const char *expected_family_name, |
| 108 |
const NLAPolicySet *policy_set, |
| 109 |
sd_netlink_message *message, |
| 110 |
const GenericNetlinkFamily **ret) { |
| 111 |
|
| 112 |
_cleanup_(genl_family_freep) GenericNetlinkFamily *f = NULL; |
| 113 |
const char *family_name; |
| 114 |
uint8_t cmd; |
| 115 |
int r; |
| 116 |
|
| 117 |
assert(nl); |
| 118 |
assert(expected_family_name); |
| 119 |
assert(policy_set); |
| 120 |
assert(message); |
| 121 |
assert(ret); |
| 122 |
|
| 123 |
f = new(GenericNetlinkFamily, 1); |
| 124 |
if (!f) |
| 125 |
return -ENOMEM; |
| 126 |
|
| 127 |
*f = (GenericNetlinkFamily) { |
| 128 |
.policy_set = policy_set, |
| 129 |
}; |
| 130 |
|
| 131 |
r = sd_genl_message_get_family_name(nl, message, &family_name); |
| 132 |
if (r < 0) |
| 133 |
return r; |
| 134 |
|
| 135 |
if (!streq(family_name, CTRL_GENL_NAME)) |
| 136 |
return -EINVAL; |
| 137 |
|
| 138 |
r = sd_genl_message_get_command(nl, message, &cmd); |
| 139 |
if (r < 0) |
| 140 |
return r; |
| 141 |
|
| 142 |
if (cmd != CTRL_CMD_NEWFAMILY) |
| 143 |
return -EINVAL; |
| 144 |
|
| 145 |
r = sd_netlink_message_read_u16(message, CTRL_ATTR_FAMILY_ID, &f->id); |
| 146 |
if (r < 0) |
| 147 |
return r; |
| 148 |
|
| 149 |
r = sd_netlink_message_read_string_strdup(message, CTRL_ATTR_FAMILY_NAME, &f->name); |
| 150 |
if (r < 0) |
| 151 |
return r; |
| 152 |
|
| 153 |
if (!streq(f->name, expected_family_name)) |
| 154 |
return -EINVAL; |
| 155 |
|
| 156 |
r = sd_netlink_message_read_u32(message, CTRL_ATTR_VERSION, &f->version); |
| 157 |
if (r < 0) |
| 158 |
return r; |
| 159 |
|
| 160 |
r = sd_netlink_message_read_u32(message, CTRL_ATTR_HDRSIZE, &f->additional_header_size); |
| 161 |
if (r < 0) |
| 162 |
return r; |
| 163 |
|
| 164 |
r = sd_netlink_message_enter_container(message, CTRL_ATTR_MCAST_GROUPS); |
| 165 |
if (r >= 0) { |
| 166 |
for (uint16_t i = 0; i < UINT16_MAX; i++) { |
| 167 |
_cleanup_free_ char *group_name = NULL; |
| 168 |
uint32_t group_id; |
| 169 |
|
| 170 |
r = sd_netlink_message_enter_array(message, i + 1); |
| 171 |
if (r == -ENODATA) |
| 172 |
break; |
| 173 |
if (r < 0) |
| 174 |
return r; |
| 175 |
|
| 176 |
r = sd_netlink_message_read_u32(message, CTRL_ATTR_MCAST_GRP_ID, &group_id); |
| 177 |
if (r < 0) |
| 178 |
return r; |
| 179 |
|
| 180 |
r = sd_netlink_message_read_string_strdup(message, CTRL_ATTR_MCAST_GRP_NAME, &group_name); |
| 181 |
if (r < 0) |
| 182 |
return r; |
| 183 |
|
| 184 |
r = sd_netlink_message_exit_container(message); |
| 185 |
if (r < 0) |
| 186 |
return r; |
| 187 |
|
| 188 |
if (group_id == 0) { |
| 189 |
log_debug("sd-netlink: received multicast group '%s' for generic netlink family '%s' with id == 0, ignoring", |
| 190 |
group_name, f->name); |
| 191 |
continue; |
| 192 |
} |
| 193 |
|
| 194 |
r = hashmap_ensure_put(&f->multicast_group_by_name, &string_hash_ops_free, group_name, UINT32_TO_PTR(group_id)); |
| 195 |
if (r < 0) |
| 196 |
return r; |
| 197 |
|
| 198 |
TAKE_PTR(group_name); |
| 199 |
} |
| 200 |
|
| 201 |
r = sd_netlink_message_exit_container(message); |
| 202 |
if (r < 0) |
| 203 |
return r; |
| 204 |
} |
| 205 |
|
| 206 |
r = hashmap_ensure_put(&nl->genl_family_by_id, &genl_family_hash_ops_by_id, UINT_TO_PTR(f->id), f); |
| 207 |
if (r < 0) |
| 208 |
return r; |
| 209 |
|
| 210 |
r = hashmap_ensure_put(&nl->genl_family_by_name, &genl_family_hash_ops_by_name, f->name, f); |
| 211 |
if (r < 0) { |
| 212 |
hashmap_remove(nl->genl_family_by_id, UINT_TO_PTR(f->id)); |
| 213 |
return r; |
| 214 |
} |
| 215 |
|
| 216 |
f->genl = nl; |
| 217 |
*ret = TAKE_PTR(f); |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
static const NLAPolicySet *genl_family_get_policy_set(const GenericNetlinkFamily *family) { |
| 222 |
assert(family); |
| 223 |
|
| 224 |
if (family->policy_set) |
| 225 |
return family->policy_set; |
| 226 |
|
| 227 |
return genl_get_policy_set_by_name(family->name); |
| 228 |
} |
| 229 |
|
| 230 |
static int genl_message_new( |
| 231 |
sd_netlink *nl, |
| 232 |
const GenericNetlinkFamily *family, |
| 233 |
uint8_t cmd, |
| 234 |
sd_netlink_message **ret) { |
| 235 |
|
| 236 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 237 |
const NLAPolicySet *policy_set; |
| 238 |
int r; |
| 239 |
|
| 240 |
assert(nl); |
| 241 |
assert(nl->protocol == NETLINK_GENERIC); |
| 242 |
assert(family); |
| 243 |
assert(ret); |
| 244 |
|
| 245 |
policy_set = genl_family_get_policy_set(family); |
| 246 |
if (!policy_set) |
| 247 |
return -EOPNOTSUPP; |
| 248 |
|
| 249 |
r = message_new_full(nl, family->id, NLM_F_REQUEST | NLM_F_ACK, policy_set, |
| 250 |
sizeof(struct genlmsghdr) + family->additional_header_size, &m); |
| 251 |
if (r < 0) |
| 252 |
return r; |
| 253 |
|
| 254 |
*(struct genlmsghdr *) NLMSG_DATA(m->hdr) = (struct genlmsghdr) { |
| 255 |
.cmd = cmd, |
| 256 |
.version = family->version, |
| 257 |
}; |
| 258 |
|
| 259 |
*ret = TAKE_PTR(m); |
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
static int genl_family_get_by_name_internal( |
| 264 |
sd_netlink *nl, |
| 265 |
const GenericNetlinkFamily *ctrl, |
| 266 |
const char *name, |
| 267 |
const GenericNetlinkFamily **ret) { |
| 268 |
|
| 269 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
| 270 |
const NLAPolicySet *policy_set; |
| 271 |
int r; |
| 272 |
|
| 273 |
assert(nl); |
| 274 |
assert(nl->protocol == NETLINK_GENERIC); |
| 275 |
assert(ctrl); |
| 276 |
assert(name); |
| 277 |
assert(ret); |
| 278 |
|
| 279 |
policy_set = genl_get_policy_set_by_name(name); |
| 280 |
if (!policy_set) |
| 281 |
return -EOPNOTSUPP; |
| 282 |
|
| 283 |
r = genl_message_new(nl, ctrl, CTRL_CMD_GETFAMILY, &req); |
| 284 |
if (r < 0) |
| 285 |
return r; |
| 286 |
|
| 287 |
r = sd_netlink_message_append_string(req, CTRL_ATTR_FAMILY_NAME, name); |
| 288 |
if (r < 0) |
| 289 |
return r; |
| 290 |
|
| 291 |
if (sd_netlink_call(nl, req, 0, &reply) < 0) { |
| 292 |
(void) genl_family_new_unsupported(nl, name, policy_set); |
| 293 |
return -EOPNOTSUPP; |
| 294 |
} |
| 295 |
|
| 296 |
return genl_family_new(nl, name, policy_set, reply, ret); |
| 297 |
} |
| 298 |
|
| 299 |
static int genl_family_get_by_name(sd_netlink *nl, const char *name, const GenericNetlinkFamily **ret) { |
| 300 |
const GenericNetlinkFamily *f, *ctrl; |
| 301 |
int r; |
| 302 |
|
| 303 |
assert(nl); |
| 304 |
assert(nl->protocol == NETLINK_GENERIC); |
| 305 |
assert(name); |
| 306 |
assert(ret); |
| 307 |
|
| 308 |
f = hashmap_get(nl->genl_family_by_name, name); |
| 309 |
if (f) { |
| 310 |
if (f->id == 0) /* kernel does not support the family. */ |
| 311 |
return -EOPNOTSUPP; |
| 312 |
|
| 313 |
*ret = f; |
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
if (streq(name, CTRL_GENL_NAME)) |
| 318 |
return genl_family_get_by_name_internal(nl, &nlctrl_static, CTRL_GENL_NAME, ret); |
| 319 |
|
| 320 |
ctrl = hashmap_get(nl->genl_family_by_name, CTRL_GENL_NAME); |
| 321 |
if (!ctrl) { |
| 322 |
r = genl_family_get_by_name_internal(nl, &nlctrl_static, CTRL_GENL_NAME, &ctrl); |
| 323 |
if (r < 0) |
| 324 |
return r; |
| 325 |
} |
| 326 |
|
| 327 |
return genl_family_get_by_name_internal(nl, ctrl, name, ret); |
| 328 |
} |
| 329 |
|
| 330 |
static int genl_family_get_by_id(sd_netlink *nl, uint16_t id, const GenericNetlinkFamily **ret) { |
| 331 |
const GenericNetlinkFamily *f; |
| 332 |
|
| 333 |
assert(nl); |
| 334 |
assert(nl->protocol == NETLINK_GENERIC); |
| 335 |
assert(ret); |
| 336 |
|
| 337 |
f = hashmap_get(nl->genl_family_by_id, UINT_TO_PTR(id)); |
| 338 |
if (f) { |
| 339 |
*ret = f; |
| 340 |
return 0; |
| 341 |
} |
| 342 |
|
| 343 |
if (id == GENL_ID_CTRL) { |
| 344 |
*ret = &nlctrl_static; |
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
return -ENOENT; |
| 349 |
} |
| 350 |
|
| 351 |
int genl_get_policy_set_and_header_size( |
| 352 |
sd_netlink *nl, |
| 353 |
uint16_t id, |
| 354 |
const NLAPolicySet **ret_policy_set, |
| 355 |
size_t *ret_header_size) { |
| 356 |
|
| 357 |
const GenericNetlinkFamily *f; |
| 358 |
int r; |
| 359 |
|
| 360 |
assert(nl); |
| 361 |
assert(nl->protocol == NETLINK_GENERIC); |
| 362 |
|
| 363 |
r = genl_family_get_by_id(nl, id, &f); |
| 364 |
if (r < 0) |
| 365 |
return r; |
| 366 |
|
| 367 |
if (ret_policy_set) { |
| 368 |
const NLAPolicySet *p; |
| 369 |
|
| 370 |
p = genl_family_get_policy_set(f); |
| 371 |
if (!p) |
| 372 |
return -EOPNOTSUPP; |
| 373 |
|
| 374 |
*ret_policy_set = p; |
| 375 |
} |
| 376 |
if (ret_header_size) |
| 377 |
*ret_header_size = sizeof(struct genlmsghdr) + f->additional_header_size; |
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
int sd_genl_message_new(sd_netlink *nl, const char *family_name, uint8_t cmd, sd_netlink_message **ret) { |
| 382 |
const GenericNetlinkFamily *family; |
| 383 |
int r; |
| 384 |
|
| 385 |
assert_return(nl, -EINVAL); |
| 386 |
assert_return(nl->protocol == NETLINK_GENERIC, -EINVAL); |
| 387 |
assert_return(family_name, -EINVAL); |
| 388 |
assert_return(ret, -EINVAL); |
| 389 |
|
| 390 |
r = genl_family_get_by_name(nl, family_name, &family); |
| 391 |
if (r < 0) |
| 392 |
return r; |
| 393 |
|
| 394 |
return genl_message_new(nl, family, cmd, ret); |
| 395 |
} |
| 396 |
|
| 397 |
int sd_genl_message_get_family_name(sd_netlink *nl, sd_netlink_message *m, const char **ret) { |
| 398 |
const GenericNetlinkFamily *family; |
| 399 |
uint16_t nlmsg_type; |
| 400 |
int r; |
| 401 |
|
| 402 |
assert_return(nl, -EINVAL); |
| 403 |
assert_return(nl->protocol == NETLINK_GENERIC, -EINVAL); |
| 404 |
assert_return(m, -EINVAL); |
| 405 |
assert_return(ret, -EINVAL); |
| 406 |
|
| 407 |
r = sd_netlink_message_get_type(m, &nlmsg_type); |
| 408 |
if (r < 0) |
| 409 |
return r; |
| 410 |
|
| 411 |
r = genl_family_get_by_id(nl, nlmsg_type, &family); |
| 412 |
if (r < 0) |
| 413 |
return r; |
| 414 |
|
| 415 |
*ret = family->name; |
| 416 |
return 0; |
| 417 |
} |
| 418 |
|
| 419 |
int sd_genl_message_get_command(sd_netlink *nl, sd_netlink_message *m, uint8_t *ret) { |
| 420 |
struct genlmsghdr *h; |
| 421 |
uint16_t nlmsg_type; |
| 422 |
size_t size; |
| 423 |
int r; |
| 424 |
|
| 425 |
assert_return(nl, -EINVAL); |
| 426 |
assert_return(nl->protocol == NETLINK_GENERIC, -EINVAL); |
| 427 |
assert_return(m, -EINVAL); |
| 428 |
assert_return(m->protocol == NETLINK_GENERIC, -EINVAL); |
| 429 |
assert_return(m->hdr, -EINVAL); |
| 430 |
assert_return(ret, -EINVAL); |
| 431 |
|
| 432 |
r = sd_netlink_message_get_type(m, &nlmsg_type); |
| 433 |
if (r < 0) |
| 434 |
return r; |
| 435 |
|
| 436 |
r = genl_get_policy_set_and_header_size(nl, nlmsg_type, NULL, &size); |
| 437 |
if (r < 0) |
| 438 |
return r; |
| 439 |
|
| 440 |
if (m->hdr->nlmsg_len < NLMSG_LENGTH(size)) |
| 441 |
return -EBADMSG; |
| 442 |
|
| 443 |
h = NLMSG_DATA(m->hdr); |
| 444 |
|
| 445 |
*ret = h->cmd; |
| 446 |
return 0; |
| 447 |
} |
| 448 |
|
| 449 |
static int genl_family_get_multicast_group_id_by_name(const GenericNetlinkFamily *f, const char *name, uint32_t *ret) { |
| 450 |
void *p; |
| 451 |
|
| 452 |
assert(f); |
| 453 |
assert(name); |
| 454 |
|
| 455 |
p = hashmap_get(f->multicast_group_by_name, name); |
| 456 |
if (!p) |
| 457 |
return -ENOENT; |
| 458 |
|
| 459 |
if (ret) |
| 460 |
*ret = PTR_TO_UINT32(p); |
| 461 |
return 0; |
| 462 |
} |
| 463 |
|
| 464 |
int sd_genl_add_match( |
| 465 |
sd_netlink *nl, |
| 466 |
sd_netlink_slot **ret_slot, |
| 467 |
const char *family_name, |
| 468 |
const char *multicast_group_name, |
| 469 |
uint8_t command, |
| 470 |
sd_netlink_message_handler_t callback, |
| 471 |
sd_netlink_destroy_t destroy_callback, |
| 472 |
void *userdata, |
| 473 |
const char *description) { |
| 474 |
|
| 475 |
const GenericNetlinkFamily *f; |
| 476 |
uint32_t multicast_group_id; |
| 477 |
int r; |
| 478 |
|
| 479 |
assert_return(nl, -EINVAL); |
| 480 |
assert_return(nl->protocol == NETLINK_GENERIC, -EINVAL); |
| 481 |
assert_return(callback, -EINVAL); |
| 482 |
assert_return(family_name, -EINVAL); |
| 483 |
assert_return(multicast_group_name, -EINVAL); |
| 484 |
|
| 485 |
/* If command == 0, then all commands belonging to the multicast group trigger the callback. */ |
| 486 |
|
| 487 |
r = genl_family_get_by_name(nl, family_name, &f); |
| 488 |
if (r < 0) |
| 489 |
return r; |
| 490 |
|
| 491 |
r = genl_family_get_multicast_group_id_by_name(f, multicast_group_name, &multicast_group_id); |
| 492 |
if (r < 0) |
| 493 |
return r; |
| 494 |
|
| 495 |
return netlink_add_match_internal(nl, ret_slot, &multicast_group_id, 1, f->id, command, |
| 496 |
callback, destroy_callback, userdata, description); |
| 497 |
} |
| 498 |
|
| 499 |
int sd_genl_socket_open(sd_netlink **ret) { |
| 500 |
return netlink_open_family(ret, NETLINK_GENERIC); |
| 501 |
} |