wlantest_cli.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719
  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. static int cmd_clear_tdls_counters(int s, int argc, char *argv[])
  354. {
  355. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  356. u8 buf[100], *pos;
  357. int rlen;
  358. if (argc < 3) {
  359. printf("clear_tdls_counters needs three arguments: BSSID, "
  360. "STA1 address, STA2 address\n");
  361. return -1;
  362. }
  363. pos = buf;
  364. WPA_PUT_BE32(pos, WLANTEST_CTRL_CLEAR_TDLS_COUNTERS);
  365. pos += 4;
  366. WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID);
  367. pos += 4;
  368. WPA_PUT_BE32(pos, ETH_ALEN);
  369. pos += 4;
  370. if (hwaddr_aton(argv[0], pos) < 0) {
  371. printf("Invalid BSSID '%s'\n", argv[0]);
  372. return -1;
  373. }
  374. pos += ETH_ALEN;
  375. WPA_PUT_BE32(pos, WLANTEST_ATTR_STA_ADDR);
  376. pos += 4;
  377. WPA_PUT_BE32(pos, ETH_ALEN);
  378. pos += 4;
  379. if (hwaddr_aton(argv[1], pos) < 0) {
  380. printf("Invalid STA1 address '%s'\n", argv[1]);
  381. return -1;
  382. }
  383. pos += ETH_ALEN;
  384. WPA_PUT_BE32(pos, WLANTEST_ATTR_STA2_ADDR);
  385. pos += 4;
  386. WPA_PUT_BE32(pos, ETH_ALEN);
  387. pos += 4;
  388. if (hwaddr_aton(argv[2], pos) < 0) {
  389. printf("Invalid STA2 address '%s'\n", argv[2]);
  390. return -1;
  391. }
  392. pos += ETH_ALEN;
  393. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  394. if (rlen < 0)
  395. return -1;
  396. printf("OK\n");
  397. return 0;
  398. }
  399. static char ** complete_clear_tdls_counters(int s, const char *str, int pos)
  400. {
  401. int arg = get_cmd_arg_num(str, pos);
  402. char **res = NULL;
  403. u8 addr[ETH_ALEN];
  404. switch (arg) {
  405. case 1:
  406. res = get_bssid_list(s);
  407. break;
  408. case 2:
  409. case 3:
  410. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  411. break;
  412. res = get_sta_list(s, addr, 0);
  413. break;
  414. }
  415. return res;
  416. }
  417. struct sta_counters {
  418. const char *name;
  419. enum wlantest_sta_counter num;
  420. };
  421. static const struct sta_counters sta_counters[] = {
  422. { "auth_tx", WLANTEST_STA_COUNTER_AUTH_TX },
  423. { "auth_rx", WLANTEST_STA_COUNTER_AUTH_RX },
  424. { "assocreq_tx", WLANTEST_STA_COUNTER_ASSOCREQ_TX },
  425. { "reassocreq_tx", WLANTEST_STA_COUNTER_REASSOCREQ_TX },
  426. { "ptk_learned", WLANTEST_STA_COUNTER_PTK_LEARNED },
  427. { "valid_deauth_tx", WLANTEST_STA_COUNTER_VALID_DEAUTH_TX },
  428. { "valid_deauth_rx", WLANTEST_STA_COUNTER_VALID_DEAUTH_RX },
  429. { "invalid_deauth_tx", WLANTEST_STA_COUNTER_INVALID_DEAUTH_TX },
  430. { "invalid_deauth_rx", WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX },
  431. { "valid_disassoc_tx", WLANTEST_STA_COUNTER_VALID_DISASSOC_TX },
  432. { "valid_disassoc_rx", WLANTEST_STA_COUNTER_VALID_DISASSOC_RX },
  433. { "invalid_disassoc_tx", WLANTEST_STA_COUNTER_INVALID_DISASSOC_TX },
  434. { "invalid_disassoc_rx", WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX },
  435. { "valid_saqueryreq_tx", WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_TX },
  436. { "valid_saqueryreq_rx", WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_RX },
  437. { "invalid_saqueryreq_tx",
  438. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_TX },
  439. { "invalid_saqueryreq_rx",
  440. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_RX },
  441. { "valid_saqueryresp_tx", WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_TX },
  442. { "valid_saqueryresp_rx", WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_RX },
  443. { "invalid_saqueryresp_tx",
  444. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_TX },
  445. { "invalid_saqueryresp_rx",
  446. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_RX },
  447. { "ping_ok", WLANTEST_STA_COUNTER_PING_OK },
  448. { "assocresp_comeback", WLANTEST_STA_COUNTER_ASSOCRESP_COMEBACK },
  449. { "reassocresp_comeback", WLANTEST_STA_COUNTER_REASSOCRESP_COMEBACK },
  450. { "ping_ok_first_assoc", WLANTEST_STA_COUNTER_PING_OK_FIRST_ASSOC },
  451. { "valid_deauth_rx_ack", WLANTEST_STA_COUNTER_VALID_DEAUTH_RX_ACK },
  452. { "valid_disassoc_rx_ack",
  453. WLANTEST_STA_COUNTER_VALID_DISASSOC_RX_ACK },
  454. { "invalid_deauth_rx_ack",
  455. WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX_ACK },
  456. { "invalid_disassoc_rx_ack",
  457. WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX_ACK },
  458. { "deauth_rx_asleep", WLANTEST_STA_COUNTER_DEAUTH_RX_ASLEEP },
  459. { "deauth_rx_awake", WLANTEST_STA_COUNTER_DEAUTH_RX_AWAKE },
  460. { "disassoc_rx_asleep", WLANTEST_STA_COUNTER_DISASSOC_RX_ASLEEP },
  461. { "disassoc_rx_awake", WLANTEST_STA_COUNTER_DISASSOC_RX_AWAKE },
  462. { "prot_data_tx", WLANTEST_STA_COUNTER_PROT_DATA_TX },
  463. { "deauth_rx_rc6", WLANTEST_STA_COUNTER_DEAUTH_RX_RC6 },
  464. { "deauth_rx_rc7", WLANTEST_STA_COUNTER_DEAUTH_RX_RC7 },
  465. { "disassoc_rx_rc6", WLANTEST_STA_COUNTER_DISASSOC_RX_RC6 },
  466. { "disassoc_rx_rc7", WLANTEST_STA_COUNTER_DISASSOC_RX_RC7 },
  467. { NULL, 0 }
  468. };
  469. static int cmd_get_sta_counter(int s, int argc, char *argv[])
  470. {
  471. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  472. u8 buf[100], *end, *pos;
  473. int rlen, i;
  474. size_t len;
  475. if (argc != 3) {
  476. printf("get_sta_counter needs at three arguments: "
  477. "counter name, BSSID, and STA address\n");
  478. return -1;
  479. }
  480. pos = buf;
  481. end = buf + sizeof(buf);
  482. WPA_PUT_BE32(pos, WLANTEST_CTRL_GET_STA_COUNTER);
  483. pos += 4;
  484. for (i = 0; sta_counters[i].name; i++) {
  485. if (os_strcasecmp(sta_counters[i].name, argv[0]) == 0)
  486. break;
  487. }
  488. if (sta_counters[i].name == NULL) {
  489. printf("Unknown STA counter '%s'\n", argv[0]);
  490. printf("Counters:");
  491. for (i = 0; sta_counters[i].name; i++)
  492. printf(" %s", sta_counters[i].name);
  493. printf("\n");
  494. return -1;
  495. }
  496. pos = attr_add_be32(pos, end, WLANTEST_ATTR_STA_COUNTER,
  497. sta_counters[i].num);
  498. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  499. if (hwaddr_aton(argv[1], pos) < 0) {
  500. printf("Invalid BSSID '%s'\n", argv[1]);
  501. return -1;
  502. }
  503. pos += ETH_ALEN;
  504. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN);
  505. if (hwaddr_aton(argv[2], pos) < 0) {
  506. printf("Invalid STA address '%s'\n", argv[2]);
  507. return -1;
  508. }
  509. pos += ETH_ALEN;
  510. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  511. if (rlen < 0)
  512. return -1;
  513. pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_COUNTER, &len);
  514. if (pos == NULL || len != 4)
  515. return -1;
  516. printf("%u\n", WPA_GET_BE32(pos));
  517. return 0;
  518. }
  519. static char ** complete_get_sta_counter(int s, const char *str, int pos)
  520. {
  521. int arg = get_cmd_arg_num(str, pos);
  522. char **res = NULL;
  523. int i, count;
  524. u8 addr[ETH_ALEN];
  525. switch (arg) {
  526. case 1:
  527. /* counter list */
  528. count = sizeof(sta_counters) / sizeof(sta_counters[0]);
  529. res = os_zalloc(count * sizeof(char *));
  530. if (res == NULL)
  531. return NULL;
  532. for (i = 0; sta_counters[i].name; i++) {
  533. res[i] = os_strdup(sta_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. case 3:
  542. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  543. break;
  544. res = get_sta_list(s, addr, 0);
  545. break;
  546. }
  547. return res;
  548. }
  549. struct bss_counters {
  550. const char *name;
  551. enum wlantest_bss_counter num;
  552. };
  553. static const struct bss_counters bss_counters[] = {
  554. { "valid_bip_mmie", WLANTEST_BSS_COUNTER_VALID_BIP_MMIE },
  555. { "invalid_bip_mmie", WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE },
  556. { "missing_bip_mmie", WLANTEST_BSS_COUNTER_MISSING_BIP_MMIE },
  557. { "bip_deauth", WLANTEST_BSS_COUNTER_BIP_DEAUTH },
  558. { "bip_disassoc", WLANTEST_BSS_COUNTER_BIP_DISASSOC },
  559. { NULL, 0 }
  560. };
  561. static int cmd_get_bss_counter(int s, int argc, char *argv[])
  562. {
  563. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  564. u8 buf[100], *end, *pos;
  565. int rlen, i;
  566. size_t len;
  567. if (argc != 2) {
  568. printf("get_bss_counter needs at two arguments: "
  569. "counter name and BSSID\n");
  570. return -1;
  571. }
  572. pos = buf;
  573. end = buf + sizeof(buf);
  574. WPA_PUT_BE32(pos, WLANTEST_CTRL_GET_BSS_COUNTER);
  575. pos += 4;
  576. for (i = 0; bss_counters[i].name; i++) {
  577. if (os_strcasecmp(bss_counters[i].name, argv[0]) == 0)
  578. break;
  579. }
  580. if (bss_counters[i].name == NULL) {
  581. printf("Unknown BSS counter '%s'\n", argv[0]);
  582. printf("Counters:");
  583. for (i = 0; bss_counters[i].name; i++)
  584. printf(" %s", bss_counters[i].name);
  585. printf("\n");
  586. return -1;
  587. }
  588. pos = attr_add_be32(pos, end, WLANTEST_ATTR_BSS_COUNTER,
  589. bss_counters[i].num);
  590. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  591. if (hwaddr_aton(argv[1], pos) < 0) {
  592. printf("Invalid BSSID '%s'\n", argv[1]);
  593. return -1;
  594. }
  595. pos += ETH_ALEN;
  596. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  597. if (rlen < 0)
  598. return -1;
  599. pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_COUNTER, &len);
  600. if (pos == NULL || len != 4)
  601. return -1;
  602. printf("%u\n", WPA_GET_BE32(pos));
  603. return 0;
  604. }
  605. static char ** complete_get_bss_counter(int s, const char *str, int pos)
  606. {
  607. int arg = get_cmd_arg_num(str, pos);
  608. char **res = NULL;
  609. int i, count;
  610. switch (arg) {
  611. case 1:
  612. /* counter list */
  613. count = sizeof(bss_counters) / sizeof(bss_counters[0]);
  614. res = os_zalloc(count * sizeof(char *));
  615. if (res == NULL)
  616. return NULL;
  617. for (i = 0; bss_counters[i].name; i++) {
  618. res[i] = os_strdup(bss_counters[i].name);
  619. if (res[i] == NULL)
  620. break;
  621. }
  622. break;
  623. case 2:
  624. res = get_bssid_list(s);
  625. break;
  626. }
  627. return res;
  628. }
  629. struct tdls_counters {
  630. const char *name;
  631. enum wlantest_tdls_counter num;
  632. };
  633. static const struct tdls_counters tdls_counters[] = {
  634. { "valid_direct_link", WLANTEST_TDLS_COUNTER_VALID_DIRECT_LINK },
  635. { "invalid_direct_link", WLANTEST_TDLS_COUNTER_INVALID_DIRECT_LINK },
  636. { "valid_ap_path", WLANTEST_TDLS_COUNTER_VALID_AP_PATH },
  637. { "invalid_ap_path", WLANTEST_TDLS_COUNTER_INVALID_AP_PATH },
  638. { "setup_req", WLANTEST_TDLS_COUNTER_SETUP_REQ },
  639. { "setup_resp_ok", WLANTEST_TDLS_COUNTER_SETUP_RESP_OK },
  640. { "setup_resp_fail", WLANTEST_TDLS_COUNTER_SETUP_RESP_FAIL },
  641. { "setup_conf_ok", WLANTEST_TDLS_COUNTER_SETUP_CONF_OK },
  642. { "setup_conf_fail", WLANTEST_TDLS_COUNTER_SETUP_CONF_FAIL },
  643. { "teardown", WLANTEST_TDLS_COUNTER_TEARDOWN },
  644. { NULL, 0 }
  645. };
  646. static int cmd_get_tdls_counter(int s, int argc, char *argv[])
  647. {
  648. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  649. u8 buf[100], *end, *pos;
  650. int rlen, i;
  651. size_t len;
  652. if (argc != 4) {
  653. printf("get_tdls_counter needs four arguments: "
  654. "counter name, BSSID, STA1 address, STA2 address\n");
  655. return -1;
  656. }
  657. pos = buf;
  658. end = buf + sizeof(buf);
  659. WPA_PUT_BE32(pos, WLANTEST_CTRL_GET_TDLS_COUNTER);
  660. pos += 4;
  661. for (i = 0; tdls_counters[i].name; i++) {
  662. if (os_strcasecmp(tdls_counters[i].name, argv[0]) == 0)
  663. break;
  664. }
  665. if (tdls_counters[i].name == NULL) {
  666. printf("Unknown TDLS counter '%s'\n", argv[0]);
  667. printf("Counters:");
  668. for (i = 0; tdls_counters[i].name; i++)
  669. printf(" %s", tdls_counters[i].name);
  670. printf("\n");
  671. return -1;
  672. }
  673. pos = attr_add_be32(pos, end, WLANTEST_ATTR_TDLS_COUNTER,
  674. tdls_counters[i].num);
  675. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  676. if (hwaddr_aton(argv[1], pos) < 0) {
  677. printf("Invalid BSSID '%s'\n", argv[1]);
  678. return -1;
  679. }
  680. pos += ETH_ALEN;
  681. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN);
  682. if (hwaddr_aton(argv[2], pos) < 0) {
  683. printf("Invalid STA1 address '%s'\n", argv[2]);
  684. return -1;
  685. }
  686. pos += ETH_ALEN;
  687. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA2_ADDR, ETH_ALEN);
  688. if (hwaddr_aton(argv[3], pos) < 0) {
  689. printf("Invalid STA2 address '%s'\n", argv[3]);
  690. return -1;
  691. }
  692. pos += ETH_ALEN;
  693. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  694. if (rlen < 0)
  695. return -1;
  696. pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_COUNTER, &len);
  697. if (pos == NULL || len != 4)
  698. return -1;
  699. printf("%u\n", WPA_GET_BE32(pos));
  700. return 0;
  701. }
  702. static char ** complete_get_tdls_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. u8 addr[ETH_ALEN];
  708. switch (arg) {
  709. case 1:
  710. /* counter list */
  711. count = sizeof(tdls_counters) / sizeof(tdls_counters[0]);
  712. res = os_zalloc(count * sizeof(char *));
  713. if (res == NULL)
  714. return NULL;
  715. for (i = 0; tdls_counters[i].name; i++) {
  716. res[i] = os_strdup(tdls_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. case 3:
  725. case 4:
  726. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  727. break;
  728. res = get_sta_list(s, addr, 0);
  729. break;
  730. }
  731. return res;
  732. }
  733. struct inject_frames {
  734. const char *name;
  735. enum wlantest_inject_frame frame;
  736. };
  737. static const struct inject_frames inject_frames[] = {
  738. { "auth", WLANTEST_FRAME_AUTH },
  739. { "assocreq", WLANTEST_FRAME_ASSOCREQ },
  740. { "reassocreq", WLANTEST_FRAME_REASSOCREQ },
  741. { "deauth", WLANTEST_FRAME_DEAUTH },
  742. { "disassoc", WLANTEST_FRAME_DISASSOC },
  743. { "saqueryreq", WLANTEST_FRAME_SAQUERYREQ },
  744. { NULL, 0 }
  745. };
  746. static int cmd_inject(int s, int argc, char *argv[])
  747. {
  748. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  749. u8 buf[100], *end, *pos;
  750. int rlen, i;
  751. enum wlantest_inject_protection prot;
  752. /* <frame> <prot> <sender> <BSSID> <STA/ff:ff:ff:ff:ff:ff> */
  753. if (argc < 5) {
  754. printf("inject needs five arguments: frame, protection, "
  755. "sender, BSSID, STA/ff:ff:ff:ff:ff:ff\n");
  756. return -1;
  757. }
  758. pos = buf;
  759. end = buf + sizeof(buf);
  760. WPA_PUT_BE32(pos, WLANTEST_CTRL_INJECT);
  761. pos += 4;
  762. for (i = 0; inject_frames[i].name; i++) {
  763. if (os_strcasecmp(inject_frames[i].name, argv[0]) == 0)
  764. break;
  765. }
  766. if (inject_frames[i].name == NULL) {
  767. printf("Unknown inject frame '%s'\n", argv[0]);
  768. printf("Frames:");
  769. for (i = 0; inject_frames[i].name; i++)
  770. printf(" %s", inject_frames[i].name);
  771. printf("\n");
  772. return -1;
  773. }
  774. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_FRAME,
  775. inject_frames[i].frame);
  776. if (os_strcasecmp(argv[1], "normal") == 0)
  777. prot = WLANTEST_INJECT_NORMAL;
  778. else if (os_strcasecmp(argv[1], "protected") == 0)
  779. prot = WLANTEST_INJECT_PROTECTED;
  780. else if (os_strcasecmp(argv[1], "unprotected") == 0)
  781. prot = WLANTEST_INJECT_UNPROTECTED;
  782. else if (os_strcasecmp(argv[1], "incorrect") == 0)
  783. prot = WLANTEST_INJECT_INCORRECT_KEY;
  784. else {
  785. printf("Unknown protection type '%s'\n", argv[1]);
  786. printf("Protection types: normal protected unprotected "
  787. "incorrect\n");
  788. return -1;
  789. }
  790. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_PROTECTION, prot);
  791. if (os_strcasecmp(argv[2], "ap") == 0) {
  792. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_SENDER_AP,
  793. 1);
  794. } else if (os_strcasecmp(argv[2], "sta") == 0) {
  795. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_SENDER_AP,
  796. 0);
  797. } else {
  798. printf("Unknown sender '%s'\n", argv[2]);
  799. printf("Sender types: ap sta\n");
  800. return -1;
  801. }
  802. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  803. if (hwaddr_aton(argv[3], pos) < 0) {
  804. printf("Invalid BSSID '%s'\n", argv[3]);
  805. return -1;
  806. }
  807. pos += ETH_ALEN;
  808. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN);
  809. if (hwaddr_aton(argv[4], pos) < 0) {
  810. printf("Invalid STA '%s'\n", argv[4]);
  811. return -1;
  812. }
  813. pos += ETH_ALEN;
  814. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  815. if (rlen < 0)
  816. return -1;
  817. printf("OK\n");
  818. return 0;
  819. }
  820. static char ** complete_inject(int s, const char *str, int pos)
  821. {
  822. int arg = get_cmd_arg_num(str, pos);
  823. char **res = NULL;
  824. int i, count;
  825. u8 addr[ETH_ALEN];
  826. switch (arg) {
  827. case 1:
  828. /* frame list */
  829. count = sizeof(inject_frames) / sizeof(inject_frames[0]);
  830. res = os_zalloc(count * sizeof(char *));
  831. if (res == NULL)
  832. break;
  833. for (i = 0; inject_frames[i].name; i++) {
  834. res[i] = os_strdup(inject_frames[i].name);
  835. if (res[i] == NULL)
  836. break;
  837. }
  838. break;
  839. case 2:
  840. res = os_zalloc(5 * sizeof(char *));
  841. if (res == NULL)
  842. break;
  843. res[0] = os_strdup("normal");
  844. if (res[0] == NULL)
  845. break;
  846. res[1] = os_strdup("protected");
  847. if (res[1] == NULL)
  848. break;
  849. res[2] = os_strdup("unprotected");
  850. if (res[2] == NULL)
  851. break;
  852. res[3] = os_strdup("incorrect");
  853. if (res[3] == NULL)
  854. break;
  855. break;
  856. case 3:
  857. res = os_zalloc(3 * sizeof(char *));
  858. if (res == NULL)
  859. break;
  860. res[0] = os_strdup("ap");
  861. if (res[0] == NULL)
  862. break;
  863. res[1] = os_strdup("sta");
  864. if (res[1] == NULL)
  865. break;
  866. break;
  867. case 4:
  868. res = get_bssid_list(s);
  869. break;
  870. case 5:
  871. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  872. break;
  873. res = get_sta_list(s, addr, 1);
  874. break;
  875. }
  876. return res;
  877. }
  878. static u8 * add_hex(u8 *pos, u8 *end, const char *str)
  879. {
  880. const char *s;
  881. int val;
  882. s = str;
  883. while (*s) {
  884. while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n' ||
  885. *s == ':')
  886. s++;
  887. if (*s == '\0')
  888. break;
  889. if (*s == '#') {
  890. while (*s != '\0' && *s != '\r' && *s != '\n')
  891. s++;
  892. continue;
  893. }
  894. val = hex2byte(s);
  895. if (val < 0) {
  896. printf("Invalid hex encoding '%s'\n", s);
  897. return NULL;
  898. }
  899. if (pos == end) {
  900. printf("Too long frame\n");
  901. return NULL;
  902. }
  903. *pos++ = val;
  904. s += 2;
  905. }
  906. return pos;
  907. }
  908. static int cmd_send(int s, int argc, char *argv[])
  909. {
  910. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  911. u8 buf[WLANTEST_CTRL_MAX_CMD_LEN], *end, *pos, *len_pos;
  912. int rlen;
  913. enum wlantest_inject_protection prot;
  914. int arg;
  915. /* <prot> <raw frame as hex dump> */
  916. if (argc < 2) {
  917. printf("send needs two arguments: protected/unprotected, "
  918. "raw frame as hex dump\n");
  919. return -1;
  920. }
  921. pos = buf;
  922. end = buf + sizeof(buf);
  923. WPA_PUT_BE32(pos, WLANTEST_CTRL_SEND);
  924. pos += 4;
  925. if (os_strcasecmp(argv[0], "normal") == 0)
  926. prot = WLANTEST_INJECT_NORMAL;
  927. else if (os_strcasecmp(argv[0], "protected") == 0)
  928. prot = WLANTEST_INJECT_PROTECTED;
  929. else if (os_strcasecmp(argv[0], "unprotected") == 0)
  930. prot = WLANTEST_INJECT_UNPROTECTED;
  931. else if (os_strcasecmp(argv[0], "incorrect") == 0)
  932. prot = WLANTEST_INJECT_INCORRECT_KEY;
  933. else {
  934. printf("Unknown protection type '%s'\n", argv[1]);
  935. printf("Protection types: normal protected unprotected "
  936. "incorrect\n");
  937. return -1;
  938. }
  939. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_PROTECTION, prot);
  940. WPA_PUT_BE32(pos, WLANTEST_ATTR_FRAME);
  941. pos += 4;
  942. len_pos = pos;
  943. pos += 4;
  944. for (arg = 1; pos && arg < argc; arg++)
  945. pos = add_hex(pos, end, argv[arg]);
  946. if (pos == NULL)
  947. return -1;
  948. WPA_PUT_BE32(len_pos, pos - len_pos - 4);
  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. static char ** complete_send(int s, const char *str, int pos)
  956. {
  957. int arg = get_cmd_arg_num(str, pos);
  958. char **res = NULL;
  959. switch (arg) {
  960. case 1:
  961. res = os_zalloc(5 * sizeof(char *));
  962. if (res == NULL)
  963. break;
  964. res[0] = os_strdup("normal");
  965. if (res[0] == NULL)
  966. break;
  967. res[1] = os_strdup("protected");
  968. if (res[1] == NULL)
  969. break;
  970. res[2] = os_strdup("unprotected");
  971. if (res[2] == NULL)
  972. break;
  973. res[3] = os_strdup("incorrect");
  974. if (res[3] == NULL)
  975. break;
  976. break;
  977. }
  978. return res;
  979. }
  980. static int cmd_version(int s, int argc, char *argv[])
  981. {
  982. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  983. u8 buf[4];
  984. char *version;
  985. size_t len;
  986. int rlen, i;
  987. WPA_PUT_BE32(buf, WLANTEST_CTRL_VERSION);
  988. rlen = cmd_send_and_recv(s, buf, sizeof(buf), resp, sizeof(resp));
  989. if (rlen < 0)
  990. return -1;
  991. version = (char *) attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_VERSION,
  992. &len);
  993. if (version == NULL)
  994. return -1;
  995. for (i = 0; i < len; i++)
  996. putchar(version[i]);
  997. printf("\n");
  998. return 0;
  999. }
  1000. static int cmd_add_passphrase(int s, int argc, char *argv[])
  1001. {
  1002. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  1003. u8 buf[100], *pos, *end;
  1004. size_t len;
  1005. int rlen;
  1006. if (argc < 1) {
  1007. printf("add_passphrase needs one argument: passphrase\n");
  1008. return -1;
  1009. }
  1010. len = os_strlen(argv[0]);
  1011. if (len < 8 || len > 63) {
  1012. printf("Invalid passphrase '%s'\n", argv[0]);
  1013. return -1;
  1014. }
  1015. pos = buf;
  1016. end = buf + sizeof(buf);
  1017. WPA_PUT_BE32(pos, WLANTEST_CTRL_ADD_PASSPHRASE);
  1018. pos += 4;
  1019. pos = attr_add_str(pos, end, WLANTEST_ATTR_PASSPHRASE,
  1020. argv[0]);
  1021. if (argc > 1) {
  1022. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  1023. if (hwaddr_aton(argv[1], pos) < 0) {
  1024. printf("Invalid BSSID '%s'\n", argv[3]);
  1025. return -1;
  1026. }
  1027. pos += ETH_ALEN;
  1028. }
  1029. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  1030. if (rlen < 0)
  1031. return -1;
  1032. return 0;
  1033. }
  1034. struct sta_infos {
  1035. const char *name;
  1036. enum wlantest_sta_info num;
  1037. };
  1038. static const struct sta_infos sta_infos[] = {
  1039. { "proto", WLANTEST_STA_INFO_PROTO },
  1040. { "pairwise", WLANTEST_STA_INFO_PAIRWISE },
  1041. { "key_mgmt", WLANTEST_STA_INFO_KEY_MGMT },
  1042. { "rsn_capab", WLANTEST_STA_INFO_RSN_CAPAB },
  1043. { "state", WLANTEST_STA_INFO_STATE },
  1044. { NULL, 0 }
  1045. };
  1046. static int cmd_info_sta(int s, int argc, char *argv[])
  1047. {
  1048. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  1049. u8 buf[100], *end, *pos;
  1050. int rlen, i;
  1051. size_t len;
  1052. char info[100];
  1053. if (argc != 3) {
  1054. printf("sta_info needs at three arguments: "
  1055. "counter name, BSSID, and STA address\n");
  1056. return -1;
  1057. }
  1058. pos = buf;
  1059. end = buf + sizeof(buf);
  1060. WPA_PUT_BE32(pos, WLANTEST_CTRL_INFO_STA);
  1061. pos += 4;
  1062. for (i = 0; sta_infos[i].name; i++) {
  1063. if (os_strcasecmp(sta_infos[i].name, argv[0]) == 0)
  1064. break;
  1065. }
  1066. if (sta_infos[i].name == NULL) {
  1067. printf("Unknown STA info '%s'\n", argv[0]);
  1068. printf("Info fields:");
  1069. for (i = 0; sta_infos[i].name; i++)
  1070. printf(" %s", sta_infos[i].name);
  1071. printf("\n");
  1072. return -1;
  1073. }
  1074. pos = attr_add_be32(pos, end, WLANTEST_ATTR_STA_INFO,
  1075. sta_infos[i].num);
  1076. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  1077. if (hwaddr_aton(argv[1], pos) < 0) {
  1078. printf("Invalid BSSID '%s'\n", argv[1]);
  1079. return -1;
  1080. }
  1081. pos += ETH_ALEN;
  1082. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN);
  1083. if (hwaddr_aton(argv[2], pos) < 0) {
  1084. printf("Invalid STA address '%s'\n", argv[2]);
  1085. return -1;
  1086. }
  1087. pos += ETH_ALEN;
  1088. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  1089. if (rlen < 0)
  1090. return -1;
  1091. pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_INFO, &len);
  1092. if (pos == NULL)
  1093. return -1;
  1094. if (len >= sizeof(info))
  1095. len = sizeof(info) - 1;
  1096. os_memcpy(info, pos, len);
  1097. info[len] = '\0';
  1098. printf("%s\n", info);
  1099. return 0;
  1100. }
  1101. static char ** complete_info_sta(int s, const char *str, int pos)
  1102. {
  1103. int arg = get_cmd_arg_num(str, pos);
  1104. char **res = NULL;
  1105. int i, count;
  1106. u8 addr[ETH_ALEN];
  1107. switch (arg) {
  1108. case 1:
  1109. /* counter list */
  1110. count = sizeof(sta_infos) / sizeof(sta_infos[0]);
  1111. res = os_zalloc(count * sizeof(char *));
  1112. if (res == NULL)
  1113. return NULL;
  1114. for (i = 0; sta_infos[i].name; i++) {
  1115. res[i] = os_strdup(sta_infos[i].name);
  1116. if (res[i] == NULL)
  1117. break;
  1118. }
  1119. break;
  1120. case 2:
  1121. res = get_bssid_list(s);
  1122. break;
  1123. case 3:
  1124. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  1125. break;
  1126. res = get_sta_list(s, addr, 0);
  1127. break;
  1128. }
  1129. return res;
  1130. }
  1131. struct bss_infos {
  1132. const char *name;
  1133. enum wlantest_bss_info num;
  1134. };
  1135. static const struct bss_infos bss_infos[] = {
  1136. { "proto", WLANTEST_BSS_INFO_PROTO },
  1137. { "pairwise", WLANTEST_BSS_INFO_PAIRWISE },
  1138. { "group", WLANTEST_BSS_INFO_GROUP },
  1139. { "group_mgmt", WLANTEST_BSS_INFO_GROUP_MGMT },
  1140. { "key_mgmt", WLANTEST_BSS_INFO_KEY_MGMT },
  1141. { "rsn_capab", WLANTEST_BSS_INFO_RSN_CAPAB },
  1142. { NULL, 0 }
  1143. };
  1144. static int cmd_info_bss(int s, int argc, char *argv[])
  1145. {
  1146. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  1147. u8 buf[100], *end, *pos;
  1148. int rlen, i;
  1149. size_t len;
  1150. char info[100];
  1151. if (argc != 2) {
  1152. printf("bss_info needs at two arguments: "
  1153. "field name and BSSID\n");
  1154. return -1;
  1155. }
  1156. pos = buf;
  1157. end = buf + sizeof(buf);
  1158. WPA_PUT_BE32(pos, WLANTEST_CTRL_INFO_BSS);
  1159. pos += 4;
  1160. for (i = 0; bss_infos[i].name; i++) {
  1161. if (os_strcasecmp(bss_infos[i].name, argv[0]) == 0)
  1162. break;
  1163. }
  1164. if (bss_infos[i].name == NULL) {
  1165. printf("Unknown BSS info '%s'\n", argv[0]);
  1166. printf("Info fields:");
  1167. for (i = 0; bss_infos[i].name; i++)
  1168. printf(" %s", bss_infos[i].name);
  1169. printf("\n");
  1170. return -1;
  1171. }
  1172. pos = attr_add_be32(pos, end, WLANTEST_ATTR_BSS_INFO,
  1173. bss_infos[i].num);
  1174. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  1175. if (hwaddr_aton(argv[1], pos) < 0) {
  1176. printf("Invalid BSSID '%s'\n", argv[1]);
  1177. return -1;
  1178. }
  1179. pos += ETH_ALEN;
  1180. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  1181. if (rlen < 0)
  1182. return -1;
  1183. pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_INFO, &len);
  1184. if (pos == NULL)
  1185. return -1;
  1186. if (len >= sizeof(info))
  1187. len = sizeof(info) - 1;
  1188. os_memcpy(info, pos, len);
  1189. info[len] = '\0';
  1190. printf("%s\n", info);
  1191. return 0;
  1192. }
  1193. static char ** complete_info_bss(int s, const char *str, int pos)
  1194. {
  1195. int arg = get_cmd_arg_num(str, pos);
  1196. char **res = NULL;
  1197. int i, count;
  1198. switch (arg) {
  1199. case 1:
  1200. /* counter list */
  1201. count = sizeof(bss_infos) / sizeof(bss_infos[0]);
  1202. res = os_zalloc(count * sizeof(char *));
  1203. if (res == NULL)
  1204. return NULL;
  1205. for (i = 0; bss_infos[i].name; i++) {
  1206. res[i] = os_strdup(bss_infos[i].name);
  1207. if (res[i] == NULL)
  1208. break;
  1209. }
  1210. break;
  1211. case 2:
  1212. res = get_bssid_list(s);
  1213. break;
  1214. }
  1215. return res;
  1216. }
  1217. struct wlantest_cli_cmd {
  1218. const char *cmd;
  1219. int (*handler)(int s, int argc, char *argv[]);
  1220. const char *usage;
  1221. char ** (*complete)(int s, const char *str, int pos);
  1222. };
  1223. static const struct wlantest_cli_cmd wlantest_cli_commands[] = {
  1224. { "ping", cmd_ping, "= test connection to wlantest", NULL },
  1225. { "terminate", cmd_terminate, "= terminate wlantest", NULL },
  1226. { "list_bss", cmd_list_bss, "= get BSS list", NULL },
  1227. { "list_sta", cmd_list_sta, "<BSSID> = get STA list",
  1228. complete_list_sta },
  1229. { "flush", cmd_flush, "= drop all collected BSS data", NULL },
  1230. { "clear_sta_counters", cmd_clear_sta_counters,
  1231. "<BSSID> <STA> = clear STA counters", complete_clear_sta_counters },
  1232. { "clear_bss_counters", cmd_clear_bss_counters,
  1233. "<BSSID> = clear BSS counters", complete_clear_bss_counters },
  1234. { "get_sta_counter", cmd_get_sta_counter,
  1235. "<counter> <BSSID> <STA> = get STA counter value",
  1236. complete_get_sta_counter },
  1237. { "get_bss_counter", cmd_get_bss_counter,
  1238. "<counter> <BSSID> = get BSS counter value",
  1239. complete_get_bss_counter },
  1240. { "inject", cmd_inject,
  1241. "<frame> <prot> <sender> <BSSID> <STA/ff:ff:ff:ff:ff:ff>",
  1242. complete_inject },
  1243. { "send", cmd_send,
  1244. "<prot> <raw frame as hex dump>",
  1245. complete_send },
  1246. { "version", cmd_version, "= get wlantest version", NULL },
  1247. { "add_passphrase", cmd_add_passphrase,
  1248. "<passphrase> = add a known passphrase", NULL },
  1249. { "info_sta", cmd_info_sta,
  1250. "<field> <BSSID> <STA> = get STA information",
  1251. complete_info_sta },
  1252. { "info_bss", cmd_info_bss,
  1253. "<field> <BSSID> = get BSS information",
  1254. complete_info_bss },
  1255. { "clear_tdls_counters", cmd_clear_tdls_counters,
  1256. "<BSSID> <STA1> <STA2> = clear TDLS counters",
  1257. complete_clear_tdls_counters },
  1258. { "get_tdls_counter", cmd_get_tdls_counter,
  1259. "<counter> <BSSID> <STA1> <STA2> = get TDLS counter value",
  1260. complete_get_tdls_counter },
  1261. { "get_bss_counter", cmd_get_bss_counter,
  1262. "<counter> <BSSID> = get BSS counter value",
  1263. complete_get_bss_counter },
  1264. { NULL, NULL, NULL, NULL }
  1265. };
  1266. static int ctrl_command(int s, int argc, char *argv[])
  1267. {
  1268. const struct wlantest_cli_cmd *cmd, *match = NULL;
  1269. int count = 0;
  1270. int ret = 0;
  1271. for (cmd = wlantest_cli_commands; cmd->cmd; cmd++) {
  1272. if (os_strncasecmp(cmd->cmd, argv[0], os_strlen(argv[0])) == 0)
  1273. {
  1274. match = cmd;
  1275. if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
  1276. /* exact match */
  1277. count = 1;
  1278. break;
  1279. }
  1280. count++;
  1281. }
  1282. }
  1283. if (count > 1) {
  1284. printf("Ambiguous command '%s'; possible commands:", argv[0]);
  1285. for (cmd = wlantest_cli_commands; cmd->cmd; cmd++) {
  1286. if (os_strncasecmp(cmd->cmd, argv[0],
  1287. os_strlen(argv[0])) == 0) {
  1288. printf(" %s", cmd->cmd);
  1289. }
  1290. }
  1291. printf("\n");
  1292. ret = 1;
  1293. } else if (count == 0) {
  1294. printf("Unknown command '%s'\n", argv[0]);
  1295. ret = 1;
  1296. } else {
  1297. ret = match->handler(s, argc - 1, &argv[1]);
  1298. }
  1299. return ret;
  1300. }
  1301. struct wlantest_cli {
  1302. int s;
  1303. };
  1304. #define max_args 10
  1305. static int tokenize_cmd(char *cmd, char *argv[])
  1306. {
  1307. char *pos;
  1308. int argc = 0;
  1309. pos = cmd;
  1310. for (;;) {
  1311. while (*pos == ' ')
  1312. pos++;
  1313. if (*pos == '\0')
  1314. break;
  1315. argv[argc] = pos;
  1316. argc++;
  1317. if (argc == max_args)
  1318. break;
  1319. if (*pos == '"') {
  1320. char *pos2 = os_strrchr(pos, '"');
  1321. if (pos2)
  1322. pos = pos2 + 1;
  1323. }
  1324. while (*pos != '\0' && *pos != ' ')
  1325. pos++;
  1326. if (*pos == ' ')
  1327. *pos++ = '\0';
  1328. }
  1329. return argc;
  1330. }
  1331. static void wlantest_cli_edit_cmd_cb(void *ctx, char *cmd)
  1332. {
  1333. struct wlantest_cli *cli = ctx;
  1334. char *argv[max_args];
  1335. int argc;
  1336. argc = tokenize_cmd(cmd, argv);
  1337. if (argc) {
  1338. int ret = ctrl_command(cli->s, argc, argv);
  1339. if (ret < 0)
  1340. printf("FAIL\n");
  1341. }
  1342. }
  1343. static void wlantest_cli_eloop_terminate(int sig, void *signal_ctx)
  1344. {
  1345. eloop_terminate();
  1346. }
  1347. static void wlantest_cli_edit_eof_cb(void *ctx)
  1348. {
  1349. eloop_terminate();
  1350. }
  1351. static char ** wlantest_cli_cmd_list(void)
  1352. {
  1353. char **res;
  1354. int i, count;
  1355. count = sizeof(wlantest_cli_commands) /
  1356. sizeof(wlantest_cli_commands[0]);
  1357. res = os_zalloc(count * sizeof(char *));
  1358. if (res == NULL)
  1359. return NULL;
  1360. for (i = 0; wlantest_cli_commands[i].cmd; i++) {
  1361. res[i] = os_strdup(wlantest_cli_commands[i].cmd);
  1362. if (res[i] == NULL)
  1363. break;
  1364. }
  1365. return res;
  1366. }
  1367. static char ** wlantest_cli_cmd_completion(struct wlantest_cli *cli,
  1368. const char *cmd, const char *str,
  1369. int pos)
  1370. {
  1371. int i;
  1372. for (i = 0; wlantest_cli_commands[i].cmd; i++) {
  1373. const struct wlantest_cli_cmd *c = &wlantest_cli_commands[i];
  1374. if (os_strcasecmp(c->cmd, cmd) == 0) {
  1375. edit_clear_line();
  1376. printf("\r%s\n", c->usage);
  1377. edit_redraw();
  1378. if (c->complete)
  1379. return c->complete(cli->s, str, pos);
  1380. break;
  1381. }
  1382. }
  1383. return NULL;
  1384. }
  1385. static char ** wlantest_cli_edit_completion_cb(void *ctx, const char *str,
  1386. int pos)
  1387. {
  1388. struct wlantest_cli *cli = ctx;
  1389. char **res;
  1390. const char *end;
  1391. char *cmd;
  1392. end = os_strchr(str, ' ');
  1393. if (end == NULL || str + pos < end)
  1394. return wlantest_cli_cmd_list();
  1395. cmd = os_malloc(pos + 1);
  1396. if (cmd == NULL)
  1397. return NULL;
  1398. os_memcpy(cmd, str, pos);
  1399. cmd[end - str] = '\0';
  1400. res = wlantest_cli_cmd_completion(cli, cmd, str, pos);
  1401. os_free(cmd);
  1402. return res;
  1403. }
  1404. static void wlantest_cli_interactive(int s)
  1405. {
  1406. struct wlantest_cli cli;
  1407. char *home, *hfile = NULL;
  1408. if (eloop_init())
  1409. return;
  1410. home = getenv("HOME");
  1411. if (home) {
  1412. const char *fname = ".wlantest_cli_history";
  1413. int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
  1414. hfile = os_malloc(hfile_len);
  1415. if (hfile)
  1416. os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
  1417. }
  1418. cli.s = s;
  1419. eloop_register_signal_terminate(wlantest_cli_eloop_terminate, &cli);
  1420. edit_init(wlantest_cli_edit_cmd_cb, wlantest_cli_edit_eof_cb,
  1421. wlantest_cli_edit_completion_cb, &cli, hfile);
  1422. eloop_run();
  1423. edit_deinit(hfile, NULL);
  1424. os_free(hfile);
  1425. eloop_destroy();
  1426. }
  1427. int main(int argc, char *argv[])
  1428. {
  1429. int s;
  1430. struct sockaddr_un addr;
  1431. int ret = 0;
  1432. if (os_program_init())
  1433. return -1;
  1434. s = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  1435. if (s < 0) {
  1436. perror("socket");
  1437. return -1;
  1438. }
  1439. os_memset(&addr, 0, sizeof(addr));
  1440. addr.sun_family = AF_UNIX;
  1441. os_strlcpy(addr.sun_path + 1, WLANTEST_SOCK_NAME,
  1442. sizeof(addr.sun_path) - 1);
  1443. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1444. perror("connect");
  1445. close(s);
  1446. return -1;
  1447. }
  1448. if (argc > 1) {
  1449. ret = ctrl_command(s, argc - 1, &argv[1]);
  1450. if (ret < 0)
  1451. printf("FAIL\n");
  1452. } else {
  1453. wlantest_cli_interactive(s);
  1454. }
  1455. close(s);
  1456. os_program_deinit();
  1457. return ret;
  1458. }