1 // vim:ts=4:sw=4:expandtab
4 #include <yajl/yajl_gen.h>
5 #include <yajl/yajl_version.h>
10 #include <netlink/netlink.h>
11 #include <netlink/genl/genl.h>
12 #include <netlink/genl/ctrl.h>
13 #include <linux/nl80211.h>
14 #include <linux/if_ether.h>
15 #define IW_ESSID_MAX_SIZE 32
19 #include <sys/param.h>
20 #include <sys/ioctl.h>
21 #include <sys/socket.h>
24 #include <net/if_media.h>
25 #include <net80211/ieee80211.h>
26 #include <net80211/ieee80211_ioctl.h>
28 #define IW_ESSID_MAX_SIZE IEEE80211_NWID_LEN
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
37 #include <net/if_media.h>
38 #include <netproto/802_11/ieee80211.h>
39 #include <netproto/802_11/ieee80211_ioctl.h>
41 #define IW_ESSID_MAX_SIZE IEEE80211_NWID_LEN
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
48 #include <sys/types.h>
49 #include <netinet/in.h>
50 #include <netinet/if_ether.h>
51 #include <net80211/ieee80211.h>
52 #include <net80211/ieee80211_ioctl.h>
57 #define WIRELESS_INFO_FLAG_HAS_ESSID (1 << 0)
58 #define WIRELESS_INFO_FLAG_HAS_QUALITY (1 << 1)
59 #define WIRELESS_INFO_FLAG_HAS_SIGNAL (1 << 2)
60 #define WIRELESS_INFO_FLAG_HAS_NOISE (1 << 3)
61 #define WIRELESS_INFO_FLAG_HAS_FREQUENCY (1 << 4)
63 #define PERCENT_VALUE(value, total) ((int)(value * 100 / (float)total + 0.5f))
67 char essid[IW_ESSID_MAX_SIZE + 1];
68 uint8_t bssid[ETH_ALEN];
80 // Like iw_print_bitrate, but without the dependency on libiw.
81 static void print_bitrate(char *buffer, int buflen, int bitrate) {
86 const double rate = bitrate;
93 } else if (rate >= mega) {
100 snprintf(buffer, buflen, "%g %cb/s", rate / divisor, scale);
103 static uint32_t nl80211_xbm_to_percent(int32_t xbm, uint32_t divisor) {
104 #define NOISE_FLOOR_DBM -90
105 #define SIGNAL_MAX_DBM -20
108 if (xbm < NOISE_FLOOR_DBM)
109 xbm = NOISE_FLOOR_DBM;
110 if (xbm > SIGNAL_MAX_DBM)
111 xbm = SIGNAL_MAX_DBM;
113 return 100 - 70 * (((float)SIGNAL_MAX_DBM - (float)xbm) / ((float)SIGNAL_MAX_DBM - (float)NOISE_FLOOR_DBM));
116 #define WLAN_EID_SSID 0
118 static void find_ssid(uint8_t *ies, uint32_t ies_len, uint8_t **ssid, uint32_t *ssid_len) {
122 while (ies_len > 2 && ies[0] != WLAN_EID_SSID) {
123 ies_len -= ies[1] + 2;
128 if (ies_len < 2 + ies[1])
135 static int gwi_sta_cb(struct nl_msg *msg, void *data) {
136 wireless_info_t *info = data;
138 struct nlattr *tb[NL80211_ATTR_MAX + 1];
139 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
140 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
141 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
142 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
143 [NL80211_STA_INFO_RX_BITRATE] = {.type = NLA_NESTED},
146 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
147 [NL80211_RATE_INFO_BITRATE32] = {.type = NLA_U32},
150 if (nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
153 if (tb[NL80211_ATTR_STA_INFO] == NULL)
156 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, tb[NL80211_ATTR_STA_INFO], stats_policy))
159 if (sinfo[NL80211_STA_INFO_RX_BITRATE] == NULL)
162 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy))
165 if (rinfo[NL80211_RATE_INFO_BITRATE32] == NULL)
168 // NL80211_RATE_INFO_BITRATE32 is specified in units of 100 kbit/s, but iw
169 // used to specify bit/s, so we convert to use the same code path.
170 info->bitrate = nla_get_u32(rinfo[NL80211_RATE_INFO_BITRATE32]) * 100 * 1000;
175 static int gwi_scan_cb(struct nl_msg *msg, void *data) {
176 wireless_info_t *info = data;
177 struct genlmsghdr *gnlh = genlmsg_hdr(nlmsg_hdr(msg));
178 struct nlattr *tb[NL80211_ATTR_MAX + 1];
179 struct nlattr *bss[NL80211_BSS_MAX + 1];
180 struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
181 [NL80211_BSS_FREQUENCY] = {.type = NLA_U32},
182 [NL80211_BSS_BSSID] = {.type = NLA_UNSPEC},
183 [NL80211_BSS_INFORMATION_ELEMENTS] = {.type = NLA_UNSPEC},
184 [NL80211_BSS_SIGNAL_MBM] = {.type = NLA_U32},
185 [NL80211_BSS_SIGNAL_UNSPEC] = {.type = NLA_U8},
186 [NL80211_BSS_STATUS] = {.type = NLA_U32},
189 if (nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
192 if (tb[NL80211_ATTR_BSS] == NULL)
195 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], bss_policy))
198 if (bss[NL80211_BSS_STATUS] == NULL)
201 const uint32_t status = nla_get_u32(bss[NL80211_BSS_STATUS]);
203 if (status != NL80211_BSS_STATUS_ASSOCIATED &&
204 status != NL80211_BSS_STATUS_IBSS_JOINED)
207 if (bss[NL80211_BSS_BSSID] == NULL)
210 memcpy(info->bssid, nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
212 if (bss[NL80211_BSS_FREQUENCY]) {
213 info->flags |= WIRELESS_INFO_FLAG_HAS_FREQUENCY;
214 info->frequency = (double)nla_get_u32(bss[NL80211_BSS_FREQUENCY]) * 1e6;
217 if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
218 info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
219 info->signal_level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
220 info->signal_level_max = 100;
222 info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
223 info->quality = info->signal_level;
224 info->quality_max = 100;
225 info->quality_average = 50;
228 if (bss[NL80211_BSS_SIGNAL_MBM]) {
229 info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
230 info->signal_level = (int)nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]) / 100;
232 info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
233 info->quality = nl80211_xbm_to_percent(nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]), 100);
234 info->quality_max = 100;
235 info->quality_average = 50;
238 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
242 find_ssid(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
243 nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
245 if (ssid && ssid_len) {
246 info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
247 snprintf(info->essid, sizeof(info->essid), "%.*s", ssid_len, ssid);
254 static int get_wireless_info(const char *interface, wireless_info_t *info) {
255 memset(info, 0, sizeof(wireless_info_t));
258 struct nl_sock *sk = nl_socket_alloc();
259 if (genl_connect(sk) != 0)
262 if (nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, gwi_scan_cb, info) < 0)
265 const int nl80211_id = genl_ctrl_resolve(sk, "nl80211");
269 const unsigned int ifidx = if_nametoindex(interface);
273 struct nl_msg *msg = NULL;
274 if ((msg = nlmsg_alloc()) == NULL)
277 if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, nl80211_id, 0, NLM_F_DUMP, NL80211_CMD_GET_SCAN, 0) ||
278 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifidx) < 0)
281 if (nl_send_sync(sk, msg) < 0)
282 // nl_send_sync calls nlmsg_free()
286 if (nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, gwi_sta_cb, info) < 0)
289 if ((msg = nlmsg_alloc()) == NULL)
292 if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, nl80211_id, 0, NLM_F_DUMP, NL80211_CMD_GET_STATION, 0) || nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifidx) < 0 || nla_put(msg, NL80211_ATTR_MAC, 6, info->bssid) < 0)
295 if (nl_send_sync(sk, msg) < 0)
296 // nl_send_sync calls nlmsg_free()
310 #if defined(__FreeBSD__) || defined(__DragonFly__)
313 struct ieee80211req_sta_req req;
314 uint8_t buf[24 * 1024];
316 struct ieee80211req na;
317 char bssid[IEEE80211_ADDR_LEN];
320 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
323 memset(&na, 0, sizeof(na));
324 strlcpy(na.i_name, interface, sizeof(na.i_name));
325 na.i_type = IEEE80211_IOC_SSID;
326 na.i_data = &info->essid[0];
327 na.i_len = IEEE80211_NWID_LEN + 1;
328 if ((inwid = ioctl(s, SIOCG80211, (caddr_t)&na)) == -1) {
333 if (na.i_len <= IEEE80211_NWID_LEN)
336 len = IEEE80211_NWID_LEN + 1;
337 info->essid[len - 1] = '\0';
342 info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
344 memset(&na, 0, sizeof(na));
345 strlcpy(na.i_name, interface, sizeof(na.i_name));
346 na.i_type = IEEE80211_IOC_BSSID;
348 na.i_len = sizeof(bssid);
350 if (ioctl(s, SIOCG80211, (caddr_t)&na) == -1) {
355 memcpy(u.req.is_u.macaddr, bssid, sizeof(bssid));
356 memset(&na, 0, sizeof(na));
357 strlcpy(na.i_name, interface, sizeof(na.i_name));
358 na.i_type = IEEE80211_IOC_STA_INFO;
360 na.i_len = sizeof(u);
362 if (ioctl(s, SIOCG80211, (caddr_t)&na) == -1) {
368 if (na.i_len >= sizeof(u.req)) {
370 * Just use the first BSSID returned even if there are
371 * multiple APs sharing the same BSSID.
373 info->signal_level = u.req.info[0].isi_rssi / 2 +
374 u.req.info[0].isi_noise;
375 info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
376 info->noise_level = u.req.info[0].isi_noise;
377 info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
384 struct ieee80211_bssid bssid;
385 struct ieee80211_nwid nwid;
386 struct ieee80211_nodereq nr;
388 struct ether_addr ea;
390 int s, len, ibssid, inwid;
391 u_int8_t zero_bssid[IEEE80211_ADDR_LEN];
393 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
396 memset(&ifr, 0, sizeof(ifr));
397 ifr.ifr_data = (caddr_t)&nwid;
398 (void)strlcpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
399 inwid = ioctl(s, SIOCG80211NWID, (caddr_t)&ifr);
401 memset(&bssid, 0, sizeof(bssid));
402 strlcpy(bssid.i_name, interface, sizeof(bssid.i_name));
403 ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
405 if (ibssid != 0 || inwid != 0) {
412 if (nwid.i_len <= IEEE80211_NWID_LEN)
413 len = nwid.i_len + 1;
415 len = IEEE80211_NWID_LEN + 1;
417 strncpy(&info->essid[0], nwid.i_nwid, len);
418 info->essid[IW_ESSID_MAX_SIZE] = '\0';
419 info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
422 /* Signal strength */
424 memset(&zero_bssid, 0, sizeof(zero_bssid));
425 if (ibssid == 0 && memcmp(bssid.i_bssid, zero_bssid, IEEE80211_ADDR_LEN) != 0) {
426 memcpy(&ea.ether_addr_octet, bssid.i_bssid, sizeof(ea.ether_addr_octet));
428 bzero(&nr, sizeof(nr));
429 bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
430 strlcpy(nr.nr_ifname, interface, sizeof(nr.nr_ifname));
432 if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
433 info->signal_level = nr.nr_rssi;
435 info->signal_level_max = nr.nr_max_rssi;
437 info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
448 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
450 char *outwalk = buffer;
451 wireless_info_t info;
455 const char *ip_address = get_ip_addr(interface);
456 if (ip_address == NULL) {
457 START_COLOR("color_bad");
458 outwalk += sprintf(outwalk, "%s", format_down);
462 if (get_wireless_info(interface, &info)) {
464 if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY)
465 START_COLOR((info.quality < info.quality_average ? "color_degraded" : "color_good"));
467 START_COLOR((BEGINS_WITH(ip_address, "no IP") ? "color_degraded" : "color_good"));
470 START_COLOR("color_bad");
473 for (; *walk != '\0'; walk++) {
475 *(outwalk++) = *walk;
479 if (BEGINS_WITH(walk + 1, "quality")) {
480 if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) {
481 if (info.quality_max)
482 outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.quality, info.quality_max));
484 outwalk += sprintf(outwalk, "%d", info.quality);
488 walk += strlen("quality");
491 if (BEGINS_WITH(walk + 1, "signal")) {
492 if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) {
493 if (info.signal_level_max)
494 outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.signal_level, info.signal_level_max));
496 outwalk += sprintf(outwalk, "%d dBm", info.signal_level);
500 walk += strlen("signal");
503 if (BEGINS_WITH(walk + 1, "noise")) {
504 if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) {
505 if (info.noise_level_max)
506 outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.noise_level, info.noise_level_max));
508 outwalk += sprintf(outwalk, "%d dBm", info.noise_level);
512 walk += strlen("noise");
515 if (BEGINS_WITH(walk + 1, "essid")) {
516 if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID)
517 outwalk += sprintf(outwalk, "%s", info.essid);
520 walk += strlen("essid");
523 if (BEGINS_WITH(walk + 1, "frequency")) {
524 if (info.flags & WIRELESS_INFO_FLAG_HAS_FREQUENCY)
525 outwalk += sprintf(outwalk, "%1.1f GHz", info.frequency / 1e9);
528 walk += strlen("frequency");
531 if (BEGINS_WITH(walk + 1, "ip")) {
532 outwalk += sprintf(outwalk, "%s", ip_address);
533 walk += strlen("ip");
537 if (BEGINS_WITH(walk + 1, "bitrate")) {
540 print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate);
542 outwalk += sprintf(outwalk, "%s", br_buffer);
543 walk += strlen("bitrate");
550 OUTPUT_FULL_TEXT(buffer);