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;
32 static int nl80211_ioctlsock = -1;
34 static int nl80211_init(void)
40 nl80211_ioctlsock = socket(AF_INET, SOCK_DGRAM, 0);
41 if( nl80211_ioctlsock < 0 )
46 else if( fcntl(nl80211_ioctlsock, F_SETFD,
47 fcntl(nl80211_ioctlsock, F_GETFD) | FD_CLOEXEC) < 0 )
53 nls = malloc(sizeof(struct nl80211_state));
59 nls->nl_sock = nl_socket_alloc();
65 if( genl_connect(nls->nl_sock)) {
70 fd = nl_socket_get_fd(nls->nl_sock);
71 if( fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0 )
77 if( genl_ctrl_alloc_cache(nls->nl_sock, &nls->nl_cache)) {
82 nls->nl80211 = genl_ctrl_search_by_name(nls->nl_cache, "nl80211");
98 static int nl80211_msg_error(struct sockaddr_nl *nla,
99 struct nlmsgerr *err, void *arg)
106 static int nl80211_msg_finish(struct nl_msg *msg, void *arg)
113 static int nl80211_msg_ack(struct nl_msg *msg, void *arg)
120 static int nl80211_msg_response(struct nl_msg *msg, void *arg)
122 struct nl80211_msg_conveyor *cv = arg;
127 cv->hdr = nlmsg_data(nlmsg_hdr(cv->msg));
129 nla_parse(cv->attr, NL80211_ATTR_MAX,
130 genlmsg_attrdata(cv->hdr, 0),
131 genlmsg_attrlen(cv->hdr, 0), NULL);
136 static void nl80211_free(struct nl80211_msg_conveyor *cv)
151 static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname, int cmd, int flags)
153 static struct nl80211_msg_conveyor cv;
155 int ifidx = -1, phyidx = -1;
156 struct nl_msg *req = NULL;
157 struct nl_cb *cb = NULL;
159 if( nl80211_init() < 0 )
162 if( !strncmp(ifname, "phy", 3) )
163 phyidx = atoi(&ifname[3]);
164 else if( !strncmp(ifname, "radio", 5) )
165 phyidx = atoi(&ifname[5]);
166 else if( !strncmp(ifname, "mon.", 4) )
167 ifidx = if_nametoindex(&ifname[4]);
169 ifidx = if_nametoindex(ifname);
171 if( (ifidx < 0) && (phyidx < 0) )
178 cb = nl_cb_alloc(NL_CB_DEFAULT);
182 genlmsg_put(req, 0, 0, genl_family_get_id(nls->nl80211), 0,
186 NLA_PUT_U32(req, NL80211_ATTR_IFINDEX, ifidx);
189 NLA_PUT_U32(req, NL80211_ATTR_WIPHY, phyidx);
210 static void nl80211_cb(struct nl80211_msg_conveyor *cv,
211 int (*cb)(struct nl_msg *, void *), void *arg)
214 nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, cb, arg);
217 static struct nl80211_msg_conveyor * nl80211_send(struct nl80211_msg_conveyor *cv)
219 static struct nl80211_msg_conveyor rcv;
223 nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_msg_response, &rcv);
225 if( nl_send_auto_complete(nls->nl_sock, cv->msg) < 0 )
228 nl_cb_err(cv->cb, NL_CB_CUSTOM, nl80211_msg_error, &err);
229 nl_cb_set(cv->cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_msg_finish, &err);
230 nl_cb_set(cv->cb, NL_CB_ACK, NL_CB_CUSTOM, nl80211_msg_ack, &err);
233 nl_recvmsgs(nls->nl_sock, cv->cb);
244 static int nl80211_freq2channel(int freq)
250 return (freq - 2407) / 5;
252 return (freq / 5) - 1000;
255 static char * nl80211_getval(const char *ifname, const char *buf, const char *key)
258 char lkey[64] = { 0 };
259 const char *ln = buf;
260 static char lval[256] = { 0 };
262 int matched_if = ifname ? 0 : 1;
265 for( i = 0, len = strlen(buf); i < len; i++ )
267 if( !lkey[0] && (buf[i] == ' ' || buf[i] == '\t') )
271 else if( !lkey[0] && (buf[i] == '=') )
273 if( (&buf[i] - ln) > 0 )
274 memcpy(lkey, ln, min(sizeof(lkey) - 1, &buf[i] - ln));
276 else if( buf[i] == '\n' )
280 memcpy(lval, ln + strlen(lkey) + 1,
281 min(sizeof(lval) - 1, &buf[i] - ln - strlen(lkey) - 1));
283 if( (ifname != NULL ) &&
284 (!strcmp(lkey, "interface") || !strcmp(lkey, "bss")) )
286 matched_if = !strcmp(lval, ifname);
288 else if( matched_if && !strcmp(lkey, key) )
295 memset(lkey, 0, sizeof(lkey));
296 memset(lval, 0, sizeof(lval));
303 static char * nl80211_ifname2phy(const char *ifname)
305 static char phy[32] = { 0 };
306 struct nl80211_msg_conveyor *req, *res;
308 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
311 res = nl80211_send(req);
314 if( res->attr[NL80211_ATTR_WIPHY_NAME] )
316 snprintf(phy, sizeof(phy), "%s",
317 nla_get_string(res->attr[NL80211_ATTR_WIPHY_NAME]));
324 return phy[0] ? phy : NULL;
327 static char * nl80211_hostapd_info(const char *ifname)
330 char path[32] = { 0 };
331 static char buf[4096] = { 0 };
334 if( (phy = nl80211_ifname2phy(ifname)) != NULL )
336 snprintf(path, sizeof(path), "/var/run/hostapd-%s.conf", phy);
338 if( (conf = fopen(path, "r")) != NULL )
340 fread(buf, sizeof(buf) - 1, 1, conf);
350 static char * nl80211_wpasupp_info(const char *ifname, const char *cmd)
354 size_t remote_length, local_length;
355 static char buffer[1024] = { 0 };
357 struct timeval tv = { 2, 0 };
358 struct sockaddr_un local = { 0 };
359 struct sockaddr_un remote = { 0 };
363 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
367 remote.sun_family = AF_UNIX;
368 remote_length = sizeof(remote.sun_family) + sprintf(remote.sun_path,
369 "/var/run/wpa_supplicant-%s/%s", ifname, ifname);
371 if( fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC) < 0 )
374 if( connect(sock, (struct sockaddr *) &remote, remote_length) )
377 local.sun_family = AF_UNIX;
378 local_length = sizeof(local.sun_family) + sprintf(local.sun_path,
379 "/var/run/iwinfo-%s-%d", ifname, getpid());
381 if( bind(sock, (struct sockaddr *) &local, local_length) )
384 send(sock, cmd, strlen(cmd), 0);
391 if( select(sock + 1, &rfds, NULL, NULL, &tv) < 0 )
394 if( !FD_ISSET(sock, &rfds) )
397 if( (len = recv(sock, buffer, sizeof(buffer), 0)) <= 0 )
402 if( buffer[0] != '<' )
411 if( local.sun_family )
412 unlink(local.sun_path);
417 static char * nl80211_phy2ifname(const char *ifname)
421 static char nif[IFNAMSIZ] = { 0 };
426 if( !strncmp(ifname, "phy", 3) )
427 phyidx = atoi(&ifname[3]);
428 else if( !strncmp(ifname, "radio", 5) )
429 phyidx = atoi(&ifname[5]);
433 if( (d = opendir("/sys/class/net")) != NULL )
435 while( (e = readdir(d)) != NULL )
437 snprintf(buffer, sizeof(buffer),
438 "/sys/class/net/%s/phy80211/index", e->d_name);
440 if( (fd = open(buffer, O_RDONLY)) > 0 )
442 if( (read(fd, buffer, sizeof(buffer)) > 0) &&
443 (atoi(buffer) == phyidx) )
445 strncpy(nif, e->d_name, sizeof(nif));
459 return nif[0] ? nif : NULL;
462 static char * nl80211_ifadd(const char *ifname)
466 static char nif[IFNAMSIZ] = { 0 };
467 struct nl80211_msg_conveyor *req, *res;
469 req = nl80211_msg(ifname, NL80211_CMD_NEW_INTERFACE, 0);
472 snprintf(nif, sizeof(nif), "tmp.%s", ifname);
474 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, nif);
475 NLA_PUT_U32(req->msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION);
477 res = nl80211_send(req);
491 static void nl80211_ifdel(const char *ifname)
493 struct nl80211_msg_conveyor *req;
495 req = nl80211_msg(ifname, NL80211_CMD_DEL_INTERFACE, 0);
498 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, ifname);
500 nl80211_free(nl80211_send(req));
507 static int nl80211_ifup(const char *ifname)
511 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
513 if( ioctl(nl80211_ioctlsock, SIOCGIFFLAGS, &ifr) )
516 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
518 return !ioctl(nl80211_ioctlsock, SIOCSIFFLAGS, &ifr);
521 static int nl80211_ifdown(const char *ifname)
525 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
527 if( ioctl(nl80211_ioctlsock, SIOCGIFFLAGS, &ifr) )
530 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
532 return !ioctl(nl80211_ioctlsock, SIOCSIFFLAGS, &ifr);
535 static int nl80211_ifmac(const char *ifname)
539 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
541 if( ioctl(nl80211_ioctlsock, SIOCGIFHWADDR, &ifr) )
544 ifr.ifr_hwaddr.sa_data[1]++;
545 ifr.ifr_hwaddr.sa_data[2]++;
547 return !ioctl(nl80211_ioctlsock, SIOCSIFHWADDR, &ifr);
550 static void nl80211_hostapd_hup(const char *ifname)
554 char *phy = nl80211_ifname2phy(ifname);
558 snprintf(buf, sizeof(buf), "/var/run/wifi-%s.pid", phy);
559 if( (fd = open(buf, O_RDONLY)) > 0 )
561 if( read(fd, buf, sizeof(buf)) > 0 )
573 int nl80211_probe(const char *ifname)
575 return !!nl80211_ifname2phy(ifname);
578 void nl80211_close(void)
580 if( nl80211_ioctlsock > -1 )
582 close(nl80211_ioctlsock);
588 nl_socket_free(nls->nl_sock);
591 nl_cache_free(nls->nl_cache);
598 int nl80211_get_mode(const char *ifname, char *buf)
600 return wext_get_mode(ifname, buf);
603 int nl80211_get_ssid(const char *ifname, char *buf)
607 if( !wext_get_ssid(ifname, buf) )
611 else if( (ssid = nl80211_hostapd_info(ifname)) &&
612 (ssid = nl80211_getval(ifname, ssid, "ssid")) )
614 memcpy(buf, ssid, strlen(ssid));
621 int nl80211_get_bssid(const char *ifname, char *buf)
624 unsigned char mac[6];
626 if( !wext_get_bssid(ifname, buf) )
630 else if( (bssid = nl80211_hostapd_info(ifname)) &&
631 (bssid = nl80211_getval(ifname, bssid, "bssid")) )
633 mac[0] = strtol(&bssid[0], NULL, 16);
634 mac[1] = strtol(&bssid[3], NULL, 16);
635 mac[2] = strtol(&bssid[6], NULL, 16);
636 mac[3] = strtol(&bssid[9], NULL, 16);
637 mac[4] = strtol(&bssid[12], NULL, 16);
638 mac[5] = strtol(&bssid[15], NULL, 16);
640 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
641 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
649 int nl80211_get_channel(const char *ifname, int *buf)
651 return wext_get_channel(ifname, buf);
654 int nl80211_get_frequency(const char *ifname, int *buf)
656 return wext_get_frequency(ifname, buf);
659 int nl80211_get_txpower(const char *ifname, int *buf)
661 return wext_get_txpower(ifname, buf);
665 static int nl80211_get_signal_cb(struct nl_msg *msg, void *arg)
669 struct nl80211_rssi_rate *rr = arg;
671 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
672 struct nlattr *attr[NL80211_ATTR_MAX + 1];
673 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
674 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
676 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
677 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
678 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
679 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
680 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
681 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
682 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
683 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
684 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
685 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
686 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
689 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
690 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
691 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
692 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
693 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
696 nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
697 genlmsg_attrlen(gnlh, 0), NULL);
699 if( attr[NL80211_ATTR_STA_INFO] )
701 if( !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
702 attr[NL80211_ATTR_STA_INFO], stats_policy) )
704 if( sinfo[NL80211_STA_INFO_SIGNAL] )
706 dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
707 rr->rssi = rr->rssi ? (int8_t)((rr->rssi + dbm) / 2) : dbm;
710 if( sinfo[NL80211_STA_INFO_TX_BITRATE] )
712 if( !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
713 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy) )
715 if( rinfo[NL80211_RATE_INFO_BITRATE] )
717 mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
719 ? (int16_t)((rr->rate + mbit) / 2) : mbit;
729 int nl80211_get_bitrate(const char *ifname, int *buf)
731 struct nl80211_rssi_rate rr;
732 struct nl80211_msg_conveyor *req;
734 if( !wext_get_bitrate(ifname, buf) )
737 req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
743 nl80211_cb(req, nl80211_get_signal_cb, &rr);
749 *buf = (rr.rate * 100);
757 int nl80211_get_signal(const char *ifname, int *buf)
759 struct nl80211_rssi_rate rr;
760 struct nl80211_msg_conveyor *req;
762 if( !wext_get_signal(ifname, buf) )
765 req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
771 nl80211_cb(req, nl80211_get_signal_cb, &rr);
785 int nl80211_get_noise(const char *ifname, int *buf)
788 struct nl80211_msg_conveyor *req, *res;
789 struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
791 static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
792 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
793 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
796 req = nl80211_msg(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP);
799 res = nl80211_send(req);
802 if( res->attr[NL80211_ATTR_SURVEY_INFO] )
804 if( !nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
805 res->attr[NL80211_ATTR_SURVEY_INFO], sp) &&
806 si[NL80211_SURVEY_INFO_NOISE] )
808 *buf = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
820 int nl80211_get_quality(const char *ifname, int *buf)
824 if( wext_get_quality(ifname, buf) )
828 if( !nl80211_get_signal(ifname, &signal) )
830 /* A positive signal level is usually just a quality
831 * value, pass through as-is */
837 /* The cfg80211 wext compat layer assumes a signal range
838 * of -110 dBm to -40 dBm, the quality value is derived
839 * by adding 110 to the signal level */
844 else if( signal > -40 )
847 *buf = (signal + 110);
855 int nl80211_get_quality_max(const char *ifname, int *buf)
857 if( wext_get_quality_max(ifname, buf) )
858 /* The cfg80211 wext compat layer assumes a maximum
865 int nl80211_get_encryption(const char *ifname, char *buf)
870 struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
873 if( (res = nl80211_hostapd_info(ifname)) )
875 if( (val = nl80211_getval(ifname, res, "auth_algs")) && (val > 0) )
877 c->auth_suites |= IWINFO_KMGMT_NONE;
881 c->auth_algs |= IWINFO_AUTH_OPEN;
885 c->auth_algs |= IWINFO_AUTH_SHARED;
889 c->auth_algs |= IWINFO_AUTH_OPEN;
890 c->auth_algs |= IWINFO_AUTH_SHARED;
897 for( i = 0; i < 4; i++ )
899 snprintf(k, sizeof(k), "wep_key%d", i);
901 if( (val = nl80211_getval(ifname, res, k)) )
903 if( (strlen(val) == 5) || (strlen(val) == 10) )
904 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
906 else if( (strlen(val) == 13) || (strlen(val) == 26) )
907 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
911 c->group_ciphers = c->pair_ciphers;
917 if( (val = nl80211_getval(ifname, res, "wpa")) != NULL )
918 c->wpa_version = atoi(val);
921 val = nl80211_getval(ifname, res, "wpa_key_mgmt");
923 if( !val || strstr(val, "PSK") )
924 c->auth_suites |= IWINFO_KMGMT_PSK;
926 if( val && strstr(val, "EAP") )
927 c->auth_suites |= IWINFO_KMGMT_8021x;
929 if( val && strstr(val, "NONE") )
930 c->auth_suites |= IWINFO_KMGMT_NONE;
933 if( (val = nl80211_getval(ifname, res, "wpa_pairwise")) != NULL )
935 if( strstr(val, "TKIP") )
936 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
938 if( strstr(val, "CCMP") )
939 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
941 if( strstr(val, "NONE") )
942 c->pair_ciphers |= IWINFO_CIPHER_NONE;
946 c->group_ciphers = c->pair_ciphers;
947 c->enabled = (c->auth_algs || c->auth_suites) ? 1 : 0;
953 else if( (res = nl80211_wpasupp_info(ifname, "STATUS")) &&
954 (val = nl80211_getval(NULL, res, "pairwise_cipher")) )
957 if( strstr(val, "WEP") )
959 if( strstr(val, "WEP-40") )
960 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
962 else if( strstr(val, "WEP-104") )
963 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
966 c->group_ciphers = c->pair_ciphers;
968 c->auth_suites |= IWINFO_KMGMT_NONE;
969 c->auth_algs |= IWINFO_AUTH_OPEN; /* XXX: assumption */
975 if( strstr(val, "TKIP") )
976 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
978 else if( strstr(val, "CCMP") )
979 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
981 else if( strstr(val, "NONE") )
982 c->pair_ciphers |= IWINFO_CIPHER_NONE;
984 else if( strstr(val, "WEP-40") )
985 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
987 else if( strstr(val, "WEP-104") )
988 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
991 if( (val = nl80211_getval(NULL, res, "group_cipher")) )
993 if( strstr(val, "TKIP") )
994 c->group_ciphers |= IWINFO_CIPHER_TKIP;
996 else if( strstr(val, "CCMP") )
997 c->group_ciphers |= IWINFO_CIPHER_CCMP;
999 else if( strstr(val, "NONE") )
1000 c->group_ciphers |= IWINFO_CIPHER_NONE;
1002 else if( strstr(val, "WEP-40") )
1003 c->group_ciphers |= IWINFO_CIPHER_WEP40;
1005 else if( strstr(val, "WEP-104") )
1006 c->group_ciphers |= IWINFO_CIPHER_WEP104;
1010 if( (val = nl80211_getval(NULL, res, "key_mgmt")) )
1012 if( strstr(val, "WPA2") )
1015 else if( strstr(val, "WPA") )
1019 if( strstr(val, "PSK") )
1020 c->auth_suites |= IWINFO_KMGMT_PSK;
1022 else if( strstr(val, "EAP") || strstr(val, "802.1X") )
1023 c->auth_suites |= IWINFO_KMGMT_8021x;
1025 else if( strstr(val, "NONE") )
1026 c->auth_suites |= IWINFO_KMGMT_NONE;
1029 c->enabled = (c->wpa_version && c->auth_suites) ? 1 : 0;
1039 static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
1041 struct nl80211_assoc_count *ac = arg;
1042 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1043 struct nlattr *attr[NL80211_ATTR_MAX + 1];
1044 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1046 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
1047 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
1048 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
1049 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
1050 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
1051 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
1052 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1053 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
1054 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
1055 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
1056 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
1059 nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1060 genlmsg_attrlen(gnlh, 0), NULL);
1062 if( attr[NL80211_ATTR_MAC] )
1063 memcpy(ac->entry->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
1065 if( attr[NL80211_ATTR_STA_INFO] )
1067 if( !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1068 attr[NL80211_ATTR_STA_INFO], stats_policy) )
1070 if( sinfo[NL80211_STA_INFO_SIGNAL] )
1071 ac->entry->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1075 ac->entry->noise = ac->noise;
1082 int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
1084 struct nl80211_assoc_count ac;
1085 struct nl80211_msg_conveyor *req;
1087 nl80211_get_noise(ifname, &ac.noise);
1089 req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
1093 ac.entry = (struct iwinfo_assoclist_entry *)buf;
1095 nl80211_cb(req, nl80211_get_assoclist_cb, &ac);
1099 *len = (ac.count * sizeof(struct iwinfo_assoclist_entry));
1106 int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
1108 int ch_cur, ch_cmp, bands_remain, freqs_remain;
1109 int dbm_max = -1, dbm_cur, dbm_cnt;
1110 struct nl80211_msg_conveyor *req, *res;
1111 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
1112 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
1113 struct nlattr *band, *freq;
1114 struct iwinfo_txpwrlist_entry entry;
1116 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1117 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1118 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1119 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1120 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1121 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1122 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
1125 if( nl80211_get_channel(ifname, &ch_cur) )
1128 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
1131 res = nl80211_send(req);
1134 nla_for_each_nested(band,
1135 res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
1137 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
1138 nla_len(band), NULL);
1140 nla_for_each_nested(freq,
1141 bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
1143 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
1144 nla_data(freq), nla_len(freq), freq_policy);
1146 ch_cmp = nl80211_freq2channel(
1147 nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]));
1149 if( (!ch_cur || (ch_cmp == ch_cur)) &&
1150 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] )
1152 dbm_max = (int)(0.01 * nla_get_u32(
1153 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
1167 for( dbm_cur = 0, dbm_cnt = 0;
1169 dbm_cur += 2, dbm_cnt++ )
1171 entry.dbm = dbm_cur;
1172 entry.mw = wext_dbm2mw(dbm_cur);
1174 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
1177 entry.dbm = dbm_max;
1178 entry.mw = wext_dbm2mw(dbm_max);
1180 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
1183 *len = dbm_cnt * sizeof(entry);
1190 static void nl80211_get_scancrypto(const char *spec,
1191 struct iwinfo_crypto_entry *c)
1193 if( strstr(spec, "OPEN") )
1201 if( strstr(spec, "WPA2-") && strstr(spec, "WPA-") )
1204 else if( strstr(spec, "WPA2") )
1207 else if( strstr(spec, "WPA") )
1210 else if( strstr(spec, "WEP") )
1211 c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
1214 if( strstr(spec, "PSK") )
1215 c->auth_suites |= IWINFO_KMGMT_PSK;
1217 if( strstr(spec, "802.1X") || strstr(spec, "EAP") )
1218 c->auth_suites |= IWINFO_KMGMT_8021x;
1220 if( strstr(spec, "WPA-NONE") )
1221 c->auth_suites |= IWINFO_KMGMT_NONE;
1224 if( strstr(spec, "TKIP") )
1225 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
1227 if( strstr(spec, "CCMP") )
1228 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
1230 if( strstr(spec, "WEP-40") )
1231 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
1233 if( strstr(spec, "WEP-104") )
1234 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
1236 c->group_ciphers = c->pair_ciphers;
1240 int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
1242 int freq, rssi, qmax, count;
1244 char ssid[128] = { 0 };
1245 char bssid[18] = { 0 };
1246 char cipher[256] = { 0 };
1248 /* Got a radioX pseudo interface, find some interface on it or create one */
1249 if( !strncmp(ifname, "radio", 5) )
1251 /* Reuse existing interface */
1252 if( (res = nl80211_phy2ifname(ifname)) != NULL )
1254 return nl80211_get_scanlist(res, buf, len);
1257 /* Need to spawn a temporary iface for scanning */
1258 else if( (res = nl80211_ifadd(ifname)) != NULL )
1260 count = nl80211_get_scanlist(res, buf, len);
1266 struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
1268 /* WPA supplicant */
1269 if( (res = nl80211_wpasupp_info(ifname, "SCAN")) &&
1270 !strcmp(res, "OK\n") )
1274 if( (res = nl80211_wpasupp_info(ifname, "SCAN_RESULTS")) )
1276 nl80211_get_quality_max(ifname, &qmax);
1278 /* skip header line */
1279 while( *res++ != '\n' );
1283 while( sscanf(res, "%17s %d %d %255s %127[^\n]\n",
1284 bssid, &freq, &rssi, cipher, ssid) > 0 )
1287 e->mac[0] = strtol(&bssid[0], NULL, 16);
1288 e->mac[1] = strtol(&bssid[3], NULL, 16);
1289 e->mac[2] = strtol(&bssid[6], NULL, 16);
1290 e->mac[3] = strtol(&bssid[9], NULL, 16);
1291 e->mac[4] = strtol(&bssid[12], NULL, 16);
1292 e->mac[5] = strtol(&bssid[15], NULL, 16);
1295 memcpy(e->ssid, ssid,
1296 min(strlen(ssid), sizeof(e->ssid) - 1));
1298 /* Mode (assume master) */
1299 sprintf((char *)e->mode, "Master");
1302 e->channel = nl80211_freq2channel(freq);
1310 /* The cfg80211 wext compat layer assumes a signal range
1311 * of -110 dBm to -40 dBm, the quality value is derived
1312 * by adding 110 to the signal level */
1315 else if( rssi > -40 )
1318 e->quality = (rssi + 110);
1326 e->quality_max = qmax;
1329 nl80211_get_scancrypto(cipher, &e->crypto);
1331 /* advance to next line */
1332 while( *res && *res++ != '\n' );
1338 *len = count * sizeof(struct iwinfo_scanlist_entry);
1346 /* Got a temp interface, don't create yet another one */
1347 if( !strncmp(ifname, "tmp.", 4) )
1349 if( !nl80211_ifup(ifname) )
1352 wext_get_scanlist(ifname, buf, len);
1353 nl80211_ifdown(ifname);
1357 /* Spawn a new scan interface */
1360 if( !(res = nl80211_ifadd(ifname)) )
1363 if( !nl80211_ifmac(res) )
1366 /* if we can take the new interface up, the driver supports an
1367 * additional interface and there's no need to tear down the ap */
1368 if( nl80211_ifup(res) )
1370 wext_get_scanlist(res, buf, len);
1371 nl80211_ifdown(res);
1374 /* driver cannot create secondary interface, take down ap
1376 else if( nl80211_ifdown(ifname) && nl80211_ifup(res) )
1378 wext_get_scanlist(res, buf, len);
1379 nl80211_ifdown(res);
1380 nl80211_ifup(ifname);
1381 nl80211_hostapd_hup(ifname);
1393 int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
1395 int count = 0, bands_remain, freqs_remain;
1396 struct nl80211_msg_conveyor *req, *res;
1397 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
1398 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
1399 struct nlattr *band, *freq;
1400 struct iwinfo_freqlist_entry *e = (struct iwinfo_freqlist_entry *)buf;
1402 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
1405 res = nl80211_send(req);
1408 nla_for_each_nested(band,
1409 res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
1411 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
1412 nla_len(band), NULL);
1414 nla_for_each_nested(freq,
1415 bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
1417 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
1418 nla_data(freq), nla_len(freq), NULL);
1420 if( !freqs[NL80211_FREQUENCY_ATTR_FREQ] ||
1421 freqs[NL80211_FREQUENCY_ATTR_DISABLED] )
1424 e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
1425 e->channel = nl80211_freq2channel(e->mhz);
1428 freqs[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] ||
1429 freqs[NL80211_FREQUENCY_ATTR_NO_IBSS] ||
1430 freqs[NL80211_FREQUENCY_ATTR_RADAR]
1444 *len = count * sizeof(struct iwinfo_freqlist_entry);
1451 int nl80211_get_country(const char *ifname, char *buf)
1454 struct nl80211_msg_conveyor *req, *res;
1456 req = nl80211_msg(ifname, NL80211_CMD_GET_REG, 0);
1459 res = nl80211_send(req);
1462 if( res->attr[NL80211_ATTR_REG_ALPHA2] )
1464 memcpy(buf, nla_data(res->attr[NL80211_ATTR_REG_ALPHA2]), 2);
1475 int nl80211_get_countrylist(const char *ifname, char *buf, int *len)
1478 struct iwinfo_iso3166_label *l;
1479 struct iwinfo_country_entry *e = (struct iwinfo_country_entry *)buf;
1481 for( l = ISO3166_Names, count = 0; l->iso3166; l++, e++, count++ )
1483 e->iso3166 = l->iso3166;
1484 e->ccode[0] = (l->iso3166 / 256);
1485 e->ccode[1] = (l->iso3166 % 256);
1488 *len = (count * sizeof(struct iwinfo_country_entry));
1492 int nl80211_get_hwmodelist(const char *ifname, int *buf)
1494 int bands_remain, freqs_remain;
1495 struct nl80211_msg_conveyor *req, *res;
1496 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
1497 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
1498 struct nlattr *band, *freq;
1501 req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
1504 res = nl80211_send(req);
1507 nla_for_each_nested(band,
1508 res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
1510 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
1511 nla_len(band), NULL);
1513 if( bands[NL80211_BAND_ATTR_HT_CAPA] )
1514 caps = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]);
1516 /* Treat any nonzero capability as 11n */
1518 *buf |= IWINFO_80211_N;
1520 nla_for_each_nested(freq,
1521 bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
1523 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
1524 nla_data(freq), nla_len(freq), NULL);
1526 if( !freqs[NL80211_FREQUENCY_ATTR_FREQ] )
1529 if( nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]) < 2485 )
1531 *buf |= IWINFO_80211_B;
1532 *buf |= IWINFO_80211_G;
1536 *buf |= IWINFO_80211_A;
1545 return *buf ? 0 : -1;
1548 int nl80211_get_mbssid_support(const char *ifname, int *buf)
1550 /* Test whether we can create another interface */
1551 char *nif = nl80211_ifadd(ifname);
1555 *buf = (nl80211_ifmac(nif) && nl80211_ifup(nif));
1557 nl80211_ifdown(nif);