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