]> git.sur5r.net Git - i3/i3status/blob - src/print_wireless_info.c
48d81d4c9d70427864655a989736b7c3a5873bd6
[i3/i3status] / src / print_wireless_info.c
1 // vim:ts=4:sw=4:expandtab
2 #include <stdio.h>
3 #include <string.h>
4 #include <yajl/yajl_gen.h>
5 #include <yajl/yajl_version.h>
6
7 #ifdef LINUX
8 #include <errno.h>
9 #include <net/if.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
16 #endif
17
18 #ifdef __FreeBSD__
19 #include <sys/param.h>
20 #include <sys/ioctl.h>
21 #include <sys/socket.h>
22 #include <ifaddrs.h>
23 #include <net/if.h>
24 #include <net/if_media.h>
25 #include <net80211/ieee80211.h>
26 #include <net80211/ieee80211_ioctl.h>
27 #include <unistd.h>
28 #define IW_ESSID_MAX_SIZE IEEE80211_NWID_LEN
29 #endif
30
31 #ifdef __DragonFly__
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <ifaddrs.h>
36 #include <net/if.h>
37 #include <net/if_media.h>
38 #include <netproto/802_11/ieee80211.h>
39 #include <netproto/802_11/ieee80211_ioctl.h>
40 #include <unistd.h>
41 #define IW_ESSID_MAX_SIZE IEEE80211_NWID_LEN
42 #endif
43
44 #ifdef __OpenBSD__
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
47 #include <net/if.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>
53 #endif
54
55 #include "i3status.h"
56
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)
62
63 #define PERCENT_VALUE(value, total) ((int)(value * 100 / (float)total + 0.5f))
64
65 typedef struct {
66     int flags;
67     char essid[IW_ESSID_MAX_SIZE + 1];
68     uint8_t bssid[ETH_ALEN];
69     int quality;
70     int quality_max;
71     int quality_average;
72     int signal_level;
73     int signal_level_max;
74     int noise_level;
75     int noise_level_max;
76     int bitrate;
77     double frequency;
78 } wireless_info_t;
79
80 // Like iw_print_bitrate, but without the dependency on libiw.
81 static void print_bitrate(char *buffer, int buflen, int bitrate) {
82     const int kilo = 1e3;
83     const int mega = 1e6;
84     const int giga = 1e9;
85
86     const double rate = bitrate;
87     char scale;
88     int divisor;
89
90     if (rate >= giga) {
91         scale = 'G';
92         divisor = giga;
93     } else if (rate >= mega) {
94         scale = 'M';
95         divisor = mega;
96     } else {
97         scale = 'k';
98         divisor = kilo;
99     }
100     snprintf(buffer, buflen, "%g %cb/s", rate / divisor, scale);
101 }
102
103 // Based on NetworkManager/src/platform/wifi/wifi-utils-nl80211.c
104 static uint32_t nl80211_xbm_to_percent(int32_t xbm, uint32_t divisor) {
105 #define NOISE_FLOOR_DBM -90
106 #define SIGNAL_MAX_DBM -20
107
108     xbm /= divisor;
109     if (xbm < NOISE_FLOOR_DBM)
110         xbm = NOISE_FLOOR_DBM;
111     if (xbm > SIGNAL_MAX_DBM)
112         xbm = SIGNAL_MAX_DBM;
113
114     return 100 - 70 * (((float)SIGNAL_MAX_DBM - (float)xbm) / ((float)SIGNAL_MAX_DBM - (float)NOISE_FLOOR_DBM));
115 }
116
117 // Based on NetworkManager/src/platform/wifi/wifi-utils-nl80211.c
118 static void find_ssid(uint8_t *ies, uint32_t ies_len, uint8_t **ssid, uint32_t *ssid_len) {
119 #define WLAN_EID_SSID 0
120     *ssid = NULL;
121     *ssid_len = 0;
122
123     while (ies_len > 2 && ies[0] != WLAN_EID_SSID) {
124         ies_len -= ies[1] + 2;
125         ies += ies[1] + 2;
126     }
127     if (ies_len < 2)
128         return;
129     if (ies_len < (uint32_t)(2 + ies[1]))
130         return;
131
132     *ssid_len = ies[1];
133     *ssid = ies + 2;
134 }
135
136 static int gwi_sta_cb(struct nl_msg *msg, void *data) {
137     wireless_info_t *info = data;
138
139     struct nlattr *tb[NL80211_ATTR_MAX + 1];
140     struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
141     struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
142     struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
143     static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
144             [NL80211_STA_INFO_RX_BITRATE] = {.type = NLA_NESTED},
145     };
146
147     static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
148             [NL80211_RATE_INFO_BITRATE] = {.type = NLA_U16},
149     };
150
151     if (nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
152         return NL_SKIP;
153
154     if (tb[NL80211_ATTR_STA_INFO] == NULL)
155         return NL_SKIP;
156
157     if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, tb[NL80211_ATTR_STA_INFO], stats_policy))
158         return NL_SKIP;
159
160     if (sinfo[NL80211_STA_INFO_RX_BITRATE] == NULL)
161         return NL_SKIP;
162
163     if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy))
164         return NL_SKIP;
165
166     if (rinfo[NL80211_RATE_INFO_BITRATE] == NULL)
167         return NL_SKIP;
168
169     // NL80211_RATE_INFO_BITRATE is specified in units of 100 kbit/s, but iw
170     // used to specify bit/s, so we convert to use the same code path.
171     info->bitrate = (int)nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]) * 100 * 1000;
172
173     return NL_SKIP;
174 }
175
176 static int gwi_scan_cb(struct nl_msg *msg, void *data) {
177     wireless_info_t *info = data;
178     struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
179     struct nlattr *tb[NL80211_ATTR_MAX + 1];
180     struct nlattr *bss[NL80211_BSS_MAX + 1];
181     struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
182             [NL80211_BSS_FREQUENCY] = {.type = NLA_U32},
183             [NL80211_BSS_BSSID] = {.type = NLA_UNSPEC},
184             [NL80211_BSS_INFORMATION_ELEMENTS] = {.type = NLA_UNSPEC},
185             [NL80211_BSS_SIGNAL_MBM] = {.type = NLA_U32},
186             [NL80211_BSS_SIGNAL_UNSPEC] = {.type = NLA_U8},
187             [NL80211_BSS_STATUS] = {.type = NLA_U32},
188     };
189
190     if (nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
191         return NL_SKIP;
192
193     if (tb[NL80211_ATTR_BSS] == NULL)
194         return NL_SKIP;
195
196     if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], bss_policy))
197         return NL_SKIP;
198
199     if (bss[NL80211_BSS_STATUS] == NULL)
200         return NL_SKIP;
201
202     const uint32_t status = nla_get_u32(bss[NL80211_BSS_STATUS]);
203
204     if (status != NL80211_BSS_STATUS_ASSOCIATED &&
205         status != NL80211_BSS_STATUS_IBSS_JOINED)
206         return NL_SKIP;
207
208     if (bss[NL80211_BSS_BSSID] == NULL)
209         return NL_SKIP;
210
211     memcpy(info->bssid, nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
212
213     if (bss[NL80211_BSS_FREQUENCY]) {
214         info->flags |= WIRELESS_INFO_FLAG_HAS_FREQUENCY;
215         info->frequency = (double)nla_get_u32(bss[NL80211_BSS_FREQUENCY]) * 1e6;
216     }
217
218     if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
219         info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
220         info->signal_level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
221         info->signal_level_max = 100;
222
223         info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
224         info->quality = info->signal_level;
225         info->quality_max = 100;
226         info->quality_average = 50;
227     }
228
229     if (bss[NL80211_BSS_SIGNAL_MBM]) {
230         info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
231         info->signal_level = (int)nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]) / 100;
232
233         info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
234         info->quality = nl80211_xbm_to_percent(nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]), 100);
235         info->quality_max = 100;
236         info->quality_average = 50;
237     }
238
239     if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
240         uint8_t *ssid;
241         uint32_t ssid_len;
242
243         find_ssid(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
244                   nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
245                   &ssid, &ssid_len);
246         if (ssid && ssid_len) {
247             info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
248             snprintf(info->essid, sizeof(info->essid), "%.*s", ssid_len, ssid);
249         }
250     }
251
252     return NL_SKIP;
253 }
254
255 static int get_wireless_info(const char *interface, wireless_info_t *info) {
256     memset(info, 0, sizeof(wireless_info_t));
257
258 #ifdef LINUX
259     struct nl_sock *sk = nl_socket_alloc();
260     if (genl_connect(sk) != 0)
261         goto error1;
262
263     if (nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, gwi_scan_cb, info) < 0)
264         goto error1;
265
266     const int nl80211_id = genl_ctrl_resolve(sk, "nl80211");
267     if (nl80211_id < 0)
268         goto error1;
269
270     const unsigned int ifidx = if_nametoindex(interface);
271     if (ifidx == 0)
272         goto error1;
273
274     struct nl_msg *msg = NULL;
275     if ((msg = nlmsg_alloc()) == NULL)
276         goto error1;
277
278     if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, nl80211_id, 0, NLM_F_DUMP, NL80211_CMD_GET_SCAN, 0) ||
279         nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifidx) < 0)
280         goto error2;
281
282     if (nl_send_sync(sk, msg) < 0)
283         // nl_send_sync calls nlmsg_free()
284         goto error1;
285     msg = NULL;
286
287     if (nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, gwi_sta_cb, info) < 0)
288         goto error1;
289
290     if ((msg = nlmsg_alloc()) == NULL)
291         goto error1;
292
293     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)
294         goto error2;
295
296     if (nl_send_sync(sk, msg) < 0)
297         // nl_send_sync calls nlmsg_free()
298         goto error1;
299     msg = NULL;
300
301     nl_socket_free(sk);
302     return 1;
303
304 error2:
305     nlmsg_free(msg);
306 error1:
307     nl_socket_free(sk);
308     return 0;
309
310 #endif
311 #if defined(__FreeBSD__) || defined(__DragonFly__)
312     int s, inwid;
313     union {
314         struct ieee80211req_sta_req req;
315         uint8_t buf[24 * 1024];
316     } u;
317     struct ieee80211req na;
318     char bssid[IEEE80211_ADDR_LEN];
319     size_t len;
320
321     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
322         return (0);
323
324     memset(&na, 0, sizeof(na));
325     strlcpy(na.i_name, interface, sizeof(na.i_name));
326     na.i_type = IEEE80211_IOC_SSID;
327     na.i_data = &info->essid[0];
328     na.i_len = IEEE80211_NWID_LEN + 1;
329     if ((inwid = ioctl(s, SIOCG80211, (caddr_t)&na)) == -1) {
330         close(s);
331         return (0);
332     }
333     if (inwid == 0) {
334         if (na.i_len <= IEEE80211_NWID_LEN)
335             len = na.i_len + 1;
336         else
337             len = IEEE80211_NWID_LEN + 1;
338         info->essid[len - 1] = '\0';
339     } else {
340         close(s);
341         return (0);
342     }
343     info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
344
345     memset(&na, 0, sizeof(na));
346     strlcpy(na.i_name, interface, sizeof(na.i_name));
347     na.i_type = IEEE80211_IOC_BSSID;
348     na.i_data = bssid;
349     na.i_len = sizeof(bssid);
350
351     if (ioctl(s, SIOCG80211, (caddr_t)&na) == -1) {
352         close(s);
353         return (0);
354     }
355
356     memcpy(u.req.is_u.macaddr, bssid, sizeof(bssid));
357     memset(&na, 0, sizeof(na));
358     strlcpy(na.i_name, interface, sizeof(na.i_name));
359     na.i_type = IEEE80211_IOC_STA_INFO;
360     na.i_data = &u;
361     na.i_len = sizeof(u);
362
363     if (ioctl(s, SIOCG80211, (caddr_t)&na) == -1) {
364         close(s);
365         return (0);
366     }
367
368     close(s);
369     if (na.i_len >= sizeof(u.req)) {
370         /*
371          * Just use the first BSSID returned even if there are
372          * multiple APs sharing the same BSSID.
373          */
374         info->signal_level = u.req.info[0].isi_rssi / 2 +
375                              u.req.info[0].isi_noise;
376         info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
377         info->noise_level = u.req.info[0].isi_noise;
378         info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
379     }
380
381     return 1;
382 #endif
383 #ifdef __OpenBSD__
384     struct ifreq ifr;
385     struct ieee80211_bssid bssid;
386     struct ieee80211_nwid nwid;
387     struct ieee80211_nodereq nr;
388
389     struct ether_addr ea;
390
391     int s, len, ibssid, inwid;
392     u_int8_t zero_bssid[IEEE80211_ADDR_LEN];
393
394     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
395         return (0);
396
397     memset(&ifr, 0, sizeof(ifr));
398     ifr.ifr_data = (caddr_t)&nwid;
399     (void)strlcpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
400     inwid = ioctl(s, SIOCG80211NWID, (caddr_t)&ifr);
401
402     memset(&bssid, 0, sizeof(bssid));
403     strlcpy(bssid.i_name, interface, sizeof(bssid.i_name));
404     ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
405
406     if (ibssid != 0 || inwid != 0) {
407         close(s);
408         return 0;
409     }
410
411     /* NWID */
412     {
413         if (nwid.i_len <= IEEE80211_NWID_LEN)
414             len = nwid.i_len + 1;
415         else
416             len = IEEE80211_NWID_LEN + 1;
417
418         strncpy(&info->essid[0], nwid.i_nwid, len);
419         info->essid[IW_ESSID_MAX_SIZE] = '\0';
420         info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
421     }
422
423     /* Signal strength */
424     {
425         memset(&zero_bssid, 0, sizeof(zero_bssid));
426         if (ibssid == 0 && memcmp(bssid.i_bssid, zero_bssid, IEEE80211_ADDR_LEN) != 0) {
427             memcpy(&ea.ether_addr_octet, bssid.i_bssid, sizeof(ea.ether_addr_octet));
428
429             bzero(&nr, sizeof(nr));
430             bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
431             strlcpy(nr.nr_ifname, interface, sizeof(nr.nr_ifname));
432
433             if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
434                 info->signal_level = nr.nr_rssi;
435                 if (nr.nr_max_rssi)
436                     info->signal_level_max = nr.nr_max_rssi;
437
438                 info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
439             }
440         }
441     }
442
443     close(s);
444     return 1;
445 #endif
446     return 0;
447 }
448
449 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
450     const char *walk;
451     char *outwalk = buffer;
452     wireless_info_t info;
453
454     INSTANCE(interface);
455
456     const char *ip_address = get_ip_addr(interface);
457     if (ip_address == NULL) {
458         START_COLOR("color_bad");
459         outwalk += sprintf(outwalk, "%s", format_down);
460         goto out;
461     }
462
463     if (get_wireless_info(interface, &info)) {
464         walk = format_up;
465         if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY)
466             START_COLOR((info.quality < info.quality_average ? "color_degraded" : "color_good"));
467         else
468             START_COLOR((BEGINS_WITH(ip_address, "no IP") ? "color_degraded" : "color_good"));
469     } else {
470         walk = format_down;
471         START_COLOR("color_bad");
472     }
473
474     for (; *walk != '\0'; walk++) {
475         if (*walk != '%') {
476             *(outwalk++) = *walk;
477             continue;
478         }
479
480         if (BEGINS_WITH(walk + 1, "quality")) {
481             if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) {
482                 if (info.quality_max)
483                     outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.quality, info.quality_max));
484                 else
485                     outwalk += sprintf(outwalk, "%d", info.quality);
486             } else {
487                 *(outwalk++) = '?';
488             }
489             walk += strlen("quality");
490         }
491
492         if (BEGINS_WITH(walk + 1, "signal")) {
493             if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) {
494                 if (info.signal_level_max)
495                     outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.signal_level, info.signal_level_max));
496                 else
497                     outwalk += sprintf(outwalk, "%d dBm", info.signal_level);
498             } else {
499                 *(outwalk++) = '?';
500             }
501             walk += strlen("signal");
502         }
503
504         if (BEGINS_WITH(walk + 1, "noise")) {
505             if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) {
506                 if (info.noise_level_max)
507                     outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.noise_level, info.noise_level_max));
508                 else
509                     outwalk += sprintf(outwalk, "%d dBm", info.noise_level);
510             } else {
511                 *(outwalk++) = '?';
512             }
513             walk += strlen("noise");
514         }
515
516         if (BEGINS_WITH(walk + 1, "essid")) {
517             if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID)
518                 outwalk += sprintf(outwalk, "%s", info.essid);
519             else
520                 *(outwalk++) = '?';
521             walk += strlen("essid");
522         }
523
524         if (BEGINS_WITH(walk + 1, "frequency")) {
525             if (info.flags & WIRELESS_INFO_FLAG_HAS_FREQUENCY)
526                 outwalk += sprintf(outwalk, "%1.1f GHz", info.frequency / 1e9);
527             else
528                 *(outwalk++) = '?';
529             walk += strlen("frequency");
530         }
531
532         if (BEGINS_WITH(walk + 1, "ip")) {
533             outwalk += sprintf(outwalk, "%s", ip_address);
534             walk += strlen("ip");
535         }
536
537 #ifdef LINUX
538         if (BEGINS_WITH(walk + 1, "bitrate")) {
539             char br_buffer[128];
540
541             print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate);
542
543             outwalk += sprintf(outwalk, "%s", br_buffer);
544             walk += strlen("bitrate");
545         }
546 #endif
547     }
548
549 out:
550     END_COLOR;
551     OUTPUT_FULL_TEXT(buffer);
552 }