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