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