wlantest_cli.c 37 KB

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