wlantest_cli.c 38 KB

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