wlantest_cli.c 37 KB

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