wlantest_cli.c 41 KB

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