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