2 * iwinfo - Wireless Information Library - NL80211 Backend
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
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.
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.
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/.
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.
22 * Parts of this code are derived from the Linux iw utility.
30 #include "iwinfo_nl80211.h"
32 #define min(x, y) ((x) < (y)) ? (x) : (y)
34 #define BIT(x) (1ULL<<(x))
36 static struct nl80211_state *nls = NULL;
38 static void nl80211_close(void)
43 genl_family_put(nls->nlctrl);
46 genl_family_put(nls->nl80211);
49 nl_socket_free(nls->nl_sock);
52 nl_cache_free(nls->nl_cache);
59 static int nl80211_init(void)
65 nls = malloc(sizeof(struct nl80211_state));
71 memset(nls, 0, sizeof(*nls));
73 nls->nl_sock = nl_socket_alloc();
79 if (genl_connect(nls->nl_sock)) {
84 fd = nl_socket_get_fd(nls->nl_sock);
85 if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0) {
90 if (genl_ctrl_alloc_cache(nls->nl_sock, &nls->nl_cache)) {
95 nls->nl80211 = genl_ctrl_search_by_name(nls->nl_cache, "nl80211");
101 nls->nlctrl = genl_ctrl_search_by_name(nls->nl_cache, "nlctrl");
116 static int nl80211_readint(const char *path)
122 if ((fd = open(path, O_RDONLY)) > -1)
124 if (read(fd, buffer, sizeof(buffer)) > 0)
133 static int nl80211_readstr(const char *path, char *buffer, int length)
138 if ((fd = open(path, O_RDONLY)) > -1)
140 if ((rv = read(fd, buffer, length - 1)) > 0)
142 if (buffer[rv - 1] == '\n')
155 static int nl80211_msg_error(struct sockaddr_nl *nla,
156 struct nlmsgerr *err, void *arg)
163 static int nl80211_msg_finish(struct nl_msg *msg, void *arg)
170 static int nl80211_msg_ack(struct nl_msg *msg, void *arg)
177 static int nl80211_msg_response(struct nl_msg *msg, void *arg)
182 static void nl80211_free(struct nl80211_msg_conveyor *cv)
197 static struct nl80211_msg_conveyor * nl80211_new(struct genl_family *family,
200 static struct nl80211_msg_conveyor cv;
202 struct nl_msg *req = NULL;
203 struct nl_cb *cb = NULL;
209 cb = nl_cb_alloc(NL_CB_DEFAULT);
213 genlmsg_put(req, 0, 0, genl_family_get_id(family), 0, flags, cmd, 0);
227 static struct nl80211_msg_conveyor * nl80211_ctl(int cmd, int flags)
229 if (nl80211_init() < 0)
232 return nl80211_new(nls->nlctrl, cmd, flags);
235 static int nl80211_phy_idx_from_uci_path(struct uci_section *s)
242 opt = uci_lookup_option_string(uci_ctx, s, "path");
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); /**/
250 if (glob(buf, 0, NULL, &gl))
254 idx = nl80211_readint(gl.gl_pathv[0]);
261 static int nl80211_phy_idx_from_uci_macaddr(struct uci_section *s)
268 opt = uci_lookup_option_string(uci_ctx, s, "macaddr");
272 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/*"); /**/
273 if (glob(buf, 0, NULL, &gl))
276 for (i = 0; i < gl.gl_pathc; i++)
278 snprintf(buf, sizeof(buf), "%s/macaddress", gl.gl_pathv[i]);
279 if (nl80211_readstr(buf, buf, sizeof(buf)) <= 0)
282 if (fnmatch(opt, buf, FNM_CASEFOLD))
285 snprintf(buf, sizeof(buf), "%s/index", gl.gl_pathv[i]);
286 if ((idx = nl80211_readint(buf)) > -1)
295 static int nl80211_phy_idx_from_uci_phy(struct uci_section *s)
300 opt = uci_lookup_option_string(uci_ctx, s, "phy");
304 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", opt);
305 return nl80211_readint(buf);
308 static int nl80211_phy_idx_from_uci(const char *name)
310 struct uci_section *s;
313 s = iwinfo_uci_get_radio(name, "mac80211");
317 idx = nl80211_phy_idx_from_uci_path(s);
320 idx = nl80211_phy_idx_from_uci_macaddr(s);
323 idx = nl80211_phy_idx_from_uci_phy(s);
330 static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname,
333 int ifidx = -1, phyidx = -1;
334 struct nl80211_msg_conveyor *cv;
339 if (nl80211_init() < 0)
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]);
349 ifidx = if_nametoindex(ifname);
351 /* Valid ifidx must be greater than 0 */
352 if ((ifidx <= 0) && (phyidx < 0))
355 cv = nl80211_new(nls->nl80211, cmd, flags);
360 NLA_PUT_U32(cv->msg, NL80211_ATTR_IFINDEX, ifidx);
363 NLA_PUT_U32(cv->msg, NL80211_ATTR_WIPHY, phyidx);
372 static int nl80211_send(struct nl80211_msg_conveyor *cv,
373 int (*cb_func)(struct nl_msg *, void *),
376 static struct nl80211_msg_conveyor rcv;
380 nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, cb_func, cb_arg);
382 nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_msg_response, &rcv);
384 err = nl_send_auto_complete(nls->nl_sock, cv->msg);
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);
396 nl_recvmsgs(nls->nl_sock, cv->cb);
403 static int nl80211_request(const char *ifname, int cmd, int flags,
404 int (*cb_func)(struct nl_msg *, void *),
407 struct nl80211_msg_conveyor *cv;
409 cv = nl80211_msg(ifname, cmd, flags);
414 return nl80211_send(cv, cb_func, cb_arg);
417 static struct nlattr ** nl80211_parse(struct nl_msg *msg)
419 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
420 static struct nlattr *attr[NL80211_ATTR_MAX + 1];
422 nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
423 genlmsg_attrlen(gnlh, 0), NULL);
428 static int nl80211_get_protocol_features_cb(struct nl_msg *msg, void *arg)
430 uint32_t *features = arg;
431 struct nlattr **attr = nl80211_parse(msg);
433 if (attr[NL80211_ATTR_PROTOCOL_FEATURES])
434 *features = nla_get_u32(attr[NL80211_ATTR_PROTOCOL_FEATURES]);
439 static int nl80211_get_protocol_features(const char *ifname)
441 struct nl80211_msg_conveyor *req;
442 uint32_t features = 0;
444 req = nl80211_msg(ifname, NL80211_CMD_GET_PROTOCOL_FEATURES, 0);
446 nl80211_send(req, nl80211_get_protocol_features_cb, &features);
453 static int nl80211_subscribe_cb(struct nl_msg *msg, void *arg)
455 struct nl80211_group_conveyor *cv = arg;
457 struct nlattr **attr = nl80211_parse(msg);
458 struct nlattr *mgrpinfo[CTRL_ATTR_MCAST_GRP_MAX + 1];
462 if (!attr[CTRL_ATTR_MCAST_GROUPS])
465 nla_for_each_nested(mgrp, attr[CTRL_ATTR_MCAST_GROUPS], mgrpidx)
467 nla_parse(mgrpinfo, CTRL_ATTR_MCAST_GRP_MAX,
468 nla_data(mgrp), nla_len(mgrp), NULL);
470 if (mgrpinfo[CTRL_ATTR_MCAST_GRP_ID] &&
471 mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME] &&
472 !strncmp(nla_data(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME]),
473 cv->name, nla_len(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME])))
475 cv->id = nla_get_u32(mgrpinfo[CTRL_ATTR_MCAST_GRP_ID]);
483 static int nl80211_subscribe(const char *family, const char *group)
485 struct nl80211_group_conveyor cv = { .name = group, .id = -ENOENT };
486 struct nl80211_msg_conveyor *req;
489 req = nl80211_ctl(CTRL_CMD_GETFAMILY, 0);
492 NLA_PUT_STRING(req->msg, CTRL_ATTR_FAMILY_NAME, family);
493 err = nl80211_send(req, nl80211_subscribe_cb, &cv);
498 return nl_socket_add_membership(nls->nl_sock, cv.id);
508 static int nl80211_wait_cb(struct nl_msg *msg, void *arg)
510 struct nl80211_event_conveyor *cv = arg;
511 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
513 if (cv->wait[gnlh->cmd / 32] & (1 << (gnlh->cmd % 32)))
514 cv->recv = gnlh->cmd;
519 static int nl80211_wait_seq_check(struct nl_msg *msg, void *arg)
524 static int __nl80211_wait(const char *family, const char *group, ...)
526 struct nl80211_event_conveyor cv = { };
532 if (nl80211_subscribe(family, group))
535 cb = nl_cb_alloc(NL_CB_DEFAULT);
540 nl_cb_err(cb, NL_CB_CUSTOM, nl80211_msg_error, &err);
541 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, nl80211_wait_seq_check, NULL);
542 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_wait_cb, &cv );
546 for (cmd = va_arg(ap, int); cmd != 0; cmd = va_arg(ap, int))
547 cv.wait[cmd / 32] |= (1 << (cmd % 32));
551 while (!cv.recv && !err)
552 nl_recvmsgs(nls->nl_sock, cb);
559 #define nl80211_wait(family, group, ...) \
560 __nl80211_wait(family, group, __VA_ARGS__, 0)
563 static int nl80211_freq2channel(int freq)
567 else if (freq < 2484)
568 return (freq - 2407) / 5;
569 else if (freq >= 4910 && freq <= 4980)
570 return (freq - 4000) / 5;
572 return (freq - 5000) / 5;
575 static int nl80211_channel2freq(int channel, const char *band)
577 if (!band || band[0] != 'a')
581 else if (channel < 14)
582 return (channel * 5) + 2407;
586 if (channel >= 182 && channel <= 196)
587 return (channel * 5) + 4000;
589 return (channel * 5) + 5000;
595 static int nl80211_ifname2phy_cb(struct nl_msg *msg, void *arg)
598 struct nlattr **attr = nl80211_parse(msg);
600 if (attr[NL80211_ATTR_WIPHY_NAME])
601 memcpy(buf, nla_data(attr[NL80211_ATTR_WIPHY_NAME]),
602 nla_len(attr[NL80211_ATTR_WIPHY_NAME]));
609 static char * nl80211_ifname2phy(const char *ifname)
611 static char phy[32] = { 0 };
613 memset(phy, 0, sizeof(phy));
615 nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
616 nl80211_ifname2phy_cb, phy);
618 return phy[0] ? phy : NULL;
621 static char * nl80211_phy2ifname(const char *ifname)
623 int ifidx = -1, cifidx = -1, phyidx = -1;
625 static char nif[IFNAMSIZ] = { 0 };
630 /* Only accept phy name of the form phy%d or radio%d */
633 else if (!strncmp(ifname, "phy", 3))
634 phyidx = atoi(&ifname[3]);
635 else if (!strncmp(ifname, "radio", 5))
636 phyidx = nl80211_phy_idx_from_uci(ifname);
640 memset(nif, 0, sizeof(nif));
644 if ((d = opendir("/sys/class/net")) != NULL)
646 while ((e = readdir(d)) != NULL)
648 snprintf(buffer, sizeof(buffer),
649 "/sys/class/net/%s/phy80211/index", e->d_name);
651 if (nl80211_readint(buffer) == phyidx)
653 snprintf(buffer, sizeof(buffer),
654 "/sys/class/net/%s/ifindex", e->d_name);
656 if ((cifidx = nl80211_readint(buffer)) >= 0 &&
657 ((ifidx < 0) || (cifidx < ifidx)))
660 strncpy(nif, e->d_name, sizeof(nif) - 1);
669 return nif[0] ? nif : NULL;
672 static int nl80211_get_mode_cb(struct nl_msg *msg, void *arg)
675 struct nlattr **tb = nl80211_parse(msg);
676 const int ifmodes[NL80211_IFTYPE_MAX + 1] = {
677 IWINFO_OPMODE_UNKNOWN, /* unspecified */
678 IWINFO_OPMODE_ADHOC, /* IBSS */
679 IWINFO_OPMODE_CLIENT, /* managed */
680 IWINFO_OPMODE_MASTER, /* AP */
681 IWINFO_OPMODE_AP_VLAN, /* AP/VLAN */
682 IWINFO_OPMODE_WDS, /* WDS */
683 IWINFO_OPMODE_MONITOR, /* monitor */
684 IWINFO_OPMODE_MESHPOINT, /* mesh point */
685 IWINFO_OPMODE_P2P_CLIENT, /* P2P-client */
686 IWINFO_OPMODE_P2P_GO, /* P2P-GO */
689 if (tb[NL80211_ATTR_IFTYPE])
690 *mode = ifmodes[nla_get_u32(tb[NL80211_ATTR_IFTYPE])];
696 static int nl80211_get_mode(const char *ifname, int *buf)
700 *buf = IWINFO_OPMODE_UNKNOWN;
702 res = nl80211_phy2ifname(ifname);
704 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
705 nl80211_get_mode_cb, buf);
707 return (*buf == IWINFO_OPMODE_UNKNOWN) ? -1 : 0;
710 static int __nl80211_hostapd_query(const char *ifname, ...)
713 char *phy, *search, *dest, *key, *val, buf[128];
714 int len, mode, found = 0, match = 1;
717 if (nl80211_get_mode(ifname, &mode))
720 if (mode != IWINFO_OPMODE_MASTER && mode != IWINFO_OPMODE_AP_VLAN)
723 phy = nl80211_ifname2phy(ifname);
728 snprintf(buf, sizeof(buf), "/var/run/hostapd-%s.conf", phy);
729 fp = fopen(buf, "r");
734 va_start(ap, ifname);
736 /* clear all destination buffers */
739 while ((search = va_arg(ap_cur, char *)) != NULL)
741 dest = va_arg(ap_cur, char *);
742 len = va_arg(ap_cur, int);
744 memset(dest, 0, len);
749 /* iterate applicable lines and copy found values into dest buffers */
750 while (fgets(buf, sizeof(buf), fp))
752 key = strtok(buf, " =\t\n");
753 val = strtok(NULL, "\n");
755 if (!key || !val || !*key || *key == '#')
758 if (!strcmp(key, "interface") || !strcmp(key, "bss"))
759 match = !strcmp(ifname, val);
766 while ((search = va_arg(ap_cur, char *)) != NULL)
768 dest = va_arg(ap_cur, char *);
769 len = va_arg(ap_cur, int);
771 if (!strcmp(search, key))
773 strncpy(dest, val, len - 1);
789 #define nl80211_hostapd_query(ifname, ...) \
790 __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
793 static inline int nl80211_wpactl_recv(int sock, char *buf, int blen)
796 struct timeval tv = { 0, 256000 };
801 memset(buf, 0, blen);
803 if (select(sock + 1, &rfds, NULL, NULL, &tv) < 0)
806 if (!FD_ISSET(sock, &rfds))
809 return recv(sock, buf, blen - 1, 0);
812 static int nl80211_wpactl_connect(const char *ifname, struct sockaddr_un *local)
814 struct sockaddr_un remote = { 0 };
815 size_t remote_length, local_length;
817 int sock = socket(PF_UNIX, SOCK_DGRAM, 0);
821 remote.sun_family = AF_UNIX;
822 remote_length = sizeof(remote.sun_family) +
823 sprintf(remote.sun_path, "/var/run/wpa_supplicant-%s/%s",
826 if (fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC) < 0)
832 if (connect(sock, (struct sockaddr *)&remote, remote_length))
834 remote_length = sizeof(remote.sun_family) +
835 sprintf(remote.sun_path, "/var/run/wpa_supplicant/%s", ifname);
837 if (connect(sock, (struct sockaddr *)&remote, remote_length))
844 local->sun_family = AF_UNIX;
845 local_length = sizeof(local->sun_family) +
846 sprintf(local->sun_path, "/var/run/iwinfo-%s-%d", ifname, getpid());
848 if (bind(sock, (struct sockaddr *)local, local_length) < 0)
857 static int __nl80211_wpactl_query(const char *ifname, ...)
860 struct sockaddr_un local = { 0 };
861 int len, mode, found = 0, sock = -1;
862 char *search, *dest, *key, *val, *line, *pos, buf[512];
864 if (nl80211_get_mode(ifname, &mode))
867 if (mode != IWINFO_OPMODE_CLIENT && mode != IWINFO_OPMODE_ADHOC)
870 sock = nl80211_wpactl_connect(ifname, &local);
875 va_start(ap, ifname);
877 /* clear all destination buffers */
880 while ((search = va_arg(ap_cur, char *)) != NULL)
882 dest = va_arg(ap_cur, char *);
883 len = va_arg(ap_cur, int);
885 memset(dest, 0, len);
890 send(sock, "STATUS", 6, 0);
894 if (nl80211_wpactl_recv(sock, buf, sizeof(buf)) <= 0)
900 for (line = strtok_r(buf, "\n", &pos);
902 line = strtok_r(NULL, "\n", &pos))
904 key = strtok(line, "=");
905 val = strtok(NULL, "\n");
912 while ((search = va_arg(ap_cur, char *)) != NULL)
914 dest = va_arg(ap_cur, char *);
915 len = va_arg(ap_cur, int);
917 if (!strcmp(search, key))
919 strncpy(dest, val, len - 1);
934 unlink(local.sun_path);
939 #define nl80211_wpactl_query(ifname, ...) \
940 __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL)
943 static char * nl80211_ifadd(const char *ifname)
946 static char nif[IFNAMSIZ] = { 0 };
947 struct nl80211_msg_conveyor *req;
950 req = nl80211_msg(ifname, NL80211_CMD_NEW_INTERFACE, 0);
953 snprintf(nif, sizeof(nif), "tmp.%s", ifname);
955 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, nif);
956 NLA_PUT_U32(req->msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION);
958 nl80211_send(req, NULL, NULL);
960 snprintf(path, sizeof(path) - 1,
961 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif);
963 if ((sysfs = fopen(path, "w")) != NULL)
965 fwrite("0\n", 1, 2, sysfs);
978 static void nl80211_ifdel(const char *ifname)
980 struct nl80211_msg_conveyor *req;
983 req = nl80211_msg(ifname, NL80211_CMD_DEL_INTERFACE, 0);
986 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, ifname);
988 nl80211_send(req, NULL, NULL);
996 static void nl80211_hostapd_hup(const char *ifname)
1000 char *phy = nl80211_ifname2phy(ifname);
1004 snprintf(buf, sizeof(buf), "/var/run/wifi-%s.pid", phy);
1005 if ((fd = open(buf, O_RDONLY)) >= 0)
1007 if (read(fd, buf, sizeof(buf)) > 0)
1019 static int nl80211_probe(const char *ifname)
1021 return !!nl80211_ifname2phy(ifname);
1024 struct nl80211_ssid_bssid {
1025 unsigned char *ssid;
1026 unsigned char bssid[7];
1029 static int nl80211_get_ssid_bssid_cb(struct nl_msg *msg, void *arg)
1033 struct nl80211_ssid_bssid *sb = arg;
1034 struct nlattr **tb = nl80211_parse(msg);
1035 struct nlattr *bss[NL80211_BSS_MAX + 1];
1037 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1038 [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 },
1039 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1042 if (!tb[NL80211_ATTR_BSS] ||
1043 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1045 !bss[NL80211_BSS_BSSID] ||
1046 !bss[NL80211_BSS_STATUS] ||
1047 !bss[NL80211_BSS_INFORMATION_ELEMENTS])
1052 switch (nla_get_u32(bss[NL80211_BSS_STATUS]))
1054 case NL80211_BSS_STATUS_ASSOCIATED:
1055 case NL80211_BSS_STATUS_AUTHENTICATED:
1056 case NL80211_BSS_STATUS_IBSS_JOINED:
1060 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1061 ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1063 while (ielen >= 2 && ielen >= ie[1])
1067 memcpy(sb->ssid, ie + 2, min(ie[1], IWINFO_ESSID_MAX_SIZE));
1078 memcpy(sb->bssid + 1, nla_data(bss[NL80211_BSS_BSSID]), 6);
1087 static int nl80211_get_ssid(const char *ifname, char *buf)
1090 struct nl80211_ssid_bssid sb = { .ssid = (unsigned char *)buf };
1092 /* try to find ssid from scan dump results */
1093 res = nl80211_phy2ifname(ifname);
1096 nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
1097 nl80211_get_ssid_bssid_cb, &sb);
1099 /* failed, try to find from hostapd info */
1100 if (sb.ssid[0] == 0)
1101 nl80211_hostapd_query(ifname, "ssid", sb.ssid,
1102 IWINFO_ESSID_MAX_SIZE + 1);
1104 return (sb.ssid[0] == 0) ? -1 : 0;
1107 static int nl80211_get_bssid(const char *ifname, char *buf)
1109 char *res, bssid[sizeof("FF:FF:FF:FF:FF:FF\0")];
1110 struct nl80211_ssid_bssid sb = { };
1112 /* try to find bssid from scan dump results */
1113 res = nl80211_phy2ifname(ifname);
1115 nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
1116 nl80211_get_ssid_bssid_cb, &sb);
1118 /* failed, try to find mac from hostapd info */
1119 if ((sb.bssid[0] == 0) &&
1120 nl80211_hostapd_query(ifname, "bssid", bssid, sizeof(bssid)))
1123 sb.bssid[1] = strtol(&bssid[0], NULL, 16);
1124 sb.bssid[2] = strtol(&bssid[3], NULL, 16);
1125 sb.bssid[3] = strtol(&bssid[6], NULL, 16);
1126 sb.bssid[4] = strtol(&bssid[9], NULL, 16);
1127 sb.bssid[5] = strtol(&bssid[12], NULL, 16);
1128 sb.bssid[6] = strtol(&bssid[15], NULL, 16);
1133 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
1134 sb.bssid[1], sb.bssid[2], sb.bssid[3],
1135 sb.bssid[4], sb.bssid[5], sb.bssid[6]);
1144 static int nl80211_get_frequency_scan_cb(struct nl_msg *msg, void *arg)
1147 struct nlattr **attr = nl80211_parse(msg);
1148 struct nlattr *binfo[NL80211_BSS_MAX + 1];
1150 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1151 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1152 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1155 if (attr[NL80211_ATTR_BSS] &&
1156 !nla_parse_nested(binfo, NL80211_BSS_MAX,
1157 attr[NL80211_ATTR_BSS], bss_policy))
1159 if (binfo[NL80211_BSS_STATUS] && binfo[NL80211_BSS_FREQUENCY])
1160 *freq = nla_get_u32(binfo[NL80211_BSS_FREQUENCY]);
1166 static int nl80211_get_frequency_info_cb(struct nl_msg *msg, void *arg)
1169 struct nlattr **tb = nl80211_parse(msg);
1171 if (tb[NL80211_ATTR_WIPHY_FREQ])
1172 *freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1177 static int nl80211_get_frequency(const char *ifname, int *buf)
1179 char *res, channel[4], hwmode[2];
1181 /* try to find frequency from interface info */
1182 res = nl80211_phy2ifname(ifname);
1185 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
1186 nl80211_get_frequency_info_cb, buf);
1188 /* failed, try to find frequency from hostapd info */
1190 nl80211_hostapd_query(ifname, "hw_mode", hwmode, sizeof(hwmode),
1191 "channel", channel, sizeof(channel)) == 2)
1193 *buf = nl80211_channel2freq(atoi(channel), hwmode);
1196 /* failed, try to find frequency from scan results */
1199 res = nl80211_phy2ifname(ifname);
1201 nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
1202 nl80211_get_frequency_scan_cb, buf);
1205 return (*buf == 0) ? -1 : 0;
1208 static int nl80211_get_channel(const char *ifname, int *buf)
1210 if (!nl80211_get_frequency(ifname, buf))
1212 *buf = nl80211_freq2channel(*buf);
1219 static int nl80211_get_txpower_cb(struct nl_msg *msg, void *arg)
1222 struct nlattr **tb = nl80211_parse(msg);
1224 if (tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL])
1225 *buf = iwinfo_mbm2dbm(nla_get_u32(tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]));
1230 static int nl80211_get_txpower(const char *ifname, int *buf)
1234 res = nl80211_phy2ifname(ifname);
1237 if (nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
1238 nl80211_get_txpower_cb, buf))
1245 static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
1249 struct nl80211_rssi_rate *rr = arg;
1250 struct nlattr **attr = nl80211_parse(msg);
1251 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1252 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1254 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
1255 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
1256 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
1257 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
1258 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
1259 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
1260 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1261 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
1262 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
1263 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
1264 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
1267 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1268 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1269 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1270 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1271 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1274 if (attr[NL80211_ATTR_STA_INFO])
1276 if (!nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1277 attr[NL80211_ATTR_STA_INFO], stats_policy))
1279 if (sinfo[NL80211_STA_INFO_SIGNAL])
1281 dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1282 rr->rssi = (rr->rssi * rr->rssi_samples + dbm) / (rr->rssi_samples + 1);
1286 if (sinfo[NL80211_STA_INFO_TX_BITRATE])
1288 if (!nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1289 sinfo[NL80211_STA_INFO_TX_BITRATE],
1292 if (rinfo[NL80211_RATE_INFO_BITRATE])
1294 mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
1295 rr->rate = (rr->rate * rr->rate_samples + mbit) / (rr->rate_samples + 1);
1306 static void nl80211_fill_signal(const char *ifname, struct nl80211_rssi_rate *r)
1311 memset(r, 0, sizeof(*r));
1313 if ((d = opendir("/sys/class/net")) != NULL)
1315 while ((de = readdir(d)) != NULL)
1317 if (!strncmp(de->d_name, ifname, strlen(ifname)) &&
1318 (!de->d_name[strlen(ifname)] ||
1319 !strncmp(&de->d_name[strlen(ifname)], ".sta", 4)))
1321 nl80211_request(de->d_name, NL80211_CMD_GET_STATION,
1322 NLM_F_DUMP, nl80211_fill_signal_cb, r);
1330 static int nl80211_get_bitrate(const char *ifname, int *buf)
1332 struct nl80211_rssi_rate rr;
1334 nl80211_fill_signal(ifname, &rr);
1336 if (rr.rate_samples)
1338 *buf = (rr.rate * 100);
1345 static int nl80211_get_signal(const char *ifname, int *buf)
1347 struct nl80211_rssi_rate rr;
1349 nl80211_fill_signal(ifname, &rr);
1351 if (rr.rssi_samples)
1360 static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg)
1362 int8_t *noise = arg;
1363 struct nlattr **tb = nl80211_parse(msg);
1364 struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
1366 static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
1367 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1368 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1371 if (!tb[NL80211_ATTR_SURVEY_INFO])
1374 if (nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
1375 tb[NL80211_ATTR_SURVEY_INFO], sp))
1378 if (!si[NL80211_SURVEY_INFO_NOISE])
1381 if (!*noise || si[NL80211_SURVEY_INFO_IN_USE])
1382 *noise = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
1388 static int nl80211_get_noise(const char *ifname, int *buf)
1392 if (nl80211_request(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP,
1393 nl80211_get_noise_cb, &noise))
1404 static int nl80211_get_quality(const char *ifname, int *buf)
1408 if (!nl80211_get_signal(ifname, &signal))
1410 /* A positive signal level is usually just a quality
1411 * value, pass through as-is */
1417 /* The cfg80211 wext compat layer assumes a signal range
1418 * of -110 dBm to -40 dBm, the quality value is derived
1419 * by adding 110 to the signal level */
1424 else if (signal > -40)
1427 *buf = (signal + 110);
1436 static int nl80211_get_quality_max(const char *ifname, int *buf)
1438 /* The cfg80211 wext compat layer assumes a maximum
1445 static int nl80211_check_wepkey(const char *key)
1449 switch (strlen(key))
1453 return IWINFO_CIPHER_WEP40;
1457 return IWINFO_CIPHER_WEP104;
1464 static int nl80211_get_encryption(const char *ifname, char *buf)
1466 char wpa[2], wpa_key_mgmt[16], wpa_pairwise[16], wpa_groupwise[16];
1467 char auth_algs[2], wep_key0[27], wep_key1[27], wep_key2[27], wep_key3[27];
1469 struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
1471 /* WPA supplicant */
1472 if (nl80211_wpactl_query(ifname,
1473 "pairwise_cipher", wpa_pairwise, sizeof(wpa_pairwise),
1474 "group_cipher", wpa_groupwise, sizeof(wpa_groupwise),
1475 "key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt)))
1478 if (!strcmp(wpa_key_mgmt, "NONE"))
1480 if (strstr(wpa_pairwise, "WEP-40"))
1481 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
1482 else if (strstr(wpa_pairwise, "WEP-104"))
1483 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
1485 if (strstr(wpa_groupwise, "WEP-40"))
1486 c->group_ciphers |= IWINFO_CIPHER_WEP40;
1487 else if (strstr(wpa_groupwise, "WEP-104"))
1488 c->group_ciphers |= IWINFO_CIPHER_WEP104;
1490 c->enabled = !!(c->pair_ciphers | c->group_ciphers);
1491 c->auth_suites |= IWINFO_KMGMT_NONE;
1492 c->auth_algs |= IWINFO_AUTH_OPEN; /* XXX: assumption */
1496 else if (strstr(wpa_key_mgmt, "WPA"))
1498 if (strstr(wpa_pairwise, "TKIP"))
1499 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
1500 else if (strstr(wpa_pairwise, "CCMP"))
1501 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
1502 else if (strstr(wpa_pairwise, "NONE"))
1503 c->pair_ciphers |= IWINFO_CIPHER_NONE;
1504 else if (strstr(wpa_pairwise, "WEP-40"))
1505 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
1506 else if (strstr(wpa_pairwise, "WEP-104"))
1507 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
1509 if (strstr(wpa_groupwise, "TKIP"))
1510 c->group_ciphers |= IWINFO_CIPHER_TKIP;
1511 else if (strstr(wpa_groupwise, "CCMP"))
1512 c->group_ciphers |= IWINFO_CIPHER_CCMP;
1513 else if (strstr(wpa_groupwise, "NONE"))
1514 c->group_ciphers |= IWINFO_CIPHER_NONE;
1515 else if (strstr(wpa_groupwise, "WEP-40"))
1516 c->group_ciphers |= IWINFO_CIPHER_WEP40;
1517 else if (strstr(wpa_groupwise, "WEP-104"))
1518 c->group_ciphers |= IWINFO_CIPHER_WEP104;
1520 if (strstr(wpa_key_mgmt, "WPA2"))
1522 else if (strstr(wpa_key_mgmt, "WPA"))
1525 if (strstr(wpa_key_mgmt, "PSK"))
1526 c->auth_suites |= IWINFO_KMGMT_PSK;
1527 else if (strstr(wpa_key_mgmt, "EAP") ||
1528 strstr(wpa_key_mgmt, "802.1X"))
1529 c->auth_suites |= IWINFO_KMGMT_8021x;
1530 else if (strstr(wpa_key_mgmt, "NONE"))
1531 c->auth_suites |= IWINFO_KMGMT_NONE;
1533 c->enabled = !!(c->wpa_version && c->auth_suites);
1540 else if (nl80211_hostapd_query(ifname,
1541 "wpa", wpa, sizeof(wpa),
1542 "wpa_key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt),
1543 "wpa_pairwise", wpa_pairwise, sizeof(wpa_pairwise),
1544 "auth_algs", auth_algs, sizeof(auth_algs),
1545 "wep_key0", wep_key0, sizeof(wep_key0),
1546 "wep_key1", wep_key1, sizeof(wep_key1),
1547 "wep_key2", wep_key2, sizeof(wep_key2),
1548 "wep_key3", wep_key3, sizeof(wep_key3)))
1550 c->wpa_version = wpa[0] ? atoi(wpa) : 0;
1552 if (wpa_key_mgmt[0])
1554 if (strstr(wpa_key_mgmt, "PSK"))
1555 c->auth_suites |= IWINFO_KMGMT_PSK;
1557 if (strstr(wpa_key_mgmt, "EAP"))
1558 c->auth_suites |= IWINFO_KMGMT_8021x;
1560 if (strstr(wpa_key_mgmt, "NONE"))
1561 c->auth_suites |= IWINFO_KMGMT_NONE;
1565 c->auth_suites |= IWINFO_KMGMT_PSK;
1568 if (wpa_pairwise[0])
1570 if (strstr(wpa_pairwise, "TKIP"))
1571 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
1573 if (strstr(wpa_pairwise, "CCMP"))
1574 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
1576 if (strstr(wpa_pairwise, "NONE"))
1577 c->pair_ciphers |= IWINFO_CIPHER_NONE;
1582 switch(atoi(auth_algs))
1585 c->auth_algs |= IWINFO_AUTH_OPEN;
1589 c->auth_algs |= IWINFO_AUTH_SHARED;
1593 c->auth_algs |= IWINFO_AUTH_OPEN;
1594 c->auth_algs |= IWINFO_AUTH_SHARED;
1598 c->pair_ciphers |= nl80211_check_wepkey(wep_key0);
1599 c->pair_ciphers |= nl80211_check_wepkey(wep_key1);
1600 c->pair_ciphers |= nl80211_check_wepkey(wep_key2);
1601 c->pair_ciphers |= nl80211_check_wepkey(wep_key3);
1604 c->group_ciphers = c->pair_ciphers;
1605 c->enabled = (c->wpa_version || c->pair_ciphers) ? 1 : 0;
1613 static int nl80211_get_phyname(const char *ifname, char *buf)
1617 name = nl80211_ifname2phy(ifname);
1624 else if ((name = nl80211_phy2ifname(ifname)) != NULL)
1626 name = nl80211_ifname2phy(name);
1630 strcpy(buf, ifname);
1639 static void nl80211_parse_rateinfo(struct nlattr **ri,
1640 struct iwinfo_rate_entry *re)
1642 if (ri[NL80211_RATE_INFO_BITRATE32])
1643 re->rate = nla_get_u32(ri[NL80211_RATE_INFO_BITRATE32]) * 100;
1644 else if (ri[NL80211_RATE_INFO_BITRATE])
1645 re->rate = nla_get_u16(ri[NL80211_RATE_INFO_BITRATE]) * 100;
1647 if (ri[NL80211_RATE_INFO_VHT_MCS])
1650 re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_VHT_MCS]);
1652 if (ri[NL80211_RATE_INFO_VHT_NSS])
1653 re->nss = nla_get_u8(ri[NL80211_RATE_INFO_VHT_NSS]);
1655 else if (ri[NL80211_RATE_INFO_MCS])
1658 re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_MCS]);
1661 if (ri[NL80211_RATE_INFO_5_MHZ_WIDTH])
1663 else if (ri[NL80211_RATE_INFO_10_MHZ_WIDTH])
1665 else if (ri[NL80211_RATE_INFO_40_MHZ_WIDTH])
1667 else if (ri[NL80211_RATE_INFO_80_MHZ_WIDTH])
1669 else if (ri[NL80211_RATE_INFO_80P80_MHZ_WIDTH] ||
1670 ri[NL80211_RATE_INFO_160_MHZ_WIDTH])
1675 if (ri[NL80211_RATE_INFO_SHORT_GI])
1676 re->is_short_gi = 1;
1678 re->is_40mhz = (re->mhz == 40);
1681 static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
1683 struct nl80211_array_buf *arr = arg;
1684 struct iwinfo_assoclist_entry *e = arr->buf;
1685 struct nlattr **attr = nl80211_parse(msg);
1686 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1687 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1688 struct nl80211_sta_flag_update *sta_flags;
1690 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
1691 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
1692 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
1693 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
1694 [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED },
1695 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
1696 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1697 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
1698 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
1699 [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
1700 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
1701 [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 },
1702 [NL80211_STA_INFO_STA_FLAGS] =
1703 { .minlen = sizeof(struct nl80211_sta_flag_update) },
1704 [NL80211_STA_INFO_EXPECTED_THROUGHPUT] = { .type = NLA_U32 },
1707 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1708 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1709 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1710 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1711 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1714 /* advance to end of array */
1716 memset(e, 0, sizeof(*e));
1718 if (attr[NL80211_ATTR_MAC])
1719 memcpy(e->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
1721 if (attr[NL80211_ATTR_STA_INFO] &&
1722 !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1723 attr[NL80211_ATTR_STA_INFO], stats_policy))
1725 if (sinfo[NL80211_STA_INFO_SIGNAL])
1726 e->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1728 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
1729 e->inactive = nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]);
1731 if (sinfo[NL80211_STA_INFO_RX_PACKETS])
1732 e->rx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]);
1734 if (sinfo[NL80211_STA_INFO_TX_PACKETS])
1735 e->tx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]);
1737 if (sinfo[NL80211_STA_INFO_RX_BITRATE] &&
1738 !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1739 sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy))
1740 nl80211_parse_rateinfo(rinfo, &e->rx_rate);
1742 if (sinfo[NL80211_STA_INFO_TX_BITRATE] &&
1743 !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1744 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy))
1745 nl80211_parse_rateinfo(rinfo, &e->tx_rate);
1747 if (sinfo[NL80211_STA_INFO_RX_BYTES])
1748 e->rx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]);
1750 if (sinfo[NL80211_STA_INFO_TX_BYTES])
1751 e->tx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]);
1753 if (sinfo[NL80211_STA_INFO_TX_RETRIES])
1754 e->tx_retries = nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]);
1756 if (sinfo[NL80211_STA_INFO_TX_FAILED])
1757 e->tx_failed = nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]);
1759 if (sinfo[NL80211_STA_INFO_T_OFFSET])
1760 e->t_offset = nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]);
1762 if (sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT])
1763 e->thr = nla_get_u32(sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]);
1766 if (sinfo[NL80211_STA_INFO_STA_FLAGS])
1768 sta_flags = (struct nl80211_sta_flag_update *)
1769 nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]);
1771 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
1772 sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED))
1773 e->is_authorized = 1;
1775 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1776 sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
1777 e->is_authenticated = 1;
1779 if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) &&
1780 sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
1781 e->is_preamble_short = 1;
1783 if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME) &&
1784 sta_flags->set & BIT(NL80211_STA_FLAG_WME))
1787 if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP) &&
1788 sta_flags->set & BIT(NL80211_STA_FLAG_MFP))
1791 if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER) &&
1792 sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER))
1797 e->noise = 0; /* filled in by caller */
1803 static int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
1808 struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
1809 struct iwinfo_assoclist_entry *e;
1811 if ((d = opendir("/sys/class/net")) != NULL)
1813 while ((de = readdir(d)) != NULL)
1815 if (!strncmp(de->d_name, ifname, strlen(ifname)) &&
1816 (!de->d_name[strlen(ifname)] ||
1817 !strncmp(&de->d_name[strlen(ifname)], ".sta", 4)))
1819 nl80211_request(de->d_name, NL80211_CMD_GET_STATION,
1820 NLM_F_DUMP, nl80211_get_assoclist_cb, &arr);
1826 if (!nl80211_get_noise(ifname, &noise))
1827 for (i = 0, e = arr.buf; i < arr.count; i++, e++)
1830 *len = (arr.count * sizeof(struct iwinfo_assoclist_entry));
1837 static int nl80211_get_txpwrlist_cb(struct nl_msg *msg, void *arg)
1840 int ch_cur, ch_cmp, bands_remain, freqs_remain;
1842 struct nlattr **attr = nl80211_parse(msg);
1843 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
1844 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
1845 struct nlattr *band, *freq;
1847 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1848 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1849 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1850 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1851 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1852 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1853 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
1856 ch_cur = *dbm_max; /* value int* is initialized with channel by caller */
1859 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
1861 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
1862 nla_len(band), NULL);
1864 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
1866 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
1867 nla_data(freq), nla_len(freq), freq_policy);
1869 ch_cmp = nl80211_freq2channel(nla_get_u32(
1870 freqs[NL80211_FREQUENCY_ATTR_FREQ]));
1872 if ((!ch_cur || (ch_cmp == ch_cur)) &&
1873 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER])
1875 *dbm_max = (int)(0.01 * nla_get_u32(
1876 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
1886 static int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
1889 int dbm_max = -1, dbm_cur, dbm_cnt;
1890 struct nl80211_msg_conveyor *req;
1891 struct iwinfo_txpwrlist_entry entry;
1893 if (nl80211_get_channel(ifname, &ch_cur))
1896 /* initialize the value pointer with channel for callback */
1899 err = nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
1900 nl80211_get_txpwrlist_cb, &dbm_max);
1904 for (dbm_cur = 0, dbm_cnt = 0;
1906 dbm_cur++, dbm_cnt++)
1908 entry.dbm = dbm_cur;
1909 entry.mw = iwinfo_dbm2mw(dbm_cur);
1911 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
1914 entry.dbm = dbm_max;
1915 entry.mw = iwinfo_dbm2mw(dbm_max);
1917 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
1920 *len = dbm_cnt * sizeof(entry);
1927 static void nl80211_get_scancrypto(const char *spec,
1928 struct iwinfo_crypto_entry *c)
1930 if (strstr(spec, "WPA") || strstr(spec, "WEP"))
1934 if (strstr(spec, "WPA2-") && strstr(spec, "WPA-"))
1937 else if (strstr(spec, "WPA2"))
1940 else if (strstr(spec, "WPA"))
1943 else if (strstr(spec, "WEP"))
1944 c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
1947 if (strstr(spec, "PSK"))
1948 c->auth_suites |= IWINFO_KMGMT_PSK;
1950 if (strstr(spec, "802.1X") || strstr(spec, "EAP"))
1951 c->auth_suites |= IWINFO_KMGMT_8021x;
1953 if (strstr(spec, "WPA-NONE"))
1954 c->auth_suites |= IWINFO_KMGMT_NONE;
1957 if (strstr(spec, "TKIP"))
1958 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
1960 if (strstr(spec, "CCMP"))
1961 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
1963 if (strstr(spec, "WEP-40"))
1964 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
1966 if (strstr(spec, "WEP-104"))
1967 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
1969 c->group_ciphers = c->pair_ciphers;
1978 struct nl80211_scanlist {
1979 struct iwinfo_scanlist_entry *e;
1984 static void nl80211_get_scanlist_ie(struct nlattr **bss,
1985 struct iwinfo_scanlist_entry *e)
1987 int ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1988 unsigned char *ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1989 static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 };
1992 while (ielen >= 2 && ielen >= ie[1])
1997 len = min(ie[1], IWINFO_ESSID_MAX_SIZE);
1998 memcpy(e->ssid, ie + 2, len);
2003 iwinfo_parse_rsn(&e->crypto, ie + 2, ie[1],
2004 IWINFO_CIPHER_CCMP, IWINFO_KMGMT_8021x);
2007 case 221: /* Vendor */
2008 if (ie[1] >= 4 && !memcmp(ie + 2, ms_oui, 3) && ie[5] == 1)
2009 iwinfo_parse_rsn(&e->crypto, ie + 6, ie[1] - 4,
2010 IWINFO_CIPHER_TKIP, IWINFO_KMGMT_PSK);
2019 static int nl80211_get_scanlist_cb(struct nl_msg *msg, void *arg)
2024 struct nl80211_scanlist *sl = arg;
2025 struct nlattr **tb = nl80211_parse(msg);
2026 struct nlattr *bss[NL80211_BSS_MAX + 1];
2028 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
2029 [NL80211_BSS_TSF] = { .type = NLA_U64 },
2030 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
2031 [NL80211_BSS_BSSID] = { 0 },
2032 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
2033 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
2034 [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 },
2035 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
2036 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
2037 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
2038 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
2039 [NL80211_BSS_BEACON_IES] = { 0 },
2042 if (!tb[NL80211_ATTR_BSS] ||
2043 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
2045 !bss[NL80211_BSS_BSSID])
2050 if (bss[NL80211_BSS_CAPABILITY])
2051 caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
2055 memset(sl->e, 0, sizeof(*sl->e));
2056 memcpy(sl->e->mac, nla_data(bss[NL80211_BSS_BSSID]), 6);
2059 sl->e->mode = IWINFO_OPMODE_ADHOC;
2060 else if (caps & (1<<0))
2061 sl->e->mode = IWINFO_OPMODE_MASTER;
2063 sl->e->mode = IWINFO_OPMODE_MESHPOINT;
2066 sl->e->crypto.enabled = 1;
2068 if (bss[NL80211_BSS_FREQUENCY])
2069 sl->e->channel = nl80211_freq2channel(nla_get_u32(
2070 bss[NL80211_BSS_FREQUENCY]));
2072 if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
2073 nl80211_get_scanlist_ie(bss, sl->e);
2075 if (bss[NL80211_BSS_SIGNAL_MBM])
2078 (uint8_t)((int32_t)nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]) / 100);
2080 rssi = sl->e->signal - 0x100;
2084 else if (rssi > -40)
2087 sl->e->quality = (rssi + 110);
2088 sl->e->quality_max = 70;
2091 if (sl->e->crypto.enabled && !sl->e->crypto.wpa_version)
2093 sl->e->crypto.auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
2094 sl->e->crypto.pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
2103 static int nl80211_get_scanlist_nl(const char *ifname, char *buf, int *len)
2105 struct nl80211_scanlist sl = { .e = (struct iwinfo_scanlist_entry *)buf };
2107 if (nl80211_request(ifname, NL80211_CMD_TRIGGER_SCAN, 0, NULL, NULL))
2110 if (nl80211_wait("nl80211", "scan",
2111 NL80211_CMD_NEW_SCAN_RESULTS, NL80211_CMD_SCAN_ABORTED))
2114 if (nl80211_request(ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
2115 nl80211_get_scanlist_cb, &sl))
2118 *len = sl.len * sizeof(struct iwinfo_scanlist_entry);
2126 static int wpasupp_ssid_decode(const char *in, char *out, int outlen)
2129 (((x) >= 'a') ? ((x) - 'a' + 10) : \
2130 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
2136 if (len + 1 >= outlen)
2146 out[len++] = '\n'; in++;
2150 out[len++] = '\r'; in++;
2154 out[len++] = '\t'; in++;
2158 out[len++] = '\033'; in++;
2162 if (isxdigit(*(in+1)) && isxdigit(*(in+2)))
2163 out[len++] = hex(*(in+1)) * 16 + hex(*(in+2));
2185 static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len)
2187 int sock, qmax, rssi, tries, count = -1, ready = 0;
2188 char *pos, *line, *bssid, *freq, *signal, *flags, *ssid, reply[4096];
2189 struct sockaddr_un local = { 0 };
2190 struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
2192 sock = nl80211_wpactl_connect(ifname, &local);
2197 send(sock, "ATTACH", 6, 0);
2198 send(sock, "SCAN", 4, 0);
2201 * wait for scan results:
2202 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
2203 * 72 channels at most. We'll also receive two "OK" messages acknowledging
2204 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
2205 * time to process the results, so try 72 + 2 + 1 times.
2207 for (tries = 0; tries < 75; tries++)
2209 if (nl80211_wpactl_recv(sock, reply, sizeof(reply)) <= 0)
2212 /* got an event notification */
2213 if (reply[0] == '<')
2215 /* scan results are ready */
2216 if (strstr(reply, "CTRL-EVENT-SCAN-RESULTS"))
2218 /* send "SCAN_RESULTS" command */
2219 ready = (send(sock, "SCAN_RESULTS", 12, 0) == 12);
2223 /* is another unrelated event, retry */
2228 /* receive and parse scan results if the wait above didn't time out */
2229 while (ready && nl80211_wpactl_recv(sock, reply, sizeof(reply)) > 0)
2231 /* received an event notification, receive again */
2232 if (reply[0] == '<')
2235 nl80211_get_quality_max(ifname, &qmax);
2237 for (line = strtok_r(reply, "\n", &pos);
2239 line = strtok_r(NULL, "\n", &pos))
2241 /* skip header line */
2248 bssid = strtok(line, "\t");
2249 freq = strtok(NULL, "\t");
2250 signal = strtok(NULL, "\t");
2251 flags = strtok(NULL, "\t");
2252 ssid = strtok(NULL, "\n");
2254 if (!bssid || !freq || !signal || !flags || !ssid)
2258 e->mac[0] = strtol(&bssid[0], NULL, 16);
2259 e->mac[1] = strtol(&bssid[3], NULL, 16);
2260 e->mac[2] = strtol(&bssid[6], NULL, 16);
2261 e->mac[3] = strtol(&bssid[9], NULL, 16);
2262 e->mac[4] = strtol(&bssid[12], NULL, 16);
2263 e->mac[5] = strtol(&bssid[15], NULL, 16);
2266 wpasupp_ssid_decode(ssid, e->ssid, sizeof(e->ssid));
2269 if (strstr(flags, "[MESH]"))
2270 e->mode = IWINFO_OPMODE_MESHPOINT;
2271 else if (strstr(flags, "[IBSS]"))
2272 e->mode = IWINFO_OPMODE_ADHOC;
2274 e->mode = IWINFO_OPMODE_MASTER;
2277 e->channel = nl80211_freq2channel(atoi(freq));
2280 rssi = atoi(signal);
2286 /* The cfg80211 wext compat layer assumes a signal range
2287 * of -110 dBm to -40 dBm, the quality value is derived
2288 * by adding 110 to the signal level */
2291 else if (rssi > -40)
2294 e->quality = (rssi + 110);
2302 e->quality_max = qmax;
2305 nl80211_get_scancrypto(flags, &e->crypto);
2311 *len = count * sizeof(struct iwinfo_scanlist_entry);
2316 unlink(local.sun_path);
2318 return (count >= 0) ? 0 : -1;
2321 static int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
2328 /* Got a radioX pseudo interface, find some interface on it or create one */
2329 if (!strncmp(ifname, "radio", 5))
2331 /* Reuse existing interface */
2332 if ((res = nl80211_phy2ifname(ifname)) != NULL)
2334 return nl80211_get_scanlist(res, buf, len);
2337 /* Need to spawn a temporary iface for scanning */
2338 else if ((res = nl80211_ifadd(ifname)) != NULL)
2340 rv = nl80211_get_scanlist(res, buf, len);
2346 /* WPA supplicant */
2347 if (!nl80211_get_scanlist_wpactl(ifname, buf, len))
2352 /* station / ad-hoc / monitor scan */
2353 else if (!nl80211_get_mode(ifname, &mode) &&
2354 (mode == IWINFO_OPMODE_ADHOC ||
2355 mode == IWINFO_OPMODE_MASTER ||
2356 mode == IWINFO_OPMODE_CLIENT ||
2357 mode == IWINFO_OPMODE_MONITOR) &&
2358 iwinfo_ifup(ifname))
2360 return nl80211_get_scanlist_nl(ifname, buf, len);
2366 /* Got a temp interface, don't create yet another one */
2367 if (!strncmp(ifname, "tmp.", 4))
2369 if (!iwinfo_ifup(ifname))
2372 rv = nl80211_get_scanlist_nl(ifname, buf, len);
2373 iwinfo_ifdown(ifname);
2377 /* Spawn a new scan interface */
2380 if (!(res = nl80211_ifadd(ifname)))
2385 /* if we can take the new interface up, the driver supports an
2386 * additional interface and there's no need to tear down the ap */
2387 if (iwinfo_ifup(res))
2389 rv = nl80211_get_scanlist_nl(res, buf, len);
2393 /* driver cannot create secondary interface, take down ap
2395 else if (iwinfo_ifdown(ifname) && iwinfo_ifup(res))
2397 rv = nl80211_get_scanlist_nl(res, buf, len);
2399 iwinfo_ifup(ifname);
2400 nl80211_hostapd_hup(ifname);
2411 static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg)
2413 int bands_remain, freqs_remain;
2415 struct nl80211_array_buf *arr = arg;
2416 struct iwinfo_freqlist_entry *e;
2418 struct nlattr **attr = nl80211_parse(msg);
2419 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
2420 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
2421 struct nlattr *band, *freq;
2426 if (attr[NL80211_ATTR_WIPHY_BANDS]) {
2427 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
2429 nla_parse(bands, NL80211_BAND_ATTR_MAX,
2430 nla_data(band), nla_len(band), NULL);
2432 if (bands[NL80211_BAND_ATTR_FREQS]) {
2433 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
2435 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
2436 nla_data(freq), nla_len(freq), NULL);
2438 if (!freqs[NL80211_FREQUENCY_ATTR_FREQ] ||
2439 freqs[NL80211_FREQUENCY_ATTR_DISABLED])
2442 e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
2443 e->channel = nl80211_freq2channel(e->mhz);
2446 freqs[NL80211_FREQUENCY_ATTR_NO_IR] &&
2447 !freqs[NL80211_FREQUENCY_ATTR_RADAR]
2450 if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS])
2451 e->flags |= IWINFO_FREQ_NO_HT40MINUS;
2452 if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS])
2453 e->flags |= IWINFO_FREQ_NO_HT40PLUS;
2454 if (freqs[NL80211_FREQUENCY_ATTR_NO_80MHZ])
2455 e->flags |= IWINFO_FREQ_NO_80MHZ;
2456 if (freqs[NL80211_FREQUENCY_ATTR_NO_160MHZ])
2457 e->flags |= IWINFO_FREQ_NO_160MHZ;
2458 if (freqs[NL80211_FREQUENCY_ATTR_NO_20MHZ])
2459 e->flags |= IWINFO_FREQ_NO_20MHZ;
2460 if (freqs[NL80211_FREQUENCY_ATTR_NO_10MHZ])
2461 e->flags |= IWINFO_FREQ_NO_10MHZ;
2473 static int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
2475 struct nl80211_msg_conveyor *cv;
2476 struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
2477 uint32_t features = nl80211_get_protocol_features(ifname);
2480 flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0;
2481 cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags);
2485 NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
2486 if (nl80211_send(cv, nl80211_get_freqlist_cb, &arr))
2489 *len = arr.count * sizeof(struct iwinfo_freqlist_entry);
2499 static int nl80211_get_country_cb(struct nl_msg *msg, void *arg)
2502 struct nlattr **attr = nl80211_parse(msg);
2504 if (attr[NL80211_ATTR_REG_ALPHA2])
2505 memcpy(buf, nla_data(attr[NL80211_ATTR_REG_ALPHA2]), 2);
2512 static int nl80211_get_country(const char *ifname, char *buf)
2514 if (nl80211_request(ifname, NL80211_CMD_GET_REG, 0,
2515 nl80211_get_country_cb, buf))
2521 static int nl80211_get_countrylist(const char *ifname, char *buf, int *len)
2524 struct iwinfo_country_entry *e = (struct iwinfo_country_entry *)buf;
2525 const struct iwinfo_iso3166_label *l;
2527 for (l = IWINFO_ISO3166_NAMES, count = 0; l->iso3166; l++, e++, count++)
2529 e->iso3166 = l->iso3166;
2530 e->ccode[0] = (l->iso3166 / 256);
2531 e->ccode[1] = (l->iso3166 % 256);
2535 *len = (count * sizeof(struct iwinfo_country_entry));
2540 struct nl80211_modes
2547 static int nl80211_get_modelist_cb(struct nl_msg *msg, void *arg)
2549 struct nl80211_modes *m = arg;
2550 int bands_remain, freqs_remain;
2552 uint32_t vht_caps = 0;
2553 struct nlattr **attr = nl80211_parse(msg);
2554 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
2555 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
2556 struct nlattr *band, *freq;
2558 if (attr[NL80211_ATTR_WIPHY_BANDS])
2560 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
2562 nla_parse(bands, NL80211_BAND_ATTR_MAX,
2563 nla_data(band), nla_len(band), NULL);
2565 if (bands[NL80211_BAND_ATTR_HT_CAPA])
2566 caps = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]);
2568 /* Treat any nonzero capability as 11n */
2571 m->hw |= IWINFO_80211_N;
2572 m->ht |= IWINFO_HTMODE_HT20;
2574 if (caps & (1 << 1))
2575 m->ht |= IWINFO_HTMODE_HT40;
2578 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS],
2581 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
2582 nla_data(freq), nla_len(freq), NULL);
2584 if (!freqs[NL80211_FREQUENCY_ATTR_FREQ])
2587 if (nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]) < 2485)
2589 m->hw |= IWINFO_80211_B;
2590 m->hw |= IWINFO_80211_G;
2592 else if (bands[NL80211_BAND_ATTR_VHT_CAPA])
2594 vht_caps = nla_get_u32(bands[NL80211_BAND_ATTR_VHT_CAPA]);
2596 /* Treat any nonzero capability as 11ac */
2599 m->hw |= IWINFO_80211_AC;
2600 m->ht |= IWINFO_HTMODE_VHT20 | IWINFO_HTMODE_VHT40 | IWINFO_HTMODE_VHT80;
2602 switch ((vht_caps >> 2) & 3)
2605 m->ht |= IWINFO_HTMODE_VHT80_80;
2609 m->ht |= IWINFO_HTMODE_VHT160;
2613 else if (!(m->hw & IWINFO_80211_AC))
2615 m->hw |= IWINFO_80211_A;
2626 static int nl80211_get_hwmodelist(const char *ifname, int *buf)
2628 struct nl80211_modes m = { 0 };
2630 if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
2631 nl80211_get_modelist_cb, &m))
2645 static int nl80211_get_htmodelist(const char *ifname, int *buf)
2647 struct nl80211_modes m = { 0 };
2649 if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
2650 nl80211_get_modelist_cb, &m))
2665 static int nl80211_get_ifcomb_cb(struct nl_msg *msg, void *arg)
2667 struct nlattr **attr = nl80211_parse(msg);
2668 struct nlattr *comb;
2670 int comb_rem, limit_rem, mode_rem;
2673 if (!attr[NL80211_ATTR_INTERFACE_COMBINATIONS])
2676 nla_for_each_nested(comb, attr[NL80211_ATTR_INTERFACE_COMBINATIONS], comb_rem)
2678 static struct nla_policy iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
2679 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
2680 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
2682 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB+1];
2683 static struct nla_policy iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
2684 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
2685 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
2687 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT+1];
2688 struct nlattr *limit;
2690 nla_parse_nested(tb_comb, NUM_NL80211_IFACE_COMB, comb, iface_combination_policy);
2692 if (!tb_comb[NL80211_IFACE_COMB_LIMITS])
2695 nla_for_each_nested(limit, tb_comb[NL80211_IFACE_COMB_LIMITS], limit_rem)
2697 struct nlattr *mode;
2699 nla_parse_nested(tb_limit, NUM_NL80211_IFACE_LIMIT, limit, iface_limit_policy);
2701 if (!tb_limit[NL80211_IFACE_LIMIT_TYPES] ||
2702 !tb_limit[NL80211_IFACE_LIMIT_MAX])
2705 if (nla_get_u32(tb_limit[NL80211_IFACE_LIMIT_MAX]) < 2)
2708 nla_for_each_nested(mode, tb_limit[NL80211_IFACE_LIMIT_TYPES], mode_rem) {
2709 if (nla_type(mode) == NL80211_IFTYPE_AP)
2718 static int nl80211_get_mbssid_support(const char *ifname, int *buf)
2720 if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
2721 nl80211_get_ifcomb_cb, buf))
2727 static int nl80211_get_hardware_id(const char *ifname, char *buf)
2732 /* Got a radioX pseudo interface, find some interface on it or create one */
2733 if (!strncmp(ifname, "radio", 5))
2735 /* Reuse existing interface */
2736 if ((res = nl80211_phy2ifname(ifname)) != NULL)
2738 rv = wext_ops.hardware_id(res, buf);
2741 /* Need to spawn a temporary iface for finding IDs */
2742 else if ((res = nl80211_ifadd(ifname)) != NULL)
2744 rv = wext_ops.hardware_id(res, buf);
2750 rv = wext_ops.hardware_id(ifname, buf);
2753 /* Failed to obtain hardware IDs, search board config */
2756 rv = iwinfo_hardware_id_from_mtd((struct iwinfo_hardware_id *)buf);
2762 static const struct iwinfo_hardware_entry *
2763 nl80211_get_hardware_entry(const char *ifname)
2765 struct iwinfo_hardware_id id;
2767 if (nl80211_get_hardware_id(ifname, (char *)&id))
2770 return iwinfo_hardware(&id);
2773 static int nl80211_get_hardware_name(const char *ifname, char *buf)
2775 const struct iwinfo_hardware_entry *hw;
2777 if (!(hw = nl80211_get_hardware_entry(ifname)))
2778 sprintf(buf, "Generic MAC80211");
2780 sprintf(buf, "%s %s", hw->vendor_name, hw->device_name);
2785 static int nl80211_get_txpower_offset(const char *ifname, int *buf)
2787 const struct iwinfo_hardware_entry *hw;
2789 if (!(hw = nl80211_get_hardware_entry(ifname)))
2792 *buf = hw->txpower_offset;
2796 static int nl80211_get_frequency_offset(const char *ifname, int *buf)
2798 const struct iwinfo_hardware_entry *hw;
2800 if (!(hw = nl80211_get_hardware_entry(ifname)))
2803 *buf = hw->frequency_offset;
2807 static int nl80211_lookup_phyname(const char *section, char *buf)
2811 if ((idx = nl80211_phy_idx_from_uci(section)) < 0)
2814 sprintf(buf, "phy%d", idx);
2818 const struct iwinfo_ops nl80211_ops = {
2820 .probe = nl80211_probe,
2821 .channel = nl80211_get_channel,
2822 .frequency = nl80211_get_frequency,
2823 .frequency_offset = nl80211_get_frequency_offset,
2824 .txpower = nl80211_get_txpower,
2825 .txpower_offset = nl80211_get_txpower_offset,
2826 .bitrate = nl80211_get_bitrate,
2827 .signal = nl80211_get_signal,
2828 .noise = nl80211_get_noise,
2829 .quality = nl80211_get_quality,
2830 .quality_max = nl80211_get_quality_max,
2831 .mbssid_support = nl80211_get_mbssid_support,
2832 .hwmodelist = nl80211_get_hwmodelist,
2833 .htmodelist = nl80211_get_htmodelist,
2834 .mode = nl80211_get_mode,
2835 .ssid = nl80211_get_ssid,
2836 .bssid = nl80211_get_bssid,
2837 .country = nl80211_get_country,
2838 .hardware_id = nl80211_get_hardware_id,
2839 .hardware_name = nl80211_get_hardware_name,
2840 .encryption = nl80211_get_encryption,
2841 .phyname = nl80211_get_phyname,
2842 .assoclist = nl80211_get_assoclist,
2843 .txpwrlist = nl80211_get_txpwrlist,
2844 .scanlist = nl80211_get_scanlist,
2845 .freqlist = nl80211_get_freqlist,
2846 .countrylist = nl80211_get_countrylist,
2847 .lookup_phy = nl80211_lookup_phyname,
2848 .close = nl80211_close