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