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