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