hostapd_cli.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943
  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[256];
  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. struct hostapd_cli_cmd {
  1115. const char *cmd;
  1116. int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
  1117. char ** (*completion)(const char *str, int pos);
  1118. const char *usage;
  1119. };
  1120. static const struct hostapd_cli_cmd hostapd_cli_commands[] = {
  1121. { "ping", hostapd_cli_cmd_ping, NULL,
  1122. "= pings hostapd" },
  1123. { "mib", hostapd_cli_cmd_mib, NULL,
  1124. "= get MIB variables (dot1x, dot11, radius)" },
  1125. { "relog", hostapd_cli_cmd_relog, NULL,
  1126. "= reload/truncate debug log output file" },
  1127. { "status", hostapd_cli_cmd_status, NULL,
  1128. "= show interface status info" },
  1129. { "sta", hostapd_cli_cmd_sta, hostapd_complete_stations,
  1130. "<addr> = get MIB variables for one station" },
  1131. { "all_sta", hostapd_cli_cmd_all_sta, NULL,
  1132. "= get MIB variables for all stations" },
  1133. { "list_sta", hostapd_cli_cmd_list_sta, NULL,
  1134. "= list all stations" },
  1135. { "new_sta", hostapd_cli_cmd_new_sta, NULL,
  1136. "<addr> = add a new station" },
  1137. { "deauthenticate", hostapd_cli_cmd_deauthenticate,
  1138. hostapd_complete_stations,
  1139. "<addr> = deauthenticate a station" },
  1140. { "disassociate", hostapd_cli_cmd_disassociate,
  1141. hostapd_complete_stations,
  1142. "<addr> = disassociate a station" },
  1143. #ifdef CONFIG_TAXONOMY
  1144. { "signature", hostapd_cli_cmd_signature, hostapd_complete_stations,
  1145. "<addr> = get taxonomy signature for a station" },
  1146. #endif /* CONFIG_TAXONOMY */
  1147. #ifdef CONFIG_IEEE80211W
  1148. { "sa_query", hostapd_cli_cmd_sa_query, hostapd_complete_stations,
  1149. "<addr> = send SA Query to a station" },
  1150. #endif /* CONFIG_IEEE80211W */
  1151. #ifdef CONFIG_WPS
  1152. { "wps_pin", hostapd_cli_cmd_wps_pin, NULL,
  1153. "<uuid> <pin> [timeout] [addr] = add WPS Enrollee PIN" },
  1154. { "wps_check_pin", hostapd_cli_cmd_wps_check_pin, NULL,
  1155. "<PIN> = verify PIN checksum" },
  1156. { "wps_pbc", hostapd_cli_cmd_wps_pbc, NULL,
  1157. "= indicate button pushed to initiate PBC" },
  1158. { "wps_cancel", hostapd_cli_cmd_wps_cancel, NULL,
  1159. "= cancel the pending WPS operation" },
  1160. #ifdef CONFIG_WPS_NFC
  1161. { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read, NULL,
  1162. "<hexdump> = report read NFC tag with WPS data" },
  1163. { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token, NULL,
  1164. "<WPS/NDEF> = build NFC configuration token" },
  1165. { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token, NULL,
  1166. "<WPS/NDEF/enable/disable> = manager NFC password token" },
  1167. { "nfc_get_handover_sel", hostapd_cli_cmd_nfc_get_handover_sel, NULL,
  1168. NULL },
  1169. #endif /* CONFIG_WPS_NFC */
  1170. { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin, NULL,
  1171. "<cmd> [params..] = enable/disable AP PIN" },
  1172. { "wps_config", hostapd_cli_cmd_wps_config, NULL,
  1173. "<SSID> <auth> <encr> <key> = configure AP" },
  1174. { "wps_get_status", hostapd_cli_cmd_wps_get_status, NULL,
  1175. "= show current WPS status" },
  1176. #endif /* CONFIG_WPS */
  1177. { "disassoc_imminent", hostapd_cli_cmd_disassoc_imminent, NULL,
  1178. "= send Disassociation Imminent notification" },
  1179. { "ess_disassoc", hostapd_cli_cmd_ess_disassoc, NULL,
  1180. "= send ESS Dissassociation Imminent notification" },
  1181. { "bss_tm_req", hostapd_cli_cmd_bss_tm_req, NULL,
  1182. "= send BSS Transition Management Request" },
  1183. { "get_config", hostapd_cli_cmd_get_config, NULL,
  1184. "= show current configuration" },
  1185. { "help", hostapd_cli_cmd_help, hostapd_cli_complete_help,
  1186. "= show this usage help" },
  1187. { "interface", hostapd_cli_cmd_interface, hostapd_complete_interface,
  1188. "[ifname] = show interfaces/select interface" },
  1189. #ifdef CONFIG_FST
  1190. { "fst", hostapd_cli_cmd_fst, NULL,
  1191. "<params...> = send FST-MANAGER control interface command" },
  1192. #endif /* CONFIG_FST */
  1193. { "raw", hostapd_cli_cmd_raw, NULL,
  1194. "<params..> = send unprocessed command" },
  1195. { "level", hostapd_cli_cmd_level, NULL,
  1196. "<debug level> = change debug level" },
  1197. { "license", hostapd_cli_cmd_license, NULL,
  1198. "= show full hostapd_cli license" },
  1199. { "quit", hostapd_cli_cmd_quit, NULL,
  1200. "= exit hostapd_cli" },
  1201. { "set", hostapd_cli_cmd_set, hostapd_complete_set,
  1202. "<name> <value> = set runtime variables" },
  1203. { "get", hostapd_cli_cmd_get, hostapd_complete_get,
  1204. "<name> = get runtime info" },
  1205. { "set_qos_map_set", hostapd_cli_cmd_set_qos_map_set, NULL,
  1206. "<arg,arg,...> = set QoS Map set element" },
  1207. { "send_qos_map_conf", hostapd_cli_cmd_send_qos_map_conf,
  1208. hostapd_complete_stations,
  1209. "<addr> = send QoS Map Configure frame" },
  1210. { "chan_switch", hostapd_cli_cmd_chan_switch, NULL,
  1211. "<cs_count> <freq> [sec_channel_offset=] [center_freq1=]\n"
  1212. " [center_freq2=] [bandwidth=] [blocktx] [ht|vht]\n"
  1213. " = initiate channel switch announcement" },
  1214. { "hs20_wnm_notif", hostapd_cli_cmd_hs20_wnm_notif, NULL,
  1215. "<addr> <url>\n"
  1216. " = send WNM-Notification Subscription Remediation Request" },
  1217. { "hs20_deauth_req", hostapd_cli_cmd_hs20_deauth_req, NULL,
  1218. "<addr> <code (0/1)> <Re-auth-Delay(sec)> [url]\n"
  1219. " = send WNM-Notification imminent deauthentication indication" },
  1220. { "vendor", hostapd_cli_cmd_vendor, NULL,
  1221. "<vendor id> <sub command id> [<hex formatted data>]\n"
  1222. " = send vendor driver command" },
  1223. { "enable", hostapd_cli_cmd_enable, NULL,
  1224. "= enable hostapd on current interface" },
  1225. { "reload", hostapd_cli_cmd_reload, NULL,
  1226. "= reload configuration for current interface" },
  1227. { "disable", hostapd_cli_cmd_disable, NULL,
  1228. "= disable hostapd on current interface" },
  1229. { "erp_flush", hostapd_cli_cmd_erp_flush, NULL,
  1230. "= drop all ERP keys"},
  1231. { "log_level", hostapd_cli_cmd_log_level, NULL,
  1232. "[level] = show/change log verbosity level" },
  1233. { "pmksa", hostapd_cli_cmd_pmksa, NULL,
  1234. " = show PMKSA cache entries" },
  1235. { "pmksa_flush", hostapd_cli_cmd_pmksa_flush, NULL,
  1236. " = flush PMKSA cache" },
  1237. { "set_neighbor", hostapd_cli_cmd_set_neighbor, NULL,
  1238. "<addr> <ssid=> <nr=> [lci=] [civic=] [stat]\n"
  1239. " = add AP to neighbor database" },
  1240. { "remove_neighbor", hostapd_cli_cmd_remove_neighbor, NULL,
  1241. "<addr> <ssid=> = remove AP from neighbor database" },
  1242. { "req_lci", hostapd_cli_cmd_req_lci, hostapd_complete_stations,
  1243. "<addr> = send LCI request to a station"},
  1244. { "req_range", hostapd_cli_cmd_req_range, NULL,
  1245. " = send FTM range request"},
  1246. { "driver_flags", hostapd_cli_cmd_driver_flags, NULL,
  1247. " = show supported driver flags"},
  1248. { NULL, NULL, NULL, NULL }
  1249. };
  1250. /*
  1251. * Prints command usage, lines are padded with the specified string.
  1252. */
  1253. static void print_cmd_help(FILE *stream, const struct hostapd_cli_cmd *cmd,
  1254. const char *pad)
  1255. {
  1256. char c;
  1257. size_t n;
  1258. if (cmd->usage == NULL)
  1259. return;
  1260. fprintf(stream, "%s%s ", pad, cmd->cmd);
  1261. for (n = 0; (c = cmd->usage[n]); n++) {
  1262. fprintf(stream, "%c", c);
  1263. if (c == '\n')
  1264. fprintf(stream, "%s", pad);
  1265. }
  1266. fprintf(stream, "\n");
  1267. }
  1268. static void print_help(FILE *stream, const char *cmd)
  1269. {
  1270. int n;
  1271. fprintf(stream, "commands:\n");
  1272. for (n = 0; hostapd_cli_commands[n].cmd; n++) {
  1273. if (cmd == NULL || str_starts(hostapd_cli_commands[n].cmd, cmd))
  1274. print_cmd_help(stream, &hostapd_cli_commands[n], " ");
  1275. }
  1276. }
  1277. static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1278. {
  1279. const struct hostapd_cli_cmd *cmd, *match = NULL;
  1280. int count;
  1281. count = 0;
  1282. cmd = hostapd_cli_commands;
  1283. while (cmd->cmd) {
  1284. if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) {
  1285. match = cmd;
  1286. if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
  1287. /* we have an exact match */
  1288. count = 1;
  1289. break;
  1290. }
  1291. count++;
  1292. }
  1293. cmd++;
  1294. }
  1295. if (count > 1) {
  1296. printf("Ambiguous command '%s'; possible commands:", argv[0]);
  1297. cmd = hostapd_cli_commands;
  1298. while (cmd->cmd) {
  1299. if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) ==
  1300. 0) {
  1301. printf(" %s", cmd->cmd);
  1302. }
  1303. cmd++;
  1304. }
  1305. printf("\n");
  1306. } else if (count == 0) {
  1307. printf("Unknown command '%s'\n", argv[0]);
  1308. } else {
  1309. match->handler(ctrl, argc - 1, &argv[1]);
  1310. }
  1311. }
  1312. static void cli_event(const char *str)
  1313. {
  1314. const char *start, *s;
  1315. start = os_strchr(str, '>');
  1316. if (start == NULL)
  1317. return;
  1318. start++;
  1319. if (str_starts(start, AP_STA_CONNECTED)) {
  1320. s = os_strchr(start, ' ');
  1321. if (s == NULL)
  1322. return;
  1323. cli_txt_list_add(&stations, s + 1);
  1324. return;
  1325. }
  1326. if (str_starts(start, AP_STA_DISCONNECTED)) {
  1327. s = os_strchr(start, ' ');
  1328. if (s == NULL)
  1329. return;
  1330. cli_txt_list_del_addr(&stations, s + 1);
  1331. return;
  1332. }
  1333. }
  1334. static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
  1335. int action_monitor)
  1336. {
  1337. int first = 1;
  1338. if (ctrl_conn == NULL)
  1339. return;
  1340. while (wpa_ctrl_pending(ctrl)) {
  1341. char buf[256];
  1342. size_t len = sizeof(buf) - 1;
  1343. if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
  1344. buf[len] = '\0';
  1345. if (action_monitor)
  1346. hostapd_cli_action_process(buf, len);
  1347. else {
  1348. cli_event(buf);
  1349. if (in_read && first)
  1350. printf("\n");
  1351. first = 0;
  1352. printf("%s\n", buf);
  1353. }
  1354. } else {
  1355. printf("Could not read pending message.\n");
  1356. break;
  1357. }
  1358. }
  1359. }
  1360. static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx)
  1361. {
  1362. hostapd_cli_recv_pending(ctrl_conn, 0, 0);
  1363. }
  1364. static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx)
  1365. {
  1366. if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
  1367. printf("Connection to hostapd lost - trying to reconnect\n");
  1368. hostapd_cli_close_connection();
  1369. }
  1370. if (!ctrl_conn && hostapd_cli_reconnect(ctrl_ifname) == 0)
  1371. printf("Connection to hostapd re-established\n");
  1372. if (ctrl_conn)
  1373. hostapd_cli_recv_pending(ctrl_conn, 1, 0);
  1374. eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
  1375. }
  1376. static void hostapd_cli_eloop_terminate(int sig, void *signal_ctx)
  1377. {
  1378. eloop_terminate();
  1379. }
  1380. static void hostapd_cli_edit_cmd_cb(void *ctx, char *cmd)
  1381. {
  1382. char *argv[max_args];
  1383. int argc;
  1384. argc = tokenize_cmd(cmd, argv);
  1385. if (argc)
  1386. wpa_request(ctrl_conn, argc, argv);
  1387. }
  1388. static void hostapd_cli_edit_eof_cb(void *ctx)
  1389. {
  1390. eloop_terminate();
  1391. }
  1392. static char ** list_cmd_list(void)
  1393. {
  1394. char **res;
  1395. int i, count;
  1396. count = ARRAY_SIZE(hostapd_cli_commands);
  1397. res = os_calloc(count + 1, sizeof(char *));
  1398. if (res == NULL)
  1399. return NULL;
  1400. for (i = 0; hostapd_cli_commands[i].cmd; i++) {
  1401. res[i] = os_strdup(hostapd_cli_commands[i].cmd);
  1402. if (res[i] == NULL)
  1403. break;
  1404. }
  1405. return res;
  1406. }
  1407. static char ** hostapd_cli_cmd_completion(const char *cmd, const char *str,
  1408. int pos)
  1409. {
  1410. int i;
  1411. for (i = 0; hostapd_cli_commands[i].cmd; i++) {
  1412. if (os_strcasecmp(hostapd_cli_commands[i].cmd, cmd) != 0)
  1413. continue;
  1414. if (hostapd_cli_commands[i].completion)
  1415. return hostapd_cli_commands[i].completion(str, pos);
  1416. if (!hostapd_cli_commands[i].usage)
  1417. return NULL;
  1418. edit_clear_line();
  1419. printf("\r%s\n", hostapd_cli_commands[i].usage);
  1420. edit_redraw();
  1421. break;
  1422. }
  1423. return NULL;
  1424. }
  1425. static char ** hostapd_cli_edit_completion_cb(void *ctx, const char *str,
  1426. int pos)
  1427. {
  1428. char **res;
  1429. const char *end;
  1430. char *cmd;
  1431. end = os_strchr(str, ' ');
  1432. if (end == NULL || str + pos < end)
  1433. return list_cmd_list();
  1434. cmd = os_malloc(pos + 1);
  1435. if (cmd == NULL)
  1436. return NULL;
  1437. os_memcpy(cmd, str, pos);
  1438. cmd[end - str] = '\0';
  1439. res = hostapd_cli_cmd_completion(cmd, str, pos);
  1440. os_free(cmd);
  1441. return res;
  1442. }
  1443. static void hostapd_cli_interactive(void)
  1444. {
  1445. char *hfile = NULL;
  1446. char *home;
  1447. printf("\nInteractive mode\n\n");
  1448. #ifdef CONFIG_HOSTAPD_CLI_HISTORY_DIR
  1449. home = CONFIG_HOSTAPD_CLI_HISTORY_DIR;
  1450. #else /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
  1451. home = getenv("HOME");
  1452. #endif /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
  1453. if (home) {
  1454. const char *fname = ".hostapd_cli_history";
  1455. int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
  1456. hfile = os_malloc(hfile_len);
  1457. if (hfile)
  1458. os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
  1459. }
  1460. eloop_register_signal_terminate(hostapd_cli_eloop_terminate, NULL);
  1461. edit_init(hostapd_cli_edit_cmd_cb, hostapd_cli_edit_eof_cb,
  1462. hostapd_cli_edit_completion_cb, NULL, hfile, NULL);
  1463. eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
  1464. eloop_run();
  1465. cli_txt_list_flush(&stations);
  1466. edit_deinit(hfile, NULL);
  1467. os_free(hfile);
  1468. eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL);
  1469. }
  1470. static void hostapd_cli_cleanup(void)
  1471. {
  1472. hostapd_cli_close_connection();
  1473. if (pid_file)
  1474. os_daemonize_terminate(pid_file);
  1475. os_program_deinit();
  1476. }
  1477. static void hostapd_cli_action(struct wpa_ctrl *ctrl)
  1478. {
  1479. fd_set rfds;
  1480. int fd, res;
  1481. struct timeval tv;
  1482. char buf[256];
  1483. size_t len;
  1484. fd = wpa_ctrl_get_fd(ctrl);
  1485. while (!hostapd_cli_quit) {
  1486. FD_ZERO(&rfds);
  1487. FD_SET(fd, &rfds);
  1488. tv.tv_sec = ping_interval;
  1489. tv.tv_usec = 0;
  1490. res = select(fd + 1, &rfds, NULL, NULL, &tv);
  1491. if (res < 0 && errno != EINTR) {
  1492. perror("select");
  1493. break;
  1494. }
  1495. if (FD_ISSET(fd, &rfds))
  1496. hostapd_cli_recv_pending(ctrl, 0, 1);
  1497. else {
  1498. len = sizeof(buf) - 1;
  1499. if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
  1500. hostapd_cli_action_process) < 0 ||
  1501. len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
  1502. printf("hostapd did not reply to PING "
  1503. "command - exiting\n");
  1504. break;
  1505. }
  1506. }
  1507. }
  1508. }
  1509. int main(int argc, char *argv[])
  1510. {
  1511. int warning_displayed = 0;
  1512. int c;
  1513. int daemonize = 0;
  1514. if (os_program_init())
  1515. return -1;
  1516. for (;;) {
  1517. c = getopt(argc, argv, "a:BhG:i:p:P:s:v");
  1518. if (c < 0)
  1519. break;
  1520. switch (c) {
  1521. case 'a':
  1522. action_file = optarg;
  1523. break;
  1524. case 'B':
  1525. daemonize = 1;
  1526. break;
  1527. case 'G':
  1528. ping_interval = atoi(optarg);
  1529. break;
  1530. case 'h':
  1531. usage();
  1532. return 0;
  1533. case 'v':
  1534. printf("%s\n", hostapd_cli_version);
  1535. return 0;
  1536. case 'i':
  1537. os_free(ctrl_ifname);
  1538. ctrl_ifname = os_strdup(optarg);
  1539. break;
  1540. case 'p':
  1541. ctrl_iface_dir = optarg;
  1542. break;
  1543. case 'P':
  1544. pid_file = optarg;
  1545. break;
  1546. case 's':
  1547. client_socket_dir = optarg;
  1548. break;
  1549. default:
  1550. usage();
  1551. return -1;
  1552. }
  1553. }
  1554. interactive = (argc == optind) && (action_file == NULL);
  1555. if (interactive) {
  1556. printf("%s\n\n%s\n\n", hostapd_cli_version, cli_license);
  1557. }
  1558. if (eloop_init())
  1559. return -1;
  1560. for (;;) {
  1561. if (ctrl_ifname == NULL) {
  1562. struct dirent *dent;
  1563. DIR *dir = opendir(ctrl_iface_dir);
  1564. if (dir) {
  1565. while ((dent = readdir(dir))) {
  1566. if (os_strcmp(dent->d_name, ".") == 0
  1567. ||
  1568. os_strcmp(dent->d_name, "..") == 0)
  1569. continue;
  1570. printf("Selected interface '%s'\n",
  1571. dent->d_name);
  1572. ctrl_ifname = os_strdup(dent->d_name);
  1573. break;
  1574. }
  1575. closedir(dir);
  1576. }
  1577. }
  1578. hostapd_cli_reconnect(ctrl_ifname);
  1579. if (ctrl_conn) {
  1580. if (warning_displayed)
  1581. printf("Connection established.\n");
  1582. break;
  1583. }
  1584. if (!interactive) {
  1585. perror("Failed to connect to hostapd - "
  1586. "wpa_ctrl_open");
  1587. return -1;
  1588. }
  1589. if (!warning_displayed) {
  1590. printf("Could not connect to hostapd - re-trying\n");
  1591. warning_displayed = 1;
  1592. }
  1593. os_sleep(1, 0);
  1594. continue;
  1595. }
  1596. if (action_file && !hostapd_cli_attached)
  1597. return -1;
  1598. if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
  1599. return -1;
  1600. if (interactive)
  1601. hostapd_cli_interactive();
  1602. else if (action_file)
  1603. hostapd_cli_action(ctrl_conn);
  1604. else
  1605. wpa_request(ctrl_conn, argc - optind, &argv[optind]);
  1606. unregister_event_handler(ctrl_conn);
  1607. os_free(ctrl_ifname);
  1608. eloop_destroy();
  1609. hostapd_cli_cleanup();
  1610. return 0;
  1611. }
  1612. #else /* CONFIG_NO_CTRL_IFACE */
  1613. int main(int argc, char *argv[])
  1614. {
  1615. return -1;
  1616. }
  1617. #endif /* CONFIG_NO_CTRL_IFACE */