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