wlantest_cli.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  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 = ARRAY_SIZE(sta_counters);
  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 = ARRAY_SIZE(bss_counters);
  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 = ARRAY_SIZE(tdls_counters);
  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 = ARRAY_SIZE(inject_frames);
  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 = ARRAY_SIZE(sta_infos);
  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 = ARRAY_SIZE(bss_infos);
  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;
  1371. res = os_calloc(ARRAY_SIZE(wlantest_cli_commands), sizeof(char *));
  1372. if (res == NULL)
  1373. return NULL;
  1374. for (i = 0; wlantest_cli_commands[i].cmd; i++) {
  1375. res[i] = os_strdup(wlantest_cli_commands[i].cmd);
  1376. if (res[i] == NULL)
  1377. break;
  1378. }
  1379. return res;
  1380. }
  1381. static char ** wlantest_cli_cmd_completion(struct wlantest_cli *cli,
  1382. const char *cmd, const char *str,
  1383. int pos)
  1384. {
  1385. int i;
  1386. for (i = 0; wlantest_cli_commands[i].cmd; i++) {
  1387. const struct wlantest_cli_cmd *c = &wlantest_cli_commands[i];
  1388. if (os_strcasecmp(c->cmd, cmd) == 0) {
  1389. edit_clear_line();
  1390. printf("\r%s\n", c->usage);
  1391. edit_redraw();
  1392. if (c->complete)
  1393. return c->complete(cli->s, str, pos);
  1394. break;
  1395. }
  1396. }
  1397. return NULL;
  1398. }
  1399. static char ** wlantest_cli_edit_completion_cb(void *ctx, const char *str,
  1400. int pos)
  1401. {
  1402. struct wlantest_cli *cli = ctx;
  1403. char **res;
  1404. const char *end;
  1405. char *cmd;
  1406. end = os_strchr(str, ' ');
  1407. if (end == NULL || str + pos < end)
  1408. return wlantest_cli_cmd_list();
  1409. cmd = os_malloc(pos + 1);
  1410. if (cmd == NULL)
  1411. return NULL;
  1412. os_memcpy(cmd, str, pos);
  1413. cmd[end - str] = '\0';
  1414. res = wlantest_cli_cmd_completion(cli, cmd, str, pos);
  1415. os_free(cmd);
  1416. return res;
  1417. }
  1418. static void wlantest_cli_interactive(int s)
  1419. {
  1420. struct wlantest_cli cli;
  1421. char *home, *hfile = NULL;
  1422. if (eloop_init())
  1423. return;
  1424. home = getenv("HOME");
  1425. if (home) {
  1426. const char *fname = ".wlantest_cli_history";
  1427. int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
  1428. hfile = os_malloc(hfile_len);
  1429. if (hfile)
  1430. os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
  1431. }
  1432. cli.s = s;
  1433. eloop_register_signal_terminate(wlantest_cli_eloop_terminate, &cli);
  1434. edit_init(wlantest_cli_edit_cmd_cb, wlantest_cli_edit_eof_cb,
  1435. wlantest_cli_edit_completion_cb, &cli, hfile, NULL);
  1436. eloop_run();
  1437. edit_deinit(hfile, NULL);
  1438. os_free(hfile);
  1439. eloop_destroy();
  1440. }
  1441. int main(int argc, char *argv[])
  1442. {
  1443. int s;
  1444. struct sockaddr_un addr;
  1445. int ret = 0;
  1446. if (os_program_init())
  1447. return -1;
  1448. s = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  1449. if (s < 0) {
  1450. perror("socket");
  1451. return -1;
  1452. }
  1453. os_memset(&addr, 0, sizeof(addr));
  1454. addr.sun_family = AF_UNIX;
  1455. os_strlcpy(addr.sun_path + 1, WLANTEST_SOCK_NAME,
  1456. sizeof(addr.sun_path) - 1);
  1457. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1458. perror("connect");
  1459. close(s);
  1460. return -1;
  1461. }
  1462. if (argc > 1) {
  1463. ret = ctrl_command(s, argc - 1, &argv[1]);
  1464. if (ret < 0)
  1465. printf("FAIL\n");
  1466. } else {
  1467. wlantest_cli_interactive(s);
  1468. }
  1469. close(s);
  1470. os_program_deinit();
  1471. return ret;
  1472. }