wlantest_cli.c 38 KB

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