| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-netlink.h" |
| 4 |
|
| 5 |
#include "ether-addr-util.h" |
| 6 |
#include "fd-util.h" |
| 7 |
#include "hashmap.h" |
| 8 |
#include "iovec-util.h" |
| 9 |
#include "log.h" |
| 10 |
#include "memory-util.h" |
| 11 |
#include "netlink-internal.h" |
| 12 |
#include "netlink-util.h" |
| 13 |
#include "parse-util.h" |
| 14 |
#include "process-util.h" |
| 15 |
#include "socket-util.h" |
| 16 |
#include "string-util.h" |
| 17 |
#include "strv.h" |
| 18 |
|
| 19 |
static int parse_newlink_message( |
| 20 |
sd_netlink_message *message, |
| 21 |
char **ret_name, |
| 22 |
char ***ret_altnames) { |
| 23 |
|
| 24 |
_cleanup_strv_free_ char **altnames = NULL; |
| 25 |
int r, ifindex; |
| 26 |
|
| 27 |
assert(message); |
| 28 |
|
| 29 |
uint16_t type; |
| 30 |
r = sd_netlink_message_get_type(message, &type); |
| 31 |
if (r < 0) |
| 32 |
return r; |
| 33 |
if (type != RTM_NEWLINK) |
| 34 |
return -EPROTO; |
| 35 |
|
| 36 |
r = sd_rtnl_message_link_get_ifindex(message, &ifindex); |
| 37 |
if (r < 0) |
| 38 |
return r; |
| 39 |
if (ifindex <= 0) |
| 40 |
return -EPROTO; |
| 41 |
|
| 42 |
if (ret_altnames) { |
| 43 |
r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames); |
| 44 |
if (r < 0 && r != -ENODATA) |
| 45 |
return r; |
| 46 |
} |
| 47 |
|
| 48 |
if (ret_name) { |
| 49 |
r = sd_netlink_message_read_string_strdup(message, IFLA_IFNAME, ret_name); |
| 50 |
if (r < 0) |
| 51 |
return r; |
| 52 |
} |
| 53 |
|
| 54 |
if (ret_altnames) |
| 55 |
*ret_altnames = TAKE_PTR(altnames); |
| 56 |
|
| 57 |
return ifindex; |
| 58 |
} |
| 59 |
|
| 60 |
int rtnl_resolve_ifname_full( |
| 61 |
sd_netlink **rtnl, |
| 62 |
ResolveInterfaceNameFlag flags, |
| 63 |
const char *name, |
| 64 |
char **ret_name, |
| 65 |
char ***ret_altnames) { |
| 66 |
|
| 67 |
_cleanup_(sd_netlink_unrefp) sd_netlink *our_rtnl = NULL; |
| 68 |
int r; |
| 69 |
|
| 70 |
assert(name); |
| 71 |
assert(flags > 0); |
| 72 |
|
| 73 |
/* This is similar to if_nametoindex(), but also resolves alternative names and decimal formatted |
| 74 |
* ifindex too. Returns ifindex, and optionally provides the main interface name and alternative |
| 75 |
* names. */ |
| 76 |
|
| 77 |
if (!rtnl) |
| 78 |
rtnl = &our_rtnl; |
| 79 |
if (!*rtnl) { |
| 80 |
r = sd_netlink_open(rtnl); |
| 81 |
if (r < 0) |
| 82 |
return r; |
| 83 |
} |
| 84 |
|
| 85 |
/* First, use IFLA_IFNAME */ |
| 86 |
if (FLAGS_SET(flags, RESOLVE_IFNAME_MAIN) && ifname_valid(name)) { |
| 87 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL, *reply = NULL; |
| 88 |
|
| 89 |
r = sd_rtnl_message_new_link(*rtnl, &message, RTM_GETLINK, 0); |
| 90 |
if (r < 0) |
| 91 |
return r; |
| 92 |
|
| 93 |
r = sd_netlink_message_append_string(message, IFLA_IFNAME, name); |
| 94 |
if (r < 0) |
| 95 |
return r; |
| 96 |
|
| 97 |
r = sd_netlink_call(*rtnl, message, 0, &reply); |
| 98 |
if (r >= 0) |
| 99 |
return parse_newlink_message(reply, ret_name, ret_altnames); |
| 100 |
if (r != -ENODEV) |
| 101 |
return r; |
| 102 |
} |
| 103 |
|
| 104 |
/* Next, try IFLA_ALT_IFNAME */ |
| 105 |
if (FLAGS_SET(flags, RESOLVE_IFNAME_ALTERNATIVE) && |
| 106 |
ifname_valid_full(name, IFNAME_VALID_ALTERNATIVE)) { |
| 107 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL, *reply = NULL; |
| 108 |
|
| 109 |
r = sd_rtnl_message_new_link(*rtnl, &message, RTM_GETLINK, 0); |
| 110 |
if (r < 0) |
| 111 |
return r; |
| 112 |
|
| 113 |
r = sd_netlink_message_append_string(message, IFLA_ALT_IFNAME, name); |
| 114 |
if (r < 0) |
| 115 |
return r; |
| 116 |
|
| 117 |
r = sd_netlink_call(*rtnl, message, 0, &reply); |
| 118 |
if (r >= 0) |
| 119 |
return parse_newlink_message(reply, ret_name, ret_altnames); |
| 120 |
if (r != -ENODEV) |
| 121 |
return r; |
| 122 |
} |
| 123 |
|
| 124 |
/* Finally, assume the string is a decimal formatted ifindex. */ |
| 125 |
if (FLAGS_SET(flags, RESOLVE_IFNAME_NUMERIC)) { |
| 126 |
int ifindex; |
| 127 |
|
| 128 |
ifindex = parse_ifindex(name); |
| 129 |
if (ifindex <= 0) |
| 130 |
return -ENODEV; |
| 131 |
|
| 132 |
return rtnl_get_ifname_full(rtnl, ifindex, ret_name, ret_altnames); |
| 133 |
} |
| 134 |
|
| 135 |
return -ENODEV; |
| 136 |
} |
| 137 |
|
| 138 |
int rtnl_resolve_interface_or_warn(sd_netlink **rtnl, const char *name) { |
| 139 |
int r; |
| 140 |
|
| 141 |
assert(name); |
| 142 |
|
| 143 |
r = rtnl_resolve_interface(rtnl, name); |
| 144 |
if (r < 0) |
| 145 |
return log_error_errno(r, "Failed to resolve interface \"%s\": %m", name); |
| 146 |
return r; |
| 147 |
} |
| 148 |
|
| 149 |
static int set_link_name(sd_netlink *rtnl, int ifindex, const char *name) { |
| 150 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL; |
| 151 |
int r; |
| 152 |
|
| 153 |
assert(rtnl); |
| 154 |
assert(ifindex > 0); |
| 155 |
assert(name); |
| 156 |
|
| 157 |
/* Assign the requested name. */ |
| 158 |
|
| 159 |
r = sd_rtnl_message_new_link(rtnl, &message, RTM_SETLINK, ifindex); |
| 160 |
if (r < 0) |
| 161 |
return r; |
| 162 |
|
| 163 |
r = sd_netlink_message_append_string(message, IFLA_IFNAME, name); |
| 164 |
if (r < 0) |
| 165 |
return r; |
| 166 |
|
| 167 |
return sd_netlink_call(rtnl, message, 0, NULL); |
| 168 |
} |
| 169 |
|
| 170 |
int rtnl_rename_link(sd_netlink **rtnl, const char *orig_name, const char *new_name) { |
| 171 |
_cleanup_(sd_netlink_unrefp) sd_netlink *our_rtnl = NULL; |
| 172 |
int r, ifindex; |
| 173 |
|
| 174 |
assert(orig_name); |
| 175 |
assert(new_name); |
| 176 |
|
| 177 |
/* This does not check alternative names. Callers must check the requested name is not used as an |
| 178 |
* alternative name. */ |
| 179 |
|
| 180 |
if (streq(orig_name, new_name)) |
| 181 |
return 0; |
| 182 |
|
| 183 |
if (!ifname_valid(new_name)) |
| 184 |
return -EINVAL; |
| 185 |
|
| 186 |
if (!rtnl) |
| 187 |
rtnl = &our_rtnl; |
| 188 |
if (!*rtnl) { |
| 189 |
r = sd_netlink_open(rtnl); |
| 190 |
if (r < 0) |
| 191 |
return r; |
| 192 |
} |
| 193 |
|
| 194 |
ifindex = rtnl_resolve_ifname(rtnl, orig_name); |
| 195 |
if (ifindex < 0) |
| 196 |
return ifindex; |
| 197 |
|
| 198 |
return set_link_name(*rtnl, ifindex, new_name); |
| 199 |
} |
| 200 |
|
| 201 |
int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name, char* const *alternative_names) { |
| 202 |
_cleanup_strv_free_ char **original_altnames = NULL, **new_altnames = NULL; |
| 203 |
bool altname_deleted = false; |
| 204 |
int r; |
| 205 |
|
| 206 |
assert(ifindex > 0); |
| 207 |
|
| 208 |
if (isempty(name) && strv_isempty(alternative_names)) |
| 209 |
return 0; |
| 210 |
|
| 211 |
if (name && !ifname_valid(name)) |
| 212 |
return -EINVAL; |
| 213 |
|
| 214 |
_cleanup_(sd_netlink_unrefp) sd_netlink *our_rtnl = NULL; |
| 215 |
if (!rtnl) |
| 216 |
rtnl = &our_rtnl; |
| 217 |
|
| 218 |
/* If the requested name is already assigned as an alternative name, then first drop it. */ |
| 219 |
r = rtnl_get_link_alternative_names(rtnl, ifindex, &original_altnames); |
| 220 |
if (r < 0) |
| 221 |
log_debug_errno(r, "Failed to get alternative names on network interface %i, ignoring: %m", |
| 222 |
ifindex); |
| 223 |
|
| 224 |
if (name) { |
| 225 |
if (strv_contains(original_altnames, name)) { |
| 226 |
r = rtnl_delete_link_alternative_names(rtnl, ifindex, STRV_MAKE(name)); |
| 227 |
if (r < 0) |
| 228 |
return log_debug_errno(r, "Failed to remove '%s' from alternative names on network interface %i: %m", |
| 229 |
name, ifindex); |
| 230 |
|
| 231 |
altname_deleted = true; |
| 232 |
} |
| 233 |
|
| 234 |
r = set_link_name(*rtnl, ifindex, name); |
| 235 |
if (r < 0) |
| 236 |
goto fail; |
| 237 |
} |
| 238 |
|
| 239 |
/* Filter out already assigned names from requested alternative names. Also, dedup the request. */ |
| 240 |
STRV_FOREACH(a, alternative_names) { |
| 241 |
if (streq_ptr(name, *a)) |
| 242 |
continue; |
| 243 |
|
| 244 |
if (strv_contains(original_altnames, *a)) |
| 245 |
continue; |
| 246 |
|
| 247 |
if (strv_contains(new_altnames, *a)) |
| 248 |
continue; |
| 249 |
|
| 250 |
if (!ifname_valid_full(*a, IFNAME_VALID_ALTERNATIVE)) |
| 251 |
continue; |
| 252 |
|
| 253 |
r = strv_extend(&new_altnames, *a); |
| 254 |
if (r < 0) |
| 255 |
return r; |
| 256 |
} |
| 257 |
|
| 258 |
strv_sort(new_altnames); |
| 259 |
|
| 260 |
/* Finally, assign alternative names. */ |
| 261 |
r = rtnl_set_link_alternative_names(rtnl, ifindex, new_altnames); |
| 262 |
if (r == -EEXIST) /* Already assigned to another interface? */ |
| 263 |
STRV_FOREACH(a, new_altnames) { |
| 264 |
r = rtnl_set_link_alternative_names(rtnl, ifindex, STRV_MAKE(*a)); |
| 265 |
if (r < 0) |
| 266 |
log_debug_errno(r, "Failed to assign '%s' as an alternative name on network interface %i, ignoring: %m", |
| 267 |
*a, ifindex); |
| 268 |
} |
| 269 |
else if (r < 0) |
| 270 |
log_debug_errno(r, "Failed to assign alternative names on network interface %i, ignoring: %m", ifindex); |
| 271 |
|
| 272 |
return 0; |
| 273 |
|
| 274 |
fail: |
| 275 |
if (altname_deleted) { |
| 276 |
int q = rtnl_set_link_alternative_names(rtnl, ifindex, STRV_MAKE(name)); |
| 277 |
if (q < 0) |
| 278 |
log_debug_errno(q, "Failed to restore '%s' as an alternative name on network interface %i, ignoring: %m", |
| 279 |
name, ifindex); |
| 280 |
} |
| 281 |
|
| 282 |
return r; |
| 283 |
} |
| 284 |
|
| 285 |
int rtnl_set_link_properties( |
| 286 |
sd_netlink **rtnl, |
| 287 |
int ifindex, |
| 288 |
const char *alias, |
| 289 |
const struct hw_addr_data *hw_addr, |
| 290 |
uint32_t txqueues, |
| 291 |
uint32_t rxqueues, |
| 292 |
uint32_t txqueuelen, |
| 293 |
uint32_t mtu, |
| 294 |
uint32_t gso_max_size, |
| 295 |
size_t gso_max_segments) { |
| 296 |
|
| 297 |
int r; |
| 298 |
|
| 299 |
assert(ifindex > 0); |
| 300 |
|
| 301 |
if (!alias && |
| 302 |
(!hw_addr || hw_addr->length == 0) && |
| 303 |
txqueues == 0 && |
| 304 |
rxqueues == 0 && |
| 305 |
txqueuelen == UINT32_MAX && |
| 306 |
mtu == 0 && |
| 307 |
gso_max_size == 0 && |
| 308 |
gso_max_segments == 0) |
| 309 |
return 0; |
| 310 |
|
| 311 |
_cleanup_(sd_netlink_unrefp) sd_netlink *our_rtnl = NULL; |
| 312 |
if (!rtnl) |
| 313 |
rtnl = &our_rtnl; |
| 314 |
if (!*rtnl) { |
| 315 |
r = sd_netlink_open(rtnl); |
| 316 |
if (r < 0) |
| 317 |
return r; |
| 318 |
} |
| 319 |
|
| 320 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL; |
| 321 |
r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex); |
| 322 |
if (r < 0) |
| 323 |
return r; |
| 324 |
|
| 325 |
if (alias) { |
| 326 |
r = sd_netlink_message_append_string(message, IFLA_IFALIAS, alias); |
| 327 |
if (r < 0) |
| 328 |
return r; |
| 329 |
} |
| 330 |
|
| 331 |
if (hw_addr && hw_addr->length > 0) { |
| 332 |
r = netlink_message_append_hw_addr(message, IFLA_ADDRESS, hw_addr); |
| 333 |
if (r < 0) |
| 334 |
return r; |
| 335 |
} |
| 336 |
|
| 337 |
if (txqueues > 0) { |
| 338 |
r = sd_netlink_message_append_u32(message, IFLA_NUM_TX_QUEUES, txqueues); |
| 339 |
if (r < 0) |
| 340 |
return r; |
| 341 |
} |
| 342 |
|
| 343 |
if (rxqueues > 0) { |
| 344 |
r = sd_netlink_message_append_u32(message, IFLA_NUM_RX_QUEUES, rxqueues); |
| 345 |
if (r < 0) |
| 346 |
return r; |
| 347 |
} |
| 348 |
|
| 349 |
if (txqueuelen < UINT32_MAX) { |
| 350 |
r = sd_netlink_message_append_u32(message, IFLA_TXQLEN, txqueuelen); |
| 351 |
if (r < 0) |
| 352 |
return r; |
| 353 |
} |
| 354 |
|
| 355 |
if (mtu != 0) { |
| 356 |
r = sd_netlink_message_append_u32(message, IFLA_MTU, mtu); |
| 357 |
if (r < 0) |
| 358 |
return r; |
| 359 |
} |
| 360 |
|
| 361 |
if (gso_max_size > 0) { |
| 362 |
r = sd_netlink_message_append_u32(message, IFLA_GSO_MAX_SIZE, gso_max_size); |
| 363 |
if (r < 0) |
| 364 |
return r; |
| 365 |
} |
| 366 |
|
| 367 |
if (gso_max_segments > 0) { |
| 368 |
r = sd_netlink_message_append_u32(message, IFLA_GSO_MAX_SEGS, gso_max_segments); |
| 369 |
if (r < 0) |
| 370 |
return r; |
| 371 |
} |
| 372 |
|
| 373 |
r = sd_netlink_call(*rtnl, message, 0, NULL); |
| 374 |
if (r < 0) |
| 375 |
return r; |
| 376 |
|
| 377 |
return 0; |
| 378 |
} |
| 379 |
|
| 380 |
static int rtnl_update_link_alternative_names( |
| 381 |
sd_netlink **rtnl, |
| 382 |
uint16_t nlmsg_type, |
| 383 |
int ifindex, |
| 384 |
char* const *alternative_names) { |
| 385 |
|
| 386 |
int r; |
| 387 |
|
| 388 |
assert(ifindex > 0); |
| 389 |
assert(IN_SET(nlmsg_type, RTM_NEWLINKPROP, RTM_DELLINKPROP)); |
| 390 |
|
| 391 |
if (strv_isempty(alternative_names)) |
| 392 |
return 0; |
| 393 |
|
| 394 |
_cleanup_(sd_netlink_unrefp) sd_netlink *our_rtnl = NULL; |
| 395 |
if (!rtnl) |
| 396 |
rtnl = &our_rtnl; |
| 397 |
if (!*rtnl) { |
| 398 |
r = sd_netlink_open(rtnl); |
| 399 |
if (r < 0) |
| 400 |
return r; |
| 401 |
} |
| 402 |
|
| 403 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL; |
| 404 |
r = sd_rtnl_message_new_link(*rtnl, &message, nlmsg_type, ifindex); |
| 405 |
if (r < 0) |
| 406 |
return r; |
| 407 |
|
| 408 |
r = sd_netlink_message_open_container(message, IFLA_PROP_LIST); |
| 409 |
if (r < 0) |
| 410 |
return r; |
| 411 |
|
| 412 |
r = sd_netlink_message_append_strv(message, IFLA_ALT_IFNAME, (const char**) alternative_names); |
| 413 |
if (r < 0) |
| 414 |
return r; |
| 415 |
|
| 416 |
r = sd_netlink_message_close_container(message); |
| 417 |
if (r < 0) |
| 418 |
return r; |
| 419 |
|
| 420 |
r = sd_netlink_call(*rtnl, message, 0, NULL); |
| 421 |
if (r < 0) |
| 422 |
return r; |
| 423 |
|
| 424 |
return 0; |
| 425 |
} |
| 426 |
|
| 427 |
int rtnl_set_link_alternative_names(sd_netlink **rtnl, int ifindex, char* const *alternative_names) { |
| 428 |
return rtnl_update_link_alternative_names(rtnl, RTM_NEWLINKPROP, ifindex, alternative_names); |
| 429 |
} |
| 430 |
|
| 431 |
int rtnl_delete_link_alternative_names(sd_netlink **rtnl, int ifindex, char* const *alternative_names) { |
| 432 |
return rtnl_update_link_alternative_names(rtnl, RTM_DELLINKPROP, ifindex, alternative_names); |
| 433 |
} |
| 434 |
|
| 435 |
int rtnl_set_link_alternative_names_by_ifname( |
| 436 |
sd_netlink **rtnl, |
| 437 |
const char *ifname, |
| 438 |
char* const *alternative_names) { |
| 439 |
|
| 440 |
int r; |
| 441 |
|
| 442 |
assert(rtnl); |
| 443 |
assert(ifname); |
| 444 |
|
| 445 |
if (strv_isempty(alternative_names)) |
| 446 |
return 0; |
| 447 |
|
| 448 |
_cleanup_(sd_netlink_unrefp) sd_netlink *our_rtnl = NULL; |
| 449 |
if (!rtnl) |
| 450 |
rtnl = &our_rtnl; |
| 451 |
if (!*rtnl) { |
| 452 |
r = sd_netlink_open(rtnl); |
| 453 |
if (r < 0) |
| 454 |
return r; |
| 455 |
} |
| 456 |
|
| 457 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL; |
| 458 |
r = sd_rtnl_message_new_link(*rtnl, &message, RTM_NEWLINKPROP, 0); |
| 459 |
if (r < 0) |
| 460 |
return r; |
| 461 |
|
| 462 |
r = sd_netlink_message_append_string(message, IFLA_IFNAME, ifname); |
| 463 |
if (r < 0) |
| 464 |
return r; |
| 465 |
|
| 466 |
r = sd_netlink_message_open_container(message, IFLA_PROP_LIST); |
| 467 |
if (r < 0) |
| 468 |
return r; |
| 469 |
|
| 470 |
r = sd_netlink_message_append_strv(message, IFLA_ALT_IFNAME, (const char**) alternative_names); |
| 471 |
if (r < 0) |
| 472 |
return r; |
| 473 |
|
| 474 |
r = sd_netlink_message_close_container(message); |
| 475 |
if (r < 0) |
| 476 |
return r; |
| 477 |
|
| 478 |
r = sd_netlink_call(*rtnl, message, 0, NULL); |
| 479 |
if (r < 0) |
| 480 |
return r; |
| 481 |
|
| 482 |
return 0; |
| 483 |
} |
| 484 |
|
| 485 |
int rtnl_get_link_info_full( |
| 486 |
sd_netlink **rtnl, |
| 487 |
int ifindex, |
| 488 |
char **ret_name, |
| 489 |
char ***ret_altnames, |
| 490 |
unsigned short *ret_iftype, |
| 491 |
unsigned *ret_flags, |
| 492 |
char **ret_kind, |
| 493 |
struct hw_addr_data *ret_hw_addr, |
| 494 |
struct hw_addr_data *ret_permanent_hw_addr) { |
| 495 |
|
| 496 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL, *reply = NULL; |
| 497 |
_cleanup_(sd_netlink_unrefp) sd_netlink *our_rtnl = NULL; |
| 498 |
struct hw_addr_data addr = HW_ADDR_NULL, perm_addr = HW_ADDR_NULL; |
| 499 |
_cleanup_free_ char *name = NULL, *kind = NULL; |
| 500 |
_cleanup_strv_free_ char **altnames = NULL; |
| 501 |
unsigned short iftype; |
| 502 |
unsigned flags; |
| 503 |
int r; |
| 504 |
|
| 505 |
assert(ifindex > 0); |
| 506 |
|
| 507 |
if (!rtnl) |
| 508 |
rtnl = &our_rtnl; |
| 509 |
if (!*rtnl) { |
| 510 |
r = sd_netlink_open(rtnl); |
| 511 |
if (r < 0) |
| 512 |
return r; |
| 513 |
} |
| 514 |
|
| 515 |
r = sd_rtnl_message_new_link(*rtnl, &message, RTM_GETLINK, ifindex); |
| 516 |
if (r < 0) |
| 517 |
return r; |
| 518 |
|
| 519 |
r = sd_netlink_call(*rtnl, message, 0, &reply); |
| 520 |
if (r == -EINVAL) |
| 521 |
return -ENODEV; /* The device does not exist */ |
| 522 |
if (r < 0) |
| 523 |
return r; |
| 524 |
|
| 525 |
r = parse_newlink_message(reply, ret_name ? &name : NULL, ret_altnames ? &altnames : NULL); |
| 526 |
if (r < 0) |
| 527 |
return r; |
| 528 |
if (r != ifindex) |
| 529 |
return -EIO; |
| 530 |
|
| 531 |
if (ret_iftype) { |
| 532 |
r = sd_rtnl_message_link_get_type(reply, &iftype); |
| 533 |
if (r < 0) |
| 534 |
return r; |
| 535 |
} |
| 536 |
|
| 537 |
if (ret_flags) { |
| 538 |
r = sd_rtnl_message_link_get_flags(reply, &flags); |
| 539 |
if (r < 0) |
| 540 |
return r; |
| 541 |
} |
| 542 |
|
| 543 |
if (ret_kind) { |
| 544 |
r = sd_netlink_message_enter_container(reply, IFLA_LINKINFO); |
| 545 |
if (r >= 0) { |
| 546 |
r = sd_netlink_message_read_string_strdup(reply, IFLA_INFO_KIND, &kind); |
| 547 |
if (r < 0 && r != -ENODATA) |
| 548 |
return r; |
| 549 |
|
| 550 |
r = sd_netlink_message_exit_container(reply); |
| 551 |
if (r < 0) |
| 552 |
return r; |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
if (ret_hw_addr) { |
| 557 |
r = netlink_message_read_hw_addr(reply, IFLA_ADDRESS, &addr); |
| 558 |
if (r < 0 && r != -ENODATA) |
| 559 |
return r; |
| 560 |
} |
| 561 |
|
| 562 |
if (ret_permanent_hw_addr) { |
| 563 |
r = netlink_message_read_hw_addr(reply, IFLA_PERM_ADDRESS, &perm_addr); |
| 564 |
if (r < 0 && r != -ENODATA) |
| 565 |
return r; |
| 566 |
} |
| 567 |
|
| 568 |
if (ret_name) |
| 569 |
*ret_name = TAKE_PTR(name); |
| 570 |
if (ret_altnames) |
| 571 |
*ret_altnames = TAKE_PTR(altnames); |
| 572 |
if (ret_iftype) |
| 573 |
*ret_iftype = iftype; |
| 574 |
if (ret_flags) |
| 575 |
*ret_flags = flags; |
| 576 |
if (ret_kind) |
| 577 |
*ret_kind = TAKE_PTR(kind); |
| 578 |
if (ret_hw_addr) |
| 579 |
*ret_hw_addr = addr; |
| 580 |
if (ret_permanent_hw_addr) |
| 581 |
*ret_permanent_hw_addr = perm_addr; |
| 582 |
return ifindex; |
| 583 |
} |
| 584 |
|
| 585 |
int rtnl_log_parse_error(int r) { |
| 586 |
return log_error_errno(r, "Failed to parse netlink message: %m"); |
| 587 |
} |
| 588 |
|
| 589 |
int rtnl_log_create_error(int r) { |
| 590 |
return log_error_errno(r, "Failed to create netlink message: %m"); |
| 591 |
} |
| 592 |
|
| 593 |
void rtattr_append_attribute_internal(struct rtattr *rta, unsigned short type, const void *data, size_t data_length) { |
| 594 |
size_t padding_length; |
| 595 |
uint8_t *padding; |
| 596 |
|
| 597 |
assert(rta); |
| 598 |
assert(!data || data_length > 0); |
| 599 |
|
| 600 |
/* fill in the attribute */ |
| 601 |
rta->rta_type = type; |
| 602 |
rta->rta_len = RTA_LENGTH(data_length); |
| 603 |
if (data) |
| 604 |
/* we don't deal with the case where the user lies about the type |
| 605 |
* and gives us too little data (so don't do that) |
| 606 |
*/ |
| 607 |
padding = mempcpy(RTA_DATA(rta), data, data_length); |
| 608 |
|
| 609 |
else |
| 610 |
/* if no data was passed, make sure we still initialize the padding |
| 611 |
note that we can have data_length > 0 (used by some containers) */ |
| 612 |
padding = RTA_DATA(rta); |
| 613 |
|
| 614 |
/* make sure also the padding at the end of the message is initialized */ |
| 615 |
padding_length = (uint8_t *) rta + RTA_SPACE(data_length) - padding; |
| 616 |
memzero(padding, padding_length); |
| 617 |
} |
| 618 |
|
| 619 |
int rtattr_append_attribute(struct rtattr **rta, unsigned short type, const void *data, size_t data_length) { |
| 620 |
struct rtattr *new_rta, *sub_rta; |
| 621 |
size_t message_length; |
| 622 |
|
| 623 |
assert(rta); |
| 624 |
assert(!data || data_length > 0); |
| 625 |
|
| 626 |
/* get the new message size (with padding at the end) */ |
| 627 |
message_length = RTA_ALIGN(rta ? (*rta)->rta_len : 0) + RTA_SPACE(data_length); |
| 628 |
|
| 629 |
/* buffer should be smaller than both one page or 8K to be accepted by the kernel */ |
| 630 |
if (message_length > MIN(page_size(), 8192UL)) |
| 631 |
return -ENOBUFS; |
| 632 |
|
| 633 |
/* realloc to fit the new attribute */ |
| 634 |
new_rta = realloc(*rta, message_length); |
| 635 |
if (!new_rta) |
| 636 |
return -ENOMEM; |
| 637 |
*rta = new_rta; |
| 638 |
|
| 639 |
/* get pointer to the attribute we are about to add */ |
| 640 |
sub_rta = (struct rtattr *) ((uint8_t *) *rta + RTA_ALIGN((*rta)->rta_len)); |
| 641 |
|
| 642 |
rtattr_append_attribute_internal(sub_rta, type, data, data_length); |
| 643 |
|
| 644 |
/* update rta_len */ |
| 645 |
(*rta)->rta_len = message_length; |
| 646 |
|
| 647 |
return 0; |
| 648 |
} |
| 649 |
|
| 650 |
bool netlink_pid_changed(sd_netlink *nl) { |
| 651 |
/* We don't support people creating an nl connection and |
| 652 |
* keeping it around over a fork(). Let's complain. */ |
| 653 |
return ASSERT_PTR(nl)->original_pid != getpid_cached(); |
| 654 |
} |
| 655 |
|
| 656 |
static int socket_open(int family) { |
| 657 |
int fd; |
| 658 |
|
| 659 |
fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, family); |
| 660 |
if (fd < 0) |
| 661 |
return -errno; |
| 662 |
|
| 663 |
return fd_move_above_stdio(fd); |
| 664 |
} |
| 665 |
|
| 666 |
int netlink_open_family(sd_netlink **ret, int family) { |
| 667 |
_cleanup_close_ int fd = -EBADF; |
| 668 |
int r; |
| 669 |
|
| 670 |
fd = socket_open(family); |
| 671 |
if (fd < 0) |
| 672 |
return fd; |
| 673 |
|
| 674 |
r = sd_netlink_open_fd(ret, fd); |
| 675 |
if (r < 0) |
| 676 |
return r; |
| 677 |
TAKE_FD(fd); |
| 678 |
|
| 679 |
return 0; |
| 680 |
} |
| 681 |
|
| 682 |
static bool serial_used(sd_netlink *nl, uint32_t serial) { |
| 683 |
assert(nl); |
| 684 |
|
| 685 |
return |
| 686 |
hashmap_contains(nl->reply_callbacks, UINT32_TO_PTR(serial)) || |
| 687 |
hashmap_contains(nl->rqueue_by_serial, UINT32_TO_PTR(serial)) || |
| 688 |
hashmap_contains(nl->rqueue_partial_by_serial, UINT32_TO_PTR(serial)); |
| 689 |
} |
| 690 |
|
| 691 |
void netlink_seal_message(sd_netlink *nl, sd_netlink_message *m) { |
| 692 |
uint32_t picked; |
| 693 |
|
| 694 |
assert(nl); |
| 695 |
assert(!netlink_pid_changed(nl)); |
| 696 |
assert(m); |
| 697 |
assert(m->hdr); |
| 698 |
|
| 699 |
/* Avoid collisions with outstanding requests */ |
| 700 |
do { |
| 701 |
picked = nl->serial; |
| 702 |
|
| 703 |
/* Don't use seq == 0, as that is used for broadcasts, so we would get confused by replies to |
| 704 |
such messages */ |
| 705 |
nl->serial = nl->serial == UINT32_MAX ? 1 : nl->serial + 1; |
| 706 |
|
| 707 |
} while (serial_used(nl, picked)); |
| 708 |
|
| 709 |
m->hdr->nlmsg_seq = picked; |
| 710 |
message_seal(m); |
| 711 |
} |
| 712 |
|
| 713 |
static int socket_writev_message(sd_netlink *nl, sd_netlink_message **m, size_t msgcount) { |
| 714 |
_cleanup_free_ struct iovec *iovs = NULL; |
| 715 |
ssize_t k; |
| 716 |
|
| 717 |
assert(nl); |
| 718 |
assert(m); |
| 719 |
assert(msgcount > 0); |
| 720 |
|
| 721 |
iovs = new(struct iovec, msgcount); |
| 722 |
if (!iovs) |
| 723 |
return -ENOMEM; |
| 724 |
|
| 725 |
for (size_t i = 0; i < msgcount; i++) { |
| 726 |
assert(m[i]->hdr); |
| 727 |
assert(m[i]->hdr->nlmsg_len > 0); |
| 728 |
|
| 729 |
iovs[i] = IOVEC_MAKE(m[i]->hdr, m[i]->hdr->nlmsg_len); |
| 730 |
} |
| 731 |
|
| 732 |
k = writev(nl->fd, iovs, msgcount); |
| 733 |
if (k < 0) |
| 734 |
return -errno; |
| 735 |
|
| 736 |
return k; |
| 737 |
} |
| 738 |
|
| 739 |
int sd_netlink_sendv( |
| 740 |
sd_netlink *nl, |
| 741 |
sd_netlink_message **messages, |
| 742 |
size_t msgcount, |
| 743 |
uint32_t **ret_serial) { |
| 744 |
|
| 745 |
_cleanup_free_ uint32_t *serials = NULL; |
| 746 |
int r; |
| 747 |
|
| 748 |
assert_return(nl, -EINVAL); |
| 749 |
assert_return(!netlink_pid_changed(nl), -ECHILD); |
| 750 |
assert_return(messages, -EINVAL); |
| 751 |
assert_return(msgcount > 0, -EINVAL); |
| 752 |
|
| 753 |
if (ret_serial) { |
| 754 |
serials = new(uint32_t, msgcount); |
| 755 |
if (!serials) |
| 756 |
return -ENOMEM; |
| 757 |
} |
| 758 |
|
| 759 |
for (size_t i = 0; i < msgcount; i++) { |
| 760 |
assert_return(!messages[i]->sealed, -EPERM); |
| 761 |
|
| 762 |
netlink_seal_message(nl, messages[i]); |
| 763 |
if (serials) |
| 764 |
serials[i] = message_get_serial(messages[i]); |
| 765 |
} |
| 766 |
|
| 767 |
r = socket_writev_message(nl, messages, msgcount); |
| 768 |
if (r < 0) |
| 769 |
return r; |
| 770 |
|
| 771 |
if (ret_serial) |
| 772 |
*ret_serial = TAKE_PTR(serials); |
| 773 |
|
| 774 |
return r; |
| 775 |
} |