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