I have a block of C code that gets a variety of information about a network interface, namely the SSID and the signal level in dBm:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define WIRELESS_INTERFACE "wlp2s0"
int main () {
// Communicate using ioctl to get information
struct iwreq wreq;
int sockfd;
char *ssid;
// Allocate memory for the request
memset(&wreq, 0, sizeof(struct iwreq));
// Assign our interface name to the request
sprintf(wreq.ifr_name, WIRELESS_INTERFACE);
// Open socket for ioctl
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
fprintf(stderr, "FAILED 1");
exit(1);
}
// Get SSID
wreq.u.essid.pointer = ssid;
wreq.u.essid.length = 32;
if (ioctl(sockfd, SIOCGIWESSID, &wreq) == -1) {
fprintf(stderr, "FAILED 2");
exit(2);
}
fwrite(wreq.u.essid.pointer, 1, wreq.u.essid.length, stdout);
struct iw_statistics *stats;
int8_t signalLevel = 0;
wreq.u.data.pointer = (struct iw_statistics *)malloc(sizeof(*stats));
wreq.u.data.length = sizeof(*stats);
wreq.u.data.flags = 1;
if (ioctl(sockfd, SIOCGIWSTATS, &wreq) == -1) {
fprintf(stderr, "FAILED 3");
exit(3);
}
if (((struct iw_statistics *)wreq.u.data.pointer)->qual.updated &
IW_QUAL_DBM) {
fputs("\nSignal valid\n", stdout);
// signal is measured in dBm and is valid for us to use
signalLevel =
((struct iw_statistics *)wreq.u.data.pointer)->qual.level - 256;
}
fprintf(stdout, "\nsignalLevel %d\n", signalLevel);
}
Compiling and running with gcc stackoverflow.c -o stackoverflow && ./stackoverflow
will always output the following:
Erebus
Signal valid
signalLevel 0
I have tried a new sockfd
and wreq
variable for each ioctl
call but this does not work.
I can confirm that other tools correctly report the signal level:
➜ nmcli -f IN-USE,SIGNAL,SSID device wifi
IN-USE SIGNAL SSID
84 other1
80 other2
75 Erebus
75 --
75 other3
75 Erebus
* 72 Erebus
65 other3
25 other5
20 other6
However one tool confirmed to be using the same ioctl calls does not work, iwconfig wlp2s0
gives Signal level=0 dbm
.
Information about my system:
➜ uname -a
Linux jack-laptop 5.15.0-52-generic #58-Ubuntu SMP Thu Oct 13 08:03:55 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Ubuntu 22.04
Edit: I believe this is a driver specific issue so adding on information about the wireless driver:
02:00.0 Network controller: Qualcomm Atheros QCNFA765 (rev 01)
Subsystem: Lenovo Device 9309
Kernel driver in use: ath11k_pci
Kernel modules: ath11k_pci
Asked by Jack828
(11 rep)
Nov 7, 2022, 09:46 PM
Last activity: Nov 8, 2022, 09:17 AM
Last activity: Nov 8, 2022, 09:17 AM