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