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