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