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