b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | /* |
| 2 | * wlantest controller |
| 3 | * Copyright (c) 2010-2013, Jouni Malinen <j@w1.fi> |
| 4 | * |
| 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
| 7 | */ |
| 8 | |
| 9 | #include "utils/includes.h" |
| 10 | #include <sys/un.h> |
| 11 | |
| 12 | #include "utils/common.h" |
| 13 | #include "utils/eloop.h" |
| 14 | #include "utils/edit.h" |
| 15 | #include "common/cli.h" |
| 16 | #include "wlantest_ctrl.h" |
| 17 | |
| 18 | static void print_help(FILE *stream, const char *cmd); |
| 19 | static char ** wlantest_cli_cmd_list(void); |
| 20 | |
| 21 | |
| 22 | static int get_prev_arg_pos(const char *str, int pos) |
| 23 | { |
| 24 | while (pos > 0 && str[pos - 1] != ' ') |
| 25 | pos--; |
| 26 | while (pos > 0 && str[pos - 1] == ' ') |
| 27 | pos--; |
| 28 | while (pos > 0 && str[pos - 1] != ' ') |
| 29 | pos--; |
| 30 | return pos; |
| 31 | } |
| 32 | |
| 33 | |
| 34 | static u8 * attr_get(u8 *buf, size_t buflen, enum wlantest_ctrl_attr attr, |
| 35 | size_t *len) |
| 36 | { |
| 37 | u8 *pos = buf; |
| 38 | |
| 39 | while (pos + 8 <= buf + buflen) { |
| 40 | enum wlantest_ctrl_attr a; |
| 41 | size_t alen; |
| 42 | a = WPA_GET_BE32(pos); |
| 43 | pos += 4; |
| 44 | alen = WPA_GET_BE32(pos); |
| 45 | pos += 4; |
| 46 | if (pos + alen > buf + buflen) { |
| 47 | printf("Invalid control message attribute\n"); |
| 48 | return NULL; |
| 49 | } |
| 50 | if (a == attr) { |
| 51 | *len = alen; |
| 52 | return pos; |
| 53 | } |
| 54 | pos += alen; |
| 55 | } |
| 56 | |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | static u8 * attr_hdr_add(u8 *pos, u8 *end, enum wlantest_ctrl_attr attr, |
| 62 | size_t len) |
| 63 | { |
| 64 | if (pos == NULL || end - pos < 8 + len) |
| 65 | return NULL; |
| 66 | WPA_PUT_BE32(pos, attr); |
| 67 | pos += 4; |
| 68 | WPA_PUT_BE32(pos, len); |
| 69 | pos += 4; |
| 70 | return pos; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | static u8 * attr_add_str(u8 *pos, u8 *end, enum wlantest_ctrl_attr attr, |
| 75 | const char *str) |
| 76 | { |
| 77 | size_t len = os_strlen(str); |
| 78 | |
| 79 | if (pos == NULL || end - pos < 8 + len) |
| 80 | return NULL; |
| 81 | WPA_PUT_BE32(pos, attr); |
| 82 | pos += 4; |
| 83 | WPA_PUT_BE32(pos, len); |
| 84 | pos += 4; |
| 85 | os_memcpy(pos, str, len); |
| 86 | pos += len; |
| 87 | return pos; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | static u8 * attr_add_be32(u8 *pos, u8 *end, enum wlantest_ctrl_attr attr, |
| 92 | u32 val) |
| 93 | { |
| 94 | if (pos == NULL || end - pos < 12) |
| 95 | return NULL; |
| 96 | WPA_PUT_BE32(pos, attr); |
| 97 | pos += 4; |
| 98 | WPA_PUT_BE32(pos, 4); |
| 99 | pos += 4; |
| 100 | WPA_PUT_BE32(pos, val); |
| 101 | pos += 4; |
| 102 | return pos; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | static int cmd_send_and_recv(int s, const u8 *cmd, size_t cmd_len, |
| 107 | u8 *resp, size_t max_resp_len) |
| 108 | { |
| 109 | int res; |
| 110 | enum wlantest_ctrl_cmd cmd_resp; |
| 111 | |
| 112 | if (send(s, cmd, cmd_len, 0) < 0) |
| 113 | return -1; |
| 114 | res = recv(s, resp, max_resp_len, 0); |
| 115 | if (res < 4) |
| 116 | return -1; |
| 117 | |
| 118 | cmd_resp = WPA_GET_BE32(resp); |
| 119 | if (cmd_resp == WLANTEST_CTRL_SUCCESS) |
| 120 | return res; |
| 121 | |
| 122 | if (cmd_resp == WLANTEST_CTRL_UNKNOWN_CMD) |
| 123 | printf("Unknown command\n"); |
| 124 | else if (cmd_resp == WLANTEST_CTRL_INVALID_CMD) |
| 125 | printf("Invalid command\n"); |
| 126 | |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | static int cmd_simple(int s, enum wlantest_ctrl_cmd cmd) |
| 132 | { |
| 133 | u8 buf[4]; |
| 134 | int res; |
| 135 | WPA_PUT_BE32(buf, cmd); |
| 136 | res = cmd_send_and_recv(s, buf, sizeof(buf), buf, sizeof(buf)); |
| 137 | return res < 0 ? -1 : 0; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | static char ** get_bssid_list(int s) |
| 142 | { |
| 143 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 144 | u8 buf[4]; |
| 145 | u8 *bssid; |
| 146 | size_t len; |
| 147 | int rlen, i; |
| 148 | char **res; |
| 149 | |
| 150 | WPA_PUT_BE32(buf, WLANTEST_CTRL_LIST_BSS); |
| 151 | rlen = cmd_send_and_recv(s, buf, sizeof(buf), resp, sizeof(resp)); |
| 152 | if (rlen < 0) |
| 153 | return NULL; |
| 154 | |
| 155 | bssid = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_BSSID, &len); |
| 156 | if (bssid == NULL) |
| 157 | return NULL; |
| 158 | |
| 159 | res = os_calloc(len / ETH_ALEN + 1, sizeof(char *)); |
| 160 | if (res == NULL) |
| 161 | return NULL; |
| 162 | for (i = 0; i < len / ETH_ALEN; i++) { |
| 163 | res[i] = os_zalloc(18); |
| 164 | if (res[i] == NULL) |
| 165 | break; |
| 166 | os_snprintf(res[i], 18, MACSTR, MAC2STR(bssid + ETH_ALEN * i)); |
| 167 | } |
| 168 | |
| 169 | return res; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | static char ** get_sta_list(int s, const u8 *bssid, int add_bcast) |
| 174 | { |
| 175 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 176 | u8 buf[100], *pos, *end; |
| 177 | u8 *addr; |
| 178 | size_t len; |
| 179 | int rlen, i; |
| 180 | char **res; |
| 181 | |
| 182 | pos = buf; |
| 183 | end = buf + sizeof(buf); |
| 184 | WPA_PUT_BE32(pos, WLANTEST_CTRL_LIST_STA); |
| 185 | pos += 4; |
| 186 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN); |
| 187 | os_memcpy(pos, bssid, ETH_ALEN); |
| 188 | pos += ETH_ALEN; |
| 189 | rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp)); |
| 190 | if (rlen < 0) |
| 191 | return NULL; |
| 192 | |
| 193 | addr = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_STA_ADDR, &len); |
| 194 | if (addr == NULL) |
| 195 | return NULL; |
| 196 | |
| 197 | res = os_calloc(len / ETH_ALEN + 1 + add_bcast, sizeof(char *)); |
| 198 | if (res == NULL) |
| 199 | return NULL; |
| 200 | for (i = 0; i < len / ETH_ALEN; i++) { |
| 201 | res[i] = os_zalloc(18); |
| 202 | if (res[i] == NULL) |
| 203 | break; |
| 204 | os_snprintf(res[i], 18, MACSTR, MAC2STR(addr + ETH_ALEN * i)); |
| 205 | } |
| 206 | if (add_bcast) |
| 207 | res[i] = os_strdup("ff:ff:ff:ff:ff:ff"); |
| 208 | |
| 209 | return res; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | static int cmd_ping(int s, int argc, char *argv[]) |
| 214 | { |
| 215 | int res = cmd_simple(s, WLANTEST_CTRL_PING); |
| 216 | if (res == 0) |
| 217 | printf("PONG\n"); |
| 218 | return res == 0; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | static int cmd_terminate(int s, int argc, char *argv[]) |
| 223 | { |
| 224 | return cmd_simple(s, WLANTEST_CTRL_TERMINATE); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | static int cmd_list_bss(int s, int argc, char *argv[]) |
| 229 | { |
| 230 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 231 | u8 buf[4]; |
| 232 | u8 *bssid; |
| 233 | size_t len; |
| 234 | int rlen, i; |
| 235 | |
| 236 | WPA_PUT_BE32(buf, WLANTEST_CTRL_LIST_BSS); |
| 237 | rlen = cmd_send_and_recv(s, buf, sizeof(buf), resp, sizeof(resp)); |
| 238 | if (rlen < 0) |
| 239 | return -1; |
| 240 | |
| 241 | bssid = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_BSSID, &len); |
| 242 | if (bssid == NULL) |
| 243 | return -1; |
| 244 | |
| 245 | for (i = 0; i < len / ETH_ALEN; i++) |
| 246 | printf(MACSTR " ", MAC2STR(bssid + ETH_ALEN * i)); |
| 247 | printf("\n"); |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | static int cmd_list_sta(int s, int argc, char *argv[]) |
| 254 | { |
| 255 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 256 | u8 buf[100], *pos; |
| 257 | u8 *addr; |
| 258 | size_t len; |
| 259 | int rlen, i; |
| 260 | |
| 261 | if (argc < 1) { |
| 262 | printf("list_sta needs one argument: BSSID\n"); |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | pos = buf; |
| 267 | WPA_PUT_BE32(pos, WLANTEST_CTRL_LIST_STA); |
| 268 | pos += 4; |
| 269 | WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID); |
| 270 | pos += 4; |
| 271 | WPA_PUT_BE32(pos, ETH_ALEN); |
| 272 | pos += 4; |
| 273 | if (hwaddr_aton(argv[0], pos) < 0) { |
| 274 | printf("Invalid BSSID '%s'\n", argv[0]); |
| 275 | return -1; |
| 276 | } |
| 277 | pos += ETH_ALEN; |
| 278 | |
| 279 | rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp)); |
| 280 | if (rlen < 0) |
| 281 | return -1; |
| 282 | |
| 283 | addr = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_STA_ADDR, &len); |
| 284 | if (addr == NULL) |
| 285 | return -1; |
| 286 | |
| 287 | for (i = 0; i < len / ETH_ALEN; i++) |
| 288 | printf(MACSTR " ", MAC2STR(addr + ETH_ALEN * i)); |
| 289 | printf("\n"); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | |
| 295 | static char ** complete_list_sta(int s, const char *str, int pos) |
| 296 | { |
| 297 | if (get_cmd_arg_num(str, pos) == 1) |
| 298 | return get_bssid_list(s); |
| 299 | return NULL; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | static int cmd_flush(int s, int argc, char *argv[]) |
| 304 | { |
| 305 | return cmd_simple(s, WLANTEST_CTRL_FLUSH); |
| 306 | } |
| 307 | |
| 308 | |
| 309 | static int cmd_clear_sta_counters(int s, int argc, char *argv[]) |
| 310 | { |
| 311 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 312 | u8 buf[100], *pos; |
| 313 | int rlen; |
| 314 | |
| 315 | if (argc < 2) { |
| 316 | printf("clear_sta_counters needs two arguments: BSSID and " |
| 317 | "STA address\n"); |
| 318 | return -1; |
| 319 | } |
| 320 | |
| 321 | pos = buf; |
| 322 | WPA_PUT_BE32(pos, WLANTEST_CTRL_CLEAR_STA_COUNTERS); |
| 323 | pos += 4; |
| 324 | WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID); |
| 325 | pos += 4; |
| 326 | WPA_PUT_BE32(pos, ETH_ALEN); |
| 327 | pos += 4; |
| 328 | if (hwaddr_aton(argv[0], pos) < 0) { |
| 329 | printf("Invalid BSSID '%s'\n", argv[0]); |
| 330 | return -1; |
| 331 | } |
| 332 | pos += ETH_ALEN; |
| 333 | |
| 334 | WPA_PUT_BE32(pos, WLANTEST_ATTR_STA_ADDR); |
| 335 | pos += 4; |
| 336 | WPA_PUT_BE32(pos, ETH_ALEN); |
| 337 | pos += 4; |
| 338 | if (hwaddr_aton(argv[1], pos) < 0) { |
| 339 | printf("Invalid STA address '%s'\n", argv[1]); |
| 340 | return -1; |
| 341 | } |
| 342 | pos += ETH_ALEN; |
| 343 | |
| 344 | rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp)); |
| 345 | if (rlen < 0) |
| 346 | return -1; |
| 347 | printf("OK\n"); |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | |
| 352 | static char ** complete_clear_sta_counters(int s, const char *str, int pos) |
| 353 | { |
| 354 | int arg = get_cmd_arg_num(str, pos); |
| 355 | char **res = NULL; |
| 356 | u8 addr[ETH_ALEN]; |
| 357 | |
| 358 | switch (arg) { |
| 359 | case 1: |
| 360 | res = get_bssid_list(s); |
| 361 | break; |
| 362 | case 2: |
| 363 | if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0) |
| 364 | break; |
| 365 | res = get_sta_list(s, addr, 0); |
| 366 | break; |
| 367 | } |
| 368 | |
| 369 | return res; |
| 370 | } |
| 371 | |
| 372 | |
| 373 | static int cmd_clear_bss_counters(int s, int argc, char *argv[]) |
| 374 | { |
| 375 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 376 | u8 buf[100], *pos; |
| 377 | int rlen; |
| 378 | |
| 379 | if (argc < 1) { |
| 380 | printf("clear_bss_counters needs one argument: BSSID\n"); |
| 381 | return -1; |
| 382 | } |
| 383 | |
| 384 | pos = buf; |
| 385 | WPA_PUT_BE32(pos, WLANTEST_CTRL_CLEAR_BSS_COUNTERS); |
| 386 | pos += 4; |
| 387 | WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID); |
| 388 | pos += 4; |
| 389 | WPA_PUT_BE32(pos, ETH_ALEN); |
| 390 | pos += 4; |
| 391 | if (hwaddr_aton(argv[0], pos) < 0) { |
| 392 | printf("Invalid BSSID '%s'\n", argv[0]); |
| 393 | return -1; |
| 394 | } |
| 395 | pos += ETH_ALEN; |
| 396 | |
| 397 | rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp)); |
| 398 | if (rlen < 0) |
| 399 | return -1; |
| 400 | printf("OK\n"); |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | |
| 405 | static char ** complete_clear_bss_counters(int s, const char *str, int pos) |
| 406 | { |
| 407 | if (get_cmd_arg_num(str, pos) == 1) |
| 408 | return get_bssid_list(s); |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | |
| 413 | static int cmd_clear_tdls_counters(int s, int argc, char *argv[]) |
| 414 | { |
| 415 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 416 | u8 buf[100], *pos; |
| 417 | int rlen; |
| 418 | |
| 419 | if (argc < 3) { |
| 420 | printf("clear_tdls_counters needs three arguments: BSSID, " |
| 421 | "STA1 address, STA2 address\n"); |
| 422 | return -1; |
| 423 | } |
| 424 | |
| 425 | pos = buf; |
| 426 | WPA_PUT_BE32(pos, WLANTEST_CTRL_CLEAR_TDLS_COUNTERS); |
| 427 | pos += 4; |
| 428 | WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID); |
| 429 | pos += 4; |
| 430 | WPA_PUT_BE32(pos, ETH_ALEN); |
| 431 | pos += 4; |
| 432 | if (hwaddr_aton(argv[0], pos) < 0) { |
| 433 | printf("Invalid BSSID '%s'\n", argv[0]); |
| 434 | return -1; |
| 435 | } |
| 436 | pos += ETH_ALEN; |
| 437 | |
| 438 | WPA_PUT_BE32(pos, WLANTEST_ATTR_STA_ADDR); |
| 439 | pos += 4; |
| 440 | WPA_PUT_BE32(pos, ETH_ALEN); |
| 441 | pos += 4; |
| 442 | if (hwaddr_aton(argv[1], pos) < 0) { |
| 443 | printf("Invalid STA1 address '%s'\n", argv[1]); |
| 444 | return -1; |
| 445 | } |
| 446 | pos += ETH_ALEN; |
| 447 | |
| 448 | WPA_PUT_BE32(pos, WLANTEST_ATTR_STA2_ADDR); |
| 449 | pos += 4; |
| 450 | WPA_PUT_BE32(pos, ETH_ALEN); |
| 451 | pos += 4; |
| 452 | if (hwaddr_aton(argv[2], pos) < 0) { |
| 453 | printf("Invalid STA2 address '%s'\n", argv[2]); |
| 454 | return -1; |
| 455 | } |
| 456 | pos += ETH_ALEN; |
| 457 | |
| 458 | rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp)); |
| 459 | if (rlen < 0) |
| 460 | return -1; |
| 461 | printf("OK\n"); |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | |
| 466 | static char ** complete_clear_tdls_counters(int s, const char *str, int pos) |
| 467 | { |
| 468 | int arg = get_cmd_arg_num(str, pos); |
| 469 | char **res = NULL; |
| 470 | u8 addr[ETH_ALEN]; |
| 471 | |
| 472 | switch (arg) { |
| 473 | case 1: |
| 474 | res = get_bssid_list(s); |
| 475 | break; |
| 476 | case 2: |
| 477 | case 3: |
| 478 | if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0) |
| 479 | break; |
| 480 | res = get_sta_list(s, addr, 0); |
| 481 | break; |
| 482 | } |
| 483 | |
| 484 | return res; |
| 485 | } |
| 486 | |
| 487 | |
| 488 | struct sta_counters { |
| 489 | const char *name; |
| 490 | enum wlantest_sta_counter num; |
| 491 | }; |
| 492 | |
| 493 | static const struct sta_counters sta_counters[] = { |
| 494 | { "auth_tx", WLANTEST_STA_COUNTER_AUTH_TX }, |
| 495 | { "auth_rx", WLANTEST_STA_COUNTER_AUTH_RX }, |
| 496 | { "assocreq_tx", WLANTEST_STA_COUNTER_ASSOCREQ_TX }, |
| 497 | { "reassocreq_tx", WLANTEST_STA_COUNTER_REASSOCREQ_TX }, |
| 498 | { "ptk_learned", WLANTEST_STA_COUNTER_PTK_LEARNED }, |
| 499 | { "valid_deauth_tx", WLANTEST_STA_COUNTER_VALID_DEAUTH_TX }, |
| 500 | { "valid_deauth_rx", WLANTEST_STA_COUNTER_VALID_DEAUTH_RX }, |
| 501 | { "invalid_deauth_tx", WLANTEST_STA_COUNTER_INVALID_DEAUTH_TX }, |
| 502 | { "invalid_deauth_rx", WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX }, |
| 503 | { "valid_disassoc_tx", WLANTEST_STA_COUNTER_VALID_DISASSOC_TX }, |
| 504 | { "valid_disassoc_rx", WLANTEST_STA_COUNTER_VALID_DISASSOC_RX }, |
| 505 | { "invalid_disassoc_tx", WLANTEST_STA_COUNTER_INVALID_DISASSOC_TX }, |
| 506 | { "invalid_disassoc_rx", WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX }, |
| 507 | { "valid_saqueryreq_tx", WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_TX }, |
| 508 | { "valid_saqueryreq_rx", WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_RX }, |
| 509 | { "invalid_saqueryreq_tx", |
| 510 | WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_TX }, |
| 511 | { "invalid_saqueryreq_rx", |
| 512 | WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_RX }, |
| 513 | { "valid_saqueryresp_tx", WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_TX }, |
| 514 | { "valid_saqueryresp_rx", WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_RX }, |
| 515 | { "invalid_saqueryresp_tx", |
| 516 | WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_TX }, |
| 517 | { "invalid_saqueryresp_rx", |
| 518 | WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_RX }, |
| 519 | { "ping_ok", WLANTEST_STA_COUNTER_PING_OK }, |
| 520 | { "assocresp_comeback", WLANTEST_STA_COUNTER_ASSOCRESP_COMEBACK }, |
| 521 | { "reassocresp_comeback", WLANTEST_STA_COUNTER_REASSOCRESP_COMEBACK }, |
| 522 | { "ping_ok_first_assoc", WLANTEST_STA_COUNTER_PING_OK_FIRST_ASSOC }, |
| 523 | { "valid_deauth_rx_ack", WLANTEST_STA_COUNTER_VALID_DEAUTH_RX_ACK }, |
| 524 | { "valid_disassoc_rx_ack", |
| 525 | WLANTEST_STA_COUNTER_VALID_DISASSOC_RX_ACK }, |
| 526 | { "invalid_deauth_rx_ack", |
| 527 | WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX_ACK }, |
| 528 | { "invalid_disassoc_rx_ack", |
| 529 | WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX_ACK }, |
| 530 | { "deauth_rx_asleep", WLANTEST_STA_COUNTER_DEAUTH_RX_ASLEEP }, |
| 531 | { "deauth_rx_awake", WLANTEST_STA_COUNTER_DEAUTH_RX_AWAKE }, |
| 532 | { "disassoc_rx_asleep", WLANTEST_STA_COUNTER_DISASSOC_RX_ASLEEP }, |
| 533 | { "disassoc_rx_awake", WLANTEST_STA_COUNTER_DISASSOC_RX_AWAKE }, |
| 534 | { "prot_data_tx", WLANTEST_STA_COUNTER_PROT_DATA_TX }, |
| 535 | { "deauth_rx_rc6", WLANTEST_STA_COUNTER_DEAUTH_RX_RC6 }, |
| 536 | { "deauth_rx_rc7", WLANTEST_STA_COUNTER_DEAUTH_RX_RC7 }, |
| 537 | { "disassoc_rx_rc6", WLANTEST_STA_COUNTER_DISASSOC_RX_RC6 }, |
| 538 | { "disassoc_rx_rc7", WLANTEST_STA_COUNTER_DISASSOC_RX_RC7 }, |
| 539 | { NULL, 0 } |
| 540 | }; |
| 541 | |
| 542 | static int cmd_get_sta_counter(int s, int argc, char *argv[]) |
| 543 | { |
| 544 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 545 | u8 buf[100], *end, *pos; |
| 546 | int rlen, i; |
| 547 | size_t len; |
| 548 | |
| 549 | if (argc != 3) { |
| 550 | printf("get_sta_counter needs at three arguments: " |
| 551 | "counter name, BSSID, and STA address\n"); |
| 552 | return -1; |
| 553 | } |
| 554 | |
| 555 | pos = buf; |
| 556 | end = buf + sizeof(buf); |
| 557 | WPA_PUT_BE32(pos, WLANTEST_CTRL_GET_STA_COUNTER); |
| 558 | pos += 4; |
| 559 | |
| 560 | for (i = 0; sta_counters[i].name; i++) { |
| 561 | if (os_strcasecmp(sta_counters[i].name, argv[0]) == 0) |
| 562 | break; |
| 563 | } |
| 564 | if (sta_counters[i].name == NULL) { |
| 565 | printf("Unknown STA counter '%s'\n", argv[0]); |
| 566 | printf("Counters:"); |
| 567 | for (i = 0; sta_counters[i].name; i++) |
| 568 | printf(" %s", sta_counters[i].name); |
| 569 | printf("\n"); |
| 570 | return -1; |
| 571 | } |
| 572 | |
| 573 | pos = attr_add_be32(pos, end, WLANTEST_ATTR_STA_COUNTER, |
| 574 | sta_counters[i].num); |
| 575 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN); |
| 576 | if (hwaddr_aton(argv[1], pos) < 0) { |
| 577 | printf("Invalid BSSID '%s'\n", argv[1]); |
| 578 | return -1; |
| 579 | } |
| 580 | pos += ETH_ALEN; |
| 581 | |
| 582 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN); |
| 583 | if (hwaddr_aton(argv[2], pos) < 0) { |
| 584 | printf("Invalid STA address '%s'\n", argv[2]); |
| 585 | return -1; |
| 586 | } |
| 587 | pos += ETH_ALEN; |
| 588 | |
| 589 | rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp)); |
| 590 | if (rlen < 0) |
| 591 | return -1; |
| 592 | |
| 593 | pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_COUNTER, &len); |
| 594 | if (pos == NULL || len != 4) |
| 595 | return -1; |
| 596 | printf("%u\n", WPA_GET_BE32(pos)); |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | |
| 601 | static char ** complete_get_sta_counter(int s, const char *str, int pos) |
| 602 | { |
| 603 | int arg = get_cmd_arg_num(str, pos); |
| 604 | char **res = NULL; |
| 605 | int i, count; |
| 606 | u8 addr[ETH_ALEN]; |
| 607 | |
| 608 | switch (arg) { |
| 609 | case 1: |
| 610 | /* counter list */ |
| 611 | count = ARRAY_SIZE(sta_counters); |
| 612 | res = os_calloc(count, sizeof(char *)); |
| 613 | if (res == NULL) |
| 614 | return NULL; |
| 615 | for (i = 0; sta_counters[i].name; i++) { |
| 616 | res[i] = os_strdup(sta_counters[i].name); |
| 617 | if (res[i] == NULL) |
| 618 | break; |
| 619 | } |
| 620 | break; |
| 621 | case 2: |
| 622 | res = get_bssid_list(s); |
| 623 | break; |
| 624 | case 3: |
| 625 | if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0) |
| 626 | break; |
| 627 | res = get_sta_list(s, addr, 0); |
| 628 | break; |
| 629 | } |
| 630 | |
| 631 | return res; |
| 632 | } |
| 633 | |
| 634 | |
| 635 | struct bss_counters { |
| 636 | const char *name; |
| 637 | enum wlantest_bss_counter num; |
| 638 | }; |
| 639 | |
| 640 | static const struct bss_counters bss_counters[] = { |
| 641 | { "valid_bip_mmie", WLANTEST_BSS_COUNTER_VALID_BIP_MMIE }, |
| 642 | { "invalid_bip_mmie", WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE }, |
| 643 | { "missing_bip_mmie", WLANTEST_BSS_COUNTER_MISSING_BIP_MMIE }, |
| 644 | { "bip_deauth", WLANTEST_BSS_COUNTER_BIP_DEAUTH }, |
| 645 | { "bip_disassoc", WLANTEST_BSS_COUNTER_BIP_DISASSOC }, |
| 646 | { "probe_response", WLANTEST_BSS_COUNTER_PROBE_RESPONSE }, |
| 647 | { NULL, 0 } |
| 648 | }; |
| 649 | |
| 650 | static int cmd_get_bss_counter(int s, int argc, char *argv[]) |
| 651 | { |
| 652 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 653 | u8 buf[100], *end, *pos; |
| 654 | int rlen, i; |
| 655 | size_t len; |
| 656 | |
| 657 | if (argc != 2) { |
| 658 | printf("get_bss_counter needs at two arguments: " |
| 659 | "counter name and BSSID\n"); |
| 660 | return -1; |
| 661 | } |
| 662 | |
| 663 | pos = buf; |
| 664 | end = buf + sizeof(buf); |
| 665 | WPA_PUT_BE32(pos, WLANTEST_CTRL_GET_BSS_COUNTER); |
| 666 | pos += 4; |
| 667 | |
| 668 | for (i = 0; bss_counters[i].name; i++) { |
| 669 | if (os_strcasecmp(bss_counters[i].name, argv[0]) == 0) |
| 670 | break; |
| 671 | } |
| 672 | if (bss_counters[i].name == NULL) { |
| 673 | printf("Unknown BSS counter '%s'\n", argv[0]); |
| 674 | printf("Counters:"); |
| 675 | for (i = 0; bss_counters[i].name; i++) |
| 676 | printf(" %s", bss_counters[i].name); |
| 677 | printf("\n"); |
| 678 | return -1; |
| 679 | } |
| 680 | |
| 681 | pos = attr_add_be32(pos, end, WLANTEST_ATTR_BSS_COUNTER, |
| 682 | bss_counters[i].num); |
| 683 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN); |
| 684 | if (hwaddr_aton(argv[1], pos) < 0) { |
| 685 | printf("Invalid BSSID '%s'\n", argv[1]); |
| 686 | return -1; |
| 687 | } |
| 688 | pos += ETH_ALEN; |
| 689 | |
| 690 | rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp)); |
| 691 | if (rlen < 0) |
| 692 | return -1; |
| 693 | |
| 694 | pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_COUNTER, &len); |
| 695 | if (pos == NULL || len != 4) |
| 696 | return -1; |
| 697 | printf("%u\n", WPA_GET_BE32(pos)); |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | |
| 702 | static char ** complete_get_bss_counter(int s, const char *str, int pos) |
| 703 | { |
| 704 | int arg = get_cmd_arg_num(str, pos); |
| 705 | char **res = NULL; |
| 706 | int i, count; |
| 707 | |
| 708 | switch (arg) { |
| 709 | case 1: |
| 710 | /* counter list */ |
| 711 | count = ARRAY_SIZE(bss_counters); |
| 712 | res = os_calloc(count, sizeof(char *)); |
| 713 | if (res == NULL) |
| 714 | return NULL; |
| 715 | for (i = 0; bss_counters[i].name; i++) { |
| 716 | res[i] = os_strdup(bss_counters[i].name); |
| 717 | if (res[i] == NULL) |
| 718 | break; |
| 719 | } |
| 720 | break; |
| 721 | case 2: |
| 722 | res = get_bssid_list(s); |
| 723 | break; |
| 724 | } |
| 725 | |
| 726 | return res; |
| 727 | } |
| 728 | |
| 729 | |
| 730 | static int cmd_relog(int s, int argc, char *argv[]) |
| 731 | { |
| 732 | return cmd_simple(s, WLANTEST_CTRL_RELOG); |
| 733 | } |
| 734 | |
| 735 | |
| 736 | struct tdls_counters { |
| 737 | const char *name; |
| 738 | enum wlantest_tdls_counter num; |
| 739 | }; |
| 740 | |
| 741 | static const struct tdls_counters tdls_counters[] = { |
| 742 | { "valid_direct_link", WLANTEST_TDLS_COUNTER_VALID_DIRECT_LINK }, |
| 743 | { "invalid_direct_link", WLANTEST_TDLS_COUNTER_INVALID_DIRECT_LINK }, |
| 744 | { "valid_ap_path", WLANTEST_TDLS_COUNTER_VALID_AP_PATH }, |
| 745 | { "invalid_ap_path", WLANTEST_TDLS_COUNTER_INVALID_AP_PATH }, |
| 746 | { "setup_req", WLANTEST_TDLS_COUNTER_SETUP_REQ }, |
| 747 | { "setup_resp_ok", WLANTEST_TDLS_COUNTER_SETUP_RESP_OK }, |
| 748 | { "setup_resp_fail", WLANTEST_TDLS_COUNTER_SETUP_RESP_FAIL }, |
| 749 | { "setup_conf_ok", WLANTEST_TDLS_COUNTER_SETUP_CONF_OK }, |
| 750 | { "setup_conf_fail", WLANTEST_TDLS_COUNTER_SETUP_CONF_FAIL }, |
| 751 | { "teardown", WLANTEST_TDLS_COUNTER_TEARDOWN }, |
| 752 | { NULL, 0 } |
| 753 | }; |
| 754 | |
| 755 | static int cmd_get_tdls_counter(int s, int argc, char *argv[]) |
| 756 | { |
| 757 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 758 | u8 buf[100], *end, *pos; |
| 759 | int rlen, i; |
| 760 | size_t len; |
| 761 | |
| 762 | if (argc != 4) { |
| 763 | printf("get_tdls_counter needs four arguments: " |
| 764 | "counter name, BSSID, STA1 address, STA2 address\n"); |
| 765 | return -1; |
| 766 | } |
| 767 | |
| 768 | pos = buf; |
| 769 | end = buf + sizeof(buf); |
| 770 | WPA_PUT_BE32(pos, WLANTEST_CTRL_GET_TDLS_COUNTER); |
| 771 | pos += 4; |
| 772 | |
| 773 | for (i = 0; tdls_counters[i].name; i++) { |
| 774 | if (os_strcasecmp(tdls_counters[i].name, argv[0]) == 0) |
| 775 | break; |
| 776 | } |
| 777 | if (tdls_counters[i].name == NULL) { |
| 778 | printf("Unknown TDLS counter '%s'\n", argv[0]); |
| 779 | printf("Counters:"); |
| 780 | for (i = 0; tdls_counters[i].name; i++) |
| 781 | printf(" %s", tdls_counters[i].name); |
| 782 | printf("\n"); |
| 783 | return -1; |
| 784 | } |
| 785 | |
| 786 | pos = attr_add_be32(pos, end, WLANTEST_ATTR_TDLS_COUNTER, |
| 787 | tdls_counters[i].num); |
| 788 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN); |
| 789 | if (hwaddr_aton(argv[1], pos) < 0) { |
| 790 | printf("Invalid BSSID '%s'\n", argv[1]); |
| 791 | return -1; |
| 792 | } |
| 793 | pos += ETH_ALEN; |
| 794 | |
| 795 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN); |
| 796 | if (hwaddr_aton(argv[2], pos) < 0) { |
| 797 | printf("Invalid STA1 address '%s'\n", argv[2]); |
| 798 | return -1; |
| 799 | } |
| 800 | pos += ETH_ALEN; |
| 801 | |
| 802 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA2_ADDR, ETH_ALEN); |
| 803 | if (hwaddr_aton(argv[3], pos) < 0) { |
| 804 | printf("Invalid STA2 address '%s'\n", argv[3]); |
| 805 | return -1; |
| 806 | } |
| 807 | pos += ETH_ALEN; |
| 808 | |
| 809 | rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp)); |
| 810 | if (rlen < 0) |
| 811 | return -1; |
| 812 | |
| 813 | pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_COUNTER, &len); |
| 814 | if (pos == NULL || len != 4) |
| 815 | return -1; |
| 816 | printf("%u\n", WPA_GET_BE32(pos)); |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | |
| 821 | static char ** complete_get_tdls_counter(int s, const char *str, int pos) |
| 822 | { |
| 823 | int arg = get_cmd_arg_num(str, pos); |
| 824 | char **res = NULL; |
| 825 | int i, count; |
| 826 | u8 addr[ETH_ALEN]; |
| 827 | |
| 828 | switch (arg) { |
| 829 | case 1: |
| 830 | /* counter list */ |
| 831 | count = ARRAY_SIZE(tdls_counters); |
| 832 | res = os_calloc(count, sizeof(char *)); |
| 833 | if (res == NULL) |
| 834 | return NULL; |
| 835 | for (i = 0; tdls_counters[i].name; i++) { |
| 836 | res[i] = os_strdup(tdls_counters[i].name); |
| 837 | if (res[i] == NULL) |
| 838 | break; |
| 839 | } |
| 840 | break; |
| 841 | case 2: |
| 842 | res = get_bssid_list(s); |
| 843 | break; |
| 844 | case 3: |
| 845 | case 4: |
| 846 | if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0) |
| 847 | break; |
| 848 | res = get_sta_list(s, addr, 0); |
| 849 | break; |
| 850 | } |
| 851 | |
| 852 | return res; |
| 853 | } |
| 854 | |
| 855 | |
| 856 | struct inject_frames { |
| 857 | const char *name; |
| 858 | enum wlantest_inject_frame frame; |
| 859 | }; |
| 860 | |
| 861 | static const struct inject_frames inject_frames[] = { |
| 862 | { "auth", WLANTEST_FRAME_AUTH }, |
| 863 | { "assocreq", WLANTEST_FRAME_ASSOCREQ }, |
| 864 | { "reassocreq", WLANTEST_FRAME_REASSOCREQ }, |
| 865 | { "deauth", WLANTEST_FRAME_DEAUTH }, |
| 866 | { "disassoc", WLANTEST_FRAME_DISASSOC }, |
| 867 | { "saqueryreq", WLANTEST_FRAME_SAQUERYREQ }, |
| 868 | { NULL, 0 } |
| 869 | }; |
| 870 | |
| 871 | static int cmd_inject(int s, int argc, char *argv[]) |
| 872 | { |
| 873 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 874 | u8 buf[100], *end, *pos; |
| 875 | int rlen, i; |
| 876 | enum wlantest_inject_protection prot; |
| 877 | |
| 878 | /* <frame> <prot> <sender> <BSSID> <STA/ff:ff:ff:ff:ff:ff> */ |
| 879 | |
| 880 | if (argc < 5) { |
| 881 | printf("inject needs five arguments: frame, protection, " |
| 882 | "sender, BSSID, STA/ff:ff:ff:ff:ff:ff\n"); |
| 883 | return -1; |
| 884 | } |
| 885 | |
| 886 | pos = buf; |
| 887 | end = buf + sizeof(buf); |
| 888 | WPA_PUT_BE32(pos, WLANTEST_CTRL_INJECT); |
| 889 | pos += 4; |
| 890 | |
| 891 | for (i = 0; inject_frames[i].name; i++) { |
| 892 | if (os_strcasecmp(inject_frames[i].name, argv[0]) == 0) |
| 893 | break; |
| 894 | } |
| 895 | if (inject_frames[i].name == NULL) { |
| 896 | printf("Unknown inject frame '%s'\n", argv[0]); |
| 897 | printf("Frames:"); |
| 898 | for (i = 0; inject_frames[i].name; i++) |
| 899 | printf(" %s", inject_frames[i].name); |
| 900 | printf("\n"); |
| 901 | return -1; |
| 902 | } |
| 903 | |
| 904 | pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_FRAME, |
| 905 | inject_frames[i].frame); |
| 906 | |
| 907 | if (os_strcasecmp(argv[1], "normal") == 0) |
| 908 | prot = WLANTEST_INJECT_NORMAL; |
| 909 | else if (os_strcasecmp(argv[1], "protected") == 0) |
| 910 | prot = WLANTEST_INJECT_PROTECTED; |
| 911 | else if (os_strcasecmp(argv[1], "unprotected") == 0) |
| 912 | prot = WLANTEST_INJECT_UNPROTECTED; |
| 913 | else if (os_strcasecmp(argv[1], "incorrect") == 0) |
| 914 | prot = WLANTEST_INJECT_INCORRECT_KEY; |
| 915 | else { |
| 916 | printf("Unknown protection type '%s'\n", argv[1]); |
| 917 | printf("Protection types: normal protected unprotected " |
| 918 | "incorrect\n"); |
| 919 | return -1; |
| 920 | } |
| 921 | pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_PROTECTION, prot); |
| 922 | |
| 923 | if (os_strcasecmp(argv[2], "ap") == 0) { |
| 924 | pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_SENDER_AP, |
| 925 | 1); |
| 926 | } else if (os_strcasecmp(argv[2], "sta") == 0) { |
| 927 | pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_SENDER_AP, |
| 928 | 0); |
| 929 | } else { |
| 930 | printf("Unknown sender '%s'\n", argv[2]); |
| 931 | printf("Sender types: ap sta\n"); |
| 932 | return -1; |
| 933 | } |
| 934 | |
| 935 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN); |
| 936 | if (hwaddr_aton(argv[3], pos) < 0) { |
| 937 | printf("Invalid BSSID '%s'\n", argv[3]); |
| 938 | return -1; |
| 939 | } |
| 940 | pos += ETH_ALEN; |
| 941 | |
| 942 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN); |
| 943 | if (hwaddr_aton(argv[4], pos) < 0) { |
| 944 | printf("Invalid STA '%s'\n", argv[4]); |
| 945 | return -1; |
| 946 | } |
| 947 | pos += ETH_ALEN; |
| 948 | |
| 949 | rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp)); |
| 950 | if (rlen < 0) |
| 951 | return -1; |
| 952 | printf("OK\n"); |
| 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | |
| 957 | static char ** complete_inject(int s, const char *str, int pos) |
| 958 | { |
| 959 | int arg = get_cmd_arg_num(str, pos); |
| 960 | char **res = NULL; |
| 961 | int i, count; |
| 962 | u8 addr[ETH_ALEN]; |
| 963 | |
| 964 | switch (arg) { |
| 965 | case 1: |
| 966 | /* frame list */ |
| 967 | count = ARRAY_SIZE(inject_frames); |
| 968 | res = os_calloc(count, sizeof(char *)); |
| 969 | if (res == NULL) |
| 970 | break; |
| 971 | for (i = 0; inject_frames[i].name; i++) { |
| 972 | res[i] = os_strdup(inject_frames[i].name); |
| 973 | if (res[i] == NULL) |
| 974 | break; |
| 975 | } |
| 976 | break; |
| 977 | case 2: |
| 978 | res = os_calloc(5, sizeof(char *)); |
| 979 | if (res == NULL) |
| 980 | break; |
| 981 | res[0] = os_strdup("normal"); |
| 982 | if (res[0] == NULL) |
| 983 | break; |
| 984 | res[1] = os_strdup("protected"); |
| 985 | if (res[1] == NULL) |
| 986 | break; |
| 987 | res[2] = os_strdup("unprotected"); |
| 988 | if (res[2] == NULL) |
| 989 | break; |
| 990 | res[3] = os_strdup("incorrect"); |
| 991 | if (res[3] == NULL) |
| 992 | break; |
| 993 | break; |
| 994 | case 3: |
| 995 | res = os_calloc(3, sizeof(char *)); |
| 996 | if (res == NULL) |
| 997 | break; |
| 998 | res[0] = os_strdup("ap"); |
| 999 | if (res[0] == NULL) |
| 1000 | break; |
| 1001 | res[1] = os_strdup("sta"); |
| 1002 | if (res[1] == NULL) |
| 1003 | break; |
| 1004 | break; |
| 1005 | case 4: |
| 1006 | res = get_bssid_list(s); |
| 1007 | break; |
| 1008 | case 5: |
| 1009 | if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0) |
| 1010 | break; |
| 1011 | res = get_sta_list(s, addr, 1); |
| 1012 | break; |
| 1013 | } |
| 1014 | |
| 1015 | return res; |
| 1016 | } |
| 1017 | |
| 1018 | |
| 1019 | static u8 * add_hex(u8 *pos, u8 *end, const char *str) |
| 1020 | { |
| 1021 | const char *s; |
| 1022 | int val; |
| 1023 | |
| 1024 | s = str; |
| 1025 | while (*s) { |
| 1026 | while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n' || |
| 1027 | *s == ':') |
| 1028 | s++; |
| 1029 | if (*s == '\0') |
| 1030 | break; |
| 1031 | if (*s == '#') { |
| 1032 | while (*s != '\0' && *s != '\r' && *s != '\n') |
| 1033 | s++; |
| 1034 | continue; |
| 1035 | } |
| 1036 | |
| 1037 | val = hex2byte(s); |
| 1038 | if (val < 0) { |
| 1039 | printf("Invalid hex encoding '%s'\n", s); |
| 1040 | return NULL; |
| 1041 | } |
| 1042 | if (pos == end) { |
| 1043 | printf("Too long frame\n"); |
| 1044 | return NULL; |
| 1045 | } |
| 1046 | *pos++ = val; |
| 1047 | s += 2; |
| 1048 | } |
| 1049 | |
| 1050 | return pos; |
| 1051 | } |
| 1052 | |
| 1053 | |
| 1054 | static int cmd_send(int s, int argc, char *argv[]) |
| 1055 | { |
| 1056 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 1057 | u8 buf[WLANTEST_CTRL_MAX_CMD_LEN], *end, *pos, *len_pos; |
| 1058 | int rlen; |
| 1059 | enum wlantest_inject_protection prot; |
| 1060 | int arg; |
| 1061 | |
| 1062 | /* <prot> <raw frame as hex dump> */ |
| 1063 | |
| 1064 | if (argc < 2) { |
| 1065 | printf("send needs two arguments: protected/unprotected, " |
| 1066 | "raw frame as hex dump\n"); |
| 1067 | return -1; |
| 1068 | } |
| 1069 | |
| 1070 | pos = buf; |
| 1071 | end = buf + sizeof(buf); |
| 1072 | WPA_PUT_BE32(pos, WLANTEST_CTRL_SEND); |
| 1073 | pos += 4; |
| 1074 | |
| 1075 | if (os_strcasecmp(argv[0], "normal") == 0) |
| 1076 | prot = WLANTEST_INJECT_NORMAL; |
| 1077 | else if (os_strcasecmp(argv[0], "protected") == 0) |
| 1078 | prot = WLANTEST_INJECT_PROTECTED; |
| 1079 | else if (os_strcasecmp(argv[0], "unprotected") == 0) |
| 1080 | prot = WLANTEST_INJECT_UNPROTECTED; |
| 1081 | else if (os_strcasecmp(argv[0], "incorrect") == 0) |
| 1082 | prot = WLANTEST_INJECT_INCORRECT_KEY; |
| 1083 | else { |
| 1084 | printf("Unknown protection type '%s'\n", argv[1]); |
| 1085 | printf("Protection types: normal protected unprotected " |
| 1086 | "incorrect\n"); |
| 1087 | return -1; |
| 1088 | } |
| 1089 | pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_PROTECTION, prot); |
| 1090 | |
| 1091 | WPA_PUT_BE32(pos, WLANTEST_ATTR_FRAME); |
| 1092 | pos += 4; |
| 1093 | len_pos = pos; |
| 1094 | pos += 4; |
| 1095 | |
| 1096 | for (arg = 1; pos && arg < argc; arg++) |
| 1097 | pos = add_hex(pos, end, argv[arg]); |
| 1098 | if (pos == NULL) |
| 1099 | return -1; |
| 1100 | |
| 1101 | WPA_PUT_BE32(len_pos, pos - len_pos - 4); |
| 1102 | |
| 1103 | rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp)); |
| 1104 | if (rlen < 0) |
| 1105 | return -1; |
| 1106 | printf("OK\n"); |
| 1107 | return 0; |
| 1108 | } |
| 1109 | |
| 1110 | |
| 1111 | static char ** complete_send(int s, const char *str, int pos) |
| 1112 | { |
| 1113 | int arg = get_cmd_arg_num(str, pos); |
| 1114 | char **res = NULL; |
| 1115 | |
| 1116 | switch (arg) { |
| 1117 | case 1: |
| 1118 | res = os_calloc(5, sizeof(char *)); |
| 1119 | if (res == NULL) |
| 1120 | break; |
| 1121 | res[0] = os_strdup("normal"); |
| 1122 | if (res[0] == NULL) |
| 1123 | break; |
| 1124 | res[1] = os_strdup("protected"); |
| 1125 | if (res[1] == NULL) |
| 1126 | break; |
| 1127 | res[2] = os_strdup("unprotected"); |
| 1128 | if (res[2] == NULL) |
| 1129 | break; |
| 1130 | res[3] = os_strdup("incorrect"); |
| 1131 | if (res[3] == NULL) |
| 1132 | break; |
| 1133 | break; |
| 1134 | } |
| 1135 | |
| 1136 | return res; |
| 1137 | } |
| 1138 | |
| 1139 | |
| 1140 | static int cmd_version(int s, int argc, char *argv[]) |
| 1141 | { |
| 1142 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 1143 | u8 buf[4]; |
| 1144 | char *version; |
| 1145 | size_t len; |
| 1146 | int rlen, i; |
| 1147 | |
| 1148 | WPA_PUT_BE32(buf, WLANTEST_CTRL_VERSION); |
| 1149 | rlen = cmd_send_and_recv(s, buf, sizeof(buf), resp, sizeof(resp)); |
| 1150 | if (rlen < 0) |
| 1151 | return -1; |
| 1152 | |
| 1153 | version = (char *) attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_VERSION, |
| 1154 | &len); |
| 1155 | if (version == NULL) |
| 1156 | return -1; |
| 1157 | |
| 1158 | for (i = 0; i < len; i++) |
| 1159 | putchar(version[i]); |
| 1160 | printf("\n"); |
| 1161 | |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | |
| 1166 | static int cmd_add_passphrase(int s, int argc, char *argv[]) |
| 1167 | { |
| 1168 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 1169 | u8 buf[100], *pos, *end; |
| 1170 | size_t len; |
| 1171 | int rlen; |
| 1172 | |
| 1173 | if (argc < 1) { |
| 1174 | printf("add_passphrase needs one argument: passphrase\n"); |
| 1175 | return -1; |
| 1176 | } |
| 1177 | |
| 1178 | len = os_strlen(argv[0]); |
| 1179 | if (len < 8 || len > 63) { |
| 1180 | printf("Invalid passphrase '%s'\n", argv[0]); |
| 1181 | return -1; |
| 1182 | } |
| 1183 | pos = buf; |
| 1184 | end = buf + sizeof(buf); |
| 1185 | WPA_PUT_BE32(pos, WLANTEST_CTRL_ADD_PASSPHRASE); |
| 1186 | pos += 4; |
| 1187 | pos = attr_add_str(pos, end, WLANTEST_ATTR_PASSPHRASE, |
| 1188 | argv[0]); |
| 1189 | if (argc > 1) { |
| 1190 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN); |
| 1191 | if (hwaddr_aton(argv[1], pos) < 0) { |
| 1192 | printf("Invalid BSSID '%s'\n", argv[3]); |
| 1193 | return -1; |
| 1194 | } |
| 1195 | pos += ETH_ALEN; |
| 1196 | } |
| 1197 | |
| 1198 | rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp)); |
| 1199 | if (rlen < 0) |
| 1200 | return -1; |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
| 1204 | |
| 1205 | static int cmd_add_wepkey(int s, int argc, char *argv[]) |
| 1206 | { |
| 1207 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 1208 | u8 buf[100], *pos, *end; |
| 1209 | int rlen; |
| 1210 | |
| 1211 | if (argc < 1) { |
| 1212 | printf("add_wepkey needs one argument: WEP key\n"); |
| 1213 | return -1; |
| 1214 | } |
| 1215 | |
| 1216 | pos = buf; |
| 1217 | end = buf + sizeof(buf); |
| 1218 | WPA_PUT_BE32(pos, WLANTEST_CTRL_ADD_PASSPHRASE); |
| 1219 | pos += 4; |
| 1220 | pos = attr_add_str(pos, end, WLANTEST_ATTR_WEPKEY, argv[0]); |
| 1221 | |
| 1222 | rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp)); |
| 1223 | if (rlen < 0) |
| 1224 | return -1; |
| 1225 | return 0; |
| 1226 | } |
| 1227 | |
| 1228 | |
| 1229 | struct sta_infos { |
| 1230 | const char *name; |
| 1231 | enum wlantest_sta_info num; |
| 1232 | }; |
| 1233 | |
| 1234 | static const struct sta_infos sta_infos[] = { |
| 1235 | { "proto", WLANTEST_STA_INFO_PROTO }, |
| 1236 | { "pairwise", WLANTEST_STA_INFO_PAIRWISE }, |
| 1237 | { "key_mgmt", WLANTEST_STA_INFO_KEY_MGMT }, |
| 1238 | { "rsn_capab", WLANTEST_STA_INFO_RSN_CAPAB }, |
| 1239 | { "state", WLANTEST_STA_INFO_STATE }, |
| 1240 | { "gtk", WLANTEST_STA_INFO_GTK }, |
| 1241 | { NULL, 0 } |
| 1242 | }; |
| 1243 | |
| 1244 | static int cmd_info_sta(int s, int argc, char *argv[]) |
| 1245 | { |
| 1246 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 1247 | u8 buf[100], *end, *pos; |
| 1248 | int rlen, i; |
| 1249 | size_t len; |
| 1250 | char info[100]; |
| 1251 | |
| 1252 | if (argc != 3) { |
| 1253 | printf("sta_info needs at three arguments: " |
| 1254 | "counter name, BSSID, and STA address\n"); |
| 1255 | return -1; |
| 1256 | } |
| 1257 | |
| 1258 | pos = buf; |
| 1259 | end = buf + sizeof(buf); |
| 1260 | WPA_PUT_BE32(pos, WLANTEST_CTRL_INFO_STA); |
| 1261 | pos += 4; |
| 1262 | |
| 1263 | for (i = 0; sta_infos[i].name; i++) { |
| 1264 | if (os_strcasecmp(sta_infos[i].name, argv[0]) == 0) |
| 1265 | break; |
| 1266 | } |
| 1267 | if (sta_infos[i].name == NULL) { |
| 1268 | printf("Unknown STA info '%s'\n", argv[0]); |
| 1269 | printf("Info fields:"); |
| 1270 | for (i = 0; sta_infos[i].name; i++) |
| 1271 | printf(" %s", sta_infos[i].name); |
| 1272 | printf("\n"); |
| 1273 | return -1; |
| 1274 | } |
| 1275 | |
| 1276 | pos = attr_add_be32(pos, end, WLANTEST_ATTR_STA_INFO, |
| 1277 | sta_infos[i].num); |
| 1278 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN); |
| 1279 | if (hwaddr_aton(argv[1], pos) < 0) { |
| 1280 | printf("Invalid BSSID '%s'\n", argv[1]); |
| 1281 | return -1; |
| 1282 | } |
| 1283 | pos += ETH_ALEN; |
| 1284 | |
| 1285 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN); |
| 1286 | if (hwaddr_aton(argv[2], pos) < 0) { |
| 1287 | printf("Invalid STA address '%s'\n", argv[2]); |
| 1288 | return -1; |
| 1289 | } |
| 1290 | pos += ETH_ALEN; |
| 1291 | |
| 1292 | rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp)); |
| 1293 | if (rlen < 0) |
| 1294 | return -1; |
| 1295 | |
| 1296 | pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_INFO, &len); |
| 1297 | if (pos == NULL) |
| 1298 | return -1; |
| 1299 | if (len >= sizeof(info)) |
| 1300 | len = sizeof(info) - 1; |
| 1301 | os_memcpy(info, pos, len); |
| 1302 | info[len] = '\0'; |
| 1303 | printf("%s\n", info); |
| 1304 | return 0; |
| 1305 | } |
| 1306 | |
| 1307 | |
| 1308 | static char ** complete_info_sta(int s, const char *str, int pos) |
| 1309 | { |
| 1310 | int arg = get_cmd_arg_num(str, pos); |
| 1311 | char **res = NULL; |
| 1312 | int i, count; |
| 1313 | u8 addr[ETH_ALEN]; |
| 1314 | |
| 1315 | switch (arg) { |
| 1316 | case 1: |
| 1317 | /* counter list */ |
| 1318 | count = ARRAY_SIZE(sta_infos); |
| 1319 | res = os_calloc(count, sizeof(char *)); |
| 1320 | if (res == NULL) |
| 1321 | return NULL; |
| 1322 | for (i = 0; sta_infos[i].name; i++) { |
| 1323 | res[i] = os_strdup(sta_infos[i].name); |
| 1324 | if (res[i] == NULL) |
| 1325 | break; |
| 1326 | } |
| 1327 | break; |
| 1328 | case 2: |
| 1329 | res = get_bssid_list(s); |
| 1330 | break; |
| 1331 | case 3: |
| 1332 | if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0) |
| 1333 | break; |
| 1334 | res = get_sta_list(s, addr, 0); |
| 1335 | break; |
| 1336 | } |
| 1337 | |
| 1338 | return res; |
| 1339 | } |
| 1340 | |
| 1341 | |
| 1342 | struct bss_infos { |
| 1343 | const char *name; |
| 1344 | enum wlantest_bss_info num; |
| 1345 | }; |
| 1346 | |
| 1347 | static const struct bss_infos bss_infos[] = { |
| 1348 | { "proto", WLANTEST_BSS_INFO_PROTO }, |
| 1349 | { "pairwise", WLANTEST_BSS_INFO_PAIRWISE }, |
| 1350 | { "group", WLANTEST_BSS_INFO_GROUP }, |
| 1351 | { "group_mgmt", WLANTEST_BSS_INFO_GROUP_MGMT }, |
| 1352 | { "key_mgmt", WLANTEST_BSS_INFO_KEY_MGMT }, |
| 1353 | { "rsn_capab", WLANTEST_BSS_INFO_RSN_CAPAB }, |
| 1354 | { NULL, 0 } |
| 1355 | }; |
| 1356 | |
| 1357 | static int cmd_info_bss(int s, int argc, char *argv[]) |
| 1358 | { |
| 1359 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 1360 | u8 buf[100], *end, *pos; |
| 1361 | int rlen, i; |
| 1362 | size_t len; |
| 1363 | char info[100]; |
| 1364 | |
| 1365 | if (argc != 2) { |
| 1366 | printf("bss_info needs at two arguments: " |
| 1367 | "field name and BSSID\n"); |
| 1368 | return -1; |
| 1369 | } |
| 1370 | |
| 1371 | pos = buf; |
| 1372 | end = buf + sizeof(buf); |
| 1373 | WPA_PUT_BE32(pos, WLANTEST_CTRL_INFO_BSS); |
| 1374 | pos += 4; |
| 1375 | |
| 1376 | for (i = 0; bss_infos[i].name; i++) { |
| 1377 | if (os_strcasecmp(bss_infos[i].name, argv[0]) == 0) |
| 1378 | break; |
| 1379 | } |
| 1380 | if (bss_infos[i].name == NULL) { |
| 1381 | printf("Unknown BSS info '%s'\n", argv[0]); |
| 1382 | printf("Info fields:"); |
| 1383 | for (i = 0; bss_infos[i].name; i++) |
| 1384 | printf(" %s", bss_infos[i].name); |
| 1385 | printf("\n"); |
| 1386 | return -1; |
| 1387 | } |
| 1388 | |
| 1389 | pos = attr_add_be32(pos, end, WLANTEST_ATTR_BSS_INFO, |
| 1390 | bss_infos[i].num); |
| 1391 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN); |
| 1392 | if (hwaddr_aton(argv[1], pos) < 0) { |
| 1393 | printf("Invalid BSSID '%s'\n", argv[1]); |
| 1394 | return -1; |
| 1395 | } |
| 1396 | pos += ETH_ALEN; |
| 1397 | |
| 1398 | rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp)); |
| 1399 | if (rlen < 0) |
| 1400 | return -1; |
| 1401 | |
| 1402 | pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_INFO, &len); |
| 1403 | if (pos == NULL) |
| 1404 | return -1; |
| 1405 | if (len >= sizeof(info)) |
| 1406 | len = sizeof(info) - 1; |
| 1407 | os_memcpy(info, pos, len); |
| 1408 | info[len] = '\0'; |
| 1409 | printf("%s\n", info); |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
| 1413 | |
| 1414 | static char ** complete_info_bss(int s, const char *str, int pos) |
| 1415 | { |
| 1416 | int arg = get_cmd_arg_num(str, pos); |
| 1417 | char **res = NULL; |
| 1418 | int i, count; |
| 1419 | |
| 1420 | switch (arg) { |
| 1421 | case 1: |
| 1422 | /* counter list */ |
| 1423 | count = ARRAY_SIZE(bss_infos); |
| 1424 | res = os_calloc(count, sizeof(char *)); |
| 1425 | if (res == NULL) |
| 1426 | return NULL; |
| 1427 | for (i = 0; bss_infos[i].name; i++) { |
| 1428 | res[i] = os_strdup(bss_infos[i].name); |
| 1429 | if (res[i] == NULL) |
| 1430 | break; |
| 1431 | } |
| 1432 | break; |
| 1433 | case 2: |
| 1434 | res = get_bssid_list(s); |
| 1435 | break; |
| 1436 | } |
| 1437 | |
| 1438 | return res; |
| 1439 | } |
| 1440 | |
| 1441 | |
| 1442 | static int cmd_get_tx_tid(int s, int argc, char *argv[]) |
| 1443 | { |
| 1444 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 1445 | u8 buf[100], *end, *pos; |
| 1446 | int rlen; |
| 1447 | size_t len; |
| 1448 | |
| 1449 | if (argc != 3) { |
| 1450 | printf("get_tx_tid needs three arguments: " |
| 1451 | "BSSID, STA address, and TID\n"); |
| 1452 | return -1; |
| 1453 | } |
| 1454 | |
| 1455 | pos = buf; |
| 1456 | end = buf + sizeof(buf); |
| 1457 | WPA_PUT_BE32(pos, WLANTEST_CTRL_GET_TX_TID); |
| 1458 | pos += 4; |
| 1459 | |
| 1460 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN); |
| 1461 | if (hwaddr_aton(argv[0], pos) < 0) { |
| 1462 | printf("Invalid BSSID '%s'\n", argv[0]); |
| 1463 | return -1; |
| 1464 | } |
| 1465 | pos += ETH_ALEN; |
| 1466 | |
| 1467 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN); |
| 1468 | if (hwaddr_aton(argv[1], pos) < 0) { |
| 1469 | printf("Invalid STA address '%s'\n", argv[1]); |
| 1470 | return -1; |
| 1471 | } |
| 1472 | pos += ETH_ALEN; |
| 1473 | |
| 1474 | pos = attr_add_be32(pos, end, WLANTEST_ATTR_TID, atoi(argv[2])); |
| 1475 | |
| 1476 | rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp)); |
| 1477 | if (rlen < 0) |
| 1478 | return -1; |
| 1479 | |
| 1480 | pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_COUNTER, &len); |
| 1481 | if (pos == NULL || len != 4) |
| 1482 | return -1; |
| 1483 | printf("%u\n", WPA_GET_BE32(pos)); |
| 1484 | return 0; |
| 1485 | } |
| 1486 | |
| 1487 | |
| 1488 | static int cmd_get_rx_tid(int s, int argc, char *argv[]) |
| 1489 | { |
| 1490 | u8 resp[WLANTEST_CTRL_MAX_RESP_LEN]; |
| 1491 | u8 buf[100], *end, *pos; |
| 1492 | int rlen; |
| 1493 | size_t len; |
| 1494 | |
| 1495 | if (argc != 3) { |
| 1496 | printf("get_tx_tid needs three arguments: " |
| 1497 | "BSSID, STA address, and TID\n"); |
| 1498 | return -1; |
| 1499 | } |
| 1500 | |
| 1501 | pos = buf; |
| 1502 | end = buf + sizeof(buf); |
| 1503 | WPA_PUT_BE32(pos, WLANTEST_CTRL_GET_RX_TID); |
| 1504 | pos += 4; |
| 1505 | |
| 1506 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN); |
| 1507 | if (hwaddr_aton(argv[0], pos) < 0) { |
| 1508 | printf("Invalid BSSID '%s'\n", argv[0]); |
| 1509 | return -1; |
| 1510 | } |
| 1511 | pos += ETH_ALEN; |
| 1512 | |
| 1513 | pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN); |
| 1514 | if (hwaddr_aton(argv[1], pos) < 0) { |
| 1515 | printf("Invalid STA address '%s'\n", argv[1]); |
| 1516 | return -1; |
| 1517 | } |
| 1518 | pos += ETH_ALEN; |
| 1519 | |
| 1520 | pos = attr_add_be32(pos, end, WLANTEST_ATTR_TID, atoi(argv[2])); |
| 1521 | |
| 1522 | rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp)); |
| 1523 | if (rlen < 0) |
| 1524 | return -1; |
| 1525 | |
| 1526 | pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_COUNTER, &len); |
| 1527 | if (pos == NULL || len != 4) |
| 1528 | return -1; |
| 1529 | printf("%u\n", WPA_GET_BE32(pos)); |
| 1530 | return 0; |
| 1531 | } |
| 1532 | |
| 1533 | |
| 1534 | static char ** complete_get_tid(int s, const char *str, int pos) |
| 1535 | { |
| 1536 | int arg = get_cmd_arg_num(str, pos); |
| 1537 | char **res = NULL; |
| 1538 | u8 addr[ETH_ALEN]; |
| 1539 | |
| 1540 | switch (arg) { |
| 1541 | case 1: |
| 1542 | res = get_bssid_list(s); |
| 1543 | break; |
| 1544 | case 2: |
| 1545 | if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0) |
| 1546 | break; |
| 1547 | res = get_sta_list(s, addr, 0); |
| 1548 | break; |
| 1549 | } |
| 1550 | |
| 1551 | return res; |
| 1552 | } |
| 1553 | |
| 1554 | |
| 1555 | static int wlantest_cli_cmd_help(int s, int argc, char *argv[]) |
| 1556 | { |
| 1557 | print_help(stdout, argc > 0 ? argv[0] : NULL); |
| 1558 | return 0; |
| 1559 | } |
| 1560 | |
| 1561 | |
| 1562 | static char ** wlantest_cli_complete_help(int s, const char *str, int pos) |
| 1563 | { |
| 1564 | int arg = get_cmd_arg_num(str, pos); |
| 1565 | char **res = NULL; |
| 1566 | |
| 1567 | switch (arg) { |
| 1568 | case 1: |
| 1569 | res = wlantest_cli_cmd_list(); |
| 1570 | break; |
| 1571 | } |
| 1572 | |
| 1573 | return res; |
| 1574 | } |
| 1575 | |
| 1576 | |
| 1577 | struct wlantest_cli_cmd { |
| 1578 | const char *cmd; |
| 1579 | int (*handler)(int s, int argc, char *argv[]); |
| 1580 | const char *usage; |
| 1581 | char ** (*complete)(int s, const char *str, int pos); |
| 1582 | }; |
| 1583 | |
| 1584 | static const struct wlantest_cli_cmd wlantest_cli_commands[] = { |
| 1585 | { "ping", cmd_ping, "= test connection to wlantest", NULL }, |
| 1586 | { "terminate", cmd_terminate, "= terminate wlantest", NULL }, |
| 1587 | { "list_bss", cmd_list_bss, "= get BSS list", NULL }, |
| 1588 | { "list_sta", cmd_list_sta, "<BSSID> = get STA list", |
| 1589 | complete_list_sta }, |
| 1590 | { "flush", cmd_flush, "= drop all collected BSS data", NULL }, |
| 1591 | { "clear_sta_counters", cmd_clear_sta_counters, |
| 1592 | "<BSSID> <STA> = clear STA counters", complete_clear_sta_counters }, |
| 1593 | { "clear_bss_counters", cmd_clear_bss_counters, |
| 1594 | "<BSSID> = clear BSS counters", complete_clear_bss_counters }, |
| 1595 | { "get_sta_counter", cmd_get_sta_counter, |
| 1596 | "<counter> <BSSID> <STA> = get STA counter value", |
| 1597 | complete_get_sta_counter }, |
| 1598 | { "get_bss_counter", cmd_get_bss_counter, |
| 1599 | "<counter> <BSSID> = get BSS counter value", |
| 1600 | complete_get_bss_counter }, |
| 1601 | { "inject", cmd_inject, |
| 1602 | "<frame> <prot> <sender> <BSSID> <STA/ff:ff:ff:ff:ff:ff>", |
| 1603 | complete_inject }, |
| 1604 | { "send", cmd_send, |
| 1605 | "<prot> <raw frame as hex dump>", |
| 1606 | complete_send }, |
| 1607 | { "version", cmd_version, "= get wlantest version", NULL }, |
| 1608 | { "add_passphrase", cmd_add_passphrase, |
| 1609 | "<passphrase> = add a known passphrase", NULL }, |
| 1610 | { "add_wepkey", cmd_add_wepkey, |
| 1611 | "<WEP key> = add a known WEP key", NULL }, |
| 1612 | { "info_sta", cmd_info_sta, |
| 1613 | "<field> <BSSID> <STA> = get STA information", |
| 1614 | complete_info_sta }, |
| 1615 | { "info_bss", cmd_info_bss, |
| 1616 | "<field> <BSSID> = get BSS information", |
| 1617 | complete_info_bss }, |
| 1618 | { "clear_tdls_counters", cmd_clear_tdls_counters, |
| 1619 | "<BSSID> <STA1> <STA2> = clear TDLS counters", |
| 1620 | complete_clear_tdls_counters }, |
| 1621 | { "get_tdls_counter", cmd_get_tdls_counter, |
| 1622 | "<counter> <BSSID> <STA1> <STA2> = get TDLS counter value", |
| 1623 | complete_get_tdls_counter }, |
| 1624 | { "get_bss_counter", cmd_get_bss_counter, |
| 1625 | "<counter> <BSSID> = get BSS counter value", |
| 1626 | complete_get_bss_counter }, |
| 1627 | { "relog", cmd_relog, "= re-open log-file (allow rolling logs)", NULL }, |
| 1628 | { "get_tx_tid", cmd_get_tx_tid, |
| 1629 | "<BSSID> <STA> <TID> = get STA TX TID counter value", |
| 1630 | complete_get_tid }, |
| 1631 | { "get_rx_tid", cmd_get_rx_tid, |
| 1632 | "<BSSID> <STA> <TID> = get STA RX TID counter value", |
| 1633 | complete_get_tid }, |
| 1634 | { "help", wlantest_cli_cmd_help, |
| 1635 | "= show this usage help", wlantest_cli_complete_help }, |
| 1636 | { NULL, NULL, NULL, NULL } |
| 1637 | }; |
| 1638 | |
| 1639 | |
| 1640 | /* |
| 1641 | * Prints command usage, lines are padded with the specified string. |
| 1642 | */ |
| 1643 | static void print_cmd_help(FILE *stream, const struct wlantest_cli_cmd *cmd, |
| 1644 | const char *pad) |
| 1645 | { |
| 1646 | char c; |
| 1647 | size_t n; |
| 1648 | |
| 1649 | if (!cmd->usage) |
| 1650 | return; |
| 1651 | fprintf(stream, "%s%s ", pad, cmd->cmd); |
| 1652 | for (n = 0; (c = cmd->usage[n]); n++) { |
| 1653 | fprintf(stream, "%c", c); |
| 1654 | if (c == '\n') |
| 1655 | fprintf(stream, "%s", pad); |
| 1656 | } |
| 1657 | fprintf(stream, "\n"); |
| 1658 | } |
| 1659 | |
| 1660 | |
| 1661 | static void print_help(FILE *stream, const char *cmd) |
| 1662 | { |
| 1663 | int n; |
| 1664 | |
| 1665 | fprintf(stream, "commands:\n"); |
| 1666 | for (n = 0; wlantest_cli_commands[n].cmd; n++) { |
| 1667 | if (!cmd || str_starts(wlantest_cli_commands[n].cmd, cmd)) |
| 1668 | print_cmd_help(stream, &wlantest_cli_commands[n], " "); |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | |
| 1673 | static int ctrl_command(int s, int argc, char *argv[]) |
| 1674 | { |
| 1675 | const struct wlantest_cli_cmd *cmd, *match = NULL; |
| 1676 | int count = 0; |
| 1677 | int ret = 0; |
| 1678 | |
| 1679 | for (cmd = wlantest_cli_commands; cmd->cmd; cmd++) { |
| 1680 | if (os_strncasecmp(cmd->cmd, argv[0], os_strlen(argv[0])) == 0) |
| 1681 | { |
| 1682 | match = cmd; |
| 1683 | if (os_strcasecmp(cmd->cmd, argv[0]) == 0) { |
| 1684 | /* exact match */ |
| 1685 | count = 1; |
| 1686 | break; |
| 1687 | } |
| 1688 | count++; |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | if (count > 1) { |
| 1693 | printf("Ambiguous command '%s'; possible commands:", argv[0]); |
| 1694 | for (cmd = wlantest_cli_commands; cmd->cmd; cmd++) { |
| 1695 | if (os_strncasecmp(cmd->cmd, argv[0], |
| 1696 | os_strlen(argv[0])) == 0) { |
| 1697 | printf(" %s", cmd->cmd); |
| 1698 | } |
| 1699 | } |
| 1700 | printf("\n"); |
| 1701 | ret = 1; |
| 1702 | } else if (count == 0) { |
| 1703 | printf("Unknown command '%s'\n", argv[0]); |
| 1704 | ret = 1; |
| 1705 | } else { |
| 1706 | ret = match->handler(s, argc - 1, &argv[1]); |
| 1707 | } |
| 1708 | |
| 1709 | return ret; |
| 1710 | } |
| 1711 | |
| 1712 | |
| 1713 | struct wlantest_cli { |
| 1714 | int s; |
| 1715 | }; |
| 1716 | |
| 1717 | |
| 1718 | static void wlantest_cli_edit_cmd_cb(void *ctx, char *cmd) |
| 1719 | { |
| 1720 | struct wlantest_cli *cli = ctx; |
| 1721 | char *argv[max_args]; |
| 1722 | int argc; |
| 1723 | argc = tokenize_cmd(cmd, argv); |
| 1724 | if (argc) { |
| 1725 | int ret = ctrl_command(cli->s, argc, argv); |
| 1726 | if (ret < 0) |
| 1727 | printf("FAIL\n"); |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | |
| 1732 | static void wlantest_cli_eloop_terminate(int sig, void *signal_ctx) |
| 1733 | { |
| 1734 | eloop_terminate(); |
| 1735 | } |
| 1736 | |
| 1737 | |
| 1738 | static void wlantest_cli_edit_eof_cb(void *ctx) |
| 1739 | { |
| 1740 | eloop_terminate(); |
| 1741 | } |
| 1742 | |
| 1743 | |
| 1744 | static char ** wlantest_cli_cmd_list(void) |
| 1745 | { |
| 1746 | char **res; |
| 1747 | int i; |
| 1748 | |
| 1749 | res = os_calloc(ARRAY_SIZE(wlantest_cli_commands), sizeof(char *)); |
| 1750 | if (res == NULL) |
| 1751 | return NULL; |
| 1752 | |
| 1753 | for (i = 0; wlantest_cli_commands[i].cmd; i++) { |
| 1754 | res[i] = os_strdup(wlantest_cli_commands[i].cmd); |
| 1755 | if (res[i] == NULL) |
| 1756 | break; |
| 1757 | } |
| 1758 | |
| 1759 | return res; |
| 1760 | } |
| 1761 | |
| 1762 | |
| 1763 | static char ** wlantest_cli_cmd_completion(struct wlantest_cli *cli, |
| 1764 | const char *cmd, const char *str, |
| 1765 | int pos) |
| 1766 | { |
| 1767 | int i; |
| 1768 | |
| 1769 | for (i = 0; wlantest_cli_commands[i].cmd; i++) { |
| 1770 | const struct wlantest_cli_cmd *c = &wlantest_cli_commands[i]; |
| 1771 | if (os_strcasecmp(c->cmd, cmd) == 0) { |
| 1772 | edit_clear_line(); |
| 1773 | printf("\r%s\n", c->usage); |
| 1774 | edit_redraw(); |
| 1775 | if (c->complete) |
| 1776 | return c->complete(cli->s, str, pos); |
| 1777 | break; |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | return NULL; |
| 1782 | } |
| 1783 | |
| 1784 | |
| 1785 | static char ** wlantest_cli_edit_completion_cb(void *ctx, const char *str, |
| 1786 | int pos) |
| 1787 | { |
| 1788 | struct wlantest_cli *cli = ctx; |
| 1789 | char **res; |
| 1790 | const char *end; |
| 1791 | char *cmd; |
| 1792 | |
| 1793 | end = os_strchr(str, ' '); |
| 1794 | if (end == NULL || str + pos < end) |
| 1795 | return wlantest_cli_cmd_list(); |
| 1796 | |
| 1797 | cmd = os_malloc(pos + 1); |
| 1798 | if (cmd == NULL) |
| 1799 | return NULL; |
| 1800 | os_memcpy(cmd, str, pos); |
| 1801 | cmd[end - str] = '\0'; |
| 1802 | res = wlantest_cli_cmd_completion(cli, cmd, str, pos); |
| 1803 | os_free(cmd); |
| 1804 | return res; |
| 1805 | } |
| 1806 | |
| 1807 | |
| 1808 | static void wlantest_cli_interactive(int s) |
| 1809 | { |
| 1810 | struct wlantest_cli cli; |
| 1811 | char *home, *hfile = NULL; |
| 1812 | |
| 1813 | if (eloop_init()) |
| 1814 | return; |
| 1815 | |
| 1816 | home = getenv("HOME"); |
| 1817 | if (home) { |
| 1818 | const char *fname = ".wlantest_cli_history"; |
| 1819 | int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1; |
| 1820 | hfile = os_malloc(hfile_len); |
| 1821 | if (hfile) |
| 1822 | os_snprintf(hfile, hfile_len, "%s/%s", home, fname); |
| 1823 | } |
| 1824 | |
| 1825 | cli.s = s; |
| 1826 | eloop_register_signal_terminate(wlantest_cli_eloop_terminate, &cli); |
| 1827 | edit_init(wlantest_cli_edit_cmd_cb, wlantest_cli_edit_eof_cb, |
| 1828 | wlantest_cli_edit_completion_cb, &cli, hfile, NULL); |
| 1829 | |
| 1830 | eloop_run(); |
| 1831 | |
| 1832 | edit_deinit(hfile, NULL); |
| 1833 | os_free(hfile); |
| 1834 | eloop_destroy(); |
| 1835 | } |
| 1836 | |
| 1837 | |
| 1838 | int main(int argc, char *argv[]) |
| 1839 | { |
| 1840 | int s; |
| 1841 | struct sockaddr_un addr; |
| 1842 | int ret = 0; |
| 1843 | |
| 1844 | if (os_program_init()) |
| 1845 | return -1; |
| 1846 | |
| 1847 | s = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
| 1848 | if (s < 0) { |
| 1849 | perror("socket"); |
| 1850 | return -1; |
| 1851 | } |
| 1852 | |
| 1853 | os_memset(&addr, 0, sizeof(addr)); |
| 1854 | addr.sun_family = AF_UNIX; |
| 1855 | os_strlcpy(addr.sun_path + 1, WLANTEST_SOCK_NAME, |
| 1856 | sizeof(addr.sun_path) - 1); |
| 1857 | if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 1858 | perror("connect"); |
| 1859 | close(s); |
| 1860 | return -1; |
| 1861 | } |
| 1862 | |
| 1863 | if (argc > 1) { |
| 1864 | ret = ctrl_command(s, argc - 1, &argv[1]); |
| 1865 | if (ret < 0) |
| 1866 | printf("FAIL\n"); |
| 1867 | } else { |
| 1868 | wlantest_cli_interactive(s); |
| 1869 | } |
| 1870 | |
| 1871 | close(s); |
| 1872 | |
| 1873 | os_program_deinit(); |
| 1874 | |
| 1875 | return ret; |
| 1876 | } |