hostapd_cli.c 41 KB

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