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