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