wlantest_cli.c 38 KB

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