2 * iwinfo - Wireless Information Library - NL80211 Backend
4 * Copyright (C) 2010 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.
25 #include "iwinfo_nl80211.h"
26 #include "iwinfo_wext.h"
28 #define min(x, y) ((x) < (y)) ? (x) : (y)
30 extern struct iwinfo_iso3166_label ISO3166_Names[];
31 static struct nl80211_state *nls = NULL;
33 static int nl80211_init(void)
39 nls = malloc(sizeof(struct nl80211_state));
45 nls->nl_sock = nl_socket_alloc();
51 if( genl_connect(nls->nl_sock)) {
56 fd = nl_socket_get_fd(nls->nl_sock);
57 if( fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0 )
63 if( genl_ctrl_alloc_cache(nls->nl_sock, &nls->nl_cache)) {
68 nls->nl80211 = genl_ctrl_search_by_name(nls->nl_cache, "nl80211");
84 static int nl80211_msg_error(struct sockaddr_nl *nla,
85 struct nlmsgerr *err, void *arg)
92 static int nl80211_msg_finish(struct nl_msg *msg, void *arg)
99 static int nl80211_msg_ack(struct nl_msg *msg, void *arg)
106 static int nl80211_msg_response(struct nl_msg *msg, void *arg)
108 struct nl80211_msg_conveyor *cv = arg;
113 cv->hdr = nlmsg_data(nlmsg_hdr(cv->msg));
115 nla_parse(cv->attr, NL80211_ATTR_MAX,
116 genlmsg_attrdata(cv->hdr, 0),
117 genlmsg_attrlen(cv->hdr, 0), NULL);
122 static void nl80211_free(struct nl80211_msg_conveyor *cv)
137 static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname, int cmd, int flags)
139 static struct nl80211_msg_conveyor cv;
141 int ifidx = -1, phyidx = -1;
142 struct nl_msg *req = NULL;
143 struct nl_cb *cb = NULL;
145 if( nl80211_init() < 0 )
148 if( !strncmp(ifname, "radio", 5) )
149 phyidx = atoi(&ifname[5]);
150 else if( !strncmp(ifname, "mon.", 4) )
151 ifidx = if_nametoindex(&ifname[4]);
153 ifidx = if_nametoindex(ifname);
155 if( (ifidx < 0) && (phyidx < 0) )
162 cb = nl_cb_alloc(NL_CB_DEFAULT);
166 genlmsg_put(req, 0, 0, genl_family_get_id(nls->nl80211), 0,
170 NLA_PUT_U32(req, NL80211_ATTR_IFINDEX, ifidx);
173 NLA_PUT_U32(req, NL80211_ATTR_WIPHY, phyidx);
194 static void nl80211_cb(struct nl80211_msg_conveyor *cv,
195 int (*cb)(struct nl_msg *, void *), void *arg)
198 nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, cb, arg);
201 static struct nl80211_msg_conveyor * nl80211_send(struct nl80211_msg_conveyor *cv)
203 static struct nl80211_msg_conveyor rcv;
207 nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_msg_response, &rcv);
209 if( nl_send_auto_complete(nls->nl_sock, cv->msg) < 0 )
212 nl_cb_err(cv->cb, NL_CB_CUSTOM, nl80211_msg_error, &err);
213 nl_cb_set(cv->cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_msg_finish, &err);
214 nl_cb_set(cv->cb, NL_CB_ACK, NL_CB_CUSTOM, nl80211_msg_ack, &err);
217 nl_recvmsgs(nls->nl_sock, cv->cb);
228 static int nl80211_freq2channel(int freq)
234 return (freq - 2407) / 5;
236 return (freq / 5) - 1000;
239 static char * nl80211_getval(const char *ifname, const char *buf, const char *key)
242 char lkey[64] = { 0 };
243 const char *ln = buf;
244 static char lval[256] = { 0 };
246 int matched_if = ifname ? 0 : 1;
249 for( i = 0, len = strlen(buf); i < len; i++ )
251 if( !lkey[0] && (buf[i] == ' ' || buf[i] == '\t') )
255 else if( !lkey[0] && (buf[i] == '=') )
257 if( (&buf[i] - ln) > 0 )
258 memcpy(lkey, ln, min(sizeof(lkey) - 1, &buf[i] - ln));
260 else if( buf[i] == '\n' )
264 memcpy(lval, ln + strlen(lkey) + 1,
265 min(sizeof(lval) - 1, &buf[i] - ln - strlen(lkey) - 1));
267 if( (ifname != NULL ) &&
268 (!strcmp(lkey, "interface") || !strcmp(lkey, "bss")) )
270 matched_if = !strcmp(lval, ifname);
272 else if( matched_if && !strcmp(lkey, key) )
279 memset(lkey, 0, sizeof(lkey));
280 memset(lval, 0, sizeof(lval));
287 static char * nl80211_ifname2phy(const char *ifname)
289 static char phy[32] = { 0 };
290 struct nl80211_msg_conveyor *req, *res;
292 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
295 res = nl80211_send(req);
298 if( res->attr[NL80211_ATTR_WIPHY_NAME] )
300 snprintf(phy, sizeof(phy), "%s",
301 nla_get_string(res->attr[NL80211_ATTR_WIPHY_NAME]));
308 return phy[0] ? phy : NULL;
311 static char * nl80211_hostapd_info(const char *ifname)
314 char path[32] = { 0 };
315 static char buf[4096] = { 0 };
318 if( (phy = nl80211_ifname2phy(ifname)) != NULL )
320 snprintf(path, sizeof(path), "/var/run/hostapd-%s.conf", phy);
322 if( (conf = fopen(path, "r")) != NULL )
324 fread(buf, sizeof(buf) - 1, 1, conf);
334 static char * nl80211_wpasupp_info(const char *ifname, const char *cmd)
338 size_t remote_length, local_length;
339 static char buffer[1024] = { 0 };
341 struct timeval tv = { 2, 0 };
342 struct sockaddr_un local = { 0 };
343 struct sockaddr_un remote = { 0 };
347 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
351 remote.sun_family = AF_UNIX;
352 remote_length = sizeof(remote.sun_family) + sprintf(remote.sun_path,
353 "/var/run/wpa_supplicant-%s/%s", ifname, ifname);
355 if( fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC) < 0 )
358 if( connect(sock, (struct sockaddr *) &remote, remote_length) )
361 local.sun_family = AF_UNIX;
362 local_length = sizeof(local.sun_family) + sprintf(local.sun_path,
363 "/var/run/iwinfo-%s-%d", ifname, getpid());
365 if( bind(sock, (struct sockaddr *) &local, local_length) )
368 send(sock, cmd, strlen(cmd), 0);
375 if( select(sock + 1, &rfds, NULL, NULL, &tv) < 0 )
378 if( !FD_ISSET(sock, &rfds) )
381 if( (len = recv(sock, buffer, sizeof(buffer), 0)) <= 0 )
386 if( buffer[0] != '<' )
394 unlink(local.sun_path);
399 static char * nl80211_phy2ifname(const char *ifname)
403 static char nif[IFNAMSIZ] = { 0 };
408 if( !strncmp(ifname, "radio", 5) )
410 phyidx = atoi(&ifname[5]);
412 if( (d = opendir("/sys/class/net")) != NULL )
414 while( (e = readdir(d)) != NULL )
416 snprintf(buffer, sizeof(buffer),
417 "/sys/class/net/%s/phy80211/index", e->d_name);
419 if( (fd = open(buffer, O_RDONLY)) > 0 )
421 if( (read(fd, buffer, sizeof(buffer)) > 0) &&
422 (atoi(buffer) == phyidx) )
424 strncpy(nif, e->d_name, sizeof(nif));
438 return nif[0] ? nif : NULL;
441 static char * nl80211_add_tempif(const char *ifname)
445 static char nif[IFNAMSIZ] = { 0 };
446 struct nl80211_msg_conveyor *req, *res;
448 req = nl80211_msg(ifname, NL80211_CMD_NEW_INTERFACE, 0);
451 snprintf(nif, sizeof(nif), "tmp.%s", ifname);
453 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, nif);
454 NLA_PUT_U32(req->msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION);
456 res = nl80211_send(req);
470 static void nl80211_del_tempif(const char *ifname)
472 struct nl80211_msg_conveyor *req, *res;
474 req = nl80211_msg(ifname, NL80211_CMD_DEL_INTERFACE, 0);
477 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, ifname);
479 nl80211_free(nl80211_send(req));
487 int nl80211_probe(const char *ifname)
489 return !!nl80211_ifname2phy(ifname);
492 void nl80211_close(void)
497 nl_socket_free(nls->nl_sock);
500 nl_cache_free(nls->nl_cache);
507 int nl80211_get_mode(const char *ifname, char *buf)
509 return wext_get_mode(ifname, buf);
512 int nl80211_get_ssid(const char *ifname, char *buf)
516 if( !wext_get_ssid(ifname, buf) )
520 else if( (ssid = nl80211_hostapd_info(ifname)) &&
521 (ssid = nl80211_getval(ifname, ssid, "ssid")) )
523 memcpy(buf, ssid, strlen(ssid));
530 int nl80211_get_bssid(const char *ifname, char *buf)
533 unsigned char mac[6];
535 if( !wext_get_bssid(ifname, buf) )
539 else if( (bssid = nl80211_hostapd_info(ifname)) &&
540 (bssid = nl80211_getval(ifname, bssid, "bssid")) )
542 mac[0] = strtol(&bssid[0], NULL, 16);
543 mac[1] = strtol(&bssid[3], NULL, 16);
544 mac[2] = strtol(&bssid[6], NULL, 16);
545 mac[3] = strtol(&bssid[9], NULL, 16);
546 mac[4] = strtol(&bssid[12], NULL, 16);
547 mac[5] = strtol(&bssid[15], NULL, 16);
549 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
550 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
558 int nl80211_get_channel(const char *ifname, int *buf)
560 return wext_get_channel(ifname, buf);
563 int nl80211_get_frequency(const char *ifname, int *buf)
565 return wext_get_frequency(ifname, buf);
568 int nl80211_get_txpower(const char *ifname, int *buf)
570 return wext_get_txpower(ifname, buf);
574 static int nl80211_get_signal_cb(struct nl_msg *msg, void *arg)
578 struct nl80211_rssi_rate *rr = arg;
580 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
581 struct nlattr *attr[NL80211_ATTR_MAX + 1];
582 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
583 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
585 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
586 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
587 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
588 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
589 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
590 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
591 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
592 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
593 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
594 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
595 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
598 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
599 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
600 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
601 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
602 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
605 nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
606 genlmsg_attrlen(gnlh, 0), NULL);
608 if( attr[NL80211_ATTR_STA_INFO] )
610 if( !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
611 attr[NL80211_ATTR_STA_INFO], stats_policy) )
613 if( sinfo[NL80211_STA_INFO_SIGNAL] )
615 dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
616 rr->rssi = rr->rssi ? (int8_t)((rr->rssi + dbm) / 2) : dbm;
619 if( sinfo[NL80211_STA_INFO_TX_BITRATE] )
621 if( !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
622 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy) )
624 if( rinfo[NL80211_RATE_INFO_BITRATE] )
626 mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
628 ? (int16_t)((rr->rate + mbit) / 2) : mbit;
638 int nl80211_get_bitrate(const char *ifname, int *buf)
640 struct nl80211_rssi_rate rr;
641 struct nl80211_msg_conveyor *req;
643 if( !wext_get_bitrate(ifname, buf) )
646 req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
652 nl80211_cb(req, nl80211_get_signal_cb, &rr);
658 *buf = (rr.rate * 100);
666 int nl80211_get_signal(const char *ifname, int *buf)
668 struct nl80211_rssi_rate rr;
669 struct nl80211_msg_conveyor *req;
671 if( !wext_get_signal(ifname, buf) )
674 req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
680 nl80211_cb(req, nl80211_get_signal_cb, &rr);
694 int nl80211_get_noise(const char *ifname, int *buf)
697 struct nl80211_msg_conveyor *req, *res;
698 struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
700 static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
701 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
702 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
705 req = nl80211_msg(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP);
708 res = nl80211_send(req);
711 if( res->attr[NL80211_ATTR_SURVEY_INFO] )
713 if( !nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
714 res->attr[NL80211_ATTR_SURVEY_INFO], sp) &&
715 si[NL80211_SURVEY_INFO_NOISE] )
717 *buf = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
729 int nl80211_get_quality(const char *ifname, int *buf)
733 if( wext_get_quality(ifname, buf) )
737 if( !nl80211_get_signal(ifname, &signal) )
739 /* A positive signal level is usually just a quality
740 * value, pass through as-is */
746 /* The cfg80211 wext compat layer assumes a signal range
747 * of -110 dBm to -40 dBm, the quality value is derived
748 * by adding 110 to the signal level */
753 else if( signal > -40 )
756 *buf = (signal + 110);
764 int nl80211_get_quality_max(const char *ifname, int *buf)
766 if( wext_get_quality_max(ifname, buf) )
767 /* The cfg80211 wext compat layer assumes a maximum
774 int nl80211_get_encryption(const char *ifname, char *buf)
779 struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
782 if( (res = nl80211_hostapd_info(ifname)) )
784 if( (val = nl80211_getval(ifname, res, "auth_algs")) && (val > 0) )
786 c->auth_suites |= IWINFO_KMGMT_NONE;
790 c->auth_algs |= IWINFO_AUTH_OPEN;
794 c->auth_algs |= IWINFO_AUTH_SHARED;
798 c->auth_algs |= IWINFO_AUTH_OPEN;
799 c->auth_algs |= IWINFO_AUTH_SHARED;
806 for( i = 0; i < 4; i++ )
808 snprintf(k, sizeof(k), "wep_key%d", i);
810 if( (val = nl80211_getval(ifname, res, k)) )
812 if( (strlen(val) == 5) || (strlen(val) == 10) )
813 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
815 else if( (strlen(val) == 13) || (strlen(val) == 26) )
816 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
820 c->group_ciphers = c->pair_ciphers;
826 if( (val = nl80211_getval(ifname, res, "wpa")) != NULL )
827 c->wpa_version = atoi(val);
830 val = nl80211_getval(ifname, res, "wpa_key_mgmt");
832 if( !val || strstr(val, "PSK") )
833 c->auth_suites |= IWINFO_KMGMT_PSK;
835 if( val && strstr(val, "EAP") )
836 c->auth_suites |= IWINFO_KMGMT_8021x;
838 if( val && strstr(val, "NONE") )
839 c->auth_suites |= IWINFO_KMGMT_NONE;
842 if( (val = nl80211_getval(ifname, res, "wpa_pairwise")) != NULL )
844 if( strstr(val, "TKIP") )
845 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
847 if( strstr(val, "CCMP") )
848 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
850 if( strstr(val, "NONE") )
851 c->pair_ciphers |= IWINFO_CIPHER_NONE;
855 c->group_ciphers = c->pair_ciphers;
856 c->enabled = (c->auth_algs || c->auth_suites) ? 1 : 0;
862 else if( (res = nl80211_wpasupp_info(ifname, "STATUS")) &&
863 (val = nl80211_getval(NULL, res, "pairwise_cipher")) )
866 if( strstr(val, "WEP") )
868 if( strstr(val, "WEP-40") )
869 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
871 else if( strstr(val, "WEP-104") )
872 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
875 c->group_ciphers = c->pair_ciphers;
877 c->auth_suites |= IWINFO_KMGMT_NONE;
878 c->auth_algs |= IWINFO_AUTH_OPEN; /* XXX: assumption */
884 if( strstr(val, "TKIP") )
885 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
887 else if( strstr(val, "CCMP") )
888 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
890 else if( strstr(val, "NONE") )
891 c->pair_ciphers |= IWINFO_CIPHER_NONE;
893 else if( strstr(val, "WEP-40") )
894 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
896 else if( strstr(val, "WEP-104") )
897 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
900 if( (val = nl80211_getval(NULL, res, "group_cipher")) )
902 if( strstr(val, "TKIP") )
903 c->group_ciphers |= IWINFO_CIPHER_TKIP;
905 else if( strstr(val, "CCMP") )
906 c->group_ciphers |= IWINFO_CIPHER_CCMP;
908 else if( strstr(val, "NONE") )
909 c->group_ciphers |= IWINFO_CIPHER_NONE;
911 else if( strstr(val, "WEP-40") )
912 c->group_ciphers |= IWINFO_CIPHER_WEP40;
914 else if( strstr(val, "WEP-104") )
915 c->group_ciphers |= IWINFO_CIPHER_WEP104;
919 if( (val = nl80211_getval(NULL, res, "key_mgmt")) )
921 if( strstr(val, "WPA2") )
924 else if( strstr(val, "WPA") )
928 if( strstr(val, "PSK") )
929 c->auth_suites |= IWINFO_KMGMT_PSK;
931 else if( strstr(val, "EAP") || strstr(val, "802.1X") )
932 c->auth_suites |= IWINFO_KMGMT_8021x;
934 else if( strstr(val, "NONE") )
935 c->auth_suites |= IWINFO_KMGMT_NONE;
938 c->enabled = (c->wpa_version && c->auth_suites) ? 1 : 0;
948 static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
950 struct nl80211_assoc_count *ac = arg;
951 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
952 struct nlattr *attr[NL80211_ATTR_MAX + 1];
953 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
955 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
956 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
957 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
958 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
959 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
960 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
961 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
962 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
963 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
964 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
965 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
968 nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
969 genlmsg_attrlen(gnlh, 0), NULL);
971 if( attr[NL80211_ATTR_MAC] )
972 memcpy(ac->entry->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
974 if( attr[NL80211_ATTR_STA_INFO] )
976 if( !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
977 attr[NL80211_ATTR_STA_INFO], stats_policy) )
979 if( sinfo[NL80211_STA_INFO_SIGNAL] )
980 ac->entry->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
984 ac->entry->noise = ac->noise;
991 int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
993 struct nl80211_assoc_count ac;
994 struct nl80211_msg_conveyor *req;
996 nl80211_get_noise(ifname, &ac.noise);
998 req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
1002 ac.entry = (struct iwinfo_assoclist_entry *)buf;
1004 nl80211_cb(req, nl80211_get_assoclist_cb, &ac);
1008 *len = (ac.count * sizeof(struct iwinfo_assoclist_entry));
1015 int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
1017 int ch_cur, ch_cmp, bands_remain, freqs_remain;
1018 int dbm_max = -1, dbm_cur, dbm_cnt;
1019 struct nl80211_msg_conveyor *req, *res;
1020 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
1021 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
1022 struct nlattr *band, *freq;
1023 struct iwinfo_txpwrlist_entry entry;
1025 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1026 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1027 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1028 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1029 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1030 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1031 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
1034 if( nl80211_get_channel(ifname, &ch_cur) )
1037 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
1040 res = nl80211_send(req);
1043 nla_for_each_nested(band,
1044 res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
1046 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
1047 nla_len(band), NULL);
1049 nla_for_each_nested(freq,
1050 bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
1052 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
1053 nla_data(freq), nla_len(freq), freq_policy);
1055 ch_cmp = nl80211_freq2channel(
1056 nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]));
1058 if( (!ch_cur || (ch_cmp == ch_cur)) &&
1059 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] )
1061 dbm_max = (int)(0.01 * nla_get_u32(
1062 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
1076 for( dbm_cur = 0, dbm_cnt = 0;
1078 dbm_cur += 2, dbm_cnt++ )
1080 entry.dbm = dbm_cur;
1081 entry.mw = wext_dbm2mw(dbm_cur);
1083 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
1086 entry.dbm = dbm_max;
1087 entry.mw = wext_dbm2mw(dbm_max);
1089 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
1092 *len = dbm_cnt * sizeof(entry);
1099 static void nl80211_get_scancrypto(const char *spec,
1100 struct iwinfo_crypto_entry *c)
1102 if( strstr(spec, "OPEN") )
1110 if( strstr(spec, "WPA2-") && strstr(spec, "WPA-") )
1113 else if( strstr(spec, "WPA2") )
1116 else if( strstr(spec, "WPA") )
1119 else if( strstr(spec, "WEP") )
1120 c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
1123 if( strstr(spec, "PSK") )
1124 c->auth_suites |= IWINFO_KMGMT_PSK;
1126 if( strstr(spec, "802.1X") || strstr(spec, "EAP") )
1127 c->auth_suites |= IWINFO_KMGMT_8021x;
1129 if( strstr(spec, "WPA-NONE") )
1130 c->auth_suites |= IWINFO_KMGMT_NONE;
1133 if( strstr(spec, "TKIP") )
1134 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
1136 if( strstr(spec, "CCMP") )
1137 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
1139 if( strstr(spec, "WEP-40") )
1140 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
1142 if( strstr(spec, "WEP-104") )
1143 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
1145 c->group_ciphers = c->pair_ciphers;
1149 int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
1151 int freq, rssi, qmax, count;
1154 char ssid[128] = { 0 };
1155 char bssid[18] = { 0 };
1156 char cipher[256] = { 0 };
1158 /* Got a radioX pseudo interface, find some interface on it or create one */
1159 if( !strncmp(ifname, "radio", 5) )
1161 /* Reuse existing interface */
1162 if( (res = nl80211_phy2ifname(ifname)) != NULL )
1164 return nl80211_get_scanlist(res, buf, len);
1167 /* Need to spawn a temporary iface for scanning */
1168 else if( (res = nl80211_add_tempif(ifname)) != NULL )
1170 count = nl80211_get_scanlist(res, buf, len);
1171 nl80211_del_tempif(res);
1176 struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
1178 /* WPA supplicant */
1179 if( (res = nl80211_wpasupp_info(ifname, "SCAN")) &&
1180 !strcmp(res, "OK\n") )
1184 if( (res = nl80211_wpasupp_info(ifname, "SCAN_RESULTS")) )
1186 nl80211_get_quality_max(ifname, &qmax);
1188 /* skip header line */
1189 while( *res++ != '\n' );
1193 while( sscanf(res, "%17s %d %d %255s %127[^\n]\n",
1194 bssid, &freq, &rssi, cipher, ssid) > 0 )
1197 e->mac[0] = strtol(&bssid[0], NULL, 16);
1198 e->mac[1] = strtol(&bssid[3], NULL, 16);
1199 e->mac[2] = strtol(&bssid[6], NULL, 16);
1200 e->mac[3] = strtol(&bssid[9], NULL, 16);
1201 e->mac[4] = strtol(&bssid[12], NULL, 16);
1202 e->mac[5] = strtol(&bssid[15], NULL, 16);
1205 memcpy(e->ssid, ssid,
1206 min(strlen(ssid), sizeof(e->ssid) - 1));
1208 /* Mode (assume master) */
1209 sprintf((char *)e->mode, "Master");
1212 e->channel = nl80211_freq2channel(freq);
1220 /* The cfg80211 wext compat layer assumes a signal range
1221 * of -110 dBm to -40 dBm, the quality value is derived
1222 * by adding 110 to the signal level */
1225 else if( rssi > -40 )
1228 e->quality = (rssi + 110);
1236 e->quality_max = qmax;
1239 nl80211_get_scancrypto(cipher, &e->crypto);
1241 /* advance to next line */
1242 while( *res && *res++ != '\n' );
1248 *len = count * sizeof(struct iwinfo_scanlist_entry);
1256 if( (res = nl80211_ifname2phy(ifname)) != NULL )
1258 /* Got a temp interface, don't create yet another one */
1259 if( !strncmp(ifname, "tmp.", 4) )
1261 sprintf(cmd, "ifconfig %s up 2>/dev/null", ifname);
1262 if( WEXITSTATUS(system(cmd)) )
1265 wext_get_scanlist(ifname, buf, len);
1267 sprintf(cmd, "ifconfig %s down 2>/dev/null", ifname);
1268 (void) WEXITSTATUS(system(cmd));
1273 /* Spawn a new scan interface */
1276 sprintf(cmd, "ifconfig %s down 2>/dev/null", ifname);
1277 if( WEXITSTATUS(system(cmd)) )
1280 sprintf(cmd, "iw phy %s interface add scan.%s "
1281 "type station 2>/dev/null", res, ifname);
1282 if( WEXITSTATUS(system(cmd)) )
1285 sprintf(cmd, "ifconfig scan.%s up 2>/dev/null", ifname);
1286 if( WEXITSTATUS(system(cmd)) )
1289 sprintf(cmd, "scan.%s", ifname);
1290 wext_get_scanlist(cmd, buf, len);
1293 sprintf(cmd, "ifconfig scan.%s down 2>/dev/null", ifname);
1294 (void) WEXITSTATUS(system(cmd));
1296 sprintf(cmd, "iw dev scan.%s del 2>/dev/null", ifname);
1297 (void) WEXITSTATUS(system(cmd));
1299 sprintf(cmd, "ifconfig %s up 2>/dev/null", ifname);
1300 (void) WEXITSTATUS(system(cmd));
1302 sprintf(cmd, "killall -HUP hostapd 2>/dev/null");
1303 (void) WEXITSTATUS(system(cmd));
1313 int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
1315 int count = 0, bands_remain, freqs_remain;
1316 struct nl80211_msg_conveyor *req, *res;
1317 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
1318 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
1319 struct nlattr *band, *freq;
1320 struct iwinfo_freqlist_entry *e = (struct iwinfo_freqlist_entry *)buf;
1322 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
1325 res = nl80211_send(req);
1328 nla_for_each_nested(band,
1329 res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
1331 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
1332 nla_len(band), NULL);
1334 nla_for_each_nested(freq,
1335 bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
1337 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
1338 nla_data(freq), nla_len(freq), NULL);
1340 if( !freqs[NL80211_FREQUENCY_ATTR_FREQ] ||
1341 freqs[NL80211_FREQUENCY_ATTR_DISABLED] )
1344 e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
1345 e->channel = nl80211_freq2channel(e->mhz);
1348 freqs[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] ||
1349 freqs[NL80211_FREQUENCY_ATTR_NO_IBSS] ||
1350 freqs[NL80211_FREQUENCY_ATTR_RADAR]
1364 *len = count * sizeof(struct iwinfo_freqlist_entry);
1371 int nl80211_get_country(const char *ifname, char *buf)
1374 struct nl80211_msg_conveyor *req, *res;
1376 req = nl80211_msg(ifname, NL80211_CMD_GET_REG, 0);
1379 res = nl80211_send(req);
1382 if( res->attr[NL80211_ATTR_REG_ALPHA2] )
1384 memcpy(buf, nla_data(res->attr[NL80211_ATTR_REG_ALPHA2]), 2);
1395 int nl80211_get_countrylist(const char *ifname, char *buf, int *len)
1398 struct iwinfo_iso3166_label *l;
1399 struct iwinfo_country_entry *e = (struct iwinfo_country_entry *)buf;
1401 for( l = ISO3166_Names, count = 0; l->iso3166; l++, e++, count++ )
1403 e->iso3166 = l->iso3166;
1404 e->ccode[0] = (l->iso3166 / 256);
1405 e->ccode[1] = (l->iso3166 % 256);
1408 *len = (count * sizeof(struct iwinfo_country_entry));
1412 int nl80211_get_hwmodelist(const char *ifname, int *buf)
1414 int bands_remain, freqs_remain;
1415 struct nl80211_msg_conveyor *req, *res;
1416 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
1417 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
1418 struct nlattr *band, *freq;
1421 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
1424 res = nl80211_send(req);
1427 nla_for_each_nested(band,
1428 res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
1430 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
1431 nla_len(band), NULL);
1433 if( bands[NL80211_BAND_ATTR_HT_CAPA] )
1434 caps = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]);
1436 /* Treat HT20/HT40 as 11n */
1437 if( caps & (1 << 1) )
1438 *buf |= IWINFO_80211_N;
1440 nla_for_each_nested(freq,
1441 bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
1443 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
1444 nla_data(freq), nla_len(freq), NULL);
1446 if( !freqs[NL80211_FREQUENCY_ATTR_FREQ] )
1449 if( nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]) < 2485 )
1451 *buf |= IWINFO_80211_B;
1452 *buf |= IWINFO_80211_G;
1456 *buf |= IWINFO_80211_A;
1465 return *buf ? 0 : -1;
1468 int nl80211_get_mbssid_support(const char *ifname, int *buf)
1470 /* We assume that multi bssid is always possible */