| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/nl80211.h> |
| 4 |
#include <net/if_arp.h> |
| 5 |
|
| 6 |
#include "sd-netlink.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "device-private.h" |
| 10 |
#include "device-util.h" |
| 11 |
#include "errno-util.h" |
| 12 |
#include "hashmap.h" |
| 13 |
#include "networkd-link.h" |
| 14 |
#include "networkd-manager.h" |
| 15 |
#include "networkd-wiphy.h" |
| 16 |
#include "path-util.h" |
| 17 |
#include "string-util.h" |
| 18 |
#include "udev-util.h" |
| 19 |
#include "wifi-util.h" |
| 20 |
|
| 21 |
static Wiphy* wiphy_free(Wiphy *w) { |
| 22 |
if (!w) |
| 23 |
return NULL; |
| 24 |
|
| 25 |
if (w->manager) { |
| 26 |
hashmap_remove_value(w->manager->wiphy_by_index, UINT32_TO_PTR(w->index), w); |
| 27 |
if (w->name) |
| 28 |
hashmap_remove_value(w->manager->wiphy_by_name, w->name, w); |
| 29 |
} |
| 30 |
|
| 31 |
sd_device_unref(w->dev); |
| 32 |
sd_device_unref(w->rfkill); |
| 33 |
|
| 34 |
free(w->name); |
| 35 |
return mfree(w); |
| 36 |
} |
| 37 |
|
| 38 |
DEFINE_TRIVIAL_CLEANUP_FUNC(Wiphy*, wiphy_free); |
| 39 |
|
| 40 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 41 |
wiphy_hash_ops, |
| 42 |
void, trivial_hash_func, trivial_compare_func, |
| 43 |
Wiphy, wiphy_free); |
| 44 |
|
| 45 |
static int wiphy_new(Manager *manager, sd_netlink_message *message, Wiphy **ret) { |
| 46 |
_cleanup_(wiphy_freep) Wiphy *w = NULL; |
| 47 |
_cleanup_free_ char *name = NULL; |
| 48 |
uint32_t index; |
| 49 |
int r; |
| 50 |
|
| 51 |
assert(manager); |
| 52 |
assert(message); |
| 53 |
|
| 54 |
r = sd_netlink_message_read_u32(message, NL80211_ATTR_WIPHY, &index); |
| 55 |
if (r < 0) |
| 56 |
return r; |
| 57 |
|
| 58 |
r = sd_netlink_message_read_string_strdup(message, NL80211_ATTR_WIPHY_NAME, &name); |
| 59 |
if (r < 0) |
| 60 |
return r; |
| 61 |
|
| 62 |
w = new(Wiphy, 1); |
| 63 |
if (!w) |
| 64 |
return -ENOMEM; |
| 65 |
|
| 66 |
*w = (Wiphy) { |
| 67 |
.manager = manager, |
| 68 |
.index = index, |
| 69 |
.name = TAKE_PTR(name), |
| 70 |
}; |
| 71 |
|
| 72 |
r = hashmap_ensure_put(&manager->wiphy_by_index, &wiphy_hash_ops, UINT32_TO_PTR(w->index), w); |
| 73 |
if (r < 0) |
| 74 |
return r; |
| 75 |
|
| 76 |
r = hashmap_ensure_put(&w->manager->wiphy_by_name, &string_hash_ops, w->name, w); |
| 77 |
if (r < 0) |
| 78 |
return r; |
| 79 |
|
| 80 |
log_wiphy_debug(w, "Saved new wiphy: index=%"PRIu32, w->index); |
| 81 |
|
| 82 |
if (ret) |
| 83 |
*ret = w; |
| 84 |
|
| 85 |
TAKE_PTR(w); |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
int wiphy_get_by_index(Manager *manager, uint32_t index, Wiphy **ret) { |
| 90 |
Wiphy *w; |
| 91 |
|
| 92 |
assert(manager); |
| 93 |
|
| 94 |
w = hashmap_get(manager->wiphy_by_index, UINT32_TO_PTR(index)); |
| 95 |
if (!w) |
| 96 |
return -ENODEV; |
| 97 |
|
| 98 |
if (ret) |
| 99 |
*ret = w; |
| 100 |
|
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
int wiphy_get_by_name(Manager *manager, const char *name, Wiphy **ret) { |
| 105 |
Wiphy *w; |
| 106 |
|
| 107 |
assert(manager); |
| 108 |
assert(name); |
| 109 |
|
| 110 |
w = hashmap_get(manager->wiphy_by_name, name); |
| 111 |
if (!w) |
| 112 |
return -ENODEV; |
| 113 |
|
| 114 |
if (ret) |
| 115 |
*ret = w; |
| 116 |
|
| 117 |
return 0; |
| 118 |
} |
| 119 |
|
| 120 |
static int link_get_wiphy(Link *link, Wiphy **ret) { |
| 121 |
_cleanup_(sd_device_unrefp) sd_device *phy = NULL; |
| 122 |
const char *s; |
| 123 |
int r; |
| 124 |
|
| 125 |
assert(link); |
| 126 |
assert(link->manager); |
| 127 |
|
| 128 |
if (link->iftype != ARPHRD_ETHER) |
| 129 |
return -EOPNOTSUPP; |
| 130 |
|
| 131 |
if (!link->dev) |
| 132 |
return -ENODEV; |
| 133 |
|
| 134 |
r = device_is_devtype(link->dev, "wlan"); |
| 135 |
if (r < 0) |
| 136 |
return r; |
| 137 |
if (r == 0) |
| 138 |
return -EOPNOTSUPP; |
| 139 |
|
| 140 |
r = sd_device_new_child(&phy, link->dev, "phy80211"); |
| 141 |
if (r < 0) |
| 142 |
return r; |
| 143 |
|
| 144 |
r = sd_device_get_sysname(phy, &s); |
| 145 |
if (r < 0) |
| 146 |
return r; |
| 147 |
|
| 148 |
/* TODO: |
| 149 |
* Maybe, it is better to cache the found Wiphy object in the Link object. |
| 150 |
* To support that, we need to investigate what happens when the _phy_ is renamed. */ |
| 151 |
|
| 152 |
return wiphy_get_by_name(link->manager, s, ret); |
| 153 |
} |
| 154 |
|
| 155 |
static int rfkill_get_state(sd_device *dev) { |
| 156 |
int r; |
| 157 |
|
| 158 |
assert(dev); |
| 159 |
|
| 160 |
/* The previous values may be outdated. Let's clear cache and re-read the values. */ |
| 161 |
device_clear_sysattr_cache(dev); |
| 162 |
|
| 163 |
r = device_get_sysattr_bool(dev, "soft"); |
| 164 |
if (r < 0 && r != -ENOENT) |
| 165 |
return r; |
| 166 |
if (r > 0) |
| 167 |
return RFKILL_SOFT; |
| 168 |
|
| 169 |
r = device_get_sysattr_bool(dev, "hard"); |
| 170 |
if (r < 0 && r != -ENOENT) |
| 171 |
return r; |
| 172 |
if (r > 0) |
| 173 |
return RFKILL_HARD; |
| 174 |
|
| 175 |
return RFKILL_UNBLOCKED; |
| 176 |
} |
| 177 |
|
| 178 |
static int wiphy_rfkilled(Wiphy *w) { |
| 179 |
int r; |
| 180 |
|
| 181 |
assert(w); |
| 182 |
|
| 183 |
if (!udev_available()) { |
| 184 |
if (w->rfkill_state != RFKILL_UNBLOCKED) { |
| 185 |
log_wiphy_debug(w, "Running in container, assuming the radio transmitter is unblocked."); |
| 186 |
w->rfkill_state = RFKILL_UNBLOCKED; /* To suppress the above log message, cache the state. */ |
| 187 |
} |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
if (!w->rfkill) { |
| 192 |
if (w->rfkill_state != RFKILL_UNBLOCKED) { |
| 193 |
log_wiphy_debug(w, "No rfkill device found, assuming the radio transmitter is unblocked."); |
| 194 |
w->rfkill_state = RFKILL_UNBLOCKED; /* To suppress the above log message, cache the state. */ |
| 195 |
} |
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
r = rfkill_get_state(w->rfkill); |
| 200 |
if (r < 0) |
| 201 |
return log_wiphy_debug_errno(w, r, "Could not get rfkill state: %m"); |
| 202 |
|
| 203 |
if (w->rfkill_state != r) |
| 204 |
switch (r) { |
| 205 |
case RFKILL_UNBLOCKED: |
| 206 |
log_wiphy_debug(w, "The radio transmitter is unblocked."); |
| 207 |
break; |
| 208 |
case RFKILL_SOFT: |
| 209 |
log_wiphy_debug(w, "The radio transmitter is turned off by software."); |
| 210 |
break; |
| 211 |
case RFKILL_HARD: |
| 212 |
log_wiphy_debug(w, "The radio transmitter is forced off by something outside of the driver's control."); |
| 213 |
break; |
| 214 |
default: |
| 215 |
assert_not_reached(); |
| 216 |
} |
| 217 |
|
| 218 |
w->rfkill_state = r; /* Cache the state to suppress the above log messages. */ |
| 219 |
return r != RFKILL_UNBLOCKED; |
| 220 |
} |
| 221 |
|
| 222 |
int link_rfkilled(Link *link) { |
| 223 |
Wiphy *w; |
| 224 |
int r; |
| 225 |
|
| 226 |
assert(link); |
| 227 |
|
| 228 |
r = link_get_wiphy(link, &w); |
| 229 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(r) || ERRNO_IS_NEG_DEVICE_ABSENT(r)) |
| 230 |
return false; /* Typically, non-wifi interface or running in container */ |
| 231 |
if (r < 0) |
| 232 |
return log_link_debug_errno(link, r, "Could not get phy: %m"); |
| 233 |
|
| 234 |
return wiphy_rfkilled(w); |
| 235 |
} |
| 236 |
|
| 237 |
static int wiphy_update_name(Wiphy *w, sd_netlink_message *message) { |
| 238 |
const char *name; |
| 239 |
int r; |
| 240 |
|
| 241 |
assert(w); |
| 242 |
assert(w->manager); |
| 243 |
assert(message); |
| 244 |
|
| 245 |
r = sd_netlink_message_read_string(message, NL80211_ATTR_WIPHY_NAME, &name); |
| 246 |
if (r == -ENODATA) |
| 247 |
return 0; |
| 248 |
if (r < 0) |
| 249 |
return r; |
| 250 |
|
| 251 |
if (streq(w->name, name)) |
| 252 |
return 0; |
| 253 |
|
| 254 |
log_wiphy_debug(w, "Wiphy name change detected, renamed to %s.", name); |
| 255 |
|
| 256 |
hashmap_remove_value(w->manager->wiphy_by_name, w->name, w); |
| 257 |
|
| 258 |
r = free_and_strdup(&w->name, name); |
| 259 |
if (r < 0) |
| 260 |
return r; |
| 261 |
|
| 262 |
r = hashmap_ensure_put(&w->manager->wiphy_by_name, &string_hash_ops, w->name, w); |
| 263 |
if (r < 0) |
| 264 |
return r; |
| 265 |
|
| 266 |
return 1; /* updated */ |
| 267 |
} |
| 268 |
|
| 269 |
static int wiphy_update_device(Wiphy *w) { |
| 270 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 271 |
int r; |
| 272 |
|
| 273 |
assert(w); |
| 274 |
assert(w->name); |
| 275 |
|
| 276 |
if (!udev_available()) |
| 277 |
return 0; |
| 278 |
|
| 279 |
w->dev = sd_device_unref(w->dev); |
| 280 |
|
| 281 |
r = sd_device_new_from_subsystem_sysname(&dev, "ieee80211", w->name); |
| 282 |
if (r < 0) |
| 283 |
return r; |
| 284 |
|
| 285 |
if (DEBUG_LOGGING) { |
| 286 |
const char *s = NULL; |
| 287 |
|
| 288 |
(void) sd_device_get_syspath(dev, &s); |
| 289 |
log_wiphy_debug(w, "Found device: %s", strna(s)); |
| 290 |
} |
| 291 |
|
| 292 |
w->dev = TAKE_PTR(dev); |
| 293 |
return 0; |
| 294 |
} |
| 295 |
|
| 296 |
static int wiphy_update_rfkill(Wiphy *w) { |
| 297 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 298 |
sd_device *rfkill; |
| 299 |
int r; |
| 300 |
|
| 301 |
assert(w); |
| 302 |
|
| 303 |
if (!udev_available()) |
| 304 |
return 0; |
| 305 |
|
| 306 |
w->rfkill = sd_device_unref(w->rfkill); |
| 307 |
|
| 308 |
if (!w->dev) |
| 309 |
return 0; |
| 310 |
|
| 311 |
r = sd_device_enumerator_new(&e); |
| 312 |
if (r < 0) |
| 313 |
return r; |
| 314 |
|
| 315 |
r = sd_device_enumerator_allow_uninitialized(e); |
| 316 |
if (r < 0) |
| 317 |
return r; |
| 318 |
|
| 319 |
r = sd_device_enumerator_add_match_subsystem(e, "rfkill", true); |
| 320 |
if (r < 0) |
| 321 |
return r; |
| 322 |
|
| 323 |
r = sd_device_enumerator_add_match_parent(e, w->dev); |
| 324 |
if (r < 0) |
| 325 |
return r; |
| 326 |
|
| 327 |
rfkill = sd_device_enumerator_get_device_first(e); |
| 328 |
if (!rfkill) |
| 329 |
/* rfkill device may not detected by the kernel yet, and may appear later. */ |
| 330 |
return -ENODEV; |
| 331 |
|
| 332 |
if (sd_device_enumerator_get_device_next(e)) |
| 333 |
return -ENXIO; /* multiple devices found */ |
| 334 |
|
| 335 |
w->rfkill = sd_device_ref(rfkill); |
| 336 |
|
| 337 |
if (DEBUG_LOGGING) { |
| 338 |
const char *s = NULL; |
| 339 |
|
| 340 |
(void) sd_device_get_syspath(rfkill, &s); |
| 341 |
log_wiphy_debug(w, "Found rfkill device: %s", strna(s)); |
| 342 |
} |
| 343 |
|
| 344 |
return 0; |
| 345 |
} |
| 346 |
|
| 347 |
static int wiphy_update(Wiphy *w) { |
| 348 |
int r; |
| 349 |
|
| 350 |
assert(w); |
| 351 |
|
| 352 |
r = wiphy_update_device(w); |
| 353 |
if (ERRNO_IS_NEG_DEVICE_ABSENT(r)) |
| 354 |
log_wiphy_debug_errno(w, r, "Failed to update wiphy device, ignoring: %m"); |
| 355 |
else if (r < 0) |
| 356 |
return log_wiphy_warning_errno(w, r, "Failed to update wiphy device: %m"); |
| 357 |
|
| 358 |
r = wiphy_update_rfkill(w); |
| 359 |
if (ERRNO_IS_NEG_DEVICE_ABSENT(r)) |
| 360 |
log_wiphy_debug_errno(w, r, "Failed to update rfkill device, ignoring: %m"); |
| 361 |
else if (r < 0) |
| 362 |
return log_wiphy_warning_errno(w, r, "Failed to update rfkill device: %m"); |
| 363 |
|
| 364 |
return 0; |
| 365 |
} |
| 366 |
|
| 367 |
int manager_genl_process_nl80211_wiphy(sd_netlink *genl, sd_netlink_message *message, Manager *manager) { |
| 368 |
const char *family; |
| 369 |
uint32_t index; |
| 370 |
uint8_t cmd; |
| 371 |
Wiphy *w = NULL; |
| 372 |
int r; |
| 373 |
|
| 374 |
assert(genl); |
| 375 |
assert(message); |
| 376 |
assert(manager); |
| 377 |
|
| 378 |
if (sd_netlink_message_is_error(message)) { |
| 379 |
r = sd_netlink_message_get_errno(message); |
| 380 |
if (r < 0) |
| 381 |
log_message_warning_errno(message, r, "nl80211: received error message, ignoring"); |
| 382 |
|
| 383 |
return 0; |
| 384 |
} |
| 385 |
|
| 386 |
r = sd_genl_message_get_family_name(genl, message, &family); |
| 387 |
if (r < 0) { |
| 388 |
log_debug_errno(r, "nl80211: failed to determine genl family, ignoring: %m"); |
| 389 |
return 0; |
| 390 |
} |
| 391 |
if (!streq(family, NL80211_GENL_NAME)) { |
| 392 |
log_debug("nl80211: Received message of unexpected genl family '%s', ignoring.", family); |
| 393 |
return 0; |
| 394 |
} |
| 395 |
|
| 396 |
r = sd_genl_message_get_command(genl, message, &cmd); |
| 397 |
if (r < 0) { |
| 398 |
log_debug_errno(r, "nl80211: failed to determine genl message command, ignoring: %m"); |
| 399 |
return 0; |
| 400 |
} |
| 401 |
|
| 402 |
r = sd_netlink_message_read_u32(message, NL80211_ATTR_WIPHY, &index); |
| 403 |
if (r < 0) { |
| 404 |
log_debug_errno(r, "nl80211: received %s(%u) message without valid index, ignoring: %m", |
| 405 |
strna(nl80211_cmd_to_string(cmd)), cmd); |
| 406 |
return 0; |
| 407 |
} |
| 408 |
|
| 409 |
(void) wiphy_get_by_index(manager, index, &w); |
| 410 |
|
| 411 |
switch (cmd) { |
| 412 |
case NL80211_CMD_NEW_WIPHY: |
| 413 |
if (!w) { |
| 414 |
r = wiphy_new(manager, message, &w); |
| 415 |
if (r < 0) { |
| 416 |
log_warning_errno(r, "Failed to save new wiphy, ignoring: %m"); |
| 417 |
return 0; |
| 418 |
} |
| 419 |
} else { |
| 420 |
r = wiphy_update_name(w, message); |
| 421 |
if (r < 0) { |
| 422 |
log_wiphy_warning_errno(w, r, "Failed to update wiphy name, ignoring: %m"); |
| 423 |
return 0; |
| 424 |
} |
| 425 |
if (r == 0) |
| 426 |
return 0; |
| 427 |
} |
| 428 |
|
| 429 |
r = wiphy_update(w); |
| 430 |
if (r < 0) |
| 431 |
log_wiphy_warning_errno(w, r, "Failed to update wiphy, ignoring: %m"); |
| 432 |
|
| 433 |
break; |
| 434 |
|
| 435 |
case NL80211_CMD_DEL_WIPHY: |
| 436 |
if (!w) { |
| 437 |
log_debug("The kernel removed wiphy we do not know, ignoring."); |
| 438 |
return 0; |
| 439 |
} |
| 440 |
|
| 441 |
log_wiphy_debug(w, "Removed."); |
| 442 |
wiphy_free(w); |
| 443 |
break; |
| 444 |
|
| 445 |
default: |
| 446 |
log_wiphy_debug(w, "nl80211: received %s(%u) message, ignoring.", |
| 447 |
strna(nl80211_cmd_to_string(cmd)), cmd); |
| 448 |
} |
| 449 |
|
| 450 |
return 0; |
| 451 |
} |
| 452 |
|
| 453 |
int manager_udev_process_wiphy(Manager *m, sd_device *device, sd_device_action_t action) { |
| 454 |
const char *name; |
| 455 |
Wiphy *w; |
| 456 |
int r; |
| 457 |
|
| 458 |
assert(m); |
| 459 |
assert(device); |
| 460 |
|
| 461 |
r = sd_device_get_sysname(device, &name); |
| 462 |
if (r < 0) |
| 463 |
return log_device_debug_errno(device, r, "Failed to get sysname: %m"); |
| 464 |
|
| 465 |
r = wiphy_get_by_name(m, name, &w); |
| 466 |
if (r < 0) { |
| 467 |
/* This error is not critical, as the corresponding genl message may be received later. */ |
| 468 |
log_device_debug_errno(device, r, "Failed to get Wiphy object, ignoring: %m"); |
| 469 |
return 0; |
| 470 |
} |
| 471 |
|
| 472 |
return device_unref_and_replace_new_ref(w->dev, action == SD_DEVICE_REMOVE ? NULL : device); |
| 473 |
} |
| 474 |
|
| 475 |
int manager_udev_process_rfkill(Manager *m, sd_device *device, sd_device_action_t action) { |
| 476 |
_cleanup_free_ char *parent_path = NULL, *parent_name = NULL; |
| 477 |
const char *s; |
| 478 |
Wiphy *w; |
| 479 |
int r; |
| 480 |
|
| 481 |
assert(m); |
| 482 |
assert(device); |
| 483 |
|
| 484 |
r = sd_device_get_syspath(device, &s); |
| 485 |
if (r < 0) |
| 486 |
return log_device_debug_errno(device, r, "Failed to get syspath: %m"); |
| 487 |
|
| 488 |
/* Do not use sd_device_get_parent() here, as this might be a 'remove' uevent. */ |
| 489 |
r = path_extract_directory(s, &parent_path); |
| 490 |
if (r < 0) |
| 491 |
return log_device_debug_errno(device, r, "Failed to get parent syspath: %m"); |
| 492 |
|
| 493 |
r = path_extract_filename(parent_path, &parent_name); |
| 494 |
if (r < 0) |
| 495 |
return log_device_debug_errno(device, r, "Failed to get parent name: %m"); |
| 496 |
|
| 497 |
r = wiphy_get_by_name(m, parent_name, &w); |
| 498 |
if (r < 0) { |
| 499 |
/* This error is not critical, as the corresponding genl message may be received later. */ |
| 500 |
log_device_debug_errno(device, r, "Failed to get Wiphy object: %m"); |
| 501 |
return 0; |
| 502 |
} |
| 503 |
|
| 504 |
return device_unref_and_replace_new_ref(w->rfkill, action == SD_DEVICE_REMOVE ? NULL : device); |
| 505 |
} |