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