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