wlantest_cli.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. /*
  2. * wlantest controller
  3. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include <sys/un.h>
  16. #include "utils/common.h"
  17. #include "utils/eloop.h"
  18. #include "utils/edit.h"
  19. #include "wlantest_ctrl.h"
  20. static int get_cmd_arg_num(const char *str, int pos)
  21. {
  22. int arg = 0, i;
  23. for (i = 0; i <= pos; i++) {
  24. if (str[i] != ' ') {
  25. arg++;
  26. while (i <= pos && str[i] != ' ')
  27. i++;
  28. }
  29. }
  30. if (arg > 0)
  31. arg--;
  32. return arg;
  33. }
  34. static int get_prev_arg_pos(const char *str, int pos)
  35. {
  36. while (pos > 0 && str[pos - 1] != ' ')
  37. pos--;
  38. while (pos > 0 && str[pos - 1] == ' ')
  39. pos--;
  40. while (pos > 0 && str[pos - 1] != ' ')
  41. pos--;
  42. return pos;
  43. }
  44. static u8 * attr_get(u8 *buf, size_t buflen, enum wlantest_ctrl_attr attr,
  45. size_t *len)
  46. {
  47. u8 *pos = buf;
  48. while (pos + 8 <= buf + buflen) {
  49. enum wlantest_ctrl_attr a;
  50. size_t alen;
  51. a = WPA_GET_BE32(pos);
  52. pos += 4;
  53. alen = WPA_GET_BE32(pos);
  54. pos += 4;
  55. if (pos + alen > buf + buflen) {
  56. printf("Invalid control message attribute\n");
  57. return NULL;
  58. }
  59. if (a == attr) {
  60. *len = alen;
  61. return pos;
  62. }
  63. pos += alen;
  64. }
  65. return NULL;
  66. }
  67. static u8 * attr_hdr_add(u8 *pos, u8 *end, enum wlantest_ctrl_attr attr,
  68. size_t len)
  69. {
  70. if (pos == NULL || end - pos < 8 + len)
  71. return NULL;
  72. WPA_PUT_BE32(pos, attr);
  73. pos += 4;
  74. WPA_PUT_BE32(pos, len);
  75. pos += 4;
  76. return pos;
  77. }
  78. static u8 * attr_add_str(u8 *pos, u8 *end, enum wlantest_ctrl_attr attr,
  79. const char *str)
  80. {
  81. size_t len = os_strlen(str);
  82. if (pos == NULL || end - pos < 8 + len)
  83. return NULL;
  84. WPA_PUT_BE32(pos, attr);
  85. pos += 4;
  86. WPA_PUT_BE32(pos, len);
  87. pos += 4;
  88. os_memcpy(pos, str, len);
  89. pos += len;
  90. return pos;
  91. }
  92. static u8 * attr_add_be32(u8 *pos, u8 *end, enum wlantest_ctrl_attr attr,
  93. u32 val)
  94. {
  95. if (pos == NULL || end - pos < 12)
  96. return NULL;
  97. WPA_PUT_BE32(pos, attr);
  98. pos += 4;
  99. WPA_PUT_BE32(pos, 4);
  100. pos += 4;
  101. WPA_PUT_BE32(pos, val);
  102. pos += 4;
  103. return pos;
  104. }
  105. static int cmd_send_and_recv(int s, const u8 *cmd, size_t cmd_len,
  106. u8 *resp, size_t max_resp_len)
  107. {
  108. int res;
  109. enum wlantest_ctrl_cmd cmd_resp;
  110. if (send(s, cmd, cmd_len, 0) < 0)
  111. return -1;
  112. res = recv(s, resp, max_resp_len, 0);
  113. if (res < 4)
  114. return -1;
  115. cmd_resp = WPA_GET_BE32(resp);
  116. if (cmd_resp == WLANTEST_CTRL_SUCCESS)
  117. return res;
  118. if (cmd_resp == WLANTEST_CTRL_UNKNOWN_CMD)
  119. printf("Unknown command\n");
  120. else if (cmd_resp == WLANTEST_CTRL_INVALID_CMD)
  121. printf("Invalid command\n");
  122. return -1;
  123. }
  124. static int cmd_simple(int s, enum wlantest_ctrl_cmd cmd)
  125. {
  126. u8 buf[4];
  127. int res;
  128. WPA_PUT_BE32(buf, cmd);
  129. res = cmd_send_and_recv(s, buf, sizeof(buf), buf, sizeof(buf));
  130. return res < 0 ? -1 : 0;
  131. }
  132. static char ** get_bssid_list(int s)
  133. {
  134. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  135. u8 buf[4];
  136. u8 *bssid;
  137. size_t len;
  138. int rlen, i;
  139. char **res;
  140. WPA_PUT_BE32(buf, WLANTEST_CTRL_LIST_BSS);
  141. rlen = cmd_send_and_recv(s, buf, sizeof(buf), resp, sizeof(resp));
  142. if (rlen < 0)
  143. return NULL;
  144. bssid = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_BSSID, &len);
  145. if (bssid == NULL)
  146. return NULL;
  147. res = os_zalloc((len / ETH_ALEN + 1) * sizeof(char *));
  148. if (res == NULL)
  149. return NULL;
  150. for (i = 0; i < len / ETH_ALEN; i++) {
  151. res[i] = os_zalloc(18);
  152. if (res[i] == NULL)
  153. break;
  154. os_snprintf(res[i], 18, MACSTR, MAC2STR(bssid + ETH_ALEN * i));
  155. }
  156. return res;
  157. }
  158. static char ** get_sta_list(int s, const u8 *bssid, int add_bcast)
  159. {
  160. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  161. u8 buf[100], *pos, *end;
  162. u8 *addr;
  163. size_t len;
  164. int rlen, i;
  165. char **res;
  166. pos = buf;
  167. end = buf + sizeof(buf);
  168. WPA_PUT_BE32(pos, WLANTEST_CTRL_LIST_STA);
  169. pos += 4;
  170. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  171. os_memcpy(pos, bssid, ETH_ALEN);
  172. pos += ETH_ALEN;
  173. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  174. if (rlen < 0)
  175. return NULL;
  176. addr = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_STA_ADDR, &len);
  177. if (addr == NULL)
  178. return NULL;
  179. res = os_zalloc((len / ETH_ALEN + 1 + add_bcast) * sizeof(char *));
  180. if (res == NULL)
  181. return NULL;
  182. for (i = 0; i < len / ETH_ALEN; i++) {
  183. res[i] = os_zalloc(18);
  184. if (res[i] == NULL)
  185. break;
  186. os_snprintf(res[i], 18, MACSTR, MAC2STR(addr + ETH_ALEN * i));
  187. }
  188. if (add_bcast)
  189. res[i] = os_strdup("ff:ff:ff:ff:ff:ff");
  190. return res;
  191. }
  192. static int cmd_ping(int s, int argc, char *argv[])
  193. {
  194. int res = cmd_simple(s, WLANTEST_CTRL_PING);
  195. if (res == 0)
  196. printf("PONG\n");
  197. return res == 0;
  198. }
  199. static int cmd_terminate(int s, int argc, char *argv[])
  200. {
  201. return cmd_simple(s, WLANTEST_CTRL_TERMINATE);
  202. }
  203. static int cmd_list_bss(int s, int argc, char *argv[])
  204. {
  205. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  206. u8 buf[4];
  207. u8 *bssid;
  208. size_t len;
  209. int rlen, i;
  210. WPA_PUT_BE32(buf, WLANTEST_CTRL_LIST_BSS);
  211. rlen = cmd_send_and_recv(s, buf, sizeof(buf), resp, sizeof(resp));
  212. if (rlen < 0)
  213. return -1;
  214. bssid = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_BSSID, &len);
  215. if (bssid == NULL)
  216. return -1;
  217. for (i = 0; i < len / ETH_ALEN; i++)
  218. printf(MACSTR " ", MAC2STR(bssid + ETH_ALEN * i));
  219. printf("\n");
  220. return 0;
  221. }
  222. static int cmd_list_sta(int s, int argc, char *argv[])
  223. {
  224. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  225. u8 buf[100], *pos;
  226. u8 *addr;
  227. size_t len;
  228. int rlen, i;
  229. if (argc < 1) {
  230. printf("list_sta needs one argument: BSSID\n");
  231. return -1;
  232. }
  233. pos = buf;
  234. WPA_PUT_BE32(pos, WLANTEST_CTRL_LIST_STA);
  235. pos += 4;
  236. WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID);
  237. pos += 4;
  238. WPA_PUT_BE32(pos, ETH_ALEN);
  239. pos += 4;
  240. if (hwaddr_aton(argv[0], pos) < 0) {
  241. printf("Invalid BSSID '%s'\n", argv[0]);
  242. return -1;
  243. }
  244. pos += ETH_ALEN;
  245. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  246. if (rlen < 0)
  247. return -1;
  248. addr = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_STA_ADDR, &len);
  249. if (addr == NULL)
  250. return -1;
  251. for (i = 0; i < len / ETH_ALEN; i++)
  252. printf(MACSTR " ", MAC2STR(addr + ETH_ALEN * i));
  253. printf("\n");
  254. return 0;
  255. }
  256. static char ** complete_list_sta(int s, const char *str, int pos)
  257. {
  258. if (get_cmd_arg_num(str, pos) == 1)
  259. return get_bssid_list(s);
  260. return NULL;
  261. }
  262. static int cmd_flush(int s, int argc, char *argv[])
  263. {
  264. return cmd_simple(s, WLANTEST_CTRL_FLUSH);
  265. }
  266. static int cmd_clear_sta_counters(int s, int argc, char *argv[])
  267. {
  268. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  269. u8 buf[100], *pos;
  270. int rlen;
  271. if (argc < 2) {
  272. printf("clear_sta_counters needs two arguments: BSSID and "
  273. "STA address\n");
  274. return -1;
  275. }
  276. pos = buf;
  277. WPA_PUT_BE32(pos, WLANTEST_CTRL_CLEAR_STA_COUNTERS);
  278. pos += 4;
  279. WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID);
  280. pos += 4;
  281. WPA_PUT_BE32(pos, ETH_ALEN);
  282. pos += 4;
  283. if (hwaddr_aton(argv[0], pos) < 0) {
  284. printf("Invalid BSSID '%s'\n", argv[0]);
  285. return -1;
  286. }
  287. pos += ETH_ALEN;
  288. WPA_PUT_BE32(pos, WLANTEST_ATTR_STA_ADDR);
  289. pos += 4;
  290. WPA_PUT_BE32(pos, ETH_ALEN);
  291. pos += 4;
  292. if (hwaddr_aton(argv[1], pos) < 0) {
  293. printf("Invalid STA address '%s'\n", argv[1]);
  294. return -1;
  295. }
  296. pos += ETH_ALEN;
  297. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  298. if (rlen < 0)
  299. return -1;
  300. printf("OK\n");
  301. return 0;
  302. }
  303. static char ** complete_clear_sta_counters(int s, const char *str, int pos)
  304. {
  305. int arg = get_cmd_arg_num(str, pos);
  306. char **res = NULL;
  307. u8 addr[ETH_ALEN];
  308. switch (arg) {
  309. case 1:
  310. res = get_bssid_list(s);
  311. break;
  312. case 2:
  313. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  314. break;
  315. res = get_sta_list(s, addr, 0);
  316. break;
  317. }
  318. return res;
  319. }
  320. static int cmd_clear_bss_counters(int s, int argc, char *argv[])
  321. {
  322. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  323. u8 buf[100], *pos;
  324. int rlen;
  325. if (argc < 1) {
  326. printf("clear_bss_counters needs one argument: BSSID\n");
  327. return -1;
  328. }
  329. pos = buf;
  330. WPA_PUT_BE32(pos, WLANTEST_CTRL_CLEAR_BSS_COUNTERS);
  331. pos += 4;
  332. WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID);
  333. pos += 4;
  334. WPA_PUT_BE32(pos, ETH_ALEN);
  335. pos += 4;
  336. if (hwaddr_aton(argv[0], pos) < 0) {
  337. printf("Invalid BSSID '%s'\n", argv[0]);
  338. return -1;
  339. }
  340. pos += ETH_ALEN;
  341. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  342. if (rlen < 0)
  343. return -1;
  344. printf("OK\n");
  345. return 0;
  346. }
  347. static char ** complete_clear_bss_counters(int s, const char *str, int pos)
  348. {
  349. if (get_cmd_arg_num(str, pos) == 1)
  350. return get_bssid_list(s);
  351. return NULL;
  352. }
  353. struct sta_counters {
  354. const char *name;
  355. enum wlantest_sta_counter num;
  356. };
  357. static const struct sta_counters sta_counters[] = {
  358. { "auth_tx", WLANTEST_STA_COUNTER_AUTH_TX },
  359. { "auth_rx", WLANTEST_STA_COUNTER_AUTH_RX },
  360. { "assocreq_tx", WLANTEST_STA_COUNTER_ASSOCREQ_TX },
  361. { "reassocreq_tx", WLANTEST_STA_COUNTER_REASSOCREQ_TX },
  362. { "ptk_learned", WLANTEST_STA_COUNTER_PTK_LEARNED },
  363. { "valid_deauth_tx", WLANTEST_STA_COUNTER_VALID_DEAUTH_TX },
  364. { "valid_deauth_rx", WLANTEST_STA_COUNTER_VALID_DEAUTH_RX },
  365. { "invalid_deauth_tx", WLANTEST_STA_COUNTER_INVALID_DEAUTH_TX },
  366. { "invalid_deauth_rx", WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX },
  367. { "valid_disassoc_tx", WLANTEST_STA_COUNTER_VALID_DISASSOC_TX },
  368. { "valid_disassoc_rx", WLANTEST_STA_COUNTER_VALID_DISASSOC_RX },
  369. { "invalid_disassoc_tx", WLANTEST_STA_COUNTER_INVALID_DISASSOC_TX },
  370. { "invalid_disassoc_rx", WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX },
  371. { "valid_saqueryreq_tx", WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_TX },
  372. { "valid_saqueryreq_rx", WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_RX },
  373. { "invalid_saqueryreq_tx",
  374. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_TX },
  375. { "invalid_saqueryreq_rx",
  376. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_RX },
  377. { "valid_saqueryresp_tx", WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_TX },
  378. { "valid_saqueryresp_rx", WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_RX },
  379. { "invalid_saqueryresp_tx",
  380. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_TX },
  381. { "invalid_saqueryresp_rx",
  382. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_RX },
  383. { "ping_ok", WLANTEST_STA_COUNTER_PING_OK },
  384. { NULL, 0 }
  385. };
  386. static int cmd_get_sta_counter(int s, int argc, char *argv[])
  387. {
  388. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  389. u8 buf[100], *end, *pos;
  390. int rlen, i;
  391. size_t len;
  392. if (argc != 3) {
  393. printf("get_sta_counter needs at three arguments: "
  394. "counter name, BSSID, and STA address\n");
  395. return -1;
  396. }
  397. pos = buf;
  398. end = buf + sizeof(buf);
  399. WPA_PUT_BE32(pos, WLANTEST_CTRL_GET_STA_COUNTER);
  400. pos += 4;
  401. for (i = 0; sta_counters[i].name; i++) {
  402. if (os_strcasecmp(sta_counters[i].name, argv[0]) == 0)
  403. break;
  404. }
  405. if (sta_counters[i].name == NULL) {
  406. printf("Unknown STA counter '%s'\n", argv[0]);
  407. printf("Counters:");
  408. for (i = 0; sta_counters[i].name; i++)
  409. printf(" %s", sta_counters[i].name);
  410. printf("\n");
  411. return -1;
  412. }
  413. pos = attr_add_be32(pos, end, WLANTEST_ATTR_STA_COUNTER,
  414. sta_counters[i].num);
  415. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  416. if (hwaddr_aton(argv[1], pos) < 0) {
  417. printf("Invalid BSSID '%s'\n", argv[1]);
  418. return -1;
  419. }
  420. pos += ETH_ALEN;
  421. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN);
  422. if (hwaddr_aton(argv[2], pos) < 0) {
  423. printf("Invalid STA address '%s'\n", argv[2]);
  424. return -1;
  425. }
  426. pos += ETH_ALEN;
  427. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  428. if (rlen < 0)
  429. return -1;
  430. pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_COUNTER, &len);
  431. if (pos == NULL || len != 4)
  432. return -1;
  433. printf("%u\n", WPA_GET_BE32(pos));
  434. return 0;
  435. }
  436. static char ** complete_get_sta_counter(int s, const char *str, int pos)
  437. {
  438. int arg = get_cmd_arg_num(str, pos);
  439. char **res = NULL;
  440. int i, count;
  441. u8 addr[ETH_ALEN];
  442. switch (arg) {
  443. case 1:
  444. /* counter list */
  445. count = sizeof(sta_counters) / sizeof(sta_counters[0]);
  446. res = os_zalloc(count * sizeof(char *));
  447. if (res == NULL)
  448. return NULL;
  449. for (i = 0; sta_counters[i].name; i++) {
  450. res[i] = os_strdup(sta_counters[i].name);
  451. if (res[i] == NULL)
  452. break;
  453. }
  454. break;
  455. case 2:
  456. res = get_bssid_list(s);
  457. break;
  458. case 3:
  459. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  460. break;
  461. res = get_sta_list(s, addr, 0);
  462. break;
  463. }
  464. return res;
  465. }
  466. struct bss_counters {
  467. const char *name;
  468. enum wlantest_bss_counter num;
  469. };
  470. static const struct bss_counters bss_counters[] = {
  471. { "valid_bip_mmie", WLANTEST_BSS_COUNTER_VALID_BIP_MMIE },
  472. { "invalid_bip_mmie", WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE },
  473. { "missing_bip_mmie", WLANTEST_BSS_COUNTER_MISSING_BIP_MMIE },
  474. { NULL, 0 }
  475. };
  476. static int cmd_get_bss_counter(int s, int argc, char *argv[])
  477. {
  478. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  479. u8 buf[100], *end, *pos;
  480. int rlen, i;
  481. size_t len;
  482. if (argc != 2) {
  483. printf("get_bss_counter needs at three arguments: "
  484. "counter name and BSSID\n");
  485. return -1;
  486. }
  487. pos = buf;
  488. end = buf + sizeof(buf);
  489. WPA_PUT_BE32(pos, WLANTEST_CTRL_GET_BSS_COUNTER);
  490. pos += 4;
  491. for (i = 0; bss_counters[i].name; i++) {
  492. if (os_strcasecmp(bss_counters[i].name, argv[0]) == 0)
  493. break;
  494. }
  495. if (bss_counters[i].name == NULL) {
  496. printf("Unknown BSS counter '%s'\n", argv[0]);
  497. printf("Counters:");
  498. for (i = 0; bss_counters[i].name; i++)
  499. printf(" %s", bss_counters[i].name);
  500. printf("\n");
  501. return -1;
  502. }
  503. pos = attr_add_be32(pos, end, WLANTEST_ATTR_BSS_COUNTER,
  504. bss_counters[i].num);
  505. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  506. if (hwaddr_aton(argv[1], pos) < 0) {
  507. printf("Invalid BSSID '%s'\n", argv[1]);
  508. return -1;
  509. }
  510. pos += ETH_ALEN;
  511. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  512. if (rlen < 0)
  513. return -1;
  514. pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_COUNTER, &len);
  515. if (pos == NULL || len != 4)
  516. return -1;
  517. printf("%u\n", WPA_GET_BE32(pos));
  518. return 0;
  519. }
  520. static char ** complete_get_bss_counter(int s, const char *str, int pos)
  521. {
  522. int arg = get_cmd_arg_num(str, pos);
  523. char **res = NULL;
  524. int i, count;
  525. switch (arg) {
  526. case 1:
  527. /* counter list */
  528. count = sizeof(bss_counters) / sizeof(bss_counters[0]);
  529. res = os_zalloc(count * sizeof(char *));
  530. if (res == NULL)
  531. return NULL;
  532. for (i = 0; bss_counters[i].name; i++) {
  533. res[i] = os_strdup(bss_counters[i].name);
  534. if (res[i] == NULL)
  535. break;
  536. }
  537. break;
  538. case 2:
  539. res = get_bssid_list(s);
  540. break;
  541. }
  542. return res;
  543. }
  544. struct inject_frames {
  545. const char *name;
  546. enum wlantest_inject_frame frame;
  547. };
  548. static const struct inject_frames inject_frames[] = {
  549. { "auth", WLANTEST_FRAME_AUTH },
  550. { "assocreq", WLANTEST_FRAME_ASSOCREQ },
  551. { "reassocreq", WLANTEST_FRAME_REASSOCREQ },
  552. { "deauth", WLANTEST_FRAME_DEAUTH },
  553. { "disassoc", WLANTEST_FRAME_DISASSOC },
  554. { "saqueryreq", WLANTEST_FRAME_SAQUERYREQ },
  555. { NULL, 0 }
  556. };
  557. static int cmd_inject(int s, int argc, char *argv[])
  558. {
  559. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  560. u8 buf[100], *end, *pos;
  561. int rlen, i;
  562. enum wlantest_inject_protection prot;
  563. /* <frame> <prot> <sender> <BSSID> <STA/ff:ff:ff:ff:ff:ff> */
  564. if (argc < 5) {
  565. printf("inject needs five arguments: frame, protection, "
  566. "sender, BSSID, STA/ff:ff:ff:ff:ff:ff\n");
  567. return -1;
  568. }
  569. pos = buf;
  570. end = buf + sizeof(buf);
  571. WPA_PUT_BE32(pos, WLANTEST_CTRL_INJECT);
  572. pos += 4;
  573. for (i = 0; inject_frames[i].name; i++) {
  574. if (os_strcasecmp(inject_frames[i].name, argv[0]) == 0)
  575. break;
  576. }
  577. if (inject_frames[i].name == NULL) {
  578. printf("Unknown inject frame '%s'\n", argv[0]);
  579. printf("Frames:");
  580. for (i = 0; inject_frames[i].name; i++)
  581. printf(" %s", inject_frames[i].name);
  582. printf("\n");
  583. return -1;
  584. }
  585. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_FRAME,
  586. inject_frames[i].frame);
  587. if (os_strcasecmp(argv[1], "normal") == 0)
  588. prot = WLANTEST_INJECT_NORMAL;
  589. else if (os_strcasecmp(argv[1], "protected") == 0)
  590. prot = WLANTEST_INJECT_PROTECTED;
  591. else if (os_strcasecmp(argv[1], "unprotected") == 0)
  592. prot = WLANTEST_INJECT_UNPROTECTED;
  593. else if (os_strcasecmp(argv[1], "incorrect") == 0)
  594. prot = WLANTEST_INJECT_INCORRECT_KEY;
  595. else {
  596. printf("Unknown protection type '%s'\n", argv[1]);
  597. printf("Protection types: normal protected unprotected "
  598. "incorrect\n");
  599. return -1;
  600. }
  601. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_PROTECTION, prot);
  602. if (os_strcasecmp(argv[2], "ap") == 0) {
  603. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_SENDER_AP,
  604. 1);
  605. } else if (os_strcasecmp(argv[2], "sta") == 0) {
  606. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_SENDER_AP,
  607. 0);
  608. } else {
  609. printf("Unknown sender '%s'\n", argv[2]);
  610. printf("Sender types: ap sta\n");
  611. return -1;
  612. }
  613. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  614. if (hwaddr_aton(argv[3], pos) < 0) {
  615. printf("Invalid BSSID '%s'\n", argv[3]);
  616. return -1;
  617. }
  618. pos += ETH_ALEN;
  619. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN);
  620. if (hwaddr_aton(argv[4], pos) < 0) {
  621. printf("Invalid STA '%s'\n", argv[4]);
  622. return -1;
  623. }
  624. pos += ETH_ALEN;
  625. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  626. if (rlen < 0)
  627. return -1;
  628. printf("OK\n");
  629. return 0;
  630. }
  631. static char ** complete_inject(int s, const char *str, int pos)
  632. {
  633. int arg = get_cmd_arg_num(str, pos);
  634. char **res = NULL;
  635. int i, count;
  636. u8 addr[ETH_ALEN];
  637. switch (arg) {
  638. case 1:
  639. /* frame list */
  640. count = sizeof(inject_frames) / sizeof(inject_frames[0]);
  641. res = os_zalloc(count * sizeof(char *));
  642. if (res == NULL)
  643. break;
  644. for (i = 0; inject_frames[i].name; i++) {
  645. res[i] = os_strdup(inject_frames[i].name);
  646. if (res[i] == NULL)
  647. break;
  648. }
  649. break;
  650. case 2:
  651. res = os_zalloc(5 * sizeof(char *));
  652. if (res == NULL)
  653. break;
  654. res[0] = os_strdup("normal");
  655. if (res[0] == NULL)
  656. break;
  657. res[1] = os_strdup("protected");
  658. if (res[1] == NULL)
  659. break;
  660. res[2] = os_strdup("unprotected");
  661. if (res[2] == NULL)
  662. break;
  663. res[3] = os_strdup("incorrect");
  664. if (res[3] == NULL)
  665. break;
  666. break;
  667. case 3:
  668. res = os_zalloc(3 * sizeof(char *));
  669. if (res == NULL)
  670. break;
  671. res[0] = os_strdup("ap");
  672. if (res[0] == NULL)
  673. break;
  674. res[1] = os_strdup("sta");
  675. if (res[1] == NULL)
  676. break;
  677. break;
  678. case 4:
  679. res = get_bssid_list(s);
  680. break;
  681. case 5:
  682. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  683. break;
  684. res = get_sta_list(s, addr, 1);
  685. break;
  686. }
  687. return res;
  688. }
  689. static int cmd_version(int s, int argc, char *argv[])
  690. {
  691. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  692. u8 buf[4];
  693. char *version;
  694. size_t len;
  695. int rlen, i;
  696. WPA_PUT_BE32(buf, WLANTEST_CTRL_VERSION);
  697. rlen = cmd_send_and_recv(s, buf, sizeof(buf), resp, sizeof(resp));
  698. if (rlen < 0)
  699. return -1;
  700. version = (char *) attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_VERSION,
  701. &len);
  702. if (version == NULL)
  703. return -1;
  704. for (i = 0; i < len; i++)
  705. putchar(version[i]);
  706. printf("\n");
  707. return 0;
  708. }
  709. static int cmd_add_passphrase(int s, int argc, char *argv[])
  710. {
  711. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  712. u8 buf[100], *pos, *end;
  713. size_t len;
  714. int rlen;
  715. if (argc < 1) {
  716. printf("add_passphrase needs one argument: passphrase\n");
  717. return -1;
  718. }
  719. len = os_strlen(argv[0]);
  720. if (len < 8 || len > 63) {
  721. printf("Invalid passphrase '%s'\n", argv[0]);
  722. return -1;
  723. }
  724. pos = buf;
  725. end = buf + sizeof(buf);
  726. WPA_PUT_BE32(pos, WLANTEST_CTRL_ADD_PASSPHRASE);
  727. pos += 4;
  728. pos = attr_add_str(pos, end, WLANTEST_ATTR_PASSPHRASE,
  729. argv[0]);
  730. if (argc > 1) {
  731. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  732. if (hwaddr_aton(argv[1], pos) < 0) {
  733. printf("Invalid BSSID '%s'\n", argv[3]);
  734. return -1;
  735. }
  736. pos += ETH_ALEN;
  737. }
  738. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  739. if (rlen < 0)
  740. return -1;
  741. return 0;
  742. }
  743. struct wlantest_cli_cmd {
  744. const char *cmd;
  745. int (*handler)(int s, int argc, char *argv[]);
  746. const char *usage;
  747. char ** (*complete)(int s, const char *str, int pos);
  748. };
  749. static const struct wlantest_cli_cmd wlantest_cli_commands[] = {
  750. { "ping", cmd_ping, "= test connection to wlantest", NULL },
  751. { "terminate", cmd_terminate, "= terminate wlantest", NULL },
  752. { "list_bss", cmd_list_bss, "= get BSS list", NULL },
  753. { "list_sta", cmd_list_sta, "<BSSID> = get STA list",
  754. complete_list_sta },
  755. { "flush", cmd_flush, "= drop all collected BSS data", NULL },
  756. { "clear_sta_counters", cmd_clear_sta_counters,
  757. "<BSSID> <STA> = clear STA counters", complete_clear_sta_counters },
  758. { "clear_bss_counters", cmd_clear_bss_counters,
  759. "<BSSID> = clear BSS counters", complete_clear_bss_counters },
  760. { "get_sta_counter", cmd_get_sta_counter,
  761. "<counter> <BSSID> <STA> = get STA counter value",
  762. complete_get_sta_counter},
  763. { "get_bss_counter", cmd_get_bss_counter,
  764. "<counter> <BSSID> = get BSS counter value",
  765. complete_get_bss_counter },
  766. { "inject", cmd_inject,
  767. "<frame> <prot> <sender> <BSSID> <STA/ff:ff:ff:ff:ff:ff>",
  768. complete_inject },
  769. { "version", cmd_version, "= get wlantest version", NULL },
  770. { "add_passphrase", cmd_add_passphrase,
  771. "<passphrase> = add a known passphrase", NULL },
  772. { NULL, NULL, NULL, NULL }
  773. };
  774. static int ctrl_command(int s, int argc, char *argv[])
  775. {
  776. const struct wlantest_cli_cmd *cmd, *match = NULL;
  777. int count = 0;
  778. int ret = 0;
  779. for (cmd = wlantest_cli_commands; cmd->cmd; cmd++) {
  780. if (os_strncasecmp(cmd->cmd, argv[0], os_strlen(argv[0])) == 0)
  781. {
  782. match = cmd;
  783. if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
  784. /* exact match */
  785. count = 1;
  786. break;
  787. }
  788. count++;
  789. }
  790. }
  791. if (count > 1) {
  792. printf("Ambiguous command '%s'; possible commands:", argv[0]);
  793. for (cmd = wlantest_cli_commands; cmd->cmd; cmd++) {
  794. if (os_strncasecmp(cmd->cmd, argv[0],
  795. os_strlen(argv[0])) == 0) {
  796. printf(" %s", cmd->cmd);
  797. }
  798. }
  799. printf("\n");
  800. ret = 1;
  801. } else if (count == 0) {
  802. printf("Unknown command '%s'\n", argv[0]);
  803. ret = 1;
  804. } else {
  805. ret = match->handler(s, argc - 1, &argv[1]);
  806. }
  807. return ret;
  808. }
  809. struct wlantest_cli {
  810. int s;
  811. };
  812. #define max_args 10
  813. static int tokenize_cmd(char *cmd, char *argv[])
  814. {
  815. char *pos;
  816. int argc = 0;
  817. pos = cmd;
  818. for (;;) {
  819. while (*pos == ' ')
  820. pos++;
  821. if (*pos == '\0')
  822. break;
  823. argv[argc] = pos;
  824. argc++;
  825. if (argc == max_args)
  826. break;
  827. if (*pos == '"') {
  828. char *pos2 = os_strrchr(pos, '"');
  829. if (pos2)
  830. pos = pos2 + 1;
  831. }
  832. while (*pos != '\0' && *pos != ' ')
  833. pos++;
  834. if (*pos == ' ')
  835. *pos++ = '\0';
  836. }
  837. return argc;
  838. }
  839. static void wlantest_cli_edit_cmd_cb(void *ctx, char *cmd)
  840. {
  841. struct wlantest_cli *cli = ctx;
  842. char *argv[max_args];
  843. int argc;
  844. argc = tokenize_cmd(cmd, argv);
  845. if (argc) {
  846. int ret = ctrl_command(cli->s, argc, argv);
  847. if (ret < 0)
  848. printf("FAIL\n");
  849. }
  850. }
  851. static void wlantest_cli_eloop_terminate(int sig, void *signal_ctx)
  852. {
  853. eloop_terminate();
  854. }
  855. static void wlantest_cli_edit_eof_cb(void *ctx)
  856. {
  857. eloop_terminate();
  858. }
  859. static char ** wlantest_cli_cmd_list(void)
  860. {
  861. char **res;
  862. int i, count;
  863. count = sizeof(wlantest_cli_commands) /
  864. sizeof(wlantest_cli_commands[0]);
  865. res = os_zalloc(count * sizeof(char *));
  866. if (res == NULL)
  867. return NULL;
  868. for (i = 0; wlantest_cli_commands[i].cmd; i++) {
  869. res[i] = os_strdup(wlantest_cli_commands[i].cmd);
  870. if (res[i] == NULL)
  871. break;
  872. }
  873. return res;
  874. }
  875. static char ** wlantest_cli_cmd_completion(struct wlantest_cli *cli,
  876. const char *cmd, const char *str,
  877. int pos)
  878. {
  879. int i;
  880. for (i = 0; wlantest_cli_commands[i].cmd; i++) {
  881. const struct wlantest_cli_cmd *c = &wlantest_cli_commands[i];
  882. if (os_strcasecmp(c->cmd, cmd) == 0) {
  883. edit_clear_line();
  884. printf("\r%s\n", c->usage);
  885. edit_redraw();
  886. if (c->complete)
  887. return c->complete(cli->s, str, pos);
  888. break;
  889. }
  890. }
  891. return NULL;
  892. }
  893. static char ** wlantest_cli_edit_completion_cb(void *ctx, const char *str,
  894. int pos)
  895. {
  896. struct wlantest_cli *cli = ctx;
  897. char **res;
  898. const char *end;
  899. char *cmd;
  900. end = os_strchr(str, ' ');
  901. if (end == NULL || str + pos < end)
  902. return wlantest_cli_cmd_list();
  903. cmd = os_malloc(pos + 1);
  904. if (cmd == NULL)
  905. return NULL;
  906. os_memcpy(cmd, str, pos);
  907. cmd[end - str] = '\0';
  908. res = wlantest_cli_cmd_completion(cli, cmd, str, pos);
  909. os_free(cmd);
  910. return res;
  911. }
  912. static void wlantest_cli_interactive(int s)
  913. {
  914. struct wlantest_cli cli;
  915. char *home, *hfile = NULL;
  916. if (eloop_init())
  917. return;
  918. home = getenv("HOME");
  919. if (home) {
  920. const char *fname = ".wlantest_cli_history";
  921. int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
  922. hfile = os_malloc(hfile_len);
  923. if (hfile)
  924. os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
  925. }
  926. cli.s = s;
  927. eloop_register_signal_terminate(wlantest_cli_eloop_terminate, &cli);
  928. edit_init(wlantest_cli_edit_cmd_cb, wlantest_cli_edit_eof_cb,
  929. wlantest_cli_edit_completion_cb, &cli, hfile);
  930. eloop_run();
  931. edit_deinit(hfile, NULL);
  932. os_free(hfile);
  933. eloop_destroy();
  934. }
  935. int main(int argc, char *argv[])
  936. {
  937. int s;
  938. struct sockaddr_un addr;
  939. int ret = 0;
  940. if (os_program_init())
  941. return -1;
  942. s = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  943. if (s < 0) {
  944. perror("socket");
  945. return -1;
  946. }
  947. os_memset(&addr, 0, sizeof(addr));
  948. addr.sun_family = AF_UNIX;
  949. os_strlcpy(addr.sun_path + 1, WLANTEST_SOCK_NAME,
  950. sizeof(addr.sun_path) - 1);
  951. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  952. perror("connect");
  953. close(s);
  954. return -1;
  955. }
  956. if (argc > 1) {
  957. ret = ctrl_command(s, argc - 1, &argv[1]);
  958. if (ret < 0)
  959. printf("FAIL\n");
  960. } else {
  961. wlantest_cli_interactive(s);
  962. }
  963. close(s);
  964. os_program_deinit();
  965. return ret;
  966. }