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