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