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);
230 static struct nl80211_msg_conveyor * nl80211_ctl(int cmd, int flags)
232 if (nl80211_init() < 0)
235 return nl80211_new(nls->nlctrl, cmd, flags);
238 static int nl80211_phy_idx_from_uci_path(struct uci_section *s)
245 opt = uci_lookup_option_string(uci_ctx, s, "path");
249 snprintf(buf, sizeof(buf), "/sys/devices/%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 struct nl80211_msg_conveyor * nl80211_send(
373 struct nl80211_msg_conveyor *cv,
374 int (*cb_func)(struct nl_msg *, void *), void *cb_arg
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 if (nl_send_auto_complete(nls->nl_sock, cv->msg) < 0)
387 nl_cb_err(cv->cb, NL_CB_CUSTOM, nl80211_msg_error, &err);
388 nl_cb_set(cv->cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_msg_finish, &err);
389 nl_cb_set(cv->cb, NL_CB_ACK, NL_CB_CUSTOM, nl80211_msg_ack, &err);
392 nl_recvmsgs(nls->nl_sock, cv->cb);
403 static struct nlattr ** nl80211_parse(struct nl_msg *msg)
405 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
406 static struct nlattr *attr[NL80211_ATTR_MAX + 1];
408 nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
409 genlmsg_attrlen(gnlh, 0), NULL);
415 static int nl80211_subscribe_cb(struct nl_msg *msg, void *arg)
417 struct nl80211_group_conveyor *cv = arg;
419 struct nlattr **attr = nl80211_parse(msg);
420 struct nlattr *mgrpinfo[CTRL_ATTR_MCAST_GRP_MAX + 1];
424 if (!attr[CTRL_ATTR_MCAST_GROUPS])
427 nla_for_each_nested(mgrp, attr[CTRL_ATTR_MCAST_GROUPS], mgrpidx)
429 nla_parse(mgrpinfo, CTRL_ATTR_MCAST_GRP_MAX,
430 nla_data(mgrp), nla_len(mgrp), NULL);
432 if (mgrpinfo[CTRL_ATTR_MCAST_GRP_ID] &&
433 mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME] &&
434 !strncmp(nla_data(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME]),
435 cv->name, nla_len(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME])))
437 cv->id = nla_get_u32(mgrpinfo[CTRL_ATTR_MCAST_GRP_ID]);
445 static int nl80211_subscribe(const char *family, const char *group)
447 struct nl80211_group_conveyor cv = { .name = group, .id = -ENOENT };
448 struct nl80211_msg_conveyor *req;
450 req = nl80211_ctl(CTRL_CMD_GETFAMILY, 0);
453 NLA_PUT_STRING(req->msg, CTRL_ATTR_FAMILY_NAME, family);
454 nl80211_send(req, nl80211_subscribe_cb, &cv);
460 return nl_socket_add_membership(nls->nl_sock, cv.id);
464 static int nl80211_wait_cb(struct nl_msg *msg, void *arg)
466 struct nl80211_event_conveyor *cv = arg;
467 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
469 if (gnlh->cmd == cv->wait)
470 cv->recv = gnlh->cmd;
475 static int nl80211_wait_seq_check(struct nl_msg *msg, void *arg)
480 static int nl80211_wait(const char *family, const char *group, int cmd)
482 struct nl80211_event_conveyor cv = { .wait = cmd };
485 if (nl80211_subscribe(family, group))
488 cb = nl_cb_alloc(NL_CB_DEFAULT);
493 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, nl80211_wait_seq_check, NULL);
494 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_wait_cb, &cv );
497 nl_recvmsgs(nls->nl_sock, cb);
505 static int nl80211_freq2channel(int freq)
509 else if (freq < 2484)
510 return (freq - 2407) / 5;
511 else if (freq >= 4910 && freq <= 4980)
512 return (freq - 4000) / 5;
514 return (freq - 5000) / 5;
517 static int nl80211_channel2freq(int channel, const char *band)
519 if (!band || band[0] != 'a')
523 else if (channel < 14)
524 return (channel * 5) + 2407;
528 if (channel >= 182 && channel <= 196)
529 return (channel * 5) + 4000;
531 return (channel * 5) + 5000;
537 static int nl80211_ifname2phy_cb(struct nl_msg *msg, void *arg)
540 struct nlattr **attr = nl80211_parse(msg);
542 if (attr[NL80211_ATTR_WIPHY_NAME])
543 memcpy(buf, nla_data(attr[NL80211_ATTR_WIPHY_NAME]),
544 nla_len(attr[NL80211_ATTR_WIPHY_NAME]));
551 static char * nl80211_ifname2phy(const char *ifname)
553 static char phy[32] = { 0 };
554 struct nl80211_msg_conveyor *req;
556 memset(phy, 0, sizeof(phy));
558 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
561 nl80211_send(req, nl80211_ifname2phy_cb, phy);
565 return phy[0] ? phy : NULL;
568 static char * nl80211_phy2ifname(const char *ifname)
570 int ifidx = -1, cifidx = -1, phyidx = -1;
572 static char nif[IFNAMSIZ] = { 0 };
577 /* Only accept phy name of the form phy%d or radio%d */
580 else if (!strncmp(ifname, "phy", 3))
581 phyidx = atoi(&ifname[3]);
582 else if (!strncmp(ifname, "radio", 5))
583 phyidx = nl80211_phy_idx_from_uci(ifname);
587 memset(nif, 0, sizeof(nif));
591 if ((d = opendir("/sys/class/net")) != NULL)
593 while ((e = readdir(d)) != NULL)
595 snprintf(buffer, sizeof(buffer),
596 "/sys/class/net/%s/phy80211/index", e->d_name);
598 if (nl80211_readint(buffer) == phyidx)
600 snprintf(buffer, sizeof(buffer),
601 "/sys/class/net/%s/ifindex", e->d_name);
603 if ((cifidx = nl80211_readint(buffer)) >= 0 &&
604 ((ifidx < 0) || (cifidx < ifidx)))
607 strncpy(nif, e->d_name, sizeof(nif));
616 return nif[0] ? nif : NULL;
619 static int nl80211_get_mode_cb(struct nl_msg *msg, void *arg)
622 struct nlattr **tb = nl80211_parse(msg);
623 const int ifmodes[NL80211_IFTYPE_MAX + 1] = {
624 IWINFO_OPMODE_UNKNOWN, /* unspecified */
625 IWINFO_OPMODE_ADHOC, /* IBSS */
626 IWINFO_OPMODE_CLIENT, /* managed */
627 IWINFO_OPMODE_MASTER, /* AP */
628 IWINFO_OPMODE_AP_VLAN, /* AP/VLAN */
629 IWINFO_OPMODE_WDS, /* WDS */
630 IWINFO_OPMODE_MONITOR, /* monitor */
631 IWINFO_OPMODE_MESHPOINT, /* mesh point */
632 IWINFO_OPMODE_P2P_CLIENT, /* P2P-client */
633 IWINFO_OPMODE_P2P_GO, /* P2P-GO */
636 if (tb[NL80211_ATTR_IFTYPE])
637 *mode = ifmodes[nla_get_u32(tb[NL80211_ATTR_IFTYPE])];
643 static int nl80211_get_mode(const char *ifname, int *buf)
646 struct nl80211_msg_conveyor *req;
648 res = nl80211_phy2ifname(ifname);
649 req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0);
650 *buf = IWINFO_OPMODE_UNKNOWN;
654 nl80211_send(req, nl80211_get_mode_cb, buf);
658 return (*buf == IWINFO_OPMODE_UNKNOWN) ? -1 : 0;
661 static int __nl80211_hostapd_query(const char *ifname, ...)
664 char *phy, *search, *dest, *key, *val, buf[128];
665 int len, mode, found = 0, match = 1;
668 if (nl80211_get_mode(ifname, &mode))
671 if (mode != IWINFO_OPMODE_MASTER && mode != IWINFO_OPMODE_AP_VLAN)
674 phy = nl80211_ifname2phy(ifname);
679 snprintf(buf, sizeof(buf), "/var/run/hostapd-%s.conf", phy);
680 fp = fopen(buf, "r");
685 va_start(ap, ifname);
687 /* clear all destination buffers */
690 while ((search = va_arg(ap_cur, char *)) != NULL)
692 dest = va_arg(ap_cur, char *);
693 len = va_arg(ap_cur, int);
695 memset(dest, 0, len);
700 /* iterate applicable lines and copy found values into dest buffers */
701 while (fgets(buf, sizeof(buf), fp))
703 key = strtok(buf, " =\t\n");
704 val = strtok(NULL, "\n");
706 if (!key || !val || !*key || *key == '#')
709 if (!strcmp(key, "interface") || !strcmp(key, "bss"))
710 match = !strcmp(ifname, val);
717 while ((search = va_arg(ap_cur, char *)) != NULL)
719 dest = va_arg(ap_cur, char *);
720 len = va_arg(ap_cur, int);
722 if (!strcmp(search, key))
724 strncpy(dest, val, len - 1);
740 #define nl80211_hostapd_query(ifname, ...) \
741 __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
744 static inline int nl80211_wpactl_recv(int sock, char *buf, int blen)
747 struct timeval tv = { 0, 256000 };
752 memset(buf, 0, blen);
754 if (select(sock + 1, &rfds, NULL, NULL, &tv) < 0)
757 if (!FD_ISSET(sock, &rfds))
760 return recv(sock, buf, blen - 1, 0);
763 static int nl80211_wpactl_connect(const char *ifname, struct sockaddr_un *local)
765 struct sockaddr_un remote = { 0 };
766 size_t remote_length, local_length;
768 int sock = socket(PF_UNIX, SOCK_DGRAM, 0);
772 remote.sun_family = AF_UNIX;
773 remote_length = sizeof(remote.sun_family) +
774 sprintf(remote.sun_path, "/var/run/wpa_supplicant-%s/%s",
777 if (fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC) < 0)
783 if (connect(sock, (struct sockaddr *)&remote, remote_length))
785 remote_length = sizeof(remote.sun_family) +
786 sprintf(remote.sun_path, "/var/run/wpa_supplicant/%s", ifname);
788 if (connect(sock, (struct sockaddr *)&remote, remote_length))
795 local->sun_family = AF_UNIX;
796 local_length = sizeof(local->sun_family) +
797 sprintf(local->sun_path, "/var/run/iwinfo-%s-%d", ifname, getpid());
799 if (bind(sock, (struct sockaddr *)local, local_length) < 0)
808 static int __nl80211_wpactl_query(const char *ifname, ...)
811 struct sockaddr_un local = { 0 };
812 int len, mode, found = 0, sock = -1;
813 char *search, *dest, *key, *val, *line, *pos, buf[512];
815 if (nl80211_get_mode(ifname, &mode))
818 if (mode != IWINFO_OPMODE_CLIENT && mode != IWINFO_OPMODE_ADHOC)
821 sock = nl80211_wpactl_connect(ifname, &local);
826 va_start(ap, ifname);
828 /* clear all destination buffers */
831 while ((search = va_arg(ap_cur, char *)) != NULL)
833 dest = va_arg(ap_cur, char *);
834 len = va_arg(ap_cur, int);
836 memset(dest, 0, len);
841 send(sock, "STATUS", 6, 0);
845 if (nl80211_wpactl_recv(sock, buf, sizeof(buf)) <= 0)
851 for (line = strtok_r(buf, "\n", &pos);
853 line = strtok_r(NULL, "\n", &pos))
855 key = strtok(line, "=");
856 val = strtok(NULL, "\n");
863 while ((search = va_arg(ap_cur, char *)) != NULL)
865 dest = va_arg(ap_cur, char *);
866 len = va_arg(ap_cur, int);
868 if (!strcmp(search, key))
870 strncpy(dest, val, len - 1);
885 unlink(local.sun_path);
890 #define nl80211_wpactl_query(ifname, ...) \
891 __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL)
894 static char * nl80211_ifadd(const char *ifname)
896 char *rv = NULL, path[PATH_MAX];
897 static char nif[IFNAMSIZ] = { 0 };
898 struct nl80211_msg_conveyor *req;
901 req = nl80211_msg(ifname, NL80211_CMD_NEW_INTERFACE, 0);
904 snprintf(nif, sizeof(nif), "tmp.%s", ifname);
906 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, nif);
907 NLA_PUT_U32(req->msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION);
909 nl80211_send(req, NULL, NULL);
911 snprintf(path, sizeof(path) - 1,
912 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif);
914 if ((sysfs = fopen(path, "w")) != NULL)
916 fwrite("0\n", 1, 2, sysfs);
929 static void nl80211_ifdel(const char *ifname)
931 struct nl80211_msg_conveyor *req;
933 req = nl80211_msg(ifname, NL80211_CMD_DEL_INTERFACE, 0);
936 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, ifname);
938 nl80211_send(req, NULL, NULL);
945 static void nl80211_hostapd_hup(const char *ifname)
949 char *phy = nl80211_ifname2phy(ifname);
953 snprintf(buf, sizeof(buf), "/var/run/wifi-%s.pid", phy);
954 if ((fd = open(buf, O_RDONLY)) > 0)
956 if (read(fd, buf, sizeof(buf)) > 0)
968 static int nl80211_probe(const char *ifname)
970 return !!nl80211_ifname2phy(ifname);
973 struct nl80211_ssid_bssid {
975 unsigned char bssid[7];
978 static int nl80211_get_ssid_bssid_cb(struct nl_msg *msg, void *arg)
982 struct nl80211_ssid_bssid *sb = arg;
983 struct nlattr **tb = nl80211_parse(msg);
984 struct nlattr *bss[NL80211_BSS_MAX + 1];
986 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
987 [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 },
988 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
991 if (!tb[NL80211_ATTR_BSS] ||
992 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
994 !bss[NL80211_BSS_BSSID] ||
995 !bss[NL80211_BSS_STATUS] ||
996 !bss[NL80211_BSS_INFORMATION_ELEMENTS])
1001 switch (nla_get_u32(bss[NL80211_BSS_STATUS]))
1003 case NL80211_BSS_STATUS_ASSOCIATED:
1004 case NL80211_BSS_STATUS_AUTHENTICATED:
1005 case NL80211_BSS_STATUS_IBSS_JOINED:
1009 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1010 ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1012 while (ielen >= 2 && ielen >= ie[1])
1016 memcpy(sb->ssid, ie + 2, min(ie[1], IWINFO_ESSID_MAX_SIZE));
1027 memcpy(sb->bssid + 1, nla_data(bss[NL80211_BSS_BSSID]), 6);
1036 static int nl80211_get_ssid(const char *ifname, char *buf)
1039 struct nl80211_msg_conveyor *req;
1040 struct nl80211_ssid_bssid sb;
1042 /* try to find ssid from scan dump results */
1043 res = nl80211_phy2ifname(ifname);
1044 req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP);
1046 sb.ssid = (unsigned char *)buf;
1051 nl80211_send(req, nl80211_get_ssid_bssid_cb, &sb);
1055 /* failed, try to find from hostapd info */
1057 nl80211_hostapd_query(ifname, "ssid", buf, IWINFO_ESSID_MAX_SIZE + 1);
1059 return (*buf == 0) ? -1 : 0;
1062 static int nl80211_get_bssid(const char *ifname, char *buf)
1064 char *res, bssid[sizeof("FF:FF:FF:FF:FF:FF\0")];
1065 struct nl80211_msg_conveyor *req;
1066 struct nl80211_ssid_bssid sb;
1068 /* try to find bssid from scan dump results */
1069 res = nl80211_phy2ifname(ifname);
1070 req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP);
1077 nl80211_send(req, nl80211_get_ssid_bssid_cb, &sb);
1081 /* failed, try to find mac from hostapd info */
1082 if ((sb.bssid[0] == 0) &&
1083 nl80211_hostapd_query(ifname, "bssid", bssid, sizeof(bssid)))
1086 sb.bssid[1] = strtol(&bssid[0], NULL, 16);
1087 sb.bssid[2] = strtol(&bssid[3], NULL, 16);
1088 sb.bssid[3] = strtol(&bssid[6], NULL, 16);
1089 sb.bssid[4] = strtol(&bssid[9], NULL, 16);
1090 sb.bssid[5] = strtol(&bssid[12], NULL, 16);
1091 sb.bssid[6] = strtol(&bssid[15], NULL, 16);
1096 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
1097 sb.bssid[1], sb.bssid[2], sb.bssid[3],
1098 sb.bssid[4], sb.bssid[5], sb.bssid[6]);
1107 static int nl80211_get_frequency_scan_cb(struct nl_msg *msg, void *arg)
1110 struct nlattr **attr = nl80211_parse(msg);
1111 struct nlattr *binfo[NL80211_BSS_MAX + 1];
1113 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1114 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1115 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1118 if (attr[NL80211_ATTR_BSS] &&
1119 !nla_parse_nested(binfo, NL80211_BSS_MAX,
1120 attr[NL80211_ATTR_BSS], bss_policy))
1122 if (binfo[NL80211_BSS_STATUS] && binfo[NL80211_BSS_FREQUENCY])
1123 *freq = nla_get_u32(binfo[NL80211_BSS_FREQUENCY]);
1129 static int nl80211_get_frequency_info_cb(struct nl_msg *msg, void *arg)
1132 struct nlattr **tb = nl80211_parse(msg);
1134 if (tb[NL80211_ATTR_WIPHY_FREQ])
1135 *freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1140 static int nl80211_get_frequency(const char *ifname, int *buf)
1142 char *res, channel[4], hwmode[2];
1143 struct nl80211_msg_conveyor *req;
1145 /* try to find frequency from interface info */
1146 res = nl80211_phy2ifname(ifname);
1147 req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0);
1152 nl80211_send(req, nl80211_get_frequency_info_cb, buf);
1156 /* failed, try to find frequency from hostapd info */
1158 nl80211_hostapd_query(ifname, "hw_mode", hwmode, sizeof(hwmode),
1159 "channel", channel, sizeof(channel)) == 2)
1161 *buf = nl80211_channel2freq(atoi(channel), hwmode);
1164 /* failed, try to find frequency from scan results */
1167 res = nl80211_phy2ifname(ifname);
1168 req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP);
1172 nl80211_send(req, nl80211_get_frequency_scan_cb, buf);
1177 return (*buf == 0) ? -1 : 0;
1180 static int nl80211_get_channel(const char *ifname, int *buf)
1182 if (!nl80211_get_frequency(ifname, buf))
1184 *buf = nl80211_freq2channel(*buf);
1191 static int nl80211_get_txpower_cb(struct nl_msg *msg, void *arg)
1194 struct nlattr **tb = nl80211_parse(msg);
1196 if (tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL])
1197 *buf = iwinfo_mbm2dbm(nla_get_u32(tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]));
1202 static int nl80211_get_txpower(const char *ifname, int *buf)
1205 struct nl80211_msg_conveyor *req;
1207 res = nl80211_phy2ifname(ifname);
1208 req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0);
1213 nl80211_send(req, nl80211_get_txpower_cb, buf);
1223 static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
1227 struct nl80211_rssi_rate *rr = arg;
1228 struct nlattr **attr = nl80211_parse(msg);
1229 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1230 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1232 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
1233 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
1234 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
1235 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
1236 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
1237 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
1238 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1239 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
1240 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
1241 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
1242 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
1245 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1246 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1247 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1248 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1249 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1252 if (attr[NL80211_ATTR_STA_INFO])
1254 if (!nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1255 attr[NL80211_ATTR_STA_INFO], stats_policy))
1257 if (sinfo[NL80211_STA_INFO_SIGNAL])
1259 dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1260 rr->rssi = rr->rssi ? (int8_t)((rr->rssi + dbm) / 2) : dbm;
1263 if (sinfo[NL80211_STA_INFO_TX_BITRATE])
1265 if (!nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1266 sinfo[NL80211_STA_INFO_TX_BITRATE],
1269 if (rinfo[NL80211_RATE_INFO_BITRATE])
1271 mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
1273 ? (int16_t)((rr->rate + mbit) / 2) : mbit;
1283 static void nl80211_fill_signal(const char *ifname, struct nl80211_rssi_rate *r)
1287 struct nl80211_msg_conveyor *req;
1292 if ((d = opendir("/sys/class/net")) != NULL)
1294 while ((de = readdir(d)) != NULL)
1296 if (!strncmp(de->d_name, ifname, strlen(ifname)) &&
1297 (!de->d_name[strlen(ifname)] ||
1298 !strncmp(&de->d_name[strlen(ifname)], ".sta", 4)))
1300 req = nl80211_msg(de->d_name, NL80211_CMD_GET_STATION,
1305 nl80211_send(req, nl80211_fill_signal_cb, r);
1315 static int nl80211_get_bitrate(const char *ifname, int *buf)
1317 struct nl80211_rssi_rate rr;
1319 nl80211_fill_signal(ifname, &rr);
1323 *buf = (rr.rate * 100);
1330 static int nl80211_get_signal(const char *ifname, int *buf)
1332 struct nl80211_rssi_rate rr;
1334 nl80211_fill_signal(ifname, &rr);
1345 static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg)
1347 int8_t *noise = arg;
1348 struct nlattr **tb = nl80211_parse(msg);
1349 struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
1351 static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
1352 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1353 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1356 if (!tb[NL80211_ATTR_SURVEY_INFO])
1359 if (nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
1360 tb[NL80211_ATTR_SURVEY_INFO], sp))
1363 if (!si[NL80211_SURVEY_INFO_NOISE])
1366 if (!*noise || si[NL80211_SURVEY_INFO_IN_USE])
1367 *noise = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
1373 static int nl80211_get_noise(const char *ifname, int *buf)
1376 struct nl80211_msg_conveyor *req;
1378 req = nl80211_msg(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP);
1383 nl80211_send(req, nl80211_get_noise_cb, &noise);
1396 static int nl80211_get_quality(const char *ifname, int *buf)
1400 if (!nl80211_get_signal(ifname, &signal))
1402 /* A positive signal level is usually just a quality
1403 * value, pass through as-is */
1409 /* The cfg80211 wext compat layer assumes a signal range
1410 * of -110 dBm to -40 dBm, the quality value is derived
1411 * by adding 110 to the signal level */
1416 else if (signal > -40)
1419 *buf = (signal + 110);
1428 static int nl80211_get_quality_max(const char *ifname, int *buf)
1430 /* The cfg80211 wext compat layer assumes a maximum
1437 static int nl80211_check_wepkey(const char *key)
1441 switch (strlen(key))
1445 return IWINFO_CIPHER_WEP40;
1449 return IWINFO_CIPHER_WEP104;
1456 static int nl80211_get_encryption(const char *ifname, char *buf)
1458 char wpa[2], wpa_key_mgmt[16], wpa_pairwise[16], wpa_groupwise[16];
1459 char auth_algs[2], wep_key0[27], wep_key1[27], wep_key2[27], wep_key3[27];
1461 struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
1463 /* WPA supplicant */
1464 if (nl80211_wpactl_query(ifname,
1465 "pairwise_cipher", wpa_pairwise, sizeof(wpa_pairwise),
1466 "group_cipher", wpa_groupwise, sizeof(wpa_groupwise),
1467 "key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt)))
1470 if (!strcmp(wpa_key_mgmt, "NONE"))
1472 if (strstr(wpa_pairwise, "WEP-40"))
1473 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
1474 else if (strstr(wpa_pairwise, "WEP-104"))
1475 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
1477 if (strstr(wpa_groupwise, "WEP-40"))
1478 c->group_ciphers |= IWINFO_CIPHER_WEP40;
1479 else if (strstr(wpa_groupwise, "WEP-104"))
1480 c->group_ciphers |= IWINFO_CIPHER_WEP104;
1482 c->enabled = !!(c->pair_ciphers | c->group_ciphers);
1483 c->auth_suites |= IWINFO_KMGMT_NONE;
1484 c->auth_algs |= IWINFO_AUTH_OPEN; /* XXX: assumption */
1488 else if (strstr(wpa_key_mgmt, "WPA"))
1490 if (strstr(wpa_pairwise, "TKIP"))
1491 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
1492 else if (strstr(wpa_pairwise, "CCMP"))
1493 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
1494 else if (strstr(wpa_pairwise, "NONE"))
1495 c->pair_ciphers |= IWINFO_CIPHER_NONE;
1496 else if (strstr(wpa_pairwise, "WEP-40"))
1497 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
1498 else if (strstr(wpa_pairwise, "WEP-104"))
1499 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
1501 if (strstr(wpa_groupwise, "TKIP"))
1502 c->group_ciphers |= IWINFO_CIPHER_TKIP;
1503 else if (strstr(wpa_groupwise, "CCMP"))
1504 c->group_ciphers |= IWINFO_CIPHER_CCMP;
1505 else if (strstr(wpa_groupwise, "NONE"))
1506 c->group_ciphers |= IWINFO_CIPHER_NONE;
1507 else if (strstr(wpa_groupwise, "WEP-40"))
1508 c->group_ciphers |= IWINFO_CIPHER_WEP40;
1509 else if (strstr(wpa_groupwise, "WEP-104"))
1510 c->group_ciphers |= IWINFO_CIPHER_WEP104;
1512 if (strstr(wpa_key_mgmt, "WPA2"))
1514 else if (strstr(wpa_key_mgmt, "WPA"))
1517 if (strstr(wpa_key_mgmt, "PSK"))
1518 c->auth_suites |= IWINFO_KMGMT_PSK;
1519 else if (strstr(wpa_key_mgmt, "EAP") ||
1520 strstr(wpa_key_mgmt, "802.1X"))
1521 c->auth_suites |= IWINFO_KMGMT_8021x;
1522 else if (strstr(wpa_key_mgmt, "NONE"))
1523 c->auth_suites |= IWINFO_KMGMT_NONE;
1525 c->enabled = !!(c->wpa_version && c->auth_suites);
1532 else if (nl80211_hostapd_query(ifname,
1533 "wpa", wpa, sizeof(wpa),
1534 "wpa_key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt),
1535 "wpa_pairwise", wpa_pairwise, sizeof(wpa_pairwise),
1536 "auth_algs", auth_algs, sizeof(auth_algs),
1537 "wep_key0", wep_key0, sizeof(wep_key0),
1538 "wep_key1", wep_key1, sizeof(wep_key1),
1539 "wep_key2", wep_key2, sizeof(wep_key2),
1540 "wep_key3", wep_key3, sizeof(wep_key3)))
1542 c->wpa_version = wpa[0] ? atoi(wpa) : 0;
1544 if (wpa_key_mgmt[0])
1546 if (strstr(wpa_key_mgmt, "PSK"))
1547 c->auth_suites |= IWINFO_KMGMT_PSK;
1549 if (strstr(wpa_key_mgmt, "EAP"))
1550 c->auth_suites |= IWINFO_KMGMT_8021x;
1552 if (strstr(wpa_key_mgmt, "NONE"))
1553 c->auth_suites |= IWINFO_KMGMT_NONE;
1557 c->auth_suites |= IWINFO_KMGMT_PSK;
1560 if (wpa_pairwise[0])
1562 if (strstr(wpa_pairwise, "TKIP"))
1563 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
1565 if (strstr(wpa_pairwise, "CCMP"))
1566 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
1568 if (strstr(wpa_pairwise, "NONE"))
1569 c->pair_ciphers |= IWINFO_CIPHER_NONE;
1574 switch(atoi(auth_algs))
1577 c->auth_algs |= IWINFO_AUTH_OPEN;
1581 c->auth_algs |= IWINFO_AUTH_SHARED;
1585 c->auth_algs |= IWINFO_AUTH_OPEN;
1586 c->auth_algs |= IWINFO_AUTH_SHARED;
1590 c->pair_ciphers |= nl80211_check_wepkey(wep_key0);
1591 c->pair_ciphers |= nl80211_check_wepkey(wep_key1);
1592 c->pair_ciphers |= nl80211_check_wepkey(wep_key2);
1593 c->pair_ciphers |= nl80211_check_wepkey(wep_key3);
1596 c->group_ciphers = c->pair_ciphers;
1597 c->enabled = (c->wpa_version || c->pair_ciphers) ? 1 : 0;
1605 static int nl80211_get_phyname(const char *ifname, char *buf)
1609 name = nl80211_ifname2phy(ifname);
1616 else if ((name = nl80211_phy2ifname(ifname)) != NULL)
1618 name = nl80211_ifname2phy(name);
1622 strcpy(buf, ifname);
1631 static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
1633 struct nl80211_array_buf *arr = arg;
1634 struct iwinfo_assoclist_entry *e = arr->buf;
1635 struct nlattr **attr = nl80211_parse(msg);
1636 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1637 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1638 struct nl80211_sta_flag_update *sta_flags;
1640 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
1641 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
1642 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
1643 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
1644 [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED },
1645 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
1646 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1647 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
1648 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
1649 [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
1650 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
1651 [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 },
1652 [NL80211_STA_INFO_STA_FLAGS] =
1653 { .minlen = sizeof(struct nl80211_sta_flag_update) },
1656 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1657 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1658 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1659 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1660 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1663 /* advance to end of array */
1665 memset(e, 0, sizeof(*e));
1667 if (attr[NL80211_ATTR_MAC])
1668 memcpy(e->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
1670 if (attr[NL80211_ATTR_STA_INFO] &&
1671 !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1672 attr[NL80211_ATTR_STA_INFO], stats_policy))
1674 if (sinfo[NL80211_STA_INFO_SIGNAL])
1675 e->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1677 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
1678 e->inactive = nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]);
1680 if (sinfo[NL80211_STA_INFO_RX_PACKETS])
1681 e->rx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]);
1683 if (sinfo[NL80211_STA_INFO_TX_PACKETS])
1684 e->tx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]);
1686 if (sinfo[NL80211_STA_INFO_RX_BITRATE] &&
1687 !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1688 sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy))
1690 if (rinfo[NL80211_RATE_INFO_BITRATE])
1692 nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]) * 100;
1694 if (rinfo[NL80211_RATE_INFO_MCS])
1695 e->rx_rate.mcs = nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]);
1697 if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
1698 e->rx_rate.is_40mhz = 1;
1700 if (rinfo[NL80211_RATE_INFO_SHORT_GI])
1701 e->rx_rate.is_short_gi = 1;
1704 if (sinfo[NL80211_STA_INFO_TX_BITRATE] &&
1705 !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1706 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy))
1708 if (rinfo[NL80211_RATE_INFO_BITRATE])
1710 nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]) * 100;
1712 if (rinfo[NL80211_RATE_INFO_MCS])
1713 e->tx_rate.mcs = nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]);
1715 if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
1716 e->tx_rate.is_40mhz = 1;
1718 if (rinfo[NL80211_RATE_INFO_SHORT_GI])
1719 e->tx_rate.is_short_gi = 1;
1722 if (sinfo[NL80211_STA_INFO_RX_BYTES])
1723 e->rx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]);
1725 if (sinfo[NL80211_STA_INFO_TX_BYTES])
1726 e->tx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]);
1728 if (sinfo[NL80211_STA_INFO_TX_RETRIES])
1729 e->tx_retries = nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]);
1731 if (sinfo[NL80211_STA_INFO_TX_FAILED])
1732 e->tx_failed = nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]);
1734 if (sinfo[NL80211_STA_INFO_T_OFFSET])
1735 e->t_offset = nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]);
1738 if (sinfo[NL80211_STA_INFO_STA_FLAGS])
1740 sta_flags = (struct nl80211_sta_flag_update *)
1741 nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]);
1743 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
1744 sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED))
1745 e->is_authorized = 1;
1747 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1748 sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
1749 e->is_authenticated = 1;
1751 if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) &&
1752 sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
1753 e->is_preamble_short = 1;
1755 if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME) &&
1756 sta_flags->set & BIT(NL80211_STA_FLAG_WME))
1759 if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP) &&
1760 sta_flags->set & BIT(NL80211_STA_FLAG_MFP))
1763 if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER) &&
1764 sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER))
1769 e->noise = 0; /* filled in by caller */
1775 static int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
1780 struct nl80211_msg_conveyor *req;
1781 struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
1782 struct iwinfo_assoclist_entry *e;
1784 if ((d = opendir("/sys/class/net")) != NULL)
1786 while ((de = readdir(d)) != NULL)
1788 if (!strncmp(de->d_name, ifname, strlen(ifname)) &&
1789 (!de->d_name[strlen(ifname)] ||
1790 !strncmp(&de->d_name[strlen(ifname)], ".sta", 4)))
1792 req = nl80211_msg(de->d_name, NL80211_CMD_GET_STATION,
1797 nl80211_send(req, nl80211_get_assoclist_cb, &arr);
1805 if (!nl80211_get_noise(ifname, &noise))
1806 for (i = 0, e = arr.buf; i < arr.count; i++, e++)
1809 *len = (arr.count * sizeof(struct iwinfo_assoclist_entry));
1816 static int nl80211_get_txpwrlist_cb(struct nl_msg *msg, void *arg)
1819 int ch_cur, ch_cmp, bands_remain, freqs_remain;
1821 struct nlattr **attr = nl80211_parse(msg);
1822 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
1823 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
1824 struct nlattr *band, *freq;
1826 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1827 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1828 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1829 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1830 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1831 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1832 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
1835 ch_cur = *dbm_max; /* value int* is initialized with channel by caller */
1838 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
1840 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
1841 nla_len(band), NULL);
1843 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
1845 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
1846 nla_data(freq), nla_len(freq), freq_policy);
1848 ch_cmp = nl80211_freq2channel(nla_get_u32(
1849 freqs[NL80211_FREQUENCY_ATTR_FREQ]));
1851 if ((!ch_cur || (ch_cmp == ch_cur)) &&
1852 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER])
1854 *dbm_max = (int)(0.01 * nla_get_u32(
1855 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
1865 static int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
1868 int dbm_max = -1, dbm_cur, dbm_cnt;
1869 struct nl80211_msg_conveyor *req;
1870 struct iwinfo_txpwrlist_entry entry;
1872 if (nl80211_get_channel(ifname, &ch_cur))
1875 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
1878 /* initialize the value pointer with channel for callback */
1881 nl80211_send(req, nl80211_get_txpwrlist_cb, &dbm_max);
1887 for (dbm_cur = 0, dbm_cnt = 0;
1889 dbm_cur++, dbm_cnt++)
1891 entry.dbm = dbm_cur;
1892 entry.mw = iwinfo_dbm2mw(dbm_cur);
1894 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
1897 entry.dbm = dbm_max;
1898 entry.mw = iwinfo_dbm2mw(dbm_max);
1900 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
1903 *len = dbm_cnt * sizeof(entry);
1910 static void nl80211_get_scancrypto(const char *spec,
1911 struct iwinfo_crypto_entry *c)
1913 if (strstr(spec, "WPA") || strstr(spec, "WEP"))
1917 if (strstr(spec, "WPA2-") && strstr(spec, "WPA-"))
1920 else if (strstr(spec, "WPA2"))
1923 else if (strstr(spec, "WPA"))
1926 else if (strstr(spec, "WEP"))
1927 c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
1930 if (strstr(spec, "PSK"))
1931 c->auth_suites |= IWINFO_KMGMT_PSK;
1933 if (strstr(spec, "802.1X") || strstr(spec, "EAP"))
1934 c->auth_suites |= IWINFO_KMGMT_8021x;
1936 if (strstr(spec, "WPA-NONE"))
1937 c->auth_suites |= IWINFO_KMGMT_NONE;
1940 if (strstr(spec, "TKIP"))
1941 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
1943 if (strstr(spec, "CCMP"))
1944 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
1946 if (strstr(spec, "WEP-40"))
1947 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
1949 if (strstr(spec, "WEP-104"))
1950 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
1952 c->group_ciphers = c->pair_ciphers;
1961 struct nl80211_scanlist {
1962 struct iwinfo_scanlist_entry *e;
1967 static void nl80211_get_scanlist_ie(struct nlattr **bss,
1968 struct iwinfo_scanlist_entry *e)
1970 int ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1971 unsigned char *ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1972 static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 };
1975 while (ielen >= 2 && ielen >= ie[1])
1980 len = min(ie[1], IWINFO_ESSID_MAX_SIZE);
1981 memcpy(e->ssid, ie + 2, len);
1986 iwinfo_parse_rsn(&e->crypto, ie + 2, ie[1],
1987 IWINFO_CIPHER_CCMP, IWINFO_KMGMT_8021x);
1990 case 221: /* Vendor */
1991 if (ie[1] >= 4 && !memcmp(ie + 2, ms_oui, 3) && ie[5] == 1)
1992 iwinfo_parse_rsn(&e->crypto, ie + 6, ie[1] - 4,
1993 IWINFO_CIPHER_TKIP, IWINFO_KMGMT_PSK);
2002 static int nl80211_get_scanlist_cb(struct nl_msg *msg, void *arg)
2007 struct nl80211_scanlist *sl = arg;
2008 struct nlattr **tb = nl80211_parse(msg);
2009 struct nlattr *bss[NL80211_BSS_MAX + 1];
2011 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
2012 [NL80211_BSS_TSF] = { .type = NLA_U64 },
2013 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
2014 [NL80211_BSS_BSSID] = { 0 },
2015 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
2016 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
2017 [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 },
2018 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
2019 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
2020 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
2021 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
2022 [NL80211_BSS_BEACON_IES] = { 0 },
2025 if (!tb[NL80211_ATTR_BSS] ||
2026 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
2028 !bss[NL80211_BSS_BSSID])
2033 if (bss[NL80211_BSS_CAPABILITY])
2034 caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
2038 memset(sl->e, 0, sizeof(*sl->e));
2039 memcpy(sl->e->mac, nla_data(bss[NL80211_BSS_BSSID]), 6);
2042 sl->e->mode = IWINFO_OPMODE_ADHOC;
2043 else if (caps & (1<<0))
2044 sl->e->mode = IWINFO_OPMODE_MASTER;
2046 sl->e->mode = IWINFO_OPMODE_MESHPOINT;
2049 sl->e->crypto.enabled = 1;
2051 if (bss[NL80211_BSS_FREQUENCY])
2052 sl->e->channel = nl80211_freq2channel(nla_get_u32(
2053 bss[NL80211_BSS_FREQUENCY]));
2055 if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
2056 nl80211_get_scanlist_ie(bss, sl->e);
2058 if (bss[NL80211_BSS_SIGNAL_MBM])
2061 (uint8_t)((int32_t)nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]) / 100);
2063 rssi = sl->e->signal - 0x100;
2067 else if (rssi > -40)
2070 sl->e->quality = (rssi + 110);
2071 sl->e->quality_max = 70;
2074 if (sl->e->crypto.enabled && !sl->e->crypto.wpa_version)
2076 sl->e->crypto.auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
2077 sl->e->crypto.pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
2086 static int nl80211_get_scanlist_nl(const char *ifname, char *buf, int *len)
2088 struct nl80211_msg_conveyor *req;
2089 struct nl80211_scanlist sl = { .e = (struct iwinfo_scanlist_entry *)buf };
2091 req = nl80211_msg(ifname, NL80211_CMD_TRIGGER_SCAN, 0);
2094 nl80211_send(req, NULL, NULL);
2098 nl80211_wait("nl80211", "scan", NL80211_CMD_NEW_SCAN_RESULTS);
2100 req = nl80211_msg(ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP);
2103 nl80211_send(req, nl80211_get_scanlist_cb, &sl);
2107 *len = sl.len * sizeof(struct iwinfo_scanlist_entry);
2108 return *len ? 0 : -1;
2111 static int wpasupp_ssid_decode(const char *in, char *out, int outlen)
2114 (((x) >= 'a') ? ((x) - 'a' + 10) : \
2115 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
2121 if (len + 1 >= outlen)
2131 out[len++] = '\n'; in++;
2135 out[len++] = '\r'; in++;
2139 out[len++] = '\t'; in++;
2143 out[len++] = '\033'; in++;
2147 if (isxdigit(*(in+1)) && isxdigit(*(in+2)))
2148 out[len++] = hex(*(in+1)) * 16 + hex(*(in+2));
2170 static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len)
2172 int sock, qmax, rssi, tries, count = -1, ready = 0;
2173 char *pos, *line, *bssid, *freq, *signal, *flags, *ssid, reply[4096];
2174 struct sockaddr_un local = { 0 };
2175 struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
2177 sock = nl80211_wpactl_connect(ifname, &local);
2182 send(sock, "ATTACH", 6, 0);
2183 send(sock, "SCAN", 4, 0);
2186 * wait for scan results:
2187 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
2188 * 72 channels at most. We'll also receive two "OK" messages acknowledging
2189 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
2190 * time to process the results, so try 72 + 2 + 1 times.
2192 for (tries = 0; tries < 75; tries++)
2194 if (nl80211_wpactl_recv(sock, reply, sizeof(reply)) <= 0)
2197 /* got an event notification */
2198 if (reply[0] == '<')
2200 /* scan results are ready */
2201 if (strstr(reply, "CTRL-EVENT-SCAN-RESULTS"))
2203 /* send "SCAN_RESULTS" command */
2204 ready = (send(sock, "SCAN_RESULTS", 12, 0) == 12);
2208 /* is another unrelated event, retry */
2213 /* receive and parse scan results if the wait above didn't time out */
2214 if (ready && nl80211_wpactl_recv(sock, reply, sizeof(reply)) > 0)
2216 nl80211_get_quality_max(ifname, &qmax);
2218 for (line = strtok_r(reply, "\n", &pos);
2220 line = strtok_r(NULL, "\n", &pos))
2222 /* skip header line */
2229 bssid = strtok(line, "\t");
2230 freq = strtok(NULL, "\t");
2231 signal = strtok(NULL, "\t");
2232 flags = strtok(NULL, "\t");
2233 ssid = strtok(NULL, "\n");
2235 if (!bssid || !freq || !signal || !flags || !ssid)
2239 e->mac[0] = strtol(&bssid[0], NULL, 16);
2240 e->mac[1] = strtol(&bssid[3], NULL, 16);
2241 e->mac[2] = strtol(&bssid[6], NULL, 16);
2242 e->mac[3] = strtol(&bssid[9], NULL, 16);
2243 e->mac[4] = strtol(&bssid[12], NULL, 16);
2244 e->mac[5] = strtol(&bssid[15], NULL, 16);
2247 wpasupp_ssid_decode(ssid, e->ssid, sizeof(e->ssid));
2250 if (strstr(flags, "[MESH]"))
2251 e->mode = IWINFO_OPMODE_MESHPOINT;
2252 else if (strstr(flags, "[IBSS]"))
2253 e->mode = IWINFO_OPMODE_ADHOC;
2255 e->mode = IWINFO_OPMODE_MASTER;
2258 e->channel = nl80211_freq2channel(atoi(freq));
2261 rssi = atoi(signal);
2267 /* The cfg80211 wext compat layer assumes a signal range
2268 * of -110 dBm to -40 dBm, the quality value is derived
2269 * by adding 110 to the signal level */
2272 else if (rssi > -40)
2275 e->quality = (rssi + 110);
2283 e->quality_max = qmax;
2286 nl80211_get_scancrypto(flags, &e->crypto);
2292 *len = count * sizeof(struct iwinfo_scanlist_entry);
2296 unlink(local.sun_path);
2298 return (count >= 0) ? 0 : -1;
2301 static int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
2308 /* Got a radioX pseudo interface, find some interface on it or create one */
2309 if (!strncmp(ifname, "radio", 5))
2311 /* Reuse existing interface */
2312 if ((res = nl80211_phy2ifname(ifname)) != NULL)
2314 return nl80211_get_scanlist(res, buf, len);
2317 /* Need to spawn a temporary iface for scanning */
2318 else if ((res = nl80211_ifadd(ifname)) != NULL)
2320 rv = nl80211_get_scanlist(res, buf, len);
2326 /* WPA supplicant */
2327 if (!nl80211_get_scanlist_wpactl(ifname, buf, len))
2332 /* station / ad-hoc / monitor scan */
2333 else if (!nl80211_get_mode(ifname, &mode) &&
2334 (mode == IWINFO_OPMODE_ADHOC ||
2335 mode == IWINFO_OPMODE_MASTER ||
2336 mode == IWINFO_OPMODE_CLIENT ||
2337 mode == IWINFO_OPMODE_MONITOR) &&
2338 iwinfo_ifup(ifname))
2340 return nl80211_get_scanlist_nl(ifname, buf, len);
2346 /* Got a temp interface, don't create yet another one */
2347 if (!strncmp(ifname, "tmp.", 4))
2349 if (!iwinfo_ifup(ifname))
2352 rv = nl80211_get_scanlist_nl(ifname, buf, len);
2353 iwinfo_ifdown(ifname);
2357 /* Spawn a new scan interface */
2360 if (!(res = nl80211_ifadd(ifname)))
2365 /* if we can take the new interface up, the driver supports an
2366 * additional interface and there's no need to tear down the ap */
2367 if (iwinfo_ifup(res))
2369 rv = nl80211_get_scanlist_nl(res, buf, len);
2373 /* driver cannot create secondary interface, take down ap
2375 else if (iwinfo_ifdown(ifname) && iwinfo_ifup(res))
2377 rv = nl80211_get_scanlist_nl(res, buf, len);
2379 iwinfo_ifup(ifname);
2380 nl80211_hostapd_hup(ifname);
2391 static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg)
2393 int bands_remain, freqs_remain;
2395 struct nl80211_array_buf *arr = arg;
2396 struct iwinfo_freqlist_entry *e = arr->buf;
2398 struct nlattr **attr = nl80211_parse(msg);
2399 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
2400 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
2401 struct nlattr *band, *freq;
2403 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
2405 nla_parse(bands, NL80211_BAND_ATTR_MAX,
2406 nla_data(band), nla_len(band), NULL);
2408 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
2410 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
2411 nla_data(freq), nla_len(freq), NULL);
2413 if (!freqs[NL80211_FREQUENCY_ATTR_FREQ] ||
2414 freqs[NL80211_FREQUENCY_ATTR_DISABLED])
2417 e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
2418 e->channel = nl80211_freq2channel(e->mhz);
2421 freqs[NL80211_FREQUENCY_ATTR_NO_IR] &&
2422 !freqs[NL80211_FREQUENCY_ATTR_RADAR]
2433 static int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
2435 struct nl80211_msg_conveyor *req;
2436 struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
2438 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
2441 nl80211_send(req, nl80211_get_freqlist_cb, &arr);
2447 *len = arr.count * sizeof(struct iwinfo_freqlist_entry);
2454 static int nl80211_get_country_cb(struct nl_msg *msg, void *arg)
2457 struct nlattr **attr = nl80211_parse(msg);
2459 if (attr[NL80211_ATTR_REG_ALPHA2])
2460 memcpy(buf, nla_data(attr[NL80211_ATTR_REG_ALPHA2]), 2);
2467 static int nl80211_get_country(const char *ifname, char *buf)
2470 struct nl80211_msg_conveyor *req;
2472 req = nl80211_msg(ifname, NL80211_CMD_GET_REG, 0);
2475 nl80211_send(req, nl80211_get_country_cb, buf);
2485 static int nl80211_get_countrylist(const char *ifname, char *buf, int *len)
2488 struct iwinfo_country_entry *e = (struct iwinfo_country_entry *)buf;
2489 const struct iwinfo_iso3166_label *l;
2491 for (l = IWINFO_ISO3166_NAMES, count = 0; l->iso3166; l++, e++, count++)
2493 e->iso3166 = l->iso3166;
2494 e->ccode[0] = (l->iso3166 / 256);
2495 e->ccode[1] = (l->iso3166 % 256);
2499 *len = (count * sizeof(struct iwinfo_country_entry));
2504 struct nl80211_modes
2511 static int nl80211_get_modelist_cb(struct nl_msg *msg, void *arg)
2513 struct nl80211_modes *m = arg;
2514 int bands_remain, freqs_remain;
2516 uint32_t vht_caps = 0;
2517 struct nlattr **attr = nl80211_parse(msg);
2518 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
2519 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
2520 struct nlattr *band, *freq;
2522 if (attr[NL80211_ATTR_WIPHY_BANDS])
2524 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
2526 nla_parse(bands, NL80211_BAND_ATTR_MAX,
2527 nla_data(band), nla_len(band), NULL);
2529 if (bands[NL80211_BAND_ATTR_HT_CAPA])
2530 caps = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]);
2532 /* Treat any nonzero capability as 11n */
2535 m->hw |= IWINFO_80211_N;
2536 m->ht |= IWINFO_HTMODE_HT20;
2538 if (caps & (1 << 1))
2539 m->ht |= IWINFO_HTMODE_HT40;
2542 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS],
2545 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
2546 nla_data(freq), nla_len(freq), NULL);
2548 if (!freqs[NL80211_FREQUENCY_ATTR_FREQ])
2551 if (nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]) < 2485)
2553 m->hw |= IWINFO_80211_B;
2554 m->hw |= IWINFO_80211_G;
2556 else if (bands[NL80211_BAND_ATTR_VHT_CAPA])
2558 vht_caps = nla_get_u32(bands[NL80211_BAND_ATTR_VHT_CAPA]);
2560 /* Treat any nonzero capability as 11ac */
2563 m->hw |= IWINFO_80211_AC;
2564 m->ht |= IWINFO_HTMODE_VHT20 | IWINFO_HTMODE_VHT40 | IWINFO_HTMODE_VHT80;
2566 switch ((vht_caps >> 2) & 3)
2569 m->ht |= IWINFO_HTMODE_VHT80_80;
2573 m->ht |= IWINFO_HTMODE_VHT160;
2577 else if (!(m->hw & IWINFO_80211_AC))
2579 m->hw |= IWINFO_80211_A;
2590 static int nl80211_get_hwmodelist(const char *ifname, int *buf)
2592 struct nl80211_msg_conveyor *req;
2593 struct nl80211_modes m = { 0 };
2595 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
2598 nl80211_send(req, nl80211_get_modelist_cb, &m);
2611 static int nl80211_get_htmodelist(const char *ifname, int *buf)
2613 struct nl80211_msg_conveyor *req;
2614 struct nl80211_modes m = { 0 };
2616 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
2619 nl80211_send(req, nl80211_get_modelist_cb, &m);
2633 static int nl80211_get_ifcomb_cb(struct nl_msg *msg, void *arg)
2635 struct nlattr **attr = nl80211_parse(msg);
2636 struct nlattr *comb;
2638 int comb_rem, limit_rem, mode_rem;
2641 if (!attr[NL80211_ATTR_INTERFACE_COMBINATIONS])
2644 nla_for_each_nested(comb, attr[NL80211_ATTR_INTERFACE_COMBINATIONS], comb_rem)
2646 static struct nla_policy iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
2647 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
2648 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
2650 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB+1];
2651 static struct nla_policy iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
2652 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
2653 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
2655 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT+1];
2656 struct nlattr *limit;
2658 nla_parse_nested(tb_comb, NUM_NL80211_IFACE_COMB, comb, iface_combination_policy);
2660 if (!tb_comb[NL80211_IFACE_COMB_LIMITS])
2663 nla_for_each_nested(limit, tb_comb[NL80211_IFACE_COMB_LIMITS], limit_rem)
2665 struct nlattr *mode;
2667 nla_parse_nested(tb_limit, NUM_NL80211_IFACE_LIMIT, limit, iface_limit_policy);
2669 if (!tb_limit[NL80211_IFACE_LIMIT_TYPES] ||
2670 !tb_limit[NL80211_IFACE_LIMIT_MAX])
2673 if (nla_get_u32(tb_limit[NL80211_IFACE_LIMIT_MAX]) < 2)
2676 nla_for_each_nested(mode, tb_limit[NL80211_IFACE_LIMIT_TYPES], mode_rem) {
2677 if (nla_type(mode) == NL80211_IFTYPE_AP)
2686 static int nl80211_get_mbssid_support(const char *ifname, int *buf)
2688 struct nl80211_msg_conveyor *req;
2690 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
2694 nl80211_send(req, nl80211_get_ifcomb_cb, buf);
2699 static int nl80211_get_hardware_id(const char *ifname, char *buf)
2704 /* Got a radioX pseudo interface, find some interface on it or create one */
2705 if (!strncmp(ifname, "radio", 5))
2707 /* Reuse existing interface */
2708 if ((res = nl80211_phy2ifname(ifname)) != NULL)
2710 rv = wext_ops.hardware_id(res, buf);
2713 /* Need to spawn a temporary iface for finding IDs */
2714 else if ((res = nl80211_ifadd(ifname)) != NULL)
2716 rv = wext_ops.hardware_id(res, buf);
2722 rv = wext_ops.hardware_id(ifname, buf);
2725 /* Failed to obtain hardware IDs, search board config */
2728 rv = iwinfo_hardware_id_from_mtd((struct iwinfo_hardware_id *)buf);
2734 static const struct iwinfo_hardware_entry *
2735 nl80211_get_hardware_entry(const char *ifname)
2737 struct iwinfo_hardware_id id;
2739 if (nl80211_get_hardware_id(ifname, (char *)&id))
2742 return iwinfo_hardware(&id);
2745 static int nl80211_get_hardware_name(const char *ifname, char *buf)
2747 const struct iwinfo_hardware_entry *hw;
2749 if (!(hw = nl80211_get_hardware_entry(ifname)))
2750 sprintf(buf, "Generic MAC80211");
2752 sprintf(buf, "%s %s", hw->vendor_name, hw->device_name);
2757 static int nl80211_get_txpower_offset(const char *ifname, int *buf)
2759 const struct iwinfo_hardware_entry *hw;
2761 if (!(hw = nl80211_get_hardware_entry(ifname)))
2764 *buf = hw->txpower_offset;
2768 static int nl80211_get_frequency_offset(const char *ifname, int *buf)
2770 const struct iwinfo_hardware_entry *hw;
2772 if (!(hw = nl80211_get_hardware_entry(ifname)))
2775 *buf = hw->frequency_offset;
2779 static int nl80211_lookup_phyname(const char *section, char *buf)
2783 if ((idx = nl80211_phy_idx_from_uci(section)) < 0)
2786 sprintf(buf, "phy%d", idx);
2790 const struct iwinfo_ops nl80211_ops = {
2792 .probe = nl80211_probe,
2793 .channel = nl80211_get_channel,
2794 .frequency = nl80211_get_frequency,
2795 .frequency_offset = nl80211_get_frequency_offset,
2796 .txpower = nl80211_get_txpower,
2797 .txpower_offset = nl80211_get_txpower_offset,
2798 .bitrate = nl80211_get_bitrate,
2799 .signal = nl80211_get_signal,
2800 .noise = nl80211_get_noise,
2801 .quality = nl80211_get_quality,
2802 .quality_max = nl80211_get_quality_max,
2803 .mbssid_support = nl80211_get_mbssid_support,
2804 .hwmodelist = nl80211_get_hwmodelist,
2805 .htmodelist = nl80211_get_htmodelist,
2806 .mode = nl80211_get_mode,
2807 .ssid = nl80211_get_ssid,
2808 .bssid = nl80211_get_bssid,
2809 .country = nl80211_get_country,
2810 .hardware_id = nl80211_get_hardware_id,
2811 .hardware_name = nl80211_get_hardware_name,
2812 .encryption = nl80211_get_encryption,
2813 .phyname = nl80211_get_phyname,
2814 .assoclist = nl80211_get_assoclist,
2815 .txpwrlist = nl80211_get_txpwrlist,
2816 .scanlist = nl80211_get_scanlist,
2817 .freqlist = nl80211_get_freqlist,
2818 .countrylist = nl80211_get_countrylist,
2819 .lookup_phy = nl80211_lookup_phyname,
2820 .close = nl80211_close