wlantest_cli.c 41 KB

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