| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/filter.h> |
| 4 |
#include <poll.h> |
| 5 |
#include <stdlib.h> |
| 6 |
|
| 7 |
#include "sd-event.h" |
| 8 |
#include "sd-netlink.h" |
| 9 |
|
| 10 |
#include "alloc-util.h" |
| 11 |
#include "errno-util.h" |
| 12 |
#include "fd-util.h" |
| 13 |
#include "hashmap.h" |
| 14 |
#include "io-util.h" |
| 15 |
#include "log.h" |
| 16 |
#include "netlink-genl.h" |
| 17 |
#include "netlink-internal.h" |
| 18 |
#include "netlink-slot.h" |
| 19 |
#include "netlink-util.h" |
| 20 |
#include "ordered-set.h" |
| 21 |
#include "prioq.h" |
| 22 |
#include "process-util.h" |
| 23 |
#include "socket-util.h" |
| 24 |
#include "string-util.h" |
| 25 |
#include "time-util.h" |
| 26 |
|
| 27 |
/* Some really high limit, to catch programming errors */ |
| 28 |
#define REPLY_CALLBACKS_MAX UINT16_MAX |
| 29 |
|
| 30 |
static int netlink_new(sd_netlink **ret) { |
| 31 |
_cleanup_(sd_netlink_unrefp) sd_netlink *nl = NULL; |
| 32 |
|
| 33 |
assert_return(ret, -EINVAL); |
| 34 |
|
| 35 |
nl = new(sd_netlink, 1); |
| 36 |
if (!nl) |
| 37 |
return -ENOMEM; |
| 38 |
|
| 39 |
*nl = (sd_netlink) { |
| 40 |
.n_ref = 1, |
| 41 |
.fd = -EBADF, |
| 42 |
.sockaddr.nl.nl_family = AF_NETLINK, |
| 43 |
.original_pid = getpid_cached(), |
| 44 |
.protocol = -1, |
| 45 |
|
| 46 |
/* Kernel change notification messages have sequence number 0. We want to avoid that with our |
| 47 |
* own serials, in order not to get confused when matching up kernel replies to our earlier |
| 48 |
* requests. |
| 49 |
* |
| 50 |
* Moreover, when using netlink socket activation (i.e. where PID 1 binds an AF_NETLINK |
| 51 |
* socket for us and passes it to us across execve()) and we get restarted multiple times |
| 52 |
* while the socket sticks around we might get confused by replies from earlier runs coming |
| 53 |
* in late — which is pretty likely if we'd start our sequence numbers always from 1. Hence, |
| 54 |
* let's start with a value based on the system clock. This should make collisions much less |
| 55 |
* likely (though still theoretically possible). We use a 32 bit μs counter starting at boot |
| 56 |
* for this (and explicitly exclude the zero, see above). This counter will wrap around after |
| 57 |
* a bit more than 1h, but that's hopefully OK as the kernel shouldn't take that long to |
| 58 |
* reply to our requests. |
| 59 |
* |
| 60 |
* We only pick the initial start value this way. For each message we simply increase the |
| 61 |
* sequence number by 1. This means we could enqueue 1 netlink message per μs without risking |
| 62 |
* collisions, which should be OK. |
| 63 |
* |
| 64 |
* Note this means the serials will be in the range 1…UINT32_MAX here. |
| 65 |
* |
| 66 |
* (In an ideal world we'd attach the current serial counter to the netlink socket itself |
| 67 |
* somehow, to avoid all this, but I couldn't come up with a nice way to do this) */ |
| 68 |
.serial = (uint32_t) (now(CLOCK_MONOTONIC) % UINT32_MAX) + 1, |
| 69 |
}; |
| 70 |
|
| 71 |
*ret = TAKE_PTR(nl); |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
int sd_netlink_open_fd(sd_netlink **ret, int fd) { |
| 76 |
_cleanup_(sd_netlink_unrefp) sd_netlink *nl = NULL; |
| 77 |
int r, protocol = 0; /* Avoid maybe-uninitialized false positive */ |
| 78 |
|
| 79 |
assert_return(ret, -EINVAL); |
| 80 |
assert_return(fd >= 0, -EBADF); |
| 81 |
|
| 82 |
r = netlink_new(&nl); |
| 83 |
if (r < 0) |
| 84 |
return r; |
| 85 |
|
| 86 |
r = getsockopt_int(fd, SOL_SOCKET, SO_PROTOCOL, &protocol); |
| 87 |
if (r < 0) |
| 88 |
return r; |
| 89 |
|
| 90 |
nl->fd = fd; |
| 91 |
nl->protocol = protocol; |
| 92 |
|
| 93 |
r = setsockopt_int(fd, SOL_NETLINK, NETLINK_EXT_ACK, true); |
| 94 |
if (r < 0) |
| 95 |
log_debug_errno(r, "sd-netlink: Failed to enable NETLINK_EXT_ACK option, ignoring: %m"); |
| 96 |
|
| 97 |
r = setsockopt_int(fd, SOL_NETLINK, NETLINK_GET_STRICT_CHK, true); |
| 98 |
if (r < 0) |
| 99 |
log_debug_errno(r, "sd-netlink: Failed to enable NETLINK_GET_STRICT_CHK option, ignoring: %m"); |
| 100 |
|
| 101 |
r = socket_bind(nl); |
| 102 |
if (r < 0) { |
| 103 |
nl->fd = -EBADF; /* on failure, the caller remains owner of the fd, hence don't close it here */ |
| 104 |
nl->protocol = -1; |
| 105 |
return r; |
| 106 |
} |
| 107 |
|
| 108 |
*ret = TAKE_PTR(nl); |
| 109 |
|
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
int sd_netlink_open(sd_netlink **ret) { |
| 114 |
return netlink_open_family(ret, NETLINK_ROUTE); |
| 115 |
} |
| 116 |
|
| 117 |
int sd_netlink_increase_rxbuf(sd_netlink *nl, size_t size) { |
| 118 |
assert_return(nl, -EINVAL); |
| 119 |
assert_return(!netlink_pid_changed(nl), -ECHILD); |
| 120 |
|
| 121 |
return fd_increase_rxbuf(nl->fd, size); |
| 122 |
} |
| 123 |
|
| 124 |
static sd_netlink *netlink_free(sd_netlink *nl) { |
| 125 |
sd_netlink_slot *s; |
| 126 |
|
| 127 |
assert(nl); |
| 128 |
|
| 129 |
hashmap_free(nl->ignored_serials); |
| 130 |
|
| 131 |
ordered_set_free(nl->rqueue); |
| 132 |
hashmap_free(nl->rqueue_by_serial); |
| 133 |
hashmap_free(nl->rqueue_partial_by_serial); |
| 134 |
free(nl->rbuffer); |
| 135 |
|
| 136 |
while ((s = nl->slots)) { |
| 137 |
assert(s->floating); |
| 138 |
netlink_slot_disconnect(s, true); |
| 139 |
} |
| 140 |
hashmap_free(nl->reply_callbacks); |
| 141 |
prioq_free(nl->reply_callbacks_prioq); |
| 142 |
|
| 143 |
sd_event_source_unref(nl->io_event_source); |
| 144 |
sd_event_source_unref(nl->time_event_source); |
| 145 |
sd_event_unref(nl->event); |
| 146 |
|
| 147 |
hashmap_free(nl->broadcast_group_refs); |
| 148 |
|
| 149 |
genl_clear_family(nl); |
| 150 |
|
| 151 |
safe_close(nl->fd); |
| 152 |
return mfree(nl); |
| 153 |
} |
| 154 |
|
| 155 |
DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_netlink, sd_netlink, netlink_free); |
| 156 |
|
| 157 |
static usec_t netlink_now(sd_netlink *nl, clock_t clock) { |
| 158 |
assert(nl); |
| 159 |
|
| 160 |
usec_t now_usec; |
| 161 |
if (nl->event && sd_event_now(nl->event, clock, &now_usec) > 0) |
| 162 |
return now_usec; |
| 163 |
|
| 164 |
return now(clock); |
| 165 |
} |
| 166 |
|
| 167 |
static usec_t timespan_to_timestamp(sd_netlink *nl, usec_t usec) { |
| 168 |
static bool default_timeout_set = false; |
| 169 |
static usec_t default_timeout; |
| 170 |
int r; |
| 171 |
|
| 172 |
assert(nl); |
| 173 |
|
| 174 |
if (usec == 0) { |
| 175 |
if (!default_timeout_set) { |
| 176 |
const char *e; |
| 177 |
|
| 178 |
default_timeout_set = true; |
| 179 |
default_timeout = NETLINK_DEFAULT_TIMEOUT_USEC; |
| 180 |
|
| 181 |
e = secure_getenv("SYSTEMD_NETLINK_DEFAULT_TIMEOUT"); |
| 182 |
if (e) { |
| 183 |
r = parse_sec(e, &default_timeout); |
| 184 |
if (r < 0) |
| 185 |
log_debug_errno(r, "sd-netlink: Failed to parse $SYSTEMD_NETLINK_DEFAULT_TIMEOUT environment variable, ignoring: %m"); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
usec = default_timeout; |
| 190 |
} |
| 191 |
|
| 192 |
return usec_add(netlink_now(nl, CLOCK_MONOTONIC), usec); |
| 193 |
} |
| 194 |
|
| 195 |
static void netlink_trim_ignored_serials(sd_netlink *nl) { |
| 196 |
NetlinkIgnoredSerial *s; |
| 197 |
usec_t now_usec = 0; |
| 198 |
|
| 199 |
assert(nl); |
| 200 |
|
| 201 |
HASHMAP_FOREACH(s, nl->ignored_serials) { |
| 202 |
if (s->timeout_usec == USEC_INFINITY) |
| 203 |
continue; |
| 204 |
|
| 205 |
if (now_usec == 0) |
| 206 |
now_usec = netlink_now(nl, CLOCK_MONOTONIC); |
| 207 |
|
| 208 |
if (s->timeout_usec < now_usec) |
| 209 |
free(hashmap_remove(nl->ignored_serials, UINT32_TO_PTR(s->serial))); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
int sd_netlink_ignore_serial(sd_netlink *nl, uint32_t serial, uint64_t timeout_usec) { |
| 214 |
int r; |
| 215 |
|
| 216 |
assert_return(nl, -EINVAL); |
| 217 |
assert_return(!netlink_pid_changed(nl), -ECHILD); |
| 218 |
assert_return(serial != 0, -EINVAL); |
| 219 |
|
| 220 |
timeout_usec = timespan_to_timestamp(nl, timeout_usec); |
| 221 |
|
| 222 |
NetlinkIgnoredSerial *existing = hashmap_get(nl->ignored_serials, UINT32_TO_PTR(serial)); |
| 223 |
if (existing) { |
| 224 |
existing->timeout_usec = timeout_usec; |
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
netlink_trim_ignored_serials(nl); |
| 229 |
|
| 230 |
_cleanup_free_ NetlinkIgnoredSerial *s = new(NetlinkIgnoredSerial, 1); |
| 231 |
if (!s) |
| 232 |
return -ENOMEM; |
| 233 |
|
| 234 |
*s = (NetlinkIgnoredSerial) { |
| 235 |
.serial = serial, |
| 236 |
.timeout_usec = timeout_usec, |
| 237 |
}; |
| 238 |
|
| 239 |
r = hashmap_ensure_put(&nl->ignored_serials, &trivial_hash_ops_value_free, UINT32_TO_PTR(s->serial), s); |
| 240 |
if (r < 0) |
| 241 |
return r; |
| 242 |
|
| 243 |
TAKE_PTR(s); |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
int sd_netlink_send( |
| 248 |
sd_netlink *nl, |
| 249 |
sd_netlink_message *message, |
| 250 |
uint32_t *ret_serial) { |
| 251 |
|
| 252 |
int r; |
| 253 |
|
| 254 |
assert_return(nl, -EINVAL); |
| 255 |
assert_return(!netlink_pid_changed(nl), -ECHILD); |
| 256 |
assert_return(message, -EINVAL); |
| 257 |
assert_return(!message->sealed, -EPERM); |
| 258 |
|
| 259 |
netlink_seal_message(nl, message); |
| 260 |
|
| 261 |
r = socket_write_message(nl, message); |
| 262 |
if (r < 0) |
| 263 |
return r; |
| 264 |
|
| 265 |
if (ret_serial) |
| 266 |
*ret_serial = message_get_serial(message); |
| 267 |
|
| 268 |
return 1; |
| 269 |
} |
| 270 |
|
| 271 |
static int dispatch_rqueue(sd_netlink *nl, sd_netlink_message **ret) { |
| 272 |
sd_netlink_message *m; |
| 273 |
int r; |
| 274 |
|
| 275 |
assert(nl); |
| 276 |
assert(ret); |
| 277 |
|
| 278 |
if (ordered_set_isempty(nl->rqueue)) { |
| 279 |
/* Try to read a new message */ |
| 280 |
r = socket_read_message(nl); |
| 281 |
if (r == -ENOBUFS) /* FIXME: ignore buffer overruns for now */ |
| 282 |
log_debug_errno(r, "sd-netlink: Got ENOBUFS from netlink socket, ignoring."); |
| 283 |
else if (r < 0) |
| 284 |
return r; |
| 285 |
} |
| 286 |
|
| 287 |
/* Dispatch a queued message */ |
| 288 |
m = ordered_set_steal_first(nl->rqueue); |
| 289 |
if (m) |
| 290 |
sd_netlink_message_unref(hashmap_remove_value(nl->rqueue_by_serial, UINT32_TO_PTR(message_get_serial(m)), m)); |
| 291 |
*ret = m; |
| 292 |
return !!m; |
| 293 |
} |
| 294 |
|
| 295 |
static int process_timeout(sd_netlink *nl) { |
| 296 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 297 |
struct reply_callback *c; |
| 298 |
sd_netlink_slot *slot; |
| 299 |
int r; |
| 300 |
|
| 301 |
assert(nl); |
| 302 |
|
| 303 |
c = prioq_peek(nl->reply_callbacks_prioq); |
| 304 |
if (!c) |
| 305 |
return 0; |
| 306 |
|
| 307 |
if (c->timeout > netlink_now(nl, CLOCK_MONOTONIC)) |
| 308 |
return 0; |
| 309 |
|
| 310 |
r = message_new_synthetic_error(nl, -ETIMEDOUT, c->serial, &m); |
| 311 |
if (r < 0) |
| 312 |
return r; |
| 313 |
|
| 314 |
assert_se(prioq_pop(nl->reply_callbacks_prioq) == c); |
| 315 |
hashmap_remove(nl->reply_callbacks, UINT32_TO_PTR(c->serial)); |
| 316 |
|
| 317 |
slot = container_of(c, sd_netlink_slot, reply_callback); |
| 318 |
|
| 319 |
r = c->callback(nl, m, slot->userdata); |
| 320 |
if (r < 0) |
| 321 |
log_debug_errno(r, "sd-netlink: timedout callback %s%s%sfailed: %m", |
| 322 |
slot->description ? "'" : "", |
| 323 |
strempty(slot->description), |
| 324 |
slot->description ? "' " : ""); |
| 325 |
|
| 326 |
if (slot->floating) |
| 327 |
netlink_slot_disconnect(slot, true); |
| 328 |
|
| 329 |
return 1; |
| 330 |
} |
| 331 |
|
| 332 |
static int process_reply(sd_netlink *nl, sd_netlink_message *m) { |
| 333 |
struct reply_callback *c; |
| 334 |
uint32_t serial; |
| 335 |
uint16_t type; |
| 336 |
int r; |
| 337 |
|
| 338 |
assert(nl); |
| 339 |
assert(m); |
| 340 |
|
| 341 |
serial = message_get_serial(m); |
| 342 |
c = hashmap_remove(nl->reply_callbacks, UINT32_TO_PTR(serial)); |
| 343 |
if (!c) |
| 344 |
return 0; |
| 345 |
|
| 346 |
if (c->timeout != USEC_INFINITY) |
| 347 |
prioq_remove(nl->reply_callbacks_prioq, c, &c->prioq_idx); |
| 348 |
|
| 349 |
r = sd_netlink_message_get_type(m, &type); |
| 350 |
if (r < 0) |
| 351 |
return r; |
| 352 |
|
| 353 |
if (type == NLMSG_DONE) |
| 354 |
m = NULL; |
| 355 |
|
| 356 |
_cleanup_(sd_netlink_slot_unrefp) sd_netlink_slot *slot = |
| 357 |
sd_netlink_slot_ref(container_of(c, sd_netlink_slot, reply_callback)); |
| 358 |
|
| 359 |
r = c->callback(nl, m, slot->userdata); |
| 360 |
if (r < 0) |
| 361 |
log_debug_errno(r, "sd-netlink: reply callback %s%s%sfailed: %m", |
| 362 |
slot->description ? "'" : "", |
| 363 |
strempty(slot->description), |
| 364 |
slot->description ? "' " : ""); |
| 365 |
|
| 366 |
if (slot->floating) |
| 367 |
netlink_slot_disconnect(slot, true); |
| 368 |
|
| 369 |
return 1; |
| 370 |
} |
| 371 |
|
| 372 |
static int process_match(sd_netlink *nl, sd_netlink_message *m) { |
| 373 |
uint16_t type; |
| 374 |
uint8_t cmd; |
| 375 |
int r; |
| 376 |
|
| 377 |
assert(nl); |
| 378 |
assert(m); |
| 379 |
|
| 380 |
r = sd_netlink_message_get_type(m, &type); |
| 381 |
if (r < 0) |
| 382 |
return r; |
| 383 |
|
| 384 |
if (m->protocol == NETLINK_GENERIC) { |
| 385 |
r = sd_genl_message_get_command(nl, m, &cmd); |
| 386 |
if (r < 0) |
| 387 |
return r; |
| 388 |
} else |
| 389 |
cmd = 0; |
| 390 |
|
| 391 |
LIST_FOREACH(match_callbacks, c, nl->match_callbacks) { |
| 392 |
sd_netlink_slot *slot; |
| 393 |
bool found = false; |
| 394 |
|
| 395 |
if (c->type != type) |
| 396 |
continue; |
| 397 |
if (c->cmd != 0 && c->cmd != cmd) |
| 398 |
continue; |
| 399 |
|
| 400 |
for (size_t i = 0; i < c->n_groups; i++) |
| 401 |
if (c->groups[i] == m->multicast_group) { |
| 402 |
found = true; |
| 403 |
break; |
| 404 |
} |
| 405 |
|
| 406 |
if (!found) |
| 407 |
continue; |
| 408 |
|
| 409 |
slot = container_of(c, sd_netlink_slot, match_callback); |
| 410 |
|
| 411 |
r = c->callback(nl, m, slot->userdata); |
| 412 |
if (r < 0) |
| 413 |
log_debug_errno(r, "sd-netlink: match callback %s%s%sfailed: %m", |
| 414 |
slot->description ? "'" : "", |
| 415 |
strempty(slot->description), |
| 416 |
slot->description ? "' " : ""); |
| 417 |
if (r != 0) |
| 418 |
break; |
| 419 |
} |
| 420 |
|
| 421 |
return 1; |
| 422 |
} |
| 423 |
|
| 424 |
static int process_running(sd_netlink *nl, sd_netlink_message **ret) { |
| 425 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 426 |
int r; |
| 427 |
|
| 428 |
assert(nl); |
| 429 |
|
| 430 |
netlink_trim_ignored_serials(nl); |
| 431 |
|
| 432 |
r = process_timeout(nl); |
| 433 |
if (r != 0) |
| 434 |
goto null_message; |
| 435 |
|
| 436 |
r = dispatch_rqueue(nl, &m); |
| 437 |
if (r < 0) |
| 438 |
return r; |
| 439 |
if (!m) |
| 440 |
goto null_message; |
| 441 |
|
| 442 |
if (sd_netlink_message_is_broadcast(m)) |
| 443 |
r = process_match(nl, m); |
| 444 |
else |
| 445 |
r = process_reply(nl, m); |
| 446 |
if (r != 0) |
| 447 |
goto null_message; |
| 448 |
|
| 449 |
if (ret) { |
| 450 |
*ret = TAKE_PTR(m); |
| 451 |
|
| 452 |
return 1; |
| 453 |
} |
| 454 |
|
| 455 |
return 1; |
| 456 |
|
| 457 |
null_message: |
| 458 |
if (r >= 0 && ret) |
| 459 |
*ret = NULL; |
| 460 |
|
| 461 |
return r; |
| 462 |
} |
| 463 |
|
| 464 |
int sd_netlink_process(sd_netlink *nl, sd_netlink_message **ret) { |
| 465 |
NETLINK_DONT_DESTROY(nl); |
| 466 |
int r; |
| 467 |
|
| 468 |
assert_return(nl, -EINVAL); |
| 469 |
assert_return(!netlink_pid_changed(nl), -ECHILD); |
| 470 |
assert_return(!nl->processing, -EBUSY); |
| 471 |
|
| 472 |
nl->processing = true; |
| 473 |
r = process_running(nl, ret); |
| 474 |
nl->processing = false; |
| 475 |
|
| 476 |
return r; |
| 477 |
} |
| 478 |
|
| 479 |
static int netlink_poll(sd_netlink *nl, bool need_more, usec_t timeout_usec) { |
| 480 |
usec_t m = USEC_INFINITY; |
| 481 |
int r, e; |
| 482 |
|
| 483 |
assert(nl); |
| 484 |
|
| 485 |
e = sd_netlink_get_events(nl); |
| 486 |
if (e < 0) |
| 487 |
return e; |
| 488 |
|
| 489 |
if (need_more) |
| 490 |
/* Caller wants more data, and doesn't care about |
| 491 |
* what's been read or any other timeouts. */ |
| 492 |
e |= POLLIN; |
| 493 |
else { |
| 494 |
usec_t until; |
| 495 |
|
| 496 |
/* Caller wants to process if there is something to |
| 497 |
* process, but doesn't care otherwise */ |
| 498 |
|
| 499 |
r = sd_netlink_get_timeout(nl, &until); |
| 500 |
if (r < 0) |
| 501 |
return r; |
| 502 |
|
| 503 |
m = usec_sub_unsigned(until, netlink_now(nl, CLOCK_MONOTONIC)); |
| 504 |
} |
| 505 |
|
| 506 |
r = fd_wait_for_event(nl->fd, e, MIN(m, timeout_usec)); |
| 507 |
if (r <= 0) |
| 508 |
return r; |
| 509 |
|
| 510 |
return 1; |
| 511 |
} |
| 512 |
|
| 513 |
int sd_netlink_wait(sd_netlink *nl, uint64_t timeout_usec) { |
| 514 |
int r; |
| 515 |
|
| 516 |
assert_return(nl, -EINVAL); |
| 517 |
assert_return(!netlink_pid_changed(nl), -ECHILD); |
| 518 |
|
| 519 |
if (!ordered_set_isempty(nl->rqueue)) |
| 520 |
return 0; |
| 521 |
|
| 522 |
r = netlink_poll(nl, false, timeout_usec); |
| 523 |
if (ERRNO_IS_NEG_TRANSIENT(r)) /* Convert EINTR to "something happened" and give user a chance to run some code before calling back into us */ |
| 524 |
return 1; |
| 525 |
return r; |
| 526 |
} |
| 527 |
|
| 528 |
static int timeout_compare(const void *a, const void *b) { |
| 529 |
const struct reply_callback *x = a, *y = b; |
| 530 |
|
| 531 |
return CMP(x->timeout, y->timeout); |
| 532 |
} |
| 533 |
|
| 534 |
size_t netlink_get_reply_callback_count(sd_netlink *nl) { |
| 535 |
if (!nl) |
| 536 |
return 0; |
| 537 |
|
| 538 |
return hashmap_size(nl->reply_callbacks); |
| 539 |
} |
| 540 |
|
| 541 |
int sd_netlink_call_async( |
| 542 |
sd_netlink *nl, |
| 543 |
sd_netlink_slot **ret_slot, |
| 544 |
sd_netlink_message *m, |
| 545 |
sd_netlink_message_handler_t callback, |
| 546 |
sd_netlink_destroy_t destroy_callback, |
| 547 |
void *userdata, |
| 548 |
uint64_t usec, |
| 549 |
const char *description) { |
| 550 |
|
| 551 |
_cleanup_free_ sd_netlink_slot *slot = NULL; |
| 552 |
int r, k; |
| 553 |
|
| 554 |
assert_return(nl, -EINVAL); |
| 555 |
assert_return(m, -EINVAL); |
| 556 |
assert_return(callback, -EINVAL); |
| 557 |
assert_return(!netlink_pid_changed(nl), -ECHILD); |
| 558 |
|
| 559 |
if (hashmap_size(nl->reply_callbacks) >= REPLY_CALLBACKS_MAX) |
| 560 |
return -EXFULL; |
| 561 |
|
| 562 |
r = hashmap_ensure_allocated(&nl->reply_callbacks, &trivial_hash_ops); |
| 563 |
if (r < 0) |
| 564 |
return r; |
| 565 |
|
| 566 |
if (usec != UINT64_MAX) { |
| 567 |
r = prioq_ensure_allocated(&nl->reply_callbacks_prioq, timeout_compare); |
| 568 |
if (r < 0) |
| 569 |
return r; |
| 570 |
} |
| 571 |
|
| 572 |
r = netlink_slot_allocate(nl, !ret_slot, NETLINK_REPLY_CALLBACK, sizeof(struct reply_callback), userdata, description, &slot); |
| 573 |
if (r < 0) |
| 574 |
return r; |
| 575 |
|
| 576 |
slot->reply_callback.callback = callback; |
| 577 |
slot->reply_callback.timeout = timespan_to_timestamp(nl, usec); |
| 578 |
|
| 579 |
k = sd_netlink_send(nl, m, &slot->reply_callback.serial); |
| 580 |
if (k < 0) |
| 581 |
return k; |
| 582 |
|
| 583 |
r = hashmap_put(nl->reply_callbacks, UINT32_TO_PTR(slot->reply_callback.serial), &slot->reply_callback); |
| 584 |
if (r < 0) |
| 585 |
return r; |
| 586 |
|
| 587 |
if (slot->reply_callback.timeout != USEC_INFINITY) { |
| 588 |
r = prioq_put(nl->reply_callbacks_prioq, &slot->reply_callback, &slot->reply_callback.prioq_idx); |
| 589 |
if (r < 0) { |
| 590 |
(void) hashmap_remove(nl->reply_callbacks, UINT32_TO_PTR(slot->reply_callback.serial)); |
| 591 |
return r; |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
/* Set this at last. Otherwise, some failures in above would call destroy_callback but some would not. */ |
| 596 |
slot->destroy_callback = destroy_callback; |
| 597 |
|
| 598 |
if (ret_slot) |
| 599 |
*ret_slot = slot; |
| 600 |
|
| 601 |
TAKE_PTR(slot); |
| 602 |
|
| 603 |
return k; |
| 604 |
} |
| 605 |
|
| 606 |
int sd_netlink_read( |
| 607 |
sd_netlink *nl, |
| 608 |
uint32_t serial, |
| 609 |
uint64_t timeout, |
| 610 |
sd_netlink_message **ret) { |
| 611 |
|
| 612 |
usec_t usec; |
| 613 |
int r; |
| 614 |
|
| 615 |
assert_return(nl, -EINVAL); |
| 616 |
assert_return(!netlink_pid_changed(nl), -ECHILD); |
| 617 |
|
| 618 |
usec = timespan_to_timestamp(nl, timeout); |
| 619 |
|
| 620 |
for (;;) { |
| 621 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 622 |
usec_t left; |
| 623 |
|
| 624 |
m = hashmap_remove(nl->rqueue_by_serial, UINT32_TO_PTR(serial)); |
| 625 |
if (m) { |
| 626 |
uint16_t type; |
| 627 |
|
| 628 |
/* found a match, remove from rqueue and return it */ |
| 629 |
sd_netlink_message_unref(ordered_set_remove(nl->rqueue, m)); |
| 630 |
|
| 631 |
r = sd_netlink_message_get_errno(m); |
| 632 |
if (r < 0) |
| 633 |
return r; |
| 634 |
|
| 635 |
r = sd_netlink_message_get_type(m, &type); |
| 636 |
if (r < 0) |
| 637 |
return r; |
| 638 |
|
| 639 |
if (type == NLMSG_DONE) { |
| 640 |
if (ret) |
| 641 |
*ret = NULL; |
| 642 |
return 0; |
| 643 |
} |
| 644 |
|
| 645 |
if (ret) |
| 646 |
*ret = TAKE_PTR(m); |
| 647 |
return 1; |
| 648 |
} |
| 649 |
|
| 650 |
r = socket_read_message(nl); |
| 651 |
if (r < 0) |
| 652 |
return r; |
| 653 |
if (r > 0) |
| 654 |
/* received message, so try to process straight away */ |
| 655 |
continue; |
| 656 |
|
| 657 |
if (usec != USEC_INFINITY) { |
| 658 |
usec_t n; |
| 659 |
|
| 660 |
n = netlink_now(nl, CLOCK_MONOTONIC); |
| 661 |
if (n >= usec) |
| 662 |
return -ETIMEDOUT; |
| 663 |
|
| 664 |
left = usec_sub_unsigned(usec, n); |
| 665 |
} else |
| 666 |
left = USEC_INFINITY; |
| 667 |
|
| 668 |
r = netlink_poll(nl, true, left); |
| 669 |
if (r < 0) |
| 670 |
return r; |
| 671 |
if (r == 0) |
| 672 |
return -ETIMEDOUT; |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
int sd_netlink_call( |
| 677 |
sd_netlink *nl, |
| 678 |
sd_netlink_message *message, |
| 679 |
uint64_t timeout, |
| 680 |
sd_netlink_message **ret) { |
| 681 |
|
| 682 |
uint32_t serial; |
| 683 |
int r; |
| 684 |
|
| 685 |
assert_return(nl, -EINVAL); |
| 686 |
assert_return(!netlink_pid_changed(nl), -ECHILD); |
| 687 |
assert_return(message, -EINVAL); |
| 688 |
|
| 689 |
r = sd_netlink_send(nl, message, &serial); |
| 690 |
if (r < 0) |
| 691 |
return r; |
| 692 |
|
| 693 |
return sd_netlink_read(nl, serial, timeout, ret); |
| 694 |
} |
| 695 |
|
| 696 |
int sd_netlink_get_events(sd_netlink *nl) { |
| 697 |
assert_return(nl, -EINVAL); |
| 698 |
assert_return(!netlink_pid_changed(nl), -ECHILD); |
| 699 |
|
| 700 |
return ordered_set_isempty(nl->rqueue) ? POLLIN : 0; |
| 701 |
} |
| 702 |
|
| 703 |
int sd_netlink_get_timeout(sd_netlink *nl, uint64_t *ret) { |
| 704 |
struct reply_callback *c; |
| 705 |
|
| 706 |
assert_return(nl, -EINVAL); |
| 707 |
assert_return(ret, -EINVAL); |
| 708 |
assert_return(!netlink_pid_changed(nl), -ECHILD); |
| 709 |
|
| 710 |
if (!ordered_set_isempty(nl->rqueue)) { |
| 711 |
*ret = 0; |
| 712 |
return 1; |
| 713 |
} |
| 714 |
|
| 715 |
c = prioq_peek(nl->reply_callbacks_prioq); |
| 716 |
if (!c) { |
| 717 |
*ret = UINT64_MAX; |
| 718 |
return 0; |
| 719 |
} |
| 720 |
|
| 721 |
*ret = c->timeout; |
| 722 |
return 1; |
| 723 |
} |
| 724 |
|
| 725 |
static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 726 |
sd_netlink *nl = ASSERT_PTR(userdata); |
| 727 |
int r; |
| 728 |
|
| 729 |
r = sd_netlink_process(nl, NULL); |
| 730 |
if (r < 0) |
| 731 |
return r; |
| 732 |
|
| 733 |
return 1; |
| 734 |
} |
| 735 |
|
| 736 |
static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) { |
| 737 |
sd_netlink *nl = ASSERT_PTR(userdata); |
| 738 |
int r; |
| 739 |
|
| 740 |
r = sd_netlink_process(nl, NULL); |
| 741 |
if (r < 0) |
| 742 |
return r; |
| 743 |
|
| 744 |
return 1; |
| 745 |
} |
| 746 |
|
| 747 |
static int prepare_callback(sd_event_source *s, void *userdata) { |
| 748 |
sd_netlink *nl = ASSERT_PTR(userdata); |
| 749 |
int r, enabled; |
| 750 |
usec_t until; |
| 751 |
|
| 752 |
assert(s); |
| 753 |
|
| 754 |
r = sd_netlink_get_events(nl); |
| 755 |
if (r < 0) |
| 756 |
return r; |
| 757 |
|
| 758 |
r = sd_event_source_set_io_events(nl->io_event_source, r); |
| 759 |
if (r < 0) |
| 760 |
return r; |
| 761 |
|
| 762 |
enabled = sd_netlink_get_timeout(nl, &until); |
| 763 |
if (enabled < 0) |
| 764 |
return enabled; |
| 765 |
if (enabled > 0) { |
| 766 |
r = sd_event_source_set_time(nl->time_event_source, until); |
| 767 |
if (r < 0) |
| 768 |
return r; |
| 769 |
} |
| 770 |
|
| 771 |
r = sd_event_source_set_enabled(nl->time_event_source, |
| 772 |
enabled > 0 ? SD_EVENT_ONESHOT : SD_EVENT_OFF); |
| 773 |
if (r < 0) |
| 774 |
return r; |
| 775 |
|
| 776 |
return 1; |
| 777 |
} |
| 778 |
|
| 779 |
int sd_netlink_attach_event(sd_netlink *nl, sd_event *event, int64_t priority) { |
| 780 |
int r; |
| 781 |
|
| 782 |
assert_return(nl, -EINVAL); |
| 783 |
assert_return(!nl->event, -EBUSY); |
| 784 |
|
| 785 |
assert(!nl->io_event_source); |
| 786 |
assert(!nl->time_event_source); |
| 787 |
|
| 788 |
if (event) |
| 789 |
nl->event = sd_event_ref(event); |
| 790 |
else { |
| 791 |
r = sd_event_default(&nl->event); |
| 792 |
if (r < 0) |
| 793 |
return r; |
| 794 |
} |
| 795 |
|
| 796 |
r = sd_event_add_io(nl->event, &nl->io_event_source, nl->fd, 0, io_callback, nl); |
| 797 |
if (r < 0) |
| 798 |
goto fail; |
| 799 |
|
| 800 |
r = sd_event_source_set_priority(nl->io_event_source, priority); |
| 801 |
if (r < 0) |
| 802 |
goto fail; |
| 803 |
|
| 804 |
r = sd_event_source_set_description(nl->io_event_source, "netlink-receive-message"); |
| 805 |
if (r < 0) |
| 806 |
goto fail; |
| 807 |
|
| 808 |
r = sd_event_source_set_prepare(nl->io_event_source, prepare_callback); |
| 809 |
if (r < 0) |
| 810 |
goto fail; |
| 811 |
|
| 812 |
r = sd_event_add_time(nl->event, &nl->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, nl); |
| 813 |
if (r < 0) |
| 814 |
goto fail; |
| 815 |
|
| 816 |
r = sd_event_source_set_priority(nl->time_event_source, priority); |
| 817 |
if (r < 0) |
| 818 |
goto fail; |
| 819 |
|
| 820 |
r = sd_event_source_set_description(nl->time_event_source, "netlink-timer"); |
| 821 |
if (r < 0) |
| 822 |
goto fail; |
| 823 |
|
| 824 |
return 0; |
| 825 |
|
| 826 |
fail: |
| 827 |
sd_netlink_detach_event(nl); |
| 828 |
return r; |
| 829 |
} |
| 830 |
|
| 831 |
int sd_netlink_detach_event(sd_netlink *nl) { |
| 832 |
assert_return(nl, -EINVAL); |
| 833 |
assert_return(nl->event, -ENXIO); |
| 834 |
|
| 835 |
nl->io_event_source = sd_event_source_unref(nl->io_event_source); |
| 836 |
|
| 837 |
nl->time_event_source = sd_event_source_unref(nl->time_event_source); |
| 838 |
|
| 839 |
nl->event = sd_event_unref(nl->event); |
| 840 |
|
| 841 |
return 0; |
| 842 |
} |
| 843 |
|
| 844 |
sd_event* sd_netlink_get_event(sd_netlink *nl) { |
| 845 |
assert_return(nl, NULL); |
| 846 |
|
| 847 |
return nl->event; |
| 848 |
} |
| 849 |
|
| 850 |
int netlink_add_match_internal( |
| 851 |
sd_netlink *nl, |
| 852 |
sd_netlink_slot **ret_slot, |
| 853 |
const uint32_t *groups, |
| 854 |
size_t n_groups, |
| 855 |
uint16_t type, |
| 856 |
uint8_t cmd, |
| 857 |
sd_netlink_message_handler_t callback, |
| 858 |
sd_netlink_destroy_t destroy_callback, |
| 859 |
void *userdata, |
| 860 |
const char *description) { |
| 861 |
|
| 862 |
_cleanup_free_ sd_netlink_slot *slot = NULL; |
| 863 |
int r; |
| 864 |
|
| 865 |
assert(groups); |
| 866 |
assert(n_groups > 0); |
| 867 |
|
| 868 |
for (size_t i = 0; i < n_groups; i++) { |
| 869 |
r = socket_broadcast_group_ref(nl, groups[i]); |
| 870 |
if (r < 0) |
| 871 |
return r; |
| 872 |
} |
| 873 |
|
| 874 |
r = netlink_slot_allocate(nl, !ret_slot, NETLINK_MATCH_CALLBACK, sizeof(struct match_callback), |
| 875 |
userdata, description, &slot); |
| 876 |
if (r < 0) |
| 877 |
return r; |
| 878 |
|
| 879 |
slot->match_callback.groups = newdup(uint32_t, groups, n_groups); |
| 880 |
if (!slot->match_callback.groups) |
| 881 |
return -ENOMEM; |
| 882 |
|
| 883 |
slot->match_callback.n_groups = n_groups; |
| 884 |
slot->match_callback.callback = callback; |
| 885 |
slot->match_callback.type = type; |
| 886 |
slot->match_callback.cmd = cmd; |
| 887 |
|
| 888 |
LIST_PREPEND(match_callbacks, nl->match_callbacks, &slot->match_callback); |
| 889 |
|
| 890 |
/* Set this at last. Otherwise, some failures in above call the destroy callback but some do not. */ |
| 891 |
slot->destroy_callback = destroy_callback; |
| 892 |
|
| 893 |
if (ret_slot) |
| 894 |
*ret_slot = slot; |
| 895 |
|
| 896 |
TAKE_PTR(slot); |
| 897 |
return 0; |
| 898 |
} |
| 899 |
|
| 900 |
int sd_netlink_add_match( |
| 901 |
sd_netlink *rtnl, |
| 902 |
sd_netlink_slot **ret_slot, |
| 903 |
uint16_t type, |
| 904 |
sd_netlink_message_handler_t callback, |
| 905 |
sd_netlink_destroy_t destroy_callback, |
| 906 |
void *userdata, |
| 907 |
const char *description) { |
| 908 |
|
| 909 |
static const uint32_t |
| 910 |
address_groups[] = { RTNLGRP_IPV4_IFADDR, RTNLGRP_IPV6_IFADDR, }, |
| 911 |
link_groups[] = { RTNLGRP_LINK, }, |
| 912 |
neighbor_groups[] = { RTNLGRP_NEIGH, }, |
| 913 |
nexthop_groups[] = { RTNLGRP_NEXTHOP, }, |
| 914 |
route_groups[] = { RTNLGRP_IPV4_ROUTE, RTNLGRP_IPV6_ROUTE, }, |
| 915 |
rule_groups[] = { RTNLGRP_IPV4_RULE, RTNLGRP_IPV6_RULE, }, |
| 916 |
tc_groups[] = { RTNLGRP_TC }; |
| 917 |
const uint32_t *groups; |
| 918 |
size_t n_groups; |
| 919 |
|
| 920 |
assert_return(rtnl, -EINVAL); |
| 921 |
assert_return(callback, -EINVAL); |
| 922 |
assert_return(!netlink_pid_changed(rtnl), -ECHILD); |
| 923 |
|
| 924 |
switch (type) { |
| 925 |
case RTM_NEWLINK: |
| 926 |
case RTM_DELLINK: |
| 927 |
groups = link_groups; |
| 928 |
n_groups = ELEMENTSOF(link_groups); |
| 929 |
break; |
| 930 |
case RTM_NEWADDR: |
| 931 |
case RTM_DELADDR: |
| 932 |
groups = address_groups; |
| 933 |
n_groups = ELEMENTSOF(address_groups); |
| 934 |
break; |
| 935 |
case RTM_NEWNEIGH: |
| 936 |
case RTM_DELNEIGH: |
| 937 |
groups = neighbor_groups; |
| 938 |
n_groups = ELEMENTSOF(neighbor_groups); |
| 939 |
break; |
| 940 |
case RTM_NEWROUTE: |
| 941 |
case RTM_DELROUTE: |
| 942 |
groups = route_groups; |
| 943 |
n_groups = ELEMENTSOF(route_groups); |
| 944 |
break; |
| 945 |
case RTM_NEWRULE: |
| 946 |
case RTM_DELRULE: |
| 947 |
groups = rule_groups; |
| 948 |
n_groups = ELEMENTSOF(rule_groups); |
| 949 |
break; |
| 950 |
case RTM_NEWNEXTHOP: |
| 951 |
case RTM_DELNEXTHOP: |
| 952 |
groups = nexthop_groups; |
| 953 |
n_groups = ELEMENTSOF(nexthop_groups); |
| 954 |
break; |
| 955 |
case RTM_NEWQDISC: |
| 956 |
case RTM_DELQDISC: |
| 957 |
case RTM_NEWTCLASS: |
| 958 |
case RTM_DELTCLASS: |
| 959 |
groups = tc_groups; |
| 960 |
n_groups = ELEMENTSOF(tc_groups); |
| 961 |
break; |
| 962 |
default: |
| 963 |
return -EOPNOTSUPP; |
| 964 |
} |
| 965 |
|
| 966 |
return netlink_add_match_internal(rtnl, ret_slot, groups, n_groups, type, 0, callback, |
| 967 |
destroy_callback, userdata, description); |
| 968 |
} |
| 969 |
|
| 970 |
int sd_netlink_attach_filter(sd_netlink *nl, size_t len, const struct sock_filter *filter) { |
| 971 |
assert_return(nl, -EINVAL); |
| 972 |
assert_return(len == 0 || filter, -EINVAL); |
| 973 |
|
| 974 |
if (setsockopt(nl->fd, SOL_SOCKET, |
| 975 |
len == 0 ? SO_DETACH_FILTER : SO_ATTACH_FILTER, |
| 976 |
&(struct sock_fprog) { |
| 977 |
.len = len, |
| 978 |
.filter = (struct sock_filter*) filter, |
| 979 |
}, sizeof(struct sock_fprog)) < 0) |
| 980 |
return -errno; |
| 981 |
|
| 982 |
return 0; |
| 983 |
} |