nl80211: simplify iterating over phy's devices
[project/iwinfo.git] / iwinfo_nl80211.c
1 /*
2 * iwinfo - Wireless Information Library - NL80211 Backend
3 *
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
5 *
6 * The iwinfo library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * The iwinfo library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with the iwinfo library. If not, see http://www.gnu.org/licenses/.
17 *
18 * The signal handling code is derived from the official madwifi tools,
19 * wlanconfig.c in particular. The encryption property handling was
20 * inspired by the hostapd madwifi driver.
21 *
22 * Parts of this code are derived from the Linux iw utility.
23 */
24
25 #include <sys/stat.h>
26 #include <limits.h>
27 #include <glob.h>
28 #include <fnmatch.h>
29 #include <stdarg.h>
30 #include <stdlib.h>
31
32 #include "iwinfo_nl80211.h"
33
34 #define min(x, y) ((x) < (y)) ? (x) : (y)
35
36 #define BIT(x) (1ULL<<(x))
37
38 static struct nl80211_state *nls = NULL;
39
40 static void nl80211_close(void)
41 {
42 if (nls)
43 {
44 if (nls->nlctrl)
45 genl_family_put(nls->nlctrl);
46
47 if (nls->nl80211)
48 genl_family_put(nls->nl80211);
49
50 if (nls->nl_sock)
51 nl_socket_free(nls->nl_sock);
52
53 if (nls->nl_cache)
54 nl_cache_free(nls->nl_cache);
55
56 free(nls);
57 nls = NULL;
58 }
59 }
60
61 static int nl80211_init(void)
62 {
63 int err, fd;
64
65 if (!nls)
66 {
67 nls = malloc(sizeof(struct nl80211_state));
68 if (!nls) {
69 err = -ENOMEM;
70 goto err;
71 }
72
73 memset(nls, 0, sizeof(*nls));
74
75 nls->nl_sock = nl_socket_alloc();
76 if (!nls->nl_sock) {
77 err = -ENOMEM;
78 goto err;
79 }
80
81 if (genl_connect(nls->nl_sock)) {
82 err = -ENOLINK;
83 goto err;
84 }
85
86 fd = nl_socket_get_fd(nls->nl_sock);
87 if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0) {
88 err = -EINVAL;
89 goto err;
90 }
91
92 if (genl_ctrl_alloc_cache(nls->nl_sock, &nls->nl_cache)) {
93 err = -ENOMEM;
94 goto err;
95 }
96
97 nls->nl80211 = genl_ctrl_search_by_name(nls->nl_cache, "nl80211");
98 if (!nls->nl80211) {
99 err = -ENOENT;
100 goto err;
101 }
102
103 nls->nlctrl = genl_ctrl_search_by_name(nls->nl_cache, "nlctrl");
104 if (!nls->nlctrl) {
105 err = -ENOENT;
106 goto err;
107 }
108 }
109
110 return 0;
111
112
113 err:
114 nl80211_close();
115 return err;
116 }
117
118 static int nl80211_readint(const char *path)
119 {
120 int fd;
121 int rv = -1;
122 char buffer[16];
123
124 if ((fd = open(path, O_RDONLY)) > -1)
125 {
126 if (read(fd, buffer, sizeof(buffer)) > 0)
127 rv = atoi(buffer);
128
129 close(fd);
130 }
131
132 return rv;
133 }
134
135 static int nl80211_readstr(const char *path, char *buffer, int length)
136 {
137 int fd;
138 int rv = -1;
139
140 if ((fd = open(path, O_RDONLY)) > -1)
141 {
142 if ((rv = read(fd, buffer, length - 1)) > 0)
143 {
144 if (buffer[rv - 1] == '\n')
145 rv--;
146
147 buffer[rv] = 0;
148 }
149
150 close(fd);
151 }
152
153 return rv;
154 }
155
156 static int nl80211_get_band(int nla_type)
157 {
158 switch (nla_type)
159 {
160 case NL80211_BAND_2GHZ:
161 return IWINFO_BAND_24;
162 case NL80211_BAND_5GHZ:
163 return IWINFO_BAND_5;
164 case NL80211_BAND_6GHZ:
165 return IWINFO_BAND_6;
166 case NL80211_BAND_60GHZ:
167 return IWINFO_BAND_60;
168 }
169
170 return 0;
171 }
172
173
174 static int nl80211_msg_error(struct sockaddr_nl *nla,
175 struct nlmsgerr *err, void *arg)
176 {
177 int *ret = arg;
178 *ret = err->error;
179 return NL_STOP;
180 }
181
182 static int nl80211_msg_finish(struct nl_msg *msg, void *arg)
183 {
184 int *ret = arg;
185 *ret = 0;
186 return NL_SKIP;
187 }
188
189 static int nl80211_msg_ack(struct nl_msg *msg, void *arg)
190 {
191 int *ret = arg;
192 *ret = 0;
193 return NL_STOP;
194 }
195
196 static int nl80211_msg_response(struct nl_msg *msg, void *arg)
197 {
198 return NL_SKIP;
199 }
200
201 static void nl80211_free(struct nl80211_msg_conveyor *cv)
202 {
203 if (cv)
204 {
205 if (cv->cb)
206 nl_cb_put(cv->cb);
207
208 if (cv->msg)
209 nlmsg_free(cv->msg);
210
211 cv->cb = NULL;
212 cv->msg = NULL;
213 }
214 }
215
216 static struct nl80211_msg_conveyor * nl80211_new(struct genl_family *family,
217 int cmd, int flags)
218 {
219 static struct nl80211_msg_conveyor cv;
220
221 struct nl_msg *req = NULL;
222 struct nl_cb *cb = NULL;
223
224 req = nlmsg_alloc();
225 if (!req)
226 goto err;
227
228 cb = nl_cb_alloc(NL_CB_DEFAULT);
229 if (!cb)
230 goto err;
231
232 genlmsg_put(req, 0, 0, genl_family_get_id(family), 0, flags, cmd, 0);
233
234 cv.msg = req;
235 cv.cb = cb;
236
237 return &cv;
238
239 err:
240 if (req)
241 nlmsg_free(req);
242
243 return NULL;
244 }
245
246 static struct nl80211_msg_conveyor * nl80211_ctl(int cmd, int flags)
247 {
248 if (nl80211_init() < 0)
249 return NULL;
250
251 return nl80211_new(nls->nlctrl, cmd, flags);
252 }
253
254 static const char *nl80211_phy_path_str(const char *phyname)
255 {
256 static char path[PATH_MAX];
257 const char *prefix = "/sys/devices/";
258 int prefix_len = strlen(prefix);
259 int buf_len, offset;
260 struct dirent *e;
261 char buf[512], *link;
262 int phy_idx;
263 int seq = 0;
264 DIR *d;
265
266 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", phyname);
267 phy_idx = nl80211_readint(buf);
268 if (phy_idx < 0)
269 return NULL;
270
271 buf_len = snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/device", phyname);
272 link = realpath(buf, path);
273 if (!link)
274 return NULL;
275
276 if (strncmp(link, prefix, prefix_len) != 0)
277 return NULL;
278
279 link += prefix_len;
280
281 prefix = "platform/";
282 prefix_len = strlen(prefix);
283 if (!strncmp(link, prefix, prefix_len) && strstr(link, "/pci"))
284 link += prefix_len;
285
286 snprintf(buf + buf_len, sizeof(buf) - buf_len, "/ieee80211");
287 d = opendir(buf);
288 if (!d)
289 return link;
290
291 while ((e = readdir(d)) != NULL) {
292 int cur_idx;
293
294 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", e->d_name);
295 cur_idx = nl80211_readint(buf);
296 if (cur_idx < 0)
297 continue;
298
299 if (cur_idx >= phy_idx)
300 continue;
301
302 seq++;
303 }
304
305 closedir(d);
306
307 if (!seq)
308 return link;
309
310 offset = link - path + strlen(link);
311 snprintf(path + offset, sizeof(path) - offset, "+%d", seq);
312
313 return link;
314 }
315
316 static int nl80211_phy_idx_from_path(const char *path)
317 {
318 char buf[512];
319 struct dirent *e;
320 const char *cur_path;
321 int cur_path_len;
322 int path_len;
323 int idx = -1;
324 DIR *d;
325
326 if (!path)
327 return -1;
328
329 path_len = strlen(path);
330 if (!path_len)
331 return -1;
332
333 d = opendir("/sys/class/ieee80211");
334 if (!d)
335 return -1;
336
337 while ((e = readdir(d)) != NULL) {
338 cur_path = nl80211_phy_path_str(e->d_name);
339 if (!cur_path)
340 continue;
341
342 cur_path_len = strlen(cur_path);
343 if (cur_path_len < path_len)
344 continue;
345
346 if (strcmp(cur_path + cur_path_len - path_len, path) != 0)
347 continue;
348
349 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", e->d_name);
350 idx = nl80211_readint(buf);
351
352 if (idx >= 0)
353 break;
354 }
355
356 closedir(d);
357
358 return idx;
359 }
360
361 static int nl80211_phy_idx_from_macaddr(const char *opt)
362 {
363 char buf[128];
364 int i, idx = -1;
365 glob_t gl;
366
367 if (!opt)
368 return -1;
369
370 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/*"); /**/
371 if (glob(buf, 0, NULL, &gl))
372 return -1;
373
374 for (i = 0; i < gl.gl_pathc; i++)
375 {
376 snprintf(buf, sizeof(buf), "%s/macaddress", gl.gl_pathv[i]);
377 if (nl80211_readstr(buf, buf, sizeof(buf)) <= 0)
378 continue;
379
380 if (fnmatch(opt, buf, FNM_CASEFOLD))
381 continue;
382
383 snprintf(buf, sizeof(buf), "%s/index", gl.gl_pathv[i]);
384 if ((idx = nl80211_readint(buf)) > -1)
385 break;
386 }
387
388 globfree(&gl);
389
390 return idx;
391 }
392
393 static int nl80211_phy_idx_from_phy(const char *opt)
394 {
395 char buf[128];
396
397 if (!opt)
398 return -1;
399
400 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", opt);
401 return nl80211_readint(buf);
402 }
403
404 static int nl80211_phy_idx_from_uci(const char *name)
405 {
406 struct uci_section *s;
407 const char *opt;
408 int idx = -1;
409
410 s = iwinfo_uci_get_radio(name, "mac80211");
411 if (!s)
412 goto out;
413
414 opt = uci_lookup_option_string(uci_ctx, s, "path");
415 idx = nl80211_phy_idx_from_path(opt);
416 if (idx >= 0)
417 goto out;
418
419 opt = uci_lookup_option_string(uci_ctx, s, "macaddr");
420 idx = nl80211_phy_idx_from_macaddr(opt);
421 if (idx >= 0)
422 goto out;
423
424 opt = uci_lookup_option_string(uci_ctx, s, "phy");
425 idx = nl80211_phy_idx_from_phy(opt);
426
427 out:
428 iwinfo_uci_free();
429 return idx;
430 }
431
432 static bool nl80211_is_ifname(const char *name)
433 {
434 struct stat st;
435 char buffer[PATH_MAX];
436
437 snprintf(buffer, sizeof(buffer), "/sys/class/net/%s", name);
438 return !lstat(buffer, &st);
439 }
440
441 static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname,
442 int cmd, int flags)
443 {
444 unsigned int ifidx = 0;
445 int phyidx = -1;
446 struct nl80211_msg_conveyor *cv;
447
448 if (ifname == NULL)
449 return NULL;
450
451 if (nl80211_init() < 0)
452 return NULL;
453
454 if (!strncmp(ifname, "mon.", 4))
455 ifidx = if_nametoindex(&ifname[4]);
456 else if (nl80211_is_ifname(ifname))
457 ifidx = if_nametoindex(ifname);
458 else
459 {
460 phyidx = nl80211_phy_idx_from_phy(ifname);
461 if (phyidx < 0)
462 phyidx = nl80211_phy_idx_from_uci(ifname);
463 }
464
465 /* Valid ifidx must be greater than 0 */
466 if ((ifidx <= 0) && (phyidx < 0))
467 return NULL;
468
469 cv = nl80211_new(nls->nl80211, cmd, flags);
470 if (!cv)
471 return NULL;
472
473 if (ifidx > 0)
474 NLA_PUT_U32(cv->msg, NL80211_ATTR_IFINDEX, ifidx);
475 else if (phyidx > -1)
476 NLA_PUT_U32(cv->msg, NL80211_ATTR_WIPHY, phyidx);
477
478 return cv;
479
480 nla_put_failure:
481 nl80211_free(cv);
482 return NULL;
483 }
484
485 static int nl80211_send(struct nl80211_msg_conveyor *cv,
486 int (*cb_func)(struct nl_msg *, void *),
487 void *cb_arg)
488 {
489 static struct nl80211_msg_conveyor rcv;
490 int err;
491
492 if (cb_func)
493 nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, cb_func, cb_arg);
494 else
495 nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_msg_response, &rcv);
496
497 err = nl_send_auto_complete(nls->nl_sock, cv->msg);
498
499 if (err < 0)
500 goto out;
501
502 err = 1;
503
504 nl_cb_err(cv->cb, NL_CB_CUSTOM, nl80211_msg_error, &err);
505 nl_cb_set(cv->cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_msg_finish, &err);
506 nl_cb_set(cv->cb, NL_CB_ACK, NL_CB_CUSTOM, nl80211_msg_ack, &err);
507
508 while (err > 0)
509 nl_recvmsgs(nls->nl_sock, cv->cb);
510
511 out:
512 nl80211_free(cv);
513 return err;
514 }
515
516 static int nl80211_request(const char *ifname, int cmd, int flags,
517 int (*cb_func)(struct nl_msg *, void *),
518 void *cb_arg)
519 {
520 struct nl80211_msg_conveyor *cv;
521
522 cv = nl80211_msg(ifname, cmd, flags);
523
524 if (!cv)
525 return -ENOMEM;
526
527 return nl80211_send(cv, cb_func, cb_arg);
528 }
529
530 static struct nlattr ** nl80211_parse(struct nl_msg *msg)
531 {
532 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
533 static struct nlattr *attr[NL80211_ATTR_MAX + 1];
534
535 nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
536 genlmsg_attrlen(gnlh, 0), NULL);
537
538 return attr;
539 }
540
541 static int nl80211_get_protocol_features_cb(struct nl_msg *msg, void *arg)
542 {
543 uint32_t *features = arg;
544 struct nlattr **attr = nl80211_parse(msg);
545
546 if (attr[NL80211_ATTR_PROTOCOL_FEATURES])
547 *features = nla_get_u32(attr[NL80211_ATTR_PROTOCOL_FEATURES]);
548
549 return NL_SKIP;
550 }
551
552 static int nl80211_get_protocol_features(const char *ifname)
553 {
554 struct nl80211_msg_conveyor *req;
555 uint32_t features = 0;
556
557 req = nl80211_msg(ifname, NL80211_CMD_GET_PROTOCOL_FEATURES, 0);
558 if (req) {
559 nl80211_send(req, nl80211_get_protocol_features_cb, &features);
560 nl80211_free(req);
561 }
562
563 return features;
564 }
565
566 static int nl80211_subscribe_cb(struct nl_msg *msg, void *arg)
567 {
568 struct nl80211_group_conveyor *cv = arg;
569
570 struct nlattr **attr = nl80211_parse(msg);
571 struct nlattr *mgrpinfo[CTRL_ATTR_MCAST_GRP_MAX + 1];
572 struct nlattr *mgrp;
573 int mgrpidx;
574
575 if (!attr[CTRL_ATTR_MCAST_GROUPS])
576 return NL_SKIP;
577
578 nla_for_each_nested(mgrp, attr[CTRL_ATTR_MCAST_GROUPS], mgrpidx)
579 {
580 nla_parse(mgrpinfo, CTRL_ATTR_MCAST_GRP_MAX,
581 nla_data(mgrp), nla_len(mgrp), NULL);
582
583 if (mgrpinfo[CTRL_ATTR_MCAST_GRP_ID] &&
584 mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME] &&
585 !strncmp(nla_data(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME]),
586 cv->name, nla_len(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME])))
587 {
588 cv->id = nla_get_u32(mgrpinfo[CTRL_ATTR_MCAST_GRP_ID]);
589 break;
590 }
591 }
592
593 return NL_SKIP;
594 }
595
596 static int nl80211_subscribe(const char *family, const char *group)
597 {
598 struct nl80211_group_conveyor cv = { .name = group, .id = -ENOENT };
599 struct nl80211_msg_conveyor *req;
600 int err;
601
602 req = nl80211_ctl(CTRL_CMD_GETFAMILY, 0);
603 if (req)
604 {
605 NLA_PUT_STRING(req->msg, CTRL_ATTR_FAMILY_NAME, family);
606 err = nl80211_send(req, nl80211_subscribe_cb, &cv);
607
608 if (err)
609 return err;
610
611 return nl_socket_add_membership(nls->nl_sock, cv.id);
612
613 nla_put_failure:
614 nl80211_free(req);
615 }
616
617 return -ENOMEM;
618 }
619
620
621 static int nl80211_wait_cb(struct nl_msg *msg, void *arg)
622 {
623 struct nl80211_event_conveyor *cv = arg;
624 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
625
626 if (cv->wait[gnlh->cmd / 32] & (1 << (gnlh->cmd % 32)))
627 cv->recv = gnlh->cmd;
628
629 return NL_SKIP;
630 }
631
632 static int nl80211_wait_seq_check(struct nl_msg *msg, void *arg)
633 {
634 return NL_OK;
635 }
636
637 static int __nl80211_wait(const char *family, const char *group, ...)
638 {
639 struct nl80211_event_conveyor cv = { };
640 struct nl_cb *cb;
641 int err = 0;
642 int cmd;
643 va_list ap;
644
645 if (nl80211_subscribe(family, group))
646 return -ENOENT;
647
648 cb = nl_cb_alloc(NL_CB_DEFAULT);
649
650 if (!cb)
651 return -ENOMEM;
652
653 nl_cb_err(cb, NL_CB_CUSTOM, nl80211_msg_error, &err);
654 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, nl80211_wait_seq_check, NULL);
655 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_wait_cb, &cv );
656
657 va_start(ap, group);
658
659 for (cmd = va_arg(ap, int); cmd != 0; cmd = va_arg(ap, int))
660 cv.wait[cmd / 32] |= (1 << (cmd % 32));
661
662 va_end(ap);
663
664 while (!cv.recv && !err)
665 nl_recvmsgs(nls->nl_sock, cb);
666
667 nl_cb_put(cb);
668
669 return err;
670 }
671
672 #define nl80211_wait(family, group, ...) \
673 __nl80211_wait(family, group, __VA_ARGS__, 0)
674
675 /* This is linux's ieee80211_freq_khz_to_channel() which is:
676 * SPDX-License-Identifier: GPL-2.0
677 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
678 * Copyright 2013-2014 Intel Mobile Communications GmbH
679 * Copyright 2017 Intel Deutschland GmbH
680 * Copyright (C) 2018-2022 Intel Corporation
681 */
682 static int nl80211_freq2channel(int freq)
683 {
684 if (freq == 2484)
685 return 14;
686 else if (freq < 2484)
687 return (freq - 2407) / 5;
688 else if (freq >= 4910 && freq <= 4980)
689 return (freq - 4000) / 5;
690 else if (freq < 5925)
691 return (freq - 5000) / 5;
692 else if (freq == 5935)
693 return 2;
694 else if (freq <= 45000) /* DMG band lower limit */
695 /* see 802.11ax D6.1 27.3.22.2 */
696 return (freq - 5950) / 5;
697 else if (freq >= 58320 && freq <= 70200)
698 return (freq - 56160) / 2160;
699 else
700 return 0;
701 }
702
703 /* This is linux's ieee80211_channel_to_freq_khz() which is:
704 * SPDX-License-Identifier: GPL-2.0
705 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
706 * Copyright 2013-2014 Intel Mobile Communications GmbH
707 * Copyright 2017 Intel Deutschland GmbH
708 * Copyright (C) 2018-2022 Intel Corporation
709 */
710 static int nl80211_channel2freq(int channel, const char *band, bool ax)
711 {
712 if (channel < 1)
713 return 0;
714
715 if (!band || band[0] != 'a')
716 {
717 if (channel == 14)
718 return 2484;
719 else if (channel < 14)
720 return (channel * 5) + 2407;
721 }
722 else if (strcmp(band, "ad") == 0)
723 {
724 if (channel < 7)
725 return 56160 + 2160 * channel;
726 }
727 else if (ax)
728 {
729 if (channel == 2)
730 return 5935;
731 if (channel < 233)
732 return (channel * 5) + 5950;
733 }
734 else
735 {
736 if (channel >= 182 && channel <= 196)
737 return (channel * 5) + 4000;
738 else
739 return (channel * 5) + 5000;
740 }
741
742 return 0;
743 }
744
745 static uint8_t nl80211_freq2band(int freq)
746 {
747 if (freq >= 2412 && freq <= 2484)
748 return IWINFO_BAND_24;
749 else if (freq >= 5160 && freq <= 5885)
750 return IWINFO_BAND_5;
751 else if (freq >= 5925 && freq <= 7125)
752 return IWINFO_BAND_6;
753 else if (freq >= 58320 && freq <= 69120)
754 return IWINFO_BAND_60;
755
756 return 0;
757 }
758
759 static int nl80211_phyname_cb(struct nl_msg *msg, void *arg)
760 {
761 char *buf = arg;
762 struct nlattr **attr = nl80211_parse(msg);
763
764 if (attr[NL80211_ATTR_WIPHY_NAME])
765 memcpy(buf, nla_data(attr[NL80211_ATTR_WIPHY_NAME]),
766 nla_len(attr[NL80211_ATTR_WIPHY_NAME]));
767 else
768 buf[0] = 0;
769
770 return NL_SKIP;
771 }
772
773 static char * nl80211_ifname2phy(const char *ifname)
774 {
775 static char phy[32] = { 0 };
776
777 memset(phy, 0, sizeof(phy));
778
779 nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
780 nl80211_phyname_cb, phy);
781
782 return phy[0] ? phy : NULL;
783 }
784
785 static char * nl80211_phyidx2name(unsigned int idx)
786 {
787 struct nl80211_msg_conveyor *cv;
788 static char phy[32] = { 0 };
789
790 if (nl80211_init() < 0)
791 return NULL;
792
793 cv = nl80211_new(nls->nl80211, NL80211_CMD_GET_WIPHY, 0);
794 if (!cv)
795 return NULL;
796
797 NLA_PUT_U32(cv->msg, NL80211_ATTR_WIPHY, idx);
798
799 memset(phy, 0, sizeof(phy));
800 nl80211_send(cv, nl80211_phyname_cb, phy);
801
802 return phy[0] ? phy : NULL;
803
804 nla_put_failure:
805 return NULL;
806 }
807
808 static char * nl80211_phy2ifname(const char *ifname)
809 {
810 int ifidx = -1, cifidx = -1, phyidx = -1;
811 char buffer[512];
812 static char nif[IFNAMSIZ] = { 0 };
813
814 DIR *d;
815 struct dirent *e;
816
817 /* Only accept phy name of the form phy%d or radio%d */
818 if (!ifname)
819 return NULL;
820
821 phyidx = nl80211_phy_idx_from_phy(ifname);
822 if (phyidx < 0)
823 phyidx = nl80211_phy_idx_from_uci(ifname);;
824 if (phyidx < 0)
825 return NULL;
826
827 memset(nif, 0, sizeof(nif));
828
829 snprintf(buffer, sizeof(buffer),
830 "/sys/class/ieee80211/phy%d/device/net", phyidx);
831
832 if ((d = opendir(buffer)) != NULL)
833 {
834 while ((e = readdir(d)) != NULL)
835 {
836 snprintf(buffer, sizeof(buffer),
837 "/sys/class/net/%s/ifindex", e->d_name);
838
839 if ((cifidx = nl80211_readint(buffer)) >= 0 &&
840 ((ifidx < 0) || (cifidx < ifidx)))
841 {
842 ifidx = cifidx;
843 strncpy(nif, e->d_name, sizeof(nif) - 1);
844 }
845 }
846
847 closedir(d);
848 }
849
850 return nif[0] ? nif : NULL;
851 }
852
853 static int nl80211_get_mode_cb(struct nl_msg *msg, void *arg)
854 {
855 int *mode = arg;
856 struct nlattr **tb = nl80211_parse(msg);
857 const int ifmodes[NL80211_IFTYPE_MAX + 1] = {
858 IWINFO_OPMODE_UNKNOWN, /* unspecified */
859 IWINFO_OPMODE_ADHOC, /* IBSS */
860 IWINFO_OPMODE_CLIENT, /* managed */
861 IWINFO_OPMODE_MASTER, /* AP */
862 IWINFO_OPMODE_AP_VLAN, /* AP/VLAN */
863 IWINFO_OPMODE_WDS, /* WDS */
864 IWINFO_OPMODE_MONITOR, /* monitor */
865 IWINFO_OPMODE_MESHPOINT, /* mesh point */
866 IWINFO_OPMODE_P2P_CLIENT, /* P2P-client */
867 IWINFO_OPMODE_P2P_GO, /* P2P-GO */
868 };
869
870 if (tb[NL80211_ATTR_IFTYPE])
871 *mode = ifmodes[nla_get_u32(tb[NL80211_ATTR_IFTYPE])];
872
873 return NL_SKIP;
874 }
875
876
877 static int nl80211_get_mode(const char *ifname, int *buf)
878 {
879 char *res;
880
881 *buf = IWINFO_OPMODE_UNKNOWN;
882
883 res = nl80211_phy2ifname(ifname);
884
885 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
886 nl80211_get_mode_cb, buf);
887
888 return (*buf == IWINFO_OPMODE_UNKNOWN) ? -1 : 0;
889 }
890
891 static int __nl80211_hostapd_query(const char *ifname, ...)
892 {
893 va_list ap, ap_cur;
894 char *phy, *search, *dest, *key, *val, buf[128];
895 int len, mode, found = 0, match = 1;
896 FILE *fp;
897
898 if (nl80211_get_mode(ifname, &mode))
899 return 0;
900
901 if (mode != IWINFO_OPMODE_MASTER && mode != IWINFO_OPMODE_AP_VLAN)
902 return 0;
903
904 phy = nl80211_ifname2phy(ifname);
905
906 if (!phy)
907 return 0;
908
909 snprintf(buf, sizeof(buf), "/var/run/hostapd-%s.conf", phy);
910 fp = fopen(buf, "r");
911
912 if (!fp)
913 return 0;
914
915 va_start(ap, ifname);
916
917 /* clear all destination buffers */
918 va_copy(ap_cur, ap);
919
920 while ((search = va_arg(ap_cur, char *)) != NULL)
921 {
922 dest = va_arg(ap_cur, char *);
923 len = va_arg(ap_cur, int);
924
925 memset(dest, 0, len);
926 }
927
928 va_end(ap_cur);
929
930 /* iterate applicable lines and copy found values into dest buffers */
931 while (fgets(buf, sizeof(buf), fp))
932 {
933 key = strtok(buf, " =\t\n");
934 val = strtok(NULL, "\n");
935
936 if (!key || !val || !*key || *key == '#')
937 continue;
938
939 if (!strcmp(key, "interface") || !strcmp(key, "bss"))
940 match = !strcmp(ifname, val);
941
942 if (!match)
943 continue;
944
945 va_copy(ap_cur, ap);
946
947 while ((search = va_arg(ap_cur, char *)) != NULL)
948 {
949 dest = va_arg(ap_cur, char *);
950 len = va_arg(ap_cur, int);
951
952 if (!strcmp(search, key))
953 {
954 strncpy(dest, val, len - 1);
955 found++;
956 break;
957 }
958 }
959
960 va_end(ap_cur);
961 }
962
963 fclose(fp);
964
965 va_end(ap);
966
967 return found;
968 }
969
970 #define nl80211_hostapd_query(ifname, ...) \
971 __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
972
973
974 static inline int nl80211_wpactl_recv(int sock, char *buf, int blen)
975 {
976 fd_set rfds;
977 struct timeval tv = { 0, 256000 };
978
979 FD_ZERO(&rfds);
980 FD_SET(sock, &rfds);
981
982 memset(buf, 0, blen);
983
984 if (select(sock + 1, &rfds, NULL, NULL, &tv) < 0)
985 return -1;
986
987 if (!FD_ISSET(sock, &rfds))
988 return -1;
989
990 return recv(sock, buf, blen - 1, 0);
991 }
992
993 static int nl80211_wpactl_connect(const char *ifname, struct sockaddr_un *local)
994 {
995 struct sockaddr_un remote = { 0 };
996 size_t remote_length, local_length;
997
998 int sock = socket(PF_UNIX, SOCK_DGRAM, 0);
999 if (sock < 0)
1000 return sock;
1001
1002 remote.sun_family = AF_UNIX;
1003 remote_length = sizeof(remote.sun_family) +
1004 sprintf(remote.sun_path, "/var/run/wpa_supplicant/%s", ifname);
1005
1006 /* Set client socket file permissions so that bind() creates the client
1007 * socket with these permissions and there is no need to try to change
1008 * them with chmod() after bind() which would have potential issues with
1009 * race conditions. These permissions are needed to make sure the server
1010 * side (wpa_supplicant or hostapd) can reply to the control interface
1011 * messages.
1012 *
1013 * The lchown() calls below after bind() are also part of the needed
1014 * operations to allow the response to go through. Those are using the
1015 * no-deference-symlinks version to avoid races. */
1016 fchmod(sock, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1017
1018 if (fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC) < 0)
1019 {
1020 close(sock);
1021 return -1;
1022 }
1023
1024 if (connect(sock, (struct sockaddr *)&remote, remote_length))
1025 {
1026 close(sock);
1027 return -1;
1028 }
1029
1030 local->sun_family = AF_UNIX;
1031 local_length = sizeof(local->sun_family) +
1032 sprintf(local->sun_path, "/var/run/iwinfo-%s-%d", ifname, getpid());
1033
1034 if (bind(sock, (struct sockaddr *)local, local_length) < 0)
1035 {
1036 close(sock);
1037 return -1;
1038 }
1039
1040 /* Set group even if we do not have privileges to change owner */
1041 lchown(local->sun_path, -1, 101);
1042 lchown(local->sun_path, 101, 101);
1043
1044 return sock;
1045 }
1046
1047 static int __nl80211_wpactl_query(const char *ifname, ...)
1048 {
1049 va_list ap, ap_cur;
1050 struct sockaddr_un local = { 0 };
1051 int len, mode, found = 0, sock = -1;
1052 char *search, *dest, *key, *val, *line, *pos, buf[512];
1053
1054 if (nl80211_get_mode(ifname, &mode))
1055 return 0;
1056
1057 if (mode != IWINFO_OPMODE_CLIENT &&
1058 mode != IWINFO_OPMODE_ADHOC &&
1059 mode != IWINFO_OPMODE_MESHPOINT)
1060 return 0;
1061
1062 sock = nl80211_wpactl_connect(ifname, &local);
1063
1064 if (sock < 0)
1065 return 0;
1066
1067 va_start(ap, ifname);
1068
1069 /* clear all destination buffers */
1070 va_copy(ap_cur, ap);
1071
1072 while ((search = va_arg(ap_cur, char *)) != NULL)
1073 {
1074 dest = va_arg(ap_cur, char *);
1075 len = va_arg(ap_cur, int);
1076
1077 memset(dest, 0, len);
1078 }
1079
1080 va_end(ap_cur);
1081
1082 send(sock, "STATUS", 6, 0);
1083
1084 while (true)
1085 {
1086 if (nl80211_wpactl_recv(sock, buf, sizeof(buf)) <= 0)
1087 break;
1088
1089 if (buf[0] == '<')
1090 continue;
1091
1092 for (line = strtok_r(buf, "\n", &pos);
1093 line != NULL;
1094 line = strtok_r(NULL, "\n", &pos))
1095 {
1096 key = strtok(line, "=");
1097 val = strtok(NULL, "\n");
1098
1099 if (!key || !val)
1100 continue;
1101
1102 va_copy(ap_cur, ap);
1103
1104 while ((search = va_arg(ap_cur, char *)) != NULL)
1105 {
1106 dest = va_arg(ap_cur, char *);
1107 len = va_arg(ap_cur, int);
1108
1109 if (!strcmp(search, key))
1110 {
1111 strncpy(dest, val, len - 1);
1112 found++;
1113 break;
1114 }
1115 }
1116
1117 va_end(ap_cur);
1118 }
1119
1120 break;
1121 }
1122
1123 va_end(ap);
1124
1125 close(sock);
1126 unlink(local.sun_path);
1127
1128 return found;
1129 }
1130
1131 #define nl80211_wpactl_query(ifname, ...) \
1132 __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL)
1133
1134
1135 static char * nl80211_ifadd(const char *ifname)
1136 {
1137 char path[PATH_MAX];
1138 static char nif[IFNAMSIZ] = { 0 };
1139 struct nl80211_msg_conveyor *req;
1140 FILE *sysfs;
1141
1142 req = nl80211_msg(ifname, NL80211_CMD_NEW_INTERFACE, 0);
1143 if (req)
1144 {
1145 snprintf(nif, sizeof(nif), "tmp.%s", ifname);
1146
1147 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, nif);
1148 NLA_PUT_U32(req->msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION);
1149
1150 nl80211_send(req, NULL, NULL);
1151
1152 snprintf(path, sizeof(path) - 1,
1153 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif);
1154
1155 if ((sysfs = fopen(path, "w")) != NULL)
1156 {
1157 fwrite("0\n", 1, 2, sysfs);
1158 fclose(sysfs);
1159 }
1160
1161 return nif;
1162
1163 nla_put_failure:
1164 nl80211_free(req);
1165 }
1166
1167 return NULL;
1168 }
1169
1170 static void nl80211_ifdel(const char *ifname)
1171 {
1172 struct nl80211_msg_conveyor *req;
1173
1174 req = nl80211_msg(ifname, NL80211_CMD_DEL_INTERFACE, 0);
1175 if (req)
1176 {
1177 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, ifname);
1178
1179 nl80211_send(req, NULL, NULL);
1180 return;
1181
1182 nla_put_failure:
1183 nl80211_free(req);
1184 }
1185 }
1186
1187 static void nl80211_hostapd_hup(const char *ifname)
1188 {
1189 int fd, pid = 0;
1190 char buf[32];
1191 char *phy = nl80211_ifname2phy(ifname);
1192
1193 if (phy)
1194 {
1195 snprintf(buf, sizeof(buf), "/var/run/wifi-%s.pid", phy);
1196 if ((fd = open(buf, O_RDONLY)) >= 0)
1197 {
1198 if (read(fd, buf, sizeof(buf)) > 0)
1199 pid = atoi(buf);
1200
1201 close(fd);
1202 }
1203
1204 if (pid > 0)
1205 kill(pid, 1);
1206 }
1207 }
1208
1209
1210 static int nl80211_probe(const char *ifname)
1211 {
1212 return !!nl80211_ifname2phy(ifname);
1213 }
1214
1215 struct nl80211_ssid_bssid {
1216 unsigned char *ssid;
1217 unsigned char bssid[7];
1218 };
1219
1220 static int nl80211_get_macaddr_cb(struct nl_msg *msg, void *arg)
1221 {
1222 struct nl80211_ssid_bssid *sb = arg;
1223 struct nlattr **tb = nl80211_parse(msg);
1224
1225 if (tb[NL80211_ATTR_MAC]) {
1226 sb->bssid[0] = 1;
1227 memcpy(sb->bssid + 1, nla_data(tb[NL80211_ATTR_MAC]),
1228 sizeof(sb->bssid) - 1);
1229 }
1230
1231 return NL_SKIP;
1232 }
1233
1234 static int nl80211_get_ssid_bssid_cb(struct nl_msg *msg, void *arg)
1235 {
1236 int ielen;
1237 unsigned char *ie;
1238 struct nl80211_ssid_bssid *sb = arg;
1239 struct nlattr **tb = nl80211_parse(msg);
1240 struct nlattr *bss[NL80211_BSS_MAX + 1];
1241
1242 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1243 [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 },
1244 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1245 };
1246
1247 if (!tb[NL80211_ATTR_BSS] ||
1248 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1249 bss_policy) ||
1250 !bss[NL80211_BSS_BSSID] ||
1251 !bss[NL80211_BSS_STATUS] ||
1252 !bss[NL80211_BSS_INFORMATION_ELEMENTS])
1253 {
1254 return NL_SKIP;
1255 }
1256
1257 switch (nla_get_u32(bss[NL80211_BSS_STATUS]))
1258 {
1259 case NL80211_BSS_STATUS_ASSOCIATED:
1260 case NL80211_BSS_STATUS_AUTHENTICATED:
1261 case NL80211_BSS_STATUS_IBSS_JOINED:
1262
1263 if (sb->ssid)
1264 {
1265 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1266 ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1267
1268 while (ielen >= 2 && ielen >= ie[1])
1269 {
1270 if (ie[0] == 0)
1271 {
1272 memcpy(sb->ssid, ie + 2, min(ie[1], IWINFO_ESSID_MAX_SIZE));
1273 return NL_SKIP;
1274 }
1275
1276 ielen -= ie[1] + 2;
1277 ie += ie[1] + 2;
1278 }
1279 }
1280 else
1281 {
1282 sb->bssid[0] = 1;
1283 memcpy(sb->bssid + 1, nla_data(bss[NL80211_BSS_BSSID]), 6);
1284 return NL_SKIP;
1285 }
1286
1287 default:
1288 return NL_SKIP;
1289 }
1290 }
1291
1292 static int nl80211_get_ssid(const char *ifname, char *buf)
1293 {
1294 char *res;
1295 struct nl80211_ssid_bssid sb = { .ssid = (unsigned char *)buf };
1296
1297 /* try to find ssid from scan dump results */
1298 res = nl80211_phy2ifname(ifname);
1299 sb.ssid[0] = 0;
1300
1301 nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
1302 nl80211_get_ssid_bssid_cb, &sb);
1303
1304 /* failed, try to find from hostapd info */
1305 if (sb.ssid[0] == 0)
1306 nl80211_hostapd_query(ifname, "ssid", sb.ssid,
1307 IWINFO_ESSID_MAX_SIZE + 1);
1308
1309 /* failed, try to obtain Mesh ID */
1310 if (sb.ssid[0] == 0)
1311 iwinfo_ubus_query(res ? res : ifname, "mesh_id",
1312 buf, IWINFO_ESSID_MAX_SIZE + 1);
1313
1314 return (sb.ssid[0] == 0) ? -1 : 0;
1315 }
1316
1317 static int nl80211_get_bssid(const char *ifname, char *buf)
1318 {
1319 char *res, bssid[sizeof("FF:FF:FF:FF:FF:FF\0")];
1320 struct nl80211_ssid_bssid sb = { };
1321
1322 res = nl80211_phy2ifname(ifname);
1323
1324 /* try to obtain mac address via NL80211_CMD_GET_INTERFACE */
1325 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
1326 nl80211_get_macaddr_cb, &sb);
1327
1328 /* failed, try to find bssid from scan dump results */
1329 if (sb.bssid[0] == 0)
1330 nl80211_request(res ? res : ifname,
1331 NL80211_CMD_GET_SCAN, NLM_F_DUMP,
1332 nl80211_get_ssid_bssid_cb, &sb);
1333
1334 /* failed, try to find mac from hostapd info */
1335 if ((sb.bssid[0] == 0) &&
1336 nl80211_hostapd_query(ifname, "bssid", bssid, sizeof(bssid)))
1337 {
1338 sb.bssid[0] = 1;
1339 sb.bssid[1] = strtol(&bssid[0], NULL, 16);
1340 sb.bssid[2] = strtol(&bssid[3], NULL, 16);
1341 sb.bssid[3] = strtol(&bssid[6], NULL, 16);
1342 sb.bssid[4] = strtol(&bssid[9], NULL, 16);
1343 sb.bssid[5] = strtol(&bssid[12], NULL, 16);
1344 sb.bssid[6] = strtol(&bssid[15], NULL, 16);
1345 }
1346
1347 if (sb.bssid[0])
1348 {
1349 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
1350 sb.bssid[1], sb.bssid[2], sb.bssid[3],
1351 sb.bssid[4], sb.bssid[5], sb.bssid[6]);
1352
1353 return 0;
1354 }
1355
1356 return -1;
1357 }
1358
1359
1360 static int nl80211_get_frequency_scan_cb(struct nl_msg *msg, void *arg)
1361 {
1362 int *freq = arg;
1363 struct nlattr **attr = nl80211_parse(msg);
1364 struct nlattr *binfo[NL80211_BSS_MAX + 1];
1365
1366 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1367 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1368 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1369 };
1370
1371 if (attr[NL80211_ATTR_BSS] &&
1372 !nla_parse_nested(binfo, NL80211_BSS_MAX,
1373 attr[NL80211_ATTR_BSS], bss_policy))
1374 {
1375 if (binfo[NL80211_BSS_STATUS] && binfo[NL80211_BSS_FREQUENCY])
1376 *freq = nla_get_u32(binfo[NL80211_BSS_FREQUENCY]);
1377 }
1378
1379 return NL_SKIP;
1380 }
1381
1382 static int nl80211_get_frequency_info_cb(struct nl_msg *msg, void *arg)
1383 {
1384 int *freq = arg;
1385 struct nlattr **tb = nl80211_parse(msg);
1386
1387 if (tb[NL80211_ATTR_WIPHY_FREQ])
1388 *freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1389
1390 return NL_SKIP;
1391 }
1392
1393 static int nl80211_get_frequency(const char *ifname, int *buf)
1394 {
1395 char *res, channel[4] = { 0 }, hwmode[3] = { 0 }, ax[2] = { 0 };
1396
1397 /* try to find frequency from interface info */
1398 res = nl80211_phy2ifname(ifname);
1399 *buf = 0;
1400
1401 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
1402 nl80211_get_frequency_info_cb, buf);
1403
1404 /* failed, try to find frequency from hostapd info */
1405 if ((*buf == 0) &&
1406 nl80211_hostapd_query(ifname, "hw_mode", hwmode, sizeof(hwmode),
1407 "channel", channel, sizeof(channel),
1408 "ieee80211ax", ax, sizeof(ax)) >= 2)
1409 {
1410 *buf = nl80211_channel2freq(atoi(channel), hwmode, ax[0] == '1');
1411 }
1412
1413 /* failed, try to find frequency from scan results */
1414 if (*buf == 0)
1415 {
1416 res = nl80211_phy2ifname(ifname);
1417
1418 nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
1419 nl80211_get_frequency_scan_cb, buf);
1420 }
1421
1422 return (*buf == 0) ? -1 : 0;
1423 }
1424
1425 static int nl80211_get_center_freq1_cb(struct nl_msg *msg, void *arg)
1426 {
1427 int *freq = arg;
1428 struct nlattr **tb = nl80211_parse(msg);
1429
1430 if (tb[NL80211_ATTR_CENTER_FREQ1])
1431 *freq = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
1432
1433 return NL_SKIP;
1434 }
1435
1436 static int nl80211_get_center_freq1(const char *ifname, int *buf)
1437 {
1438 char *res;
1439
1440 /* try to find frequency from interface info */
1441 res = nl80211_phy2ifname(ifname);
1442 *buf = 0;
1443
1444 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
1445 nl80211_get_center_freq1_cb, buf);
1446
1447 return (*buf == 0) ? -1 : 0;
1448 }
1449
1450 static int nl80211_get_center_freq2_cb(struct nl_msg *msg, void *arg)
1451 {
1452 int *freq = arg;
1453 struct nlattr **tb = nl80211_parse(msg);
1454
1455 if (tb[NL80211_ATTR_CENTER_FREQ2])
1456 *freq = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
1457
1458 return NL_SKIP;
1459 }
1460
1461 static int nl80211_get_center_freq2(const char *ifname, int *buf)
1462 {
1463 char *res;
1464
1465 /* try to find frequency from interface info */
1466 res = nl80211_phy2ifname(ifname);
1467 *buf = 0;
1468
1469 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
1470 nl80211_get_center_freq2_cb, buf);
1471
1472 return (*buf == 0) ? -1 : 0;
1473 }
1474
1475 static int nl80211_get_channel(const char *ifname, int *buf)
1476 {
1477 if (!nl80211_get_frequency(ifname, buf))
1478 {
1479 *buf = nl80211_freq2channel(*buf);
1480 return 0;
1481 }
1482
1483 return -1;
1484 }
1485
1486 static int nl80211_get_center_chan1(const char *ifname, int *buf)
1487 {
1488 if (!nl80211_get_center_freq1(ifname, buf))
1489 {
1490 *buf = nl80211_freq2channel(*buf);
1491 return 0;
1492 }
1493
1494 return -1;
1495 }
1496
1497 static int nl80211_get_center_chan2(const char *ifname, int *buf)
1498 {
1499 if (!nl80211_get_center_freq2(ifname, buf))
1500 {
1501 *buf = nl80211_freq2channel(*buf);
1502 return 0;
1503 }
1504
1505 return -1;
1506 }
1507
1508 static int nl80211_get_txpower_cb(struct nl_msg *msg, void *arg)
1509 {
1510 int *buf = arg;
1511 struct nlattr **tb = nl80211_parse(msg);
1512
1513 if (tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL])
1514 *buf = iwinfo_mbm2dbm(nla_get_u32(tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]));
1515
1516 return NL_SKIP;
1517 }
1518
1519 static int nl80211_get_txpower(const char *ifname, int *buf)
1520 {
1521 char *res;
1522
1523 res = nl80211_phy2ifname(ifname);
1524 *buf = 0;
1525
1526 if (nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
1527 nl80211_get_txpower_cb, buf))
1528 return -1;
1529
1530 return 0;
1531 }
1532
1533
1534 static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
1535 {
1536 int8_t dbm;
1537 int16_t mbit;
1538 struct nl80211_rssi_rate *rr = arg;
1539 struct nlattr **attr = nl80211_parse(msg);
1540 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1541 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1542
1543 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
1544 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
1545 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
1546 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
1547 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
1548 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
1549 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1550 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
1551 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
1552 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
1553 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
1554 };
1555
1556 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1557 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1558 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1559 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1560 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1561 };
1562
1563 if (attr[NL80211_ATTR_STA_INFO])
1564 {
1565 if (!nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1566 attr[NL80211_ATTR_STA_INFO], stats_policy))
1567 {
1568 if (sinfo[NL80211_STA_INFO_SIGNAL])
1569 {
1570 dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1571 rr->rssi = (rr->rssi * rr->rssi_samples + dbm) / (rr->rssi_samples + 1);
1572 rr->rssi_samples++;
1573 }
1574
1575 if (sinfo[NL80211_STA_INFO_TX_BITRATE])
1576 {
1577 if (!nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1578 sinfo[NL80211_STA_INFO_TX_BITRATE],
1579 rate_policy))
1580 {
1581 if (rinfo[NL80211_RATE_INFO_BITRATE])
1582 {
1583 mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
1584 rr->rate = (rr->rate * rr->rate_samples + mbit) / (rr->rate_samples + 1);
1585 rr->rate_samples++;
1586 }
1587 }
1588 }
1589 }
1590 }
1591
1592 return NL_SKIP;
1593 }
1594
1595 static void nl80211_fill_signal(const char *ifname, struct nl80211_rssi_rate *r)
1596 {
1597 DIR *d;
1598 struct dirent *de;
1599
1600 memset(r, 0, sizeof(*r));
1601
1602 if ((d = opendir("/sys/class/net")) != NULL)
1603 {
1604 while ((de = readdir(d)) != NULL)
1605 {
1606 if (!strncmp(de->d_name, ifname, strlen(ifname)) &&
1607 (!de->d_name[strlen(ifname)] ||
1608 !strncmp(&de->d_name[strlen(ifname)], ".sta", 4)))
1609 {
1610 nl80211_request(de->d_name, NL80211_CMD_GET_STATION,
1611 NLM_F_DUMP, nl80211_fill_signal_cb, r);
1612 }
1613 }
1614
1615 closedir(d);
1616 }
1617 }
1618
1619 static int nl80211_get_bitrate(const char *ifname, int *buf)
1620 {
1621 struct nl80211_rssi_rate rr;
1622
1623 nl80211_fill_signal(ifname, &rr);
1624
1625 if (rr.rate_samples)
1626 {
1627 *buf = (rr.rate * 100);
1628 return 0;
1629 }
1630
1631 return -1;
1632 }
1633
1634 static int nl80211_get_signal(const char *ifname, int *buf)
1635 {
1636 struct nl80211_rssi_rate rr;
1637
1638 nl80211_fill_signal(ifname, &rr);
1639
1640 if (rr.rssi_samples)
1641 {
1642 *buf = rr.rssi;
1643 return 0;
1644 }
1645
1646 return -1;
1647 }
1648
1649 static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg)
1650 {
1651 int8_t *noise = arg;
1652 struct nlattr **tb = nl80211_parse(msg);
1653 struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
1654
1655 static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
1656 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1657 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1658 };
1659
1660 if (!tb[NL80211_ATTR_SURVEY_INFO])
1661 return NL_SKIP;
1662
1663 if (nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
1664 tb[NL80211_ATTR_SURVEY_INFO], sp))
1665 return NL_SKIP;
1666
1667 if (!si[NL80211_SURVEY_INFO_NOISE])
1668 return NL_SKIP;
1669
1670 if (!*noise || si[NL80211_SURVEY_INFO_IN_USE])
1671 *noise = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
1672
1673 return NL_SKIP;
1674 }
1675
1676
1677 static int nl80211_get_noise(const char *ifname, int *buf)
1678 {
1679 int8_t noise = 0;
1680
1681 if (nl80211_request(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP,
1682 nl80211_get_noise_cb, &noise))
1683 goto out;
1684
1685 *buf = noise;
1686 return 0;
1687
1688 out:
1689 *buf = 0;
1690 return -1;
1691 }
1692
1693 static int nl80211_get_quality(const char *ifname, int *buf)
1694 {
1695 int signal;
1696
1697 if (!nl80211_get_signal(ifname, &signal))
1698 {
1699 /* A positive signal level is usually just a quality
1700 * value, pass through as-is */
1701 if (signal >= 0)
1702 {
1703 *buf = signal;
1704 }
1705
1706 /* The cfg80211 wext compat layer assumes a signal range
1707 * of -110 dBm to -40 dBm, the quality value is derived
1708 * by adding 110 to the signal level */
1709 else
1710 {
1711 if (signal < -110)
1712 signal = -110;
1713 else if (signal > -40)
1714 signal = -40;
1715
1716 *buf = (signal + 110);
1717 }
1718
1719 return 0;
1720 }
1721
1722 return -1;
1723 }
1724
1725 static int nl80211_get_quality_max(const char *ifname, int *buf)
1726 {
1727 /* The cfg80211 wext compat layer assumes a maximum
1728 * quality of 70 */
1729 *buf = 70;
1730
1731 return 0;
1732 }
1733
1734 static int nl80211_check_wepkey(const char *key)
1735 {
1736 if (key && *key)
1737 {
1738 switch (strlen(key))
1739 {
1740 case 5:
1741 case 10:
1742 return IWINFO_CIPHER_WEP40;
1743
1744 case 13:
1745 case 26:
1746 return IWINFO_CIPHER_WEP104;
1747 }
1748 }
1749
1750 return 0;
1751 }
1752
1753 static struct {
1754 const char *match;
1755 int version;
1756 int suite;
1757 } wpa_key_mgmt_strings[] = {
1758 { "IEEE 802.1X/EAP", 0, IWINFO_KMGMT_8021x },
1759 { "EAP-SUITE-B-192", 4, IWINFO_KMGMT_8021x },
1760 { "EAP-SUITE-B", 4, IWINFO_KMGMT_8021x },
1761 { "EAP-SHA384", 4, IWINFO_KMGMT_8021x },
1762 { "EAP-SHA256", 0, IWINFO_KMGMT_8021x },
1763 { "PSK-SHA256", 0, IWINFO_KMGMT_PSK },
1764 { "NONE", 0, IWINFO_KMGMT_NONE },
1765 { "None", 0, IWINFO_KMGMT_NONE },
1766 { "PSK", 0, IWINFO_KMGMT_PSK },
1767 { "EAP", 0, IWINFO_KMGMT_8021x },
1768 { "SAE", 4, IWINFO_KMGMT_SAE },
1769 { "OWE", 4, IWINFO_KMGMT_OWE }
1770 };
1771
1772 static void parse_wpa_suites(const char *str, int defversion,
1773 uint8_t *versions, uint8_t *suites)
1774 {
1775 size_t l;
1776 int i, version;
1777 const char *p, *q, *m, *sep = " \t\n,-+/";
1778
1779 for (p = str; *p; )
1780 {
1781 q = p;
1782
1783 for (i = 0; i < ARRAY_SIZE(wpa_key_mgmt_strings); i++)
1784 {
1785 m = wpa_key_mgmt_strings[i].match;
1786 l = strlen(m);
1787
1788 if (!strncmp(q, m, l) && (!q[l] || strchr(sep, q[l])))
1789 {
1790 if (wpa_key_mgmt_strings[i].version != 0)
1791 version = wpa_key_mgmt_strings[i].version;
1792 else
1793 version = defversion;
1794
1795 *versions |= version;
1796 *suites |= wpa_key_mgmt_strings[i].suite;
1797
1798 q += l;
1799 break;
1800 }
1801 }
1802
1803 if (q == p)
1804 q += strcspn(q, sep);
1805
1806 p = q + strspn(q, sep);
1807 }
1808 }
1809
1810 static struct {
1811 const char *match;
1812 int cipher;
1813 } wpa_cipher_strings[] = {
1814 { "WEP-104", IWINFO_CIPHER_WEP104 },
1815 { "WEP-40", IWINFO_CIPHER_WEP40 },
1816 { "NONE", IWINFO_CIPHER_NONE },
1817 { "TKIP", IWINFO_CIPHER_TKIP },
1818 { "CCMP-256",IWINFO_CIPHER_CCMP256 },
1819 { "CCMP", IWINFO_CIPHER_CCMP },
1820 { "GCMP-256",IWINFO_CIPHER_GCMP256 },
1821 { "GCMP", IWINFO_CIPHER_GCMP }
1822 };
1823
1824 static void parse_wpa_ciphers(const char *str, uint16_t *ciphers)
1825 {
1826 int i;
1827 size_t l;
1828 const char *m, *p, *q, *sep = " \t\n,-+/";
1829
1830 for (p = str; *p; )
1831 {
1832 q = p;
1833
1834 for (i = 0; i < ARRAY_SIZE(wpa_cipher_strings); i++)
1835 {
1836 m = wpa_cipher_strings[i].match;
1837 l = strlen(m);
1838
1839 if (!strncmp(q, m, l) && (!q[l] || strchr(sep, q[l])))
1840 {
1841 *ciphers |= wpa_cipher_strings[i].cipher;
1842
1843 q += l;
1844 break;
1845 }
1846 }
1847
1848 if (q == p)
1849 q += strcspn(q, sep);
1850
1851 p = q + strspn(q, sep);
1852 }
1853 }
1854
1855 static int nl80211_get_encryption(const char *ifname, char *buf)
1856 {
1857 char *p;
1858 int opmode;
1859 uint8_t wpa_version = 0;
1860 char wpa[2], wpa_key_mgmt[64], wpa_pairwise[16], wpa_groupwise[16];
1861 char auth_algs[2], wep_key0[27], wep_key1[27], wep_key2[27], wep_key3[27];
1862 char mode[16];
1863
1864 struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
1865
1866 /* WPA supplicant */
1867 if (nl80211_wpactl_query(ifname,
1868 "pairwise_cipher", wpa_pairwise, sizeof(wpa_pairwise),
1869 "group_cipher", wpa_groupwise, sizeof(wpa_groupwise),
1870 "key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt),
1871 "mode", mode, sizeof(mode)))
1872 {
1873 /* WEP or Open */
1874 if (!strcmp(wpa_key_mgmt, "NONE"))
1875 {
1876 parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers);
1877 parse_wpa_ciphers(wpa_groupwise, &c->group_ciphers);
1878
1879 if (c->pair_ciphers != 0 && c->pair_ciphers != IWINFO_CIPHER_NONE) {
1880 c->enabled = 1;
1881 c->auth_suites = IWINFO_KMGMT_NONE;
1882 c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
1883 }
1884 else {
1885 c->pair_ciphers = 0;
1886 c->group_ciphers = 0;
1887 }
1888 }
1889
1890 /* MESH with SAE */
1891 else if (!strcmp(mode, "mesh") && !strcmp(wpa_key_mgmt, "UNKNOWN"))
1892 {
1893 c->enabled = 1;
1894 c->wpa_version = 4;
1895 c->auth_suites = IWINFO_KMGMT_SAE;
1896 c->pair_ciphers = IWINFO_CIPHER_CCMP;
1897 c->group_ciphers = IWINFO_CIPHER_CCMP;
1898 }
1899
1900 /* WPA */
1901 else
1902 {
1903 parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers);
1904 parse_wpa_ciphers(wpa_groupwise, &c->group_ciphers);
1905
1906 p = wpa_key_mgmt;
1907
1908 if (!strncmp(p, "WPA2-", 5) || !strncmp(p, "WPA2/", 5))
1909 {
1910 p += 5;
1911 wpa_version = 2;
1912 }
1913 else if (!strncmp(p, "WPA-", 4))
1914 {
1915 p += 4;
1916 wpa_version = 1;
1917 }
1918
1919 parse_wpa_suites(p, wpa_version, &c->wpa_version, &c->auth_suites);
1920
1921 c->enabled = !!(c->wpa_version && c->auth_suites);
1922 }
1923
1924 return 0;
1925 }
1926
1927 /* Hostapd */
1928 else if (nl80211_hostapd_query(ifname,
1929 "wpa", wpa, sizeof(wpa),
1930 "wpa_key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt),
1931 "wpa_pairwise", wpa_pairwise, sizeof(wpa_pairwise),
1932 "auth_algs", auth_algs, sizeof(auth_algs),
1933 "wep_key0", wep_key0, sizeof(wep_key0),
1934 "wep_key1", wep_key1, sizeof(wep_key1),
1935 "wep_key2", wep_key2, sizeof(wep_key2),
1936 "wep_key3", wep_key3, sizeof(wep_key3)))
1937 {
1938 c->wpa_version = 0;
1939
1940 if (wpa_key_mgmt[0])
1941 {
1942 for (p = strtok(wpa_key_mgmt, " \t"); p != NULL; p = strtok(NULL, " \t"))
1943 {
1944 if (!strncmp(p, "WPA-", 4))
1945 p += 4;
1946
1947 if (!strncmp(p, "FT-", 3))
1948 p += 3;
1949
1950 parse_wpa_suites(p, atoi(wpa), &c->wpa_version, &c->auth_suites);
1951 }
1952
1953 c->enabled = c->wpa_version ? 1 : 0;
1954 }
1955
1956 if (wpa_pairwise[0])
1957 parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers);
1958
1959 if (auth_algs[0])
1960 {
1961 switch (atoi(auth_algs))
1962 {
1963 case 1:
1964 c->auth_algs |= IWINFO_AUTH_OPEN;
1965 break;
1966
1967 case 2:
1968 c->auth_algs |= IWINFO_AUTH_SHARED;
1969 break;
1970
1971 case 3:
1972 c->auth_algs |= IWINFO_AUTH_OPEN;
1973 c->auth_algs |= IWINFO_AUTH_SHARED;
1974 break;
1975 }
1976
1977 c->pair_ciphers |= nl80211_check_wepkey(wep_key0);
1978 c->pair_ciphers |= nl80211_check_wepkey(wep_key1);
1979 c->pair_ciphers |= nl80211_check_wepkey(wep_key2);
1980 c->pair_ciphers |= nl80211_check_wepkey(wep_key3);
1981
1982 c->enabled = (c->auth_algs && c->pair_ciphers) ? 1 : 0;
1983 }
1984
1985 c->group_ciphers = c->pair_ciphers;
1986
1987 return 0;
1988 }
1989
1990 /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */
1991 else if (!nl80211_get_mode(ifname, &opmode) &&
1992 (opmode == IWINFO_OPMODE_ADHOC ||
1993 opmode == IWINFO_OPMODE_MESHPOINT))
1994 {
1995 c->enabled = 0;
1996
1997 return 0;
1998 }
1999
2000
2001 return -1;
2002 }
2003
2004 static int nl80211_get_phyname(const char *ifname, char *buf)
2005 {
2006 const char *name;
2007
2008 name = nl80211_ifname2phy(ifname);
2009
2010 if (name)
2011 {
2012 strcpy(buf, name);
2013 return 0;
2014 }
2015 else if ((name = nl80211_phy2ifname(ifname)) != NULL)
2016 {
2017 name = nl80211_ifname2phy(name);
2018
2019 if (name)
2020 {
2021 strcpy(buf, ifname);
2022 return 0;
2023 }
2024 }
2025
2026 return -1;
2027 }
2028
2029
2030 static void nl80211_parse_rateinfo(struct nlattr **ri,
2031 struct iwinfo_rate_entry *re)
2032 {
2033 if (ri[NL80211_RATE_INFO_BITRATE32])
2034 re->rate = nla_get_u32(ri[NL80211_RATE_INFO_BITRATE32]) * 100;
2035 else if (ri[NL80211_RATE_INFO_BITRATE])
2036 re->rate = nla_get_u16(ri[NL80211_RATE_INFO_BITRATE]) * 100;
2037
2038 if (ri[NL80211_RATE_INFO_HE_MCS])
2039 {
2040 re->is_he = 1;
2041 re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_HE_MCS]);
2042
2043 if (ri[NL80211_RATE_INFO_HE_NSS])
2044 re->nss = nla_get_u8(ri[NL80211_RATE_INFO_HE_NSS]);
2045 if (ri[NL80211_RATE_INFO_HE_GI])
2046 re->he_gi = nla_get_u8(ri[NL80211_RATE_INFO_HE_GI]);
2047 if (ri[NL80211_RATE_INFO_HE_DCM])
2048 re->he_dcm = nla_get_u8(ri[NL80211_RATE_INFO_HE_DCM]);
2049 }
2050 else if (ri[NL80211_RATE_INFO_VHT_MCS])
2051 {
2052 re->is_vht = 1;
2053 re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_VHT_MCS]);
2054
2055 if (ri[NL80211_RATE_INFO_VHT_NSS])
2056 re->nss = nla_get_u8(ri[NL80211_RATE_INFO_VHT_NSS]);
2057 }
2058 else if (ri[NL80211_RATE_INFO_MCS])
2059 {
2060 re->is_ht = 1;
2061 re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_MCS]);
2062 }
2063
2064 if (ri[NL80211_RATE_INFO_5_MHZ_WIDTH])
2065 re->mhz = 5;
2066 else if (ri[NL80211_RATE_INFO_10_MHZ_WIDTH])
2067 re->mhz = 10;
2068 else if (ri[NL80211_RATE_INFO_40_MHZ_WIDTH])
2069 re->mhz = 40;
2070 else if (ri[NL80211_RATE_INFO_80_MHZ_WIDTH])
2071 re->mhz = 80;
2072 else if (ri[NL80211_RATE_INFO_80P80_MHZ_WIDTH] ||
2073 ri[NL80211_RATE_INFO_160_MHZ_WIDTH])
2074 re->mhz = 160;
2075 else
2076 re->mhz = 20;
2077
2078 if (ri[NL80211_RATE_INFO_SHORT_GI])
2079 re->is_short_gi = 1;
2080
2081 re->is_40mhz = (re->mhz == 40);
2082 }
2083
2084 static int nl80211_get_survey_cb(struct nl_msg *msg, void *arg)
2085 {
2086 struct nl80211_array_buf *arr = arg;
2087 struct iwinfo_survey_entry *e = arr->buf;
2088 struct nlattr **attr = nl80211_parse(msg);
2089 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2090 int rc;
2091
2092 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2093 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2094 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2095 [NL80211_SURVEY_INFO_TIME] = { .type = NLA_U64 },
2096 [NL80211_SURVEY_INFO_TIME_BUSY] = { .type = NLA_U64 },
2097 [NL80211_SURVEY_INFO_TIME_EXT_BUSY] = { .type = NLA_U64 },
2098 [NL80211_SURVEY_INFO_TIME_RX] = { .type = NLA_U64 },
2099 [NL80211_SURVEY_INFO_TIME_TX] = { .type = NLA_U64 },
2100 };
2101
2102 rc = nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2103 attr[NL80211_ATTR_SURVEY_INFO],
2104 survey_policy);
2105 if (rc)
2106 return NL_SKIP;
2107
2108 /* advance to end of array */
2109 e += arr->count;
2110 memset(e, 0, sizeof(*e));
2111
2112 if (sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2113 e->mhz = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
2114
2115 if (sinfo[NL80211_SURVEY_INFO_NOISE])
2116 e->noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2117
2118 if (sinfo[NL80211_SURVEY_INFO_TIME])
2119 e->active_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME]);
2120
2121 if (sinfo[NL80211_SURVEY_INFO_TIME_BUSY])
2122 e->busy_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_BUSY]);
2123
2124 if (sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY])
2125 e->busy_time_ext = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]);
2126
2127 if (sinfo[NL80211_SURVEY_INFO_TIME_RX])
2128 e->rxtime = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_RX]);
2129
2130 if (sinfo[NL80211_SURVEY_INFO_TIME_TX])
2131 e->txtime = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_TX]);
2132
2133 arr->count++;
2134 return NL_SKIP;
2135 }
2136
2137
2138 static void plink_state_to_str(char *dst, unsigned state)
2139 {
2140 switch (state) {
2141 case NL80211_PLINK_LISTEN:
2142 strcpy(dst, "LISTEN");
2143 break;
2144 case NL80211_PLINK_OPN_SNT:
2145 strcpy(dst, "OPN_SNT");
2146 break;
2147 case NL80211_PLINK_OPN_RCVD:
2148 strcpy(dst, "OPN_RCVD");
2149 break;
2150 case NL80211_PLINK_CNF_RCVD:
2151 strcpy(dst, "CNF_RCVD");
2152 break;
2153 case NL80211_PLINK_ESTAB:
2154 strcpy(dst, "ESTAB");
2155 break;
2156 case NL80211_PLINK_HOLDING:
2157 strcpy(dst, "HOLDING");
2158 break;
2159 case NL80211_PLINK_BLOCKED:
2160 strcpy(dst, "BLOCKED");
2161 break;
2162 default:
2163 strcpy(dst, "UNKNOWN");
2164 break;
2165 }
2166 }
2167
2168 static void power_mode_to_str(char *dst, struct nlattr *a)
2169 {
2170 enum nl80211_mesh_power_mode pm = nla_get_u32(a);
2171
2172 switch (pm) {
2173 case NL80211_MESH_POWER_ACTIVE:
2174 strcpy(dst, "ACTIVE");
2175 break;
2176 case NL80211_MESH_POWER_LIGHT_SLEEP:
2177 strcpy(dst, "LIGHT SLEEP");
2178 break;
2179 case NL80211_MESH_POWER_DEEP_SLEEP:
2180 strcpy(dst, "DEEP SLEEP");
2181 break;
2182 default:
2183 strcpy(dst, "UNKNOWN");
2184 break;
2185 }
2186 }
2187
2188 static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
2189 {
2190 struct nl80211_array_buf *arr = arg;
2191 struct iwinfo_assoclist_entry *e = arr->buf;
2192 struct nlattr **attr = nl80211_parse(msg);
2193 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
2194 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
2195 struct nl80211_sta_flag_update *sta_flags;
2196
2197 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
2198 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
2199 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
2200 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
2201 [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED },
2202 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
2203 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
2204 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
2205 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
2206 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
2207 [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
2208 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
2209 [NL80211_STA_INFO_CONNECTED_TIME]= { .type = NLA_U32 },
2210 [NL80211_STA_INFO_RX_DROP_MISC] = { .type = NLA_U64 },
2211 [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 },
2212 [NL80211_STA_INFO_STA_FLAGS] =
2213 { .minlen = sizeof(struct nl80211_sta_flag_update) },
2214 [NL80211_STA_INFO_EXPECTED_THROUGHPUT] = { .type = NLA_U32 },
2215 /* mesh */
2216 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
2217 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
2218 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
2219 [NL80211_STA_INFO_LOCAL_PM] = { .type = NLA_U32 },
2220 [NL80211_STA_INFO_PEER_PM] = { .type = NLA_U32 },
2221 [NL80211_STA_INFO_NONPEER_PM] = { .type = NLA_U32 },
2222 };
2223
2224 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
2225 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
2226 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
2227 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
2228 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
2229 };
2230
2231 /* advance to end of array */
2232 e += arr->count;
2233 memset(e, 0, sizeof(*e));
2234
2235 if (attr[NL80211_ATTR_MAC])
2236 memcpy(e->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
2237
2238 if (attr[NL80211_ATTR_STA_INFO] &&
2239 !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
2240 attr[NL80211_ATTR_STA_INFO], stats_policy))
2241 {
2242 if (sinfo[NL80211_STA_INFO_SIGNAL])
2243 e->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
2244
2245 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
2246 e->signal_avg = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
2247
2248 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
2249 e->inactive = nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]);
2250
2251 if (sinfo[NL80211_STA_INFO_CONNECTED_TIME])
2252 e->connected_time = nla_get_u32(sinfo[NL80211_STA_INFO_CONNECTED_TIME]);
2253
2254 if (sinfo[NL80211_STA_INFO_RX_PACKETS])
2255 e->rx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]);
2256
2257 if (sinfo[NL80211_STA_INFO_TX_PACKETS])
2258 e->tx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]);
2259
2260 if (sinfo[NL80211_STA_INFO_RX_BITRATE] &&
2261 !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2262 sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy))
2263 nl80211_parse_rateinfo(rinfo, &e->rx_rate);
2264
2265 if (sinfo[NL80211_STA_INFO_TX_BITRATE] &&
2266 !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2267 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy))
2268 nl80211_parse_rateinfo(rinfo, &e->tx_rate);
2269
2270 if (sinfo[NL80211_STA_INFO_RX_BYTES])
2271 e->rx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]);
2272
2273 if (sinfo[NL80211_STA_INFO_TX_BYTES])
2274 e->tx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]);
2275
2276 if (sinfo[NL80211_STA_INFO_TX_RETRIES])
2277 e->tx_retries = nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]);
2278
2279 if (sinfo[NL80211_STA_INFO_TX_FAILED])
2280 e->tx_failed = nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]);
2281
2282 if (sinfo[NL80211_STA_INFO_T_OFFSET])
2283 e->t_offset = nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]);
2284
2285 if (sinfo[NL80211_STA_INFO_RX_DROP_MISC])
2286 e->rx_drop_misc = nla_get_u64(sinfo[NL80211_STA_INFO_RX_DROP_MISC]);
2287
2288 if (sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT])
2289 e->thr = nla_get_u32(sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]);
2290
2291 /* mesh */
2292 if (sinfo[NL80211_STA_INFO_LLID])
2293 e->llid = nla_get_u16(sinfo[NL80211_STA_INFO_LLID]);
2294
2295 if (sinfo[NL80211_STA_INFO_PLID])
2296 e->plid = nla_get_u16(sinfo[NL80211_STA_INFO_PLID]);
2297
2298 if (sinfo[NL80211_STA_INFO_PLINK_STATE])
2299 plink_state_to_str(e->plink_state,
2300 nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE]));
2301
2302 if (sinfo[NL80211_STA_INFO_LOCAL_PM])
2303 power_mode_to_str(e->local_ps, sinfo[NL80211_STA_INFO_LOCAL_PM]);
2304 if (sinfo[NL80211_STA_INFO_PEER_PM])
2305 power_mode_to_str(e->peer_ps, sinfo[NL80211_STA_INFO_PEER_PM]);
2306 if (sinfo[NL80211_STA_INFO_NONPEER_PM])
2307 power_mode_to_str(e->nonpeer_ps, sinfo[NL80211_STA_INFO_NONPEER_PM]);
2308
2309 /* Station flags */
2310 if (sinfo[NL80211_STA_INFO_STA_FLAGS])
2311 {
2312 sta_flags = (struct nl80211_sta_flag_update *)
2313 nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]);
2314
2315 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
2316 sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED))
2317 e->is_authorized = 1;
2318
2319 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
2320 sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
2321 e->is_authenticated = 1;
2322
2323 if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) &&
2324 sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
2325 e->is_preamble_short = 1;
2326
2327 if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME) &&
2328 sta_flags->set & BIT(NL80211_STA_FLAG_WME))
2329 e->is_wme = 1;
2330
2331 if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP) &&
2332 sta_flags->set & BIT(NL80211_STA_FLAG_MFP))
2333 e->is_mfp = 1;
2334
2335 if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER) &&
2336 sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2337 e->is_tdls = 1;
2338 }
2339 }
2340
2341 e->noise = 0; /* filled in by caller */
2342 arr->count++;
2343
2344 return NL_SKIP;
2345 }
2346
2347 static int nl80211_get_survey(const char *ifname, char *buf, int *len)
2348 {
2349 struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
2350 int rc;
2351
2352 rc = nl80211_request(ifname, NL80211_CMD_GET_SURVEY,
2353 NLM_F_DUMP, nl80211_get_survey_cb, &arr);
2354 if (!rc)
2355 *len = (arr.count * sizeof(struct iwinfo_survey_entry));
2356 else
2357 *len = 0;
2358
2359 return 0;
2360 }
2361
2362 static int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
2363 {
2364 DIR *d;
2365 int i, noise = 0;
2366 struct dirent *de;
2367 struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
2368 struct iwinfo_assoclist_entry *e;
2369
2370 if ((d = opendir("/sys/class/net")) != NULL)
2371 {
2372 while ((de = readdir(d)) != NULL)
2373 {
2374 if (!strncmp(de->d_name, ifname, strlen(ifname)) &&
2375 (!de->d_name[strlen(ifname)] ||
2376 !strncmp(&de->d_name[strlen(ifname)], ".sta", 4)))
2377 {
2378 nl80211_request(de->d_name, NL80211_CMD_GET_STATION,
2379 NLM_F_DUMP, nl80211_get_assoclist_cb, &arr);
2380 }
2381 }
2382
2383 closedir(d);
2384
2385 if (!nl80211_get_noise(ifname, &noise))
2386 for (i = 0, e = arr.buf; i < arr.count; i++, e++)
2387 e->noise = noise;
2388
2389 *len = (arr.count * sizeof(struct iwinfo_assoclist_entry));
2390 return 0;
2391 }
2392
2393 return -1;
2394 }
2395
2396 static int nl80211_get_txpwrlist_cb(struct nl_msg *msg, void *arg)
2397 {
2398 int *dbm_max = arg;
2399 int ch_cur, ch_cmp, bands_remain, freqs_remain;
2400
2401 struct nlattr **attr = nl80211_parse(msg);
2402 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
2403 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
2404 struct nlattr *band, *freq;
2405
2406 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
2407 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
2408 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
2409 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
2410 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
2411 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
2412 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
2413 };
2414
2415 ch_cur = *dbm_max; /* value int* is initialized with channel by caller */
2416 *dbm_max = -1;
2417
2418 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
2419 {
2420 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
2421 nla_len(band), NULL);
2422
2423 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
2424 {
2425 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
2426 nla_data(freq), nla_len(freq), freq_policy);
2427
2428 ch_cmp = nl80211_freq2channel(nla_get_u32(
2429 freqs[NL80211_FREQUENCY_ATTR_FREQ]));
2430
2431 if ((!ch_cur || (ch_cmp == ch_cur)) &&
2432 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER])
2433 {
2434 *dbm_max = (int)(0.01 * nla_get_u32(
2435 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
2436
2437 break;
2438 }
2439 }
2440 }
2441
2442 return NL_SKIP;
2443 }
2444
2445 static int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
2446 {
2447 int err, ch_cur;
2448 int dbm_max = -1, dbm_cur, dbm_cnt;
2449 struct iwinfo_txpwrlist_entry entry;
2450
2451 if (nl80211_get_channel(ifname, &ch_cur))
2452 ch_cur = 0;
2453
2454 /* initialize the value pointer with channel for callback */
2455 dbm_max = ch_cur;
2456
2457 err = nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
2458 nl80211_get_txpwrlist_cb, &dbm_max);
2459
2460 if (!err)
2461 {
2462 for (dbm_cur = 0, dbm_cnt = 0;
2463 dbm_cur < dbm_max;
2464 dbm_cur++, dbm_cnt++)
2465 {
2466 entry.dbm = dbm_cur;
2467 entry.mw = iwinfo_dbm2mw(dbm_cur);
2468
2469 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
2470 }
2471
2472 entry.dbm = dbm_max;
2473 entry.mw = iwinfo_dbm2mw(dbm_max);
2474
2475 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
2476 dbm_cnt++;
2477
2478 *len = dbm_cnt * sizeof(entry);
2479 return 0;
2480 }
2481
2482 return -1;
2483 }
2484
2485 static void nl80211_get_scancrypto(char *spec, struct iwinfo_crypto_entry *c)
2486 {
2487 int wpa_version = 0;
2488 char *p, *q, *proto, *suites;
2489
2490 c->enabled = 0;
2491
2492 for (p = strtok_r(spec, "[]", &q); p; p = strtok_r(NULL, "[]", &q)) {
2493 if (!strcmp(p, "WEP")) {
2494 c->enabled = 1;
2495 c->auth_suites = IWINFO_KMGMT_NONE;
2496 c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
2497 c->pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
2498 break;
2499 }
2500
2501 proto = strtok(p, "-");
2502 suites = strtok(NULL, "]");
2503
2504 if (!proto || !suites)
2505 continue;
2506
2507 if (!strcmp(proto, "WPA2") || !strcmp(proto, "RSN"))
2508 wpa_version = 2;
2509 else if (!strcmp(proto, "WPA"))
2510 wpa_version = 1;
2511 else
2512 continue;
2513
2514 c->enabled = 1;
2515
2516 parse_wpa_suites(suites, wpa_version, &c->wpa_version, &c->auth_suites);
2517 parse_wpa_ciphers(suites, &c->pair_ciphers);
2518 }
2519 }
2520
2521
2522 struct nl80211_scanlist {
2523 struct iwinfo_scanlist_entry *e;
2524 int len;
2525 };
2526
2527
2528 static void nl80211_get_scanlist_ie(struct nlattr **bss,
2529 struct iwinfo_scanlist_entry *e)
2530 {
2531 int ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
2532 unsigned char *ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
2533 static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 };
2534 int len;
2535
2536 while (ielen >= 2 && ielen >= ie[1])
2537 {
2538 switch (ie[0])
2539 {
2540 case 0: /* SSID */
2541 case 114: /* Mesh ID */
2542 if (e->ssid[0] == 0) {
2543 len = min(ie[1], IWINFO_ESSID_MAX_SIZE);
2544 memcpy(e->ssid, ie + 2, len);
2545 e->ssid[len] = 0;
2546 }
2547 break;
2548
2549 case 48: /* RSN */
2550 iwinfo_parse_rsn(&e->crypto, ie + 2, ie[1],
2551 IWINFO_CIPHER_CCMP, IWINFO_KMGMT_8021x);
2552 break;
2553
2554 case 221: /* Vendor */
2555 if (ie[1] >= 4 && !memcmp(ie + 2, ms_oui, 3) && ie[5] == 1)
2556 iwinfo_parse_rsn(&e->crypto, ie + 6, ie[1] - 4,
2557 IWINFO_CIPHER_TKIP, IWINFO_KMGMT_PSK);
2558 break;
2559 case 61: /* HT oeration */
2560 if (ie[1] >= 3) {
2561 e->ht_chan_info.primary_chan = ie[2];
2562 e->ht_chan_info.secondary_chan_off = ie[3] & 0x3;
2563 e->ht_chan_info.chan_width = (ie[4] & 0x4)>>2;
2564 }
2565 break;
2566 case 192: /* VHT operation */
2567 if (ie[1] >= 3) {
2568 e->vht_chan_info.chan_width = ie[2];
2569 e->vht_chan_info.center_chan_1 = ie[3];
2570 e->vht_chan_info.center_chan_2 = ie[4];
2571 }
2572 break;
2573 }
2574
2575 ielen -= ie[1] + 2;
2576 ie += ie[1] + 2;
2577 }
2578 }
2579
2580 static int nl80211_get_scanlist_cb(struct nl_msg *msg, void *arg)
2581 {
2582 int8_t rssi;
2583 uint16_t caps;
2584
2585 struct nl80211_scanlist *sl = arg;
2586 struct nlattr **tb = nl80211_parse(msg);
2587 struct nlattr *bss[NL80211_BSS_MAX + 1];
2588
2589 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
2590 [NL80211_BSS_TSF] = { .type = NLA_U64 },
2591 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
2592 [NL80211_BSS_BSSID] = { 0 },
2593 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
2594 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
2595 [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 },
2596 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
2597 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
2598 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
2599 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
2600 [NL80211_BSS_BEACON_IES] = { 0 },
2601 };
2602
2603 if (!tb[NL80211_ATTR_BSS] ||
2604 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
2605 bss_policy) ||
2606 !bss[NL80211_BSS_BSSID])
2607 {
2608 return NL_SKIP;
2609 }
2610
2611 if (bss[NL80211_BSS_CAPABILITY])
2612 caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
2613 else
2614 caps = 0;
2615
2616 memset(sl->e, 0, sizeof(*sl->e));
2617 memcpy(sl->e->mac, nla_data(bss[NL80211_BSS_BSSID]), 6);
2618
2619 if (caps & (1<<1))
2620 sl->e->mode = IWINFO_OPMODE_ADHOC;
2621 else if (caps & (1<<0))
2622 sl->e->mode = IWINFO_OPMODE_MASTER;
2623 else
2624 sl->e->mode = IWINFO_OPMODE_MESHPOINT;
2625
2626 if (caps & (1<<4))
2627 sl->e->crypto.enabled = 1;
2628
2629 if (bss[NL80211_BSS_FREQUENCY])
2630 {
2631 sl->e->mhz = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
2632 sl->e->band = nl80211_freq2band(sl->e->mhz);
2633 sl->e->channel = nl80211_freq2channel(sl->e->mhz);
2634 }
2635
2636 if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
2637 nl80211_get_scanlist_ie(bss, sl->e);
2638
2639 if (bss[NL80211_BSS_SIGNAL_MBM])
2640 {
2641 sl->e->signal =
2642 (uint8_t)((int32_t)nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]) / 100);
2643
2644 rssi = sl->e->signal - 0x100;
2645
2646 if (rssi < -110)
2647 rssi = -110;
2648 else if (rssi > -40)
2649 rssi = -40;
2650
2651 sl->e->quality = (rssi + 110);
2652 sl->e->quality_max = 70;
2653 }
2654
2655 if (sl->e->crypto.enabled && !sl->e->crypto.wpa_version)
2656 {
2657 sl->e->crypto.auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
2658 sl->e->crypto.pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
2659 }
2660
2661 sl->e++;
2662 sl->len++;
2663
2664 return NL_SKIP;
2665 }
2666
2667 static int nl80211_get_scanlist_nl(const char *ifname, char *buf, int *len)
2668 {
2669 struct nl80211_scanlist sl = { .e = (struct iwinfo_scanlist_entry *)buf };
2670
2671 if (nl80211_request(ifname, NL80211_CMD_TRIGGER_SCAN, 0, NULL, NULL))
2672 goto out;
2673
2674 if (nl80211_wait("nl80211", "scan",
2675 NL80211_CMD_NEW_SCAN_RESULTS, NL80211_CMD_SCAN_ABORTED))
2676 goto out;
2677
2678 if (nl80211_request(ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
2679 nl80211_get_scanlist_cb, &sl))
2680 goto out;
2681
2682 *len = sl.len * sizeof(struct iwinfo_scanlist_entry);
2683 return 0;
2684
2685 out:
2686 *len = 0;
2687 return -1;
2688 }
2689
2690 static int wpasupp_ssid_decode(const char *in, char *out, int outlen)
2691 {
2692 #define hex(x) \
2693 (((x) >= 'a') ? ((x) - 'a' + 10) : \
2694 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
2695
2696 int len = 0;
2697
2698 while (*in)
2699 {
2700 if (len + 1 >= outlen)
2701 break;
2702
2703 switch (*in)
2704 {
2705 case '\\':
2706 in++;
2707 switch (*in)
2708 {
2709 case 'n':
2710 out[len++] = '\n'; in++;
2711 break;
2712
2713 case 'r':
2714 out[len++] = '\r'; in++;
2715 break;
2716
2717 case 't':
2718 out[len++] = '\t'; in++;
2719 break;
2720
2721 case 'e':
2722 out[len++] = '\033'; in++;
2723 break;
2724
2725 case 'x':
2726 if (isxdigit(*(in+1)) && isxdigit(*(in+2)))
2727 out[len++] = hex(*(in+1)) * 16 + hex(*(in+2));
2728 in += 3;
2729 break;
2730
2731 default:
2732 out[len++] = *in++;
2733 break;
2734 }
2735 break;
2736
2737 default:
2738 out[len++] = *in++;
2739 break;
2740 }
2741 }
2742
2743 if (outlen > len)
2744 out[len] = '\0';
2745
2746 return len;
2747 }
2748
2749 static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len)
2750 {
2751 int sock, qmax, rssi, tries, count = -1, ready = 0;
2752 char *pos, *line, *bssid, *freq, *signal, *flags, *ssid, reply[4096];
2753 struct sockaddr_un local = { 0 };
2754 struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
2755
2756 sock = nl80211_wpactl_connect(ifname, &local);
2757
2758 if (sock < 0)
2759 return sock;
2760
2761 send(sock, "ATTACH", 6, 0);
2762 send(sock, "SCAN", 4, 0);
2763
2764 /*
2765 * wait for scan results:
2766 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
2767 * 72 channels at most. We'll also receive two "OK" messages acknowledging
2768 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
2769 * time to process the results, so try 72 + 2 + 1 times.
2770 */
2771 for (tries = 0; tries < 75; tries++)
2772 {
2773 if (nl80211_wpactl_recv(sock, reply, sizeof(reply)) <= 0)
2774 continue;
2775
2776 /* got an event notification */
2777 if (reply[0] == '<')
2778 {
2779 /* scan results are ready */
2780 if (strstr(reply, "CTRL-EVENT-SCAN-RESULTS"))
2781 {
2782 /* send "SCAN_RESULTS" command */
2783 ready = (send(sock, "SCAN_RESULTS", 12, 0) == 12);
2784 break;
2785 }
2786
2787 /* is another unrelated event, retry */
2788 tries--;
2789 }
2790
2791 /* scanning already in progress, keep awaiting results */
2792 else if (!strcmp(reply, "FAIL-BUSY\n"))
2793 {
2794 tries--;
2795 }
2796
2797 /* another failure, abort */
2798 else if (!strncmp(reply, "FAIL-", 5))
2799 {
2800 break;
2801 }
2802 }
2803
2804 /* receive and parse scan results if the wait above didn't time out */
2805 while (ready && nl80211_wpactl_recv(sock, reply, sizeof(reply)) > 0)
2806 {
2807 /* received an event notification, receive again */
2808 if (reply[0] == '<')
2809 continue;
2810
2811 nl80211_get_quality_max(ifname, &qmax);
2812
2813 for (line = strtok_r(reply, "\n", &pos);
2814 line != NULL;
2815 line = strtok_r(NULL, "\n", &pos))
2816 {
2817 /* skip header line */
2818 if (count < 0)
2819 {
2820 count++;
2821 continue;
2822 }
2823
2824 bssid = strtok(line, "\t");
2825 freq = strtok(NULL, "\t");
2826 signal = strtok(NULL, "\t");
2827 flags = strtok(NULL, "\t");
2828 ssid = strtok(NULL, "\n");
2829
2830 if (!bssid || !freq || !signal || !flags)
2831 continue;
2832
2833 /* BSSID */
2834 e->mac[0] = strtol(&bssid[0], NULL, 16);
2835 e->mac[1] = strtol(&bssid[3], NULL, 16);
2836 e->mac[2] = strtol(&bssid[6], NULL, 16);
2837 e->mac[3] = strtol(&bssid[9], NULL, 16);
2838 e->mac[4] = strtol(&bssid[12], NULL, 16);
2839 e->mac[5] = strtol(&bssid[15], NULL, 16);
2840
2841 /* SSID */
2842 if (ssid)
2843 wpasupp_ssid_decode(ssid, e->ssid, sizeof(e->ssid));
2844 else
2845 e->ssid[0] = 0;
2846
2847 /* Mode */
2848 if (strstr(flags, "[MESH]"))
2849 e->mode = IWINFO_OPMODE_MESHPOINT;
2850 else if (strstr(flags, "[IBSS]"))
2851 e->mode = IWINFO_OPMODE_ADHOC;
2852 else
2853 e->mode = IWINFO_OPMODE_MASTER;
2854
2855 /* Channel */
2856 e->mhz = atoi(freq);
2857 e->band = nl80211_freq2band(e->mhz);
2858 e->channel = nl80211_freq2channel(e->mhz);
2859
2860 /* Signal */
2861 rssi = atoi(signal);
2862 e->signal = rssi;
2863
2864 /* Quality */
2865 if (rssi < 0)
2866 {
2867 /* The cfg80211 wext compat layer assumes a signal range
2868 * of -110 dBm to -40 dBm, the quality value is derived
2869 * by adding 110 to the signal level */
2870 if (rssi < -110)
2871 rssi = -110;
2872 else if (rssi > -40)
2873 rssi = -40;
2874
2875 e->quality = (rssi + 110);
2876 }
2877 else
2878 {
2879 e->quality = rssi;
2880 }
2881
2882 /* Max. Quality */
2883 e->quality_max = qmax;
2884
2885 /* Crypto */
2886 nl80211_get_scancrypto(flags, &e->crypto);
2887
2888 count++;
2889 e++;
2890 }
2891
2892 *len = count * sizeof(struct iwinfo_scanlist_entry);
2893 break;
2894 }
2895
2896 close(sock);
2897 unlink(local.sun_path);
2898
2899 return (count >= 0) ? 0 : -1;
2900 }
2901
2902 static int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
2903 {
2904 char *res;
2905 int rv, mode;
2906
2907 *len = 0;
2908
2909 /* Got a radioX pseudo interface, find some interface on it or create one */
2910 if (!strncmp(ifname, "radio", 5))
2911 {
2912 /* Reuse existing interface */
2913 if ((res = nl80211_phy2ifname(ifname)) != NULL)
2914 {
2915 return nl80211_get_scanlist(res, buf, len);
2916 }
2917
2918 /* Need to spawn a temporary iface for scanning */
2919 else if ((res = nl80211_ifadd(ifname)) != NULL)
2920 {
2921 rv = nl80211_get_scanlist(res, buf, len);
2922 nl80211_ifdel(res);
2923 return rv;
2924 }
2925 }
2926
2927 /* WPA supplicant */
2928 if (!nl80211_get_scanlist_wpactl(ifname, buf, len))
2929 {
2930 return 0;
2931 }
2932
2933 /* station / ad-hoc / monitor scan */
2934 else if (!nl80211_get_mode(ifname, &mode) &&
2935 (mode == IWINFO_OPMODE_ADHOC ||
2936 mode == IWINFO_OPMODE_MASTER ||
2937 mode == IWINFO_OPMODE_CLIENT ||
2938 mode == IWINFO_OPMODE_MONITOR) &&
2939 iwinfo_ifup(ifname))
2940 {
2941 return nl80211_get_scanlist_nl(ifname, buf, len);
2942 }
2943
2944 /* AP scan */
2945 else
2946 {
2947 /* Got a temp interface, don't create yet another one */
2948 if (!strncmp(ifname, "tmp.", 4))
2949 {
2950 if (!iwinfo_ifup(ifname))
2951 return -1;
2952
2953 rv = nl80211_get_scanlist_nl(ifname, buf, len);
2954 iwinfo_ifdown(ifname);
2955 return rv;
2956 }
2957
2958 /* Spawn a new scan interface */
2959 else
2960 {
2961 if (!(res = nl80211_ifadd(ifname)))
2962 return -1;
2963
2964 iwinfo_ifmac(res);
2965
2966 /* if we can take the new interface up, the driver supports an
2967 * additional interface and there's no need to tear down the ap */
2968 if (iwinfo_ifup(res))
2969 {
2970 rv = nl80211_get_scanlist_nl(res, buf, len);
2971 iwinfo_ifdown(res);
2972 }
2973
2974 /* driver cannot create secondary interface, take down ap
2975 * during scan */
2976 else if (iwinfo_ifdown(ifname) && iwinfo_ifup(res))
2977 {
2978 rv = nl80211_get_scanlist_nl(res, buf, len);
2979 iwinfo_ifdown(res);
2980 iwinfo_ifup(ifname);
2981 nl80211_hostapd_hup(ifname);
2982 }
2983 else
2984 rv = -1;
2985
2986 nl80211_ifdel(res);
2987 return rv;
2988 }
2989 }
2990
2991 return -1;
2992 }
2993
2994 static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg)
2995 {
2996 int bands_remain, freqs_remain;
2997
2998 struct nl80211_array_buf *arr = arg;
2999 struct iwinfo_freqlist_entry *e;
3000
3001 struct nlattr **attr = nl80211_parse(msg);
3002 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
3003 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
3004 struct nlattr *band, *freq;
3005
3006 e = arr->buf;
3007 e += arr->count;
3008
3009 if (attr[NL80211_ATTR_WIPHY_BANDS]) {
3010 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
3011 {
3012 nla_parse(bands, NL80211_BAND_ATTR_MAX,
3013 nla_data(band), nla_len(band), NULL);
3014
3015 if (bands[NL80211_BAND_ATTR_FREQS]) {
3016 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
3017 {
3018 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
3019 nla_data(freq), nla_len(freq), NULL);
3020
3021 if (!freqs[NL80211_FREQUENCY_ATTR_FREQ] ||
3022 freqs[NL80211_FREQUENCY_ATTR_DISABLED])
3023 continue;
3024
3025 e->band = nl80211_get_band(band->nla_type);
3026 e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
3027 e->channel = nl80211_freq2channel(e->mhz);
3028
3029 if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS])
3030 e->flags |= IWINFO_FREQ_NO_HT40MINUS;
3031 if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS])
3032 e->flags |= IWINFO_FREQ_NO_HT40PLUS;
3033 if (freqs[NL80211_FREQUENCY_ATTR_NO_80MHZ])
3034 e->flags |= IWINFO_FREQ_NO_80MHZ;
3035 if (freqs[NL80211_FREQUENCY_ATTR_NO_160MHZ])
3036 e->flags |= IWINFO_FREQ_NO_160MHZ;
3037 if (freqs[NL80211_FREQUENCY_ATTR_NO_20MHZ])
3038 e->flags |= IWINFO_FREQ_NO_20MHZ;
3039 if (freqs[NL80211_FREQUENCY_ATTR_NO_10MHZ])
3040 e->flags |= IWINFO_FREQ_NO_10MHZ;
3041 if (freqs[NL80211_FREQUENCY_ATTR_NO_HE])
3042 e->flags |= IWINFO_FREQ_NO_HE;
3043 if (freqs[NL80211_FREQUENCY_ATTR_NO_IR] &&
3044 !freqs[NL80211_FREQUENCY_ATTR_RADAR])
3045 e->flags |= IWINFO_FREQ_NO_IR;
3046 if (freqs[NL80211_FREQUENCY_ATTR_INDOOR_ONLY])
3047 e->flags |= IWINFO_FREQ_INDOOR_ONLY;
3048
3049 /* keep backwards compatibility */
3050 e->restricted = (e->flags & IWINFO_FREQ_NO_IR) ? 1 : 0;
3051
3052 e++;
3053 arr->count++;
3054 }
3055 }
3056 }
3057 }
3058
3059 return NL_SKIP;
3060 }
3061
3062 static int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
3063 {
3064 struct nl80211_msg_conveyor *cv;
3065 struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
3066 uint32_t features = nl80211_get_protocol_features(ifname);
3067 int flags;
3068
3069 flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0;
3070 cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags);
3071 if (!cv)
3072 goto out;
3073
3074 NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
3075 if (nl80211_send(cv, nl80211_get_freqlist_cb, &arr))
3076 goto out;
3077
3078 *len = arr.count * sizeof(struct iwinfo_freqlist_entry);
3079 return 0;
3080
3081 nla_put_failure:
3082 nl80211_free(cv);
3083 out:
3084 *len = 0;
3085 return -1;
3086 }
3087
3088 static int nl80211_get_country_cb(struct nl_msg *msg, void *arg)
3089 {
3090 char *buf = arg;
3091 struct nlattr **attr = nl80211_parse(msg);
3092
3093 if (attr[NL80211_ATTR_REG_ALPHA2])
3094 memcpy(buf, nla_data(attr[NL80211_ATTR_REG_ALPHA2]), 2);
3095 else
3096 buf[0] = 0;
3097
3098 return NL_SKIP;
3099 }
3100
3101 static int nl80211_get_country(const char *ifname, char *buf)
3102 {
3103 if (nl80211_request(ifname, NL80211_CMD_GET_REG, 0,
3104 nl80211_get_country_cb, buf))
3105 return -1;
3106
3107 return 0;
3108 }
3109
3110 static int nl80211_get_countrylist(const char *ifname, char *buf, int *len)
3111 {
3112 int count;
3113 struct iwinfo_country_entry *e = (struct iwinfo_country_entry *)buf;
3114 const struct iwinfo_iso3166_label *l;
3115
3116 for (l = IWINFO_ISO3166_NAMES, count = 0; l->iso3166; l++, e++, count++)
3117 {
3118 e->iso3166 = l->iso3166;
3119 e->ccode[0] = (l->iso3166 / 256);
3120 e->ccode[1] = (l->iso3166 % 256);
3121 e->ccode[2] = 0;
3122 }
3123
3124 *len = (count * sizeof(struct iwinfo_country_entry));
3125 return 0;
3126 }
3127
3128
3129 struct nl80211_modes
3130 {
3131 bool ok;
3132 uint32_t hw;
3133 uint32_t ht;
3134
3135 uint8_t bands;
3136
3137 uint16_t nl_ht;
3138 uint32_t nl_vht;
3139 uint16_t he_phy_cap[6];
3140 };
3141
3142 static void nl80211_eval_modelist(struct nl80211_modes *m)
3143 {
3144 /* Treat any nonzero capability as 11n */
3145 if (m->nl_ht > 0)
3146 {
3147 m->hw |= IWINFO_80211_N;
3148 m->ht |= IWINFO_HTMODE_HT20;
3149
3150 if (m->nl_ht & (1 << 1))
3151 m->ht |= IWINFO_HTMODE_HT40;
3152 }
3153
3154 if (m->he_phy_cap[0] != 0) {
3155 m->hw |= IWINFO_80211_AX;
3156 m->ht |= IWINFO_HTMODE_HE20;
3157
3158 if (m->he_phy_cap[0] & BIT(9))
3159 m->ht |= IWINFO_HTMODE_HE40;
3160 if (m->he_phy_cap[0] & BIT(10))
3161 m->ht |= IWINFO_HTMODE_HE40 | IWINFO_HTMODE_HE80;
3162 if (m->he_phy_cap[0] & BIT(11))
3163 m->ht |= IWINFO_HTMODE_HE160;
3164 if (m->he_phy_cap[0] & BIT(12))
3165 m->ht |= IWINFO_HTMODE_HE160 | IWINFO_HTMODE_HE80_80;
3166 }
3167
3168 if (m->bands & IWINFO_BAND_24)
3169 {
3170 m->hw |= IWINFO_80211_B;
3171 m->hw |= IWINFO_80211_G;
3172 }
3173
3174 if (m->bands & IWINFO_BAND_5)
3175 {
3176 /* Treat any nonzero capability as 11ac */
3177 if (m->nl_vht > 0)
3178 {
3179 m->hw |= IWINFO_80211_AC;
3180 m->ht |= IWINFO_HTMODE_VHT20 | IWINFO_HTMODE_VHT40 | IWINFO_HTMODE_VHT80;
3181
3182 switch ((m->nl_vht >> 2) & 3)
3183 {
3184 case 2:
3185 m->ht |= IWINFO_HTMODE_VHT80_80;
3186 /* fall through */
3187
3188 case 1:
3189 m->ht |= IWINFO_HTMODE_VHT160;
3190 }
3191 }
3192 else
3193 {
3194 m->hw |= IWINFO_80211_A;
3195 }
3196 }
3197
3198 if (m->bands & IWINFO_BAND_60)
3199 {
3200 m->hw |= IWINFO_80211_AD;
3201 }
3202
3203 }
3204
3205 static int nl80211_get_modelist_cb(struct nl_msg *msg, void *arg)
3206 {
3207 struct nl80211_modes *m = arg;
3208 int bands_remain;
3209 struct nlattr **attr = nl80211_parse(msg);
3210 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
3211 struct nlattr *band;
3212
3213 if (attr[NL80211_ATTR_WIPHY_BANDS])
3214 {
3215 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
3216 {
3217 m->bands |= nl80211_get_band(band->nla_type);
3218
3219 nla_parse(bands, NL80211_BAND_ATTR_MAX,
3220 nla_data(band), nla_len(band), NULL);
3221
3222 if (bands[NL80211_BAND_ATTR_HT_CAPA])
3223 m->nl_ht = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]);
3224
3225 if (bands[NL80211_BAND_ATTR_VHT_CAPA])
3226 m->nl_vht = nla_get_u32(bands[NL80211_BAND_ATTR_VHT_CAPA]);
3227
3228 if (bands[NL80211_BAND_ATTR_IFTYPE_DATA]) {
3229 struct nlattr *tb[NL80211_BAND_IFTYPE_ATTR_MAX + 1];
3230 struct nlattr *nl_iftype;
3231 int rem_band;
3232 int len;
3233
3234 nla_for_each_nested(nl_iftype, bands[NL80211_BAND_ATTR_IFTYPE_DATA], rem_band) {
3235 nla_parse(tb, NL80211_BAND_IFTYPE_ATTR_MAX,
3236 nla_data(nl_iftype), nla_len(nl_iftype), NULL);
3237 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]) {
3238 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]);
3239
3240 if (len > sizeof(m->he_phy_cap) - 1)
3241 len = sizeof(m->he_phy_cap) - 1;
3242 memcpy(&((__u8 *)m->he_phy_cap)[1],
3243 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]),
3244 len);
3245 }
3246 }
3247 }
3248 }
3249
3250 m->ok = 1;
3251 }
3252
3253 return NL_SKIP;
3254 }
3255
3256 static int nl80211_get_hwmodelist(const char *ifname, int *buf)
3257 {
3258 struct nl80211_msg_conveyor *cv;
3259 struct nl80211_modes m = {};
3260 uint32_t features = nl80211_get_protocol_features(ifname);
3261 int flags;
3262
3263 flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0;
3264 cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags);
3265 if (!cv)
3266 goto out;
3267
3268 NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
3269 if (nl80211_send(cv, nl80211_get_modelist_cb, &m))
3270 goto nla_put_failure;
3271
3272 nl80211_eval_modelist(&m);
3273
3274 *buf = m.hw;
3275
3276 return 0;
3277
3278 nla_put_failure:
3279 nl80211_free(cv);
3280 out:
3281 return -1;
3282 }
3283
3284 struct chan_info {
3285 int width;
3286 int mode;
3287 };
3288
3289 static int nl80211_get_htmode_cb(struct nl_msg *msg, void *arg)
3290 {
3291 struct nlattr **tb = nl80211_parse(msg);
3292 struct nlattr *cur;
3293 struct chan_info *chn = arg;
3294
3295 if ((cur = tb[NL80211_ATTR_CHANNEL_WIDTH]))
3296 chn->width = nla_get_u32(cur);
3297
3298 if ((cur = tb[NL80211_ATTR_BSS_HT_OPMODE]))
3299 chn->mode = nla_get_u32(cur);
3300
3301 return NL_SKIP;
3302 }
3303
3304 static int nl80211_get_htmode(const char *ifname, int *buf)
3305 {
3306 struct chan_info chn = { 0 };
3307 char *res, b[2] = { 0 };
3308 int err;
3309 bool he = false;
3310
3311 res = nl80211_phy2ifname(ifname);
3312 *buf = 0;
3313
3314 err = nl80211_request(res ? res : ifname,
3315 NL80211_CMD_GET_INTERFACE, 0,
3316 nl80211_get_htmode_cb, &chn);
3317 if (err)
3318 return -1;
3319
3320 if (nl80211_hostapd_query(res ? res : ifname, "ieee80211ax", b, sizeof(b)))
3321 he = b[0] == '1';
3322 else if (nl80211_wpactl_query(res ? res : ifname, "wifi_generation", b, sizeof(b)))
3323 he = b[0] == '6';
3324
3325 switch (chn.width) {
3326 case NL80211_CHAN_WIDTH_20:
3327 if (he)
3328 *buf = IWINFO_HTMODE_HE20;
3329 else if (chn.mode == -1)
3330 *buf = IWINFO_HTMODE_VHT20;
3331 else
3332 *buf = IWINFO_HTMODE_HT20;
3333 break;
3334 case NL80211_CHAN_WIDTH_40:
3335 if (he)
3336 *buf = IWINFO_HTMODE_HE40;
3337 else if (chn.mode == -1)
3338 *buf = IWINFO_HTMODE_VHT40;
3339 else
3340 *buf = IWINFO_HTMODE_HT40;
3341 break;
3342 case NL80211_CHAN_WIDTH_80:
3343 if (he)
3344 *buf = IWINFO_HTMODE_HE80;
3345 else
3346 *buf = IWINFO_HTMODE_VHT80;
3347 break;
3348 case NL80211_CHAN_WIDTH_80P80:
3349 if (he)
3350 *buf = IWINFO_HTMODE_HE80_80;
3351 else
3352 *buf = IWINFO_HTMODE_VHT80_80;
3353 break;
3354 case NL80211_CHAN_WIDTH_160:
3355 if (he)
3356 *buf = IWINFO_HTMODE_HE160;
3357 else
3358 *buf = IWINFO_HTMODE_VHT160;
3359 break;
3360 case NL80211_CHAN_WIDTH_5:
3361 case NL80211_CHAN_WIDTH_10:
3362 case NL80211_CHAN_WIDTH_20_NOHT:
3363 *buf = IWINFO_HTMODE_NOHT;
3364 break;
3365 default:
3366 return -1;
3367 }
3368
3369 return 0;
3370 }
3371
3372 static int nl80211_get_htmodelist(const char *ifname, int *buf)
3373 {
3374 struct nl80211_msg_conveyor *cv;
3375 struct nl80211_modes m = {};
3376 uint32_t features = nl80211_get_protocol_features(ifname);
3377 int flags;
3378
3379 flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0;
3380 cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags);
3381 if (!cv)
3382 goto out;
3383
3384 NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
3385 if (nl80211_send(cv, nl80211_get_modelist_cb, &m))
3386 goto nla_put_failure;
3387
3388 nl80211_eval_modelist(&m);
3389
3390 *buf = m.ht;
3391
3392 return 0;
3393
3394 nla_put_failure:
3395 nl80211_free(cv);
3396 out:
3397 return -1;
3398 }
3399
3400
3401 static int nl80211_get_ifcomb_cb(struct nl_msg *msg, void *arg)
3402 {
3403 struct nlattr **attr = nl80211_parse(msg);
3404 struct nlattr *comb;
3405 int *ret = arg;
3406 int comb_rem, limit_rem, mode_rem;
3407
3408 *ret = 0;
3409 if (!attr[NL80211_ATTR_INTERFACE_COMBINATIONS])
3410 return NL_SKIP;
3411
3412 nla_for_each_nested(comb, attr[NL80211_ATTR_INTERFACE_COMBINATIONS], comb_rem)
3413 {
3414 static struct nla_policy iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
3415 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
3416 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
3417 };
3418 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB+1];
3419 static struct nla_policy iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
3420 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
3421 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
3422 };
3423 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT+1];
3424 struct nlattr *limit;
3425
3426 nla_parse_nested(tb_comb, NUM_NL80211_IFACE_COMB, comb, iface_combination_policy);
3427
3428 if (!tb_comb[NL80211_IFACE_COMB_LIMITS])
3429 continue;
3430
3431 nla_for_each_nested(limit, tb_comb[NL80211_IFACE_COMB_LIMITS], limit_rem)
3432 {
3433 struct nlattr *mode;
3434
3435 nla_parse_nested(tb_limit, NUM_NL80211_IFACE_LIMIT, limit, iface_limit_policy);
3436
3437 if (!tb_limit[NL80211_IFACE_LIMIT_TYPES] ||
3438 !tb_limit[NL80211_IFACE_LIMIT_MAX])
3439 continue;
3440
3441 if (nla_get_u32(tb_limit[NL80211_IFACE_LIMIT_MAX]) < 2)
3442 continue;
3443
3444 nla_for_each_nested(mode, tb_limit[NL80211_IFACE_LIMIT_TYPES], mode_rem) {
3445 if (nla_type(mode) == NL80211_IFTYPE_AP)
3446 *ret = 1;
3447 }
3448 }
3449 }
3450
3451 return NL_SKIP;
3452 }
3453
3454 static int nl80211_get_mbssid_support(const char *ifname, int *buf)
3455 {
3456 if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
3457 nl80211_get_ifcomb_cb, buf))
3458 return -1;
3459
3460 return 0;
3461 }
3462
3463 static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id *id, const char *ifname)
3464 {
3465 char *phy, path[PATH_MAX];
3466
3467 /* Try to determine the phy name from the given interface */
3468 phy = nl80211_ifname2phy(ifname);
3469
3470 snprintf(path, sizeof(path), "/sys/class/%s/%s/device/of_node/compatible",
3471 phy ? "ieee80211" : "net", phy ? phy : ifname);
3472
3473 if (nl80211_readstr(path, id->compatible, sizeof(id->compatible)) <= 0)
3474 return -1;
3475
3476 return 0;
3477 }
3478
3479
3480 static int nl80211_get_hardware_id(const char *ifname, char *buf)
3481 {
3482 struct iwinfo_hardware_id *id = (struct iwinfo_hardware_id *)buf;
3483 char *phy, num[8], path[PATH_MAX];
3484 int i;
3485
3486 struct { const char *path; uint16_t *dest; } lookup[] = {
3487 { "vendor", &id->vendor_id },
3488 { "device", &id->device_id },
3489 { "subsystem_vendor", &id->subsystem_vendor_id },
3490 { "subsystem_device", &id->subsystem_device_id },
3491 { "../idVendor", &id->subsystem_vendor_id },
3492 { "../idProduct", &id->subsystem_device_id }
3493 };
3494
3495 memset(id, 0, sizeof(*id));
3496
3497 /* Try to determine the phy name from the given interface */
3498 phy = nl80211_ifname2phy(ifname);
3499
3500 for (i = 0; i < ARRAY_SIZE(lookup); i++)
3501 {
3502 snprintf(path, sizeof(path), "/sys/class/%s/%s/device/%s",
3503 phy ? "ieee80211" : "net",
3504 phy ? phy : ifname, lookup[i].path);
3505
3506 if (nl80211_readstr(path, num, sizeof(num)) > 0)
3507 *lookup[i].dest = strtoul(num, NULL, 16);
3508 }
3509
3510 /* Failed to obtain hardware PCI/USB IDs... */
3511 if (id->vendor_id == 0 && id->device_id == 0 &&
3512 id->subsystem_vendor_id == 0 && id->subsystem_device_id == 0)
3513 /* ... first fallback to FDT ... */
3514 if (nl80211_hardware_id_from_fdt(id, ifname) == -1)
3515 /* ... then board config */
3516 return iwinfo_hardware_id_from_mtd(id);
3517
3518 return 0;
3519 }
3520
3521 static const struct iwinfo_hardware_entry *
3522 nl80211_get_hardware_entry(const char *ifname)
3523 {
3524 struct iwinfo_hardware_id id;
3525
3526 if (nl80211_get_hardware_id(ifname, (char *)&id))
3527 return NULL;
3528
3529 return iwinfo_hardware(&id);
3530 }
3531
3532 static int nl80211_get_hardware_name(const char *ifname, char *buf)
3533 {
3534 const struct iwinfo_hardware_entry *hw;
3535
3536 if (!(hw = nl80211_get_hardware_entry(ifname)))
3537 sprintf(buf, "Generic MAC80211");
3538 else
3539 sprintf(buf, "%s %s", hw->vendor_name, hw->device_name);
3540
3541 return 0;
3542 }
3543
3544 static int nl80211_get_txpower_offset(const char *ifname, int *buf)
3545 {
3546 const struct iwinfo_hardware_entry *hw;
3547
3548 if (!(hw = nl80211_get_hardware_entry(ifname)))
3549 return -1;
3550
3551 *buf = hw->txpower_offset;
3552 return 0;
3553 }
3554
3555 static int nl80211_get_frequency_offset(const char *ifname, int *buf)
3556 {
3557 const struct iwinfo_hardware_entry *hw;
3558
3559 if (!(hw = nl80211_get_hardware_entry(ifname)))
3560 return -1;
3561
3562 *buf = hw->frequency_offset;
3563 return 0;
3564 }
3565
3566 static int nl80211_lookup_phyname(const char *section, char *buf)
3567 {
3568 const char *name;
3569 int idx;
3570
3571 if (!strncmp(section, "path=", 5))
3572 idx = nl80211_phy_idx_from_path(section + 5);
3573 else if (!strncmp(section, "macaddr=", 8))
3574 idx = nl80211_phy_idx_from_macaddr(section + 8);
3575 else
3576 idx = nl80211_phy_idx_from_uci(section);
3577
3578 if (idx < 0)
3579 return -1;
3580
3581 name = nl80211_phyidx2name(idx);
3582 if (!name)
3583 return -1;
3584
3585 strcpy(buf, name);
3586 return 0;
3587 }
3588
3589 static int nl80211_phy_path(const char *phyname, const char **path)
3590 {
3591 if (strchr(phyname, '/'))
3592 return -1;
3593
3594 *path = nl80211_phy_path_str(phyname);
3595 if (!*path)
3596 return -1;
3597
3598 return 0;
3599 }
3600
3601 const struct iwinfo_ops nl80211_ops = {
3602 .name = "nl80211",
3603 .probe = nl80211_probe,
3604 .channel = nl80211_get_channel,
3605 .center_chan1 = nl80211_get_center_chan1,
3606 .center_chan2 = nl80211_get_center_chan2,
3607 .frequency = nl80211_get_frequency,
3608 .frequency_offset = nl80211_get_frequency_offset,
3609 .txpower = nl80211_get_txpower,
3610 .txpower_offset = nl80211_get_txpower_offset,
3611 .bitrate = nl80211_get_bitrate,
3612 .signal = nl80211_get_signal,
3613 .noise = nl80211_get_noise,
3614 .quality = nl80211_get_quality,
3615 .quality_max = nl80211_get_quality_max,
3616 .mbssid_support = nl80211_get_mbssid_support,
3617 .hwmodelist = nl80211_get_hwmodelist,
3618 .htmodelist = nl80211_get_htmodelist,
3619 .htmode = nl80211_get_htmode,
3620 .mode = nl80211_get_mode,
3621 .ssid = nl80211_get_ssid,
3622 .bssid = nl80211_get_bssid,
3623 .country = nl80211_get_country,
3624 .hardware_id = nl80211_get_hardware_id,
3625 .hardware_name = nl80211_get_hardware_name,
3626 .encryption = nl80211_get_encryption,
3627 .phyname = nl80211_get_phyname,
3628 .assoclist = nl80211_get_assoclist,
3629 .txpwrlist = nl80211_get_txpwrlist,
3630 .scanlist = nl80211_get_scanlist,
3631 .freqlist = nl80211_get_freqlist,
3632 .countrylist = nl80211_get_countrylist,
3633 .survey = nl80211_get_survey,
3634 .lookup_phy = nl80211_lookup_phyname,
3635 .phy_path = nl80211_phy_path,
3636 .close = nl80211_close
3637 };