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