hostapd_cli.c 41 KB

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