hostapd_cli.c 41 KB

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