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