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