wlantest: Add functionality for fetching STA/BSS information

This commit is contained in:
Jouni Malinen 2010-11-21 18:01:18 +02:00
parent 221519dea3
commit 2fc0cd54db
3 changed files with 514 additions and 2 deletions

View File

@ -17,6 +17,7 @@
#include "utils/common.h"
#include "utils/eloop.h"
#include "common/defs.h"
#include "common/version.h"
#include "common/ieee802_11_defs.h"
#include "wlantest.h"
@ -741,6 +742,271 @@ static void ctrl_add_passphrase(struct wlantest *wt, int sock, u8 *cmd,
}
static void info_print_proto(char *buf, size_t len, int proto)
{
char *pos, *end;
if (proto == 0) {
os_snprintf(buf, len, "OPEN");
return;
}
pos = buf;
end = buf + len;
if (proto & WPA_PROTO_WPA)
pos += os_snprintf(pos, end - pos, "%sWPA",
pos == buf ? "" : " ");
if (proto & WPA_PROTO_RSN)
pos += os_snprintf(pos, end - pos, "%sWPA2",
pos == buf ? "" : " ");
}
static void info_print_cipher(char *buf, size_t len, int cipher)
{
char *pos, *end;
if (cipher == 0) {
os_snprintf(buf, len, "N/A");
return;
}
pos = buf;
end = buf + len;
if (cipher & WPA_CIPHER_NONE)
pos += os_snprintf(pos, end - pos, "%sNONE",
pos == buf ? "" : " ");
if (cipher & WPA_CIPHER_WEP40)
pos += os_snprintf(pos, end - pos, "%sWEP40",
pos == buf ? "" : " ");
if (cipher & WPA_CIPHER_WEP104)
pos += os_snprintf(pos, end - pos, "%sWEP104",
pos == buf ? "" : " ");
if (cipher & WPA_CIPHER_TKIP)
pos += os_snprintf(pos, end - pos, "%sTKIP",
pos == buf ? "" : " ");
if (cipher & WPA_CIPHER_CCMP)
pos += os_snprintf(pos, end - pos, "%sCCMP",
pos == buf ? "" : " ");
if (cipher & WPA_CIPHER_AES_128_CMAC)
pos += os_snprintf(pos, end - pos, "%sBIP",
pos == buf ? "" : " ");
}
static void info_print_key_mgmt(char *buf, size_t len, int key_mgmt)
{
char *pos, *end;
if (key_mgmt == 0) {
os_snprintf(buf, len, "N/A");
return;
}
pos = buf;
end = buf + len;
if (key_mgmt & WPA_KEY_MGMT_IEEE8021X)
pos += os_snprintf(pos, end - pos, "%sEAP",
pos == buf ? "" : " ");
if (key_mgmt & WPA_KEY_MGMT_PSK)
pos += os_snprintf(pos, end - pos, "%sPSK",
pos == buf ? "" : " ");
if (key_mgmt & WPA_KEY_MGMT_WPA_NONE)
pos += os_snprintf(pos, end - pos, "%sWPA-NONE",
pos == buf ? "" : " ");
if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
pos += os_snprintf(pos, end - pos, "%sFT-EAP",
pos == buf ? "" : " ");
if (key_mgmt & WPA_KEY_MGMT_FT_PSK)
pos += os_snprintf(pos, end - pos, "%sFT-PSK",
pos == buf ? "" : " ");
if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
pos += os_snprintf(pos, end - pos, "%sEAP-SHA256",
pos == buf ? "" : " ");
if (key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
pos += os_snprintf(pos, end - pos, "%sPSK-SHA256",
pos == buf ? "" : " ");
}
static void info_print_rsn_capab(char *buf, size_t len, int capab)
{
char *pos, *end;
pos = buf;
end = buf + len;
if (capab & WPA_CAPABILITY_PREAUTH)
pos += os_snprintf(pos, end - pos, "%sPREAUTH",
pos == buf ? "" : " ");
if (capab & WPA_CAPABILITY_NO_PAIRWISE)
pos += os_snprintf(pos, end - pos, "%sNO_PAIRWISE",
pos == buf ? "" : " ");
if (capab & WPA_CAPABILITY_MFPR)
pos += os_snprintf(pos, end - pos, "%sMFPR",
pos == buf ? "" : " ");
if (capab & WPA_CAPABILITY_MFPC)
pos += os_snprintf(pos, end - pos, "%sMFPC",
pos == buf ? "" : " ");
if (capab & WPA_CAPABILITY_PEERKEY_ENABLED)
pos += os_snprintf(pos, end - pos, "%sPEERKEY",
pos == buf ? "" : " ");
}
static void info_print_state(char *buf, size_t len, int state)
{
switch (state) {
case STATE1:
os_strlcpy(buf, "NOT-AUTH", len);
break;
case STATE2:
os_strlcpy(buf, "AUTH", len);
break;
case STATE3:
os_strlcpy(buf, "AUTH+ASSOC", len);
break;
}
}
static void ctrl_info_sta(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
{
u8 *addr;
size_t addr_len;
struct wlantest_bss *bss;
struct wlantest_sta *sta;
enum wlantest_sta_info info;
u8 buf[4 + 108], *end, *pos;
char resp[100];
addr = attr_get(cmd, clen, WLANTEST_ATTR_BSSID, &addr_len);
if (addr == NULL || addr_len != ETH_ALEN) {
ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
return;
}
bss = bss_find(wt, addr);
if (bss == NULL) {
ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
return;
}
addr = attr_get(cmd, clen, WLANTEST_ATTR_STA_ADDR, &addr_len);
if (addr == NULL || addr_len != ETH_ALEN) {
ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
return;
}
sta = sta_find(bss, addr);
if (sta == NULL) {
ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
return;
}
addr = attr_get(cmd, clen, WLANTEST_ATTR_STA_INFO, &addr_len);
if (addr == NULL || addr_len != 4) {
ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
return;
}
info = WPA_GET_BE32(addr);
resp[0] = '\0';
switch (info) {
case WLANTEST_STA_INFO_PROTO:
info_print_proto(resp, sizeof(resp), sta->proto);
break;
case WLANTEST_STA_INFO_PAIRWISE:
info_print_cipher(resp, sizeof(resp), sta->pairwise_cipher);
break;
case WLANTEST_STA_INFO_KEY_MGMT:
info_print_key_mgmt(resp, sizeof(resp), sta->key_mgmt);
break;
case WLANTEST_STA_INFO_RSN_CAPAB:
info_print_rsn_capab(resp, sizeof(resp), sta->rsn_capab);
break;
case WLANTEST_STA_INFO_STATE:
info_print_state(resp, sizeof(resp), sta->state);
break;
default:
ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
return;
}
pos = buf;
end = buf + sizeof(buf);
WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
pos += 4;
pos = attr_add_str(pos, end, WLANTEST_ATTR_INFO, resp);
ctrl_send(wt, sock, buf, pos - buf);
}
static void ctrl_info_bss(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
{
u8 *addr;
size_t addr_len;
struct wlantest_bss *bss;
enum wlantest_bss_info info;
u8 buf[4 + 108], *end, *pos;
char resp[100];
addr = attr_get(cmd, clen, WLANTEST_ATTR_BSSID, &addr_len);
if (addr == NULL || addr_len != ETH_ALEN) {
ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
return;
}
bss = bss_find(wt, addr);
if (bss == NULL) {
ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
return;
}
addr = attr_get(cmd, clen, WLANTEST_ATTR_BSS_INFO, &addr_len);
if (addr == NULL || addr_len != 4) {
ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
return;
}
info = WPA_GET_BE32(addr);
resp[0] = '\0';
switch (info) {
case WLANTEST_BSS_INFO_PROTO:
info_print_proto(resp, sizeof(resp), bss->proto);
break;
case WLANTEST_BSS_INFO_PAIRWISE:
info_print_cipher(resp, sizeof(resp), bss->pairwise_cipher);
break;
case WLANTEST_BSS_INFO_GROUP:
info_print_cipher(resp, sizeof(resp), bss->group_cipher);
break;
case WLANTEST_BSS_INFO_GROUP_MGMT:
info_print_cipher(resp, sizeof(resp), bss->mgmt_group_cipher);
break;
case WLANTEST_BSS_INFO_KEY_MGMT:
info_print_key_mgmt(resp, sizeof(resp), bss->key_mgmt);
break;
case WLANTEST_BSS_INFO_RSN_CAPAB:
info_print_rsn_capab(resp, sizeof(resp), bss->rsn_capab);
break;
default:
ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
return;
}
pos = buf;
end = buf + sizeof(buf);
WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
pos += 4;
pos = attr_add_str(pos, end, WLANTEST_ATTR_INFO, resp);
ctrl_send(wt, sock, buf, pos - buf);
}
static void ctrl_read(int sock, void *eloop_ctx, void *sock_ctx)
{
struct wlantest *wt = eloop_ctx;
@ -809,6 +1075,12 @@ static void ctrl_read(int sock, void *eloop_ctx, void *sock_ctx)
case WLANTEST_CTRL_ADD_PASSPHRASE:
ctrl_add_passphrase(wt, sock, buf + 4, len - 4);
break;
case WLANTEST_CTRL_INFO_STA:
ctrl_info_sta(wt, sock, buf + 4, len - 4);
break;
case WLANTEST_CTRL_INFO_BSS:
ctrl_info_bss(wt, sock, buf + 4, len - 4);
break;
default:
ctrl_send_simple(wt, sock, WLANTEST_CTRL_UNKNOWN_CMD);
break;

View File

@ -578,7 +578,7 @@ static int cmd_get_bss_counter(int s, int argc, char *argv[])
size_t len;
if (argc != 2) {
printf("get_bss_counter needs at three arguments: "
printf("get_bss_counter needs at two arguments: "
"counter name and BSSID\n");
return -1;
}
@ -878,6 +878,218 @@ static int cmd_add_passphrase(int s, int argc, char *argv[])
}
struct sta_infos {
const char *name;
enum wlantest_sta_info num;
};
static const struct sta_infos sta_infos[] = {
{ "proto", WLANTEST_STA_INFO_PROTO },
{ "pairwise", WLANTEST_STA_INFO_PAIRWISE },
{ "key_mgmt", WLANTEST_STA_INFO_KEY_MGMT },
{ "rsn_capab", WLANTEST_STA_INFO_RSN_CAPAB },
{ "state", WLANTEST_STA_INFO_STATE },
{ NULL, 0 }
};
static int cmd_info_sta(int s, int argc, char *argv[])
{
u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
u8 buf[100], *end, *pos;
int rlen, i;
size_t len;
char info[100];
if (argc != 3) {
printf("sta_info needs at three arguments: "
"counter name, BSSID, and STA address\n");
return -1;
}
pos = buf;
end = buf + sizeof(buf);
WPA_PUT_BE32(pos, WLANTEST_CTRL_INFO_STA);
pos += 4;
for (i = 0; sta_infos[i].name; i++) {
if (os_strcasecmp(sta_infos[i].name, argv[0]) == 0)
break;
}
if (sta_infos[i].name == NULL) {
printf("Unknown STA info '%s'\n", argv[0]);
printf("Info fields:");
for (i = 0; sta_infos[i].name; i++)
printf(" %s", sta_infos[i].name);
printf("\n");
return -1;
}
pos = attr_add_be32(pos, end, WLANTEST_ATTR_STA_INFO,
sta_infos[i].num);
pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
if (hwaddr_aton(argv[1], pos) < 0) {
printf("Invalid BSSID '%s'\n", argv[1]);
return -1;
}
pos += ETH_ALEN;
pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN);
if (hwaddr_aton(argv[2], pos) < 0) {
printf("Invalid STA address '%s'\n", argv[2]);
return -1;
}
pos += ETH_ALEN;
rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
if (rlen < 0)
return -1;
pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_INFO, &len);
if (pos == NULL)
return -1;
if (len >= sizeof(info))
len = sizeof(info) - 1;
os_memcpy(info, pos, len);
info[len] = '\0';
printf("%s\n", info);
return 0;
}
static char ** complete_info_sta(int s, const char *str, int pos)
{
int arg = get_cmd_arg_num(str, pos);
char **res = NULL;
int i, count;
u8 addr[ETH_ALEN];
switch (arg) {
case 1:
/* counter list */
count = sizeof(sta_infos) / sizeof(sta_infos[0]);
res = os_zalloc(count * sizeof(char *));
if (res == NULL)
return NULL;
for (i = 0; sta_infos[i].name; i++) {
res[i] = os_strdup(sta_infos[i].name);
if (res[i] == NULL)
break;
}
break;
case 2:
res = get_bssid_list(s);
break;
case 3:
if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
break;
res = get_sta_list(s, addr, 0);
break;
}
return res;
}
struct bss_infos {
const char *name;
enum wlantest_bss_info num;
};
static const struct bss_infos bss_infos[] = {
{ "proto", WLANTEST_BSS_INFO_PROTO },
{ "pairwise", WLANTEST_BSS_INFO_PAIRWISE },
{ "group", WLANTEST_BSS_INFO_GROUP },
{ "group_mgmt", WLANTEST_BSS_INFO_GROUP_MGMT },
{ "key_mgmt", WLANTEST_BSS_INFO_KEY_MGMT },
{ "rsn_capab", WLANTEST_BSS_INFO_RSN_CAPAB },
{ NULL, 0 }
};
static int cmd_info_bss(int s, int argc, char *argv[])
{
u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
u8 buf[100], *end, *pos;
int rlen, i;
size_t len;
char info[100];
if (argc != 2) {
printf("bss_info needs at two arguments: "
"field name and BSSID\n");
return -1;
}
pos = buf;
end = buf + sizeof(buf);
WPA_PUT_BE32(pos, WLANTEST_CTRL_INFO_BSS);
pos += 4;
for (i = 0; bss_infos[i].name; i++) {
if (os_strcasecmp(bss_infos[i].name, argv[0]) == 0)
break;
}
if (bss_infos[i].name == NULL) {
printf("Unknown BSS info '%s'\n", argv[0]);
printf("Info fields:");
for (i = 0; bss_infos[i].name; i++)
printf(" %s", bss_infos[i].name);
printf("\n");
return -1;
}
pos = attr_add_be32(pos, end, WLANTEST_ATTR_BSS_INFO,
bss_infos[i].num);
pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
if (hwaddr_aton(argv[1], pos) < 0) {
printf("Invalid BSSID '%s'\n", argv[1]);
return -1;
}
pos += ETH_ALEN;
rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
if (rlen < 0)
return -1;
pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_INFO, &len);
if (pos == NULL)
return -1;
if (len >= sizeof(info))
len = sizeof(info) - 1;
os_memcpy(info, pos, len);
info[len] = '\0';
printf("%s\n", info);
return 0;
}
static char ** complete_info_bss(int s, const char *str, int pos)
{
int arg = get_cmd_arg_num(str, pos);
char **res = NULL;
int i, count;
switch (arg) {
case 1:
/* counter list */
count = sizeof(bss_infos) / sizeof(bss_infos[0]);
res = os_zalloc(count * sizeof(char *));
if (res == NULL)
return NULL;
for (i = 0; bss_infos[i].name; i++) {
res[i] = os_strdup(bss_infos[i].name);
if (res[i] == NULL)
break;
}
break;
case 2:
res = get_bssid_list(s);
break;
}
return res;
}
struct wlantest_cli_cmd {
const char *cmd;
int (*handler)(int s, int argc, char *argv[]);
@ -898,7 +1110,7 @@ static const struct wlantest_cli_cmd wlantest_cli_commands[] = {
"<BSSID> = clear BSS counters", complete_clear_bss_counters },
{ "get_sta_counter", cmd_get_sta_counter,
"<counter> <BSSID> <STA> = get STA counter value",
complete_get_sta_counter},
complete_get_sta_counter },
{ "get_bss_counter", cmd_get_bss_counter,
"<counter> <BSSID> = get BSS counter value",
complete_get_bss_counter },
@ -908,6 +1120,12 @@ static const struct wlantest_cli_cmd wlantest_cli_commands[] = {
{ "version", cmd_version, "= get wlantest version", NULL },
{ "add_passphrase", cmd_add_passphrase,
"<passphrase> = add a known passphrase", NULL },
{ "info_sta", cmd_info_sta,
"<field> <BSSID> <STA> = get STA information",
complete_info_sta },
{ "info_bss", cmd_info_bss,
"<field> <BSSID> = get BSS information",
complete_info_bss },
{ NULL, NULL, NULL, NULL }
};

View File

@ -36,6 +36,8 @@ enum wlantest_ctrl_cmd {
WLANTEST_CTRL_INJECT,
WLANTEST_CTRL_VERSION,
WLANTEST_CTRL_ADD_PASSPHRASE,
WLANTEST_CTRL_INFO_STA,
WLANTEST_CTRL_INFO_BSS,
};
enum wlantest_ctrl_attr {
@ -49,6 +51,9 @@ enum wlantest_ctrl_attr {
WLANTEST_ATTR_INJECT_PROTECTION,
WLANTEST_ATTR_VERSION,
WLANTEST_ATTR_PASSPHRASE,
WLANTEST_ATTR_STA_INFO,
WLANTEST_ATTR_BSS_INFO,
WLANTEST_ATTR_INFO,
};
enum wlantest_bss_counter {
@ -107,4 +112,21 @@ enum wlantest_inject_protection {
WLANTEST_INJECT_INCORRECT_KEY,
};
enum wlantest_sta_info {
WLANTEST_STA_INFO_PROTO,
WLANTEST_STA_INFO_PAIRWISE,
WLANTEST_STA_INFO_KEY_MGMT,
WLANTEST_STA_INFO_RSN_CAPAB,
WLANTEST_STA_INFO_STATE,
};
enum wlantest_bss_info {
WLANTEST_BSS_INFO_PROTO,
WLANTEST_BSS_INFO_PAIRWISE,
WLANTEST_BSS_INFO_GROUP,
WLANTEST_BSS_INFO_GROUP_MGMT,
WLANTEST_BSS_INFO_KEY_MGMT,
WLANTEST_BSS_INFO_RSN_CAPAB,
};
#endif /* WLANTEST_CTRL_H */