hostapd_cli.c 41 KB

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