X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=iwinfo_nl80211.c;h=c7b0222e152485f4ca756edbca89d9ea10ba3057;hb=3ac846e7dc915969fba316d3070d327a94aefbbe;hp=1a1fc69db4f15f1cb4685f49bc4318db39f824b4;hpb=41ae5bd556c5e07c9a0a51ef84a2aaa204a96e73;p=oweals%2Fiwinfo.git diff --git a/iwinfo_nl80211.c b/iwinfo_nl80211.c index 1a1fc69..c7b0222 100644 --- a/iwinfo_nl80211.c +++ b/iwinfo_nl80211.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "iwinfo_nl80211.h" @@ -218,9 +219,6 @@ static struct nl80211_msg_conveyor * nl80211_new(struct genl_family *family, return &cv; err: - if (cb) - nl_cb_put(cb); - if (req) nlmsg_free(req); @@ -237,23 +235,49 @@ static struct nl80211_msg_conveyor * nl80211_ctl(int cmd, int flags) static int nl80211_phy_idx_from_uci_path(struct uci_section *s) { - const char *opt; - char buf[128]; + size_t linklen, pathlen; + char buf[128], *link; + struct dirent *e; + const char *path; int idx = -1; - glob_t gl; + DIR *d; - opt = uci_lookup_option_string(uci_ctx, s, "path"); - if (!opt) + path = uci_lookup_option_string(uci_ctx, s, "path"); + if (!path) return -1; - snprintf(buf, sizeof(buf), "/sys/devices/%s/ieee80211/*/index", opt); /**/ - if (glob(buf, 0, NULL, &gl)) - return -1; + if ((d = opendir("/sys/class/ieee80211")) != NULL) + { + while ((e = readdir(d)) != NULL) + { + snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/device", e->d_name); - if (gl.gl_pathc > 0) - idx = nl80211_readint(gl.gl_pathv[0]); + link = realpath(buf, NULL); - globfree(&gl); + if (link == NULL) + continue; + + linklen = strlen(link); + pathlen = strlen(path); + + if (pathlen >= linklen || strcmp(link + (linklen - pathlen), path)) + linklen = 0; + + free(link); + + if (linklen == 0) + continue; + + snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", e->d_name); + + idx = nl80211_readint(buf); + + if (idx >= 0) + break; + } + + closedir(d); + } return idx; } @@ -369,20 +393,24 @@ nla_put_failure: return NULL; } -static struct nl80211_msg_conveyor * nl80211_send( - struct nl80211_msg_conveyor *cv, - int (*cb_func)(struct nl_msg *, void *), void *cb_arg -) { +static int nl80211_send(struct nl80211_msg_conveyor *cv, + int (*cb_func)(struct nl_msg *, void *), + void *cb_arg) +{ static struct nl80211_msg_conveyor rcv; - int err = 1; + int err; if (cb_func) nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, cb_func, cb_arg); else nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_msg_response, &rcv); - if (nl_send_auto_complete(nls->nl_sock, cv->msg) < 0) - goto err; + err = nl_send_auto_complete(nls->nl_sock, cv->msg); + + if (err < 0) + goto out; + + err = 1; nl_cb_err(cv->cb, NL_CB_CUSTOM, nl80211_msg_error, &err); nl_cb_set(cv->cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_msg_finish, &err); @@ -391,13 +419,23 @@ static struct nl80211_msg_conveyor * nl80211_send( while (err > 0) nl_recvmsgs(nls->nl_sock, cv->cb); - return &rcv; +out: + nl80211_free(cv); + return err; +} -err: - nl_cb_put(cv->cb); - nlmsg_free(cv->msg); +static int nl80211_request(const char *ifname, int cmd, int flags, + int (*cb_func)(struct nl_msg *, void *), + void *cb_arg) +{ + struct nl80211_msg_conveyor *cv; - return NULL; + cv = nl80211_msg(ifname, cmd, flags); + + if (!cv) + return -ENOMEM; + + return nl80211_send(cv, cb_func, cb_arg); } static struct nlattr ** nl80211_parse(struct nl_msg *msg) @@ -411,6 +449,30 @@ static struct nlattr ** nl80211_parse(struct nl_msg *msg) return attr; } +static int nl80211_get_protocol_features_cb(struct nl_msg *msg, void *arg) +{ + uint32_t *features = arg; + struct nlattr **attr = nl80211_parse(msg); + + if (attr[NL80211_ATTR_PROTOCOL_FEATURES]) + *features = nla_get_u32(attr[NL80211_ATTR_PROTOCOL_FEATURES]); + + return NL_SKIP; +} + +static int nl80211_get_protocol_features(const char *ifname) +{ + struct nl80211_msg_conveyor *req; + uint32_t features = 0; + + req = nl80211_msg(ifname, NL80211_CMD_GET_PROTOCOL_FEATURES, 0); + if (req) { + nl80211_send(req, nl80211_get_protocol_features_cb, &features); + nl80211_free(req); + } + + return features; +} static int nl80211_subscribe_cb(struct nl_msg *msg, void *arg) { @@ -446,18 +508,24 @@ static int nl80211_subscribe(const char *family, const char *group) { struct nl80211_group_conveyor cv = { .name = group, .id = -ENOENT }; struct nl80211_msg_conveyor *req; + int err; req = nl80211_ctl(CTRL_CMD_GETFAMILY, 0); if (req) { NLA_PUT_STRING(req->msg, CTRL_ATTR_FAMILY_NAME, family); - nl80211_send(req, nl80211_subscribe_cb, &cv); + err = nl80211_send(req, nl80211_subscribe_cb, &cv); + + if (err) + return err; + + return nl_socket_add_membership(nls->nl_sock, cv.id); nla_put_failure: nl80211_free(req); } - return nl_socket_add_membership(nls->nl_sock, cv.id); + return -ENOMEM; } @@ -466,7 +534,7 @@ static int nl80211_wait_cb(struct nl_msg *msg, void *arg) struct nl80211_event_conveyor *cv = arg; struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); - if (gnlh->cmd == cv->wait) + if (cv->wait[gnlh->cmd / 32] & (1 << (gnlh->cmd % 32))) cv->recv = gnlh->cmd; return NL_SKIP; @@ -477,10 +545,13 @@ static int nl80211_wait_seq_check(struct nl_msg *msg, void *arg) return NL_OK; } -static int nl80211_wait(const char *family, const char *group, int cmd) +static int __nl80211_wait(const char *family, const char *group, ...) { - struct nl80211_event_conveyor cv = { .wait = cmd }; + struct nl80211_event_conveyor cv = { }; struct nl_cb *cb; + int err = 0; + int cmd; + va_list ap; if (nl80211_subscribe(family, group)) return -ENOENT; @@ -490,17 +561,28 @@ static int nl80211_wait(const char *family, const char *group, int cmd) if (!cb) return -ENOMEM; + nl_cb_err(cb, NL_CB_CUSTOM, nl80211_msg_error, &err); nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, nl80211_wait_seq_check, NULL); nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_wait_cb, &cv ); - while (!cv.recv) + va_start(ap, group); + + for (cmd = va_arg(ap, int); cmd != 0; cmd = va_arg(ap, int)) + cv.wait[cmd / 32] |= (1 << (cmd % 32)); + + va_end(ap); + + while (!cv.recv && !err) nl_recvmsgs(nls->nl_sock, cb); nl_cb_put(cb); - return 0; + return err; } +#define nl80211_wait(family, group, ...) \ + __nl80211_wait(family, group, __VA_ARGS__, 0) + static int nl80211_freq2channel(int freq) { @@ -510,6 +592,8 @@ static int nl80211_freq2channel(int freq) return (freq - 2407) / 5; else if (freq >= 4910 && freq <= 4980) return (freq - 4000) / 5; + else if(freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) + return (freq - 56160) / 2160; else return (freq - 5000) / 5; } @@ -523,6 +607,10 @@ static int nl80211_channel2freq(int channel, const char *band) else if (channel < 14) return (channel * 5) + 2407; } + else if ( strcmp(band, "ad") == 0) + { + return 56160 + 2160 * channel; + } else { if (channel >= 182 && channel <= 196) @@ -551,16 +639,11 @@ static int nl80211_ifname2phy_cb(struct nl_msg *msg, void *arg) static char * nl80211_ifname2phy(const char *ifname) { static char phy[32] = { 0 }; - struct nl80211_msg_conveyor *req; memset(phy, 0, sizeof(phy)); - req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0); - if (req) - { - nl80211_send(req, nl80211_ifname2phy_cb, phy); - nl80211_free(req); - } + nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0, + nl80211_ifname2phy_cb, phy); return phy[0] ? phy : NULL; } @@ -643,17 +726,13 @@ static int nl80211_get_mode_cb(struct nl_msg *msg, void *arg) static int nl80211_get_mode(const char *ifname, int *buf) { char *res; - struct nl80211_msg_conveyor *req; - res = nl80211_phy2ifname(ifname); - req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0); *buf = IWINFO_OPMODE_UNKNOWN; - if (req) - { - nl80211_send(req, nl80211_get_mode_cb, buf); - nl80211_free(req); - } + res = nl80211_phy2ifname(ifname); + + nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0, + nl80211_get_mode_cb, buf); return (*buf == IWINFO_OPMODE_UNKNOWN) ? -1 : 0; } @@ -815,7 +894,9 @@ static int __nl80211_wpactl_query(const char *ifname, ...) if (nl80211_get_mode(ifname, &mode)) return 0; - if (mode != IWINFO_OPMODE_CLIENT && mode != IWINFO_OPMODE_ADHOC) + if (mode != IWINFO_OPMODE_CLIENT && + mode != IWINFO_OPMODE_ADHOC && + mode != IWINFO_OPMODE_MESHPOINT) return 0; sock = nl80211_wpactl_connect(ifname, &local); @@ -893,7 +974,7 @@ static int __nl80211_wpactl_query(const char *ifname, ...) static char * nl80211_ifadd(const char *ifname) { - char *rv = NULL, path[PATH_MAX]; + char path[PATH_MAX]; static char nif[IFNAMSIZ] = { 0 }; struct nl80211_msg_conveyor *req; FILE *sysfs; @@ -917,18 +998,19 @@ static char * nl80211_ifadd(const char *ifname) fclose(sysfs); } - rv = nif; + return nif; nla_put_failure: nl80211_free(req); } - return rv; + return NULL; } static void nl80211_ifdel(const char *ifname) { struct nl80211_msg_conveyor *req; + int err; req = nl80211_msg(ifname, NL80211_CMD_DEL_INTERFACE, 0); if (req) @@ -936,6 +1018,7 @@ static void nl80211_ifdel(const char *ifname) NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, ifname); nl80211_send(req, NULL, NULL); + return; nla_put_failure: nl80211_free(req); @@ -975,6 +1058,20 @@ struct nl80211_ssid_bssid { unsigned char bssid[7]; }; +static int nl80211_get_macaddr_cb(struct nl_msg *msg, void *arg) +{ + struct nl80211_ssid_bssid *sb = arg; + struct nlattr **tb = nl80211_parse(msg); + + if (tb[NL80211_ATTR_MAC]) { + sb->bssid[0] = 1; + memcpy(sb->bssid + 1, nla_data(tb[NL80211_ATTR_MAC]), + sizeof(sb->bssid) - 1); + } + + return NL_SKIP; +} + static int nl80211_get_ssid_bssid_cb(struct nl_msg *msg, void *arg) { int ielen; @@ -1036,47 +1133,44 @@ static int nl80211_get_ssid_bssid_cb(struct nl_msg *msg, void *arg) static int nl80211_get_ssid(const char *ifname, char *buf) { char *res; - struct nl80211_msg_conveyor *req; - struct nl80211_ssid_bssid sb; + struct nl80211_ssid_bssid sb = { .ssid = (unsigned char *)buf }; /* try to find ssid from scan dump results */ res = nl80211_phy2ifname(ifname); - req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP); - - sb.ssid = (unsigned char *)buf; - *buf = 0; + sb.ssid[0] = 0; - if (req) - { - nl80211_send(req, nl80211_get_ssid_bssid_cb, &sb); - nl80211_free(req); - } + nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP, + nl80211_get_ssid_bssid_cb, &sb); /* failed, try to find from hostapd info */ - if (*buf == 0) - nl80211_hostapd_query(ifname, "ssid", buf, IWINFO_ESSID_MAX_SIZE + 1); + if (sb.ssid[0] == 0) + nl80211_hostapd_query(ifname, "ssid", sb.ssid, + IWINFO_ESSID_MAX_SIZE + 1); - return (*buf == 0) ? -1 : 0; + /* failed, try to obtain Mesh ID */ + if (sb.ssid[0] == 0) + iwinfo_ubus_query(res ? res : ifname, "mesh_id", + sb.ssid, IWINFO_ESSID_MAX_SIZE + 1); + + return (sb.ssid[0] == 0) ? -1 : 0; } static int nl80211_get_bssid(const char *ifname, char *buf) { char *res, bssid[sizeof("FF:FF:FF:FF:FF:FF\0")]; - struct nl80211_msg_conveyor *req; - struct nl80211_ssid_bssid sb; + struct nl80211_ssid_bssid sb = { }; - /* try to find bssid from scan dump results */ res = nl80211_phy2ifname(ifname); - req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP); - sb.ssid = NULL; - sb.bssid[0] = 0; + /* try to obtain mac address via NL80211_CMD_GET_INTERFACE */ + nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0, + nl80211_get_macaddr_cb, &sb); - if (req) - { - nl80211_send(req, nl80211_get_ssid_bssid_cb, &sb); - nl80211_free(req); - } + /* failed, try to find bssid from scan dump results */ + if (sb.bssid[0] == 0) + nl80211_request(res ? res : ifname, + NL80211_CMD_GET_SCAN, NLM_F_DUMP, + nl80211_get_ssid_bssid_cb, &sb); /* failed, try to find mac from hostapd info */ if ((sb.bssid[0] == 0) && @@ -1139,19 +1233,14 @@ static int nl80211_get_frequency_info_cb(struct nl_msg *msg, void *arg) static int nl80211_get_frequency(const char *ifname, int *buf) { - char *res, channel[4], hwmode[2]; - struct nl80211_msg_conveyor *req; + char *res, channel[4], hwmode[3]; /* try to find frequency from interface info */ res = nl80211_phy2ifname(ifname); - req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0); *buf = 0; - if (req) - { - nl80211_send(req, nl80211_get_frequency_info_cb, buf); - nl80211_free(req); - } + nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0, + nl80211_get_frequency_info_cb, buf); /* failed, try to find frequency from hostapd info */ if ((*buf == 0) && @@ -1165,13 +1254,9 @@ static int nl80211_get_frequency(const char *ifname, int *buf) if (*buf == 0) { res = nl80211_phy2ifname(ifname); - req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP); - if (req) - { - nl80211_send(req, nl80211_get_frequency_scan_cb, buf); - nl80211_free(req); - } + nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP, + nl80211_get_frequency_scan_cb, buf); } return (*buf == 0) ? -1 : 0; @@ -1202,21 +1287,15 @@ static int nl80211_get_txpower_cb(struct nl_msg *msg, void *arg) static int nl80211_get_txpower(const char *ifname, int *buf) { char *res; - struct nl80211_msg_conveyor *req; res = nl80211_phy2ifname(ifname); - req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0); + *buf = 0; - if (req) - { - *buf = 0; - nl80211_send(req, nl80211_get_txpower_cb, buf); - nl80211_free(req); - if (*buf) - return 0; - } + if (nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0, + nl80211_get_txpower_cb, buf)) + return -1; - return -1; + return 0; } @@ -1257,7 +1336,8 @@ static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg) if (sinfo[NL80211_STA_INFO_SIGNAL]) { dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]); - rr->rssi = rr->rssi ? (int8_t)((rr->rssi + dbm) / 2) : dbm; + rr->rssi = (rr->rssi * rr->rssi_samples + dbm) / (rr->rssi_samples + 1); + rr->rssi_samples++; } if (sinfo[NL80211_STA_INFO_TX_BITRATE]) @@ -1269,8 +1349,8 @@ static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg) if (rinfo[NL80211_RATE_INFO_BITRATE]) { mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]); - rr->rate = rr->rate - ? (int16_t)((rr->rate + mbit) / 2) : mbit; + rr->rate = (rr->rate * rr->rate_samples + mbit) / (rr->rate_samples + 1); + rr->rate_samples++; } } } @@ -1284,10 +1364,8 @@ static void nl80211_fill_signal(const char *ifname, struct nl80211_rssi_rate *r) { DIR *d; struct dirent *de; - struct nl80211_msg_conveyor *req; - r->rssi = 0; - r->rate = 0; + memset(r, 0, sizeof(*r)); if ((d = opendir("/sys/class/net")) != NULL) { @@ -1297,14 +1375,8 @@ static void nl80211_fill_signal(const char *ifname, struct nl80211_rssi_rate *r) (!de->d_name[strlen(ifname)] || !strncmp(&de->d_name[strlen(ifname)], ".sta", 4))) { - req = nl80211_msg(de->d_name, NL80211_CMD_GET_STATION, - NLM_F_DUMP); - - if (req) - { - nl80211_send(req, nl80211_fill_signal_cb, r); - nl80211_free(req); - } + nl80211_request(de->d_name, NL80211_CMD_GET_STATION, + NLM_F_DUMP, nl80211_fill_signal_cb, r); } } @@ -1318,7 +1390,7 @@ static int nl80211_get_bitrate(const char *ifname, int *buf) nl80211_fill_signal(ifname, &rr); - if (rr.rate) + if (rr.rate_samples) { *buf = (rr.rate * 100); return 0; @@ -1333,7 +1405,7 @@ static int nl80211_get_signal(const char *ifname, int *buf) nl80211_fill_signal(ifname, &rr); - if (rr.rssi) + if (rr.rssi_samples) { *buf = rr.rssi; return 0; @@ -1372,24 +1444,17 @@ static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg) static int nl80211_get_noise(const char *ifname, int *buf) { - int8_t noise; - struct nl80211_msg_conveyor *req; + int8_t noise = 0; - req = nl80211_msg(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP); - if (req) - { - noise = 0; - - nl80211_send(req, nl80211_get_noise_cb, &noise); - nl80211_free(req); + if (nl80211_request(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP, + nl80211_get_noise_cb, &noise)) + goto out; - if (noise) - { - *buf = noise; - return 0; - } - } + *buf = noise; + return 0; +out: + *buf = 0; return -1; } @@ -1453,10 +1518,112 @@ static int nl80211_check_wepkey(const char *key) return 0; } +static struct { + const char *match; + int version; + int suite; +} wpa_key_mgmt_strings[] = { + { "IEEE 802.1X/EAP", 0, IWINFO_KMGMT_8021x }, + { "EAP-SUITE-B-192", 4, IWINFO_KMGMT_8021x }, + { "EAP-SUITE-B", 4, IWINFO_KMGMT_8021x }, + { "EAP-SHA256", 0, IWINFO_KMGMT_8021x }, + { "PSK-SHA256", 0, IWINFO_KMGMT_PSK }, + { "NONE", 0, IWINFO_KMGMT_NONE }, + { "None", 0, IWINFO_KMGMT_NONE }, + { "PSK", 0, IWINFO_KMGMT_PSK }, + { "EAP", 0, IWINFO_KMGMT_8021x }, + { "SAE", 4, IWINFO_KMGMT_SAE }, + { "OWE", 4, IWINFO_KMGMT_OWE } +}; + +static void parse_wpa_suites(const char *str, int defversion, + uint8_t *versions, uint8_t *suites) +{ + size_t l; + int i, version; + const char *p, *q, *m, *sep = " \t\n,-+/"; + + for (p = str; *p; ) + { + q = p; + + for (i = 0; i < ARRAY_SIZE(wpa_key_mgmt_strings); i++) + { + m = wpa_key_mgmt_strings[i].match; + l = strlen(m); + + if (!strncmp(q, m, l) && (!q[l] || strchr(sep, q[l]))) + { + if (wpa_key_mgmt_strings[i].version != 0) + version = wpa_key_mgmt_strings[i].version; + else + version = defversion; + + *versions |= version; + *suites |= wpa_key_mgmt_strings[i].suite; + + q += l; + break; + } + } + + if (q == p) + q += strcspn(q, sep); + + p = q + strspn(q, sep); + } +} + +static struct { + const char *match; + int cipher; +} wpa_cipher_strings[] = { + { "WEP-104", IWINFO_CIPHER_WEP104 }, + { "WEP-40", IWINFO_CIPHER_WEP40 }, + { "NONE", IWINFO_CIPHER_NONE }, + { "TKIP", IWINFO_CIPHER_TKIP }, + { "CCMP", IWINFO_CIPHER_CCMP } +}; + +static void parse_wpa_ciphers(const char *str, uint8_t *ciphers) +{ + int i; + size_t l; + const char *m, *p, *q, *sep = " \t\n,-+/"; + + for (p = str; *p; ) + { + q = p; + + for (i = 0; i < ARRAY_SIZE(wpa_cipher_strings); i++) + { + m = wpa_cipher_strings[i].match; + l = strlen(m); + + if (!strncmp(q, m, l) && (!q[l] || strchr(sep, q[l]))) + { + *ciphers |= wpa_cipher_strings[i].cipher; + + q += l; + break; + } + } + + if (q == p) + q += strcspn(q, sep); + + p = q + strspn(q, sep); + } +} + static int nl80211_get_encryption(const char *ifname, char *buf) { - char wpa[2], wpa_key_mgmt[16], wpa_pairwise[16], wpa_groupwise[16]; + char *p; + int opmode; + uint8_t wpa_version = 0; + char wpa[2], wpa_key_mgmt[64], wpa_pairwise[16], wpa_groupwise[16]; char auth_algs[2], wep_key0[27], wep_key1[27], wep_key2[27], wep_key3[27]; + char mode[16]; struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf; @@ -1464,63 +1631,56 @@ static int nl80211_get_encryption(const char *ifname, char *buf) if (nl80211_wpactl_query(ifname, "pairwise_cipher", wpa_pairwise, sizeof(wpa_pairwise), "group_cipher", wpa_groupwise, sizeof(wpa_groupwise), - "key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt))) + "key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt), + "mode", mode, sizeof(mode))) { - /* WEP */ + /* WEP or Open */ if (!strcmp(wpa_key_mgmt, "NONE")) { - if (strstr(wpa_pairwise, "WEP-40")) - c->pair_ciphers |= IWINFO_CIPHER_WEP40; - else if (strstr(wpa_pairwise, "WEP-104")) - c->pair_ciphers |= IWINFO_CIPHER_WEP104; - - if (strstr(wpa_groupwise, "WEP-40")) - c->group_ciphers |= IWINFO_CIPHER_WEP40; - else if (strstr(wpa_groupwise, "WEP-104")) - c->group_ciphers |= IWINFO_CIPHER_WEP104; - - c->enabled = !!(c->pair_ciphers | c->group_ciphers); - c->auth_suites |= IWINFO_KMGMT_NONE; - c->auth_algs |= IWINFO_AUTH_OPEN; /* XXX: assumption */ + parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers); + parse_wpa_ciphers(wpa_groupwise, &c->group_ciphers); + + if (c->pair_ciphers != 0 && c->pair_ciphers != IWINFO_CIPHER_NONE) { + c->enabled = 1; + c->auth_suites = IWINFO_KMGMT_NONE; + c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED; + } + else { + c->pair_ciphers = 0; + c->group_ciphers = 0; + } + } + + /* MESH with SAE */ + else if (!strcmp(mode, "mesh") && !strcmp(wpa_key_mgmt, "UNKNOWN")) + { + c->enabled = 1; + c->wpa_version = 4; + c->auth_suites = IWINFO_KMGMT_SAE; + c->pair_ciphers = IWINFO_CIPHER_CCMP; + c->group_ciphers = IWINFO_CIPHER_CCMP; } /* WPA */ - else if (strstr(wpa_key_mgmt, "WPA")) + else { - if (strstr(wpa_pairwise, "TKIP")) - c->pair_ciphers |= IWINFO_CIPHER_TKIP; - else if (strstr(wpa_pairwise, "CCMP")) - c->pair_ciphers |= IWINFO_CIPHER_CCMP; - else if (strstr(wpa_pairwise, "NONE")) - c->pair_ciphers |= IWINFO_CIPHER_NONE; - else if (strstr(wpa_pairwise, "WEP-40")) - c->pair_ciphers |= IWINFO_CIPHER_WEP40; - else if (strstr(wpa_pairwise, "WEP-104")) - c->pair_ciphers |= IWINFO_CIPHER_WEP104; - - if (strstr(wpa_groupwise, "TKIP")) - c->group_ciphers |= IWINFO_CIPHER_TKIP; - else if (strstr(wpa_groupwise, "CCMP")) - c->group_ciphers |= IWINFO_CIPHER_CCMP; - else if (strstr(wpa_groupwise, "NONE")) - c->group_ciphers |= IWINFO_CIPHER_NONE; - else if (strstr(wpa_groupwise, "WEP-40")) - c->group_ciphers |= IWINFO_CIPHER_WEP40; - else if (strstr(wpa_groupwise, "WEP-104")) - c->group_ciphers |= IWINFO_CIPHER_WEP104; - - if (strstr(wpa_key_mgmt, "WPA2")) - c->wpa_version = 2; - else if (strstr(wpa_key_mgmt, "WPA")) - c->wpa_version = 1; - - if (strstr(wpa_key_mgmt, "PSK")) - c->auth_suites |= IWINFO_KMGMT_PSK; - else if (strstr(wpa_key_mgmt, "EAP") || - strstr(wpa_key_mgmt, "802.1X")) - c->auth_suites |= IWINFO_KMGMT_8021x; - else if (strstr(wpa_key_mgmt, "NONE")) - c->auth_suites |= IWINFO_KMGMT_NONE; + parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers); + parse_wpa_ciphers(wpa_groupwise, &c->group_ciphers); + + p = wpa_key_mgmt; + + if (!strncmp(p, "WPA2-", 5) || !strncmp(p, "WPA2/", 5)) + { + p += 5; + wpa_version = 2; + } + else if (!strncmp(p, "WPA-", 4)) + { + p += 4; + wpa_version = 1; + } + + parse_wpa_suites(p, wpa_version, &c->wpa_version, &c->auth_suites); c->enabled = !!(c->wpa_version && c->auth_suites); } @@ -1539,39 +1699,27 @@ static int nl80211_get_encryption(const char *ifname, char *buf) "wep_key2", wep_key2, sizeof(wep_key2), "wep_key3", wep_key3, sizeof(wep_key3))) { - c->wpa_version = wpa[0] ? atoi(wpa) : 0; + c->wpa_version = 0; if (wpa_key_mgmt[0]) { - if (strstr(wpa_key_mgmt, "PSK")) - c->auth_suites |= IWINFO_KMGMT_PSK; + for (p = strtok(wpa_key_mgmt, " \t"); p != NULL; p = strtok(NULL, " \t")) + { + if (!strncmp(p, "WPA-", 4)) + p += 4; - if (strstr(wpa_key_mgmt, "EAP")) - c->auth_suites |= IWINFO_KMGMT_8021x; + parse_wpa_suites(p, atoi(wpa), &c->wpa_version, &c->auth_suites); + } - if (strstr(wpa_key_mgmt, "NONE")) - c->auth_suites |= IWINFO_KMGMT_NONE; - } - else - { - c->auth_suites |= IWINFO_KMGMT_PSK; + c->enabled = c->wpa_version ? 1 : 0; } if (wpa_pairwise[0]) - { - if (strstr(wpa_pairwise, "TKIP")) - c->pair_ciphers |= IWINFO_CIPHER_TKIP; - - if (strstr(wpa_pairwise, "CCMP")) - c->pair_ciphers |= IWINFO_CIPHER_CCMP; - - if (strstr(wpa_pairwise, "NONE")) - c->pair_ciphers |= IWINFO_CIPHER_NONE; - } + parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers); if (auth_algs[0]) { - switch(atoi(auth_algs)) + switch (atoi(auth_algs)) { case 1: c->auth_algs |= IWINFO_AUTH_OPEN; @@ -1591,14 +1739,26 @@ static int nl80211_get_encryption(const char *ifname, char *buf) c->pair_ciphers |= nl80211_check_wepkey(wep_key1); c->pair_ciphers |= nl80211_check_wepkey(wep_key2); c->pair_ciphers |= nl80211_check_wepkey(wep_key3); + + c->enabled = (c->auth_algs && c->pair_ciphers) ? 1 : 0; } c->group_ciphers = c->pair_ciphers; - c->enabled = (c->wpa_version || c->pair_ciphers) ? 1 : 0; return 0; } + /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */ + else if (!nl80211_get_mode(ifname, &opmode) && + (opmode == IWINFO_OPMODE_ADHOC || + opmode == IWINFO_OPMODE_MESHPOINT)) + { + c->enabled = 0; + + return 0; + } + + return -1; } @@ -1628,6 +1788,152 @@ static int nl80211_get_phyname(const char *ifname, char *buf) } +static void nl80211_parse_rateinfo(struct nlattr **ri, + struct iwinfo_rate_entry *re) +{ + if (ri[NL80211_RATE_INFO_BITRATE32]) + re->rate = nla_get_u32(ri[NL80211_RATE_INFO_BITRATE32]) * 100; + else if (ri[NL80211_RATE_INFO_BITRATE]) + re->rate = nla_get_u16(ri[NL80211_RATE_INFO_BITRATE]) * 100; + + if (ri[NL80211_RATE_INFO_VHT_MCS]) + { + re->is_vht = 1; + re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_VHT_MCS]); + + if (ri[NL80211_RATE_INFO_VHT_NSS]) + re->nss = nla_get_u8(ri[NL80211_RATE_INFO_VHT_NSS]); + } + else if (ri[NL80211_RATE_INFO_MCS]) + { + re->is_ht = 1; + re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_MCS]); + } + + if (ri[NL80211_RATE_INFO_5_MHZ_WIDTH]) + re->mhz = 5; + else if (ri[NL80211_RATE_INFO_10_MHZ_WIDTH]) + re->mhz = 10; + else if (ri[NL80211_RATE_INFO_40_MHZ_WIDTH]) + re->mhz = 40; + else if (ri[NL80211_RATE_INFO_80_MHZ_WIDTH]) + re->mhz = 80; + else if (ri[NL80211_RATE_INFO_80P80_MHZ_WIDTH] || + ri[NL80211_RATE_INFO_160_MHZ_WIDTH]) + re->mhz = 160; + else + re->mhz = 20; + + if (ri[NL80211_RATE_INFO_SHORT_GI]) + re->is_short_gi = 1; + + re->is_40mhz = (re->mhz == 40); +} + +static int nl80211_get_survey_cb(struct nl_msg *msg, void *arg) +{ + struct nl80211_array_buf *arr = arg; + struct iwinfo_survey_entry *e = arr->buf; + struct nlattr **attr = nl80211_parse(msg); + struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1]; + int rc; + + static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = { + [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 }, + [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 }, + [NL80211_SURVEY_INFO_TIME] = { .type = NLA_U64 }, + [NL80211_SURVEY_INFO_TIME_BUSY] = { .type = NLA_U64 }, + [NL80211_SURVEY_INFO_TIME_EXT_BUSY] = { .type = NLA_U64 }, + [NL80211_SURVEY_INFO_TIME_RX] = { .type = NLA_U64 }, + [NL80211_SURVEY_INFO_TIME_TX] = { .type = NLA_U64 }, + }; + + rc = nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX, + attr[NL80211_ATTR_SURVEY_INFO], + survey_policy); + if (rc) + return NL_SKIP; + + /* advance to end of array */ + e += arr->count; + memset(e, 0, sizeof(*e)); + + if (sinfo[NL80211_SURVEY_INFO_FREQUENCY]) + e->mhz = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]); + + if (sinfo[NL80211_SURVEY_INFO_NOISE]) + e->noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]); + + if (sinfo[NL80211_SURVEY_INFO_TIME]) + e->active_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME]); + + if (sinfo[NL80211_SURVEY_INFO_TIME_BUSY]) + e->busy_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_BUSY]); + + if (sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]) + e->busy_time_ext = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]); + + if (sinfo[NL80211_SURVEY_INFO_TIME_RX]) + e->rxtime = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_RX]); + + if (sinfo[NL80211_SURVEY_INFO_TIME_TX]) + e->txtime = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_TX]); + + arr->count++; + return NL_SKIP; +} + + +static void plink_state_to_str(char *dst, unsigned state) +{ + switch (state) { + case NL80211_PLINK_LISTEN: + strcpy(dst, "LISTEN"); + break; + case NL80211_PLINK_OPN_SNT: + strcpy(dst, "OPN_SNT"); + break; + case NL80211_PLINK_OPN_RCVD: + strcpy(dst, "OPN_RCVD"); + break; + case NL80211_PLINK_CNF_RCVD: + strcpy(dst, "CNF_RCVD"); + break; + case NL80211_PLINK_ESTAB: + strcpy(dst, "ESTAB"); + break; + case NL80211_PLINK_HOLDING: + strcpy(dst, "HOLDING"); + break; + case NL80211_PLINK_BLOCKED: + strcpy(dst, "BLOCKED"); + break; + default: + strcpy(dst, "UNKNOWN"); + break; + } +} + +static void power_mode_to_str(char *dst, struct nlattr *a) +{ + enum nl80211_mesh_power_mode pm = nla_get_u32(a); + + switch (pm) { + case NL80211_MESH_POWER_ACTIVE: + strcpy(dst, "ACTIVE"); + break; + case NL80211_MESH_POWER_LIGHT_SLEEP: + strcpy(dst, "LIGHT SLEEP"); + break; + case NL80211_MESH_POWER_DEEP_SLEEP: + strcpy(dst, "DEEP SLEEP"); + break; + default: + strcpy(dst, "UNKNOWN"); + break; + } +} + static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg) { struct nl80211_array_buf *arr = arg; @@ -1644,13 +1950,24 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg) [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED }, [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED }, [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 }, + [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 }, [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 }, [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 }, [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 }, [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 }, + [NL80211_STA_INFO_CONNECTED_TIME]= { .type = NLA_U32 }, + [NL80211_STA_INFO_RX_DROP_MISC] = { .type = NLA_U64 }, [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 }, [NL80211_STA_INFO_STA_FLAGS] = { .minlen = sizeof(struct nl80211_sta_flag_update) }, + [NL80211_STA_INFO_EXPECTED_THROUGHPUT] = { .type = NLA_U32 }, + /* mesh */ + [NL80211_STA_INFO_LLID] = { .type = NLA_U16 }, + [NL80211_STA_INFO_PLID] = { .type = NLA_U16 }, + [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 }, + [NL80211_STA_INFO_LOCAL_PM] = { .type = NLA_U32 }, + [NL80211_STA_INFO_PEER_PM] = { .type = NLA_U32 }, + [NL80211_STA_INFO_NONPEER_PM] = { .type = NLA_U32 }, }; static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = { @@ -1674,9 +1991,15 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg) if (sinfo[NL80211_STA_INFO_SIGNAL]) e->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]); + if (sinfo[NL80211_STA_INFO_SIGNAL_AVG]) + e->signal_avg = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]); + if (sinfo[NL80211_STA_INFO_INACTIVE_TIME]) e->inactive = nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]); + if (sinfo[NL80211_STA_INFO_CONNECTED_TIME]) + e->connected_time = nla_get_u32(sinfo[NL80211_STA_INFO_CONNECTED_TIME]); + if (sinfo[NL80211_STA_INFO_RX_PACKETS]) e->rx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]); @@ -1686,38 +2009,12 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg) if (sinfo[NL80211_STA_INFO_RX_BITRATE] && !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy)) - { - if (rinfo[NL80211_RATE_INFO_BITRATE]) - e->rx_rate.rate = - nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]) * 100; - - if (rinfo[NL80211_RATE_INFO_MCS]) - e->rx_rate.mcs = nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]); - - if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH]) - e->rx_rate.is_40mhz = 1; - - if (rinfo[NL80211_RATE_INFO_SHORT_GI]) - e->rx_rate.is_short_gi = 1; - } + nl80211_parse_rateinfo(rinfo, &e->rx_rate); if (sinfo[NL80211_STA_INFO_TX_BITRATE] && !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) - { - if (rinfo[NL80211_RATE_INFO_BITRATE]) - e->tx_rate.rate = - nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]) * 100; - - if (rinfo[NL80211_RATE_INFO_MCS]) - e->tx_rate.mcs = nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]); - - if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH]) - e->tx_rate.is_40mhz = 1; - - if (rinfo[NL80211_RATE_INFO_SHORT_GI]) - e->tx_rate.is_short_gi = 1; - } + nl80211_parse_rateinfo(rinfo, &e->tx_rate); if (sinfo[NL80211_STA_INFO_RX_BYTES]) e->rx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]); @@ -1734,6 +2031,30 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg) if (sinfo[NL80211_STA_INFO_T_OFFSET]) e->t_offset = nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]); + if (sinfo[NL80211_STA_INFO_RX_DROP_MISC]) + e->rx_drop_misc = nla_get_u64(sinfo[NL80211_STA_INFO_RX_DROP_MISC]); + + if (sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]) + e->thr = nla_get_u32(sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]); + + /* mesh */ + if (sinfo[NL80211_STA_INFO_LLID]) + e->llid = nla_get_u16(sinfo[NL80211_STA_INFO_LLID]); + + if (sinfo[NL80211_STA_INFO_PLID]) + e->plid = nla_get_u16(sinfo[NL80211_STA_INFO_PLID]); + + if (sinfo[NL80211_STA_INFO_PLINK_STATE]) + plink_state_to_str(e->plink_state, + nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE])); + + if (sinfo[NL80211_STA_INFO_LOCAL_PM]) + power_mode_to_str(e->local_ps, sinfo[NL80211_STA_INFO_LOCAL_PM]); + if (sinfo[NL80211_STA_INFO_PEER_PM]) + power_mode_to_str(e->peer_ps, sinfo[NL80211_STA_INFO_PEER_PM]); + if (sinfo[NL80211_STA_INFO_NONPEER_PM]) + power_mode_to_str(e->nonpeer_ps, sinfo[NL80211_STA_INFO_NONPEER_PM]); + /* Station flags */ if (sinfo[NL80211_STA_INFO_STA_FLAGS]) { @@ -1772,12 +2093,26 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg) return NL_SKIP; } +static int nl80211_get_survey(const char *ifname, char *buf, int *len) +{ + struct nl80211_array_buf arr = { .buf = buf, .count = 0 }; + int rc; + + rc = nl80211_request(ifname, NL80211_CMD_GET_SURVEY, + NLM_F_DUMP, nl80211_get_survey_cb, &arr); + if (!rc) + *len = (arr.count * sizeof(struct iwinfo_survey_entry)); + else + *len = 0; + + return 0; +} + static int nl80211_get_assoclist(const char *ifname, char *buf, int *len) { DIR *d; int i, noise = 0; struct dirent *de; - struct nl80211_msg_conveyor *req; struct nl80211_array_buf arr = { .buf = buf, .count = 0 }; struct iwinfo_assoclist_entry *e; @@ -1789,14 +2124,8 @@ static int nl80211_get_assoclist(const char *ifname, char *buf, int *len) (!de->d_name[strlen(ifname)] || !strncmp(&de->d_name[strlen(ifname)], ".sta", 4))) { - req = nl80211_msg(de->d_name, NL80211_CMD_GET_STATION, - NLM_F_DUMP); - - if (req) - { - nl80211_send(req, nl80211_get_assoclist_cb, &arr); - nl80211_free(req); - } + nl80211_request(de->d_name, NL80211_CMD_GET_STATION, + NLM_F_DUMP, nl80211_get_assoclist_cb, &arr); } } @@ -1864,7 +2193,7 @@ static int nl80211_get_txpwrlist_cb(struct nl_msg *msg, void *arg) static int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len) { - int ch_cur; + int err, ch_cur; int dbm_max = -1, dbm_cur, dbm_cnt; struct nl80211_msg_conveyor *req; struct iwinfo_txpwrlist_entry entry; @@ -1872,17 +2201,13 @@ static int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len) if (nl80211_get_channel(ifname, &ch_cur)) ch_cur = 0; - req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0); - if (req) - { - /* initialize the value pointer with channel for callback */ - dbm_max = ch_cur; + /* initialize the value pointer with channel for callback */ + dbm_max = ch_cur; - nl80211_send(req, nl80211_get_txpwrlist_cb, &dbm_max); - nl80211_free(req); - } + err = nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0, + nl80211_get_txpwrlist_cb, &dbm_max); - if (dbm_max > 0) + if (!err) { for (dbm_cur = 0, dbm_cnt = 0; dbm_cur < dbm_max; @@ -1907,53 +2232,39 @@ static int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len) return -1; } -static void nl80211_get_scancrypto(const char *spec, - struct iwinfo_crypto_entry *c) +static void nl80211_get_scancrypto(char *spec, struct iwinfo_crypto_entry *c) { - if (strstr(spec, "WPA") || strstr(spec, "WEP")) - { - c->enabled = 1; - - if (strstr(spec, "WPA2-") && strstr(spec, "WPA-")) - c->wpa_version = 3; + int wpa_version = 0; + char *p, *q, *proto, *suites; - else if (strstr(spec, "WPA2")) - c->wpa_version = 2; - - else if (strstr(spec, "WPA")) - c->wpa_version = 1; - - else if (strstr(spec, "WEP")) - c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED; - - - if (strstr(spec, "PSK")) - c->auth_suites |= IWINFO_KMGMT_PSK; - - if (strstr(spec, "802.1X") || strstr(spec, "EAP")) - c->auth_suites |= IWINFO_KMGMT_8021x; - - if (strstr(spec, "WPA-NONE")) - c->auth_suites |= IWINFO_KMGMT_NONE; + c->enabled = 0; + for (p = strtok_r(spec, "[]", &q); p; p = strtok_r(NULL, "[]", &q)) { + if (!strcmp(p, "WEP")) { + c->enabled = 1; + c->auth_suites = IWINFO_KMGMT_NONE; + c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED; + c->pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104; + break; + } - if (strstr(spec, "TKIP")) - c->pair_ciphers |= IWINFO_CIPHER_TKIP; + proto = strtok(p, "-"); + suites = strtok(NULL, "]"); - if (strstr(spec, "CCMP")) - c->pair_ciphers |= IWINFO_CIPHER_CCMP; + if (!proto || !suites) + continue; - if (strstr(spec, "WEP-40")) - c->pair_ciphers |= IWINFO_CIPHER_WEP40; + if (!strcmp(proto, "WPA2") || !strcmp(proto, "RSN")) + wpa_version = 2; + else if (!strcmp(proto, "WPA")) + wpa_version = 1; + else + continue; - if (strstr(spec, "WEP-104")) - c->pair_ciphers |= IWINFO_CIPHER_WEP104; + c->enabled = 1; - c->group_ciphers = c->pair_ciphers; - } - else - { - c->enabled = 0; + parse_wpa_suites(suites, wpa_version, &c->wpa_version, &c->auth_suites); + parse_wpa_ciphers(suites, &c->pair_ciphers); } } @@ -1977,9 +2288,12 @@ static void nl80211_get_scanlist_ie(struct nlattr **bss, switch (ie[0]) { case 0: /* SSID */ - len = min(ie[1], IWINFO_ESSID_MAX_SIZE); - memcpy(e->ssid, ie + 2, len); - e->ssid[len] = 0; + case 114: /* Mesh ID */ + if (e->ssid[0] == 0) { + len = min(ie[1], IWINFO_ESSID_MAX_SIZE); + memcpy(e->ssid, ie + 2, len); + e->ssid[len] = 0; + } break; case 48: /* RSN */ @@ -2085,27 +2399,25 @@ static int nl80211_get_scanlist_cb(struct nl_msg *msg, void *arg) static int nl80211_get_scanlist_nl(const char *ifname, char *buf, int *len) { - struct nl80211_msg_conveyor *req; struct nl80211_scanlist sl = { .e = (struct iwinfo_scanlist_entry *)buf }; - req = nl80211_msg(ifname, NL80211_CMD_TRIGGER_SCAN, 0); - if (req) - { - nl80211_send(req, NULL, NULL); - nl80211_free(req); - } + if (nl80211_request(ifname, NL80211_CMD_TRIGGER_SCAN, 0, NULL, NULL)) + goto out; - nl80211_wait("nl80211", "scan", NL80211_CMD_NEW_SCAN_RESULTS); + if (nl80211_wait("nl80211", "scan", + NL80211_CMD_NEW_SCAN_RESULTS, NL80211_CMD_SCAN_ABORTED)) + goto out; - req = nl80211_msg(ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP); - if (req) - { - nl80211_send(req, nl80211_get_scanlist_cb, &sl); - nl80211_free(req); - } + if (nl80211_request(ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP, + nl80211_get_scanlist_cb, &sl)) + goto out; *len = sl.len * sizeof(struct iwinfo_scanlist_entry); - return *len ? 0 : -1; + return 0; + +out: + *len = 0; + return -1; } static int wpasupp_ssid_decode(const char *in, char *out, int outlen) @@ -2208,11 +2520,27 @@ static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len) /* is another unrelated event, retry */ tries--; } + + /* scanning already in progress, keep awaiting results */ + else if (!strcmp(reply, "FAIL-BUSY\n")) + { + tries--; + } + + /* another failure, abort */ + else if (!strncmp(reply, "FAIL-", 5)) + { + break; + } } /* receive and parse scan results if the wait above didn't time out */ - if (ready && nl80211_wpactl_recv(sock, reply, sizeof(reply)) > 0) + while (ready && nl80211_wpactl_recv(sock, reply, sizeof(reply)) > 0) { + /* received an event notification, receive again */ + if (reply[0] == '<') + continue; + nl80211_get_quality_max(ifname, &qmax); for (line = strtok_r(reply, "\n", &pos); @@ -2290,6 +2618,7 @@ static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len) } *len = count * sizeof(struct iwinfo_scanlist_entry); + break; } close(sock); @@ -2393,37 +2722,57 @@ static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg) int bands_remain, freqs_remain; struct nl80211_array_buf *arr = arg; - struct iwinfo_freqlist_entry *e = arr->buf; + struct iwinfo_freqlist_entry *e; struct nlattr **attr = nl80211_parse(msg); struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1]; struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1]; struct nlattr *band, *freq; - nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain) - { - nla_parse(bands, NL80211_BAND_ATTR_MAX, - nla_data(band), nla_len(band), NULL); + e = arr->buf; + e += arr->count; - nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain) + if (attr[NL80211_ATTR_WIPHY_BANDS]) { + nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain) { - nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX, - nla_data(freq), nla_len(freq), NULL); - - if (!freqs[NL80211_FREQUENCY_ATTR_FREQ] || - freqs[NL80211_FREQUENCY_ATTR_DISABLED]) - continue; - - e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]); - e->channel = nl80211_freq2channel(e->mhz); - - e->restricted = ( - freqs[NL80211_FREQUENCY_ATTR_NO_IR] && - !freqs[NL80211_FREQUENCY_ATTR_RADAR] - ) ? 1 : 0; + nla_parse(bands, NL80211_BAND_ATTR_MAX, + nla_data(band), nla_len(band), NULL); - e++; - arr->count++; + if (bands[NL80211_BAND_ATTR_FREQS]) { + nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain) + { + nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX, + nla_data(freq), nla_len(freq), NULL); + + if (!freqs[NL80211_FREQUENCY_ATTR_FREQ] || + freqs[NL80211_FREQUENCY_ATTR_DISABLED]) + continue; + + e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]); + e->channel = nl80211_freq2channel(e->mhz); + + e->restricted = ( + freqs[NL80211_FREQUENCY_ATTR_NO_IR] && + !freqs[NL80211_FREQUENCY_ATTR_RADAR] + ) ? 1 : 0; + + if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS]) + e->flags |= IWINFO_FREQ_NO_HT40MINUS; + if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS]) + e->flags |= IWINFO_FREQ_NO_HT40PLUS; + if (freqs[NL80211_FREQUENCY_ATTR_NO_80MHZ]) + e->flags |= IWINFO_FREQ_NO_80MHZ; + if (freqs[NL80211_FREQUENCY_ATTR_NO_160MHZ]) + e->flags |= IWINFO_FREQ_NO_160MHZ; + if (freqs[NL80211_FREQUENCY_ATTR_NO_20MHZ]) + e->flags |= IWINFO_FREQ_NO_20MHZ; + if (freqs[NL80211_FREQUENCY_ATTR_NO_10MHZ]) + e->flags |= IWINFO_FREQ_NO_10MHZ; + + e++; + arr->count++; + } + } } } @@ -2432,22 +2781,27 @@ static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg) static int nl80211_get_freqlist(const char *ifname, char *buf, int *len) { - struct nl80211_msg_conveyor *req; + struct nl80211_msg_conveyor *cv; struct nl80211_array_buf arr = { .buf = buf, .count = 0 }; + uint32_t features = nl80211_get_protocol_features(ifname); + int flags; - req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0); - if (req) - { - nl80211_send(req, nl80211_get_freqlist_cb, &arr); - nl80211_free(req); - } + flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0; + cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags); + if (!cv) + goto out; - if (arr.count > 0) - { - *len = arr.count * sizeof(struct iwinfo_freqlist_entry); - return 0; - } + NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP); + if (nl80211_send(cv, nl80211_get_freqlist_cb, &arr)) + goto out; + *len = arr.count * sizeof(struct iwinfo_freqlist_entry); + return 0; + +nla_put_failure: + nl80211_free(cv); +out: + *len = 0; return -1; } @@ -2466,20 +2820,11 @@ static int nl80211_get_country_cb(struct nl_msg *msg, void *arg) static int nl80211_get_country(const char *ifname, char *buf) { - int rv = -1; - struct nl80211_msg_conveyor *req; - - req = nl80211_msg(ifname, NL80211_CMD_GET_REG, 0); - if (req) - { - nl80211_send(req, nl80211_get_country_cb, buf); - nl80211_free(req); - - if (buf[0]) - rv = 0; - } + if (nl80211_request(ifname, NL80211_CMD_GET_REG, 0, + nl80211_get_country_cb, buf)) + return -1; - return rv; + return 0; } static int nl80211_get_countrylist(const char *ifname, char *buf, int *len) @@ -2574,6 +2919,10 @@ static int nl80211_get_modelist_cb(struct nl_msg *msg, void *arg) } } } + else if (nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]) >= 56160) + { + m->hw |= IWINFO_80211_AD; + } else if (!(m->hw & IWINFO_80211_AC)) { m->hw |= IWINFO_80211_A; @@ -2589,43 +2938,39 @@ static int nl80211_get_modelist_cb(struct nl_msg *msg, void *arg) static int nl80211_get_hwmodelist(const char *ifname, int *buf) { - struct nl80211_msg_conveyor *req; struct nl80211_modes m = { 0 }; - req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0); - if (req) - { - nl80211_send(req, nl80211_get_modelist_cb, &m); - nl80211_free(req); - } + if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0, + nl80211_get_modelist_cb, &m)) + goto out; - if (m.ok) - { - *buf = m.hw; - return 0; - } + if (!m.ok) + goto out; + *buf = m.hw; + return 0; + +out: + *buf = 0; return -1; } static int nl80211_get_htmodelist(const char *ifname, int *buf) { - struct nl80211_msg_conveyor *req; struct nl80211_modes m = { 0 }; - req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0); - if (req) - { - nl80211_send(req, nl80211_get_modelist_cb, &m); - nl80211_free(req); - } + if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0, + nl80211_get_modelist_cb, &m)) + goto out; - if (m.ok) - { - *buf = m.ht; - return 0; - } + if (!m.ok) + goto out; + *buf = m.ht; + return 0; + +out: + *buf = 0; return -1; } @@ -2685,50 +3030,46 @@ static int nl80211_get_ifcomb_cb(struct nl_msg *msg, void *arg) static int nl80211_get_mbssid_support(const char *ifname, int *buf) { - struct nl80211_msg_conveyor *req; - - req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0); - if (!req) + if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0, + nl80211_get_ifcomb_cb, buf)) return -1; - nl80211_send(req, nl80211_get_ifcomb_cb, buf); - nl80211_free(req); return 0; } static int nl80211_get_hardware_id(const char *ifname, char *buf) { - int rv = -1; - char *res; + struct iwinfo_hardware_id *id = (struct iwinfo_hardware_id *)buf; + char *phy, num[8], path[PATH_MAX]; + int i; - /* Got a radioX pseudo interface, find some interface on it or create one */ - if (!strncmp(ifname, "radio", 5)) - { - /* Reuse existing interface */ - if ((res = nl80211_phy2ifname(ifname)) != NULL) - { - rv = wext_ops.hardware_id(res, buf); - } + struct { const char *path; uint16_t *dest; } lookup[] = { + { "vendor", &id->vendor_id }, + { "device", &id->device_id }, + { "subsystem_vendor", &id->subsystem_vendor_id }, + { "subsystem_device", &id->subsystem_device_id } + }; - /* Need to spawn a temporary iface for finding IDs */ - else if ((res = nl80211_ifadd(ifname)) != NULL) - { - rv = wext_ops.hardware_id(res, buf); - nl80211_ifdel(res); - } - } - else + memset(id, 0, sizeof(*id)); + + /* Try to determine the phy name from the given interface */ + phy = nl80211_ifname2phy(ifname); + + for (i = 0; i < ARRAY_SIZE(lookup); i++) { - rv = wext_ops.hardware_id(ifname, buf); + snprintf(path, sizeof(path), "/sys/class/%s/%s/device/%s", + phy ? "ieee80211" : "net", + phy ? phy : ifname, lookup[i].path); + + if (nl80211_readstr(path, num, sizeof(num)) > 0) + *lookup[i].dest = strtoul(num, NULL, 16); } /* Failed to obtain hardware IDs, search board config */ - if (rv) - { - rv = iwinfo_hardware_id_from_mtd((struct iwinfo_hardware_id *)buf); - } + if (id->vendor_id == 0 || id->device_id == 0) + return iwinfo_hardware_id_from_mtd(id); - return rv; + return 0; } static const struct iwinfo_hardware_entry * @@ -2816,6 +3157,7 @@ const struct iwinfo_ops nl80211_ops = { .scanlist = nl80211_get_scanlist, .freqlist = nl80211_get_freqlist, .countrylist = nl80211_get_countrylist, + .survey = nl80211_get_survey, .lookup_phy = nl80211_lookup_phyname, .close = nl80211_close };