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.
27 #include "iwinfo_nl80211.h"
29 #define min(x, y) ((x) < (y)) ? (x) : (y)
31 static struct nl80211_state *nls = NULL;
33 static void nl80211_close(void)
38 genl_family_put(nls->nlctrl);
41 genl_family_put(nls->nl80211);
44 nl_socket_free(nls->nl_sock);
47 nl_cache_free(nls->nl_cache);
54 static int nl80211_init(void)
60 nls = malloc(sizeof(struct nl80211_state));
66 memset(nls, 0, sizeof(*nls));
68 nls->nl_sock = nl_socket_alloc();
74 if (genl_connect(nls->nl_sock)) {
79 fd = nl_socket_get_fd(nls->nl_sock);
80 if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0) {
85 if (genl_ctrl_alloc_cache(nls->nl_sock, &nls->nl_cache)) {
90 nls->nl80211 = genl_ctrl_search_by_name(nls->nl_cache, "nl80211");
96 nls->nlctrl = genl_ctrl_search_by_name(nls->nl_cache, "nlctrl");
111 static int nl80211_readint(const char *path)
117 if ((fd = open(path, O_RDONLY)) > -1)
119 if (read(fd, buffer, sizeof(buffer)) > 0)
129 static int nl80211_msg_error(struct sockaddr_nl *nla,
130 struct nlmsgerr *err, void *arg)
137 static int nl80211_msg_finish(struct nl_msg *msg, void *arg)
144 static int nl80211_msg_ack(struct nl_msg *msg, void *arg)
151 static int nl80211_msg_response(struct nl_msg *msg, void *arg)
156 static void nl80211_free(struct nl80211_msg_conveyor *cv)
171 static struct nl80211_msg_conveyor * nl80211_new(struct genl_family *family,
174 static struct nl80211_msg_conveyor cv;
176 struct nl_msg *req = NULL;
177 struct nl_cb *cb = NULL;
183 cb = nl_cb_alloc(NL_CB_DEFAULT);
187 genlmsg_put(req, 0, 0, genl_family_get_id(family), 0, flags, cmd, 0);
205 static struct nl80211_msg_conveyor * nl80211_ctl(int cmd, int flags)
207 if (nl80211_init() < 0)
210 return nl80211_new(nls->nlctrl, cmd, flags);
213 static int nl80211_phy_idx_from_uci(const char *name)
215 struct uci_section *s;
223 s = iwinfo_uci_get_radio(name, "mac80211");
227 opt = uci_lookup_option_string(uci_ctx, s, "path");
231 snprintf(buf, sizeof(buf), "/sys/devices/%s/ieee80211/*/index", opt);
232 err = glob(buf, 0, NULL, &gl);
237 f = fopen(gl.gl_pathv[0], "r");
244 err = fread(buf, 1, sizeof(buf) - 1, f);
258 static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname,
261 int ifidx = -1, phyidx = -1;
262 struct nl80211_msg_conveyor *cv;
267 if (nl80211_init() < 0)
270 if (!strncmp(ifname, "phy", 3))
271 phyidx = atoi(&ifname[3]);
272 else if (!strncmp(ifname, "radio", 5))
273 phyidx = nl80211_phy_idx_from_uci(ifname);
274 else if (!strncmp(ifname, "mon.", 4))
275 ifidx = if_nametoindex(&ifname[4]);
277 ifidx = if_nametoindex(ifname);
279 /* Valid ifidx must be greater than 0 */
280 if ((ifidx <= 0) && (phyidx < 0))
283 cv = nl80211_new(nls->nl80211, cmd, flags);
288 NLA_PUT_U32(cv->msg, NL80211_ATTR_IFINDEX, ifidx);
291 NLA_PUT_U32(cv->msg, NL80211_ATTR_WIPHY, phyidx);
300 static struct nl80211_msg_conveyor * nl80211_send(
301 struct nl80211_msg_conveyor *cv,
302 int (*cb_func)(struct nl_msg *, void *), void *cb_arg
304 static struct nl80211_msg_conveyor rcv;
308 nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, cb_func, cb_arg);
310 nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_msg_response, &rcv);
312 if (nl_send_auto_complete(nls->nl_sock, cv->msg) < 0)
315 nl_cb_err(cv->cb, NL_CB_CUSTOM, nl80211_msg_error, &err);
316 nl_cb_set(cv->cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_msg_finish, &err);
317 nl_cb_set(cv->cb, NL_CB_ACK, NL_CB_CUSTOM, nl80211_msg_ack, &err);
320 nl_recvmsgs(nls->nl_sock, cv->cb);
331 static struct nlattr ** nl80211_parse(struct nl_msg *msg)
333 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
334 static struct nlattr *attr[NL80211_ATTR_MAX + 1];
336 nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
337 genlmsg_attrlen(gnlh, 0), NULL);
343 static int nl80211_subscribe_cb(struct nl_msg *msg, void *arg)
345 struct nl80211_group_conveyor *cv = arg;
347 struct nlattr **attr = nl80211_parse(msg);
348 struct nlattr *mgrpinfo[CTRL_ATTR_MCAST_GRP_MAX + 1];
352 if (!attr[CTRL_ATTR_MCAST_GROUPS])
355 nla_for_each_nested(mgrp, attr[CTRL_ATTR_MCAST_GROUPS], mgrpidx)
357 nla_parse(mgrpinfo, CTRL_ATTR_MCAST_GRP_MAX,
358 nla_data(mgrp), nla_len(mgrp), NULL);
360 if (mgrpinfo[CTRL_ATTR_MCAST_GRP_ID] &&
361 mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME] &&
362 !strncmp(nla_data(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME]),
363 cv->name, nla_len(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME])))
365 cv->id = nla_get_u32(mgrpinfo[CTRL_ATTR_MCAST_GRP_ID]);
373 static int nl80211_subscribe(const char *family, const char *group)
375 struct nl80211_group_conveyor cv = { .name = group, .id = -ENOENT };
376 struct nl80211_msg_conveyor *req;
378 req = nl80211_ctl(CTRL_CMD_GETFAMILY, 0);
381 NLA_PUT_STRING(req->msg, CTRL_ATTR_FAMILY_NAME, family);
382 nl80211_send(req, nl80211_subscribe_cb, &cv);
388 return nl_socket_add_membership(nls->nl_sock, cv.id);
392 static int nl80211_wait_cb(struct nl_msg *msg, void *arg)
394 struct nl80211_event_conveyor *cv = arg;
395 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
397 if (gnlh->cmd == cv->wait)
398 cv->recv = gnlh->cmd;
403 static int nl80211_wait_seq_check(struct nl_msg *msg, void *arg)
408 static int nl80211_wait(const char *family, const char *group, int cmd)
410 struct nl80211_event_conveyor cv = { .wait = cmd };
413 if (nl80211_subscribe(family, group))
416 cb = nl_cb_alloc(NL_CB_DEFAULT);
421 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, nl80211_wait_seq_check, NULL);
422 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_wait_cb, &cv );
425 nl_recvmsgs(nls->nl_sock, cb);
433 static int nl80211_freq2channel(int freq)
437 else if (freq < 2484)
438 return (freq - 2407) / 5;
439 else if (freq >= 4910 && freq <= 4980)
440 return (freq - 4000) / 5;
442 return (freq - 5000) / 5;
445 static int nl80211_channel2freq(int channel, const char *band)
447 if (!band || band[0] != 'a')
451 else if (channel < 14)
452 return (channel * 5) + 2407;
456 if (channel >= 182 && channel <= 196)
457 return (channel * 5) + 4000;
459 return (channel * 5) + 5000;
465 static char * nl80211_getval(const char *ifname, const char *buf, const char *key)
468 char lkey[64] = { 0 };
469 const char *ln = buf;
470 static char lval[256] = { 0 };
472 int matched_if = ifname ? 0 : 1;
475 for( i = 0, len = strlen(buf); i < len; i++ )
477 if (!lkey[0] && (buf[i] == ' ' || buf[i] == '\t'))
481 else if (!lkey[0] && (buf[i] == '='))
483 if ((&buf[i] - ln) > 0)
484 memcpy(lkey, ln, min(sizeof(lkey) - 1, &buf[i] - ln));
486 else if (buf[i] == '\n')
490 memcpy(lval, ln + strlen(lkey) + 1,
491 min(sizeof(lval) - 1, &buf[i] - ln - strlen(lkey) - 1));
493 if ((ifname != NULL) &&
494 (!strcmp(lkey, "interface") || !strcmp(lkey, "bss")) )
496 matched_if = !strcmp(lval, ifname);
498 else if (matched_if && !strcmp(lkey, key))
505 memset(lkey, 0, sizeof(lkey));
506 memset(lval, 0, sizeof(lval));
513 static int nl80211_ifname2phy_cb(struct nl_msg *msg, void *arg)
516 struct nlattr **attr = nl80211_parse(msg);
518 if (attr[NL80211_ATTR_WIPHY_NAME])
519 memcpy(buf, nla_data(attr[NL80211_ATTR_WIPHY_NAME]),
520 nla_len(attr[NL80211_ATTR_WIPHY_NAME]));
527 static char * nl80211_ifname2phy(const char *ifname)
529 static char phy[32] = { 0 };
530 struct nl80211_msg_conveyor *req;
532 memset(phy, 0, sizeof(phy));
534 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
537 nl80211_send(req, nl80211_ifname2phy_cb, phy);
541 return phy[0] ? phy : NULL;
544 static char * nl80211_phy2ifname(const char *ifname)
546 int fd, ifidx = -1, cifidx = -1, phyidx = -1;
548 static char nif[IFNAMSIZ] = { 0 };
553 /* Only accept phy name of the form phy%d or radio%d */
556 else if (!strncmp(ifname, "phy", 3))
557 phyidx = atoi(&ifname[3]);
558 else if (!strncmp(ifname, "radio", 5))
559 phyidx = nl80211_phy_idx_from_uci(ifname);
563 memset(nif, 0, sizeof(nif));
567 if ((d = opendir("/sys/class/net")) != NULL)
569 while ((e = readdir(d)) != NULL)
571 snprintf(buffer, sizeof(buffer),
572 "/sys/class/net/%s/phy80211/index", e->d_name);
574 if (nl80211_readint(buffer) == phyidx)
576 snprintf(buffer, sizeof(buffer),
577 "/sys/class/net/%s/ifindex", e->d_name);
579 if ((cifidx = nl80211_readint(buffer)) >= 0 &&
580 ((ifidx < 0) || (cifidx < ifidx)))
583 strncpy(nif, e->d_name, sizeof(nif));
592 return nif[0] ? nif : NULL;
595 static int nl80211_get_mode_cb(struct nl_msg *msg, void *arg)
598 struct nlattr **tb = nl80211_parse(msg);
599 const int ifmodes[NL80211_IFTYPE_MAX + 1] = {
600 IWINFO_OPMODE_UNKNOWN, /* unspecified */
601 IWINFO_OPMODE_ADHOC, /* IBSS */
602 IWINFO_OPMODE_CLIENT, /* managed */
603 IWINFO_OPMODE_MASTER, /* AP */
604 IWINFO_OPMODE_AP_VLAN, /* AP/VLAN */
605 IWINFO_OPMODE_WDS, /* WDS */
606 IWINFO_OPMODE_MONITOR, /* monitor */
607 IWINFO_OPMODE_MESHPOINT, /* mesh point */
608 IWINFO_OPMODE_P2P_CLIENT, /* P2P-client */
609 IWINFO_OPMODE_P2P_GO, /* P2P-GO */
612 if (tb[NL80211_ATTR_IFTYPE])
613 *mode = ifmodes[nla_get_u32(tb[NL80211_ATTR_IFTYPE])];
619 static int nl80211_get_mode(const char *ifname, int *buf)
622 struct nl80211_msg_conveyor *req;
624 res = nl80211_phy2ifname(ifname);
625 req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0);
626 *buf = IWINFO_OPMODE_UNKNOWN;
630 nl80211_send(req, nl80211_get_mode_cb, buf);
634 return (*buf == IWINFO_OPMODE_UNKNOWN) ? -1 : 0;
637 static char * nl80211_hostapd_info(const char *ifname)
641 char path[32] = { 0 };
642 static char buf[4096] = { 0 };
645 if (nl80211_get_mode(ifname, &mode))
648 if ((mode == IWINFO_OPMODE_MASTER || mode == IWINFO_OPMODE_AP_VLAN) &&
649 (phy = nl80211_ifname2phy(ifname)) != NULL)
651 snprintf(path, sizeof(path), "/var/run/hostapd-%s.conf", phy);
653 if ((conf = fopen(path, "r")) != NULL)
655 fread(buf, sizeof(buf) - 1, 1, conf);
665 static inline int nl80211_wpactl_recv(int sock, char *buf, int blen)
668 struct timeval tv = { 2, 0 };
673 memset(buf, 0, blen);
676 if (select(sock + 1, &rfds, NULL, NULL, &tv) < 0)
679 if (!FD_ISSET(sock, &rfds))
682 return recv(sock, buf, blen, 0);
685 static char * nl80211_wpactl_info(const char *ifname, const char *cmd,
691 size_t remote_length, local_length;
692 static char buffer[10240] = { 0 };
694 struct sockaddr_un local = { 0 };
695 struct sockaddr_un remote = { 0 };
698 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
702 remote.sun_family = AF_UNIX;
703 remote_length = sizeof(remote.sun_family) + sprintf(remote.sun_path,
704 "/var/run/wpa_supplicant-%s/%s", ifname, ifname);
706 if (fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC) < 0)
709 if (connect(sock, (struct sockaddr *) &remote, remote_length))
711 remote_length = sizeof(remote.sun_family) + sprintf(remote.sun_path,
712 "/var/run/wpa_supplicant/%s", ifname);
714 if (connect(sock, (struct sockaddr *) &remote, remote_length))
718 local.sun_family = AF_UNIX;
719 local_length = sizeof(local.sun_family) +
720 sprintf(local.sun_path, "/var/run/iwinfo-%s-%d", ifname, getpid());
722 if (bind(sock, (struct sockaddr *) &local, local_length))
728 send(sock, "ATTACH", 6, 0);
730 if (nl80211_wpactl_recv(sock, buffer, sizeof(buffer)-1) <= 0)
735 send(sock, cmd, strlen(cmd), 0);
737 /* we might have to scan up to 72 channels / 256ms per channel */
738 /* this makes up to 18.5s hence 10 tries */
739 while( numtry++ < 10 )
743 /* make sure there is a terminating nul byte */
744 if (nl80211_wpactl_recv(sock, buffer, sizeof(buffer)-1) <= 0)
752 if ((!event && buffer[0] != '<') || (event && strstr(buffer, event)))
755 /* there may be more than max(numtry) BSS-ADDED events */
756 /* ignore them similar to wpa_cli */
757 if (buffer[0] == '<' &&
758 (bracket=strchr(buffer,'>')) != NULL &&
759 strncmp(bracket+1,"CTRL-EVENT-BSS-ADDED",20) == 0)
768 if (local.sun_family)
769 unlink(local.sun_path);
774 static char * nl80211_ifadd(const char *ifname)
777 char *rv = NULL, path[PATH_MAX];
778 static char nif[IFNAMSIZ] = { 0 };
779 struct nl80211_msg_conveyor *req, *res;
782 req = nl80211_msg(ifname, NL80211_CMD_NEW_INTERFACE, 0);
785 snprintf(nif, sizeof(nif), "tmp.%s", ifname);
787 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, nif);
788 NLA_PUT_U32(req->msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION);
790 nl80211_send(req, NULL, NULL);
792 snprintf(path, sizeof(path) - 1,
793 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif);
795 if ((sysfs = fopen(path, "w")) != NULL)
797 fwrite("0\n", 1, 2, sysfs);
810 static void nl80211_ifdel(const char *ifname)
812 struct nl80211_msg_conveyor *req;
814 req = nl80211_msg(ifname, NL80211_CMD_DEL_INTERFACE, 0);
817 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, ifname);
819 nl80211_send(req, NULL, NULL);
826 static void nl80211_hostapd_hup(const char *ifname)
830 char *phy = nl80211_ifname2phy(ifname);
834 snprintf(buf, sizeof(buf), "/var/run/wifi-%s.pid", phy);
835 if ((fd = open(buf, O_RDONLY)) > 0)
837 if (read(fd, buf, sizeof(buf)) > 0)
849 static int nl80211_probe(const char *ifname)
851 return !!nl80211_ifname2phy(ifname);
854 struct nl80211_ssid_bssid {
856 unsigned char bssid[7];
859 static int nl80211_get_ssid_bssid_cb(struct nl_msg *msg, void *arg)
863 struct nl80211_ssid_bssid *sb = arg;
864 struct nlattr **tb = nl80211_parse(msg);
865 struct nlattr *bss[NL80211_BSS_MAX + 1];
867 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
868 [NL80211_BSS_INFORMATION_ELEMENTS] = { },
869 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
872 if (!tb[NL80211_ATTR_BSS] ||
873 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
875 !bss[NL80211_BSS_BSSID] ||
876 !bss[NL80211_BSS_STATUS] ||
877 !bss[NL80211_BSS_INFORMATION_ELEMENTS])
882 switch (nla_get_u32(bss[NL80211_BSS_STATUS]))
884 case NL80211_BSS_STATUS_ASSOCIATED:
885 case NL80211_BSS_STATUS_AUTHENTICATED:
886 case NL80211_BSS_STATUS_IBSS_JOINED:
890 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
891 ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
893 while (ielen >= 2 && ielen >= ie[1])
897 memcpy(sb->ssid, ie + 2, min(ie[1], IWINFO_ESSID_MAX_SIZE));
908 memcpy(sb->bssid + 1, nla_data(bss[NL80211_BSS_BSSID]), 6);
917 static int nl80211_get_ssid(const char *ifname, char *buf)
920 struct nl80211_msg_conveyor *req;
921 struct nl80211_ssid_bssid sb;
923 /* try to find ssid from scan dump results */
924 res = nl80211_phy2ifname(ifname);
925 req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP);
932 nl80211_send(req, nl80211_get_ssid_bssid_cb, &sb);
936 /* failed, try to find from hostapd info */
938 (res = nl80211_hostapd_info(ifname)) &&
939 (res = nl80211_getval(ifname, res, "ssid")))
941 memcpy(buf, res, strlen(res));
944 return (*buf == 0) ? -1 : 0;
947 static int nl80211_get_bssid(const char *ifname, char *buf)
950 struct nl80211_msg_conveyor *req;
951 struct nl80211_ssid_bssid sb;
953 /* try to find bssid from scan dump results */
954 res = nl80211_phy2ifname(ifname);
955 req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP);
962 nl80211_send(req, nl80211_get_ssid_bssid_cb, &sb);
966 /* failed, try to find mac from hostapd info */
967 if ((sb.bssid[0] == 0) &&
968 (res = nl80211_hostapd_info(ifname)) &&
969 (res = nl80211_getval(ifname, res, "bssid")))
972 sb.bssid[1] = strtol(&res[0], NULL, 16);
973 sb.bssid[2] = strtol(&res[3], NULL, 16);
974 sb.bssid[3] = strtol(&res[6], NULL, 16);
975 sb.bssid[4] = strtol(&res[9], NULL, 16);
976 sb.bssid[5] = strtol(&res[12], NULL, 16);
977 sb.bssid[6] = strtol(&res[15], NULL, 16);
982 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
983 sb.bssid[1], sb.bssid[2], sb.bssid[3],
984 sb.bssid[4], sb.bssid[5], sb.bssid[6]);
993 static int nl80211_get_frequency_scan_cb(struct nl_msg *msg, void *arg)
996 struct nlattr **attr = nl80211_parse(msg);
997 struct nlattr *binfo[NL80211_BSS_MAX + 1];
999 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1000 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1001 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1004 if (attr[NL80211_ATTR_BSS] &&
1005 !nla_parse_nested(binfo, NL80211_BSS_MAX,
1006 attr[NL80211_ATTR_BSS], bss_policy))
1008 if (binfo[NL80211_BSS_STATUS] && binfo[NL80211_BSS_FREQUENCY])
1009 *freq = nla_get_u32(binfo[NL80211_BSS_FREQUENCY]);
1015 static int nl80211_get_frequency_info_cb(struct nl_msg *msg, void *arg)
1018 struct nlattr **tb = nl80211_parse(msg);
1020 if (tb[NL80211_ATTR_WIPHY_FREQ])
1021 *freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1026 static int nl80211_get_frequency(const char *ifname, int *buf)
1029 char *res, *channel;
1030 struct nl80211_msg_conveyor *req;
1032 /* try to find frequency from interface info */
1033 res = nl80211_phy2ifname(ifname);
1034 req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0);
1039 nl80211_send(req, nl80211_get_frequency_info_cb, buf);
1043 /* failed, try to find frequency from hostapd info */
1045 (res = nl80211_hostapd_info(ifname)) &&
1046 (channel = nl80211_getval(NULL, res, "channel")))
1048 chn = atoi(channel);
1049 *buf = nl80211_channel2freq(chn, nl80211_getval(NULL, res, "hw_mode"));
1053 /* failed, try to find frequency from scan results */
1056 res = nl80211_phy2ifname(ifname);
1057 req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_SCAN,
1062 nl80211_send(req, nl80211_get_frequency_scan_cb, buf);
1068 return (*buf == 0) ? -1 : 0;
1071 static int nl80211_get_channel(const char *ifname, int *buf)
1073 if (!nl80211_get_frequency(ifname, buf))
1075 *buf = nl80211_freq2channel(*buf);
1083 static int nl80211_get_txpower(const char *ifname, int *buf)
1087 char path[PATH_MAX];
1089 res = nl80211_ifname2phy(ifname);
1090 snprintf(path, sizeof(path), "/sys/kernel/debug/ieee80211/%s/power",
1091 res ? res : ifname);
1093 if ((*buf = nl80211_readint(path)) > -1)
1097 return wext_ops.txpower(ifname, buf);
1101 static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
1105 struct nl80211_rssi_rate *rr = arg;
1106 struct nlattr **attr = nl80211_parse(msg);
1107 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1108 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1110 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
1111 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
1112 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
1113 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
1114 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
1115 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
1116 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1117 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
1118 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
1119 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
1120 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
1123 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1124 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1125 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1126 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1127 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1130 if (attr[NL80211_ATTR_STA_INFO])
1132 if (!nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1133 attr[NL80211_ATTR_STA_INFO], stats_policy))
1135 if (sinfo[NL80211_STA_INFO_SIGNAL])
1137 dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1138 rr->rssi = rr->rssi ? (int8_t)((rr->rssi + dbm) / 2) : dbm;
1141 if (sinfo[NL80211_STA_INFO_TX_BITRATE])
1143 if (!nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1144 sinfo[NL80211_STA_INFO_TX_BITRATE],
1147 if (rinfo[NL80211_RATE_INFO_BITRATE])
1149 mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
1151 ? (int16_t)((rr->rate + mbit) / 2) : mbit;
1161 static void nl80211_fill_signal(const char *ifname, struct nl80211_rssi_rate *r)
1165 struct nl80211_msg_conveyor *req;
1170 if ((d = opendir("/sys/class/net")) != NULL)
1172 while ((de = readdir(d)) != NULL)
1174 if (!strncmp(de->d_name, ifname, strlen(ifname)) &&
1175 (!de->d_name[strlen(ifname)] ||
1176 !strncmp(&de->d_name[strlen(ifname)], ".sta", 4)))
1178 req = nl80211_msg(de->d_name, NL80211_CMD_GET_STATION,
1183 nl80211_send(req, nl80211_fill_signal_cb, r);
1193 static int nl80211_get_bitrate(const char *ifname, int *buf)
1195 struct nl80211_rssi_rate rr;
1197 nl80211_fill_signal(ifname, &rr);
1201 *buf = (rr.rate * 100);
1208 static int nl80211_get_signal(const char *ifname, int *buf)
1210 struct nl80211_rssi_rate rr;
1212 nl80211_fill_signal(ifname, &rr);
1223 static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg)
1225 int8_t *noise = arg;
1226 struct nlattr **tb = nl80211_parse(msg);
1227 struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
1229 static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
1230 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1231 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1234 if (!tb[NL80211_ATTR_SURVEY_INFO])
1237 if (nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
1238 tb[NL80211_ATTR_SURVEY_INFO], sp))
1241 if (!si[NL80211_SURVEY_INFO_NOISE])
1244 if (!*noise || si[NL80211_SURVEY_INFO_IN_USE])
1245 *noise = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
1251 static int nl80211_get_noise(const char *ifname, int *buf)
1254 struct nl80211_msg_conveyor *req;
1256 req = nl80211_msg(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP);
1261 nl80211_send(req, nl80211_get_noise_cb, &noise);
1274 static int nl80211_get_quality(const char *ifname, int *buf)
1278 if (!nl80211_get_signal(ifname, &signal))
1280 /* A positive signal level is usually just a quality
1281 * value, pass through as-is */
1287 /* The cfg80211 wext compat layer assumes a signal range
1288 * of -110 dBm to -40 dBm, the quality value is derived
1289 * by adding 110 to the signal level */
1294 else if (signal > -40)
1297 *buf = (signal + 110);
1306 static int nl80211_get_quality_max(const char *ifname, int *buf)
1308 /* The cfg80211 wext compat layer assumes a maximum
1315 static int nl80211_get_encryption(const char *ifname, char *buf)
1320 struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
1322 /* WPA supplicant */
1323 if ((res = nl80211_wpactl_info(ifname, "STATUS", NULL)) &&
1324 (val = nl80211_getval(NULL, res, "pairwise_cipher")))
1327 if (strstr(val, "WEP"))
1329 if (strstr(val, "WEP-40"))
1330 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
1332 else if (strstr(val, "WEP-104"))
1333 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
1336 c->group_ciphers = c->pair_ciphers;
1338 c->auth_suites |= IWINFO_KMGMT_NONE;
1339 c->auth_algs |= IWINFO_AUTH_OPEN; /* XXX: assumption */
1345 if (strstr(val, "TKIP"))
1346 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
1348 else if (strstr(val, "CCMP"))
1349 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
1351 else if (strstr(val, "NONE"))
1352 c->pair_ciphers |= IWINFO_CIPHER_NONE;
1354 else if (strstr(val, "WEP-40"))
1355 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
1357 else if (strstr(val, "WEP-104"))
1358 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
1361 if ((val = nl80211_getval(NULL, res, "group_cipher")))
1363 if (strstr(val, "TKIP"))
1364 c->group_ciphers |= IWINFO_CIPHER_TKIP;
1366 else if (strstr(val, "CCMP"))
1367 c->group_ciphers |= IWINFO_CIPHER_CCMP;
1369 else if (strstr(val, "NONE"))
1370 c->group_ciphers |= IWINFO_CIPHER_NONE;
1372 else if (strstr(val, "WEP-40"))
1373 c->group_ciphers |= IWINFO_CIPHER_WEP40;
1375 else if (strstr(val, "WEP-104"))
1376 c->group_ciphers |= IWINFO_CIPHER_WEP104;
1380 if ((val = nl80211_getval(NULL, res, "key_mgmt")))
1382 if (strstr(val, "WPA2"))
1385 else if (strstr(val, "WPA"))
1389 if (strstr(val, "PSK"))
1390 c->auth_suites |= IWINFO_KMGMT_PSK;
1392 else if (strstr(val, "EAP") || strstr(val, "802.1X"))
1393 c->auth_suites |= IWINFO_KMGMT_8021x;
1395 else if (strstr(val, "NONE"))
1396 c->auth_suites |= IWINFO_KMGMT_NONE;
1399 c->enabled = (c->wpa_version && c->auth_suites) ? 1 : 0;
1406 else if ((res = nl80211_hostapd_info(ifname)))
1408 if ((val = nl80211_getval(ifname, res, "wpa")) != NULL)
1409 c->wpa_version = atoi(val);
1411 val = nl80211_getval(ifname, res, "wpa_key_mgmt");
1413 if (!val || strstr(val, "PSK"))
1414 c->auth_suites |= IWINFO_KMGMT_PSK;
1416 if (val && strstr(val, "EAP"))
1417 c->auth_suites |= IWINFO_KMGMT_8021x;
1419 if (val && strstr(val, "NONE"))
1420 c->auth_suites |= IWINFO_KMGMT_NONE;
1422 if ((val = nl80211_getval(ifname, res, "wpa_pairwise")) != NULL)
1424 if (strstr(val, "TKIP"))
1425 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
1427 if (strstr(val, "CCMP"))
1428 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
1430 if (strstr(val, "NONE"))
1431 c->pair_ciphers |= IWINFO_CIPHER_NONE;
1434 if ((val = nl80211_getval(ifname, res, "auth_algs")) != NULL)
1438 c->auth_algs |= IWINFO_AUTH_OPEN;
1442 c->auth_algs |= IWINFO_AUTH_SHARED;
1446 c->auth_algs |= IWINFO_AUTH_OPEN;
1447 c->auth_algs |= IWINFO_AUTH_SHARED;
1454 for (i = 0; i < 4; i++)
1456 snprintf(k, sizeof(k), "wep_key%d", i);
1458 if ((val = nl80211_getval(ifname, res, k)))
1460 if ((strlen(val) == 5) || (strlen(val) == 10))
1461 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
1463 else if ((strlen(val) == 13) || (strlen(val) == 26))
1464 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
1469 c->group_ciphers = c->pair_ciphers;
1470 c->enabled = (c->wpa_version || c->pair_ciphers) ? 1 : 0;
1478 static int nl80211_get_phyname(const char *ifname, char *buf)
1482 name = nl80211_ifname2phy(ifname);
1489 else if ((name = nl80211_phy2ifname(ifname)) != NULL)
1491 name = nl80211_ifname2phy(name);
1495 strcpy(buf, ifname);
1504 static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
1506 struct nl80211_array_buf *arr = arg;
1507 struct iwinfo_assoclist_entry *e = arr->buf;
1508 struct nlattr **attr = nl80211_parse(msg);
1509 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1510 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1512 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
1513 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
1514 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
1515 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
1516 [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED },
1517 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
1518 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1521 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1522 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1523 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1524 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1525 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1528 /* advance to end of array */
1530 memset(e, 0, sizeof(*e));
1532 if (attr[NL80211_ATTR_MAC])
1533 memcpy(e->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
1535 if (attr[NL80211_ATTR_STA_INFO] &&
1536 !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1537 attr[NL80211_ATTR_STA_INFO], stats_policy))
1539 if (sinfo[NL80211_STA_INFO_SIGNAL])
1540 e->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1542 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
1543 e->inactive = nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]);
1545 if (sinfo[NL80211_STA_INFO_RX_PACKETS])
1546 e->rx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]);
1548 if (sinfo[NL80211_STA_INFO_TX_PACKETS])
1549 e->tx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]);
1551 if (sinfo[NL80211_STA_INFO_RX_BITRATE] &&
1552 !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1553 sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy))
1555 if (rinfo[NL80211_RATE_INFO_BITRATE])
1557 nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]) * 100;
1559 if (rinfo[NL80211_RATE_INFO_MCS])
1560 e->rx_rate.mcs = nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]);
1562 if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
1563 e->rx_rate.is_40mhz = 1;
1565 if (rinfo[NL80211_RATE_INFO_SHORT_GI])
1566 e->rx_rate.is_short_gi = 1;
1569 if (sinfo[NL80211_STA_INFO_TX_BITRATE] &&
1570 !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1571 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy))
1573 if (rinfo[NL80211_RATE_INFO_BITRATE])
1575 nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]) * 100;
1577 if (rinfo[NL80211_RATE_INFO_MCS])
1578 e->tx_rate.mcs = nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]);
1580 if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
1581 e->tx_rate.is_40mhz = 1;
1583 if (rinfo[NL80211_RATE_INFO_SHORT_GI])
1584 e->tx_rate.is_short_gi = 1;
1588 e->noise = 0; /* filled in by caller */
1594 static int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
1599 struct nl80211_msg_conveyor *req;
1600 struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
1601 struct iwinfo_assoclist_entry *e;
1603 if ((d = opendir("/sys/class/net")) != NULL)
1605 while ((de = readdir(d)) != NULL)
1607 if (!strncmp(de->d_name, ifname, strlen(ifname)) &&
1608 (!de->d_name[strlen(ifname)] ||
1609 !strncmp(&de->d_name[strlen(ifname)], ".sta", 4)))
1611 req = nl80211_msg(de->d_name, NL80211_CMD_GET_STATION,
1616 nl80211_send(req, nl80211_get_assoclist_cb, &arr);
1624 if (!nl80211_get_noise(ifname, &noise))
1625 for (i = 0, e = arr.buf; i < arr.count; i++, e++)
1628 *len = (arr.count * sizeof(struct iwinfo_assoclist_entry));
1635 static int nl80211_get_txpwrlist_cb(struct nl_msg *msg, void *arg)
1638 int ch_cur, ch_cmp, bands_remain, freqs_remain;
1640 struct nlattr **attr = nl80211_parse(msg);
1641 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
1642 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
1643 struct nlattr *band, *freq;
1645 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1646 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1647 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1648 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1649 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1650 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1651 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
1654 ch_cur = *dbm_max; /* value int* is initialized with channel by caller */
1657 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
1659 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
1660 nla_len(band), NULL);
1662 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
1664 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
1665 nla_data(freq), nla_len(freq), freq_policy);
1667 ch_cmp = nl80211_freq2channel(nla_get_u32(
1668 freqs[NL80211_FREQUENCY_ATTR_FREQ]));
1670 if ((!ch_cur || (ch_cmp == ch_cur)) &&
1671 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER])
1673 *dbm_max = (int)(0.01 * nla_get_u32(
1674 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
1684 static int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
1687 int dbm_max = -1, dbm_cur, dbm_cnt;
1688 struct nl80211_msg_conveyor *req;
1689 struct iwinfo_txpwrlist_entry entry;
1691 if (nl80211_get_channel(ifname, &ch_cur))
1694 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
1697 /* initialize the value pointer with channel for callback */
1700 nl80211_send(req, nl80211_get_txpwrlist_cb, &dbm_max);
1706 for (dbm_cur = 0, dbm_cnt = 0;
1708 dbm_cur++, dbm_cnt++)
1710 entry.dbm = dbm_cur;
1711 entry.mw = iwinfo_dbm2mw(dbm_cur);
1713 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
1716 entry.dbm = dbm_max;
1717 entry.mw = iwinfo_dbm2mw(dbm_max);
1719 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
1722 *len = dbm_cnt * sizeof(entry);
1729 static void nl80211_get_scancrypto(const char *spec,
1730 struct iwinfo_crypto_entry *c)
1732 if (strstr(spec, "WPA") || strstr(spec, "WEP"))
1736 if (strstr(spec, "WPA2-") && strstr(spec, "WPA-"))
1739 else if (strstr(spec, "WPA2"))
1742 else if (strstr(spec, "WPA"))
1745 else if (strstr(spec, "WEP"))
1746 c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
1749 if (strstr(spec, "PSK"))
1750 c->auth_suites |= IWINFO_KMGMT_PSK;
1752 if (strstr(spec, "802.1X") || strstr(spec, "EAP"))
1753 c->auth_suites |= IWINFO_KMGMT_8021x;
1755 if (strstr(spec, "WPA-NONE"))
1756 c->auth_suites |= IWINFO_KMGMT_NONE;
1759 if (strstr(spec, "TKIP"))
1760 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
1762 if (strstr(spec, "CCMP"))
1763 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
1765 if (strstr(spec, "WEP-40"))
1766 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
1768 if (strstr(spec, "WEP-104"))
1769 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
1771 c->group_ciphers = c->pair_ciphers;
1780 struct nl80211_scanlist {
1781 struct iwinfo_scanlist_entry *e;
1786 static void nl80211_get_scanlist_ie(struct nlattr **bss,
1787 struct iwinfo_scanlist_entry *e)
1789 int ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1790 unsigned char *ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1791 static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 };
1794 while (ielen >= 2 && ielen >= ie[1])
1799 len = min(ie[1], IWINFO_ESSID_MAX_SIZE);
1800 memcpy(e->ssid, ie + 2, len);
1805 iwinfo_parse_rsn(&e->crypto, ie + 2, ie[1],
1806 IWINFO_CIPHER_CCMP, IWINFO_KMGMT_8021x);
1809 case 221: /* Vendor */
1810 if (ie[1] >= 4 && !memcmp(ie + 2, ms_oui, 3) && ie[5] == 1)
1811 iwinfo_parse_rsn(&e->crypto, ie + 6, ie[1] - 4,
1812 IWINFO_CIPHER_TKIP, IWINFO_KMGMT_PSK);
1821 static int nl80211_get_scanlist_cb(struct nl_msg *msg, void *arg)
1826 struct nl80211_scanlist *sl = arg;
1827 struct nlattr **tb = nl80211_parse(msg);
1828 struct nlattr *bss[NL80211_BSS_MAX + 1];
1830 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1831 [NL80211_BSS_TSF] = { .type = NLA_U64 },
1832 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1833 [NL80211_BSS_BSSID] = { },
1834 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
1835 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
1836 [NL80211_BSS_INFORMATION_ELEMENTS] = { },
1837 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
1838 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
1839 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1840 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
1841 [NL80211_BSS_BEACON_IES] = { },
1844 if (!tb[NL80211_ATTR_BSS] ||
1845 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1847 !bss[NL80211_BSS_BSSID])
1852 if (bss[NL80211_BSS_CAPABILITY])
1853 caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
1857 memset(sl->e, 0, sizeof(*sl->e));
1858 memcpy(sl->e->mac, nla_data(bss[NL80211_BSS_BSSID]), 6);
1861 sl->e->mode = IWINFO_OPMODE_ADHOC;
1862 else if (caps & (1<<0))
1863 sl->e->mode = IWINFO_OPMODE_MASTER;
1865 sl->e->mode = IWINFO_OPMODE_MESHPOINT;
1868 sl->e->crypto.enabled = 1;
1870 if (bss[NL80211_BSS_FREQUENCY])
1871 sl->e->channel = nl80211_freq2channel(nla_get_u32(
1872 bss[NL80211_BSS_FREQUENCY]));
1874 if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
1875 nl80211_get_scanlist_ie(bss, sl->e);
1877 if (bss[NL80211_BSS_SIGNAL_MBM])
1880 (uint8_t)((int32_t)nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]) / 100);
1882 rssi = sl->e->signal - 0x100;
1886 else if (rssi > -40)
1889 sl->e->quality = (rssi + 110);
1890 sl->e->quality_max = 70;
1893 if (sl->e->crypto.enabled && !sl->e->crypto.wpa_version)
1895 sl->e->crypto.auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
1896 sl->e->crypto.pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
1905 static int nl80211_get_scanlist_nl(const char *ifname, char *buf, int *len)
1907 struct nl80211_msg_conveyor *req;
1908 struct nl80211_scanlist sl = { .e = (struct iwinfo_scanlist_entry *)buf };
1910 req = nl80211_msg(ifname, NL80211_CMD_TRIGGER_SCAN, 0);
1913 nl80211_send(req, NULL, NULL);
1917 nl80211_wait("nl80211", "scan", NL80211_CMD_NEW_SCAN_RESULTS);
1919 req = nl80211_msg(ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP);
1922 nl80211_send(req, nl80211_get_scanlist_cb, &sl);
1926 *len = sl.len * sizeof(struct iwinfo_scanlist_entry);
1927 return *len ? 0 : -1;
1930 static int wpasupp_ssid_decode(const char *in, char *out, int outlen)
1933 (((x) >= 'a') ? ((x) - 'a' + 10) : \
1934 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
1940 if (len + 1 >= outlen)
1950 out[len++] = '\n'; in++;
1954 out[len++] = '\r'; in++;
1958 out[len++] = '\t'; in++;
1962 out[len++] = '\e'; in++;
1966 if (isxdigit(*(in+1)) && isxdigit(*(in+2)))
1967 out[len++] = hex(*(in+1)) * 16 + hex(*(in+2));
1989 static int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
1991 int freq, rssi, qmax, count, mode;
1993 char ssid[129] = { 0 };
1994 char bssid[18] = { 0 };
1995 char cipher[256] = { 0 };
1997 /* Got a radioX pseudo interface, find some interface on it or create one */
1998 if (!strncmp(ifname, "radio", 5))
2000 /* Reuse existing interface */
2001 if ((res = nl80211_phy2ifname(ifname)) != NULL)
2003 return nl80211_get_scanlist(res, buf, len);
2006 /* Need to spawn a temporary iface for scanning */
2007 else if ((res = nl80211_ifadd(ifname)) != NULL)
2009 count = nl80211_get_scanlist(res, buf, len);
2015 struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
2017 /* WPA supplicant */
2018 if ((res = nl80211_wpactl_info(ifname, "SCAN", "CTRL-EVENT-SCAN-RESULTS")))
2020 if ((res = nl80211_wpactl_info(ifname, "SCAN_RESULTS", NULL)))
2022 nl80211_get_quality_max(ifname, &qmax);
2029 /* skip log lines */
2034 /* skip header line */
2038 if (sscanf(res, "%17s %d %d %255s%*[ \t]%128[^\n]\n",
2039 bssid, &freq, &rssi, cipher, ssid) < 5)
2041 /* skip malformed lines */
2045 e->mac[0] = strtol(&bssid[0], NULL, 16);
2046 e->mac[1] = strtol(&bssid[3], NULL, 16);
2047 e->mac[2] = strtol(&bssid[6], NULL, 16);
2048 e->mac[3] = strtol(&bssid[9], NULL, 16);
2049 e->mac[4] = strtol(&bssid[12], NULL, 16);
2050 e->mac[5] = strtol(&bssid[15], NULL, 16);
2053 wpasupp_ssid_decode(ssid, e->ssid, sizeof(e->ssid));
2055 /* Mode (assume master) */
2056 if (strstr(cipher,"[MESH]"))
2057 e->mode = IWINFO_OPMODE_MESHPOINT;
2059 e->mode = IWINFO_OPMODE_MASTER;
2062 e->channel = nl80211_freq2channel(freq);
2070 /* The cfg80211 wext compat layer assumes a signal range
2071 * of -110 dBm to -40 dBm, the quality value is derived
2072 * by adding 110 to the signal level */
2075 else if (rssi > -40)
2078 e->quality = (rssi + 110);
2086 e->quality_max = qmax;
2089 nl80211_get_scancrypto(cipher, &e->crypto);
2094 memset(ssid, 0, sizeof(ssid));
2095 memset(bssid, 0, sizeof(bssid));
2096 memset(cipher, 0, sizeof(cipher));
2099 /* advance to next line */
2100 while( *res && *res++ != '\n' );
2104 *len = count * sizeof(struct iwinfo_scanlist_entry);
2109 /* station / ad-hoc / monitor scan */
2110 else if (!nl80211_get_mode(ifname, &mode) &&
2111 (mode == IWINFO_OPMODE_ADHOC ||
2112 mode == IWINFO_OPMODE_MASTER ||
2113 mode == IWINFO_OPMODE_CLIENT ||
2114 mode == IWINFO_OPMODE_MONITOR) &&
2115 iwinfo_ifup(ifname))
2117 return nl80211_get_scanlist_nl(ifname, buf, len);
2123 /* Got a temp interface, don't create yet another one */
2124 if (!strncmp(ifname, "tmp.", 4))
2126 if (!iwinfo_ifup(ifname))
2129 nl80211_get_scanlist_nl(ifname, buf, len);
2130 iwinfo_ifdown(ifname);
2134 /* Spawn a new scan interface */
2137 if (!(res = nl80211_ifadd(ifname)))
2142 /* if we can take the new interface up, the driver supports an
2143 * additional interface and there's no need to tear down the ap */
2144 if (iwinfo_ifup(res))
2146 nl80211_get_scanlist_nl(res, buf, len);
2150 /* driver cannot create secondary interface, take down ap
2152 else if (iwinfo_ifdown(ifname) && iwinfo_ifup(res))
2154 nl80211_get_scanlist_nl(res, buf, len);
2156 iwinfo_ifup(ifname);
2157 nl80211_hostapd_hup(ifname);
2169 static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg)
2171 int bands_remain, freqs_remain;
2173 struct nl80211_array_buf *arr = arg;
2174 struct iwinfo_freqlist_entry *e = arr->buf;
2176 struct nlattr **attr = nl80211_parse(msg);
2177 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
2178 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
2179 struct nlattr *band, *freq;
2181 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
2182 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
2183 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
2184 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
2185 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
2186 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
2187 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
2190 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
2192 nla_parse(bands, NL80211_BAND_ATTR_MAX,
2193 nla_data(band), nla_len(band), NULL);
2195 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
2197 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
2198 nla_data(freq), nla_len(freq), NULL);
2200 if (!freqs[NL80211_FREQUENCY_ATTR_FREQ] ||
2201 freqs[NL80211_FREQUENCY_ATTR_DISABLED])
2204 e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
2205 e->channel = nl80211_freq2channel(e->mhz);
2208 freqs[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] ||
2209 freqs[NL80211_FREQUENCY_ATTR_NO_IBSS] ||
2210 freqs[NL80211_FREQUENCY_ATTR_RADAR]
2221 static int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
2223 struct nl80211_msg_conveyor *req;
2224 struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
2226 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
2229 nl80211_send(req, nl80211_get_freqlist_cb, &arr);
2235 *len = arr.count * sizeof(struct iwinfo_freqlist_entry);
2242 static int nl80211_get_country_cb(struct nl_msg *msg, void *arg)
2245 struct nlattr **attr = nl80211_parse(msg);
2247 if (attr[NL80211_ATTR_REG_ALPHA2])
2248 memcpy(buf, nla_data(attr[NL80211_ATTR_REG_ALPHA2]), 2);
2255 static int nl80211_get_country(const char *ifname, char *buf)
2258 struct nl80211_msg_conveyor *req;
2260 req = nl80211_msg(ifname, NL80211_CMD_GET_REG, 0);
2263 nl80211_send(req, nl80211_get_country_cb, buf);
2273 static int nl80211_get_countrylist(const char *ifname, char *buf, int *len)
2276 struct iwinfo_country_entry *e = (struct iwinfo_country_entry *)buf;
2277 const struct iwinfo_iso3166_label *l;
2279 for (l = IWINFO_ISO3166_NAMES, count = 0; l->iso3166; l++, e++, count++)
2281 e->iso3166 = l->iso3166;
2282 e->ccode[0] = (l->iso3166 / 256);
2283 e->ccode[1] = (l->iso3166 % 256);
2286 *len = (count * sizeof(struct iwinfo_country_entry));
2290 static int nl80211_get_hwmodelist_cb(struct nl_msg *msg, void *arg)
2293 int bands_remain, freqs_remain;
2295 uint32_t vht_caps = 0;
2296 struct nlattr **attr = nl80211_parse(msg);
2297 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
2298 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
2299 struct nlattr *band, *freq;
2303 if (attr[NL80211_ATTR_WIPHY_BANDS])
2305 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
2307 nla_parse(bands, NL80211_BAND_ATTR_MAX,
2308 nla_data(band), nla_len(band), NULL);
2310 if (bands[NL80211_BAND_ATTR_HT_CAPA])
2311 caps = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]);
2313 /* Treat any nonzero capability as 11n */
2315 *modes |= IWINFO_80211_N;
2317 if (bands[NL80211_BAND_ATTR_VHT_CAPA])
2318 vht_caps = nla_get_u32(bands[NL80211_BAND_ATTR_VHT_CAPA]);
2320 /* Treat any nonzero capability as 11ac */
2322 *modes |= IWINFO_80211_AC;
2324 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS],
2327 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
2328 nla_data(freq), nla_len(freq), NULL);
2330 if (!freqs[NL80211_FREQUENCY_ATTR_FREQ])
2333 if (nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]) < 2485)
2335 *modes |= IWINFO_80211_B;
2336 *modes |= IWINFO_80211_G;
2338 else if (!(*modes & IWINFO_80211_AC))
2340 *modes |= IWINFO_80211_A;
2349 static int nl80211_get_hwmodelist(const char *ifname, int *buf)
2351 struct nl80211_msg_conveyor *req;
2353 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
2356 nl80211_send(req, nl80211_get_hwmodelist_cb, buf);
2360 return *buf ? 0 : -1;
2363 static int nl80211_get_ifcomb_cb(struct nl_msg *msg, void *arg)
2365 struct nlattr **attr = nl80211_parse(msg);
2366 struct nlattr *comb;
2368 int comb_rem, limit_rem, mode_rem;
2371 if (!attr[NL80211_ATTR_INTERFACE_COMBINATIONS])
2374 nla_for_each_nested(comb, attr[NL80211_ATTR_INTERFACE_COMBINATIONS], comb_rem)
2376 static struct nla_policy iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
2377 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
2378 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
2380 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
2381 static struct nla_policy iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
2382 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
2383 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
2385 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
2386 struct nlattr *limit;
2388 nla_parse_nested(tb_comb, NL80211_BAND_ATTR_MAX, comb, iface_combination_policy);
2390 if (!tb_comb[NL80211_IFACE_COMB_LIMITS])
2393 nla_for_each_nested(limit, tb_comb[NL80211_IFACE_COMB_LIMITS], limit_rem)
2395 struct nlattr *mode;
2397 nla_parse_nested(tb_limit, NUM_NL80211_IFACE_LIMIT, limit, iface_limit_policy);
2399 if (!tb_limit[NL80211_IFACE_LIMIT_TYPES] ||
2400 !tb_limit[NL80211_IFACE_LIMIT_MAX])
2403 if (nla_get_u32(tb_limit[NL80211_IFACE_LIMIT_MAX]) < 2)
2406 nla_for_each_nested(mode, tb_limit[NL80211_IFACE_LIMIT_TYPES], mode_rem) {
2407 if (nla_type(mode) == NL80211_IFTYPE_AP)
2416 static int nl80211_get_mbssid_support(const char *ifname, int *buf)
2418 struct nl80211_msg_conveyor *req;
2420 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
2424 nl80211_send(req, nl80211_get_ifcomb_cb, buf);
2429 static int nl80211_get_hardware_id(const char *ifname, char *buf)
2434 /* Got a radioX pseudo interface, find some interface on it or create one */
2435 if (!strncmp(ifname, "radio", 5))
2437 /* Reuse existing interface */
2438 if ((res = nl80211_phy2ifname(ifname)) != NULL)
2440 rv = wext_ops.hardware_id(res, buf);
2443 /* Need to spawn a temporary iface for finding IDs */
2444 else if ((res = nl80211_ifadd(ifname)) != NULL)
2446 rv = wext_ops.hardware_id(res, buf);
2452 rv = wext_ops.hardware_id(ifname, buf);
2455 /* Failed to obtain hardware IDs, search board config */
2458 rv = iwinfo_hardware_id_from_mtd((struct iwinfo_hardware_id *)buf);
2464 static const struct iwinfo_hardware_entry *
2465 nl80211_get_hardware_entry(const char *ifname)
2467 struct iwinfo_hardware_id id;
2469 if (nl80211_get_hardware_id(ifname, (char *)&id))
2472 return iwinfo_hardware(&id);
2475 static int nl80211_get_hardware_name(const char *ifname, char *buf)
2477 const struct iwinfo_hardware_entry *hw;
2479 if (!(hw = nl80211_get_hardware_entry(ifname)))
2480 sprintf(buf, "Generic MAC80211");
2482 sprintf(buf, "%s %s", hw->vendor_name, hw->device_name);
2487 static int nl80211_get_txpower_offset(const char *ifname, int *buf)
2489 const struct iwinfo_hardware_entry *hw;
2491 if (!(hw = nl80211_get_hardware_entry(ifname)))
2494 *buf = hw->txpower_offset;
2498 static int nl80211_get_frequency_offset(const char *ifname, int *buf)
2500 const struct iwinfo_hardware_entry *hw;
2502 if (!(hw = nl80211_get_hardware_entry(ifname)))
2505 *buf = hw->frequency_offset;
2509 const struct iwinfo_ops nl80211_ops = {
2511 .probe = nl80211_probe,
2512 .channel = nl80211_get_channel,
2513 .frequency = nl80211_get_frequency,
2514 .frequency_offset = nl80211_get_frequency_offset,
2515 .txpower = nl80211_get_txpower,
2516 .txpower_offset = nl80211_get_txpower_offset,
2517 .bitrate = nl80211_get_bitrate,
2518 .signal = nl80211_get_signal,
2519 .noise = nl80211_get_noise,
2520 .quality = nl80211_get_quality,
2521 .quality_max = nl80211_get_quality_max,
2522 .mbssid_support = nl80211_get_mbssid_support,
2523 .hwmodelist = nl80211_get_hwmodelist,
2524 .mode = nl80211_get_mode,
2525 .ssid = nl80211_get_ssid,
2526 .bssid = nl80211_get_bssid,
2527 .country = nl80211_get_country,
2528 .hardware_id = nl80211_get_hardware_id,
2529 .hardware_name = nl80211_get_hardware_name,
2530 .encryption = nl80211_get_encryption,
2531 .phyname = nl80211_get_phyname,
2532 .assoclist = nl80211_get_assoclist,
2533 .txpwrlist = nl80211_get_txpwrlist,
2534 .scanlist = nl80211_get_scanlist,
2535 .freqlist = nl80211_get_freqlist,
2536 .countrylist = nl80211_get_countrylist,
2537 .close = nl80211_close