wlantest_cli.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  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. #include "utils/includes.h"
  9. #include <sys/un.h>
  10. #include "utils/common.h"
  11. #include "utils/eloop.h"
  12. #include "utils/edit.h"
  13. #include "wlantest_ctrl.h"
  14. static int get_cmd_arg_num(const char *str, int pos)
  15. {
  16. int arg = 0, i;
  17. for (i = 0; i <= pos; i++) {
  18. if (str[i] != ' ') {
  19. arg++;
  20. while (i <= pos && str[i] != ' ')
  21. i++;
  22. }
  23. }
  24. if (arg > 0)
  25. arg--;
  26. return arg;
  27. }
  28. static int get_prev_arg_pos(const char *str, int pos)
  29. {
  30. while (pos > 0 && str[pos - 1] != ' ')
  31. pos--;
  32. while (pos > 0 && str[pos - 1] == ' ')
  33. pos--;
  34. while (pos > 0 && str[pos - 1] != ' ')
  35. pos--;
  36. return pos;
  37. }
  38. static u8 * attr_get(u8 *buf, size_t buflen, enum wlantest_ctrl_attr attr,
  39. size_t *len)
  40. {
  41. u8 *pos = buf;
  42. while (pos + 8 <= buf + buflen) {
  43. enum wlantest_ctrl_attr a;
  44. size_t alen;
  45. a = WPA_GET_BE32(pos);
  46. pos += 4;
  47. alen = WPA_GET_BE32(pos);
  48. pos += 4;
  49. if (pos + alen > buf + buflen) {
  50. printf("Invalid control message attribute\n");
  51. return NULL;
  52. }
  53. if (a == attr) {
  54. *len = alen;
  55. return pos;
  56. }
  57. pos += alen;
  58. }
  59. return NULL;
  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. static u8 * attr_add_str(u8 *pos, u8 *end, enum wlantest_ctrl_attr attr,
  73. const char *str)
  74. {
  75. size_t len = os_strlen(str);
  76. if (pos == NULL || end - pos < 8 + len)
  77. return NULL;
  78. WPA_PUT_BE32(pos, attr);
  79. pos += 4;
  80. WPA_PUT_BE32(pos, len);
  81. pos += 4;
  82. os_memcpy(pos, str, len);
  83. pos += len;
  84. return pos;
  85. }
  86. static u8 * attr_add_be32(u8 *pos, u8 *end, enum wlantest_ctrl_attr attr,
  87. u32 val)
  88. {
  89. if (pos == NULL || end - pos < 12)
  90. return NULL;
  91. WPA_PUT_BE32(pos, attr);
  92. pos += 4;
  93. WPA_PUT_BE32(pos, 4);
  94. pos += 4;
  95. WPA_PUT_BE32(pos, val);
  96. pos += 4;
  97. return pos;
  98. }
  99. static int cmd_send_and_recv(int s, const u8 *cmd, size_t cmd_len,
  100. u8 *resp, size_t max_resp_len)
  101. {
  102. int res;
  103. enum wlantest_ctrl_cmd cmd_resp;
  104. if (send(s, cmd, cmd_len, 0) < 0)
  105. return -1;
  106. res = recv(s, resp, max_resp_len, 0);
  107. if (res < 4)
  108. return -1;
  109. cmd_resp = WPA_GET_BE32(resp);
  110. if (cmd_resp == WLANTEST_CTRL_SUCCESS)
  111. return res;
  112. if (cmd_resp == WLANTEST_CTRL_UNKNOWN_CMD)
  113. printf("Unknown command\n");
  114. else if (cmd_resp == WLANTEST_CTRL_INVALID_CMD)
  115. printf("Invalid command\n");
  116. return -1;
  117. }
  118. static int cmd_simple(int s, enum wlantest_ctrl_cmd cmd)
  119. {
  120. u8 buf[4];
  121. int res;
  122. WPA_PUT_BE32(buf, cmd);
  123. res = cmd_send_and_recv(s, buf, sizeof(buf), buf, sizeof(buf));
  124. return res < 0 ? -1 : 0;
  125. }
  126. static char ** get_bssid_list(int s)
  127. {
  128. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  129. u8 buf[4];
  130. u8 *bssid;
  131. size_t len;
  132. int rlen, i;
  133. char **res;
  134. WPA_PUT_BE32(buf, WLANTEST_CTRL_LIST_BSS);
  135. rlen = cmd_send_and_recv(s, buf, sizeof(buf), resp, sizeof(resp));
  136. if (rlen < 0)
  137. return NULL;
  138. bssid = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_BSSID, &len);
  139. if (bssid == NULL)
  140. return NULL;
  141. res = os_calloc(len / ETH_ALEN + 1, sizeof(char *));
  142. if (res == NULL)
  143. return NULL;
  144. for (i = 0; i < len / ETH_ALEN; i++) {
  145. res[i] = os_zalloc(18);
  146. if (res[i] == NULL)
  147. break;
  148. os_snprintf(res[i], 18, MACSTR, MAC2STR(bssid + ETH_ALEN * i));
  149. }
  150. return res;
  151. }
  152. static char ** get_sta_list(int s, const u8 *bssid, int add_bcast)
  153. {
  154. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  155. u8 buf[100], *pos, *end;
  156. u8 *addr;
  157. size_t len;
  158. int rlen, i;
  159. char **res;
  160. pos = buf;
  161. end = buf + sizeof(buf);
  162. WPA_PUT_BE32(pos, WLANTEST_CTRL_LIST_STA);
  163. pos += 4;
  164. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  165. os_memcpy(pos, bssid, ETH_ALEN);
  166. pos += ETH_ALEN;
  167. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  168. if (rlen < 0)
  169. return NULL;
  170. addr = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_STA_ADDR, &len);
  171. if (addr == NULL)
  172. return NULL;
  173. res = os_calloc(len / ETH_ALEN + 1 + add_bcast, sizeof(char *));
  174. if (res == NULL)
  175. return NULL;
  176. for (i = 0; i < len / ETH_ALEN; i++) {
  177. res[i] = os_zalloc(18);
  178. if (res[i] == NULL)
  179. break;
  180. os_snprintf(res[i], 18, MACSTR, MAC2STR(addr + ETH_ALEN * i));
  181. }
  182. if (add_bcast)
  183. res[i] = os_strdup("ff:ff:ff:ff:ff:ff");
  184. return res;
  185. }
  186. static int cmd_ping(int s, int argc, char *argv[])
  187. {
  188. int res = cmd_simple(s, WLANTEST_CTRL_PING);
  189. if (res == 0)
  190. printf("PONG\n");
  191. return res == 0;
  192. }
  193. static int cmd_terminate(int s, int argc, char *argv[])
  194. {
  195. return cmd_simple(s, WLANTEST_CTRL_TERMINATE);
  196. }
  197. static int cmd_list_bss(int s, int argc, char *argv[])
  198. {
  199. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  200. u8 buf[4];
  201. u8 *bssid;
  202. size_t len;
  203. int rlen, i;
  204. WPA_PUT_BE32(buf, WLANTEST_CTRL_LIST_BSS);
  205. rlen = cmd_send_and_recv(s, buf, sizeof(buf), resp, sizeof(resp));
  206. if (rlen < 0)
  207. return -1;
  208. bssid = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_BSSID, &len);
  209. if (bssid == NULL)
  210. return -1;
  211. for (i = 0; i < len / ETH_ALEN; i++)
  212. printf(MACSTR " ", MAC2STR(bssid + ETH_ALEN * i));
  213. printf("\n");
  214. return 0;
  215. }
  216. static int cmd_list_sta(int s, int argc, char *argv[])
  217. {
  218. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  219. u8 buf[100], *pos;
  220. u8 *addr;
  221. size_t len;
  222. int rlen, i;
  223. if (argc < 1) {
  224. printf("list_sta needs one argument: BSSID\n");
  225. return -1;
  226. }
  227. pos = buf;
  228. WPA_PUT_BE32(pos, WLANTEST_CTRL_LIST_STA);
  229. pos += 4;
  230. WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID);
  231. pos += 4;
  232. WPA_PUT_BE32(pos, ETH_ALEN);
  233. pos += 4;
  234. if (hwaddr_aton(argv[0], pos) < 0) {
  235. printf("Invalid BSSID '%s'\n", argv[0]);
  236. return -1;
  237. }
  238. pos += ETH_ALEN;
  239. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  240. if (rlen < 0)
  241. return -1;
  242. addr = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_STA_ADDR, &len);
  243. if (addr == NULL)
  244. return -1;
  245. for (i = 0; i < len / ETH_ALEN; i++)
  246. printf(MACSTR " ", MAC2STR(addr + ETH_ALEN * i));
  247. printf("\n");
  248. return 0;
  249. }
  250. static char ** complete_list_sta(int s, const char *str, int pos)
  251. {
  252. if (get_cmd_arg_num(str, pos) == 1)
  253. return get_bssid_list(s);
  254. return NULL;
  255. }
  256. static int cmd_flush(int s, int argc, char *argv[])
  257. {
  258. return cmd_simple(s, WLANTEST_CTRL_FLUSH);
  259. }
  260. static int cmd_clear_sta_counters(int s, int argc, char *argv[])
  261. {
  262. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  263. u8 buf[100], *pos;
  264. int rlen;
  265. if (argc < 2) {
  266. printf("clear_sta_counters needs two arguments: BSSID and "
  267. "STA address\n");
  268. return -1;
  269. }
  270. pos = buf;
  271. WPA_PUT_BE32(pos, WLANTEST_CTRL_CLEAR_STA_COUNTERS);
  272. pos += 4;
  273. WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID);
  274. pos += 4;
  275. WPA_PUT_BE32(pos, ETH_ALEN);
  276. pos += 4;
  277. if (hwaddr_aton(argv[0], pos) < 0) {
  278. printf("Invalid BSSID '%s'\n", argv[0]);
  279. return -1;
  280. }
  281. pos += ETH_ALEN;
  282. WPA_PUT_BE32(pos, WLANTEST_ATTR_STA_ADDR);
  283. pos += 4;
  284. WPA_PUT_BE32(pos, ETH_ALEN);
  285. pos += 4;
  286. if (hwaddr_aton(argv[1], pos) < 0) {
  287. printf("Invalid STA address '%s'\n", argv[1]);
  288. return -1;
  289. }
  290. pos += ETH_ALEN;
  291. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  292. if (rlen < 0)
  293. return -1;
  294. printf("OK\n");
  295. return 0;
  296. }
  297. static char ** complete_clear_sta_counters(int s, const char *str, int pos)
  298. {
  299. int arg = get_cmd_arg_num(str, pos);
  300. char **res = NULL;
  301. u8 addr[ETH_ALEN];
  302. switch (arg) {
  303. case 1:
  304. res = get_bssid_list(s);
  305. break;
  306. case 2:
  307. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  308. break;
  309. res = get_sta_list(s, addr, 0);
  310. break;
  311. }
  312. return res;
  313. }
  314. static int cmd_clear_bss_counters(int s, int argc, char *argv[])
  315. {
  316. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  317. u8 buf[100], *pos;
  318. int rlen;
  319. if (argc < 1) {
  320. printf("clear_bss_counters needs one argument: BSSID\n");
  321. return -1;
  322. }
  323. pos = buf;
  324. WPA_PUT_BE32(pos, WLANTEST_CTRL_CLEAR_BSS_COUNTERS);
  325. pos += 4;
  326. WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID);
  327. pos += 4;
  328. WPA_PUT_BE32(pos, ETH_ALEN);
  329. pos += 4;
  330. if (hwaddr_aton(argv[0], pos) < 0) {
  331. printf("Invalid BSSID '%s'\n", argv[0]);
  332. return -1;
  333. }
  334. pos += ETH_ALEN;
  335. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  336. if (rlen < 0)
  337. return -1;
  338. printf("OK\n");
  339. return 0;
  340. }
  341. static char ** complete_clear_bss_counters(int s, const char *str, int pos)
  342. {
  343. if (get_cmd_arg_num(str, pos) == 1)
  344. return get_bssid_list(s);
  345. return NULL;
  346. }
  347. static int cmd_clear_tdls_counters(int s, int argc, char *argv[])
  348. {
  349. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  350. u8 buf[100], *pos;
  351. int rlen;
  352. if (argc < 3) {
  353. printf("clear_tdls_counters needs three arguments: BSSID, "
  354. "STA1 address, STA2 address\n");
  355. return -1;
  356. }
  357. pos = buf;
  358. WPA_PUT_BE32(pos, WLANTEST_CTRL_CLEAR_TDLS_COUNTERS);
  359. pos += 4;
  360. WPA_PUT_BE32(pos, WLANTEST_ATTR_BSSID);
  361. pos += 4;
  362. WPA_PUT_BE32(pos, ETH_ALEN);
  363. pos += 4;
  364. if (hwaddr_aton(argv[0], pos) < 0) {
  365. printf("Invalid BSSID '%s'\n", argv[0]);
  366. return -1;
  367. }
  368. pos += ETH_ALEN;
  369. WPA_PUT_BE32(pos, WLANTEST_ATTR_STA_ADDR);
  370. pos += 4;
  371. WPA_PUT_BE32(pos, ETH_ALEN);
  372. pos += 4;
  373. if (hwaddr_aton(argv[1], pos) < 0) {
  374. printf("Invalid STA1 address '%s'\n", argv[1]);
  375. return -1;
  376. }
  377. pos += ETH_ALEN;
  378. WPA_PUT_BE32(pos, WLANTEST_ATTR_STA2_ADDR);
  379. pos += 4;
  380. WPA_PUT_BE32(pos, ETH_ALEN);
  381. pos += 4;
  382. if (hwaddr_aton(argv[2], pos) < 0) {
  383. printf("Invalid STA2 address '%s'\n", argv[2]);
  384. return -1;
  385. }
  386. pos += ETH_ALEN;
  387. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  388. if (rlen < 0)
  389. return -1;
  390. printf("OK\n");
  391. return 0;
  392. }
  393. static char ** complete_clear_tdls_counters(int s, const char *str, int pos)
  394. {
  395. int arg = get_cmd_arg_num(str, pos);
  396. char **res = NULL;
  397. u8 addr[ETH_ALEN];
  398. switch (arg) {
  399. case 1:
  400. res = get_bssid_list(s);
  401. break;
  402. case 2:
  403. case 3:
  404. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  405. break;
  406. res = get_sta_list(s, addr, 0);
  407. break;
  408. }
  409. return res;
  410. }
  411. struct sta_counters {
  412. const char *name;
  413. enum wlantest_sta_counter num;
  414. };
  415. static const struct sta_counters sta_counters[] = {
  416. { "auth_tx", WLANTEST_STA_COUNTER_AUTH_TX },
  417. { "auth_rx", WLANTEST_STA_COUNTER_AUTH_RX },
  418. { "assocreq_tx", WLANTEST_STA_COUNTER_ASSOCREQ_TX },
  419. { "reassocreq_tx", WLANTEST_STA_COUNTER_REASSOCREQ_TX },
  420. { "ptk_learned", WLANTEST_STA_COUNTER_PTK_LEARNED },
  421. { "valid_deauth_tx", WLANTEST_STA_COUNTER_VALID_DEAUTH_TX },
  422. { "valid_deauth_rx", WLANTEST_STA_COUNTER_VALID_DEAUTH_RX },
  423. { "invalid_deauth_tx", WLANTEST_STA_COUNTER_INVALID_DEAUTH_TX },
  424. { "invalid_deauth_rx", WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX },
  425. { "valid_disassoc_tx", WLANTEST_STA_COUNTER_VALID_DISASSOC_TX },
  426. { "valid_disassoc_rx", WLANTEST_STA_COUNTER_VALID_DISASSOC_RX },
  427. { "invalid_disassoc_tx", WLANTEST_STA_COUNTER_INVALID_DISASSOC_TX },
  428. { "invalid_disassoc_rx", WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX },
  429. { "valid_saqueryreq_tx", WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_TX },
  430. { "valid_saqueryreq_rx", WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_RX },
  431. { "invalid_saqueryreq_tx",
  432. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_TX },
  433. { "invalid_saqueryreq_rx",
  434. WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_RX },
  435. { "valid_saqueryresp_tx", WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_TX },
  436. { "valid_saqueryresp_rx", WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_RX },
  437. { "invalid_saqueryresp_tx",
  438. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_TX },
  439. { "invalid_saqueryresp_rx",
  440. WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_RX },
  441. { "ping_ok", WLANTEST_STA_COUNTER_PING_OK },
  442. { "assocresp_comeback", WLANTEST_STA_COUNTER_ASSOCRESP_COMEBACK },
  443. { "reassocresp_comeback", WLANTEST_STA_COUNTER_REASSOCRESP_COMEBACK },
  444. { "ping_ok_first_assoc", WLANTEST_STA_COUNTER_PING_OK_FIRST_ASSOC },
  445. { "valid_deauth_rx_ack", WLANTEST_STA_COUNTER_VALID_DEAUTH_RX_ACK },
  446. { "valid_disassoc_rx_ack",
  447. WLANTEST_STA_COUNTER_VALID_DISASSOC_RX_ACK },
  448. { "invalid_deauth_rx_ack",
  449. WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX_ACK },
  450. { "invalid_disassoc_rx_ack",
  451. WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX_ACK },
  452. { "deauth_rx_asleep", WLANTEST_STA_COUNTER_DEAUTH_RX_ASLEEP },
  453. { "deauth_rx_awake", WLANTEST_STA_COUNTER_DEAUTH_RX_AWAKE },
  454. { "disassoc_rx_asleep", WLANTEST_STA_COUNTER_DISASSOC_RX_ASLEEP },
  455. { "disassoc_rx_awake", WLANTEST_STA_COUNTER_DISASSOC_RX_AWAKE },
  456. { "prot_data_tx", WLANTEST_STA_COUNTER_PROT_DATA_TX },
  457. { "deauth_rx_rc6", WLANTEST_STA_COUNTER_DEAUTH_RX_RC6 },
  458. { "deauth_rx_rc7", WLANTEST_STA_COUNTER_DEAUTH_RX_RC7 },
  459. { "disassoc_rx_rc6", WLANTEST_STA_COUNTER_DISASSOC_RX_RC6 },
  460. { "disassoc_rx_rc7", WLANTEST_STA_COUNTER_DISASSOC_RX_RC7 },
  461. { NULL, 0 }
  462. };
  463. static int cmd_get_sta_counter(int s, int argc, char *argv[])
  464. {
  465. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  466. u8 buf[100], *end, *pos;
  467. int rlen, i;
  468. size_t len;
  469. if (argc != 3) {
  470. printf("get_sta_counter needs at three arguments: "
  471. "counter name, BSSID, and STA address\n");
  472. return -1;
  473. }
  474. pos = buf;
  475. end = buf + sizeof(buf);
  476. WPA_PUT_BE32(pos, WLANTEST_CTRL_GET_STA_COUNTER);
  477. pos += 4;
  478. for (i = 0; sta_counters[i].name; i++) {
  479. if (os_strcasecmp(sta_counters[i].name, argv[0]) == 0)
  480. break;
  481. }
  482. if (sta_counters[i].name == NULL) {
  483. printf("Unknown STA counter '%s'\n", argv[0]);
  484. printf("Counters:");
  485. for (i = 0; sta_counters[i].name; i++)
  486. printf(" %s", sta_counters[i].name);
  487. printf("\n");
  488. return -1;
  489. }
  490. pos = attr_add_be32(pos, end, WLANTEST_ATTR_STA_COUNTER,
  491. sta_counters[i].num);
  492. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  493. if (hwaddr_aton(argv[1], pos) < 0) {
  494. printf("Invalid BSSID '%s'\n", argv[1]);
  495. return -1;
  496. }
  497. pos += ETH_ALEN;
  498. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN);
  499. if (hwaddr_aton(argv[2], pos) < 0) {
  500. printf("Invalid STA address '%s'\n", argv[2]);
  501. return -1;
  502. }
  503. pos += ETH_ALEN;
  504. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  505. if (rlen < 0)
  506. return -1;
  507. pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_COUNTER, &len);
  508. if (pos == NULL || len != 4)
  509. return -1;
  510. printf("%u\n", WPA_GET_BE32(pos));
  511. return 0;
  512. }
  513. static char ** complete_get_sta_counter(int s, const char *str, int pos)
  514. {
  515. int arg = get_cmd_arg_num(str, pos);
  516. char **res = NULL;
  517. int i, count;
  518. u8 addr[ETH_ALEN];
  519. switch (arg) {
  520. case 1:
  521. /* counter list */
  522. count = sizeof(sta_counters) / sizeof(sta_counters[0]);
  523. res = os_calloc(count, sizeof(char *));
  524. if (res == NULL)
  525. return NULL;
  526. for (i = 0; sta_counters[i].name; i++) {
  527. res[i] = os_strdup(sta_counters[i].name);
  528. if (res[i] == NULL)
  529. break;
  530. }
  531. break;
  532. case 2:
  533. res = get_bssid_list(s);
  534. break;
  535. case 3:
  536. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  537. break;
  538. res = get_sta_list(s, addr, 0);
  539. break;
  540. }
  541. return res;
  542. }
  543. struct bss_counters {
  544. const char *name;
  545. enum wlantest_bss_counter num;
  546. };
  547. static const struct bss_counters bss_counters[] = {
  548. { "valid_bip_mmie", WLANTEST_BSS_COUNTER_VALID_BIP_MMIE },
  549. { "invalid_bip_mmie", WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE },
  550. { "missing_bip_mmie", WLANTEST_BSS_COUNTER_MISSING_BIP_MMIE },
  551. { "bip_deauth", WLANTEST_BSS_COUNTER_BIP_DEAUTH },
  552. { "bip_disassoc", WLANTEST_BSS_COUNTER_BIP_DISASSOC },
  553. { NULL, 0 }
  554. };
  555. static int cmd_get_bss_counter(int s, int argc, char *argv[])
  556. {
  557. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  558. u8 buf[100], *end, *pos;
  559. int rlen, i;
  560. size_t len;
  561. if (argc != 2) {
  562. printf("get_bss_counter needs at two arguments: "
  563. "counter name and BSSID\n");
  564. return -1;
  565. }
  566. pos = buf;
  567. end = buf + sizeof(buf);
  568. WPA_PUT_BE32(pos, WLANTEST_CTRL_GET_BSS_COUNTER);
  569. pos += 4;
  570. for (i = 0; bss_counters[i].name; i++) {
  571. if (os_strcasecmp(bss_counters[i].name, argv[0]) == 0)
  572. break;
  573. }
  574. if (bss_counters[i].name == NULL) {
  575. printf("Unknown BSS counter '%s'\n", argv[0]);
  576. printf("Counters:");
  577. for (i = 0; bss_counters[i].name; i++)
  578. printf(" %s", bss_counters[i].name);
  579. printf("\n");
  580. return -1;
  581. }
  582. pos = attr_add_be32(pos, end, WLANTEST_ATTR_BSS_COUNTER,
  583. bss_counters[i].num);
  584. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  585. if (hwaddr_aton(argv[1], pos) < 0) {
  586. printf("Invalid BSSID '%s'\n", argv[1]);
  587. return -1;
  588. }
  589. pos += ETH_ALEN;
  590. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  591. if (rlen < 0)
  592. return -1;
  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. static char ** complete_get_bss_counter(int s, const char *str, int pos)
  600. {
  601. int arg = get_cmd_arg_num(str, pos);
  602. char **res = NULL;
  603. int i, count;
  604. switch (arg) {
  605. case 1:
  606. /* counter list */
  607. count = sizeof(bss_counters) / sizeof(bss_counters[0]);
  608. res = os_calloc(count, sizeof(char *));
  609. if (res == NULL)
  610. return NULL;
  611. for (i = 0; bss_counters[i].name; i++) {
  612. res[i] = os_strdup(bss_counters[i].name);
  613. if (res[i] == NULL)
  614. break;
  615. }
  616. break;
  617. case 2:
  618. res = get_bssid_list(s);
  619. break;
  620. }
  621. return res;
  622. }
  623. struct tdls_counters {
  624. const char *name;
  625. enum wlantest_tdls_counter num;
  626. };
  627. static const struct tdls_counters tdls_counters[] = {
  628. { "valid_direct_link", WLANTEST_TDLS_COUNTER_VALID_DIRECT_LINK },
  629. { "invalid_direct_link", WLANTEST_TDLS_COUNTER_INVALID_DIRECT_LINK },
  630. { "valid_ap_path", WLANTEST_TDLS_COUNTER_VALID_AP_PATH },
  631. { "invalid_ap_path", WLANTEST_TDLS_COUNTER_INVALID_AP_PATH },
  632. { "setup_req", WLANTEST_TDLS_COUNTER_SETUP_REQ },
  633. { "setup_resp_ok", WLANTEST_TDLS_COUNTER_SETUP_RESP_OK },
  634. { "setup_resp_fail", WLANTEST_TDLS_COUNTER_SETUP_RESP_FAIL },
  635. { "setup_conf_ok", WLANTEST_TDLS_COUNTER_SETUP_CONF_OK },
  636. { "setup_conf_fail", WLANTEST_TDLS_COUNTER_SETUP_CONF_FAIL },
  637. { "teardown", WLANTEST_TDLS_COUNTER_TEARDOWN },
  638. { NULL, 0 }
  639. };
  640. static int cmd_get_tdls_counter(int s, int argc, char *argv[])
  641. {
  642. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  643. u8 buf[100], *end, *pos;
  644. int rlen, i;
  645. size_t len;
  646. if (argc != 4) {
  647. printf("get_tdls_counter needs four arguments: "
  648. "counter name, BSSID, STA1 address, STA2 address\n");
  649. return -1;
  650. }
  651. pos = buf;
  652. end = buf + sizeof(buf);
  653. WPA_PUT_BE32(pos, WLANTEST_CTRL_GET_TDLS_COUNTER);
  654. pos += 4;
  655. for (i = 0; tdls_counters[i].name; i++) {
  656. if (os_strcasecmp(tdls_counters[i].name, argv[0]) == 0)
  657. break;
  658. }
  659. if (tdls_counters[i].name == NULL) {
  660. printf("Unknown TDLS counter '%s'\n", argv[0]);
  661. printf("Counters:");
  662. for (i = 0; tdls_counters[i].name; i++)
  663. printf(" %s", tdls_counters[i].name);
  664. printf("\n");
  665. return -1;
  666. }
  667. pos = attr_add_be32(pos, end, WLANTEST_ATTR_TDLS_COUNTER,
  668. tdls_counters[i].num);
  669. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  670. if (hwaddr_aton(argv[1], pos) < 0) {
  671. printf("Invalid BSSID '%s'\n", argv[1]);
  672. return -1;
  673. }
  674. pos += ETH_ALEN;
  675. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN);
  676. if (hwaddr_aton(argv[2], pos) < 0) {
  677. printf("Invalid STA1 address '%s'\n", argv[2]);
  678. return -1;
  679. }
  680. pos += ETH_ALEN;
  681. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA2_ADDR, ETH_ALEN);
  682. if (hwaddr_aton(argv[3], pos) < 0) {
  683. printf("Invalid STA2 address '%s'\n", argv[3]);
  684. return -1;
  685. }
  686. pos += ETH_ALEN;
  687. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  688. if (rlen < 0)
  689. return -1;
  690. pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_COUNTER, &len);
  691. if (pos == NULL || len != 4)
  692. return -1;
  693. printf("%u\n", WPA_GET_BE32(pos));
  694. return 0;
  695. }
  696. static char ** complete_get_tdls_counter(int s, const char *str, int pos)
  697. {
  698. int arg = get_cmd_arg_num(str, pos);
  699. char **res = NULL;
  700. int i, count;
  701. u8 addr[ETH_ALEN];
  702. switch (arg) {
  703. case 1:
  704. /* counter list */
  705. count = sizeof(tdls_counters) / sizeof(tdls_counters[0]);
  706. res = os_calloc(count, sizeof(char *));
  707. if (res == NULL)
  708. return NULL;
  709. for (i = 0; tdls_counters[i].name; i++) {
  710. res[i] = os_strdup(tdls_counters[i].name);
  711. if (res[i] == NULL)
  712. break;
  713. }
  714. break;
  715. case 2:
  716. res = get_bssid_list(s);
  717. break;
  718. case 3:
  719. case 4:
  720. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  721. break;
  722. res = get_sta_list(s, addr, 0);
  723. break;
  724. }
  725. return res;
  726. }
  727. struct inject_frames {
  728. const char *name;
  729. enum wlantest_inject_frame frame;
  730. };
  731. static const struct inject_frames inject_frames[] = {
  732. { "auth", WLANTEST_FRAME_AUTH },
  733. { "assocreq", WLANTEST_FRAME_ASSOCREQ },
  734. { "reassocreq", WLANTEST_FRAME_REASSOCREQ },
  735. { "deauth", WLANTEST_FRAME_DEAUTH },
  736. { "disassoc", WLANTEST_FRAME_DISASSOC },
  737. { "saqueryreq", WLANTEST_FRAME_SAQUERYREQ },
  738. { NULL, 0 }
  739. };
  740. static int cmd_inject(int s, int argc, char *argv[])
  741. {
  742. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  743. u8 buf[100], *end, *pos;
  744. int rlen, i;
  745. enum wlantest_inject_protection prot;
  746. /* <frame> <prot> <sender> <BSSID> <STA/ff:ff:ff:ff:ff:ff> */
  747. if (argc < 5) {
  748. printf("inject needs five arguments: frame, protection, "
  749. "sender, BSSID, STA/ff:ff:ff:ff:ff:ff\n");
  750. return -1;
  751. }
  752. pos = buf;
  753. end = buf + sizeof(buf);
  754. WPA_PUT_BE32(pos, WLANTEST_CTRL_INJECT);
  755. pos += 4;
  756. for (i = 0; inject_frames[i].name; i++) {
  757. if (os_strcasecmp(inject_frames[i].name, argv[0]) == 0)
  758. break;
  759. }
  760. if (inject_frames[i].name == NULL) {
  761. printf("Unknown inject frame '%s'\n", argv[0]);
  762. printf("Frames:");
  763. for (i = 0; inject_frames[i].name; i++)
  764. printf(" %s", inject_frames[i].name);
  765. printf("\n");
  766. return -1;
  767. }
  768. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_FRAME,
  769. inject_frames[i].frame);
  770. if (os_strcasecmp(argv[1], "normal") == 0)
  771. prot = WLANTEST_INJECT_NORMAL;
  772. else if (os_strcasecmp(argv[1], "protected") == 0)
  773. prot = WLANTEST_INJECT_PROTECTED;
  774. else if (os_strcasecmp(argv[1], "unprotected") == 0)
  775. prot = WLANTEST_INJECT_UNPROTECTED;
  776. else if (os_strcasecmp(argv[1], "incorrect") == 0)
  777. prot = WLANTEST_INJECT_INCORRECT_KEY;
  778. else {
  779. printf("Unknown protection type '%s'\n", argv[1]);
  780. printf("Protection types: normal protected unprotected "
  781. "incorrect\n");
  782. return -1;
  783. }
  784. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_PROTECTION, prot);
  785. if (os_strcasecmp(argv[2], "ap") == 0) {
  786. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_SENDER_AP,
  787. 1);
  788. } else if (os_strcasecmp(argv[2], "sta") == 0) {
  789. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_SENDER_AP,
  790. 0);
  791. } else {
  792. printf("Unknown sender '%s'\n", argv[2]);
  793. printf("Sender types: ap sta\n");
  794. return -1;
  795. }
  796. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  797. if (hwaddr_aton(argv[3], pos) < 0) {
  798. printf("Invalid BSSID '%s'\n", argv[3]);
  799. return -1;
  800. }
  801. pos += ETH_ALEN;
  802. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN);
  803. if (hwaddr_aton(argv[4], pos) < 0) {
  804. printf("Invalid STA '%s'\n", argv[4]);
  805. return -1;
  806. }
  807. pos += ETH_ALEN;
  808. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  809. if (rlen < 0)
  810. return -1;
  811. printf("OK\n");
  812. return 0;
  813. }
  814. static char ** complete_inject(int s, const char *str, int pos)
  815. {
  816. int arg = get_cmd_arg_num(str, pos);
  817. char **res = NULL;
  818. int i, count;
  819. u8 addr[ETH_ALEN];
  820. switch (arg) {
  821. case 1:
  822. /* frame list */
  823. count = sizeof(inject_frames) / sizeof(inject_frames[0]);
  824. res = os_calloc(count, sizeof(char *));
  825. if (res == NULL)
  826. break;
  827. for (i = 0; inject_frames[i].name; i++) {
  828. res[i] = os_strdup(inject_frames[i].name);
  829. if (res[i] == NULL)
  830. break;
  831. }
  832. break;
  833. case 2:
  834. res = os_calloc(5, sizeof(char *));
  835. if (res == NULL)
  836. break;
  837. res[0] = os_strdup("normal");
  838. if (res[0] == NULL)
  839. break;
  840. res[1] = os_strdup("protected");
  841. if (res[1] == NULL)
  842. break;
  843. res[2] = os_strdup("unprotected");
  844. if (res[2] == NULL)
  845. break;
  846. res[3] = os_strdup("incorrect");
  847. if (res[3] == NULL)
  848. break;
  849. break;
  850. case 3:
  851. res = os_calloc(3, sizeof(char *));
  852. if (res == NULL)
  853. break;
  854. res[0] = os_strdup("ap");
  855. if (res[0] == NULL)
  856. break;
  857. res[1] = os_strdup("sta");
  858. if (res[1] == NULL)
  859. break;
  860. break;
  861. case 4:
  862. res = get_bssid_list(s);
  863. break;
  864. case 5:
  865. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  866. break;
  867. res = get_sta_list(s, addr, 1);
  868. break;
  869. }
  870. return res;
  871. }
  872. static u8 * add_hex(u8 *pos, u8 *end, const char *str)
  873. {
  874. const char *s;
  875. int val;
  876. s = str;
  877. while (*s) {
  878. while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n' ||
  879. *s == ':')
  880. s++;
  881. if (*s == '\0')
  882. break;
  883. if (*s == '#') {
  884. while (*s != '\0' && *s != '\r' && *s != '\n')
  885. s++;
  886. continue;
  887. }
  888. val = hex2byte(s);
  889. if (val < 0) {
  890. printf("Invalid hex encoding '%s'\n", s);
  891. return NULL;
  892. }
  893. if (pos == end) {
  894. printf("Too long frame\n");
  895. return NULL;
  896. }
  897. *pos++ = val;
  898. s += 2;
  899. }
  900. return pos;
  901. }
  902. static int cmd_send(int s, int argc, char *argv[])
  903. {
  904. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  905. u8 buf[WLANTEST_CTRL_MAX_CMD_LEN], *end, *pos, *len_pos;
  906. int rlen;
  907. enum wlantest_inject_protection prot;
  908. int arg;
  909. /* <prot> <raw frame as hex dump> */
  910. if (argc < 2) {
  911. printf("send needs two arguments: protected/unprotected, "
  912. "raw frame as hex dump\n");
  913. return -1;
  914. }
  915. pos = buf;
  916. end = buf + sizeof(buf);
  917. WPA_PUT_BE32(pos, WLANTEST_CTRL_SEND);
  918. pos += 4;
  919. if (os_strcasecmp(argv[0], "normal") == 0)
  920. prot = WLANTEST_INJECT_NORMAL;
  921. else if (os_strcasecmp(argv[0], "protected") == 0)
  922. prot = WLANTEST_INJECT_PROTECTED;
  923. else if (os_strcasecmp(argv[0], "unprotected") == 0)
  924. prot = WLANTEST_INJECT_UNPROTECTED;
  925. else if (os_strcasecmp(argv[0], "incorrect") == 0)
  926. prot = WLANTEST_INJECT_INCORRECT_KEY;
  927. else {
  928. printf("Unknown protection type '%s'\n", argv[1]);
  929. printf("Protection types: normal protected unprotected "
  930. "incorrect\n");
  931. return -1;
  932. }
  933. pos = attr_add_be32(pos, end, WLANTEST_ATTR_INJECT_PROTECTION, prot);
  934. WPA_PUT_BE32(pos, WLANTEST_ATTR_FRAME);
  935. pos += 4;
  936. len_pos = pos;
  937. pos += 4;
  938. for (arg = 1; pos && arg < argc; arg++)
  939. pos = add_hex(pos, end, argv[arg]);
  940. if (pos == NULL)
  941. return -1;
  942. WPA_PUT_BE32(len_pos, pos - len_pos - 4);
  943. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  944. if (rlen < 0)
  945. return -1;
  946. printf("OK\n");
  947. return 0;
  948. }
  949. static char ** complete_send(int s, const char *str, int pos)
  950. {
  951. int arg = get_cmd_arg_num(str, pos);
  952. char **res = NULL;
  953. switch (arg) {
  954. case 1:
  955. res = os_calloc(5, sizeof(char *));
  956. if (res == NULL)
  957. break;
  958. res[0] = os_strdup("normal");
  959. if (res[0] == NULL)
  960. break;
  961. res[1] = os_strdup("protected");
  962. if (res[1] == NULL)
  963. break;
  964. res[2] = os_strdup("unprotected");
  965. if (res[2] == NULL)
  966. break;
  967. res[3] = os_strdup("incorrect");
  968. if (res[3] == NULL)
  969. break;
  970. break;
  971. }
  972. return res;
  973. }
  974. static int cmd_version(int s, int argc, char *argv[])
  975. {
  976. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  977. u8 buf[4];
  978. char *version;
  979. size_t len;
  980. int rlen, i;
  981. WPA_PUT_BE32(buf, WLANTEST_CTRL_VERSION);
  982. rlen = cmd_send_and_recv(s, buf, sizeof(buf), resp, sizeof(resp));
  983. if (rlen < 0)
  984. return -1;
  985. version = (char *) attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_VERSION,
  986. &len);
  987. if (version == NULL)
  988. return -1;
  989. for (i = 0; i < len; i++)
  990. putchar(version[i]);
  991. printf("\n");
  992. return 0;
  993. }
  994. static int cmd_add_passphrase(int s, int argc, char *argv[])
  995. {
  996. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  997. u8 buf[100], *pos, *end;
  998. size_t len;
  999. int rlen;
  1000. if (argc < 1) {
  1001. printf("add_passphrase needs one argument: passphrase\n");
  1002. return -1;
  1003. }
  1004. len = os_strlen(argv[0]);
  1005. if (len < 8 || len > 63) {
  1006. printf("Invalid passphrase '%s'\n", argv[0]);
  1007. return -1;
  1008. }
  1009. pos = buf;
  1010. end = buf + sizeof(buf);
  1011. WPA_PUT_BE32(pos, WLANTEST_CTRL_ADD_PASSPHRASE);
  1012. pos += 4;
  1013. pos = attr_add_str(pos, end, WLANTEST_ATTR_PASSPHRASE,
  1014. argv[0]);
  1015. if (argc > 1) {
  1016. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  1017. if (hwaddr_aton(argv[1], pos) < 0) {
  1018. printf("Invalid BSSID '%s'\n", argv[3]);
  1019. return -1;
  1020. }
  1021. pos += ETH_ALEN;
  1022. }
  1023. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  1024. if (rlen < 0)
  1025. return -1;
  1026. return 0;
  1027. }
  1028. static int cmd_add_wepkey(int s, int argc, char *argv[])
  1029. {
  1030. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  1031. u8 buf[100], *pos, *end;
  1032. int rlen;
  1033. if (argc < 1) {
  1034. printf("add_wepkey needs one argument: WEP key\n");
  1035. return -1;
  1036. }
  1037. pos = buf;
  1038. end = buf + sizeof(buf);
  1039. WPA_PUT_BE32(pos, WLANTEST_CTRL_ADD_PASSPHRASE);
  1040. pos += 4;
  1041. pos = attr_add_str(pos, end, WLANTEST_ATTR_WEPKEY, argv[0]);
  1042. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  1043. if (rlen < 0)
  1044. return -1;
  1045. return 0;
  1046. }
  1047. struct sta_infos {
  1048. const char *name;
  1049. enum wlantest_sta_info num;
  1050. };
  1051. static const struct sta_infos sta_infos[] = {
  1052. { "proto", WLANTEST_STA_INFO_PROTO },
  1053. { "pairwise", WLANTEST_STA_INFO_PAIRWISE },
  1054. { "key_mgmt", WLANTEST_STA_INFO_KEY_MGMT },
  1055. { "rsn_capab", WLANTEST_STA_INFO_RSN_CAPAB },
  1056. { "state", WLANTEST_STA_INFO_STATE },
  1057. { "gtk", WLANTEST_STA_INFO_GTK },
  1058. { NULL, 0 }
  1059. };
  1060. static int cmd_info_sta(int s, int argc, char *argv[])
  1061. {
  1062. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  1063. u8 buf[100], *end, *pos;
  1064. int rlen, i;
  1065. size_t len;
  1066. char info[100];
  1067. if (argc != 3) {
  1068. printf("sta_info needs at three arguments: "
  1069. "counter name, BSSID, and STA address\n");
  1070. return -1;
  1071. }
  1072. pos = buf;
  1073. end = buf + sizeof(buf);
  1074. WPA_PUT_BE32(pos, WLANTEST_CTRL_INFO_STA);
  1075. pos += 4;
  1076. for (i = 0; sta_infos[i].name; i++) {
  1077. if (os_strcasecmp(sta_infos[i].name, argv[0]) == 0)
  1078. break;
  1079. }
  1080. if (sta_infos[i].name == NULL) {
  1081. printf("Unknown STA info '%s'\n", argv[0]);
  1082. printf("Info fields:");
  1083. for (i = 0; sta_infos[i].name; i++)
  1084. printf(" %s", sta_infos[i].name);
  1085. printf("\n");
  1086. return -1;
  1087. }
  1088. pos = attr_add_be32(pos, end, WLANTEST_ATTR_STA_INFO,
  1089. sta_infos[i].num);
  1090. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  1091. if (hwaddr_aton(argv[1], pos) < 0) {
  1092. printf("Invalid BSSID '%s'\n", argv[1]);
  1093. return -1;
  1094. }
  1095. pos += ETH_ALEN;
  1096. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_STA_ADDR, ETH_ALEN);
  1097. if (hwaddr_aton(argv[2], pos) < 0) {
  1098. printf("Invalid STA address '%s'\n", argv[2]);
  1099. return -1;
  1100. }
  1101. pos += ETH_ALEN;
  1102. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  1103. if (rlen < 0)
  1104. return -1;
  1105. pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_INFO, &len);
  1106. if (pos == NULL)
  1107. return -1;
  1108. if (len >= sizeof(info))
  1109. len = sizeof(info) - 1;
  1110. os_memcpy(info, pos, len);
  1111. info[len] = '\0';
  1112. printf("%s\n", info);
  1113. return 0;
  1114. }
  1115. static char ** complete_info_sta(int s, const char *str, int pos)
  1116. {
  1117. int arg = get_cmd_arg_num(str, pos);
  1118. char **res = NULL;
  1119. int i, count;
  1120. u8 addr[ETH_ALEN];
  1121. switch (arg) {
  1122. case 1:
  1123. /* counter list */
  1124. count = sizeof(sta_infos) / sizeof(sta_infos[0]);
  1125. res = os_calloc(count, sizeof(char *));
  1126. if (res == NULL)
  1127. return NULL;
  1128. for (i = 0; sta_infos[i].name; i++) {
  1129. res[i] = os_strdup(sta_infos[i].name);
  1130. if (res[i] == NULL)
  1131. break;
  1132. }
  1133. break;
  1134. case 2:
  1135. res = get_bssid_list(s);
  1136. break;
  1137. case 3:
  1138. if (hwaddr_aton(&str[get_prev_arg_pos(str, pos)], addr) < 0)
  1139. break;
  1140. res = get_sta_list(s, addr, 0);
  1141. break;
  1142. }
  1143. return res;
  1144. }
  1145. struct bss_infos {
  1146. const char *name;
  1147. enum wlantest_bss_info num;
  1148. };
  1149. static const struct bss_infos bss_infos[] = {
  1150. { "proto", WLANTEST_BSS_INFO_PROTO },
  1151. { "pairwise", WLANTEST_BSS_INFO_PAIRWISE },
  1152. { "group", WLANTEST_BSS_INFO_GROUP },
  1153. { "group_mgmt", WLANTEST_BSS_INFO_GROUP_MGMT },
  1154. { "key_mgmt", WLANTEST_BSS_INFO_KEY_MGMT },
  1155. { "rsn_capab", WLANTEST_BSS_INFO_RSN_CAPAB },
  1156. { NULL, 0 }
  1157. };
  1158. static int cmd_info_bss(int s, int argc, char *argv[])
  1159. {
  1160. u8 resp[WLANTEST_CTRL_MAX_RESP_LEN];
  1161. u8 buf[100], *end, *pos;
  1162. int rlen, i;
  1163. size_t len;
  1164. char info[100];
  1165. if (argc != 2) {
  1166. printf("bss_info needs at two arguments: "
  1167. "field name and BSSID\n");
  1168. return -1;
  1169. }
  1170. pos = buf;
  1171. end = buf + sizeof(buf);
  1172. WPA_PUT_BE32(pos, WLANTEST_CTRL_INFO_BSS);
  1173. pos += 4;
  1174. for (i = 0; bss_infos[i].name; i++) {
  1175. if (os_strcasecmp(bss_infos[i].name, argv[0]) == 0)
  1176. break;
  1177. }
  1178. if (bss_infos[i].name == NULL) {
  1179. printf("Unknown BSS info '%s'\n", argv[0]);
  1180. printf("Info fields:");
  1181. for (i = 0; bss_infos[i].name; i++)
  1182. printf(" %s", bss_infos[i].name);
  1183. printf("\n");
  1184. return -1;
  1185. }
  1186. pos = attr_add_be32(pos, end, WLANTEST_ATTR_BSS_INFO,
  1187. bss_infos[i].num);
  1188. pos = attr_hdr_add(pos, end, WLANTEST_ATTR_BSSID, ETH_ALEN);
  1189. if (hwaddr_aton(argv[1], pos) < 0) {
  1190. printf("Invalid BSSID '%s'\n", argv[1]);
  1191. return -1;
  1192. }
  1193. pos += ETH_ALEN;
  1194. rlen = cmd_send_and_recv(s, buf, pos - buf, resp, sizeof(resp));
  1195. if (rlen < 0)
  1196. return -1;
  1197. pos = attr_get(resp + 4, rlen - 4, WLANTEST_ATTR_INFO, &len);
  1198. if (pos == NULL)
  1199. return -1;
  1200. if (len >= sizeof(info))
  1201. len = sizeof(info) - 1;
  1202. os_memcpy(info, pos, len);
  1203. info[len] = '\0';
  1204. printf("%s\n", info);
  1205. return 0;
  1206. }
  1207. static char ** complete_info_bss(int s, const char *str, int pos)
  1208. {
  1209. int arg = get_cmd_arg_num(str, pos);
  1210. char **res = NULL;
  1211. int i, count;
  1212. switch (arg) {
  1213. case 1:
  1214. /* counter list */
  1215. count = sizeof(bss_infos) / sizeof(bss_infos[0]);
  1216. res = os_calloc(count, sizeof(char *));
  1217. if (res == NULL)
  1218. return NULL;
  1219. for (i = 0; bss_infos[i].name; i++) {
  1220. res[i] = os_strdup(bss_infos[i].name);
  1221. if (res[i] == NULL)
  1222. break;
  1223. }
  1224. break;
  1225. case 2:
  1226. res = get_bssid_list(s);
  1227. break;
  1228. }
  1229. return res;
  1230. }
  1231. struct wlantest_cli_cmd {
  1232. const char *cmd;
  1233. int (*handler)(int s, int argc, char *argv[]);
  1234. const char *usage;
  1235. char ** (*complete)(int s, const char *str, int pos);
  1236. };
  1237. static const struct wlantest_cli_cmd wlantest_cli_commands[] = {
  1238. { "ping", cmd_ping, "= test connection to wlantest", NULL },
  1239. { "terminate", cmd_terminate, "= terminate wlantest", NULL },
  1240. { "list_bss", cmd_list_bss, "= get BSS list", NULL },
  1241. { "list_sta", cmd_list_sta, "<BSSID> = get STA list",
  1242. complete_list_sta },
  1243. { "flush", cmd_flush, "= drop all collected BSS data", NULL },
  1244. { "clear_sta_counters", cmd_clear_sta_counters,
  1245. "<BSSID> <STA> = clear STA counters", complete_clear_sta_counters },
  1246. { "clear_bss_counters", cmd_clear_bss_counters,
  1247. "<BSSID> = clear BSS counters", complete_clear_bss_counters },
  1248. { "get_sta_counter", cmd_get_sta_counter,
  1249. "<counter> <BSSID> <STA> = get STA counter value",
  1250. complete_get_sta_counter },
  1251. { "get_bss_counter", cmd_get_bss_counter,
  1252. "<counter> <BSSID> = get BSS counter value",
  1253. complete_get_bss_counter },
  1254. { "inject", cmd_inject,
  1255. "<frame> <prot> <sender> <BSSID> <STA/ff:ff:ff:ff:ff:ff>",
  1256. complete_inject },
  1257. { "send", cmd_send,
  1258. "<prot> <raw frame as hex dump>",
  1259. complete_send },
  1260. { "version", cmd_version, "= get wlantest version", NULL },
  1261. { "add_passphrase", cmd_add_passphrase,
  1262. "<passphrase> = add a known passphrase", NULL },
  1263. { "add_wepkey", cmd_add_wepkey,
  1264. "<WEP key> = add a known WEP key", NULL },
  1265. { "info_sta", cmd_info_sta,
  1266. "<field> <BSSID> <STA> = get STA information",
  1267. complete_info_sta },
  1268. { "info_bss", cmd_info_bss,
  1269. "<field> <BSSID> = get BSS information",
  1270. complete_info_bss },
  1271. { "clear_tdls_counters", cmd_clear_tdls_counters,
  1272. "<BSSID> <STA1> <STA2> = clear TDLS counters",
  1273. complete_clear_tdls_counters },
  1274. { "get_tdls_counter", cmd_get_tdls_counter,
  1275. "<counter> <BSSID> <STA1> <STA2> = get TDLS counter value",
  1276. complete_get_tdls_counter },
  1277. { "get_bss_counter", cmd_get_bss_counter,
  1278. "<counter> <BSSID> = get BSS counter value",
  1279. complete_get_bss_counter },
  1280. { NULL, NULL, NULL, NULL }
  1281. };
  1282. static int ctrl_command(int s, int argc, char *argv[])
  1283. {
  1284. const struct wlantest_cli_cmd *cmd, *match = NULL;
  1285. int count = 0;
  1286. int ret = 0;
  1287. for (cmd = wlantest_cli_commands; cmd->cmd; cmd++) {
  1288. if (os_strncasecmp(cmd->cmd, argv[0], os_strlen(argv[0])) == 0)
  1289. {
  1290. match = cmd;
  1291. if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
  1292. /* exact match */
  1293. count = 1;
  1294. break;
  1295. }
  1296. count++;
  1297. }
  1298. }
  1299. if (count > 1) {
  1300. printf("Ambiguous command '%s'; possible commands:", argv[0]);
  1301. for (cmd = wlantest_cli_commands; cmd->cmd; cmd++) {
  1302. if (os_strncasecmp(cmd->cmd, argv[0],
  1303. os_strlen(argv[0])) == 0) {
  1304. printf(" %s", cmd->cmd);
  1305. }
  1306. }
  1307. printf("\n");
  1308. ret = 1;
  1309. } else if (count == 0) {
  1310. printf("Unknown command '%s'\n", argv[0]);
  1311. ret = 1;
  1312. } else {
  1313. ret = match->handler(s, argc - 1, &argv[1]);
  1314. }
  1315. return ret;
  1316. }
  1317. struct wlantest_cli {
  1318. int s;
  1319. };
  1320. #define max_args 10
  1321. static int tokenize_cmd(char *cmd, char *argv[])
  1322. {
  1323. char *pos;
  1324. int argc = 0;
  1325. pos = cmd;
  1326. for (;;) {
  1327. while (*pos == ' ')
  1328. pos++;
  1329. if (*pos == '\0')
  1330. break;
  1331. argv[argc] = pos;
  1332. argc++;
  1333. if (argc == max_args)
  1334. break;
  1335. if (*pos == '"') {
  1336. char *pos2 = os_strrchr(pos, '"');
  1337. if (pos2)
  1338. pos = pos2 + 1;
  1339. }
  1340. while (*pos != '\0' && *pos != ' ')
  1341. pos++;
  1342. if (*pos == ' ')
  1343. *pos++ = '\0';
  1344. }
  1345. return argc;
  1346. }
  1347. static void wlantest_cli_edit_cmd_cb(void *ctx, char *cmd)
  1348. {
  1349. struct wlantest_cli *cli = ctx;
  1350. char *argv[max_args];
  1351. int argc;
  1352. argc = tokenize_cmd(cmd, argv);
  1353. if (argc) {
  1354. int ret = ctrl_command(cli->s, argc, argv);
  1355. if (ret < 0)
  1356. printf("FAIL\n");
  1357. }
  1358. }
  1359. static void wlantest_cli_eloop_terminate(int sig, void *signal_ctx)
  1360. {
  1361. eloop_terminate();
  1362. }
  1363. static void wlantest_cli_edit_eof_cb(void *ctx)
  1364. {
  1365. eloop_terminate();
  1366. }
  1367. static char ** wlantest_cli_cmd_list(void)
  1368. {
  1369. char **res;
  1370. int i, count;
  1371. count = sizeof(wlantest_cli_commands) /
  1372. sizeof(wlantest_cli_commands[0]);
  1373. res = os_calloc(count, sizeof(char *));
  1374. if (res == NULL)
  1375. return NULL;
  1376. for (i = 0; wlantest_cli_commands[i].cmd; i++) {
  1377. res[i] = os_strdup(wlantest_cli_commands[i].cmd);
  1378. if (res[i] == NULL)
  1379. break;
  1380. }
  1381. return res;
  1382. }
  1383. static char ** wlantest_cli_cmd_completion(struct wlantest_cli *cli,
  1384. const char *cmd, const char *str,
  1385. int pos)
  1386. {
  1387. int i;
  1388. for (i = 0; wlantest_cli_commands[i].cmd; i++) {
  1389. const struct wlantest_cli_cmd *c = &wlantest_cli_commands[i];
  1390. if (os_strcasecmp(c->cmd, cmd) == 0) {
  1391. edit_clear_line();
  1392. printf("\r%s\n", c->usage);
  1393. edit_redraw();
  1394. if (c->complete)
  1395. return c->complete(cli->s, str, pos);
  1396. break;
  1397. }
  1398. }
  1399. return NULL;
  1400. }
  1401. static char ** wlantest_cli_edit_completion_cb(void *ctx, const char *str,
  1402. int pos)
  1403. {
  1404. struct wlantest_cli *cli = ctx;
  1405. char **res;
  1406. const char *end;
  1407. char *cmd;
  1408. end = os_strchr(str, ' ');
  1409. if (end == NULL || str + pos < end)
  1410. return wlantest_cli_cmd_list();
  1411. cmd = os_malloc(pos + 1);
  1412. if (cmd == NULL)
  1413. return NULL;
  1414. os_memcpy(cmd, str, pos);
  1415. cmd[end - str] = '\0';
  1416. res = wlantest_cli_cmd_completion(cli, cmd, str, pos);
  1417. os_free(cmd);
  1418. return res;
  1419. }
  1420. static void wlantest_cli_interactive(int s)
  1421. {
  1422. struct wlantest_cli cli;
  1423. char *home, *hfile = NULL;
  1424. if (eloop_init())
  1425. return;
  1426. home = getenv("HOME");
  1427. if (home) {
  1428. const char *fname = ".wlantest_cli_history";
  1429. int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
  1430. hfile = os_malloc(hfile_len);
  1431. if (hfile)
  1432. os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
  1433. }
  1434. cli.s = s;
  1435. eloop_register_signal_terminate(wlantest_cli_eloop_terminate, &cli);
  1436. edit_init(wlantest_cli_edit_cmd_cb, wlantest_cli_edit_eof_cb,
  1437. wlantest_cli_edit_completion_cb, &cli, hfile, NULL);
  1438. eloop_run();
  1439. edit_deinit(hfile, NULL);
  1440. os_free(hfile);
  1441. eloop_destroy();
  1442. }
  1443. int main(int argc, char *argv[])
  1444. {
  1445. int s;
  1446. struct sockaddr_un addr;
  1447. int ret = 0;
  1448. if (os_program_init())
  1449. return -1;
  1450. s = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  1451. if (s < 0) {
  1452. perror("socket");
  1453. return -1;
  1454. }
  1455. os_memset(&addr, 0, sizeof(addr));
  1456. addr.sun_family = AF_UNIX;
  1457. os_strlcpy(addr.sun_path + 1, WLANTEST_SOCK_NAME,
  1458. sizeof(addr.sun_path) - 1);
  1459. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1460. perror("connect");
  1461. close(s);
  1462. return -1;
  1463. }
  1464. if (argc > 1) {
  1465. ret = ctrl_command(s, argc - 1, &argv[1]);
  1466. if (ret < 0)
  1467. printf("FAIL\n");
  1468. } else {
  1469. wlantest_cli_interactive(s);
  1470. }
  1471. close(s);
  1472. os_program_deinit();
  1473. return ret;
  1474. }