network: split out functions to get ssid and bssid
This commit is contained in:
parent
516e9c802f
commit
b492aea093
|
@ -12,101 +12,9 @@
|
|||
#include "networkd-manager.h"
|
||||
#include "networkd-wifi.h"
|
||||
#include "string-util.h"
|
||||
|
||||
static int wifi_get_ssid(Link *link) {
|
||||
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *reply = NULL;
|
||||
_cleanup_free_ char *ssid = NULL;
|
||||
sd_genl_family family;
|
||||
int r;
|
||||
|
||||
r = sd_genl_message_new(link->manager->genl, SD_GENL_NL80211, NL80211_CMD_GET_INTERFACE, &m);
|
||||
if (r < 0)
|
||||
return log_link_error_errno(link, r, "Failed to create generic netlink message: %m");
|
||||
|
||||
r = sd_netlink_message_append_u32(m, NL80211_ATTR_IFINDEX, link->ifindex);
|
||||
if (r < 0)
|
||||
return log_link_error_errno(link, r, "Could not append NL80211_ATTR_IFINDEX attribute: %m");
|
||||
|
||||
r = sd_netlink_call(link->manager->genl, m, 0, &reply);
|
||||
if (r < 0)
|
||||
return log_link_error_errno(link, r, "Failed to request information about wifi interface: %m");
|
||||
if (!reply)
|
||||
return 0;
|
||||
|
||||
r = sd_netlink_message_get_errno(reply);
|
||||
if (r < 0)
|
||||
return log_link_warning_errno(link, r, "Failed to get information about wifi interface: %m");
|
||||
|
||||
r = sd_genl_message_get_family(link->manager->genl, reply, &family);
|
||||
if (r < 0)
|
||||
return log_link_warning_errno(link, r, "Failed to determine genl family: %m");
|
||||
if (family != SD_GENL_NL80211) {
|
||||
log_link_debug(link, "Received message of unexpected genl family %u, ignoring.", family);
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = sd_netlink_message_read_string_strdup(reply, NL80211_ATTR_SSID, &ssid);
|
||||
if (r < 0 && r != -ENODATA)
|
||||
return log_link_warning_errno(link, r, "Failed to get NL80211_ATTR_SSID attribute: %m");
|
||||
|
||||
free_and_replace(link->ssid, ssid);
|
||||
return r == -ENODATA ? 0 : 1;
|
||||
}
|
||||
|
||||
static int wifi_get_bssid(Link *link) {
|
||||
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *reply = NULL;
|
||||
struct ether_addr mac = {};
|
||||
sd_genl_family family;
|
||||
int r;
|
||||
|
||||
assert(link);
|
||||
assert(link->manager);
|
||||
assert(link->manager->genl);
|
||||
|
||||
r = sd_genl_message_new(link->manager->genl, SD_GENL_NL80211, NL80211_CMD_GET_STATION, &m);
|
||||
if (r < 0)
|
||||
return log_link_error_errno(link, r, "Failed to create generic netlink message: %m");
|
||||
|
||||
r = sd_netlink_message_set_flags(m, NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP);
|
||||
if (r < 0)
|
||||
return log_link_error_errno(link, r, "Failed to set dump flag: %m");
|
||||
|
||||
r = sd_netlink_message_append_u32(m, NL80211_ATTR_IFINDEX, link->ifindex);
|
||||
if (r < 0)
|
||||
return log_link_error_errno(link, r, "Could not append NL80211_ATTR_IFINDEX attribute: %m");
|
||||
|
||||
r = sd_netlink_call(link->manager->genl, m, 0, &reply);
|
||||
if (r < 0)
|
||||
return log_link_error_errno(link, r, "Failed to request information about wifi station: %m");
|
||||
if (!reply)
|
||||
return 0;
|
||||
|
||||
r = sd_netlink_message_get_errno(reply);
|
||||
if (r < 0)
|
||||
return log_link_error_errno(link, r, "Failed to get information about wifi station: %m");
|
||||
|
||||
r = sd_genl_message_get_family(link->manager->genl, reply, &family);
|
||||
if (r < 0)
|
||||
return log_link_warning_errno(link, r, "Failed to determine genl family: %m");
|
||||
if (family != SD_GENL_NL80211) {
|
||||
log_link_debug(link, "Received message of unexpected genl family %u, ignoring.", family);
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = sd_netlink_message_read_ether_addr(reply, NL80211_ATTR_MAC, &mac);
|
||||
if (r < 0 && r != -ENODATA)
|
||||
return log_link_warning_errno(link, r, "Failed to get NL80211_ATTR_MAC attribute: %m");
|
||||
|
||||
r = memcmp(&link->bssid, &mac, sizeof(mac));
|
||||
if (r == 0)
|
||||
return 0;
|
||||
|
||||
memcpy(&link->bssid, &mac, sizeof(mac));
|
||||
return 1;
|
||||
}
|
||||
#include "wifi-util.h"
|
||||
|
||||
int wifi_get_info(Link *link) {
|
||||
char buf[ETHER_ADDR_TO_STRING_MAX];
|
||||
const char *type;
|
||||
int r, s;
|
||||
|
||||
|
@ -124,15 +32,24 @@ int wifi_get_info(Link *link) {
|
|||
if (!streq(type, "wlan"))
|
||||
return 0;
|
||||
|
||||
r = wifi_get_ssid(link);
|
||||
_cleanup_free_ char *ssid = NULL;
|
||||
r = wifi_get_ssid(link->manager->genl, link->ifindex, &ssid);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r > 0 && streq_ptr(link->ssid, ssid))
|
||||
r = 0;
|
||||
free_and_replace(link->ssid, ssid);
|
||||
|
||||
s = wifi_get_bssid(link);
|
||||
struct ether_addr old_bssid = link->bssid;
|
||||
s = wifi_get_bssid(link->manager->genl, link->ifindex, &link->bssid);
|
||||
if (s < 0)
|
||||
return s;
|
||||
if (s > 0 && memcmp(&old_bssid, &link->bssid, sizeof old_bssid) == 0)
|
||||
s = 0;
|
||||
|
||||
if (r > 0 || s > 0) {
|
||||
char buf[ETHER_ADDR_TO_STRING_MAX];
|
||||
|
||||
if (link->ssid)
|
||||
log_link_info(link, "Connected WiFi access point: %s (%s)",
|
||||
link->ssid, ether_addr_to_string(&link->bssid, buf));
|
||||
|
|
|
@ -189,6 +189,8 @@ shared_sources = files('''
|
|||
watchdog.h
|
||||
web-util.c
|
||||
web-util.h
|
||||
wifi-util.c
|
||||
wifi-util.h
|
||||
xml.c
|
||||
xml.h
|
||||
'''.split())
|
||||
|
|
|
@ -0,0 +1,90 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#include <net/ethernet.h>
|
||||
#include <linux/nl80211.h>
|
||||
|
||||
#include "sd-bus.h"
|
||||
|
||||
#include "log.h"
|
||||
#include "netlink-util.h"
|
||||
#include "wifi-util.h"
|
||||
|
||||
int wifi_get_ssid(sd_netlink *genl, int ifindex, char **ssid) {
|
||||
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *reply = NULL;
|
||||
sd_genl_family family;
|
||||
int r;
|
||||
|
||||
r = sd_genl_message_new(genl, SD_GENL_NL80211, NL80211_CMD_GET_INTERFACE, &m);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to create generic netlink message: %m");
|
||||
|
||||
r = sd_netlink_message_append_u32(m, NL80211_ATTR_IFINDEX, ifindex);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Could not append NL80211_ATTR_IFINDEX attribute: %m");
|
||||
|
||||
r = sd_netlink_call(genl, m, 0, &reply);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to request information about wifi interface %d: %m", ifindex);
|
||||
if (!reply)
|
||||
return 0;
|
||||
|
||||
r = sd_netlink_message_get_errno(reply);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to get information about wifi interface %d: %m", ifindex);
|
||||
|
||||
r = sd_genl_message_get_family(genl, reply, &family);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to determine genl family: %m");
|
||||
if (family != SD_GENL_NL80211) {
|
||||
log_debug("Received message of unexpected genl family %u, ignoring.", family);
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = sd_netlink_message_read_string_strdup(reply, NL80211_ATTR_SSID, ssid);
|
||||
if (r < 0 && r != -ENODATA)
|
||||
return log_debug_errno(r, "Failed to get NL80211_ATTR_SSID attribute: %m");
|
||||
|
||||
return r == -ENODATA ? 0 : 1;
|
||||
}
|
||||
|
||||
int wifi_get_bssid(sd_netlink *genl, int ifindex, struct ether_addr *bssid) {
|
||||
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *reply = NULL;
|
||||
sd_genl_family family;
|
||||
int r;
|
||||
|
||||
r = sd_genl_message_new(genl, SD_GENL_NL80211, NL80211_CMD_GET_STATION, &m);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to create generic netlink message: %m");
|
||||
|
||||
r = sd_netlink_message_set_flags(m, NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to set dump flag: %m");
|
||||
|
||||
r = sd_netlink_message_append_u32(m, NL80211_ATTR_IFINDEX, ifindex);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Could not append NL80211_ATTR_IFINDEX attribute: %m");
|
||||
|
||||
r = sd_netlink_call(genl, m, 0, &reply);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to request information about wifi station: %m");
|
||||
if (!reply)
|
||||
return 0;
|
||||
|
||||
r = sd_netlink_message_get_errno(reply);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to get information about wifi station: %m");
|
||||
|
||||
r = sd_genl_message_get_family(genl, reply, &family);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to determine genl family: %m");
|
||||
if (family != SD_GENL_NL80211) {
|
||||
log_debug("Received message of unexpected genl family %u, ignoring.", family);
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = sd_netlink_message_read_ether_addr(reply, NL80211_ATTR_MAC, bssid);
|
||||
if (r < 0 && r != -ENODATA)
|
||||
return log_debug_errno(r, "Failed to get NL80211_ATTR_MAC attribute: %m");
|
||||
|
||||
return r == -ENODATA ? 0 : 1;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "netlink-util.h"
|
||||
|
||||
int wifi_get_ssid(sd_netlink *genl, int ifindex, char **ssid);
|
||||
int wifi_get_bssid(sd_netlink *genl, int ifindex, struct ether_addr *bssid);
|
Loading…
Reference in New Issue