Correctly identify key management algorithms starting with "FT-"
[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-SHA384", 4, IWINFO_KMGMT_8021x },
1687 { "EAP-SHA256", 0, IWINFO_KMGMT_8021x },
1688 { "PSK-SHA256", 0, IWINFO_KMGMT_PSK },
1689 { "NONE", 0, IWINFO_KMGMT_NONE },
1690 { "None", 0, IWINFO_KMGMT_NONE },
1691 { "PSK", 0, IWINFO_KMGMT_PSK },
1692 { "EAP", 0, IWINFO_KMGMT_8021x },
1693 { "SAE", 4, IWINFO_KMGMT_SAE },
1694 { "OWE", 4, IWINFO_KMGMT_OWE }
1695 };
1696
1697 static void parse_wpa_suites(const char *str, int defversion,
1698 uint8_t *versions, uint8_t *suites)
1699 {
1700 size_t l;
1701 int i, version;
1702 const char *p, *q, *m, *sep = " \t\n,-+/";
1703
1704 for (p = str; *p; )
1705 {
1706 q = p;
1707
1708 for (i = 0; i < ARRAY_SIZE(wpa_key_mgmt_strings); i++)
1709 {
1710 m = wpa_key_mgmt_strings[i].match;
1711 l = strlen(m);
1712
1713 if (!strncmp(q, m, l) && (!q[l] || strchr(sep, q[l])))
1714 {
1715 if (wpa_key_mgmt_strings[i].version != 0)
1716 version = wpa_key_mgmt_strings[i].version;
1717 else
1718 version = defversion;
1719
1720 *versions |= version;
1721 *suites |= wpa_key_mgmt_strings[i].suite;
1722
1723 q += l;
1724 break;
1725 }
1726 }
1727
1728 if (q == p)
1729 q += strcspn(q, sep);
1730
1731 p = q + strspn(q, sep);
1732 }
1733 }
1734
1735 static struct {
1736 const char *match;
1737 int cipher;
1738 } wpa_cipher_strings[] = {
1739 { "WEP-104", IWINFO_CIPHER_WEP104 },
1740 { "WEP-40", IWINFO_CIPHER_WEP40 },
1741 { "NONE", IWINFO_CIPHER_NONE },
1742 { "TKIP", IWINFO_CIPHER_TKIP },
1743 { "CCMP-256",IWINFO_CIPHER_CCMP256 },
1744 { "CCMP", IWINFO_CIPHER_CCMP },
1745 { "GCMP-256",IWINFO_CIPHER_GCMP256 },
1746 { "GCMP", IWINFO_CIPHER_GCMP }
1747 };
1748
1749 static void parse_wpa_ciphers(const char *str, uint16_t *ciphers)
1750 {
1751 int i;
1752 size_t l;
1753 const char *m, *p, *q, *sep = " \t\n,-+/";
1754
1755 for (p = str; *p; )
1756 {
1757 q = p;
1758
1759 for (i = 0; i < ARRAY_SIZE(wpa_cipher_strings); i++)
1760 {
1761 m = wpa_cipher_strings[i].match;
1762 l = strlen(m);
1763
1764 if (!strncmp(q, m, l) && (!q[l] || strchr(sep, q[l])))
1765 {
1766 *ciphers |= wpa_cipher_strings[i].cipher;
1767
1768 q += l;
1769 break;
1770 }
1771 }
1772
1773 if (q == p)
1774 q += strcspn(q, sep);
1775
1776 p = q + strspn(q, sep);
1777 }
1778 }
1779
1780 static int nl80211_get_encryption(const char *ifname, char *buf)
1781 {
1782 char *p;
1783 int opmode;
1784 uint8_t wpa_version = 0;
1785 char wpa[2], wpa_key_mgmt[64], wpa_pairwise[16], wpa_groupwise[16];
1786 char auth_algs[2], wep_key0[27], wep_key1[27], wep_key2[27], wep_key3[27];
1787 char mode[16];
1788
1789 struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
1790
1791 /* WPA supplicant */
1792 if (nl80211_wpactl_query(ifname,
1793 "pairwise_cipher", wpa_pairwise, sizeof(wpa_pairwise),
1794 "group_cipher", wpa_groupwise, sizeof(wpa_groupwise),
1795 "key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt),
1796 "mode", mode, sizeof(mode)))
1797 {
1798 /* WEP or Open */
1799 if (!strcmp(wpa_key_mgmt, "NONE"))
1800 {
1801 parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers);
1802 parse_wpa_ciphers(wpa_groupwise, &c->group_ciphers);
1803
1804 if (c->pair_ciphers != 0 && c->pair_ciphers != IWINFO_CIPHER_NONE) {
1805 c->enabled = 1;
1806 c->auth_suites = IWINFO_KMGMT_NONE;
1807 c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
1808 }
1809 else {
1810 c->pair_ciphers = 0;
1811 c->group_ciphers = 0;
1812 }
1813 }
1814
1815 /* MESH with SAE */
1816 else if (!strcmp(mode, "mesh") && !strcmp(wpa_key_mgmt, "UNKNOWN"))
1817 {
1818 c->enabled = 1;
1819 c->wpa_version = 4;
1820 c->auth_suites = IWINFO_KMGMT_SAE;
1821 c->pair_ciphers = IWINFO_CIPHER_CCMP;
1822 c->group_ciphers = IWINFO_CIPHER_CCMP;
1823 }
1824
1825 /* WPA */
1826 else
1827 {
1828 parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers);
1829 parse_wpa_ciphers(wpa_groupwise, &c->group_ciphers);
1830
1831 p = wpa_key_mgmt;
1832
1833 if (!strncmp(p, "WPA2-", 5) || !strncmp(p, "WPA2/", 5))
1834 {
1835 p += 5;
1836 wpa_version = 2;
1837 }
1838 else if (!strncmp(p, "WPA-", 4))
1839 {
1840 p += 4;
1841 wpa_version = 1;
1842 }
1843
1844 parse_wpa_suites(p, wpa_version, &c->wpa_version, &c->auth_suites);
1845
1846 c->enabled = !!(c->wpa_version && c->auth_suites);
1847 }
1848
1849 return 0;
1850 }
1851
1852 /* Hostapd */
1853 else if (nl80211_hostapd_query(ifname,
1854 "wpa", wpa, sizeof(wpa),
1855 "wpa_key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt),
1856 "wpa_pairwise", wpa_pairwise, sizeof(wpa_pairwise),
1857 "auth_algs", auth_algs, sizeof(auth_algs),
1858 "wep_key0", wep_key0, sizeof(wep_key0),
1859 "wep_key1", wep_key1, sizeof(wep_key1),
1860 "wep_key2", wep_key2, sizeof(wep_key2),
1861 "wep_key3", wep_key3, sizeof(wep_key3)))
1862 {
1863 c->wpa_version = 0;
1864
1865 if (wpa_key_mgmt[0])
1866 {
1867 for (p = strtok(wpa_key_mgmt, " \t"); p != NULL; p = strtok(NULL, " \t"))
1868 {
1869 if (!strncmp(p, "WPA-", 4))
1870 p += 4;
1871
1872 if (!strncmp(p, "FT-", 3))
1873 p += 3;
1874
1875 parse_wpa_suites(p, atoi(wpa), &c->wpa_version, &c->auth_suites);
1876 }
1877
1878 c->enabled = c->wpa_version ? 1 : 0;
1879 }
1880
1881 if (wpa_pairwise[0])
1882 parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers);
1883
1884 if (auth_algs[0])
1885 {
1886 switch (atoi(auth_algs))
1887 {
1888 case 1:
1889 c->auth_algs |= IWINFO_AUTH_OPEN;
1890 break;
1891
1892 case 2:
1893 c->auth_algs |= IWINFO_AUTH_SHARED;
1894 break;
1895
1896 case 3:
1897 c->auth_algs |= IWINFO_AUTH_OPEN;
1898 c->auth_algs |= IWINFO_AUTH_SHARED;
1899 break;
1900 }
1901
1902 c->pair_ciphers |= nl80211_check_wepkey(wep_key0);
1903 c->pair_ciphers |= nl80211_check_wepkey(wep_key1);
1904 c->pair_ciphers |= nl80211_check_wepkey(wep_key2);
1905 c->pair_ciphers |= nl80211_check_wepkey(wep_key3);
1906
1907 c->enabled = (c->auth_algs && c->pair_ciphers) ? 1 : 0;
1908 }
1909
1910 c->group_ciphers = c->pair_ciphers;
1911
1912 return 0;
1913 }
1914
1915 /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */
1916 else if (!nl80211_get_mode(ifname, &opmode) &&
1917 (opmode == IWINFO_OPMODE_ADHOC ||
1918 opmode == IWINFO_OPMODE_MESHPOINT))
1919 {
1920 c->enabled = 0;
1921
1922 return 0;
1923 }
1924
1925
1926 return -1;
1927 }
1928
1929 static int nl80211_get_phyname(const char *ifname, char *buf)
1930 {
1931 const char *name;
1932
1933 name = nl80211_ifname2phy(ifname);
1934
1935 if (name)
1936 {
1937 strcpy(buf, name);
1938 return 0;
1939 }
1940 else if ((name = nl80211_phy2ifname(ifname)) != NULL)
1941 {
1942 name = nl80211_ifname2phy(name);
1943
1944 if (name)
1945 {
1946 strcpy(buf, ifname);
1947 return 0;
1948 }
1949 }
1950
1951 return -1;
1952 }
1953
1954
1955 static void nl80211_parse_rateinfo(struct nlattr **ri,
1956 struct iwinfo_rate_entry *re)
1957 {
1958 if (ri[NL80211_RATE_INFO_BITRATE32])
1959 re->rate = nla_get_u32(ri[NL80211_RATE_INFO_BITRATE32]) * 100;
1960 else if (ri[NL80211_RATE_INFO_BITRATE])
1961 re->rate = nla_get_u16(ri[NL80211_RATE_INFO_BITRATE]) * 100;
1962
1963 if (ri[NL80211_RATE_INFO_HE_MCS])
1964 {
1965 re->is_he = 1;
1966 re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_HE_MCS]);
1967
1968 if (ri[NL80211_RATE_INFO_HE_NSS])
1969 re->nss = nla_get_u8(ri[NL80211_RATE_INFO_HE_NSS]);
1970 if (ri[NL80211_RATE_INFO_HE_GI])
1971 re->he_gi = nla_get_u8(ri[NL80211_RATE_INFO_HE_GI]);
1972 if (ri[NL80211_RATE_INFO_HE_DCM])
1973 re->he_dcm = nla_get_u8(ri[NL80211_RATE_INFO_HE_DCM]);
1974 }
1975 else if (ri[NL80211_RATE_INFO_VHT_MCS])
1976 {
1977 re->is_vht = 1;
1978 re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_VHT_MCS]);
1979
1980 if (ri[NL80211_RATE_INFO_VHT_NSS])
1981 re->nss = nla_get_u8(ri[NL80211_RATE_INFO_VHT_NSS]);
1982 }
1983 else if (ri[NL80211_RATE_INFO_MCS])
1984 {
1985 re->is_ht = 1;
1986 re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_MCS]);
1987 }
1988
1989 if (ri[NL80211_RATE_INFO_5_MHZ_WIDTH])
1990 re->mhz = 5;
1991 else if (ri[NL80211_RATE_INFO_10_MHZ_WIDTH])
1992 re->mhz = 10;
1993 else if (ri[NL80211_RATE_INFO_40_MHZ_WIDTH])
1994 re->mhz = 40;
1995 else if (ri[NL80211_RATE_INFO_80_MHZ_WIDTH])
1996 re->mhz = 80;
1997 else if (ri[NL80211_RATE_INFO_80P80_MHZ_WIDTH] ||
1998 ri[NL80211_RATE_INFO_160_MHZ_WIDTH])
1999 re->mhz = 160;
2000 else
2001 re->mhz = 20;
2002
2003 if (ri[NL80211_RATE_INFO_SHORT_GI])
2004 re->is_short_gi = 1;
2005
2006 re->is_40mhz = (re->mhz == 40);
2007 }
2008
2009 static int nl80211_get_survey_cb(struct nl_msg *msg, void *arg)
2010 {
2011 struct nl80211_array_buf *arr = arg;
2012 struct iwinfo_survey_entry *e = arr->buf;
2013 struct nlattr **attr = nl80211_parse(msg);
2014 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2015 int rc;
2016
2017 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2018 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2019 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2020 [NL80211_SURVEY_INFO_TIME] = { .type = NLA_U64 },
2021 [NL80211_SURVEY_INFO_TIME_BUSY] = { .type = NLA_U64 },
2022 [NL80211_SURVEY_INFO_TIME_EXT_BUSY] = { .type = NLA_U64 },
2023 [NL80211_SURVEY_INFO_TIME_RX] = { .type = NLA_U64 },
2024 [NL80211_SURVEY_INFO_TIME_TX] = { .type = NLA_U64 },
2025 };
2026
2027 rc = nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2028 attr[NL80211_ATTR_SURVEY_INFO],
2029 survey_policy);
2030 if (rc)
2031 return NL_SKIP;
2032
2033 /* advance to end of array */
2034 e += arr->count;
2035 memset(e, 0, sizeof(*e));
2036
2037 if (sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2038 e->mhz = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
2039
2040 if (sinfo[NL80211_SURVEY_INFO_NOISE])
2041 e->noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2042
2043 if (sinfo[NL80211_SURVEY_INFO_TIME])
2044 e->active_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME]);
2045
2046 if (sinfo[NL80211_SURVEY_INFO_TIME_BUSY])
2047 e->busy_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_BUSY]);
2048
2049 if (sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY])
2050 e->busy_time_ext = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]);
2051
2052 if (sinfo[NL80211_SURVEY_INFO_TIME_RX])
2053 e->rxtime = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_RX]);
2054
2055 if (sinfo[NL80211_SURVEY_INFO_TIME_TX])
2056 e->txtime = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_TX]);
2057
2058 arr->count++;
2059 return NL_SKIP;
2060 }
2061
2062
2063 static void plink_state_to_str(char *dst, unsigned state)
2064 {
2065 switch (state) {
2066 case NL80211_PLINK_LISTEN:
2067 strcpy(dst, "LISTEN");
2068 break;
2069 case NL80211_PLINK_OPN_SNT:
2070 strcpy(dst, "OPN_SNT");
2071 break;
2072 case NL80211_PLINK_OPN_RCVD:
2073 strcpy(dst, "OPN_RCVD");
2074 break;
2075 case NL80211_PLINK_CNF_RCVD:
2076 strcpy(dst, "CNF_RCVD");
2077 break;
2078 case NL80211_PLINK_ESTAB:
2079 strcpy(dst, "ESTAB");
2080 break;
2081 case NL80211_PLINK_HOLDING:
2082 strcpy(dst, "HOLDING");
2083 break;
2084 case NL80211_PLINK_BLOCKED:
2085 strcpy(dst, "BLOCKED");
2086 break;
2087 default:
2088 strcpy(dst, "UNKNOWN");
2089 break;
2090 }
2091 }
2092
2093 static void power_mode_to_str(char *dst, struct nlattr *a)
2094 {
2095 enum nl80211_mesh_power_mode pm = nla_get_u32(a);
2096
2097 switch (pm) {
2098 case NL80211_MESH_POWER_ACTIVE:
2099 strcpy(dst, "ACTIVE");
2100 break;
2101 case NL80211_MESH_POWER_LIGHT_SLEEP:
2102 strcpy(dst, "LIGHT SLEEP");
2103 break;
2104 case NL80211_MESH_POWER_DEEP_SLEEP:
2105 strcpy(dst, "DEEP SLEEP");
2106 break;
2107 default:
2108 strcpy(dst, "UNKNOWN");
2109 break;
2110 }
2111 }
2112
2113 static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
2114 {
2115 struct nl80211_array_buf *arr = arg;
2116 struct iwinfo_assoclist_entry *e = arr->buf;
2117 struct nlattr **attr = nl80211_parse(msg);
2118 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
2119 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
2120 struct nl80211_sta_flag_update *sta_flags;
2121
2122 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
2123 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
2124 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
2125 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
2126 [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED },
2127 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
2128 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
2129 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
2130 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
2131 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
2132 [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
2133 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
2134 [NL80211_STA_INFO_CONNECTED_TIME]= { .type = NLA_U32 },
2135 [NL80211_STA_INFO_RX_DROP_MISC] = { .type = NLA_U64 },
2136 [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 },
2137 [NL80211_STA_INFO_STA_FLAGS] =
2138 { .minlen = sizeof(struct nl80211_sta_flag_update) },
2139 [NL80211_STA_INFO_EXPECTED_THROUGHPUT] = { .type = NLA_U32 },
2140 /* mesh */
2141 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
2142 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
2143 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
2144 [NL80211_STA_INFO_LOCAL_PM] = { .type = NLA_U32 },
2145 [NL80211_STA_INFO_PEER_PM] = { .type = NLA_U32 },
2146 [NL80211_STA_INFO_NONPEER_PM] = { .type = NLA_U32 },
2147 };
2148
2149 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
2150 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
2151 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
2152 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
2153 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
2154 };
2155
2156 /* advance to end of array */
2157 e += arr->count;
2158 memset(e, 0, sizeof(*e));
2159
2160 if (attr[NL80211_ATTR_MAC])
2161 memcpy(e->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
2162
2163 if (attr[NL80211_ATTR_STA_INFO] &&
2164 !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
2165 attr[NL80211_ATTR_STA_INFO], stats_policy))
2166 {
2167 if (sinfo[NL80211_STA_INFO_SIGNAL])
2168 e->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
2169
2170 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
2171 e->signal_avg = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
2172
2173 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
2174 e->inactive = nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]);
2175
2176 if (sinfo[NL80211_STA_INFO_CONNECTED_TIME])
2177 e->connected_time = nla_get_u32(sinfo[NL80211_STA_INFO_CONNECTED_TIME]);
2178
2179 if (sinfo[NL80211_STA_INFO_RX_PACKETS])
2180 e->rx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]);
2181
2182 if (sinfo[NL80211_STA_INFO_TX_PACKETS])
2183 e->tx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]);
2184
2185 if (sinfo[NL80211_STA_INFO_RX_BITRATE] &&
2186 !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2187 sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy))
2188 nl80211_parse_rateinfo(rinfo, &e->rx_rate);
2189
2190 if (sinfo[NL80211_STA_INFO_TX_BITRATE] &&
2191 !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2192 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy))
2193 nl80211_parse_rateinfo(rinfo, &e->tx_rate);
2194
2195 if (sinfo[NL80211_STA_INFO_RX_BYTES])
2196 e->rx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]);
2197
2198 if (sinfo[NL80211_STA_INFO_TX_BYTES])
2199 e->tx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]);
2200
2201 if (sinfo[NL80211_STA_INFO_TX_RETRIES])
2202 e->tx_retries = nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]);
2203
2204 if (sinfo[NL80211_STA_INFO_TX_FAILED])
2205 e->tx_failed = nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]);
2206
2207 if (sinfo[NL80211_STA_INFO_T_OFFSET])
2208 e->t_offset = nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]);
2209
2210 if (sinfo[NL80211_STA_INFO_RX_DROP_MISC])
2211 e->rx_drop_misc = nla_get_u64(sinfo[NL80211_STA_INFO_RX_DROP_MISC]);
2212
2213 if (sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT])
2214 e->thr = nla_get_u32(sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]);
2215
2216 /* mesh */
2217 if (sinfo[NL80211_STA_INFO_LLID])
2218 e->llid = nla_get_u16(sinfo[NL80211_STA_INFO_LLID]);
2219
2220 if (sinfo[NL80211_STA_INFO_PLID])
2221 e->plid = nla_get_u16(sinfo[NL80211_STA_INFO_PLID]);
2222
2223 if (sinfo[NL80211_STA_INFO_PLINK_STATE])
2224 plink_state_to_str(e->plink_state,
2225 nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE]));
2226
2227 if (sinfo[NL80211_STA_INFO_LOCAL_PM])
2228 power_mode_to_str(e->local_ps, sinfo[NL80211_STA_INFO_LOCAL_PM]);
2229 if (sinfo[NL80211_STA_INFO_PEER_PM])
2230 power_mode_to_str(e->peer_ps, sinfo[NL80211_STA_INFO_PEER_PM]);
2231 if (sinfo[NL80211_STA_INFO_NONPEER_PM])
2232 power_mode_to_str(e->nonpeer_ps, sinfo[NL80211_STA_INFO_NONPEER_PM]);
2233
2234 /* Station flags */
2235 if (sinfo[NL80211_STA_INFO_STA_FLAGS])
2236 {
2237 sta_flags = (struct nl80211_sta_flag_update *)
2238 nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]);
2239
2240 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
2241 sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED))
2242 e->is_authorized = 1;
2243
2244 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
2245 sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
2246 e->is_authenticated = 1;
2247
2248 if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) &&
2249 sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
2250 e->is_preamble_short = 1;
2251
2252 if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME) &&
2253 sta_flags->set & BIT(NL80211_STA_FLAG_WME))
2254 e->is_wme = 1;
2255
2256 if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP) &&
2257 sta_flags->set & BIT(NL80211_STA_FLAG_MFP))
2258 e->is_mfp = 1;
2259
2260 if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER) &&
2261 sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2262 e->is_tdls = 1;
2263 }
2264 }
2265
2266 e->noise = 0; /* filled in by caller */
2267 arr->count++;
2268
2269 return NL_SKIP;
2270 }
2271
2272 static int nl80211_get_survey(const char *ifname, char *buf, int *len)
2273 {
2274 struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
2275 int rc;
2276
2277 rc = nl80211_request(ifname, NL80211_CMD_GET_SURVEY,
2278 NLM_F_DUMP, nl80211_get_survey_cb, &arr);
2279 if (!rc)
2280 *len = (arr.count * sizeof(struct iwinfo_survey_entry));
2281 else
2282 *len = 0;
2283
2284 return 0;
2285 }
2286
2287 static int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
2288 {
2289 DIR *d;
2290 int i, noise = 0;
2291 struct dirent *de;
2292 struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
2293 struct iwinfo_assoclist_entry *e;
2294
2295 if ((d = opendir("/sys/class/net")) != NULL)
2296 {
2297 while ((de = readdir(d)) != NULL)
2298 {
2299 if (!strncmp(de->d_name, ifname, strlen(ifname)) &&
2300 (!de->d_name[strlen(ifname)] ||
2301 !strncmp(&de->d_name[strlen(ifname)], ".sta", 4)))
2302 {
2303 nl80211_request(de->d_name, NL80211_CMD_GET_STATION,
2304 NLM_F_DUMP, nl80211_get_assoclist_cb, &arr);
2305 }
2306 }
2307
2308 closedir(d);
2309
2310 if (!nl80211_get_noise(ifname, &noise))
2311 for (i = 0, e = arr.buf; i < arr.count; i++, e++)
2312 e->noise = noise;
2313
2314 *len = (arr.count * sizeof(struct iwinfo_assoclist_entry));
2315 return 0;
2316 }
2317
2318 return -1;
2319 }
2320
2321 static int nl80211_get_txpwrlist_cb(struct nl_msg *msg, void *arg)
2322 {
2323 int *dbm_max = arg;
2324 int ch_cur, ch_cmp, bands_remain, freqs_remain;
2325
2326 struct nlattr **attr = nl80211_parse(msg);
2327 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
2328 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
2329 struct nlattr *band, *freq;
2330
2331 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
2332 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
2333 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
2334 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
2335 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
2336 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
2337 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
2338 };
2339
2340 ch_cur = *dbm_max; /* value int* is initialized with channel by caller */
2341 *dbm_max = -1;
2342
2343 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
2344 {
2345 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
2346 nla_len(band), NULL);
2347
2348 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
2349 {
2350 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
2351 nla_data(freq), nla_len(freq), freq_policy);
2352
2353 ch_cmp = nl80211_freq2channel(nla_get_u32(
2354 freqs[NL80211_FREQUENCY_ATTR_FREQ]));
2355
2356 if ((!ch_cur || (ch_cmp == ch_cur)) &&
2357 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER])
2358 {
2359 *dbm_max = (int)(0.01 * nla_get_u32(
2360 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
2361
2362 break;
2363 }
2364 }
2365 }
2366
2367 return NL_SKIP;
2368 }
2369
2370 static int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
2371 {
2372 int err, ch_cur;
2373 int dbm_max = -1, dbm_cur, dbm_cnt;
2374 struct nl80211_msg_conveyor *req;
2375 struct iwinfo_txpwrlist_entry entry;
2376
2377 if (nl80211_get_channel(ifname, &ch_cur))
2378 ch_cur = 0;
2379
2380 /* initialize the value pointer with channel for callback */
2381 dbm_max = ch_cur;
2382
2383 err = nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
2384 nl80211_get_txpwrlist_cb, &dbm_max);
2385
2386 if (!err)
2387 {
2388 for (dbm_cur = 0, dbm_cnt = 0;
2389 dbm_cur < dbm_max;
2390 dbm_cur++, dbm_cnt++)
2391 {
2392 entry.dbm = dbm_cur;
2393 entry.mw = iwinfo_dbm2mw(dbm_cur);
2394
2395 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
2396 }
2397
2398 entry.dbm = dbm_max;
2399 entry.mw = iwinfo_dbm2mw(dbm_max);
2400
2401 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
2402 dbm_cnt++;
2403
2404 *len = dbm_cnt * sizeof(entry);
2405 return 0;
2406 }
2407
2408 return -1;
2409 }
2410
2411 static void nl80211_get_scancrypto(char *spec, struct iwinfo_crypto_entry *c)
2412 {
2413 int wpa_version = 0;
2414 char *p, *q, *proto, *suites;
2415
2416 c->enabled = 0;
2417
2418 for (p = strtok_r(spec, "[]", &q); p; p = strtok_r(NULL, "[]", &q)) {
2419 if (!strcmp(p, "WEP")) {
2420 c->enabled = 1;
2421 c->auth_suites = IWINFO_KMGMT_NONE;
2422 c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
2423 c->pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
2424 break;
2425 }
2426
2427 proto = strtok(p, "-");
2428 suites = strtok(NULL, "]");
2429
2430 if (!proto || !suites)
2431 continue;
2432
2433 if (!strcmp(proto, "WPA2") || !strcmp(proto, "RSN"))
2434 wpa_version = 2;
2435 else if (!strcmp(proto, "WPA"))
2436 wpa_version = 1;
2437 else
2438 continue;
2439
2440 c->enabled = 1;
2441
2442 parse_wpa_suites(suites, wpa_version, &c->wpa_version, &c->auth_suites);
2443 parse_wpa_ciphers(suites, &c->pair_ciphers);
2444 }
2445 }
2446
2447
2448 struct nl80211_scanlist {
2449 struct iwinfo_scanlist_entry *e;
2450 int len;
2451 };
2452
2453
2454 static void nl80211_get_scanlist_ie(struct nlattr **bss,
2455 struct iwinfo_scanlist_entry *e)
2456 {
2457 int ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
2458 unsigned char *ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
2459 static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 };
2460 int len;
2461
2462 while (ielen >= 2 && ielen >= ie[1])
2463 {
2464 switch (ie[0])
2465 {
2466 case 0: /* SSID */
2467 case 114: /* Mesh ID */
2468 if (e->ssid[0] == 0) {
2469 len = min(ie[1], IWINFO_ESSID_MAX_SIZE);
2470 memcpy(e->ssid, ie + 2, len);
2471 e->ssid[len] = 0;
2472 }
2473 break;
2474
2475 case 48: /* RSN */
2476 iwinfo_parse_rsn(&e->crypto, ie + 2, ie[1],
2477 IWINFO_CIPHER_CCMP, IWINFO_KMGMT_8021x);
2478 break;
2479
2480 case 221: /* Vendor */
2481 if (ie[1] >= 4 && !memcmp(ie + 2, ms_oui, 3) && ie[5] == 1)
2482 iwinfo_parse_rsn(&e->crypto, ie + 6, ie[1] - 4,
2483 IWINFO_CIPHER_TKIP, IWINFO_KMGMT_PSK);
2484 break;
2485 case 61: /* HT oeration */
2486 if (ie[1] >= 3) {
2487 e->ht_chan_info.primary_chan = ie[2];
2488 e->ht_chan_info.secondary_chan_off = ie[3] & 0x3;
2489 e->ht_chan_info.chan_width = (ie[4] & 0x4)>>2;
2490 }
2491 break;
2492 case 192: /* VHT operation */
2493 if (ie[1] >= 3) {
2494 e->vht_chan_info.chan_width = ie[2];
2495 e->vht_chan_info.center_chan_1 = ie[3];
2496 e->vht_chan_info.center_chan_2 = ie[4];
2497 }
2498 break;
2499 }
2500
2501 ielen -= ie[1] + 2;
2502 ie += ie[1] + 2;
2503 }
2504 }
2505
2506 static int nl80211_get_scanlist_cb(struct nl_msg *msg, void *arg)
2507 {
2508 int8_t rssi;
2509 uint16_t caps;
2510
2511 struct nl80211_scanlist *sl = arg;
2512 struct nlattr **tb = nl80211_parse(msg);
2513 struct nlattr *bss[NL80211_BSS_MAX + 1];
2514
2515 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
2516 [NL80211_BSS_TSF] = { .type = NLA_U64 },
2517 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
2518 [NL80211_BSS_BSSID] = { 0 },
2519 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
2520 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
2521 [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 },
2522 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
2523 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
2524 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
2525 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
2526 [NL80211_BSS_BEACON_IES] = { 0 },
2527 };
2528
2529 if (!tb[NL80211_ATTR_BSS] ||
2530 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
2531 bss_policy) ||
2532 !bss[NL80211_BSS_BSSID])
2533 {
2534 return NL_SKIP;
2535 }
2536
2537 if (bss[NL80211_BSS_CAPABILITY])
2538 caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
2539 else
2540 caps = 0;
2541
2542 memset(sl->e, 0, sizeof(*sl->e));
2543 memcpy(sl->e->mac, nla_data(bss[NL80211_BSS_BSSID]), 6);
2544
2545 if (caps & (1<<1))
2546 sl->e->mode = IWINFO_OPMODE_ADHOC;
2547 else if (caps & (1<<0))
2548 sl->e->mode = IWINFO_OPMODE_MASTER;
2549 else
2550 sl->e->mode = IWINFO_OPMODE_MESHPOINT;
2551
2552 if (caps & (1<<4))
2553 sl->e->crypto.enabled = 1;
2554
2555 if (bss[NL80211_BSS_FREQUENCY])
2556 sl->e->channel = nl80211_freq2channel(nla_get_u32(
2557 bss[NL80211_BSS_FREQUENCY]));
2558
2559 if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
2560 nl80211_get_scanlist_ie(bss, sl->e);
2561
2562 if (bss[NL80211_BSS_SIGNAL_MBM])
2563 {
2564 sl->e->signal =
2565 (uint8_t)((int32_t)nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]) / 100);
2566
2567 rssi = sl->e->signal - 0x100;
2568
2569 if (rssi < -110)
2570 rssi = -110;
2571 else if (rssi > -40)
2572 rssi = -40;
2573
2574 sl->e->quality = (rssi + 110);
2575 sl->e->quality_max = 70;
2576 }
2577
2578 if (sl->e->crypto.enabled && !sl->e->crypto.wpa_version)
2579 {
2580 sl->e->crypto.auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
2581 sl->e->crypto.pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
2582 }
2583
2584 sl->e++;
2585 sl->len++;
2586
2587 return NL_SKIP;
2588 }
2589
2590 static int nl80211_get_scanlist_nl(const char *ifname, char *buf, int *len)
2591 {
2592 struct nl80211_scanlist sl = { .e = (struct iwinfo_scanlist_entry *)buf };
2593
2594 if (nl80211_request(ifname, NL80211_CMD_TRIGGER_SCAN, 0, NULL, NULL))
2595 goto out;
2596
2597 if (nl80211_wait("nl80211", "scan",
2598 NL80211_CMD_NEW_SCAN_RESULTS, NL80211_CMD_SCAN_ABORTED))
2599 goto out;
2600
2601 if (nl80211_request(ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
2602 nl80211_get_scanlist_cb, &sl))
2603 goto out;
2604
2605 *len = sl.len * sizeof(struct iwinfo_scanlist_entry);
2606 return 0;
2607
2608 out:
2609 *len = 0;
2610 return -1;
2611 }
2612
2613 static int wpasupp_ssid_decode(const char *in, char *out, int outlen)
2614 {
2615 #define hex(x) \
2616 (((x) >= 'a') ? ((x) - 'a' + 10) : \
2617 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
2618
2619 int len = 0;
2620
2621 while (*in)
2622 {
2623 if (len + 1 >= outlen)
2624 break;
2625
2626 switch (*in)
2627 {
2628 case '\\':
2629 in++;
2630 switch (*in)
2631 {
2632 case 'n':
2633 out[len++] = '\n'; in++;
2634 break;
2635
2636 case 'r':
2637 out[len++] = '\r'; in++;
2638 break;
2639
2640 case 't':
2641 out[len++] = '\t'; in++;
2642 break;
2643
2644 case 'e':
2645 out[len++] = '\033'; in++;
2646 break;
2647
2648 case 'x':
2649 if (isxdigit(*(in+1)) && isxdigit(*(in+2)))
2650 out[len++] = hex(*(in+1)) * 16 + hex(*(in+2));
2651 in += 3;
2652 break;
2653
2654 default:
2655 out[len++] = *in++;
2656 break;
2657 }
2658 break;
2659
2660 default:
2661 out[len++] = *in++;
2662 break;
2663 }
2664 }
2665
2666 if (outlen > len)
2667 out[len] = '\0';
2668
2669 return len;
2670 }
2671
2672 static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len)
2673 {
2674 int sock, qmax, rssi, tries, count = -1, ready = 0;
2675 char *pos, *line, *bssid, *freq, *signal, *flags, *ssid, reply[4096];
2676 struct sockaddr_un local = { 0 };
2677 struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
2678
2679 sock = nl80211_wpactl_connect(ifname, &local);
2680
2681 if (sock < 0)
2682 return sock;
2683
2684 send(sock, "ATTACH", 6, 0);
2685 send(sock, "SCAN", 4, 0);
2686
2687 /*
2688 * wait for scan results:
2689 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
2690 * 72 channels at most. We'll also receive two "OK" messages acknowledging
2691 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
2692 * time to process the results, so try 72 + 2 + 1 times.
2693 */
2694 for (tries = 0; tries < 75; tries++)
2695 {
2696 if (nl80211_wpactl_recv(sock, reply, sizeof(reply)) <= 0)
2697 continue;
2698
2699 /* got an event notification */
2700 if (reply[0] == '<')
2701 {
2702 /* scan results are ready */
2703 if (strstr(reply, "CTRL-EVENT-SCAN-RESULTS"))
2704 {
2705 /* send "SCAN_RESULTS" command */
2706 ready = (send(sock, "SCAN_RESULTS", 12, 0) == 12);
2707 break;
2708 }
2709
2710 /* is another unrelated event, retry */
2711 tries--;
2712 }
2713
2714 /* scanning already in progress, keep awaiting results */
2715 else if (!strcmp(reply, "FAIL-BUSY\n"))
2716 {
2717 tries--;
2718 }
2719
2720 /* another failure, abort */
2721 else if (!strncmp(reply, "FAIL-", 5))
2722 {
2723 break;
2724 }
2725 }
2726
2727 /* receive and parse scan results if the wait above didn't time out */
2728 while (ready && nl80211_wpactl_recv(sock, reply, sizeof(reply)) > 0)
2729 {
2730 /* received an event notification, receive again */
2731 if (reply[0] == '<')
2732 continue;
2733
2734 nl80211_get_quality_max(ifname, &qmax);
2735
2736 for (line = strtok_r(reply, "\n", &pos);
2737 line != NULL;
2738 line = strtok_r(NULL, "\n", &pos))
2739 {
2740 /* skip header line */
2741 if (count < 0)
2742 {
2743 count++;
2744 continue;
2745 }
2746
2747 bssid = strtok(line, "\t");
2748 freq = strtok(NULL, "\t");
2749 signal = strtok(NULL, "\t");
2750 flags = strtok(NULL, "\t");
2751 ssid = strtok(NULL, "\n");
2752
2753 if (!bssid || !freq || !signal || !flags)
2754 continue;
2755
2756 /* BSSID */
2757 e->mac[0] = strtol(&bssid[0], NULL, 16);
2758 e->mac[1] = strtol(&bssid[3], NULL, 16);
2759 e->mac[2] = strtol(&bssid[6], NULL, 16);
2760 e->mac[3] = strtol(&bssid[9], NULL, 16);
2761 e->mac[4] = strtol(&bssid[12], NULL, 16);
2762 e->mac[5] = strtol(&bssid[15], NULL, 16);
2763
2764 /* SSID */
2765 if (ssid)
2766 wpasupp_ssid_decode(ssid, e->ssid, sizeof(e->ssid));
2767 else
2768 e->ssid[0] = 0;
2769
2770 /* Mode */
2771 if (strstr(flags, "[MESH]"))
2772 e->mode = IWINFO_OPMODE_MESHPOINT;
2773 else if (strstr(flags, "[IBSS]"))
2774 e->mode = IWINFO_OPMODE_ADHOC;
2775 else
2776 e->mode = IWINFO_OPMODE_MASTER;
2777
2778 /* Channel */
2779 e->channel = nl80211_freq2channel(atoi(freq));
2780
2781 /* Signal */
2782 rssi = atoi(signal);
2783 e->signal = rssi;
2784
2785 /* Quality */
2786 if (rssi < 0)
2787 {
2788 /* The cfg80211 wext compat layer assumes a signal range
2789 * of -110 dBm to -40 dBm, the quality value is derived
2790 * by adding 110 to the signal level */
2791 if (rssi < -110)
2792 rssi = -110;
2793 else if (rssi > -40)
2794 rssi = -40;
2795
2796 e->quality = (rssi + 110);
2797 }
2798 else
2799 {
2800 e->quality = rssi;
2801 }
2802
2803 /* Max. Quality */
2804 e->quality_max = qmax;
2805
2806 /* Crypto */
2807 nl80211_get_scancrypto(flags, &e->crypto);
2808
2809 count++;
2810 e++;
2811 }
2812
2813 *len = count * sizeof(struct iwinfo_scanlist_entry);
2814 break;
2815 }
2816
2817 close(sock);
2818 unlink(local.sun_path);
2819
2820 return (count >= 0) ? 0 : -1;
2821 }
2822
2823 static int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
2824 {
2825 char *res;
2826 int rv, mode;
2827
2828 *len = 0;
2829
2830 /* Got a radioX pseudo interface, find some interface on it or create one */
2831 if (!strncmp(ifname, "radio", 5))
2832 {
2833 /* Reuse existing interface */
2834 if ((res = nl80211_phy2ifname(ifname)) != NULL)
2835 {
2836 return nl80211_get_scanlist(res, buf, len);
2837 }
2838
2839 /* Need to spawn a temporary iface for scanning */
2840 else if ((res = nl80211_ifadd(ifname)) != NULL)
2841 {
2842 rv = nl80211_get_scanlist(res, buf, len);
2843 nl80211_ifdel(res);
2844 return rv;
2845 }
2846 }
2847
2848 /* WPA supplicant */
2849 if (!nl80211_get_scanlist_wpactl(ifname, buf, len))
2850 {
2851 return 0;
2852 }
2853
2854 /* station / ad-hoc / monitor scan */
2855 else if (!nl80211_get_mode(ifname, &mode) &&
2856 (mode == IWINFO_OPMODE_ADHOC ||
2857 mode == IWINFO_OPMODE_MASTER ||
2858 mode == IWINFO_OPMODE_CLIENT ||
2859 mode == IWINFO_OPMODE_MONITOR) &&
2860 iwinfo_ifup(ifname))
2861 {
2862 return nl80211_get_scanlist_nl(ifname, buf, len);
2863 }
2864
2865 /* AP scan */
2866 else
2867 {
2868 /* Got a temp interface, don't create yet another one */
2869 if (!strncmp(ifname, "tmp.", 4))
2870 {
2871 if (!iwinfo_ifup(ifname))
2872 return -1;
2873
2874 rv = nl80211_get_scanlist_nl(ifname, buf, len);
2875 iwinfo_ifdown(ifname);
2876 return rv;
2877 }
2878
2879 /* Spawn a new scan interface */
2880 else
2881 {
2882 if (!(res = nl80211_ifadd(ifname)))
2883 return -1;
2884
2885 iwinfo_ifmac(res);
2886
2887 /* if we can take the new interface up, the driver supports an
2888 * additional interface and there's no need to tear down the ap */
2889 if (iwinfo_ifup(res))
2890 {
2891 rv = nl80211_get_scanlist_nl(res, buf, len);
2892 iwinfo_ifdown(res);
2893 }
2894
2895 /* driver cannot create secondary interface, take down ap
2896 * during scan */
2897 else if (iwinfo_ifdown(ifname) && iwinfo_ifup(res))
2898 {
2899 rv = nl80211_get_scanlist_nl(res, buf, len);
2900 iwinfo_ifdown(res);
2901 iwinfo_ifup(ifname);
2902 nl80211_hostapd_hup(ifname);
2903 }
2904
2905 nl80211_ifdel(res);
2906 return rv;
2907 }
2908 }
2909
2910 return -1;
2911 }
2912
2913 static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg)
2914 {
2915 int bands_remain, freqs_remain;
2916
2917 struct nl80211_array_buf *arr = arg;
2918 struct iwinfo_freqlist_entry *e;
2919
2920 struct nlattr **attr = nl80211_parse(msg);
2921 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
2922 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
2923 struct nlattr *band, *freq;
2924
2925 e = arr->buf;
2926 e += arr->count;
2927
2928 if (attr[NL80211_ATTR_WIPHY_BANDS]) {
2929 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
2930 {
2931 nla_parse(bands, NL80211_BAND_ATTR_MAX,
2932 nla_data(band), nla_len(band), NULL);
2933
2934 if (bands[NL80211_BAND_ATTR_FREQS]) {
2935 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
2936 {
2937 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
2938 nla_data(freq), nla_len(freq), NULL);
2939
2940 if (!freqs[NL80211_FREQUENCY_ATTR_FREQ] ||
2941 freqs[NL80211_FREQUENCY_ATTR_DISABLED])
2942 continue;
2943
2944 e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
2945 e->channel = nl80211_freq2channel(e->mhz);
2946
2947 e->restricted = (
2948 freqs[NL80211_FREQUENCY_ATTR_NO_IR] &&
2949 !freqs[NL80211_FREQUENCY_ATTR_RADAR]
2950 ) ? 1 : 0;
2951
2952 if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS])
2953 e->flags |= IWINFO_FREQ_NO_HT40MINUS;
2954 if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS])
2955 e->flags |= IWINFO_FREQ_NO_HT40PLUS;
2956 if (freqs[NL80211_FREQUENCY_ATTR_NO_80MHZ])
2957 e->flags |= IWINFO_FREQ_NO_80MHZ;
2958 if (freqs[NL80211_FREQUENCY_ATTR_NO_160MHZ])
2959 e->flags |= IWINFO_FREQ_NO_160MHZ;
2960 if (freqs[NL80211_FREQUENCY_ATTR_NO_20MHZ])
2961 e->flags |= IWINFO_FREQ_NO_20MHZ;
2962 if (freqs[NL80211_FREQUENCY_ATTR_NO_10MHZ])
2963 e->flags |= IWINFO_FREQ_NO_10MHZ;
2964
2965 e++;
2966 arr->count++;
2967 }
2968 }
2969 }
2970 }
2971
2972 return NL_SKIP;
2973 }
2974
2975 static int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
2976 {
2977 struct nl80211_msg_conveyor *cv;
2978 struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
2979 uint32_t features = nl80211_get_protocol_features(ifname);
2980 int flags;
2981
2982 flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0;
2983 cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags);
2984 if (!cv)
2985 goto out;
2986
2987 NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
2988 if (nl80211_send(cv, nl80211_get_freqlist_cb, &arr))
2989 goto out;
2990
2991 *len = arr.count * sizeof(struct iwinfo_freqlist_entry);
2992 return 0;
2993
2994 nla_put_failure:
2995 nl80211_free(cv);
2996 out:
2997 *len = 0;
2998 return -1;
2999 }
3000
3001 static int nl80211_get_country_cb(struct nl_msg *msg, void *arg)
3002 {
3003 char *buf = arg;
3004 struct nlattr **attr = nl80211_parse(msg);
3005
3006 if (attr[NL80211_ATTR_REG_ALPHA2])
3007 memcpy(buf, nla_data(attr[NL80211_ATTR_REG_ALPHA2]), 2);
3008 else
3009 buf[0] = 0;
3010
3011 return NL_SKIP;
3012 }
3013
3014 static int nl80211_get_country(const char *ifname, char *buf)
3015 {
3016 if (nl80211_request(ifname, NL80211_CMD_GET_REG, 0,
3017 nl80211_get_country_cb, buf))
3018 return -1;
3019
3020 return 0;
3021 }
3022
3023 static int nl80211_get_countrylist(const char *ifname, char *buf, int *len)
3024 {
3025 int count;
3026 struct iwinfo_country_entry *e = (struct iwinfo_country_entry *)buf;
3027 const struct iwinfo_iso3166_label *l;
3028
3029 for (l = IWINFO_ISO3166_NAMES, count = 0; l->iso3166; l++, e++, count++)
3030 {
3031 e->iso3166 = l->iso3166;
3032 e->ccode[0] = (l->iso3166 / 256);
3033 e->ccode[1] = (l->iso3166 % 256);
3034 e->ccode[2] = 0;
3035 }
3036
3037 *len = (count * sizeof(struct iwinfo_country_entry));
3038 return 0;
3039 }
3040
3041
3042 struct nl80211_modes
3043 {
3044 bool ok;
3045 uint32_t hw;
3046 uint32_t ht;
3047
3048 uint8_t bands;
3049
3050 uint16_t nl_ht;
3051 uint32_t nl_vht;
3052 uint16_t he_phy_cap[6];
3053 };
3054
3055 static int nl80211_eval_modelist(struct nl80211_modes *m)
3056 {
3057 /* Treat any nonzero capability as 11n */
3058 if (m->nl_ht > 0)
3059 {
3060 m->hw |= IWINFO_80211_N;
3061 m->ht |= IWINFO_HTMODE_HT20;
3062
3063 if (m->nl_ht & (1 << 1))
3064 m->ht |= IWINFO_HTMODE_HT40;
3065 }
3066
3067 if (m->he_phy_cap[0] != 0) {
3068 m->hw |= IWINFO_80211_AX;
3069 m->ht |= IWINFO_HTMODE_HE20;
3070
3071 if (m->he_phy_cap[0] & BIT(9))
3072 m->ht |= IWINFO_HTMODE_HE40;
3073 if (m->he_phy_cap[0] & BIT(10))
3074 m->ht |= IWINFO_HTMODE_HE40 | IWINFO_HTMODE_HE80;
3075 if (m->he_phy_cap[0] & BIT(11))
3076 m->ht |= IWINFO_HTMODE_HE160;
3077 if (m->he_phy_cap[0] & BIT(12))
3078 m->ht |= IWINFO_HTMODE_HE160 | IWINFO_HTMODE_HE80_80;
3079 }
3080
3081 if (m->bands & IWINFO_BAND_24)
3082 {
3083 m->hw |= IWINFO_80211_B;
3084 m->hw |= IWINFO_80211_G;
3085 }
3086
3087 if (m->bands & IWINFO_BAND_5)
3088 {
3089 /* Treat any nonzero capability as 11ac */
3090 if (m->nl_vht > 0)
3091 {
3092 m->hw |= IWINFO_80211_AC;
3093 m->ht |= IWINFO_HTMODE_VHT20 | IWINFO_HTMODE_VHT40 | IWINFO_HTMODE_VHT80;
3094
3095 switch ((m->nl_vht >> 2) & 3)
3096 {
3097 case 2:
3098 m->ht |= IWINFO_HTMODE_VHT80_80;
3099 /* fall through */
3100
3101 case 1:
3102 m->ht |= IWINFO_HTMODE_VHT160;
3103 }
3104 }
3105 else
3106 {
3107 m->hw |= IWINFO_80211_A;
3108 }
3109 }
3110
3111 if (m->bands & IWINFO_BAND_60)
3112 {
3113 m->hw |= IWINFO_80211_AD;
3114 }
3115
3116 }
3117
3118 static int nl80211_get_modelist_cb(struct nl_msg *msg, void *arg)
3119 {
3120 struct nl80211_modes *m = arg;
3121 int bands_remain, freqs_remain;
3122 uint16_t caps = 0;
3123 uint32_t vht_caps = 0;
3124 struct nlattr **attr = nl80211_parse(msg);
3125 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
3126 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
3127 struct nlattr *band, *freq;
3128 uint32_t freq_mhz;
3129
3130 if (attr[NL80211_ATTR_WIPHY_BANDS])
3131 {
3132 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
3133 {
3134 nla_parse(bands, NL80211_BAND_ATTR_MAX,
3135 nla_data(band), nla_len(band), NULL);
3136
3137 if (bands[NL80211_BAND_ATTR_HT_CAPA])
3138 m->nl_ht = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]);
3139
3140 if (bands[NL80211_BAND_ATTR_VHT_CAPA])
3141 m->nl_vht = nla_get_u32(bands[NL80211_BAND_ATTR_VHT_CAPA]);
3142
3143 if (bands[NL80211_BAND_ATTR_IFTYPE_DATA]) {
3144 struct nlattr *tb[NL80211_BAND_IFTYPE_ATTR_MAX + 1];
3145 struct nlattr *nl_iftype;
3146 int rem_band;
3147 int len;
3148
3149 nla_for_each_nested(nl_iftype, bands[NL80211_BAND_ATTR_IFTYPE_DATA], rem_band) {
3150 nla_parse(tb, NL80211_BAND_IFTYPE_ATTR_MAX,
3151 nla_data(nl_iftype), nla_len(nl_iftype), NULL);
3152 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]) {
3153 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]);
3154
3155 if (len > sizeof(m->he_phy_cap) - 1)
3156 len = sizeof(m->he_phy_cap) - 1;
3157 memcpy(&((__u8 *)m->he_phy_cap)[1],
3158 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]),
3159 len);
3160 }
3161 }
3162 }
3163
3164 if (bands[NL80211_BAND_ATTR_FREQS]) {
3165 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS],
3166 freqs_remain)
3167 {
3168 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
3169 nla_data(freq), nla_len(freq), NULL);
3170
3171 if (!freqs[NL80211_FREQUENCY_ATTR_FREQ])
3172 continue;
3173
3174 freq_mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
3175
3176 if (freq_mhz > 2400 && freq_mhz < 2485)
3177 {
3178 m->bands |= IWINFO_BAND_24;
3179 }
3180 else if (freq_mhz > 5000 && freq_mhz < 5850)
3181 {
3182 m->bands |= IWINFO_BAND_5;
3183 }
3184 else if (freq_mhz > 6000 && freq_mhz < 7120)
3185 {
3186 m->bands |= IWINFO_BAND_6;
3187 }
3188 else if (freq_mhz >= 56160)
3189 {
3190 m->bands |= IWINFO_BAND_60;
3191 }
3192 }
3193 }
3194 }
3195
3196 m->ok = 1;
3197 }
3198
3199 return NL_SKIP;
3200 }
3201
3202 static int nl80211_get_hwmodelist(const char *ifname, int *buf)
3203 {
3204 struct nl80211_msg_conveyor *cv;
3205 struct nl80211_modes m = {};
3206 uint32_t features = nl80211_get_protocol_features(ifname);
3207 int flags;
3208
3209 flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0;
3210 cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags);
3211 if (!cv)
3212 goto out;
3213
3214 NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
3215 if (nl80211_send(cv, nl80211_get_modelist_cb, &m))
3216 goto nla_put_failure;
3217
3218 nl80211_eval_modelist(&m);
3219
3220 *buf = m.hw;
3221
3222 return 0;
3223
3224 nla_put_failure:
3225 nl80211_free(cv);
3226 out:
3227 return -1;
3228 }
3229
3230 struct chan_info {
3231 int width;
3232 int mode;
3233 };
3234
3235 static int nl80211_get_htmode_cb(struct nl_msg *msg, void *arg)
3236 {
3237 struct nlattr **tb = nl80211_parse(msg);
3238 struct nlattr *cur;
3239 struct chan_info *chn = arg;
3240
3241 if ((cur = tb[NL80211_ATTR_CHANNEL_WIDTH]))
3242 chn->width = nla_get_u32(cur);
3243
3244 if ((cur = tb[NL80211_ATTR_BSS_HT_OPMODE]))
3245 chn->mode = nla_get_u32(cur);
3246
3247 return NL_SKIP;
3248 }
3249
3250 static int nl80211_get_htmode(const char *ifname, int *buf)
3251 {
3252 struct chan_info chn = { .width = 0, .mode = 0 };
3253 char *res;
3254 int err;
3255
3256 res = nl80211_phy2ifname(ifname);
3257 *buf = 0;
3258
3259 err = nl80211_request(res ? res : ifname,
3260 NL80211_CMD_GET_INTERFACE, 0,
3261 nl80211_get_htmode_cb, &chn);
3262 if (err)
3263 return -1;
3264
3265 switch (chn.width) {
3266 case NL80211_CHAN_WIDTH_20:
3267 if (chn.mode == -1)
3268 *buf = IWINFO_HTMODE_VHT20;
3269 else
3270 *buf = IWINFO_HTMODE_HT20;
3271 break;
3272 case NL80211_CHAN_WIDTH_40:
3273 if (chn.mode == -1)
3274 *buf = IWINFO_HTMODE_VHT40;
3275 else
3276 *buf = IWINFO_HTMODE_HT40;
3277 break;
3278 case NL80211_CHAN_WIDTH_80:
3279 *buf = IWINFO_HTMODE_VHT80;
3280 break;
3281 case NL80211_CHAN_WIDTH_80P80:
3282 *buf = IWINFO_HTMODE_VHT80_80;
3283 break;
3284 case NL80211_CHAN_WIDTH_160:
3285 *buf = IWINFO_HTMODE_VHT160;
3286 break;
3287 case NL80211_CHAN_WIDTH_5:
3288 case NL80211_CHAN_WIDTH_10:
3289 case NL80211_CHAN_WIDTH_20_NOHT:
3290 *buf = IWINFO_HTMODE_NOHT;
3291 break;
3292 default:
3293 return -1;
3294 }
3295
3296 return 0;
3297 }
3298
3299 static int nl80211_get_htmodelist(const char *ifname, int *buf)
3300 {
3301 struct nl80211_msg_conveyor *cv;
3302 struct nl80211_modes m = {};
3303 uint32_t features = nl80211_get_protocol_features(ifname);
3304 int flags;
3305
3306 flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0;
3307 cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags);
3308 if (!cv)
3309 goto out;
3310
3311 NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
3312 if (nl80211_send(cv, nl80211_get_modelist_cb, &m))
3313 goto nla_put_failure;
3314
3315 nl80211_eval_modelist(&m);
3316
3317 *buf = m.ht;
3318
3319 return 0;
3320
3321 nla_put_failure:
3322 nl80211_free(cv);
3323 out:
3324 return -1;
3325 }
3326
3327
3328 static int nl80211_get_ifcomb_cb(struct nl_msg *msg, void *arg)
3329 {
3330 struct nlattr **attr = nl80211_parse(msg);
3331 struct nlattr *comb;
3332 int *ret = arg;
3333 int comb_rem, limit_rem, mode_rem;
3334
3335 *ret = 0;
3336 if (!attr[NL80211_ATTR_INTERFACE_COMBINATIONS])
3337 return NL_SKIP;
3338
3339 nla_for_each_nested(comb, attr[NL80211_ATTR_INTERFACE_COMBINATIONS], comb_rem)
3340 {
3341 static struct nla_policy iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
3342 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
3343 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
3344 };
3345 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB+1];
3346 static struct nla_policy iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
3347 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
3348 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
3349 };
3350 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT+1];
3351 struct nlattr *limit;
3352
3353 nla_parse_nested(tb_comb, NUM_NL80211_IFACE_COMB, comb, iface_combination_policy);
3354
3355 if (!tb_comb[NL80211_IFACE_COMB_LIMITS])
3356 continue;
3357
3358 nla_for_each_nested(limit, tb_comb[NL80211_IFACE_COMB_LIMITS], limit_rem)
3359 {
3360 struct nlattr *mode;
3361
3362 nla_parse_nested(tb_limit, NUM_NL80211_IFACE_LIMIT, limit, iface_limit_policy);
3363
3364 if (!tb_limit[NL80211_IFACE_LIMIT_TYPES] ||
3365 !tb_limit[NL80211_IFACE_LIMIT_MAX])
3366 continue;
3367
3368 if (nla_get_u32(tb_limit[NL80211_IFACE_LIMIT_MAX]) < 2)
3369 continue;
3370
3371 nla_for_each_nested(mode, tb_limit[NL80211_IFACE_LIMIT_TYPES], mode_rem) {
3372 if (nla_type(mode) == NL80211_IFTYPE_AP)
3373 *ret = 1;
3374 }
3375 }
3376 }
3377
3378 return NL_SKIP;
3379 }
3380
3381 static int nl80211_get_mbssid_support(const char *ifname, int *buf)
3382 {
3383 if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
3384 nl80211_get_ifcomb_cb, buf))
3385 return -1;
3386
3387 return 0;
3388 }
3389
3390 static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id *id, const char *ifname)
3391 {
3392 char *phy, compat[64], path[PATH_MAX];
3393 int i;
3394
3395 /* Try to determine the phy name from the given interface */
3396 phy = nl80211_ifname2phy(ifname);
3397
3398 snprintf(path, sizeof(path), "/sys/class/%s/%s/device/of_node/compatible",
3399 phy ? "ieee80211" : "net", phy ? phy : ifname);
3400
3401 if (nl80211_readstr(path, compat, sizeof(compat)) <= 0)
3402 return -1;
3403
3404 if (!strcmp(compat, "qca,ar9130-wmac")) {
3405 id->vendor_id = 0x168c;
3406 id->device_id = 0x0029;
3407 id->subsystem_vendor_id = 0x168c;
3408 id->subsystem_device_id = 0x9130;
3409 } else if (!strcmp(compat, "qca,ar9330-wmac")) {
3410 id->vendor_id = 0x168c;
3411 id->device_id = 0x0030;
3412 id->subsystem_vendor_id = 0x168c;
3413 id->subsystem_device_id = 0x9330;
3414 } else if (!strcmp(compat, "qca,ar9340-wmac")) {
3415 id->vendor_id = 0x168c;
3416 id->device_id = 0x0030;
3417 id->subsystem_vendor_id = 0x168c;
3418 id->subsystem_device_id = 0x9340;
3419 } else if (!strcmp(compat, "qca,qca9530-wmac")) {
3420 id->vendor_id = 0x168c;
3421 id->device_id = 0x0033;
3422 id->subsystem_vendor_id = 0x168c;
3423 id->subsystem_device_id = 0x9530;
3424 } else if (!strcmp(compat, "qca,qca9550-wmac")) {
3425 id->vendor_id = 0x168c;
3426 id->device_id = 0x0033;
3427 id->subsystem_vendor_id = 0x168c;
3428 id->subsystem_device_id = 0x9550;
3429 } else if (!strcmp(compat, "qca,qca9560-wmac")) {
3430 id->vendor_id = 0x168c;
3431 id->device_id = 0x0033;
3432 id->subsystem_vendor_id = 0x168c;
3433 id->subsystem_device_id = 0x9560;
3434 } else if (!strcmp(compat, "qcom,ipq4019-wifi")) {
3435 id->vendor_id = 0x168c;
3436 id->device_id = 0x003c;
3437 id->subsystem_vendor_id = 0x168c;
3438 id->subsystem_device_id = 0x4019;
3439 } else if (!strcmp(compat, "mediatek,mt7622-wmac")) {
3440 id->vendor_id = 0x14c3;
3441 id->device_id = 0x7622;
3442 id->subsystem_vendor_id = 0x14c3;
3443 id->subsystem_device_id = 0x7622;
3444 } else if (!strcmp(compat, "mediatek,mt7986-wmac")) {
3445 id->vendor_id = 0x14c3;
3446 id->device_id = 0x7986;
3447 id->subsystem_vendor_id = 0x14c3;
3448 id->subsystem_device_id = 0x7986;
3449 }
3450
3451 return (id->vendor_id && id->device_id) ? 0 : -1;
3452 }
3453
3454
3455 static int nl80211_get_hardware_id(const char *ifname, char *buf)
3456 {
3457 struct iwinfo_hardware_id *id = (struct iwinfo_hardware_id *)buf;
3458 char *phy, num[8], path[PATH_MAX];
3459 int i;
3460
3461 struct { const char *path; uint16_t *dest; } lookup[] = {
3462 { "vendor", &id->vendor_id },
3463 { "device", &id->device_id },
3464 { "subsystem_vendor", &id->subsystem_vendor_id },
3465 { "subsystem_device", &id->subsystem_device_id }
3466 };
3467
3468 memset(id, 0, sizeof(*id));
3469
3470 /* Try to determine the phy name from the given interface */
3471 phy = nl80211_ifname2phy(ifname);
3472
3473 for (i = 0; i < ARRAY_SIZE(lookup); i++)
3474 {
3475 snprintf(path, sizeof(path), "/sys/class/%s/%s/device/%s",
3476 phy ? "ieee80211" : "net",
3477 phy ? phy : ifname, lookup[i].path);
3478
3479 if (nl80211_readstr(path, num, sizeof(num)) > 0)
3480 *lookup[i].dest = strtoul(num, NULL, 16);
3481 }
3482
3483 /* Failed to obtain hardware IDs, try FDT */
3484 if (id->vendor_id == 0 || id->device_id == 0)
3485 if (!nl80211_hardware_id_from_fdt(id, ifname))
3486 return 0;
3487
3488 /* Failed to obtain hardware IDs, search board config */
3489 if (id->vendor_id == 0 || id->device_id == 0)
3490 return iwinfo_hardware_id_from_mtd(id);
3491
3492 return 0;
3493 }
3494
3495 static const struct iwinfo_hardware_entry *
3496 nl80211_get_hardware_entry(const char *ifname)
3497 {
3498 struct iwinfo_hardware_id id;
3499
3500 if (nl80211_get_hardware_id(ifname, (char *)&id))
3501 return NULL;
3502
3503 return iwinfo_hardware(&id);
3504 }
3505
3506 static int nl80211_get_hardware_name(const char *ifname, char *buf)
3507 {
3508 const struct iwinfo_hardware_entry *hw;
3509
3510 if (!(hw = nl80211_get_hardware_entry(ifname)))
3511 sprintf(buf, "Generic MAC80211");
3512 else
3513 sprintf(buf, "%s %s", hw->vendor_name, hw->device_name);
3514
3515 return 0;
3516 }
3517
3518 static int nl80211_get_txpower_offset(const char *ifname, int *buf)
3519 {
3520 const struct iwinfo_hardware_entry *hw;
3521
3522 if (!(hw = nl80211_get_hardware_entry(ifname)))
3523 return -1;
3524
3525 *buf = hw->txpower_offset;
3526 return 0;
3527 }
3528
3529 static int nl80211_get_frequency_offset(const char *ifname, int *buf)
3530 {
3531 const struct iwinfo_hardware_entry *hw;
3532
3533 if (!(hw = nl80211_get_hardware_entry(ifname)))
3534 return -1;
3535
3536 *buf = hw->frequency_offset;
3537 return 0;
3538 }
3539
3540 static int nl80211_lookup_phyname(const char *section, char *buf)
3541 {
3542 const char *name;
3543 int idx;
3544
3545 if (!strncmp(section, "path=", 5))
3546 idx = nl80211_phy_idx_from_path(section + 5);
3547 else if (!strncmp(section, "macaddr=", 8))
3548 idx = nl80211_phy_idx_from_macaddr(section + 8);
3549 else
3550 idx = nl80211_phy_idx_from_uci(section);
3551
3552 if (idx < 0)
3553 return -1;
3554
3555 name = nl80211_phyidx2name(idx);
3556 if (!name)
3557 return -1;
3558
3559 strcpy(buf, name);
3560 return 0;
3561 }
3562
3563 static int nl80211_phy_path(const char *phyname, const char **path)
3564 {
3565 if (strchr(phyname, '/'))
3566 return -1;
3567
3568 *path = nl80211_phy_path_str(phyname);
3569 if (!*path)
3570 return -1;
3571
3572 return 0;
3573 }
3574
3575 const struct iwinfo_ops nl80211_ops = {
3576 .name = "nl80211",
3577 .probe = nl80211_probe,
3578 .channel = nl80211_get_channel,
3579 .center_chan1 = nl80211_get_center_chan1,
3580 .center_chan2 = nl80211_get_center_chan2,
3581 .frequency = nl80211_get_frequency,
3582 .frequency_offset = nl80211_get_frequency_offset,
3583 .txpower = nl80211_get_txpower,
3584 .txpower_offset = nl80211_get_txpower_offset,
3585 .bitrate = nl80211_get_bitrate,
3586 .signal = nl80211_get_signal,
3587 .noise = nl80211_get_noise,
3588 .quality = nl80211_get_quality,
3589 .quality_max = nl80211_get_quality_max,
3590 .mbssid_support = nl80211_get_mbssid_support,
3591 .hwmodelist = nl80211_get_hwmodelist,
3592 .htmodelist = nl80211_get_htmodelist,
3593 .htmode = nl80211_get_htmode,
3594 .mode = nl80211_get_mode,
3595 .ssid = nl80211_get_ssid,
3596 .bssid = nl80211_get_bssid,
3597 .country = nl80211_get_country,
3598 .hardware_id = nl80211_get_hardware_id,
3599 .hardware_name = nl80211_get_hardware_name,
3600 .encryption = nl80211_get_encryption,
3601 .phyname = nl80211_get_phyname,
3602 .assoclist = nl80211_get_assoclist,
3603 .txpwrlist = nl80211_get_txpwrlist,
3604 .scanlist = nl80211_get_scanlist,
3605 .freqlist = nl80211_get_freqlist,
3606 .countrylist = nl80211_get_countrylist,
3607 .survey = nl80211_get_survey,
3608 .lookup_phy = nl80211_lookup_phyname,
3609 .phy_path = nl80211_phy_path,
3610 .close = nl80211_close
3611 };