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