hostapd_cli.c 42 KB

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