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");
80 if( nls && nls->nl_sock )
81 nl_socket_free(nls->nl_sock);
83 if( nls && nls->nl_cache )
84 nl_cache_free(nls->nl_cache);
94 static int nl80211_msg_error(struct sockaddr_nl *nla,
95 struct nlmsgerr *err, void *arg)
102 static int nl80211_msg_finish(struct nl_msg *msg, void *arg)
109 static int nl80211_msg_ack(struct nl_msg *msg, void *arg)
116 static int nl80211_msg_response(struct nl_msg *msg, void *arg)
118 struct nl80211_msg_conveyor *cv = arg;
123 cv->hdr = nlmsg_data(nlmsg_hdr(cv->msg));
125 nla_parse(cv->attr, NL80211_ATTR_MAX,
126 genlmsg_attrdata(cv->hdr, 0),
127 genlmsg_attrlen(cv->hdr, 0), NULL);
132 static void nl80211_free(struct nl80211_msg_conveyor *cv)
147 static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname, int cmd, int flags)
149 static struct nl80211_msg_conveyor cv;
152 struct nl_msg *req = NULL;
153 struct nl_cb *cb = NULL;
155 if( nl80211_init() < 0 )
158 if( !strncmp(ifname, "mon.", 4) )
159 ifidx = if_nametoindex(&ifname[4]);
161 ifidx = if_nametoindex(ifname);
170 cb = nl_cb_alloc(NL_CB_DEFAULT);
174 genlmsg_put(req, 0, 0, genl_family_get_id(nls->nl80211), 0,
177 NLA_PUT_U32(req, NL80211_ATTR_IFINDEX, ifidx);
198 static void nl80211_cb(struct nl80211_msg_conveyor *cv,
199 int (*cb)(struct nl_msg *, void *), void *arg)
202 nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, cb, arg);
205 static struct nl80211_msg_conveyor * nl80211_send(struct nl80211_msg_conveyor *cv)
207 static struct nl80211_msg_conveyor rcv;
211 nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_msg_response, &rcv);
213 if( nl_send_auto_complete(nls->nl_sock, cv->msg) < 0 )
216 nl_cb_err(cv->cb, NL_CB_CUSTOM, nl80211_msg_error, &err);
217 nl_cb_set(cv->cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_msg_finish, &err);
218 nl_cb_set(cv->cb, NL_CB_ACK, NL_CB_CUSTOM, nl80211_msg_ack, &err);
221 nl_recvmsgs(nls->nl_sock, cv->cb);
232 static int nl80211_freq2channel(int freq)
238 return (freq - 2407) / 5;
240 return (freq / 5) - 1000;
243 static char * nl80211_getval(const char *buf, const char *key)
246 char lkey[64] = { 0 };
247 const char *ln = buf;
248 static char lval[256] = { 0 };
250 for( i = 0, len = strlen(buf); i < len; i++ )
252 if( !lkey[0] && (buf[i] == ' ' || buf[i] == '\t') )
256 else if( !lkey[0] && (buf[i] == '=') )
258 if( (&buf[i] - ln) > 0 )
259 memcpy(lkey, ln, min(sizeof(lkey) - 1, &buf[i] - ln));
261 else if( buf[i] == '\n' )
263 if( lkey[0] && !strcmp(lkey, key) )
265 memcpy(lval, ln + strlen(lkey) + 1,
266 min(sizeof(lval) - 1, &buf[i] - ln - strlen(lkey) - 1));
272 memset(lkey, 0, sizeof(lkey));
273 memset(lval, 0, sizeof(lval));
280 static char * nl80211_ifname2phy(const char *ifname)
282 static char phy[32] = { 0 };
283 struct nl80211_msg_conveyor *req, *res;
285 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
288 res = nl80211_send(req);
291 if( res->attr[NL80211_ATTR_WIPHY_NAME] )
293 snprintf(phy, sizeof(phy), "%s",
294 nla_get_string(res->attr[NL80211_ATTR_WIPHY_NAME]));
301 return phy[0] ? phy : NULL;
304 static char * nl80211_hostapd_info(const char *ifname)
307 char path[32] = { 0 };
308 static char buf[4096] = { 0 };
311 if( (phy = nl80211_ifname2phy(ifname)) != NULL )
313 snprintf(path, sizeof(path), "/var/run/hostapd-%s.conf", phy);
315 if( (conf = fopen(path, "r")) != NULL )
317 fread(buf, sizeof(buf) - 1, 1, conf);
327 static char * nl80211_wpasupp_info(const char *ifname, const char *cmd)
331 size_t remote_length, local_length;
332 static char buffer[1024] = { 0 };
334 struct timeval tv = { 2, 0 };
335 struct sockaddr_un local = { 0 };
336 struct sockaddr_un remote = { 0 };
340 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
344 remote.sun_family = AF_UNIX;
345 remote_length = sizeof(remote.sun_family) + sprintf(remote.sun_path,
346 "/var/run/wpa_supplicant-%s/%s", ifname, ifname);
348 if( fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC) < 0 )
351 if( connect(sock, (struct sockaddr *) &remote, remote_length) )
354 local.sun_family = AF_UNIX;
355 local_length = sizeof(local.sun_family) + sprintf(local.sun_path,
356 "/var/run/iwinfo-%s-%d", ifname, getpid());
358 if( bind(sock, (struct sockaddr *) &local, local_length) )
361 send(sock, cmd, strlen(cmd), 0);
368 if( select(sock + 1, &rfds, NULL, NULL, &tv) < 0 )
371 if( !FD_ISSET(sock, &rfds) )
374 if( (len = recv(sock, buffer, sizeof(buffer), 0)) <= 0 )
379 if( buffer[0] != '<' )
387 unlink(local.sun_path);
393 int nl80211_probe(const char *ifname)
395 return !!nl80211_ifname2phy(ifname);
398 int nl80211_get_mode(const char *ifname, char *buf)
400 return wext_get_mode(ifname, buf);
403 int nl80211_get_ssid(const char *ifname, char *buf)
407 if( !wext_get_ssid(ifname, buf) )
411 else if( (ssid = nl80211_hostapd_info(ifname)) &&
412 (ssid = nl80211_getval(ssid, "ssid")) )
414 memcpy(buf, ssid, strlen(ssid));
421 int nl80211_get_bssid(const char *ifname, char *buf)
424 unsigned char mac[6];
426 if( !wext_get_bssid(ifname, buf) )
430 else if( (bssid = nl80211_hostapd_info(ifname)) &&
431 (bssid = nl80211_getval(bssid, "bssid")) )
433 mac[0] = strtol(&bssid[0], NULL, 16);
434 mac[1] = strtol(&bssid[3], NULL, 16);
435 mac[2] = strtol(&bssid[6], NULL, 16);
436 mac[3] = strtol(&bssid[9], NULL, 16);
437 mac[4] = strtol(&bssid[12], NULL, 16);
438 mac[5] = strtol(&bssid[15], NULL, 16);
440 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
441 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
449 int nl80211_get_channel(const char *ifname, int *buf)
451 return wext_get_channel(ifname, buf);
454 int nl80211_get_frequency(const char *ifname, int *buf)
456 return wext_get_frequency(ifname, buf);
459 int nl80211_get_txpower(const char *ifname, int *buf)
461 return wext_get_txpower(ifname, buf);
465 static int nl80211_get_signal_cb(struct nl_msg *msg, void *arg)
469 struct nl80211_rssi_rate *rr = arg;
471 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
472 struct nlattr *attr[NL80211_ATTR_MAX + 1];
473 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
474 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
476 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
477 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
478 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
479 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
480 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
481 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
482 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
483 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
484 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
485 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
486 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
489 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
490 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
491 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
492 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
493 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
496 nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
497 genlmsg_attrlen(gnlh, 0), NULL);
499 if( attr[NL80211_ATTR_STA_INFO] )
501 if( !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
502 attr[NL80211_ATTR_STA_INFO], stats_policy) )
504 if( sinfo[NL80211_STA_INFO_SIGNAL] )
506 dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
507 rr->rssi = rr->rssi ? (int8_t)((rr->rssi + dbm) / 2) : dbm;
510 if( sinfo[NL80211_STA_INFO_TX_BITRATE] )
512 if( !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
513 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy) )
515 if( rinfo[NL80211_RATE_INFO_BITRATE] )
517 mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
519 ? (int16_t)((rr->rate + mbit) / 2) : mbit;
529 int nl80211_get_bitrate(const char *ifname, int *buf)
531 struct nl80211_rssi_rate rr;
532 struct nl80211_msg_conveyor *req;
534 if( !wext_get_bitrate(ifname, buf) )
537 req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
543 nl80211_cb(req, nl80211_get_signal_cb, &rr);
549 *buf = (rr.rate * 100);
557 int nl80211_get_signal(const char *ifname, int *buf)
559 struct nl80211_rssi_rate rr;
560 struct nl80211_msg_conveyor *req;
562 if( !wext_get_signal(ifname, buf) )
565 req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
571 nl80211_cb(req, nl80211_get_signal_cb, &rr);
585 int nl80211_get_noise(const char *ifname, int *buf)
588 struct nl80211_msg_conveyor *req, *res;
589 struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
591 static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
592 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
593 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
596 req = nl80211_msg(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP);
599 res = nl80211_send(req);
602 if( res->attr[NL80211_ATTR_SURVEY_INFO] )
604 if( !nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
605 res->attr[NL80211_ATTR_SURVEY_INFO], sp) &&
606 si[NL80211_SURVEY_INFO_NOISE] )
608 *buf = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
620 int nl80211_get_quality(const char *ifname, int *buf)
624 if( wext_get_quality(ifname, buf) )
628 if( !nl80211_get_signal(ifname, &signal) )
630 /* A positive signal level is usually just a quality
631 * value, pass through as-is */
637 /* The cfg80211 wext compat layer assumes a signal range
638 * of -110 dBm to -40 dBm, the quality value is derived
639 * by adding 110 to the signal level */
644 else if( signal > -40 )
647 *buf = (signal + 110);
655 int nl80211_get_quality_max(const char *ifname, int *buf)
657 if( wext_get_quality_max(ifname, buf) )
658 /* The cfg80211 wext compat layer assumes a maximum
665 int nl80211_get_encryption(const char *ifname, char *buf)
670 struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
673 if( (res = nl80211_hostapd_info(ifname)) &&
674 nl80211_getval(res, "interface") )
676 if( (val = nl80211_getval(res, "auth_algs")) && (val > 0) )
678 c->auth_suites |= IWINFO_KMGMT_NONE;
682 c->auth_algs |= IWINFO_AUTH_OPEN;
686 c->auth_algs |= IWINFO_AUTH_SHARED;
690 c->auth_algs |= IWINFO_AUTH_OPEN;
691 c->auth_algs |= IWINFO_AUTH_SHARED;
698 for( i = 0; i < 4; i++ )
700 snprintf(k, sizeof(k), "wep_key%d", i);
702 if( (val = nl80211_getval(res, k)) )
704 if( (strlen(val) == 5) || (strlen(val) == 10) )
705 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
707 else if( (strlen(val) == 13) || (strlen(val) == 26) )
708 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
712 c->group_ciphers = c->pair_ciphers;
718 if( (val = nl80211_getval(res, "wpa")) != NULL )
719 c->wpa_version = atoi(val);
722 val = nl80211_getval(res, "wpa_key_mgmt");
724 if( !val || strstr(val, "PSK") )
725 c->auth_suites |= IWINFO_KMGMT_PSK;
727 if( val && strstr(val, "EAP") )
728 c->auth_suites |= IWINFO_KMGMT_8021x;
730 if( val && strstr(val, "NONE") )
731 c->auth_suites |= IWINFO_KMGMT_NONE;
734 if( (val = nl80211_getval(res, "wpa_pairwise")) != NULL )
736 if( strstr(val, "TKIP") )
737 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
739 if( strstr(val, "CCMP") )
740 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
742 if( strstr(val, "NONE") )
743 c->pair_ciphers |= IWINFO_CIPHER_NONE;
747 c->group_ciphers = c->pair_ciphers;
748 c->enabled = (c->auth_algs || c->auth_suites) ? 1 : 0;
754 else if( (res = nl80211_wpasupp_info(ifname, "STATUS")) &&
755 (val = nl80211_getval(res, "pairwise_cipher")) )
758 if( strstr(val, "WEP") )
760 if( strstr(val, "WEP-40") )
761 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
763 else if( strstr(val, "WEP-104") )
764 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
767 c->group_ciphers = c->pair_ciphers;
769 c->auth_suites |= IWINFO_KMGMT_NONE;
770 c->auth_algs |= IWINFO_AUTH_OPEN; /* XXX: assumption */
776 if( strstr(val, "TKIP") )
777 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
779 else if( strstr(val, "CCMP") )
780 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
782 else if( strstr(val, "NONE") )
783 c->pair_ciphers |= IWINFO_CIPHER_NONE;
785 else if( strstr(val, "WEP-40") )
786 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
788 else if( strstr(val, "WEP-104") )
789 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
792 if( (val = nl80211_getval(res, "group_cipher")) )
794 if( strstr(val, "TKIP") )
795 c->group_ciphers |= IWINFO_CIPHER_TKIP;
797 else if( strstr(val, "CCMP") )
798 c->group_ciphers |= IWINFO_CIPHER_CCMP;
800 else if( strstr(val, "NONE") )
801 c->group_ciphers |= IWINFO_CIPHER_NONE;
803 else if( strstr(val, "WEP-40") )
804 c->group_ciphers |= IWINFO_CIPHER_WEP40;
806 else if( strstr(val, "WEP-104") )
807 c->group_ciphers |= IWINFO_CIPHER_WEP104;
811 if( (val = nl80211_getval(res, "key_mgmt")) )
813 if( strstr(val, "WPA2") )
816 else if( strstr(val, "WPA") )
820 if( strstr(val, "PSK") )
821 c->auth_suites |= IWINFO_KMGMT_PSK;
823 else if( strstr(val, "EAP") || strstr(val, "802.1X") )
824 c->auth_suites |= IWINFO_KMGMT_8021x;
826 else if( strstr(val, "NONE") )
827 c->auth_suites |= IWINFO_KMGMT_NONE;
830 c->enabled = (c->wpa_version && c->auth_suites) ? 1 : 0;
840 static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
842 struct nl80211_assoc_count *ac = arg;
843 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
844 struct nlattr *attr[NL80211_ATTR_MAX + 1];
845 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
847 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
848 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
849 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
850 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
851 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
852 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
853 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
854 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
855 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
856 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
857 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
860 nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
861 genlmsg_attrlen(gnlh, 0), NULL);
863 if( attr[NL80211_ATTR_MAC] )
864 memcpy(ac->entry->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
866 if( attr[NL80211_ATTR_STA_INFO] )
868 if( !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
869 attr[NL80211_ATTR_STA_INFO], stats_policy) )
871 if( sinfo[NL80211_STA_INFO_SIGNAL] )
872 ac->entry->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
876 ac->entry->noise = ac->noise;
883 int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
885 struct nl80211_assoc_count ac;
886 struct nl80211_msg_conveyor *req;
888 nl80211_get_noise(ifname, &ac.noise);
890 req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
894 ac.entry = (struct iwinfo_assoclist_entry *)buf;
896 nl80211_cb(req, nl80211_get_assoclist_cb, &ac);
900 *len = (ac.count * sizeof(struct iwinfo_assoclist_entry));
907 int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
909 int ch_cur, ch_cmp, bands_remain, freqs_remain;
910 int dbm_max = -1, dbm_cur, dbm_cnt;
911 struct nl80211_msg_conveyor *req, *res;
912 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
913 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
914 struct nlattr *band, *freq;
915 struct iwinfo_txpwrlist_entry entry;
917 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
918 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
919 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
920 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
921 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
922 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
923 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
926 if( nl80211_get_channel(ifname, &ch_cur) )
929 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
932 res = nl80211_send(req);
935 nla_for_each_nested(band,
936 res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
938 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
939 nla_len(band), NULL);
941 nla_for_each_nested(freq,
942 bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
944 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
945 nla_data(freq), nla_len(freq), freq_policy);
947 ch_cmp = nl80211_freq2channel(
948 nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]));
950 if( (ch_cmp == ch_cur) &&
951 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] )
953 dbm_max = (int)(0.01 * nla_get_u32(
954 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
968 for( dbm_cur = 0, dbm_cnt = 0;
970 dbm_cur += 2, dbm_cnt++ )
973 entry.mw = wext_dbm2mw(dbm_cur);
975 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
979 entry.mw = wext_dbm2mw(dbm_max);
981 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
984 *len = dbm_cnt * sizeof(entry);
991 static void nl80211_get_scancrypto(const char *spec,
992 struct iwinfo_crypto_entry *c)
994 if( strstr(spec, "OPEN") )
1002 if( strstr(spec, "WPA2-") && strstr(spec, "WPA-") )
1005 else if( strstr(spec, "WPA2") )
1008 else if( strstr(spec, "WPA") )
1011 else if( strstr(spec, "WEP") )
1012 c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
1015 if( strstr(spec, "PSK") )
1016 c->auth_suites |= IWINFO_KMGMT_PSK;
1018 if( strstr(spec, "802.1X") || strstr(spec, "EAP") )
1019 c->auth_suites |= IWINFO_KMGMT_8021x;
1021 if( strstr(spec, "WPA-NONE") )
1022 c->auth_suites |= IWINFO_KMGMT_NONE;
1025 if( strstr(spec, "TKIP") )
1026 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
1028 if( strstr(spec, "CCMP") )
1029 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
1031 if( strstr(spec, "WEP-40") )
1032 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
1034 if( strstr(spec, "WEP-104") )
1035 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
1037 c->group_ciphers = c->pair_ciphers;
1041 int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
1043 int freq, rssi, qmax, count;
1046 char ssid[128] = { 0 };
1047 char bssid[18] = { 0 };
1048 char cipher[256] = { 0 };
1050 struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
1052 /* WPA supplicant */
1053 if( (res = nl80211_wpasupp_info(ifname, "SCAN")) &&
1054 !strcmp(res, "OK\n") )
1058 if( (res = nl80211_wpasupp_info(ifname, "SCAN_RESULTS")) )
1060 nl80211_get_quality_max(ifname, &qmax);
1062 /* skip header line */
1063 while( *res++ != '\n' );
1067 while( sscanf(res, "%17s %d %d %255s %127[^\n]\n",
1068 bssid, &freq, &rssi, cipher, ssid) > 0 )
1071 e->mac[0] = strtol(&bssid[0], NULL, 16);
1072 e->mac[1] = strtol(&bssid[3], NULL, 16);
1073 e->mac[2] = strtol(&bssid[6], NULL, 16);
1074 e->mac[3] = strtol(&bssid[9], NULL, 16);
1075 e->mac[4] = strtol(&bssid[12], NULL, 16);
1076 e->mac[5] = strtol(&bssid[15], NULL, 16);
1079 memcpy(e->ssid, ssid,
1080 min(strlen(ssid), sizeof(e->ssid) - 1));
1082 /* Mode (assume master) */
1083 sprintf((char *)e->mode, "Master");
1086 e->channel = nl80211_freq2channel(freq);
1094 /* The cfg80211 wext compat layer assumes a signal range
1095 * of -110 dBm to -40 dBm, the quality value is derived
1096 * by adding 110 to the signal level */
1099 else if( rssi > -40 )
1102 e->quality = (rssi + 110);
1110 e->quality_max = qmax;
1113 nl80211_get_scancrypto(cipher, &e->crypto);
1115 /* advance to next line */
1116 while( *res && *res++ != '\n' );
1122 *len = count * sizeof(struct iwinfo_scanlist_entry);
1130 if( (res = nl80211_ifname2phy(ifname)) != NULL )
1133 * This is a big ugly hack, just look away.
1136 sprintf(cmd, "ifconfig %s down 2>/dev/null", ifname);
1137 if( WEXITSTATUS(system(cmd)) )
1140 sprintf(cmd, "iw phy %s interface add scan.%s "
1141 "type station 2>/dev/null", res, ifname);
1142 if( WEXITSTATUS(system(cmd)) )
1145 sprintf(cmd, "ifconfig scan.%s up 2>/dev/null", ifname);
1146 if( WEXITSTATUS(system(cmd)) )
1149 sprintf(cmd, "scan.%s", ifname);
1150 wext_get_scanlist(cmd, buf, len);
1153 sprintf(cmd, "ifconfig scan.%s down 2>/dev/null", ifname);
1154 (void) WEXITSTATUS(system(cmd));
1156 sprintf(cmd, "iw dev scan.%s del 2>/dev/null", ifname);
1157 (void) WEXITSTATUS(system(cmd));
1159 sprintf(cmd, "ifconfig %s up 2>/dev/null", ifname);
1160 (void) WEXITSTATUS(system(cmd));
1162 sprintf(cmd, "killall -HUP hostapd 2>/dev/null");
1163 (void) WEXITSTATUS(system(cmd));
1172 int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
1174 return wext_get_freqlist(ifname, buf, len);
1177 int nl80211_get_country(const char *ifname, char *buf)
1180 struct nl80211_msg_conveyor *req, *res;
1182 req = nl80211_msg(ifname, NL80211_CMD_GET_REG, 0);
1185 res = nl80211_send(req);
1188 if( res->attr[NL80211_ATTR_REG_ALPHA2] )
1190 memcpy(buf, nla_data(res->attr[NL80211_ATTR_REG_ALPHA2]), 2);
1201 int nl80211_get_countrylist(const char *ifname, char *buf, int *len)
1204 struct iwinfo_iso3166_label *l;
1205 struct iwinfo_country_entry *e = (struct iwinfo_country_entry *)buf;
1207 for( l = ISO3166_Names, count = 0; l->iso3166; l++, e++, count++ )
1209 e->iso3166 = l->iso3166;
1210 e->ccode[0] = (l->iso3166 / 256);
1211 e->ccode[1] = (l->iso3166 % 256);
1214 *len = (count * sizeof(struct iwinfo_country_entry));
1218 int nl80211_get_mbssid_support(const char *ifname, int *buf)
1220 /* We assume that multi bssid is always possible */