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