hostapd_cli.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058
  1. /*
  2. * hostapd - command line interface for hostapd daemon
  3. * Copyright (c) 2004-2017, 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 "includes.h"
  9. #include <dirent.h>
  10. #include "common/wpa_ctrl.h"
  11. #include "common/ieee802_11_defs.h"
  12. #include "utils/common.h"
  13. #include "utils/eloop.h"
  14. #include "utils/edit.h"
  15. #include "common/version.h"
  16. #include "common/cli.h"
  17. #ifndef CONFIG_NO_CTRL_IFACE
  18. static const char *const hostapd_cli_version =
  19. "hostapd_cli v" VERSION_STR "\n"
  20. "Copyright (c) 2004-2017, Jouni Malinen <j@w1.fi> and contributors";
  21. static struct wpa_ctrl *ctrl_conn;
  22. static int hostapd_cli_quit = 0;
  23. static int hostapd_cli_attached = 0;
  24. #ifndef CONFIG_CTRL_IFACE_DIR
  25. #define CONFIG_CTRL_IFACE_DIR "/var/run/hostapd"
  26. #endif /* CONFIG_CTRL_IFACE_DIR */
  27. static const char *ctrl_iface_dir = CONFIG_CTRL_IFACE_DIR;
  28. static const char *client_socket_dir = NULL;
  29. static char *ctrl_ifname = NULL;
  30. static const char *pid_file = NULL;
  31. static const char *action_file = NULL;
  32. static int ping_interval = 5;
  33. static int interactive = 0;
  34. static int event_handler_registered = 0;
  35. static DEFINE_DL_LIST(stations); /* struct cli_txt_entry */
  36. static void print_help(FILE *stream, const char *cmd);
  37. static char ** list_cmd_list(void);
  38. static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx);
  39. static void update_stations(struct wpa_ctrl *ctrl);
  40. static void cli_event(const char *str);
  41. static void usage(void)
  42. {
  43. fprintf(stderr, "%s\n", hostapd_cli_version);
  44. fprintf(stderr,
  45. "\n"
  46. "usage: hostapd_cli [-p<path>] [-i<ifname>] [-hvB] "
  47. "[-a<path>] \\\n"
  48. " [-P<pid file>] [-G<ping interval>] [command..]\n"
  49. "\n"
  50. "Options:\n"
  51. " -h help (show this usage text)\n"
  52. " -v shown version information\n"
  53. " -p<path> path to find control sockets (default: "
  54. "/var/run/hostapd)\n"
  55. " -s<dir_path> dir path to open client sockets (default: "
  56. CONFIG_CTRL_IFACE_DIR ")\n"
  57. " -a<file> run in daemon mode executing the action file "
  58. "based on events\n"
  59. " from hostapd\n"
  60. " -B run a daemon in the background\n"
  61. " -i<ifname> Interface to listen on (default: first "
  62. "interface found in the\n"
  63. " socket path)\n\n");
  64. print_help(stderr, NULL);
  65. }
  66. static void register_event_handler(struct wpa_ctrl *ctrl)
  67. {
  68. if (!ctrl_conn)
  69. return;
  70. if (interactive) {
  71. event_handler_registered =
  72. !eloop_register_read_sock(wpa_ctrl_get_fd(ctrl),
  73. hostapd_cli_receive,
  74. NULL, NULL);
  75. }
  76. }
  77. static void unregister_event_handler(struct wpa_ctrl *ctrl)
  78. {
  79. if (!ctrl_conn)
  80. return;
  81. if (interactive && event_handler_registered) {
  82. eloop_unregister_read_sock(wpa_ctrl_get_fd(ctrl));
  83. event_handler_registered = 0;
  84. }
  85. }
  86. static struct wpa_ctrl * hostapd_cli_open_connection(const char *ifname)
  87. {
  88. #ifndef CONFIG_CTRL_IFACE_UDP
  89. char *cfile;
  90. int flen;
  91. #endif /* !CONFIG_CTRL_IFACE_UDP */
  92. if (ifname == NULL)
  93. return NULL;
  94. #ifdef CONFIG_CTRL_IFACE_UDP
  95. ctrl_conn = wpa_ctrl_open(ifname);
  96. return ctrl_conn;
  97. #else /* CONFIG_CTRL_IFACE_UDP */
  98. flen = strlen(ctrl_iface_dir) + strlen(ifname) + 2;
  99. cfile = malloc(flen);
  100. if (cfile == NULL)
  101. return NULL;
  102. snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname);
  103. if (client_socket_dir && client_socket_dir[0] &&
  104. access(client_socket_dir, F_OK) < 0) {
  105. perror(client_socket_dir);
  106. free(cfile);
  107. return NULL;
  108. }
  109. ctrl_conn = wpa_ctrl_open2(cfile, client_socket_dir);
  110. free(cfile);
  111. return ctrl_conn;
  112. #endif /* CONFIG_CTRL_IFACE_UDP */
  113. }
  114. static void hostapd_cli_close_connection(void)
  115. {
  116. if (ctrl_conn == NULL)
  117. return;
  118. unregister_event_handler(ctrl_conn);
  119. if (hostapd_cli_attached) {
  120. wpa_ctrl_detach(ctrl_conn);
  121. hostapd_cli_attached = 0;
  122. }
  123. wpa_ctrl_close(ctrl_conn);
  124. ctrl_conn = NULL;
  125. }
  126. static int hostapd_cli_reconnect(const char *ifname)
  127. {
  128. char *next_ctrl_ifname;
  129. hostapd_cli_close_connection();
  130. if (!ifname)
  131. return -1;
  132. next_ctrl_ifname = os_strdup(ifname);
  133. os_free(ctrl_ifname);
  134. ctrl_ifname = next_ctrl_ifname;
  135. if (!ctrl_ifname)
  136. return -1;
  137. ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);
  138. if (!ctrl_conn)
  139. return -1;
  140. if (!interactive && !action_file)
  141. return 0;
  142. if (wpa_ctrl_attach(ctrl_conn) == 0) {
  143. hostapd_cli_attached = 1;
  144. register_event_handler(ctrl_conn);
  145. update_stations(ctrl_conn);
  146. } else {
  147. printf("Warning: Failed to attach to hostapd.\n");
  148. }
  149. return 0;
  150. }
  151. static void hostapd_cli_msg_cb(char *msg, size_t len)
  152. {
  153. cli_event(msg);
  154. printf("%s\n", msg);
  155. }
  156. static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd, int print)
  157. {
  158. char buf[4096];
  159. size_t len;
  160. int ret;
  161. if (ctrl_conn == NULL) {
  162. printf("Not connected to hostapd - command dropped.\n");
  163. return -1;
  164. }
  165. len = sizeof(buf) - 1;
  166. ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
  167. hostapd_cli_msg_cb);
  168. if (ret == -2) {
  169. printf("'%s' command timed out.\n", cmd);
  170. return -2;
  171. } else if (ret < 0) {
  172. printf("'%s' command failed.\n", cmd);
  173. return -1;
  174. }
  175. if (print) {
  176. buf[len] = '\0';
  177. printf("%s", buf);
  178. }
  179. return 0;
  180. }
  181. static inline int wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd)
  182. {
  183. return _wpa_ctrl_command(ctrl, cmd, 1);
  184. }
  185. static int hostapd_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd,
  186. int min_args, int argc, char *argv[])
  187. {
  188. char buf[4096];
  189. if (argc < min_args) {
  190. printf("Invalid %s command - at least %d argument%s required.\n",
  191. cmd, min_args, min_args > 1 ? "s are" : " is");
  192. return -1;
  193. }
  194. if (write_cmd(buf, sizeof(buf), cmd, argc, argv) < 0)
  195. return -1;
  196. return wpa_ctrl_command(ctrl, buf);
  197. }
  198. static int hostapd_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[])
  199. {
  200. return wpa_ctrl_command(ctrl, "PING");
  201. }
  202. static int hostapd_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[])
  203. {
  204. return wpa_ctrl_command(ctrl, "RELOG");
  205. }
  206. static int hostapd_cli_cmd_status(struct wpa_ctrl *ctrl, int argc, char *argv[])
  207. {
  208. if (argc > 0 && os_strcmp(argv[0], "driver") == 0)
  209. return wpa_ctrl_command(ctrl, "STATUS-DRIVER");
  210. return wpa_ctrl_command(ctrl, "STATUS");
  211. }
  212. static int hostapd_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[])
  213. {
  214. if (argc > 0) {
  215. char buf[100];
  216. os_snprintf(buf, sizeof(buf), "MIB %s", argv[0]);
  217. return wpa_ctrl_command(ctrl, buf);
  218. }
  219. return wpa_ctrl_command(ctrl, "MIB");
  220. }
  221. static int hostapd_cli_exec(const char *program, const char *arg1,
  222. const char *arg2)
  223. {
  224. char *arg;
  225. size_t len;
  226. int res;
  227. len = os_strlen(arg1) + os_strlen(arg2) + 2;
  228. arg = os_malloc(len);
  229. if (arg == NULL)
  230. return -1;
  231. os_snprintf(arg, len, "%s %s", arg1, arg2);
  232. res = os_exec(program, arg, 1);
  233. os_free(arg);
  234. return res;
  235. }
  236. static void hostapd_cli_action_process(char *msg, size_t len)
  237. {
  238. const char *pos;
  239. pos = msg;
  240. if (*pos == '<') {
  241. pos = os_strchr(pos, '>');
  242. if (pos)
  243. pos++;
  244. else
  245. pos = msg;
  246. }
  247. hostapd_cli_exec(action_file, ctrl_ifname, pos);
  248. }
  249. static int hostapd_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
  250. {
  251. char buf[64];
  252. if (argc < 1) {
  253. printf("Invalid 'sta' command - at least one argument, STA "
  254. "address, is required.\n");
  255. return -1;
  256. }
  257. if (argc > 1)
  258. snprintf(buf, sizeof(buf), "STA %s %s", argv[0], argv[1]);
  259. else
  260. snprintf(buf, sizeof(buf), "STA %s", argv[0]);
  261. return wpa_ctrl_command(ctrl, buf);
  262. }
  263. static char ** hostapd_complete_stations(const char *str, int pos)
  264. {
  265. int arg = get_cmd_arg_num(str, pos);
  266. char **res = NULL;
  267. switch (arg) {
  268. case 1:
  269. res = cli_txt_list_array(&stations);
  270. break;
  271. }
  272. return res;
  273. }
  274. static int hostapd_cli_cmd_new_sta(struct wpa_ctrl *ctrl, int argc,
  275. char *argv[])
  276. {
  277. char buf[64];
  278. if (argc != 1) {
  279. printf("Invalid 'new_sta' command - exactly one argument, STA "
  280. "address, is required.\n");
  281. return -1;
  282. }
  283. snprintf(buf, sizeof(buf), "NEW_STA %s", argv[0]);
  284. return wpa_ctrl_command(ctrl, buf);
  285. }
  286. static int hostapd_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc,
  287. char *argv[])
  288. {
  289. char buf[64];
  290. if (argc < 1) {
  291. printf("Invalid 'deauthenticate' command - exactly one "
  292. "argument, STA address, is required.\n");
  293. return -1;
  294. }
  295. if (argc > 1)
  296. os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s %s",
  297. argv[0], argv[1]);
  298. else
  299. os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s", argv[0]);
  300. return wpa_ctrl_command(ctrl, buf);
  301. }
  302. static int hostapd_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
  303. char *argv[])
  304. {
  305. char buf[64];
  306. if (argc < 1) {
  307. printf("Invalid 'disassociate' command - exactly one "
  308. "argument, STA address, is required.\n");
  309. return -1;
  310. }
  311. if (argc > 1)
  312. os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s %s",
  313. argv[0], argv[1]);
  314. else
  315. os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s", argv[0]);
  316. return wpa_ctrl_command(ctrl, buf);
  317. }
  318. #ifdef CONFIG_TAXONOMY
  319. static int hostapd_cli_cmd_signature(struct wpa_ctrl *ctrl, int argc,
  320. char *argv[])
  321. {
  322. char buf[64];
  323. if (argc != 1) {
  324. printf("Invalid 'signature' command - exactly one argument, STA address, is required.\n");
  325. return -1;
  326. }
  327. os_snprintf(buf, sizeof(buf), "SIGNATURE %s", argv[0]);
  328. return wpa_ctrl_command(ctrl, buf);
  329. }
  330. #endif /* CONFIG_TAXONOMY */
  331. #ifdef CONFIG_IEEE80211W
  332. static int hostapd_cli_cmd_sa_query(struct wpa_ctrl *ctrl, int argc,
  333. char *argv[])
  334. {
  335. char buf[64];
  336. if (argc != 1) {
  337. printf("Invalid 'sa_query' command - exactly one argument, "
  338. "STA address, is required.\n");
  339. return -1;
  340. }
  341. snprintf(buf, sizeof(buf), "SA_QUERY %s", argv[0]);
  342. return wpa_ctrl_command(ctrl, buf);
  343. }
  344. #endif /* CONFIG_IEEE80211W */
  345. #ifdef CONFIG_WPS
  346. static int hostapd_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc,
  347. char *argv[])
  348. {
  349. char buf[256];
  350. if (argc < 2) {
  351. printf("Invalid 'wps_pin' command - at least two arguments, "
  352. "UUID and PIN, are required.\n");
  353. return -1;
  354. }
  355. if (argc > 3)
  356. snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s %s",
  357. argv[0], argv[1], argv[2], argv[3]);
  358. else if (argc > 2)
  359. snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s",
  360. argv[0], argv[1], argv[2]);
  361. else
  362. snprintf(buf, sizeof(buf), "WPS_PIN %s %s", argv[0], argv[1]);
  363. return wpa_ctrl_command(ctrl, buf);
  364. }
  365. static int hostapd_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc,
  366. char *argv[])
  367. {
  368. char cmd[256];
  369. int res;
  370. if (argc != 1 && argc != 2) {
  371. printf("Invalid WPS_CHECK_PIN command: needs one argument:\n"
  372. "- PIN to be verified\n");
  373. return -1;
  374. }
  375. if (argc == 2)
  376. res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s %s",
  377. argv[0], argv[1]);
  378. else
  379. res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s",
  380. argv[0]);
  381. if (os_snprintf_error(sizeof(cmd), res)) {
  382. printf("Too long WPS_CHECK_PIN command.\n");
  383. return -1;
  384. }
  385. return wpa_ctrl_command(ctrl, cmd);
  386. }
  387. static int hostapd_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc,
  388. char *argv[])
  389. {
  390. return wpa_ctrl_command(ctrl, "WPS_PBC");
  391. }
  392. static int hostapd_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc,
  393. char *argv[])
  394. {
  395. return wpa_ctrl_command(ctrl, "WPS_CANCEL");
  396. }
  397. #ifdef CONFIG_WPS_NFC
  398. static int hostapd_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl *ctrl, int argc,
  399. char *argv[])
  400. {
  401. int ret;
  402. char *buf;
  403. size_t buflen;
  404. if (argc != 1) {
  405. printf("Invalid 'wps_nfc_tag_read' command - one argument "
  406. "is required.\n");
  407. return -1;
  408. }
  409. buflen = 18 + os_strlen(argv[0]);
  410. buf = os_malloc(buflen);
  411. if (buf == NULL)
  412. return -1;
  413. os_snprintf(buf, buflen, "WPS_NFC_TAG_READ %s", argv[0]);
  414. ret = wpa_ctrl_command(ctrl, buf);
  415. os_free(buf);
  416. return ret;
  417. }
  418. static int hostapd_cli_cmd_wps_nfc_config_token(struct wpa_ctrl *ctrl,
  419. int argc, char *argv[])
  420. {
  421. char cmd[64];
  422. int res;
  423. if (argc != 1) {
  424. printf("Invalid 'wps_nfc_config_token' command - one argument "
  425. "is required.\n");
  426. return -1;
  427. }
  428. res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_CONFIG_TOKEN %s",
  429. argv[0]);
  430. if (os_snprintf_error(sizeof(cmd), res)) {
  431. printf("Too long WPS_NFC_CONFIG_TOKEN command.\n");
  432. return -1;
  433. }
  434. return wpa_ctrl_command(ctrl, cmd);
  435. }
  436. static int hostapd_cli_cmd_wps_nfc_token(struct wpa_ctrl *ctrl,
  437. int argc, char *argv[])
  438. {
  439. char cmd[64];
  440. int res;
  441. if (argc != 1) {
  442. printf("Invalid 'wps_nfc_token' command - one argument is "
  443. "required.\n");
  444. return -1;
  445. }
  446. res = os_snprintf(cmd, sizeof(cmd), "WPS_NFC_TOKEN %s", argv[0]);
  447. if (os_snprintf_error(sizeof(cmd), res)) {
  448. printf("Too long WPS_NFC_TOKEN command.\n");
  449. return -1;
  450. }
  451. return wpa_ctrl_command(ctrl, cmd);
  452. }
  453. static int hostapd_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl *ctrl,
  454. int argc, char *argv[])
  455. {
  456. char cmd[64];
  457. int res;
  458. if (argc != 2) {
  459. printf("Invalid 'nfc_get_handover_sel' command - two arguments "
  460. "are required.\n");
  461. return -1;
  462. }
  463. res = os_snprintf(cmd, sizeof(cmd), "NFC_GET_HANDOVER_SEL %s %s",
  464. argv[0], argv[1]);
  465. if (os_snprintf_error(sizeof(cmd), res)) {
  466. printf("Too long NFC_GET_HANDOVER_SEL command.\n");
  467. return -1;
  468. }
  469. return wpa_ctrl_command(ctrl, cmd);
  470. }
  471. #endif /* CONFIG_WPS_NFC */
  472. static int hostapd_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc,
  473. char *argv[])
  474. {
  475. char buf[64];
  476. if (argc < 1) {
  477. printf("Invalid 'wps_ap_pin' command - at least one argument "
  478. "is required.\n");
  479. return -1;
  480. }
  481. if (argc > 2)
  482. snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s %s",
  483. argv[0], argv[1], argv[2]);
  484. else if (argc > 1)
  485. snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s",
  486. argv[0], argv[1]);
  487. else
  488. snprintf(buf, sizeof(buf), "WPS_AP_PIN %s", argv[0]);
  489. return wpa_ctrl_command(ctrl, buf);
  490. }
  491. static int hostapd_cli_cmd_wps_get_status(struct wpa_ctrl *ctrl, int argc,
  492. char *argv[])
  493. {
  494. return wpa_ctrl_command(ctrl, "WPS_GET_STATUS");
  495. }
  496. static int hostapd_cli_cmd_wps_config(struct wpa_ctrl *ctrl, int argc,
  497. char *argv[])
  498. {
  499. char buf[256];
  500. char ssid_hex[2 * SSID_MAX_LEN + 1];
  501. char key_hex[2 * 64 + 1];
  502. int i;
  503. if (argc < 1) {
  504. printf("Invalid 'wps_config' command - at least two arguments "
  505. "are required.\n");
  506. return -1;
  507. }
  508. ssid_hex[0] = '\0';
  509. for (i = 0; i < SSID_MAX_LEN; i++) {
  510. if (argv[0][i] == '\0')
  511. break;
  512. os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[0][i]);
  513. }
  514. key_hex[0] = '\0';
  515. if (argc > 3) {
  516. for (i = 0; i < 64; i++) {
  517. if (argv[3][i] == '\0')
  518. break;
  519. os_snprintf(&key_hex[i * 2], 3, "%02x",
  520. argv[3][i]);
  521. }
  522. }
  523. if (argc > 3)
  524. snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s %s",
  525. ssid_hex, argv[1], argv[2], key_hex);
  526. else if (argc > 2)
  527. snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s",
  528. ssid_hex, argv[1], argv[2]);
  529. else
  530. snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s",
  531. ssid_hex, argv[1]);
  532. return wpa_ctrl_command(ctrl, buf);
  533. }
  534. #endif /* CONFIG_WPS */
  535. static int hostapd_cli_cmd_disassoc_imminent(struct wpa_ctrl *ctrl, int argc,
  536. char *argv[])
  537. {
  538. char buf[300];
  539. int res;
  540. if (argc < 2) {
  541. printf("Invalid 'disassoc_imminent' command - two arguments "
  542. "(STA addr and Disassociation Timer) are needed\n");
  543. return -1;
  544. }
  545. res = os_snprintf(buf, sizeof(buf), "DISASSOC_IMMINENT %s %s",
  546. argv[0], argv[1]);
  547. if (os_snprintf_error(sizeof(buf), res))
  548. return -1;
  549. return wpa_ctrl_command(ctrl, buf);
  550. }
  551. static int hostapd_cli_cmd_ess_disassoc(struct wpa_ctrl *ctrl, int argc,
  552. char *argv[])
  553. {
  554. char buf[300];
  555. int res;
  556. if (argc < 3) {
  557. printf("Invalid 'ess_disassoc' command - three arguments (STA "
  558. "addr, disassoc timer, and URL) are needed\n");
  559. return -1;
  560. }
  561. res = os_snprintf(buf, sizeof(buf), "ESS_DISASSOC %s %s %s",
  562. argv[0], argv[1], argv[2]);
  563. if (os_snprintf_error(sizeof(buf), res))
  564. return -1;
  565. return wpa_ctrl_command(ctrl, buf);
  566. }
  567. static int hostapd_cli_cmd_bss_tm_req(struct wpa_ctrl *ctrl, int argc,
  568. char *argv[])
  569. {
  570. char buf[2000], *tmp;
  571. int res, i, total;
  572. if (argc < 1) {
  573. printf("Invalid 'bss_tm_req' command - at least one argument (STA addr) is needed\n");
  574. return -1;
  575. }
  576. res = os_snprintf(buf, sizeof(buf), "BSS_TM_REQ %s", argv[0]);
  577. if (os_snprintf_error(sizeof(buf), res))
  578. return -1;
  579. total = res;
  580. for (i = 1; i < argc; i++) {
  581. tmp = &buf[total];
  582. res = os_snprintf(tmp, sizeof(buf) - total, " %s", argv[i]);
  583. if (os_snprintf_error(sizeof(buf) - total, res))
  584. return -1;
  585. total += res;
  586. }
  587. return wpa_ctrl_command(ctrl, buf);
  588. }
  589. static int hostapd_cli_cmd_get_config(struct wpa_ctrl *ctrl, int argc,
  590. char *argv[])
  591. {
  592. return wpa_ctrl_command(ctrl, "GET_CONFIG");
  593. }
  594. static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, const char *cmd,
  595. char *addr, size_t addr_len, int print)
  596. {
  597. char buf[4096], *pos;
  598. size_t len;
  599. int ret;
  600. if (ctrl_conn == NULL) {
  601. printf("Not connected to hostapd - command dropped.\n");
  602. return -1;
  603. }
  604. len = sizeof(buf) - 1;
  605. ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
  606. hostapd_cli_msg_cb);
  607. if (ret == -2) {
  608. printf("'%s' command timed out.\n", cmd);
  609. return -2;
  610. } else if (ret < 0) {
  611. printf("'%s' command failed.\n", cmd);
  612. return -1;
  613. }
  614. buf[len] = '\0';
  615. if (memcmp(buf, "FAIL", 4) == 0)
  616. return -1;
  617. if (print)
  618. printf("%s", buf);
  619. pos = buf;
  620. while (*pos != '\0' && *pos != '\n')
  621. pos++;
  622. *pos = '\0';
  623. os_strlcpy(addr, buf, addr_len);
  624. return 0;
  625. }
  626. static int hostapd_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc,
  627. char *argv[])
  628. {
  629. char addr[32], cmd[64];
  630. if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 1))
  631. return 0;
  632. do {
  633. snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
  634. } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 1) == 0);
  635. return -1;
  636. }
  637. static int hostapd_cli_cmd_list_sta(struct wpa_ctrl *ctrl, int argc,
  638. char *argv[])
  639. {
  640. char addr[32], cmd[64];
  641. if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0))
  642. return 0;
  643. do {
  644. if (os_strcmp(addr, "") != 0)
  645. printf("%s\n", addr);
  646. os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
  647. } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0);
  648. return 0;
  649. }
  650. static int hostapd_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
  651. {
  652. print_help(stdout, argc > 0 ? argv[0] : NULL);
  653. return 0;
  654. }
  655. static char ** hostapd_cli_complete_help(const char *str, int pos)
  656. {
  657. int arg = get_cmd_arg_num(str, pos);
  658. char **res = NULL;
  659. switch (arg) {
  660. case 1:
  661. res = list_cmd_list();
  662. break;
  663. }
  664. return res;
  665. }
  666. static int hostapd_cli_cmd_license(struct wpa_ctrl *ctrl, int argc,
  667. char *argv[])
  668. {
  669. printf("%s\n\n%s\n", hostapd_cli_version, cli_full_license);
  670. return 0;
  671. }
  672. static int hostapd_cli_cmd_set_qos_map_set(struct wpa_ctrl *ctrl,
  673. int argc, char *argv[])
  674. {
  675. char buf[200];
  676. int res;
  677. if (argc != 1) {
  678. printf("Invalid 'set_qos_map_set' command - "
  679. "one argument (comma delimited QoS map set) "
  680. "is needed\n");
  681. return -1;
  682. }
  683. res = os_snprintf(buf, sizeof(buf), "SET_QOS_MAP_SET %s", argv[0]);
  684. if (os_snprintf_error(sizeof(buf), res))
  685. return -1;
  686. return wpa_ctrl_command(ctrl, buf);
  687. }
  688. static int hostapd_cli_cmd_send_qos_map_conf(struct wpa_ctrl *ctrl,
  689. int argc, char *argv[])
  690. {
  691. char buf[50];
  692. int res;
  693. if (argc != 1) {
  694. printf("Invalid 'send_qos_map_conf' command - "
  695. "one argument (STA addr) is needed\n");
  696. return -1;
  697. }
  698. res = os_snprintf(buf, sizeof(buf), "SEND_QOS_MAP_CONF %s", argv[0]);
  699. if (os_snprintf_error(sizeof(buf), res))
  700. return -1;
  701. return wpa_ctrl_command(ctrl, buf);
  702. }
  703. static int hostapd_cli_cmd_hs20_wnm_notif(struct wpa_ctrl *ctrl, int argc,
  704. char *argv[])
  705. {
  706. char buf[300];
  707. int res;
  708. if (argc < 2) {
  709. printf("Invalid 'hs20_wnm_notif' command - two arguments (STA "
  710. "addr and URL) are needed\n");
  711. return -1;
  712. }
  713. res = os_snprintf(buf, sizeof(buf), "HS20_WNM_NOTIF %s %s",
  714. argv[0], argv[1]);
  715. if (os_snprintf_error(sizeof(buf), res))
  716. return -1;
  717. return wpa_ctrl_command(ctrl, buf);
  718. }
  719. static int hostapd_cli_cmd_hs20_deauth_req(struct wpa_ctrl *ctrl, int argc,
  720. char *argv[])
  721. {
  722. char buf[300];
  723. int res;
  724. if (argc < 3) {
  725. printf("Invalid 'hs20_deauth_req' command - at least three arguments (STA addr, Code, Re-auth Delay) are needed\n");
  726. return -1;
  727. }
  728. if (argc > 3)
  729. res = os_snprintf(buf, sizeof(buf),
  730. "HS20_DEAUTH_REQ %s %s %s %s",
  731. argv[0], argv[1], argv[2], argv[3]);
  732. else
  733. res = os_snprintf(buf, sizeof(buf),
  734. "HS20_DEAUTH_REQ %s %s %s",
  735. argv[0], argv[1], argv[2]);
  736. if (os_snprintf_error(sizeof(buf), res))
  737. return -1;
  738. return wpa_ctrl_command(ctrl, buf);
  739. }
  740. static int hostapd_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
  741. {
  742. hostapd_cli_quit = 1;
  743. if (interactive)
  744. eloop_terminate();
  745. return 0;
  746. }
  747. static int hostapd_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
  748. {
  749. char cmd[256];
  750. if (argc != 1) {
  751. printf("Invalid LEVEL command: needs one argument (debug "
  752. "level)\n");
  753. return 0;
  754. }
  755. snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]);
  756. return wpa_ctrl_command(ctrl, cmd);
  757. }
  758. static void update_stations(struct wpa_ctrl *ctrl)
  759. {
  760. char addr[32], cmd[64];
  761. if (!ctrl || !interactive)
  762. return;
  763. cli_txt_list_flush(&stations);
  764. if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0))
  765. return;
  766. do {
  767. if (os_strcmp(addr, "") != 0)
  768. cli_txt_list_add(&stations, addr);
  769. os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
  770. } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0);
  771. }
  772. static void hostapd_cli_get_interfaces(struct wpa_ctrl *ctrl,
  773. struct dl_list *interfaces)
  774. {
  775. struct dirent *dent;
  776. DIR *dir;
  777. if (!ctrl || !interfaces)
  778. return;
  779. dir = opendir(ctrl_iface_dir);
  780. if (dir == NULL)
  781. return;
  782. while ((dent = readdir(dir))) {
  783. if (strcmp(dent->d_name, ".") == 0 ||
  784. strcmp(dent->d_name, "..") == 0)
  785. continue;
  786. cli_txt_list_add(interfaces, dent->d_name);
  787. }
  788. closedir(dir);
  789. }
  790. static void hostapd_cli_list_interfaces(struct wpa_ctrl *ctrl)
  791. {
  792. struct dirent *dent;
  793. DIR *dir;
  794. dir = opendir(ctrl_iface_dir);
  795. if (dir == NULL) {
  796. printf("Control interface directory '%s' could not be "
  797. "openned.\n", ctrl_iface_dir);
  798. return;
  799. }
  800. printf("Available interfaces:\n");
  801. while ((dent = readdir(dir))) {
  802. if (strcmp(dent->d_name, ".") == 0 ||
  803. strcmp(dent->d_name, "..") == 0)
  804. continue;
  805. printf("%s\n", dent->d_name);
  806. }
  807. closedir(dir);
  808. }
  809. static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc,
  810. char *argv[])
  811. {
  812. if (argc < 1) {
  813. hostapd_cli_list_interfaces(ctrl);
  814. return 0;
  815. }
  816. if (hostapd_cli_reconnect(argv[0]) != 0) {
  817. printf("Could not connect to interface '%s' - re-trying\n",
  818. ctrl_ifname);
  819. }
  820. return 0;
  821. }
  822. static char ** hostapd_complete_interface(const char *str, int pos)
  823. {
  824. int arg = get_cmd_arg_num(str, pos);
  825. char **res = NULL;
  826. DEFINE_DL_LIST(interfaces);
  827. switch (arg) {
  828. case 1:
  829. hostapd_cli_get_interfaces(ctrl_conn, &interfaces);
  830. res = cli_txt_list_array(&interfaces);
  831. cli_txt_list_flush(&interfaces);
  832. break;
  833. }
  834. return res;
  835. }
  836. static int hostapd_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
  837. {
  838. char cmd[2048];
  839. int res;
  840. if (argc != 2) {
  841. printf("Invalid SET command: needs two arguments (variable "
  842. "name and value)\n");
  843. return -1;
  844. }
  845. res = os_snprintf(cmd, sizeof(cmd), "SET %s %s", argv[0], argv[1]);
  846. if (os_snprintf_error(sizeof(cmd), res)) {
  847. printf("Too long SET command.\n");
  848. return -1;
  849. }
  850. return wpa_ctrl_command(ctrl, cmd);
  851. }
  852. static char ** hostapd_complete_set(const char *str, int pos)
  853. {
  854. int arg = get_cmd_arg_num(str, pos);
  855. const char *fields[] = {
  856. #ifdef CONFIG_WPS_TESTING
  857. "wps_version_number", "wps_testing_dummy_cred",
  858. "wps_corrupt_pkhash",
  859. #endif /* CONFIG_WPS_TESTING */
  860. #ifdef CONFIG_INTERWORKING
  861. "gas_frag_limit",
  862. #endif /* CONFIG_INTERWORKING */
  863. #ifdef CONFIG_TESTING_OPTIONS
  864. "ext_mgmt_frame_handling", "ext_eapol_frame_io",
  865. #endif /* CONFIG_TESTING_OPTIONS */
  866. #ifdef CONFIG_MBO
  867. "mbo_assoc_disallow",
  868. #endif /* CONFIG_MBO */
  869. "deny_mac_file", "accept_mac_file",
  870. };
  871. int i, num_fields = ARRAY_SIZE(fields);
  872. if (arg == 1) {
  873. char **res;
  874. res = os_calloc(num_fields + 1, sizeof(char *));
  875. if (!res)
  876. return NULL;
  877. for (i = 0; i < num_fields; i++) {
  878. res[i] = os_strdup(fields[i]);
  879. if (!res[i])
  880. return res;
  881. }
  882. return res;
  883. }
  884. return NULL;
  885. }
  886. static int hostapd_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
  887. {
  888. char cmd[256];
  889. int res;
  890. if (argc != 1) {
  891. printf("Invalid GET command: needs one argument (variable "
  892. "name)\n");
  893. return -1;
  894. }
  895. res = os_snprintf(cmd, sizeof(cmd), "GET %s", argv[0]);
  896. if (os_snprintf_error(sizeof(cmd), res)) {
  897. printf("Too long GET command.\n");
  898. return -1;
  899. }
  900. return wpa_ctrl_command(ctrl, cmd);
  901. }
  902. static char ** hostapd_complete_get(const char *str, int pos)
  903. {
  904. int arg = get_cmd_arg_num(str, pos);
  905. const char *fields[] = {
  906. "version", "tls_library",
  907. };
  908. int i, num_fields = ARRAY_SIZE(fields);
  909. if (arg == 1) {
  910. char **res;
  911. res = os_calloc(num_fields + 1, sizeof(char *));
  912. if (!res)
  913. return NULL;
  914. for (i = 0; i < num_fields; i++) {
  915. res[i] = os_strdup(fields[i]);
  916. if (!res[i])
  917. return res;
  918. }
  919. return res;
  920. }
  921. return NULL;
  922. }
  923. #ifdef CONFIG_FST
  924. static int hostapd_cli_cmd_fst(struct wpa_ctrl *ctrl, int argc, char *argv[])
  925. {
  926. char cmd[256];
  927. int res;
  928. int i;
  929. int total;
  930. if (argc <= 0) {
  931. printf("FST command: parameters are required.\n");
  932. return -1;
  933. }
  934. total = os_snprintf(cmd, sizeof(cmd), "FST-MANAGER");
  935. for (i = 0; i < argc; i++) {
  936. res = os_snprintf(cmd + total, sizeof(cmd) - total, " %s",
  937. argv[i]);
  938. if (os_snprintf_error(sizeof(cmd) - total, res)) {
  939. printf("Too long fst command.\n");
  940. return -1;
  941. }
  942. total += res;
  943. }
  944. return wpa_ctrl_command(ctrl, cmd);
  945. }
  946. #endif /* CONFIG_FST */
  947. static int hostapd_cli_cmd_chan_switch(struct wpa_ctrl *ctrl,
  948. int argc, char *argv[])
  949. {
  950. char cmd[256];
  951. int res;
  952. int i;
  953. char *tmp;
  954. int total;
  955. if (argc < 2) {
  956. printf("Invalid chan_switch command: needs at least two "
  957. "arguments (count and freq)\n"
  958. "usage: <cs_count> <freq> [sec_channel_offset=] "
  959. "[center_freq1=] [center_freq2=] [bandwidth=] "
  960. "[blocktx] [ht|vht]\n");
  961. return -1;
  962. }
  963. res = os_snprintf(cmd, sizeof(cmd), "CHAN_SWITCH %s %s",
  964. argv[0], argv[1]);
  965. if (os_snprintf_error(sizeof(cmd), res)) {
  966. printf("Too long CHAN_SWITCH command.\n");
  967. return -1;
  968. }
  969. total = res;
  970. for (i = 2; i < argc; i++) {
  971. tmp = cmd + total;
  972. res = os_snprintf(tmp, sizeof(cmd) - total, " %s", argv[i]);
  973. if (os_snprintf_error(sizeof(cmd) - total, res)) {
  974. printf("Too long CHAN_SWITCH command.\n");
  975. return -1;
  976. }
  977. total += res;
  978. }
  979. return wpa_ctrl_command(ctrl, cmd);
  980. }
  981. static int hostapd_cli_cmd_enable(struct wpa_ctrl *ctrl, int argc,
  982. char *argv[])
  983. {
  984. return wpa_ctrl_command(ctrl, "ENABLE");
  985. }
  986. static int hostapd_cli_cmd_reload(struct wpa_ctrl *ctrl, int argc,
  987. char *argv[])
  988. {
  989. return wpa_ctrl_command(ctrl, "RELOAD");
  990. }
  991. static int hostapd_cli_cmd_disable(struct wpa_ctrl *ctrl, int argc,
  992. char *argv[])
  993. {
  994. return wpa_ctrl_command(ctrl, "DISABLE");
  995. }
  996. static int hostapd_cli_cmd_vendor(struct wpa_ctrl *ctrl, int argc, char *argv[])
  997. {
  998. char cmd[256];
  999. int res;
  1000. if (argc < 2 || argc > 3) {
  1001. printf("Invalid vendor command\n"
  1002. "usage: <vendor id> <command id> [<hex formatted command argument>]\n");
  1003. return -1;
  1004. }
  1005. res = os_snprintf(cmd, sizeof(cmd), "VENDOR %s %s %s", argv[0], argv[1],
  1006. argc == 3 ? argv[2] : "");
  1007. if (os_snprintf_error(sizeof(cmd), res)) {
  1008. printf("Too long VENDOR command.\n");
  1009. return -1;
  1010. }
  1011. return wpa_ctrl_command(ctrl, cmd);
  1012. }
  1013. static int hostapd_cli_cmd_erp_flush(struct wpa_ctrl *ctrl, int argc,
  1014. char *argv[])
  1015. {
  1016. return wpa_ctrl_command(ctrl, "ERP_FLUSH");
  1017. }
  1018. static int hostapd_cli_cmd_log_level(struct wpa_ctrl *ctrl, int argc,
  1019. char *argv[])
  1020. {
  1021. char cmd[256];
  1022. int res;
  1023. res = os_snprintf(cmd, sizeof(cmd), "LOG_LEVEL%s%s%s%s",
  1024. argc >= 1 ? " " : "",
  1025. argc >= 1 ? argv[0] : "",
  1026. argc == 2 ? " " : "",
  1027. argc == 2 ? argv[1] : "");
  1028. if (os_snprintf_error(sizeof(cmd), res)) {
  1029. printf("Too long option\n");
  1030. return -1;
  1031. }
  1032. return wpa_ctrl_command(ctrl, cmd);
  1033. }
  1034. static int hostapd_cli_cmd_raw(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1035. {
  1036. if (argc == 0)
  1037. return -1;
  1038. return hostapd_cli_cmd(ctrl, argv[0], 0, argc - 1, &argv[1]);
  1039. }
  1040. static int hostapd_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1041. {
  1042. return wpa_ctrl_command(ctrl, "PMKSA");
  1043. }
  1044. static int hostapd_cli_cmd_pmksa_flush(struct wpa_ctrl *ctrl, int argc,
  1045. char *argv[])
  1046. {
  1047. return wpa_ctrl_command(ctrl, "PMKSA_FLUSH");
  1048. }
  1049. static int hostapd_cli_cmd_set_neighbor(struct wpa_ctrl *ctrl, int argc,
  1050. char *argv[])
  1051. {
  1052. char cmd[2048];
  1053. int res;
  1054. if (argc < 3 || argc > 6) {
  1055. printf("Invalid set_neighbor command: needs 3-6 arguments\n");
  1056. return -1;
  1057. }
  1058. res = os_snprintf(cmd, sizeof(cmd), "SET_NEIGHBOR %s %s %s %s %s %s",
  1059. argv[0], argv[1], argv[2], argc >= 4 ? argv[3] : "",
  1060. argc >= 5 ? argv[4] : "", argc == 6 ? argv[5] : "");
  1061. if (os_snprintf_error(sizeof(cmd), res)) {
  1062. printf("Too long SET_NEIGHBOR command.\n");
  1063. return -1;
  1064. }
  1065. return wpa_ctrl_command(ctrl, cmd);
  1066. }
  1067. static int hostapd_cli_cmd_remove_neighbor(struct wpa_ctrl *ctrl, int argc,
  1068. char *argv[])
  1069. {
  1070. char cmd[400];
  1071. int res;
  1072. if (argc != 2) {
  1073. printf("Invalid remove_neighbor command: needs 2 arguments\n");
  1074. return -1;
  1075. }
  1076. res = os_snprintf(cmd, sizeof(cmd), "REMOVE_NEIGHBOR %s %s",
  1077. argv[0], argv[1]);
  1078. if (os_snprintf_error(sizeof(cmd), res)) {
  1079. printf("Too long REMOVE_NEIGHBOR command.\n");
  1080. return -1;
  1081. }
  1082. return wpa_ctrl_command(ctrl, cmd);
  1083. }
  1084. static int hostapd_cli_cmd_req_lci(struct wpa_ctrl *ctrl, int argc,
  1085. char *argv[])
  1086. {
  1087. char cmd[256];
  1088. int res;
  1089. if (argc != 1) {
  1090. printf("Invalid req_lci command - requires destination address\n");
  1091. return -1;
  1092. }
  1093. res = os_snprintf(cmd, sizeof(cmd), "REQ_LCI %s", argv[0]);
  1094. if (os_snprintf_error(sizeof(cmd), res)) {
  1095. printf("Too long REQ_LCI command.\n");
  1096. return -1;
  1097. }
  1098. return wpa_ctrl_command(ctrl, cmd);
  1099. }
  1100. static int hostapd_cli_cmd_req_range(struct wpa_ctrl *ctrl, int argc,
  1101. char *argv[])
  1102. {
  1103. if (argc < 4) {
  1104. printf("Invalid req_range command: needs at least 4 arguments - dest address, randomization interval, min AP count, and 1 to 16 AP addresses\n");
  1105. return -1;
  1106. }
  1107. return hostapd_cli_cmd(ctrl, "REQ_RANGE", 4, argc, argv);
  1108. }
  1109. static int hostapd_cli_cmd_driver_flags(struct wpa_ctrl *ctrl, int argc,
  1110. char *argv[])
  1111. {
  1112. return wpa_ctrl_command(ctrl, "DRIVER_FLAGS");
  1113. }
  1114. #ifdef CONFIG_DPP
  1115. static int hostapd_cli_cmd_dpp_qr_code(struct wpa_ctrl *ctrl, int argc,
  1116. char *argv[])
  1117. {
  1118. return hostapd_cli_cmd(ctrl, "DPP_QR_CODE", 1, argc, argv);
  1119. }
  1120. static int hostapd_cli_cmd_dpp_bootstrap_gen(struct wpa_ctrl *ctrl, int argc,
  1121. char *argv[])
  1122. {
  1123. return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_GEN", 1, argc, argv);
  1124. }
  1125. static int hostapd_cli_cmd_dpp_bootstrap_remove(struct wpa_ctrl *ctrl, int argc,
  1126. char *argv[])
  1127. {
  1128. return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_REMOVE", 1, argc, argv);
  1129. }
  1130. static int hostapd_cli_cmd_dpp_bootstrap_get_uri(struct wpa_ctrl *ctrl,
  1131. int argc, char *argv[])
  1132. {
  1133. return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_GET_URI", 1, argc, argv);
  1134. }
  1135. static int hostapd_cli_cmd_dpp_bootstrap_info(struct wpa_ctrl *ctrl, int argc,
  1136. char *argv[])
  1137. {
  1138. return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_INFO", 1, argc, argv);
  1139. }
  1140. static int hostapd_cli_cmd_dpp_auth_init(struct wpa_ctrl *ctrl, int argc,
  1141. char *argv[])
  1142. {
  1143. return hostapd_cli_cmd(ctrl, "DPP_AUTH_INIT", 1, argc, argv);
  1144. }
  1145. static int hostapd_cli_cmd_dpp_listen(struct wpa_ctrl *ctrl, int argc,
  1146. char *argv[])
  1147. {
  1148. return hostapd_cli_cmd(ctrl, "DPP_LISTEN", 1, argc, argv);
  1149. }
  1150. static int hostapd_cli_cmd_dpp_stop_listen(struct wpa_ctrl *ctrl, int argc,
  1151. char *argv[])
  1152. {
  1153. return wpa_ctrl_command(ctrl, "DPP_STOP_LISTEN");
  1154. }
  1155. static int hostapd_cli_cmd_dpp_configurator_add(struct wpa_ctrl *ctrl, int argc,
  1156. char *argv[])
  1157. {
  1158. return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_ADD", 0, argc, argv);
  1159. }
  1160. static int hostapd_cli_cmd_dpp_configurator_remove(struct wpa_ctrl *ctrl,
  1161. int argc, char *argv[])
  1162. {
  1163. return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_REMOVE", 1, argc, argv);
  1164. }
  1165. static int hostapd_cli_cmd_dpp_pkex_add(struct wpa_ctrl *ctrl, int argc,
  1166. char *argv[])
  1167. {
  1168. return hostapd_cli_cmd(ctrl, "DPP_PKEX_ADD", 1, argc, argv);
  1169. }
  1170. static int hostapd_cli_cmd_dpp_pkex_remove(struct wpa_ctrl *ctrl, int argc,
  1171. char *argv[])
  1172. {
  1173. return hostapd_cli_cmd(ctrl, "DPP_PKEX_REMOVE", 1, argc, argv);
  1174. }
  1175. #endif /* CONFIG_DPP */
  1176. struct hostapd_cli_cmd {
  1177. const char *cmd;
  1178. int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
  1179. char ** (*completion)(const char *str, int pos);
  1180. const char *usage;
  1181. };
  1182. static const struct hostapd_cli_cmd hostapd_cli_commands[] = {
  1183. { "ping", hostapd_cli_cmd_ping, NULL,
  1184. "= pings hostapd" },
  1185. { "mib", hostapd_cli_cmd_mib, NULL,
  1186. "= get MIB variables (dot1x, dot11, radius)" },
  1187. { "relog", hostapd_cli_cmd_relog, NULL,
  1188. "= reload/truncate debug log output file" },
  1189. { "status", hostapd_cli_cmd_status, NULL,
  1190. "= show interface status info" },
  1191. { "sta", hostapd_cli_cmd_sta, hostapd_complete_stations,
  1192. "<addr> = get MIB variables for one station" },
  1193. { "all_sta", hostapd_cli_cmd_all_sta, NULL,
  1194. "= get MIB variables for all stations" },
  1195. { "list_sta", hostapd_cli_cmd_list_sta, NULL,
  1196. "= list all stations" },
  1197. { "new_sta", hostapd_cli_cmd_new_sta, NULL,
  1198. "<addr> = add a new station" },
  1199. { "deauthenticate", hostapd_cli_cmd_deauthenticate,
  1200. hostapd_complete_stations,
  1201. "<addr> = deauthenticate a station" },
  1202. { "disassociate", hostapd_cli_cmd_disassociate,
  1203. hostapd_complete_stations,
  1204. "<addr> = disassociate a station" },
  1205. #ifdef CONFIG_TAXONOMY
  1206. { "signature", hostapd_cli_cmd_signature, hostapd_complete_stations,
  1207. "<addr> = get taxonomy signature for a station" },
  1208. #endif /* CONFIG_TAXONOMY */
  1209. #ifdef CONFIG_IEEE80211W
  1210. { "sa_query", hostapd_cli_cmd_sa_query, hostapd_complete_stations,
  1211. "<addr> = send SA Query to a station" },
  1212. #endif /* CONFIG_IEEE80211W */
  1213. #ifdef CONFIG_WPS
  1214. { "wps_pin", hostapd_cli_cmd_wps_pin, NULL,
  1215. "<uuid> <pin> [timeout] [addr] = add WPS Enrollee PIN" },
  1216. { "wps_check_pin", hostapd_cli_cmd_wps_check_pin, NULL,
  1217. "<PIN> = verify PIN checksum" },
  1218. { "wps_pbc", hostapd_cli_cmd_wps_pbc, NULL,
  1219. "= indicate button pushed to initiate PBC" },
  1220. { "wps_cancel", hostapd_cli_cmd_wps_cancel, NULL,
  1221. "= cancel the pending WPS operation" },
  1222. #ifdef CONFIG_WPS_NFC
  1223. { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read, NULL,
  1224. "<hexdump> = report read NFC tag with WPS data" },
  1225. { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token, NULL,
  1226. "<WPS/NDEF> = build NFC configuration token" },
  1227. { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token, NULL,
  1228. "<WPS/NDEF/enable/disable> = manager NFC password token" },
  1229. { "nfc_get_handover_sel", hostapd_cli_cmd_nfc_get_handover_sel, NULL,
  1230. NULL },
  1231. #endif /* CONFIG_WPS_NFC */
  1232. { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin, NULL,
  1233. "<cmd> [params..] = enable/disable AP PIN" },
  1234. { "wps_config", hostapd_cli_cmd_wps_config, NULL,
  1235. "<SSID> <auth> <encr> <key> = configure AP" },
  1236. { "wps_get_status", hostapd_cli_cmd_wps_get_status, NULL,
  1237. "= show current WPS status" },
  1238. #endif /* CONFIG_WPS */
  1239. { "disassoc_imminent", hostapd_cli_cmd_disassoc_imminent, NULL,
  1240. "= send Disassociation Imminent notification" },
  1241. { "ess_disassoc", hostapd_cli_cmd_ess_disassoc, NULL,
  1242. "= send ESS Dissassociation Imminent notification" },
  1243. { "bss_tm_req", hostapd_cli_cmd_bss_tm_req, NULL,
  1244. "= send BSS Transition Management Request" },
  1245. { "get_config", hostapd_cli_cmd_get_config, NULL,
  1246. "= show current configuration" },
  1247. { "help", hostapd_cli_cmd_help, hostapd_cli_complete_help,
  1248. "= show this usage help" },
  1249. { "interface", hostapd_cli_cmd_interface, hostapd_complete_interface,
  1250. "[ifname] = show interfaces/select interface" },
  1251. #ifdef CONFIG_FST
  1252. { "fst", hostapd_cli_cmd_fst, NULL,
  1253. "<params...> = send FST-MANAGER control interface command" },
  1254. #endif /* CONFIG_FST */
  1255. { "raw", hostapd_cli_cmd_raw, NULL,
  1256. "<params..> = send unprocessed command" },
  1257. { "level", hostapd_cli_cmd_level, NULL,
  1258. "<debug level> = change debug level" },
  1259. { "license", hostapd_cli_cmd_license, NULL,
  1260. "= show full hostapd_cli license" },
  1261. { "quit", hostapd_cli_cmd_quit, NULL,
  1262. "= exit hostapd_cli" },
  1263. { "set", hostapd_cli_cmd_set, hostapd_complete_set,
  1264. "<name> <value> = set runtime variables" },
  1265. { "get", hostapd_cli_cmd_get, hostapd_complete_get,
  1266. "<name> = get runtime info" },
  1267. { "set_qos_map_set", hostapd_cli_cmd_set_qos_map_set, NULL,
  1268. "<arg,arg,...> = set QoS Map set element" },
  1269. { "send_qos_map_conf", hostapd_cli_cmd_send_qos_map_conf,
  1270. hostapd_complete_stations,
  1271. "<addr> = send QoS Map Configure frame" },
  1272. { "chan_switch", hostapd_cli_cmd_chan_switch, NULL,
  1273. "<cs_count> <freq> [sec_channel_offset=] [center_freq1=]\n"
  1274. " [center_freq2=] [bandwidth=] [blocktx] [ht|vht]\n"
  1275. " = initiate channel switch announcement" },
  1276. { "hs20_wnm_notif", hostapd_cli_cmd_hs20_wnm_notif, NULL,
  1277. "<addr> <url>\n"
  1278. " = send WNM-Notification Subscription Remediation Request" },
  1279. { "hs20_deauth_req", hostapd_cli_cmd_hs20_deauth_req, NULL,
  1280. "<addr> <code (0/1)> <Re-auth-Delay(sec)> [url]\n"
  1281. " = send WNM-Notification imminent deauthentication indication" },
  1282. { "vendor", hostapd_cli_cmd_vendor, NULL,
  1283. "<vendor id> <sub command id> [<hex formatted data>]\n"
  1284. " = send vendor driver command" },
  1285. { "enable", hostapd_cli_cmd_enable, NULL,
  1286. "= enable hostapd on current interface" },
  1287. { "reload", hostapd_cli_cmd_reload, NULL,
  1288. "= reload configuration for current interface" },
  1289. { "disable", hostapd_cli_cmd_disable, NULL,
  1290. "= disable hostapd on current interface" },
  1291. { "erp_flush", hostapd_cli_cmd_erp_flush, NULL,
  1292. "= drop all ERP keys"},
  1293. { "log_level", hostapd_cli_cmd_log_level, NULL,
  1294. "[level] = show/change log verbosity level" },
  1295. { "pmksa", hostapd_cli_cmd_pmksa, NULL,
  1296. " = show PMKSA cache entries" },
  1297. { "pmksa_flush", hostapd_cli_cmd_pmksa_flush, NULL,
  1298. " = flush PMKSA cache" },
  1299. { "set_neighbor", hostapd_cli_cmd_set_neighbor, NULL,
  1300. "<addr> <ssid=> <nr=> [lci=] [civic=] [stat]\n"
  1301. " = add AP to neighbor database" },
  1302. { "remove_neighbor", hostapd_cli_cmd_remove_neighbor, NULL,
  1303. "<addr> <ssid=> = remove AP from neighbor database" },
  1304. { "req_lci", hostapd_cli_cmd_req_lci, hostapd_complete_stations,
  1305. "<addr> = send LCI request to a station"},
  1306. { "req_range", hostapd_cli_cmd_req_range, NULL,
  1307. " = send FTM range request"},
  1308. { "driver_flags", hostapd_cli_cmd_driver_flags, NULL,
  1309. " = show supported driver flags"},
  1310. #ifdef CONFIG_DPP
  1311. { "dpp_qr_code", hostapd_cli_cmd_dpp_qr_code, NULL,
  1312. "report a scanned DPP URI from a QR Code" },
  1313. { "dpp_bootstrap_gen", hostapd_cli_cmd_dpp_bootstrap_gen, NULL,
  1314. "type=<qrcode> [chan=..] [mac=..] [info=..] [curve=..] [key=..] = generate DPP bootstrap information" },
  1315. { "dpp_bootstrap_remove", hostapd_cli_cmd_dpp_bootstrap_remove, NULL,
  1316. "*|<id> = remove DPP bootstrap information" },
  1317. { "dpp_bootstrap_get_uri", hostapd_cli_cmd_dpp_bootstrap_get_uri, NULL,
  1318. "<id> = get DPP bootstrap URI" },
  1319. { "dpp_bootstrap_info", hostapd_cli_cmd_dpp_bootstrap_info, NULL,
  1320. "<id> = show DPP bootstrap information" },
  1321. { "dpp_auth_init", hostapd_cli_cmd_dpp_auth_init, NULL,
  1322. "peer=<id> [own=<id>] = initiate DPP bootstrapping" },
  1323. { "dpp_listen", hostapd_cli_cmd_dpp_listen, NULL,
  1324. "<freq in MHz> = start DPP listen" },
  1325. { "dpp_stop_listen", hostapd_cli_cmd_dpp_stop_listen, NULL,
  1326. "= stop DPP listen" },
  1327. { "dpp_configurator_add", hostapd_cli_cmd_dpp_configurator_add, NULL,
  1328. "[curve=..] [key=..] = add DPP configurator" },
  1329. { "dpp_configurator_remove", hostapd_cli_cmd_dpp_configurator_remove,
  1330. NULL,
  1331. "*|<id> = remove DPP configurator" },
  1332. { "dpp_pkex_add", hostapd_cli_cmd_dpp_pkex_add, NULL,
  1333. "add PKEX code" },
  1334. { "dpp_pkex_remove", hostapd_cli_cmd_dpp_pkex_remove, NULL,
  1335. "*|<id> = remove DPP pkex information" },
  1336. #endif /* CONFIG_DPP */
  1337. { NULL, NULL, NULL, NULL }
  1338. };
  1339. /*
  1340. * Prints command usage, lines are padded with the specified string.
  1341. */
  1342. static void print_cmd_help(FILE *stream, const struct hostapd_cli_cmd *cmd,
  1343. const char *pad)
  1344. {
  1345. char c;
  1346. size_t n;
  1347. if (cmd->usage == NULL)
  1348. return;
  1349. fprintf(stream, "%s%s ", pad, cmd->cmd);
  1350. for (n = 0; (c = cmd->usage[n]); n++) {
  1351. fprintf(stream, "%c", c);
  1352. if (c == '\n')
  1353. fprintf(stream, "%s", pad);
  1354. }
  1355. fprintf(stream, "\n");
  1356. }
  1357. static void print_help(FILE *stream, const char *cmd)
  1358. {
  1359. int n;
  1360. fprintf(stream, "commands:\n");
  1361. for (n = 0; hostapd_cli_commands[n].cmd; n++) {
  1362. if (cmd == NULL || str_starts(hostapd_cli_commands[n].cmd, cmd))
  1363. print_cmd_help(stream, &hostapd_cli_commands[n], " ");
  1364. }
  1365. }
  1366. static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1367. {
  1368. const struct hostapd_cli_cmd *cmd, *match = NULL;
  1369. int count;
  1370. count = 0;
  1371. cmd = hostapd_cli_commands;
  1372. while (cmd->cmd) {
  1373. if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) {
  1374. match = cmd;
  1375. if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
  1376. /* we have an exact match */
  1377. count = 1;
  1378. break;
  1379. }
  1380. count++;
  1381. }
  1382. cmd++;
  1383. }
  1384. if (count > 1) {
  1385. printf("Ambiguous command '%s'; possible commands:", argv[0]);
  1386. cmd = hostapd_cli_commands;
  1387. while (cmd->cmd) {
  1388. if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) ==
  1389. 0) {
  1390. printf(" %s", cmd->cmd);
  1391. }
  1392. cmd++;
  1393. }
  1394. printf("\n");
  1395. } else if (count == 0) {
  1396. printf("Unknown command '%s'\n", argv[0]);
  1397. } else {
  1398. match->handler(ctrl, argc - 1, &argv[1]);
  1399. }
  1400. }
  1401. static void cli_event(const char *str)
  1402. {
  1403. const char *start, *s;
  1404. start = os_strchr(str, '>');
  1405. if (start == NULL)
  1406. return;
  1407. start++;
  1408. if (str_starts(start, AP_STA_CONNECTED)) {
  1409. s = os_strchr(start, ' ');
  1410. if (s == NULL)
  1411. return;
  1412. cli_txt_list_add(&stations, s + 1);
  1413. return;
  1414. }
  1415. if (str_starts(start, AP_STA_DISCONNECTED)) {
  1416. s = os_strchr(start, ' ');
  1417. if (s == NULL)
  1418. return;
  1419. cli_txt_list_del_addr(&stations, s + 1);
  1420. return;
  1421. }
  1422. }
  1423. static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
  1424. int action_monitor)
  1425. {
  1426. int first = 1;
  1427. if (ctrl_conn == NULL)
  1428. return;
  1429. while (wpa_ctrl_pending(ctrl)) {
  1430. char buf[4096];
  1431. size_t len = sizeof(buf) - 1;
  1432. if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
  1433. buf[len] = '\0';
  1434. if (action_monitor)
  1435. hostapd_cli_action_process(buf, len);
  1436. else {
  1437. cli_event(buf);
  1438. if (in_read && first)
  1439. printf("\n");
  1440. first = 0;
  1441. printf("%s\n", buf);
  1442. }
  1443. } else {
  1444. printf("Could not read pending message.\n");
  1445. break;
  1446. }
  1447. }
  1448. }
  1449. static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx)
  1450. {
  1451. hostapd_cli_recv_pending(ctrl_conn, 0, 0);
  1452. }
  1453. static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx)
  1454. {
  1455. if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
  1456. printf("Connection to hostapd lost - trying to reconnect\n");
  1457. hostapd_cli_close_connection();
  1458. }
  1459. if (!ctrl_conn && hostapd_cli_reconnect(ctrl_ifname) == 0)
  1460. printf("Connection to hostapd re-established\n");
  1461. if (ctrl_conn)
  1462. hostapd_cli_recv_pending(ctrl_conn, 1, 0);
  1463. eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
  1464. }
  1465. static void hostapd_cli_eloop_terminate(int sig, void *signal_ctx)
  1466. {
  1467. eloop_terminate();
  1468. }
  1469. static void hostapd_cli_edit_cmd_cb(void *ctx, char *cmd)
  1470. {
  1471. char *argv[max_args];
  1472. int argc;
  1473. argc = tokenize_cmd(cmd, argv);
  1474. if (argc)
  1475. wpa_request(ctrl_conn, argc, argv);
  1476. }
  1477. static void hostapd_cli_edit_eof_cb(void *ctx)
  1478. {
  1479. eloop_terminate();
  1480. }
  1481. static char ** list_cmd_list(void)
  1482. {
  1483. char **res;
  1484. int i, count;
  1485. count = ARRAY_SIZE(hostapd_cli_commands);
  1486. res = os_calloc(count + 1, sizeof(char *));
  1487. if (res == NULL)
  1488. return NULL;
  1489. for (i = 0; hostapd_cli_commands[i].cmd; i++) {
  1490. res[i] = os_strdup(hostapd_cli_commands[i].cmd);
  1491. if (res[i] == NULL)
  1492. break;
  1493. }
  1494. return res;
  1495. }
  1496. static char ** hostapd_cli_cmd_completion(const char *cmd, const char *str,
  1497. int pos)
  1498. {
  1499. int i;
  1500. for (i = 0; hostapd_cli_commands[i].cmd; i++) {
  1501. if (os_strcasecmp(hostapd_cli_commands[i].cmd, cmd) != 0)
  1502. continue;
  1503. if (hostapd_cli_commands[i].completion)
  1504. return hostapd_cli_commands[i].completion(str, pos);
  1505. if (!hostapd_cli_commands[i].usage)
  1506. return NULL;
  1507. edit_clear_line();
  1508. printf("\r%s\n", hostapd_cli_commands[i].usage);
  1509. edit_redraw();
  1510. break;
  1511. }
  1512. return NULL;
  1513. }
  1514. static char ** hostapd_cli_edit_completion_cb(void *ctx, const char *str,
  1515. int pos)
  1516. {
  1517. char **res;
  1518. const char *end;
  1519. char *cmd;
  1520. end = os_strchr(str, ' ');
  1521. if (end == NULL || str + pos < end)
  1522. return list_cmd_list();
  1523. cmd = os_malloc(pos + 1);
  1524. if (cmd == NULL)
  1525. return NULL;
  1526. os_memcpy(cmd, str, pos);
  1527. cmd[end - str] = '\0';
  1528. res = hostapd_cli_cmd_completion(cmd, str, pos);
  1529. os_free(cmd);
  1530. return res;
  1531. }
  1532. static void hostapd_cli_interactive(void)
  1533. {
  1534. char *hfile = NULL;
  1535. char *home;
  1536. printf("\nInteractive mode\n\n");
  1537. #ifdef CONFIG_HOSTAPD_CLI_HISTORY_DIR
  1538. home = CONFIG_HOSTAPD_CLI_HISTORY_DIR;
  1539. #else /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
  1540. home = getenv("HOME");
  1541. #endif /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
  1542. if (home) {
  1543. const char *fname = ".hostapd_cli_history";
  1544. int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
  1545. hfile = os_malloc(hfile_len);
  1546. if (hfile)
  1547. os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
  1548. }
  1549. eloop_register_signal_terminate(hostapd_cli_eloop_terminate, NULL);
  1550. edit_init(hostapd_cli_edit_cmd_cb, hostapd_cli_edit_eof_cb,
  1551. hostapd_cli_edit_completion_cb, NULL, hfile, NULL);
  1552. eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
  1553. eloop_run();
  1554. cli_txt_list_flush(&stations);
  1555. edit_deinit(hfile, NULL);
  1556. os_free(hfile);
  1557. eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL);
  1558. }
  1559. static void hostapd_cli_cleanup(void)
  1560. {
  1561. hostapd_cli_close_connection();
  1562. if (pid_file)
  1563. os_daemonize_terminate(pid_file);
  1564. os_program_deinit();
  1565. }
  1566. static void hostapd_cli_action(struct wpa_ctrl *ctrl)
  1567. {
  1568. fd_set rfds;
  1569. int fd, res;
  1570. struct timeval tv;
  1571. char buf[256];
  1572. size_t len;
  1573. fd = wpa_ctrl_get_fd(ctrl);
  1574. while (!hostapd_cli_quit) {
  1575. FD_ZERO(&rfds);
  1576. FD_SET(fd, &rfds);
  1577. tv.tv_sec = ping_interval;
  1578. tv.tv_usec = 0;
  1579. res = select(fd + 1, &rfds, NULL, NULL, &tv);
  1580. if (res < 0 && errno != EINTR) {
  1581. perror("select");
  1582. break;
  1583. }
  1584. if (FD_ISSET(fd, &rfds))
  1585. hostapd_cli_recv_pending(ctrl, 0, 1);
  1586. else {
  1587. len = sizeof(buf) - 1;
  1588. if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
  1589. hostapd_cli_action_process) < 0 ||
  1590. len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
  1591. printf("hostapd did not reply to PING "
  1592. "command - exiting\n");
  1593. break;
  1594. }
  1595. }
  1596. }
  1597. }
  1598. int main(int argc, char *argv[])
  1599. {
  1600. int warning_displayed = 0;
  1601. int c;
  1602. int daemonize = 0;
  1603. if (os_program_init())
  1604. return -1;
  1605. for (;;) {
  1606. c = getopt(argc, argv, "a:BhG:i:p:P:s:v");
  1607. if (c < 0)
  1608. break;
  1609. switch (c) {
  1610. case 'a':
  1611. action_file = optarg;
  1612. break;
  1613. case 'B':
  1614. daemonize = 1;
  1615. break;
  1616. case 'G':
  1617. ping_interval = atoi(optarg);
  1618. break;
  1619. case 'h':
  1620. usage();
  1621. return 0;
  1622. case 'v':
  1623. printf("%s\n", hostapd_cli_version);
  1624. return 0;
  1625. case 'i':
  1626. os_free(ctrl_ifname);
  1627. ctrl_ifname = os_strdup(optarg);
  1628. break;
  1629. case 'p':
  1630. ctrl_iface_dir = optarg;
  1631. break;
  1632. case 'P':
  1633. pid_file = optarg;
  1634. break;
  1635. case 's':
  1636. client_socket_dir = optarg;
  1637. break;
  1638. default:
  1639. usage();
  1640. return -1;
  1641. }
  1642. }
  1643. interactive = (argc == optind) && (action_file == NULL);
  1644. if (interactive) {
  1645. printf("%s\n\n%s\n\n", hostapd_cli_version, cli_license);
  1646. }
  1647. if (eloop_init())
  1648. return -1;
  1649. for (;;) {
  1650. if (ctrl_ifname == NULL) {
  1651. struct dirent *dent;
  1652. DIR *dir = opendir(ctrl_iface_dir);
  1653. if (dir) {
  1654. while ((dent = readdir(dir))) {
  1655. if (os_strcmp(dent->d_name, ".") == 0
  1656. ||
  1657. os_strcmp(dent->d_name, "..") == 0)
  1658. continue;
  1659. printf("Selected interface '%s'\n",
  1660. dent->d_name);
  1661. ctrl_ifname = os_strdup(dent->d_name);
  1662. break;
  1663. }
  1664. closedir(dir);
  1665. }
  1666. }
  1667. hostapd_cli_reconnect(ctrl_ifname);
  1668. if (ctrl_conn) {
  1669. if (warning_displayed)
  1670. printf("Connection established.\n");
  1671. break;
  1672. }
  1673. if (!interactive) {
  1674. perror("Failed to connect to hostapd - "
  1675. "wpa_ctrl_open");
  1676. return -1;
  1677. }
  1678. if (!warning_displayed) {
  1679. printf("Could not connect to hostapd - re-trying\n");
  1680. warning_displayed = 1;
  1681. }
  1682. os_sleep(1, 0);
  1683. continue;
  1684. }
  1685. if (action_file && !hostapd_cli_attached)
  1686. return -1;
  1687. if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
  1688. return -1;
  1689. if (interactive)
  1690. hostapd_cli_interactive();
  1691. else if (action_file)
  1692. hostapd_cli_action(ctrl_conn);
  1693. else
  1694. wpa_request(ctrl_conn, argc - optind, &argv[optind]);
  1695. unregister_event_handler(ctrl_conn);
  1696. os_free(ctrl_ifname);
  1697. eloop_destroy();
  1698. hostapd_cli_cleanup();
  1699. return 0;
  1700. }
  1701. #else /* CONFIG_NO_CTRL_IFACE */
  1702. int main(int argc, char *argv[])
  1703. {
  1704. return -1;
  1705. }
  1706. #endif /* CONFIG_NO_CTRL_IFACE */