hostapd_cli.c 43 KB

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