wlantest_cli.c 32 KB

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